From 6ead6066bfc3af68b157e2b386e992928a2ffa3b Mon Sep 17 00:00:00 2001 From: Mitra Ardron Date: Thu, 22 Mar 2018 18:21:11 +1100 Subject: [PATCH 1/2] Upgraded webpack and fixed errors on restarting browser or reloading page --- .gitignore | 1 + README.md | 9 +++++++++ package.json | 4 ++-- src/index.js | 37 ++++++++++++++++++++++++++----------- 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 8d104c8..b78ed0a 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,4 @@ jspm_packages .node_repl_history examples/use-from-another-page/public+sw/bundle.js examples/use-from-another-page/public+sw/service-worker-bundle.js +examples/use-from-another-page/dist diff --git a/README.md b/README.md index 533ebbd..c5637cd 100644 --- a/README.md +++ b/README.md @@ -86,3 +86,12 @@ if ('serviceWorker' in navigator) { }) } ``` +### Installation and testing + +* Clone the repo from here +* cd to ipfs-service-worker +* npm install +* cd examples +* npm run build +* cp -r public+sw/* dist/public+sw + diff --git a/package.json b/package.json index c7ebfd7..f36fafd 100644 --- a/package.json +++ b/package.json @@ -23,8 +23,8 @@ }, "homepage": "https://github.com/ipfs/ipfs-service-worker#readme", "dependencies": { - "ipfs": "^0.27.7", - "ipfs-postmsg-proxy": "^2.3.0" + "ipfs": "^0.28.2", + "ipfs-postmsg-proxy": "^2.3.0", }, "devDependencies": { "standard": "^10.0.3" diff --git a/src/index.js b/src/index.js index 02b6bae..27f5856 100644 --- a/src/index.js +++ b/src/index.js @@ -13,19 +13,33 @@ self.addEventListener('install', (event) => { event.waitUntil(self.skipWaiting()) }) -self.addEventListener('activate', (event) => { - console.log('activate step') - node = new IPFS({ - config: { - Addresses: { - Swarm: [] - } - } - }) - node.on('ready', () => console.log('js-ipfs node is ready')) - node.on('error', (err) => console.log('js-ipfs node errored', err)) +async function ipfsstart(verbose) { + /* Just start IPFS - promise resolves when its started + */ + const self = this; + return new Promise((resolve, reject) => { + node = new IPFS({ + config: { + Addresses: { + Swarm: [] + } + } + }); + node.on('ready', () => { resolve(); }); + node.on('error', (err) => reject(err)); + }) + .then(() => node.version()) + .then((version) => console.log('IPFS READY',version)) + .catch((err) => { + console.log("Error caught in ipfsstart"); + throw(err); + }); +} +self.addEventListener('activate', (event) => { + console.log('activate step') + ipfsstart(); // Ignore promise event.waitUntil(self.clients.claim()) }) @@ -41,6 +55,7 @@ self.addEventListener('fetch', (event) => { }) async function catAndRespond (hash) { + if (!node) await ipfsstart(); const data = await node.files.cat(hash) const headers = { status: 200, statusText: 'OK', headers: {} } return new Response(data, headers) From 1337d431eb9550cdf2dccc87f174e3af5f4a2349 Mon Sep 17 00:00:00 2001 From: Mitra Ardron Date: Thu, 22 Mar 2018 18:21:11 +1100 Subject: [PATCH 2/2] Upgraded webpack and fixed errors on restarting browser or reloading page --- examples/use-from-another-page/package.json | 5 +++-- examples/use-from-another-page/webpack.config.js | 14 +++++++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/examples/use-from-another-page/package.json b/examples/use-from-another-page/package.json index 1d4dc23..6b1c280 100644 --- a/examples/use-from-another-page/package.json +++ b/examples/use-from-another-page/package.json @@ -5,7 +5,8 @@ "main": "src/index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "build": "webpack" + "build": "webpack", + "builddev": "webpack -w --mode development", }, "author": "David Dias ", "license": "MIT", @@ -14,6 +15,6 @@ }, "devDependencies": { "standard": "^10.0.3", - "webpack": "^2.5.1" + "webpack": "^4.2.0", } } diff --git a/examples/use-from-another-page/webpack.config.js b/examples/use-from-another-page/webpack.config.js index 34cc90a..6794730 100644 --- a/examples/use-from-another-page/webpack.config.js +++ b/examples/use-from-another-page/webpack.config.js @@ -9,9 +9,17 @@ module.exports = { node: { fs: 'empty', net: 'empty', - tls: 'empty' - }, - resolve: { + tls: 'empty', + crypto: 'empty', + process: true, + module: false, + clearImmediate: false, + Buffer: true, + setImmediate: false, + console: false + }, + + resolve: { alias: { zlib: 'browserify-zlib-next' }