@syncular/server 0.59.0 → 0.60.1

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.
Files changed (54) hide show
  1. package/README.md +20 -16
  2. package/dist/admin.d.ts +32 -20
  3. package/dist/admin.js +42 -40
  4. package/dist/context.d.ts +6 -8
  5. package/dist/context.js +1 -3
  6. package/dist/d1-storage.js +42 -24
  7. package/dist/errors.js +8 -0
  8. package/dist/events.d.ts +35 -17
  9. package/dist/frame-bytes.js +24 -6
  10. package/dist/handler.js +486 -223
  11. package/dist/local-authority.js +7 -9
  12. package/dist/operations.d.ts +5 -2
  13. package/dist/operations.js +20 -29
  14. package/dist/postgres-storage.d.ts +24 -19
  15. package/dist/postgres-storage.js +249 -168
  16. package/dist/pull.d.ts +68 -53
  17. package/dist/pull.js +258 -287
  18. package/dist/push.d.ts +12 -3
  19. package/dist/push.js +95 -32
  20. package/dist/realtime.d.ts +17 -11
  21. package/dist/realtime.js +65 -90
  22. package/dist/relational-rows.d.ts +0 -57
  23. package/dist/relational-rows.js +4 -100
  24. package/dist/seed.js +10 -15
  25. package/dist/segment-download.d.ts +4 -1
  26. package/dist/segment-download.js +4 -2
  27. package/dist/sqlite-dialect.d.ts +36 -59
  28. package/dist/sqlite-dialect.js +255 -159
  29. package/dist/sqlite-storage.js +24 -20
  30. package/dist/storage-query.d.ts +30 -1
  31. package/dist/storage-query.js +68 -0
  32. package/dist/storage.d.ts +156 -54
  33. package/dist/storage.js +15 -0
  34. package/package.json +2 -2
  35. package/src/admin.ts +87 -59
  36. package/src/context.ts +7 -11
  37. package/src/d1-storage.ts +49 -33
  38. package/src/errors.ts +8 -0
  39. package/src/events.ts +36 -17
  40. package/src/frame-bytes.ts +26 -6
  41. package/src/handler.ts +628 -286
  42. package/src/local-authority.ts +10 -11
  43. package/src/operations.ts +44 -42
  44. package/src/postgres-storage.ts +336 -226
  45. package/src/pull.ts +394 -414
  46. package/src/push.ts +116 -21
  47. package/src/realtime.ts +85 -129
  48. package/src/relational-rows.ts +4 -107
  49. package/src/seed.ts +13 -19
  50. package/src/segment-download.ts +8 -3
  51. package/src/sqlite-dialect.ts +363 -204
  52. package/src/sqlite-storage.ts +45 -25
  53. package/src/storage-query.ts +102 -0
  54. package/src/storage.ts +185 -58
@@ -5,11 +5,12 @@
5
5
  * server with `attach` (§12.3).
6
6
  */
7
7
  import {
8
- decodeMessage,
8
+ decodeResponse,
9
9
  decodeRow,
10
- encodeMessage,
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 = decodeMessage(
289
+ const message = decodeResponse(
289
290
  await transport(
290
- encodeMessage({
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, readonly 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
- const coverageByTable = new Map<string, RemoteQueryCoverage>();
388
- for (const entry of coverage) {
389
- if (
390
- coverageByTable.has(entry.base.table) ||
391
- !descriptor.tables.includes(entry.base.table)
392
- ) {
393
- throw syncError(
394
- 'operation.invalid_request',
395
- 'scoped remote query has invalid generated scope coverage',
396
- );
397
- }
398
- coverageByTable.set(entry.base.table, entry);
399
- }
400
- if (descriptor.tables.some((table) => !coverageByTable.has(table))) {
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
- table === undefined ||
415
- Object.prototype.hasOwnProperty.call(
416
- fixedScopes,
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 lacks complete generated scope coverage',
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
- for (const value of entry.units) {
436
- if (!scopeAllowed(allowed, entry.base.variable, value)) {
437
- throw syncError('operation.forbidden');
438
- }
439
- }
440
- for (const [variable, values] of Object.entries(fixedScopes)) {
441
- if (
442
- values.length === 0 ||
443
- values.some((value) => !scopeAllowed(allowed, variable, value))
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 (