// 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":{"access-control-expose-headers":"*","cf-cache-status":"HIT","accept-ranges":"bytes","cache-control":"public, max-age=31536000, immutable","content-length":"798","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","set-cookie":"__cf_bm=U8SK1MpQ2XF1PRGOe10p3QaYPd_VcR8ZIgbPuKXqkl0-1785241875.3647745-1.0.1.1-cbWyNhz1N5WPPTV8igxmv3mWjVFRHlB32bvRwl8dyCc1QaQlqJ8nYTnDQ0h77kMyZJvw9OlwDwKf.hrI8nat63VEsIpUeb80Ws8eDRet0kIUvNh9WP_O5XoLV.uUYjuJ; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","cross-origin-resource-policy":"cross-origin","access-control-allow-origin":"*","x-content-type-options":"nosniff","age":"36251","server":"cloudflare","cf-ray":"a223e658fe207db6-LAX","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=AnQSG2y22VvkqX3gH26ro%2BkBCjhqP19wdI2CwDKPwoKtaDe2NhC4UtlAwZd5qf2XIon%2FoLXqdq2KdmacdMSVt6nPU6zsMCBUfofRZL3CCtUeXwgJ4noh1wA%3D\"}]}","content-type":"text/typescript","date":"Tue, 28 Jul 2026 12:31:15 GMT","etag":"\"d13b3b0dcf6d4f86b4aed186221caac0\"","x-jsr-backend":"modules","vary":"Origin"},"url":"https://jsr.io/@std/encoding/0.224.3/_validate_binary_like.ts","time":1785241875}