Skip to content
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

Expose cjs friendly module and function #24

Open
Gozala opened this issue Oct 27, 2023 · 0 comments
Open

Expose cjs friendly module and function #24

Gozala opened this issue Oct 27, 2023 · 0 comments

Comments

@Gozala
Copy link
Contributor

Gozala commented Oct 27, 2023

@travis reported issues with top level await, but then context it's been used is fairly trivial to address see code path

          (async () => {
            const hasher = PieceHasher.create()
            hasher.write(bytes)
            const digest = new Uint8Array(hasher.multihashByteLength())
            hasher.digestInto(digest, 0, true)
            // There's no GC (yet) in WASM so you should free up
            // memory manually once you're done.
            hasher.free()
            const multihashDigest = Digest.decode(digest)
            return /** @type {import('@web3-storage/capabilities/types').PieceLink} */ (
              Link.create(raw.code, multihashDigest)
            )
          })()

We could create a simple solution here by adding another module to the package e.g. async.js that implements the more traditional Multihasher interface which in other words would look like

import load, { create } from "../gen/wasm.js"
import * as API from "./type.js"
export * from "./type.js"

let ready = load()

/**
 * @see https://github.com/multiformats/multicodec/pull/331/files
 */
export const name = /** @type {const} */ (
  "fr32-sha2-256-trunc254-padded-binary-tree"
)

/**
 * @type {API.MulticodecCode<0x1011, typeof name>}
 * @see https://github.com/multiformats/multicodec/pull/331/files
 */
export const code = 0x1011

/**
 * Multihash digest length in varint bytes
 */
export const DIGEST_SIZE_LENGTH = 1

/**
 * Multihash code size in varint bytes
 */
export const CODE_LENGTH = 2


/**
 * @param {Uint8Array} payload
 * @returns {Promise<MultihashDigest<typeof code>>}
 */
export const digest = async (payload) => {
   await ready
   const hasher = create()
   hasher.write(payload)
   const bytes = new Uint8Array(hasher.multihashByteLength())
   hasher.digestInto(bytes, 0, true)
   hasher.free()
   return {
       code,
       // next byte will hold digest varint and it never exceeds the one byte
       size: bytes[CODE_LENGTH],
       digest: bytes.subarray(CODE_LENGTH + DIGEST_SIZE_LENGTH),
       bytes
   } 
}

That would allow earlier quoted code could be rewritten as follows

import * as PieceHasher from "fr32-sha2-256-trunc254-padded-binary-tree-multihash/async"

          (async () => {
            const multihashDigest = PieceHasher.digest(bytes)
            return /** @type {import('@web3-storage/capabilities/types').PieceLink} */ (
              Link.create(raw.code, multihashDigest)
            )
          })()
travis added a commit that referenced this issue Oct 30, 2023
per #24 introduce an entrypoint that does not use a top-level await, as many target environments still do not support them
travis added a commit that referenced this issue Oct 30, 2023
per
#24
introduce an entrypoint that does not use a top-level await, as many
target environments still do not support them
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant