File tree Expand file tree Collapse file tree 3 files changed +21
-11
lines changed Expand file tree Collapse file tree 3 files changed +21
-11
lines changed Original file line number Diff line number Diff line change 1
- import type { Hash , BinaryToTextEncoding } from "crypto" ;
1
+ import type { BinaryToTextEncoding } from "crypto" ;
2
2
3
3
const baseEncodeTables = {
4
4
26 : "abcdefghijklmnopqrstuvwxyz" ,
@@ -95,7 +95,7 @@ export default function getHashDigest(
95
95
}
96
96
}
97
97
98
- hash = new BatchedHash ( createXXHash64 ( ) as unknown as Hash ) ;
98
+ hash = new BatchedHash ( createXXHash64 ( ) ) ;
99
99
} else if ( algorithm === "md4" ) {
100
100
if ( createMd4 === undefined ) {
101
101
createMd4 = require ( "./hash/md4" ) . create ;
@@ -105,7 +105,7 @@ export default function getHashDigest(
105
105
}
106
106
}
107
107
108
- hash = new BatchedHash ( createMd4 ( ) as unknown as Hash ) ;
108
+ hash = new BatchedHash ( createMd4 ( ) ) ;
109
109
} else if ( algorithm === "native-md4" ) {
110
110
if ( typeof crypto === "undefined" ) {
111
111
crypto = require ( "crypto" ) ;
Original file line number Diff line number Diff line change 1
- import type { Hash , Encoding , BinaryToTextEncoding } from "crypto" ;
1
+ import type { Encoding , BinaryToTextEncoding } from "crypto" ;
2
2
import { MAX_SHORT_STRING } from "./wasm-hash" ;
3
3
4
+ export interface IHashLike {
5
+ update ( data : string | Buffer , inputEncoding ?: Encoding ) : this;
6
+ digest ( encoding ?: BinaryToTextEncoding ) : string | Buffer ;
7
+ }
8
+
4
9
export class BatchedHash {
5
10
public string ?: string ;
6
11
public encoding ?: Encoding ;
7
- public readonly hash : Hash ;
12
+ public readonly hash : IHashLike ;
8
13
9
- constructor ( hash : Hash ) {
14
+ constructor ( hash : IHashLike ) {
10
15
this . string = undefined ;
11
16
this . encoding = undefined ;
12
17
this . hash = hash ;
Original file line number Diff line number Diff line change 1
1
import { BinaryToTextEncoding } from "crypto" ;
2
2
3
+ import type { IHashLike } from "./BatchedHash" ;
4
+
3
5
/*
4
6
MIT License http://www.opensource.org/licenses/mit-license.php
5
7
Author Tobias Koppers @sokra
@@ -11,7 +13,7 @@ import { BinaryToTextEncoding } from "crypto";
11
13
// ~3 makes sure that it's always a block of 4 chars, so avoid partially encoded bytes for base64
12
14
export const MAX_SHORT_STRING : number = Math . floor ( ( 65536 - 64 ) / 4 ) & ~ 3 ;
13
15
14
- export class WasmHash {
16
+ export class WasmHash implements IHashLike {
15
17
/**
16
18
* @param {WebAssembly.Instance } instance wasm instance
17
19
* @param {WebAssembly.Instance[] } instancesPool pool of instances
@@ -200,20 +202,23 @@ export const create = (
200
202
chunkSize : number ,
201
203
digestSize : number
202
204
) => {
205
+ let result : WasmHash | undefined ;
203
206
if ( instancesPool . length > 0 ) {
204
- const old = instancesPool . pop ( ) ;
207
+ result = instancesPool . pop ( ) ;
205
208
206
209
// old is possibly undefined
207
210
// protect reset call here
208
- old && old . reset ( ) ;
211
+ result ?. reset ( ) ;
212
+ }
209
213
210
- return old ;
211
- } else {
214
+ if ( result === undefined ) {
212
215
return new WasmHash (
213
216
new WebAssembly . Instance ( wasmModule ) ,
214
217
instancesPool ,
215
218
chunkSize ,
216
219
digestSize
217
220
) ;
218
221
}
222
+
223
+ return result ;
219
224
} ;
You can’t perform that action at this time.
0 commit comments