Skip to content

Commit

Permalink
feat: scaffolding for window.ipfs.enable
Browse files Browse the repository at this point in the history
This simply changes the flow and API for getting IPFS proxy instance,
does not implement UX nor decrease the size of injected content script.
  • Loading branch information
lidel committed Nov 14, 2018
1 parent 458bf34 commit f755c5e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
20 changes: 18 additions & 2 deletions add-on/src/contentScripts/ipfs-proxy/page.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
'use strict'

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

// TODO: this should not be injected by default into every page,
// instead should be lazy-loaded when .enable() method is called for the first time
const { createProxyClient } = require('ipfs-postmsg-proxy')

function windowIpfs2 () {
return Object.freeze({
enable: async (args) => {
// TODO: pass args to ipfs-postmsg-proxy constructor
// to trigger user prompt if list of requested capabilities is not empty
const proxyClient = createProxyClient()
console.log('Called window.ipfs.enable', args)
return proxyClient
}
})
}

// TODO: we should remove Buffer and add support for Uint8Array/ArrayBuffer natively
window.Buffer = window.Buffer || _Buffer
window.ipfs = window.ipfs || createProxyClient()
window.ipfs = window.ipfs || windowIpfs2()
17 changes: 12 additions & 5 deletions docs/window.ipfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ IPFS Companion is exposing a subset of IPFS APIs as `window.ipfs` on every webpa

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.

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

## Creating applications using `window.ipfs`

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:

```js
if (window.ipfs) {
await ipfs.id()
if (window.ipfs && window.ipfs.enable) {
const ipfs = await window.ipfs.enable( { capabilities: ['id'] } )
console.log(await ipfs.id())
} else {
// Fallback
}
Expand All @@ -42,8 +45,10 @@ if (window.ipfs) {
To add and get content, you could do something like this:

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

## What _is_ a `window.ipfs`?

It is an endpoint that enables you to obtain IPFS API instance.

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.

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.
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.

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.

Expand Down

0 comments on commit f755c5e

Please sign in to comment.