@ultimat3/realtime 31.38.0 → 31.39.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 +5 -0
- package/package.json +5 -5
- package/src/change-tenant.ts +28 -0
- package/src/live-replicator.ts +10 -3
- package/src/matcher-bridge.ts +26 -2
- package/src/pg-replication-row.ts +8 -5
- package/src/pg-replication.ts +1 -1
- package/src/query-window.ts +11 -5
- package/src/record-publisher.ts +10 -4
package/CLAUDE.md
CHANGED
|
@@ -111,6 +111,11 @@ A rule marked `†` has its full text in [`docs/history/realtime-rules.md`](../.
|
|
|
111
111
|
re-lookup (`live-definition-input.test.ts`). A cold window is staled only by a table it reads.
|
|
112
112
|
- **A WINDOW is per TENANT** (`windowId(queryHash, tenant)`, `live-tenant.ts`); the read runs as a
|
|
113
113
|
service actor carrying ONLY that org, never the subscriber. †
|
|
114
|
+
- **A change's tenant is its entity's `$tenantColumn`, on every producer** (`change-tenant.ts`),
|
|
115
|
+
never a property assumed to be `orgId`; a window lists its tenanted tables (`shape.tenantTables`)
|
|
116
|
+
and `canAffect` refuses a tenant-less insert/update on one for an org-scoped window; a
|
|
117
|
+
tenant-less DELETE (key-only image) passes, and `bridgeChange` keeps a delete only for an id
|
|
118
|
+
the window holds (`live-tenant-column.test.ts`, `tenant-less-delete.test.ts`).
|
|
114
119
|
- **A `sid` is CLIENT data: a subscription is keyed by `(socket, sid)`** (`subscription-book.ts`);
|
|
115
120
|
reusing one the same socket holds is `X_SUBSCRIPTION_ID_TAKEN`.
|
|
116
121
|
- **`rateLimit:` is spent once per NEW subscription** (`live-spend.ts`).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/realtime",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.39.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.39.0",
|
|
56
|
+
"@ultimat3/entity": "31.39.0",
|
|
57
|
+
"@ultimat3/policy": "31.39.0",
|
|
58
|
+
"@ultimat3/query": "31.39.0",
|
|
59
59
|
"nats": "2.29.3"
|
|
60
60
|
}
|
|
61
61
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Single responsibility: the tenant a change belongs to, read off the column its ENTITY declares
|
|
2
|
+
// (`$tenantColumn`), never off a property assumed to be called `orgId`. Every change producer —
|
|
3
|
+
// the write-ahead-log decoder, the in-process replicator, `recordPublisher` — and every live window
|
|
4
|
+
// ask here, so "which column is the tenant" has one answer: `@ultimat3/entity`'s.
|
|
5
|
+
|
|
6
|
+
import { entityForTable } from '@ultimat3/entity';
|
|
7
|
+
|
|
8
|
+
/** The tenant property of the entity stored in `table`, or `null` for an untenanted one. */
|
|
9
|
+
export const tenantColumnOfTable = (table: string): string | null =>
|
|
10
|
+
entityForTable(table)?.$tenantColumn ?? null;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The tenant `row` names in `column`, as the text an actor's `orgId` is. An integer tenant column
|
|
14
|
+
* decodes as a number and is spelled as its digits; anything else names no tenant — which, on a
|
|
15
|
+
* tenanted table, keeps the change out of every org-scoped window (`canAffect`).
|
|
16
|
+
*/
|
|
17
|
+
export function tenantIn(
|
|
18
|
+
column: string | null,
|
|
19
|
+
row: Readonly<Record<string, unknown>> | null,
|
|
20
|
+
): string | null {
|
|
21
|
+
if (column === null || row === null) return null;
|
|
22
|
+
const value = row[column];
|
|
23
|
+
if (typeof value === 'string') return value;
|
|
24
|
+
if (typeof value === 'bigint' || (typeof value === 'number' && Number.isSafeInteger(value))) {
|
|
25
|
+
return String(value);
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
package/src/live-replicator.ts
CHANGED
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
|
|
26
26
|
import type { EntityBulkEvent, EntityRowEvent } from '@ultimat3/core';
|
|
27
27
|
import { subscribe } from '@ultimat3/core';
|
|
28
|
+
import { tenantIn } from './change-tenant';
|
|
28
29
|
import type { ChangeEvent, ChangeOp } from './changefeed';
|
|
29
30
|
import type { LiveRow } from './json';
|
|
30
31
|
import type { LiveQueryRegistry } from './live-query';
|
|
@@ -44,7 +45,11 @@ export interface LiveReplicatorOptions {
|
|
|
44
45
|
deliverChange(change: ChangeEvent): unknown;
|
|
45
46
|
invalidate(entity?: string): unknown;
|
|
46
47
|
};
|
|
47
|
-
/**
|
|
48
|
+
/**
|
|
49
|
+
* @deprecated Ignored since the tenant-column security fix: a change's tenant is read off the
|
|
50
|
+
* column its entity declares (`$tenantColumn`), the one answer `entity(name, { tenant })` and
|
|
51
|
+
* `.tenant()` already give. Delete the option; it is removed in the next major.
|
|
52
|
+
*/
|
|
48
53
|
readonly tenantColumn?: string;
|
|
49
54
|
readonly onError?: (error: unknown) => void;
|
|
50
55
|
}
|
|
@@ -83,7 +88,6 @@ export async function startLiveReplicator(options: LiveReplicatorOptions): Promi
|
|
|
83
88
|
// no write between the call and the subscribe can slip past unobserved.
|
|
84
89
|
const entity = await import('@ultimat3/entity');
|
|
85
90
|
const { registry } = options;
|
|
86
|
-
const tenant = options.tenantColumn ?? 'orgId';
|
|
87
91
|
let position = 0;
|
|
88
92
|
let delivered = 0;
|
|
89
93
|
// One promise chain, because ORDERING is the guarantee the whole pipeline is built on — the same
|
|
@@ -99,6 +103,9 @@ export async function startLiveReplicator(options: LiveReplicatorOptions): Promi
|
|
|
99
103
|
* passed through, such an entity reached no channel, no window and no bulk re-read here.
|
|
100
104
|
*/
|
|
101
105
|
const tableOf = (name: string): string => entity.getEntity(name)?.tableName ?? name;
|
|
106
|
+
/** The tenant property the entity DECLARES — never assumed to be `orgId` (`change-tenant.ts`). */
|
|
107
|
+
const tenantColumnOf = (name: string): string | null =>
|
|
108
|
+
entity.getEntity(name)?.core?.$tenantColumn ?? null;
|
|
102
109
|
|
|
103
110
|
const enqueue = (work: () => Promise<void>): void => {
|
|
104
111
|
tail = tail.then(work).catch((error: unknown) => {
|
|
@@ -114,7 +121,7 @@ export async function startLiveReplicator(options: LiveReplicatorOptions): Promi
|
|
|
114
121
|
position += 1;
|
|
115
122
|
const at = position;
|
|
116
123
|
const row = change.after ?? change.before;
|
|
117
|
-
const orgId =
|
|
124
|
+
const orgId = tenantIn(tenantColumnOf(change.entity), row ?? null);
|
|
118
125
|
const event: ChangeEvent = {
|
|
119
126
|
table: tableOf(change.entity),
|
|
120
127
|
// A repository's three row ops are three of the feed's four; a truncate never comes this way.
|
package/src/matcher-bridge.ts
CHANGED
|
@@ -22,6 +22,12 @@ export interface SubscriptionShape {
|
|
|
22
22
|
readonly orgId: string | null;
|
|
23
23
|
/** Read set. An update touching none of these columns cannot change the result. */
|
|
24
24
|
readonly columns?: readonly string[];
|
|
25
|
+
/**
|
|
26
|
+
* The tables of `entities` whose entity declares a tenant column. A change to one that carries
|
|
27
|
+
* no tenant is refused by a window scoped to an org: its rows belong to SOME tenant, and which
|
|
28
|
+
* one is unknown. Every window the registry builds names them (`queryEntry`).
|
|
29
|
+
*/
|
|
30
|
+
readonly tenantTables?: readonly string[];
|
|
25
31
|
}
|
|
26
32
|
|
|
27
33
|
export interface BridgeResult {
|
|
@@ -44,7 +50,13 @@ export interface IncrementalMatcher {
|
|
|
44
50
|
/** The cheap pre-filter. Pure, and the only thing that runs for a change nobody subscribed to. */
|
|
45
51
|
export function canAffect(shape: SubscriptionShape, change: ChangeEvent): boolean {
|
|
46
52
|
if (!shape.entities.includes(change.table)) return false;
|
|
47
|
-
if (shape.orgId !== null &&
|
|
53
|
+
if (shape.orgId !== null && shape.orgId !== change.orgId) {
|
|
54
|
+
if (change.orgId !== null) return false;
|
|
55
|
+
// Fail closed: a tenanted table's row with no tenant is no evidence it is this org's. Except a
|
|
56
|
+
// DELETE — under REPLICA IDENTITY DEFAULT its old image is the key alone, so it never names a
|
|
57
|
+
// tenant — which `bridgeChange` turns into a patch only for an id this window already holds.
|
|
58
|
+
if (change.op !== 'delete' && shape.tenantTables?.includes(change.table)) return false;
|
|
59
|
+
}
|
|
48
60
|
if (shape.columns && change.op === 'update' && change.after !== null) {
|
|
49
61
|
const touched = Object.keys(changedColumns(change.before, change.after));
|
|
50
62
|
if (touched.length > 0 && !touched.some((column) => shape.columns?.includes(column)))
|
|
@@ -61,10 +73,22 @@ export function bridgeChange(
|
|
|
61
73
|
rows: readonly LiveRow[],
|
|
62
74
|
): BridgeResult | null {
|
|
63
75
|
if (!canAffect(shape, change)) return null;
|
|
64
|
-
const result = incremental.match(change, rows);
|
|
76
|
+
const result = heldDeletesOnly(incremental.match(change, rows), rows);
|
|
65
77
|
return result.patches.length === 0 && !result.refill ? null : result;
|
|
66
78
|
}
|
|
67
79
|
|
|
80
|
+
/**
|
|
81
|
+
* A delete names a row by id, and only a window holding that id has anything to remove. Asked here
|
|
82
|
+
* of EVERY matcher's answer, `patchFromChange`'s included, because a delete's tenant may be unknown
|
|
83
|
+
* (`canAffect`): a window holds its own tenant's rows alone, so another tenant's ids never leave.
|
|
84
|
+
*/
|
|
85
|
+
function heldDeletesOnly(result: BridgeResult, rows: readonly LiveRow[]): BridgeResult {
|
|
86
|
+
if (!result.patches.some((patch) => patch.op === 'delete')) return result;
|
|
87
|
+
const held = new Set(rows.map((row) => row.id));
|
|
88
|
+
const patches = result.patches.filter((patch) => patch.op !== 'delete' || held.has(patch.id));
|
|
89
|
+
return patches.length === result.patches.length ? result : { ...result, patches };
|
|
90
|
+
}
|
|
91
|
+
|
|
68
92
|
/** Default derivation, used by matchers that only answer "affected" without describing the delta. */
|
|
69
93
|
export function patchFromChange(change: ChangeEvent): RowPatch | null {
|
|
70
94
|
// A truncate names no row, so there is no patch — the window is re-read (`live-fanout.ts`).
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// becomes the `before`/`after` the matcher's predicates are written against, plus the properties
|
|
3
3
|
// `after` could not carry. Split out of `pg-replication.ts`, which decides ordering and nothing else.
|
|
4
4
|
|
|
5
|
+
import { tenantColumnOfTable, tenantIn } from './change-tenant';
|
|
5
6
|
import { ReplicationProtocolError } from './errors';
|
|
6
7
|
import { isRow, type LiveRow } from './json';
|
|
7
8
|
import { entityRow, omittedProperties } from './pg-entity-row';
|
|
@@ -75,8 +76,10 @@ function toRow(
|
|
|
75
76
|
});
|
|
76
77
|
}
|
|
77
78
|
|
|
78
|
-
/**
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
79
|
+
/**
|
|
80
|
+
* The tenant, hoisted out of the row so fanout filters without parsing it — from the column the
|
|
81
|
+
* table's entity declares (`$tenantColumn`). It read `row.orgId`, so a table declared
|
|
82
|
+
* `tenant: 'workspaceId'` replicated every change with no tenant, into every org's window.
|
|
83
|
+
*/
|
|
84
|
+
export const tenantOf = (table: string, row: LiveRow | null): string | null =>
|
|
85
|
+
tenantIn(tenantColumnOfTable(table), row);
|
package/src/pg-replication.ts
CHANGED
|
@@ -409,7 +409,7 @@ export class PgReplicationStream {
|
|
|
409
409
|
after,
|
|
410
410
|
lsn,
|
|
411
411
|
txid: transaction.xid.toString(10),
|
|
412
|
-
orgId: tenantOf(after ?? before),
|
|
412
|
+
orgId: tenantOf(relation.name, after ?? before),
|
|
413
413
|
at: transaction.commitAt,
|
|
414
414
|
write: transaction.write ?? null,
|
|
415
415
|
...(omitted.length === 0 ? {} : { omitted }),
|
package/src/query-window.ts
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// decision is never here — `live-query.ts` owns that, once per subscriber, over what this returns.
|
|
4
4
|
|
|
5
5
|
import type { LiveReadPhase } from '@ultimat3/query';
|
|
6
|
+
import { tenantColumnOfTable } from './change-tenant';
|
|
6
7
|
import { WindowReadTimeoutError } from './errors';
|
|
7
8
|
import type { JsonValue, LiveRow } from './json';
|
|
8
9
|
import type {
|
|
@@ -45,7 +46,8 @@ export interface QueryEntry {
|
|
|
45
46
|
readonly rowEntity: string | null;
|
|
46
47
|
/** The record key a row travels under; `null` = its `id` (a plain table). */
|
|
47
48
|
readonly rowKey: ((row: LiveRow) => string) | null;
|
|
48
|
-
|
|
49
|
+
/** `tenantTables` required here: a window the registry builds always names them. */
|
|
50
|
+
readonly shape: SubscriptionShape & { readonly tenantTables: readonly string[] };
|
|
49
51
|
readonly matcher: IncrementalMatcher;
|
|
50
52
|
readonly subscribers: Map<string, LiveSubscription>;
|
|
51
53
|
/**
|
|
@@ -107,6 +109,10 @@ export function queryEntry(
|
|
|
107
109
|
},
|
|
108
110
|
): QueryEntry {
|
|
109
111
|
const tenant = options?.tenant ?? null;
|
|
112
|
+
// The matcher knows the dependency set this *input* produced; `definition.entities` is the
|
|
113
|
+
// static declaration and can only be a superset of it. Preferring the matcher is what lets a
|
|
114
|
+
// definition built from a real query carry no static list at all.
|
|
115
|
+
const entities = matcher.entities.length > 0 ? matcher.entities : definition.entities;
|
|
110
116
|
// `stale` is written from four modules; an accessor is the one place every write passes, so the
|
|
111
117
|
// registry's owing set (`live-entries.ts`) is kept here rather than found by a walk per change.
|
|
112
118
|
let stale = false;
|
|
@@ -121,13 +127,13 @@ export function queryEntry(
|
|
|
121
127
|
...rowScope(definition, input, options?.prepared),
|
|
122
128
|
shape: {
|
|
123
129
|
qid,
|
|
124
|
-
|
|
125
|
-
// static declaration and can only be a superset of it. Preferring the matcher is what lets a
|
|
126
|
-
// definition built from a real query carry no static list at all.
|
|
127
|
-
entities: matcher.entities.length > 0 ? matcher.entities : definition.entities,
|
|
130
|
+
entities,
|
|
128
131
|
// The window's own tenant first: a change in another org is dropped before any predicate
|
|
129
132
|
// runs, whether or not the query's `where` happens to name one.
|
|
130
133
|
orgId: tenant ?? orgIdOf(input),
|
|
134
|
+
// Resolved once per window, so `canAffect` refuses a tenant-less change on these with a
|
|
135
|
+
// lookup in a short list rather than a registry walk per change.
|
|
136
|
+
tenantTables: entities.filter((table) => tenantColumnOfTable(table) !== null),
|
|
131
137
|
...(definition.columns ? { columns: definition.columns } : {}),
|
|
132
138
|
},
|
|
133
139
|
matcher,
|
package/src/record-publisher.ts
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
uuidV7,
|
|
13
13
|
} from '@ultimat3/core';
|
|
14
14
|
import { type RecordProjection, recordProjection } from '@ultimat3/entity/record';
|
|
15
|
+
import { tenantColumnOfTable, tenantIn } from './change-tenant';
|
|
15
16
|
import { type ChangeEvent, formatLsn } from './changefeed';
|
|
16
17
|
import type { ChannelEntity } from './channel-decl';
|
|
17
18
|
import type { Transport } from './fanout';
|
|
@@ -27,7 +28,11 @@ export interface RecordPublisherOptions {
|
|
|
27
28
|
* the one delivery, exactly as it is in production with no replicator.
|
|
28
29
|
*/
|
|
29
30
|
readonly entities: readonly ChannelEntity[];
|
|
30
|
-
/**
|
|
31
|
+
/**
|
|
32
|
+
* @deprecated Ignored since the tenant-column security fix: a change's tenant is read off the
|
|
33
|
+
* column its entity declares (`$tenantColumn`), as every replicator reads it. Delete the
|
|
34
|
+
* option; it is removed in the next major.
|
|
35
|
+
*/
|
|
31
36
|
readonly tenantColumn?: string;
|
|
32
37
|
readonly clock?: Clock;
|
|
33
38
|
}
|
|
@@ -73,7 +78,6 @@ export function isPublishedTable(table: string): boolean {
|
|
|
73
78
|
|
|
74
79
|
export function recordPublisher(options: RecordPublisherOptions): RecordPublisher {
|
|
75
80
|
const clock = options.clock ?? systemClock;
|
|
76
|
-
const tenant = options.tenantColumn ?? 'orgId';
|
|
77
81
|
const projections = new Map<string, RecordProjection>();
|
|
78
82
|
for (const declared of options.entities) {
|
|
79
83
|
const projection = recordProjection(declared);
|
|
@@ -112,7 +116,9 @@ export function recordPublisher(options: RecordPublisherOptions): RecordPublishe
|
|
|
112
116
|
) as LiveRow;
|
|
113
117
|
// Refused here, before any row of the call is sequenced: a keyless row matches no record.
|
|
114
118
|
projection.key(image);
|
|
115
|
-
|
|
119
|
+
// The entity's declared tenant column, as every producer reads it: a table declared
|
|
120
|
+
// `tenant: 'workspaceId'` published `orgId: null` and reached every org's channel subject.
|
|
121
|
+
const orgId = tenantIn(tenantColumnOfTable(projection.table), image);
|
|
116
122
|
return {
|
|
117
123
|
table: projection.table,
|
|
118
124
|
op: op === 'delete' ? 'delete' : 'update',
|
|
@@ -122,7 +128,7 @@ export function recordPublisher(options: RecordPublisherOptions): RecordPublishe
|
|
|
122
128
|
// live window is fed one.
|
|
123
129
|
lsn: '',
|
|
124
130
|
txid: producer,
|
|
125
|
-
orgId
|
|
131
|
+
orgId,
|
|
126
132
|
at: clock.now().getTime(),
|
|
127
133
|
write,
|
|
128
134
|
};
|