@zero-server/grpc 0.9.0 → 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.
@@ -0,0 +1,398 @@
1
+ /**
2
+ * @module grpc/credentials
3
+ * @description Channel credentials for gRPC connections.
4
+ * Provides factory functions for creating insecure, SSL/TLS,
5
+ * and metadata-based credentials. Supports certificate rotation
6
+ * and credential composition.
7
+ *
8
+ * Uses only Node.js built-in `tls` and `fs` — no external packages.
9
+ *
10
+ * @example | Insecure (plaintext)
11
+ * const { ChannelCredentials, GrpcClient } = require('@zero-server/sdk');
12
+ * const creds = ChannelCredentials.createInsecure();
13
+ * const client = new GrpcClient({ address: 'http://localhost:50051', credentials: creds }, schema, 'Greeter');
14
+ *
15
+ * @example | Server-only TLS
16
+ * const creds = ChannelCredentials.createSsl(fs.readFileSync('ca.pem'));
17
+ *
18
+ * @example | Mutual TLS (mTLS)
19
+ * const creds = ChannelCredentials.createSsl(
20
+ * fs.readFileSync('ca.pem'),
21
+ * fs.readFileSync('client-key.pem'),
22
+ * fs.readFileSync('client-cert.pem'),
23
+ * );
24
+ *
25
+ * @example | Metadata credentials (e.g. Bearer token)
26
+ * const creds = ChannelCredentials.createFromMetadata((params) => ({
27
+ * authorization: 'Bearer ' + getToken(),
28
+ * }));
29
+ *
30
+ * @example | Composed credentials (TLS + per-call metadata)
31
+ * const creds = ChannelCredentials.combine(
32
+ * ChannelCredentials.createSsl(ca),
33
+ * ChannelCredentials.createFromMetadata(() => ({ authorization: 'Bearer ' + token })),
34
+ * );
35
+ */
36
+
37
+ const fs = require('fs');
38
+ const log = require('../debug')('zero:grpc:credentials');
39
+
40
+ // -- Credential Types ----------------------------------------
41
+
42
+ /** @enum {string} */
43
+ const CredentialType = {
44
+ INSECURE: 'insecure',
45
+ SSL: 'ssl',
46
+ METADATA: 'metadata',
47
+ COMPOSITE: 'composite',
48
+ };
49
+
50
+ // -- ChannelCredentials Class --------------------------------
51
+
52
+ /**
53
+ * Channel credentials define how a gRPC client authenticates to the server.
54
+ *
55
+ * @class
56
+ */
57
+ class ChannelCredentials
58
+ {
59
+ /**
60
+ * @param {string} type - Credential type.
61
+ * @param {object} [config] - Type-specific configuration.
62
+ * @private
63
+ */
64
+ constructor(type, config = {})
65
+ {
66
+ /** @type {string} */
67
+ this.type = type;
68
+ /** @private */
69
+ this._config = config;
70
+ }
71
+
72
+ /**
73
+ * Create insecure (plaintext) credentials.
74
+ * No TLS — suitable for development or service-mesh environments
75
+ * where transport security is handled by the infrastructure.
76
+ *
77
+ * @returns {ChannelCredentials}
78
+ */
79
+ static createInsecure()
80
+ {
81
+ return new ChannelCredentials(CredentialType.INSECURE);
82
+ }
83
+
84
+ /**
85
+ * Create SSL/TLS credentials.
86
+ *
87
+ * @param {Buffer|string|null} [rootCerts] - Root CA certificate(s) in PEM format.
88
+ * If null, uses the system default trust store.
89
+ * @param {Buffer|string|null} [clientKey] - Client private key in PEM format (for mTLS).
90
+ * @param {Buffer|string|null} [clientCert] - Client certificate in PEM format (for mTLS).
91
+ * @param {object} [opts] - Additional options.
92
+ * @param {boolean} [opts.rejectUnauthorized=true] - Reject connections with invalid certs.
93
+ * @returns {ChannelCredentials}
94
+ *
95
+ * @example | Server-only TLS
96
+ * const creds = ChannelCredentials.createSsl(fs.readFileSync('ca.pem'));
97
+ *
98
+ * @example | Mutual TLS
99
+ * const creds = ChannelCredentials.createSsl(caPem, keyPem, certPem);
100
+ */
101
+ static createSsl(rootCerts, clientKey, clientCert, opts = {})
102
+ {
103
+ const config = {
104
+ rejectUnauthorized: opts.rejectUnauthorized !== false,
105
+ };
106
+
107
+ if (rootCerts)
108
+ {
109
+ config.ca = Buffer.isBuffer(rootCerts) ? rootCerts : Buffer.from(rootCerts);
110
+ }
111
+ if (clientKey)
112
+ {
113
+ config.key = Buffer.isBuffer(clientKey) ? clientKey : Buffer.from(clientKey);
114
+ }
115
+ if (clientCert)
116
+ {
117
+ config.cert = Buffer.isBuffer(clientCert) ? clientCert : Buffer.from(clientCert);
118
+ }
119
+
120
+ if (config.key && !config.cert)
121
+ throw new Error('Client key provided without client certificate');
122
+ if (config.cert && !config.key)
123
+ throw new Error('Client certificate provided without client key');
124
+
125
+ return new ChannelCredentials(CredentialType.SSL, config);
126
+ }
127
+
128
+ /**
129
+ * Create SSL credentials from PEM file paths.
130
+ * Files are read once at creation time.
131
+ *
132
+ * @param {string|null} [caPath] - Path to CA certificate file.
133
+ * @param {string|null} [keyPath] - Path to client key file.
134
+ * @param {string|null} [certPath] - Path to client certificate file.
135
+ * @param {object} [opts] - Additional options.
136
+ * @returns {ChannelCredentials}
137
+ *
138
+ * @example
139
+ * const creds = ChannelCredentials.createSslFromFiles('./certs/ca.pem', './certs/client.key', './certs/client.pem');
140
+ */
141
+ static createSslFromFiles(caPath, keyPath, certPath, opts = {})
142
+ {
143
+ const ca = caPath ? fs.readFileSync(caPath) : null;
144
+ const key = keyPath ? fs.readFileSync(keyPath) : null;
145
+ const cert = certPath ? fs.readFileSync(certPath) : null;
146
+ return ChannelCredentials.createSsl(ca, key, cert, opts);
147
+ }
148
+
149
+ /**
150
+ * Create per-call metadata credentials.
151
+ * The generator function is called before each RPC to produce
152
+ * metadata headers (e.g. authorization tokens).
153
+ *
154
+ * @param {Function} metadataGenerator - `(params?) => object|Promise<object>`.
155
+ * Returns key-value pairs to merge into call metadata.
156
+ * `params` includes `{ serviceUrl, methodName }`.
157
+ * @returns {ChannelCredentials}
158
+ *
159
+ * @example
160
+ * const creds = ChannelCredentials.createFromMetadata(async () => ({
161
+ * authorization: 'Bearer ' + await getAccessToken(),
162
+ * }));
163
+ */
164
+ static createFromMetadata(metadataGenerator)
165
+ {
166
+ if (typeof metadataGenerator !== 'function')
167
+ throw new Error('createFromMetadata requires a function');
168
+
169
+ return new ChannelCredentials(CredentialType.METADATA, {
170
+ generator: metadataGenerator,
171
+ });
172
+ }
173
+
174
+ /**
175
+ * Combine multiple credentials into one.
176
+ * At most one channel credential (insecure/SSL) and any number
177
+ * of call credentials (metadata) can be combined.
178
+ *
179
+ * @param {...ChannelCredentials} credentials - Credentials to combine.
180
+ * @returns {ChannelCredentials}
181
+ *
182
+ * @example
183
+ * const creds = ChannelCredentials.combine(
184
+ * ChannelCredentials.createSsl(ca),
185
+ * ChannelCredentials.createFromMetadata(() => ({ 'x-api-key': apiKey })),
186
+ * ChannelCredentials.createFromMetadata(async () => ({ authorization: 'Bearer ' + token })),
187
+ * );
188
+ */
189
+ static combine(...credentials)
190
+ {
191
+ let channelCred = null;
192
+ const metadataGens = [];
193
+
194
+ for (const cred of credentials)
195
+ {
196
+ if (!(cred instanceof ChannelCredentials))
197
+ throw new Error('combine() arguments must be ChannelCredentials instances');
198
+
199
+ if (cred.type === CredentialType.COMPOSITE)
200
+ {
201
+ // Flatten nested composites
202
+ if (cred._config.channelCred) channelCred = cred._config.channelCred;
203
+ metadataGens.push(...cred._config.metadataGenerators);
204
+ }
205
+ else if (cred.type === CredentialType.INSECURE || cred.type === CredentialType.SSL)
206
+ {
207
+ if (channelCred)
208
+ throw new Error('Cannot combine multiple channel credentials (use at most one insecure/SSL)');
209
+ channelCred = cred;
210
+ }
211
+ else if (cred.type === CredentialType.METADATA)
212
+ {
213
+ metadataGens.push(cred._config.generator);
214
+ }
215
+ }
216
+
217
+ return new ChannelCredentials(CredentialType.COMPOSITE, {
218
+ channelCred,
219
+ metadataGenerators: metadataGens,
220
+ });
221
+ }
222
+
223
+ /**
224
+ * Whether this credential uses TLS.
225
+ * @returns {boolean}
226
+ */
227
+ isSecure()
228
+ {
229
+ if (this.type === CredentialType.INSECURE) return false;
230
+ if (this.type === CredentialType.SSL) return true;
231
+ if (this.type === CredentialType.METADATA) return false;
232
+ if (this.type === CredentialType.COMPOSITE)
233
+ {
234
+ const ch = this._config.channelCred;
235
+ return ch ? ch.isSecure() : false;
236
+ }
237
+ return false;
238
+ }
239
+
240
+ /**
241
+ * Get the TLS connection options for `http2.connect()`.
242
+ * Returns `null` for insecure/metadata-only credentials.
243
+ *
244
+ * @returns {object|null} TLS options `{ ca, key, cert, rejectUnauthorized }`.
245
+ */
246
+ getConnectionOptions()
247
+ {
248
+ if (this.type === CredentialType.SSL)
249
+ {
250
+ const opts = {};
251
+ if (this._config.ca) opts.ca = this._config.ca;
252
+ if (this._config.key) opts.key = this._config.key;
253
+ if (this._config.cert) opts.cert = this._config.cert;
254
+ opts.rejectUnauthorized = this._config.rejectUnauthorized;
255
+ return opts;
256
+ }
257
+
258
+ if (this.type === CredentialType.COMPOSITE && this._config.channelCred)
259
+ {
260
+ return this._config.channelCred.getConnectionOptions();
261
+ }
262
+
263
+ return null;
264
+ }
265
+
266
+ /**
267
+ * Generate per-call metadata by running all metadata generators.
268
+ *
269
+ * @param {object} [params] - Call parameters `{ serviceUrl, methodName }`.
270
+ * @returns {Promise<object>} Merged metadata key-value pairs.
271
+ */
272
+ async generateMetadata(params)
273
+ {
274
+ const generators = [];
275
+
276
+ if (this.type === CredentialType.METADATA)
277
+ {
278
+ generators.push(this._config.generator);
279
+ }
280
+ else if (this.type === CredentialType.COMPOSITE)
281
+ {
282
+ generators.push(...this._config.metadataGenerators);
283
+ }
284
+
285
+ if (generators.length === 0) return {};
286
+
287
+ const merged = {};
288
+ for (const gen of generators)
289
+ {
290
+ const md = await gen(params);
291
+ if (md && typeof md === 'object')
292
+ {
293
+ Object.assign(merged, md);
294
+ }
295
+ }
296
+
297
+ return merged;
298
+ }
299
+ }
300
+
301
+ // -- Certificate Rotation Helper ---------------------------------
302
+
303
+ /**
304
+ * Create SSL credentials with automatic certificate rotation.
305
+ * Watches certificate files for changes and reloads them.
306
+ *
307
+ * Returns a credentials-like object with `getCurrent()` to get
308
+ * the latest credentials and `stop()` to cease watching.
309
+ *
310
+ * @param {object} opts - Options.
311
+ * @param {string} opts.caPath - Path to CA certificate file.
312
+ * @param {string} [opts.keyPath] - Path to client key file (for mTLS).
313
+ * @param {string} [opts.certPath] - Path to client certificate file (for mTLS).
314
+ * @param {number} [opts.pollInterval=30000] - Check interval in ms (default 30s).
315
+ * @param {object} [opts.sslOpts] - Additional SSL options.
316
+ * @returns {{ getCurrent: () => ChannelCredentials, stop: () => void }}
317
+ *
318
+ * @example
319
+ * const rotating = createRotatingCredentials({
320
+ * caPath: '/certs/ca.pem',
321
+ * keyPath: '/certs/client.key',
322
+ * certPath: '/certs/client.pem',
323
+ * });
324
+ * // Use rotating.getCurrent() when creating clients
325
+ * const client = new GrpcClient({ address, credentials: rotating.getCurrent() }, schema, service);
326
+ * // Stop watching on shutdown
327
+ * rotating.stop();
328
+ */
329
+ function createRotatingCredentials(opts)
330
+ {
331
+ if (!opts || !opts.caPath)
332
+ throw new Error('createRotatingCredentials requires caPath');
333
+
334
+ let current = ChannelCredentials.createSslFromFiles(
335
+ opts.caPath, opts.keyPath || null, opts.certPath || null, opts.sslOpts || {}
336
+ );
337
+
338
+ /** @private */ let lastMtimes = _getMtimes(opts);
339
+
340
+ const timer = setInterval(() =>
341
+ {
342
+ try
343
+ {
344
+ const mtimes = _getMtimes(opts);
345
+ if (mtimes.ca !== lastMtimes.ca ||
346
+ mtimes.key !== lastMtimes.key ||
347
+ mtimes.cert !== lastMtimes.cert)
348
+ {
349
+ current = ChannelCredentials.createSslFromFiles(
350
+ opts.caPath, opts.keyPath || null, opts.certPath || null, opts.sslOpts || {}
351
+ );
352
+ lastMtimes = mtimes;
353
+ log.info('SSL credentials rotated');
354
+ }
355
+ }
356
+ catch (err)
357
+ {
358
+ log.error('credential rotation check failed: %s', err.message);
359
+ }
360
+ }, opts.pollInterval || 30000);
361
+
362
+ if (timer.unref) timer.unref();
363
+
364
+ return {
365
+ getCurrent() { return current; },
366
+ stop() { clearInterval(timer); },
367
+ };
368
+ }
369
+
370
+ /**
371
+ * Get file modification times.
372
+ * @private
373
+ */
374
+ function _getMtimes(opts)
375
+ {
376
+ return {
377
+ ca: opts.caPath ? _safeMtime(opts.caPath) : 0,
378
+ key: opts.keyPath ? _safeMtime(opts.keyPath) : 0,
379
+ cert: opts.certPath ? _safeMtime(opts.certPath) : 0,
380
+ };
381
+ }
382
+
383
+ /**
384
+ * @private
385
+ */
386
+ function _safeMtime(filePath)
387
+ {
388
+ try { return fs.statSync(filePath).mtimeMs; }
389
+ catch (_) { return 0; }
390
+ }
391
+
392
+ // -- Exports -------------------------------------------------
393
+
394
+ module.exports = {
395
+ ChannelCredentials,
396
+ CredentialType,
397
+ createRotatingCredentials,
398
+ };
@@ -0,0 +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
+ };