storacha-network / w3up

⁂ w3up protocol implementation
https://github.com/storacha-network/specs
Other
54 stars 19 forks source link

upload client not working on cloudflare workers #1273

Closed sirpy closed 6 months ago

sirpy commented 7 months ago

Previously we've been using web3storage with cloudflare workers. Trying to get it to work with w3up we encounter multiple issues to get it to work

  1. @ipld/car imports from "stream" in indexed-reader, looks like there's also an indexed-reader-browser that probably should be imported in this case, but isnt. temporariliy solved by patching the package to import from "node:stream"
  2. cloudflare URL doesnt parse "did" urls. solved by importing from "whatwg-url" and setting globalThis.URL
  3. need to enable compatability flags "transformstream_enable_standard_constructor","streams_enable_constructors"

lastly couldnt solve the last error when calling client.uploadFile

TypeError: Invalid URL string.
    at __wbg_init (file:///Users/hadarro/workspace/GoodDollar/IpfsGatewayWorker/node_modules/fr32-sha2-256-trunc254-padded-binary-tree-multihash/gen/wasm.js:260:17)
    at null.<anonymous> (file:///Users/hadarro/workspace/GoodDollar/IpfsGatewayWorker/node_modules/fr32-sha2-256-trunc254-padded-binary-tree-multihash/src/async.js:6:13)
alanshaw commented 7 months ago

Not sure but this still seems to relate to URLs. Perhaps you could try moving your compatibility_date forward, or enable flags to do with URLs e.g. https://developers.cloudflare.com/workers/configuration/compatibility-dates/#new-url-parser-implementation

sirpy commented 7 months ago

@alanshaw I don't think so. looking at the code it uses a value from import.meta which resolves to undefined, and that is the issue.

sirpy commented 7 months ago

@alanshaw probably even resolving import.meta.url wouldnt work as there's no url for the cloudflare worker to resolve the wasm file from. this looks more like browser/non-browser env detection issue

alanshaw commented 7 months ago

Hmm yes looks like you're right. @gozala would you mind looking into this next CIC?

vasco-santos commented 6 months ago

Hey @sirpy Apologies for late response here. We have been working on getting this supported into Cloudflare workers. The good news are that once https://github.com/web3-storage/w3up/pull/1294 is merged and released, you will be able to use w3up-client in CF Workers environment. However, it won't be an out of the box solution. Cloudflare has some strict limitations on how to load WASM bytecode, which sadly goes against how other environments accept to load WASM bytecode (where are the standards? 😭 ). To try to make life easier to deploy this into Cloudflare workers, I put together a esbuild-plugin you can use, as well as an example:

To use this, you will just need to wait for #1294 to be merged/released and guarantee afterwards that when installing dependencies you have fr32-sha2-256-trunc254-padded-binary-tree-multihash@3.3.0 (latest)

sirpy commented 6 months ago

@vasco-santos thanks

sirpy commented 6 months ago

@vasco-santos ok it worked I still had to patch URL importing the storage requires do to the import like you did, directly from the class file, importing from top level exports breaks.