Skip to content

Commit

Permalink
fix: missing Files APIs over window.ipfs (#657)
Browse files Browse the repository at this point in the history
This change makes ipfs-postmsg-proxy work with both old and new APIs
#651
  • Loading branch information
lidel authored Jan 15, 2019
1 parent a3cef39 commit d179f32
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
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

0 comments on commit d179f32

Please sign in to comment.