Skip to content

Commit

Permalink
perf: remove through and duplexer
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed May 15, 2020
1 parent 07202ec commit 4b11b1e
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 70 deletions.
84 changes: 45 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,60 @@
/* eslint-disable no-console */
const run = require('./lib/browser-run');
const through = require('through');
const {Writable} = require('stream');
const duplex = require('duplexer');
const {Transform} = require('stream');
const tapParse = require('./lib/tap-parse');

module.exports = function(opts = {}) {
const tap = opts.tap || opts.tape || opts.jasmine || opts.mocha;

const chunks = [];
const readInput = new Writable({
write(chunk, enc, cb) {
const dpl = new Transform({
transform(chunk, enc, cb) {
chunks.push(chunk);
cb();
}
});

const holdOutput = through();
const dpl = duplex(readInput, holdOutput);

dpl.failed = false;
let browserDo;
dpl.stop = () => browserDo && browserDo.stop();

readInput.on('finish', () => {
const data = Buffer.concat(chunks).toString();
browserDo = run(opts, data, holdOutput);

if (tap) {
tapParse(holdOutput, (err, passed) => {
dpl.failed = !passed;

if (err) {
console.error(err.message);
}

if (opts.onCoverage) {
browserDo.askCoverage();
}

if (!opts.keepOpen) {
setTimeout(() => {
if (opts.onCoverage) {
browserDo.checkCoverage(opts.onCoverage);
}
dpl.stop();
dpl.emit('exit', dpl.failed ? 1 : 0);
}, 1000);
},
flush(cb) {
const self = this;
let failed = false;
let browserDo;
self.stop = () => browserDo && browserDo.stop();
const holdOutput = new Transform({
transform(chunk, enc, _cb) {
_cb(null, chunk);
self.push(chunk);
},
flush(_cb) {
if (!tap) cb();
_cb();
}
});

const data = Buffer.concat(chunks).toString();
browserDo = run(opts, data, holdOutput);

if (tap) {
tapParse(holdOutput, (err, passed) => {
failed = !passed;

if (err) {
console.error(err.message);
}

if (opts.onCoverage) {
browserDo.askCoverage();
}

if (!opts.keepOpen) {
setTimeout(() => {
if (opts.onCoverage) {
browserDo.checkCoverage(opts.onCoverage);
}
self.stop();
cb();
self.emit('exit', failed ? 1 : 0);
}, 1000);
}
});
}
}
});

Expand Down
5 changes: 1 addition & 4 deletions lib/browsers/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ module.exports = function() {
args: [
'--user-data-dir=' + tmpobj.name,
...args
],
onExit() {
tmpobj.removeCallback();
}
]
};
};
5 changes: 1 addition & 4 deletions lib/browsers/chromium.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ module.exports = function() {
args: [
'--user-data-dir=' + tmpobj.name,
...args
],
onExit() {
tmpobj.removeCallback();
}
]
};
};
5 changes: 1 addition & 4 deletions lib/browsers/edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ module.exports = function() {
args: [
'--user-data-dir=' + tmpobj.name,
...args
],
onExit() {
tmpobj.removeCallback();
}
]
};
};
5 changes: 1 addition & 4 deletions lib/browsers/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ module.exports = function() {
args: [
path.join(__dirname, 'electron-runner.js'),
tmpobj.name
],
onExit() {
tmpobj.removeCallback();
}
]
};
};
5 changes: 1 addition & 4 deletions lib/browsers/firefox.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ module.exports = function() {
'-profile',
tmpobj.name,
'-no-remote'
],
onExit() {
tmpobj.removeCallback();
}
]
};
};
18 changes: 8 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,30 @@
},
"homepage": "https://github.com/3cp/browser-do#readme",
"devDependencies": {
"browserify": "^16.5.0",
"browserify": "^16.5.1",
"cat": "^0.2.0",
"chai": "^4.2.0",
"concat-stream": "^2.0.0",
"es6ify": "^1.6.0",
"eslint": "^6.8.0",
"jasmine-core": "^3.5.0",
"mocha": "^7.1.0",
"mocha": "^7.1.2",
"socket.io-client": "^2.3.0",
"source-map-support": "^0.5.16",
"standard-changelog": "^2.0.21",
"tape": "^4.13.2"
"source-map-support": "^0.5.19",
"standard-changelog": "^2.0.24",
"tape": "^5.0.0"
},
"dependencies": {
"ansi-colors": "^4.1.1",
"commander": "^4.1.1",
"duplexer": "^0.1.1",
"electron": "^8.1.0",
"commander": "^5.1.0",
"electron": "^8.3.0",
"finalhandler": "^1.1.2",
"lodash.kebabcase": "^4.1.1",
"lodash.once": "^4.1.1",
"serve-static": "^1.14.1",
"server-destroy": "^1.0.1",
"socket.io": "^2.3.0",
"through": "^2.3.8",
"tmp": "^0.1.0",
"tmp": "^0.2.1",
"which": "^2.0.2"
}
}
7 changes: 6 additions & 1 deletion test/tap-parse.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const test = require('tape');
const {Transform} = require('stream');
const tapParse = require('../lib/tap-parse');
const through = require('through');
const through = () => new Transform({
transform(chunk, enc, cb) {
cb(null, chunk);
}
});

test('tapParse passes all ok', t => {
const inp = through();
Expand Down

0 comments on commit 4b11b1e

Please sign in to comment.