forked from royaltm/node-zmq-raft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframes_protocol.js
345 lines (306 loc) · 11.3 KB
/
frames_protocol.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* Copyright (c) 2016 Rafał Michalski <[email protected]>
*/
"use strict";
const isBuffer = Buffer.isBuffer
, isArray = Array.isArray;
const msgpack = require('msgpack-lite');
const bufconv = require('../utils/bufconv')
, intEncode = bufconv.allocBufIntLE
, uintEncode = bufconv.allocBufUIntLE
, numberEncode = bufconv.allocBufNumberLE
, boolEncode = bufconv.boolToBuffer
, readBool = bufconv.bufferToBool
, readUInt = bufconv.readBufUIntLE
, readInt = bufconv.readBufIntLE
, readNumber = bufconv.readBufNumberLE
const { ZmqSocket } = require('../utils/zmqsocket');
const debug = require('debug')('zmq-raft:frames-protocol');
class FramesProtocol {
/**
* Create FramesProtocol
*
* `options` may be one of:
*
* - `name` {string}: used for protocol identification in error messages
* - `required` {number|Array}: how many frame elements are required,
* if array first element for request and the second for response
* - `extraArgs` {boolean|Array}: pass additional args exceedeing schema
* if array first element for request and the second for response
*
* @param {Array} requestSchema
* @param {Array} responseSchema
* @param {Object} options
* @return {FramesProtocol}
**/
constructor(requestSchema, responseSchema, options) {
if (!isArray(requestSchema)) throw new TypeError('FramesProtocol error: requestSchema must be an array');
if (!isArray(responseSchema)) throw new TypeError('FramesProtocol error: responseSchema must be an array');
options || (options = {});
var required = options.required;
if (!isArray(required)) required = [required, required];
var extraArgs = options.extraArgs;
if (!isArray(extraArgs)) extraArgs = [extraArgs, extraArgs];
this.name = String(options.name || 'unnamed');
this.encodeRequest = createSchemaEncoder(requestSchema, required[0]|0, !!extraArgs[0]);
this.decodeRequest = createSchemaDecoder(requestSchema, required[0]|0, !!extraArgs[0]);
this.encodeResponse = createSchemaEncoder(responseSchema, required[1]|0, !!extraArgs[1]);
this.decodeResponse = createSchemaDecoder(responseSchema, required[1]|0, !!extraArgs[1]);
}
toString() {
return this.name + ' protocol';
}
/**
* Creates specialized request function for RpcSocket instance
*
* @param {RpcSocket} rpc
* @return {Function}
**/
createRequestFunctionFor(rpc) {
if (!rpc || 'object' !== typeof rpc) throw new TypeError(`${this.name}: rpc must be an object`);
if ('function' !== typeof rpc.request) throw new TypeError(`${this.name}: rpc must have request method`);
const encodeRequest = this.encodeRequest;
const decodeResponse = this.decodeResponse;
/* create unbound request function */
return (req, opt) => rpc.request(encodeRequest(req), opt).then(resp => decodeResponse(resp));
}
/**
* Creates specialized router message listener and attaches it to 'frames' event
*
* @param {zmq.Socket} router
* @param {Function} handler(reply{Function}, decodedArgs{Array})
* @param {Object} [context] handler's calling context
* @return {Function} listener for removing from events
**/
createRouterMessageListener(router, handler, context) {
if (!(router instanceof ZmqSocket)) throw new TypeError(`${this.name}: router must be an instance of ZmqSocket`);
const name = this.name;
const decodeRequest = this.decodeRequest;
const encodeResponse = this.encodeResponse;
var listener = (args) => {
var ident = args.shift(), requestId = args.shift();
try {
if (ident === undefined) throw new Error("router received message without ident frame")
if (requestId === undefined) throw new Error("router received message without requestId frame")
args = decodeRequest(args);
} catch(err) {
/* prevent easy DDoS */
debug('ERROR decoding %s: %s', this, err);
return;
}
var payload = [ident, requestId];
var reply = (args) => {
router.send(payload.concat(encodeResponse(args)));
};
reply.requestId = requestId;
reply.ident = ident;
handler.call(context, reply, args);
};
router.on('frames', listener);
return listener;
}
/**
* Creates specialized subscriber message listener and attaches it to 'frames' event
*
* @param {zmq.Socket} sub
* @param {Function} handler(decodedArgs{Array})
* @param {Object} [context] handler's calling context
* @return {Function} listener for removing from events
**/
createSubMessageListener(sub, handler, context) {
if (!(sub instanceof ZmqSocket)) throw new TypeError(`${this.name}: sub must be an instance of ZmqSocket`);
const name = this.name;
const decodeRequest = this.decodeRequest;
var listener = (args) => {
try {
args = decodeRequest(args);
} catch(err) {
/* prevent easy DDoS */
debug('ERROR decoding %s: %s', this, err);
return;
}
handler.call(context, args);
};
sub.on('frames', listener);
return listener;
}
/**
* Creates specialized fan out send function for (preferably ZMQ_PUB) socket instance
*
* @param {zmq.socket} pub
* @return {Function}
**/
createSendFunctionFor(pub) {
if (!(pub instanceof ZmqSocket)) throw new TypeError(`${this.name}: pub must be an instance of ZmqSocket`);
const encodeRequest = this.encodeRequest;
/* create unbound request function */
return (req, flags) => pub.send(encodeRequest(req), flags);
}
}
module.exports = exports = FramesProtocol;
const emptyFrameCoder = () => [];
const emptyFrameEncoderArgs = (args) => isArray(args) ? args : args === undefined ? [] : [args];
const encoderBodies = {
'string': 'buf("string"===typeof aN ? aN : String(aN))',
'utf8': 'buf("string"===typeof aN ? aN : String(aN), "utf8")',
'utf-8': 'buf("string"===typeof aN ? aN : String(aN), "utf8")',
'hex': 'buf("string"===typeof aN ? aN : String(aN), "hex")',
'base64': 'buf("string"===typeof aN ? aN : String(aN), "base64")',
'binary': 'buf("string"===typeof aN ? aN : String(aN), "binary")',
'latin1': 'buf("string"===typeof aN ? aN : String(aN), "latin1")',
'utf16le': 'buf("string"===typeof aN ? aN : String(aN), "utf16le")',
'ucs2': 'buf("string"===typeof aN ? aN : String(aN), "ucs2")',
'ascii': 'buf("string"===typeof aN ? aN : String(aN), "ascii")',
'bool': 'boolEncode(aN)',
'boolean': 'boolEncode(aN)',
'unsigned': 'uintEncode(aN, true)',
'uint': 'uintEncode(aN, true)',
'nuint': 'uintEncode(aN)',
'integer': 'intEncode(aN, true)',
'int': 'intEncode(aN, true)',
'nint': 'intEncode(aN)',
'number': 'numberEncode(aN, true)',
'nnumber': 'numberEncode(aN)',
'object': 'encode(aN)',
'json': 'encode(aN)',
'buffer': 'buf(aN)'
};
function createSchemaEncoder(schema, required, extraArgs) {
if (required > schema.length) throw new TypeError('encoder schema error: too much required arguments');
if (schema.length === 0) {
return extraArgs ? emptyFrameEncoderArgs : emptyFrameCoder;
}
var extraBody = '';
var handleSingleArg;
var components = schema.map((type, index) => {
var body = encoderBodies[type];
if (!body) throw new TypeError('encoder schema error: unknown type: ' + type);
body = 'aN instanceof Buffer ? aN : ' + body;
if (index === 0) {
handleSingleArg = body.replace(/aN/g, 'args');
}
if (index >= required) {
body = `if (aN === undefined) return frms; frms.push(${body});`;
}
return body.replace(/aN/g, 'a' + index);
});
if (extraArgs) {
extraBody = 'if (args.length > size) return frms.concat(args.slice(size)); ';
}
if (required > 1) {
handleSingleArg = 'throw new Error("encode frames error: not enough arguments");'
}
else if (required === 1) {
handleSingleArg = `if (args === undefined) throw new Error("encode frames error: not enough arguments");
return [${handleSingleArg}];`;
}
else handleSingleArg = `return args === undefined ? [] : [${handleSingleArg}];`;
return new Function(
'size',
'required',
'isArray',
'buf',
'encode',
'intEncode',
'uintEncode',
'numberEncode',
'boolEncode',
`return (args) => {
if (!isArray(args)) {
${handleSingleArg}
}
if (args.length < required) throw new Error("encode frames error: not enough arguments");
var ${schema.map((_,i) => 'a' + i + ' = args[' + i + ']').join(',')}, frms = [${components.slice(0, required).join(',')}];
${components.slice(required).join('\n ')}
${extraBody}return frms;
}`)(
schema.length,
required,
isArray,
Buffer.from,
msgpack.encode,
intEncode,
uintEncode,
numberEncode,
boolEncode);
}
const emptyFrameDecoderArgs = (args) => args;
const decoderBodies = {
'string': 'aN.toString()',
'utf8': 'aN.toString("utf8")',
'utf-8': 'aN.toString("utf8")',
'hex': 'aN.toString("hex")',
'base64': 'aN.toString("base64")',
'binary': 'aN.toString("binary")',
'latin1': 'aN.toString("latin1")',
'utf16le': 'aN.toString("utf16le")',
'ucs2': 'aN.toString("ucs2")',
'ascii': 'aN.toString("ascii")',
'bool': 'readBool(aN)',
'boolean': 'readBool(aN)',
'unsigned': 'readUInt(aN)',
'uint': 'readUInt(aN)',
'nuint': 'readUInt(aN)',
'integer': 'readInt(aN)',
'int': 'readInt(aN)',
'nint': 'readInt(aN)',
'number': 'readNumber(aN)',
'nnumber': 'readNumber(aN)',
'object': 'decode(aN)',
'json': 'decode(aN)',
'buffer': 'aN'
};
const errorOnNull = {
'unsigned': 'decode frames error: unsigned integer must not be null at frame ',
'uint': 'decode frames error: unsigned integer must not be null at frame ',
'int': 'decode frames error: integer must not be null at frame ',
'integer': 'decode frames error: integer must not be null at frame ',
'number': 'decode frames error: number must not be null at frame '
};
function createSchemaDecoder(schema, required, extraArgs) {
if (required > schema.length) throw new TypeError('decoder schema error: too much required arguments');
if (schema.length === 0) {
return extraArgs ? emptyFrameDecoderArgs : emptyFrameCoder;
}
var extraBody = '';
var components = schema.map((type, index) => {
var body = decoderBodies[type]
, nullErrMsg = errorOnNull[type];
if (!body) throw new TypeError('decoder schema error: unknown type: ' + type);
if (nullErrMsg) {
body = `((aN = ${body}) === null ? nullError(${JSON.stringify(nullErrMsg + (index+1))}) : aN)`;
}
if (index >= required) {
body = `aN === undefined ? undefined : ${body}`;
}
return body.replace(/aN/g, 'a' + index);
});
if (extraArgs) {
extraBody = 'if (frms.length > size) return args.concat(frms.slice(size)); ';
}
return new Function(
'size',
'required',
'decode',
'readInt',
'readUInt',
'readNumber',
'readBool',
'nullError',
`return (frms) => {
if (frms.length < required) throw new Error("decode frames error: not enough frames");
var ${schema.map((_,i) => 'a' + i + ' = frms[' + i + ']').join(',')}, args = [${components.join(',')}];
${extraBody}return args;
}`)(
schema.length,
required,
msgpack.decode,
readInt,
readUInt,
readNumber,
readBool,
throwTypeError);
}
function throwTypeError(msg) {
throw new TypeError(msg);
}