-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
202 lines (175 loc) · 5.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
'use strict';
class Jmsg {
constructor(writeFn, handlers) {
this.handlers = handlers || {};
this.timeout = 60000;
this._callbacks = [];
this._writeFn = writeFn;
}
// Remove undefineds off the end of the callback list.
_trimCallbacks() {
const callbacks = this._callbacks;
let i = callbacks.length - 1;
while (i >= 0 && callbacks[i] === undefined)
i--;
callbacks.length = i + 1;
}
// Send a JSON message.
send(a, b, c, d) {
if (!this._writeFn)
exports.dummySend(a, b, c, d);
// type, value, [cb], [handle]
else if (typeof(b) !== 'function')
this._sendRaw({ t: a, v: b }, c, d);
// type, [cb], [handle]
else
this._sendRaw({ t: a }, b, d);
}
_sendRaw(msg, cb, handle) {
if (typeof(cb) === 'function') {
const handlers = this._handlers;
const callbacks = this._callbacks;
let seq = 0;
while (callbacks[seq])
seq++;
msg.s = seq;
callbacks[seq] = {
fn: cb,
timeout: setTimeout(() => {
callbacks[seq] = undefined;
this._trimCallbacks();
cb.call(handlers, Error("Timeout"),
null, exports.noReplyCallback);
}, this.timeout)
};
}
this._writeFn(msg, handle);
}
// Dispatch a JSON message.
dispatch(msg, handle) {
const seq = msg.s;
const callback = seq !== undefined ? (err, val, cb, handle) => {
this._sendRaw({
r: seq,
e: errorSerializer(err),
v: val
}, cb, handle);
} : exports.noReplyCallback;
let fn, tmp;
const handlers = this.handlers;
if ((tmp = msg.r) !== undefined) {
const callbacks = this._callbacks;
fn = callbacks[tmp];
if (fn) {
callbacks[tmp] = undefined;
clearTimeout(fn.timeout);
this._trimCallbacks();
fn.fn.call(handlers, msg.e, msg.v, callback, handle);
}
else if (seq !== undefined) {
callback(Error("Unknown sequence number"));
}
}
else if ((tmp = msg.t) !== undefined) {
fn = handlers.hasOwnProperty(tmp) && handlers[tmp];
if (fn)
fn.call(handlers, msg.v, callback, handle);
else if (seq !== undefined)
callback(Error("No such action"));
}
}
// Close the instance, finishing all callbacks.
close(err) {
if (!err)
err = Error("Connection closed");
const handlers = this._handlers;
const callbacks = this._callbacks.slice(0);
this._callbacks.length = 0;
this._writeFn = null;
callbacks.forEach((fn) => {
clearTimeout(fn.timeout);
fn.fn.call(handlers, err, null, exports.noReplyCallback, null);
});
}
}
// Main export.
exports = module.exports = (writeFn, handlers) => {
return new Jmsg(writeFn, handlers);
};
// Process on a pair of readable and writable streams.
exports.streams = (readable, writable, handlers) => {
const handle = exports((msg) => {
writable.write(JSON.stringify(msg) + "\n");
}, handlers);
const carry = require('carrier').carry(readable);
carry.on('line', (msg) => {
try { msg = JSON.parse(msg); }
catch (err) { /* ignore */ }
if (typeof(msg) === 'object' && msg !== null)
handle.dispatch(msg);
});
carry.on('end', () => {
handle.close();
});
return handle;
};
// Process on a duplex stream.
exports.stream = (stream, handlers) => {
return exports.streams(stream, stream, handlers);
};
// Process cluster messages.
exports.cluster = (a, b) => {
let channel, handlers;
// worker, [handlers]
if (process.send) {
channel = process;
handlers = a;
}
// [handlers]
else {
channel = a;
handlers = b;
}
const handle = exports(channel.send.bind(channel), handlers);
channel.on('message', handle.dispatch.bind(handle));
channel.on('exit', () => {
handle.close();
});
return handle;
};
// The callback function passed to handlers when the other side doesn't
// expect a reply to the message.
exports.noReplyCallback = (err, obj, cb) => {
if (cb)
cb(Error("No reply expected"), null, exports.noReplyCallback);
};
// A dummy send function used when the connection is down. Events are silently
// dropped, and actions immediately error.
exports.dummySend = (a, b, c) => {
const cb = c || b;
if (typeof(cb) === 'function')
cb(Error("Connection closed"), null, exports.noReplyCallback);
};
// Error serialization matching bunyan. (Both MIT)
// https://github.com/trentm/node-bunyan/blob/e43a1a405f379c37d59c4227168dca0e8f41d052/lib/bunyan.js#L968-L1002
const getFullErrorStack = (ex) => {
let ret = ex.stack || ex.toString();
if (typeof(ex.cause) === 'function') {
const cex = ex.cause();
if (cex)
ret += '\nCaused by: ' + getFullErrorStack(cex);
}
return ret;
};
const errorSerializer = (v) => {
if (v && v.stack) {
v = {
message: v.message,
name: v.name,
stack: getFullErrorStack(v),
code: v.code,
signal: v.signal
};
}
return v;
};