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

Init ipfs-api or js-ipfs #320

Merged
merged 20 commits into from
Dec 12, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix destroy logic. Pin deps
  • Loading branch information
olizilla committed Nov 29, 2017
commit f603614f123ad7b49a5859ddfd38b11d4832c86c
1 change: 1 addition & 0 deletions add-on/src/lib/ipfs-client/embedded.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ exports.init = function init () {
}

exports.destroy = async function () {
console.log('[ipfs-companion] Embedded ipfs destroy')
if (!node) return

await node.stop()
Expand Down
6 changes: 6 additions & 0 deletions add-on/src/lib/ipfs-client/external.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ exports.init = async function (opts) {
const api = IpfsApi({host: url.hostname, port: url.port, procotol: url.protocol})
return api
}

exports.destroy = async function () {
console.log('[ipfs-companion] Embedded ipfs destroy')
}

// TODO: I become a caching proxy for ipfs-api
21 changes: 14 additions & 7 deletions add-on/src/lib/ipfs-client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ const external = require('./external')

let client = null

exports.initIpfsClient = async function (opts) {
if (client && client.destroy) {
await client.destroy()
}
async function initIpfsClient (opts) {
await destroyIpfsClient()

if (opts.ipfsNodeType === 'embedded') {
client = await embedded.init(opts)
client = embedded
} else {
client = await external.init(opts)
client = external
}

return client
return client.init(opts)
}

async function destroyIpfsClient () {
if (client && client.destroy) {
return client.destroy()
}
}

exports.initIpfsClient = initIpfsClient
exports.destroyIpfsClient = destroyIpfsClient
5 changes: 3 additions & 2 deletions add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const IsIpfs = require('is-ipfs')
const { createIpfsPathValidator, safeIpfsPath, urlAtPublicGw } = require('./ipfs-path')
const createDnsLink = require('./dns-link')
const { createRequestModifier } = require('./ipfs-request')
const { initIpfsClient } = require('./ipfs-client')
const { initIpfsClient, destroyIpfsClient } = require('./ipfs-client')

// INIT
// ===================================================================
Expand Down Expand Up @@ -37,14 +37,15 @@ module.exports = async function init () {
}
}

module.exports.destroy = function () {
module.exports.destroy = async function () {
clearInterval(apiStatusUpdateInterval)
apiStatusUpdateInterval = null
ipfs = null
state = null
dnsLink = null
modifyRequest = null
ipfsPathValidator = null
await destroyIpfsClient()
}

function getState () {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
},
"dependencies": {
"choo": "6.6.0",
"ipfs": "^0.26.0",
"ipfs": "0.26.0",
"ipfs-api": "17.1.3",
"is-ipfs": "0.3.2",
"lru_map": "0.3.3",
Expand Down
16 changes: 8 additions & 8 deletions test/functional/lib/ipfs-companion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ const { URL } = require('url')
const { optionDefaults } = require('../../../add-on/src/lib/options')

describe('init', () => {
let init
let ipfsCompanion

before(() => {
global.window = {}
global.browser = browser
global.URL = URL
init = require('../../../add-on/src/lib/ipfs-companion')
ipfsCompanion = require('../../../add-on/src/lib/ipfs-companion')
})

beforeEach(() => {
Expand All @@ -21,9 +21,9 @@ describe('init', () => {
it('should query local storage for options with hardcoded defaults for fallback', async () => {
browser.storage.local.get.returns(Promise.resolve(optionDefaults))
browser.storage.local.set.returns(Promise.resolve())
await init()
await ipfsCompanion()
browser.storage.local.get.calledWith(optionDefaults)
init.destroy()
await ipfsCompanion.destroy()
})

after(() => {
Expand All @@ -35,13 +35,13 @@ describe('init', () => {
})

describe.skip('onStorageChange()', function () {
let init
let ipfsCompanion

before(() => {
global.window = {}
global.browser = browser
global.URL = URL
init = require('../../../add-on/src/lib/ipfs-companion')
ipfsCompanion = require('../../../add-on/src/lib/ipfs-companion')
})

beforeEach(() => {
Expand All @@ -58,7 +58,7 @@ describe.skip('onStorageChange()', function () {
browser.contextMenus.update.returns(Promise.resolve())
browser.idle.queryState.returns(Promise.resolve('active'))

await init()
await ipfsCompanion()

const oldIpfsApiUrl = 'http://127.0.0.1:5001'
const newIpfsApiUrl = 'http://1.2.3.4:8080'
Expand All @@ -67,7 +67,7 @@ describe.skip('onStorageChange()', function () {
const ipfs = global.window.ipfs
browser.storage.onChanged.dispatch(changes, area)
expect(ipfs).to.not.equal(window.ipfs)
init.destroy()
ipfsCompanion.destroy()
})

after(() => {
Expand Down
Loading