@steve31415/baselib 4.0.1 → 5.0.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.
@@ -1,4 +1,6 @@
1
- // A cap on WebSocket age, applied by wrapping a Hono `upgradeWebSocket`.
1
+ // A cap on WebSocket age, applied by wrapping a Hono `upgradeWebSocket`, and
2
+ // (since 5.0.0) a cap on the size of one incoming message, applied to the
3
+ // app's ws server.
2
4
  //
3
5
  // An open socket is an open request, and Cloud Run keeps a retired instance
4
6
  // alive (and counted against --max-instances) until its last request ends.
@@ -8,6 +10,7 @@
8
10
  // laptop, an old client) at 300 s of age, in code rather than through the
9
11
  // service's --timeout, which must stay long enough for long background
10
12
  // routes (PW docs/OPERATIONS.md, "Socket lifetime").
13
+ import { truncate } from './log-core.js';
11
14
  /** Sockets older than this are closed by the server. */
12
15
  export const SOCKET_MAX_AGE_MS = 300_000;
13
16
  /** After the close frame, how long the client gets to answer it before the
@@ -100,3 +103,29 @@ function withAgeCap(events, path, maxAgeMs, graceMs, logger) {
100
103
  },
101
104
  };
102
105
  }
106
+ // ---- message size ----
107
+ /** The default cap on one incoming WebSocket message: 1 MiB. ws's own
108
+ * default is 100 MiB, which lets one client make the server buffer that
109
+ * much per frame. */
110
+ export const SOCKET_MAX_PAYLOAD = 1024 * 1024;
111
+ /** Caps the size of one incoming message on every socket of the app's ws
112
+ * server, the way the hub caps its own: a client that sends more has its
113
+ * socket closed with 1009 (message too big). Takes effect for sockets
114
+ * opened after the call (ws reads the option at each upgrade). */
115
+ export function capSocketMessageSize(wss, options = {}) {
116
+ const maxBytes = options.maxBytes ?? SOCKET_MAX_PAYLOAD;
117
+ // ws reads 0 as "no limit": a computed 0 must not remove the cap.
118
+ if (!Number.isSafeInteger(maxBytes) || maxBytes < 1)
119
+ throw new Error(`capSocketMessageSize: maxBytes must be a positive whole number, got ${maxBytes}`);
120
+ wss.options.maxPayload = maxBytes;
121
+ const logger = options.logger;
122
+ if (!logger || !wss.on)
123
+ return;
124
+ wss.on('connection', (ws, request) => {
125
+ ws.on('error', (error) => {
126
+ if (error.code !== 'WS_ERR_UNSUPPORTED_MESSAGE_LENGTH')
127
+ return;
128
+ logger.warn('socket message too large', { path: truncate(new URL(request.url ?? '/', 'http://x').pathname, 200), maxBytes });
129
+ });
130
+ });
131
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@steve31415/baselib",
3
- "version": "4.0.1",
3
+ "version": "5.0.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",