sindresorhus / hash-object

Get the hash of an object
MIT License
134 stars 10 forks source link

Support for non-node.js environments #6

Open transitive-bullshit opened 3 weeks ago

transitive-bullshit commented 3 weeks ago

Hey hey 👋

Love this module, but it relies in node:crypto which is not available in edge environments or browser.

Here's a version which uses the web crypto API instead (gist), but since the web crypto API's digest is async, it requires hashObject to return a Promise now instead.

If you'd rather just focus on Node.js support, that's fine, but the readme mentions deterministic execution on node.js / browsers, so that should probably be changed to be Node.js-only. Otherwise, happy to submit a PR for a breaking change to update the package to use the web crypto version I linked to above.

This was prompted by https://github.com/transitive-bullshit/agentic/issues/677

sindresorhus commented 3 weeks ago

I don't want to change the current default export function, but there could be a hashObjectAsync named export.

transitive-bullshit commented 3 weeks ago

Hmmm I don't think having a named export alone would be enough, since importing the package would still import node:crypto which would fail on non-node platforms that don't polyfill the node stdlib.

We'd have to have a separate package.json export with the hashObjectAsync functionality. Something like this:

  "exports": {
    "node": {
      "types": "./index-node.d.ts",
      "default": "./index-node.js"
    },
    "default": {
      "types": "./index.d.ts",
      "default": "./index.js"
    }
  },

It's disappointing that web crypto doesn't offer a synchronous digest API, but we'd just have to document that the exports differ depending on the platform (hashObject on Node.js vs hashObjectAsync on everything else).

@sindresorhus have you ran up against this type of thing on any of your other packages? Curious if you have a preferred way of solving this. Let me know what you prefer && I'll create a PR. Thanks! 🙏