@syncular/client 0.48.0 → 0.49.2
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 +50 -1
- package/dist/availability.d.ts +1 -1
- package/dist/availability.js +1 -1
- package/dist/client.js +18 -3
- package/dist/errors.d.ts +5 -0
- package/dist/errors.js +5 -0
- package/dist/leader-lock.d.ts +18 -7
- package/dist/leader-lock.js +40 -22
- package/dist/multi-tab.d.ts +158 -22
- package/dist/multi-tab.js +361 -10
- package/dist/outbox.d.ts +5 -0
- package/dist/outbox.js +11 -3
- package/dist/outcomes.js +6 -2
- package/dist/previous-version.d.ts +3 -1
- package/dist/previous-version.js +24 -5
- package/dist/reactive-store.js +2 -1
- package/dist/wasm-database.d.ts +6 -0
- package/dist/wasm-database.js +36 -6
- package/dist/worker-entry.d.ts +17 -1
- package/dist/worker-entry.js +74 -4
- package/dist/worker-host.d.ts +45 -26
- package/dist/worker-host.js +172 -310
- package/dist/worker-protocol.d.ts +22 -0
- package/dist/worker-protocol.js +2 -0
- package/package.json +3 -3
- package/src/availability.ts +3 -2
- package/src/client.ts +26 -3
- package/src/errors.ts +6 -0
- package/src/leader-lock.ts +64 -34
- package/src/multi-tab.ts +576 -34
- package/src/outbox.ts +16 -2
- package/src/outcomes.ts +6 -2
- package/src/previous-version.ts +32 -5
- package/src/reactive-store.ts +2 -1
- package/src/wasm-database.ts +64 -6
- package/src/worker-entry.ts +124 -4
- package/src/worker-host.ts +219 -379
- package/src/worker-protocol.ts +24 -0
package/dist/multi-tab.js
CHANGED
|
@@ -20,7 +20,8 @@
|
|
|
20
20
|
* — "I am the leader, this epoch";
|
|
21
21
|
* the answer to every hello
|
|
22
22
|
* res {t,epoch,reqId,ok,value|error} — reply to one req
|
|
23
|
-
* event {t,epoch,event}
|
|
23
|
+
* event {t,epoch,event,to?} — fan-out (invalidate/presence/…),
|
|
24
|
+
* or one tab's event when `to` is set
|
|
24
25
|
*
|
|
25
26
|
* Epoch (leader generation token): a monotonic counter carried in a shared
|
|
26
27
|
* BroadcastChannel and bumped by every promotion. Followers stamp requests
|
|
@@ -57,8 +58,14 @@
|
|
|
57
58
|
* is exactly ONE presence peer collectively — `(actorId, leaderClientId)`.
|
|
58
59
|
* A follower's `setPresence` forwards to the leader's single publisher; there
|
|
59
60
|
* is no per-tab presence peer (documented in the web-client README).
|
|
61
|
+
*
|
|
62
|
+
* Tab lifetime: every tab of a {@link TabCoordinator} holds an exclusive Web
|
|
63
|
+
* Lock named for its tab id until it closes. A leader whose core keeps per-tab
|
|
64
|
+
* state waits on that lock for each tab that called it; the grant means the
|
|
65
|
+
* tab is gone.
|
|
60
66
|
*/
|
|
61
67
|
import { ClientSyncError } from './errors.js';
|
|
68
|
+
import { webLocksLeaderLock, } from './leader-lock.js';
|
|
62
69
|
import { WORKER_FAILED_CODE } from './worker-protocol.js';
|
|
63
70
|
/** Client-local: a follower call could not reach a leader before its deadline. */
|
|
64
71
|
export const FOLLOWER_TIMEOUT_CODE = 'client.follower_timeout';
|
|
@@ -66,12 +73,16 @@ export const FOLLOWER_TIMEOUT_CODE = 'client.follower_timeout';
|
|
|
66
73
|
export const LEADER_INCOMPATIBLE_CODE = 'client.leader_incompatible';
|
|
67
74
|
/** Client-local: the leader that received a call stopped leading before it answered. */
|
|
68
75
|
export const LEADER_HANDOVER_CODE = 'client.leader_handover';
|
|
76
|
+
/** Client-local: another tab took this tab's leadership with `takeOver()`. */
|
|
77
|
+
export const LEADERSHIP_TAKEN_CODE = 'client.leadership_taken';
|
|
78
|
+
/** Client-local: `takeOver()` was called while its conditions do not hold. */
|
|
79
|
+
export const TAKEOVER_REFUSED_CODE = 'client.takeover_refused';
|
|
69
80
|
/**
|
|
70
81
|
* Version of the leader/follower wire and the forwarded handle API. Bump it
|
|
71
82
|
* with every change to a message shape or to a forwarded method's arguments
|
|
72
83
|
* or result, so tabs of different builds never serve each other.
|
|
73
84
|
*/
|
|
74
|
-
export const MULTI_TAB_PROTOCOL_VERSION =
|
|
85
|
+
export const MULTI_TAB_PROTOCOL_VERSION = 2;
|
|
75
86
|
/**
|
|
76
87
|
* Order a peer's identity against our own: -1 older, 0 same, 1 newer. A peer
|
|
77
88
|
* from before protocol 1 sends no identity and is older.
|
|
@@ -110,11 +121,11 @@ export function newTabId() {
|
|
|
110
121
|
// ---------------------------------------------------------------------------
|
|
111
122
|
/**
|
|
112
123
|
* Runs on the leader tab. Announces leadership, answers follower `req`s by
|
|
113
|
-
* invoking the (already-running)
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
* newer tab calls `onNewerTab`, whose owner closes the core and
|
|
117
|
-
* lock.
|
|
124
|
+
* invoking the (already-running) core via `invoke` with the calling tab's id,
|
|
125
|
+
* and relays core events to followers. Owns nothing about the core lifecycle;
|
|
126
|
+
* this is a relay. `onFirstCall` runs once per tab, at its first request. A
|
|
127
|
+
* hello from a newer tab calls `onNewerTab`, whose owner closes the core and
|
|
128
|
+
* releases the lock.
|
|
118
129
|
*/
|
|
119
130
|
export class LeaderBridge {
|
|
120
131
|
#channel;
|
|
@@ -122,8 +133,10 @@ export class LeaderBridge {
|
|
|
122
133
|
#clientId;
|
|
123
134
|
#identity;
|
|
124
135
|
#invoke;
|
|
136
|
+
#onFirstCall;
|
|
125
137
|
#onNewerTab;
|
|
126
138
|
#onMessage;
|
|
139
|
+
#callers = new Set();
|
|
127
140
|
#closed = false;
|
|
128
141
|
constructor(options) {
|
|
129
142
|
this.#channel = options.channel;
|
|
@@ -131,6 +144,7 @@ export class LeaderBridge {
|
|
|
131
144
|
this.#clientId = options.clientId;
|
|
132
145
|
this.#identity = options.identity;
|
|
133
146
|
this.#invoke = options.invoke;
|
|
147
|
+
this.#onFirstCall = options.onFirstCall;
|
|
134
148
|
this.#onNewerTab = options.onNewerTab;
|
|
135
149
|
this.#onMessage = (event) => this.#handle(event.data);
|
|
136
150
|
this.#channel.addEventListener('message', this.#onMessage);
|
|
@@ -147,12 +161,23 @@ export class LeaderBridge {
|
|
|
147
161
|
...this.#identity,
|
|
148
162
|
});
|
|
149
163
|
}
|
|
150
|
-
/** Fan
|
|
164
|
+
/** Fan an event out to all followers on the current epoch. */
|
|
151
165
|
broadcastEvent(event) {
|
|
152
166
|
if (this.#closed)
|
|
153
167
|
return;
|
|
154
168
|
this.#channel.postMessage({ t: 'event', epoch: this.#epoch, event });
|
|
155
169
|
}
|
|
170
|
+
/** Post an event that only the follower `tabId` delivers. */
|
|
171
|
+
sendEvent(tabId, event) {
|
|
172
|
+
if (this.#closed)
|
|
173
|
+
return;
|
|
174
|
+
this.#channel.postMessage({
|
|
175
|
+
t: 'event',
|
|
176
|
+
epoch: this.#epoch,
|
|
177
|
+
event,
|
|
178
|
+
to: tabId,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
156
181
|
#handle(message) {
|
|
157
182
|
if (this.#closed)
|
|
158
183
|
return;
|
|
@@ -172,7 +197,7 @@ export class LeaderBridge {
|
|
|
172
197
|
// follower will re-stamp once it sees our announce.
|
|
173
198
|
if (message.epoch !== this.#epoch)
|
|
174
199
|
return;
|
|
175
|
-
const { reqId, method, args } = message;
|
|
200
|
+
const { reqId, method, args, fromId } = message;
|
|
176
201
|
if (compareTabIdentity(message, this.#identity) !== 0) {
|
|
177
202
|
this.#channel.postMessage({
|
|
178
203
|
t: 'res',
|
|
@@ -187,8 +212,12 @@ export class LeaderBridge {
|
|
|
187
212
|
});
|
|
188
213
|
return;
|
|
189
214
|
}
|
|
215
|
+
if (!this.#callers.has(fromId)) {
|
|
216
|
+
this.#callers.add(fromId);
|
|
217
|
+
this.#onFirstCall?.(fromId);
|
|
218
|
+
}
|
|
190
219
|
// A call can outlive close(); a closed BroadcastChannel throws on post.
|
|
191
|
-
this.#invoke(method, args).then((value) => {
|
|
220
|
+
this.#invoke(fromId, method, args).then((value) => {
|
|
192
221
|
if (this.#closed)
|
|
193
222
|
return;
|
|
194
223
|
this.#channel.postMessage({
|
|
@@ -480,6 +509,8 @@ export class FollowerLink {
|
|
|
480
509
|
if (message.t === 'event') {
|
|
481
510
|
if (message.epoch !== this.#epoch)
|
|
482
511
|
return;
|
|
512
|
+
if (message.to !== undefined && message.to !== this.#fromId)
|
|
513
|
+
return;
|
|
483
514
|
this.#onEvent(message.event);
|
|
484
515
|
return;
|
|
485
516
|
}
|
|
@@ -648,3 +679,323 @@ export class FollowerLink {
|
|
|
648
679
|
this.#cancelBlocked = undefined;
|
|
649
680
|
}
|
|
650
681
|
}
|
|
682
|
+
function tabLockName(name, tabId) {
|
|
683
|
+
return `${name}:tab:${tabId}`;
|
|
684
|
+
}
|
|
685
|
+
function takeOverRefused(reason, takeOverAt) {
|
|
686
|
+
return new ClientSyncError(TAKEOVER_REFUSED_CODE, 'takeOver() requires a leader that has been unreachable for takeOverAfterMs', reason === 'too-early' || reason === 'in-progress', { reason, ...(takeOverAt !== undefined ? { takeOverAt } : {}) });
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* Elect one leader tab per `name` and run `lead` there; every other tab
|
|
690
|
+
* forwards its calls to that core and receives its events. When the leader
|
|
691
|
+
* tab closes, one follower promotes in place on a higher epoch and runs
|
|
692
|
+
* `lead` itself. Resolves once this tab leads or is bound to a leader.
|
|
693
|
+
*/
|
|
694
|
+
export async function createTabCoordinator(config) {
|
|
695
|
+
const lock = config.lock ?? webLocksLeaderLock();
|
|
696
|
+
const clock = config.clock ?? {
|
|
697
|
+
now: () => Date.now(),
|
|
698
|
+
schedule: (callback, delayMs) => {
|
|
699
|
+
const timer = setTimeout(callback, delayMs);
|
|
700
|
+
return () => clearTimeout(timer);
|
|
701
|
+
},
|
|
702
|
+
};
|
|
703
|
+
const callTimeoutMs = config.callTimeoutMs ?? DEFAULT_FOLLOWER_CALL_TIMEOUT_MS;
|
|
704
|
+
const takeOverAfterMs = config.takeOverAfterMs ?? 2 * callTimeoutMs;
|
|
705
|
+
const openChannel = config.channelFactory ?? broadcastChannelFactory();
|
|
706
|
+
const channelName = multiTabChannelName(config.name);
|
|
707
|
+
const identity = {
|
|
708
|
+
protocol: MULTI_TAB_PROTOCOL_VERSION,
|
|
709
|
+
schemaVersion: config.version,
|
|
710
|
+
};
|
|
711
|
+
const tabId = newTabId();
|
|
712
|
+
let state = {
|
|
713
|
+
state: 'waiting',
|
|
714
|
+
reason: 'leader-announcement',
|
|
715
|
+
};
|
|
716
|
+
let leading;
|
|
717
|
+
let follower;
|
|
718
|
+
let closed = false;
|
|
719
|
+
let takingOver = false;
|
|
720
|
+
/** Start of the current `leader-unreachable` period. */
|
|
721
|
+
let unreachableSince;
|
|
722
|
+
const report = (event) => {
|
|
723
|
+
try {
|
|
724
|
+
config.onLeaderUnreachable?.(event);
|
|
725
|
+
}
|
|
726
|
+
catch {
|
|
727
|
+
// A telemetry listener must never break cross-tab coordination.
|
|
728
|
+
}
|
|
729
|
+
};
|
|
730
|
+
const endUnreachable = (outcome) => {
|
|
731
|
+
if (unreachableSince === undefined)
|
|
732
|
+
return;
|
|
733
|
+
const at = clock.now();
|
|
734
|
+
report({ phase: 'ended', at, durationMs: at - unreachableSince, outcome });
|
|
735
|
+
unreachableSince = undefined;
|
|
736
|
+
};
|
|
737
|
+
const setState = (next) => {
|
|
738
|
+
state = next;
|
|
739
|
+
if (next.state === 'blocked' && next.reason === 'leader-unreachable') {
|
|
740
|
+
if (unreachableSince === undefined) {
|
|
741
|
+
unreachableSince = clock.now();
|
|
742
|
+
report({
|
|
743
|
+
phase: 'started',
|
|
744
|
+
at: unreachableSince,
|
|
745
|
+
takeOverAt: unreachableSince + takeOverAfterMs,
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
else if (next.state === 'follower') {
|
|
750
|
+
endUnreachable('rebound');
|
|
751
|
+
}
|
|
752
|
+
else if (next.state === 'leader') {
|
|
753
|
+
endUnreachable(takingOver ? 'taken-over' : 'promoted');
|
|
754
|
+
}
|
|
755
|
+
else if (next.state === 'blocked') {
|
|
756
|
+
endUnreachable('incompatible');
|
|
757
|
+
}
|
|
758
|
+
try {
|
|
759
|
+
config.onStateChange?.(next);
|
|
760
|
+
}
|
|
761
|
+
catch {
|
|
762
|
+
// A status listener must never break cross-tab coordination.
|
|
763
|
+
}
|
|
764
|
+
};
|
|
765
|
+
// Held until close: a leader waits on it to learn this tab is gone.
|
|
766
|
+
const tabLease = await lock.acquire(tabLockName(config.name, tabId));
|
|
767
|
+
const lead = async (lease, epoch, takeover) => {
|
|
768
|
+
let bridge;
|
|
769
|
+
let current = true;
|
|
770
|
+
let stepDown;
|
|
771
|
+
let lostWhileStarting = false;
|
|
772
|
+
const taken = {
|
|
773
|
+
state: 'blocked',
|
|
774
|
+
reason: 'leadership-taken',
|
|
775
|
+
code: LEADERSHIP_TAKEN_CODE,
|
|
776
|
+
retryable: true,
|
|
777
|
+
};
|
|
778
|
+
void lease.lost?.then(() => {
|
|
779
|
+
if (stepDown !== undefined)
|
|
780
|
+
stepDown(taken);
|
|
781
|
+
else
|
|
782
|
+
lostWhileStarting = true;
|
|
783
|
+
});
|
|
784
|
+
const leader = await config.lead({
|
|
785
|
+
epoch,
|
|
786
|
+
tabId,
|
|
787
|
+
takeover,
|
|
788
|
+
lease,
|
|
789
|
+
broadcast: (event) => {
|
|
790
|
+
if (!current)
|
|
791
|
+
return;
|
|
792
|
+
bridge?.broadcastEvent(event);
|
|
793
|
+
config.onEvent?.(event);
|
|
794
|
+
},
|
|
795
|
+
send: (to, event) => {
|
|
796
|
+
if (!current)
|
|
797
|
+
return;
|
|
798
|
+
if (to === tabId)
|
|
799
|
+
config.onEvent?.(event);
|
|
800
|
+
else
|
|
801
|
+
bridge?.sendEvent(to, event);
|
|
802
|
+
},
|
|
803
|
+
});
|
|
804
|
+
if (closed || lostWhileStarting) {
|
|
805
|
+
// Closed while the core was starting, or another tab took the lock: a
|
|
806
|
+
// follower link, if any, binds to that tab's announcement.
|
|
807
|
+
current = false;
|
|
808
|
+
await leader.close();
|
|
809
|
+
await lease.release();
|
|
810
|
+
return;
|
|
811
|
+
}
|
|
812
|
+
const ending = {
|
|
813
|
+
leader,
|
|
814
|
+
lease,
|
|
815
|
+
end: () => {
|
|
816
|
+
current = false;
|
|
817
|
+
bridge?.close();
|
|
818
|
+
},
|
|
819
|
+
};
|
|
820
|
+
leading = ending;
|
|
821
|
+
stepDown = (next) => {
|
|
822
|
+
// Stop the core and release the lock; this tab stays blocked until it
|
|
823
|
+
// reloads.
|
|
824
|
+
if (leading !== ending)
|
|
825
|
+
return;
|
|
826
|
+
leading = undefined;
|
|
827
|
+
ending.end();
|
|
828
|
+
setState(next);
|
|
829
|
+
void leader.close().finally(() => lease.release());
|
|
830
|
+
};
|
|
831
|
+
bridge = new LeaderBridge({
|
|
832
|
+
channel: openChannel(channelName),
|
|
833
|
+
epoch,
|
|
834
|
+
clientId: leader.clientId,
|
|
835
|
+
identity,
|
|
836
|
+
invoke: (from, method, args) => leader.call(from, method, args),
|
|
837
|
+
...(leader.tabClosed !== undefined
|
|
838
|
+
? {
|
|
839
|
+
onFirstCall: (from) => {
|
|
840
|
+
void lock
|
|
841
|
+
.acquire(tabLockName(config.name, from))
|
|
842
|
+
.then(async (gone) => {
|
|
843
|
+
await gone.release();
|
|
844
|
+
if (current)
|
|
845
|
+
leader.tabClosed?.(from);
|
|
846
|
+
});
|
|
847
|
+
},
|
|
848
|
+
}
|
|
849
|
+
: {}),
|
|
850
|
+
// A newer build asked for the core.
|
|
851
|
+
onNewerTab: () => stepDown?.({
|
|
852
|
+
state: 'blocked',
|
|
853
|
+
reason: 'leader-incompatible',
|
|
854
|
+
code: LEADER_INCOMPATIBLE_CODE,
|
|
855
|
+
leader: 'newer',
|
|
856
|
+
retryable: true,
|
|
857
|
+
}),
|
|
858
|
+
});
|
|
859
|
+
// Calls queued for the next leader run on this tab's own core.
|
|
860
|
+
follower?.handOver((method, args) => leader.call(tabId, method, args));
|
|
861
|
+
follower = undefined;
|
|
862
|
+
setState({ state: 'leader', clientId: leader.clientId });
|
|
863
|
+
};
|
|
864
|
+
const coordinator = {
|
|
865
|
+
tabId,
|
|
866
|
+
get state() {
|
|
867
|
+
return state;
|
|
868
|
+
},
|
|
869
|
+
call: (method, args) => {
|
|
870
|
+
if (closed) {
|
|
871
|
+
return Promise.reject(new ClientSyncError(WORKER_FAILED_CODE, 'the tab coordinator is closed'));
|
|
872
|
+
}
|
|
873
|
+
if (leading !== undefined)
|
|
874
|
+
return leading.leader.call(tabId, method, args);
|
|
875
|
+
if (follower !== undefined)
|
|
876
|
+
return follower.call(method, args);
|
|
877
|
+
if (state.state === 'blocked' && state.reason === 'leadership-taken') {
|
|
878
|
+
return Promise.reject(new ClientSyncError(LEADERSHIP_TAKEN_CODE, 'another tab took over the core this tab ran', true));
|
|
879
|
+
}
|
|
880
|
+
if (state.state === 'blocked' && state.reason === 'leader-incompatible') {
|
|
881
|
+
return Promise.reject(new ClientSyncError(LEADER_INCOMPATIBLE_CODE, 'the tab that owns the database runs a different protocol or schema version', true));
|
|
882
|
+
}
|
|
883
|
+
return Promise.reject(new ClientSyncError(WORKER_FAILED_CODE, 'no core serves this tab'));
|
|
884
|
+
},
|
|
885
|
+
takeOver: async () => {
|
|
886
|
+
if (closed) {
|
|
887
|
+
throw new ClientSyncError(WORKER_FAILED_CODE, 'the tab coordinator is closed');
|
|
888
|
+
}
|
|
889
|
+
const link = follower;
|
|
890
|
+
if (link === undefined ||
|
|
891
|
+
unreachableSince === undefined ||
|
|
892
|
+
state.state !== 'blocked' ||
|
|
893
|
+
state.reason !== 'leader-unreachable') {
|
|
894
|
+
throw takeOverRefused('not-unreachable');
|
|
895
|
+
}
|
|
896
|
+
if (takingOver)
|
|
897
|
+
throw takeOverRefused('in-progress');
|
|
898
|
+
const takeOverAt = unreachableSince + takeOverAfterMs;
|
|
899
|
+
if (clock.now() < takeOverAt)
|
|
900
|
+
throw takeOverRefused('too-early', takeOverAt);
|
|
901
|
+
if (lock.steal === undefined)
|
|
902
|
+
throw takeOverRefused('unsupported');
|
|
903
|
+
takingOver = true;
|
|
904
|
+
try {
|
|
905
|
+
const lease = await lock.steal(config.name);
|
|
906
|
+
if (closed) {
|
|
907
|
+
await lease.release();
|
|
908
|
+
return;
|
|
909
|
+
}
|
|
910
|
+
try {
|
|
911
|
+
// Exceed every epoch this tab saw, so the old leader's late replies
|
|
912
|
+
// and events are discarded everywhere.
|
|
913
|
+
await lead(lease, link.maxEpochSeen + 1, true);
|
|
914
|
+
}
|
|
915
|
+
catch (error) {
|
|
916
|
+
await lease.release();
|
|
917
|
+
throw error;
|
|
918
|
+
}
|
|
919
|
+
if (!closed && leading === undefined) {
|
|
920
|
+
// A third tab took the lock while this tab's core was starting.
|
|
921
|
+
throw new ClientSyncError(LEADERSHIP_TAKEN_CODE, 'another tab took over the core this tab ran', true);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
finally {
|
|
925
|
+
takingOver = false;
|
|
926
|
+
}
|
|
927
|
+
},
|
|
928
|
+
close: async () => {
|
|
929
|
+
if (closed)
|
|
930
|
+
return;
|
|
931
|
+
closed = true;
|
|
932
|
+
endUnreachable('closed');
|
|
933
|
+
follower?.close();
|
|
934
|
+
follower = undefined;
|
|
935
|
+
const ending = leading;
|
|
936
|
+
leading = undefined;
|
|
937
|
+
if (ending !== undefined) {
|
|
938
|
+
ending.end();
|
|
939
|
+
await ending.leader.close();
|
|
940
|
+
await ending.lease.release();
|
|
941
|
+
}
|
|
942
|
+
await tabLease.release();
|
|
943
|
+
},
|
|
944
|
+
};
|
|
945
|
+
// Leadership before any core exists: a losing tab never starts one.
|
|
946
|
+
const lease = lock.tryAcquire !== undefined
|
|
947
|
+
? await lock.tryAcquire(config.name)
|
|
948
|
+
: await lock.acquire(config.name);
|
|
949
|
+
if (lease !== undefined) {
|
|
950
|
+
try {
|
|
951
|
+
await lead(lease, 0, false);
|
|
952
|
+
}
|
|
953
|
+
catch (error) {
|
|
954
|
+
await lease.release();
|
|
955
|
+
await tabLease.release();
|
|
956
|
+
throw error;
|
|
957
|
+
}
|
|
958
|
+
return coordinator;
|
|
959
|
+
}
|
|
960
|
+
const link = new FollowerLink({
|
|
961
|
+
channel: openChannel(channelName),
|
|
962
|
+
fromId: tabId,
|
|
963
|
+
identity,
|
|
964
|
+
onEvent: (event) => config.onEvent?.(event),
|
|
965
|
+
onLeaderChange: () => { },
|
|
966
|
+
onStateChange: setState,
|
|
967
|
+
callTimeoutMs,
|
|
968
|
+
schedule: clock.schedule,
|
|
969
|
+
});
|
|
970
|
+
follower = link;
|
|
971
|
+
// Granted only when the lock is released: the leader's tab closed, or this
|
|
972
|
+
// tab's own failed takeover let go of it.
|
|
973
|
+
void lock.acquire(config.name).then(async (promoted) => {
|
|
974
|
+
if (closed || follower !== link) {
|
|
975
|
+
// Closed, or this tab already leads through takeOver(): promoting would
|
|
976
|
+
// hold the lock forever.
|
|
977
|
+
await promoted.release();
|
|
978
|
+
return;
|
|
979
|
+
}
|
|
980
|
+
// The new leader must exceed every epoch this tab saw, so stale replies
|
|
981
|
+
// and events are discarded everywhere.
|
|
982
|
+
const epoch = link.maxEpochSeen + 1;
|
|
983
|
+
link.unbind();
|
|
984
|
+
try {
|
|
985
|
+
await lead(promoted, epoch, false);
|
|
986
|
+
}
|
|
987
|
+
catch {
|
|
988
|
+
// The core failed to start: release so the next tab tries.
|
|
989
|
+
await promoted.release();
|
|
990
|
+
}
|
|
991
|
+
});
|
|
992
|
+
// Return bound: an event the leader fans out before binding is dropped.
|
|
993
|
+
try {
|
|
994
|
+
await link.waitUntilBound();
|
|
995
|
+
}
|
|
996
|
+
catch {
|
|
997
|
+
// Bind timed out: the link keeps probing, queued calls flush on the next
|
|
998
|
+
// announce, and the state says `blocked`.
|
|
999
|
+
}
|
|
1000
|
+
return coordinator;
|
|
1001
|
+
}
|
package/dist/outbox.d.ts
CHANGED
|
@@ -26,6 +26,11 @@ export interface OutboxOperation {
|
|
|
26
26
|
* presence set — `mutate` marks every column, `patch` the supplied ones).
|
|
27
27
|
*/
|
|
28
28
|
readonly values?: Readonly<Record<string, JsonRowValue>>;
|
|
29
|
+
/**
|
|
30
|
+
* §7.1 insert intent: a `mutate` of a row absent from the replica. Replay
|
|
31
|
+
* and encoding treat the operation as a full row; a missing column is NULL.
|
|
32
|
+
*/
|
|
33
|
+
readonly insert?: true;
|
|
29
34
|
}
|
|
30
35
|
export interface OutboxCommit {
|
|
31
36
|
readonly seq: number;
|
package/dist/outbox.js
CHANGED
|
@@ -106,7 +106,7 @@ export class OutboxEncodeError extends ClientSyncError {
|
|
|
106
106
|
super('sync.outbox_incompatible', message, false);
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
|
-
function sparseValues(table, values) {
|
|
109
|
+
function sparseValues(table, values, insert) {
|
|
110
110
|
// Any persisted key that is not a column of the CURRENT schema means the
|
|
111
111
|
// bump removed (or renamed) it — the commit cannot be expressed now.
|
|
112
112
|
for (const key of Object.keys(values)) {
|
|
@@ -116,7 +116,15 @@ function sparseValues(table, values) {
|
|
|
116
116
|
}
|
|
117
117
|
return table.columns.map((column) => {
|
|
118
118
|
const value = values[column.name];
|
|
119
|
-
|
|
119
|
+
if (value !== undefined)
|
|
120
|
+
return jsonToRowValue(value);
|
|
121
|
+
if (!insert)
|
|
122
|
+
return undefined;
|
|
123
|
+
// §7.1: an insert-intent operation presents every current column.
|
|
124
|
+
if (!column.nullable) {
|
|
125
|
+
throw new OutboxEncodeError('an outbox insert lacks a non-nullable column (§7.4.4)');
|
|
126
|
+
}
|
|
127
|
+
return null;
|
|
120
128
|
});
|
|
121
129
|
}
|
|
122
130
|
/**
|
|
@@ -149,7 +157,7 @@ export async function encodeOutboxCommit(schema, commit, encryption, storedKeyId
|
|
|
149
157
|
if (op.values === undefined) {
|
|
150
158
|
throw new ClientSyncError('sync.invalid_request', `outbox upsert on ${op.table}/${op.rowId} has no values`);
|
|
151
159
|
}
|
|
152
|
-
let values = sparseValues(table, op.values);
|
|
160
|
+
let values = sparseValues(table, op.values, op.insert === true);
|
|
153
161
|
if (encryption !== undefined && table.hasEncryptedColumns) {
|
|
154
162
|
// Lazy: opt-in E2EE never enters an encryption-free app's bundle.
|
|
155
163
|
const { encryptRowValues } = await import('./encryption.js');
|
package/dist/outcomes.js
CHANGED
|
@@ -44,8 +44,12 @@ export function isStoredOperation(value) {
|
|
|
44
44
|
typeof value.rowId === 'string' &&
|
|
45
45
|
(value.baseVersion === undefined ||
|
|
46
46
|
Number.isSafeInteger(value.baseVersion)) &&
|
|
47
|
-
((value.op === 'upsert' &&
|
|
48
|
-
(value.
|
|
47
|
+
((value.op === 'upsert' &&
|
|
48
|
+
isStoredRow(value.values) &&
|
|
49
|
+
(value.insert === undefined || value.insert === true)) ||
|
|
50
|
+
(value.op === 'delete' &&
|
|
51
|
+
value.values === undefined &&
|
|
52
|
+
value.insert === undefined)));
|
|
49
53
|
}
|
|
50
54
|
function decodeResults(raw) {
|
|
51
55
|
function corrupt() {
|
|
@@ -103,7 +103,7 @@ type PreviousVersionCaptureOutcome = {
|
|
|
103
103
|
interface PreviousVersionAuditEntry {
|
|
104
104
|
readonly commitId: string;
|
|
105
105
|
readonly table: string;
|
|
106
|
-
readonly reason: 'unknown-table' | 'unknown-column';
|
|
106
|
+
readonly reason: 'unknown-table' | 'unknown-column' | 'missing-required-column';
|
|
107
107
|
/** The offending column; present for every reason except `unknown-table`. */
|
|
108
108
|
readonly column?: string;
|
|
109
109
|
}
|
|
@@ -177,7 +177,9 @@ interface PendingCommitLike {
|
|
|
177
177
|
readonly clientCommitId: string;
|
|
178
178
|
readonly operations: readonly {
|
|
179
179
|
readonly table: string;
|
|
180
|
+
readonly op: 'upsert' | 'delete';
|
|
180
181
|
readonly values?: Readonly<Record<string, JsonRowValue>>;
|
|
182
|
+
readonly insert?: true;
|
|
181
183
|
}[];
|
|
182
184
|
}
|
|
183
185
|
/**
|
package/dist/previous-version.js
CHANGED
|
@@ -499,6 +499,10 @@ export function buildPreviousVersionAudit(schema, pending, fromVersion, toVersio
|
|
|
499
499
|
}
|
|
500
500
|
function firstIncompatibility(schema, commit) {
|
|
501
501
|
for (const operation of commit.operations) {
|
|
502
|
+
// §7.4.4: a value-free delete stays encodable and takes ordinary server
|
|
503
|
+
// validation, even against a removed table.
|
|
504
|
+
if (operation.op === 'delete')
|
|
505
|
+
continue;
|
|
502
506
|
const table = schema.tables.get(operation.table);
|
|
503
507
|
if (table === undefined) {
|
|
504
508
|
return {
|
|
@@ -507,9 +511,7 @@ function firstIncompatibility(schema, commit) {
|
|
|
507
511
|
reason: 'unknown-table',
|
|
508
512
|
};
|
|
509
513
|
}
|
|
510
|
-
|
|
511
|
-
continue;
|
|
512
|
-
for (const key of Object.keys(operation.values)) {
|
|
514
|
+
for (const key of Object.keys(operation.values ?? {})) {
|
|
513
515
|
if (!table.columnIndex.has(key)) {
|
|
514
516
|
return {
|
|
515
517
|
commitId: commit.clientCommitId,
|
|
@@ -519,6 +521,19 @@ function firstIncompatibility(schema, commit) {
|
|
|
519
521
|
};
|
|
520
522
|
}
|
|
521
523
|
}
|
|
524
|
+
// §7.1: an insert-intent operation presents every current column, so a
|
|
525
|
+
// non-nullable column it lacks leaves the commit unencodable.
|
|
526
|
+
if (operation.insert === true) {
|
|
527
|
+
const missing = table.columns.find((column) => !column.nullable && operation.values?.[column.name] === undefined);
|
|
528
|
+
if (missing !== undefined) {
|
|
529
|
+
return {
|
|
530
|
+
commitId: commit.clientCommitId,
|
|
531
|
+
table: operation.table,
|
|
532
|
+
reason: 'missing-required-column',
|
|
533
|
+
column: missing.name,
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
}
|
|
522
537
|
}
|
|
523
538
|
return undefined;
|
|
524
539
|
}
|
|
@@ -548,7 +563,9 @@ export function storedPreviousVersionAudit(db) {
|
|
|
548
563
|
const item = entry;
|
|
549
564
|
if (typeof item.commitId !== 'string' ||
|
|
550
565
|
typeof item.table !== 'string' ||
|
|
551
|
-
(item.reason !== 'unknown-table' &&
|
|
566
|
+
(item.reason !== 'unknown-table' &&
|
|
567
|
+
item.reason !== 'unknown-column' &&
|
|
568
|
+
item.reason !== 'missing-required-column') ||
|
|
552
569
|
(item.column !== undefined && typeof item.column !== 'string')) {
|
|
553
570
|
return undefined;
|
|
554
571
|
}
|
|
@@ -678,7 +695,9 @@ export function decodePreviousVersionAuditResult(value) {
|
|
|
678
695
|
if (!Object.keys(item).every((key) => AUDIT_ENTRY_KEYS.includes(key)) ||
|
|
679
696
|
typeof item.commitId !== 'string' ||
|
|
680
697
|
typeof item.table !== 'string' ||
|
|
681
|
-
(item.reason !== 'unknown-table' &&
|
|
698
|
+
(item.reason !== 'unknown-table' &&
|
|
699
|
+
item.reason !== 'unknown-column' &&
|
|
700
|
+
item.reason !== 'missing-required-column') ||
|
|
682
701
|
(item.column !== undefined && typeof item.column !== 'string')) {
|
|
683
702
|
return invalidHostResponse(method);
|
|
684
703
|
}
|
package/dist/reactive-store.js
CHANGED
|
@@ -251,7 +251,8 @@ const unreadQuerySnapshots = new Map();
|
|
|
251
251
|
function localReadsBlocked(availability) {
|
|
252
252
|
return (availability.state === 'blocked' &&
|
|
253
253
|
(availability.reason === 'leader-unreachable' ||
|
|
254
|
-
availability.reason === 'leader-incompatible'
|
|
254
|
+
availability.reason === 'leader-incompatible' ||
|
|
255
|
+
availability.reason === 'leadership-taken'));
|
|
255
256
|
}
|
|
256
257
|
/** One frozen snapshot per phase and availability for entries that have no
|
|
257
258
|
* successful read, so switching between unread entries keeps the snapshot and
|
package/dist/wasm-database.d.ts
CHANGED
|
@@ -16,6 +16,12 @@ export interface PersistentWasmDatabaseOptions {
|
|
|
16
16
|
readonly directory?: string;
|
|
17
17
|
/** Initial SAH pool capacity (sqlite-wasm default: 6). */
|
|
18
18
|
readonly initialCapacity?: number;
|
|
19
|
+
/**
|
|
20
|
+
* Take the storage lock from the worker that holds it, which fences that
|
|
21
|
+
* worker's database. Its SAHs stay held until it runs again, so the open
|
|
22
|
+
* fails with a retryable `client.storage_busy` until then.
|
|
23
|
+
*/
|
|
24
|
+
readonly takeOver?: boolean;
|
|
19
25
|
}
|
|
20
26
|
/**
|
|
21
27
|
* THE reload-persistent browser mode: a named database on OPFS via the
|