digitalbazaar / hashlink

JavaScript implementation of Cryptographic Hyperlinks specification.
https://tools.ietf.org/html/draft-sporny-hashlink
BSD 3-Clause "New" or "Revised" License
13 stars 3 forks source link

Using encode causes Error: Cannot find module '../algorithms/SHA' #22

Closed thedocbwarren closed 3 years ago

thedocbwarren commented 3 years ago

When using:

  const data = JSON.stringify(json[key]);
  const retval =  await hl.encode({ data, key });

during mocha test I receive:

Error: Cannot find module '../algorithms/SHA'

Code is webpacked and using node 14 along with node output target

thedocbwarren commented 3 years ago

This appears to be coming from @trust/webcrypto. This is a dependency of hashlink.

davidlehn commented 3 years ago

Looks like that code is in the @trust/webcrypto dependency that gets installed. src/algorithms/index.js file registers something with ../algorithms/SHA path, that path exists, and SupportedAlgorithms.js requires it. So probably some issue with how webpack tries to handle dynamic requires.

davidlehn commented 3 years ago

I doubt using webpack with a node output target has been tested much. If fixes are needed to make that work better, let us know. Sometimes the crypto issues with webpack can get tricky.

thedocbwarren commented 3 years ago

Ok, think I solved the issue. The webpack process bundles this library unless it's used as an external. I am publishing a UMD module. It's clobbering that path as you mentioned. Using as an "external" in the webpack.config solves it like so:

externals: [
    {
      "hashlink": {
        commonjs: "hashlink",
        commonjs2: "hashlink",
        amd: "hashlink",
        root: "hashlink"
      }
]

This tells me you are right, it's the dynamic require with the ../ path.

thedocbwarren commented 3 years ago

Ultimately for my usage I do not want to bundle, an external is what I want. Other users may wish different.

thedocbwarren commented 3 years ago

I think this is good honestly. Thank you for the support.