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: missing Files APIs over window.ipfs #657

Merged
merged 1 commit into from
Jan 15, 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
18 changes: 13 additions & 5 deletions add-on/src/lib/ipfs-client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,29 @@ async function _reloadIpfsClientDependents () {
}
}

// Ensures Companion can be used with backends that provide old and new versions
// of the same API moved into different namespace
const movedFilesApis = ['add', 'addPullStream', 'addReadableStream', 'cat', 'catPullStream', 'catReadableStream', 'get', 'getPullStream', 'getReadableStream']

// This enables use of dependencies without worrying if they already migrated to the new API.
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 => {
movedFilesApis.forEach(cmd => {
// Fix old backend (add new methods)
if (typeof ipfs[cmd] !== 'function' && ipfs.files && typeof ipfs.files[cmd] === 'function') {
ipfs[cmd] = ipfs.files[cmd]
console.log(`[ipfs-companion] fixed missing ipfs.${cmd}: added an alias for ipfs.files.${cmd}`)
// console.log(`[ipfs-companion] fixed missing ipfs.${cmd}: added an alias for ipfs.files.${cmd}`)
}
// Fix new backend (add old methods)
// This ensures ipfs-postmsg-proxy always works and can be migrated later
if (ipfs.files && typeof ipfs.files[cmd] !== 'function' && typeof ipfs[cmd] === 'function') {
ipfs.files[cmd] = ipfs[cmd]
// console.log(`[ipfs-companion] fixed missing ipfs.files.${cmd}: added an alias for ipfs.${cmd}`)
}
})
}

exports.movedFilesApis = movedFilesApis
exports.initIpfsClient = initIpfsClient
exports.destroyIpfsClient = destroyIpfsClient
17 changes: 17 additions & 0 deletions test/functional/lib/ipfs-companion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ describe('init', function () {
ipfsCompanion.destroy()
})

it('should fixup migrated files APIs', async function () {
// Companion should gracefully handle the move of regular files api to top
// level by supporting both old and new API. This way we can use
// dependencies without worrying if they already migrated to the new API.
// https://github.com/ipfs/interface-ipfs-core/pull/378
// https://github.com/ipfs/js-ipfs/releases/tag/v0.34.0-pre.0
browser.storage.local.get.returns(Promise.resolve(optionDefaults))
browser.storage.local.set.returns(Promise.resolve())
const ipfsCompanion = await init()
const { movedFilesApis } = require('../../../add-on/src/lib/ipfs-client/index.js')
for (let cmd of movedFilesApis) {
expect(typeof ipfsCompanion.ipfs[cmd], `ipfs.${cmd} expected to be a function`).to.equal('function')
expect(typeof ipfsCompanion.ipfs.files[cmd], `ipfs.files.${cmd} expected to be a function`).to.equal('function')
}
ipfsCompanion.destroy()
})

after(function () {
delete global.window
delete global.browser
Expand Down