Skip to content

Commit

Permalink
change default exports for classes to just regular exports for compat
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLarkInn committed Dec 23, 2022
1 parent 1b3706a commit b9a9336
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 43 deletions.
22 changes: 12 additions & 10 deletions lib/getHashDigest.ts
Expand Up @@ -62,10 +62,10 @@ function encodeBufferToBase(
}

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;
let createXXHash64: typeof import("./hash/xxhash64").create;
let createMd4: typeof import("./hash/md4").create;
let BatchedHash: typeof import("./hash/BatchedHash").BatchedHash;
let BulkUpdateDecorator: typeof import("./hash/BulkUpdateDecorator").BulkUpdateDecorator;

export default function getHashDigest(
buffer: Buffer,
Expand All @@ -80,20 +80,20 @@ export default function getHashDigest(

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

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

hash = new BatchedHash(createXXHash64() as unknown as Hash);
} else if (algorithm === "md4") {
if (createMd4 === undefined) {
createMd4 = require("./hash/md4").default;
createMd4 = require("./hash/md4").create;

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

Expand All @@ -103,7 +103,8 @@ export default function getHashDigest(
crypto = require("crypto");

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

Expand All @@ -113,7 +114,8 @@ export default function getHashDigest(
crypto = require("crypto");

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

Expand Down
15 changes: 3 additions & 12 deletions lib/hash/BatchedHash.ts
@@ -1,7 +1,7 @@
import type { Hash, Encoding, BinaryToTextEncoding } from "crypto";
import { MAX_SHORT_STRING } from "./wasm-hash";

export default class BatchedHash {
export class BatchedHash {
public string?: string;
public encoding?: Encoding;
public readonly hash: Hash;
Expand All @@ -12,12 +12,7 @@ export default class BatchedHash {
this.hash = hash;
}

/**
* Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
* @param {string|Buffer} data data
* @param {string=} inputEncoding data encoding
* @returns {this} updated hash
*/
// Updates the hash https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding
update(data: string | Buffer, inputEncoding?: Encoding): this {
if (this.string !== undefined) {
if (
Expand Down Expand Up @@ -61,11 +56,7 @@ export default class BatchedHash {
return this;
}

/**
* Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
* @param {string=} encoding encoding of the return value
* @returns {string|Buffer} digest
*/
// Calculates the digest https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding
digest(encoding?: BinaryToTextEncoding): string | Buffer {
if (this.string !== undefined) {
if (this.encoding !== undefined) {
Expand Down
21 changes: 4 additions & 17 deletions lib/hash/BulkUpdateDecorator.ts
Expand Up @@ -5,13 +5,9 @@ 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: { [key: string]: any } = {};
const digestCaches: Record<string, any> = {};

export default class BulkUpdateDecorator {
/**
* @param {HashOrFactory} hashOrFactory function to create a hash
* @param {string=} hashKey key for caching
*/
export class BulkUpdateDecorator {
hash?: Hash;
hashFactory?: () => Hash;
hashKey: string;
Expand All @@ -31,12 +27,7 @@ export default class BulkUpdateDecorator {
this.buffer = "";
}

/**
* Update hash {@link https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding}
* @param {string|Buffer} data data
* @param {string=} inputEncoding data encoding
* @returns {this} updated hash
*/
// Updates the hash https://nodejs.org/api/crypto.html#crypto_hash_update_data_inputencoding
update(data: string | Buffer, inputEncoding?: Encoding): this {
if (
inputEncoding !== undefined ||
Expand Down Expand Up @@ -75,11 +66,7 @@ export default class BulkUpdateDecorator {
return this;
}

/**
* Calculates the digest {@link https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding}
* @param {string=} encoding encoding of the return value
* @returns {string|Buffer} digest
*/
// Calculates the digest https://nodejs.org/api/crypto.html#crypto_hash_digest_encoding
digest(encoding?: BinaryToTextEncoding): string | Buffer {
let digestCache;
let digestResult: string | Buffer;
Expand Down
4 changes: 2 additions & 2 deletions lib/hash/md4.ts
Expand Up @@ -3,7 +3,7 @@
Author Tobias Koppers @sokra
*/

import { create } from "./wasm-hash";
import { create as createFn } from "./wasm-hash";

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

export default create.bind(null, md4, [], 64, 32);
export const create = createFn.bind(null, md4, [], 64, 32);
4 changes: 2 additions & 2 deletions lib/hash/xxhash64.ts
Expand Up @@ -2,7 +2,7 @@
MIT License http://www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @sokra
*/
import { create } from "./wasm-hash";
import { create as createFn } from "./wasm-hash";

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

export default create.bind(null, xxhash64, [], 32, 16);
export const create = createFn.bind(null, xxhash64, [], 32, 16);

0 comments on commit b9a9336

Please sign in to comment.