Skip to content

Commit c1d8c6e

Browse files
committed
Clean up some hash typings.
1 parent 8216851 commit c1d8c6e

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

lib/getHashDigest.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Hash, BinaryToTextEncoding } from "crypto";
1+
import type { BinaryToTextEncoding } from "crypto";
22

33
const baseEncodeTables = {
44
26: "abcdefghijklmnopqrstuvwxyz",
@@ -95,7 +95,7 @@ export default function getHashDigest(
9595
}
9696
}
9797

98-
hash = new BatchedHash(createXXHash64() as unknown as Hash);
98+
hash = new BatchedHash(createXXHash64());
9999
} else if (algorithm === "md4") {
100100
if (createMd4 === undefined) {
101101
createMd4 = require("./hash/md4").create;
@@ -105,7 +105,7 @@ export default function getHashDigest(
105105
}
106106
}
107107

108-
hash = new BatchedHash(createMd4() as unknown as Hash);
108+
hash = new BatchedHash(createMd4());
109109
} else if (algorithm === "native-md4") {
110110
if (typeof crypto === "undefined") {
111111
crypto = require("crypto");

lib/hash/BatchedHash.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
import type { Hash, Encoding, BinaryToTextEncoding } from "crypto";
1+
import type { Encoding, BinaryToTextEncoding } from "crypto";
22
import { MAX_SHORT_STRING } from "./wasm-hash";
33

4+
export interface IHashLike {
5+
update(data: string | Buffer, inputEncoding?: Encoding): this;
6+
digest(encoding?: BinaryToTextEncoding): string | Buffer;
7+
}
8+
49
export class BatchedHash {
510
public string?: string;
611
public encoding?: Encoding;
7-
public readonly hash: Hash;
12+
public readonly hash: IHashLike;
813

9-
constructor(hash: Hash) {
14+
constructor(hash: IHashLike) {
1015
this.string = undefined;
1116
this.encoding = undefined;
1217
this.hash = hash;

lib/hash/wasm-hash.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { BinaryToTextEncoding } from "crypto";
22

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

14-
export class WasmHash {
16+
export class WasmHash implements IHashLike {
1517
/**
1618
* @param {WebAssembly.Instance} instance wasm instance
1719
* @param {WebAssembly.Instance[]} instancesPool pool of instances
@@ -200,20 +202,23 @@ export const create = (
200202
chunkSize: number,
201203
digestSize: number
202204
) => {
205+
let result: WasmHash | undefined;
203206
if (instancesPool.length > 0) {
204-
const old = instancesPool.pop();
207+
result = instancesPool.pop();
205208

206209
// old is possibly undefined
207210
// protect reset call here
208-
old && old.reset();
211+
result?.reset();
212+
}
209213

210-
return old;
211-
} else {
214+
if (result === undefined) {
212215
return new WasmHash(
213216
new WebAssembly.Instance(wasmModule),
214217
instancesPool,
215218
chunkSize,
216219
digestSize
217220
);
218221
}
222+
223+
return result;
219224
};

0 commit comments

Comments
 (0)