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

Feat/ipfs init #1

Closed
wants to merge 4 commits into from
Closed
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
34 changes: 34 additions & 0 deletions add-on/src/lib/ipfs-client/embedded.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict'

const Ipfs = require('ipfs')

let node = null

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

node = new Ipfs({
config: {
Addresses: {
Swarm: []
}
}
})

if (node.isOnline()) {
return Promise.resolve(node)
}

return new Promise((resolve, reject) => {
// TODO: replace error listener after a 'ready' event.
node.once('error', (err) => reject(err))
node.once('ready', () => resolve(node))
})
}

exports.destroy = async function () {
if (!node) return

await node.stop()
node = null
}
12 changes: 12 additions & 0 deletions add-on/src/lib/ipfs-client/external.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'
/* eslint-env browser */

const IpfsApi = require('ipfs-api')

exports.init = async function (opts) {
console.log('[ipfs-companion] External ipfs init')

const url = new URL(opts.ipfsApiUrl)
const api = IpfsApi({host: url.hostname, port: url.port, procotol: url.protocol})
return api
}
20 changes: 20 additions & 0 deletions add-on/src/lib/ipfs-client/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

const embedded = require('./embedded')
const external = require('./external')

let client = null

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

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

return client
}
16 changes: 6 additions & 10 deletions add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const browser = require('webextension-polyfill')
const { optionDefaults, storeMissingOptions } = require('./options')
const { initState } = require('./state')
const IsIpfs = require('is-ipfs')
const IpfsApi = require('ipfs-api')
const { createIpfsPathValidator, safeIpfsPath, urlAtPublicGw } = require('./ipfs-path')
const createDnsLink = require('./dns-link')
const { createRequestModifier } = require('./ipfs-request')
const { initIpfsClient } = require('./ipfs-client')

// INIT
// ===================================================================
Expand All @@ -23,7 +23,8 @@ module.exports = async function init () {
try {
const options = await browser.storage.local.get(optionDefaults)
state = window.state = initState(options)
ipfs = window.ipfs = initIpfsApi(options.ipfsApiUrl)
ipfs = window.ipfs = await initIpfsClient(options)
console.log('[ipfs-companion] ipfs init complete')
dnsLink = createDnsLink(getState)
ipfsPathValidator = createIpfsPathValidator(getState, dnsLink)
modifyRequest = createRequestModifier(getState, dnsLink, ipfsPathValidator)
Expand All @@ -50,11 +51,6 @@ function getState () {
return state
}

function initIpfsApi (ipfsApiUrl) {
const url = new URL(ipfsApiUrl)
return IpfsApi({host: url.hostname, port: url.port, procotol: url.protocol})
}

function registerListeners () {
browser.webRequest.onBeforeSendHeaders.addListener(onBeforeSendHeaders, {urls: ['<all_urls>']}, ['blocking', 'requestHeaders'])
browser.webRequest.onBeforeRequest.addListener(onBeforeRequest, {urls: ['<all_urls>']}, ['blocking'])
Expand Down Expand Up @@ -249,7 +245,7 @@ async function addFromURL (info) {
const response = await fetch(srcUrl, fetchOptions)
const reader = new FileReader()
reader.onloadend = () => {
const buffer = ipfs.Buffer.from(reader.result)
const buffer = Buffer.from(reader.result)
ipfs.add(buffer, uploadResultHandler)
}
reader.readAsArrayBuffer(await response.blob())
Expand Down Expand Up @@ -590,7 +586,7 @@ function updateAutomaticModeRedirectState (oldPeerCount, newPeerCount) {
}
}

function onStorageChange (changes, area) {
async function onStorageChange (changes, area) {
for (let key in changes) {
let change = changes[key]
if (change.oldValue !== change.newValue) {
Expand All @@ -599,7 +595,7 @@ function onStorageChange (changes, area) {
if (key === 'ipfsApiUrl') {
state.apiURL = new URL(change.newValue)
state.apiURLString = state.apiURL.toString()
ipfs = window.ipfs = initIpfsApi(state.apiURLString)
ipfs = window.ipfs = await initIpfsClient(state)
apiStatusUpdate()
} else if (key === 'ipfsApiPollMs') {
setApiStatusUpdateInterval(change.newValue)
Expand Down
1 change: 1 addition & 0 deletions add-on/src/lib/options.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const optionDefaults = Object.freeze({
ipfsNodeType: 'external',
publicGatewayUrl: 'https://ipfs.io',
useCustomGateway: true,
automaticMode: true,
Expand Down
1 change: 1 addition & 0 deletions add-on/src/lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function initState (options) {
const state = {}
// we store the most used values in optimized form
// to minimize performance impact on overall browsing experience
state.ipfsNodeType = options.ipfsNodeType
state.pubGwURL = new URL(options.publicGatewayUrl)
state.pubGwURLString = state.pubGwURL.toString()
state.redirect = options.useCustomGateway
Expand Down
2 changes: 1 addition & 1 deletion add-on/src/popup/quick-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function quickUploadStore (state, emitter) {
let reader = new FileReader()
reader.onloadend = () => {
const buffer = Buffer.from(reader.result)
bg.ipfs.add(buffer, (err, result) => {
bg.ipfs.files.add(buffer, (err, result) => {
if (err || !result) {
// keep upload tab and display error message in it
state.message = `Unable to upload to IPFS API: ${err}`
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"build:copy": "run-s build:copy:*",
"build:copy:src": "shx mkdir -p add-on/dist && shx cp -R add-on/src/* add-on/dist",
"build:copy:wx-polyfill-lib": "shx cp node_modules/webextension-polyfill/dist/browser-polyfill.min.js add-on/dist/contentScripts/browser-polyfill.min.js",
"build:js": "browserify -t [ browserify-package-json --global ] add-on/src/background/background.js add-on/src/options/options.js add-on/src/popup/browser-action/index.js add-on/src/popup/quick-upload.js -p [ factor-bundle -o add-on/dist/background/background.js -o add-on/dist/options/options.js -o add-on/dist/popup/browser-action/browser-action.js -o add-on/dist/popup/quick-upload.js ] -o add-on/dist/ipfs-companion-common.js",
"build:js": "browserify -p prundupify -t [ browserify-package-json --global ] add-on/src/background/background.js add-on/src/options/options.js add-on/src/popup/browser-action/index.js add-on/src/popup/quick-upload.js -p [ factor-bundle -o add-on/dist/background/background.js -o add-on/dist/options/options.js -o add-on/dist/popup/browser-action/browser-action.js -o add-on/dist/popup/quick-upload.js ] -o add-on/dist/ipfs-companion-common.js",
"build:minimize-dist": "shx rm -rf add-on/dist/lib",
"build:bundle-extension": "web-ext build -s add-on/ -i src/ -a build/",
"watch": "run-p watch:*",
"watch:js": "watchify add-on/src/background/background.js add-on/src/options/options.js add-on/src/popup/browser-action/index.js add-on/src/popup/quick-upload.js -p [ factor-bundle -o add-on/dist/background/background.js -o add-on/dist/options/options.js -o add-on/dist/popup/browser-action/browser-action.js -o add-on/dist/popup/quick-upload.js ] -o add-on/dist/ipfs-companion-common.js -v",
"watch:js": "watchify -p prundupify -t [ browserify-package-json --global ] add-on/src/background/background.js add-on/src/options/options.js add-on/src/popup/browser-action/index.js add-on/src/popup/quick-upload.js -p [ factor-bundle -o add-on/dist/background/background.js -o add-on/dist/options/options.js -o add-on/dist/popup/browser-action/browser-action.js -o add-on/dist/popup/quick-upload.js ] -o add-on/dist/ipfs-companion-common.js -v",
"test": "run-s test:*",
"test:functional": "mocha test/functional/**/*.test.js",
"lint": "run-s lint:*",
Expand Down Expand Up @@ -68,9 +68,11 @@
},
"dependencies": {
"choo": "6.6.0",
"ipfs": "^0.26.0",
"ipfs-api": "17.1.3",
"is-ipfs": "0.3.2",
"lru_map": "0.3.3",
"prundupify": "^1.0.0",
"webextension-polyfill": "0.1.2"
}
}