@zero-server/sdk 0.9.1 → 0.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +460 -443
- package/index.js +414 -412
- package/lib/app.js +1172 -1172
- package/lib/auth/authorize.js +399 -399
- package/lib/auth/enrollment.js +367 -367
- package/lib/auth/index.js +57 -57
- package/lib/auth/jwt.js +731 -731
- package/lib/auth/oauth.js +362 -362
- package/lib/auth/session.js +588 -588
- package/lib/auth/trustedDevice.js +409 -409
- package/lib/auth/twoFactor.js +1150 -1150
- package/lib/auth/webauthn.js +946 -946
- package/lib/body/index.js +14 -14
- package/lib/body/json.js +109 -109
- package/lib/body/multipart.js +440 -440
- package/lib/body/raw.js +71 -71
- package/lib/body/rawBuffer.js +160 -160
- package/lib/body/sendError.js +25 -25
- package/lib/body/text.js +75 -75
- package/lib/body/typeMatch.js +41 -41
- package/lib/body/urlencoded.js +235 -235
- package/lib/cli.js +845 -845
- package/lib/cluster.js +666 -666
- package/lib/debug.js +372 -372
- package/lib/env/index.js +465 -465
- package/lib/errors.js +683 -683
- package/lib/fetch/index.js +256 -256
- package/lib/grpc/balancer.js +378 -378
- package/lib/grpc/call.js +708 -708
- package/lib/grpc/client.js +764 -764
- package/lib/grpc/codec.js +1221 -1221
- package/lib/grpc/credentials.js +398 -398
- package/lib/grpc/frame.js +262 -262
- package/lib/grpc/health.js +287 -287
- package/lib/grpc/index.js +121 -121
- package/lib/grpc/metadata.js +461 -461
- package/lib/grpc/proto.js +821 -821
- package/lib/grpc/reflection.js +590 -590
- package/lib/grpc/server.js +445 -445
- package/lib/grpc/status.js +118 -118
- package/lib/grpc/watch.js +173 -173
- package/lib/http/index.js +10 -10
- package/lib/http/request.js +727 -727
- package/lib/http/response.js +799 -799
- package/lib/lifecycle.js +557 -557
- package/lib/middleware/compress.js +230 -230
- package/lib/middleware/cookieParser.js +237 -237
- package/lib/middleware/cors.js +93 -93
- package/lib/middleware/csrf.js +137 -137
- package/lib/middleware/errorHandler.js +101 -101
- package/lib/middleware/helmet.js +175 -175
- package/lib/middleware/index.js +19 -17
- package/lib/middleware/logger.js +74 -74
- package/lib/middleware/rateLimit.js +88 -88
- package/lib/middleware/requestId.js +53 -53
- package/lib/middleware/static.js +326 -326
- package/lib/middleware/timeout.js +71 -71
- package/lib/middleware/validator.js +255 -255
- package/lib/observe/health.js +326 -326
- package/lib/observe/index.js +50 -50
- package/lib/observe/logger.js +359 -359
- package/lib/observe/metrics.js +805 -805
- package/lib/observe/tracing.js +592 -592
- package/lib/orm/adapters/json.js +290 -290
- package/lib/orm/adapters/memory.js +764 -764
- package/lib/orm/adapters/mongo.js +764 -764
- package/lib/orm/adapters/mysql.js +933 -933
- package/lib/orm/adapters/postgres.js +1144 -1144
- package/lib/orm/adapters/redis.js +1534 -1534
- package/lib/orm/adapters/sql-base.js +212 -212
- package/lib/orm/adapters/sqlite.js +858 -858
- package/lib/orm/audit.js +649 -649
- package/lib/orm/cache.js +394 -394
- package/lib/orm/geo.js +387 -387
- package/lib/orm/index.js +784 -784
- package/lib/orm/migrate.js +432 -432
- package/lib/orm/model.js +1706 -1706
- package/lib/orm/plugin.js +375 -375
- package/lib/orm/procedures.js +836 -836
- package/lib/orm/profiler.js +233 -233
- package/lib/orm/query.js +1772 -1772
- package/lib/orm/replicas.js +241 -241
- package/lib/orm/schema.js +307 -307
- package/lib/orm/search.js +380 -380
- package/lib/orm/seed/data/commerce.js +136 -136
- package/lib/orm/seed/data/internet.js +111 -111
- package/lib/orm/seed/data/locations.js +204 -204
- package/lib/orm/seed/data/names.js +338 -338
- package/lib/orm/seed/data/person.js +128 -128
- package/lib/orm/seed/data/phone.js +211 -211
- package/lib/orm/seed/data/words.js +134 -134
- package/lib/orm/seed/factory.js +178 -178
- package/lib/orm/seed/fake.js +1186 -1186
- package/lib/orm/seed/index.js +18 -18
- package/lib/orm/seed/rng.js +70 -70
- package/lib/orm/seed/seeder.js +124 -124
- package/lib/orm/seed/unique.js +68 -68
- package/lib/orm/snapshot.js +366 -366
- package/lib/orm/tenancy.js +605 -605
- package/lib/orm/views.js +350 -350
- package/lib/router/index.js +436 -436
- package/lib/sse/index.js +8 -8
- package/lib/sse/stream.js +349 -349
- package/lib/ws/connection.js +451 -451
- package/lib/ws/handshake.js +125 -125
- package/lib/ws/index.js +14 -14
- package/lib/ws/room.js +223 -223
- package/package.json +73 -73
- package/types/app.d.ts +223 -223
- package/types/auth.d.ts +520 -520
- package/types/cluster.d.ts +75 -75
- package/types/env.d.ts +80 -80
- package/types/errors.d.ts +316 -316
- package/types/fetch.d.ts +43 -43
- package/types/grpc.d.ts +432 -432
- package/types/index.d.ts +384 -384
- package/types/lifecycle.d.ts +60 -60
- package/types/middleware.d.ts +320 -320
- package/types/observe.d.ts +304 -304
- package/types/orm.d.ts +1887 -1887
- package/types/request.d.ts +109 -109
- package/types/response.d.ts +157 -157
- package/types/router.d.ts +78 -78
- package/types/sse.d.ts +78 -78
- package/types/websocket.d.ts +126 -126
package/lib/grpc/frame.js
CHANGED
|
@@ -1,262 +1,262 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module grpc/frame
|
|
3
|
-
* @description gRPC length-prefixed message framing over HTTP/2.
|
|
4
|
-
* Each gRPC message on the wire has a 5-byte header:
|
|
5
|
-
* - Byte 0: Compression flag (0 = uncompressed, 1 = compressed)
|
|
6
|
-
* - Bytes 1-4: Message length as 32-bit big-endian unsigned integer
|
|
7
|
-
* Followed by the protobuf-encoded message body.
|
|
8
|
-
*
|
|
9
|
-
* Also handles optional gzip compression/decompression.
|
|
10
|
-
*
|
|
11
|
-
* @see https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
const zlib = require('zlib');
|
|
15
|
-
const log = require('../debug')('zero:grpc');
|
|
16
|
-
|
|
17
|
-
// -- Constants ---------------------------------------------
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Size of the gRPC frame header in bytes.
|
|
21
|
-
* @type {number}
|
|
22
|
-
*/
|
|
23
|
-
const FRAME_HEADER_SIZE = 5;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Maximum frame size (16 MB — matches the default gRPC max).
|
|
27
|
-
* @type {number}
|
|
28
|
-
*/
|
|
29
|
-
const MAX_FRAME_SIZE = 16 * 1024 * 1024;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Compression flag values.
|
|
33
|
-
* @enum {number}
|
|
34
|
-
*/
|
|
35
|
-
const COMPRESS_FLAG = {
|
|
36
|
-
NONE: 0,
|
|
37
|
-
GZIP: 1,
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
// -- Frame Encoder -----------------------------------------
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Encode a protobuf message buffer into a gRPC framed message.
|
|
44
|
-
*
|
|
45
|
-
* @param {Buffer} message - Protobuf-encoded message data.
|
|
46
|
-
* @param {object} [opts] - Framing options.
|
|
47
|
-
* @param {boolean} [opts.compress=false] - Whether to gzip-compress the message.
|
|
48
|
-
* @returns {Promise<Buffer>|Buffer} Framed message (async if compressing).
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
* const frame = frameEncode(protobufBytes);
|
|
52
|
-
* stream.write(frame);
|
|
53
|
-
*
|
|
54
|
-
* @example | With compression
|
|
55
|
-
* const frame = await frameEncode(protobufBytes, { compress: true });
|
|
56
|
-
*/
|
|
57
|
-
function frameEncode(message, opts = {})
|
|
58
|
-
{
|
|
59
|
-
if (!Buffer.isBuffer(message))
|
|
60
|
-
message = Buffer.from(message || []);
|
|
61
|
-
|
|
62
|
-
if (opts.compress)
|
|
63
|
-
{
|
|
64
|
-
return new Promise((resolve, reject) =>
|
|
65
|
-
{
|
|
66
|
-
zlib.gzip(message, (err, compressed) =>
|
|
67
|
-
{
|
|
68
|
-
if (err) return reject(err);
|
|
69
|
-
resolve(_buildFrame(compressed, COMPRESS_FLAG.GZIP));
|
|
70
|
-
});
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return _buildFrame(message, COMPRESS_FLAG.NONE);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Build the 5-byte header + payload buffer.
|
|
79
|
-
* @private
|
|
80
|
-
* @param {Buffer} payload
|
|
81
|
-
* @param {number} flag
|
|
82
|
-
* @returns {Buffer}
|
|
83
|
-
*/
|
|
84
|
-
function _buildFrame(payload, flag)
|
|
85
|
-
{
|
|
86
|
-
const header = Buffer.alloc(FRAME_HEADER_SIZE);
|
|
87
|
-
header[0] = flag;
|
|
88
|
-
header.writeUInt32BE(payload.length, 1);
|
|
89
|
-
return Buffer.concat([header, payload]);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// -- Frame Decoder -----------------------------------------
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Stateful gRPC frame parser — buffers incoming data and emits complete
|
|
96
|
-
* decompressed messages. Designed to be fed chunks from an HTTP/2 stream.
|
|
97
|
-
*
|
|
98
|
-
* @class
|
|
99
|
-
*
|
|
100
|
-
* @param {object} [opts] - Parser options.
|
|
101
|
-
* @param {number} [opts.maxMessageSize=16777216] - Maximum allowed message size in bytes.
|
|
102
|
-
* @param {boolean} [opts.allowCompressed=true] - Whether to accept compressed frames.
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* const parser = new FrameParser();
|
|
106
|
-
* parser.onMessage = (buf) => { /* fully deframed protobuf bytes */ };
|
|
107
|
-
* stream.on('data', (chunk) => parser.push(chunk));
|
|
108
|
-
*/
|
|
109
|
-
class FrameParser
|
|
110
|
-
{
|
|
111
|
-
/**
|
|
112
|
-
* @constructor
|
|
113
|
-
* @param {object} [opts] - Parser options.
|
|
114
|
-
* @param {number} [opts.maxMessageSize=16777216] - Max message size.
|
|
115
|
-
* @param {boolean} [opts.allowCompressed=true] - Accept gzip frames.
|
|
116
|
-
*/
|
|
117
|
-
constructor(opts = {})
|
|
118
|
-
{
|
|
119
|
-
/** @private */
|
|
120
|
-
this._buffer = Buffer.alloc(0);
|
|
121
|
-
/** @private */
|
|
122
|
-
this._maxMessageSize = opts.maxMessageSize || MAX_FRAME_SIZE;
|
|
123
|
-
/** @private */
|
|
124
|
-
this._allowCompressed = opts.allowCompressed !== false;
|
|
125
|
-
/** @private */
|
|
126
|
-
this._destroyed = false;
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Callback invoked with each complete deframed message buffer.
|
|
130
|
-
* @type {Function|null}
|
|
131
|
-
*/
|
|
132
|
-
this.onMessage = null;
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* Callback invoked on parse errors.
|
|
136
|
-
* @type {Function|null}
|
|
137
|
-
*/
|
|
138
|
-
this.onError = null;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Feed a data chunk to the parser. May trigger zero or more `onMessage` callbacks.
|
|
143
|
-
*
|
|
144
|
-
* @param {Buffer} chunk - Raw bytes from the HTTP/2 stream.
|
|
145
|
-
*/
|
|
146
|
-
push(chunk)
|
|
147
|
-
{
|
|
148
|
-
if (this._destroyed) return;
|
|
149
|
-
|
|
150
|
-
this._buffer = this._buffer.length > 0
|
|
151
|
-
? Buffer.concat([this._buffer, chunk])
|
|
152
|
-
: chunk;
|
|
153
|
-
|
|
154
|
-
this._drain();
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Process buffered data, extracting complete frames.
|
|
159
|
-
* @private
|
|
160
|
-
*/
|
|
161
|
-
_drain()
|
|
162
|
-
{
|
|
163
|
-
while (this._buffer.length >= FRAME_HEADER_SIZE)
|
|
164
|
-
{
|
|
165
|
-
const compressed = this._buffer[0];
|
|
166
|
-
const msgLen = this._buffer.readUInt32BE(1);
|
|
167
|
-
|
|
168
|
-
// Security: reject oversized messages before buffering
|
|
169
|
-
if (msgLen > this._maxMessageSize)
|
|
170
|
-
{
|
|
171
|
-
const err = new Error(`gRPC message size ${msgLen} exceeds limit ${this._maxMessageSize}`);
|
|
172
|
-
err.code = 'RESOURCE_EXHAUSTED';
|
|
173
|
-
this._emitError(err);
|
|
174
|
-
this._destroyed = true;
|
|
175
|
-
return;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const totalLen = FRAME_HEADER_SIZE + msgLen;
|
|
179
|
-
if (this._buffer.length < totalLen)
|
|
180
|
-
{
|
|
181
|
-
// Wait for more data
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
const payload = this._buffer.subarray(FRAME_HEADER_SIZE, totalLen);
|
|
186
|
-
this._buffer = this._buffer.subarray(totalLen);
|
|
187
|
-
|
|
188
|
-
if (compressed && !this._allowCompressed)
|
|
189
|
-
{
|
|
190
|
-
const err = new Error('Compressed message received but compression is disabled');
|
|
191
|
-
err.code = 'UNIMPLEMENTED';
|
|
192
|
-
this._emitError(err);
|
|
193
|
-
continue;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
if (compressed)
|
|
197
|
-
{
|
|
198
|
-
// Async decompress
|
|
199
|
-
zlib.gunzip(payload, (err, decompressed) =>
|
|
200
|
-
{
|
|
201
|
-
if (err)
|
|
202
|
-
{
|
|
203
|
-
err.code = 'INTERNAL';
|
|
204
|
-
this._emitError(err);
|
|
205
|
-
return;
|
|
206
|
-
}
|
|
207
|
-
this._emitMessage(decompressed);
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
else
|
|
211
|
-
{
|
|
212
|
-
this._emitMessage(Buffer.from(payload));
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Emit a parsed message.
|
|
219
|
-
* @private
|
|
220
|
-
*/
|
|
221
|
-
_emitMessage(buf)
|
|
222
|
-
{
|
|
223
|
-
if (this._destroyed) return;
|
|
224
|
-
if (this.onMessage) this.onMessage(buf);
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Emit a parse error.
|
|
229
|
-
* @private
|
|
230
|
-
*/
|
|
231
|
-
_emitError(err)
|
|
232
|
-
{
|
|
233
|
-
log.error('frame parse error: %s', err.message);
|
|
234
|
-
if (this.onError) this.onError(err);
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/**
|
|
238
|
-
* Reset the parser state, discarding any buffered data.
|
|
239
|
-
*/
|
|
240
|
-
reset()
|
|
241
|
-
{
|
|
242
|
-
this._buffer = Buffer.alloc(0);
|
|
243
|
-
this._destroyed = false;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* Destroy the parser, preventing further processing.
|
|
248
|
-
*/
|
|
249
|
-
destroy()
|
|
250
|
-
{
|
|
251
|
-
this._destroyed = true;
|
|
252
|
-
this._buffer = Buffer.alloc(0);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
module.exports = {
|
|
257
|
-
FRAME_HEADER_SIZE,
|
|
258
|
-
MAX_FRAME_SIZE,
|
|
259
|
-
COMPRESS_FLAG,
|
|
260
|
-
frameEncode,
|
|
261
|
-
FrameParser,
|
|
262
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* @module grpc/frame
|
|
3
|
+
* @description gRPC length-prefixed message framing over HTTP/2.
|
|
4
|
+
* Each gRPC message on the wire has a 5-byte header:
|
|
5
|
+
* - Byte 0: Compression flag (0 = uncompressed, 1 = compressed)
|
|
6
|
+
* - Bytes 1-4: Message length as 32-bit big-endian unsigned integer
|
|
7
|
+
* Followed by the protobuf-encoded message body.
|
|
8
|
+
*
|
|
9
|
+
* Also handles optional gzip compression/decompression.
|
|
10
|
+
*
|
|
11
|
+
* @see https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const zlib = require('zlib');
|
|
15
|
+
const log = require('../debug')('zero:grpc');
|
|
16
|
+
|
|
17
|
+
// -- Constants ---------------------------------------------
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Size of the gRPC frame header in bytes.
|
|
21
|
+
* @type {number}
|
|
22
|
+
*/
|
|
23
|
+
const FRAME_HEADER_SIZE = 5;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Maximum frame size (16 MB — matches the default gRPC max).
|
|
27
|
+
* @type {number}
|
|
28
|
+
*/
|
|
29
|
+
const MAX_FRAME_SIZE = 16 * 1024 * 1024;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Compression flag values.
|
|
33
|
+
* @enum {number}
|
|
34
|
+
*/
|
|
35
|
+
const COMPRESS_FLAG = {
|
|
36
|
+
NONE: 0,
|
|
37
|
+
GZIP: 1,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// -- Frame Encoder -----------------------------------------
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Encode a protobuf message buffer into a gRPC framed message.
|
|
44
|
+
*
|
|
45
|
+
* @param {Buffer} message - Protobuf-encoded message data.
|
|
46
|
+
* @param {object} [opts] - Framing options.
|
|
47
|
+
* @param {boolean} [opts.compress=false] - Whether to gzip-compress the message.
|
|
48
|
+
* @returns {Promise<Buffer>|Buffer} Framed message (async if compressing).
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* const frame = frameEncode(protobufBytes);
|
|
52
|
+
* stream.write(frame);
|
|
53
|
+
*
|
|
54
|
+
* @example | With compression
|
|
55
|
+
* const frame = await frameEncode(protobufBytes, { compress: true });
|
|
56
|
+
*/
|
|
57
|
+
function frameEncode(message, opts = {})
|
|
58
|
+
{
|
|
59
|
+
if (!Buffer.isBuffer(message))
|
|
60
|
+
message = Buffer.from(message || []);
|
|
61
|
+
|
|
62
|
+
if (opts.compress)
|
|
63
|
+
{
|
|
64
|
+
return new Promise((resolve, reject) =>
|
|
65
|
+
{
|
|
66
|
+
zlib.gzip(message, (err, compressed) =>
|
|
67
|
+
{
|
|
68
|
+
if (err) return reject(err);
|
|
69
|
+
resolve(_buildFrame(compressed, COMPRESS_FLAG.GZIP));
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return _buildFrame(message, COMPRESS_FLAG.NONE);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Build the 5-byte header + payload buffer.
|
|
79
|
+
* @private
|
|
80
|
+
* @param {Buffer} payload
|
|
81
|
+
* @param {number} flag
|
|
82
|
+
* @returns {Buffer}
|
|
83
|
+
*/
|
|
84
|
+
function _buildFrame(payload, flag)
|
|
85
|
+
{
|
|
86
|
+
const header = Buffer.alloc(FRAME_HEADER_SIZE);
|
|
87
|
+
header[0] = flag;
|
|
88
|
+
header.writeUInt32BE(payload.length, 1);
|
|
89
|
+
return Buffer.concat([header, payload]);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// -- Frame Decoder -----------------------------------------
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Stateful gRPC frame parser — buffers incoming data and emits complete
|
|
96
|
+
* decompressed messages. Designed to be fed chunks from an HTTP/2 stream.
|
|
97
|
+
*
|
|
98
|
+
* @class
|
|
99
|
+
*
|
|
100
|
+
* @param {object} [opts] - Parser options.
|
|
101
|
+
* @param {number} [opts.maxMessageSize=16777216] - Maximum allowed message size in bytes.
|
|
102
|
+
* @param {boolean} [opts.allowCompressed=true] - Whether to accept compressed frames.
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* const parser = new FrameParser();
|
|
106
|
+
* parser.onMessage = (buf) => { /* fully deframed protobuf bytes */ };
|
|
107
|
+
* stream.on('data', (chunk) => parser.push(chunk));
|
|
108
|
+
*/
|
|
109
|
+
class FrameParser
|
|
110
|
+
{
|
|
111
|
+
/**
|
|
112
|
+
* @constructor
|
|
113
|
+
* @param {object} [opts] - Parser options.
|
|
114
|
+
* @param {number} [opts.maxMessageSize=16777216] - Max message size.
|
|
115
|
+
* @param {boolean} [opts.allowCompressed=true] - Accept gzip frames.
|
|
116
|
+
*/
|
|
117
|
+
constructor(opts = {})
|
|
118
|
+
{
|
|
119
|
+
/** @private */
|
|
120
|
+
this._buffer = Buffer.alloc(0);
|
|
121
|
+
/** @private */
|
|
122
|
+
this._maxMessageSize = opts.maxMessageSize || MAX_FRAME_SIZE;
|
|
123
|
+
/** @private */
|
|
124
|
+
this._allowCompressed = opts.allowCompressed !== false;
|
|
125
|
+
/** @private */
|
|
126
|
+
this._destroyed = false;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Callback invoked with each complete deframed message buffer.
|
|
130
|
+
* @type {Function|null}
|
|
131
|
+
*/
|
|
132
|
+
this.onMessage = null;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Callback invoked on parse errors.
|
|
136
|
+
* @type {Function|null}
|
|
137
|
+
*/
|
|
138
|
+
this.onError = null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Feed a data chunk to the parser. May trigger zero or more `onMessage` callbacks.
|
|
143
|
+
*
|
|
144
|
+
* @param {Buffer} chunk - Raw bytes from the HTTP/2 stream.
|
|
145
|
+
*/
|
|
146
|
+
push(chunk)
|
|
147
|
+
{
|
|
148
|
+
if (this._destroyed) return;
|
|
149
|
+
|
|
150
|
+
this._buffer = this._buffer.length > 0
|
|
151
|
+
? Buffer.concat([this._buffer, chunk])
|
|
152
|
+
: chunk;
|
|
153
|
+
|
|
154
|
+
this._drain();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Process buffered data, extracting complete frames.
|
|
159
|
+
* @private
|
|
160
|
+
*/
|
|
161
|
+
_drain()
|
|
162
|
+
{
|
|
163
|
+
while (this._buffer.length >= FRAME_HEADER_SIZE)
|
|
164
|
+
{
|
|
165
|
+
const compressed = this._buffer[0];
|
|
166
|
+
const msgLen = this._buffer.readUInt32BE(1);
|
|
167
|
+
|
|
168
|
+
// Security: reject oversized messages before buffering
|
|
169
|
+
if (msgLen > this._maxMessageSize)
|
|
170
|
+
{
|
|
171
|
+
const err = new Error(`gRPC message size ${msgLen} exceeds limit ${this._maxMessageSize}`);
|
|
172
|
+
err.code = 'RESOURCE_EXHAUSTED';
|
|
173
|
+
this._emitError(err);
|
|
174
|
+
this._destroyed = true;
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const totalLen = FRAME_HEADER_SIZE + msgLen;
|
|
179
|
+
if (this._buffer.length < totalLen)
|
|
180
|
+
{
|
|
181
|
+
// Wait for more data
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const payload = this._buffer.subarray(FRAME_HEADER_SIZE, totalLen);
|
|
186
|
+
this._buffer = this._buffer.subarray(totalLen);
|
|
187
|
+
|
|
188
|
+
if (compressed && !this._allowCompressed)
|
|
189
|
+
{
|
|
190
|
+
const err = new Error('Compressed message received but compression is disabled');
|
|
191
|
+
err.code = 'UNIMPLEMENTED';
|
|
192
|
+
this._emitError(err);
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (compressed)
|
|
197
|
+
{
|
|
198
|
+
// Async decompress
|
|
199
|
+
zlib.gunzip(payload, (err, decompressed) =>
|
|
200
|
+
{
|
|
201
|
+
if (err)
|
|
202
|
+
{
|
|
203
|
+
err.code = 'INTERNAL';
|
|
204
|
+
this._emitError(err);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
this._emitMessage(decompressed);
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
else
|
|
211
|
+
{
|
|
212
|
+
this._emitMessage(Buffer.from(payload));
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Emit a parsed message.
|
|
219
|
+
* @private
|
|
220
|
+
*/
|
|
221
|
+
_emitMessage(buf)
|
|
222
|
+
{
|
|
223
|
+
if (this._destroyed) return;
|
|
224
|
+
if (this.onMessage) this.onMessage(buf);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Emit a parse error.
|
|
229
|
+
* @private
|
|
230
|
+
*/
|
|
231
|
+
_emitError(err)
|
|
232
|
+
{
|
|
233
|
+
log.error('frame parse error: %s', err.message);
|
|
234
|
+
if (this.onError) this.onError(err);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Reset the parser state, discarding any buffered data.
|
|
239
|
+
*/
|
|
240
|
+
reset()
|
|
241
|
+
{
|
|
242
|
+
this._buffer = Buffer.alloc(0);
|
|
243
|
+
this._destroyed = false;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Destroy the parser, preventing further processing.
|
|
248
|
+
*/
|
|
249
|
+
destroy()
|
|
250
|
+
{
|
|
251
|
+
this._destroyed = true;
|
|
252
|
+
this._buffer = Buffer.alloc(0);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
module.exports = {
|
|
257
|
+
FRAME_HEADER_SIZE,
|
|
258
|
+
MAX_FRAME_SIZE,
|
|
259
|
+
COMPRESS_FLAG,
|
|
260
|
+
frameEncode,
|
|
261
|
+
FrameParser,
|
|
262
|
+
};
|