@syncular/server 0.58.0 → 0.60.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/README.md +49 -36
- package/dist/admin.d.ts +32 -20
- package/dist/admin.js +46 -40
- package/dist/blob-handlers.d.ts +25 -31
- package/dist/blob-handlers.js +63 -41
- package/dist/blob-store.d.ts +42 -24
- package/dist/blob-store.js +0 -0
- package/dist/bucket-blob-store.d.ts +12 -8
- package/dist/bucket-blob-store.js +54 -45
- package/dist/bucket-segment-store.js +18 -16
- package/dist/content-encoding.d.ts +5 -2
- package/dist/content-encoding.js +42 -13
- package/dist/context.d.ts +19 -5
- package/dist/context.js +19 -1
- package/dist/d1-storage.d.ts +14 -98
- package/dist/d1-storage.js +249 -1119
- package/dist/errors.js +16 -0
- package/dist/events.d.ts +36 -18
- package/dist/frame-bytes.js +24 -6
- package/dist/handler.js +503 -208
- package/dist/local-authority.d.ts +2 -0
- package/dist/local-authority.js +24 -12
- package/dist/object-bucket.d.ts +2 -0
- package/dist/operations-realtime.js +69 -50
- package/dist/operations.d.ts +5 -2
- package/dist/operations.js +20 -29
- package/dist/pg-executor-pglite.d.ts +5 -6
- package/dist/pg-executor-pglite.js +22 -14
- package/dist/pg-executor.d.ts +4 -1
- package/dist/postgres-storage.d.ts +24 -19
- package/dist/postgres-storage.js +420 -262
- package/dist/pull.d.ts +70 -47
- package/dist/pull.js +270 -301
- package/dist/push.d.ts +12 -3
- package/dist/push.js +361 -173
- package/dist/realtime.d.ts +85 -39
- package/dist/realtime.js +355 -219
- package/dist/relational-rows.d.ts +21 -66
- package/dist/relational-rows.js +28 -109
- package/dist/s3-blob-store.d.ts +11 -12
- package/dist/s3-blob-store.js +20 -13
- package/dist/s3-object-bucket.d.ts +8 -1
- package/dist/s3-object-bucket.js +16 -3
- package/dist/schema.d.ts +14 -0
- package/dist/schema.js +23 -0
- package/dist/scopes.d.ts +6 -2
- package/dist/scopes.js +22 -4
- package/dist/seed.js +10 -16
- package/dist/segment-download.d.ts +4 -1
- package/dist/segment-download.js +22 -6
- package/dist/signed-url.d.ts +2 -2
- package/dist/signed-url.js +9 -2
- package/dist/sigv4.d.ts +8 -2
- package/dist/sigv4.js +8 -4
- package/dist/sqlite-blob-store.d.ts +1 -1
- package/dist/sqlite-blob-store.js +23 -22
- package/dist/sqlite-dialect.d.ts +164 -84
- package/dist/sqlite-dialect.js +1145 -150
- package/dist/sqlite-lease-store.js +13 -18
- package/dist/sqlite-storage.d.ts +9 -62
- package/dist/sqlite-storage.js +303 -1123
- package/dist/storage-query.d.ts +30 -1
- package/dist/storage-query.js +68 -0
- package/dist/storage.d.ts +185 -63
- package/dist/storage.js +15 -0
- package/package.json +2 -2
- package/src/admin.ts +99 -57
- package/src/blob-handlers.ts +91 -72
- package/src/blob-store.ts +0 -0
- package/src/bucket-blob-store.ts +75 -48
- package/src/bucket-segment-store.ts +20 -15
- package/src/content-encoding.ts +48 -16
- package/src/context.ts +33 -6
- package/src/d1-storage.ts +350 -1700
- package/src/errors.ts +16 -0
- package/src/events.ts +37 -18
- package/src/frame-bytes.ts +26 -6
- package/src/handler.ts +654 -261
- package/src/local-authority.ts +27 -14
- package/src/object-bucket.ts +2 -0
- package/src/operations-realtime.ts +85 -61
- package/src/operations.ts +44 -42
- package/src/pg-executor-pglite.ts +27 -23
- package/src/pg-executor.ts +5 -1
- package/src/postgres-storage.ts +557 -354
- package/src/pull.ts +406 -440
- package/src/push.ts +466 -220
- package/src/realtime.ts +447 -290
- package/src/relational-rows.ts +43 -121
- package/src/s3-blob-store.ts +23 -13
- package/src/s3-object-bucket.ts +20 -2
- package/src/schema.ts +31 -0
- package/src/scopes.ts +23 -3
- package/src/seed.ts +13 -20
- package/src/segment-download.ts +33 -7
- package/src/signed-url.ts +10 -4
- package/src/sigv4.ts +17 -4
- package/src/sqlite-blob-store.ts +27 -26
- package/src/sqlite-dialect.ts +1851 -199
- package/src/sqlite-lease-store.ts +12 -23
- package/src/sqlite-storage.ts +390 -1821
- package/src/storage-query.ts +102 -0
- package/src/storage.ts +215 -67
|
@@ -169,13 +169,13 @@ export declare function dropIndexDdl(indexName: string): string;
|
|
|
169
169
|
* (JSONB would reject the INSERT — the payload keeps the verbatim value);
|
|
170
170
|
* - everything else binds naturally (Uint8Array → BLOB/BYTEA).
|
|
171
171
|
*/
|
|
172
|
-
export declare function toSqlValue(column: RowColumn, value: RowValue, dialect: RelationalDialect):
|
|
172
|
+
export declare function toSqlValue(column: RowColumn, value: RowValue, dialect: RelationalDialect): RowValue;
|
|
173
173
|
/**
|
|
174
174
|
* Bind values for an upsert, in `tableColumnNames` order. Decodes the
|
|
175
175
|
* payload once; the typed columns and the verbatim payload land in one
|
|
176
176
|
* statement, so they cannot drift.
|
|
177
177
|
*/
|
|
178
|
-
export declare function upsertValues(table: CompiledTable, partition: string, row: StoredRow, dialect: RelationalDialect):
|
|
178
|
+
export declare function upsertValues(table: CompiledTable, partition: string, row: StoredRow, dialect: RelationalDialect): RowValue[];
|
|
179
179
|
/**
|
|
180
180
|
* Upsert statement text (bind with `upsertValues`). PostgreSQL takes `rows`
|
|
181
181
|
* rows in one statement, bound as consecutive `upsertValues` lists; their
|
|
@@ -190,14 +190,15 @@ export declare function upsertSql(table: CompiledTable, dialect: RelationalDiale
|
|
|
190
190
|
export declare function selectRowSql(table: CompiledTable, dialect: RelationalDialect): string;
|
|
191
191
|
/**
|
|
192
192
|
* SELECT many stored rows of one table by primary key in one statement.
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
* rowIds]; absent ids contribute no
|
|
193
|
+
* The ids bind as one parameter (Postgres `text[]` with `= ANY($2)`, SQLite
|
|
194
|
+
* a JSON array through `json_each`), so the statement count does not grow
|
|
195
|
+
* with the id count. Params: [partition, rowIds]; absent ids contribute no
|
|
196
|
+
* row.
|
|
196
197
|
*/
|
|
197
|
-
export declare function selectRowsSql(table: CompiledTable): string;
|
|
198
|
+
export declare function selectRowsSql(table: CompiledTable, dialect: RelationalDialect): string;
|
|
198
199
|
export interface IndexRowPageStatement {
|
|
199
200
|
readonly sql: string;
|
|
200
|
-
readonly params: readonly
|
|
201
|
+
readonly params: readonly RowValue[];
|
|
201
202
|
}
|
|
202
203
|
/**
|
|
203
204
|
* Bounded exact lookup through one declared relational index. Unlike
|
|
@@ -222,39 +223,13 @@ export declare function indexRowPageStatement(table: CompiledTable, index: Index
|
|
|
222
223
|
* Bind order:
|
|
223
224
|
* - postgres: [partition, tbl, var, ...values, afterRowId, limit]
|
|
224
225
|
* (the join reuses `$1` for the partition);
|
|
225
|
-
* - sqlite: [partition, tbl, var,
|
|
226
|
-
* partition] (positional
|
|
226
|
+
* - sqlite: [partition, tbl, var, values as JSON array text, afterRowId,
|
|
227
|
+
* limit, partition] (positional `?`; the partition binds again for the
|
|
228
|
+
* join). The values expand through `json_each`, so the statement text and
|
|
229
|
+
* parameter count do not depend on `valueCount` and D1's 100-parameter
|
|
230
|
+
* cap is unreachable through scope values.
|
|
227
231
|
*/
|
|
228
232
|
export declare function scanRowPageSql(table: CompiledTable, valueCount: number, dialect: RelationalDialect): string;
|
|
229
|
-
/**
|
|
230
|
-
* One-round-trip page read for `readCommitWindow` (sync_* tables only; it
|
|
231
|
-
* lives here beside `scanRowPageSql` because it is the same join-the-index
|
|
232
|
-
* posture, dialect-parameterized the same way): candidates from the inverted
|
|
233
|
-
* change-scope index (ordered + LIMITed at the covering `sync_change_scopes`
|
|
234
|
-
* PK — exactly the old candidate query, so the index-first invariant of
|
|
235
|
-
* `CommitWindowQuery` is unchanged) LEFT JOINed to the commit metadata and
|
|
236
|
-
* to the commit's changes for the table, so a whole window page arrives in
|
|
237
|
-
* ONE statement instead of 1 + 2×candidates round trips. This is the
|
|
238
|
-
* incremental-pull hot path — it runs on every sync round; before the join a
|
|
239
|
-
* 500-candidate catch-up window on Postgres paid ~1000 network round trips.
|
|
240
|
-
*
|
|
241
|
-
* LEFT JOIN (not INNER): a candidate whose commit vanished (scope-index
|
|
242
|
-
* entry without a commit/changes row) must still reach the caller — its
|
|
243
|
-
* `commit_seq` advances the window cursor — so it comes back with NULL
|
|
244
|
-
* meta/change columns rather than disappearing from the page.
|
|
245
|
-
*
|
|
246
|
-
* Rows arrive ordered (commit_seq, idx): oldest-first commits, each commit's
|
|
247
|
-
* change rows consecutive in `idx` order — the caller groups consecutive
|
|
248
|
-
* rows and verifies the full multi-variable scope match in JS.
|
|
249
|
-
*
|
|
250
|
-
* Bind order:
|
|
251
|
-
* - postgres: [partition, tbl, var, ...values, afterSeq, throughSeq,
|
|
252
|
-
* limit] (the joins reuse `$1`/`$2`);
|
|
253
|
-
* - sqlite: [partition, tbl, var, ...values, afterSeq, throughSeq,
|
|
254
|
-
* limit, partition, partition, tbl] (positional `?` — partition/tbl bind
|
|
255
|
-
* again for the joins).
|
|
256
|
-
*/
|
|
257
|
-
export declare function commitWindowPageSql(valueCount: number, dialect: RelationalDialect): string;
|
|
258
233
|
/**
|
|
259
234
|
* PostgreSQL set-based form of `scanRowPageSql`: one statement answers the
|
|
260
235
|
* page of every subscription scanning any table in a pull. `$2` is a JSON
|
|
@@ -270,17 +245,6 @@ export declare function commitWindowPageSql(valueCount: number, dialect: Relatio
|
|
|
270
245
|
* parameter (Bun.sql) cannot turn the array into a JSON string.
|
|
271
246
|
*/
|
|
272
247
|
export declare function scanRowBatchSql(tables: readonly CompiledTable[]): string;
|
|
273
|
-
/**
|
|
274
|
-
* PostgreSQL set-based form of `commitWindowPageSql`: one statement answers
|
|
275
|
-
* the window page of every subscription in a pull, across all tables. `$2`
|
|
276
|
-
* is a JSON array of `{i, tbl, var, vals, after, through, lim}` records, each
|
|
277
|
-
* running the same index-first candidate subquery through a LATERAL join.
|
|
278
|
-
* Rows arrive ordered by (i, commit_seq, idx).
|
|
279
|
-
*
|
|
280
|
-
* Params: [partition, specs (JSON text)], the specs bound as text like
|
|
281
|
-
* `scanRowBatchSql`.
|
|
282
|
-
*/
|
|
283
|
-
export declare const COMMIT_WINDOW_BATCH_SQL = "SELECT s.i AS sub, c.commit_seq AS commit_seq, m.actor_id AS actor_id, m.created_at_ms AS created_at_ms,\n ch.tbl AS tbl, ch.row_id AS row_id, ch.op AS op, ch.row_version AS row_version, ch.scopes AS scopes, ch.payload AS payload\n FROM jsonb_to_recordset($2::text::jsonb) AS s(i int, tbl text, var text, vals text[], after bigint, through bigint, lim int)\n CROSS JOIN LATERAL (SELECT DISTINCT commit_seq FROM sync_change_scopes\n WHERE partition=$1 AND tbl=s.tbl AND var=s.var AND value=ANY(s.vals)\n AND commit_seq>s.after AND commit_seq<=s.through\n ORDER BY commit_seq LIMIT s.lim) c\n LEFT JOIN sync_commits m\n ON m.partition=$1 AND m.commit_seq=c.commit_seq\n LEFT JOIN sync_changes ch\n ON ch.partition=$1 AND ch.commit_seq=c.commit_seq AND ch.tbl=s.tbl\n ORDER BY s.i, c.commit_seq, ch.idx";
|
|
284
248
|
/**
|
|
285
249
|
* PostgreSQL push read-ahead across every table a commit targets, in one
|
|
286
250
|
* statement: each requested row id with its stored row (NULL columns when
|
|
@@ -292,23 +256,14 @@ export declare function prefetchRowsBatchSql(tables: readonly CompiledTable[], p
|
|
|
292
256
|
readonly specs: string;
|
|
293
257
|
}): string;
|
|
294
258
|
/**
|
|
295
|
-
*
|
|
296
|
-
* one
|
|
297
|
-
*
|
|
298
|
-
*
|
|
299
|
-
*
|
|
300
|
-
*
|
|
301
|
-
* Params: [partition, tbl, var, values as JSON array text].
|
|
302
|
-
*/
|
|
303
|
-
export declare const LATEST_SCOPED_CHANGE_SEQ_SQLITE_SQL = "SELECT max((SELECT max(commit_seq) FROM sync_change_scopes\n WHERE partition=? AND tbl=? AND var=? AND value=v.value)) AS seq\n FROM json_each(?) AS v";
|
|
304
|
-
/**
|
|
305
|
-
* PostgreSQL set-based form of `LATEST_SCOPED_CHANGE_SEQ_SQLITE_SQL` for the
|
|
306
|
-
* subscriptions of a pull, across all tables. `$2` is a JSON array of
|
|
307
|
-
* `{i, tbl, var, vals}` records. Params: [partition, specs (JSON text)].
|
|
259
|
+
* SELECT the rows that reference one blob with their stored scope maps, in
|
|
260
|
+
* one statement for any number of references: the by-blob index range
|
|
261
|
+
* (§5.9.4) supplies the candidates and a `CASE` on the candidate's table
|
|
262
|
+
* name looks each scope map up by primary key in that table's own relation.
|
|
263
|
+
* A reference whose table left the schema or whose row is gone has NULL
|
|
264
|
+
* `scopes`. Needs at least one table. Params: [partition, blobId].
|
|
308
265
|
*/
|
|
309
|
-
export declare
|
|
310
|
-
/** SELECT a row's version + scope map (admin/blob authz). Params: [partition, rowId]. */
|
|
311
|
-
export declare function selectRowScopesSql(table: CompiledTable, dialect: RelationalDialect): string;
|
|
266
|
+
export declare function rowsReferencingBlobSql(tables: Iterable<CompiledTable>, dialect: RelationalDialect): string;
|
|
312
267
|
/** DELETE one stored row. Params: [partition, rowId]. */
|
|
313
268
|
export declare function deleteRowSql(table: CompiledTable, dialect: RelationalDialect): string;
|
|
314
269
|
/**
|
|
@@ -385,7 +340,7 @@ export declare function selectRowsForRewriteSql(table: CompiledTable, dialect: R
|
|
|
385
340
|
*/
|
|
386
341
|
export declare function rewriteRowSql(table: CompiledTable): string;
|
|
387
342
|
/** Bind values for `rewriteRowSql` from the (already migrated) payload. */
|
|
388
|
-
export declare function rewriteValues(table: CompiledTable, partition: string, rowId: string, payload: Uint8Array, dialect: RelationalDialect):
|
|
343
|
+
export declare function rewriteValues(table: CompiledTable, partition: string, rowId: string, payload: Uint8Array, dialect: RelationalDialect): RowValue[];
|
|
389
344
|
/**
|
|
390
345
|
* What the migration rewrite phase must do for one table, derived from the
|
|
391
346
|
* persisted layouts + the physical columns present before this run.
|
package/dist/relational-rows.js
CHANGED
|
@@ -493,12 +493,16 @@ export function selectRowSql(table, dialect) {
|
|
|
493
493
|
}
|
|
494
494
|
/**
|
|
495
495
|
* SELECT many stored rows of one table by primary key in one statement.
|
|
496
|
-
*
|
|
497
|
-
*
|
|
498
|
-
* rowIds]; absent ids contribute no
|
|
496
|
+
* The ids bind as one parameter (Postgres `text[]` with `= ANY($2)`, SQLite
|
|
497
|
+
* a JSON array through `json_each`), so the statement count does not grow
|
|
498
|
+
* with the id count. Params: [partition, rowIds]; absent ids contribute no
|
|
499
|
+
* row.
|
|
499
500
|
*/
|
|
500
|
-
export function selectRowsSql(table) {
|
|
501
|
-
|
|
501
|
+
export function selectRowsSql(table, dialect) {
|
|
502
|
+
const [partition, rowIds] = dialect === 'sqlite'
|
|
503
|
+
? ['?', 'IN (SELECT value FROM json_each(?))']
|
|
504
|
+
: ['$1', '=ANY($2::text[])'];
|
|
505
|
+
return `SELECT ${quoteIdent(SYNC_ROW_ID_COLUMN)} AS row_id, ${quoteIdent(SYNC_VERSION_COLUMN)} AS server_version, ${quoteIdent(SYNC_SCOPES_COLUMN)} AS scopes, ${quoteIdent(SYNC_PAYLOAD_COLUMN)} AS payload, ${quoteIdent(SYNC_COLUMN_VERSIONS_COLUMN)} AS column_versions FROM ${quoteIdent(table.name)} WHERE ${quoteIdent(SYNC_PARTITION_COLUMN)}=${partition} AND ${quoteIdent(SYNC_ROW_ID_COLUMN)}${rowIds}`;
|
|
502
506
|
}
|
|
503
507
|
/**
|
|
504
508
|
* Bounded exact lookup through one declared relational index. Unlike
|
|
@@ -546,76 +550,29 @@ export function indexRowPageStatement(table, index, values, partition, afterRowI
|
|
|
546
550
|
* Bind order:
|
|
547
551
|
* - postgres: [partition, tbl, var, ...values, afterRowId, limit]
|
|
548
552
|
* (the join reuses `$1` for the partition);
|
|
549
|
-
* - sqlite: [partition, tbl, var,
|
|
550
|
-
* partition] (positional
|
|
553
|
+
* - sqlite: [partition, tbl, var, values as JSON array text, afterRowId,
|
|
554
|
+
* limit, partition] (positional `?`; the partition binds again for the
|
|
555
|
+
* join). The values expand through `json_each`, so the statement text and
|
|
556
|
+
* parameter count do not depend on `valueCount` and D1's 100-parameter
|
|
557
|
+
* cap is unreachable through scope values.
|
|
551
558
|
*/
|
|
552
559
|
export function scanRowPageSql(table, valueCount, dialect) {
|
|
553
560
|
const p = (n) => (dialect === 'sqlite' ? '?' : `$${n}`);
|
|
554
|
-
const values =
|
|
555
|
-
|
|
556
|
-
|
|
561
|
+
const values = dialect === 'sqlite'
|
|
562
|
+
? 'SELECT value FROM json_each(?)'
|
|
563
|
+
: Array.from({ length: valueCount }, (_, i) => p(4 + i)).join(',');
|
|
557
564
|
const after = p(4 + valueCount);
|
|
558
565
|
const limit = p(5 + valueCount);
|
|
559
566
|
const joinPartition = dialect === 'sqlite' ? '?' : '$1';
|
|
560
567
|
return `SELECT c.row_id AS row_id, r.${quoteIdent(SYNC_VERSION_COLUMN)} AS server_version, r.${quoteIdent(SYNC_SCOPES_COLUMN)} AS scopes, r.${quoteIdent(SYNC_PAYLOAD_COLUMN)} AS payload, r.${quoteIdent(SYNC_COLUMN_VERSIONS_COLUMN)} AS column_versions
|
|
561
568
|
FROM (SELECT DISTINCT row_id FROM sync_row_scopes
|
|
562
|
-
WHERE partition=${p(1)} AND tbl=${p(2)} AND var=${p(3)} AND value IN (${values
|
|
569
|
+
WHERE partition=${p(1)} AND tbl=${p(2)} AND var=${p(3)} AND value IN (${values})
|
|
563
570
|
AND row_id>${after}
|
|
564
571
|
ORDER BY row_id LIMIT ${limit}) c
|
|
565
572
|
LEFT JOIN ${quoteIdent(table.name)} r
|
|
566
573
|
ON r.${quoteIdent(SYNC_PARTITION_COLUMN)}=${joinPartition} AND r.${quoteIdent(SYNC_ROW_ID_COLUMN)}=c.row_id
|
|
567
574
|
ORDER BY c.row_id`;
|
|
568
575
|
}
|
|
569
|
-
/**
|
|
570
|
-
* One-round-trip page read for `readCommitWindow` (sync_* tables only; it
|
|
571
|
-
* lives here beside `scanRowPageSql` because it is the same join-the-index
|
|
572
|
-
* posture, dialect-parameterized the same way): candidates from the inverted
|
|
573
|
-
* change-scope index (ordered + LIMITed at the covering `sync_change_scopes`
|
|
574
|
-
* PK — exactly the old candidate query, so the index-first invariant of
|
|
575
|
-
* `CommitWindowQuery` is unchanged) LEFT JOINed to the commit metadata and
|
|
576
|
-
* to the commit's changes for the table, so a whole window page arrives in
|
|
577
|
-
* ONE statement instead of 1 + 2×candidates round trips. This is the
|
|
578
|
-
* incremental-pull hot path — it runs on every sync round; before the join a
|
|
579
|
-
* 500-candidate catch-up window on Postgres paid ~1000 network round trips.
|
|
580
|
-
*
|
|
581
|
-
* LEFT JOIN (not INNER): a candidate whose commit vanished (scope-index
|
|
582
|
-
* entry without a commit/changes row) must still reach the caller — its
|
|
583
|
-
* `commit_seq` advances the window cursor — so it comes back with NULL
|
|
584
|
-
* meta/change columns rather than disappearing from the page.
|
|
585
|
-
*
|
|
586
|
-
* Rows arrive ordered (commit_seq, idx): oldest-first commits, each commit's
|
|
587
|
-
* change rows consecutive in `idx` order — the caller groups consecutive
|
|
588
|
-
* rows and verifies the full multi-variable scope match in JS.
|
|
589
|
-
*
|
|
590
|
-
* Bind order:
|
|
591
|
-
* - postgres: [partition, tbl, var, ...values, afterSeq, throughSeq,
|
|
592
|
-
* limit] (the joins reuse `$1`/`$2`);
|
|
593
|
-
* - sqlite: [partition, tbl, var, ...values, afterSeq, throughSeq,
|
|
594
|
-
* limit, partition, partition, tbl] (positional `?` — partition/tbl bind
|
|
595
|
-
* again for the joins).
|
|
596
|
-
*/
|
|
597
|
-
export function commitWindowPageSql(valueCount, dialect) {
|
|
598
|
-
const p = (n) => (dialect === 'sqlite' ? '?' : `$${n}`);
|
|
599
|
-
const values = [];
|
|
600
|
-
for (let i = 0; i < valueCount; i++)
|
|
601
|
-
values.push(p(4 + i));
|
|
602
|
-
const after = p(4 + valueCount);
|
|
603
|
-
const through = p(5 + valueCount);
|
|
604
|
-
const limit = p(6 + valueCount);
|
|
605
|
-
const joinPartition = dialect === 'sqlite' ? '?' : '$1';
|
|
606
|
-
const joinTbl = dialect === 'sqlite' ? '?' : '$2';
|
|
607
|
-
return `SELECT c.commit_seq AS commit_seq, m.actor_id AS actor_id, m.created_at_ms AS created_at_ms,
|
|
608
|
-
ch.tbl AS tbl, ch.row_id AS row_id, ch.op AS op, ch.row_version AS row_version, ch.scopes AS scopes, ch.payload AS payload
|
|
609
|
-
FROM (SELECT DISTINCT commit_seq FROM sync_change_scopes
|
|
610
|
-
WHERE partition=${p(1)} AND tbl=${p(2)} AND var=${p(3)} AND value IN (${values.join(',')})
|
|
611
|
-
AND commit_seq>${after} AND commit_seq<=${through}
|
|
612
|
-
ORDER BY commit_seq LIMIT ${limit}) c
|
|
613
|
-
LEFT JOIN sync_commits m
|
|
614
|
-
ON m.partition=${joinPartition} AND m.commit_seq=c.commit_seq
|
|
615
|
-
LEFT JOIN sync_changes ch
|
|
616
|
-
ON ch.partition=${joinPartition} AND ch.commit_seq=c.commit_seq AND ch.tbl=${joinTbl}
|
|
617
|
-
ORDER BY c.commit_seq, ch.idx`;
|
|
618
|
-
}
|
|
619
576
|
/**
|
|
620
577
|
* PostgreSQL set-based form of `scanRowPageSql`: one statement answers the
|
|
621
578
|
* page of every subscription scanning any table in a pull. `$2` is a JSON
|
|
@@ -653,28 +610,6 @@ export function scanRowBatchSql(tables) {
|
|
|
653
610
|
${projection}
|
|
654
611
|
ORDER BY sub, row_id`;
|
|
655
612
|
}
|
|
656
|
-
/**
|
|
657
|
-
* PostgreSQL set-based form of `commitWindowPageSql`: one statement answers
|
|
658
|
-
* the window page of every subscription in a pull, across all tables. `$2`
|
|
659
|
-
* is a JSON array of `{i, tbl, var, vals, after, through, lim}` records, each
|
|
660
|
-
* running the same index-first candidate subquery through a LATERAL join.
|
|
661
|
-
* Rows arrive ordered by (i, commit_seq, idx).
|
|
662
|
-
*
|
|
663
|
-
* Params: [partition, specs (JSON text)], the specs bound as text like
|
|
664
|
-
* `scanRowBatchSql`.
|
|
665
|
-
*/
|
|
666
|
-
export const COMMIT_WINDOW_BATCH_SQL = `SELECT s.i AS sub, c.commit_seq AS commit_seq, m.actor_id AS actor_id, m.created_at_ms AS created_at_ms,
|
|
667
|
-
ch.tbl AS tbl, ch.row_id AS row_id, ch.op AS op, ch.row_version AS row_version, ch.scopes AS scopes, ch.payload AS payload
|
|
668
|
-
FROM jsonb_to_recordset($2::text::jsonb) AS s(i int, tbl text, var text, vals text[], after bigint, through bigint, lim int)
|
|
669
|
-
CROSS JOIN LATERAL (SELECT DISTINCT commit_seq FROM sync_change_scopes
|
|
670
|
-
WHERE partition=$1 AND tbl=s.tbl AND var=s.var AND value=ANY(s.vals)
|
|
671
|
-
AND commit_seq>s.after AND commit_seq<=s.through
|
|
672
|
-
ORDER BY commit_seq LIMIT s.lim) c
|
|
673
|
-
LEFT JOIN sync_commits m
|
|
674
|
-
ON m.partition=$1 AND m.commit_seq=c.commit_seq
|
|
675
|
-
LEFT JOIN sync_changes ch
|
|
676
|
-
ON ch.partition=$1 AND ch.commit_seq=c.commit_seq AND ch.tbl=s.tbl
|
|
677
|
-
ORDER BY s.i, c.commit_seq, ch.idx`;
|
|
678
613
|
/**
|
|
679
614
|
* PostgreSQL push read-ahead across every table a commit targets, in one
|
|
680
615
|
* statement: each requested row id with its stored row (NULL columns when
|
|
@@ -690,7 +625,7 @@ export function prefetchRowsBatchSql(tables, params = {
|
|
|
690
625
|
const projection = tables
|
|
691
626
|
.map((table) => {
|
|
692
627
|
const quoted = quoteIdent(table.name);
|
|
693
|
-
return `SELECT ids.tbl AS tbl, ids.id AS lookup_id, r.${quoteIdent(SYNC_ROW_ID_COLUMN)} AS row_id, r.${quoteIdent(SYNC_VERSION_COLUMN)} AS server_version, r.${quoteIdent(SYNC_SCOPES_COLUMN)} AS scopes, r.${quoteIdent(SYNC_PAYLOAD_COLUMN)} AS payload, r.${quoteIdent(SYNC_COLUMN_VERSIONS_COLUMN)} AS column_versions, t.commit_seq AS tombstone_seq
|
|
628
|
+
return `SELECT ids.tbl AS tbl, ids.id AS lookup_id, r.${quoteIdent(SYNC_ROW_ID_COLUMN)} AS row_id, r.${quoteIdent(SYNC_VERSION_COLUMN)} AS server_version, r.${quoteIdent(SYNC_SCOPES_COLUMN)} AS scopes, r.${quoteIdent(SYNC_PAYLOAD_COLUMN)} AS payload, r.${quoteIdent(SYNC_COLUMN_VERSIONS_COLUMN)} AS column_versions, t.commit_seq AS tombstone_seq, t.server_version AS tombstone_version, t.scopes AS tombstone_scopes
|
|
694
629
|
FROM ids
|
|
695
630
|
LEFT JOIN ${quoted} r
|
|
696
631
|
ON r.${quoteIdent(SYNC_PARTITION_COLUMN)}=${params.partition} AND r.${quoteIdent(SYNC_ROW_ID_COLUMN)}=ids.id
|
|
@@ -707,33 +642,17 @@ export function prefetchRowsBatchSql(tables, params = {
|
|
|
707
642
|
${projection}`;
|
|
708
643
|
}
|
|
709
644
|
/**
|
|
710
|
-
*
|
|
711
|
-
* one
|
|
712
|
-
*
|
|
713
|
-
*
|
|
714
|
-
*
|
|
715
|
-
*
|
|
716
|
-
* Params: [partition, tbl, var, values as JSON array text].
|
|
717
|
-
*/
|
|
718
|
-
export const LATEST_SCOPED_CHANGE_SEQ_SQLITE_SQL = `SELECT max((SELECT max(commit_seq) FROM sync_change_scopes
|
|
719
|
-
WHERE partition=? AND tbl=? AND var=? AND value=v.value)) AS seq
|
|
720
|
-
FROM json_each(?) AS v`;
|
|
721
|
-
/**
|
|
722
|
-
* PostgreSQL set-based form of `LATEST_SCOPED_CHANGE_SEQ_SQLITE_SQL` for the
|
|
723
|
-
* subscriptions of a pull, across all tables. `$2` is a JSON array of
|
|
724
|
-
* `{i, tbl, var, vals}` records. Params: [partition, specs (JSON text)].
|
|
645
|
+
* SELECT the rows that reference one blob with their stored scope maps, in
|
|
646
|
+
* one statement for any number of references: the by-blob index range
|
|
647
|
+
* (§5.9.4) supplies the candidates and a `CASE` on the candidate's table
|
|
648
|
+
* name looks each scope map up by primary key in that table's own relation.
|
|
649
|
+
* A reference whose table left the schema or whose row is gone has NULL
|
|
650
|
+
* `scopes`. Needs at least one table. Params: [partition, blobId].
|
|
725
651
|
*/
|
|
726
|
-
export
|
|
727
|
-
(SELECT max(latest.seq) FROM (
|
|
728
|
-
SELECT (SELECT max(commit_seq) FROM sync_change_scopes
|
|
729
|
-
WHERE partition=$1 AND tbl=s.tbl AND var=s.var AND value=v.value) AS seq
|
|
730
|
-
FROM jsonb_array_elements_text(s.vals) AS v(value)) AS latest) AS seq
|
|
731
|
-
FROM jsonb_to_recordset($2::text::jsonb) AS s(i int, tbl text, var text, vals jsonb)
|
|
732
|
-
ORDER BY s.i`;
|
|
733
|
-
/** SELECT a row's version + scope map (admin/blob authz). Params: [partition, rowId]. */
|
|
734
|
-
export function selectRowScopesSql(table, dialect) {
|
|
652
|
+
export function rowsReferencingBlobSql(tables, dialect) {
|
|
735
653
|
const p = dialect === 'sqlite' ? ['?', '?'] : ['$1', '$2'];
|
|
736
|
-
|
|
654
|
+
const branches = [...tables].map((table) => `WHEN '${table.name.replaceAll("'", "''")}' THEN (SELECT t.${quoteIdent(SYNC_SCOPES_COLUMN)} FROM ${quoteIdent(table.name)} t WHERE t.${quoteIdent(SYNC_PARTITION_COLUMN)}=r.partition AND t.${quoteIdent(SYNC_ROW_ID_COLUMN)}=r.row_id)`);
|
|
655
|
+
return `SELECT r.tbl AS tbl, r.row_id AS row_id, CASE r.tbl ${branches.join(' ')} END AS scopes FROM sync_blob_refs r WHERE r.partition=${p[0]} AND r.blob_id=${p[1]}`;
|
|
737
656
|
}
|
|
738
657
|
/** DELETE one stored row. Params: [partition, rowId]. */
|
|
739
658
|
export function deleteRowSql(table, dialect) {
|
package/dist/s3-blob-store.d.ts
CHANGED
|
@@ -48,17 +48,16 @@ export declare class S3BlobStore extends BucketBlobStore {
|
|
|
48
48
|
}): Promise<SegmentUrlIssue>;
|
|
49
49
|
/**
|
|
50
50
|
* SigV4 presigned PUT for a blob object (§5.9.3 direct-to-storage upload).
|
|
51
|
-
* The
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* handler after host authentication; TTL SHOULD be ≤ 15 min (default 900).
|
|
51
|
+
* The signed key embeds the `blobId`, and the signature binds
|
|
52
|
+
* `content-length` to the granted `byteLength` and `x-amz-checksum-sha256`
|
|
53
|
+
* to the `blobId` digest (the same SHA-256, re-encoded from lowercase hex to
|
|
54
|
+
* the base64 of its 32 raw bytes). The object store therefore rejects a PUT
|
|
55
|
+
* of any other size (signature mismatch) or any other bytes (`BadDigest`),
|
|
56
|
+
* so a grant can only place the content-addressed bytes at their key.
|
|
57
|
+
* Issued by the upload-grant handler after host authentication and the
|
|
58
|
+
* size-cap check; TTL SHOULD be ≤ 15 min (default 900).
|
|
60
59
|
*/
|
|
61
|
-
presignBlobPut(partition: string, blobId: string, options?: {
|
|
60
|
+
presignBlobPut(partition: string, blobId: string, byteLength: number, options?: {
|
|
62
61
|
readonly ttlSeconds?: number;
|
|
63
62
|
readonly nowMs?: number;
|
|
64
63
|
}): Promise<SegmentUrlIssue>;
|
|
@@ -79,8 +78,8 @@ export declare function s3PresignedBlobUrls(store: S3BlobStore, options?: {
|
|
|
79
78
|
* `blobUploadUrls: s3PresignedBlobUploads(blobStore, { ttlSeconds: 900 })`.
|
|
80
79
|
* The upload-grant handler issues the presigned PUT only after host
|
|
81
80
|
* authentication + the size-cap check; the client PUTs bytes straight to the
|
|
82
|
-
* object store with no host auth
|
|
83
|
-
*
|
|
81
|
+
* object store with no host auth, and the store verifies the signed
|
|
82
|
+
* `content-length` and `x-amz-checksum-sha256` (§5.9.3).
|
|
84
83
|
*/
|
|
85
84
|
export declare function s3PresignedBlobUploads(store: S3BlobStore, options?: {
|
|
86
85
|
readonly ttlSeconds?: number;
|
package/dist/s3-blob-store.js
CHANGED
|
@@ -60,22 +60,22 @@ export class S3BlobStore extends BucketBlobStore {
|
|
|
60
60
|
}
|
|
61
61
|
/**
|
|
62
62
|
* SigV4 presigned PUT for a blob object (§5.9.3 direct-to-storage upload).
|
|
63
|
-
* The
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* handler after host authentication; TTL SHOULD be ≤ 15 min (default 900).
|
|
63
|
+
* The signed key embeds the `blobId`, and the signature binds
|
|
64
|
+
* `content-length` to the granted `byteLength` and `x-amz-checksum-sha256`
|
|
65
|
+
* to the `blobId` digest (the same SHA-256, re-encoded from lowercase hex to
|
|
66
|
+
* the base64 of its 32 raw bytes). The object store therefore rejects a PUT
|
|
67
|
+
* of any other size (signature mismatch) or any other bytes (`BadDigest`),
|
|
68
|
+
* so a grant can only place the content-addressed bytes at their key.
|
|
69
|
+
* Issued by the upload-grant handler after host authentication and the
|
|
70
|
+
* size-cap check; TTL SHOULD be ≤ 15 min (default 900).
|
|
72
71
|
*/
|
|
73
|
-
async presignBlobPut(partition, blobId, options) {
|
|
72
|
+
async presignBlobPut(partition, blobId, byteLength, options) {
|
|
74
73
|
if (!BLOB_ID_PATTERN.test(blobId)) {
|
|
75
74
|
throw new Error(`S3BlobStore: malformed blobId ${blobId}`);
|
|
76
75
|
}
|
|
77
76
|
const ttlSeconds = options?.ttlSeconds ?? 900;
|
|
78
77
|
const nowMs = options?.nowMs ?? Date.now();
|
|
78
|
+
const digest = (blobId.slice('sha256:'.length).match(/../g) ?? []).map((hex) => Number.parseInt(hex, 16));
|
|
79
79
|
const url = await presignUrl({
|
|
80
80
|
method: 'PUT',
|
|
81
81
|
url: this.#bucket.urlFor(this.objectKeyFor(partition, blobId)),
|
|
@@ -83,6 +83,10 @@ export class S3BlobStore extends BucketBlobStore {
|
|
|
83
83
|
credentials: this.#credentials,
|
|
84
84
|
nowMs,
|
|
85
85
|
expiresSeconds: ttlSeconds,
|
|
86
|
+
headers: {
|
|
87
|
+
'content-length': String(byteLength),
|
|
88
|
+
'x-amz-checksum-sha256': btoa(String.fromCharCode(...digest)),
|
|
89
|
+
},
|
|
86
90
|
});
|
|
87
91
|
return {
|
|
88
92
|
url: url.toString(),
|
|
@@ -111,14 +115,17 @@ export function s3PresignedBlobUrls(store, options) {
|
|
|
111
115
|
* `blobUploadUrls: s3PresignedBlobUploads(blobStore, { ttlSeconds: 900 })`.
|
|
112
116
|
* The upload-grant handler issues the presigned PUT only after host
|
|
113
117
|
* authentication + the size-cap check; the client PUTs bytes straight to the
|
|
114
|
-
* object store with no host auth
|
|
115
|
-
*
|
|
118
|
+
* object store with no host auth, and the store verifies the signed
|
|
119
|
+
* `content-length` and `x-amz-checksum-sha256` (§5.9.3).
|
|
116
120
|
*/
|
|
117
121
|
export function s3PresignedBlobUploads(store, options) {
|
|
118
122
|
return {
|
|
119
123
|
...(options?.ttlSeconds !== undefined
|
|
120
124
|
? { ttlSeconds: options.ttlSeconds }
|
|
121
125
|
: {}),
|
|
122
|
-
presign: ({ partition, blobId, ttlSeconds, nowMs }) => store.presignBlobPut(partition, blobId,
|
|
126
|
+
presign: ({ partition, blobId, byteLength, ttlSeconds, nowMs }) => store.presignBlobPut(partition, blobId, byteLength, {
|
|
127
|
+
ttlSeconds,
|
|
128
|
+
nowMs,
|
|
129
|
+
}),
|
|
123
130
|
};
|
|
124
131
|
}
|
|
@@ -29,7 +29,14 @@ export interface S3ObjectBucketConfig {
|
|
|
29
29
|
export declare class S3ObjectBucket implements ObjectBucket {
|
|
30
30
|
#private;
|
|
31
31
|
constructor(config: S3ObjectBucketConfig);
|
|
32
|
-
/**
|
|
32
|
+
/**
|
|
33
|
+
* The object URL the presign helpers sign (`{endpoint}/{bucket}/{key}`).
|
|
34
|
+
* Every `/`-separated key segment is URI-encoded the way SigV4
|
|
35
|
+
* canonicalizes an S3 path (RFC 3986 unreserved characters stay raw), so a
|
|
36
|
+
* partition id holding `#`, `?`, `%` or `:` stays inside its own key and the
|
|
37
|
+
* pathname is already the canonical path. A `.` or `..` segment has no URL
|
|
38
|
+
* form (the URL parser and S3 clients collapse it), so it is refused.
|
|
39
|
+
*/
|
|
33
40
|
urlFor(key: string): URL;
|
|
34
41
|
get(key: string): Promise<BucketObject | undefined>;
|
|
35
42
|
head(key: string): Promise<BucketObjectHead | undefined>;
|
package/dist/s3-object-bucket.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EMPTY_PAYLOAD_SHA256, sha256Hex, signRequest, } from './sigv4.js';
|
|
1
|
+
import { EMPTY_PAYLOAD_SHA256, sha256Hex, signRequest, uriEncode, } from './sigv4.js';
|
|
2
2
|
const META_HEADER_PREFIX = 'x-amz-meta-';
|
|
3
3
|
/** User metadata carried as `x-amz-meta-*` headers (lowercased by `Headers`). */
|
|
4
4
|
function metadataOf(headers) {
|
|
@@ -56,10 +56,21 @@ export class S3ObjectBucket {
|
|
|
56
56
|
: {}),
|
|
57
57
|
};
|
|
58
58
|
}
|
|
59
|
-
/**
|
|
59
|
+
/**
|
|
60
|
+
* The object URL the presign helpers sign (`{endpoint}/{bucket}/{key}`).
|
|
61
|
+
* Every `/`-separated key segment is URI-encoded the way SigV4
|
|
62
|
+
* canonicalizes an S3 path (RFC 3986 unreserved characters stay raw), so a
|
|
63
|
+
* partition id holding `#`, `?`, `%` or `:` stays inside its own key and the
|
|
64
|
+
* pathname is already the canonical path. A `.` or `..` segment has no URL
|
|
65
|
+
* form (the URL parser and S3 clients collapse it), so it is refused.
|
|
66
|
+
*/
|
|
60
67
|
urlFor(key) {
|
|
61
68
|
const endpoint = this.#config.endpoint.replace(/\/+$/, '');
|
|
62
|
-
|
|
69
|
+
const segments = key.split('/');
|
|
70
|
+
if (segments.some((segment) => segment === '.' || segment === '..')) {
|
|
71
|
+
throw new Error('S3ObjectBucket: object key has a dot segment');
|
|
72
|
+
}
|
|
73
|
+
return new URL(`${endpoint}/${this.#config.bucket}/${segments.map((segment) => uriEncode(segment, true)).join('/')}`);
|
|
63
74
|
}
|
|
64
75
|
async #request(method, key, options) {
|
|
65
76
|
const url = this.urlFor(key);
|
|
@@ -113,9 +124,11 @@ export class S3ObjectBucket {
|
|
|
113
124
|
if (response === undefined)
|
|
114
125
|
return undefined;
|
|
115
126
|
await response.arrayBuffer();
|
|
127
|
+
const lastModified = Date.parse(response.headers.get('last-modified') ?? '');
|
|
116
128
|
return {
|
|
117
129
|
etag: response.headers.get('etag') ?? undefined,
|
|
118
130
|
size: sizeOf(response.headers),
|
|
131
|
+
lastModifiedMs: Number.isNaN(lastModified) ? undefined : lastModified,
|
|
119
132
|
metadata: metadataOf(response.headers),
|
|
120
133
|
};
|
|
121
134
|
}
|
package/dist/schema.d.ts
CHANGED
|
@@ -146,4 +146,18 @@ export interface CompiledSchema {
|
|
|
146
146
|
/** Union of scope variables declared by any table (§3.2 resolver check). */
|
|
147
147
|
readonly declaredVariables: ReadonlySet<string>;
|
|
148
148
|
}
|
|
149
|
+
/** A scope column has a type other than string or integer (§3.1). */
|
|
150
|
+
export declare class ScopeColumnTypeError extends Error {
|
|
151
|
+
readonly details: {
|
|
152
|
+
readonly table: string;
|
|
153
|
+
readonly column: string;
|
|
154
|
+
readonly type: string;
|
|
155
|
+
};
|
|
156
|
+
readonly code = "schema.scope_column_type";
|
|
157
|
+
constructor(details: {
|
|
158
|
+
readonly table: string;
|
|
159
|
+
readonly column: string;
|
|
160
|
+
readonly type: string;
|
|
161
|
+
});
|
|
162
|
+
}
|
|
149
163
|
export declare function compileSchema(schema: ServerSchema): CompiledSchema;
|
package/dist/schema.js
CHANGED
|
@@ -7,6 +7,16 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { validatePortableRelationalIdentifier, } from '@syncular/core';
|
|
9
9
|
const PATTERN_RE = /^([^{}]+):\{([^{}:]+)\}$/;
|
|
10
|
+
/** A scope column has a type other than string or integer (§3.1). */
|
|
11
|
+
export class ScopeColumnTypeError extends Error {
|
|
12
|
+
details;
|
|
13
|
+
code = 'schema.scope_column_type';
|
|
14
|
+
constructor(details) {
|
|
15
|
+
super('a scope column must have type string or integer (§3.1)');
|
|
16
|
+
this.details = details;
|
|
17
|
+
this.name = 'ScopeColumnTypeError';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
10
20
|
/**
|
|
11
21
|
* Compile one `prefix:{variable}` scope pattern against the table's column
|
|
12
22
|
* list, resolving the scope column's positional index for row extraction.
|
|
@@ -24,6 +34,14 @@ function compilePattern(table, spec, columnIndex) {
|
|
|
24
34
|
if (index === undefined) {
|
|
25
35
|
throw new Error(`table ${table.name}: scope pattern ${JSON.stringify(pattern)} names unknown column ${JSON.stringify(column)}`);
|
|
26
36
|
}
|
|
37
|
+
const type = table.columns[index]?.type;
|
|
38
|
+
if (type !== 'string' && type !== 'integer') {
|
|
39
|
+
throw new ScopeColumnTypeError({
|
|
40
|
+
table: table.name,
|
|
41
|
+
column,
|
|
42
|
+
type: type ?? 'undefined',
|
|
43
|
+
});
|
|
44
|
+
}
|
|
27
45
|
return { variable, prefix, column, columnIndex: index };
|
|
28
46
|
}
|
|
29
47
|
const compiledCache = new WeakMap();
|
|
@@ -190,6 +208,11 @@ export function compileSchema(schema) {
|
|
|
190
208
|
if (parent === undefined) {
|
|
191
209
|
throw new Error(`table ${child.name}: reference column ${JSON.stringify(reference.column)} names unknown table ${JSON.stringify(reference.parentTable)}`);
|
|
192
210
|
}
|
|
211
|
+
if (!child.materialize) {
|
|
212
|
+
// Enforcement reaches children through `scanRowsByIndex`, which reads
|
|
213
|
+
// the materialized columns.
|
|
214
|
+
throw new Error('a declared reference requires a materialized child table (§6.11)');
|
|
215
|
+
}
|
|
193
216
|
const childColumn = child.columns[reference.columnIndex];
|
|
194
217
|
const parentKey = parent.columns[parent.primaryKeyIndex];
|
|
195
218
|
if (childColumn === undefined || parentKey === undefined) {
|
package/dist/scopes.d.ts
CHANGED
|
@@ -20,10 +20,14 @@ export type EffectiveOutcome = {
|
|
|
20
20
|
readonly status: 'revoked';
|
|
21
21
|
};
|
|
22
22
|
/**
|
|
23
|
-
* Effective = requested ∩ allowed per §3.2 rules 4–
|
|
23
|
+
* Effective = requested ∩ allowed per §3.2 rules 4–6. Inputs are assumed
|
|
24
24
|
* key-validated (requested keys declared, no `'*'` requested values).
|
|
25
|
+
* `declared` is the table's declared variables: when a requested key passes
|
|
26
|
+
* through an allowed `'*'`, every declared variable the request omits and
|
|
27
|
+
* the actor holds a non-wildcard list for joins effective with that list
|
|
28
|
+
* (rule 4), so the wildcard never lifts the actor's other fences.
|
|
25
29
|
*/
|
|
26
|
-
export declare function computeEffective(requested: ScopeMap, resolved: ResolvedScopes): EffectiveOutcome;
|
|
30
|
+
export declare function computeEffective(declared: ReadonlySet<string>, requested: ScopeMap, resolved: ResolvedScopes): EffectiveOutcome;
|
|
27
31
|
/** Render a stored scope column value; `undefined` means missing/empty. */
|
|
28
32
|
export declare function renderScopeValue(value: RowValue | undefined): string | undefined;
|
|
29
33
|
/**
|
package/dist/scopes.js
CHANGED
|
@@ -5,21 +5,26 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { canonicalScopeJson, } from '@syncular/core';
|
|
7
7
|
/**
|
|
8
|
-
* Effective = requested ∩ allowed per §3.2 rules 4–
|
|
8
|
+
* Effective = requested ∩ allowed per §3.2 rules 4–6. Inputs are assumed
|
|
9
9
|
* key-validated (requested keys declared, no `'*'` requested values).
|
|
10
|
+
* `declared` is the table's declared variables: when a requested key passes
|
|
11
|
+
* through an allowed `'*'`, every declared variable the request omits and
|
|
12
|
+
* the actor holds a non-wildcard list for joins effective with that list
|
|
13
|
+
* (rule 4), so the wildcard never lifts the actor's other fences.
|
|
10
14
|
*/
|
|
11
|
-
export function computeEffective(requested, resolved) {
|
|
15
|
+
export function computeEffective(declared, requested, resolved) {
|
|
12
16
|
if (!resolved.ok)
|
|
13
17
|
return { status: 'revoked' };
|
|
14
18
|
const effective = {};
|
|
15
|
-
|
|
16
|
-
for (const key of
|
|
19
|
+
let wildcard = false;
|
|
20
|
+
for (const key of Object.keys(requested)) {
|
|
17
21
|
const requestedValues = requested[key] ?? [];
|
|
18
22
|
const allowedValues = resolved.allowed[key];
|
|
19
23
|
if (allowedValues === undefined)
|
|
20
24
|
return { status: 'revoked' };
|
|
21
25
|
let values;
|
|
22
26
|
if (allowedValues.includes('*')) {
|
|
27
|
+
wildcard = true;
|
|
23
28
|
values = [...requestedValues];
|
|
24
29
|
}
|
|
25
30
|
else {
|
|
@@ -31,6 +36,19 @@ export function computeEffective(requested, resolved) {
|
|
|
31
36
|
}
|
|
32
37
|
if (Object.keys(effective).length === 0)
|
|
33
38
|
return { status: 'revoked' };
|
|
39
|
+
if (wildcard) {
|
|
40
|
+
for (const variable of declared) {
|
|
41
|
+
const allowedValues = resolved.allowed[variable];
|
|
42
|
+
if (Object.hasOwn(requested, variable) ||
|
|
43
|
+
allowedValues === undefined ||
|
|
44
|
+
allowedValues.includes('*')) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (allowedValues.length === 0)
|
|
48
|
+
return { status: 'revoked' };
|
|
49
|
+
effective[variable] = [...allowedValues];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
34
52
|
return { status: 'active', effective };
|
|
35
53
|
}
|
|
36
54
|
/** Render a stored scope column value; `undefined` means missing/empty. */
|