-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoab.js
70 lines (67 loc) · 2.36 KB
/
toab.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
async function toab(data, { debug = false } = { debug: false }) {
let result;
if (data instanceof Promise) data = await data;
if (data instanceof ArrayBuffer) {
if (debug) console.log("[toab] data appears to already be an array buffer");
result = data;
} else if (typeof Buffer !== "undefined" && Buffer.isBuffer(data)) {
if (debug) console.log("[toab] data appears to be a buffer");
result = data.buffer.slice(
data.byteOffset,
data.byteOffset + data.byteLength
);
} else if (
data instanceof DataView ||
data instanceof Int8Array ||
data instanceof Uint8Array ||
data instanceof Int16Array ||
data instanceof Uint16Array ||
data instanceof Int32Array ||
data instanceof Uint32Array ||
data instanceof Float32Array ||
data instanceof Float64Array ||
data instanceof BigInt64Array ||
data instanceof BigUint64Array
) {
if (data.byteLength !== data.buffer.byteLength) {
result = data.buffer.slice(
data.byteOffset,
data.byteOffset + data.byteLength
);
} else {
result = data.buffer;
}
} else if (typeof File !== "undefined" && data instanceof File) {
if (data.arrayBuffer) {
return data.arrayBuffer();
}
} else if (
(typeof Response !== "undefined" && data instanceof Response) ||
(typeof data === "object" &&
data.__proto__ &&
data.__proto__.constructor &&
data.__proto__.constructor.name === "Response")
) {
return data.arrayBuffer();
} else if (typeof data === "string") {
if (debug) console.log("[toab] data appears to be a string");
if (data.startsWith("data:")) {
if (typeof fetch !== "undefined") {
return fetch(data).then(response => response.arrayBuffer());
} else if (typeof Buffer !== "undefined" && data.includes(";base64,")) {
const encoding = "base64";
const buffer = Buffer.from(data.split(";base64,")[1], encoding);
return toab(buffer);
}
} else if (typeof TextEncoder === "function") {
const encoder = new TextEncoder();
const uint8Array = encoder.encode(data);
result = uint8Array.buffer;
}
}
if (debug) console.log("[toab] result is:", result);
return result;
}
if (typeof module === "object") module.exports = toab;
if (typeof self !== "undefined") self.toab = toab;
if (typeof window !== "undefined") window.toab = toab;