| | |
| | | // ~3 makes sure that it's always a block of 4 chars, so avoid partially encoded bytes for base64 |
| | | const MAX_SHORT_STRING = Math.floor((65536 - 64) / 4) & ~3; |
| | | |
| | | /** |
| | | * Represents the wasm hash runtime component. |
| | | * @typedef {object} WasmExports |
| | | * @property {WebAssembly.Memory} memory |
| | | * @property {() => void} init |
| | | * @property {(length: number) => void} update |
| | | * @property {(length: number) => void} final |
| | | */ |
| | | |
| | | class WasmHash extends Hash { |
| | | /** |
| | | * Creates an instance of WasmHash. |
| | | * @param {WebAssembly.Instance} instance wasm instance |
| | | * @param {WebAssembly.Instance[]} instancesPool pool of instances |
| | | * @param {number} chunkSize size of data chunks passed to wasm |
| | |
| | | constructor(instance, instancesPool, chunkSize, digestSize) { |
| | | super(); |
| | | |
| | | const exports = /** @type {EXPECTED_ANY} */ (instance.exports); |
| | | const exports = /** @type {WasmExports} */ (instance.exports); |
| | | exports.init(); |
| | | /** @type {WasmExports} */ |
| | | this.exports = exports; |
| | | /** @type {Buffer} */ |
| | | this.mem = Buffer.from(exports.memory.buffer, 0, 65536); |
| | | /** @type {number} */ |
| | | this.buffered = 0; |
| | | /** @type {WebAssembly.Instance[]} */ |
| | | this.instancesPool = instancesPool; |
| | | /** @type {number} */ |
| | | this.chunkSize = chunkSize; |
| | | /** @type {number} */ |
| | | this.digestSize = digestSize; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * Update with short string. |
| | | * @param {string} data data |
| | | * @param {BufferEncoding=} encoding encoding |
| | | * @returns {void} |
| | | */ |
| | | _updateWithShortString(data, encoding) { |
| | | const { exports, buffered, mem, chunkSize } = this; |
| | | /** @type {number} */ |
| | | let endPos; |
| | | if (data.length < 70) { |
| | | // eslint-disable-next-line unicorn/text-encoding-identifier-case |
| | |
| | | } |
| | | |
| | | /** |
| | | * Update with buffer. |
| | | * @param {Buffer} data data |
| | | * @returns {void} |
| | | */ |
| | |
| | | } |
| | | |
| | | /** |
| | | * Returns wasm hash. |
| | | * @param {WebAssembly.Module} wasmModule wasm module |
| | | * @param {WasmHash[]} instancesPool pool of instances |
| | | * @param {number} chunkSize size of data chunks passed to wasm |
| | |
| | | ); |
| | | }; |
| | | |
| | | create.MAX_SHORT_STRING = MAX_SHORT_STRING; |
| | | |
| | | module.exports = create; |
| | | module.exports.MAX_SHORT_STRING = MAX_SHORT_STRING; |