99// a bunch of places that parse it to extract (code, digest, size). By creating
1010// this first class representation we avoid reparsing and things generally fit
1111// really nicely.
12- export interface MultihashDigest {
12+ export interface MultihashDigest < Code extends number = number > {
1313 /**
1414 * Code of the multihash
1515 */
16- code : number
16+ code : Code
1717
1818 /**
1919 * Raw digest (without a hashing algorithm info)
@@ -35,20 +35,38 @@ export interface MultihashDigest {
3535 * Hasher represents a hashing algorithm implementation that produces as
3636 * `MultihashDigest`.
3737 */
38- export interface MultihashHasher {
38+ export interface MultihashHasher < Code extends number = number > {
3939 /**
40- * Takes binary `input` and returns it (multi) hash digest.
40+ * Takes binary `input` and returns it (multi) hash digest. Return value is
41+ * either promise of a digest or a digest. This way general use can `await`
42+ * while performance critical code may asses return value to decide whether
43+ * await is needed.
44+ *
4145 * @param {Uint8Array } input
4246 */
43- digest ( input : Uint8Array ) : Promise < MultihashDigest >
47+ digest ( input : Uint8Array ) : Promise < MultihashDigest > | MultihashDigest
4448
4549 /**
4650 * Name of the multihash
4751 */
48- name : string
52+ name : string
4953
5054 /**
5155 * Code of the multihash
5256 */
53- code : number
57+ code : Code
58+ }
59+
60+ /**
61+ * Sync variant of `MultihashHasher` that refines return type of the `digest`
62+ * to `MultihashDigest`. It is subtype of `MultihashHasher` so implementations
63+ * of this interface can be passed anywhere `MultihashHasher` is expected,
64+ * allowing consumer to either `await` or check the return type to decide
65+ * whether to await or proceed with return value.
66+ *
67+ * `SyncMultihashHasher` is useful in certain APIs where async hashing would be
68+ * impractical e.g. implementation of Hash Array Mapped Trie (HAMT).
69+ */
70+ export interface SyncMultihashHasher < Code extends number = number > extends MultihashHasher < Code > {
71+ digest ( input : Uint8Array ) : MultihashDigest
5472}
0 commit comments