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

Initialization and webpack #10

Closed
wants to merge 2 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you intended to write examples/use-from-another-page

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are correct

* npm run build
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is necessary a npm install previously, in order to install webpack

Copy link
Author

@mitra42 mitra42 Apr 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this, I am running npm install prior to the build, in the ipfs-service-worker.

To be honest, I think this example is much too complicated to be useful, though it helped me figure out how to do it myself. The example is completely unclear what "use-from-another-page" means and in my own implementation I've simplified it significantly to a single package.json, and a single webpack.config.js at the same level which builds the bundles in the dist directory.

I can't post that as a PR since its launching our own code (which includes IPFS) as the SW, rather than just IPFS.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you that the example is pretty unclear.

I believe it may be considered as an independent project, with its own dependencies. In this PR, there are still 2 package.json. This way, with the implementation in this PR, it needs the npm install.

* cp -r public+sw/* dist/public+sw

5 changes: 3 additions & 2 deletions examples/use-from-another-page/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>",
"license": "MIT",
Expand All @@ -14,6 +15,6 @@
},
"devDependencies": {
"standard": "^10.0.3",
"webpack": "^2.5.1"
"webpack": "^4.2.0",
}
}
14 changes: 11 additions & 3 deletions examples/use-from-another-page/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
37 changes: 26 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})

Expand All @@ -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)
Expand Down