forked from eshaz/wasm-audio-decoders
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
54 lines (46 loc) · 1.72 KB
/
build.js
File metadata and controls
54 lines (46 loc) · 1.72 KB
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
import fs from "fs";
import yenc from "simple-yenc";
import { deflateSync } from "fflate";
const distPath = process.argv[2];
const decoder = fs.readFileSync(distPath, { encoding: "ascii" });
const wasmBase64ContentMatcher =
/Module\["wasm"\] = base64Decode\("(?<wasm>(.+))"\)/;
const wasmBase64DeclarationMatcher = 'Module["wasm"] = base64Decode("';
// original wasm
const wasmContent = decoder.match(wasmBase64ContentMatcher).groups.wasm;
// compressed buffer
const wasmBuffer = Uint8Array.from(Buffer.from(wasmContent, "base64"));
const wasmBufferCompressed = deflateSync(wasmBuffer, {
level: 9,
mem: 12,
});
// yEnc encoded wasm
const yencEncodedWasm = yenc.encode(wasmBufferCompressed);
const yencStringifiedWasm = yenc.stringify(yencEncodedWasm);
// code before the wasm
const startIdx = decoder.indexOf(wasmBase64DeclarationMatcher);
// code after the wasm
const endIdx =
startIdx + wasmBase64DeclarationMatcher.length + wasmContent.length + 2;
const banner =
"/* **************************************************\n" +
" * This file is auto-generated during the build process.\n" +
" * Any edits to this file will be overwritten.\n" +
" ****************************************************/" +
"\n\n";
// Concatenate the strings as buffers to preserve extended ascii
const finalString = Buffer.concat(
[
banner,
"export default class EmscriptenWASM {\n",
"constructor(WASMAudioDecoderCommon) {\n",
decoder.substring(0, startIdx),
'Module["wasm"] = WASMAudioDecoderCommon.inflateYencString(`',
yencStringifiedWasm,
`\`, new Uint8Array(${wasmBuffer.length}))`,
decoder.substring(endIdx),
"}",
"}",
].map(Buffer.from)
);
fs.writeFileSync(distPath, finalString, { encoding: "binary" });