@earendil-works/chord 1.1.0 → 1.3.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/CHANGELOG.md +78 -0
- package/README.md +79 -195
- package/dist/cell.d.ts +122 -0
- package/dist/cell.d.ts.map +1 -0
- package/dist/cell.js +356 -0
- package/dist/cell.js.map +1 -0
- package/dist/cluster.d.ts +27 -0
- package/dist/cluster.d.ts.map +1 -0
- package/dist/cluster.js +230 -0
- package/dist/cluster.js.map +1 -0
- package/dist/connection.d.ts +270 -0
- package/dist/connection.d.ts.map +1 -0
- package/dist/connection.js +1216 -0
- package/dist/connection.js.map +1 -0
- package/dist/context/index.d.ts +12 -1
- package/dist/context/index.d.ts.map +1 -1
- package/dist/context/index.js.map +1 -1
- package/dist/contract.d.ts +208 -0
- package/dist/contract.d.ts.map +1 -0
- package/dist/contract.js +246 -0
- package/dist/contract.js.map +1 -0
- package/dist/declare.d.ts +33 -0
- package/dist/declare.d.ts.map +1 -0
- package/dist/declare.js +2 -0
- package/dist/declare.js.map +1 -0
- package/dist/delta/apply-immutable-trusted.js.map +1 -1
- package/dist/delta/diff.d.ts +1 -1
- package/dist/delta/diff.d.ts.map +1 -1
- package/dist/delta/diff.js.map +1 -1
- package/dist/delta/index.d.ts +4 -3
- package/dist/delta/index.d.ts.map +1 -1
- package/dist/delta/index.js +45 -0
- package/dist/delta/index.js.map +1 -1
- package/dist/delta/revision-validator.d.ts +1 -1
- package/dist/delta/revision-validator.d.ts.map +1 -1
- package/dist/delta/revision-validator.js.map +1 -1
- package/dist/delta/tracker.d.ts +1 -1
- package/dist/delta/tracker.d.ts.map +1 -1
- package/dist/delta/tracker.js +40 -4
- package/dist/delta/tracker.js.map +1 -1
- package/dist/errors.d.ts +28 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +59 -0
- package/dist/errors.js.map +1 -0
- package/dist/host.d.ts +348 -0
- package/dist/host.d.ts.map +1 -0
- package/dist/host.js +1000 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +17 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -8
- package/dist/index.js.map +1 -1
- package/dist/json.d.ts +9 -1
- package/dist/json.d.ts.map +1 -1
- package/dist/json.js.map +1 -1
- package/dist/link.d.ts +107 -0
- package/dist/link.d.ts.map +1 -0
- package/dist/link.js +355 -0
- package/dist/link.js.map +1 -0
- package/dist/loader.d.ts +4 -0
- package/dist/loader.d.ts.map +1 -0
- package/dist/loader.js +73 -0
- package/dist/loader.js.map +1 -0
- package/dist/manager.d.ts +115 -0
- package/dist/manager.d.ts.map +1 -0
- package/dist/manager.js +345 -0
- package/dist/manager.js.map +1 -0
- package/dist/node.d.ts +3 -4
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +3 -2
- package/dist/node.js.map +1 -1
- package/dist/object.d.ts +438 -0
- package/dist/object.d.ts.map +1 -0
- package/dist/object.js +1571 -0
- package/dist/object.js.map +1 -0
- package/dist/output.d.ts +85 -0
- package/dist/output.d.ts.map +1 -0
- package/dist/output.js +350 -0
- package/dist/output.js.map +1 -0
- package/dist/publish.d.ts +5 -0
- package/dist/publish.d.ts.map +1 -0
- package/dist/publish.js +40 -0
- package/dist/publish.js.map +1 -0
- package/dist/registry.d.ts +25 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +6 -0
- package/dist/registry.js.map +1 -0
- package/dist/reload.d.ts +126 -0
- package/dist/reload.d.ts.map +1 -0
- package/dist/reload.js +653 -0
- package/dist/reload.js.map +1 -0
- package/dist/remote.d.ts +165 -0
- package/dist/remote.d.ts.map +1 -0
- package/dist/remote.js +612 -0
- package/dist/remote.js.map +1 -0
- package/dist/schema.d.ts +21 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +228 -0
- package/dist/schema.js.map +1 -0
- package/dist/state.d.ts +92 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/{services/state.js → state.js} +28 -138
- package/dist/state.js.map +1 -0
- package/dist/types.d.ts +69 -258
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/websocket.d.ts +15 -0
- package/dist/websocket.d.ts.map +1 -0
- package/dist/websocket.js +16 -0
- package/dist/websocket.js.map +1 -0
- package/dist/wire.d.ts +216 -0
- package/dist/wire.d.ts.map +1 -0
- package/dist/wire.js +165 -0
- package/dist/wire.js.map +1 -0
- package/docs/guide.md +814 -0
- package/docs/spec.md +288 -0
- package/docs/tutorial.md +931 -0
- package/package.json +14 -10
- package/src/delta/README.md +5 -3
- package/dist/api.d.ts +0 -20
- package/dist/api.d.ts.map +0 -1
- package/dist/api.js +0 -77
- package/dist/api.js.map +0 -1
- package/dist/bundler.d.ts +0 -6
- package/dist/bundler.d.ts.map +0 -1
- package/dist/bundler.js +0 -3
- package/dist/bundler.js.map +0 -1
- package/dist/facets/host.d.ts +0 -12
- package/dist/facets/host.d.ts.map +0 -1
- package/dist/facets/host.js +0 -763
- package/dist/facets/host.js.map +0 -1
- package/dist/facets/loader.d.ts +0 -3
- package/dist/facets/loader.d.ts.map +0 -1
- package/dist/facets/loader.js +0 -5
- package/dist/facets/loader.js.map +0 -1
- package/dist/node/bundle-loader.d.ts +0 -30
- package/dist/node/bundle-loader.d.ts.map +0 -1
- package/dist/node/bundle-loader.js +0 -377
- package/dist/node/bundle-loader.js.map +0 -1
- package/dist/node/bundle.d.ts +0 -26
- package/dist/node/bundle.d.ts.map +0 -1
- package/dist/node/bundle.js +0 -183
- package/dist/node/bundle.js.map +0 -1
- package/dist/node/manifest.d.ts +0 -36
- package/dist/node/manifest.d.ts.map +0 -1
- package/dist/node/manifest.js +0 -6
- package/dist/node/manifest.js.map +0 -1
- package/dist/node/package.d.ts +0 -15
- package/dist/node/package.d.ts.map +0 -1
- package/dist/node/package.js +0 -199
- package/dist/node/package.js.map +0 -1
- package/dist/services/consumer.d.ts +0 -11
- package/dist/services/consumer.d.ts.map +0 -1
- package/dist/services/consumer.js +0 -585
- package/dist/services/consumer.js.map +0 -1
- package/dist/services/errors.d.ts +0 -8
- package/dist/services/errors.d.ts.map +0 -1
- package/dist/services/errors.js +0 -22
- package/dist/services/errors.js.map +0 -1
- package/dist/services/handle.d.ts +0 -15
- package/dist/services/handle.d.ts.map +0 -1
- package/dist/services/handle.js +0 -101
- package/dist/services/handle.js.map +0 -1
- package/dist/services/instances.d.ts +0 -26
- package/dist/services/instances.d.ts.map +0 -1
- package/dist/services/instances.js +0 -140
- package/dist/services/instances.js.map +0 -1
- package/dist/services/loopback.d.ts +0 -5
- package/dist/services/loopback.d.ts.map +0 -1
- package/dist/services/loopback.js +0 -15
- package/dist/services/loopback.js.map +0 -1
- package/dist/services/provider.d.ts +0 -37
- package/dist/services/provider.d.ts.map +0 -1
- package/dist/services/provider.js +0 -521
- package/dist/services/provider.js.map +0 -1
- package/dist/services/state-codec.d.ts +0 -15
- package/dist/services/state-codec.d.ts.map +0 -1
- package/dist/services/state-codec.js +0 -133
- package/dist/services/state-codec.js.map +0 -1
- package/dist/services/state-internals.d.ts +0 -13
- package/dist/services/state-internals.d.ts.map +0 -1
- package/dist/services/state-internals.js +0 -10
- package/dist/services/state-internals.js.map +0 -1
- package/dist/services/state.d.ts +0 -28
- package/dist/services/state.d.ts.map +0 -1
- package/dist/services/state.js.map +0 -1
- package/dist/services/wire.d.ts +0 -63
- package/dist/services/wire.d.ts.map +0 -1
- package/dist/services/wire.js +0 -186
- package/dist/services/wire.js.map +0 -1
package/dist/wire.d.ts
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import { type Static, type TSchema, Type } from "typebox";
|
|
2
|
+
import { ChordError } from "./errors.ts";
|
|
3
|
+
/**
|
|
4
|
+
* The link protocol version, announced in `hello`. Peers of different versions do not talk: the side that notices
|
|
5
|
+
* closes the link with reason `protocol-version <its version>`, so the other side learns it too
|
|
6
|
+
* (`LinkOptions.onProtocolMismatch`).
|
|
7
|
+
*/
|
|
8
|
+
export declare const PROTOCOL_VERSION = 4;
|
|
9
|
+
declare const RootRefSchema: Type.TObject<{
|
|
10
|
+
rootId: Type.TString;
|
|
11
|
+
codeVersion: Type.TInteger;
|
|
12
|
+
}>;
|
|
13
|
+
export type RootRef = Static<typeof RootRefSchema>;
|
|
14
|
+
declare const WireErrorSchema: Type.TObject<{
|
|
15
|
+
code: Type.TEnum<["aborted", "contract_mismatch", "forbidden", "gone", "invalid", "not_in_variant", "not_ready", "released", "superseded", "unavailable", "upgrade_conflict", "upgrade_required"]>;
|
|
16
|
+
message: Type.TString;
|
|
17
|
+
reason: Type.TOptional<Type.TString>;
|
|
18
|
+
}>;
|
|
19
|
+
export type WireError = Static<typeof WireErrorSchema>;
|
|
20
|
+
declare const WireReloadOptionsSchema: Type.TObject<{
|
|
21
|
+
remove: Type.TOptional<Type.TImmutable<Type.TArray<Type.TString>>>;
|
|
22
|
+
gateDeadline: Type.TOptional<Type.TNumber>;
|
|
23
|
+
retireDeadline: Type.TOptional<Type.TNumber>;
|
|
24
|
+
}>;
|
|
25
|
+
export type WireReloadOptions = Static<typeof WireReloadOptionsSchema>;
|
|
26
|
+
/** A child's prepared reload, sent with its vote (same fields as `ReloadReport`, without children). */
|
|
27
|
+
declare const WireReloadReportSchema: Type.TObject<{
|
|
28
|
+
codeVersion: Type.TInteger;
|
|
29
|
+
previous: Type.TInteger;
|
|
30
|
+
changed: Type.TImmutable<Type.TArray<Type.TString>>;
|
|
31
|
+
removed: Type.TImmutable<Type.TArray<Type.TString>>;
|
|
32
|
+
prepared: Type.TImmutable<Type.TArray<Type.TString>>;
|
|
33
|
+
gated: Type.TImmutable<Type.TArray<Type.TString>>;
|
|
34
|
+
}>;
|
|
35
|
+
export type WireReloadReport = Static<typeof WireReloadReportSchema>;
|
|
36
|
+
declare const FrameSchema: Type.TUnion<[Type.TObject<{
|
|
37
|
+
t: Type.TLiteral<"hello">;
|
|
38
|
+
/** The link protocol version (`PROTOCOL_VERSION`; absent: 1). Peers of another version close the link. */
|
|
39
|
+
protocol: Type.TOptional<Type.TInteger>;
|
|
40
|
+
name: Type.TString;
|
|
41
|
+
kind: Type.TUnion<[Type.TLiteral<"host">, Type.TLiteral<"client">]>;
|
|
42
|
+
codeVersion: Type.TInteger;
|
|
43
|
+
/** The sender acknowledges what it receives (`ack`). Flow control applies only when both hellos say so. */
|
|
44
|
+
acks: Type.TOptional<Type.TBoolean>;
|
|
45
|
+
}>, Type.TObject<{
|
|
46
|
+
t: Type.TLiteral<"ack">;
|
|
47
|
+
n: Type.TInteger;
|
|
48
|
+
}>, Type.TObject<{
|
|
49
|
+
t: Type.TLiteral<"hold">;
|
|
50
|
+
id: Type.TInteger;
|
|
51
|
+
/**
|
|
52
|
+
* Target host name, resolved by the consumer from the token's `host` and `input`. Relays route on it after
|
|
53
|
+
* checking it against both; the target refuses an input its own contract places elsewhere.
|
|
54
|
+
*/
|
|
55
|
+
at: Type.TString;
|
|
56
|
+
/** The consumer's contract token; the provider checks that it covers it. Remote tokens carry `host`. */
|
|
57
|
+
token: Type.TObject<{
|
|
58
|
+
name: Type.TString;
|
|
59
|
+
scope: Type.TUnion<[Type.TLiteral<"remote">, Type.TLiteral<"local">]>;
|
|
60
|
+
version: Type.TInteger;
|
|
61
|
+
input: Type.TUnsafe<TSchema>;
|
|
62
|
+
host: Type.TOptional<Type.TUnion<[Type.TObject<{
|
|
63
|
+
name: Type.TString;
|
|
64
|
+
}>, Type.TObject<{
|
|
65
|
+
prefix: Type.TString;
|
|
66
|
+
key: Type.TString;
|
|
67
|
+
}>]>>;
|
|
68
|
+
variants: Type.TRecord<"^.*$", Type.TRecord<"^.*$", Type.TUnion<[Type.TObject<{
|
|
69
|
+
kind: Type.TLiteral<"method">;
|
|
70
|
+
args: Type.TOptional<Type.TUnsafe<TSchema>>;
|
|
71
|
+
result: Type.TOptional<Type.TUnsafe<TSchema>>;
|
|
72
|
+
}>, Type.TObject<{
|
|
73
|
+
kind: Type.TLiteral<"state">;
|
|
74
|
+
schema: Type.TUnsafe<TSchema>;
|
|
75
|
+
}>, Type.TObject<{
|
|
76
|
+
kind: Type.TLiteral<"opaque">;
|
|
77
|
+
id: Type.TString;
|
|
78
|
+
}>]>>>;
|
|
79
|
+
}>;
|
|
80
|
+
/** Normalized with the consumer's contract. */
|
|
81
|
+
input: Type.TUnknown;
|
|
82
|
+
/** A forwarded principal. Honored only on frames from the parent link. */
|
|
83
|
+
principal: Type.TOptional<Type.TUnknown>;
|
|
84
|
+
/**
|
|
85
|
+
* The client connection the hold comes from: an opaque id the server assigned to it, forwarded by relays. Honored
|
|
86
|
+
* only on frames from the parent link; present means the hold is a client's (not a reload participant).
|
|
87
|
+
*/
|
|
88
|
+
origin: Type.TOptional<Type.TString>;
|
|
89
|
+
/** False: no state for this hold until `sub` (a method-only binding). */
|
|
90
|
+
state: Type.TOptional<Type.TLiteral<false>>;
|
|
91
|
+
/** The consumer already has values (a re-hold after a link loss): its unit is not urgent. */
|
|
92
|
+
again: Type.TOptional<Type.TLiteral<true>>;
|
|
93
|
+
}>, Type.TObject<{
|
|
94
|
+
t: Type.TLiteral<"release">;
|
|
95
|
+
id: Type.TInteger;
|
|
96
|
+
}>, Type.TObject<{
|
|
97
|
+
t: Type.TLiteral<"shape">;
|
|
98
|
+
id: Type.TInteger;
|
|
99
|
+
variant: Type.TOptional<Type.TString>;
|
|
100
|
+
members: Type.TImmutable<Type.TArray<Type.TString>>;
|
|
101
|
+
contract: Type.TString;
|
|
102
|
+
/** The state members whose snapshots follow this frame; the consumer applies the shape once they are here. */
|
|
103
|
+
state: Type.TImmutable<Type.TArray<Type.TString>>;
|
|
104
|
+
}>, Type.TObject<{
|
|
105
|
+
t: Type.TLiteral<"gone">;
|
|
106
|
+
id: Type.TInteger;
|
|
107
|
+
error: Type.TObject<{
|
|
108
|
+
code: Type.TEnum<["aborted", "contract_mismatch", "forbidden", "gone", "invalid", "not_in_variant", "not_ready", "released", "superseded", "unavailable", "upgrade_conflict", "upgrade_required"]>;
|
|
109
|
+
message: Type.TString;
|
|
110
|
+
reason: Type.TOptional<Type.TString>;
|
|
111
|
+
}>;
|
|
112
|
+
}>, Type.TObject<{
|
|
113
|
+
t: Type.TLiteral<"call">;
|
|
114
|
+
id: Type.TInteger;
|
|
115
|
+
hold: Type.TInteger;
|
|
116
|
+
member: Type.TString;
|
|
117
|
+
args: Type.TOptional<Type.TUnknown>;
|
|
118
|
+
/** Absent: the receiving host starts a root (browsers never carry roots). */
|
|
119
|
+
root: Type.TOptional<Type.TObject<{
|
|
120
|
+
rootId: Type.TString;
|
|
121
|
+
codeVersion: Type.TInteger;
|
|
122
|
+
}>>;
|
|
123
|
+
}>, Type.TObject<{
|
|
124
|
+
t: Type.TLiteral<"result">;
|
|
125
|
+
id: Type.TInteger;
|
|
126
|
+
value: Type.TOptional<Type.TUnknown>;
|
|
127
|
+
error: Type.TOptional<Type.TObject<{
|
|
128
|
+
code: Type.TEnum<["aborted", "contract_mismatch", "forbidden", "gone", "invalid", "not_in_variant", "not_ready", "released", "superseded", "unavailable", "upgrade_conflict", "upgrade_required"]>;
|
|
129
|
+
message: Type.TString;
|
|
130
|
+
reason: Type.TOptional<Type.TString>;
|
|
131
|
+
}>>;
|
|
132
|
+
}>, Type.TObject<{
|
|
133
|
+
t: Type.TLiteral<"cancel">;
|
|
134
|
+
id: Type.TInteger;
|
|
135
|
+
}>, Type.TObject<{
|
|
136
|
+
t: Type.TLiteral<"sub">;
|
|
137
|
+
hold: Type.TInteger;
|
|
138
|
+
}>, Type.TObject<{
|
|
139
|
+
t: Type.TLiteral<"unsub">;
|
|
140
|
+
hold: Type.TInteger;
|
|
141
|
+
}>, Type.TObject<{
|
|
142
|
+
t: Type.TLiteral<"resync">;
|
|
143
|
+
hold: Type.TInteger;
|
|
144
|
+
m: Type.TString;
|
|
145
|
+
}>, Type.TObject<{
|
|
146
|
+
t: Type.TLiteral<"state">;
|
|
147
|
+
hold: Type.TInteger;
|
|
148
|
+
m: Type.TString;
|
|
149
|
+
/** `snap`/`reset` without `value`: the value is `undefined`. */
|
|
150
|
+
as: Type.TUnion<[Type.TLiteral<"snap">, Type.TLiteral<"reset">, Type.TLiteral<"ops">, Type.TLiteral<"error">]>;
|
|
151
|
+
value: Type.TOptional<Type.TUnknown>;
|
|
152
|
+
/** Encoded with the stream's delta encoder; the decoder validates each op. */
|
|
153
|
+
ops: Type.TOptional<Type.TImmutable<Type.TArray<Type.TImmutable<Type.TArray<Type.TUnknown>>>>>;
|
|
154
|
+
error: Type.TOptional<Type.TObject<{
|
|
155
|
+
code: Type.TEnum<["aborted", "contract_mismatch", "forbidden", "gone", "invalid", "not_in_variant", "not_ready", "released", "superseded", "unavailable", "upgrade_conflict", "upgrade_required"]>;
|
|
156
|
+
message: Type.TString;
|
|
157
|
+
reason: Type.TOptional<Type.TString>;
|
|
158
|
+
}>>;
|
|
159
|
+
}>, Type.TObject<{
|
|
160
|
+
t: Type.TLiteral<"ping">;
|
|
161
|
+
n: Type.TInteger;
|
|
162
|
+
}>, Type.TObject<{
|
|
163
|
+
t: Type.TLiteral<"pong">;
|
|
164
|
+
n: Type.TInteger;
|
|
165
|
+
}>, Type.TObject<{
|
|
166
|
+
t: Type.TLiteral<"bye">;
|
|
167
|
+
reason: Type.TOptional<Type.TString>;
|
|
168
|
+
}>, Type.TObject<{
|
|
169
|
+
t: Type.TLiteral<"root_end">;
|
|
170
|
+
rootId: Type.TString;
|
|
171
|
+
}>, Type.TObject<{
|
|
172
|
+
t: Type.TLiteral<"invalidate">;
|
|
173
|
+
pattern: Type.TRecord<"^.*$", Type.TUnknown>;
|
|
174
|
+
}>, Type.TObject<{
|
|
175
|
+
t: Type.TLiteral<"prepare">;
|
|
176
|
+
reload: Type.TInteger;
|
|
177
|
+
codeVersion: Type.TInteger;
|
|
178
|
+
plugins: Type.TImmutable<Type.TArray<Type.TString>>;
|
|
179
|
+
options: Type.TObject<{
|
|
180
|
+
remove: Type.TOptional<Type.TImmutable<Type.TArray<Type.TString>>>;
|
|
181
|
+
gateDeadline: Type.TOptional<Type.TNumber>;
|
|
182
|
+
retireDeadline: Type.TOptional<Type.TNumber>;
|
|
183
|
+
}>;
|
|
184
|
+
}>, Type.TObject<{
|
|
185
|
+
t: Type.TLiteral<"vote">;
|
|
186
|
+
reload: Type.TInteger;
|
|
187
|
+
error: Type.TOptional<Type.TObject<{
|
|
188
|
+
code: Type.TEnum<["aborted", "contract_mismatch", "forbidden", "gone", "invalid", "not_in_variant", "not_ready", "released", "superseded", "unavailable", "upgrade_conflict", "upgrade_required"]>;
|
|
189
|
+
message: Type.TString;
|
|
190
|
+
reason: Type.TOptional<Type.TString>;
|
|
191
|
+
}>>;
|
|
192
|
+
report: Type.TOptional<Type.TObject<{
|
|
193
|
+
codeVersion: Type.TInteger;
|
|
194
|
+
previous: Type.TInteger;
|
|
195
|
+
changed: Type.TImmutable<Type.TArray<Type.TString>>;
|
|
196
|
+
removed: Type.TImmutable<Type.TArray<Type.TString>>;
|
|
197
|
+
prepared: Type.TImmutable<Type.TArray<Type.TString>>;
|
|
198
|
+
gated: Type.TImmutable<Type.TArray<Type.TString>>;
|
|
199
|
+
}>>;
|
|
200
|
+
}>, Type.TObject<{
|
|
201
|
+
t: Type.TLiteral<"commit">;
|
|
202
|
+
reload: Type.TInteger;
|
|
203
|
+
}>, Type.TObject<{
|
|
204
|
+
t: Type.TLiteral<"retire">;
|
|
205
|
+
codeVersion: Type.TInteger;
|
|
206
|
+
}>, Type.TObject<{
|
|
207
|
+
t: Type.TLiteral<"abort">;
|
|
208
|
+
reload: Type.TInteger;
|
|
209
|
+
}>]>;
|
|
210
|
+
export type Frame = Static<typeof FrameSchema>;
|
|
211
|
+
/** A frame that passes validation; anything else is a protocol violation. */
|
|
212
|
+
export declare const isFrame: (value: unknown) => value is Frame;
|
|
213
|
+
export declare function toWireError(error: unknown): WireError;
|
|
214
|
+
export declare const fromWireError: (error: WireError) => ChordError;
|
|
215
|
+
export {};
|
|
216
|
+
//# sourceMappingURL=wire.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"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,UAAW,OAAO,KAAG,KAAK,IAAI,KAAwC,CAAC;AAE3F,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,SAAS,CAOrD;AAED,eAAO,MAAM,aAAa,UAAW,SAAS,KAAG,UACyD,CAAC"}
|
package/dist/wire.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { Type } from "typebox";
|
|
2
|
+
import { Value } from "typebox/value";
|
|
3
|
+
import { DescriptionSchema } from "./contract.js";
|
|
4
|
+
import { CHORD_ERROR_CODES, ChordError } from "./errors.js";
|
|
5
|
+
// JSON frames between two hosts (or a host and a client). Ids are chosen by the side that creates the hold, call or
|
|
6
|
+
// subscription; every frame type has one direction, so ids never collide:
|
|
7
|
+
// consumer → provider: hold, release, call, cancel, sub, unsub, resync
|
|
8
|
+
// provider → consumer: shape, gone, result, state
|
|
9
|
+
// State is addressed by (hold id, member): a hold implies the state of its variant, sent as units (a shape followed by
|
|
10
|
+
// a snapshot of each member the consumer lacks; see Connection).
|
|
11
|
+
// both: hello, ping, pong, bye, root_end, ack; parent → child: invalidate, prepare, commit, abort, retire;
|
|
12
|
+
// child → parent: vote
|
|
13
|
+
//
|
|
14
|
+
// Each frame is declared once, as the schema every received frame is checked against (`isFrame`); the types are derived
|
|
15
|
+
// from it. Payloads (hold inputs, call args, results, snapshots) stay `unknown`: they are validated where they are
|
|
16
|
+
// used, against the contract. State ops are checked op by op by the delta decoder.
|
|
17
|
+
/**
|
|
18
|
+
* The link protocol version, announced in `hello`. Peers of different versions do not talk: the side that notices
|
|
19
|
+
* closes the link with reason `protocol-version <its version>`, so the other side learns it too
|
|
20
|
+
* (`LinkOptions.onProtocolMismatch`).
|
|
21
|
+
*/
|
|
22
|
+
export const PROTOCOL_VERSION = 4;
|
|
23
|
+
const Id = Type.Integer({ minimum: 1 });
|
|
24
|
+
const Name = Type.String({ minLength: 1 });
|
|
25
|
+
const Reload = Type.Integer({ minimum: 1 });
|
|
26
|
+
/** Arrays a sender may pass as readonly. */
|
|
27
|
+
const List = (item) => Type.Immutable(Type.Array(item));
|
|
28
|
+
const RootRefSchema = Type.Object({ rootId: Name, codeVersion: Type.Integer({ minimum: 1 }) });
|
|
29
|
+
const WireErrorSchema = Type.Object({
|
|
30
|
+
code: Type.Enum(CHORD_ERROR_CODES),
|
|
31
|
+
message: Type.String(),
|
|
32
|
+
reason: Type.Optional(Type.String()),
|
|
33
|
+
});
|
|
34
|
+
const WireReloadOptionsSchema = Type.Object({
|
|
35
|
+
remove: Type.Optional(List(Type.String())),
|
|
36
|
+
gateDeadline: Type.Optional(Type.Number()),
|
|
37
|
+
retireDeadline: Type.Optional(Type.Number()),
|
|
38
|
+
});
|
|
39
|
+
/** A child's prepared reload, sent with its vote (same fields as `ReloadReport`, without children). */
|
|
40
|
+
const WireReloadReportSchema = Type.Object({
|
|
41
|
+
codeVersion: Type.Integer(),
|
|
42
|
+
previous: Type.Integer(),
|
|
43
|
+
changed: List(Type.String()),
|
|
44
|
+
removed: List(Type.String()),
|
|
45
|
+
prepared: List(Type.String()),
|
|
46
|
+
gated: List(Type.String()),
|
|
47
|
+
});
|
|
48
|
+
const FrameSchema = Type.Union([
|
|
49
|
+
Type.Object({
|
|
50
|
+
t: Type.Literal("hello"),
|
|
51
|
+
/** The link protocol version (`PROTOCOL_VERSION`; absent: 1). Peers of another version close the link. */
|
|
52
|
+
protocol: Type.Optional(Type.Integer({ minimum: 1 })),
|
|
53
|
+
name: Name,
|
|
54
|
+
kind: Type.Union([Type.Literal("host"), Type.Literal("client")]),
|
|
55
|
+
codeVersion: Type.Integer({ minimum: 0 }),
|
|
56
|
+
/** The sender acknowledges what it receives (`ack`). Flow control applies only when both hellos say so. */
|
|
57
|
+
acks: Type.Optional(Type.Boolean()),
|
|
58
|
+
}),
|
|
59
|
+
/** Every frame received so far except `ack` frames, counted from the link's start, once processed. */
|
|
60
|
+
Type.Object({ t: Type.Literal("ack"), n: Type.Integer({ minimum: 1 }) }),
|
|
61
|
+
Type.Object({
|
|
62
|
+
t: Type.Literal("hold"),
|
|
63
|
+
id: Id,
|
|
64
|
+
/**
|
|
65
|
+
* Target host name, resolved by the consumer from the token's `host` and `input`. Relays route on it after
|
|
66
|
+
* checking it against both; the target refuses an input its own contract places elsewhere.
|
|
67
|
+
*/
|
|
68
|
+
at: Name,
|
|
69
|
+
/** The consumer's contract token; the provider checks that it covers it. Remote tokens carry `host`. */
|
|
70
|
+
token: DescriptionSchema,
|
|
71
|
+
/** Normalized with the consumer's contract. */
|
|
72
|
+
input: Type.Unknown(),
|
|
73
|
+
/** A forwarded principal. Honored only on frames from the parent link. */
|
|
74
|
+
principal: Type.Optional(Type.Unknown()),
|
|
75
|
+
/**
|
|
76
|
+
* The client connection the hold comes from: an opaque id the server assigned to it, forwarded by relays. Honored
|
|
77
|
+
* only on frames from the parent link; present means the hold is a client's (not a reload participant).
|
|
78
|
+
*/
|
|
79
|
+
origin: Type.Optional(Name),
|
|
80
|
+
/** False: no state for this hold until `sub` (a method-only binding). */
|
|
81
|
+
state: Type.Optional(Type.Literal(false)),
|
|
82
|
+
/** The consumer already has values (a re-hold after a link loss): its unit is not urgent. */
|
|
83
|
+
again: Type.Optional(Type.Literal(true)),
|
|
84
|
+
}),
|
|
85
|
+
Type.Object({ t: Type.Literal("release"), id: Id }),
|
|
86
|
+
Type.Object({
|
|
87
|
+
t: Type.Literal("shape"),
|
|
88
|
+
id: Id,
|
|
89
|
+
variant: Type.Optional(Type.String()),
|
|
90
|
+
members: List(Type.String()),
|
|
91
|
+
contract: Type.String(),
|
|
92
|
+
/** The state members whose snapshots follow this frame; the consumer applies the shape once they are here. */
|
|
93
|
+
state: List(Name),
|
|
94
|
+
}),
|
|
95
|
+
Type.Object({ t: Type.Literal("gone"), id: Id, error: WireErrorSchema }),
|
|
96
|
+
Type.Object({
|
|
97
|
+
t: Type.Literal("call"),
|
|
98
|
+
id: Id,
|
|
99
|
+
hold: Id,
|
|
100
|
+
member: Name,
|
|
101
|
+
args: Type.Optional(Type.Unknown()),
|
|
102
|
+
/** Absent: the receiving host starts a root (browsers never carry roots). */
|
|
103
|
+
root: Type.Optional(RootRefSchema),
|
|
104
|
+
}),
|
|
105
|
+
Type.Object({
|
|
106
|
+
t: Type.Literal("result"),
|
|
107
|
+
id: Id,
|
|
108
|
+
value: Type.Optional(Type.Unknown()),
|
|
109
|
+
error: Type.Optional(WireErrorSchema),
|
|
110
|
+
}),
|
|
111
|
+
Type.Object({ t: Type.Literal("cancel"), id: Id }),
|
|
112
|
+
/** Starts the state of a hold opened with `state: false` (answered with a unit). */
|
|
113
|
+
Type.Object({ t: Type.Literal("sub"), hold: Id }),
|
|
114
|
+
/** Stops the state of a hold. */
|
|
115
|
+
Type.Object({ t: Type.Literal("unsub"), hold: Id }),
|
|
116
|
+
/** Sends member `m` again as a reset (a relay that had to drop what waited for it). */
|
|
117
|
+
Type.Object({ t: Type.Literal("resync"), hold: Id, m: Name }),
|
|
118
|
+
Type.Object({
|
|
119
|
+
t: Type.Literal("state"),
|
|
120
|
+
hold: Id,
|
|
121
|
+
m: Name,
|
|
122
|
+
/** `snap`/`reset` without `value`: the value is `undefined`. */
|
|
123
|
+
as: Type.Union([Type.Literal("snap"), Type.Literal("reset"), Type.Literal("ops"), Type.Literal("error")]),
|
|
124
|
+
value: Type.Optional(Type.Unknown()),
|
|
125
|
+
/** Encoded with the stream's delta encoder; the decoder validates each op. */
|
|
126
|
+
ops: Type.Optional(List(List(Type.Unknown()))),
|
|
127
|
+
error: Type.Optional(WireErrorSchema),
|
|
128
|
+
}),
|
|
129
|
+
Type.Object({ t: Type.Literal("ping"), n: Type.Integer() }),
|
|
130
|
+
Type.Object({ t: Type.Literal("pong"), n: Type.Integer() }),
|
|
131
|
+
/** Ends the link (consumed by the link itself). `reason: "protocol-version <n>"`: the sender speaks version n. */
|
|
132
|
+
Type.Object({ t: Type.Literal("bye"), reason: Type.Optional(Type.String()) }),
|
|
133
|
+
/** The root ended at its origin: its leases on objects end (sent on every link the root's calls used). */
|
|
134
|
+
Type.Object({ t: Type.Literal("root_end"), rootId: Name }),
|
|
135
|
+
Type.Object({ t: Type.Literal("invalidate"), pattern: Type.Record(Type.String(), Type.Unknown()) }),
|
|
136
|
+
Type.Object({
|
|
137
|
+
t: Type.Literal("prepare"),
|
|
138
|
+
reload: Reload,
|
|
139
|
+
codeVersion: Type.Integer({ minimum: 1 }),
|
|
140
|
+
plugins: List(Type.String()),
|
|
141
|
+
options: WireReloadOptionsSchema,
|
|
142
|
+
}),
|
|
143
|
+
Type.Object({
|
|
144
|
+
t: Type.Literal("vote"),
|
|
145
|
+
reload: Reload,
|
|
146
|
+
error: Type.Optional(WireErrorSchema),
|
|
147
|
+
report: Type.Optional(WireReloadReportSchema),
|
|
148
|
+
}),
|
|
149
|
+
Type.Object({ t: Type.Literal("commit"), reload: Reload }),
|
|
150
|
+
/** The parent dropped `codeVersion`: roots of it may still come from there no longer. */
|
|
151
|
+
Type.Object({ t: Type.Literal("retire"), codeVersion: Type.Integer({ minimum: 1 }) }),
|
|
152
|
+
Type.Object({ t: Type.Literal("abort"), reload: Reload }),
|
|
153
|
+
]);
|
|
154
|
+
/** A frame that passes validation; anything else is a protocol violation. */
|
|
155
|
+
export const isFrame = (value) => Value.Check(FrameSchema, value);
|
|
156
|
+
export function toWireError(error) {
|
|
157
|
+
if (error instanceof ChordError) {
|
|
158
|
+
return error.reason === undefined
|
|
159
|
+
? { code: error.code, message: error.message }
|
|
160
|
+
: { code: error.code, message: error.message, reason: error.reason };
|
|
161
|
+
}
|
|
162
|
+
return { code: "unavailable", message: error instanceof Error ? error.message : String(error) };
|
|
163
|
+
}
|
|
164
|
+
export const fromWireError = (error) => new ChordError(error.code, error.message.replace(/^[a-z_]+(\/[a-z_]+)?: /, ""), { reason: error.reason });
|
|
165
|
+
//# sourceMappingURL=wire.js.map
|
package/dist/wire.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wire.js","sourceRoot":"","sources":["../src/wire.ts"],"names":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CAAC;IACrF,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;CACzD,CAAC,CAAC;AAGH,6EAA6E;AAC7E,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,KAAc,EAAkB,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,WAAW,EAAE,KAAK,CAAC,CAAC;AAE3F,MAAM,UAAU,WAAW,CAAC,KAAc;IACzC,IAAI,KAAK,YAAY,UAAU,EAAE,CAAC;QACjC,OAAO,KAAK,CAAC,MAAM,KAAK,SAAS;YAChC,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE;YAC9C,CAAC,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC;IACvE,CAAC;IACD,OAAO,EAAE,IAAI,EAAE,aAAa,EAAE,OAAO,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;AACjG,CAAC;AAED,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,KAAgB,EAAc,EAAE,CAC7D,IAAI,UAAU,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,wBAAwB,EAAE,EAAE,CAAC,EAAE,EAAE,MAAM,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC","sourcesContent":["import { type Static, type TSchema, Type } from \"typebox\";\nimport { Value } from \"typebox/value\";\nimport { DescriptionSchema } from \"./contract.ts\";\nimport { CHORD_ERROR_CODES, ChordError } from \"./errors.ts\";\n\n// JSON frames between two hosts (or a host and a client). Ids are chosen by the side that creates the hold, call or\n// subscription; every frame type has one direction, so ids never collide:\n// consumer → provider: hold, release, call, cancel, sub, unsub, resync\n// provider → consumer: shape, gone, result, state\n// State is addressed by (hold id, member): a hold implies the state of its variant, sent as units (a shape followed by\n// a snapshot of each member the consumer lacks; see Connection).\n// both: hello, ping, pong, bye, root_end, ack; parent → child: invalidate, prepare, commit, abort, retire;\n// child → parent: vote\n//\n// Each frame is declared once, as the schema every received frame is checked against (`isFrame`); the types are derived\n// from it. Payloads (hold inputs, call args, results, snapshots) stay `unknown`: they are validated where they are\n// used, against the contract. State ops are checked op by op by the delta decoder.\n\n/**\n * The link protocol version, announced in `hello`. Peers of different versions do not talk: the side that notices\n * closes the link with reason `protocol-version <its version>`, so the other side learns it too\n * (`LinkOptions.onProtocolMismatch`).\n */\nexport const PROTOCOL_VERSION = 4;\n\nconst Id = Type.Integer({ minimum: 1 });\nconst Name = Type.String({ minLength: 1 });\nconst Reload = Type.Integer({ minimum: 1 });\n/** Arrays a sender may pass as readonly. */\nconst List = <T extends TSchema>(item: T) => Type.Immutable(Type.Array(item));\n\nconst RootRefSchema = Type.Object({ rootId: Name, codeVersion: Type.Integer({ minimum: 1 }) });\nexport type RootRef = Static<typeof RootRefSchema>;\n\nconst WireErrorSchema = Type.Object({\n\tcode: Type.Enum(CHORD_ERROR_CODES),\n\tmessage: Type.String(),\n\treason: Type.Optional(Type.String()),\n});\nexport type WireError = Static<typeof WireErrorSchema>;\n\nconst WireReloadOptionsSchema = Type.Object({\n\tremove: Type.Optional(List(Type.String())),\n\tgateDeadline: Type.Optional(Type.Number()),\n\tretireDeadline: Type.Optional(Type.Number()),\n});\nexport type WireReloadOptions = Static<typeof WireReloadOptionsSchema>;\n\n/** A child's prepared reload, sent with its vote (same fields as `ReloadReport`, without children). */\nconst WireReloadReportSchema = Type.Object({\n\tcodeVersion: Type.Integer(),\n\tprevious: Type.Integer(),\n\tchanged: List(Type.String()),\n\tremoved: List(Type.String()),\n\tprepared: List(Type.String()),\n\tgated: List(Type.String()),\n});\nexport type WireReloadReport = Static<typeof WireReloadReportSchema>;\n\nconst FrameSchema = Type.Union([\n\tType.Object({\n\t\tt: Type.Literal(\"hello\"),\n\t\t/** The link protocol version (`PROTOCOL_VERSION`; absent: 1). Peers of another version close the link. */\n\t\tprotocol: Type.Optional(Type.Integer({ minimum: 1 })),\n\t\tname: Name,\n\t\tkind: Type.Union([Type.Literal(\"host\"), Type.Literal(\"client\")]),\n\t\tcodeVersion: Type.Integer({ minimum: 0 }),\n\t\t/** The sender acknowledges what it receives (`ack`). Flow control applies only when both hellos say so. */\n\t\tacks: Type.Optional(Type.Boolean()),\n\t}),\n\t/** Every frame received so far except `ack` frames, counted from the link's start, once processed. */\n\tType.Object({ t: Type.Literal(\"ack\"), n: Type.Integer({ minimum: 1 }) }),\n\tType.Object({\n\t\tt: Type.Literal(\"hold\"),\n\t\tid: Id,\n\t\t/**\n\t\t * Target host name, resolved by the consumer from the token's `host` and `input`. Relays route on it after\n\t\t * checking it against both; the target refuses an input its own contract places elsewhere.\n\t\t */\n\t\tat: Name,\n\t\t/** The consumer's contract token; the provider checks that it covers it. Remote tokens carry `host`. */\n\t\ttoken: DescriptionSchema,\n\t\t/** Normalized with the consumer's contract. */\n\t\tinput: Type.Unknown(),\n\t\t/** A forwarded principal. Honored only on frames from the parent link. */\n\t\tprincipal: Type.Optional(Type.Unknown()),\n\t\t/**\n\t\t * The client connection the hold comes from: an opaque id the server assigned to it, forwarded by relays. Honored\n\t\t * only on frames from the parent link; present means the hold is a client's (not a reload participant).\n\t\t */\n\t\torigin: Type.Optional(Name),\n\t\t/** False: no state for this hold until `sub` (a method-only binding). */\n\t\tstate: Type.Optional(Type.Literal(false)),\n\t\t/** The consumer already has values (a re-hold after a link loss): its unit is not urgent. */\n\t\tagain: Type.Optional(Type.Literal(true)),\n\t}),\n\tType.Object({ t: Type.Literal(\"release\"), id: Id }),\n\tType.Object({\n\t\tt: Type.Literal(\"shape\"),\n\t\tid: Id,\n\t\tvariant: Type.Optional(Type.String()),\n\t\tmembers: List(Type.String()),\n\t\tcontract: Type.String(),\n\t\t/** The state members whose snapshots follow this frame; the consumer applies the shape once they are here. */\n\t\tstate: List(Name),\n\t}),\n\tType.Object({ t: Type.Literal(\"gone\"), id: Id, error: WireErrorSchema }),\n\tType.Object({\n\t\tt: Type.Literal(\"call\"),\n\t\tid: Id,\n\t\thold: Id,\n\t\tmember: Name,\n\t\targs: Type.Optional(Type.Unknown()),\n\t\t/** Absent: the receiving host starts a root (browsers never carry roots). */\n\t\troot: Type.Optional(RootRefSchema),\n\t}),\n\tType.Object({\n\t\tt: Type.Literal(\"result\"),\n\t\tid: Id,\n\t\tvalue: Type.Optional(Type.Unknown()),\n\t\terror: Type.Optional(WireErrorSchema),\n\t}),\n\tType.Object({ t: Type.Literal(\"cancel\"), id: Id }),\n\t/** Starts the state of a hold opened with `state: false` (answered with a unit). */\n\tType.Object({ t: Type.Literal(\"sub\"), hold: Id }),\n\t/** Stops the state of a hold. */\n\tType.Object({ t: Type.Literal(\"unsub\"), hold: Id }),\n\t/** Sends member `m` again as a reset (a relay that had to drop what waited for it). */\n\tType.Object({ t: Type.Literal(\"resync\"), hold: Id, m: Name }),\n\tType.Object({\n\t\tt: Type.Literal(\"state\"),\n\t\thold: Id,\n\t\tm: Name,\n\t\t/** `snap`/`reset` without `value`: the value is `undefined`. */\n\t\tas: Type.Union([Type.Literal(\"snap\"), Type.Literal(\"reset\"), Type.Literal(\"ops\"), Type.Literal(\"error\")]),\n\t\tvalue: Type.Optional(Type.Unknown()),\n\t\t/** Encoded with the stream's delta encoder; the decoder validates each op. */\n\t\tops: Type.Optional(List(List(Type.Unknown()))),\n\t\terror: Type.Optional(WireErrorSchema),\n\t}),\n\tType.Object({ t: Type.Literal(\"ping\"), n: Type.Integer() }),\n\tType.Object({ t: Type.Literal(\"pong\"), n: Type.Integer() }),\n\t/** Ends the link (consumed by the link itself). `reason: \"protocol-version <n>\"`: the sender speaks version n. */\n\tType.Object({ t: Type.Literal(\"bye\"), reason: Type.Optional(Type.String()) }),\n\t/** The root ended at its origin: its leases on objects end (sent on every link the root's calls used). */\n\tType.Object({ t: Type.Literal(\"root_end\"), rootId: Name }),\n\tType.Object({ t: Type.Literal(\"invalidate\"), pattern: Type.Record(Type.String(), Type.Unknown()) }),\n\tType.Object({\n\t\tt: Type.Literal(\"prepare\"),\n\t\treload: Reload,\n\t\tcodeVersion: Type.Integer({ minimum: 1 }),\n\t\tplugins: List(Type.String()),\n\t\toptions: WireReloadOptionsSchema,\n\t}),\n\tType.Object({\n\t\tt: Type.Literal(\"vote\"),\n\t\treload: Reload,\n\t\terror: Type.Optional(WireErrorSchema),\n\t\treport: Type.Optional(WireReloadReportSchema),\n\t}),\n\tType.Object({ t: Type.Literal(\"commit\"), reload: Reload }),\n\t/** The parent dropped `codeVersion`: roots of it may still come from there no longer. */\n\tType.Object({ t: Type.Literal(\"retire\"), codeVersion: Type.Integer({ minimum: 1 }) }),\n\tType.Object({ t: Type.Literal(\"abort\"), reload: Reload }),\n]);\nexport type Frame = Static<typeof FrameSchema>;\n\n/** A frame that passes validation; anything else is a protocol violation. */\nexport const isFrame = (value: unknown): value is Frame => Value.Check(FrameSchema, value);\n\nexport function toWireError(error: unknown): WireError {\n\tif (error instanceof ChordError) {\n\t\treturn error.reason === undefined\n\t\t\t? { code: error.code, message: error.message }\n\t\t\t: { code: error.code, message: error.message, reason: error.reason };\n\t}\n\treturn { code: \"unavailable\", message: error instanceof Error ? error.message : String(error) };\n}\n\nexport const fromWireError = (error: WireError): ChordError =>\n\tnew ChordError(error.code, error.message.replace(/^[a-z_]+(\\/[a-z_]+)?: /, \"\"), { reason: error.reason });\n"]}
|