-
Notifications
You must be signed in to change notification settings - Fork 55
feat: support truncating digests #329
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
Changes from 3 commits
64a3d68
b55d072
d42373b
161d781
2a32fe7
01ae0bb
cba078d
71db8d2
f250f45
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 |
---|---|---|
|
@@ -3,8 +3,35 @@ import type { MultihashHasher } from './interface.js' | |
|
||
type Await<T> = Promise<T> | T | ||
|
||
export function from <Name extends string, Code extends number> ({ name, code, encode }: { name: Name, code: Code, encode(input: Uint8Array): Await<Uint8Array> }): Hasher<Name, Code> { | ||
return new Hasher(name, code, encode) | ||
const DEFAULT_MIN_DIGEST_LENGTH = 20 | ||
const DEFAULT_MAX_DIGEST_LENGTH = 128 | ||
|
||
export interface HasherInit <Name extends string, Code extends number> { | ||
name: Name | ||
code: Code | ||
encode(input: Uint8Array): Await<Uint8Array> | ||
|
||
/** | ||
* The minimum length a hash is allowed to be in bytes | ||
* | ||
* @default 20 | ||
*/ | ||
minDigestLength?: number | ||
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. Without any other customization will this break small identity multihashes? 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. shouldn't do, unless you 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. Turns out the types wouldn't allow passing options to |
||
|
||
/** | ||
* The maximum length a hash is allowed to be in bytes | ||
* | ||
* @default 128 | ||
*/ | ||
maxDigestLength?: number | ||
} | ||
|
||
export function from <Name extends string, Code extends number> ({ name, code, encode, minDigestLength, maxDigestLength }: HasherInit<Name, Code>): Hasher<Name, Code> { | ||
return new Hasher(name, code, encode, minDigestLength, maxDigestLength) | ||
} | ||
|
||
export interface DigestOptions { | ||
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. I'd add the unit type in the JS doc so it's clear 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. I've added a doc comment, let me know if you think it's clear. |
||
truncate?: number | ||
} | ||
|
||
/** | ||
|
@@ -15,23 +42,55 @@ export class Hasher<Name extends string, Code extends number> implements Multiha | |
readonly name: Name | ||
readonly code: Code | ||
readonly encode: (input: Uint8Array) => Await<Uint8Array> | ||
readonly minDigestLength: number | ||
readonly maxDigestLength: number | ||
|
||
constructor (name: Name, code: Code, encode: (input: Uint8Array) => Await<Uint8Array>) { | ||
constructor (name: Name, code: Code, encode: (input: Uint8Array) => Await<Uint8Array>, minDigestLength?: number, maxDigestLength?: number) { | ||
this.name = name | ||
this.code = code | ||
this.encode = encode | ||
this.minDigestLength = minDigestLength ?? DEFAULT_MIN_DIGEST_LENGTH | ||
this.maxDigestLength = maxDigestLength ?? DEFAULT_MAX_DIGEST_LENGTH | ||
} | ||
|
||
digest (input: Uint8Array): Await<Digest.Digest<Code, number>> { | ||
digest (input: Uint8Array, options?: DigestOptions): Await<Digest.Digest<Code, number>> { | ||
if (options?.truncate != null) { | ||
if (options.truncate < this.minDigestLength) { | ||
throw new Error(`Invalid truncate option, must be greater than or equal to ${this.minDigestLength}`) | ||
} | ||
|
||
if (options.truncate > this.maxDigestLength) { | ||
throw new Error(`Invalid truncate option, must be less than or equal to ${this.maxDigestLength}`) | ||
} | ||
} | ||
|
||
if (input instanceof Uint8Array) { | ||
const result = this.encode(input) | ||
return result instanceof Uint8Array | ||
? Digest.create(this.code, result) | ||
/* c8 ignore next 1 */ | ||
: result.then(digest => Digest.create(this.code, digest)) | ||
|
||
if (result instanceof Uint8Array) { | ||
return createDigest(result, this.code, options?.truncate) | ||
} | ||
|
||
return result.then(digest => createDigest(digest, this.code, options?.truncate)) | ||
} else { | ||
throw Error('Unknown type, must be binary type') | ||
/* c8 ignore next 1 */ | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Create a Digest from the passed uint8array and code, optionally truncating it | ||
* first. | ||
*/ | ||
function createDigest <Code extends number> (digest: Uint8Array, code: Code, truncate?: number): Digest.Digest<Code, number> { | ||
if (truncate != null && truncate !== digest.byteLength) { | ||
if (truncate > digest.byteLength) { | ||
throw new Error(`Invalid truncate option, must be less than or equal to ${digest.byteLength}`) | ||
} | ||
|
||
digest = digest.subarray(0, truncate) | ||
} | ||
|
||
return Digest.create(code, digest) | ||
} |
Uh oh!
There was an error while loading. Please reload this page.