Create Browser gateway
vasco-santos opened this issue · 9 comments
More information will be availble from the OKRs
I've implemented a naive one https://github.com/linonetwo/ipfs-browser-gateway, but I'm not familiar with ipfs' DAG traversal, so I stopped there.
Can you give me some advice, so I can make PR here?
@linonetwo that's super cool! What we want is to mimic the IPFS HTTP Gateway that one gets when spawning a daemon but through a service worker http proxy.
Why did you need DAG Traversal? The HTTP Gateway only operates over Files. Could you give me an example?
Besides adding a single file, people would serve the website and add folders too.
For example, https://ipfs.io/ipfs/QmeYxwj4CwCeGVhwi3xLrmBZUUFQdftshSiGLrTdTnWEVV is a folder. I thought they were added by ipfs-unixfs, thus properly reading them needs traversal of DAG.
When I node.files.get
this hash, I got an array of object that has depth
, but I was not clear whether this is related to DAG.
https://github.com/linonetwo/ipfs-browser-gateway/blob/master/public/serviceWorker.js#L67-L76
https://ipfs.io/ipfs/QmRoYXgYMfcP7YQR4sCuSeJy9afgA5XDJ78JzWntpRhmcu seems to be another kind of folder. And use node.files.get
on it will get huge data. ipfs.io gateway seems to deal with these two kinds of folders differently.
@diasdavid Maybe I need to read the code in the ipfs daemon?
@linonetwo check the http gateway code at https://github.com/ipfs/js-ipfs/tree/master/src/http/gateway
Thanks, I know how it works now.
I can reuse /src/http/gateway/resolver.js and copy some core parts of /src/http/gateway/resources/gateway.js if ipfs/src/http/gateway/resolver
is exported and browserifyed.
But I didn't find a way to properly import ipfs/http/gateway/resolver.js
into service worker now, so I have just copied them.
Most depended npm package was replaced by importScripts( CDN URL )
. But some of them use exports
internally, so I import them in following ways:
- some packages can use this hack to import, but packages with
const xxx = require
can't - I use
https://wzrd.in/standalone/xxxpackage
to browserifyconst xxx = require
on the fly, this may be slow since it's not a professional CDN
And I'm still learning how to return a stream from service worker, so still WIP.
It's mostly finished, but:
- loading dependencies by importScripts synchronously is slow (10s)
- it's not returning a
Response
, there is a bug, but I'm too sleepy to figure out, it's too late
Hope somebody can review it, it's mostly the same as https://github.com/ipfs/js-ipfs/tree/master/src/http/gateway
@linonetwo I have already been working on this issue, so thanks for your contribution. I will analyse your implementation asap
Repo for the service-worker-gateway
project.
@vasco-santos Thank you too. I'm eager to discuss with you.