@inflatable-cookie/longhorn 0.5.0 → 0.7.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 +2 -2
- package/package.json +1 -7
- package/src/agent-control/generated/protocol.ts +228 -0
- package/src/agent-control/index.ts +1 -5
- package/src/agent-control/inject.ts +4 -1
- package/src/agent-control/selection.ts +42 -25
- package/src/agent-control/shim.ts +428 -198
- package/src/bridge/direct.ts +1 -1
- package/src/bridge/generated/fields.ts +4 -3
- package/src/bridge/generated/protocol.ts +62 -7
- package/src/bridge/generated/variant-fields.ts +1 -1
- package/src/bridge/job.ts +49 -12
- package/src/bridge/loopback.ts +7 -25
- package/src/bridge/operation.ts +13 -43
- package/src/bridge/router.ts +10 -23
- package/src/bridge/runtime/connection.ts +78 -109
- package/src/bridge/runtime/policy.ts +8 -28
- package/src/bridge/runtime/retry.ts +6 -18
- package/src/bridge/session.ts +4 -14
- package/src/bridge/stream/connection.ts +24 -55
- package/src/bridge/stream/source.ts +2 -6
- package/src/bridge/stream/tracker.ts +12 -12
- package/src/bridge/supervision.ts +104 -97
- package/src/bridge/validation/base.ts +21 -63
- package/src/bridge/validation/jobs.ts +11 -25
- package/src/bridge/validation/negotiation.ts +28 -43
- package/src/bridge/validation/operations.ts +29 -65
- package/src/bridge/validation/streams.ts +3 -10
- package/src/commands/client.ts +37 -28
- package/src/commands/controller.ts +119 -96
- package/src/commands/generated/fields.ts +7 -6
- package/src/commands/generated/protocol.ts +28 -1
- package/src/commands/generated/variant-fields.ts +1 -1
- package/src/commands/keyboard.ts +94 -100
- package/src/commands/physical-code.ts +11 -0
- package/src/commands/ports.ts +8 -13
- package/src/commands/projectors.ts +100 -75
- package/src/commands/validation.ts +434 -241
- package/src/config/client.ts +9 -22
- package/src/config/generated/base.ts +22 -6
- package/src/config/generated/fields.ts +4 -3
- package/src/config/generated/label-template.ts +1 -1
- package/src/config/generated/labels.ts +1 -1
- package/src/config/generated/protocol.ts +1 -1
- package/src/config/generated/restore.ts +35 -12
- package/src/config/generated/variant-fields.ts +1 -1
- package/src/config/names.ts +6 -12
- package/src/config/validation/commands.ts +11 -25
- package/src/config/validation/outcomes.ts +163 -70
- package/src/config/validation/primitives.ts +35 -80
- package/src/config/validation/projection.ts +100 -128
- package/src/core/connection.ts +187 -83
- package/src/core/controller-lifecycle.ts +144 -0
- package/src/core/generated/identifiers.ts +4 -0
- package/src/core/identifiers.ts +40 -0
- package/src/core/index.ts +10 -1
- package/src/core/json-value.ts +58 -0
- package/src/core/listeners.ts +35 -0
- package/src/core/record.ts +80 -0
- package/src/core/transport.ts +9 -11
- package/src/core/wire-copy.ts +7 -0
- package/src/history/client.ts +11 -13
- package/src/history/controller.ts +106 -108
- package/src/history/direct.ts +1 -3
- package/src/history/generated/fields.ts +4 -3
- package/src/history/generated/protocol.ts +8 -5
- package/src/history/generated/variant-fields.ts +1 -1
- package/src/history/ports.ts +7 -4
- package/src/history/serialized.ts +9 -13
- package/src/history/validation.ts +69 -75
- package/src/history-tree/client.ts +89 -11
- package/src/history-tree/controller.ts +380 -96
- package/src/history-tree/direct.ts +3 -1
- package/src/history-tree/generated/fields.ts +5 -3
- package/src/history-tree/generated/protocol.ts +43 -7
- package/src/history-tree/generated/variant-fields.ts +7 -1
- package/src/history-tree/ports.ts +12 -5
- package/src/history-tree/serialized.ts +39 -11
- package/src/history-tree/validation.ts +286 -78
- package/src/layout/generated/protocol.ts +4 -3
- package/src/layout/generated/variant-fields.ts +47 -0
- package/src/layout/index.ts +1 -1
- package/src/layout/ratio.ts +10 -9
- package/src/layout/validation.ts +98 -32
- package/src/layout/visibility.ts +1 -2
- package/src/licence/client.ts +48 -7
- package/src/licence/controller.ts +127 -70
- package/src/licence/direct.ts +3 -1
- package/src/licence/generated/fields.ts +4 -3
- package/src/licence/generated/key-conformance.json +28 -0
- package/src/licence/generated/protocol.ts +9 -5
- package/src/licence/generated/variant-fields.ts +1 -1
- package/src/licence/key.ts +42 -18
- package/src/licence/ports.ts +10 -3
- package/src/licence/serialized.ts +32 -9
- package/src/licence/validation.ts +134 -43
- package/src/native-content/client.ts +207 -234
- package/src/native-content/generated/fields.ts +6 -6
- package/src/native-content/generated/protocol.ts +16 -5
- package/src/native-content/generated/variant-fields.ts +1 -1
- package/src/native-content/ports.ts +11 -16
- package/src/native-content/serialized.ts +12 -19
- package/src/native-content/validation/base.ts +57 -80
- package/src/native-content/validation/messages.ts +24 -35
- package/src/native-content/validation/state.ts +31 -33
- package/src/notifications/client.ts +85 -51
- package/src/notifications/controller.ts +400 -145
- package/src/notifications/direct.ts +3 -1
- package/src/notifications/generated/fields.ts +4 -3
- package/src/notifications/generated/labels.ts +1 -1
- package/src/notifications/generated/protocol.ts +5 -1
- package/src/notifications/generated/variant-fields.ts +1 -1
- package/src/notifications/ports.ts +9 -4
- package/src/notifications/serialized.ts +11 -9
- package/src/notifications/tone.ts +1 -1
- package/src/notifications/validation.ts +165 -71
- package/src/operation/bridge.ts +187 -60
- package/src/operation/client.ts +48 -62
- package/src/operation/controller.ts +67 -97
- package/src/operation/direct.ts +1 -3
- package/src/operation/generated/fields.ts +4 -3
- package/src/operation/generated/labels.ts +1 -1
- package/src/operation/generated/protocol.ts +5 -1
- package/src/operation/generated/variant-fields.ts +1 -1
- package/src/operation/ports.ts +12 -10
- package/src/operation/serialized.ts +8 -9
- package/src/operation/validation.ts +189 -104
- package/src/settings/client.ts +11 -44
- package/src/settings/connection.ts +35 -53
- package/src/settings/generated/fields.ts +5 -4
- package/src/settings/generated/protocol.ts +14 -5
- package/src/settings/generated/variant-fields.ts +1 -1
- package/src/settings/names.ts +2 -4
- package/src/settings/registry.ts +23 -25
- package/src/settings/validation/authority.ts +21 -19
- package/src/settings/validation/command.ts +22 -41
- package/src/settings/validation/error.ts +2 -8
- package/src/settings/validation/primitives.ts +46 -97
- package/src/settings/validation/projection.ts +8 -24
- package/src/settings/validation/registry.ts +33 -79
- package/src/surface-transfer/client.ts +6 -18
- package/src/surface-transfer/generated/fields.ts +3 -3
- package/src/surface-transfer/generated/protocol.ts +3 -3
- package/src/surface-transfer/generated/variant-fields.ts +1 -1
- package/src/surface-transfer/validation.ts +91 -86
- package/src/surfaces/client.ts +33 -55
- package/src/surfaces/generated/fields.ts +3 -3
- package/src/surfaces/generated/protocol.ts +1 -1
- package/src/surfaces/generated/variant-fields.ts +1 -1
- package/src/surfaces/validation.ts +227 -99
- package/src/transfer/client.ts +26 -60
- package/src/transfer/connection.ts +29 -51
- package/src/transfer/generated/fields.ts +7 -6
- package/src/transfer/generated/protocol.ts +13 -5
- package/src/transfer/generated/variant-fields.ts +1 -1
- package/src/transfer/index.ts +8 -1
- package/src/transfer/payload.ts +20 -35
- package/src/transfer/validation.ts +169 -119
- package/src/update/client.ts +86 -9
- package/src/update/controller.ts +264 -116
- package/src/update/direct.ts +3 -1
- package/src/update/generated/fields.ts +5 -4
- package/src/update/generated/protocol.ts +25 -9
- package/src/update/generated/variant-fields.ts +4 -2
- package/src/update/ports.ts +14 -6
- package/src/update/serialized.ts +36 -10
- package/src/update/validation.ts +118 -48
package/src/core/connection.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ListenerDisposal } from "./listeners.ts";
|
|
1
2
|
import type { Unlisten } from "./transport.ts";
|
|
2
3
|
|
|
3
4
|
export type SnapshotEventAction =
|
|
@@ -5,37 +6,92 @@ export type SnapshotEventAction =
|
|
|
5
6
|
| { readonly kind: "refresh" }
|
|
6
7
|
| { readonly kind: "snapshot"; readonly value: unknown };
|
|
7
8
|
|
|
8
|
-
export type ConnectionFailurePhase =
|
|
9
|
-
| "registration"
|
|
10
|
-
| "event"
|
|
11
|
-
| "snapshot"
|
|
12
|
-
| "unlisten";
|
|
9
|
+
export type ConnectionFailurePhase = "registration" | "event" | "snapshot" | "unlisten";
|
|
13
10
|
|
|
14
11
|
export interface ConnectionFailure {
|
|
15
12
|
readonly phase: ConnectionFailurePhase;
|
|
16
13
|
readonly error: unknown;
|
|
17
14
|
}
|
|
18
15
|
|
|
19
|
-
export type ConnectionFailureReporter = (
|
|
16
|
+
export type ConnectionFailureReporter = (failure: ConnectionFailure) => void;
|
|
17
|
+
|
|
18
|
+
interface CheckedListenerOptions<Event> {
|
|
19
|
+
readonly listen: (listener: (payload: unknown) => void) => Unlisten | Promise<Unlisten>;
|
|
20
|
+
readonly validate: (payload: unknown) => asserts payload is Event;
|
|
21
|
+
readonly listener: (event: Event) => void;
|
|
22
|
+
readonly onFailure?: ConnectionFailureReporter | undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function listenChecked<Event>(
|
|
26
|
+
options: CheckedListenerOptions<Event>,
|
|
27
|
+
): Promise<Unlisten> {
|
|
28
|
+
const disposal = new ListenerDisposal();
|
|
29
|
+
const { listener, onFailure } = options;
|
|
30
|
+
const validate: CheckedListenerOptions<Event>["validate"] = options.validate;
|
|
31
|
+
let closed = false;
|
|
32
|
+
let registered = false;
|
|
33
|
+
const close = () => {
|
|
34
|
+
closed = true;
|
|
35
|
+
return disposal.close();
|
|
36
|
+
};
|
|
37
|
+
const closeAfterFailure = async () => {
|
|
38
|
+
try {
|
|
39
|
+
await close();
|
|
40
|
+
} catch (error) {
|
|
41
|
+
reportFailure(onFailure, { phase: "unlisten", error });
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
disposal.unlisten = await options.listen((payload) => {
|
|
46
|
+
if (closed) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
try {
|
|
50
|
+
validate(payload);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
closed = true;
|
|
53
|
+
reportFailure(onFailure, { phase: "event", error });
|
|
54
|
+
if (registered) {
|
|
55
|
+
void closeAfterFailure();
|
|
56
|
+
}
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
listener(payload);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
// A callback during registration cannot unwind a listen that still owes us its disposer.
|
|
63
|
+
if (registered) {
|
|
64
|
+
throw error;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
registered = true;
|
|
69
|
+
if (closed) {
|
|
70
|
+
await closeAfterFailure();
|
|
71
|
+
}
|
|
72
|
+
return close;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Reports to an optional consumer callback, which cannot throw back into the caller. */
|
|
76
|
+
export function reportFailure(
|
|
77
|
+
reporter: ConnectionFailureReporter | undefined,
|
|
20
78
|
failure: ConnectionFailure,
|
|
21
|
-
)
|
|
79
|
+
): void {
|
|
80
|
+
try {
|
|
81
|
+
reporter?.(failure);
|
|
82
|
+
} catch {
|
|
83
|
+
// A failure reporter cannot interrupt teardown or replace the recorded error.
|
|
84
|
+
}
|
|
85
|
+
}
|
|
22
86
|
|
|
23
87
|
export interface CheckedSnapshotConnectionOptions<Snapshot> {
|
|
24
|
-
readonly listen: (
|
|
25
|
-
listener: (payload: unknown) => void,
|
|
26
|
-
) => Promise<Unlisten>;
|
|
88
|
+
readonly listen: (listener: (payload: unknown) => void) => Promise<Unlisten>;
|
|
27
89
|
readonly loadSnapshot: () => Promise<unknown>;
|
|
28
90
|
readonly validateSnapshot: (value: unknown) => Snapshot;
|
|
29
|
-
readonly handleEvent: (
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
readonly isNewer: (
|
|
34
|
-
candidate: Snapshot,
|
|
35
|
-
current: Snapshot | undefined,
|
|
36
|
-
) => boolean;
|
|
37
|
-
readonly onSnapshot?: (snapshot: Snapshot) => void;
|
|
38
|
-
readonly onFailure?: ConnectionFailureReporter;
|
|
91
|
+
readonly handleEvent: (payload: unknown, current: Snapshot | undefined) => SnapshotEventAction;
|
|
92
|
+
readonly isNewer: (candidate: Snapshot, current: Snapshot | undefined) => boolean;
|
|
93
|
+
readonly onSnapshot?: ((snapshot: Snapshot) => void) | undefined;
|
|
94
|
+
readonly onFailure?: ConnectionFailureReporter | undefined;
|
|
39
95
|
readonly disposedBeforeReadyError?: () => Error;
|
|
40
96
|
}
|
|
41
97
|
|
|
@@ -43,10 +99,9 @@ export class CheckedSnapshotConnection<Snapshot> {
|
|
|
43
99
|
readonly ready: Promise<Snapshot | undefined>;
|
|
44
100
|
|
|
45
101
|
readonly #options: CheckedSnapshotConnectionOptions<Snapshot>;
|
|
46
|
-
readonly #failures: ConnectionFailure
|
|
102
|
+
readonly #failures: Array<ConnectionFailure> = [];
|
|
47
103
|
#current: Snapshot | undefined;
|
|
48
|
-
#
|
|
49
|
-
#cleanup: Promise<void> | undefined;
|
|
104
|
+
readonly #listener = new ListenerDisposal();
|
|
50
105
|
#pump: Promise<void> | undefined;
|
|
51
106
|
#registered = false;
|
|
52
107
|
#refreshPending = false;
|
|
@@ -61,44 +116,70 @@ export class CheckedSnapshotConnection<Snapshot> {
|
|
|
61
116
|
return this.#current;
|
|
62
117
|
}
|
|
63
118
|
|
|
64
|
-
failures():
|
|
119
|
+
failures(): ReadonlyArray<ConnectionFailure> {
|
|
65
120
|
return [...this.#failures];
|
|
66
121
|
}
|
|
67
122
|
|
|
123
|
+
/**
|
|
124
|
+
* Asks for one more authoritative load: the caller learned that the snapshot
|
|
125
|
+
* it was just given does not cover everything already observed. Requests made
|
|
126
|
+
* while a load is in flight collapse into one further pass. It is a no-op
|
|
127
|
+
* after dispose or failure.
|
|
128
|
+
*/
|
|
129
|
+
requestRefresh(): void {
|
|
130
|
+
if (this.#disposed || this.#failures.length > 0) {
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
this.#refreshPending = true;
|
|
134
|
+
if (this.#registered) {
|
|
135
|
+
void this.#synchronize().catch((error: unknown) => {
|
|
136
|
+
this.#recordFailure("snapshot", error);
|
|
137
|
+
void this.#closeAfterFailure();
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Accepts a snapshot the caller obtained from its own request (for example,
|
|
144
|
+
* a mutation result), under the same `isNewer` rule the refresh pump uses.
|
|
145
|
+
* It is a no-op after dispose.
|
|
146
|
+
*/
|
|
147
|
+
offer(candidate: Snapshot): void {
|
|
148
|
+
if (!this.#disposed && this.#accept(candidate)) {
|
|
149
|
+
this.#options.onSnapshot?.(candidate);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
68
153
|
async dispose(): Promise<void> {
|
|
69
154
|
this.#disposed = true;
|
|
70
155
|
|
|
71
|
-
let
|
|
156
|
+
let cleanupFailure: { error: unknown } | undefined;
|
|
72
157
|
try {
|
|
73
|
-
await this.#
|
|
158
|
+
await this.#listener.close();
|
|
74
159
|
} catch (error) {
|
|
75
|
-
|
|
160
|
+
cleanupFailure = { error };
|
|
76
161
|
this.#recordFailure("unlisten", error);
|
|
77
162
|
}
|
|
78
163
|
|
|
79
|
-
let
|
|
164
|
+
let readyFailure: ConnectionFailure | undefined;
|
|
80
165
|
try {
|
|
81
166
|
await this.ready;
|
|
82
167
|
} catch (error) {
|
|
83
|
-
|
|
84
|
-
readyError = error;
|
|
85
|
-
}
|
|
168
|
+
readyFailure = this.#failures.find((failure) => Object.is(failure.error, error));
|
|
86
169
|
}
|
|
87
170
|
|
|
88
|
-
if (
|
|
171
|
+
if (cleanupFailure === undefined) {
|
|
89
172
|
try {
|
|
90
|
-
await this.#
|
|
173
|
+
await this.#listener.close();
|
|
91
174
|
} catch (error) {
|
|
92
|
-
|
|
175
|
+
cleanupFailure = { error };
|
|
93
176
|
this.#recordFailure("unlisten", error);
|
|
94
177
|
}
|
|
95
178
|
}
|
|
96
179
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
if (readyError !== undefined) {
|
|
101
|
-
throw readyError;
|
|
180
|
+
const failure = cleanupFailure ?? readyFailure;
|
|
181
|
+
if (failure !== undefined) {
|
|
182
|
+
throw failure.error;
|
|
102
183
|
}
|
|
103
184
|
}
|
|
104
185
|
|
|
@@ -113,7 +194,7 @@ export class CheckedSnapshotConnection<Snapshot> {
|
|
|
113
194
|
throw error;
|
|
114
195
|
}
|
|
115
196
|
|
|
116
|
-
this.#unlisten = unlisten;
|
|
197
|
+
this.#listener.unlisten = unlisten;
|
|
117
198
|
this.#registered = true;
|
|
118
199
|
|
|
119
200
|
if (this.#disposed) {
|
|
@@ -125,6 +206,11 @@ export class CheckedSnapshotConnection<Snapshot> {
|
|
|
125
206
|
return undefined;
|
|
126
207
|
}
|
|
127
208
|
|
|
209
|
+
if (this.#failures.length > 0) {
|
|
210
|
+
await this.#closeAfterFailure();
|
|
211
|
+
return this.#current;
|
|
212
|
+
}
|
|
213
|
+
|
|
128
214
|
this.#refreshPending = true;
|
|
129
215
|
try {
|
|
130
216
|
await this.#synchronize();
|
|
@@ -150,36 +236,32 @@ export class CheckedSnapshotConnection<Snapshot> {
|
|
|
150
236
|
return;
|
|
151
237
|
}
|
|
152
238
|
|
|
239
|
+
let candidate: Snapshot;
|
|
153
240
|
try {
|
|
154
241
|
const action = this.#options.handleEvent(payload, this.#current);
|
|
155
242
|
switch (action.kind) {
|
|
156
243
|
case "ignore":
|
|
157
244
|
return;
|
|
158
245
|
case "refresh":
|
|
159
|
-
this
|
|
160
|
-
if (this.#registered) {
|
|
161
|
-
void this.#synchronize().catch((error) => {
|
|
162
|
-
this.#recordFailure("snapshot", error);
|
|
163
|
-
void this.#closeAfterFailure();
|
|
164
|
-
});
|
|
165
|
-
}
|
|
246
|
+
this.requestRefresh();
|
|
166
247
|
return;
|
|
167
248
|
case "snapshot":
|
|
168
|
-
this.#
|
|
169
|
-
|
|
249
|
+
candidate = this.#options.validateSnapshot(action.value);
|
|
250
|
+
if (!this.#accept(candidate)) {
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
break;
|
|
170
254
|
}
|
|
171
255
|
} catch (error) {
|
|
172
256
|
this.#recordFailure("event", error);
|
|
173
257
|
void this.#closeAfterFailure();
|
|
258
|
+
return;
|
|
174
259
|
}
|
|
260
|
+
this.#notifySnapshot(candidate, this.#registered && this.#pump === undefined);
|
|
175
261
|
}
|
|
176
262
|
|
|
177
263
|
#synchronize(): Promise<void> {
|
|
178
|
-
if (
|
|
179
|
-
!this.#registered ||
|
|
180
|
-
this.#disposed ||
|
|
181
|
-
this.#failures.length > 0
|
|
182
|
-
) {
|
|
264
|
+
if (!this.#registered || this.#disposed || this.#failures.length > 0) {
|
|
183
265
|
return Promise.resolve();
|
|
184
266
|
}
|
|
185
267
|
this.#pump ??= Promise.resolve().then(() => this.#runPump());
|
|
@@ -188,65 +270,87 @@ export class CheckedSnapshotConnection<Snapshot> {
|
|
|
188
270
|
|
|
189
271
|
async #runPump(): Promise<void> {
|
|
190
272
|
try {
|
|
191
|
-
while (
|
|
192
|
-
this.#refreshPending &&
|
|
193
|
-
!this.#disposed &&
|
|
194
|
-
this.#failures.length === 0
|
|
195
|
-
) {
|
|
273
|
+
while (this.#refreshPending && !this.#disposed && this.#failures.length === 0) {
|
|
196
274
|
this.#refreshPending = false;
|
|
197
275
|
const value = await this.#options.loadSnapshot();
|
|
198
276
|
if (this.#disposed || this.#failures.length > 0) {
|
|
199
277
|
return;
|
|
200
278
|
}
|
|
201
|
-
this.#
|
|
279
|
+
const candidate = this.#options.validateSnapshot(value);
|
|
280
|
+
if (this.#accept(candidate)) {
|
|
281
|
+
this.#notifySnapshot(candidate, false);
|
|
282
|
+
}
|
|
202
283
|
}
|
|
203
284
|
} finally {
|
|
204
285
|
this.#pump = undefined;
|
|
205
286
|
}
|
|
206
287
|
}
|
|
207
288
|
|
|
208
|
-
#accept(candidate: Snapshot):
|
|
289
|
+
#accept(candidate: Snapshot): boolean {
|
|
209
290
|
if (!this.#options.isNewer(candidate, this.#current)) {
|
|
210
|
-
return;
|
|
291
|
+
return false;
|
|
211
292
|
}
|
|
212
293
|
this.#current = candidate;
|
|
213
|
-
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
#notifySnapshot(candidate: Snapshot, propagate: boolean): void {
|
|
298
|
+
try {
|
|
299
|
+
this.#options.onSnapshot?.(candidate);
|
|
300
|
+
} catch (error) {
|
|
301
|
+
// A callback during registration or a query can otherwise reject that operation.
|
|
302
|
+
if (propagate) {
|
|
303
|
+
throw error;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
214
306
|
}
|
|
215
307
|
|
|
216
|
-
#recordFailure(
|
|
217
|
-
phase: ConnectionFailurePhase,
|
|
218
|
-
error: unknown,
|
|
219
|
-
): void {
|
|
308
|
+
#recordFailure(phase: ConnectionFailurePhase, error: unknown): void {
|
|
220
309
|
if (
|
|
221
|
-
this.#failures.some(
|
|
222
|
-
(failure) => failure.phase === phase && failure.error === error,
|
|
223
|
-
)
|
|
310
|
+
this.#failures.some((failure) => failure.phase === phase && Object.is(failure.error, error))
|
|
224
311
|
) {
|
|
225
312
|
return;
|
|
226
313
|
}
|
|
227
314
|
const failure = { phase, error };
|
|
228
315
|
this.#failures.push(failure);
|
|
229
|
-
this.#options.onFailure
|
|
316
|
+
reportFailure(this.#options.onFailure, failure);
|
|
230
317
|
}
|
|
231
318
|
|
|
232
319
|
async #closeAfterFailure(): Promise<void> {
|
|
233
320
|
try {
|
|
234
|
-
await this.#
|
|
321
|
+
await this.#listener.close();
|
|
235
322
|
} catch (error) {
|
|
236
323
|
this.#recordFailure("unlisten", error);
|
|
237
324
|
}
|
|
238
325
|
}
|
|
326
|
+
}
|
|
239
327
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
328
|
+
/**
|
|
329
|
+
* The lifetime a domain subscription exposes over one connection. `ready`
|
|
330
|
+
* settles when the first synchronization does, and `present` decides what it
|
|
331
|
+
* resolves to.
|
|
332
|
+
*/
|
|
333
|
+
export class CheckedSubscription<Snapshot, Ready = void> {
|
|
334
|
+
readonly ready: Promise<Ready>;
|
|
335
|
+
readonly #connection: CheckedSnapshotConnection<Snapshot>;
|
|
336
|
+
|
|
337
|
+
constructor(
|
|
338
|
+
connection: CheckedSnapshotConnection<Snapshot>,
|
|
339
|
+
present: (snapshot: Snapshot | undefined) => Ready,
|
|
340
|
+
) {
|
|
341
|
+
this.#connection = connection;
|
|
342
|
+
this.ready = connection.ready.then(present);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
current(): Snapshot | undefined {
|
|
346
|
+
return this.#connection.current();
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
failures(): ReadonlyArray<ConnectionFailure> {
|
|
350
|
+
return this.#connection.failures();
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
dispose(): Promise<void> {
|
|
354
|
+
return this.#connection.dispose();
|
|
251
355
|
}
|
|
252
356
|
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import type { ConnectionFailure, ConnectionFailureReporter } from "./connection.ts";
|
|
2
|
+
import { disposeAll } from "./listeners.ts";
|
|
3
|
+
import type { Unlisten } from "./transport.ts";
|
|
4
|
+
|
|
5
|
+
interface ControllerLifetime {
|
|
6
|
+
readonly isCurrent: () => boolean;
|
|
7
|
+
readonly own: (...unlisten: Array<Unlisten>) => Promise<void>;
|
|
8
|
+
readonly reportFailure: ConnectionFailureReporter;
|
|
9
|
+
readonly watch: (subscription: SnapshotSubscription, ready: () => void) => Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface SnapshotSubscription {
|
|
13
|
+
readonly ready: Promise<unknown>;
|
|
14
|
+
failures(): ReadonlyArray<ConnectionFailure>;
|
|
15
|
+
dispose(): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface ControllerStart {
|
|
19
|
+
readonly loading: () => void;
|
|
20
|
+
readonly connect: (lifetime: ControllerLifetime) => Promise<void>;
|
|
21
|
+
readonly failed: (error: unknown) => void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class ControllerLifecycle {
|
|
25
|
+
#unlisten: Array<Unlisten> | undefined;
|
|
26
|
+
#revision = 0;
|
|
27
|
+
#eventFailure: { readonly kind: "failed"; readonly error: unknown } | undefined;
|
|
28
|
+
|
|
29
|
+
get revision(): number {
|
|
30
|
+
return this.#revision;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
get active(): boolean {
|
|
34
|
+
return this.#unlisten !== undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
get eventFailure() {
|
|
38
|
+
return this.#eventFailure;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
isCurrent(revision: number): boolean {
|
|
42
|
+
return this.active && this.#revision === revision;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Retains the lifetime's first event failure until the controller stops. */
|
|
46
|
+
recordEventFailure(error: unknown): void {
|
|
47
|
+
this.#eventFailure ??= { kind: "failed", error };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async start({ loading, connect, failed }: ControllerStart): Promise<void> {
|
|
51
|
+
if (this.active) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const listeners: Array<Unlisten> = [];
|
|
55
|
+
this.#unlisten = listeners;
|
|
56
|
+
const revision = ++this.#revision;
|
|
57
|
+
const isCurrent = () => this.isCurrent(revision);
|
|
58
|
+
const own = async (...unlisten: Array<Unlisten>) => {
|
|
59
|
+
if (isCurrent()) {
|
|
60
|
+
listeners.push(...unlisten);
|
|
61
|
+
} else {
|
|
62
|
+
await disposeAll(unlisten);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
try {
|
|
66
|
+
loading();
|
|
67
|
+
if (isCurrent()) {
|
|
68
|
+
await connect({
|
|
69
|
+
isCurrent,
|
|
70
|
+
own,
|
|
71
|
+
reportFailure: ({ phase, error }) => {
|
|
72
|
+
if (isCurrent() && phase !== "unlisten") {
|
|
73
|
+
if (phase === "event") {
|
|
74
|
+
this.recordEventFailure(error);
|
|
75
|
+
}
|
|
76
|
+
failed(error);
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
watch: async (subscription, ready) => {
|
|
80
|
+
// The connection owns cleanup on registration and snapshot failures.
|
|
81
|
+
await own(() => subscription.dispose());
|
|
82
|
+
try {
|
|
83
|
+
await subscription.ready;
|
|
84
|
+
if (isCurrent() && subscription.failures().length === 0) {
|
|
85
|
+
ready();
|
|
86
|
+
}
|
|
87
|
+
} catch (error) {
|
|
88
|
+
if (isCurrent()) {
|
|
89
|
+
failed(error);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
} catch (error) {
|
|
96
|
+
if (isCurrent()) {
|
|
97
|
+
const unlisten = listeners.splice(0);
|
|
98
|
+
try {
|
|
99
|
+
failed(error);
|
|
100
|
+
} finally {
|
|
101
|
+
await disposeAll(unlisten);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Replaces a lifetime unless another lifecycle action supersedes its teardown. */
|
|
108
|
+
async restart(
|
|
109
|
+
status: { readonly kind: string; readonly error?: unknown },
|
|
110
|
+
stop: () => Promise<void>,
|
|
111
|
+
start: () => Promise<void>,
|
|
112
|
+
): Promise<void> {
|
|
113
|
+
const stopping = stop();
|
|
114
|
+
const revision = this.#revision;
|
|
115
|
+
try {
|
|
116
|
+
await stopping;
|
|
117
|
+
} catch (error) {
|
|
118
|
+
if (status.kind !== "failed" || !Object.is(error, status.error)) {
|
|
119
|
+
throw error;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (this.#revision === revision) {
|
|
123
|
+
await start();
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Resets the controller, then releases every owned listener before reporting a failure. */
|
|
128
|
+
async stop(reset: () => void): Promise<void> {
|
|
129
|
+
const listeners = this.#unlisten ?? [];
|
|
130
|
+
this.#unlisten = undefined;
|
|
131
|
+
this.#eventFailure = undefined;
|
|
132
|
+
this.#revision += 1;
|
|
133
|
+
let results: Array<PromiseSettledResult<void>>;
|
|
134
|
+
try {
|
|
135
|
+
reset();
|
|
136
|
+
} finally {
|
|
137
|
+
results = await disposeAll(listeners);
|
|
138
|
+
}
|
|
139
|
+
const failure = results.find((result) => result.status === "rejected");
|
|
140
|
+
if (failure !== undefined) {
|
|
141
|
+
throw failure.reason;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { OPAQUE_ID_RULE } from "./generated/identifiers.ts";
|
|
2
|
+
|
|
3
|
+
/** Whether Rust would admit `value` as an opaque Longhorn identifier. */
|
|
4
|
+
export function isOpaqueId(value: unknown): value is string {
|
|
5
|
+
// Every admitted character is one byte, so the length is also the byte count.
|
|
6
|
+
return (
|
|
7
|
+
typeof value === "string" &&
|
|
8
|
+
value.length > 0 &&
|
|
9
|
+
value.length <= OPAQUE_ID_RULE.maximumBytes &&
|
|
10
|
+
Array.from(value).every((character) => OPAQUE_ID_RULE.characters.includes(character))
|
|
11
|
+
);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** The shape of a generated `DomainId` grammar. */
|
|
15
|
+
export interface DomainIdRule {
|
|
16
|
+
readonly maximumBytes: number;
|
|
17
|
+
readonly separator: string;
|
|
18
|
+
readonly segmentInitial: string;
|
|
19
|
+
readonly segmentAdditional: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Whether Rust would admit `value` as a `DomainId` under the generated rule. */
|
|
23
|
+
export function matchesDomainIdRule(value: unknown, rule: DomainIdRule): value is string {
|
|
24
|
+
const { maximumBytes, separator, segmentInitial, segmentAdditional } = rule;
|
|
25
|
+
return (
|
|
26
|
+
typeof value === "string" &&
|
|
27
|
+
new TextEncoder().encode(value).length <= maximumBytes &&
|
|
28
|
+
value.split(separator).every((segment) => {
|
|
29
|
+
const [first, ...rest] = segment;
|
|
30
|
+
return (
|
|
31
|
+
first !== undefined &&
|
|
32
|
+
segmentInitial.includes(first) &&
|
|
33
|
+
rest.every(
|
|
34
|
+
(character) =>
|
|
35
|
+
segmentInitial.includes(character) || segmentAdditional.includes(character),
|
|
36
|
+
)
|
|
37
|
+
);
|
|
38
|
+
})
|
|
39
|
+
);
|
|
40
|
+
}
|
package/src/core/index.ts
CHANGED
|
@@ -1,2 +1,11 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { CheckedSnapshotConnection, CheckedSubscription } from "./connection.ts";
|
|
2
|
+
export type {
|
|
3
|
+
CheckedSnapshotConnectionOptions,
|
|
4
|
+
ConnectionFailure,
|
|
5
|
+
ConnectionFailurePhase,
|
|
6
|
+
ConnectionFailureReporter,
|
|
7
|
+
SnapshotEventAction,
|
|
8
|
+
} from "./connection.ts";
|
|
2
9
|
export * from "./transport.ts";
|
|
10
|
+
export { isOpaqueId } from "./identifiers.ts";
|
|
11
|
+
export { OPAQUE_ID_RULE } from "./generated/identifiers.ts";
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { hasHole } from "./record.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Whether JSON encoding carries `value` to Rust unchanged: nothing in it
|
|
5
|
+
* throws, is dropped, turns into `null`, is rewritten by a `toJSON`, holds text
|
|
6
|
+
* Rust cannot decode or nests more than `maximumDepth` arrays and records.
|
|
7
|
+
*/
|
|
8
|
+
export function isJsonValue(value: unknown, maximumDepth: number): boolean {
|
|
9
|
+
return carries(value, new Set(), maximumDepth);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Rust strings are Unicode, so it cannot decode an unpaired surrogate. */
|
|
13
|
+
export function isUnicodeText(value: string): boolean {
|
|
14
|
+
return !/\p{Surrogate}/u.test(value);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function carries(value: unknown, ancestors: Set<object>, depth: number): boolean {
|
|
18
|
+
if (value === null || typeof value === "boolean") {
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
if (typeof value === "string") {
|
|
22
|
+
return isUnicodeText(value);
|
|
23
|
+
}
|
|
24
|
+
if (typeof value === "number") {
|
|
25
|
+
return Number.isFinite(value);
|
|
26
|
+
}
|
|
27
|
+
if (typeof value !== "object" || depth === 0 || ancestors.has(value)) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
if ("toJSON" in value && typeof value.toJSON === "function") {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
const keys = Reflect.ownKeys(value).filter((key) =>
|
|
34
|
+
Object.prototype.propertyIsEnumerable.call(value, key),
|
|
35
|
+
);
|
|
36
|
+
let members: ReadonlyArray<unknown>;
|
|
37
|
+
if (Array.isArray(value)) {
|
|
38
|
+
// A hole encodes as `null` and a property beside the indices is dropped.
|
|
39
|
+
if (keys.length !== value.length || hasHole(value)) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
members = value;
|
|
43
|
+
} else {
|
|
44
|
+
// A plain object of any realm: its prototype, if any, is a root.
|
|
45
|
+
const prototype: unknown = Object.getPrototypeOf(value);
|
|
46
|
+
if (prototype !== null && Object.getPrototypeOf(prototype) !== null) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
if (keys.some((key) => typeof key !== "string" || !isUnicodeText(key))) {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
members = Object.values(value);
|
|
53
|
+
}
|
|
54
|
+
ancestors.add(value);
|
|
55
|
+
const carried = members.every((member) => carries(member, ancestors, depth - 1));
|
|
56
|
+
ancestors.delete(value);
|
|
57
|
+
return carried;
|
|
58
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Unlisten } from "./transport.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Runs `run` inside a promise so a synchronous throw becomes a rejection that
|
|
5
|
+
* the caller can settle alongside the others, instead of abandoning them.
|
|
6
|
+
*/
|
|
7
|
+
export function attempt<Value>(run: () => Value | PromiseLike<Value>): Promise<Value> {
|
|
8
|
+
return new Promise<Value>((resolve) => {
|
|
9
|
+
resolve(run());
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Disposes every registration. One unlisten that throws, sync or async, does
|
|
15
|
+
* not skip the rest. Results include any failures and follow registration order.
|
|
16
|
+
*/
|
|
17
|
+
export function disposeAll(
|
|
18
|
+
unlisten: ReadonlyArray<() => void | Promise<void>>,
|
|
19
|
+
): Promise<Array<PromiseSettledResult<void>>> {
|
|
20
|
+
return Promise.allSettled(unlisten.map((dispose) => attempt(dispose)));
|
|
21
|
+
}
|
|
22
|
+
/** Holds one unlisten and shares its result between competing cleanup paths. */
|
|
23
|
+
export class ListenerDisposal {
|
|
24
|
+
unlisten: Unlisten | undefined;
|
|
25
|
+
#cleanup: Promise<void> | undefined;
|
|
26
|
+
|
|
27
|
+
close(): Promise<void> {
|
|
28
|
+
const unlisten = this.unlisten;
|
|
29
|
+
if (unlisten !== undefined) {
|
|
30
|
+
this.unlisten = undefined;
|
|
31
|
+
this.#cleanup = Promise.resolve().then(() => unlisten());
|
|
32
|
+
}
|
|
33
|
+
return this.#cleanup ?? Promise.resolve();
|
|
34
|
+
}
|
|
35
|
+
}
|