446 lines
12 KiB
JavaScript
446 lines
12 KiB
JavaScript
|
|
let wasm;
|
|
|
|
const heap = new Array(32).fill(undefined);
|
|
|
|
heap.push(undefined, null, true, false);
|
|
|
|
function getObject(idx) { return heap[idx]; }
|
|
|
|
let WASM_VECTOR_LEN = 0;
|
|
|
|
let cachegetUint8Memory0 = null;
|
|
function getUint8Memory0() {
|
|
if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
|
|
cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
|
|
}
|
|
return cachegetUint8Memory0;
|
|
}
|
|
|
|
const cachedTextEncoder = new TextEncoder('utf-8');
|
|
|
|
const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
|
|
? function (arg, view) {
|
|
return cachedTextEncoder.encodeInto(arg, view);
|
|
}
|
|
: function (arg, view) {
|
|
const buf = cachedTextEncoder.encode(arg);
|
|
view.set(buf);
|
|
return {
|
|
read: arg.length,
|
|
written: buf.length
|
|
};
|
|
});
|
|
|
|
function passStringToWasm0(arg, malloc, realloc) {
|
|
|
|
if (realloc === undefined) {
|
|
const buf = cachedTextEncoder.encode(arg);
|
|
const ptr = malloc(buf.length);
|
|
getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
WASM_VECTOR_LEN = buf.length;
|
|
return ptr;
|
|
}
|
|
|
|
let len = arg.length;
|
|
let ptr = malloc(len);
|
|
|
|
const mem = getUint8Memory0();
|
|
|
|
let offset = 0;
|
|
|
|
for (; offset < len; offset++) {
|
|
const code = arg.charCodeAt(offset);
|
|
if (code > 0x7F) break;
|
|
mem[ptr + offset] = code;
|
|
}
|
|
|
|
if (offset !== len) {
|
|
if (offset !== 0) {
|
|
arg = arg.slice(offset);
|
|
}
|
|
ptr = realloc(ptr, len, len = offset + arg.length * 3);
|
|
const view = getUint8Memory0().subarray(ptr + offset, ptr + len);
|
|
const ret = encodeString(arg, view);
|
|
|
|
offset += ret.written;
|
|
}
|
|
|
|
WASM_VECTOR_LEN = offset;
|
|
return ptr;
|
|
}
|
|
|
|
let cachegetInt32Memory0 = null;
|
|
function getInt32Memory0() {
|
|
if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) {
|
|
cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer);
|
|
}
|
|
return cachegetInt32Memory0;
|
|
}
|
|
|
|
const cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
|
|
cachedTextDecoder.decode();
|
|
|
|
function getStringFromWasm0(ptr, len) {
|
|
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
|
|
}
|
|
|
|
let heap_next = heap.length;
|
|
|
|
function addHeapObject(obj) {
|
|
if (heap_next === heap.length) heap.push(heap.length + 1);
|
|
const idx = heap_next;
|
|
heap_next = heap[idx];
|
|
|
|
heap[idx] = obj;
|
|
return idx;
|
|
}
|
|
|
|
function dropObject(idx) {
|
|
if (idx < 36) return;
|
|
heap[idx] = heap_next;
|
|
heap_next = idx;
|
|
}
|
|
|
|
function takeObject(idx) {
|
|
const ret = getObject(idx);
|
|
dropObject(idx);
|
|
return ret;
|
|
}
|
|
|
|
function passArray8ToWasm0(arg, malloc) {
|
|
const ptr = malloc(arg.length * 1);
|
|
getUint8Memory0().set(arg, ptr / 1);
|
|
WASM_VECTOR_LEN = arg.length;
|
|
return ptr;
|
|
}
|
|
|
|
let stack_pointer = 32;
|
|
|
|
function addBorrowedObject(obj) {
|
|
if (stack_pointer == 1) throw new Error('out of js stack');
|
|
heap[--stack_pointer] = obj;
|
|
return stack_pointer;
|
|
}
|
|
|
|
function getArrayU8FromWasm0(ptr, len) {
|
|
return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
|
|
}
|
|
/**
|
|
* @param {Uint8Array} buf
|
|
* @param {any} raw_options
|
|
* @returns {Uint8Array}
|
|
*/
|
|
export function compress(buf, raw_options) {
|
|
try {
|
|
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
const ptr0 = passArray8ToWasm0(buf, wasm.__wbindgen_malloc);
|
|
const len0 = WASM_VECTOR_LEN;
|
|
wasm.compress(retptr, ptr0, len0, addBorrowedObject(raw_options));
|
|
var r0 = getInt32Memory0()[retptr / 4 + 0];
|
|
var r1 = getInt32Memory0()[retptr / 4 + 1];
|
|
var r2 = getInt32Memory0()[retptr / 4 + 2];
|
|
var r3 = getInt32Memory0()[retptr / 4 + 3];
|
|
if (r3) {
|
|
throw takeObject(r2);
|
|
}
|
|
var v1 = getArrayU8FromWasm0(r0, r1).slice();
|
|
wasm.__wbindgen_free(r0, r1 * 1);
|
|
return v1;
|
|
} finally {
|
|
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
heap[stack_pointer++] = undefined;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param {Uint8Array} buf
|
|
* @returns {Uint8Array}
|
|
*/
|
|
export function decompress(buf) {
|
|
try {
|
|
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
const ptr0 = passArray8ToWasm0(buf, wasm.__wbindgen_malloc);
|
|
const len0 = WASM_VECTOR_LEN;
|
|
wasm.decompress(retptr, ptr0, len0);
|
|
var r0 = getInt32Memory0()[retptr / 4 + 0];
|
|
var r1 = getInt32Memory0()[retptr / 4 + 1];
|
|
var r2 = getInt32Memory0()[retptr / 4 + 2];
|
|
var r3 = getInt32Memory0()[retptr / 4 + 3];
|
|
if (r3) {
|
|
throw takeObject(r2);
|
|
}
|
|
var v1 = getArrayU8FromWasm0(r0, r1).slice();
|
|
wasm.__wbindgen_free(r0, r1 * 1);
|
|
return v1;
|
|
} finally {
|
|
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
}
|
|
}
|
|
|
|
function isLikeNone(x) {
|
|
return x === undefined || x === null;
|
|
}
|
|
/**
|
|
*/
|
|
export const BrotliStreamResult = Object.freeze({
|
|
/**
|
|
* The stream is just initialized and no input is provided currently.
|
|
* `BrotliResult` uses `ResultFailure = 0`, but as we will convert `ResultFailure` to a negative actual error code,
|
|
* 0 is reused as no input currently.
|
|
* As for Brotli compressing, since offical API does not provide a way to retrieve a detailed error code, -1 is used.
|
|
*/
|
|
Init: 0, "0": "Init", ResultSuccess: 1, "1": "ResultSuccess", NeedsMoreInput: 2, "2": "NeedsMoreInput", NeedsMoreOutput: 3, "3": "NeedsMoreOutput",
|
|
});
|
|
/**
|
|
*/
|
|
export class CompressStream {
|
|
|
|
static __wrap(ptr) {
|
|
const obj = Object.create(CompressStream.prototype);
|
|
obj.ptr = ptr;
|
|
|
|
return obj;
|
|
}
|
|
|
|
__destroy_into_raw() {
|
|
const ptr = this.ptr;
|
|
this.ptr = 0;
|
|
|
|
return ptr;
|
|
}
|
|
|
|
free() {
|
|
const ptr = this.__destroy_into_raw();
|
|
wasm.__wbg_compressstream_free(ptr);
|
|
}
|
|
/**
|
|
* @param {number | undefined} quality
|
|
*/
|
|
constructor(quality) {
|
|
const ret = wasm.compressstream_new(!isLikeNone(quality), isLikeNone(quality) ? 0 : quality);
|
|
return CompressStream.__wrap(ret);
|
|
}
|
|
/**
|
|
* @param {Uint8Array | undefined} input_opt
|
|
* @param {number} output_size
|
|
* @returns {Uint8Array}
|
|
*/
|
|
compress(input_opt, output_size) {
|
|
try {
|
|
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
var ptr0 = isLikeNone(input_opt) ? 0 : passArray8ToWasm0(input_opt, wasm.__wbindgen_malloc);
|
|
var len0 = WASM_VECTOR_LEN;
|
|
wasm.compressstream_compress(retptr, this.ptr, ptr0, len0, output_size);
|
|
var r0 = getInt32Memory0()[retptr / 4 + 0];
|
|
var r1 = getInt32Memory0()[retptr / 4 + 1];
|
|
var r2 = getInt32Memory0()[retptr / 4 + 2];
|
|
var r3 = getInt32Memory0()[retptr / 4 + 3];
|
|
if (r3) {
|
|
throw takeObject(r2);
|
|
}
|
|
var v1 = getArrayU8FromWasm0(r0, r1).slice();
|
|
wasm.__wbindgen_free(r0, r1 * 1);
|
|
return v1;
|
|
} finally {
|
|
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
}
|
|
}
|
|
/**
|
|
* @returns {number}
|
|
*/
|
|
total_out() {
|
|
const ret = wasm.compressstream_total_out(this.ptr);
|
|
return ret >>> 0;
|
|
}
|
|
/**
|
|
* @returns {number}
|
|
*/
|
|
result() {
|
|
const ret = wasm.compressstream_result(this.ptr);
|
|
return ret;
|
|
}
|
|
/**
|
|
* @returns {number}
|
|
*/
|
|
last_input_offset() {
|
|
const ret = wasm.compressstream_last_input_offset(this.ptr);
|
|
return ret >>> 0;
|
|
}
|
|
}
|
|
/**
|
|
*/
|
|
export class DecompressStream {
|
|
|
|
static __wrap(ptr) {
|
|
const obj = Object.create(DecompressStream.prototype);
|
|
obj.ptr = ptr;
|
|
|
|
return obj;
|
|
}
|
|
|
|
__destroy_into_raw() {
|
|
const ptr = this.ptr;
|
|
this.ptr = 0;
|
|
|
|
return ptr;
|
|
}
|
|
|
|
free() {
|
|
const ptr = this.__destroy_into_raw();
|
|
wasm.__wbg_decompressstream_free(ptr);
|
|
}
|
|
/**
|
|
*/
|
|
constructor() {
|
|
const ret = wasm.decompressstream_new();
|
|
return DecompressStream.__wrap(ret);
|
|
}
|
|
/**
|
|
* @param {Uint8Array} input
|
|
* @param {number} output_size
|
|
* @returns {Uint8Array}
|
|
*/
|
|
decompress(input, output_size) {
|
|
try {
|
|
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
|
|
const ptr0 = passArray8ToWasm0(input, wasm.__wbindgen_malloc);
|
|
const len0 = WASM_VECTOR_LEN;
|
|
wasm.decompressstream_decompress(retptr, this.ptr, ptr0, len0, output_size);
|
|
var r0 = getInt32Memory0()[retptr / 4 + 0];
|
|
var r1 = getInt32Memory0()[retptr / 4 + 1];
|
|
var r2 = getInt32Memory0()[retptr / 4 + 2];
|
|
var r3 = getInt32Memory0()[retptr / 4 + 3];
|
|
if (r3) {
|
|
throw takeObject(r2);
|
|
}
|
|
var v1 = getArrayU8FromWasm0(r0, r1).slice();
|
|
wasm.__wbindgen_free(r0, r1 * 1);
|
|
return v1;
|
|
} finally {
|
|
wasm.__wbindgen_add_to_stack_pointer(16);
|
|
}
|
|
}
|
|
/**
|
|
* @returns {number}
|
|
*/
|
|
total_out() {
|
|
const ret = wasm.decompressstream_total_out(this.ptr);
|
|
return ret >>> 0;
|
|
}
|
|
/**
|
|
* @returns {number}
|
|
*/
|
|
result() {
|
|
const ret = wasm.decompressstream_result(this.ptr);
|
|
return ret;
|
|
}
|
|
/**
|
|
* @returns {number}
|
|
*/
|
|
last_input_offset() {
|
|
const ret = wasm.decompressstream_last_input_offset(this.ptr);
|
|
return ret >>> 0;
|
|
}
|
|
}
|
|
|
|
async function load(module, imports) {
|
|
if (typeof Response === 'function' && module instanceof Response) {
|
|
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
|
try {
|
|
return await WebAssembly.instantiateStreaming(module, imports);
|
|
|
|
} catch (e) {
|
|
if (module.headers.get('Content-Type') != 'application/wasm') {
|
|
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
|
|
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
|
|
const bytes = await module.arrayBuffer();
|
|
return await WebAssembly.instantiate(bytes, imports);
|
|
|
|
} else {
|
|
const instance = await WebAssembly.instantiate(module, imports);
|
|
|
|
if (instance instanceof WebAssembly.Instance) {
|
|
return { instance, module };
|
|
|
|
} else {
|
|
return instance;
|
|
}
|
|
}
|
|
}
|
|
|
|
async function init(input) {
|
|
if (typeof input === 'undefined') {
|
|
input = new URL('brotli_wasm_bg.wasm', import.meta.url);
|
|
}
|
|
const imports = {};
|
|
imports.wbg = {};
|
|
imports.wbg.__wbindgen_json_serialize = function (arg0, arg1) {
|
|
const obj = getObject(arg1);
|
|
const ret = JSON.stringify(obj === undefined ? null : obj);
|
|
const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
const len0 = WASM_VECTOR_LEN;
|
|
getInt32Memory0()[arg0 / 4 + 1] = len0;
|
|
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
|
|
};
|
|
imports.wbg.__wbindgen_is_undefined = function (arg0) {
|
|
const ret = getObject(arg0) === undefined;
|
|
return ret;
|
|
};
|
|
imports.wbg.__wbindgen_is_object = function (arg0) {
|
|
const val = getObject(arg0);
|
|
const ret = typeof (val) === 'object' && val !== null;
|
|
return ret;
|
|
};
|
|
imports.wbg.__wbindgen_string_new = function (arg0, arg1) {
|
|
const ret = getStringFromWasm0(arg0, arg1);
|
|
return addHeapObject(ret);
|
|
};
|
|
imports.wbg.__wbg_new_693216e109162396 = function () {
|
|
const ret = new Error();
|
|
return addHeapObject(ret);
|
|
};
|
|
imports.wbg.__wbg_stack_0ddaca5d1abfb52f = function (arg0, arg1) {
|
|
const ret = getObject(arg1).stack;
|
|
const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
const len0 = WASM_VECTOR_LEN;
|
|
getInt32Memory0()[arg0 / 4 + 1] = len0;
|
|
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
|
|
};
|
|
imports.wbg.__wbg_error_09919627ac0992f5 = function (arg0, arg1) {
|
|
try {
|
|
console.error(getStringFromWasm0(arg0, arg1));
|
|
} finally {
|
|
wasm.__wbindgen_free(arg0, arg1);
|
|
}
|
|
};
|
|
imports.wbg.__wbindgen_object_drop_ref = function (arg0) {
|
|
takeObject(arg0);
|
|
};
|
|
imports.wbg.__wbindgen_throw = function (arg0, arg1) {
|
|
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
};
|
|
|
|
if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
|
|
input = fetch(input);
|
|
}
|
|
|
|
|
|
|
|
const { instance, module } = await load(await input, imports);
|
|
|
|
wasm = instance.exports;
|
|
init.__wbindgen_wasm_module = module;
|
|
|
|
return wasm;
|
|
}
|
|
|
|
export default init;
|
|
|