Debian Codegraph @

wasm/browser/script/rollup-arraybuffer.js

raw
1 import { readFileSync, realpathSync } from "fs";
2
3 const arraybufferCode = () =>
4 `
5 function atobPolyfill(input) {
6 var chars =
7 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
8 var str = String(input).replace(/[=]+$/, "");
9 if (str.length % 4 === 1) {
10 console.error(
11 "'atob' failed: The string to be decoded is not correctly encoded."
12 );
13 }
14 for (
15 var bc = 0, bs, buffer, idx = 0, output = "";
16 (buffer = str.charAt(idx++));
17 ~buffer &&
18 ((bs = bc % 4 ? bs * 64 + buffer : buffer),
19 bc++ % 4)
20 ? (output += String.fromCharCode(255 & (bs >> ((-2 * bc) & 6))))
21 : 0
22 ) {
23 buffer = chars.indexOf(buffer);
24 }
25 return output;
26 }
27
28 function bufferFromBrowser(base64Data) {
29 if (typeof AudioWorkletGlobalScope != "undefined") {
30 return atobPolyfill(base64Data);
31 } else {
32 return window.atob(base64Data);
33 }
34 }
35 function bufferFromNodeJS(base64Data) {
36 return Buffer.from(base64Data, "base64").toString("binary");
37 }
38 function __toArrayBuffer(base64Data) {
39 var window = window || this;
40 var isBrowser = typeof process === "undefined";
41 var binary = isBrowser
42 ? bufferFromBrowser(base64Data)
43 : bufferFromNodeJS(base64Data);
44 var bytes = new Uint8Array(binary.length);
45 for (var i = 0; i < binary.length; ++i) {
46 bytes[i] = binary.charCodeAt(i);
47 }
48 return bytes.buffer;
49 }
50 `
51 .split("\n")
52 .map(Function.prototype.call, String.prototype.trim)
53 .join("");
54
55 export default function arraybufferPlugin(options = {}) {
56 // const filter = createFilter(options.include, options.exclude);
57 // const wasmPath = "node_modules/@csound/wasm/lib/libcsound.wasm.zlib";
58 return {
59 name: "arraybuffer",
60 intro: arraybufferCode,
61 load(id) {
62 if (id.endsWith(".wasm.z")) {
63 id = realpathSync(id);
64 return {
65 code: `export default __toArrayBuffer("${readFileSync(id).toString("base64")}");`,
66 map: { mappings: "" },
67 };
68 } else if (id.includes("transformer.wasm")) {
69 // id = resolve("../");
70 return {
71 code: `export default __toArrayBuffer("${readFileSync(require.resolve(id)).toString(
72 "base64",
73 )}");`,
74 map: { mappings: "" },
75 };
76 }
77 },
78 };
79 }