Skip to content

Commit 0e41a7c

Browse files
committed
introduce typescript
1 parent c382671 commit 0e41a7c

17 files changed

+697
-158
lines changed

.eslintrc.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"root": true,
3-
"plugins": ["node"],
4-
"extends": ["eslint:recommended", "plugin:node/recommended"],
3+
"plugins": ["node", "@typescript-eslint"],
4+
"extends": ["eslint:recommended", "plugin:node/recommended", "plugin:@typescript-eslint/recommended"],
55
"env": {
66
"node": true
77
},
8+
"parser": "@typescript-eslint/parser",
89
"rules": {
910
"no-template-curly-in-string": "error",
1011
"no-caller": "error",
@@ -18,6 +19,7 @@
1819
"no-var": "error",
1920
"prefer-const": "error",
2021
"prefer-arrow-callback": "error",
21-
"object-shorthand": "error"
22+
"object-shorthand": "error",
23+
"es-syntax": "false"
2224
}
2325
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
22
node_modules
33
coverage
4+
/dist

lib/getHashDigest.js renamed to lib/getHashDigest.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"use strict";
1+
import {Hash} from 'crypto';
22

33
const 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;

lib/hash/BatchedHash.js renamed to lib/hash/BatchedHash.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
const MAX_SHORT_STRING = require("./wasm-hash").MAX_SHORT_STRING;
1+
import type { Hash, Encoding, BinaryToTextEncoding } from "crypto";
2+
import { MAX_SHORT_STRING } from './wasm-hash'
23

3-
class BatchedHash {
4-
constructor(hash) {
4+
export default class BatchedHash {
5+
public string?: string;
6+
public encoding?: Encoding;
7+
public readonly hash: Hash;
8+
9+
constructor(hash: Hash) {
510
this.string = undefined;
611
this.encoding = undefined;
712
this.hash = hash;
@@ -13,7 +18,7 @@ class BatchedHash {
1318
* @param {string=} inputEncoding data encoding
1419
* @returns {this} updated hash
1520
*/
16-
update(data, inputEncoding) {
21+
update(data: string | Buffer, inputEncoding?: Encoding): this {
1722
if (this.string !== undefined) {
1823
if (
1924
typeof data === "string" &&
@@ -25,7 +30,12 @@ class BatchedHash {
2530
return this;
2631
}
2732

28-
this.hash.update(this.string, this.encoding);
33+
if (this.encoding !== undefined) {
34+
this.hash.update(this.string, this.encoding);
35+
} else {
36+
this.hash.update(this.string)
37+
}
38+
2939
this.string = undefined;
3040
}
3141

@@ -38,7 +48,11 @@ class BatchedHash {
3848
this.string = data;
3949
this.encoding = inputEncoding;
4050
} else {
41-
this.hash.update(data, inputEncoding);
51+
if (inputEncoding !== undefined) {
52+
this.hash.update(data, inputEncoding);
53+
} else {
54+
this.hash.update(data);
55+
}
4256
}
4357
} else {
4458
this.hash.update(data);
@@ -52,13 +66,19 @@ class BatchedHash {
5266
* @param {string=} encoding encoding of the return value
5367
* @returns {string|Buffer} digest
5468
*/
55-
digest(encoding) {
69+
digest(encoding?: BinaryToTextEncoding): string | Buffer {
5670
if (this.string !== undefined) {
57-
this.hash.update(this.string, this.encoding);
58-
}
71+
if (this.encoding !== undefined) {
72+
this.hash.update(this.string, this.encoding);
73+
} else {
74+
this.hash.update(this.string);
75+
}
5976

60-
return this.hash.digest(encoding);
77+
}
78+
if (encoding !== undefined) {
79+
return this.hash.digest(encoding);
80+
} else {
81+
return this.hash.digest();
82+
}
6183
}
6284
}
63-
64-
module.exports = BatchedHash;

lib/hash/BulkUpdateDecorator.js renamed to lib/hash/BulkUpdateDecorator.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1-
const BULK_SIZE = 2000;
1+
import type { Hash, Encoding, BinaryToTextEncoding } from "crypto";
2+
type HashOrFactory = Hash | (() => Hash);
3+
4+
const BULK_SIZE: number = 2000;
25

36
// We are using an object instead of a Map as this will stay static during the runtime
47
// so access to it can be optimized by v8
5-
const digestCaches = {};
8+
const digestCaches: {[key: string]: any} = {};
9+
610

7-
class BulkUpdateDecorator {
11+
export default class BulkUpdateDecorator {
812
/**
9-
* @param {Hash | function(): Hash} hashOrFactory function to create a hash
13+
* @param {HashOrFactory} hashOrFactory function to create a hash
1014
* @param {string=} hashKey key for caching
1115
*/
12-
constructor(hashOrFactory, hashKey) {
16+
hash?: Hash;
17+
hashFactory?: (() => Hash);
18+
hashKey: string;
19+
buffer: string;
20+
21+
constructor(hashOrFactory: HashOrFactory, hashKey: string) {
1322
this.hashKey = hashKey;
1423

1524
if (typeof hashOrFactory === "function") {
@@ -29,28 +38,32 @@ class BulkUpdateDecorator {
2938
* @param {string=} inputEncoding data encoding
3039
* @returns {this} updated hash
3140
*/
32-
update(data, inputEncoding) {
41+
update(data: string | Buffer, inputEncoding?: Encoding): this {
3342
if (
3443
inputEncoding !== undefined ||
3544
typeof data !== "string" ||
3645
data.length > BULK_SIZE
3746
) {
3847
if (this.hash === undefined) {
39-
this.hash = this.hashFactory();
48+
this.hash = this.hashFactory!();
4049
}
4150

4251
if (this.buffer.length > 0) {
4352
this.hash.update(this.buffer);
4453
this.buffer = "";
4554
}
4655

47-
this.hash.update(data, inputEncoding);
56+
if (inputEncoding === undefined) {
57+
this.hash.update(data);
58+
} else {
59+
this.hash.update(data as string, inputEncoding);
60+
}
4861
} else {
4962
this.buffer += data;
5063

5164
if (this.buffer.length > BULK_SIZE) {
5265
if (this.hash === undefined) {
53-
this.hash = this.hashFactory();
66+
this.hash = this.hashFactory!();
5467
}
5568

5669
this.hash.update(this.buffer);
@@ -66,8 +79,9 @@ class BulkUpdateDecorator {
6679
* @param {string=} encoding encoding of the return value
6780
* @returns {string|Buffer} digest
6881
*/
69-
digest(encoding) {
82+
digest(encoding?: BinaryToTextEncoding): string | Buffer {
7083
let digestCache;
84+
let digestResult: string | Buffer;
7185

7286
const buffer = this.buffer;
7387

@@ -87,14 +101,18 @@ class BulkUpdateDecorator {
87101
return cacheEntry;
88102
}
89103

90-
this.hash = this.hashFactory();
104+
this.hash = this.hashFactory!();
91105
}
92106

93107
if (buffer.length > 0) {
94108
this.hash.update(buffer);
95109
}
96110

97-
const digestResult = this.hash.digest(encoding);
111+
if (encoding !== undefined) {
112+
digestResult = this.hash.digest(encoding);
113+
} else {
114+
digestResult = this.hash.digest();
115+
}
98116

99117
if (digestCache !== undefined) {
100118
digestCache.set(buffer, digestResult);
@@ -103,5 +121,3 @@ class BulkUpdateDecorator {
103121
return digestResult;
104122
}
105123
}
106-
107-
module.exports = BulkUpdateDecorator;

lib/hash/md4.js renamed to lib/hash/md4.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
Author Tobias Koppers @sokra
44
*/
55

6-
"use strict";
7-
8-
const create = require("./wasm-hash");
6+
import { create } from './wasm-hash';
97

108
//#region wasm code: md4 (../../../assembly/hash/md4.asm.ts) --initialMemory 1
119
const md4 = new WebAssembly.Module(
@@ -17,4 +15,4 @@ const md4 = new WebAssembly.Module(
1715
);
1816
//#endregion
1917

20-
module.exports = create.bind(null, md4, [], 64, 32);
18+
export default create.bind(null, md4, [], 64, 32);

0 commit comments

Comments
 (0)