@syncular/server 0.57.0 → 0.59.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 +31 -22
- package/dist/admin.js +8 -4
- 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 +16 -0
- package/dist/context.js +21 -1
- package/dist/d1-storage.d.ts +14 -98
- package/dist/d1-storage.js +223 -1111
- package/dist/errors.js +8 -0
- package/dist/events.d.ts +1 -1
- package/dist/handler.js +135 -103
- package/dist/local-authority.d.ts +2 -0
- package/dist/local-authority.js +17 -3
- package/dist/object-bucket.d.ts +2 -0
- package/dist/operations-realtime.js +69 -50
- 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 +1 -1
- package/dist/postgres-storage.js +182 -105
- package/dist/pull.d.ts +28 -20
- package/dist/pull.js +85 -87
- package/dist/push.js +272 -147
- package/dist/realtime.d.ts +68 -28
- package/dist/realtime.js +297 -136
- package/dist/relational-rows.d.ts +27 -15
- package/dist/relational-rows.js +38 -22
- 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 +1 -2
- package/dist/segment-download.js +18 -4
- 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 +150 -47
- package/dist/sqlite-dialect.js +987 -88
- package/dist/sqlite-lease-store.js +13 -18
- package/dist/sqlite-storage.d.ts +9 -62
- package/dist/sqlite-storage.js +293 -1117
- package/dist/storage.d.ts +32 -12
- package/package.json +2 -2
- package/src/admin.ts +16 -2
- 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 +32 -1
- package/src/d1-storage.ts +326 -1692
- package/src/errors.ts +8 -0
- package/src/events.ts +1 -1
- package/src/handler.ts +178 -127
- package/src/local-authority.ts +17 -3
- package/src/object-bucket.ts +2 -0
- package/src/operations-realtime.ts +85 -61
- package/src/pg-executor-pglite.ts +27 -23
- package/src/pg-executor.ts +5 -1
- package/src/postgres-storage.ts +231 -138
- package/src/pull.ts +133 -147
- package/src/push.ts +356 -205
- package/src/realtime.ts +380 -179
- package/src/relational-rows.ts +54 -27
- 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 +1 -2
- package/src/segment-download.ts +25 -4
- 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 +1607 -114
- package/src/sqlite-lease-store.ts +12 -23
- package/src/sqlite-storage.ts +360 -1811
- package/src/storage.ts +33 -12
package/src/errors.ts
CHANGED
|
@@ -212,6 +212,14 @@ export const ERROR_CATALOG: Readonly<Record<string, ErrorCatalogEntry>> = {
|
|
|
212
212
|
recommendedAction: 'splitBatch',
|
|
213
213
|
httpStatus: 400,
|
|
214
214
|
},
|
|
215
|
+
// §1.7 / D3: a request or watch session past a configured `ServerLimits`
|
|
216
|
+
// cap; `details.limit` names the cap.
|
|
217
|
+
'sync.limit_exceeded': {
|
|
218
|
+
category: 'invalid-request',
|
|
219
|
+
retryable: false,
|
|
220
|
+
recommendedAction: 'fixRequest',
|
|
221
|
+
httpStatus: 413,
|
|
222
|
+
},
|
|
215
223
|
'sync.not_found': {
|
|
216
224
|
category: 'not-found',
|
|
217
225
|
retryable: false,
|
package/src/events.ts
CHANGED
|
@@ -238,7 +238,7 @@ export interface BlobSweptEvent {
|
|
|
238
238
|
readonly partition: string;
|
|
239
239
|
/** Blobs deleted this pass (unreferenced and older than the grace period). */
|
|
240
240
|
readonly swept: number;
|
|
241
|
-
/**
|
|
241
|
+
/** Old candidates a live row still references (§5.9.4 reference index). */
|
|
242
242
|
readonly referenced: number;
|
|
243
243
|
/** The grace period applied (ms) — the upload→push race protection. */
|
|
244
244
|
readonly graceMs: number;
|
package/src/handler.ts
CHANGED
|
@@ -225,18 +225,13 @@ async function planRequest(
|
|
|
225
225
|
else if (frame.type === 'SUBSCRIPTION') subFrames.push(frame);
|
|
226
226
|
}
|
|
227
227
|
|
|
228
|
-
if (
|
|
229
|
-
request.wireVersion >= 2 &&
|
|
230
|
-
header.logEpoch === undefined &&
|
|
231
|
-
pushes.length > 0
|
|
232
|
-
) {
|
|
228
|
+
if (header.logEpoch === undefined && pushes.length > 0) {
|
|
233
229
|
throw syncError(
|
|
234
230
|
'sync.invalid_request',
|
|
235
231
|
'epoch acquisition requests must not carry push commits (§2.1)',
|
|
236
232
|
);
|
|
237
233
|
}
|
|
238
|
-
const epochReset =
|
|
239
|
-
request.wireVersion >= 2 && header.logEpoch !== registry.logEpoch;
|
|
234
|
+
const epochReset = header.logEpoch !== registry.logEpoch;
|
|
240
235
|
|
|
241
236
|
if (header.schemaVersion !== schema.version) {
|
|
242
237
|
// §2.4: no degraded encoding — answer with the schema floor (§1.6).
|
|
@@ -300,6 +295,34 @@ async function planRequest(
|
|
|
300
295
|
`request carries ${totalOperations} operations (cap ${limits.maxOperationsPerRequest}, §6.1)`,
|
|
301
296
|
);
|
|
302
297
|
}
|
|
298
|
+
// §1.7: subscription caps, checked before any subscription is planned.
|
|
299
|
+
if (subFrames.length > limits.maxSubscriptionsPerRequest) {
|
|
300
|
+
throw syncError(
|
|
301
|
+
'sync.limit_exceeded',
|
|
302
|
+
'request carries more subscriptions than the server accepts (§1.7)',
|
|
303
|
+
JSON.stringify({
|
|
304
|
+
limit: 'maxSubscriptionsPerRequest',
|
|
305
|
+
max: limits.maxSubscriptionsPerRequest,
|
|
306
|
+
actual: subFrames.length,
|
|
307
|
+
}),
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
for (const frame of subFrames) {
|
|
311
|
+
let values = 0;
|
|
312
|
+
for (const list of Object.values(frame.scopes)) values += list.length;
|
|
313
|
+
if (values > limits.maxScopeValuesPerSubscription) {
|
|
314
|
+
throw syncError(
|
|
315
|
+
'sync.limit_exceeded',
|
|
316
|
+
'subscription requests more scope values than the server accepts (§1.7)',
|
|
317
|
+
JSON.stringify({
|
|
318
|
+
limit: 'maxScopeValuesPerSubscription',
|
|
319
|
+
max: limits.maxScopeValuesPerSubscription,
|
|
320
|
+
actual: values,
|
|
321
|
+
subscriptionId: frame.id,
|
|
322
|
+
}),
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
303
326
|
|
|
304
327
|
if (pull !== undefined) {
|
|
305
328
|
// §4.2: rows support is mandatory (bits 0 and 1) — reject its absence
|
|
@@ -357,7 +380,11 @@ async function planRequest(
|
|
|
357
380
|
const subscriptions: SubscriptionPlan[] = validated.map(({ frame }) => {
|
|
358
381
|
const table = schema.tables.get(frame.table);
|
|
359
382
|
if (table === undefined) throw new Error('unreachable: validated table');
|
|
360
|
-
const outcome = computeEffective(
|
|
383
|
+
const outcome = computeEffective(
|
|
384
|
+
table.declaredVariables,
|
|
385
|
+
frame.scopes,
|
|
386
|
+
resolved,
|
|
387
|
+
);
|
|
361
388
|
if (outcome.status === 'active') {
|
|
362
389
|
return { frame, table, status: 'active', effective: outcome.effective };
|
|
363
390
|
}
|
|
@@ -383,6 +410,12 @@ async function planRequest(
|
|
|
383
410
|
interface RequestReport {
|
|
384
411
|
outcome: 'ok' | 'schema_floor' | 'epoch_acquired' | 'reset' | 'error';
|
|
385
412
|
errorCode?: string;
|
|
413
|
+
/**
|
|
414
|
+
* The completed round's client-record write (§4.5, §8.1). Set only by a
|
|
415
|
+
* round that streamed without an in-band error; runs after the serve-gate
|
|
416
|
+
* verification.
|
|
417
|
+
*/
|
|
418
|
+
persist?: () => Promise<void>;
|
|
386
419
|
}
|
|
387
420
|
|
|
388
421
|
function pushResultDetailsFrame(
|
|
@@ -470,12 +503,12 @@ async function* streamResponse(
|
|
|
470
503
|
plan: RequestPlan,
|
|
471
504
|
ctx: SyncRequestContext,
|
|
472
505
|
schema: CompiledSchema,
|
|
473
|
-
report
|
|
506
|
+
report: RequestReport,
|
|
474
507
|
): AsyncGenerator<Uint8Array> {
|
|
475
508
|
const events = ctx.events;
|
|
476
509
|
yield responseEnvelopeHeader(plan.wireVersion);
|
|
477
510
|
if (plan.schemaFloor) {
|
|
478
|
-
|
|
511
|
+
report.outcome = 'schema_floor';
|
|
479
512
|
yield encodeResponseFrame(
|
|
480
513
|
{
|
|
481
514
|
type: 'RESP_HEADER',
|
|
@@ -483,9 +516,8 @@ async function* streamResponse(
|
|
|
483
516
|
-1,
|
|
484
517
|
)!.version,
|
|
485
518
|
latestSchemaVersion: ctx.schema.version,
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
: {}),
|
|
519
|
+
logEpoch: plan.logEpoch,
|
|
520
|
+
resetRequired: plan.epochReset,
|
|
489
521
|
},
|
|
490
522
|
plan.wireVersion,
|
|
491
523
|
);
|
|
@@ -496,17 +528,14 @@ async function* streamResponse(
|
|
|
496
528
|
{
|
|
497
529
|
type: 'RESP_HEADER',
|
|
498
530
|
latestSchemaVersion: ctx.schema.version,
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
: {}),
|
|
531
|
+
logEpoch: plan.logEpoch,
|
|
532
|
+
resetRequired: plan.epochReset,
|
|
502
533
|
},
|
|
503
534
|
plan.wireVersion,
|
|
504
535
|
);
|
|
505
536
|
if (plan.epochReset) {
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
plan.header.logEpoch === undefined ? 'epoch_acquired' : 'reset';
|
|
509
|
-
}
|
|
537
|
+
report.outcome =
|
|
538
|
+
plan.header.logEpoch === undefined ? 'epoch_acquired' : 'reset';
|
|
510
539
|
yield END_FRAME_BYTES;
|
|
511
540
|
return;
|
|
512
541
|
}
|
|
@@ -612,78 +641,95 @@ async function* streamResponse(
|
|
|
612
641
|
JSON.stringify({ acceptedThrough, storageMaxCommitSeq: maxSeq }),
|
|
613
642
|
);
|
|
614
643
|
const horizonSeq = await ctx.storage.getHorizonSeq(ctx.partition);
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
644
|
+
// A window of sections runs its pre-frame reads together, so a batching
|
|
645
|
+
// storage answers them in one statement per table. The window holds at
|
|
646
|
+
// most 100,000 first-page or window rows (two maximal §4.2 pages): a
|
|
647
|
+
// request's read memory follows the configured limits, never its
|
|
648
|
+
// subscription count.
|
|
649
|
+
const window = Math.max(
|
|
650
|
+
1,
|
|
651
|
+
Math.floor(
|
|
652
|
+
100_000 /
|
|
653
|
+
(Math.max(limits.limitSnapshotRows, limits.limitCommits) + 1),
|
|
654
|
+
),
|
|
623
655
|
);
|
|
624
|
-
for (
|
|
625
|
-
const
|
|
626
|
-
|
|
627
|
-
const reads = prepared[index];
|
|
628
|
-
if (reads === undefined) throw new Error('prepareSections lost a plan');
|
|
629
|
-
const section = preparedSection(
|
|
656
|
+
for (let start = 0; start < plan.subscriptions.length; start += window) {
|
|
657
|
+
const batch = plan.subscriptions.slice(start, start + window);
|
|
658
|
+
const prepared = await prepareSections(
|
|
630
659
|
ctx,
|
|
631
660
|
schema,
|
|
632
661
|
limits,
|
|
633
|
-
|
|
662
|
+
batch,
|
|
634
663
|
maxSeq,
|
|
635
|
-
|
|
636
|
-
trace,
|
|
664
|
+
horizonSeq,
|
|
637
665
|
plan.logEpoch,
|
|
638
666
|
);
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
667
|
+
for (const [index, subscription] of batch.entries()) {
|
|
668
|
+
const trace: PullSectionTrace | undefined =
|
|
669
|
+
summaries !== undefined ? { segments: [] } : undefined;
|
|
670
|
+
const reads = prepared[index];
|
|
671
|
+
if (reads === undefined) {
|
|
672
|
+
throw new Error('prepareSections lost a plan');
|
|
673
|
+
}
|
|
674
|
+
const section = preparedSection(
|
|
675
|
+
ctx,
|
|
676
|
+
schema,
|
|
677
|
+
limits,
|
|
678
|
+
subscription,
|
|
679
|
+
maxSeq,
|
|
680
|
+
reads,
|
|
681
|
+
trace,
|
|
682
|
+
plan.logEpoch,
|
|
683
|
+
);
|
|
684
|
+
let status: 'active' | 'revoked' | 'reset' = 'active';
|
|
685
|
+
let bootstrap = false;
|
|
686
|
+
let commits = 0;
|
|
687
|
+
let changes = 0;
|
|
688
|
+
let effectiveScopes: ScopeMap = {};
|
|
689
|
+
let firstCommitSeq: number | undefined;
|
|
690
|
+
let lastCommitSeq: number | undefined;
|
|
691
|
+
let step = await section.next();
|
|
692
|
+
while (!step.done) {
|
|
693
|
+
const frame = step.value as ResponseFrame;
|
|
694
|
+
if (summaries !== undefined) {
|
|
695
|
+
if (frame.type === 'SUB_START') {
|
|
696
|
+
status = frame.status;
|
|
697
|
+
bootstrap = frame.bootstrap;
|
|
698
|
+
effectiveScopes = frame.effectiveScopes;
|
|
699
|
+
} else if (frame.type === 'COMMIT') {
|
|
700
|
+
commits += 1;
|
|
701
|
+
changes += frame.changes.length;
|
|
702
|
+
firstCommitSeq ??= frame.commitSeq;
|
|
703
|
+
lastCommitSeq = frame.commitSeq;
|
|
704
|
+
}
|
|
659
705
|
}
|
|
706
|
+
yield encodeResponseFrame(frame, plan.wireVersion);
|
|
707
|
+
step = await section.next();
|
|
708
|
+
}
|
|
709
|
+
if (step.value.active) cursors.push(step.value.nextCursor);
|
|
710
|
+
if (summaries !== undefined && trace !== undefined) {
|
|
711
|
+
summaries.push({
|
|
712
|
+
id: subscription.frame.id,
|
|
713
|
+
table: subscription.frame.table,
|
|
714
|
+
status,
|
|
715
|
+
mode:
|
|
716
|
+
status !== 'active'
|
|
717
|
+
? 'none'
|
|
718
|
+
: bootstrap
|
|
719
|
+
? 'bootstrap'
|
|
720
|
+
: 'incremental',
|
|
721
|
+
requestedScopes: subscription.frame.scopes,
|
|
722
|
+
effectiveScopes,
|
|
723
|
+
...(firstCommitSeq !== undefined && lastCommitSeq !== undefined
|
|
724
|
+
? { firstCommitSeq, lastCommitSeq }
|
|
725
|
+
: {}),
|
|
726
|
+
fromCursor: subscription.frame.cursor,
|
|
727
|
+
nextCursor: step.value.nextCursor,
|
|
728
|
+
commits,
|
|
729
|
+
changes,
|
|
730
|
+
segments: trace.segments,
|
|
731
|
+
});
|
|
660
732
|
}
|
|
661
|
-
yield encodeResponseFrame(frame, plan.wireVersion);
|
|
662
|
-
step = await section.next();
|
|
663
|
-
}
|
|
664
|
-
if (step.value.active) cursors.push(step.value.nextCursor);
|
|
665
|
-
if (summaries !== undefined && trace !== undefined) {
|
|
666
|
-
summaries.push({
|
|
667
|
-
id: subscription.frame.id,
|
|
668
|
-
table: subscription.frame.table,
|
|
669
|
-
status,
|
|
670
|
-
mode:
|
|
671
|
-
status !== 'active'
|
|
672
|
-
? 'none'
|
|
673
|
-
: bootstrap
|
|
674
|
-
? 'bootstrap'
|
|
675
|
-
: 'incremental',
|
|
676
|
-
requestedScopes: subscription.frame.scopes,
|
|
677
|
-
effectiveScopes,
|
|
678
|
-
...(firstCommitSeq !== undefined && lastCommitSeq !== undefined
|
|
679
|
-
? { firstCommitSeq, lastCommitSeq }
|
|
680
|
-
: {}),
|
|
681
|
-
fromCursor: subscription.frame.cursor,
|
|
682
|
-
nextCursor: step.value.nextCursor,
|
|
683
|
-
commits,
|
|
684
|
-
changes,
|
|
685
|
-
segments: trace.segments,
|
|
686
|
-
});
|
|
687
733
|
}
|
|
688
734
|
}
|
|
689
735
|
if (events !== undefined && summaries !== undefined) {
|
|
@@ -700,55 +746,59 @@ async function* streamResponse(
|
|
|
700
746
|
}
|
|
701
747
|
}
|
|
702
748
|
|
|
703
|
-
// §4.5/§8.1:
|
|
704
|
-
//
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
);
|
|
709
|
-
const subscriptions: ClientSubscription[] =
|
|
710
|
-
plan.pull !== undefined
|
|
711
|
-
? plan.subscriptions.map((s) => ({
|
|
712
|
-
id: s.frame.id,
|
|
713
|
-
table: s.frame.table,
|
|
714
|
-
scopes: s.frame.scopes,
|
|
715
|
-
schemaVersion: schema.version,
|
|
716
|
-
}))
|
|
717
|
-
: [...(previous?.subscriptions ?? [])];
|
|
718
|
-
const cursor =
|
|
719
|
-
cursors.length > 0 ? Math.min(...cursors) : (previous?.cursor ?? -1);
|
|
720
|
-
await ctx.storage.putClientRecord(
|
|
721
|
-
ctx.partition,
|
|
722
|
-
{
|
|
723
|
-
clientId: plan.header.clientId,
|
|
724
|
-
actorId: ctx.actorId,
|
|
725
|
-
wireVersion: plan.wireVersion,
|
|
726
|
-
cursor,
|
|
727
|
-
updatedAtMs: clockOf(ctx)(),
|
|
728
|
-
subscriptions,
|
|
729
|
-
},
|
|
730
|
-
// §4.5: a round that carried a `ROUND_FAILURE` report completes without
|
|
731
|
-
// clearing the reported code and streak; the next request without the
|
|
732
|
-
// frame clears them as a plain completed round does.
|
|
733
|
-
plan.roundFailure !== undefined ? { keepRoundOutcome: true } : undefined,
|
|
734
|
-
);
|
|
735
|
-
if (
|
|
736
|
-
ctx.realtime?.notifySubscriptions !== undefined &&
|
|
737
|
-
JSON.stringify(
|
|
738
|
-
(previous?.subscriptions ?? []).map((s) => [s.id, s.table, s.scopes]),
|
|
739
|
-
) !== JSON.stringify(subscriptions.map((s) => [s.id, s.table, s.scopes]))
|
|
740
|
-
) {
|
|
741
|
-
await ctx.realtime.notifySubscriptions(
|
|
749
|
+
// §4.5/§8.1: the cursor watermark and the request's subscription list
|
|
750
|
+
// per (partition, clientId), persisted only after the serve gate
|
|
751
|
+
// verified the round (§4.5: a failed round leaves the cursor alone).
|
|
752
|
+
report.persist = async () => {
|
|
753
|
+
const previous = await ctx.storage.getClientRecord(
|
|
742
754
|
ctx.partition,
|
|
743
755
|
plan.header.clientId,
|
|
744
756
|
);
|
|
745
|
-
|
|
757
|
+
const subscriptions: ClientSubscription[] =
|
|
758
|
+
plan.pull !== undefined
|
|
759
|
+
? plan.subscriptions.map((s) => ({
|
|
760
|
+
id: s.frame.id,
|
|
761
|
+
table: s.frame.table,
|
|
762
|
+
scopes: s.frame.scopes,
|
|
763
|
+
schemaVersion: schema.version,
|
|
764
|
+
}))
|
|
765
|
+
: [...(previous?.subscriptions ?? [])];
|
|
766
|
+
let cursor = cursors[0] ?? previous?.cursor ?? -1;
|
|
767
|
+
for (const next of cursors) cursor = Math.min(cursor, next);
|
|
768
|
+
await ctx.storage.putClientRecord(
|
|
769
|
+
ctx.partition,
|
|
770
|
+
{
|
|
771
|
+
clientId: plan.header.clientId,
|
|
772
|
+
actorId: ctx.actorId,
|
|
773
|
+
wireVersion: plan.wireVersion,
|
|
774
|
+
cursor,
|
|
775
|
+
updatedAtMs: clockOf(ctx)(),
|
|
776
|
+
subscriptions,
|
|
777
|
+
},
|
|
778
|
+
// §4.5: a round that carried a `ROUND_FAILURE` report completes
|
|
779
|
+
// without clearing the reported code and streak; the next request
|
|
780
|
+
// without the frame clears them as a plain completed round does.
|
|
781
|
+
plan.roundFailure !== undefined
|
|
782
|
+
? { keepRoundOutcome: true }
|
|
783
|
+
: undefined,
|
|
784
|
+
);
|
|
785
|
+
if (
|
|
786
|
+
ctx.realtime?.notifySubscriptions !== undefined &&
|
|
787
|
+
JSON.stringify(
|
|
788
|
+
(previous?.subscriptions ?? []).map((s) => [s.id, s.table, s.scopes]),
|
|
789
|
+
) !==
|
|
790
|
+
JSON.stringify(subscriptions.map((s) => [s.id, s.table, s.scopes]))
|
|
791
|
+
) {
|
|
792
|
+
await ctx.realtime.notifySubscriptions(
|
|
793
|
+
ctx.partition,
|
|
794
|
+
plan.header.clientId,
|
|
795
|
+
);
|
|
796
|
+
}
|
|
797
|
+
};
|
|
746
798
|
} catch (error) {
|
|
747
799
|
if (error instanceof SyncError) {
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
report.errorCode = error.code;
|
|
751
|
-
}
|
|
800
|
+
report.outcome = 'error';
|
|
801
|
+
report.errorCode = error.code;
|
|
752
802
|
await recordRoundFailure(ctx, plan.header.clientId, error);
|
|
753
803
|
// §1.6: in-band ERROR, then END, nothing else.
|
|
754
804
|
yield encodeResponseFrame(
|
|
@@ -982,6 +1032,7 @@ async function createStreamCore(
|
|
|
982
1032
|
);
|
|
983
1033
|
if (afterRefusal !== undefined) throw serveNotReadyError(afterRefusal);
|
|
984
1034
|
if (changed !== undefined) throw serveNotReadyError(changed);
|
|
1035
|
+
await report.persist?.();
|
|
985
1036
|
for (const chunk of buffered) yield chunk;
|
|
986
1037
|
};
|
|
987
1038
|
if (events === undefined) {
|
package/src/local-authority.ts
CHANGED
|
@@ -102,6 +102,7 @@ export type LocalAuthorityAttachDetails =
|
|
|
102
102
|
readonly retryable: boolean;
|
|
103
103
|
}
|
|
104
104
|
| { readonly reason: 'log_epoch_changed' }
|
|
105
|
+
| { readonly reason: 'undo_pending' }
|
|
105
106
|
| {
|
|
106
107
|
readonly reason: 'schema_floor';
|
|
107
108
|
readonly requiredSchemaVersion: number;
|
|
@@ -431,7 +432,13 @@ export async function openLocalAuthority(
|
|
|
431
432
|
uploading = false;
|
|
432
433
|
}
|
|
433
434
|
}
|
|
434
|
-
writeMeta({
|
|
435
|
+
writeMeta({
|
|
436
|
+
state: 'local',
|
|
437
|
+
attach_id: null,
|
|
438
|
+
sent: null,
|
|
439
|
+
applied: null,
|
|
440
|
+
undo: null,
|
|
441
|
+
});
|
|
435
442
|
state = 'local';
|
|
436
443
|
},
|
|
437
444
|
};
|
|
@@ -451,6 +458,10 @@ export async function openLocalAuthority(
|
|
|
451
458
|
state = 'local';
|
|
452
459
|
throw error;
|
|
453
460
|
}
|
|
461
|
+
} else if (readMeta('undo') !== undefined) {
|
|
462
|
+
// §12.3: resuming after a started rollback would count `cached`
|
|
463
|
+
// uploads whose rows the rollback deleted.
|
|
464
|
+
throw new LocalAuthorityAttachError({ reason: 'undo_pending' });
|
|
454
465
|
}
|
|
455
466
|
const attachId = attachIdOf();
|
|
456
467
|
const commits = planAttach(database, compiled, attachId);
|
|
@@ -479,7 +490,8 @@ export async function openLocalAuthority(
|
|
|
479
490
|
|
|
480
491
|
/**
|
|
481
492
|
* §12.3 abandon: settle the one commit whose outcome may be unknown, then
|
|
482
|
-
* delete the rows of every applied commit, children before parents
|
|
493
|
+
* delete the rows of every applied commit, children before parents, under
|
|
494
|
+
* commit ids fresh to this attempt (the persisted `undo` marker).
|
|
483
495
|
*/
|
|
484
496
|
async function rollback(
|
|
485
497
|
transport: (request: Uint8Array) => Promise<Uint8Array>,
|
|
@@ -505,6 +517,8 @@ export async function openLocalAuthority(
|
|
|
505
517
|
op: 'delete',
|
|
506
518
|
}),
|
|
507
519
|
);
|
|
520
|
+
const attempt = ordinal('undo') + 1;
|
|
521
|
+
writeMeta({ undo: String(attempt) });
|
|
508
522
|
for (
|
|
509
523
|
let start = 0;
|
|
510
524
|
start < deletes.length;
|
|
@@ -512,7 +526,7 @@ export async function openLocalAuthority(
|
|
|
512
526
|
) {
|
|
513
527
|
const commit: PushCommitFrame = {
|
|
514
528
|
type: 'PUSH_COMMIT',
|
|
515
|
-
clientCommitId: `${attachId}:undo:${start / ATTACH_OPERATION_BUDGET}`,
|
|
529
|
+
clientCommitId: `${attachId}:undo:${attempt}:${start / ATTACH_OPERATION_BUDGET}`,
|
|
516
530
|
operations: deletes.slice(start, start + ATTACH_OPERATION_BUDGET),
|
|
517
531
|
};
|
|
518
532
|
const result = await send(commit);
|
package/src/object-bucket.ts
CHANGED
|
@@ -34,6 +34,8 @@ export interface BucketObject {
|
|
|
34
34
|
export interface BucketObjectHead {
|
|
35
35
|
readonly etag: string | undefined;
|
|
36
36
|
readonly size: number | undefined;
|
|
37
|
+
/** Backend last-modified time (epoch ms): S3 `Last-Modified`, R2 `uploaded`. */
|
|
38
|
+
readonly lastModifiedMs: number | undefined;
|
|
37
39
|
readonly metadata: Readonly<Record<string, string>>;
|
|
38
40
|
}
|
|
39
41
|
|