Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Loader Utils to TypeScript #235

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions .eslintrc.json
@@ -1,10 +1,15 @@
{
"root": true,
"plugins": ["node"],
"extends": ["eslint:recommended", "plugin:node/recommended"],
"plugins": ["node", "@typescript-eslint"],
"extends": [
"eslint:recommended",
"plugin:node/recommended",
"plugin:@typescript-eslint/recommended"
],
"env": {
"node": true
},
"parser": "@typescript-eslint/parser",
"rules": {
"no-template-curly-in-string": "error",
"no-caller": "error",
Expand All @@ -18,6 +23,12 @@
"no-var": "error",
"prefer-const": "error",
"prefer-arrow-callback": "error",
"object-shorthand": "error"
"object-shorthand": "error",
"node/no-unsupported-features/es-syntax": 0,
"node/no-missing-require": 0,
"node/no-missing-import": 0,
"node/no-unpublished-import": 0,
"@typescript-eslint/no-var-requires": 0,
"@typescript-eslint/no-explicit-any": 0
}
}
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
.idea
node_modules
coverage
/dist
2 changes: 1 addition & 1 deletion .prettierignore
Expand Up @@ -2,4 +2,4 @@
/dist
/node_modules
/test/fixtures
CHANGELOG.md
CHANGELOG.md
72 changes: 50 additions & 22 deletions lib/getHashDigest.js → lib/getHashDigest.ts
@@ -1,4 +1,4 @@
"use strict";
import { Hash } from "crypto";
TheLarkInn marked this conversation as resolved.
Show resolved Hide resolved

const baseEncodeTables = {
26: "abcdefghijklmnopqrstuvwxyz",
Expand All @@ -11,12 +11,23 @@ const baseEncodeTables = {
64: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_",
};

type DigestTypes =
| "base26"
| "base32"
| "base36"
| "base49"
| "base52"
| "base58"
| "base62"
| "base64";
type BaseEncodings = 26 | 32 | 36 | 49 | 52 | 58 | 62 | 64;
TheLarkInn marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param {Uint32Array} uint32Array Treated as a long base-0x100000000 number, little endian
* @param {number} divisor The divisor
* @return {number} Modulo (remainder) of the division
*/
function divmod32(uint32Array, divisor) {
function divmod32(uint32Array: Uint32Array, divisor: number): number {
let carry = 0;
for (let i = uint32Array.length - 1; i >= 0; i--) {
const value = carry * 0x100000000 + uint32Array[i];
Expand All @@ -26,8 +37,12 @@ function divmod32(uint32Array, divisor) {
return carry;
}

function encodeBufferToBase(buffer, base, length) {
const encodeTable = baseEncodeTables[base];
function encodeBufferToBase(
buffer: Buffer,
base: BaseEncodings | number,
TheLarkInn marked this conversation as resolved.
Show resolved Hide resolved
length: number
) {
const encodeTable = baseEncodeTables[base as keyof typeof baseEncodeTables];
TheLarkInn marked this conversation as resolved.
Show resolved Hide resolved

if (!encodeTable) {
throw new Error("Unknown encoding base" + base);
Expand All @@ -54,44 +69,49 @@ function encodeBufferToBase(buffer, base, length) {
return output;
}

let crypto = undefined;
let createXXHash64 = undefined;
let createMd4 = undefined;
let BatchedHash = undefined;
let BulkUpdateDecorator = undefined;

function getHashDigest(buffer, algorithm, digestType, maxLength) {
let crypto: typeof import("crypto");
let createXXHash64: typeof import("./hash/xxhash64").default;
let createMd4: typeof import("./hash/md4").default;
let BatchedHash: typeof import("./hash/BatchedHash").default;
let BulkUpdateDecorator: typeof import("./hash/BulkUpdateDecorator").default;

export default function getHashDigest(
buffer: Buffer,
algorithm: string | "xxhash64" | "md4" | "native-md4",
digestType: DigestTypes | string,
TheLarkInn marked this conversation as resolved.
Show resolved Hide resolved
maxLength: number
) {
algorithm = algorithm || "xxhash64";
maxLength = maxLength || 9999;

let hash;

if (algorithm === "xxhash64") {
if (createXXHash64 === undefined) {
createXXHash64 = require("./hash/xxhash64");
createXXHash64 = require("./hash/xxhash64").default;

if (BatchedHash === undefined) {
BatchedHash = require("./hash/BatchedHash");
BatchedHash = require("./hash/BatchedHash").default;
}
}

hash = new BatchedHash(createXXHash64());
hash = new BatchedHash(createXXHash64() as unknown as Hash);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cast is unfortunate. Can it be elimianted?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createXXHash64() & createMd4() is of type WasmHash and I'm having a hard time understanding the best way to 'coerce' these together to be typed properly. Advice here is welcomed.

} else if (algorithm === "md4") {
if (createMd4 === undefined) {
createMd4 = require("./hash/md4");
createMd4 = require("./hash/md4").default;

if (BatchedHash === undefined) {
BatchedHash = require("./hash/BatchedHash");
BatchedHash = require("./hash/BatchedHash").default;
}
}

hash = new BatchedHash(createMd4());
hash = new BatchedHash(createMd4() as unknown as Hash);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

} else if (algorithm === "native-md4") {
if (typeof crypto === "undefined") {
crypto = require("crypto");

if (BulkUpdateDecorator === undefined) {
BulkUpdateDecorator = require("./hash/BulkUpdateDecorator");
BulkUpdateDecorator = require("./hash/BulkUpdateDecorator").default;
}
}

Expand All @@ -101,7 +121,7 @@ function getHashDigest(buffer, algorithm, digestType, maxLength) {
crypto = require("crypto");

if (BulkUpdateDecorator === undefined) {
BulkUpdateDecorator = require("./hash/BulkUpdateDecorator");
BulkUpdateDecorator = require("./hash/BulkUpdateDecorator").default;
}
}

Expand All @@ -122,10 +142,18 @@ function getHashDigest(buffer, algorithm, digestType, maxLength) {
digestType === "base58" ||
digestType === "base62"
) {
return encodeBufferToBase(hash.digest(), digestType.substr(4), maxLength);
const digestTypeToDigest: number = digestType.substr(
4
) as unknown as number;
TheLarkInn marked this conversation as resolved.
Show resolved Hide resolved

return encodeBufferToBase(
hash.digest() as Buffer,
digestTypeToDigest,
maxLength
);
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the issue here? Can this be eliminated?

return hash.digest(digestType || "hex").substr(0, maxLength);
TheLarkInn marked this conversation as resolved.
Show resolved Hide resolved
}
}

module.exports = getHashDigest;
43 changes: 31 additions & 12 deletions lib/hash/BatchedHash.js → lib/hash/BatchedHash.ts
@@ -1,7 +1,12 @@
const MAX_SHORT_STRING = require("./wasm-hash").MAX_SHORT_STRING;
import type { Hash, Encoding, BinaryToTextEncoding } from "crypto";
import { MAX_SHORT_STRING } from "./wasm-hash";

class BatchedHash {
constructor(hash) {
export default class BatchedHash {
public string?: string;
public encoding?: Encoding;
public readonly hash: Hash;

constructor(hash: Hash) {
this.string = undefined;
this.encoding = undefined;
this.hash = hash;
Expand All @@ -13,7 +18,7 @@ class BatchedHash {
* @param {string=} inputEncoding data encoding
* @returns {this} updated hash
*/
update(data, inputEncoding) {
update(data: string | Buffer, inputEncoding?: Encoding): this {
if (this.string !== undefined) {
if (
typeof data === "string" &&
Expand All @@ -25,7 +30,12 @@ class BatchedHash {
return this;
}

this.hash.update(this.string, this.encoding);
if (this.encoding !== undefined) {
this.hash.update(this.string, this.encoding);
} else {
this.hash.update(this.string);
}

this.string = undefined;
}

Expand All @@ -38,7 +48,11 @@ class BatchedHash {
this.string = data;
this.encoding = inputEncoding;
} else {
this.hash.update(data, inputEncoding);
if (inputEncoding !== undefined) {
this.hash.update(data, inputEncoding);
} else {
this.hash.update(data);
}
}
} else {
this.hash.update(data);
Expand All @@ -52,13 +66,18 @@ class BatchedHash {
* @param {string=} encoding encoding of the return value
* @returns {string|Buffer} digest
*/
digest(encoding) {
digest(encoding?: BinaryToTextEncoding): string | Buffer {
if (this.string !== undefined) {
this.hash.update(this.string, this.encoding);
if (this.encoding !== undefined) {
this.hash.update(this.string, this.encoding);
} else {
this.hash.update(this.string);
}
}
if (encoding !== undefined) {
return this.hash.digest(encoding);
} else {
return this.hash.digest();
}

return this.hash.digest(encoding);
}
}

module.exports = BatchedHash;
44 changes: 31 additions & 13 deletions lib/hash/BulkUpdateDecorator.js → lib/hash/BulkUpdateDecorator.ts
@@ -1,15 +1,23 @@
import type { Hash, Encoding, BinaryToTextEncoding } from "crypto";
type HashOrFactory = Hash | (() => Hash);

const BULK_SIZE = 2000;

// We are using an object instead of a Map as this will stay static during the runtime
// so access to it can be optimized by v8
const digestCaches = {};
const digestCaches: { [key: string]: any } = {};
TheLarkInn marked this conversation as resolved.
Show resolved Hide resolved

class BulkUpdateDecorator {
export default class BulkUpdateDecorator {
TheLarkInn marked this conversation as resolved.
Show resolved Hide resolved
/**
* @param {Hash | function(): Hash} hashOrFactory function to create a hash
* @param {HashOrFactory} hashOrFactory function to create a hash
* @param {string=} hashKey key for caching
TheLarkInn marked this conversation as resolved.
Show resolved Hide resolved
*/
constructor(hashOrFactory, hashKey) {
hash?: Hash;
hashFactory?: () => Hash;
hashKey: string;
buffer: string;

constructor(hashOrFactory: HashOrFactory, hashKey: string) {
this.hashKey = hashKey;

if (typeof hashOrFactory === "function") {
Expand All @@ -29,28 +37,34 @@ class BulkUpdateDecorator {
* @param {string=} inputEncoding data encoding
* @returns {this} updated hash
*/
update(data, inputEncoding) {
update(data: string | Buffer, inputEncoding?: Encoding): this {
if (
inputEncoding !== undefined ||
typeof data !== "string" ||
data.length > BULK_SIZE
) {
if (this.hash === undefined) {
this.hash = this.hashFactory();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.hash = this.hashFactory!();
}

if (this.buffer.length > 0) {
this.hash.update(this.buffer);
this.buffer = "";
}

this.hash.update(data, inputEncoding);
if (inputEncoding === undefined) {
this.hash.update(data);
} else {
this.hash.update(data as string, inputEncoding);
}
} else {
this.buffer += data;

if (this.buffer.length > BULK_SIZE) {
if (this.hash === undefined) {
this.hash = this.hashFactory();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.hash = this.hashFactory!();
}

this.hash.update(this.buffer);
Expand All @@ -66,8 +80,9 @@ class BulkUpdateDecorator {
* @param {string=} encoding encoding of the return value
* @returns {string|Buffer} digest
*/
digest(encoding) {
digest(encoding?: BinaryToTextEncoding): string | Buffer {
let digestCache;
let digestResult: string | Buffer;

const buffer = this.buffer;

Expand All @@ -87,14 +102,19 @@ class BulkUpdateDecorator {
return cacheEntry;
}

this.hash = this.hashFactory();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.hash = this.hashFactory!();
}

if (buffer.length > 0) {
this.hash.update(buffer);
}

const digestResult = this.hash.digest(encoding);
if (encoding !== undefined) {
digestResult = this.hash.digest(encoding);
} else {
digestResult = this.hash.digest();
}

if (digestCache !== undefined) {
digestCache.set(buffer, digestResult);
Expand All @@ -103,5 +123,3 @@ class BulkUpdateDecorator {
return digestResult;
}
}

module.exports = BulkUpdateDecorator;
6 changes: 2 additions & 4 deletions lib/hash/md4.js → lib/hash/md4.ts
Expand Up @@ -3,9 +3,7 @@
Author Tobias Koppers @sokra
*/

"use strict";

const create = require("./wasm-hash");
import { create } from "./wasm-hash";

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

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