@dstanesc/wasm-chunking-fastcdc-node
Version:
This crate generates WebAssembly bindings to the FastCDC content defined slicing library
194 lines (167 loc) • 5.87 kB
JavaScript
let imports = {};
imports['__wbindgen_placeholder__'] = module.exports;
let wasm;
const { TextDecoder } = require(`util`);
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
cachedTextDecoder.decode();
let cachedUint8Memory0 = new Uint8Array();
function getUint8Memory0() {
if (cachedUint8Memory0.byteLength === 0) {
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
}
return cachedUint8Memory0;
}
function getStringFromWasm0(ptr, len) {
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}
let WASM_VECTOR_LEN = 0;
function passArray8ToWasm0(arg, malloc) {
const ptr = malloc(arg.length * 1);
getUint8Memory0().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
let cachedInt32Memory0 = new Int32Array();
function getInt32Memory0() {
if (cachedInt32Memory0.byteLength === 0) {
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
}
return cachedInt32Memory0;
}
const heap = new Array(32).fill(undefined);
heap.push(undefined, null, true, false);
function getObject(idx) { return heap[idx]; }
let heap_next = heap.length;
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;
}
let cachedUint32Memory0 = new Uint32Array();
function getUint32Memory0() {
if (cachedUint32Memory0.byteLength === 0) {
cachedUint32Memory0 = new Uint32Array(wasm.memory.buffer);
}
return cachedUint32Memory0;
}
function getArrayU32FromWasm0(ptr, len) {
return getUint32Memory0().subarray(ptr / 4, ptr / 4 + len);
}
/**
*
* Compute chunks from a given slice of bytes.
*
* The `min_size` specifies the preferred minimum chunk size,
* `max_size` the preferred maximum chunk size; the `avg_size` is the
* desired "normal size" of the chunks. The smallest acceptable
* `min_size` is 64 bytes and likewise 256 bytes and 1024 bytes
* for `avg_size` and respectively `max_size`
*
* A js_sys::RangeError is returned when the above chunking specification is out of range.
*
*
* Example:
*
* ```
* use wasm_chunking_fastcdc::compute_chunks;
* let data: Vec<u8> = br"Lorem ipsum dolor sit amet, consectetur adipiscing elit...put more bits in here...".to_vec();
* let slice: &[u8] = &data;
* let min_size: u32 = 64;
* let avg_size: u32 = 256;
* let max_size: u32 = 1024;
* let offsets: Vec<u32> = compute_chunks(slice, min_size, avg_size, max_size).unwrap();
* ```
* @param {Uint8Array} source
* @param {number} min_size
* @param {number} avg_size
* @param {number} max_size
* @returns {Uint32Array}
*/
module.exports.compute_chunks = function(source, min_size, avg_size, max_size) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(source, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
wasm.compute_chunks(retptr, ptr0, len0, min_size, avg_size, max_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 = getArrayU32FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 4);
return v1;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
};
/**
*
* Compute chunks from a given slice of bytes.
*
* The `min_size` specifies the preferred minimum chunk size,
* `max_size` the preferred maximum chunk size; the `avg_size` is the
* desired "normal size" of the chunks. The smallest acceptable
* `min_size` is 64 bytes and likewise 256 bytes and 1024 bytes
* for `avg_size` and respectively `max_size`
*
* No js friendly chunking specification range validation
* Example:
*
* ```
* use wasm_chunking_fastcdc::compute_chunks_nocheck;
* let data: Vec<u8> = br"Lorem ipsum dolor sit amet, consectetur adipiscing elit...put more bits in here...".to_vec();
* let slice: &[u8] = &data;
* let min_size: u32 = 64;
* let avg_size: u32 = 256;
* let max_size: u32 = 1024;
* let offsets: Vec<u32> = compute_chunks_nocheck(slice, min_size, avg_size, max_size);
* ```
* @param {Uint8Array} source
* @param {number} min_size
* @param {number} avg_size
* @param {number} max_size
* @returns {Uint32Array}
*/
module.exports.compute_chunks_nocheck = function(source, min_size, avg_size, max_size) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passArray8ToWasm0(source, wasm.__wbindgen_malloc);
const len0 = WASM_VECTOR_LEN;
wasm.compute_chunks_nocheck(retptr, ptr0, len0, min_size, avg_size, max_size);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v1 = getArrayU32FromWasm0(r0, r1).slice();
wasm.__wbindgen_free(r0, r1 * 4);
return v1;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
};
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;
}
module.exports.__wbg_new_bec064441604955e = function(arg0, arg1) {
const ret = new RangeError(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
};
module.exports.__wbindgen_throw = function(arg0, arg1) {
throw new Error(getStringFromWasm0(arg0, arg1));
};
const path = require('path').join(__dirname, 'chunking_bg.wasm');
const bytes = require('fs').readFileSync(path);
const wasmModule = new WebAssembly.Module(bytes);
const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
wasm = wasmInstance.exports;
module.exports.__wasm = wasm;