Files
talk-pro/node_modules/xxhash-wasm/cjs/xxhash-wasm.cjs.map

1 line
24 KiB
Plaintext
Raw Normal View History

{"version":3,"file":"xxhash-wasm.cjs","sources":["../src/index.js","../src/xxhash.js"],"sourcesContent":["import { xxhash } from \"./xxhash\";\n\n// The .wasm is filled in by the build process, so the user doesn't need to load\n// xxhash.wasm by themselves because it's part of the bundle. Otherwise it\n// couldn't be distributed easily as the user would need to host xxhash.wasm\n// and then fetch it, to be able to use it.\n// eslint-disable-next-line no-undef\nconst wasmBytes = new Uint8Array(WASM_PRECOMPILED_BYTES);\n\nexport default async function () {\n return xxhash((await WebAssembly.instantiate(wasmBytes)).instance);\n}\n","const u32_BYTES = 4;\nconst u64_BYTES = 8;\n\n// The xxh32 hash state struct:\nconst XXH32_STATE_SIZE_BYTES =\n u32_BYTES + // total_len\n u32_BYTES + // large_len\n u32_BYTES * 4 + // Accumulator lanes\n u32_BYTES * 4 + // Internal buffer\n u32_BYTES + // memsize\n u32_BYTES; // reserved\n\n// The xxh64 hash state struct:\nconst XXH64_STATE_SIZE_BYTES =\n u64_BYTES + // total_len\n u64_BYTES * 4 + // Accumulator lanes\n u64_BYTES * 4 + // Internal buffer\n u32_BYTES + // memsize\n u32_BYTES + // reserved32\n u64_BYTES; // reserved64\n\nexport function xxhash(instance) {\n const {\n exports: {\n mem,\n xxh32,\n xxh64,\n init32,\n update32,\n digest32,\n init64,\n update64,\n digest64,\n },\n } = instance;\n\n let memory = new Uint8Array(mem.buffer);\n // Grow the wasm linear memory to accommodate length + offset bytes\n function growMemory(length, offset) {\n if (mem.buffer.byteLength < length + offset) {\n const extraPages = Math.ceil(\n // Wasm pages are spec'd to 64K\n (length + offset - mem.buffer.byteLength) / (64 * 1024),\n );\n mem.grow(extraPages);\n // After growing, the original memory's ArrayBuffer is detached, so we'll\n // need to replace our view over it with a new one over the new backing\n // ArrayBuffer.\n memory = new Uint8Array(mem.buffer);\n }\n }\n\n // The h32 and h64 streaming hash APIs are identical, so we can implement\n // them both by way of a templated call to this generalized function.\n function create(size, seed, init, update, digest, finalize) {\n // Ensure that we've actually got enough space in the wasm memory to store\n // the state blob for this hasher.\n growMemory(size);\n\n // We'll hold our hashing state in this closure.\n const state = new Uint8Array(size);\n memory.set(state);\n init(0, seed);\n\n // Each time we interact with wasm, it may have mutated our state so we'll\n // need to read it back into our closed copy.\n state.set(memory.subarray(0, size));\n\n return {\n update(input) {\n memory.set(state);\n let length;\n if (typeof input === \"string\") {\n growMemory(input.length * 3, size);\n length = encoder.encodeInto(input, memory.subarray(size)).written;\n } else {\n // The only other valid input type is a Uint8Array\n growMemory(input.byteLength, size);\n memory.set(input, size);\n length = input.byteLength;\n }\n update(0, size, length);\n state.set(memory.subarray(0, size));\n return this;\n },\n digest() {\n memory.set(state);\n return finalize(digest(0));\n },\n };\n }\n\n // Logical shift right makes it an u32, otherwise it's interpreted as an i32.\n function forceUnsigned32(i) {\n return i >>> 0;\n }\n\n // BigInts are arbitrary precision and signed, so to get the \"correct\" u64\n // value from the return, we'll need to force that interpretation.\n const u64Max = 2n ** 64n - 1n;\n function forceUnsigned64(i) {\n return i & u64Max;\n }\n\n const encoder = new TextEncoder();\n const defaultSeed = 0;\n const defaultBigSeed = 0n;\n\n function h32(str, seed = defaultSeed) {\n // https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder/encodeInto#buffer_sizing\n // By sizing the buffer to 3 * string-length we gua