Skip to content

Commit

Permalink
feat: replace xhr-write-stream with socket.io
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed Jun 19, 2019
1 parent 17f9b5c commit 9be4d62
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
16 changes: 8 additions & 8 deletions lib/browser-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
const http = require('http');
const path = require('path');
const fs = require('fs');
const xws = require('xhr-write-stream')();
const socketIo = require('socket.io');
const launch = require('./launch');
const serveStatic = require('serve-static');
const finalhandler = require('finalhandler');
Expand Down Expand Up @@ -117,13 +117,6 @@ function runner (opts, data, output) {
}
}

if (req.url == '/xws') {
req.pipe(xws(function (stream) {
stream.pipe(output);
}));
return req.on('end', res.end.bind(res));
}

if (req.url == '/reporter.js') {
res.setHeader('content-type', 'application/javascript');
fs.createReadStream(reporterPath).pipe(res);
Expand Down Expand Up @@ -175,6 +168,13 @@ function runner (opts, data, output) {

res.end('not supported');
});

const io = socketIo(server);

io.on('connection', socket => {
socket.on('log', msg => output.write(msg + '\n'));
});

destroyable(server);

let browserProc;
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
"eslint": "^5.16.0",
"jasmine-core": "^3.4.0",
"mocha": "^6.1.4",
"socket.io-client": "^2.2.0",
"source-map-support": "^0.5.12",
"standard-changelog": "^2.0.11",
"tape": "^4.10.2",
"utf8-stream": "0.0.0"
"tape": "^4.10.2"
},
"dependencies": {
"commander": "^2.20.0",
Expand All @@ -58,9 +58,9 @@
"lodash.kebabcase": "^4.1.1",
"serve-static": "^1.14.1",
"server-destroy": "^1.0.1",
"socket.io": "^2.2.0",
"through": "^2.3.8",
"tmp": "^0.1.0",
"which": "^1.3.1",
"xhr-write-stream": "^0.1.2"
"which": "^1.3.1"
}
}
26 changes: 14 additions & 12 deletions reporter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global window */
// copied from browser-run
// changed xhr-write-stream to socket.io

require('source-map-support').install();

Expand All @@ -18,20 +19,21 @@ window.onerror = function (msg, file, line, column, err) {
console.error(err && err.stack || err);
};

var xws = require('xhr-write-stream')('/xws');
// buffer utf8 characters that would otherwise span chunk boundaries
var ws = require('utf8-stream')();
ws.pipe(xws);
var io = require('socket.io-client')();

var console = window.console || {};
var methods = ['log', 'error', 'warn', 'dir', 'debug', 'info', 'trace'];
for (var i = 0; i < methods.length; i++) (function (method) {

function patch(method) {
var old = console[method];
console[method] = function(msg) {
ws.write(Array.prototype.slice.call(arguments, 0).join(' ') + '\n');
console[method] = function() {
const message = Array.prototype.slice.call(arguments, 0).join(' ');
if (message) io.emit(method, message);
if (old) old.apply(console, arguments);
if (msg instanceof Error && typeof JSON != 'undefined') {
ws.write(JSON.stringify(msg) + '\n');
}
};
})(methods[i]);
}

var methods = ['log', 'error', 'warn', 'dir', 'debug', 'info', 'trace'];
var i;
for (i = 0; i < methods.length; i++) {
patch(methods[i]);
}

0 comments on commit 9be4d62

Please sign in to comment.