@webiny/api-websockets-standalone 0.0.0-unstable.7c2def32a7 → 0.0.0-unstable.9609e4e0c9

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.
@@ -5,10 +5,15 @@ import { WebSocket } from "ws";
5
5
  import { WebsocketsServerAdapter } from "./abstractions.js";
6
6
  declare class NodeWsAdapterImpl implements WebsocketsServerAdapter.Interface<WebSocket> {
7
7
  private wss;
8
+ private pingTimer;
9
+ private readonly answered;
10
+ private pingInterval;
11
+ static withPingInterval(pingInterval: number): NodeWsAdapterImpl;
8
12
  private getWss;
9
13
  start(_server: HttpServer): void;
10
14
  handleUpgrade(request: IncomingMessage, socket: Duplex, head: Buffer): void;
11
15
  stop(): Promise<void>;
16
+ private pingAll;
12
17
  onConnection(cb: (socket: WebSocket, request: import("node:http").IncomingMessage) => void): void;
13
18
  onMessage(socket: WebSocket, cb: (data: Buffer) => void): void;
14
19
  onClose(socket: WebSocket, cb: (code: number, reason: Buffer) => void): void;
@@ -3,6 +3,7 @@ import { WebsocketsServerAdapter } from "./abstractions.js";
3
3
  const MAX_PAYLOAD_BYTES = 65536;
4
4
  const SERVICE_RESTART = 1012;
5
5
  const CLOSE_WAIT_MS = 1000;
6
+ const PING_INTERVAL_MS = 30000;
6
7
  const closeForRestart = (client)=>new Promise((resolve)=>{
7
8
  if (client.readyState === WebSocket.CLOSED) return void resolve();
8
9
  const timer = setTimeout(()=>{
@@ -15,15 +16,31 @@ const closeForRestart = (client)=>new Promise((resolve)=>{
15
16
  client.close(SERVICE_RESTART, "Server restarting");
16
17
  });
17
18
  class NodeWsAdapterImpl {
19
+ static withPingInterval(pingInterval) {
20
+ const adapter = new NodeWsAdapterImpl();
21
+ adapter.pingInterval = pingInterval;
22
+ return adapter;
23
+ }
18
24
  getWss() {
19
25
  if (!this.wss) throw new Error("WebSocketServer has not been started. Call start() first.");
20
26
  return this.wss;
21
27
  }
22
28
  start(_server) {
23
- this.wss = new WebSocketServer({
29
+ if (this.wss) return;
30
+ const wss = new WebSocketServer({
24
31
  noServer: true,
25
32
  maxPayload: MAX_PAYLOAD_BYTES
26
33
  });
34
+ wss.on("connection", (socket)=>{
35
+ this.answered.add(socket);
36
+ socket.on("pong", ()=>{
37
+ this.answered.add(socket);
38
+ });
39
+ });
40
+ this.wss = wss;
41
+ this.pingTimer = setInterval(()=>{
42
+ this.pingAll(wss);
43
+ }, this.pingInterval);
27
44
  }
28
45
  handleUpgrade(request, socket, head) {
29
46
  this.getWss().handleUpgrade(request, socket, head, (ws)=>{
@@ -34,6 +51,7 @@ class NodeWsAdapterImpl {
34
51
  const wss = this.wss;
35
52
  if (!wss) return;
36
53
  this.wss = void 0;
54
+ clearInterval(this.pingTimer);
37
55
  const clients = [
38
56
  ...wss.clients
39
57
  ];
@@ -46,6 +64,16 @@ class NodeWsAdapterImpl {
46
64
  });
47
65
  });
48
66
  }
67
+ pingAll(wss) {
68
+ for (const socket of wss.clients){
69
+ if (!this.answered.has(socket)) {
70
+ socket.terminate();
71
+ continue;
72
+ }
73
+ this.answered.delete(socket);
74
+ if (socket.readyState === WebSocket.OPEN) socket.ping();
75
+ }
76
+ }
49
77
  onConnection(cb) {
50
78
  this.getWss().on("connection", cb);
51
79
  }
@@ -69,6 +97,10 @@ class NodeWsAdapterImpl {
69
97
  close(socket, code, reason) {
70
98
  socket.close(code, reason);
71
99
  }
100
+ constructor(){
101
+ this.answered = new WeakSet();
102
+ this.pingInterval = PING_INTERVAL_MS;
103
+ }
72
104
  }
73
105
  const NodeWsAdapter = WebsocketsServerAdapter.createImplementation({
74
106
  implementation: NodeWsAdapterImpl,
@@ -1 +1 @@
1
- {"version":3,"file":"adapter/NodeWsAdapter.js","sources":["../../src/adapter/NodeWsAdapter.ts"],"sourcesContent":["import type { Server as HttpServer } from \"node:http\";\nimport type { IncomingMessage } from \"node:http\";\nimport type { Duplex } from \"node:stream\";\nimport { WebSocketServer } from \"ws\";\nimport { WebSocket } from \"ws\";\nimport { WebsocketsServerAdapter } from \"./abstractions.js\";\n\n/*\n * Clients only send small JSON envelopes (a ping carrying a token). The `ws` default is 100 MiB,\n * which any client, authenticated or not, could make the server buffer per message.\n */\nconst MAX_PAYLOAD_BYTES = 64 * 1024;\n\n/*\n * Sent to every client when the server stops. 1012 means \"service restart\": the client reconnects,\n * and behind a load balancer it reaches a process that is still up.\n */\nconst SERVICE_RESTART = 1012;\n\n// How long a client gets to answer the close before its socket is cut.\nconst CLOSE_WAIT_MS = 1000;\n\n/*\n * Closes the socket with `SERVICE_RESTART` and resolves once it is closed. A client that has not\n * answered within `CLOSE_WAIT_MS` (a frozen tab, a dead network) is cut.\n */\nconst closeForRestart = (client: WebSocket): Promise<void> => {\n return new Promise(resolve => {\n if (client.readyState === WebSocket.CLOSED) {\n resolve();\n return;\n }\n const timer = setTimeout(() => {\n client.terminate();\n }, CLOSE_WAIT_MS);\n client.once(\"close\", () => {\n clearTimeout(timer);\n resolve();\n });\n client.close(SERVICE_RESTART, \"Server restarting\");\n });\n};\n\nclass NodeWsAdapterImpl implements WebsocketsServerAdapter.Interface<WebSocket> {\n private wss: WebSocketServer | undefined;\n\n private getWss(): WebSocketServer {\n if (!this.wss) {\n throw new Error(\"WebSocketServer has not been started. Call start() first.\");\n }\n return this.wss;\n }\n\n /* The server param is kept for interface compliance but not used —\n the HTTP upgrade event is handled externally via handleUpgrade(). */\n public start(_server: HttpServer): void {\n this.wss = new WebSocketServer({ noServer: true, maxPayload: MAX_PAYLOAD_BYTES });\n }\n\n public handleUpgrade(request: IncomingMessage, socket: Duplex, head: Buffer): void {\n this.getWss().handleUpgrade(request, socket, head, ws => {\n this.getWss().emit(\"connection\", ws, request);\n });\n }\n\n // Safe to call again: a stopped adapter has nothing left to close.\n public async stop(): Promise<void> {\n const wss = this.wss;\n if (!wss) {\n return;\n }\n this.wss = undefined;\n\n const clients = [...wss.clients];\n const closing = clients.map(closeForRestart);\n await Promise.all(closing);\n\n await new Promise<void>((resolve, reject) => {\n wss.close(err => {\n if (err) {\n reject(err);\n return;\n }\n resolve();\n });\n });\n }\n\n public onConnection(\n cb: (socket: WebSocket, request: import(\"node:http\").IncomingMessage) => void\n ): void {\n this.getWss().on(\"connection\", cb);\n }\n\n public onMessage(socket: WebSocket, cb: (data: Buffer) => void): void {\n socket.on(\"message\", cb);\n }\n\n public onClose(socket: WebSocket, cb: (code: number, reason: Buffer) => void): void {\n socket.on(\"close\", cb);\n }\n\n public onError(socket: WebSocket, cb: (error: Error) => void): void {\n socket.on(\"error\", cb);\n }\n\n public send(socket: WebSocket, data: string): Promise<void> {\n return new Promise((resolve, reject) => {\n socket.send(data, err => {\n if (err) {\n reject(err);\n return;\n }\n resolve();\n });\n });\n }\n\n public close(socket: WebSocket, code?: number, reason?: string): void {\n socket.close(code, reason);\n }\n}\n\nexport { NodeWsAdapterImpl };\n\nexport const NodeWsAdapter = WebsocketsServerAdapter.createImplementation({\n implementation: NodeWsAdapterImpl,\n dependencies: []\n});\n"],"names":["MAX_PAYLOAD_BYTES","SERVICE_RESTART","CLOSE_WAIT_MS","closeForRestart","client","Promise","resolve","WebSocket","timer","setTimeout","clearTimeout","NodeWsAdapterImpl","Error","_server","WebSocketServer","request","socket","head","ws","wss","undefined","clients","closing","reject","err","cb","data","code","reason","NodeWsAdapter","WebsocketsServerAdapter"],"mappings":";;AAWA,MAAMA,oBAAoB;AAM1B,MAAMC,kBAAkB;AAGxB,MAAMC,gBAAgB;AAMtB,MAAMC,kBAAkB,CAACC,SACd,IAAIC,QAAQC,CAAAA;QACf,IAAIF,OAAO,UAAU,KAAKG,UAAU,MAAM,EAAE,YACxCD;QAGJ,MAAME,QAAQC,WAAW;YACrBL,OAAO,SAAS;QACpB,GAAGF;QACHE,OAAO,IAAI,CAAC,SAAS;YACjBM,aAAaF;YACbF;QACJ;QACAF,OAAO,KAAK,CAACH,iBAAiB;IAClC;AAGJ,MAAMU;IAGM,SAA0B;QAC9B,IAAI,CAAC,IAAI,CAAC,GAAG,EACT,MA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AMU,MAAiB,EAAEW,IAAa,EAAEC,MAAe,EAAQ;QAClEZ,OAAO,KAAK,CAACW,MAAMC;IACvB;AACJ;AAIO,MAAMC,gBAAgBC,wBAAwB,oBAAoB,CAAC;IACtE,gBAAgBnB;IAChB,cAAc,EAAE;AACpB"}
1
+ {"version":3,"file":"adapter/NodeWsAdapter.js","sources":["../../src/adapter/NodeWsAdapter.ts"],"sourcesContent":["import type { Server as HttpServer } from \"node:http\";\nimport type { IncomingMessage } from \"node:http\";\nimport type { Duplex } from \"node:stream\";\nimport { WebSocketServer } from \"ws\";\nimport { WebSocket } from \"ws\";\nimport { WebsocketsServerAdapter } from \"./abstractions.js\";\n\n/*\n * Clients only send small JSON envelopes (a ping carrying a token). The `ws` default is 100 MiB,\n * which any client, authenticated or not, could make the server buffer per message.\n */\nconst MAX_PAYLOAD_BYTES = 64 * 1024;\n\n/*\n * Sent to every client when the server stops. 1012 means \"service restart\": the client reconnects,\n * and behind a load balancer it reaches a process that is still up.\n */\nconst SERVICE_RESTART = 1012;\n\n// How long a client gets to answer the close before its socket is cut.\nconst CLOSE_WAIT_MS = 1000;\n\n/*\n * How often every open socket gets a WebSocket ping frame. Proxies and load balancers close a\n * connection that carries nothing for a while (60 seconds by default on nginx and AWS ALB), and the\n * admin's own ping comes only every 5 minutes. Browsers answer ping frames by themselves; no client\n * code is involved, and nothing is written to the registry.\n */\nconst PING_INTERVAL_MS = 30_000;\n\n/*\n * Closes the socket with `SERVICE_RESTART` and resolves once it is closed. A client that has not\n * answered within `CLOSE_WAIT_MS` (a frozen tab, a dead network) is cut.\n */\nconst closeForRestart = (client: WebSocket): Promise<void> => {\n return new Promise(resolve => {\n if (client.readyState === WebSocket.CLOSED) {\n resolve();\n return;\n }\n const timer = setTimeout(() => {\n client.terminate();\n }, CLOSE_WAIT_MS);\n client.once(\"close\", () => {\n clearTimeout(timer);\n resolve();\n });\n client.close(SERVICE_RESTART, \"Server restarting\");\n });\n};\n\nclass NodeWsAdapterImpl implements WebsocketsServerAdapter.Interface<WebSocket> {\n private wss: WebSocketServer | undefined;\n private pingTimer: ReturnType<typeof setInterval> | undefined;\n // Sockets that answered the last ping, or connected since it was sent.\n private readonly answered = new WeakSet<WebSocket>();\n\n private pingInterval = PING_INTERVAL_MS;\n\n /*\n * An adapter that pings more often, for tests. The constructor takes nothing: DI builds this class\n * with no dependencies.\n */\n public static withPingInterval(pingInterval: number): NodeWsAdapterImpl {\n const adapter = new NodeWsAdapterImpl();\n adapter.pingInterval = pingInterval;\n return adapter;\n }\n\n private getWss(): WebSocketServer {\n if (!this.wss) {\n throw new Error(\"WebSocketServer has not been started. Call start() first.\");\n }\n return this.wss;\n }\n\n /* The server param is kept for interface compliance but not used —\n the HTTP upgrade event is handled externally via handleUpgrade(). */\n public start(_server: HttpServer): void {\n // Already started: a second server and timer would be left running, unreachable by stop().\n if (this.wss) {\n return;\n }\n const wss = new WebSocketServer({ noServer: true, maxPayload: MAX_PAYLOAD_BYTES });\n wss.on(\"connection\", socket => {\n this.answered.add(socket);\n socket.on(\"pong\", () => {\n this.answered.add(socket);\n });\n });\n this.wss = wss;\n this.pingTimer = setInterval(() => {\n this.pingAll(wss);\n }, this.pingInterval);\n }\n\n public handleUpgrade(request: IncomingMessage, socket: Duplex, head: Buffer): void {\n this.getWss().handleUpgrade(request, socket, head, ws => {\n this.getWss().emit(\"connection\", ws, request);\n });\n }\n\n // Safe to call again: a stopped adapter has nothing left to close.\n public async stop(): Promise<void> {\n const wss = this.wss;\n if (!wss) {\n return;\n }\n this.wss = undefined;\n clearInterval(this.pingTimer);\n\n const clients = [...wss.clients];\n const closing = clients.map(closeForRestart);\n await Promise.all(closing);\n\n await new Promise<void>((resolve, reject) => {\n wss.close(err => {\n if (err) {\n reject(err);\n return;\n }\n resolve();\n });\n });\n }\n\n /*\n * Pings every open socket. One that has not answered the previous ping within a whole interval is\n * gone without having closed (a laptop asleep, a dropped network), so it is cut; its close\n * handler then removes the connection.\n */\n private pingAll(wss: WebSocketServer): void {\n for (const socket of wss.clients) {\n if (!this.answered.has(socket)) {\n socket.terminate();\n continue;\n }\n this.answered.delete(socket);\n if (socket.readyState === WebSocket.OPEN) {\n socket.ping();\n }\n }\n }\n\n public onConnection(\n cb: (socket: WebSocket, request: import(\"node:http\").IncomingMessage) => void\n ): void {\n this.getWss().on(\"connection\", cb);\n }\n\n public onMessage(socket: WebSocket, cb: (data: Buffer) => void): void {\n socket.on(\"message\", cb);\n }\n\n public onClose(socket: WebSocket, cb: (code: number, reason: Buffer) => void): void {\n socket.on(\"close\", cb);\n }\n\n public onError(socket: WebSocket, cb: (error: Error) => void): void {\n socket.on(\"error\", cb);\n }\n\n public send(socket: WebSocket, data: string): Promise<void> {\n return new Promise((resolve, reject) => {\n socket.send(data, err => {\n if (err) {\n reject(err);\n return;\n }\n resolve();\n });\n });\n }\n\n public close(socket: WebSocket, code?: number, reason?: string): void {\n socket.close(code, reason);\n }\n}\n\nexport { NodeWsAdapterImpl };\n\nexport const NodeWsAdapter = WebsocketsServerAdapter.createImplementation({\n implementation: NodeWsAdapterImpl,\n dependencies: []\n});\n"],"names":["MAX_PAYLOAD_BYTES","SERVICE_RESTART","CLOSE_WAIT_MS","PING_INTERVAL_MS","closeForRestart","client","Promise","resolve","WebSocket","timer","setTimeout","clearTimeout","NodeWsAdapterImpl","pingInterval","adapter","Error","_server","wss","WebSocketServer","socket","setInterval","request","head","ws","undefined","clearInterval","clients","closing","reject","err","cb","data","code","reason","WeakSet","NodeWsAdapter","WebsocketsServerAdapter"],"mappings":";;AAWA,MAAMA,oBAAoB;AAM1B,MAAMC,kBAAkB;AAGxB,MAAMC,gBAAgB;AAQtB,MAAMC,mBAAmB;AAMzB,MAAMC,kBAAkB,CAACC,SACd,IAAIC,QAAQC,CAAAA;QACf,IAAIF,OAAO,UAAU,KAAKG,UAAU,MAAM,EAAE,YACxCD;QAGJ,MAAME,QAAQC,WAAW;YACrBL,OAAO,SAAS;QACpB,GAAGH;QACHG,OAAO,IAAI,CAAC,S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AACC,SAASqB;YAC9BX,IAAI,KAAK,CAACY,CAAAA;gBACN,IAAIA,KAAK,YACLD,OAAOC;gBAGXtB;YACJ;QACJ;IACJ;IAOQ,QAAQU,GAAoB,EAAQ;QACxC,KAAK,MAAME,UAAUF,IAAI,OAAO,CAAE;YAC9B,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAACE,SAAS;gBAC5BA,OAAO,SAAS;gBAChB;YACJ;YACA,IAAI,CAAC,QAAQ,CAAC,MAAM,CAACA;YACrB,IAAIA,OAAO,UAAU,KAAKX,UAAU,IAAI,EACpCW,OAAO,IAAI;QAEnB;IACJ;IAEO,aACHW,EAA6E,EACzE;QACJ,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC,cAAcA;IACnC;IAEO,UAAUX,MAAiB,EAAEW,EAA0B,EAAQ;QAClEX,OAAO,EAAE,CAAC,WAAWW;IACzB;IAEO,QAAQX,MAAiB,EAAEW,EAA0C,EAAQ;QAChFX,OAAO,EAAE,CAAC,SAASW;IACvB;IAEO,QAAQX,MAAiB,EAAEW,EAA0B,EAAQ;QAChEX,OAAO,EAAE,CAAC,SAASW;IACvB;IAEO,KAAKX,MAAiB,EAAEY,IAAY,EAAiB;QACxD,OAAO,IAAIzB,QAAQ,CAACC,SAASqB;YACzBT,OAAO,IAAI,CAACY,MAAMF,CAAAA;gBACd,IAAIA,KAAK,YACLD,OAAOC;gBAGXtB;YACJ;QACJ;IACJ;IAEO,MAAMY,MAAiB,EAAEa,IAAa,EAAEC,MAAe,EAAQ;QAClEd,OAAO,KAAK,CAACa,MAAMC;IACvB;;aAzHiB,QAAQ,GAAG,IAAIC;aAExB,YAAY,GAAG/B;;AAwH3B;AAIO,MAAMgC,gBAAgBC,wBAAwB,oBAAoB,CAAC;IACtE,gBAAgBxB;IAChB,cAAc,EAAE;AACpB"}
@@ -0,0 +1,11 @@
1
+ import { WebsocketsMessageBus } from "./abstractions.js";
2
+ declare class InProcessMessageBusImpl implements WebsocketsMessageBus.Interface {
3
+ private readonly handlers;
4
+ publish(owner: string, message: WebsocketsMessageBus.Message): Promise<void>;
5
+ subscribe(owner: string, handler: WebsocketsMessageBus.Handler): Promise<void>;
6
+ close(): Promise<void>;
7
+ }
8
+ export { InProcessMessageBusImpl };
9
+ export declare const InProcessMessageBus: typeof InProcessMessageBusImpl & {
10
+ __abstraction: import("@webiny/di").Abstraction<import("./abstractions.js").IWebsocketsMessageBus>;
11
+ };
@@ -0,0 +1,24 @@
1
+ import { WebsocketsMessageBus } from "./abstractions.js";
2
+ class InProcessMessageBusImpl {
3
+ async publish(owner, message) {
4
+ const handler = this.handlers.get(owner);
5
+ if (!handler) return;
6
+ await handler(message);
7
+ }
8
+ async subscribe(owner, handler) {
9
+ this.handlers.set(owner, handler);
10
+ }
11
+ async close() {
12
+ this.handlers.clear();
13
+ }
14
+ constructor(){
15
+ this.handlers = new Map();
16
+ }
17
+ }
18
+ const InProcessMessageBus = WebsocketsMessageBus.createImplementation({
19
+ implementation: InProcessMessageBusImpl,
20
+ dependencies: []
21
+ });
22
+ export { InProcessMessageBus, InProcessMessageBusImpl };
23
+
24
+ //# sourceMappingURL=InProcessMessageBus.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bus/InProcessMessageBus.js","sources":["../../src/bus/InProcessMessageBus.ts"],"sourcesContent":["import { WebsocketsMessageBus } from \"./abstractions.js\";\n\n/*\n * The bus for a single process, and the default. Every socket is in this process, so the only\n * listener it can reach is its own. Rows another process wrote, such as this process's predecessor\n * before a restart, name an owner nobody listens for here; those messages are dropped and the rows\n * are left to the heartbeat's stale cleanup.\n */\nclass InProcessMessageBusImpl implements WebsocketsMessageBus.Interface {\n private readonly handlers = new Map<string, WebsocketsMessageBus.Handler>();\n\n public async publish(owner: string, message: WebsocketsMessageBus.Message): Promise<void> {\n const handler = this.handlers.get(owner);\n if (!handler) {\n return;\n }\n await handler(message);\n }\n\n public async subscribe(owner: string, handler: WebsocketsMessageBus.Handler): Promise<void> {\n this.handlers.set(owner, handler);\n }\n\n public async close(): Promise<void> {\n this.handlers.clear();\n }\n}\n\nexport { InProcessMessageBusImpl };\n\nexport const InProcessMessageBus = WebsocketsMessageBus.createImplementation({\n implementation: InProcessMessageBusImpl,\n dependencies: []\n});\n"],"names":["InProcessMessageBusImpl","owner","message","handler","Map","InProcessMessageBus","WebsocketsMessageBus"],"mappings":";AAQA,MAAMA;IAGF,MAAa,QAAQC,KAAa,EAAEC,OAAqC,EAAiB;QACtF,MAAMC,UAAU,IAAI,CAAC,QAAQ,CAAC,GAAG,CAACF;QAClC,IAAI,CAACE,SACD;QAEJ,MAAMA,QAAQD;IAClB;IAEA,MAAa,UAAUD,KAAa,EAAEE,OAAqC,EAAiB;QACxF,IAAI,CAAC,QAAQ,CAAC,GAAG,CAACF,OAAOE;IAC7B;IAEA,MAAa,QAAuB;QAChC,IAAI,CAAC,QAAQ,CAAC,KAAK;IACvB;;aAhBiB,QAAQ,GAAG,IAAIC;;AAiBpC;AAIO,MAAMC,sBAAsBC,qBAAqB,oBAAoB,CAAC;IACzE,gBAAgBN;IAChB,cAAc,EAAE;AACpB"}
@@ -0,0 +1,30 @@
1
+ import type { GenericRecord } from "@webiny/api/types.js";
2
+ export interface IWebsocketsBusSendMessage {
3
+ action: "send";
4
+ connectionIds: string[];
5
+ data: GenericRecord;
6
+ }
7
+ export interface IWebsocketsBusDisconnectMessage {
8
+ action: "disconnect";
9
+ connectionIds: string[];
10
+ }
11
+ export type IWebsocketsBusMessage = IWebsocketsBusSendMessage | IWebsocketsBusDisconnectMessage;
12
+ export type IWebsocketsBusHandler = (message: IWebsocketsBusMessage) => Promise<void>;
13
+ /**
14
+ * Carries a send or a disconnect to the process that holds the sockets. Each process listens on its
15
+ * own channel, named after its `ProcessOwner` id, and other processes publish to it.
16
+ *
17
+ * Delivery is at most once and nothing is queued: a message for a process that is not listening is
18
+ * dropped, as a push to a reconnecting client is on AWS.
19
+ */
20
+ export interface IWebsocketsMessageBus {
21
+ publish(owner: string, message: IWebsocketsBusMessage): Promise<void>;
22
+ subscribe(owner: string, handler: IWebsocketsBusHandler): Promise<void>;
23
+ close(): Promise<void>;
24
+ }
25
+ export declare const WebsocketsMessageBus: import("@webiny/di").Abstraction<IWebsocketsMessageBus>;
26
+ export declare namespace WebsocketsMessageBus {
27
+ type Interface = IWebsocketsMessageBus;
28
+ type Message = IWebsocketsBusMessage;
29
+ type Handler = IWebsocketsBusHandler;
30
+ }
@@ -0,0 +1,5 @@
1
+ import { createAbstraction } from "@webiny/feature/api";
2
+ const WebsocketsMessageBus = createAbstraction("Websockets/MessageBus");
3
+ export { WebsocketsMessageBus };
4
+
5
+ //# sourceMappingURL=abstractions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bus/abstractions.js","sources":["../../src/bus/abstractions.ts"],"sourcesContent":["import type { GenericRecord } from \"@webiny/api/types.js\";\nimport { createAbstraction } from \"@webiny/feature/api\";\n\nexport interface IWebsocketsBusSendMessage {\n action: \"send\";\n connectionIds: string[];\n data: GenericRecord;\n}\n\nexport interface IWebsocketsBusDisconnectMessage {\n action: \"disconnect\";\n connectionIds: string[];\n}\n\nexport type IWebsocketsBusMessage = IWebsocketsBusSendMessage | IWebsocketsBusDisconnectMessage;\n\nexport type IWebsocketsBusHandler = (message: IWebsocketsBusMessage) => Promise<void>;\n\n/**\n * Carries a send or a disconnect to the process that holds the sockets. Each process listens on its\n * own channel, named after its `ProcessOwner` id, and other processes publish to it.\n *\n * Delivery is at most once and nothing is queued: a message for a process that is not listening is\n * dropped, as a push to a reconnecting client is on AWS.\n */\nexport interface IWebsocketsMessageBus {\n publish(owner: string, message: IWebsocketsBusMessage): Promise<void>;\n subscribe(owner: string, handler: IWebsocketsBusHandler): Promise<void>;\n close(): Promise<void>;\n}\n\nexport const WebsocketsMessageBus =\n createAbstraction<IWebsocketsMessageBus>(\"Websockets/MessageBus\");\n\nexport namespace WebsocketsMessageBus {\n export type Interface = IWebsocketsMessageBus;\n export type Message = IWebsocketsBusMessage;\n export type Handler = IWebsocketsBusHandler;\n}\n"],"names":["WebsocketsMessageBus","createAbstraction"],"mappings":";AA+BO,MAAMA,uBACTC,kBAAyC"}
@@ -0,0 +1,10 @@
1
+ import type { Logger } from "@webiny/api-core/features/logger/abstractions.js";
2
+ import type { WebsocketsConnectionManager } from "../connectionManager/abstractions.js";
3
+ import type { WebsocketsServerAdapter } from "../adapter/abstractions.js";
4
+ import type { WebsocketsMessageBus } from "./abstractions.js";
5
+ export interface ICreateBusMessageHandlerParams {
6
+ connectionManager: WebsocketsConnectionManager.Interface<unknown>;
7
+ adapter: WebsocketsServerAdapter.Interface<unknown>;
8
+ logger: Logger.Interface;
9
+ }
10
+ export declare const createBusMessageHandler: (params: ICreateBusMessageHandlerParams) => WebsocketsMessageBus.Handler;
@@ -0,0 +1,20 @@
1
+ import { sendToLocalSockets } from "../delivery/sendToLocalSockets.js";
2
+ import { disconnectLocalSockets } from "../delivery/disconnectLocalSockets.js";
3
+ const createBusMessageHandler = (params)=>{
4
+ const { connectionManager } = params;
5
+ return async (message)=>{
6
+ let missing;
7
+ missing = "send" === message.action ? await sendToLocalSockets({
8
+ ...params,
9
+ connectionIds: message.connectionIds,
10
+ data: message.data
11
+ }) : await disconnectLocalSockets({
12
+ ...params,
13
+ connectionIds: message.connectionIds
14
+ });
15
+ for (const connectionId of missing)await connectionManager.remove(connectionId);
16
+ };
17
+ };
18
+ export { createBusMessageHandler };
19
+
20
+ //# sourceMappingURL=createBusMessageHandler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bus/createBusMessageHandler.js","sources":["../../src/bus/createBusMessageHandler.ts"],"sourcesContent":["import type { Logger } from \"@webiny/api-core/features/logger/abstractions.js\";\nimport type { WebsocketsConnectionManager } from \"~/connectionManager/abstractions.js\";\nimport type { WebsocketsServerAdapter } from \"~/adapter/abstractions.js\";\nimport type { WebsocketsMessageBus } from \"./abstractions.js\";\nimport { sendToLocalSockets } from \"~/delivery/sendToLocalSockets.js\";\nimport { disconnectLocalSockets } from \"~/delivery/disconnectLocalSockets.js\";\n\nexport interface ICreateBusMessageHandlerParams {\n connectionManager: WebsocketsConnectionManager.Interface<unknown>;\n adapter: WebsocketsServerAdapter.Interface<unknown>;\n logger: Logger.Interface;\n}\n\n/*\n * Handles what other processes publish to this one: sends and disconnects for sockets this process\n * holds. Messages arrive here only for connections this process owns, so one with no socket left is\n * gone, and its connection is removed.\n */\nexport const createBusMessageHandler = (\n params: ICreateBusMessageHandlerParams\n): WebsocketsMessageBus.Handler => {\n const { connectionManager } = params;\n return async message => {\n let missing: string[];\n if (message.action === \"send\") {\n missing = await sendToLocalSockets({\n ...params,\n connectionIds: message.connectionIds,\n data: message.data\n });\n } else {\n missing = await disconnectLocalSockets({\n ...params,\n connectionIds: message.connectionIds\n });\n }\n for (const connectionId of missing) {\n await connectionManager.remove(connectionId);\n }\n };\n};\n"],"names":["createBusMessageHandler","params","connectionManager","message","missing","sendToLocalSockets","disconnectLocalSockets","connectionId"],"mappings":";;AAkBO,MAAMA,0BAA0B,CACnCC;IAEA,MAAM,EAAEC,iBAAiB,EAAE,GAAGD;IAC9B,OAAO,OAAME;QACT,IAAIC;QAEAA,UADAD,AAAmB,WAAnBA,QAAQ,MAAM,GACJ,MAAME,mBAAmB;YAC/B,GAAGJ,MAAM;YACT,eAAeE,QAAQ,aAAa;YACpC,MAAMA,QAAQ,IAAI;QACtB,KAEU,MAAMG,uBAAuB;YACnC,GAAGL,MAAM;YACT,eAAeE,QAAQ,aAAa;QACxC;QAEJ,KAAK,MAAMI,gBAAgBH,QACvB,MAAMF,kBAAkB,MAAM,CAACK;IAEvC;AACJ"}
@@ -9,7 +9,7 @@ export declare class ServerConnectionManagerImpl implements WebsocketsConnection
9
9
  constructor(registry: ConnectionRegistry.Interface, owner: ProcessOwner.Interface);
10
10
  add(params: WebsocketsConnectionManager.AddParams<unknown>): Promise<void>;
11
11
  remove(connectionId: string): Promise<void>;
12
- removeOwnedHere(connectionIds: string[]): Promise<void>;
12
+ groupByOwner(connectionIds: string[]): Promise<Map<string, string[]>>;
13
13
  getSocket(connectionId: string): unknown | undefined;
14
14
  getMetadata(connectionId: string): WebsocketsConnectionManager.ConnectionMetadata | undefined;
15
15
  updateLastSeen(connectionId: string): Promise<void>;
@@ -38,11 +38,17 @@ class ServerConnectionManagerImpl {
38
38
  throw err;
39
39
  }
40
40
  }
41
- async removeOwnedHere(connectionIds) {
42
- if (0 === connectionIds.length) return;
41
+ async groupByOwner(connectionIds) {
42
+ const byOwner = new Map();
43
+ if (0 === connectionIds.length) return byOwner;
43
44
  const connections = await this.registry.listViaConnections(connectionIds);
44
- const owned = connections.filter((connection)=>connection.owner === this.owner.id);
45
- for (const connection of owned)await this.remove(connection.connectionId);
45
+ for (const connection of connections){
46
+ if (!connection.owner) continue;
47
+ const group = byOwner.get(connection.owner) ?? [];
48
+ group.push(connection.connectionId);
49
+ byOwner.set(connection.owner, group);
50
+ }
51
+ return byOwner;
46
52
  }
47
53
  getSocket(connectionId) {
48
54
  return this.sockets.get(connectionId);
@@ -1 +1 @@
1
- {"version":3,"file":"connectionManager/ServerConnectionManager.js","sources":["../../src/connectionManager/ServerConnectionManager.ts"],"sourcesContent":["import { ConnectionRegistry } from \"@webiny/api-websockets/exports/api.js\";\nimport { ProcessOwner } from \"@webiny/event-handler-standalone\";\nimport { WebsocketsConnectionManager } from \"./abstractions.js\";\n\nexport class ServerConnectionManagerImpl implements WebsocketsConnectionManager.Interface<unknown> {\n private readonly sockets = new Map<string, unknown>();\n private readonly metadata = new Map<string, WebsocketsConnectionManager.ConnectionMetadata>();\n\n public constructor(\n private readonly registry: ConnectionRegistry.Interface,\n private readonly owner: ProcessOwner.Interface\n ) {}\n\n public async add(params: WebsocketsConnectionManager.AddParams<unknown>): Promise<void> {\n this.sockets.set(params.connectionId, params.socket);\n this.metadata.set(params.connectionId, {\n connectionId: params.connectionId,\n endpoint: params.endpoint,\n connectedAt: params.connectedAt,\n host: params.host,\n headers: params.headers\n });\n\n // Save the connection to the shared registry. This is what lets the server later push a\n // message to a specific user — e.g. when a background task finishes and calls\n // SendToIdentity. That flow looks connections up in the registry by identity id to get their\n // connection ids, then comes back here to fetch each live socket by connection id and send.\n // The in-memory map above only answers \"socket for this connection id?\" — it can't be\n // searched by identity, so without this registry row a targeted send finds nobody. The\n // registry stores `connectedOn` as an ISO string (it compares connections against a recency\n // cutoff), so we convert the epoch-ms `connectedAt`.\n await this.registry.register({\n connectionId: params.connectionId,\n identity: params.identity,\n tenant: params.tenant,\n endpoint: params.endpoint,\n connectedOn: new Date(params.connectedAt).toISOString(),\n owner: this.owner.id\n });\n }\n\n public async remove(connectionId: string): Promise<void> {\n this.sockets.delete(connectionId);\n this.metadata.delete(connectionId);\n try {\n await this.registry.unregister({ connectionId });\n } catch (err) {\n if ((err as any).code === \"CONNECTION_NOT_FOUND\") {\n return;\n }\n throw err;\n }\n }\n\n /*\n * For connections this process has no socket for. Only the process that holds a socket can tell\n * that it is gone, so a connection is removed only when this process is its owner. Another\n * process's connections are left alone: with several processes, a socket missing here is\n * usually open there. Rows without an owner are left to the heartbeat's stale cleanup.\n */\n public async removeOwnedHere(connectionIds: string[]): Promise<void> {\n if (connectionIds.length === 0) {\n return;\n }\n const connections = await this.registry.listViaConnections(connectionIds);\n const owned = connections.filter(connection => connection.owner === this.owner.id);\n for (const connection of owned) {\n await this.remove(connection.connectionId);\n }\n }\n\n public getSocket(connectionId: string): unknown | undefined {\n return this.sockets.get(connectionId);\n }\n\n public getMetadata(\n connectionId: string\n ): WebsocketsConnectionManager.ConnectionMetadata | undefined {\n return this.metadata.get(connectionId);\n }\n\n public async updateLastSeen(connectionId: string): Promise<void> {\n await this.registry.updateLastSeen(connectionId);\n }\n\n public async cleanup(maxAge: number): Promise<string[]> {\n const olderThan = new Date(Date.now() - maxAge);\n const stale = await this.registry.listStale(olderThan);\n const removed: string[] = [];\n for (const entry of stale) {\n await this.remove(entry.connectionId);\n removed.push(entry.connectionId);\n }\n return removed;\n }\n\n public getActiveConnectionIds(): string[] {\n return Array.from(this.sockets.keys());\n }\n}\n\nexport const ServerConnectionManager = WebsocketsConnectionManager.createImplementation({\n implementation: ServerConnectionManagerImpl,\n dependencies: [ConnectionRegistry, ProcessOwner]\n});\n"],"names":["ServerConnectionManagerImpl","registry","owner","Map","params","Date","connectionId","err","connectionIds","connections","owned","connection","maxAge","olderThan","stale","removed","entry","Array","ServerConnectionManager","WebsocketsConnectionManager","ConnectionRegistry","ProcessOwner"],"mappings":";;;AAIO,MAAMA;IAIT,YACqBC,QAAsC,EACtCC,KAA6B,CAChD;aAFmBD,QAAQ,GAARA;aACAC,KAAK,GAALA;aALJ,OAAO,GAAG,IAAIC;aACd,QAAQ,GAAG,IAAIA;IAK7B;IAEH,MAAa,IAAIC,MAAsD,EAAiB;QACpF,IAAI,CAAC,OAAO,CAAC,GAAG,CAACA,OAAO,YAAY,EAAEA,OAAO,MAAM;QACnD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAACA,OAAO,YAAY,EAAE;YACnC,cAAcA,OAAO,YAAY;YACjC,UAAUA,OAAO,QAAQ;YACzB,aAAaA,OAAO,WAAW;YAC/B,MAAMA,OAAO,IAAI;YACjB,SAASA,OAAO,OAAO;QAC3B;QAUA,MAAM,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC;YACzB,cAAcA,OAAO,YAAY;YACjC,UAAUA,OAAO,QAAQ;YACzB,QAAQA,OAAO,MAAM;YACrB,UAAUA,OAAO,QAAQ;YACzB,aAAa,IAAIC,KAAKD,OAAO,WAAW,EAAE,WAAW;YACrD,OAAO,IAAI,CAAC,KAAK,CAAC,EAAE;QACxB;IACJ;IAEA,MAAa,OAAOE,YAAoB,EAAiB;QACrD,IAAI,CAAC,OAAO,CAAC,MAAM,CAACA;QACpB,IAAI,CAAC,QAAQ,CAAC,MAAM,CAACA;QACrB,IAAI;YACA,MAAM,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC;gBAAEA;YAAa;QAClD,EAAE,OAAOC,KAAK;YACV,IAAKA,AAAqB,2BAArBA,IAAY,IAAI,EACjB;YAEJ,MAAMA;QACV;IACJ;IAQA,MAAa,gBAAgBC,aAAuB,EAAiB;QACjE,IAAIA,AAAyB,MAAzBA,cAAc,MAAM,EACpB;QAEJ,MAAMC,cAAc,MAAM,IAAI,CAAC,QAAQ,CAAC,kBAAkB,CAACD;QAC3D,MAAME,QAAQD,YAAY,MAAM,CAACE,CAAAA,aAAcA,WAAW,KAAK,KAAK,IAAI,CAAC,KAAK,CAAC,EAAE;QACjF,KAAK,MAAMA,cAAcD,MACrB,MAAM,IAAI,CAAC,MAAM,CAACC,WAAW,YAAY;IAEjD;IAEO,UAAUL,YAAoB,EAAuB;QACxD,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAACA;IAC5B;IAEO,YACHA,YAAoB,EACsC;QAC1D,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAACA;IAC7B;IAEA,MAAa,eAAeA,YAAoB,EAAiB;QAC7D,MAAM,IAAI,CAAC,QAAQ,CAAC,cAAc,CAACA;IACvC;IAEA,MAAa,QAAQM,MAAc,EAAqB;QACpD,MAAMC,YAAY,IAAIR,KAAKA,KAAK,GAAG,KAAKO;QACxC,MAAME,QAAQ,MAAM,IAAI,CAAC,QAAQ,CAAC,SAAS,CAACD;QAC5C,MAAME,UAAoB,EAAE;QAC5B,KAAK,MAAMC,SAASF,MAAO;YACvB,MAAM,IAAI,CAAC,MAAM,CAACE,MAAM,YAAY;YACpCD,QAAQ,IAAI,CAACC,MAAM,YAAY;QACnC;QACA,OAAOD;IACX;IAEO,yBAAmC;QACtC,OAAOE,MAAM,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI;IACvC;AACJ;AAEO,MAAMC,0BAA0BC,4BAA4B,oBAAoB,CAAC;IACpF,gBAAgBnB;IAChB,cAAc;QAACoB;QAAoBC;KAAa;AACpD"}
1
+ {"version":3,"file":"connectionManager/ServerConnectionManager.js","sources":["../../src/connectionManager/ServerConnectionManager.ts"],"sourcesContent":["import { ConnectionRegistry } from \"@webiny/api-websockets/exports/api.js\";\nimport { ProcessOwner } from \"@webiny/event-handler-standalone\";\nimport { WebsocketsConnectionManager } from \"./abstractions.js\";\n\nexport class ServerConnectionManagerImpl implements WebsocketsConnectionManager.Interface<unknown> {\n private readonly sockets = new Map<string, unknown>();\n private readonly metadata = new Map<string, WebsocketsConnectionManager.ConnectionMetadata>();\n\n public constructor(\n private readonly registry: ConnectionRegistry.Interface,\n private readonly owner: ProcessOwner.Interface\n ) {}\n\n public async add(params: WebsocketsConnectionManager.AddParams<unknown>): Promise<void> {\n this.sockets.set(params.connectionId, params.socket);\n this.metadata.set(params.connectionId, {\n connectionId: params.connectionId,\n endpoint: params.endpoint,\n connectedAt: params.connectedAt,\n host: params.host,\n headers: params.headers\n });\n\n // Save the connection to the shared registry. This is what lets the server later push a\n // message to a specific user — e.g. when a background task finishes and calls\n // SendToIdentity. That flow looks connections up in the registry by identity id to get their\n // connection ids, then comes back here to fetch each live socket by connection id and send.\n // The in-memory map above only answers \"socket for this connection id?\" — it can't be\n // searched by identity, so without this registry row a targeted send finds nobody. The\n // registry stores `connectedOn` as an ISO string (it compares connections against a recency\n // cutoff), so we convert the epoch-ms `connectedAt`.\n await this.registry.register({\n connectionId: params.connectionId,\n identity: params.identity,\n tenant: params.tenant,\n endpoint: params.endpoint,\n connectedOn: new Date(params.connectedAt).toISOString(),\n owner: this.owner.id\n });\n }\n\n public async remove(connectionId: string): Promise<void> {\n this.sockets.delete(connectionId);\n this.metadata.delete(connectionId);\n try {\n await this.registry.unregister({ connectionId });\n } catch (err) {\n if ((err as any).code === \"CONNECTION_NOT_FOUND\") {\n return;\n }\n throw err;\n }\n }\n\n /*\n * The process holding each of these connections, for connections this process has no socket for.\n * Rows without an owner are left out: nobody can say who holds them, so they wait for the\n * heartbeat's stale cleanup.\n */\n public async groupByOwner(connectionIds: string[]): Promise<Map<string, string[]>> {\n const byOwner = new Map<string, string[]>();\n if (connectionIds.length === 0) {\n return byOwner;\n }\n const connections = await this.registry.listViaConnections(connectionIds);\n for (const connection of connections) {\n if (!connection.owner) {\n continue;\n }\n const group = byOwner.get(connection.owner) ?? [];\n group.push(connection.connectionId);\n byOwner.set(connection.owner, group);\n }\n return byOwner;\n }\n\n public getSocket(connectionId: string): unknown | undefined {\n return this.sockets.get(connectionId);\n }\n\n public getMetadata(\n connectionId: string\n ): WebsocketsConnectionManager.ConnectionMetadata | undefined {\n return this.metadata.get(connectionId);\n }\n\n public async updateLastSeen(connectionId: string): Promise<void> {\n await this.registry.updateLastSeen(connectionId);\n }\n\n public async cleanup(maxAge: number): Promise<string[]> {\n const olderThan = new Date(Date.now() - maxAge);\n const stale = await this.registry.listStale(olderThan);\n const removed: string[] = [];\n for (const entry of stale) {\n await this.remove(entry.connectionId);\n removed.push(entry.connectionId);\n }\n return removed;\n }\n\n public getActiveConnectionIds(): string[] {\n return Array.from(this.sockets.keys());\n }\n}\n\nexport const ServerConnectionManager = WebsocketsConnectionManager.createImplementation({\n implementation: ServerConnectionManagerImpl,\n dependencies: [ConnectionRegistry, ProcessOwner]\n});\n"],"names":["ServerConnectionManagerImpl","registry","owner","Map","params","Date","connectionId","err","connectionIds","byOwner","connections","connection","group","maxAge","olderThan","stale","removed","entry","Array","ServerConnectionManager","WebsocketsConnectionManager","ConnectionRegistry","ProcessOwner"],"mappings":";;;AAIO,MAAMA;IAIT,YACqBC,QAAsC,EACtCC,KAA6B,CAChD;aAFmBD,QAAQ,GAARA;aACAC,KAAK,GAALA;aALJ,OAAO,GAAG,IAAIC;aACd,QAAQ,GAAG,IAAIA;IAK7B;IAEH,MAAa,IAAIC,MAAsD,EAAiB;QACpF,IAAI,CAAC,OAAO,CAAC,GAAG,CAACA,OAAO,YAAY,EAAEA,OAAO,MAAM;QACnD,IAAI,CAAC,QAAQ,CAAC,GAAG,CAACA,OAAO,YAAY,EAAE;YACnC,cAAcA,OAAO,YAAY;YACjC,UAAUA,OAAO,QAAQ;YACzB,aAAaA,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EAAiB;QAC7D,MAAM,IAAI,CAAC,QAAQ,CAAC,cAAc,CAACA;IACvC;IAEA,MAAa,QAAQO,MAAc,EAAqB;QACpD,MAAMC,YAAY,IAAIT,KAAKA,KAAK,GAAG,KAAKQ;QACxC,MAAME,QAAQ,MAAM,IAAI,CAAC,QAAQ,CAAC,SAAS,CAACD;QAC5C,MAAME,UAAoB,EAAE;QAC5B,KAAK,MAAMC,SAASF,MAAO;YACvB,MAAM,IAAI,CAAC,MAAM,CAACE,MAAM,YAAY;YACpCD,QAAQ,IAAI,CAACC,MAAM,YAAY;QACnC;QACA,OAAOD;IACX;IAEO,yBAAmC;QACtC,OAAOE,MAAM,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI;IACvC;AACJ;AAEO,MAAMC,0BAA0BC,4BAA4B,oBAAoB,CAAC;IACpF,gBAAgBpB;IAChB,cAAc;QAACqB;QAAoBC;KAAa;AACpD"}
@@ -19,7 +19,7 @@ export interface IWebsocketsConnectionManagerMetadata {
19
19
  export interface IWebsocketsConnectionManager<TSocket> {
20
20
  add(params: IWebsocketsConnectionManagerAddParams<TSocket>): Promise<void>;
21
21
  remove(connectionId: string): Promise<void>;
22
- removeOwnedHere(connectionIds: string[]): Promise<void>;
22
+ groupByOwner(connectionIds: string[]): Promise<Map<string, string[]>>;
23
23
  getSocket(connectionId: string): TSocket | undefined;
24
24
  getMetadata(connectionId: string): IWebsocketsConnectionManagerMetadata | undefined;
25
25
  updateLastSeen(connectionId: string): Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"connectionManager/abstractions.js","sources":["../../src/connectionManager/abstractions.ts"],"sourcesContent":["import { ConnectionRegistry } from \"@webiny/api-websockets/exports/api.js\";\nimport { createAbstraction } from \"@webiny/feature/api\";\n\nexport interface IWebsocketsConnectionManagerAddParams<TSocket> {\n connectionId: string;\n socket: TSocket;\n endpoint: string;\n identity: ConnectionRegistry.Identity;\n tenant: string;\n connectedAt: number;\n host: string;\n headers: Record<string, string>;\n}\n\nexport interface IWebsocketsConnectionManagerMetadata {\n connectionId: string;\n endpoint: string;\n connectedAt: number;\n host: string;\n headers: Record<string, string>;\n}\n\nexport interface IWebsocketsConnectionManager<TSocket> {\n add(params: IWebsocketsConnectionManagerAddParams<TSocket>): Promise<void>;\n remove(connectionId: string): Promise<void>;\n removeOwnedHere(connectionIds: string[]): Promise<void>;\n getSocket(connectionId: string): TSocket | undefined;\n getMetadata(connectionId: string): IWebsocketsConnectionManagerMetadata | undefined;\n updateLastSeen(connectionId: string): Promise<void>;\n cleanup(maxAge: number): Promise<string[]>;\n getActiveConnectionIds(): string[];\n}\n\nexport const WebsocketsConnectionManager = createAbstraction<IWebsocketsConnectionManager<unknown>>(\n \"WebsocketsConnectionManager\"\n);\n\nexport namespace WebsocketsConnectionManager {\n export type Interface<TSocket> = IWebsocketsConnectionManager<TSocket>;\n export type AddParams<TSocket> = IWebsocketsConnectionManagerAddParams<TSocket>;\n export type ConnectionMetadata = IWebsocketsConnectionManagerMetadata;\n}\n"],"names":["WebsocketsConnectionManager","createAbstraction"],"mappings":";AAiCO,MAAMA,8BAA8BC,kBACvC"}
1
+ {"version":3,"file":"connectionManager/abstractions.js","sources":["../../src/connectionManager/abstractions.ts"],"sourcesContent":["import { ConnectionRegistry } from \"@webiny/api-websockets/exports/api.js\";\nimport { createAbstraction } from \"@webiny/feature/api\";\n\nexport interface IWebsocketsConnectionManagerAddParams<TSocket> {\n connectionId: string;\n socket: TSocket;\n endpoint: string;\n identity: ConnectionRegistry.Identity;\n tenant: string;\n connectedAt: number;\n host: string;\n headers: Record<string, string>;\n}\n\nexport interface IWebsocketsConnectionManagerMetadata {\n connectionId: string;\n endpoint: string;\n connectedAt: number;\n host: string;\n headers: Record<string, string>;\n}\n\nexport interface IWebsocketsConnectionManager<TSocket> {\n add(params: IWebsocketsConnectionManagerAddParams<TSocket>): Promise<void>;\n remove(connectionId: string): Promise<void>;\n groupByOwner(connectionIds: string[]): Promise<Map<string, string[]>>;\n getSocket(connectionId: string): TSocket | undefined;\n getMetadata(connectionId: string): IWebsocketsConnectionManagerMetadata | undefined;\n updateLastSeen(connectionId: string): Promise<void>;\n cleanup(maxAge: number): Promise<string[]>;\n getActiveConnectionIds(): string[];\n}\n\nexport const WebsocketsConnectionManager = createAbstraction<IWebsocketsConnectionManager<unknown>>(\n \"WebsocketsConnectionManager\"\n);\n\nexport namespace WebsocketsConnectionManager {\n export type Interface<TSocket> = IWebsocketsConnectionManager<TSocket>;\n export type AddParams<TSocket> = IWebsocketsConnectionManagerAddParams<TSocket>;\n export type ConnectionMetadata = IWebsocketsConnectionManagerMetadata;\n}\n"],"names":["WebsocketsConnectionManager","createAbstraction"],"mappings":";AAiCO,MAAMA,8BAA8BC,kBACvC"}
@@ -0,0 +1,10 @@
1
+ import type { Logger } from "@webiny/api-core/features/logger/abstractions.js";
2
+ import type { WebsocketsConnectionManager } from "../connectionManager/abstractions.js";
3
+ import type { WebsocketsServerAdapter } from "../adapter/abstractions.js";
4
+ export interface IDisconnectLocalSocketsParams {
5
+ connectionManager: WebsocketsConnectionManager.Interface<unknown>;
6
+ adapter: WebsocketsServerAdapter.Interface<unknown>;
7
+ logger: Logger.Interface;
8
+ connectionIds: string[];
9
+ }
10
+ export declare const disconnectLocalSockets: (params: IDisconnectLocalSocketsParams) => Promise<string[]>;
@@ -0,0 +1,24 @@
1
+ const disconnectLocalSockets = async (params)=>{
2
+ const { connectionManager, adapter, logger, connectionIds } = params;
3
+ const missing = [];
4
+ for (const connectionId of connectionIds){
5
+ const socket = connectionManager.getSocket(connectionId);
6
+ if (void 0 === socket) {
7
+ missing.push(connectionId);
8
+ continue;
9
+ }
10
+ try {
11
+ adapter.close(socket);
12
+ await connectionManager.remove(connectionId);
13
+ } catch (error) {
14
+ logger.error({
15
+ error,
16
+ connectionId
17
+ }, "[websockets] could not disconnect a connection");
18
+ }
19
+ }
20
+ return missing;
21
+ };
22
+ export { disconnectLocalSockets };
23
+
24
+ //# sourceMappingURL=disconnectLocalSockets.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"delivery/disconnectLocalSockets.js","sources":["../../src/delivery/disconnectLocalSockets.ts"],"sourcesContent":["import type { Logger } from \"@webiny/api-core/features/logger/abstractions.js\";\nimport type { WebsocketsConnectionManager } from \"~/connectionManager/abstractions.js\";\nimport type { WebsocketsServerAdapter } from \"~/adapter/abstractions.js\";\n\nexport interface IDisconnectLocalSocketsParams {\n connectionManager: WebsocketsConnectionManager.Interface<unknown>;\n adapter: WebsocketsServerAdapter.Interface<unknown>;\n logger: Logger.Interface;\n connectionIds: string[];\n}\n\n/*\n * Closes each socket this process holds, removes its connection, and returns the ids it holds no\n * socket for. A failed close is logged and the others go on.\n */\nexport const disconnectLocalSockets = async (\n params: IDisconnectLocalSocketsParams\n): Promise<string[]> => {\n const { connectionManager, adapter, logger, connectionIds } = params;\n const missing: string[] = [];\n for (const connectionId of connectionIds) {\n const socket = connectionManager.getSocket(connectionId);\n if (socket === undefined) {\n missing.push(connectionId);\n continue;\n }\n try {\n adapter.close(socket);\n await connectionManager.remove(connectionId);\n } catch (error) {\n logger.error({ error, connectionId }, \"[websockets] could not disconnect a connection\");\n }\n }\n return missing;\n};\n"],"names":["disconnectLocalSockets","params","connectionManager","adapter","logger","connectionIds","missing","connectionId","socket","undefined","error"],"mappings":"AAeO,MAAMA,yBAAyB,OAClCC;IAEA,MAAM,EAAEC,iBAAiB,EAAEC,OAAO,EAAEC,MAAM,EAAEC,aAAa,EAAE,GAAGJ;IAC9D,MAAMK,UAAoB,EAAE;IAC5B,KAAK,MAAMC,gBAAgBF,cAAe;QACtC,MAAMG,SAASN,kBAAkB,SAAS,CAACK;QAC3C,IAAIC,AAAWC,WAAXD,QAAsB;YACtBF,QAAQ,IAAI,CAACC;YACb;QACJ;QACA,IAAI;YACAJ,QAAQ,KAAK,CAACK;YACd,MAAMN,kBAAkB,MAAM,CAACK;QACnC,EAAE,OAAOG,OAAO;YACZN,OAAO,KAAK,CAAC;gBAAEM;gBAAOH;YAAa,GAAG;QAC1C;IACJ;IACA,OAAOD;AACX"}
@@ -0,0 +1,12 @@
1
+ import type { GenericRecord } from "@webiny/api/types.js";
2
+ import type { Logger } from "@webiny/api-core/features/logger/abstractions.js";
3
+ import type { WebsocketsConnectionManager } from "../connectionManager/abstractions.js";
4
+ import type { WebsocketsServerAdapter } from "../adapter/abstractions.js";
5
+ export interface ISendToLocalSocketsParams {
6
+ connectionManager: WebsocketsConnectionManager.Interface<unknown>;
7
+ adapter: WebsocketsServerAdapter.Interface<unknown>;
8
+ logger: Logger.Interface;
9
+ connectionIds: string[];
10
+ data: GenericRecord;
11
+ }
12
+ export declare const sendToLocalSockets: (params: ISendToLocalSocketsParams) => Promise<string[]>;
@@ -0,0 +1,24 @@
1
+ const sendToLocalSockets = async (params)=>{
2
+ const { connectionManager, adapter, logger, connectionIds, data } = params;
3
+ const payload = JSON.stringify(data);
4
+ const missing = [];
5
+ for (const connectionId of connectionIds){
6
+ const socket = connectionManager.getSocket(connectionId);
7
+ if (void 0 === socket) {
8
+ missing.push(connectionId);
9
+ continue;
10
+ }
11
+ try {
12
+ await adapter.send(socket, payload);
13
+ } catch (error) {
14
+ logger.error({
15
+ error,
16
+ connectionId
17
+ }, "[websockets] could not send to a connection");
18
+ }
19
+ }
20
+ return missing;
21
+ };
22
+ export { sendToLocalSockets };
23
+
24
+ //# sourceMappingURL=sendToLocalSockets.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"delivery/sendToLocalSockets.js","sources":["../../src/delivery/sendToLocalSockets.ts"],"sourcesContent":["import type { GenericRecord } from \"@webiny/api/types.js\";\nimport type { Logger } from \"@webiny/api-core/features/logger/abstractions.js\";\nimport type { WebsocketsConnectionManager } from \"~/connectionManager/abstractions.js\";\nimport type { WebsocketsServerAdapter } from \"~/adapter/abstractions.js\";\n\nexport interface ISendToLocalSocketsParams {\n connectionManager: WebsocketsConnectionManager.Interface<unknown>;\n adapter: WebsocketsServerAdapter.Interface<unknown>;\n logger: Logger.Interface;\n connectionIds: string[];\n data: GenericRecord;\n}\n\n/*\n * Sends `data` on each socket this process holds and returns the ids it holds no socket for. A failed\n * send is logged and the others go on.\n */\nexport const sendToLocalSockets = async (params: ISendToLocalSocketsParams): Promise<string[]> => {\n const { connectionManager, adapter, logger, connectionIds, data } = params;\n const payload = JSON.stringify(data);\n const missing: string[] = [];\n for (const connectionId of connectionIds) {\n const socket = connectionManager.getSocket(connectionId);\n if (socket === undefined) {\n missing.push(connectionId);\n continue;\n }\n try {\n await adapter.send(socket, payload);\n } catch (error) {\n logger.error({ error, connectionId }, \"[websockets] could not send to a connection\");\n }\n }\n return missing;\n};\n"],"names":["sendToLocalSockets","params","connectionManager","adapter","logger","connectionIds","data","payload","JSON","missing","connectionId","socket","undefined","error"],"mappings":"AAiBO,MAAMA,qBAAqB,OAAOC;IACrC,MAAM,EAAEC,iBAAiB,EAAEC,OAAO,EAAEC,MAAM,EAAEC,aAAa,EAAEC,IAAI,EAAE,GAAGL;IACpE,MAAMM,UAAUC,KAAK,SAAS,CAACF;IAC/B,MAAMG,UAAoB,EAAE;IAC5B,KAAK,MAAMC,gBAAgBL,cAAe;QACtC,MAAMM,SAAST,kBAAkB,SAAS,CAACQ;QAC3C,IAAIC,AAAWC,WAAXD,QAAsB;YACtBF,QAAQ,IAAI,CAACC;YACb;QACJ;QACA,IAAI;YACA,MAAMP,QAAQ,IAAI,CAACQ,QAAQJ;QAC/B,EAAE,OAAOM,OAAO;YACZT,OAAO,KAAK,CAAC;gBAAES;gBAAOH;YAAa,GAAG;QAC1C;IACJ;IACA,OAAOD;AACX"}
package/index.d.ts CHANGED
@@ -3,7 +3,10 @@ export type { IWebsocketsServer, WebsocketsConnectionAuthenticator } from "./ser
3
3
  export * from "./adapter/abstractions.js";
4
4
  export * from "./upgradeHandler/abstractions.js";
5
5
  export * from "./connectionManager/abstractions.js";
6
+ export { WebsocketsMessageBus } from "./bus/abstractions.js";
7
+ export { createBusMessageHandler } from "./bus/createBusMessageHandler.js";
6
8
  export { ServerConnectionManager } from "./connectionManager/ServerConnectionManager.js";
7
9
  export { NodeWsAdapter } from "./adapter/NodeWsAdapter.js";
8
10
  export { ServerWebsocketsTransport } from "./transport/ServerWebsocketsTransport.js";
11
+ export { InProcessMessageBus } from "./bus/InProcessMessageBus.js";
9
12
  export { WebsocketsStandaloneFeature } from "./WebsocketsStandaloneFeature.js";
package/index.js CHANGED
@@ -2,7 +2,10 @@ export * from "./adapter/abstractions.js";
2
2
  export * from "./upgradeHandler/abstractions.js";
3
3
  export * from "./connectionManager/abstractions.js";
4
4
  export { attachWebsocketsServer, createWebsocketsServer } from "./server/WebsocketsServer.js";
5
+ export { WebsocketsMessageBus } from "./bus/abstractions.js";
6
+ export { createBusMessageHandler } from "./bus/createBusMessageHandler.js";
5
7
  export { ServerConnectionManager } from "./connectionManager/ServerConnectionManager.js";
6
8
  export { NodeWsAdapter } from "./adapter/NodeWsAdapter.js";
7
9
  export { ServerWebsocketsTransport } from "./transport/ServerWebsocketsTransport.js";
10
+ export { InProcessMessageBus } from "./bus/InProcessMessageBus.js";
8
11
  export { WebsocketsStandaloneFeature } from "./WebsocketsStandaloneFeature.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/api-websockets-standalone",
3
- "version": "0.0.0-unstable.7c2def32a7",
3
+ "version": "0.0.0-unstable.9609e4e0c9",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -16,21 +16,22 @@
16
16
  },
17
17
  "license": "MIT",
18
18
  "dependencies": {
19
- "@webiny/api": "0.0.0-unstable.7c2def32a7",
20
- "@webiny/api-websockets": "0.0.0-unstable.7c2def32a7",
21
- "@webiny/event-handler-standalone": "0.0.0-unstable.7c2def32a7",
22
- "@webiny/feature": "0.0.0-unstable.7c2def32a7",
23
- "@webiny/plugins": "0.0.0-unstable.7c2def32a7",
24
- "@webiny/utils": "0.0.0-unstable.7c2def32a7",
25
- "ws": "8.21.3",
26
- "zod": "4.4.3"
19
+ "@webiny/api": "0.0.0-unstable.9609e4e0c9",
20
+ "@webiny/api-core": "0.0.0-unstable.9609e4e0c9",
21
+ "@webiny/api-websockets": "0.0.0-unstable.9609e4e0c9",
22
+ "@webiny/event-handler-standalone": "0.0.0-unstable.9609e4e0c9",
23
+ "@webiny/feature": "0.0.0-unstable.9609e4e0c9",
24
+ "@webiny/plugins": "0.0.0-unstable.9609e4e0c9",
25
+ "@webiny/utils": "0.0.0-unstable.9609e4e0c9",
26
+ "ws": "8.22.0",
27
+ "zod": "4.6.7"
27
28
  },
28
29
  "devDependencies": {
29
- "@types/ws": "8.18.1",
30
- "@webiny/build-tools": "0.0.0-unstable.7c2def32a7",
30
+ "@types/ws": "8.18.2",
31
+ "@webiny/build-tools": "0.0.0-unstable.9609e4e0c9",
31
32
  "rimraf": "6.1.3",
32
33
  "typescript": "7.0.2",
33
- "vitest": "4.1.11"
34
+ "vitest": "5.0.5"
34
35
  },
35
36
  "publishConfig": {
36
37
  "access": "public"
@@ -1,13 +1,20 @@
1
1
  import { WebsocketsTransport } from "@webiny/api-websockets/exports/api.js";
2
2
  import type { GenericRecord } from "@webiny/api/types.js";
3
+ import { Logger } from "@webiny/api-core/features/logger/abstractions.js";
4
+ import { ProcessOwner } from "@webiny/event-handler-standalone";
3
5
  import { WebsocketsConnectionManager } from "../connectionManager/abstractions.js";
4
6
  import { WebsocketsServerAdapter } from "../adapter/abstractions.js";
7
+ import { WebsocketsMessageBus } from "../bus/abstractions.js";
5
8
  export declare class ServerWebsocketsTransportImpl implements WebsocketsTransport.Interface {
6
9
  private readonly connectionManager;
7
10
  private readonly adapter;
8
- constructor(connectionManager: WebsocketsConnectionManager.Interface<unknown>, adapter: WebsocketsServerAdapter.Interface<unknown>);
11
+ private readonly bus;
12
+ private readonly owner;
13
+ private readonly logger;
14
+ constructor(connectionManager: WebsocketsConnectionManager.Interface<unknown>, adapter: WebsocketsServerAdapter.Interface<unknown>, bus: WebsocketsMessageBus.Interface, owner: ProcessOwner.Interface, logger: Logger.Interface);
9
15
  send<T extends GenericRecord = GenericRecord>(connections: WebsocketsTransport.SendConnection[], data: WebsocketsTransport.SendData<T>): Promise<void>;
10
16
  disconnect(connections: WebsocketsTransport.DisconnectConnection[]): Promise<void>;
17
+ private forward;
11
18
  }
12
19
  export declare const ServerWebsocketsTransport: typeof ServerWebsocketsTransportImpl & {
13
20
  __abstraction: import("@webiny/di").Abstraction<import("@webiny/api-websockets/transport").IWebsocketsTransport>;
@@ -1,54 +1,75 @@
1
1
  import { WebsocketsTransport } from "@webiny/api-websockets/exports/api.js";
2
+ import { Logger } from "@webiny/api-core/features/logger/abstractions.js";
3
+ import { ProcessOwner } from "@webiny/event-handler-standalone";
2
4
  import { WebsocketsConnectionManager } from "../connectionManager/abstractions.js";
3
5
  import { WebsocketsServerAdapter } from "../adapter/abstractions.js";
6
+ import { WebsocketsMessageBus } from "../bus/abstractions.js";
7
+ import { sendToLocalSockets } from "../delivery/sendToLocalSockets.js";
8
+ import { disconnectLocalSockets } from "../delivery/disconnectLocalSockets.js";
4
9
  class ServerWebsocketsTransportImpl {
5
- constructor(connectionManager, adapter){
10
+ constructor(connectionManager, adapter, bus, owner, logger){
6
11
  this.connectionManager = connectionManager;
7
12
  this.adapter = adapter;
13
+ this.bus = bus;
14
+ this.owner = owner;
15
+ this.logger = logger;
8
16
  }
9
17
  async send(connections, data) {
10
- const missing = [];
11
- for (const connection of connections){
12
- const { connectionId } = connection;
13
- const socket = this.connectionManager.getSocket(connectionId);
14
- if (void 0 === socket) {
15
- missing.push(connectionId);
16
- continue;
17
- }
18
- try {
19
- await this.adapter.send(socket, JSON.stringify(data));
20
- } catch (ex) {
21
- console.error(`Failed to send message to connection "${connectionId}". Check logs for more information.`);
22
- console.log(ex);
23
- }
24
- }
25
- await this.connectionManager.removeOwnedHere(missing);
18
+ const connectionIds = connections.map((connection)=>connection.connectionId);
19
+ const missing = await sendToLocalSockets({
20
+ connectionManager: this.connectionManager,
21
+ adapter: this.adapter,
22
+ logger: this.logger,
23
+ connectionIds,
24
+ data
25
+ });
26
+ await this.forward(missing, (ids)=>({
27
+ action: "send",
28
+ connectionIds: ids,
29
+ data
30
+ }));
26
31
  }
27
32
  async disconnect(connections) {
28
- const missing = [];
29
- for (const connection of connections){
30
- const { connectionId } = connection;
31
- const socket = this.connectionManager.getSocket(connectionId);
32
- if (void 0 === socket) {
33
- missing.push(connectionId);
33
+ const connectionIds = connections.map((connection)=>connection.connectionId);
34
+ const missing = await disconnectLocalSockets({
35
+ connectionManager: this.connectionManager,
36
+ adapter: this.adapter,
37
+ logger: this.logger,
38
+ connectionIds
39
+ });
40
+ await this.forward(missing, (ids)=>({
41
+ action: "disconnect",
42
+ connectionIds: ids
43
+ }));
44
+ }
45
+ async forward(missing, toMessage) {
46
+ const byOwner = await this.connectionManager.groupByOwner(missing);
47
+ for (const [owner, connectionIds] of byOwner){
48
+ if (owner === this.owner.id) {
49
+ for (const connectionId of connectionIds)await this.connectionManager.remove(connectionId);
34
50
  continue;
35
51
  }
52
+ const message = toMessage(connectionIds);
36
53
  try {
37
- this.adapter.close(socket);
38
- await this.connectionManager.remove(connectionId);
39
- } catch (ex) {
40
- console.error(`Failed to disconnect connection "${connectionId}". Check logs for more information.`);
41
- console.log(ex);
54
+ await this.bus.publish(owner, message);
55
+ } catch (error) {
56
+ this.logger.error({
57
+ error,
58
+ owner,
59
+ connectionIds
60
+ }, "[websockets] could not publish to the process holding these connections");
42
61
  }
43
62
  }
44
- await this.connectionManager.removeOwnedHere(missing);
45
63
  }
46
64
  }
47
65
  const ServerWebsocketsTransport = WebsocketsTransport.createImplementation({
48
66
  implementation: ServerWebsocketsTransportImpl,
49
67
  dependencies: [
50
68
  WebsocketsConnectionManager,
51
- WebsocketsServerAdapter
69
+ WebsocketsServerAdapter,
70
+ WebsocketsMessageBus,
71
+ ProcessOwner,
72
+ Logger
52
73
  ]
53
74
  });
54
75
  export { ServerWebsocketsTransport, ServerWebsocketsTransportImpl };
@@ -1 +1 @@
1
- {"version":3,"file":"transport/ServerWebsocketsTransport.js","sources":["../../src/transport/ServerWebsocketsTransport.ts"],"sourcesContent":["import { WebsocketsTransport } from \"@webiny/api-websockets/exports/api.js\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\nimport { WebsocketsConnectionManager } from \"~/connectionManager/abstractions.js\";\nimport { WebsocketsServerAdapter } from \"~/adapter/abstractions.js\";\n\nexport class ServerWebsocketsTransportImpl implements WebsocketsTransport.Interface {\n private readonly connectionManager: WebsocketsConnectionManager.Interface<unknown>;\n private readonly adapter: WebsocketsServerAdapter.Interface<unknown>;\n\n public constructor(\n connectionManager: WebsocketsConnectionManager.Interface<unknown>,\n adapter: WebsocketsServerAdapter.Interface<unknown>\n ) {\n this.connectionManager = connectionManager;\n this.adapter = adapter;\n }\n\n public async send<T extends GenericRecord = GenericRecord>(\n connections: WebsocketsTransport.SendConnection[],\n data: WebsocketsTransport.SendData<T>\n ): Promise<void> {\n // Connections with no socket in this process; see `removeOwnedHere`.\n const missing: string[] = [];\n for (const connection of connections) {\n const { connectionId } = connection;\n const socket = this.connectionManager.getSocket(connectionId);\n if (socket === undefined) {\n missing.push(connectionId);\n continue;\n }\n try {\n await this.adapter.send(socket, JSON.stringify(data));\n } catch (ex) {\n console.error(\n `Failed to send message to connection \"${connectionId}\". Check logs for more information.`\n );\n console.log(ex);\n }\n }\n await this.connectionManager.removeOwnedHere(missing);\n }\n\n public async disconnect(\n connections: WebsocketsTransport.DisconnectConnection[]\n ): Promise<void> {\n const missing: string[] = [];\n for (const connection of connections) {\n const { connectionId } = connection;\n const socket = this.connectionManager.getSocket(connectionId);\n if (socket === undefined) {\n missing.push(connectionId);\n continue;\n }\n try {\n this.adapter.close(socket);\n await this.connectionManager.remove(connectionId);\n } catch (ex) {\n console.error(\n `Failed to disconnect connection \"${connectionId}\". Check logs for more information.`\n );\n console.log(ex);\n }\n }\n await this.connectionManager.removeOwnedHere(missing);\n }\n}\n\nexport const ServerWebsocketsTransport = WebsocketsTransport.createImplementation({\n implementation: ServerWebsocketsTransportImpl,\n dependencies: [WebsocketsConnectionManager, WebsocketsServerAdapter]\n});\n"],"names":["ServerWebsocketsTransportImpl","connectionManager","adapter","connections","data","missing","connection","connectionId","socket","undefined","JSON","ex","console","ServerWebsocketsTransport","WebsocketsTransport","WebsocketsConnectionManager","WebsocketsServerAdapter"],"mappings":";;;AAKO,MAAMA;IAIT,YACIC,iBAAiE,EACjEC,OAAmD,CACrD;QACE,IAAI,CAAC,iBAAiB,GAAGD;QACzB,IAAI,CAAC,OAAO,GAAGC;IACnB;IAEA,MAAa,KACTC,WAAiD,EACjDC,IAAqC,EACxB;QAEb,MAAMC,UAAoB,EAAE;QAC5B,KAAK,MAAMC,cAAcH,YAAa;YAClC,MAAM,EAAEI,YAAY,EAAE,GAAGD;YACzB,MAAME,SAAS,IAAI,CAAC,iBAAiB,CAAC,SAAS,CAACD;YAChD,IAAIC,AAAWC,WAAXD,QAAsB;gBACtBH,QAAQ,IAAI,CAACE;gBACb;YACJ;YACA,IAAI;gBACA,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CAACC,QAAQE,KAAK,SAAS,CAACN;YACnD,EAAE,OAAOO,IAAI;gBACTC,QAAQ,KAAK,CACT,CAAC,sCAAsC,EAAEL,aAAa,mCAAmC,CAAC;gBAE9FK,QAAQ,GAAG,CAACD;YAChB;QACJ;QACA,MAAM,IAAI,CAAC,iBAAiB,CAAC,eAAe,CAACN;IACjD;IAEA,MAAa,WACTF,WAAuD,EAC1C;QACb,MAAME,UAAoB,EAAE;QAC5B,KAAK,MAAMC,cAAcH,YAAa;YAClC,MAAM,EAAEI,YAAY,EAAE,GAAGD;YACzB,MAAME,SAAS,IAAI,CAAC,iBAAiB,CAAC,SAAS,CAACD;YAChD,IAAIC,AAAWC,WAAXD,QAAsB;gBACtBH,QAAQ,IAAI,CAACE;gBACb;YACJ;YACA,IAAI;gBACA,IAAI,CAAC,OAAO,CAAC,KAAK,CAACC;gBACnB,MAAM,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAACD;YACxC,EAAE,OAAOI,IAAI;gBACTC,QAAQ,KAAK,CACT,CAAC,iCAAiC,EAAEL,aAAa,mCAAmC,CAAC;gBAEzFK,QAAQ,GAAG,CAACD;YAChB;QACJ;QACA,MAAM,IAAI,CAAC,iBAAiB,CAAC,eAAe,CAACN;IACjD;AACJ;AAEO,MAAMQ,4BAA4BC,oBAAoB,oBAAoB,CAAC;IAC9E,gBAAgBd;IAChB,cAAc;QAACe;QAA6BC;KAAwB;AACxE"}
1
+ {"version":3,"file":"transport/ServerWebsocketsTransport.js","sources":["../../src/transport/ServerWebsocketsTransport.ts"],"sourcesContent":["import { WebsocketsTransport } from \"@webiny/api-websockets/exports/api.js\";\nimport type { GenericRecord } from \"@webiny/api/types.js\";\nimport { Logger } from \"@webiny/api-core/features/logger/abstractions.js\";\nimport { ProcessOwner } from \"@webiny/event-handler-standalone\";\nimport { WebsocketsConnectionManager } from \"~/connectionManager/abstractions.js\";\nimport { WebsocketsServerAdapter } from \"~/adapter/abstractions.js\";\nimport { WebsocketsMessageBus } from \"~/bus/abstractions.js\";\nimport { sendToLocalSockets } from \"~/delivery/sendToLocalSockets.js\";\nimport { disconnectLocalSockets } from \"~/delivery/disconnectLocalSockets.js\";\n\ntype ToMessage = (connectionIds: string[]) => WebsocketsMessageBus.Message;\n\n/*\n * Sends to the sockets this process holds, and hands the rest to the process that holds them, through\n * the message bus. With one process there is no rest, apart from rows a previous run of the process\n * left behind.\n */\nexport class ServerWebsocketsTransportImpl implements WebsocketsTransport.Interface {\n public constructor(\n private readonly connectionManager: WebsocketsConnectionManager.Interface<unknown>,\n private readonly adapter: WebsocketsServerAdapter.Interface<unknown>,\n private readonly bus: WebsocketsMessageBus.Interface,\n private readonly owner: ProcessOwner.Interface,\n private readonly logger: Logger.Interface\n ) {}\n\n public async send<T extends GenericRecord = GenericRecord>(\n connections: WebsocketsTransport.SendConnection[],\n data: WebsocketsTransport.SendData<T>\n ): Promise<void> {\n const connectionIds = connections.map(connection => connection.connectionId);\n const missing = await sendToLocalSockets({\n connectionManager: this.connectionManager,\n adapter: this.adapter,\n logger: this.logger,\n connectionIds,\n data\n });\n await this.forward(missing, ids => {\n return { action: \"send\", connectionIds: ids, data };\n });\n }\n\n public async disconnect(\n connections: WebsocketsTransport.DisconnectConnection[]\n ): Promise<void> {\n const connectionIds = connections.map(connection => connection.connectionId);\n const missing = await disconnectLocalSockets({\n connectionManager: this.connectionManager,\n adapter: this.adapter,\n logger: this.logger,\n connectionIds\n });\n await this.forward(missing, ids => {\n return { action: \"disconnect\", connectionIds: ids };\n });\n }\n\n /*\n * Connections this process has no socket for. Its own are gone, so they are removed; another\n * process's are published to that process. A failed publish is logged, not thrown: the message\n * is lost, as it would be if that process were restarting.\n */\n private async forward(missing: string[], toMessage: ToMessage): Promise<void> {\n const byOwner = await this.connectionManager.groupByOwner(missing);\n for (const [owner, connectionIds] of byOwner) {\n if (owner === this.owner.id) {\n for (const connectionId of connectionIds) {\n await this.connectionManager.remove(connectionId);\n }\n continue;\n }\n const message = toMessage(connectionIds);\n try {\n await this.bus.publish(owner, message);\n } catch (error) {\n this.logger.error(\n { error, owner, connectionIds },\n \"[websockets] could not publish to the process holding these connections\"\n );\n }\n }\n }\n}\n\nexport const ServerWebsocketsTransport = WebsocketsTransport.createImplementation({\n implementation: ServerWebsocketsTransportImpl,\n dependencies: [\n WebsocketsConnectionManager,\n WebsocketsServerAdapter,\n WebsocketsMessageBus,\n ProcessOwner,\n Logger\n ]\n});\n"],"names":["ServerWebsocketsTransportImpl","connectionManager","adapter","bus","owner","logger","connections","data","connectionIds","connection","missing","sendToLocalSockets","ids","disconnectLocalSockets","toMessage","byOwner","connectionId","message","error","ServerWebsocketsTransport","WebsocketsTransport","WebsocketsConnectionManager","WebsocketsServerAdapter","WebsocketsMessageBus","ProcessOwner","Logger"],"mappings":";;;;;;;;AAiBO,MAAMA;IACT,YACqBC,iBAAiE,EACjEC,OAAmD,EACnDC,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