-
Notifications
You must be signed in to change notification settings - Fork 0
/
hysnappy.js
77 lines (67 loc) · 6.96 KB
/
hysnappy.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
71
72
73
74
75
76
77
/**
* Uncompress a snappy compressed buffer.
*
* @param {Uint8Array} input
* @param {number} outputLength
* @returns {Uint8Array}
*/
export function snappyUncompress(input, outputLength) {
return snappyUncompressor()(input, outputLength)
}
/**
* Load wasm and return uncompressor function.
*
* @returns {(input: Uint8Array, outputLength: number) => Uint8Array}
*/
export function snappyUncompressor() {
// Instantiate wasm module
const wasm = instantiateWasm()
return (input, outputLength) => {
/** @type {any} */
const { memory, uncompress } = wasm.exports
// Input data is passed into wasm memory at inputStart
// Output data is expected to be written to wasm memory at outputStart
// clang uses some wasm memory, so we need to skip past that
const inputStart = 68000 // 68 kb
const outputStart = inputStart + input.byteLength
// WebAssembly memory
const totalSize = inputStart + input.byteLength + outputLength
if (memory.buffer.byteLength < totalSize) {
// Calculate the number of pages needed, rounding up
const pageSize = 64 * 1024 // 64KiB per page
const currentPages = memory.buffer.byteLength / pageSize
const requiredPages = Math.ceil(totalSize / pageSize)
const pagesToGrow = requiredPages - currentPages
memory.grow(pagesToGrow)
}
// Copy the compressed data to WASM memory
const byteArray = new Uint8Array(memory.buffer)
byteArray.set(input, inputStart)
// Call wasm uncompress function
const result = uncompress(inputStart, input.byteLength, outputStart)
// Check for errors
if (result === -1) throw new Error('invalid snappy length header')
if (result === -2) throw new Error('missing eof marker')
if (result === -3) throw new Error('premature end of input')
if (result) throw new Error(`failed to uncompress data ${result}`)
// Get uncompressed data from WASM memory
return byteArray.slice(outputStart, outputStart + outputLength)
}
}
/**
* Instantiate WASM module from a base64 string.
*
* @returns {WebAssembly.Instance}
*/
function instantiateWasm() {
const binaryString = atob(wasm64)
const byteArray = new Uint8Array(binaryString.length)
for (let i = 0; i < binaryString.length; i += 1) {
byteArray[i] = binaryString.charCodeAt(i)
}
// only works for payload less than 4kb:
const mod = new WebAssembly.Module(byteArray)
return new WebAssembly.Instance(mod)
}
// Base64 encoded hysnappy.wasm
const wasm64 = 'AGFzbQEAAAABEANgAABgA39/fwF/YAF/AX8DBgUAAQEBAgUDAQACBj8KfwFBoIwEC38AQYAIC38AQaAMC38AQaAMC38AQaCMBAt/AEGACAt/AEGgjAQLfwBBgIAIC38AQQALfwBBAQsHwQEOBm1lbW9yeQIAEV9fd2FzbV9jYWxsX2N0b3JzAAAGbWVtY3B5AAEHbWVtbW92ZQACCnVuY29tcHJlc3MAAwxfX2Rzb19oYW5kbGUDAQpfX2RhdGFfZW5kAwILX19zdGFja19sb3cDAwxfX3N0YWNrX2hpZ2gDBA1fX2dsb2JhbF9iYXNlAwULX19oZWFwX2Jhc2UDBgpfX2hlYXBfZW5kAwcNX19tZW1vcnlfYmFzZQMIDF9fdGFibGVfYmFzZQMJCuASBQIAC8sBAQN/AkAgAkUNAAJAAkAgAkEHcSIDDQAgACEEIAIhBQwBCyAAIQQgAiEFA0AgBCABLQAAOgAAIARBAWohBCABQQFqIQEgBUF/aiEFIANBf2oiAw0ACwsgAkEISQ0AA0AgBCABLQAAOgAAIAQgAS0AAToAASAEIAEtAAI6AAIgBCABLQADOgADIAQgAS0ABDoABCAEIAEtAAU6AAUgBCABLQAGOgAGIAQgAS0ABzoAByAEQQhqIQQgAUEIaiEBIAVBeGoiBQ0ACwsgAAugAwEEfwJAIAAgAUYNAAJAAkAgACABSQ0AIAEgAmoiAyAASw0BCyACRQ0BAkACQCACQQdxIgMNACAAIQQgAiEFDAELIAAhBCACIQUDQCAEIAEtAAA6AAAgBEEBaiEEIAFBAWohASAFQX9qIQUgA0F/aiIDDQALCyACQQhJDQEDQCAEIAEtAAA6AAAgBCABLQABOgABIAQgAS0AAjoAAiAEIAEtAAM6AAMgBCABLQ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