| 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 |
1
1
1
1
1
1
1
1
1
2770
1385
1385
1385
1385
1385
1385
1385
1385
1385
692
693
1385
1
1
1
1
1
1381
1381
1381
1381
1381
1381
1381
1381
1381
4001
4001
4001
3995
3995
6
6
6
6
1381
1381
1381
6
6
6
6
6
6
1381
1
693
693
1
691
691
1
2760
1380
1380
1380
1380
1380
1380
1380
1380
690
690
1380
1
1
1
1
1
1
1
690
690
1
689
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 bufrw = require('bufrw');
var ArgsRW = require('./args');
var Checksum = require('./checksum');
var header = require('./header');
var Tracing = require('./tracing');
var Flags = {
Fragment: 0x01
};
module.exports.Request = CallRequest;
module.exports.Response = CallResponse;
// TODO: validate transport header names?
// TODO: Checksum-like class for tracing
/* jshint maxparams:10 */
// flags:1 ttl:4 tracing:24 traceflags:1 service~1 nh:1 (hk~1 hv~1){nh} csumtype:1 (csum:4){0,1} (arg~2)*
function CallRequest(flags, ttl, tracing, service, headers, csum, args) {
if (!(this instanceof CallRequest)) {
return new CallRequest(flags, ttl, tracing, service, headers, csum, args);
}
var self = this;
self.type = CallRequest.TypeCode;
self.flags = flags || 0;
self.ttl = ttl || 0;
self.tracing = tracing || Tracing.emptyTracing;
self.service = service || '';
self.headers = headers || {};
if (csum === undefined || csum === null) {
self.csum = Checksum(Checksum.Types.None);
} else {
self.csum = Checksum.objOrType(csum);
}
self.args = args || [];
}
CallRequest.Cont = require('./cont').RequestCont;
CallRequest.TypeCode = 0x03;
CallRequest.Flags = Flags;
CallRequest.RW = bufrw.Struct(CallRequest, [
{name: 'flags', rw: bufrw.UInt8}, // flags:1
{name: 'ttl', rw: bufrw.UInt32BE}, // ttl:4
{name: 'tracing', rw: Tracing.RW}, // tracing:24 traceflags:1
{name: 'service', rw: bufrw.str1}, // service~1
{name: 'headers', rw: header.header1}, // nh:1 (hk~1 hv~1){nh}
{name: 'csum', rw: Checksum.RW}, // csumtype:1 (csum:4){0,1}
{name: 'args', rw: ArgsRW(bufrw.buf2)}, // (arg~2)*
]);
CallRequest.prototype.splitArgs = function splitArgs(args, maxSize) {
var self = this;
// assert not self.args
var lenRes = self.constructor.RW.byteLength(self);
Iif (lenRes.err) throw lenRes.err;
var maxBodySize = maxSize - lenRes.length;
var remain = maxBodySize;
var first = [];
var argSize = 2;
var split = false;
for (var i = 0; i < args.length; i++) {
var arg = args[i] || Buffer(0);
var argLength = argSize + arg.length;
if (argLength < remain) {
first.push(arg);
remain -= argLength;
} else {
first.push(arg.slice(0, remain - argSize));
args = [arg.slice(remain - argSize)].concat(args.slice(i+1));
split = true;
break;
}
}
self.args = first;
var ret = [self];
if (split) {
var isLast = !(self.flags & CallRequest.Flags.Fragment);
self.flags |= Flags.Fragment;
var cont = self.constructor.Cont(self.flags, self.csum.type);
ret = cont.splitArgs(args, maxSize);
ret.unshift(self);
if (isLast) ret[ret.length - 1].flags &= ~ CallRequest.Flags.Fragment;
}
return ret;
};
CallRequest.prototype.updateChecksum = function updateChecksum() {
var self = this;
return self.csum.update(self.args);
};
CallRequest.prototype.verifyChecksum = function verifyChecksum() {
var self = this;
return self.csum.verify(self.args);
};
// flags:1 code:1 tracing:24 traceflags:1 nh:1 (hk~1 hv~1){nh} csumtype:1 (csum:4){0,1} (arg~2)*
function CallResponse(flags, code, tracing, headers, csum, args) {
if (!(this instanceof CallResponse)) {
return new CallResponse(flags, code, tracing, headers, csum, args);
}
var self = this;
self.type = CallResponse.TypeCode;
self.flags = flags || 0;
self.code = code || CallResponse.Codes.OK;
self.tracing = tracing || Tracing.emptyTracing;
self.headers = headers || {};
if (csum === undefined || csum === null) {
self.csum = Checksum(Checksum.Types.None);
} else {
self.csum = Checksum.objOrType(csum);
}
self.args = args || [];
}
CallResponse.Cont = require('./cont').ResponseCont;
CallResponse.TypeCode = 0x04;
CallResponse.Flags = CallRequest.Flags;
CallResponse.Codes = {
OK: 0x00,
Error: 0x01
};
CallResponse.RW = bufrw.Struct(CallResponse, [
{name: 'flags', rw: bufrw.UInt8}, // flags:1
{name: 'code', rw: bufrw.UInt8}, // code:1
{name: 'tracing', rw: Tracing.RW}, // tracing:24 traceflags:1
{name: 'headers', rw: header.header1}, // nh:1 (hk~1 hv~1){nh}
{name: 'csum', rw: Checksum.RW}, // csumtype:1 (csum:4){0},1}
{name: 'args', rw: ArgsRW(bufrw.buf2)}, // (arg~2)*
]);
CallResponse.prototype.splitArgs = CallRequest.prototype.splitArgs;
CallResponse.prototype.updateChecksum = function updateChecksum() {
var self = this;
return self.csum.update(self.args);
};
CallResponse.prototype.verifyChecksum = function verifyChecksum() {
var self = this;
return self.csum.verify(self.args);
};
|