@cotal-ai/core 0.171.4 → 0.171.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 (35) hide show
  1. package/dist/control-exchange.d.ts.map +1 -1
  2. package/dist/control-exchange.js +3 -4
  3. package/dist/control-exchange.js.map +1 -1
  4. package/dist/endpoint-anycast.d.ts +110 -0
  5. package/dist/endpoint-anycast.d.ts.map +1 -0
  6. package/dist/endpoint-anycast.js +316 -0
  7. package/dist/endpoint-anycast.js.map +1 -0
  8. package/dist/endpoint-consumers.d.ts +147 -0
  9. package/dist/endpoint-consumers.d.ts.map +1 -0
  10. package/dist/endpoint-consumers.js +459 -0
  11. package/dist/endpoint-consumers.js.map +1 -0
  12. package/dist/endpoint-dial.d.ts +3 -1
  13. package/dist/endpoint-dial.d.ts.map +1 -1
  14. package/dist/endpoint-dial.js +5 -2
  15. package/dist/endpoint-dial.js.map +1 -1
  16. package/dist/endpoint-durable.d.ts +82 -0
  17. package/dist/endpoint-durable.d.ts.map +1 -0
  18. package/dist/endpoint-durable.js +164 -0
  19. package/dist/endpoint-durable.js.map +1 -0
  20. package/dist/endpoint-principal-serve.d.ts +110 -0
  21. package/dist/endpoint-principal-serve.d.ts.map +1 -0
  22. package/dist/endpoint-principal-serve.js +233 -0
  23. package/dist/endpoint-principal-serve.js.map +1 -0
  24. package/dist/endpoint.d.ts +30 -189
  25. package/dist/endpoint.d.ts.map +1 -1
  26. package/dist/endpoint.js +201 -1089
  27. package/dist/endpoint.js.map +1 -1
  28. package/dist/launch-artifacts.d.ts.map +1 -1
  29. package/dist/launch-artifacts.js +17 -9
  30. package/dist/launch-artifacts.js.map +1 -1
  31. package/dist/websocket-dial.d.ts +9 -0
  32. package/dist/websocket-dial.d.ts.map +1 -0
  33. package/dist/websocket-dial.js +121 -0
  34. package/dist/websocket-dial.js.map +1 -0
  35. package/package.json +1 -1
@@ -0,0 +1,121 @@
1
+ /** How long a close waits for the peer to answer the websocket closing handshake. */
2
+ export const CLOSE_ANSWER_MS = 5_000;
3
+ function duration(ms) {
4
+ if (ms < 1_000)
5
+ return `${ms} ms`;
6
+ if (ms < 120_000)
7
+ return `${(ms / 1_000).toFixed(1)} s`;
8
+ if (ms < 7_200_000)
9
+ return `${Math.round(ms / 60_000)} min`;
10
+ return `${(ms / 3_600_000).toFixed(1)} h`;
11
+ }
12
+ /** The reason an error event carries, when the runtime gives one. */
13
+ function givenReason(event) {
14
+ const ev = event;
15
+ for (const text of [ev?.message, ev?.error?.message, ev?.error?.cause?.message])
16
+ if (typeof text === "string" && text !== "")
17
+ return text;
18
+ return "";
19
+ }
20
+ /**
21
+ * The socket every websocket dial hands nats-core, through its public `wsFactory` option. It wraps
22
+ * the runtime's WebSocket for two things nats-core's websocket transport does not do.
23
+ *
24
+ * It names a failure. The transport builds its `ConnectionError` from the error event's `message`,
25
+ * and Node's WebSocket leaves that empty for every cause (a refusal, a DNS failure, a connect
26
+ * timeout, a non-101 answer), so a failed dial was reported as a bare "ConnectionError". The events
27
+ * this socket passes on say which URL failed, whether the socket had opened, how long it had been
28
+ * trying or open, and any reason the runtime or the server gave.
29
+ *
30
+ * It bounds a close. The transport closes the socket and then waits for its close event: when
31
+ * nats-core's ping check finds the connection stale, on a requested reconnect, and on `close()`.
32
+ * When the peer is gone without a reset, as after a sleep that outlasted the far end's and the
33
+ * NAT's memory of the connection, nobody answers the closing handshake and the event never comes.
34
+ * The library then neither reconnects nor finishes closing, and an endpoint stop waits on it for
35
+ * good. After {@link CLOSE_ANSWER_MS} without that event this socket delivers an unclean close
36
+ * itself. The runtime's socket is left to finish on its own; the operating system ends it when its
37
+ * retransmissions give up.
38
+ *
39
+ * It carries only the members of a WebSocket that the transport uses.
40
+ */
41
+ class DialedWebSocket {
42
+ onopen = null;
43
+ onmessage = null;
44
+ onerror = null;
45
+ onclose = null;
46
+ socket;
47
+ /** The URL without credentials or query, for messages. */
48
+ target;
49
+ startedAt = Date.now();
50
+ openedAt;
51
+ closeTimer;
52
+ finished = false;
53
+ constructor(url) {
54
+ this.socket = new WebSocket(url);
55
+ const shown = new URL(url);
56
+ this.target = `${shown.protocol}//${shown.host}${shown.pathname}`;
57
+ this.socket.binaryType = "arraybuffer";
58
+ this.socket.onopen = (event) => {
59
+ this.openedAt = Date.now();
60
+ this.onopen?.(event);
61
+ };
62
+ this.socket.onmessage = (event) => this.onmessage?.(event);
63
+ this.socket.onerror = (event) => {
64
+ const reason = givenReason(event) || "the runtime gave no reason";
65
+ const message = this.openedAt === undefined
66
+ ? `could not open a websocket to ${this.target}: it failed after ${duration(Date.now() - this.startedAt)}; ${reason}`
67
+ : `the websocket to ${this.target} failed after ${duration(Date.now() - this.openedAt)} open; ${reason}`;
68
+ this.onerror?.({ type: "error", message, error: event.error });
69
+ };
70
+ this.socket.onclose = (event) => {
71
+ const { wasClean, code, reason } = event;
72
+ if (wasClean) {
73
+ this.finish({ wasClean, code, reason });
74
+ return;
75
+ }
76
+ // The transport turns an unclean close into an error only when it has a reason, and with no
77
+ // error it first waits for the send buffer to drain on a socket that has already closed.
78
+ const when = this.openedAt === undefined ? "before it opened" : `after ${duration(Date.now() - this.openedAt)} open`;
79
+ this.finish({ wasClean, code, reason: `the websocket to ${this.target} closed with code ${code} ${when}${reason === "" ? "" : `: ${reason}`}` });
80
+ };
81
+ }
82
+ get binaryType() {
83
+ return this.socket.binaryType;
84
+ }
85
+ set binaryType(type) {
86
+ this.socket.binaryType = type;
87
+ }
88
+ get bufferedAmount() {
89
+ return this.socket.bufferedAmount;
90
+ }
91
+ send(data) {
92
+ this.socket.send(data);
93
+ }
94
+ close() {
95
+ this.socket.close();
96
+ if (this.finished || this.closeTimer !== undefined)
97
+ return;
98
+ this.closeTimer = setTimeout(() => this.finish({
99
+ wasClean: false,
100
+ code: 1006,
101
+ reason: `the websocket to ${this.target} did not answer its closing handshake within ${duration(CLOSE_ANSWER_MS)}`,
102
+ }), CLOSE_ANSWER_MS);
103
+ this.closeTimer.unref?.();
104
+ }
105
+ /** Deliver one close to the transport: the runtime's, or the one {@link close} supplies when the
106
+ * runtime's does not come. A later one is dropped. */
107
+ finish(event) {
108
+ if (this.finished)
109
+ return;
110
+ this.finished = true;
111
+ clearTimeout(this.closeTimer);
112
+ this.closeTimer = undefined;
113
+ this.onclose?.({ type: "close", ...event });
114
+ }
115
+ }
116
+ /** nats-core's `wsFactory` for a websocket dial: `url` is the server address as listed. nats-core
117
+ * passes its connection options as a second argument, which this does not read. */
118
+ export function dialWebSocket(url) {
119
+ return Promise.resolve({ socket: new DialedWebSocket(url), encrypted: url.startsWith("wss://") });
120
+ }
121
+ //# sourceMappingURL=websocket-dial.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"websocket-dial.js","sourceRoot":"","sources":["../src/websocket-dial.ts"],"names":[],"mappings":"AAAA,qFAAqF;AACrF,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,CAAC;AAKrC,SAAS,QAAQ,CAAC,EAAU;IAC1B,IAAI,EAAE,GAAG,KAAK;QAAE,OAAO,GAAG,EAAE,KAAK,CAAC;IAClC,IAAI,EAAE,GAAG,OAAO;QAAE,OAAO,GAAG,CAAC,EAAE,GAAG,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;IACxD,IAAI,EAAE,GAAG,SAAS;QAAE,OAAO,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,GAAG,MAAM,CAAC,MAAM,CAAC;IAC5D,OAAO,GAAG,CAAC,EAAE,GAAG,SAAS,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC;AAC5C,CAAC;AAED,qEAAqE;AACrE,SAAS,WAAW,CAAC,KAAc;IACjC,MAAM,EAAE,GAAG,KAAwG,CAAC;IACpH,KAAK,MAAM,IAAI,IAAI,CAAC,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,CAAC;QAC7E,IAAI,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AAO,CAAC,EAAE,MAAM,EAAE,IAAI,eAAe,CAAC,GAAG,CAAyB,EAAE,SAAS,EAAE,GAAG,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;AAC5H,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cotal-ai/core",
3
3
  "description": "The Cotal protocol: endpoint, subjects, message types, the NATS client layer, and the extension contracts.",
4
- "version": "0.171.4",
4
+ "version": "0.171.5",
5
5
  "license": "Apache-2.0",
6
6
  "keywords": [
7
7
  "cotal",