@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.
Files changed (126) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +460 -443
  3. package/index.js +414 -412
  4. package/lib/app.js +1172 -1172
  5. package/lib/auth/authorize.js +399 -399
  6. package/lib/auth/enrollment.js +367 -367
  7. package/lib/auth/index.js +57 -57
  8. package/lib/auth/jwt.js +731 -731
  9. package/lib/auth/oauth.js +362 -362
  10. package/lib/auth/session.js +588 -588
  11. package/lib/auth/trustedDevice.js +409 -409
  12. package/lib/auth/twoFactor.js +1150 -1150
  13. package/lib/auth/webauthn.js +946 -946
  14. package/lib/body/index.js +14 -14
  15. package/lib/body/json.js +109 -109
  16. package/lib/body/multipart.js +440 -440
  17. package/lib/body/raw.js +71 -71
  18. package/lib/body/rawBuffer.js +160 -160
  19. package/lib/body/sendError.js +25 -25
  20. package/lib/body/text.js +75 -75
  21. package/lib/body/typeMatch.js +41 -41
  22. package/lib/body/urlencoded.js +235 -235
  23. package/lib/cli.js +845 -845
  24. package/lib/cluster.js +666 -666
  25. package/lib/debug.js +372 -372
  26. package/lib/env/index.js +465 -465
  27. package/lib/errors.js +683 -683
  28. package/lib/fetch/index.js +256 -256
  29. package/lib/grpc/balancer.js +378 -378
  30. package/lib/grpc/call.js +708 -708
  31. package/lib/grpc/client.js +764 -764
  32. package/lib/grpc/codec.js +1221 -1221
  33. package/lib/grpc/credentials.js +398 -398
  34. package/lib/grpc/frame.js +262 -262
  35. package/lib/grpc/health.js +287 -287
  36. package/lib/grpc/index.js +121 -121
  37. package/lib/grpc/metadata.js +461 -461
  38. package/lib/grpc/proto.js +821 -821
  39. package/lib/grpc/reflection.js +590 -590
  40. package/lib/grpc/server.js +445 -445
  41. package/lib/grpc/status.js +118 -118
  42. package/lib/grpc/watch.js +173 -173
  43. package/lib/http/index.js +10 -10
  44. package/lib/http/request.js +727 -727
  45. package/lib/http/response.js +799 -799
  46. package/lib/lifecycle.js +557 -557
  47. package/lib/middleware/compress.js +230 -230
  48. package/lib/middleware/cookieParser.js +237 -237
  49. package/lib/middleware/cors.js +93 -93
  50. package/lib/middleware/csrf.js +137 -137
  51. package/lib/middleware/errorHandler.js +101 -101
  52. package/lib/middleware/helmet.js +175 -175
  53. package/lib/middleware/index.js +19 -17
  54. package/lib/middleware/logger.js +74 -74
  55. package/lib/middleware/rateLimit.js +88 -88
  56. package/lib/middleware/requestId.js +53 -53
  57. package/lib/middleware/static.js +326 -326
  58. package/lib/middleware/timeout.js +71 -71
  59. package/lib/middleware/validator.js +255 -255
  60. package/lib/observe/health.js +326 -326
  61. package/lib/observe/index.js +50 -50
  62. package/lib/observe/logger.js +359 -359
  63. package/lib/observe/metrics.js +805 -805
  64. package/lib/observe/tracing.js +592 -592
  65. package/lib/orm/adapters/json.js +290 -290
  66. package/lib/orm/adapters/memory.js +764 -764
  67. package/lib/orm/adapters/mongo.js +764 -764
  68. package/lib/orm/adapters/mysql.js +933 -933
  69. package/lib/orm/adapters/postgres.js +1144 -1144
  70. package/lib/orm/adapters/redis.js +1534 -1534
  71. package/lib/orm/adapters/sql-base.js +212 -212
  72. package/lib/orm/adapters/sqlite.js +858 -858
  73. package/lib/orm/audit.js +649 -649
  74. package/lib/orm/cache.js +394 -394
  75. package/lib/orm/geo.js +387 -387
  76. package/lib/orm/index.js +784 -784
  77. package/lib/orm/migrate.js +432 -432
  78. package/lib/orm/model.js +1706 -1706
  79. package/lib/orm/plugin.js +375 -375
  80. package/lib/orm/procedures.js +836 -836
  81. package/lib/orm/profiler.js +233 -233
  82. package/lib/orm/query.js +1772 -1772
  83. package/lib/orm/replicas.js +241 -241
  84. package/lib/orm/schema.js +307 -307
  85. package/lib/orm/search.js +380 -380
  86. package/lib/orm/seed/data/commerce.js +136 -136
  87. package/lib/orm/seed/data/internet.js +111 -111
  88. package/lib/orm/seed/data/locations.js +204 -204
  89. package/lib/orm/seed/data/names.js +338 -338
  90. package/lib/orm/seed/data/person.js +128 -128
  91. package/lib/orm/seed/data/phone.js +211 -211
  92. package/lib/orm/seed/data/words.js +134 -134
  93. package/lib/orm/seed/factory.js +178 -178
  94. package/lib/orm/seed/fake.js +1186 -1186
  95. package/lib/orm/seed/index.js +18 -18
  96. package/lib/orm/seed/rng.js +70 -70
  97. package/lib/orm/seed/seeder.js +124 -124
  98. package/lib/orm/seed/unique.js +68 -68
  99. package/lib/orm/snapshot.js +366 -366
  100. package/lib/orm/tenancy.js +605 -605
  101. package/lib/orm/views.js +350 -350
  102. package/lib/router/index.js +436 -436
  103. package/lib/sse/index.js +8 -8
  104. package/lib/sse/stream.js +349 -349
  105. package/lib/ws/connection.js +451 -451
  106. package/lib/ws/handshake.js +125 -125
  107. package/lib/ws/index.js +14 -14
  108. package/lib/ws/room.js +223 -223
  109. package/package.json +73 -73
  110. package/types/app.d.ts +223 -223
  111. package/types/auth.d.ts +520 -520
  112. package/types/cluster.d.ts +75 -75
  113. package/types/env.d.ts +80 -80
  114. package/types/errors.d.ts +316 -316
  115. package/types/fetch.d.ts +43 -43
  116. package/types/grpc.d.ts +432 -432
  117. package/types/index.d.ts +384 -384
  118. package/types/lifecycle.d.ts +60 -60
  119. package/types/middleware.d.ts +320 -320
  120. package/types/observe.d.ts +304 -304
  121. package/types/orm.d.ts +1887 -1887
  122. package/types/request.d.ts +109 -109
  123. package/types/response.d.ts +157 -157
  124. package/types/router.d.ts +78 -78
  125. package/types/sse.d.ts +78 -78
  126. package/types/websocket.d.ts +126 -126
@@ -1,445 +1,445 @@
1
- /**
2
- * @module grpc/server
3
- * @description gRPC server for zero-server.
4
- * Intercepts HTTP/2 streams with `content-type: application/grpc`,
5
- * routes by `:path` pseudo-header (`/package.Service/Method`),
6
- * and dispatches to registered service handlers.
7
- *
8
- * Handles all four gRPC call types:
9
- * - Unary (single request → single response)
10
- * - Server streaming (single request → multiple responses)
11
- * - Client streaming (multiple requests → single response)
12
- * - Bidirectional streaming (multiple requests ↔ multiple responses)
13
- *
14
- * Supports interceptors (server-side middleware), deadline enforcement,
15
- * message size limits, and graceful shutdown with call draining.
16
- *
17
- * @example
18
- * const { createApp, parseProto } = require('@zero-server/sdk');
19
- * const app = createApp();
20
- * const schema = parseProto(fs.readFileSync('hello.proto', 'utf8'));
21
- *
22
- * app.grpc(schema, 'Greeter', {
23
- * SayHello(call) {
24
- * return { message: 'Hello ' + call.request.name };
25
- * },
26
- * });
27
- *
28
- * app.listen(50051, { http2: true });
29
- *
30
- * @example | Server streaming
31
- * app.grpc(schema, 'DataService', {
32
- * StreamData(call) {
33
- * for (let i = 0; i < 100; i++) {
34
- * call.write({ seq: i, payload: 'chunk-' + i });
35
- * }
36
- * call.end();
37
- * },
38
- * });
39
- *
40
- * @example | Bidirectional streaming with interceptors
41
- * app.grpc(schema, 'ChatService', {
42
- * Chat(call) {
43
- * for await (const msg of call) {
44
- * call.write({ echo: msg.text, ts: Date.now() });
45
- * }
46
- * call.end();
47
- * },
48
- * }, {
49
- * interceptors: [authInterceptor, loggingInterceptor],
50
- * });
51
- */
52
-
53
- const log = require('../debug')('zero:grpc');
54
- const { GrpcStatus, statusName } = require('./status');
55
- const { Metadata } = require('./metadata');
56
- const { UnaryCall, ServerStreamCall, ClientStreamCall, BidiStreamCall } = require('./call');
57
-
58
- // -- Service Registry --------------------------------------
59
-
60
- /**
61
- * Registry of gRPC services and their handlers.
62
- * Manages routing of HTTP/2 streams to the correct service method.
63
- *
64
- * @class
65
- *
66
- * @example
67
- * const registry = new GrpcServiceRegistry();
68
- * registry.addService(schema, 'Greeter', handlers, opts);
69
- */
70
- class GrpcServiceRegistry
71
- {
72
- constructor()
73
- {
74
- /**
75
- * Map of path → handler descriptor.
76
- * Keys are in the format `/package.ServiceName/MethodName`.
77
- * @type {Map<string, { service: string, method: object, handler: Function, schema: object, opts: object }>}
78
- */
79
- this._routes = new Map();
80
-
81
- /**
82
- * Global interceptors applied to all services.
83
- * @type {Function[]}
84
- */
85
- this._interceptors = [];
86
-
87
- /**
88
- * Active calls for graceful shutdown draining.
89
- * @type {Set<import('./call').BaseCall>}
90
- */
91
- this._activeCalls = new Set();
92
-
93
- /**
94
- * Whether the server is draining (rejecting new calls).
95
- * @type {boolean}
96
- */
97
- this._draining = false;
98
- }
99
-
100
- /**
101
- * Register a service with its handlers.
102
- *
103
- * @param {object} schema - Parsed proto schema from `parseProto()`.
104
- * @param {string} serviceName - Name of the service as defined in the proto file.
105
- * @param {Object<string, Function>} handlers - Map of method names to handler functions.
106
- * @param {object} [opts] - Service options.
107
- * @param {Function[]} [opts.interceptors] - Per-service interceptors.
108
- * @param {number} [opts.maxMessageSize] - Max incoming message size in bytes.
109
- * @param {boolean} [opts.compress=false] - Whether to compress outgoing messages.
110
- *
111
- * @example
112
- * registry.addService(schema, 'Greeter', {
113
- * SayHello(call) { return { message: 'Hello ' + call.request.name }; },
114
- * });
115
- */
116
- addService(schema, serviceName, handlers, opts = {})
117
- {
118
- const service = schema.services[serviceName];
119
- if (!service)
120
- {
121
- throw new Error(`Service "${serviceName}" not found in proto schema. ` +
122
- `Available: ${Object.keys(schema.services).join(', ') || 'none'}`);
123
- }
124
-
125
- // Build the package prefix for routing
126
- const packagePrefix = schema.package ? schema.package + '.' : '';
127
- const pathPrefix = '/' + packagePrefix + serviceName;
128
-
129
- for (const [methodName, methodDef] of Object.entries(service.methods))
130
- {
131
- if (!handlers[methodName])
132
- {
133
- log.warn('no handler for %s/%s — will return UNIMPLEMENTED', serviceName, methodName);
134
- }
135
-
136
- const routePath = pathPrefix + '/' + methodName;
137
- this._routes.set(routePath, {
138
- service: serviceName,
139
- method: methodDef,
140
- handler: handlers[methodName] || null,
141
- schema,
142
- opts,
143
- });
144
-
145
- log.info('registered gRPC method %s [%s]', routePath,
146
- _callType(methodDef));
147
- }
148
- }
149
-
150
- /**
151
- * Add a global interceptor that runs before every gRPC call.
152
- * Interceptors receive `(call, next)` and must call `next()` to continue.
153
- *
154
- * @param {Function} fn - Interceptor function `(call, next) => void`.
155
- *
156
- * @example
157
- * registry.addInterceptor(async (call, next) => {
158
- * const token = call.metadata.get('authorization');
159
- * if (!token) return call.sendError(GrpcStatus.UNAUTHENTICATED, 'Missing auth');
160
- * await next();
161
- * });
162
- */
163
- addInterceptor(fn)
164
- {
165
- this._interceptors.push(fn);
166
- }
167
-
168
- /**
169
- * Handle an incoming HTTP/2 stream. Determines if it's a gRPC call,
170
- * routes to the correct handler, and manages the call lifecycle.
171
- *
172
- * @param {import('http2').Http2Stream} stream - The HTTP/2 stream.
173
- * @param {object} headers - HTTP/2 headers from the stream event.
174
- * @returns {boolean} `true` if this was handled as a gRPC call.
175
- */
176
- handleStream(stream, headers)
177
- {
178
- const contentType = headers['content-type'] || '';
179
- if (!contentType.startsWith('application/grpc'))
180
- {
181
- return false; // Not a gRPC request — let the normal HTTP pipeline handle it
182
- }
183
-
184
- const grpcPath = headers[':path'];
185
- const method = headers[':method'];
186
-
187
- // gRPC always uses POST
188
- if (method !== 'POST')
189
- {
190
- _sendError(stream, GrpcStatus.UNIMPLEMENTED,
191
- 'gRPC requires POST method');
192
- return true;
193
- }
194
-
195
- // Reject new calls during shutdown drain
196
- if (this._draining)
197
- {
198
- _sendError(stream, GrpcStatus.UNAVAILABLE,
199
- 'Server is shutting down');
200
- return true;
201
- }
202
-
203
- // Look up the route
204
- const route = this._routes.get(grpcPath);
205
- if (!route)
206
- {
207
- log.warn('unregistered gRPC path: %s', grpcPath);
208
- _sendError(stream, GrpcStatus.UNIMPLEMENTED,
209
- `Method not found: ${grpcPath}`);
210
- return true;
211
- }
212
-
213
- if (!route.handler)
214
- {
215
- _sendError(stream, GrpcStatus.UNIMPLEMENTED,
216
- `Method not implemented: ${grpcPath}`);
217
- return true;
218
- }
219
-
220
- // Dispatch the call
221
- this._dispatch(stream, headers, route)
222
- .catch((err) =>
223
- {
224
- log.error('unhandled error in gRPC handler %s: %s', grpcPath, err.message);
225
- });
226
-
227
- return true;
228
- }
229
-
230
- /**
231
- * Dispatch a gRPC call to the appropriate handler.
232
- * @private
233
- * @param {import('http2').Http2Stream} stream
234
- * @param {object} headers
235
- * @param {object} route
236
- */
237
- async _dispatch(stream, headers, route)
238
- {
239
- const { method: methodDef, handler, schema, opts } = route;
240
-
241
- // Parse metadata from headers
242
- const metadata = Metadata.fromHeaders(headers);
243
-
244
- // Create the appropriate call object
245
- const CallClass = _pickCallClass(methodDef);
246
- const call = new CallClass(stream, methodDef, schema.messages, metadata, {
247
- maxMessageSize: opts.maxMessageSize,
248
- compress: opts.compress,
249
- });
250
-
251
- // Track for graceful shutdown
252
- this._activeCalls.add(call);
253
- stream.on('close', () => this._activeCalls.delete(call));
254
-
255
- try
256
- {
257
- // Initialize the call (collect request body / set up streaming)
258
- await call._init();
259
-
260
- // Run interceptors + handler
261
- const interceptors = [
262
- ...this._interceptors,
263
- ...(opts.interceptors || []),
264
- ];
265
-
266
- await _runInterceptors(interceptors, call, async () =>
267
- {
268
- if (call.cancelled) return;
269
-
270
- const result = await handler(call);
271
-
272
- // For unary and client-streaming: if the handler returned
273
- // a value, send it as the response automatically
274
- if (result !== undefined && result !== null && !call._ended)
275
- {
276
- call.write(result);
277
- call.sendStatus(GrpcStatus.OK);
278
- }
279
- });
280
- }
281
- catch (err)
282
- {
283
- log.error('gRPC handler error in %s: %s', methodDef.name, err.message);
284
- if (!call._ended)
285
- {
286
- const code = err.grpcCode || GrpcStatus.INTERNAL;
287
- call.sendError(code, err.message);
288
- }
289
- }
290
- }
291
-
292
- /**
293
- * Begin draining — reject new calls and wait for active calls to finish.
294
- *
295
- * @param {number} [timeout=30000] - Maximum time to wait in ms.
296
- * @returns {Promise<void>}
297
- */
298
- async drain(timeout = 30000)
299
- {
300
- this._draining = true;
301
- log.info('gRPC draining, %d active calls', this._activeCalls.size);
302
-
303
- if (this._activeCalls.size === 0) return;
304
-
305
- return new Promise((resolve) =>
306
- {
307
- const check = () =>
308
- {
309
- if (this._activeCalls.size === 0)
310
- {
311
- clearTimeout(timer);
312
- resolve();
313
- }
314
- };
315
-
316
- // Check periodically
317
- const interval = setInterval(check, 100);
318
- if (interval.unref) interval.unref();
319
-
320
- const timer = setTimeout(() =>
321
- {
322
- clearInterval(interval);
323
- log.warn('gRPC drain timed out with %d active calls', this._activeCalls.size);
324
- // Force-close remaining calls
325
- for (const call of this._activeCalls)
326
- {
327
- call.sendError(GrpcStatus.UNAVAILABLE, 'Server shutting down');
328
- }
329
- resolve();
330
- }, timeout);
331
- if (timer.unref) timer.unref();
332
- });
333
- }
334
-
335
- /**
336
- * Get all registered routes for introspection.
337
- *
338
- * @returns {{ method: string, path: string, type: string }[]}
339
- *
340
- * @example
341
- * registry.routes();
342
- * // [{ method: 'GRPC', path: '/myapp.Greeter/SayHello', type: 'unary' }]
343
- */
344
- routes()
345
- {
346
- const list = [];
347
- for (const [path, route] of this._routes)
348
- {
349
- list.push({
350
- method: 'GRPC',
351
- path,
352
- type: _callType(route.method),
353
- implemented: !!route.handler,
354
- });
355
- }
356
- return list;
357
- }
358
- }
359
-
360
- // -- Helpers -----------------------------------------------
361
-
362
- /**
363
- * Pick the Call class based on method streaming flags.
364
- * @private
365
- * @param {object} methodDef
366
- * @returns {typeof import('./call').BaseCall}
367
- */
368
- function _pickCallClass(methodDef)
369
- {
370
- if (methodDef.clientStreaming && methodDef.serverStreaming) return BidiStreamCall;
371
- if (methodDef.clientStreaming) return ClientStreamCall;
372
- if (methodDef.serverStreaming) return ServerStreamCall;
373
- return UnaryCall;
374
- }
375
-
376
- /**
377
- * Describe the call type for logging/introspection.
378
- * @private
379
- * @param {object} methodDef
380
- * @returns {string}
381
- */
382
- function _callType(methodDef)
383
- {
384
- if (methodDef.clientStreaming && methodDef.serverStreaming) return 'bidi';
385
- if (methodDef.clientStreaming) return 'client-stream';
386
- if (methodDef.serverStreaming) return 'server-stream';
387
- return 'unary';
388
- }
389
-
390
- /**
391
- * Run a chain of interceptors with final handler.
392
- * @private
393
- * @param {Function[]} interceptors
394
- * @param {import('./call').BaseCall} call
395
- * @param {Function} finalHandler
396
- */
397
- async function _runInterceptors(interceptors, call, finalHandler)
398
- {
399
- let idx = 0;
400
-
401
- async function next()
402
- {
403
- if (call._ended || call._cancelled) return;
404
- if (idx < interceptors.length)
405
- {
406
- const fn = interceptors[idx++];
407
- await fn(call, next);
408
- }
409
- else
410
- {
411
- await finalHandler();
412
- }
413
- }
414
-
415
- await next();
416
- }
417
-
418
- /**
419
- * Send a gRPC error on a raw HTTP/2 stream (before a Call object is created).
420
- * @private
421
- * @param {import('http2').Http2Stream} stream
422
- * @param {number} code
423
- * @param {string} message
424
- */
425
- function _sendError(stream, code, message)
426
- {
427
- try
428
- {
429
- stream.respond({
430
- ':status': 200,
431
- 'content-type': 'application/grpc+proto',
432
- 'grpc-status': String(code),
433
- 'grpc-message': encodeURIComponent(message),
434
- }, { endStream: true });
435
- }
436
- catch (_)
437
- {
438
- try { stream.close(); }
439
- catch (__) { /* stream already closed */ }
440
- }
441
- }
442
-
443
- module.exports = {
444
- GrpcServiceRegistry,
445
- };
1
+ /**
2
+ * @module grpc/server
3
+ * @description gRPC server for zero-server.
4
+ * Intercepts HTTP/2 streams with `content-type: application/grpc`,
5
+ * routes by `:path` pseudo-header (`/package.Service/Method`),
6
+ * and dispatches to registered service handlers.
7
+ *
8
+ * Handles all four gRPC call types:
9
+ * - Unary (single request → single response)
10
+ * - Server streaming (single request → multiple responses)
11
+ * - Client streaming (multiple requests → single response)
12
+ * - Bidirectional streaming (multiple requests ↔ multiple responses)
13
+ *
14
+ * Supports interceptors (server-side middleware), deadline enforcement,
15
+ * message size limits, and graceful shutdown with call draining.
16
+ *
17
+ * @example
18
+ * const { createApp, parseProto } = require('@zero-server/sdk');
19
+ * const app = createApp();
20
+ * const schema = parseProto(fs.readFileSync('hello.proto', 'utf8'));
21
+ *
22
+ * app.grpc(schema, 'Greeter', {
23
+ * SayHello(call) {
24
+ * return { message: 'Hello ' + call.request.name };
25
+ * },
26
+ * });
27
+ *
28
+ * app.listen(50051, { http2: true });
29
+ *
30
+ * @example | Server streaming
31
+ * app.grpc(schema, 'DataService', {
32
+ * StreamData(call) {
33
+ * for (let i = 0; i < 100; i++) {
34
+ * call.write({ seq: i, payload: 'chunk-' + i });
35
+ * }
36
+ * call.end();
37
+ * },
38
+ * });
39
+ *
40
+ * @example | Bidirectional streaming with interceptors
41
+ * app.grpc(schema, 'ChatService', {
42
+ * Chat(call) {
43
+ * for await (const msg of call) {
44
+ * call.write({ echo: msg.text, ts: Date.now() });
45
+ * }
46
+ * call.end();
47
+ * },
48
+ * }, {
49
+ * interceptors: [authInterceptor, loggingInterceptor],
50
+ * });
51
+ */
52
+
53
+ const log = require('../debug')('zero:grpc');
54
+ const { GrpcStatus, statusName } = require('./status');
55
+ const { Metadata } = require('./metadata');
56
+ const { UnaryCall, ServerStreamCall, ClientStreamCall, BidiStreamCall } = require('./call');
57
+
58
+ // -- Service Registry --------------------------------------
59
+
60
+ /**
61
+ * Registry of gRPC services and their handlers.
62
+ * Manages routing of HTTP/2 streams to the correct service method.
63
+ *
64
+ * @class
65
+ *
66
+ * @example
67
+ * const registry = new GrpcServiceRegistry();
68
+ * registry.addService(schema, 'Greeter', handlers, opts);
69
+ */
70
+ class GrpcServiceRegistry
71
+ {
72
+ constructor()
73
+ {
74
+ /**
75
+ * Map of path → handler descriptor.
76
+ * Keys are in the format `/package.ServiceName/MethodName`.
77
+ * @type {Map<string, { service: string, method: object, handler: Function, schema: object, opts: object }>}
78
+ */
79
+ this._routes = new Map();
80
+
81
+ /**
82
+ * Global interceptors applied to all services.
83
+ * @type {Function[]}
84
+ */
85
+ this._interceptors = [];
86
+
87
+ /**
88
+ * Active calls for graceful shutdown draining.
89
+ * @type {Set<import('./call').BaseCall>}
90
+ */
91
+ this._activeCalls = new Set();
92
+
93
+ /**
94
+ * Whether the server is draining (rejecting new calls).
95
+ * @type {boolean}
96
+ */
97
+ this._draining = false;
98
+ }
99
+
100
+ /**
101
+ * Register a service with its handlers.
102
+ *
103
+ * @param {object} schema - Parsed proto schema from `parseProto()`.
104
+ * @param {string} serviceName - Name of the service as defined in the proto file.
105
+ * @param {Object<string, Function>} handlers - Map of method names to handler functions.
106
+ * @param {object} [opts] - Service options.
107
+ * @param {Function[]} [opts.interceptors] - Per-service interceptors.
108
+ * @param {number} [opts.maxMessageSize] - Max incoming message size in bytes.
109
+ * @param {boolean} [opts.compress=false] - Whether to compress outgoing messages.
110
+ *
111
+ * @example
112
+ * registry.addService(schema, 'Greeter', {
113
+ * SayHello(call) { return { message: 'Hello ' + call.request.name }; },
114
+ * });
115
+ */
116
+ addService(schema, serviceName, handlers, opts = {})
117
+ {
118
+ const service = schema.services[serviceName];
119
+ if (!service)
120
+ {
121
+ throw new Error(`Service "${serviceName}" not found in proto schema. ` +
122
+ `Available: ${Object.keys(schema.services).join(', ') || 'none'}`);
123
+ }
124
+
125
+ // Build the package prefix for routing
126
+ const packagePrefix = schema.package ? schema.package + '.' : '';
127
+ const pathPrefix = '/' + packagePrefix + serviceName;
128
+
129
+ for (const [methodName, methodDef] of Object.entries(service.methods))
130
+ {
131
+ if (!handlers[methodName])
132
+ {
133
+ log.warn('no handler for %s/%s — will return UNIMPLEMENTED', serviceName, methodName);
134
+ }
135
+
136
+ const routePath = pathPrefix + '/' + methodName;
137
+ this._routes.set(routePath, {
138
+ service: serviceName,
139
+ method: methodDef,
140
+ handler: handlers[methodName] || null,
141
+ schema,
142
+ opts,
143
+ });
144
+
145
+ log.info('registered gRPC method %s [%s]', routePath,
146
+ _callType(methodDef));
147
+ }
148
+ }
149
+
150
+ /**
151
+ * Add a global interceptor that runs before every gRPC call.
152
+ * Interceptors receive `(call, next)` and must call `next()` to continue.
153
+ *
154
+ * @param {Function} fn - Interceptor function `(call, next) => void`.
155
+ *
156
+ * @example
157
+ * registry.addInterceptor(async (call, next) => {
158
+ * const token = call.metadata.get('authorization');
159
+ * if (!token) return call.sendError(GrpcStatus.UNAUTHENTICATED, 'Missing auth');
160
+ * await next();
161
+ * });
162
+ */
163
+ addInterceptor(fn)
164
+ {
165
+ this._interceptors.push(fn);
166
+ }
167
+
168
+ /**
169
+ * Handle an incoming HTTP/2 stream. Determines if it's a gRPC call,
170
+ * routes to the correct handler, and manages the call lifecycle.
171
+ *
172
+ * @param {import('http2').Http2Stream} stream - The HTTP/2 stream.
173
+ * @param {object} headers - HTTP/2 headers from the stream event.
174
+ * @returns {boolean} `true` if this was handled as a gRPC call.
175
+ */
176
+ handleStream(stream, headers)
177
+ {
178
+ const contentType = headers['content-type'] || '';
179
+ if (!contentType.startsWith('application/grpc'))
180
+ {
181
+ return false; // Not a gRPC request — let the normal HTTP pipeline handle it
182
+ }
183
+
184
+ const grpcPath = headers[':path'];
185
+ const method = headers[':method'];
186
+
187
+ // gRPC always uses POST
188
+ if (method !== 'POST')
189
+ {
190
+ _sendError(stream, GrpcStatus.UNIMPLEMENTED,
191
+ 'gRPC requires POST method');
192
+ return true;
193
+ }
194
+
195
+ // Reject new calls during shutdown drain
196
+ if (this._draining)
197
+ {
198
+ _sendError(stream, GrpcStatus.UNAVAILABLE,
199
+ 'Server is shutting down');
200
+ return true;
201
+ }
202
+
203
+ // Look up the route
204
+ const route = this._routes.get(grpcPath);
205
+ if (!route)
206
+ {
207
+ log.warn('unregistered gRPC path: %s', grpcPath);
208
+ _sendError(stream, GrpcStatus.UNIMPLEMENTED,
209
+ `Method not found: ${grpcPath}`);
210
+ return true;
211
+ }
212
+
213
+ if (!route.handler)
214
+ {
215
+ _sendError(stream, GrpcStatus.UNIMPLEMENTED,
216
+ `Method not implemented: ${grpcPath}`);
217
+ return true;
218
+ }
219
+
220
+ // Dispatch the call
221
+ this._dispatch(stream, headers, route)
222
+ .catch((err) =>
223
+ {
224
+ log.error('unhandled error in gRPC handler %s: %s', grpcPath, err.message);
225
+ });
226
+
227
+ return true;
228
+ }
229
+
230
+ /**
231
+ * Dispatch a gRPC call to the appropriate handler.
232
+ * @private
233
+ * @param {import('http2').Http2Stream} stream
234
+ * @param {object} headers
235
+ * @param {object} route
236
+ */
237
+ async _dispatch(stream, headers, route)
238
+ {
239
+ const { method: methodDef, handler, schema, opts } = route;
240
+
241
+ // Parse metadata from headers
242
+ const metadata = Metadata.fromHeaders(headers);
243
+
244
+ // Create the appropriate call object
245
+ const CallClass = _pickCallClass(methodDef);
246
+ const call = new CallClass(stream, methodDef, schema.messages, metadata, {
247
+ maxMessageSize: opts.maxMessageSize,
248
+ compress: opts.compress,
249
+ });
250
+
251
+ // Track for graceful shutdown
252
+ this._activeCalls.add(call);
253
+ stream.on('close', () => this._activeCalls.delete(call));
254
+
255
+ try
256
+ {
257
+ // Initialize the call (collect request body / set up streaming)
258
+ await call._init();
259
+
260
+ // Run interceptors + handler
261
+ const interceptors = [
262
+ ...this._interceptors,
263
+ ...(opts.interceptors || []),
264
+ ];
265
+
266
+ await _runInterceptors(interceptors, call, async () =>
267
+ {
268
+ if (call.cancelled) return;
269
+
270
+ const result = await handler(call);
271
+
272
+ // For unary and client-streaming: if the handler returned
273
+ // a value, send it as the response automatically
274
+ if (result !== undefined && result !== null && !call._ended)
275
+ {
276
+ call.write(result);
277
+ call.sendStatus(GrpcStatus.OK);
278
+ }
279
+ });
280
+ }
281
+ catch (err)
282
+ {
283
+ log.error('gRPC handler error in %s: %s', methodDef.name, err.message);
284
+ if (!call._ended)
285
+ {
286
+ const code = err.grpcCode || GrpcStatus.INTERNAL;
287
+ call.sendError(code, err.message);
288
+ }
289
+ }
290
+ }
291
+
292
+ /**
293
+ * Begin draining — reject new calls and wait for active calls to finish.
294
+ *
295
+ * @param {number} [timeout=30000] - Maximum time to wait in ms.
296
+ * @returns {Promise<void>}
297
+ */
298
+ async drain(timeout = 30000)
299
+ {
300
+ this._draining = true;
301
+ log.info('gRPC draining, %d active calls', this._activeCalls.size);
302
+
303
+ if (this._activeCalls.size === 0) return;
304
+
305
+ return new Promise((resolve) =>
306
+ {
307
+ const check = () =>
308
+ {
309
+ if (this._activeCalls.size === 0)
310
+ {
311
+ clearTimeout(timer);
312
+ resolve();
313
+ }
314
+ };
315
+
316
+ // Check periodically
317
+ const interval = setInterval(check, 100);
318
+ if (interval.unref) interval.unref();
319
+
320
+ const timer = setTimeout(() =>
321
+ {
322
+ clearInterval(interval);
323
+ log.warn('gRPC drain timed out with %d active calls', this._activeCalls.size);
324
+ // Force-close remaining calls
325
+ for (const call of this._activeCalls)
326
+ {
327
+ call.sendError(GrpcStatus.UNAVAILABLE, 'Server shutting down');
328
+ }
329
+ resolve();
330
+ }, timeout);
331
+ if (timer.unref) timer.unref();
332
+ });
333
+ }
334
+
335
+ /**
336
+ * Get all registered routes for introspection.
337
+ *
338
+ * @returns {{ method: string, path: string, type: string }[]}
339
+ *
340
+ * @example
341
+ * registry.routes();
342
+ * // [{ method: 'GRPC', path: '/myapp.Greeter/SayHello', type: 'unary' }]
343
+ */
344
+ routes()
345
+ {
346
+ const list = [];
347
+ for (const [path, route] of this._routes)
348
+ {
349
+ list.push({
350
+ method: 'GRPC',
351
+ path,
352
+ type: _callType(route.method),
353
+ implemented: !!route.handler,
354
+ });
355
+ }
356
+ return list;
357
+ }
358
+ }
359
+
360
+ // -- Helpers -----------------------------------------------
361
+
362
+ /**
363
+ * Pick the Call class based on method streaming flags.
364
+ * @private
365
+ * @param {object} methodDef
366
+ * @returns {typeof import('./call').BaseCall}
367
+ */
368
+ function _pickCallClass(methodDef)
369
+ {
370
+ if (methodDef.clientStreaming && methodDef.serverStreaming) return BidiStreamCall;
371
+ if (methodDef.clientStreaming) return ClientStreamCall;
372
+ if (methodDef.serverStreaming) return ServerStreamCall;
373
+ return UnaryCall;
374
+ }
375
+
376
+ /**
377
+ * Describe the call type for logging/introspection.
378
+ * @private
379
+ * @param {object} methodDef
380
+ * @returns {string}
381
+ */
382
+ function _callType(methodDef)
383
+ {
384
+ if (methodDef.clientStreaming && methodDef.serverStreaming) return 'bidi';
385
+ if (methodDef.clientStreaming) return 'client-stream';
386
+ if (methodDef.serverStreaming) return 'server-stream';
387
+ return 'unary';
388
+ }
389
+
390
+ /**
391
+ * Run a chain of interceptors with final handler.
392
+ * @private
393
+ * @param {Function[]} interceptors
394
+ * @param {import('./call').BaseCall} call
395
+ * @param {Function} finalHandler
396
+ */
397
+ async function _runInterceptors(interceptors, call, finalHandler)
398
+ {
399
+ let idx = 0;
400
+
401
+ async function next()
402
+ {
403
+ if (call._ended || call._cancelled) return;
404
+ if (idx < interceptors.length)
405
+ {
406
+ const fn = interceptors[idx++];
407
+ await fn(call, next);
408
+ }
409
+ else
410
+ {
411
+ await finalHandler();
412
+ }
413
+ }
414
+
415
+ await next();
416
+ }
417
+
418
+ /**
419
+ * Send a gRPC error on a raw HTTP/2 stream (before a Call object is created).
420
+ * @private
421
+ * @param {import('http2').Http2Stream} stream
422
+ * @param {number} code
423
+ * @param {string} message
424
+ */
425
+ function _sendError(stream, code, message)
426
+ {
427
+ try
428
+ {
429
+ stream.respond({
430
+ ':status': 200,
431
+ 'content-type': 'application/grpc+proto',
432
+ 'grpc-status': String(code),
433
+ 'grpc-message': encodeURIComponent(message),
434
+ }, { endStream: true });
435
+ }
436
+ catch (_)
437
+ {
438
+ try { stream.close(); }
439
+ catch (__) { /* stream already closed */ }
440
+ }
441
+ }
442
+
443
+ module.exports = {
444
+ GrpcServiceRegistry,
445
+ };