Skip to content

Commit

Permalink
feat: add brotli compression support, (WIP) make it configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
okikio authored Mar 2, 2022
1 parent 3d3db6d commit e56a174
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 602 deletions.
Binary file removed src/assets/brotli_wasm_bg.wasm
Binary file not shown.
150 changes: 67 additions & 83 deletions src/ts/util/brotli-wasm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


// Based on wasm-brotli (https://github.com/dfrankland/wasm-brotli)
const heap = new Array(32);
heap.fill(undefined);
heap.push(undefined, null, true, false);
Expand Down Expand Up @@ -51,11 +50,13 @@ const wasmImportObjects = {
};
},
};
let promises = [];

export let wasmExports;
let promises = [];
let promise;

let importObject = wasmImportObjects[wasmPath]();
let req = fetch("/wasm_brotli_browser_bg.wasm");
let promise;
if (importObject instanceof Promise && typeof WebAssembly.compileStreaming === 'function') {
promise = Promise.all([WebAssembly.compileStreaming(req), importObject]).then(function (items) {
return WebAssembly.instantiate(items[0], items[1]);
Expand All @@ -68,96 +69,79 @@ if (importObject instanceof Promise && typeof WebAssembly.compileStreaming === '
return WebAssembly.instantiate(bytes, importObject);
});
}

promises.push(promise.then(function (res) {
return (wasmExports = (res.instance || res).exports);
}));

let wasmPromise = Promise.all(promises);
async function getWASM() {
if (!wasmExports) await wasmPromise;
return wasmPromise;
};
export const memory = (async () => {
await getWASM();
return wasmExports.memory;
})();













let cachegetUint8Memory = null;
async function getUint8Memory() {
await getWASM();
if (cachegetUint8Memory === null || cachegetUint8Memory.buffer !== wasmExports["memory"].buffer) {
cachegetUint8Memory = new Uint8Array(wasmExports["memory"].buffer);
}
return cachegetUint8Memory;
}

let WASM_VECTOR_LEN = 0;
async function passArray8ToWasm(arg) {
await getWASM();
const ptr = wasmExports["__wbindgen_malloc"](arg.length * 1);
(await getUint8Memory()).set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}

let cachegetInt32Memory = null;
async function getInt32Memory() {
await getWASM();
if (cachegetInt32Memory === null || cachegetInt32Memory.buffer !== wasmExports["memory"].buffer) {
cachegetInt32Memory = new Int32Array(wasmExports["memory"].buffer);
}
return cachegetInt32Memory;
}

async function getArrayU8FromWasm(ptr, len) {
await getWASM();
return (await getUint8Memory()).subarray(ptr / 1, ptr / 1 + len);
}
/**
* @param {Uint8Array} buffer
* @returns {Uint8Array}
*/
export async function compress(buffer) {
await getWASM();
const retptr = 8;
const ret = wasmExports["compress"](retptr, await passArray8ToWasm(buffer), WASM_VECTOR_LEN);
const memi32 = await getInt32Memory();
const v0 = (await getArrayU8FromWasm(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1])).slice();
wasmExports["__wbindgen_free"](memi32[retptr / 4 + 0], memi32[retptr / 4 + 1] * 1);
return v0;
}

/**
* @param {Uint8Array} buffer
* @returns {Uint8Array}
*/
export async function decompress(buffer) {
await getWASM();
const retptr = 8;
const ret = wasmExports["decompress"](retptr, await passArray8ToWasm(buffer), WASM_VECTOR_LEN);
const memi32 = (await getInt32Memory());
const v0 = (await getArrayU8FromWasm(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1])).slice();
wasmExports["__wbindgen_free"](memi32[retptr / 4 + 0], memi32[retptr / 4 + 1] * 1);
return v0;
}

// let _wasm_brotli_browser_bg_wasm__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./wasm_brotli_browser_bg.wasm */ "./node_modules/wasm-brotli/wasm_brotli_browser_bg.wasm");

// let cachegetUint8Memory = null;
// function getUint8Memory() {
// if (cachegetUint8Memory === null || cachegetUint8Memory.buffer !== _wasm_brotli_browser_bg_wasm__WEBPACK_IMPORTED_MODULE_0__["memory"].buffer) {
// cachegetUint8Memory = new Uint8Array(_wasm_brotli_browser_bg_wasm__WEBPACK_IMPORTED_MODULE_0__["memory"].buffer);
// }
// return cachegetUint8Memory;
// }

// let WASM_VECTOR_LEN = 0;

// function passArray8ToWasm(arg) {
// const ptr = _wasm_brotli_browser_bg_wasm__WEBPACK_IMPORTED_MODULE_0__["__wbindgen_malloc"](arg.length * 1);
// getUint8Memory().set(arg, ptr / 1);
// WASM_VECTOR_LEN = arg.length;
// return ptr;
// }

// let cachegetInt32Memory = null;
// function getInt32Memory() {
// if (cachegetInt32Memory === null || cachegetInt32Memory.buffer !== _wasm_brotli_browser_bg_wasm__WEBPACK_IMPORTED_MODULE_0__["memory"].buffer) {
// cachegetInt32Memory = new Int32Array(_wasm_brotli_browser_bg_wasm__WEBPACK_IMPORTED_MODULE_0__["memory"].buffer);
// }
// return cachegetInt32Memory;
// }

// function getArrayU8FromWasm(ptr, len) {
// return getUint8Memory().subarray(ptr / 1, ptr / 1 + len);
// }
// /**
// * @param {Uint8Array} buffer
// * @returns {Uint8Array}
// */
// export function compress(buffer) {
// const retptr = 8;
// const ret = _wasm_brotli_browser_bg_wasm__WEBPACK_IMPORTED_MODULE_0__["compress"](retptr, passArray8ToWasm(buffer), WASM_VECTOR_LEN);
// const memi32 = getInt32Memory();
// const v0 = getArrayU8FromWasm(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1]).slice();
// _wasm_brotli_browser_bg_wasm__WEBPACK_IMPORTED_MODULE_0__["__wbindgen_free"](memi32[retptr / 4 + 0], memi32[retptr / 4 + 1] * 1);
// return v0;
// }

// /**
// * @param {Uint8Array} buffer
// * @returns {Uint8Array}
// */
// export function decompress(buffer) {
// const retptr = 8;
// const ret = _wasm_brotli_browser_bg_wasm__WEBPACK_IMPORTED_MODULE_0__["decompress"](retptr, passArray8ToWasm(buffer), WASM_VECTOR_LEN);
// const memi32 = getInt32Memory();
// const v0 = getArrayU8FromWasm(memi32[retptr / 4 + 0], memi32[retptr / 4 + 1]).slice();
// _wasm_brotli_browser_bg_wasm__WEBPACK_IMPORTED_MODULE_0__["__wbindgen_free"](memi32[retptr / 4 + 0], memi32[retptr / 4 + 1] * 1);
// return v0;
// }

// let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });

// cachedTextDecoder.decode();

// function getStringFromWasm(ptr, len) {
// return cachedTextDecoder.decode(getUint8Memory().subarray(ptr, ptr + len));
// }
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
cachedTextDecoder.decode();
async function getStringFromWasm(ptr, len) {
await getWASM();
return cachedTextDecoder.decode((await getUint8Memory()).subarray(ptr, ptr + len));
}
Loading

0 comments on commit e56a174

Please sign in to comment.