Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/getHashDigest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Hash, BinaryToTextEncoding } from "crypto";
import type { BinaryToTextEncoding } from "crypto";

const baseEncodeTables = {
26: "abcdefghijklmnopqrstuvwxyz",
Expand Down Expand Up @@ -95,7 +95,7 @@ export default function getHashDigest(
}
}

hash = new BatchedHash(createXXHash64() as unknown as Hash);
hash = new BatchedHash(createXXHash64());
} else if (algorithm === "md4") {
if (createMd4 === undefined) {
createMd4 = require("./hash/md4").create;
Expand All @@ -105,7 +105,7 @@ export default function getHashDigest(
}
}

hash = new BatchedHash(createMd4() as unknown as Hash);
hash = new BatchedHash(createMd4());
} else if (algorithm === "native-md4") {
if (typeof crypto === "undefined") {
crypto = require("crypto");
Expand Down
11 changes: 8 additions & 3 deletions lib/hash/BatchedHash.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import type { Hash, Encoding, BinaryToTextEncoding } from "crypto";
import type { Encoding, BinaryToTextEncoding } from "crypto";
import { MAX_SHORT_STRING } from "./wasm-hash";

export interface IHashLike {
update(data: string | Buffer, inputEncoding?: Encoding): this;
digest(encoding?: BinaryToTextEncoding): string | Buffer;
}

export class BatchedHash {
public string?: string;
public encoding?: Encoding;
public readonly hash: Hash;
public readonly hash: IHashLike;

constructor(hash: Hash) {
constructor(hash: IHashLike) {
this.string = undefined;
this.encoding = undefined;
this.hash = hash;
Expand Down
15 changes: 10 additions & 5 deletions lib/hash/wasm-hash.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { BinaryToTextEncoding } from "crypto";

import type { IHashLike } from "./BatchedHash";

/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
Expand All @@ -11,7 +13,7 @@ import { BinaryToTextEncoding } from "crypto";
// ~3 makes sure that it's always a block of 4 chars, so avoid partially encoded bytes for base64
export const MAX_SHORT_STRING: number = Math.floor((65536 - 64) / 4) & ~3;

export class WasmHash {
export class WasmHash implements IHashLike {
/**
* @param {WebAssembly.Instance} instance wasm instance
* @param {WebAssembly.Instance[]} instancesPool pool of instances
Expand Down Expand Up @@ -200,20 +202,23 @@ export const create = (
chunkSize: number,
digestSize: number
) => {
let result: WasmHash | undefined;
if (instancesPool.length > 0) {
const old = instancesPool.pop();
result = instancesPool.pop();

// old is possibly undefined
// protect reset call here
old && old.reset();
result?.reset();
}

return old;
} else {
if (result === undefined) {
return new WasmHash(
new WebAssembly.Instance(wasmModule),
instancesPool,
chunkSize,
digestSize
);
}

return result;
};