Skip to content

Commit

Permalink
feat(window.ipfs): opt-in ipfsx experiment
Browse files Browse the repository at this point in the history
This adds an opt-in ipfsx experiment.
In short, if `experiments:{ipfsx:true}` is passed `window.ipfs.enable`
will return IPFS API instance wrapped in ipfsx prototype from
https://github.com/ipfs-shipyard/ipfsx

```
let ipfs = await window.ipfs.enable({
    commands: ['add','files.addPullStream'],
    experiments: { ipfsx: true }
})
```
  • Loading branch information
lidel committed Dec 16, 2018
1 parent 19a9577 commit b633eb4
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 100 deletions.
19 changes: 10 additions & 9 deletions add-on/src/contentScripts/ipfs-proxy/page.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
'use strict'

const _Buffer = Buffer
const { assign, freeze } = Object

// TODO: (wip) 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')
const { call } = require('postmsg-rpc')

function createEnableCommand (proxyClient) {
return {
enable: async (opts) => {
// (This should be a lazy-load)
// Send message to proxy server for additional validation
// eg. trigger user prompt if a list of requested capabilities is not empty
// or fail fast and throw if IPFS Proxy is disabled globally
await call('proxy.enable', opts)
await require('postmsg-rpc').call('proxy.enable', opts)
// Additional client-side features
if (opts) {
if (opts.ipfsx === true) {
// TODO: wrap API in https://github.com/alanshaw/ipfsx
// return ipfsx(proxyClient)
if (opts && opts.experiments) {
if (opts.experiments.ipfsx) {
// Experiment: wrap API with https://github.com/alanshaw/ipfsx
return freeze(require('ipfsx')(proxyClient))
}
}
return Object.freeze(proxyClient)
return freeze(proxyClient)
}
}
}

function createWindowIpfs () {
const proxyClient = createProxyClient()
Object.assign(proxyClient, createEnableCommand(proxyClient))
assign(proxyClient, createEnableCommand(proxyClient))
// TODO: return thin object with lazy-init inside of window.ipfs.enable
return Object.freeze(proxyClient)
return freeze(proxyClient)
}

// TODO: we should remove Buffer and add support for Uint8Array/ArrayBuffer natively
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
"ipfs-http-client": "27.1.0",
"ipfs-http-response": "0.2.1",
"ipfs-postmsg-proxy": "3.1.1",
"ipfsx": "0.17.0",
"is-ipfs": "0.4.8",
"is-svg": "3.0.0",
"lru-cache": "5.1.1",
Expand Down
Loading

0 comments on commit b633eb4

Please sign in to comment.