@ultimat3/realtime 31.29.0 → 31.31.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.
- package/package.json +5 -5
- package/src/nats-client.ts +3 -1
- package/src/nats-subscriptions.ts +51 -4
- package/src/nats-transport.ts +10 -19
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/realtime",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.31.0",
|
|
4
4
|
"description": "Three-tier realtime: channels, live queries, local-first sync — one protocol, one mutator shape",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -52,10 +52,10 @@
|
|
|
52
52
|
"test": "bun test"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@ultimat3/core": "31.
|
|
56
|
-
"@ultimat3/entity": "31.
|
|
57
|
-
"@ultimat3/policy": "31.
|
|
58
|
-
"@ultimat3/query": "31.
|
|
55
|
+
"@ultimat3/core": "31.31.0",
|
|
56
|
+
"@ultimat3/entity": "31.31.0",
|
|
57
|
+
"@ultimat3/policy": "31.31.0",
|
|
58
|
+
"@ultimat3/query": "31.31.0",
|
|
59
59
|
"nats": "2.29.3"
|
|
60
60
|
}
|
|
61
61
|
}
|
package/src/nats-client.ts
CHANGED
|
@@ -46,7 +46,9 @@ export interface NatsClient {
|
|
|
46
46
|
/**
|
|
47
47
|
* Answers once the server has processed every line sent before it — a `SUB` included. `subscribe`
|
|
48
48
|
* only WRITES that line: a message another connection publishes before the server reads it is
|
|
49
|
-
* not delivered, so "subscribed" means flushed.
|
|
49
|
+
* not delivered, so "subscribed" means flushed. An implementation BOUNDS it — settles, answered
|
|
50
|
+
* or refused, inside its own request timeout (`nats-lib-client.ts`): a silent server never sends
|
|
51
|
+
* the PONG, and a flush that never settles holds whatever awaits it.
|
|
50
52
|
*/
|
|
51
53
|
flush(): Promise<void>;
|
|
52
54
|
request(subject: string, payload: Uint8Array, options?: NatsRequestOptions): Promise<NatsMessage>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
// Single responsibility: the transport's
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
1
|
+
// Single responsibility: the transport's subscriptions — kept, so a client the library closed for
|
|
2
|
+
// good is replaced and every one bound again on the new one; and the reconnect told to listeners
|
|
3
|
+
// only once the SERVER holds what was re-sent (a peer's change in that gap was lost). The library's
|
|
4
|
+
// own reconnect re-establishes the SAME client's, so a re-bind can never double a delivery.
|
|
5
5
|
|
|
6
6
|
import type { NatsClient, NatsMessageHandler, NatsSubscription } from './nats-client';
|
|
7
7
|
|
|
@@ -61,3 +61,50 @@ export class NatsSubscriptions {
|
|
|
61
61
|
return failures;
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Answers once the server has read every line this client sent before it — a `SUB` included
|
|
67
|
+
* (`NatsClient.flush`). With no connection there is no server to ask: the client re-sends its SUBs
|
|
68
|
+
* on the way back, and that reconnect is announced in its turn. A flush that fails is that case.
|
|
69
|
+
*/
|
|
70
|
+
export async function untilServerHolds(client: NatsClient): Promise<void> {
|
|
71
|
+
if (client.connected) await client.flush().catch(() => undefined);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The listeners, and the one way to tell them: after `untilServerHolds`, each in its own `try`. */
|
|
75
|
+
export class NatsReconnectAnnouncer {
|
|
76
|
+
readonly #listeners = new Set<() => void>();
|
|
77
|
+
readonly #fault: (error: unknown) => void;
|
|
78
|
+
#closed = false;
|
|
79
|
+
|
|
80
|
+
/** `fault`: where a listener that throws is reported — it must not stop the others hearing. */
|
|
81
|
+
constructor(fault: (error: unknown) => void) {
|
|
82
|
+
this.#fault = fault;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
on(listener: () => void): () => void {
|
|
86
|
+
this.#listeners.add(listener);
|
|
87
|
+
return () => {
|
|
88
|
+
this.#listeners.delete(listener);
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The transport closed: an announcement whose flush is still out tells nobody. */
|
|
93
|
+
close(): void {
|
|
94
|
+
this.#closed = true;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Never rejects: the library calls this from its status loop, the re-dial from its own. */
|
|
98
|
+
async announce(client: NatsClient): Promise<void> {
|
|
99
|
+
await untilServerHolds(client);
|
|
100
|
+
// Asked AFTER the await: a close that landed while the flush was out is the answer.
|
|
101
|
+
if (this.#closed) return;
|
|
102
|
+
for (const listener of [...this.#listeners]) {
|
|
103
|
+
try {
|
|
104
|
+
listener();
|
|
105
|
+
} catch (error) {
|
|
106
|
+
this.#fault(error);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
package/src/nats-transport.ts
CHANGED
|
@@ -25,7 +25,7 @@ import { ensureKvBucket } from './nats-jetstream';
|
|
|
25
25
|
import { NatsKvSet } from './nats-kv';
|
|
26
26
|
import { openNatsClient } from './nats-open';
|
|
27
27
|
import { NatsReports } from './nats-reports';
|
|
28
|
-
import { NatsSubscriptions } from './nats-subscriptions';
|
|
28
|
+
import { NatsReconnectAnnouncer, NatsSubscriptions, untilServerHolds } from './nats-subscriptions';
|
|
29
29
|
import {
|
|
30
30
|
defaultBackoff,
|
|
31
31
|
policyDelay,
|
|
@@ -105,7 +105,7 @@ export class NatsTransport implements Transport {
|
|
|
105
105
|
/** Where background failures go: a fault every time, an outage thinned (`nats-reports.ts`). */
|
|
106
106
|
readonly #reports: NatsReports;
|
|
107
107
|
readonly #subscriptions = new NatsSubscriptions();
|
|
108
|
-
readonly #
|
|
108
|
+
readonly #announcer: NatsReconnectAnnouncer;
|
|
109
109
|
|
|
110
110
|
constructor(options: NatsTransportOptions) {
|
|
111
111
|
// Parsed here rather than at the first publish: a malformed NATS_URL is a boot-time fault, and
|
|
@@ -117,6 +117,7 @@ export class NatsTransport implements Transport {
|
|
|
117
117
|
onError: options.onError,
|
|
118
118
|
outageLevel: options.outageLevel ?? 'error',
|
|
119
119
|
});
|
|
120
|
+
this.#announcer = new NatsReconnectAnnouncer((error) => this.#reports.fault(error, this.name));
|
|
120
121
|
this.#connect = options.connect ?? openNatsClient;
|
|
121
122
|
this.#backoff = options.backoff ?? defaultBackoff;
|
|
122
123
|
this.#attempts = finiteOption(
|
|
@@ -222,7 +223,7 @@ export class NatsTransport implements Transport {
|
|
|
222
223
|
// writing the SUB and the server reading it was lost (`nats-black-hole.live.test.ts`, CI run
|
|
223
224
|
// 39145087176). Mid-reconnect there is no server to ask: the library re-sends the SUB on the
|
|
224
225
|
// way back, and `onReconnect` reports that gap. A flush that fails is that same case.
|
|
225
|
-
|
|
226
|
+
await untilServerHolds(client);
|
|
226
227
|
return { subject, unsubscribe: release };
|
|
227
228
|
}
|
|
228
229
|
|
|
@@ -233,14 +234,12 @@ export class NatsTransport implements Transport {
|
|
|
233
234
|
* the unsubscribe.
|
|
234
235
|
*/
|
|
235
236
|
onReconnect(listener: () => void): () => void {
|
|
236
|
-
this.#
|
|
237
|
-
return () => {
|
|
238
|
-
this.#reconnectListeners.delete(listener);
|
|
239
|
-
};
|
|
237
|
+
return this.#announcer.on(listener);
|
|
240
238
|
}
|
|
241
239
|
|
|
242
240
|
async close(): Promise<void> {
|
|
243
241
|
this.#closed = true;
|
|
242
|
+
this.#announcer.close();
|
|
244
243
|
this.#wakeRedial?.();
|
|
245
244
|
const client = this.#client;
|
|
246
245
|
this.#client = undefined;
|
|
@@ -393,7 +392,9 @@ export class NatsTransport implements Transport {
|
|
|
393
392
|
failure.subject,
|
|
394
393
|
);
|
|
395
394
|
}
|
|
396
|
-
|
|
395
|
+
// Announced once the server holds what `bindAll` re-sent (`nats-subscriptions.ts`), and
|
|
396
|
+
// never awaited: a flush that hangs would hold this memoised loop, and no loss re-dials.
|
|
397
|
+
void this.#announcer.announce(client);
|
|
397
398
|
} catch (error) {
|
|
398
399
|
if (this.#closed) return;
|
|
399
400
|
this.#lastFailure = renderThrowable(error);
|
|
@@ -451,17 +452,7 @@ export class NatsTransport implements Transport {
|
|
|
451
452
|
// A lazy bucket is forgotten instead, and asserted again by whoever next reads `shared`.
|
|
452
453
|
if (this.#lazyBucket) this.#bucketed.delete(client);
|
|
453
454
|
else void this.#ensureBucket(client).catch((error: unknown) => this.#reports.outage(error));
|
|
454
|
-
this.#announce();
|
|
455
|
-
}
|
|
456
|
-
|
|
457
|
-
#announce(): void {
|
|
458
|
-
for (const listener of [...this.#reconnectListeners]) {
|
|
459
|
-
try {
|
|
460
|
-
listener();
|
|
461
|
-
} catch (error) {
|
|
462
|
-
this.#reports.fault(error, this.name);
|
|
463
|
-
}
|
|
464
|
-
}
|
|
455
|
+
void this.#announcer.announce(client);
|
|
465
456
|
}
|
|
466
457
|
|
|
467
458
|
/**
|