@steve31415/baselib 3.64.1 → 3.66.0

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/dist/http.d.ts CHANGED
@@ -1,10 +1,17 @@
1
1
  import type { Hono, Context } from 'hono';
2
2
  import { serve } from '@hono/node-server';
3
3
  import type { Logger } from './log-core.js';
4
+ export interface RequestLoggingOptions {
5
+ /** Extra fields for the exit line, read after the handler (e.g. the hub's
6
+ * `?app=`, so one service's sockets can be told apart by app); fields
7
+ * whose value is undefined are left out. */
8
+ meta?: (c: Context) => Record<string, unknown>;
9
+ }
4
10
  /** One INFO line per request on exit (method, path, status, elapsed,
5
- * identity); /health excluded. Entry-point detail beyond this (key
6
- * parameters, outcomes) is the handler's job per OPERATIONS.md. */
7
- export declare function requestLogging(logger: Logger): import("hono").MiddlewareHandler<any, string, {}, Response>;
11
+ * identity, plus `options.meta`); /health excluded. Entry-point detail
12
+ * beyond this (key parameters, outcomes) is the handler's job per
13
+ * OPERATIONS.md. */
14
+ export declare function requestLogging(logger: Logger, options?: RequestLoggingOptions): import("hono").MiddlewareHandler<any, string, {}, Response>;
8
15
  /** The address of the client that connected to Google's edge, read from
9
16
  * X-Forwarded-For — for per-source rate limits, never for authorization.
10
17
  * Google appends to whatever the client sent, so only the tail can be
@@ -25,10 +32,16 @@ export interface StartServerOptions {
25
32
  app: Hono;
26
33
  logger: Logger;
27
34
  port?: number;
35
+ /** Work to finish on SIGTERM before the process exits — work that is not
36
+ * an HTTP request, which closing the server does not wait for (radar's
37
+ * timer-driven held sends, 2026-11-16). Awaited after the server stops
38
+ * accepting; still bounded by the 5 s exit, inside Cloud Run's 10 s
39
+ * grace. A rejection is logged and the exit goes on. */
40
+ beforeExit?: () => Promise<unknown>;
28
41
  }
29
42
  /** Standard entry point: safety check, crash logging (logUncaughtErrors),
30
43
  * serve, graceful SIGTERM shutdown
31
- * (stop accepting, then flush all loggers, then exit). Returns the Node
44
+ * (stop accepting, then `beforeExit`, then flush all loggers, then exit). Returns the Node
32
45
  * server so callers can attach protocol upgrades (e.g. @hono/node-ws's
33
46
  * injectWebSocket). */
34
47
  export declare function startServer(opts: StartServerOptions): ReturnType<typeof serve>;
package/dist/http.js CHANGED
@@ -7,21 +7,24 @@ import { assertBootSafety } from './config.js';
7
7
  import { flushAllLoggers, logUncaughtErrors } from './log.js';
8
8
  import { truncate } from './log-core.js';
9
9
  /** One INFO line per request on exit (method, path, status, elapsed,
10
- * identity); /health excluded. Entry-point detail beyond this (key
11
- * parameters, outcomes) is the handler's job per OPERATIONS.md. */
12
- export function requestLogging(logger) {
10
+ * identity, plus `options.meta`); /health excluded. Entry-point detail
11
+ * beyond this (key parameters, outcomes) is the handler's job per
12
+ * OPERATIONS.md. */
13
+ export function requestLogging(logger, options = {}) {
13
14
  return createMiddleware(async (c, next) => {
14
15
  if (c.req.path === '/health')
15
16
  return next();
16
17
  const started = Date.now();
17
18
  await next();
18
19
  const identity = c.var.identity;
20
+ const extra = Object.entries(options.meta?.(c) ?? {}).filter(([, value]) => value !== undefined);
19
21
  logger.info(`${c.req.method} ${c.req.path} ${c.res.status}`, {
20
22
  method: c.req.method,
21
23
  path: truncate(c.req.path, 200),
22
24
  status: c.res.status,
23
25
  elapsed_ms: Date.now() - started,
24
26
  ...(identity ? { identity: identity.email } : {}),
27
+ ...Object.fromEntries(extra),
25
28
  });
26
29
  });
27
30
  }
@@ -65,7 +68,7 @@ export function errorHandler(logger) {
65
68
  }
66
69
  /** Standard entry point: safety check, crash logging (logUncaughtErrors),
67
70
  * serve, graceful SIGTERM shutdown
68
- * (stop accepting, then flush all loggers, then exit). Returns the Node
71
+ * (stop accepting, then `beforeExit`, then flush all loggers, then exit). Returns the Node
69
72
  * server so callers can attach protocol upgrades (e.g. @hono/node-ws's
70
73
  * injectWebSocket). */
71
74
  export function startServer(opts) {
@@ -80,11 +83,14 @@ export function startServer(opts) {
80
83
  });
81
84
  process.once('SIGTERM', () => {
82
85
  opts.logger.info('SIGTERM received, shutting down');
86
+ const drained = opts.beforeExit
87
+ ? opts.beforeExit().catch((error) => opts.logger.error('beforeExit failed', { error: error instanceof Error ? error.message : String(error) }))
88
+ : Promise.resolve();
83
89
  // final: last-chance delivery + drop markers, time-boxed. The two racing
84
90
  // callers below join the same single-flight finalFlush, so whichever
85
91
  // fires second waits for the real work instead of exiting under it.
86
92
  server.close(() => {
87
- void flushAllLoggers({ final: true }).finally(() => process.exit(0));
93
+ void drained.finally(() => flushAllLoggers({ final: true }).finally(() => process.exit(0)));
88
94
  });
89
95
  // Cloud Run's grace period is 10s; don't rely on every socket closing.
90
96
  setTimeout(() => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steve31415/baselib",
3
- "version": "3.64.1",
3
+ "version": "3.66.0",
4
4
  "description": "Plasticine new-world shared platform library: logging, auth, service-to-service auth, db, HTTP, sync, app updates",
5
5
  "type": "module",
6
6
  "license": "MIT",