2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-09-22 09:47:24 +00:00
|
|
|
|
|
|
|
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(
|
2024-08-23 03:32:15 +00:00
|
|
|
`Cannot validate the input as it must be a Uint8Array, a string, or an ArrayBuffer: received a value of the type ${
|
2023-09-22 09:47:24 +00:00
|
|
|
getTypeName(source)
|
2024-08-23 03:32:15 +00:00
|
|
|
}`,
|
2023-09-22 09:47:24 +00:00
|
|
|
);
|
|
|
|
}
|