Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support ipfs.add and ipfs.files.add #651

Merged
merged 1 commit into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
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