Prefer web APIs over node APIs
- Dominant language
- TypeScript
- Stars
- 95
- Forks
- 15
- PR merge metrics
- No merged PRs in 30d
Description
I often struggle using JS-IPFS in the browser because it predates all the nice web APIs that made working with binary data and files convenient. Often time this implies to using some node polyfills and / or needing to convert node constructs to browser ones so that other browser APIs can interact with it.
Here are few things that I have noticed.
- `addFile` depends on node read stream etc..
https://github.com/textileio/js-ipfs-lite/blob/0bb38fc91e10c480aef3109fde9ef98612a9c5a4/src/index.ts#L110-L124
I would suggest to use web native [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File) API instead which can have a `path` and can be constructed in arbitrary ways.
As of files bundles there is [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData/append) and I think it would make more sense to us that instead. It also has advantage that web forms can directly be written to IPFS. And all this works well with `fetch` and `XMLHTTPRequest` stuff.
- `getFile` returns node `Buffer`. Which luckily in browser pollyfill implemented as a subclass of `Uint8Array` so it is somewhat more usable. However that still implies allocating memory for the whole content etc... On the other hand web has a [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) API available who's body can be read as string, bytes, json, `FormData` or a stream. In fact it's also possible to use slices over the whole thing and also has notion of `Error`. It also can be stored in browser [`cache`](https://developer.mozilla.org/en-US/docs/Web/API/Cache) etc...
I would propose to draw inspiration from web native APIs and try to be as similar to `fetch` API as possible so it can be used as a drop in replacement. Specifically I'd very much prefer following intterface:
```
interface Peer {
put(FormData|File|Blob):Promise
get(CID|Path):Response;
head(CID|Path):Response; // 404 if not found, 502 if network is spotty
delete(CID):Response; // Delete from local storage
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.