@ultimat3/realtime 31.47.1 → 31.49.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/CLAUDE.md +2 -1
- package/package.json +5 -5
- package/src/channel-authz.ts +2 -13
- package/src/channel-decl.ts +2 -2
- package/src/channel-gaps.ts +19 -1
- package/src/channel-logs.ts +28 -5
- package/src/channel-records.ts +6 -3
- package/src/channel-render.ts +2 -2
- package/src/channel-ring.ts +7 -0
- package/src/channel-tenant.ts +43 -4
- package/src/channel.ts +8 -1
- package/src/fanout.ts +32 -2
- package/src/policy-gate.ts +37 -2
- package/src/socket.ts +7 -23
- package/src/sync-listen.ts +3 -1
package/CLAUDE.md
CHANGED
|
@@ -172,7 +172,8 @@ A rule marked `†` has its full text in [`docs/history/realtime-rules.md`](../.
|
|
|
172
172
|
- **A channel is a DECLARATION, no primitive** (docs/history/primitive-factories.md):
|
|
173
173
|
`channel(name, { params, policy, catchUp, records?, events? })`, deny by default; `policy`
|
|
174
174
|
REQUIRED (`X_CHANNEL_DECLARATION_INVALID`; public: `allow('public')`). Presence: `events`.
|
|
175
|
-
- **A
|
|
175
|
+
- **A `$tenantColumn` param is the subscriber's org; elsewhere a tenanted row reaches only its
|
|
176
|
+
tenant** (`channel-tenant.ts`). †
|
|
176
177
|
- **A guard that FAILS on re-auth SUSPENDS the seat** (`guardFailures`): kept, silent until a pass.
|
|
177
178
|
A `null` actor on a `row` channel and a tenancy refusal are DENIALS.
|
|
178
179
|
- **A seat is decided for the actor ON the socket when the guard resolves** (`#settle`); a denied
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/realtime",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.49.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.49.0",
|
|
56
|
+
"@ultimat3/entity": "31.49.0",
|
|
57
|
+
"@ultimat3/policy": "31.49.0",
|
|
58
|
+
"@ultimat3/query": "31.49.0",
|
|
59
59
|
"nats": "2.29.3"
|
|
60
60
|
}
|
|
61
61
|
}
|
package/src/channel-authz.ts
CHANGED
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
// with the params as input and the row the channel's own loader answered. Through
|
|
3
3
|
// `@ultimat3/query`'s `guardQuery`, the package's one authz seam — the call `policy-gate.ts` makes.
|
|
4
4
|
|
|
5
|
-
import { type Actor, type Ctx,
|
|
5
|
+
import { type Actor, type Ctx, runWithContext } from '@ultimat3/core';
|
|
6
6
|
import { guardQuery, QueryDeniedError } from '@ultimat3/query';
|
|
7
7
|
import type { Channel } from './channel-decl';
|
|
8
8
|
import { assertChannelTenant } from './channel-tenant';
|
|
9
9
|
import { isTenancyDenial, TopicForbiddenError } from './errors';
|
|
10
|
+
import { subscriberContext } from './policy-gate';
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* A denial is `X_TOPIC_FORBIDDEN`. A loader or a rule that RAISED is not a denial and leaves as it
|
|
@@ -74,15 +75,3 @@ function decide(
|
|
|
74
75
|
});
|
|
75
76
|
}
|
|
76
77
|
}
|
|
77
|
-
|
|
78
|
-
/** The node's context, re-made for one subscriber: same deploy, role and clock, their actor. */
|
|
79
|
-
function subscriberContext(node: Ctx, actor: Actor): Ctx {
|
|
80
|
-
return ctxOf({
|
|
81
|
-
actor,
|
|
82
|
-
role: node.role,
|
|
83
|
-
buildId: node.buildId,
|
|
84
|
-
clock: node.clock,
|
|
85
|
-
locale: node.locale,
|
|
86
|
-
tz: node.tz,
|
|
87
|
-
});
|
|
88
|
-
}
|
package/src/channel-decl.ts
CHANGED
|
@@ -72,8 +72,8 @@ export interface ChannelServerInit {
|
|
|
72
72
|
* Evaluated on subscribe: the params are the input, and `row` is what the loader below answered
|
|
73
73
|
* (`null` without one). REQUIRED, as on an `action` and a `query`: it was optional, and an omitted
|
|
74
74
|
* one let any socket — anonymous included — join with any param and receive every committed row.
|
|
75
|
-
* A public channel says so with `allow('public')`. Records are NOT gated per row
|
|
76
|
-
* params are
|
|
75
|
+
* A public channel says so with `allow('public')`. Records are NOT gated per row by the policy —
|
|
76
|
+
* the topic's params are its scope; only a row's TENANT is checked per member (`rowTenant`).
|
|
77
77
|
*/
|
|
78
78
|
readonly policy: QueryPolicy;
|
|
79
79
|
/**
|
package/src/channel-gaps.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// The repair half of a dropped `records` frame: the per-socket mark is `SyncSocket.gaps`, and this
|
|
2
2
|
// is what answers it — one `replay-gap` per (socket, topic), sent once the socket takes frames
|
|
3
|
-
// again, counted in `channel_replay_gaps_total` beside `channel_frames_dropped_total
|
|
3
|
+
// again, counted in `channel_replay_gaps_total` beside `channel_frames_dropped_total` (declared
|
|
4
|
+
// here too; `socket.ts` counts the drops).
|
|
4
5
|
|
|
5
6
|
import { type Counter, counter } from '@ultimat3/core';
|
|
6
7
|
import type { ReplayGapFrame } from './channel-wire';
|
|
@@ -16,6 +17,23 @@ const channelReplayGaps: Counter = counter('channel_replay_gaps_total', {
|
|
|
16
17
|
description: 'replay-gap frames delivered to a socket that lost a channel records frame',
|
|
17
18
|
});
|
|
18
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Channel frames this process dropped under backpressure. A dropped `records` frame is now marked
|
|
22
|
+
* and REPAIRED — the socket is sent `replay-gap` once it drains and re-reads the channel's catch-up
|
|
23
|
+
* query (`channel_replay_gaps_total` counts those) — while a dropped `events` frame is ephemeral
|
|
24
|
+
* by definition and is only counted. The two series side by side are the whole delivery story.
|
|
25
|
+
*
|
|
26
|
+
* Declared here rather than in `@ultimat3/core`'s `runtime-metrics.ts` because that file is the
|
|
27
|
+
* series EVERY Ultimate process emits and the deploy chart scales on; this one exists only where
|
|
28
|
+
* channels do. **No attributes**: a topic is client-chosen (`topic()` admits any
|
|
29
|
+
* `[A-Za-z0-9_-]+` segment), so a per-topic label is an unbounded series count one socket can mint
|
|
30
|
+
* — the topic goes in the log line, where cardinality is somebody else's index.
|
|
31
|
+
*/
|
|
32
|
+
export const channelFramesDropped: Counter = counter('channel_frames_dropped_total', {
|
|
33
|
+
unit: '{frame}',
|
|
34
|
+
description: 'Channel frames dropped by socket backpressure',
|
|
35
|
+
});
|
|
36
|
+
|
|
19
37
|
export class GapRepairs {
|
|
20
38
|
#announced = 0;
|
|
21
39
|
|
package/src/channel-logs.ts
CHANGED
|
@@ -7,7 +7,8 @@ import type { ChangeEvent } from './changefeed';
|
|
|
7
7
|
import type { Channel } from './channel-decl';
|
|
8
8
|
import { carriesTable, updatesFor } from './channel-records';
|
|
9
9
|
import { renderRecords } from './channel-render';
|
|
10
|
-
import { ChannelRing } from './channel-ring';
|
|
10
|
+
import { ChannelRing, type RecordsEntry } from './channel-ring';
|
|
11
|
+
import { type Audience, admitsTenant, rowTenant } from './channel-tenant';
|
|
11
12
|
import type { ChannelSince, ReplayGapFrame } from './channel-wire';
|
|
12
13
|
import type { SocketRegistry, SyncSocket } from './socket';
|
|
13
14
|
import { PROTOCOL_VERSION } from './sync-protocol';
|
|
@@ -79,9 +80,19 @@ export class ChannelLogs {
|
|
|
79
80
|
frames += this.#reopen(update.topic, open.target);
|
|
80
81
|
continue;
|
|
81
82
|
}
|
|
82
|
-
const
|
|
83
|
-
const
|
|
84
|
-
|
|
83
|
+
const tenant = rowTenant(update.channel, change.table, update.row);
|
|
84
|
+
const write = change.write ?? undefined;
|
|
85
|
+
const entry = open.ring.append(update.adopt, update.remove, write, tenant);
|
|
86
|
+
const epoch = open.ring.epoch;
|
|
87
|
+
const frame = renderRecords(update.topic, epoch, entry);
|
|
88
|
+
const audience: Audience | undefined =
|
|
89
|
+
tenant === undefined
|
|
90
|
+
? undefined
|
|
91
|
+
: {
|
|
92
|
+
admits: (actor) => admitsTenant(actor, tenant),
|
|
93
|
+
withheld: renderRecords(update.topic, epoch, withheld(entry)),
|
|
94
|
+
};
|
|
95
|
+
frames += this.#sockets.deliverRecords(update.topic, epoch, frame, audience);
|
|
85
96
|
}
|
|
86
97
|
return frames;
|
|
87
98
|
}
|
|
@@ -127,6 +138,9 @@ export class ChannelLogs {
|
|
|
127
138
|
* A `since` that is already CURRENT is answered too: the frame AT its cursor, carrying nothing.
|
|
128
139
|
* Sent nothing, the client had no frame to leave `joining` on — a channel that was up to date
|
|
129
140
|
* read as never joined. The seq is one it already holds, so its cursor does not move.
|
|
141
|
+
*
|
|
142
|
+
* A replayed entry holding another tenant's rows (`RecordsEntry.tenant`) is sent as its seq alone,
|
|
143
|
+
* exactly as the live frame was: a later joiner is never shown what a live member was not.
|
|
130
144
|
*/
|
|
131
145
|
resume(socket: SyncSocket, topic: string, since: ChannelSince | undefined, fresh: boolean): void {
|
|
132
146
|
const open = this.#byTopic.get(topic);
|
|
@@ -146,10 +160,19 @@ export class ChannelLogs {
|
|
|
146
160
|
}
|
|
147
161
|
const answer = entries.length > 0 ? entries : [{ seq: open.ring.seq, adopt: [], remove: [] }];
|
|
148
162
|
for (const entry of answer) {
|
|
149
|
-
|
|
163
|
+
const shown = admitsTenant(socket.actor, entry.tenant) ? entry : withheld(entry);
|
|
164
|
+
if (!socket.send(renderRecords(topic, open.ring.epoch, shown))) {
|
|
150
165
|
socket.gaps.set(topic, open.ring.epoch);
|
|
151
166
|
return;
|
|
152
167
|
}
|
|
153
168
|
}
|
|
154
169
|
}
|
|
155
170
|
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* One entry as a member outside its tenant receives it: the seq, so the cursor stays contiguous and
|
|
174
|
+
* no `replay-gap` is owed, and nothing else — no row, no key, and no write id to match an echo on.
|
|
175
|
+
*/
|
|
176
|
+
function withheld(entry: RecordsEntry): RecordsEntry {
|
|
177
|
+
return { seq: entry.seq, adopt: [], remove: [] };
|
|
178
|
+
}
|
package/src/channel-records.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// A committed change → the record updates each declared channel owes its subscribers. Pure: no
|
|
2
|
-
// socket, no seq, no policy — `ChannelHub` does those per node and per socket. The only
|
|
3
|
-
// of "which channel carries this row" lives here, so a write never names a channel
|
|
2
|
+
// socket, no seq, no policy, no tenant — `ChannelHub` does those per node and per socket. The only
|
|
3
|
+
// derivation of "which channel carries this row" lives here, so a write never names a channel.
|
|
4
4
|
|
|
5
5
|
import { type Counter, counter, logger } from '@ultimat3/core';
|
|
6
6
|
import type { Row } from '@ultimat3/core/page';
|
|
@@ -9,7 +9,10 @@ import type { ChangeEvent } from './changefeed';
|
|
|
9
9
|
import type { Channel, Topic } from './channel-decl';
|
|
10
10
|
import type { RecordPart } from './channel-ring';
|
|
11
11
|
|
|
12
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* One topic's share of one change. `row` is the image it came from: no policy re-decides it, but
|
|
14
|
+
* its tenant decides which of the topic's members are shown it (`rowTenant`, `channel-tenant.ts`).
|
|
15
|
+
*/
|
|
13
16
|
export interface TopicUpdate {
|
|
14
17
|
readonly channel: Channel;
|
|
15
18
|
readonly topic: Topic;
|
package/src/channel-render.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
// One ring entry → the `records` frame
|
|
2
|
-
//
|
|
1
|
+
// One ring entry → the `records` frame its topic's members receive. The same frame for all of them,
|
|
2
|
+
// but for an entry holding another tenant's rows (`RecordsEntry.tenant`): that member gets its seq.
|
|
3
3
|
|
|
4
4
|
import type { Row } from '@ultimat3/core/page';
|
|
5
5
|
import type { RecordsEntry } from './channel-ring';
|
package/src/channel-ring.ts
CHANGED
|
@@ -12,6 +12,11 @@ export interface RecordsEntry {
|
|
|
12
12
|
readonly remove: readonly { readonly type: string; readonly key: string }[];
|
|
13
13
|
/** The write that produced the change (`ChangeEvent.write`), kept so a replay still names it. */
|
|
14
14
|
readonly write?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Present when the rows are one tenant's on a topic that is not that tenant's (`rowTenant`): only
|
|
17
|
+
* its members are shown them, live or replayed. `null`: the row named no tenant — nobody is.
|
|
18
|
+
*/
|
|
19
|
+
readonly tenant?: string | null;
|
|
15
20
|
}
|
|
16
21
|
|
|
17
22
|
export interface RecordPart {
|
|
@@ -48,6 +53,7 @@ export class ChannelRing {
|
|
|
48
53
|
adopt: readonly RecordPart[],
|
|
49
54
|
remove: RecordsEntry['remove'],
|
|
50
55
|
write?: string,
|
|
56
|
+
tenant?: string | null,
|
|
51
57
|
): RecordsEntry {
|
|
52
58
|
this.#seq += 1;
|
|
53
59
|
const entry: RecordsEntry = {
|
|
@@ -55,6 +61,7 @@ export class ChannelRing {
|
|
|
55
61
|
adopt,
|
|
56
62
|
remove,
|
|
57
63
|
...(write === undefined ? {} : { write }),
|
|
64
|
+
...(tenant === undefined ? {} : { tenant }),
|
|
58
65
|
};
|
|
59
66
|
this.#entries.push(entry);
|
|
60
67
|
if (this.#entries.length > this.#capacity) this.#entries.shift();
|
package/src/channel-tenant.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
// The tenant half of a channel
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// The tenant half of a channel: a param that names a records entity's tenant column must be the
|
|
2
|
+
// subscriber's own org, decided at subscribe by `@ultimat3/entity`'s own guard (`assertScoped`); a
|
|
3
|
+
// tenanted row on a topic NOT keyed by its tenant column reaches only that tenant's members.
|
|
4
4
|
|
|
5
5
|
import { type Actor, type Ctx, isUltimateError, runWithContext } from '@ultimat3/core';
|
|
6
6
|
import { assertScoped, type QueryPlan } from '@ultimat3/entity';
|
|
7
|
-
import { tenantColumnOfTable } from './change-tenant';
|
|
7
|
+
import { tenantColumnOfTable, tenantIn } from './change-tenant';
|
|
8
8
|
import type { Channel } from './channel-decl';
|
|
9
|
+
import type { ChannelRecordsFrame } from './channel-wire';
|
|
9
10
|
import { isTenancyDenial, TopicForbiddenError } from './errors';
|
|
10
11
|
|
|
11
12
|
/**
|
|
@@ -53,3 +54,41 @@ export function assertChannelTenant(
|
|
|
53
54
|
}
|
|
54
55
|
}
|
|
55
56
|
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Who may read one change's rows on `channel`: `undefined` for everyone on the topic — an untenanted
|
|
60
|
+
* table, or a channel keyed by the tenant column, whose subscribe already proved the tenant
|
|
61
|
+
* (`assertChannelTenant`) — else the tenant `row` names, `null` when it names none (no member).
|
|
62
|
+
*
|
|
63
|
+
* A topic keyed by `roomId` carries every org's rows that name that room, and its policy decided a
|
|
64
|
+
* permission, not a tenant: `can('message:read')` admitted org A to org B's room. The subscribe
|
|
65
|
+
* cannot decide this (the rows are not known yet), so delivery and ring replay do, per row.
|
|
66
|
+
*/
|
|
67
|
+
export function rowTenant(
|
|
68
|
+
channel: Channel,
|
|
69
|
+
table: string,
|
|
70
|
+
row: Readonly<Record<string, unknown>>,
|
|
71
|
+
): string | null | undefined {
|
|
72
|
+
const column = tenantColumnOfTable(table);
|
|
73
|
+
if (column === null || channel.params.includes(column)) return undefined;
|
|
74
|
+
return tenantIn(column, row);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* `assertScoped`'s verdict for a plan naming `tenant`, without a context to run it in: the actor's
|
|
79
|
+
* own org, and nothing else. A `crossTenant()` scope is never open around a delivery — it is a
|
|
80
|
+
* change, not a call — so there is no proof of one to honour here.
|
|
81
|
+
*/
|
|
82
|
+
export function admitsTenant(actor: Actor | null, tenant: string | null | undefined): boolean {
|
|
83
|
+
if (tenant === undefined) return true;
|
|
84
|
+
return tenant !== null && actor !== null && actor.orgId !== undefined && actor.orgId === tenant;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* A records frame's audience when it carries one tenant's rows (`rowTenant`): a member `admits`
|
|
89
|
+
* takes gets the frame, every other member `withheld` — the same seq, carrying nothing.
|
|
90
|
+
*/
|
|
91
|
+
export interface Audience {
|
|
92
|
+
readonly admits: (actor: Actor | null) => boolean;
|
|
93
|
+
readonly withheld: ChannelRecordsFrame;
|
|
94
|
+
}
|
package/src/channel.ts
CHANGED
|
@@ -449,7 +449,14 @@ export class ChannelHub {
|
|
|
449
449
|
bridge.sub ??= this.#transport.subscribe(channelSubject(name), (payload) => {
|
|
450
450
|
this.#sockets.deliver(name, decode(payload));
|
|
451
451
|
});
|
|
452
|
-
|
|
452
|
+
const opening = bridge.sub;
|
|
453
|
+
try {
|
|
454
|
+
await opening;
|
|
455
|
+
} catch (error) {
|
|
456
|
+
// Forgotten once answered: kept, it refused every later subscriber while any seat held it.
|
|
457
|
+
if (bridge.sub === opening) bridge.sub = null;
|
|
458
|
+
throw error;
|
|
459
|
+
}
|
|
453
460
|
// The hub shut down while the transport was answering. `close()` either never saw this bridge
|
|
454
461
|
// or saw it with nothing to close, so this is the last reference to the subscription: it closes
|
|
455
462
|
// here or never. The entry goes with it, so a second post-close subscribe opens and closes its
|
package/src/fanout.ts
CHANGED
|
@@ -69,17 +69,34 @@ export interface InProcessTransportOptions {
|
|
|
69
69
|
class InProcessSet implements TransportSet {
|
|
70
70
|
readonly #keys = new Map<string, Map<string, TransportSetEntry>>();
|
|
71
71
|
readonly #clock: Clock;
|
|
72
|
+
#prunedAt = 0;
|
|
72
73
|
|
|
73
74
|
constructor(clock: Clock) {
|
|
74
75
|
this.#clock = clock;
|
|
75
76
|
}
|
|
76
77
|
|
|
77
78
|
async put(key: string, member: string, value: string, ttlMs: number): Promise<void> {
|
|
79
|
+
const now = this.#clock.now().getTime();
|
|
80
|
+
if (now >= this.#prunedAt + ttlMs) this.#prune(now);
|
|
78
81
|
const bucket = this.#keys.get(key) ?? new Map<string, TransportSetEntry>();
|
|
79
|
-
bucket.set(member, { member, value, expiresAt:
|
|
82
|
+
bucket.set(member, { member, value, expiresAt: now + ttlMs });
|
|
80
83
|
this.#keys.set(key, bucket);
|
|
81
84
|
}
|
|
82
85
|
|
|
86
|
+
/**
|
|
87
|
+
* Expiry on the store's own clock, as the KV's server does it (`nats-kv.ts`). Read-time expiry
|
|
88
|
+
* alone kept every key nobody reads again — a forgotten room's sweep lease, which must outlive
|
|
89
|
+
* the room by one TTL so a follower reads its last roster — for the life of the process. A full
|
|
90
|
+
* pass at most once per TTL, the interval `sweepAll` already walks every room on.
|
|
91
|
+
*/
|
|
92
|
+
#prune(now: number): void {
|
|
93
|
+
this.#prunedAt = now;
|
|
94
|
+
for (const [key, bucket] of this.#keys) {
|
|
95
|
+
for (const entry of bucket.values()) if (entry.expiresAt <= now) bucket.delete(entry.member);
|
|
96
|
+
if (bucket.size === 0) this.#keys.delete(key);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
83
100
|
async touch(key: string, member: string, ttlMs: number): Promise<boolean> {
|
|
84
101
|
const entry = this.#keys.get(key)?.get(member);
|
|
85
102
|
if (!entry || entry.expiresAt <= this.#clock.now().getTime()) return false;
|
|
@@ -104,18 +121,24 @@ class InProcessSet implements TransportSet {
|
|
|
104
121
|
}
|
|
105
122
|
return live;
|
|
106
123
|
}
|
|
124
|
+
|
|
125
|
+
get size(): number {
|
|
126
|
+
return this.#keys.size;
|
|
127
|
+
}
|
|
107
128
|
}
|
|
108
129
|
|
|
109
130
|
/** Single-node default: `x dev`, tests, and small deployments that have not earned a bus yet. */
|
|
110
131
|
export class InProcessTransport implements Transport {
|
|
111
132
|
readonly name = 'in-process';
|
|
112
133
|
readonly shared: TransportSet;
|
|
134
|
+
readonly #set: InProcessSet;
|
|
113
135
|
readonly #handlers = new Map<string, Set<TransportHandler>>();
|
|
114
136
|
readonly #onError: (error: unknown, subject: string) => void;
|
|
115
137
|
#closed = false;
|
|
116
138
|
|
|
117
139
|
constructor(options: InProcessTransportOptions = {}) {
|
|
118
|
-
this
|
|
140
|
+
this.#set = new InProcessSet(options.clock ?? systemClock);
|
|
141
|
+
this.shared = this.#set;
|
|
119
142
|
this.#onError = options.onError ?? (() => undefined);
|
|
120
143
|
}
|
|
121
144
|
|
|
@@ -168,5 +191,12 @@ export class InProcessTransport implements Transport {
|
|
|
168
191
|
}
|
|
169
192
|
}
|
|
170
193
|
|
|
194
|
+
/**
|
|
195
|
+
* Keys `transport.shared` holds, emptied ones included: the in-process read a leak test needs. A
|
|
196
|
+
* module function, never a member — no caller of the public class has a use for it.
|
|
197
|
+
*/
|
|
198
|
+
export const inProcessKeyCount = (transport: InProcessTransport): number =>
|
|
199
|
+
transport.shared instanceof InProcessSet ? transport.shared.size : 0;
|
|
200
|
+
|
|
171
201
|
// The production bus lives in `nats-transport.ts`: it needs a socket, a codec and a JetStream
|
|
172
202
|
// client, none of which belong in the file that defines what a transport *is*.
|
package/src/policy-gate.ts
CHANGED
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
// policy is dropped, never sent. That is the rule from the live-query pipeline, implemented once.
|
|
7
7
|
// A *denial* only — a gate that could not reach a decision raises, because "denied" and "the
|
|
8
8
|
// database is down" are different facts and one of them has to page someone.
|
|
9
|
+
// Both gates decide in the SUBSCRIBER's context: `ctx.actor` is who asked, as it is over HTTP.
|
|
9
10
|
|
|
10
|
-
import type
|
|
11
|
+
import { type Actor, anonymousActor, type Ctx, ctxOf } from '@ultimat3/core';
|
|
11
12
|
import { guardQuery, QueryDeniedError, type QueryPolicy, type QuerySubject } from '@ultimat3/query';
|
|
12
13
|
import type { JsonValue, LiveRow } from './json';
|
|
13
14
|
|
|
@@ -60,5 +61,39 @@ function subjectOf(
|
|
|
60
61
|
input: unknown,
|
|
61
62
|
row: unknown,
|
|
62
63
|
): QuerySubject {
|
|
63
|
-
|
|
64
|
+
// Anonymous is the anonymous actor, as over HTTP — never the node's own, which may be a system one.
|
|
65
|
+
const ctx =
|
|
66
|
+
actor === null ? subscriberContext(options.ctx, anonymousActor()) : ctxFor(options.ctx, actor);
|
|
67
|
+
return { actor, input, row, ctx, query: options.query };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Keyed by the actor OBJECT a socket holds — a re-auth puts a new one there — so no other actor
|
|
72
|
+
* can be answered from it; a row gate runs per subscriber per row, and `ctxOf` builds services.
|
|
73
|
+
*/
|
|
74
|
+
const contexts = new WeakMap<Ctx, WeakMap<Actor, Ctx>>();
|
|
75
|
+
|
|
76
|
+
function ctxFor(node: Ctx, actor: Actor): Ctx {
|
|
77
|
+
let byActor = contexts.get(node);
|
|
78
|
+
if (byActor === undefined) {
|
|
79
|
+
byActor = new WeakMap();
|
|
80
|
+
contexts.set(node, byActor);
|
|
81
|
+
}
|
|
82
|
+
const known = byActor.get(actor);
|
|
83
|
+
if (known !== undefined) return known;
|
|
84
|
+
const made = subscriberContext(node, actor);
|
|
85
|
+
byActor.set(actor, made);
|
|
86
|
+
return made;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The node's context, re-made for one subscriber: same deploy, role and clock, their actor. */
|
|
90
|
+
export function subscriberContext(node: Ctx, actor: Actor): Ctx {
|
|
91
|
+
return ctxOf({
|
|
92
|
+
actor,
|
|
93
|
+
role: node.role,
|
|
94
|
+
buildId: node.buildId,
|
|
95
|
+
clock: node.clock,
|
|
96
|
+
locale: node.locale,
|
|
97
|
+
tz: node.tz,
|
|
98
|
+
});
|
|
64
99
|
}
|
package/src/socket.ts
CHANGED
|
@@ -10,15 +10,14 @@
|
|
|
10
10
|
import {
|
|
11
11
|
type Actor,
|
|
12
12
|
type Clock,
|
|
13
|
-
type Counter,
|
|
14
|
-
counter,
|
|
15
13
|
finiteOption,
|
|
16
14
|
logger,
|
|
17
15
|
recordConnection,
|
|
18
16
|
systemClock,
|
|
19
17
|
uuidV7,
|
|
20
18
|
} from '@ultimat3/core';
|
|
21
|
-
import { GapRepairs } from './channel-gaps';
|
|
19
|
+
import { channelFramesDropped, GapRepairs } from './channel-gaps';
|
|
20
|
+
import type { Audience } from './channel-tenant';
|
|
22
21
|
import type { ChannelRecordsFrame } from './channel-wire';
|
|
23
22
|
import { CLOSE } from './close-codes';
|
|
24
23
|
import { DropWindow } from './socket-drops';
|
|
@@ -79,23 +78,6 @@ import {
|
|
|
79
78
|
DEFAULT_MAX_FRAMES_PER_SECOND,
|
|
80
79
|
} from './socket-defaults';
|
|
81
80
|
|
|
82
|
-
/**
|
|
83
|
-
* Channel frames this process dropped under backpressure. A dropped `records` frame is now marked
|
|
84
|
-
* and REPAIRED — the socket is sent `replay-gap` once it drains and re-reads the channel's catch-up
|
|
85
|
-
* query (`channel_replay_gaps_total` counts those) — while a dropped `events` frame is ephemeral
|
|
86
|
-
* by definition and is only counted. The two series side by side are the whole delivery story.
|
|
87
|
-
*
|
|
88
|
-
* Declared here rather than in `@ultimat3/core`'s `runtime-metrics.ts` because that file is the
|
|
89
|
-
* series EVERY Ultimate process emits and the deploy chart scales on; this one exists only where
|
|
90
|
-
* channels do. **No attributes**: a topic is client-chosen (`topic()` admits any
|
|
91
|
-
* `[A-Za-z0-9_-]+` segment), so a per-topic label is an unbounded series count one socket can mint
|
|
92
|
-
* — the topic goes in the log line, where cardinality is somebody else's index.
|
|
93
|
-
*/
|
|
94
|
-
const channelFramesDropped: Counter = counter('channel_frames_dropped_total', {
|
|
95
|
-
unit: '{frame}',
|
|
96
|
-
description: 'Channel frames dropped by socket backpressure',
|
|
97
|
-
});
|
|
98
|
-
|
|
99
81
|
export function actorIdOf(actor: Actor | null): string | null {
|
|
100
82
|
return actor === null ? null : actor.id;
|
|
101
83
|
}
|
|
@@ -428,12 +410,14 @@ export class SocketRegistry {
|
|
|
428
410
|
|
|
429
411
|
/**
|
|
430
412
|
* A `records` delivery. An owed `replay-gap` goes out first; a socket that then drops the frame is
|
|
431
|
-
* marked for the next one. `deliver`'s twin, because only a records stream can be repaired.
|
|
413
|
+
* marked for the next one. `deliver`'s twin, because only a records stream can be repaired. A
|
|
414
|
+
* member `to` does not admit is sent `to.withheld` instead (`channel-tenant.ts`).
|
|
432
415
|
*/
|
|
433
|
-
deliverRecords(topic: string, epoch: string, frame: ChannelRecordsFrame): number {
|
|
416
|
+
deliverRecords(topic: string, epoch: string, frame: ChannelRecordsFrame, to?: Audience): number {
|
|
434
417
|
const members = this.#byTopic.get(topic);
|
|
435
418
|
if (!members) return 0;
|
|
436
419
|
const text = encode(frame);
|
|
420
|
+
const other = to === undefined ? text : encode(to.withheld);
|
|
437
421
|
let sent = 0;
|
|
438
422
|
let dropped = 0;
|
|
439
423
|
for (const socket of members) {
|
|
@@ -442,7 +426,7 @@ export class SocketRegistry {
|
|
|
442
426
|
continue;
|
|
443
427
|
}
|
|
444
428
|
this.gapRepairs.repair(socket, topic);
|
|
445
|
-
if (socket.sendEncoded(text)) {
|
|
429
|
+
if (socket.sendEncoded(to === undefined || to.admits(socket.actor) ? text : other)) {
|
|
446
430
|
sent += 1;
|
|
447
431
|
continue;
|
|
448
432
|
}
|
package/src/sync-listen.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// so the node itself stays testable with no server: this file is the only place realtime calls
|
|
3
3
|
// `Bun.serve`, and the only thing in the package that knows a port exists.
|
|
4
4
|
|
|
5
|
-
import { finiteOption, markListening, onShutdown } from '@ultimat3/core';
|
|
5
|
+
import { finiteOption, markListening, onShutdown, sharedPortOption } from '@ultimat3/core';
|
|
6
6
|
import type { SyncNode } from './sync-node';
|
|
7
7
|
|
|
8
8
|
export interface ListenOptions {
|
|
@@ -45,6 +45,8 @@ export function listenSyncNode(node: SyncNode, options: ListenOptions = {}): Syn
|
|
|
45
45
|
// Spread, not `hostname: options.hostname` — Bun reads an explicit `undefined` as a request
|
|
46
46
|
// and not as an absence, and the default it applies to a missing key is the one we want.
|
|
47
47
|
...(options.hostname === undefined ? {} : { hostname: options.hostname }),
|
|
48
|
+
// A supervised child on Linux shares the port with its siblings; anything else binds alone.
|
|
49
|
+
...sharedPortOption(process.env),
|
|
48
50
|
fetch: node.fetch,
|
|
49
51
|
websocket: node.websocket,
|
|
50
52
|
});
|