From 9f27e15bfcefc2e629a97c154ec2391e2a962623 Mon Sep 17 00:00:00 2001 From: Jasper De Moor Date: Mon, 18 Dec 2017 21:10:24 +0100 Subject: [PATCH] Improve websocket error handling and fix #315 (#320) * run prettier * add proper error handling to sending data * replace hacky bugfix with gracefull connection closing on client side * improve error handling to catch all errors not just when sending * fix tests * code cleanup --- .github/ISSUE_TEMPLATE.md | 25 ++++++++++++++++++------- CONTRIBUTING.md | 7 +------ README.md | 6 ++---- package.json | 2 +- src/HMRServer.js | 12 ++++++++++++ src/builtins/hmr-runtime.js | 5 ++++- test/hmr.js | 21 +++++++++++++++------ test/plugins.js | 4 +++- yarn.lock | 6 +++--- 9 files changed, 59 insertions(+), 29 deletions(-) diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md index 5fbb5d3f786..cbd69f8d53a 100644 --- a/.github/ISSUE_TEMPLATE.md +++ b/.github/ISSUE_TEMPLATE.md @@ -9,6 +9,7 @@ Search open/closed issues before submitting since someone might have asked the s ### 🎛 Configuration (.babelrc, package.json, cli command) + ```js @@ -18,31 +19,41 @@ Search open/closed issues before submitting since someone might have asked the s ``` ### 🤔 Expected Behavior + + ### 😯 Current Behavior + + + ### 💁 Possible Solution + + ### 🔦 Context + + ### 🌍 Your Environment + -| Software | Version(s) -| ---------------- | ---------- -| Parcel | -| Node | -| npm/Yarn | -| Operating System | +| Software | Version(s) | +| ---------------- | ---------- | +| Parcel | +| Node | +| npm/Yarn | +| Operating System | \ No newline at end of file +👉 https://opencollective.com/parcel/donate --> diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 357d881faa1..fb327ce89a3 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -35,29 +35,24 @@ yarn test [node]: https://nodejs.org/ [yarn]: https://yarnpkg.com/ - ## Financial contributions We also welcome financial contributions in full transparency on our [open collective](https://opencollective.com/parcel). Anyone can file an expense. If the expense makes sense for the development of the community, it will be "merged" in the ledger of our open collective by the core contributors and the person who filed the expense will be reimbursed. - ## Credits - ### Contributors Thank you to all the people who have already contributed to parcel! - ### Backers Thank you to all our backers! [[Become a backer](https://opencollective.com/parcel#backer)] - ### Sponsors Thank you to all our sponsors! (please ask your company to also support this open source project by [becoming a sponsor](https://opencollective.com/parcel#sponsor)) @@ -71,4 +66,4 @@ Thank you to all our sponsors! (please ask your company to also support this ope - \ No newline at end of file + diff --git a/README.md b/README.md index f5c19afd3e2..5889dff1ffe 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,9 @@ ```shell yarn global add parcel-bundler ``` + or with npm: + ```shell npm install -g parcel-bundler ``` @@ -97,14 +99,12 @@ All feedback and suggestions are welcome! This project exists thanks to all the people who contribute. [[Contribute]](CONTRIBUTING.md). - ## Backers Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/parcel#backer)] - ## Sponsors Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/parcel#sponsor)] @@ -120,8 +120,6 @@ Support this project by becoming a sponsor. Your logo will show up here with a l - - ## License MIT diff --git a/package.json b/package.json index 78fd7dbb5be..8957db0d5e6 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "uglify-es": "^3.2.1", "v8-compile-cache": "^1.1.0", "worker-farm": "^1.4.1", - "ws": "^3.3.2" + "ws": "^3.3.3" }, "devDependencies": { "babel-cli": "^6.26.0", diff --git a/src/HMRServer.js b/src/HMRServer.js index 47a0053ba6a..8ce9b79832d 100644 --- a/src/HMRServer.js +++ b/src/HMRServer.js @@ -8,11 +8,14 @@ class HMRServer { }); this.wss.on('connection', ws => { + ws.onerror = this.handleSocketError; if (this.unresolvedError) { ws.send(JSON.stringify(this.unresolvedError)); } }); + this.wss.on('error', this.handleSocketError); + return this.wss._server.address().port; } @@ -69,6 +72,15 @@ class HMRServer { } } + handleSocketError(err) { + if (err.code === 'ECONNRESET') { + // This gets triggered on page refresh, ignore this + return; + } + // TODO: Use logger to print errors + console.log(prettyError(err)); + } + broadcast(msg) { const json = JSON.stringify(msg); for (let ws of this.wss.clients) { diff --git a/src/builtins/hmr-runtime.js b/src/builtins/hmr-runtime.js index df035a4b5cc..15ba3bc02bc 100644 --- a/src/builtins/hmr-runtime.js +++ b/src/builtins/hmr-runtime.js @@ -32,7 +32,10 @@ if (!module.bundle.parent) { } if (data.type === 'reload') { - window.location.reload(); + ws.close(); + ws.onclose = () => { + window.location.reload(); + } } if (data.type === 'error-resolved') { diff --git a/test/hmr.js b/test/hmr.js index ba54a1806be..a7a6dc592d3 100644 --- a/test/hmr.js +++ b/test/hmr.js @@ -14,15 +14,24 @@ describe('hmr', function() { rimraf.sync(__dirname + '/input'); }); - afterEach(function() { - if (b) { - b.stop(); - b = null; - } + afterEach(function(done) { + let finalise = () => { + if (b) { + b.stop(); + b = null; + + done(); + } + }; if (ws) { ws.close(); - ws = null; + ws.onclose = () => { + ws = null; + finalise(); + }; + } else { + finalise(); } }); diff --git a/test/plugins.js b/test/plugins.js index 54e9296787e..142716f960f 100644 --- a/test/plugins.js +++ b/test/plugins.js @@ -16,7 +16,9 @@ describe('plugins', function() { }); it('should load package.json from parent tree', async function() { - let b = await bundle(__dirname + '/integration/plugins/sub-folder/index.js'); + let b = await bundle( + __dirname + '/integration/plugins/sub-folder/index.js' + ); assertBundleTree(b, { name: 'index.js', diff --git a/yarn.lock b/yarn.lock index 75625233d5a..5741c3a066e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4934,9 +4934,9 @@ write-file-atomic@^1.1.4: imurmurhash "^0.1.4" slide "^1.1.5" -ws@^3.3.2: - version "3.3.2" - resolved "https://registry.yarnpkg.com/ws/-/ws-3.3.2.tgz#96c1d08b3fefda1d5c1e33700d3bfaa9be2d5608" +ws@^3.3.3: + version "3.3.3" + resolved "https://registry.yarnpkg.com/ws/-/ws-3.3.3.tgz#f1cf84fe2d5e901ebce94efaece785f187a228f2" dependencies: async-limiter "~1.0.0" safe-buffer "~5.1.0"