1- "use strict" ;
1+ import { Hash } from 'crypto' ;
22
33const baseEncodeTables = {
44 26 : "abcdefghijklmnopqrstuvwxyz" ,
@@ -11,12 +11,15 @@ const baseEncodeTables = {
1111 64 : "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_" ,
1212} ;
1313
14+ type DigestTypes = "base26" | "base32" | "base36" | "base49" | "base52" | "base58" | "base62" | "base64" ;
15+ type BaseEncodings = 26 | 32 | 36 | 49 | 52 | 58 | 62 | 64 ;
16+
1417/**
1518 * @param {Uint32Array } uint32Array Treated as a long base-0x100000000 number, little endian
1619 * @param {number } divisor The divisor
1720 * @return {number } Modulo (remainder) of the division
1821 */
19- function divmod32 ( uint32Array , divisor ) {
22+ function divmod32 ( uint32Array : Uint32Array , divisor : number ) : number {
2023 let carry = 0 ;
2124 for ( let i = uint32Array . length - 1 ; i >= 0 ; i -- ) {
2225 const value = carry * 0x100000000 + uint32Array [ i ] ;
@@ -26,8 +29,8 @@ function divmod32(uint32Array, divisor) {
2629 return carry ;
2730}
2831
29- function encodeBufferToBase ( buffer , base , length ) {
30- const encodeTable = baseEncodeTables [ base ] ;
32+ function encodeBufferToBase ( buffer : Buffer , base : BaseEncodings | number , length : number ) {
33+ const encodeTable = baseEncodeTables [ ( base as keyof typeof baseEncodeTables ) ] ;
3134
3235 if ( ! encodeTable ) {
3336 throw new Error ( "Unknown encoding base" + base ) ;
@@ -54,13 +57,13 @@ function encodeBufferToBase(buffer, base, length) {
5457 return output ;
5558}
5659
57- let crypto = undefined ;
58- let createXXHash64 = undefined ;
59- let createMd4 = undefined ;
60- let BatchedHash = undefined ;
61- let BulkUpdateDecorator = undefined ;
60+ let crypto : typeof import ( 'crypto' )
61+ let createXXHash64 : typeof import ( './hash/xxhash64' ) . default ;
62+ let createMd4 : typeof import ( './hash/md4' ) . default ;
63+ let BatchedHash : typeof import ( './hash/BatchedHash' ) . default ;
64+ let BulkUpdateDecorator : typeof import ( './hash/BulkUpdateDecorator' ) . default ;
6265
63- function getHashDigest ( buffer , algorithm , digestType , maxLength ) {
66+ export default function getHashDigest ( buffer : Buffer , algorithm : string | "xxhash64" | "md4" | "native-md4" , digestType : DigestTypes | string , maxLength : number ) {
6467 algorithm = algorithm || "xxhash64" ;
6568 maxLength = maxLength || 9999 ;
6669
@@ -75,7 +78,7 @@ function getHashDigest(buffer, algorithm, digestType, maxLength) {
7578 }
7679 }
7780
78- hash = new BatchedHash ( createXXHash64 ( ) ) ;
81+ hash = new BatchedHash ( createXXHash64 ( ) as unknown as Hash ) ;
7982 } else if ( algorithm === "md4" ) {
8083 if ( createMd4 === undefined ) {
8184 createMd4 = require ( "./hash/md4" ) ;
@@ -85,7 +88,7 @@ function getHashDigest(buffer, algorithm, digestType, maxLength) {
8588 }
8689 }
8790
88- hash = new BatchedHash ( createMd4 ( ) ) ;
91+ hash = new BatchedHash ( createMd4 ( ) as unknown as Hash ) ;
8992 } else if ( algorithm === "native-md4" ) {
9093 if ( typeof crypto === "undefined" ) {
9194 crypto = require ( "crypto" ) ;
@@ -122,10 +125,11 @@ function getHashDigest(buffer, algorithm, digestType, maxLength) {
122125 digestType === "base58" ||
123126 digestType === "base62"
124127 ) {
125- return encodeBufferToBase ( hash . digest ( ) , digestType . substr ( 4 ) , maxLength ) ;
128+ const digestTypeToDigest : number = digestType . substr ( 4 ) as unknown as number ;
129+
130+ return encodeBufferToBase ( hash . digest ( ) as Buffer , digestTypeToDigest , maxLength ) ;
126131 } else {
132+ // @ts -ignore
127133 return hash . digest ( digestType || "hex" ) . substr ( 0 , maxLength ) ;
128134 }
129135}
130-
131- module . exports = getHashDigest ;
0 commit comments