Skip to content

Commit

Permalink
Added CRC64 hash class
Browse files Browse the repository at this point in the history
  • Loading branch information
JrMasterModelBuilder committed May 3, 2024
1 parent 49f7537 commit 5968911
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins:
- eslint-plugin-import

parserOptions:
ecmaVersion: 13
ecmaVersion: latest
sourceType: module

extends:
Expand All @@ -14,6 +14,7 @@ env:
es6: true
commonjs: true
node: true
es2020: true

rules:
# Possible Errors:
Expand Down
22 changes: 18 additions & 4 deletions util/hash.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,25 @@ import {createHash} from 'node:crypto';
import {Transform, Writable} from 'node:stream';
import {pipeline} from 'node:stream/promises';

import {Crc64} from './crc64.mjs';

Check failure on line 8 in util/hash.mjs

View workflow job for this annotation

GitHub Actions / build

Unable to resolve path to module './crc64.mjs'

function hasher(algo) {
switch (algo) {
case 'crc64': {
return new Crc64();
}
default: {
// Do nothing.
}
}
return createHash(algo);
}

export class HashWritable extends Writable {
constructor(algos) {
super();

this._hashers = algos.map(algo => createHash(algo));
this._hashers = algos.map(hasher);
}

digests(encoding = null) {
Expand All @@ -21,7 +35,7 @@ export class HashWritable extends Writable {

_write(chunk, encoding, callback) {
for (const hasher of this._hashers) {
hasher.update(chunk, encoding);
hasher.update(Buffer.from(chunk, encoding));
}
callback();
}
Expand All @@ -31,7 +45,7 @@ export class HashTransform extends Transform {
constructor(algos) {
super();

this._hashers = algos.map(algo => createHash(algo));
this._hashers = algos.map(hasher);
}

digests(encoding = null) {
Expand All @@ -43,7 +57,7 @@ export class HashTransform extends Transform {

_transform(chunk, encoding, callback) {
for (const hasher of this._hashers) {
hasher.update(chunk, encoding);
hasher.update(Buffer.from(chunk, encoding));
}
this.push(chunk, encoding);
callback();
Expand Down

0 comments on commit 5968911

Please sign in to comment.