diff --git a/add-on/src/lib/ipfs-client/index.js b/add-on/src/lib/ipfs-client/index.js index 46475f38c..3f7e302f6 100644 --- a/add-on/src/lib/ipfs-client/index.js +++ b/add-on/src/lib/ipfs-client/index.js @@ -16,6 +16,7 @@ async function initIpfsClient (opts) { } const instance = await client.init(opts) + easeApiChanges(instance) _reloadIpfsClientDependents() // async (API is present) return instance } @@ -52,5 +53,20 @@ async function _reloadIpfsClientDependents () { } } +// Ensures Companion can be used with backends that provide old and new versions +// of the same API moved into different namespace +function easeApiChanges (ipfs) { + if (!ipfs) return + // Handle the move of regular files api to top level + // https://github.com/ipfs/interface-ipfs-core/pull/378 + // https://github.com/ipfs/js-ipfs/releases/tag/v0.34.0-pre.0 + const movedToTop = ['add', 'addPullStream', 'addReadableStream', 'cat', 'catPullStream', 'catReadableStream', 'get', 'getPullStream', 'getReadableStream'] + movedToTop.forEach(cmd => { + if (typeof ipfs[cmd] !== 'function' && ipfs.files && ipfs.files[cmd] === 'function') { + ipfs[cmd] = ipfs.files[cmd] + } + }) +} + exports.initIpfsClient = initIpfsClient exports.destroyIpfsClient = destroyIpfsClient diff --git a/add-on/src/lib/ipfs-companion.js b/add-on/src/lib/ipfs-companion.js index 7fd94f38a..4346e1c44 100644 --- a/add-on/src/lib/ipfs-companion.js +++ b/add-on/src/lib/ipfs-companion.js @@ -265,7 +265,7 @@ module.exports = async function init () { try { const dataSrc = await findValueForContext(context, contextType) if (contextType === 'selection') { - result = await ipfs.files.add(Buffer.from(dataSrc), options) + result = await ipfs.add(Buffer.from(dataSrc), options) } else { // Enchanced addFromURL // -------------------- @@ -294,7 +294,7 @@ module.exports = async function init () { path: decodeURIComponent(filename), content: buffer } - result = await ipfs.files.add(data, options) + result = await ipfs.add(data, options) } } catch (error) { console.error('Error in upload to IPFS context menu', error) diff --git a/add-on/src/lib/ipfs-proxy/access-control.js b/add-on/src/lib/ipfs-proxy/access-control.js index 71dc91b6a..2de08649f 100644 --- a/add-on/src/lib/ipfs-proxy/access-control.js +++ b/add-on/src/lib/ipfs-proxy/access-control.js @@ -70,7 +70,7 @@ class AccessControl extends EventEmitter { } // Get a Map of granted permissions for a given scope - // e.g. Map { 'files.add' => true, 'object.new' => false } + // e.g. Map { 'add' => true, 'object.new' => false } async _getAllAccess (scope) { const key = this._getAccessKey(scope) return new Map( diff --git a/add-on/src/lib/ipfs-request.js b/add-on/src/lib/ipfs-request.js index 8335e09e0..9253a44bb 100644 --- a/add-on/src/lib/ipfs-request.js +++ b/add-on/src/lib/ipfs-request.js @@ -153,20 +153,20 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru // Fix "http: invalid Read on closed Body" // ---------------------------------- // There is a bug in go-ipfs related to keep-alive connections - // that results in partial response for ipfs.files.add + // that results in partial response for ipfs.add // mangled by error "http: invalid Read on closed Body" // More info (ipfs-companion): https://github.com/ipfs-shipyard/ipfs-companion/issues/480 // More info (go-ipfs): https://github.com/ipfs/go-ipfs/issues/5168 if (request.url.includes('/api/v0/add') && request.url.includes('stream-channels=true')) { let addExpectHeader = true const expectHeader = { name: 'Expect', value: '100-continue' } - const warningMsg = '[ipfs-companion] Executing "Expect: 100-continue" workaround for ipfs.files.add due to https://github.com/ipfs/go-ipfs/issues/5168' + const warningMsg = '[ipfs-companion] Executing "Expect: 100-continue" workaround for ipfs.add due to https://github.com/ipfs/go-ipfs/issues/5168' for (let header of request.requestHeaders) { // Workaround A: https://github.com/ipfs/go-ipfs/issues/5168#issuecomment-401417420 // (works in Firefox, but Chromium does not expose Connection header) /* (disabled so we use the workaround B in all browsers) if (header.name === 'Connection' && header.value !== 'close') { - console.warn('[ipfs-companion] Executing "Connection: close" workaround for ipfs.files.add due to https://github.com/ipfs/go-ipfs/issues/5168') + console.warn('[ipfs-companion] Executing "Connection: close" workaround for ipfs.add due to https://github.com/ipfs/go-ipfs/issues/5168') header.value = 'close' addExpectHeader = false break diff --git a/add-on/src/popup/quick-upload.js b/add-on/src/popup/quick-upload.js index e609c4480..57fcba2bf 100644 --- a/add-on/src/popup/quick-upload.js +++ b/add-on/src/popup/quick-upload.js @@ -72,14 +72,14 @@ async function processFiles (state, emitter, files) { } let result try { - result = await ipfsCompanion.ipfs.files.add(streams, options) + result = await ipfsCompanion.ipfs.add(streams, options) // This is just an additional safety check, as in past combination // of specific go-ipfs/js-ipfs-http-client versions // produced silent errors in form of partial responses: // https://github.com/ipfs-shipyard/ipfs-companion/issues/480 const partialResponse = result.length !== streams.length + (options.wrapWithDirectory ? 1 : 0) if (partialResponse) { - throw new Error('Result of ipfs.files.add call is missing entries. This may be due to a bug in HTTP API similar to https://github.com/ipfs/go-ipfs/issues/5168') + throw new Error('Result of ipfs.add call is missing entries. This may be due to a bug in HTTP API similar to https://github.com/ipfs/go-ipfs/issues/5168') } await ipfsCompanion.uploadResultHandler({ result, openRootInNewTab: true }) } catch (err) {