-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
62 lines (57 loc) · 1.47 KB
/
index.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
const fs = require("fs");
const path = require("path");
const compiled = new WebAssembly.Module(
fs.readFileSync(path.resolve(__dirname, "./build/optimized.wasm"))
);
const memory = new WebAssembly.Memory({ initial: 10 });
const imports = {
env: {
memory,
abort: (filename, line, column) => {
throw Error(
`abort called at ${filename ? filename + ":" : ""}${line}:${column}`
);
}
}
};
const lzma = new WebAssembly.Instance(compiled, imports).exports;
// AssemblyScript ArrayBuffer header length
const AS_ARRAY_OFFSET = 24;
function decodeResult(ptr, memory) {
const resultBuffer = new Uint32Array(memory.buffer, ptr, 4);
const [success, error, unpackSize, dataPtr] = resultBuffer;
if (success) {
const data = new Uint8Array(
memory.buffer,
dataPtr + AS_ARRAY_OFFSET,
unpackSize
);
return {
error,
unpackSize,
data
};
}
return {
error,
unpackSize,
data: null
};
}
// Decode LZMA data
module.exports = {
decode(inputBuffer) {
// Allocate memory to copy input data.
const inputDataPtr = lzma.newU8Array(inputBuffer.length);
// Create an Uint8Array using allocated buffer and set input data
const u8Array = new Uint8Array(
memory.buffer,
inputDataPtr + AS_ARRAY_OFFSET,
inputBuffer.length
);
u8Array.set(inputBuffer);
const resultPtr = lzma.decode(inputDataPtr);
const result = decodeResult(resultPtr, memory);
return result;
}
};