@syncular/server 0.59.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 +20 -16
- package/dist/admin.d.ts +32 -20
- package/dist/admin.js +42 -40
- package/dist/context.d.ts +6 -8
- package/dist/context.js +1 -3
- package/dist/d1-storage.js +42 -24
- package/dist/errors.js +8 -0
- package/dist/events.d.ts +35 -17
- package/dist/frame-bytes.js +24 -6
- package/dist/handler.js +486 -223
- package/dist/local-authority.js +7 -9
- package/dist/operations.d.ts +5 -2
- package/dist/operations.js +20 -29
- package/dist/postgres-storage.d.ts +24 -19
- package/dist/postgres-storage.js +249 -168
- package/dist/pull.d.ts +68 -53
- package/dist/pull.js +258 -287
- package/dist/push.d.ts +12 -3
- package/dist/push.js +95 -32
- package/dist/realtime.d.ts +17 -11
- package/dist/realtime.js +65 -90
- package/dist/relational-rows.d.ts +0 -57
- package/dist/relational-rows.js +0 -97
- package/dist/seed.js +10 -15
- package/dist/segment-download.d.ts +4 -1
- package/dist/segment-download.js +4 -2
- package/dist/sqlite-dialect.d.ts +36 -59
- package/dist/sqlite-dialect.js +255 -159
- package/dist/sqlite-storage.js +24 -20
- package/dist/storage-query.d.ts +30 -1
- package/dist/storage-query.js +68 -0
- package/dist/storage.d.ts +156 -54
- package/dist/storage.js +15 -0
- package/package.json +2 -2
- package/src/admin.ts +87 -59
- package/src/context.ts +7 -11
- package/src/d1-storage.ts +49 -33
- package/src/errors.ts +8 -0
- package/src/events.ts +36 -17
- package/src/frame-bytes.ts +26 -6
- package/src/handler.ts +628 -286
- package/src/local-authority.ts +10 -11
- package/src/operations.ts +44 -42
- package/src/postgres-storage.ts +336 -226
- package/src/pull.ts +394 -414
- package/src/push.ts +116 -21
- package/src/realtime.ts +85 -129
- package/src/relational-rows.ts +0 -105
- package/src/seed.ts +13 -19
- package/src/segment-download.ts +8 -3
- package/src/sqlite-dialect.ts +363 -204
- package/src/sqlite-storage.ts +45 -25
- package/src/storage-query.ts +102 -0
- package/src/storage.ts +185 -58
package/src/local-authority.ts
CHANGED
|
@@ -5,11 +5,12 @@
|
|
|
5
5
|
* server with `attach` (§12.3).
|
|
6
6
|
*/
|
|
7
7
|
import {
|
|
8
|
-
|
|
8
|
+
decodeResponse,
|
|
9
9
|
decodeRow,
|
|
10
|
-
|
|
10
|
+
encodeRequest,
|
|
11
11
|
encodeSparseRow,
|
|
12
12
|
PROTOCOL_WIRE_VERSION,
|
|
13
|
+
registrationDigest,
|
|
13
14
|
type PushCommitFrame,
|
|
14
15
|
type PushOperation,
|
|
15
16
|
type PushResultFrame,
|
|
@@ -285,22 +286,15 @@ export async function openLocalAuthority(
|
|
|
285
286
|
transport: (request: Uint8Array) => Promise<Uint8Array>,
|
|
286
287
|
frames: RequestFrame[],
|
|
287
288
|
): Promise<ResponseFrame[]> => {
|
|
288
|
-
const message =
|
|
289
|
+
const message = decodeResponse(
|
|
289
290
|
await transport(
|
|
290
|
-
|
|
291
|
+
encodeRequest({
|
|
291
292
|
wireVersion: PROTOCOL_WIRE_VERSION,
|
|
292
293
|
msgKind: 'request',
|
|
293
294
|
frames,
|
|
294
295
|
}),
|
|
295
296
|
),
|
|
296
297
|
);
|
|
297
|
-
if (message.msgKind !== 'response') {
|
|
298
|
-
throw new LocalAuthorityAttachError({
|
|
299
|
-
reason: 'request_failed',
|
|
300
|
-
code: 'sync.invalid_request',
|
|
301
|
-
retryable: false,
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
298
|
const header = message.frames[0];
|
|
305
299
|
if (header?.type === 'RESP_HEADER' && header.requiredSchemaVersion) {
|
|
306
300
|
throw new LocalAuthorityAttachError({
|
|
@@ -336,6 +330,9 @@ export async function openLocalAuthority(
|
|
|
336
330
|
limitSnapshotRows: 0,
|
|
337
331
|
maxSnapshotPages: 0,
|
|
338
332
|
accept: 0b0011,
|
|
333
|
+
cursor: -1,
|
|
334
|
+
registrationDigest: registrationDigest([]),
|
|
335
|
+
echoDigest: new Uint8Array(),
|
|
339
336
|
},
|
|
340
337
|
])
|
|
341
338
|
)[0];
|
|
@@ -527,6 +524,7 @@ export async function openLocalAuthority(
|
|
|
527
524
|
const commit: PushCommitFrame = {
|
|
528
525
|
type: 'PUSH_COMMIT',
|
|
529
526
|
clientCommitId: `${attachId}:undo:${attempt}:${start / ATTACH_OPERATION_BUDGET}`,
|
|
527
|
+
clientSeq: 0,
|
|
530
528
|
operations: deletes.slice(start, start + ATTACH_OPERATION_BUDGET),
|
|
531
529
|
};
|
|
532
530
|
const result = await send(commit);
|
|
@@ -633,6 +631,7 @@ function planAttach(
|
|
|
633
631
|
commits.push({
|
|
634
632
|
type: 'PUSH_COMMIT',
|
|
635
633
|
clientCommitId: `${attachId}:${commits.length}`,
|
|
634
|
+
clientSeq: 0,
|
|
636
635
|
operations,
|
|
637
636
|
});
|
|
638
637
|
operations = [];
|
package/src/operations.ts
CHANGED
|
@@ -46,11 +46,14 @@ export interface RemoteQueryDependency {
|
|
|
46
46
|
readonly scopeKeys?: readonly string[];
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
/**
|
|
50
|
+
* The window units a scoped query reads (§4.8): the client's `WindowBase`
|
|
51
|
+
* (a variable and one value per other variable) and the variable's values.
|
|
52
|
+
*/
|
|
49
53
|
export interface RemoteQueryCoverage {
|
|
50
54
|
readonly base: {
|
|
51
|
-
readonly table: string;
|
|
52
55
|
readonly variable: string;
|
|
53
|
-
readonly fixedScopes?: Readonly<Record<string,
|
|
56
|
+
readonly fixedScopes?: Readonly<Record<string, string>>;
|
|
54
57
|
};
|
|
55
58
|
readonly units: readonly string[];
|
|
56
59
|
}
|
|
@@ -384,46 +387,48 @@ export function registerRemoteQuery<Params>(
|
|
|
384
387
|
);
|
|
385
388
|
}
|
|
386
389
|
const coverage = descriptor.coverage(params);
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
)
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
390
|
+
// A unit names every variable of a table it lets a query read in
|
|
391
|
+
// full: a variable left open would read rows outside the grant, and
|
|
392
|
+
// the wildcard fence (§3.2) has no counterpart in a SQL read.
|
|
393
|
+
const exact = (
|
|
394
|
+
entry: RemoteQueryCoverage,
|
|
395
|
+
declared: ReadonlySet<string>,
|
|
396
|
+
): boolean => {
|
|
397
|
+
const variables = new Set([
|
|
398
|
+
entry.base.variable,
|
|
399
|
+
...Object.keys(entry.base.fixedScopes ?? {}),
|
|
400
|
+
]);
|
|
401
|
+
return (
|
|
402
|
+
variables.size === declared.size &&
|
|
403
|
+
[...declared].every((variable) => variables.has(variable))
|
|
404
|
+
);
|
|
405
|
+
};
|
|
406
|
+
const declared = descriptor.tables.map(
|
|
407
|
+
(name) => schema.tables.get(name)?.declaredVariables,
|
|
408
|
+
);
|
|
409
|
+
if (
|
|
410
|
+
declared.some(
|
|
411
|
+
(variables) =>
|
|
412
|
+
variables === undefined ||
|
|
413
|
+
!coverage.some((entry) => exact(entry, variables)),
|
|
414
|
+
)
|
|
415
|
+
) {
|
|
401
416
|
throw syncError(
|
|
402
417
|
'operation.invalid_request',
|
|
403
418
|
'scoped remote query lacks complete generated scope coverage',
|
|
404
419
|
);
|
|
405
420
|
}
|
|
406
421
|
for (const entry of coverage) {
|
|
407
|
-
const table = schema.tables.get(entry.base.table);
|
|
408
422
|
const fixedScopes = entry.base.fixedScopes ?? {};
|
|
409
|
-
const coveredVariables = new Set([
|
|
410
|
-
entry.base.variable,
|
|
411
|
-
...Object.keys(fixedScopes),
|
|
412
|
-
]);
|
|
413
423
|
if (
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
entry.base.variable,
|
|
418
|
-
) ||
|
|
419
|
-
coveredVariables.size !== table.declaredVariables.size ||
|
|
420
|
-
[...table.declaredVariables].some(
|
|
421
|
-
(variable) => !coveredVariables.has(variable),
|
|
424
|
+
Object.hasOwn(fixedScopes, entry.base.variable) ||
|
|
425
|
+
!declared.some(
|
|
426
|
+
(variables) => variables !== undefined && exact(entry, variables),
|
|
422
427
|
)
|
|
423
428
|
) {
|
|
424
429
|
throw syncError(
|
|
425
430
|
'operation.invalid_request',
|
|
426
|
-
'scoped remote query
|
|
431
|
+
'scoped remote query has invalid generated scope coverage',
|
|
427
432
|
);
|
|
428
433
|
}
|
|
429
434
|
if (entry.units.length === 0) {
|
|
@@ -432,18 +437,15 @@ export function registerRemoteQuery<Params>(
|
|
|
432
437
|
'scoped remote query has an empty scope unit',
|
|
433
438
|
);
|
|
434
439
|
}
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
) {
|
|
445
|
-
throw syncError('operation.forbidden');
|
|
446
|
-
}
|
|
440
|
+
if (
|
|
441
|
+
entry.units.some(
|
|
442
|
+
(value) => !scopeAllowed(allowed, entry.base.variable, value),
|
|
443
|
+
) ||
|
|
444
|
+
Object.entries(fixedScopes).some(
|
|
445
|
+
([variable, value]) => !scopeAllowed(allowed, variable, value),
|
|
446
|
+
)
|
|
447
|
+
) {
|
|
448
|
+
throw syncError('operation.forbidden');
|
|
447
449
|
}
|
|
448
450
|
}
|
|
449
451
|
} else if (
|