@ng-annotate/vite-plugin 0.2.3 → 0.2.5

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 (2) hide show
  1. package/dist/ws-handler.js +10 -5
  2. package/package.json +1 -1
@@ -2,6 +2,11 @@ import { WebSocketServer, WebSocket } from 'ws';
2
2
  import { store } from './store.js';
3
3
  const WS_PATH = '/__annotate';
4
4
  const SYNC_INTERVAL_MS = 2000;
5
+ function safeSend(ws, data) {
6
+ if (ws.readyState !== WebSocket.OPEN)
7
+ return;
8
+ ws.send(JSON.stringify(data), (err) => { if (err) { /* connection closed mid-send */ } });
9
+ }
5
10
  export function createWsHandler(server) {
6
11
  const wss = new WebSocketServer({ noServer: true });
7
12
  const sessionSockets = new Map();
@@ -19,7 +24,7 @@ export function createWsHandler(server) {
19
24
  const referer = req.headers.referer ?? req.headers.origin ?? '';
20
25
  const session = await store.createSession({ active: true, url: referer });
21
26
  const sessionId = session.id;
22
- ws.send(JSON.stringify({ type: 'session:created', session }));
27
+ safeSend(ws, { type: 'session:created', session });
23
28
  sessionSockets.set(sessionId, ws);
24
29
  ws.on('message', (raw) => {
25
30
  void (async () => {
@@ -35,18 +40,18 @@ export function createWsHandler(server) {
35
40
  ...msg.payload,
36
41
  sessionId,
37
42
  });
38
- ws.send(JSON.stringify({ type: 'annotation:created', annotation }));
43
+ safeSend(ws, { type: 'annotation:created', annotation });
39
44
  }
40
45
  else if (msg.type === 'annotation:reply') {
41
46
  const annotation = await store.addReply(msg.id, { author: 'user', message: msg.message });
42
47
  if (annotation) {
43
- ws.send(JSON.stringify({ type: 'annotation:updated', annotation }));
48
+ safeSend(ws, { type: 'annotation:updated', annotation });
44
49
  }
45
50
  }
46
51
  else {
47
52
  const annotation = await store.updateAnnotation(msg.id, { status: 'dismissed' });
48
53
  if (annotation) {
49
- ws.send(JSON.stringify({ type: 'annotation:updated', annotation }));
54
+ safeSend(ws, { type: 'annotation:updated', annotation });
50
55
  }
51
56
  }
52
57
  })();
@@ -64,7 +69,7 @@ export function createWsHandler(server) {
64
69
  if (ws.readyState !== WebSocket.OPEN)
65
70
  continue;
66
71
  const annotations = await store.listAnnotations(sessionId);
67
- ws.send(JSON.stringify({ type: 'annotations:sync', annotations }));
72
+ safeSend(ws, { type: 'annotations:sync', annotations });
68
73
  }
69
74
  })();
70
75
  }, SYNC_INTERVAL_MS);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ng-annotate/vite-plugin",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",