@dokebi/websocket 0.1.1 → 0.1.3

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/AI.md CHANGED
@@ -1,4 +1,4 @@
1
- # AI 작업 안내 — @dokebi/websocket 0.1.1
1
+ # AI 작업 안내 — @dokebi/websocket 0.1.3
2
2
 
3
3
  dokebi-web(Fastify) 위에 WebSocket 라우트를 열고 @dokebi/websocket-hub로 서버·워커 사이 방송·접속자 목록을 잇는다. 방송은 **모든 수신자의 ACK**를 받아야 끝난다(D-0025). 뷰·DB를 강제하지 않는다. Node >=24, core ^0.2.0, peer: @dokebi/web·@dokebi/websocket-hub. 세부는 [README](README.md)다.
4
4
 
@@ -17,7 +17,7 @@ npx dokebi add @dokebi/websocket
17
17
  | `maxPayload` | 65536 | 메시지 크기 |
18
18
  | `maxConnections` | 128 | 이 프로세스의 연결 수 |
19
19
  | `maxPendingMessages` | 32 | 연결별 대기 메시지(연결마다 순차 처리) |
20
- | `maxBufferedBytes` | 1048576 | 송신 버퍼. 넘으면 그 소켓을 1013 `Slow consumer`로 닫음 |
20
+ | `maxBufferedBytes` | 1048576 | 송신 버퍼. 넘으면 그 소켓을 1013 `Slow consumer`로 닫고 warn `WebSocket slow consumer closed`(code `SLOW_CONSUMER`)를 남김. 그 소켓에 쓰려던 응답은 버려진다(핸들러 실패로 세지 않음) |
21
21
  | `closeTimeoutMs` | 2000 | 소켓 종료 대기(업무 제한 아님) |
22
22
  | `heartbeatMs` | 10000 | ping/pong |
23
23
  | `idleTimeoutMs` | 0(끔) | 클라이언트가 이 시간 동안 아무것도 안 보내면 4408 `Idle`. 페이지가 `{"$dokebi":"ping-v1"}`을 주기적으로 보낸다 |
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @dokebi/websocket
2
2
 
3
- Node >=24, 0.1.1(npm 공개 패키지). Dokebi Fastify 웹과 websocket-hub를 연결하는 선택 플러그인. API 서버에 템플릿/뷰를 강제하지 않는다.
3
+ Node >=24, 0.1.3. Dokebi Fastify 웹과 websocket-hub를 연결하는 선택 플러그인. API 서버에 템플릿/뷰를 강제하지 않는다.
4
4
 
5
5
  ```ts
6
6
  import { Application, dependencyOf, loadConfig } from '@dokebi/core';
package/dist/gateway.js CHANGED
@@ -157,19 +157,29 @@ export class Gateway {
157
157
  #send(socket, data) {
158
158
  const bytes = typeof data === 'string' ? Buffer.byteLength(data) : data.byteLength;
159
159
  if (socket.readyState !== WebSocket.OPEN)
160
- return Promise.reject(new Error('Socket closed'));
160
+ return Promise.reject(Object.assign(new Error('Socket closed'), { code: 'SOCKET_CLOSED' }));
161
161
  if (socket.bufferedAmount + bytes > this.settings.maxBufferedBytes) {
162
- socket.close(1013, 'Slow consumer');
163
- return Promise.reject(new Error('Socket backpressure'));
162
+ this.#slow(socket);
163
+ return Promise.reject(Object.assign(new Error('Socket backpressure'), { code: 'SLOW_CONSUMER' }));
164
164
  }
165
165
  return this.#watch(new Promise((resolve, reject) => socket.send(data, (error) => (error ? reject(error) : resolve()))));
166
166
  }
167
+ /** Closes a socket whose send buffer passed maxBufferedBytes (1013), logging it once. */
168
+ #slow(socket) {
169
+ if (socket.readyState !== WebSocket.OPEN)
170
+ return;
171
+ this.context.logger.log('warn', 'WebSocket slow consumer closed', {
172
+ code: 'SLOW_CONSUMER',
173
+ bufferedBytes: socket.bufferedAmount,
174
+ });
175
+ socket.close(1013, 'Slow consumer');
176
+ }
167
177
  /** Queues one broadcast frame without waiting; a slow socket never delays the others. */
168
178
  #deliver(socket, frame) {
169
179
  if (socket.readyState !== WebSocket.OPEN)
170
180
  return;
171
181
  if (socket.bufferedAmount + frame.byteLength > this.settings.maxBufferedBytes) {
172
- socket.close(1013, 'Slow consumer');
182
+ this.#slow(socket);
173
183
  return;
174
184
  }
175
185
  this.#sends++;
@@ -336,10 +346,16 @@ export class Gateway {
336
346
  return route.message(data, session);
337
347
  });
338
348
  value.tail = work
339
- .then(() => { }, () => {
349
+ .then(() => { }, (error) => {
340
350
  value.failed = true;
351
+ // A reply that cannot be written because the socket is closing (slow consumer, the
352
+ // client left) is not a handler failure; it was logged where the socket closed.
353
+ const code = error?.code;
354
+ if (code === 'SLOW_CONSUMER' || code === 'SOCKET_CLOSED')
355
+ return;
341
356
  this.context.logger.log('error', 'WebSocket message handler failed', {
342
357
  connection: id,
358
+ ...(typeof code === 'string' && /^[A-Z][A-Z0-9_]{0,63}$/.test(code) ? { code } : {}),
343
359
  });
344
360
  socket.close(1011, 'Message failed');
345
361
  })
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@ export * from './router.js';
5
5
  export * from './types.js';
6
6
  const hub = providerDependency({
7
7
  name: 'dokebi-websocket-hub',
8
- version: '^0.1.0',
8
+ version: '^0.1.0 || ^0.2.0',
9
9
  packageName: '@dokebi/websocket-hub',
10
10
  load: () => import('@dokebi/websocket-hub'),
11
11
  });
@@ -35,7 +35,7 @@ class WebsocketRuntime {
35
35
  /** Factory input: `{ app, options? }`. Options default to `app.config['dokebi-websocket']`. */
36
36
  export const createPlugin = definePlugin({
37
37
  name: 'dokebi-websocket',
38
- version: '0.1.1',
38
+ version: '0.1.3',
39
39
  core: '^0.2.0',
40
40
  options: parseOptions,
41
41
  dependencies: () => [hub, web],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dokebi/websocket",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -38,11 +38,11 @@
38
38
  "peerDependencies": {
39
39
  "@dokebi/core": "^0.2.0",
40
40
  "@dokebi/web": "^0.1.0",
41
- "@dokebi/websocket-hub": "^0.1.0"
41
+ "@dokebi/websocket-hub": "^0.1.0 || ^0.2.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@dokebi/web": "0.1.1",
45
- "@dokebi/websocket-hub": "0.1.1"
45
+ "@dokebi/websocket-hub": "0.2.0"
46
46
  },
47
47
  "dokebi": {
48
48
  "schemaVersion": 2,
@@ -51,7 +51,7 @@
51
51
  "dependencies": [
52
52
  {
53
53
  "name": "dokebi-websocket-hub",
54
- "version": "^0.1.0"
54
+ "version": "^0.1.0 || ^0.2.0"
55
55
  },
56
56
  {
57
57
  "name": "dokebi-web",