Spaces:
Sleeping
Sleeping
| ; | |
| /** | |
| * gearhash-jit — Fast GEAR rolling hash for content-defined chunking. | |
| * | |
| * Uses a tiny hand-written WASM module with native i64 arithmetic. | |
| * The hash state is kept as raw bytes in JS (avoiding BigInt in the hot path) | |
| * and written to WASM memory only for the `nextMatch` call. | |
| */ | |
| Object.defineProperty(exports, "__esModule", { value: true }); | |
| exports.Hasher = exports.GEAR_TABLE = void 0; | |
| const wasm_js_1 = require("./wasm.js"); | |
| var table_js_1 = require("./table.js"); | |
| Object.defineProperty(exports, "GEAR_TABLE", { enumerable: true, get: function () { return table_js_1.GEAR_TABLE; } }); | |
| class Hasher { | |
| maskBytes; | |
| /** | |
| * The current 64-bit rolling hash state as 8 little-endian bytes. | |
| * Updated after every `nextMatch` call. Zeroed by `resetHash()`. | |
| */ | |
| hash; | |
| constructor(mask) { | |
| (0, wasm_js_1.initWasm)(); | |
| this.maskBytes = new Uint8Array(8); | |
| this.hash = new Uint8Array(8); | |
| new DataView(this.maskBytes.buffer).setBigUint64(0, mask, true); | |
| } | |
| /** | |
| * Scan `buf` for the next gear-hash match. The internal hash state | |
| * carries over between calls (for split-buffer scanning). | |
| * | |
| * @returns 1-based byte position of the match, or -1 if none found. | |
| */ | |
| nextMatch(buf) { | |
| const len = buf.length; | |
| if (len === 0) | |
| return -1; | |
| if (len > wasm_js_1.MAX_INPUT_SIZE) { | |
| throw new RangeError(`Input too large: ${len} > ${wasm_js_1.MAX_INPUT_SIZE}`); | |
| } | |
| const view = (0, wasm_js_1.getView)(); | |
| view.set(this.hash, wasm_js_1.HASH_OFFSET); | |
| view.set(this.maskBytes, wasm_js_1.MASK_OFFSET); | |
| view.set(buf, wasm_js_1.INPUT_OFFSET); | |
| const pos = (0, wasm_js_1.wasmNextMatch)(wasm_js_1.INPUT_OFFSET, len); | |
| this.hash.set(view.subarray(wasm_js_1.HASH_OFFSET, wasm_js_1.HASH_OFFSET + 8)); | |
| return pos; | |
| } | |
| /** Reset rolling hash to zero (call when starting a new chunk). */ | |
| resetHash() { | |
| this.hash.fill(0); | |
| } | |
| } | |
| exports.Hasher = Hasher; | |