Skip to content

Commit

Permalink
fix: support ipfs.add and ipfs.files.add (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Jan 7, 2019
1 parent 17fa4e5 commit c90114b
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
16 changes: 16 additions & 0 deletions add-on/src/lib/ipfs-client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async function initIpfsClient (opts) {
}

const instance = await client.init(opts)
easeApiChanges(instance)
_reloadIpfsClientDependents() // async (API is present)
return instance
}
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
// --------------------
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion add-on/src/lib/ipfs-proxy/access-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions add-on/src/lib/ipfs-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions add-on/src/popup/quick-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c90114b

Please sign in to comment.