Skip to content
This repository has been archived by the owner on May 13, 2020. It is now read-only.

feat: detect and support window.ipfs.enable experiment #2

Merged
merged 3 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

> Get `window.ipfs` with fallback to CDN if unavailable

The [IPFS Companion](https://github.com/ipfs-shipyard/ipfs-companion) web extension provides a `window.ipfs` object to web pages you visit.
The [IPFS Companion](https://github.com/ipfs-shipyard/ipfs-companion) web extension provides a [`window.ipfs` object](https://github.com/ipfs-shipyard/ipfs-companion/blob/master/docs/window.ipfs.md) to web pages you visit.

This module will detects the presence of `window.ipfs` and automatically falls back to downloading the latest version of IPFS from `https://unpkg.com/ipfs/dist/index.min.js` if it's unavailable. Note: can be configured to fallback to IPFS API.

Expand All @@ -22,7 +22,7 @@ console.log(await ipfs.id())

### `getIpfs([options])`

If `window.ipfs` is available, the promise is resolved with that node.
If `window.ipfs` is available, the promise is resolved with that node and default ACLs.

If `window.ipfs` is unavailable, a `<script src="https://unpkg.com/ipfs/dist/index.min.js" />` is inserted into the document and when the script has loaded a new IPFS node is created and the promise is resolved.

Expand Down
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ module.exports = function getIpfs (opts) {
opts = opts || {}

return new Promise(function (resolve, reject) {
if (window.ipfs) return resolve(window.ipfs)
if (window.ipfs) {
// forward-compatible migration
// https://github.com/ipfs-shipyard/ipfs-companion/issues/589
if (typeof window.ipfs.enable === 'function') {
return resolve(window.ipfs.enable())
lidel marked this conversation as resolved.
Show resolved Hide resolved
}
// backward-compatible
return resolve(window.ipfs)
}

var onLoad = function () {
var Ipfs = getConstructor(opts.api)
Expand Down
Loading