Skip to content

Commit

Permalink
fix: do not send data when socket is not READY (#128)
Browse files Browse the repository at this point in the history
Follow up to #113.

Even with the above fix, I encountered a similar issue, this time in the `invalid` handler:

```
uncaughtException Error: WebSocket is not open: readyState 2 (CLOSING)
     at WebSocket.send (/app/node_modules/webpack-plugin-serve/node_modules/ws/lib/websocket.js:322:19)
     at WebpackPluginServe.socket.invalid (/app/node_modules/webpack-plugin-serve/lib/routes.js:83:16)
     at WebpackPluginServe.emit (events.js:189:13)
     at invalid.tap (/app/node_modules/webpack-plugin-serve/lib/index.js:155:41)
     at SyncHook.eval (eval at create (/app/node_modules/webpack/node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:7:1)
     at Watchpack.watcher.compiler.watchFileSystem.watch (/app/node_modules/webpack/lib/Watching.js:139:33)
     at Object.onceWrapper (events.js:277:13)
     at Watchpack.emit (events.js:189:13)
     at Watchpack._onChange (/app/node_modules/watchpack/lib/watchpack.js:118:7)
     at Watchpack.<anonymous> (/app/node_modules/watchpack/lib/watchpack.js:99:8)
     at Watcher.emit (events.js:189:13)
     at /app/node_modules/watchpack/lib/DirectoryWatcher.js:109:7
     at Array.forEach (<anonymous>)
     at DirectoryWatcher.setFileTime (/app/node_modules/watchpack/lib/DirectoryWatcher.js:108:41)
     at DirectoryWatcher.onChange (/app/node_modules/watchpack/lib/DirectoryWatcher.js:264:7)
     at FSWatcher.emit (events.js:189:13)
     at FSWatcher.<anonymous> (/app/node_modules/watchpack/node_modules/chokidar/index.js:199:15)
     at /app/node_modules/watchpack/node_modules/chokidar/index.js:238:7
     at FSReqWrap.oncomplete (fs.js:155:5)
```

As suggested in #113 (comment) I've wrapped `send` so the error should not come back anymore.
  • Loading branch information
niieani authored and shellscape committed May 1, 2019
1 parent 3fc8cf4 commit 3327580
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions lib/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ const setupRoutes = function setupRoutes() {
const connect = async (ctx) => {
if (ctx.ws) {
const socket = await ctx.ws();
const send = (data) => {
if (socket.readyState !== 1) {
return;
}
socket.send(data);
};

socket.build = (compilerName = '<unknown>', { wpsId }) => {
socket.send(prep({ action: 'build', data: { compilerName, wpsId } }));
send(prep({ action: 'build', data: { compilerName, wpsId } }));
};

socket.done = (stats, { wpsId }) => {
Expand All @@ -41,14 +47,14 @@ const setupRoutes = function setupRoutes() {
return;
}

socket.send(prep({ action: 'done', data: { hash, wpsId } }));
send(prep({ action: 'done', data: { hash, wpsId } }));

socket.lastHash = hash;

const { errors = [], warnings = [] } = stats.toJson(statsOptions);

if (errors.length || warnings.length) {
socket.send(
send(
prep({
action: 'problems',
data: {
Expand All @@ -67,27 +73,20 @@ const setupRoutes = function setupRoutes() {

if (options.hmr || options.liveReload) {
const action = options.liveReload ? 'reload' : 'replace';
socket.send(prep({ action, data: { hash, wpsId } }));
send(prep({ action, data: { hash, wpsId } }));
}
};

socket.invalid = (filePath = '<unknown>', compiler) => {
if (socket.readyState === 3) {
return;
}

const context = compiler.context || compiler.options.context || process.cwd();
const fileName = filePath.replace && filePath.replace(context, '') || filePath;
const { wpsId } = compiler;

socket.send(prep({ action: 'invalid', data: { fileName, wpsId } }));
send(prep({ action: 'invalid', data: { fileName, wpsId } }));
};

socket.progress = (data) => {
if (socket.readyState !== 1) {
return;
}
socket.send(prep({ action: 'progress', data }));
send(prep({ action: 'progress', data }));
};

for (const event of events) {
Expand All @@ -98,7 +97,7 @@ const setupRoutes = function setupRoutes() {
});
}

socket.send(prep({ action: 'connected' }));
send(prep({ action: 'connected' }));
}
};

Expand Down

0 comments on commit 3327580

Please sign in to comment.