-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
52 lines (42 loc) · 1.28 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
import oozFactory from './build/ooz.js';
const OOZ_SAFE_SPACE = 64;
const oozInst = await oozFactory();
let lastDecompressionPtr_ = 0;
/**
* @param {Uint8Array} data
* @param {number} rawSize
* @returns {Uint8Array}
*/
export function decompressUnsafe (data, rawSize) {
if (lastDecompressionPtr_) {
oozInst._free(lastDecompressionPtr_);
lastDecompressionPtr_ = 0;
}
const compressedPtr = oozInst._malloc(data.byteLength);
oozInst.HEAPU8.set(data, compressedPtr);
const decompressedPtr = oozInst._malloc(rawSize + OOZ_SAFE_SPACE);
lastDecompressionPtr_ = decompressedPtr;
const res = oozInst._Kraken_Decompress(
compressedPtr, data.byteLength,
decompressedPtr, rawSize
);
oozInst._free(compressedPtr);
if (res < 0) {
throw new Error('Failed to decode');
}
if (res !== rawSize) {
throw new Error('Decompresed size is different from expected');
}
return oozInst.HEAPU8.subarray(decompressedPtr, decompressedPtr + rawSize);
}
/**
* @param {Uint8Array} data
* @param {number} rawSize
* @returns {Uint8Array}
*/
export function decompress (data, rawSize) {
const decompressed = decompressUnsafe(data, rawSize);
const decompressedCopy = new Uint8Array(decompressed.byteLength);
decompressedCopy.set(decompressed);
return decompressedCopy;
}