helia icon indicating copy to clipboard operation
helia copied to clipboard

Compatibility with deno

Open sebilasse opened this issue 2 years ago • 29 comments

For an ActivityPub project I need ipfs in deno and this seems most promising to me. Currently it seems that the crypto.generateKeyPair is not supported, coming from https://github.com/libp2p/js-libp2p-crypto

See https://stackoverflow.com/questions/72584422/how-to-convert-node-crypto-generatekeypairsync-to-deno

This is the error stack:

[AsyncFunction: createLibp2p] [Class: MemoryDatastore] [Class: MemoryBlockstore] [Function: unixfs] [Class: CID] [AsyncFunction: createHelia]
error: Uncaught Error: Not implemented: crypto.generateKeyPair
    at notImplemented (ext:deno_node/_utils.ts:7:11)
    at generateKeyPair (ext:deno_node/internal/crypto/keygen.ts:8:5)
    at ext:deno_node/internal/util.mjs:83:15
    at new Promise (<anonymous>)
    at generateKeyPair (ext:deno_node/internal/util.mjs:68:12)
    at Module.generateKey (file:///Users/ed/Library/Caches/deno/npm/registry.npmjs.org/@libp2p/crypto/1.0.14/dist/src/keys/ed25519.js:31:23)
    at Module.generateKeyPair (file:///Users/ed/Library/Caches/deno/npm/registry.npmjs.org/@libp2p/crypto/1.0.14/dist/src/keys/ed25519-class.js:105:52)
    at generateKeyPair (file:///Users/ed/Library/Caches/deno/npm/registry.npmjs.org/@libp2p/crypto/1.0.14/dist/src/keys/index.js:35:34)
    at createEd25519PeerId (file:///Users/ed/Library/Caches/deno/npm/registry.npmjs.org/@libp2p/peer-id-factory/2.0.3/dist/src/index.js:6:23)
    at createLibp2pNode (file:///Users/ed/Library/Caches/deno/npm/registry.npmjs.org/libp2p/0.43.3/dist/src/libp2p.js:395:32)

sebilasse avatar Apr 03 '23 02:04 sebilasse

I'm not massively familiar with approaches for polyfilling APIs in deno.

For the browser there's config in @libp2p/crypto to load a pure-js implementation of Ed25519 - is there a way we can get deno to do the same thing?

achingbrain avatar Apr 04 '23 17:04 achingbrain

[discovering deno] @kitsonk @bartlomieju @denoland ^ Can we?

tl;dr ipfs in deno would be superfun

sebilasse avatar Apr 05 '23 07:04 sebilasse

If you're asking about the missing crypto API, you can watch https://github.com/denoland/deno/issues/18455 for updates on the provided APIs. We intend to ship crypto.generateKeyPair() next week.

bartlomieju avatar Apr 06 '23 12:04 bartlomieju

That's awesome!

I wasn't asking about the missing API, I was asking if there's a way to override the files deno loads for a given module so we can polyfill missing APIs until they are shipped, in a similar way to using the "browser" field in a package.json to load browser-compatible implementations of whatever feature.

If the missing parts are being implemented though I'm more than happy to hang on and use the proper versions of APIs ❤️

achingbrain avatar Apr 07 '23 16:04 achingbrain

There's no way to override the files Deno uses to polyfill these APIs. Unfortunately you will have to wait for it :)

bartlomieju avatar Apr 07 '23 17:04 bartlomieju

@sebilasse Have you been able to work on this any more with the latest version of Deno? Has anyone else made any progress?

justin0mcateer avatar Jul 10 '23 21:07 justin0mcateer

FYI crypto.generateKeyPair() is available in Deno v1.33

bartlomieju avatar Jul 11 '23 00:07 bartlomieju

FWIW, I tried the latest version Deno with one of the simple examples from the 'js-libp2p' repository yesterday and I couldn't get it to run (with minimal effort). Unfortunately, there are some transitive dependencies deep in the stack (in this case 'jsbn') that are extremely old/unmaintained, that use archaic export patterns that Deno does not seem to support (at least not well).

I spent quite a lot of time on this last year and ultimately concluded that it was not feasible at that time. I think the situation has gotten much better, a lot (thanks @achingbrain and team) of hard work has gone into overhauling the top level packages to be:

  1. TypeScript
  2. Fully ESM
  3. Browser Friendly
  4. Having less dependencies (esp Helia vs js-ipfs)

I would like to spend some time to do some deep analysis on the transitive dependencies and see how much effort it would be to eliminate the old/outdated transitive dependencies. Unfortunately, we have other pressing matters at this time. However, we might be willing/able to sponsor someone to take on the analysis and/or remediation effort if there is interest.

justin0mcateer avatar Jul 20 '23 14:07 justin0mcateer

Thanks for looking in to this. If a hitlist of problematic dependencies can be produced, we can certainly look at removing or replacing them.

A quick glance through the code shows that jsbin is a dependency of @achingbrain/ip-address (a temporary fork of ip-address with https://github.com/beaugunderson/ip-address/pull/148 applied), itself only used to validate IP4/IP6 addresses supplied as strings so could probably be replaced with a regex.

achingbrain avatar Jul 20 '23 14:07 achingbrain

Has anyone published a repo containing the attempts discussed in this thread? It would be nice to have something for us to hack on and push fixes to.

SgtPooki avatar Aug 02 '23 23:08 SgtPooki

I threw together https://github.com/SgtPooki/helia-deno quick and am blocked on asymmetricKeyType. see https://github.com/SgtPooki/helia-deno#current-blockers

that issue seems to be tracked by https://github.com/denoland/deno/issues/18928 on the deno side.

SgtPooki avatar Aug 03 '23 00:08 SgtPooki

I would be very interested in collaborating on that.

Have you seen this PR?: https://github.com/denoland/deno/pull/18651

justin0mcateer avatar Aug 03 '23 01:08 justin0mcateer

@justin0mcateer feel free to push a PR to SgtPooki/helia-deno if you have any improvements. I skimmed https://github.com/denoland/deno/pull/18651 but it doesn't mean much to me right now. What am I missing?

SgtPooki avatar Aug 03 '23 01:08 SgtPooki

Related: https://github.com/esm-dev/esm.sh/issues/562

SgtPooki avatar Nov 15 '23 21:11 SgtPooki

FYI https://jsr.io/@std/crypto is now stable

sebilasse avatar Jul 08 '24 17:07 sebilasse

Hi, I ran another test. I am using this repository here: https://github.com/yeus/deno_helia

the error I am getting is this one here (run it using deno run -A test_helia.ts):

error: Uncaught (in promise) TypeError: /home/tom/git/taskyon/tyserver/node_modules/.deno/[email protected]/node_modules/node-datachannel/build/Release/node_datachannel.node: undefined symbol: _ZTVN10__cxxabiv117__class_type_infoE
    at Object.Module._extensions..node (node:module:807:20)
    at Module.load (node:module:662:32)
    at Function.Module._load (node:module:534:12)
    at Module.require (node:module:681:19)
    at require (node:module:818:16)
    at file:///home/tom/git/taskyon/tyserver/node_modules/.deno/[email protected]/node_modules/node-datachannel/lib/node-datachannel.js:5:25

I used deno --version

deno 2.0.6 (stable, release, x86_64-unknown-linux-gnu) v8 12.9.202.13-rusty typescript 5.6.2

which I believe has the crypto library and a few more things...

yeus avatar Nov 21 '24 21:11 yeus

There's no way to override the files Deno uses to polyfill these APIs. Unfortunately you will have to wait for it :)

what about this here: https://deno.land/x/[email protected]/npm_nodejs/import_maps.md can this be used for polyfills?

yeus avatar Nov 21 '24 21:11 yeus