-
-
Notifications
You must be signed in to change notification settings - Fork 187
Migrate Loader Utils to TypeScript #235
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
0e41a7c
abceae4
fd6ceac
f2db90e
1b3706a
b9a9336
ec2d6a0
8216851
c1d8c6e
54eb6d7
9e88813
95c938d
3505c4a
500cb37
dca41c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.idea | ||
node_modules | ||
coverage | ||
/dist |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
/dist | ||
/node_modules | ||
/test/fixtures | ||
CHANGELOG.md | ||
CHANGELOG.md |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
"use strict"; | ||
import { Hash } from "crypto"; | ||
|
||
const baseEncodeTables = { | ||
26: "abcdefghijklmnopqrstuvwxyz", | ||
|
@@ -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]; | ||
|
@@ -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); | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This cast is unfortunate. Can it be elimianted? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} | ||
|
||
|
@@ -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; | ||
} | ||
} | ||
|
||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
Uh oh!
There was an error while loading. Please reload this page.