// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

const encoder = new TextEncoder();

function getTypeName(value: unknown): string {
  const type = typeof value;
  if (type !== "object") {
    return type;
  } else if (value === null) {
    return "null";
  } else {
    return value?.constructor?.name ?? "object";
  }
}

export function validateBinaryLike(source: unknown): Uint8Array {
  if (typeof source === "string") {
    return encoder.encode(source);
  } else if (source instanceof Uint8Array) {
    return source;
  } else if (source instanceof ArrayBuffer) {
    return new Uint8Array(source);
  }
  throw new TypeError(
    `The input must be a Uint8Array, a string, or an ArrayBuffer. Received a value of the type ${
      getTypeName(source)
    }.`,
  );
}

// denoCacheMetadata={"headers":{"cf-cache-status":"HIT","accept-ranges":"bytes","x-jsr-backend":"modules","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=qtABz8EiGoE3uPfZhWQBbonl0YBMVrvK%2FE41cfUhtoHZpP8Vth0AUDQsX%2B1CjABgIyYKdQqiuL%2FYVckzTBo8gK6i9LhTRvJtunbC4hD7%2FqRdL4d4%2BuVkcbQ%3D\"}]}","cross-origin-resource-policy":"cross-origin","content-type":"text/typescript","content-length":"798","cache-control":"public, max-age=31536000, immutable","access-control-allow-origin":"*","vary":"Origin","access-control-expose-headers":"*","set-cookie":"__cf_bm=FF4kTVFJb5x9XRTb6_25pddwQqA6g7j_vIGsZmpHtmo-1785241875.338559-1.0.1.1-u_8onGgjTz4nJ66HvAEl6jp4IltbIeyhK8ypumB6PTHkJGht8y2fqIda3N4p.nmx09.RKAkCV3LYJpNnr8caa0bai.dJCqQMBGy7bBkjbSeX2ytLlrJwnypcAksJC2Ku; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","date":"Tue, 28 Jul 2026 12:31:15 GMT","etag":"\"d13b3b0dcf6d4f86b4aed186221caac0\"","server":"cloudflare","x-content-type-options":"nosniff","alt-svc":"h3=\":443\"; ma=86400","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","x-robots-tag":"noindex","age":"123928","cf-ray":"a223e658cda37db6-LAX"},"url":"https://jsr.io/@std/encoding/1.0.0-rc.2/_validate_binary_like.ts","time":1785241875}