Skip to content

Commit c90114b

Browse files
authored
fix: support ipfs.add and ipfs.files.add (#651)
1 parent 17fa4e5 commit c90114b

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

add-on/src/lib/ipfs-client/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ async function initIpfsClient (opts) {
1616
}
1717

1818
const instance = await client.init(opts)
19+
easeApiChanges(instance)
1920
_reloadIpfsClientDependents() // async (API is present)
2021
return instance
2122
}
@@ -52,5 +53,20 @@ async function _reloadIpfsClientDependents () {
5253
}
5354
}
5455

56+
// Ensures Companion can be used with backends that provide old and new versions
57+
// of the same API moved into different namespace
58+
function easeApiChanges (ipfs) {
59+
if (!ipfs) return
60+
// Handle the move of regular files api to top level
61+
// https://github.com/ipfs/interface-ipfs-core/pull/378
62+
// https://github.com/ipfs/js-ipfs/releases/tag/v0.34.0-pre.0
63+
const movedToTop = ['add', 'addPullStream', 'addReadableStream', 'cat', 'catPullStream', 'catReadableStream', 'get', 'getPullStream', 'getReadableStream']
64+
movedToTop.forEach(cmd => {
65+
if (typeof ipfs[cmd] !== 'function' && ipfs.files && ipfs.files[cmd] === 'function') {
66+
ipfs[cmd] = ipfs.files[cmd]
67+
}
68+
})
69+
}
70+
5571
exports.initIpfsClient = initIpfsClient
5672
exports.destroyIpfsClient = destroyIpfsClient

add-on/src/lib/ipfs-companion.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ module.exports = async function init () {
265265
try {
266266
const dataSrc = await findValueForContext(context, contextType)
267267
if (contextType === 'selection') {
268-
result = await ipfs.files.add(Buffer.from(dataSrc), options)
268+
result = await ipfs.add(Buffer.from(dataSrc), options)
269269
} else {
270270
// Enchanced addFromURL
271271
// --------------------
@@ -294,7 +294,7 @@ module.exports = async function init () {
294294
path: decodeURIComponent(filename),
295295
content: buffer
296296
}
297-
result = await ipfs.files.add(data, options)
297+
result = await ipfs.add(data, options)
298298
}
299299
} catch (error) {
300300
console.error('Error in upload to IPFS context menu', error)

add-on/src/lib/ipfs-proxy/access-control.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class AccessControl extends EventEmitter {
7070
}
7171

7272
// Get a Map of granted permissions for a given scope
73-
// e.g. Map { 'files.add' => true, 'object.new' => false }
73+
// e.g. Map { 'add' => true, 'object.new' => false }
7474
async _getAllAccess (scope) {
7575
const key = this._getAccessKey(scope)
7676
return new Map(

add-on/src/lib/ipfs-request.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,20 @@ function createRequestModifier (getState, dnslinkResolver, ipfsPathValidator, ru
153153
// Fix "http: invalid Read on closed Body"
154154
// ----------------------------------
155155
// There is a bug in go-ipfs related to keep-alive connections
156-
// that results in partial response for ipfs.files.add
156+
// that results in partial response for ipfs.add
157157
// mangled by error "http: invalid Read on closed Body"
158158
// More info (ipfs-companion): https://github.com/ipfs-shipyard/ipfs-companion/issues/480
159159
// More info (go-ipfs): https://github.com/ipfs/go-ipfs/issues/5168
160160
if (request.url.includes('/api/v0/add') && request.url.includes('stream-channels=true')) {
161161
let addExpectHeader = true
162162
const expectHeader = { name: 'Expect', value: '100-continue' }
163-
const warningMsg = '[ipfs-companion] Executing "Expect: 100-continue" workaround for ipfs.files.add due to https://github.com/ipfs/go-ipfs/issues/5168'
163+
const warningMsg = '[ipfs-companion] Executing "Expect: 100-continue" workaround for ipfs.add due to https://github.com/ipfs/go-ipfs/issues/5168'
164164
for (let header of request.requestHeaders) {
165165
// Workaround A: https://github.com/ipfs/go-ipfs/issues/5168#issuecomment-401417420
166166
// (works in Firefox, but Chromium does not expose Connection header)
167167
/* (disabled so we use the workaround B in all browsers)
168168
if (header.name === 'Connection' && header.value !== 'close') {
169-
console.warn('[ipfs-companion] Executing "Connection: close" workaround for ipfs.files.add due to https://github.com/ipfs/go-ipfs/issues/5168')
169+
console.warn('[ipfs-companion] Executing "Connection: close" workaround for ipfs.add due to https://github.com/ipfs/go-ipfs/issues/5168')
170170
header.value = 'close'
171171
addExpectHeader = false
172172
break

add-on/src/popup/quick-upload.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,14 @@ async function processFiles (state, emitter, files) {
7272
}
7373
let result
7474
try {
75-
result = await ipfsCompanion.ipfs.files.add(streams, options)
75+
result = await ipfsCompanion.ipfs.add(streams, options)
7676
// This is just an additional safety check, as in past combination
7777
// of specific go-ipfs/js-ipfs-http-client versions
7878
// produced silent errors in form of partial responses:
7979
// https://github.com/ipfs-shipyard/ipfs-companion/issues/480
8080
const partialResponse = result.length !== streams.length + (options.wrapWithDirectory ? 1 : 0)
8181
if (partialResponse) {
82-
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')
82+
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')
8383
}
8484
await ipfsCompanion.uploadResultHandler({ result, openRootInNewTab: true })
8585
} catch (err) {

0 commit comments

Comments
 (0)