Skip to content

Commit 4e6f65d

Browse files
committed
feat: scaffolding for window.ipfs.enable
This simply changes the flow and API for getting IPFS proxy instance, does not implement UX nor decrease the size of injected content script.
1 parent 1a7e1d3 commit 4e6f65d

File tree

3 files changed

+2461
-1035
lines changed

3 files changed

+2461
-1035
lines changed
+18-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
11
'use strict'
22

3-
const { createProxyClient } = require('ipfs-postmsg-proxy')
43
const _Buffer = Buffer
54

5+
// TODO: this should not be injected by default into every page,
6+
// instead should be lazy-loaded when .enable() method is called for the first time
7+
const { createProxyClient } = require('ipfs-postmsg-proxy')
8+
9+
function windowIpfs2 () {
10+
return Object.freeze({
11+
enable: async (args) => {
12+
// TODO: pass args to ipfs-postmsg-proxy constructor
13+
// to trigger user prompt if list of requested capabilities is not empty
14+
const proxyClient = createProxyClient()
15+
console.log('Called window.ipfs.enable', args)
16+
return proxyClient
17+
}
18+
})
19+
}
20+
21+
// TODO: we should remove Buffer and add support for Uint8Array/ArrayBuffer natively
622
window.Buffer = window.Buffer || _Buffer
7-
window.ipfs = window.ipfs || createProxyClient()
23+
window.ipfs = window.ipfs || windowIpfs2()

docs/window.ipfs.md

+12-5
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@ IPFS Companion is exposing a subset of IPFS APIs as `window.ipfs` on every webpa
2525

2626
This means websites can detect that `window.ipfs` already exists and use it instead of spawning own `js-ipfs` node, which saves resources, battery etc.
2727

28-
For more context, see original issue: [Expose IPFS API as window.ipfs #330](https://github.com/ipfs-shipyard/ipfs-companion/issues/330)
28+
For more context, see:
29+
- first iteration: [Expose IPFS API as window.ipfs #330](https://github.com/ipfs-shipyard/ipfs-companion/issues/330)
30+
- second iteration: [window.ipfs 2.0](https://github.com/ipfs-shipyard/ipfs-companion/issues/589)
2931

3032
## Creating applications using `window.ipfs`
3133

3234
If a user has installed IPFS companion, `window.ipfs` will be available as soon as the first script runs on your web page, so you'll be able to detect it using a simple `if` statement:
3335

3436
```js
35-
if (window.ipfs) {
36-
await ipfs.id()
37+
if (window.ipfs && window.ipfs.enable) {
38+
const ipfs = await window.ipfs.enable( { capabilities: ['id'] } )
39+
console.log(await ipfs.id())
3740
} else {
3841
// Fallback
3942
}
@@ -42,8 +45,10 @@ if (window.ipfs) {
4245
To add and get content, you could do something like this:
4346

4447
```js
45-
if (window.ipfs) {
48+
if (window.ipfs && window.ipfs.enable) {
4649
try {
50+
const capabilities = ["add","cat"]
51+
const ipfs = await window.ipfs.enable({capabilities})
4752
const [{ hash }] = await ipfs.add(Buffer.from('=^.^='))
4853
const data = await ipfs.cat(hash)
4954
console.log(data.toString()) // =^.^=
@@ -92,9 +97,11 @@ Note these might have been re-worded already. Please send a PR.
9297

9398
## What _is_ a `window.ipfs`?
9499

100+
It is an endpoint that enables you to obtain IPFS API instance.
101+
95102
Depending how IPFS companion is configured, you may be talking directly to a `js-ipfs` node running in the companion, a `go-ipfs` daemon over `js-ipfs-api` or a `js-ipfs` daemon over `js-ipfs-api` and potentially others in the future.
96103

97-
Note that `window.ipfs` is _not_ an instance of `js-ipfs` or `js-ipfs-api` but is a proxy to one of them, so don't expect to be able to detect either of them or be able to use any undocumented or instance specific functions.
104+
Note that object returned by `window.ipfs.enable` is _not_ an instance of `js-ipfs` or `js-ipfs-api` but is a proxy to one of them, so don't expect to be able to detect either of them or be able to use any undocumented or instance specific functions.
98105

99106
See the [js-ipfs](https://github.com/ipfs/js-ipfs#api)/[js-ipfs-api](https://github.com/ipfs/js-ipfs-api#api) docs for available functions. If you find that some new functions are missing, the proxy might be out of date. Please check the [current status](https://github.com/tableflip/ipfs-postmsg-proxy#current-status) and submit a PR.
100107

0 commit comments

Comments
 (0)