| 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406 |
1
1
1
1
1
1
1
1
1
1
1
1
1
1344
1344
1344
1344
1344
1344
1344
1344
1344
1
1
1369
1369
1369
1
3164
3164
667
667
691
689
228
221
1
1
1
667
667
667
667
667
667
667
667
1
667
667
667
667
667
667
667
1
691
691
691
691
691
691
137
691
691
1
689
689
689
689
689
689
136
689
689
1
228
228
228
228
228
228
1
221
221
221
221
221
221
1
1
1
1
1
1
1
1
1
1
1829
1829
1829
1829
1829
1829
1829
1829
1829
1380
1380
449
273
176
1829
1
677
677
677
677
677
677
677
1
667
667
667
667
667
667
667
1
692
692
692
1
689
689
689
689
1
214
214
214
1
208
208
208
1
1803
1803
1803
1830
1830
1830
1830
1830
1803
1
1
1
1
1
1
1
1
692
692
692
692
692
692
692
692
692
1
692
692
692
1
214
214
214
1
691
691
691
1
689
689
689
1
208
208
208
1
1
1
691
1
689
| // Copyright (c) 2015 Uber Technologies, Inc.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
'use strict';
var TypedError = require('error/typed');
var Duplex = require('stream').Duplex;
var util = require('util');
var reqres = require('../reqres');
var TChannelOutgoingRequest = reqres.OutgoingRequest;
var TChannelOutgoingResponse = reqres.OutgoingResponse;
var TChannelIncomingRequest = reqres.IncomingRequest;
var TChannelIncomingResponse = reqres.IncomingResponse;
var v2 = require('./index');
module.exports = TChannelV2Handler;
var TChannelUnhandledFrameTypeError = TypedError({
type: 'tchannel.unhandled-frame-type',
message: 'unhandled frame type {typeCode}',
typeCode: null
});
var InvalidCodeStringError = TypedError({
type: 'tchannel.invalid-code-string',
message: 'Invalid Error frame code: {codeString}',
codeString: null
});
function TChannelV2Handler(channel, options) {
Iif (!(this instanceof TChannelV2Handler)) {
return new TChannelV2Handler(channel, options);
}
var self = this;
Duplex.call(self, {
objectMode: true
});
self.channel = channel;
self.tracer = options.tracer;
self.remoteHostPort = null; // filled in by identify message
self.lastSentFrameId = 0;
// TODO: GC these... maybe that's up to TChannel itself wrt ops
self.streamingReq = Object.create(null);
self.streamingRes = Object.create(null);
}
util.inherits(TChannelV2Handler, Duplex);
TChannelV2Handler.prototype.nextFrameId = function nextFrameId() {
var self = this;
self.lastSentFrameId = (self.lastSentFrameId + 1) % v2.Frame.MaxId;
return self.lastSentFrameId;
};
TChannelV2Handler.prototype._write = function _write(frame, encoding, callback) {
var self = this;
switch (frame.body.type) {
case v2.Types.InitRequest:
return self.handleInitRequest(frame, callback);
case v2.Types.InitResponse:
return self.handleInitResponse(frame, callback);
case v2.Types.CallRequest:
return self.handleCallRequest(frame, callback);
case v2.Types.CallResponse:
return self.handleCallResponse(frame, callback);
case v2.Types.CallRequestCont:
return self.handleCallRequestCont(frame, callback);
case v2.Types.CallResponseCont:
return self.handleCallResponseCont(frame, callback);
case v2.Types.ErrorResponse:
return self.handleError(frame, callback);
default:
return callback(TChannelUnhandledFrameTypeError({
typeCode: frame.body.type
}));
}
};
TChannelV2Handler.prototype._read = function _read(/* n */) {
/* noop */
};
TChannelV2Handler.prototype.handleInitRequest = function handleInitRequest(reqFrame, callback) {
var self = this;
Iif (self.remoteHostPort !== null) {
return callback(new Error('duplicate init request')); // TODO typed error
}
/* jshint camelcase:false */
var headers = reqFrame.body.headers;
var init = {
hostPort: headers.host_port,
processName: headers.process_name
};
/* jshint camelcase:true */
self.remoteHostPort = init.hostPort;
self.emit('init.request', init);
self.sendInitResponse(reqFrame);
callback();
};
TChannelV2Handler.prototype.handleInitResponse = function handleInitResponse(resFrame, callback) {
var self = this;
Iif (self.remoteHostPort !== null) {
return callback(new Error('duplicate init response')); // TODO typed error
}
/* jshint camelcase:false */
var headers = resFrame.body.headers;
var init = {
hostPort: headers.host_port,
processName: headers.process_name
};
/* jshint camelcase:true */
self.remoteHostPort = init.hostPort;
self.emit('init.response', init);
callback();
};
TChannelV2Handler.prototype.handleCallRequest = function handleCallRequest(reqFrame, callback) {
var self = this;
Iif (self.remoteHostPort === null) {
return callback(new Error('call request before init request')); // TODO typed error
}
var req = self.buildIncomingRequest(reqFrame);
self._handleCallFrame(req, reqFrame, function(err) {
Iif (err) return callback(err);
if (req.state === reqres.States.Streaming) {
self.streamingReq[req.id] = req;
}
self.emit('call.incoming.request', req);
callback();
});
};
TChannelV2Handler.prototype.handleCallResponse = function handleCallResponse(resFrame, callback) {
var self = this;
Iif (self.remoteHostPort === null) {
return callback(new Error('call response before init response')); // TODO typed error
}
var res = self.buildIncomingResponse(resFrame);
self._handleCallFrame(res, resFrame, function(err) {
Iif (err) return callback(err);
if (res.state === reqres.States.Streaming) {
self.streamingRes[res.id] = res;
}
self.emit('call.incoming.response', res);
callback();
});
};
TChannelV2Handler.prototype.handleCallRequestCont = function handleCallRequestCont(reqFrame, callback) {
var self = this;
Iif (self.remoteHostPort === null) {
return callback(new Error('call request cont before init request')); // TODO typed error
}
var id = reqFrame.id;
var req = self.streamingReq[id];
Iif (!req) {
return callback(new Error('call request cont for unknown request')); // TODO typed error
}
self._handleCallFrame(req, reqFrame, callback);
};
TChannelV2Handler.prototype.handleCallResponseCont = function handleCallResponseCont(resFrame, callback) {
var self = this;
Iif (self.remoteHostPort === null) {
return callback(new Error('call response cont before init response')); // TODO typed error
}
var id = resFrame.id;
var res = self.streamingRes[id];
Iif (!res) {
return callback(new Error('call response cont for unknown response')); // TODO typed error
}
self._handleCallFrame(res, resFrame, callback);
};
TChannelV2Handler.prototype.handleError = function handleError(errFrame, callback) {
var self = this;
var id = errFrame.id;
var code = errFrame.body.code;
var message = String(errFrame.body.message);
var err = v2.ErrorResponse.CodeErrors[code]({
originalId: id,
message: message
});
Iif (id === v2.Frame.NullId) {
// fatal error not associated with a prior frame
callback(err);
} else {
self.emit('call.incoming.error', err);
callback();
}
};
TChannelV2Handler.prototype._handleCallFrame = function _handleCallFrame(r, frame, callback) {
Iif (r.state === reqres.States.Done) {
callback(new Error('got cont in done state')); // TODO typed error
return;
}
var checksum = r.checksum;
Iif (checksum.type !== frame.body.csum.type) {
callback(new Error('checksum type changed mid-tream')); // TODO typed error
return;
}
var err = frame.body.verifyChecksum(checksum.val);
Iif (err) {
callback(err); // TODO wrap context
return;
}
r.checksum = frame.body.csum;
var isLast = !(frame.body.flags & v2.CallRequest.Flags.Fragment);
r.handleFrame(frame.body.args);
if (isLast) {
r.handleFrame(null);
r.state = reqres.States.Done;
} else if (r.state === reqres.States.Initial) {
r.state = reqres.States.Streaming;
} else Iif (r.state !== reqres.States.Streaming) {
throw new Error('unknown frame handling state');
}
callback();
};
TChannelV2Handler.prototype.sendInitRequest = function sendInitRequest() {
var self = this;
var id = self.nextFrameId(); // TODO: assert(id === 1)?
var hostPort = self.channel.hostPort || '0.0.0.0:0';
var processName = self.channel.processName;
var body = v2.InitRequest(v2.VERSION, {
/* jshint camelcase:false */
host_port: hostPort,
process_name: processName
/* jshint camelcase:true */
});
var reqFrame = v2.Frame(id, body);
self.push(reqFrame);
};
TChannelV2Handler.prototype.sendInitResponse = function sendInitResponse(reqFrame) {
var self = this;
var id = reqFrame.id;
var hostPort = self.channel.hostPort;
var processName = self.channel.processName;
var body = v2.InitResponse(v2.VERSION, {
/* jshint camelcase:false */
host_port: hostPort,
process_name: processName
/* jshint camelcase:true */
});
var resFrame = v2.Frame(id, body);
self.push(resFrame);
};
TChannelV2Handler.prototype.sendCallRequestFrame = function sendCallRequestFrame(req, flags, args) {
var self = this;
var reqBody = v2.CallRequest(flags, req.ttl, req.tracing, req.service, req.headers, req.checksum.type);
req.checksum = self._sendCallBodies(req.id, reqBody.csum, reqBody, args);
};
TChannelV2Handler.prototype.sendCallResponseFrame = function sendCallResponseFrame(res, flags, args) {
var self = this;
var code = res.ok ? v2.CallResponse.Codes.OK : v2.CallResponse.Codes.Error;
var resBody = v2.CallResponse(flags, code, res.tracing, res.headers, res.checksum.type);
res.checksum = self._sendCallBodies(res.id, resBody.csum, resBody, args);
};
TChannelV2Handler.prototype.sendCallRequestContFrame = function sendCallRequestContFrame(req, flags, args) {
var self = this;
var reqBody = v2.CallRequestCont(flags, req.checksum.type);
req.checksum = self._sendCallBodies(req.id, req.checksum, reqBody, args);
};
TChannelV2Handler.prototype.sendCallResponseContFrame = function sendCallResponseContFrame(res, flags, args) {
var self = this;
var resBody = v2.CallResponseCont(flags, res.checksum.type);
res.checksum = self._sendCallBodies(res.id, res.checksum, resBody, args);
};
TChannelV2Handler.prototype._sendCallBodies = function _sendCallBodies(id, checksum, body, args) {
var self = this;
var bodies = body.splitArgs(args, v2.Frame.MaxBodySize);
for (var i = 0; i < bodies.length; i++) {
body = bodies[i];
body.updateChecksum(checksum.val);
checksum = body.csum;
var frame = v2.Frame(id, body);
self.push(frame);
}
return checksum;
};
TChannelV2Handler.prototype.sendErrorFrame = function sendErrorFrame(req, codeString, message) {
var self = this;
var code = v2.ErrorResponse.Codes[codeString];
Iif (code === undefined) {
// TODO: could/should map to UnexpectedError
throw InvalidCodeStringError({
codeString: codeString
});
}
var errBody = v2.ErrorResponse(code, req.id, message);
var errFrame = v2.Frame(req.id, errBody);
self.push(errFrame);
};
TChannelV2Handler.prototype.buildOutgoingRequest = function buildOutgoingRequest(options) {
var self = this;
var id = self.nextFrameId();
options.tracer = self.tracer;
Eif (options.checksumType === undefined || options.checksumType === null) {
options.checksumType = v2.Checksum.Types.Farm32;
}
options.checksum = v2.Checksum(options.checksumType);
options.sendFrame = {
callRequest: sendCallRequestFrame,
callRequestCont: sendCallRequestContFrame
};
var req = TChannelOutgoingRequest(id, options);
return req;
function sendCallRequestFrame(args, isLast) {
var flags = 0;
if (!isLast) flags |= v2.CallResponse.Flags.Fragment;
self.sendCallRequestFrame(req, flags, args);
}
function sendCallRequestContFrame(args, isLast) {
var flags = 0;
if (!isLast) flags |= v2.CallResponse.Flags.Fragment;
self.sendCallRequestContFrame(req, flags, args);
}
};
TChannelV2Handler.prototype.buildOutgoingResponse = function buildOutgoingResponse(req) {
var self = this;
var res = TChannelOutgoingResponse(req.id, {
tracing: req.tracing,
headers: {},
checksumType: req.checksum.type,
checksum: v2.Checksum(req.checksum.type),
sendFrame: {
callResponse: sendCallResponseFrame,
callResponseCont: sendCallResponseContFrame,
error: sendErrorFrame
},
tracer: self.tracer
});
return res;
function sendCallResponseFrame(args, isLast) {
var flags = 0;
if (!isLast) flags |= v2.CallResponse.Flags.Fragment;
self.sendCallResponseFrame(res, flags, args);
}
function sendCallResponseContFrame(args, isLast) {
var flags = 0;
if (!isLast) flags |= v2.CallResponse.Flags.Fragment;
self.sendCallResponseContFrame(res, flags, args);
}
function sendErrorFrame(codeString, message) {
self.sendErrorFrame(req, codeString, message);
}
};
TChannelV2Handler.prototype.buildIncomingRequest = function buildIncomingRequest(reqFrame) {
return TChannelIncomingRequest(reqFrame.id, {
ttl: reqFrame.body.ttl,
tracing: reqFrame.body.tracing,
service: reqFrame.body.service,
headers: reqFrame.body.headers,
checksum: v2.Checksum(reqFrame.body.csum.type)
});
};
TChannelV2Handler.prototype.buildIncomingResponse = function buildIncomingResponse(resFrame) {
return TChannelIncomingResponse(resFrame.id, {
code: resFrame.body.code,
checksum: v2.Checksum(resFrame.body.csum.type)
});
};
|