Skip to content

Commit 25c0386

Browse files
committed
Use ipc instead of our custom bufferedBySeperator code.
Closes #55.
1 parent 5aaeb25 commit 25c0386

File tree

6 files changed

+21
-118
lines changed

6 files changed

+21
-118
lines changed

lib/worker/messages.js

-38
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,3 @@
1-
var BufferedBySeperatorHandler = (function () {
2-
function BufferedBySeperatorHandler(callback) {
3-
this.callback = callback;
4-
this.totalTrailing = [];
5-
this.incompleteEnding = '';
6-
}
7-
BufferedBySeperatorHandler.prototype.handle = function (data) {
8-
var m = data.toString();
9-
if (!m && this.totalTrailing.length) {
10-
m = this.totalTrailing.shift();
11-
if (this.totalTrailing.length == 1 && this.incompleteEnding) {
12-
m = m + this.incompleteEnding;
13-
this.incompleteEnding = '';
14-
this.handle(m);
15-
return;
16-
}
17-
}
18-
else {
19-
m = m.toString();
20-
var parts = m.split(BufferedBySeperatorHandler.seperator);
21-
if (parts.length == 2 && parts[1] == '') {
22-
}
23-
else if (parts.length > 1) {
24-
var more = parts.slice(1, parts.length - 1);
25-
this.totalTrailing = this.totalTrailing.concat(more);
26-
this.incompleteEnding = parts[parts.length - 1];
27-
}
28-
m = parts[0];
29-
}
30-
this.callback(m);
31-
if (this.totalTrailing.length) {
32-
this.handle('');
33-
}
34-
};
35-
BufferedBySeperatorHandler.seperator = new Buffer('fHxhdG9tdHN8fA==', 'base64').toString();
36-
return BufferedBySeperatorHandler;
37-
})();
38-
exports.BufferedBySeperatorHandler = BufferedBySeperatorHandler;
391
exports.orphanExitCode = 100;
402
exports.echo = 'echo';
413
exports.updateText = 'updateText';

lib/worker/messages.ts

-51
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,3 @@
1-
// To make it easier for me to serilize json on stdin
2-
export class BufferedBySeperatorHandler {
3-
static seperator = new Buffer('fHxhdG9tdHN8fA==', 'base64').toString(); // Mustn't speak his name
4-
totalTrailing: string[] = [];
5-
incompleteEnding = '';
6-
constructor(public callback: (data: any) => any) { }
7-
8-
handle(data: any) {
9-
var m = data.toString(); // Fix this and use encoding.
10-
if (!m && this.totalTrailing.length) { // process totalTrailing
11-
m = this.totalTrailing.shift();
12-
13-
// If length == 1. We should account for our previous incompleteEnding
14-
if (this.totalTrailing.length == 1 && this.incompleteEnding) {
15-
m = m + this.incompleteEnding;
16-
this.incompleteEnding = '';
17-
18-
// Adding an incompleteEnding might have made it *two* messages
19-
// so keep processing
20-
this.handle(m);
21-
return;
22-
}
23-
}
24-
else {
25-
m = m.toString();
26-
var parts = m.split(BufferedBySeperatorHandler.seperator);
27-
if (parts.length == 2 && parts[1] == '') { // Great the perfect match!
28-
}
29-
else if (parts.length > 1) { // oh oh ... an unperfect match and we have more than 1 items
30-
// -1 because the last part is just because we always end on trailer
31-
// Potential TODO: If the last part isn't "seperator" then we have an incomplete part
32-
33-
var more = parts.slice(1, parts.length - 1);
34-
this.totalTrailing = this.totalTrailing.concat(more);
35-
36-
// If the last part is not "" we have an imperfect ending
37-
this.incompleteEnding = parts[parts.length - 1];
38-
}
39-
m = parts[0];
40-
}
41-
42-
// TADA
43-
this.callback(m);
44-
45-
// Process remaining
46-
if (this.totalTrailing.length) {
47-
this.handle('');
48-
}
49-
}
50-
}
51-
521
export var orphanExitCode = 100;
532

543
// Parent makes queries

lib/worker/parent.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ function startWorker() {
1212
var node = os.platform() === 'win32' ? "node" : process.execPath;
1313
child = spawn(node, [
1414
__dirname + '/workerProcess.js',
15-
], { env: env });
15+
], { env: env, stdio: ['ipc'] });
1616
child.on('error', function (err) {
1717
console.log('CHILD ERR:', err.toString());
1818
child = null;
1919
});
2020
console.log('ts worker started');
2121
function processResponse(m) {
2222
try {
23-
var parsed = JSON.parse(m.toString());
23+
var parsed = m;
2424
}
2525
catch (ex) {
2626
console.log('PARENT ERR: Non JSON data from child:', m);
@@ -34,10 +34,7 @@ function startWorker() {
3434
delete currentListeners[parsed.message][parsed.id];
3535
}
3636
}
37-
var bufferedResponseHandler = new messages.BufferedBySeperatorHandler(processResponse);
38-
child.stdout.on('data', function (m) {
39-
bufferedResponseHandler.handle(m);
40-
});
37+
child.on('message', function (resp) { return processResponse(resp); });
4138
child.stderr.on('data', function (err) {
4239
console.log("CHILD ERR:", err.toString());
4340
});
@@ -80,7 +77,7 @@ function createId() {
8077
});
8178
}
8279
function query(message, data) {
83-
if (!child || !child.stdin.writable) {
80+
if (!child) {
8481
console.log('PARENT ERR: no child when you tried to send :', message);
8582
return;
8683
}
@@ -89,7 +86,7 @@ function query(message, data) {
8986
var id = createId();
9087
var defer = Promise.defer();
9188
currentListeners[message][id] = defer;
92-
child.stdin.write(JSON.stringify({ message: message, id: id, data: data }) + messages.BufferedBySeperatorHandler.seperator);
89+
child.send({ message: message, id: id, data: data });
9390
return defer.promise;
9491
}
9592
function showError(error) {

lib/worker/parent.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ export function startWorker() {
2121
child = spawn(node, [
2222
// '--debug', // Uncomment if you want to debug the child process
2323
__dirname + '/workerProcess.js',
24-
], { env });
24+
], { env:env, stdio:['ipc'] });
2525

2626
child.on('error',(err) => {
2727
console.log('CHILD ERR:', err.toString());
2828
child = null;
2929
});
3030

3131
console.log('ts worker started');
32-
function processResponse(m: string) {
32+
function processResponse(m: any) {
3333
try {
34-
var parsed: messages.Message<any> = JSON.parse(m.toString());
34+
var parsed: messages.Message<any> = m;
3535
}
3636
catch (ex) {
3737
console.log('PARENT ERR: Non JSON data from child:', m);
@@ -45,11 +45,8 @@ export function startWorker() {
4545
delete currentListeners[parsed.message][parsed.id];
4646
}
4747
}
48-
var bufferedResponseHandler = new messages.BufferedBySeperatorHandler(processResponse);
4948

50-
child.stdout.on('data',(m) => {
51-
bufferedResponseHandler.handle(m)
52-
});
49+
child.on('message',(resp)=>processResponse(resp));
5350

5451

5552
child.stderr.on('data',(err) => {
@@ -101,7 +98,7 @@ function createId(): string {
10198
function query<Query, Response>(message: string, data: Query): Promise<Response> {
10299

103100
// If we don't have a child exit
104-
if (!child || !child.stdin.writable) {
101+
if (!child) {
105102
console.log('PARENT ERR: no child when you tried to send :', message);
106103
return;
107104
}
@@ -115,7 +112,7 @@ function query<Query, Response>(message: string, data: Query): Promise<Response>
115112
currentListeners[message][id] = defer;
116113

117114
// Send data to worker
118-
child.stdin.write(JSON.stringify({ message: message, id: id, data: data }) + messages.BufferedBySeperatorHandler.seperator);
115+
child.send({ message: message, id: id, data: data });
119116
return defer.promise;
120117
}
121118

lib/worker/workerProcess.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,19 @@ setInterval(function () {
88
}, 1000);
99
var responders = {};
1010
function processData(m) {
11-
var parsed = JSON.parse(m);
11+
var parsed = m;
1212
if (!parsed.message || !responders[parsed.message])
1313
return;
1414
var message = parsed.message;
15-
process.stdout.write(JSON.stringify({
15+
process.send({
1616
message: message,
1717
id: parsed.id,
1818
data: responders[message](parsed.data)
19-
}) + messages.BufferedBySeperatorHandler.seperator);
19+
});
2020
}
21-
var bufferedHandler = new messages.BufferedBySeperatorHandler(processData);
22-
process.stdin.on('data', function (data) {
21+
process.on('message', function (data) {
2322
gotMessageDate = new Date();
24-
bufferedHandler.handle(data);
23+
processData(data);
2524
});
2625
var programManager = require('../main/lang/programManager');
2726
responders[messages.echo] = function (data) {

lib/worker/workerProcess.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,21 @@ var responders: { [message: string]: (query: any) => any } = {};
1717

1818
// Note: child doesn't care about 'id'
1919
function processData(m: any) {
20-
var parsed: messages.Message<any> = JSON.parse(m);
20+
var parsed: messages.Message<any> = m;
2121
if (!parsed.message || !responders[parsed.message]) return; // TODO: handle this error scenario
2222
var message = parsed.message;
2323

24-
process.stdout.write(JSON.stringify({
24+
process.send({
2525
message: message,
2626
id: parsed.id,
2727
data: responders[message](parsed.data)
28-
}) + messages.BufferedBySeperatorHandler.seperator);
28+
});
2929
}
3030

31-
var bufferedHandler = new messages.BufferedBySeperatorHandler(processData)
32-
process.stdin.on('data',(data) => {
31+
process.on('message',(data) => {
3332
gotMessageDate = new Date();
3433
// console.log('child got:', data.toString());
35-
bufferedHandler.handle(data)
34+
processData(data)
3635
});
3736

3837
///////////////// END INFRASTRUCTURE /////////////////////////////////////

0 commit comments

Comments
 (0)