@northtalk/ts 0.6.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/{binary-value-C4ZiXVjv.d.ts → binary-value-M8v567iA.d.ts} +1 -1
- package/{checker-CUhn2XaM.js → checker-Coml0Tf9.js} +581 -71
- package/{core-egHbNmve.d.ts → core-D4qYURer.d.ts} +1 -1
- package/{debug--Of4MSLw.d.ts → debug-C_Ob5rkl.d.ts} +126 -9
- package/debug.d.ts +1 -1
- package/debug.js +1 -1
- package/{driver-BIm1mL77.d.ts → driver-CLi_OmyQ.d.ts} +1 -1
- package/driver.d.ts +1 -1
- package/driver.js +1 -1
- package/encoding-BL2ECJmg.js +338 -0
- package/{engine-DbDcopGM.d.ts → engine-BBNHy4-F.d.ts} +2 -2
- package/{engine-CXf5FCwi.js → engine-DhBk0lTt.js} +6 -6
- package/{errors-D11zizZg.js → errors-P3ga1I-9.js} +124 -21
- package/{group-BpzTo0k4.js → group-BqccKTWz.js} +367 -68
- package/{http-fetch-DFwVIOQg.js → http-fetch-DA-NbFIM.js} +2 -2
- package/http.d.ts +1 -1
- package/http.js +4 -4
- package/index-CBuczWK0.d.ts +9 -0
- package/index.d.ts +6 -12
- package/index.js +15 -14
- package/{inline-grants-XbX9HL_w.js → inline-grants-BT2nnfLA.js} +4 -5
- package/{lean-aCvN6y52.js → lean-BJmLobmd.js} +13 -8
- package/{lean-DiS9rUcn.d.ts → lean-CFLJ9VrX.d.ts} +155 -6
- package/lean.d.ts +5 -6
- package/lean.js +15 -14
- package/{manifest-CCfpn0kC.js → manifest-37x-HSxH.js} +4 -3
- package/message-layer.d.ts +2 -2
- package/message-layer.js +2 -2
- package/notify.d.ts +1 -1
- package/notify.js +1 -1
- package/{objects-DmUc0lha.js → objects-DE6kRk0r.js} +1 -1
- package/{objects-DYq_YvaQ.d.ts → objects-DVlpWsi0.d.ts} +7 -3
- package/{operations-DnCcztkZ.js → operations-CGjDlkGU.js} +73 -15
- package/package.json +5 -1
- package/peer-capability-elnbQy-J.js +149 -0
- package/peer.d.ts +173 -0
- package/peer.js +542 -0
- package/{readers-8WCimMop.d.ts → readers-ChQ0e92c.d.ts} +1 -1
- package/{readers-CsvlahLi.js → readers-yMS1eAOp.js} +1 -1
- package/{request-pump-binary-oqjWFqZA.js → request-pump-binary-BS1J8DF2.js} +4 -4
- package/{router-BZ-aYeXA.js → router-Bm7A2kjt.js} +4 -4
- package/{router-BQsO8aPV.d.ts → router-D0rf5SkW.d.ts} +2 -2
- package/session.d.ts +2 -2
- package/session.js +64 -16
- package/setup.d.ts +3 -2
- package/setup.js +13 -16
- package/sqlite.d.ts +2 -2
- package/sqlite.js +5 -5
- package/{store-DHlo6fP5.js → store-D80OlpyG.js} +2 -2
- package/store-capability-DBLlWaNO.js +187 -0
- package/store.d.ts +2 -2
- package/store.js +2 -2
- package/{user-capability-BcRQyyEw.js → user-capability-D6hGa3eF.js} +3 -4
- package/wasm.d.ts +13 -6
- package/wasm.js +45 -19
- package/encoding-RUuES_e4.js +0 -99
- package/manifest-mpivREn-.d.ts +0 -122
- package/store-capability-BrUUAwVS.js +0 -389
package/peer.js
ADDED
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
|
|
2
|
+
import { L as nothing, N as map, R as num, et as text, i as ScriptError, k as list } from "./errors-P3ga1I-9.js";
|
|
3
|
+
import { n as toBase64, t as fromBase64 } from "./base64-CDO-a06R.js";
|
|
4
|
+
import { r as decodeValue } from "./readers-yMS1eAOp.js";
|
|
5
|
+
import { t as encodeValue } from "./encoding-BL2ECJmg.js";
|
|
6
|
+
import { a as peerKind } from "./peer-capability-elnbQy-J.js";
|
|
7
|
+
//#region src/peer-message-port.ts
|
|
8
|
+
/** The largest frame a link sends or reads, in UTF-8 bytes. */
|
|
9
|
+
const MAX_FRAME$1 = 16777216;
|
|
10
|
+
const tooLarge$1 = (frame) => frame.length * 3 > MAX_FRAME$1 && new TextEncoder().encode(frame).length > MAX_FRAME$1;
|
|
11
|
+
var MessagePortLink = class {
|
|
12
|
+
port;
|
|
13
|
+
name;
|
|
14
|
+
key = "";
|
|
15
|
+
closed = false;
|
|
16
|
+
address;
|
|
17
|
+
events;
|
|
18
|
+
held = [];
|
|
19
|
+
constructor(port, name) {
|
|
20
|
+
this.port = port;
|
|
21
|
+
this.name = name;
|
|
22
|
+
this.address = name;
|
|
23
|
+
port.addEventListener("message", this.message);
|
|
24
|
+
port.addEventListener("close", this.ended);
|
|
25
|
+
port.start?.();
|
|
26
|
+
}
|
|
27
|
+
get identity() {
|
|
28
|
+
return map([
|
|
29
|
+
["key", text(this.key)],
|
|
30
|
+
["via", text("port")],
|
|
31
|
+
...this.name === void 0 ? [] : [["name", text(this.name)]]
|
|
32
|
+
]);
|
|
33
|
+
}
|
|
34
|
+
message = (event) => {
|
|
35
|
+
const body = event.data;
|
|
36
|
+
if (this.closed) return;
|
|
37
|
+
if (typeof body !== "string") return;
|
|
38
|
+
if (tooLarge$1(body)) return this.close();
|
|
39
|
+
if (!this.key) return this.hello(body);
|
|
40
|
+
if (this.events) this.events.frame(body);
|
|
41
|
+
else this.held.push(body);
|
|
42
|
+
};
|
|
43
|
+
ended = () => this.close();
|
|
44
|
+
hello(body) {
|
|
45
|
+
let claim;
|
|
46
|
+
try {
|
|
47
|
+
claim = JSON.parse(body).hello;
|
|
48
|
+
} catch {
|
|
49
|
+
claim = void 0;
|
|
50
|
+
}
|
|
51
|
+
if (typeof claim !== "string" || !/^[\da-f]{64}$/.test(claim)) return this.close();
|
|
52
|
+
this.key = claim;
|
|
53
|
+
this.events?.keyed();
|
|
54
|
+
}
|
|
55
|
+
open(events, own) {
|
|
56
|
+
this.events = events;
|
|
57
|
+
if (this.closed) return events.close();
|
|
58
|
+
this.send(JSON.stringify({ hello: own }));
|
|
59
|
+
if (this.key) events.keyed();
|
|
60
|
+
for (const body of this.held.splice(0)) events.frame(body);
|
|
61
|
+
}
|
|
62
|
+
send(frame) {
|
|
63
|
+
if (this.closed || tooLarge$1(frame)) return false;
|
|
64
|
+
this.port.postMessage(frame);
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
close() {
|
|
68
|
+
if (this.closed) return;
|
|
69
|
+
this.closed = true;
|
|
70
|
+
this.port.removeEventListener("message", this.message);
|
|
71
|
+
this.port.removeEventListener("close", this.ended);
|
|
72
|
+
const port = this.port;
|
|
73
|
+
if (typeof port.start === "function") port.close?.();
|
|
74
|
+
this.events?.close();
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* A link to the Host at the other end of a `MessagePort`, or of a `Worker`
|
|
79
|
+
* from the page, or of the page from a worker's `self`. Closing a port ends
|
|
80
|
+
* the link at both ends. A worker that is terminated sends no word, so the
|
|
81
|
+
* page closes the link itself, as `WorkerSupervisor.closeOnTerminate` does.
|
|
82
|
+
*/
|
|
83
|
+
const messagePortLink = (port, options = {}) => new MessagePortLink(port, options.name);
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region src/peer-websocket.ts
|
|
86
|
+
/** The largest frame a link sends or reads, in UTF-8 bytes. */
|
|
87
|
+
const MAX_FRAME = 16777216;
|
|
88
|
+
const PROOF = "northtalk peer ws";
|
|
89
|
+
const REFUSED = 1008;
|
|
90
|
+
const tooLarge = (frame) => frame.length * 3 > MAX_FRAME && new TextEncoder().encode(frame).length > MAX_FRAME;
|
|
91
|
+
const hex$1 = (bytes) => Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
92
|
+
const fingerprint = /^[\da-f]{64}$/;
|
|
93
|
+
const proved = (nonce, key) => new TextEncoder().encode(PROOF + nonce + key);
|
|
94
|
+
var WebSocketLink = class {
|
|
95
|
+
socket;
|
|
96
|
+
server;
|
|
97
|
+
client;
|
|
98
|
+
key = "";
|
|
99
|
+
closed = false;
|
|
100
|
+
address;
|
|
101
|
+
events;
|
|
102
|
+
own = "";
|
|
103
|
+
signer;
|
|
104
|
+
nonce = hex$1(crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16)));
|
|
105
|
+
theirs;
|
|
106
|
+
caller = [];
|
|
107
|
+
early = [];
|
|
108
|
+
work = Promise.resolve();
|
|
109
|
+
outbox = [];
|
|
110
|
+
unsent = [];
|
|
111
|
+
constructor(socket, server, client) {
|
|
112
|
+
this.socket = socket;
|
|
113
|
+
this.server = server;
|
|
114
|
+
this.client = client;
|
|
115
|
+
this.address = (server ?? client)?.name;
|
|
116
|
+
socket.addEventListener("message", this.message);
|
|
117
|
+
socket.addEventListener("open", this.opened);
|
|
118
|
+
socket.addEventListener("close", this.ended);
|
|
119
|
+
socket.addEventListener("error", this.ended);
|
|
120
|
+
}
|
|
121
|
+
get identity() {
|
|
122
|
+
const entries = [["key", text(this.key)], ["via", text("ws")]];
|
|
123
|
+
if (this.client) {
|
|
124
|
+
const origin = this.origin();
|
|
125
|
+
if (origin) entries.push(["origin", text(origin)]);
|
|
126
|
+
}
|
|
127
|
+
return map([...entries, ...this.caller]);
|
|
128
|
+
}
|
|
129
|
+
origin() {
|
|
130
|
+
try {
|
|
131
|
+
const origin = new URL(this.socket.url ?? "").origin;
|
|
132
|
+
return origin === "null" ? void 0 : origin;
|
|
133
|
+
} catch {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
get pin() {
|
|
138
|
+
return this.client?.pin ?? /#([\da-f]{64})$/.exec(this.socket.url ?? "")?.[1];
|
|
139
|
+
}
|
|
140
|
+
message = (event) => {
|
|
141
|
+
const body = event.data;
|
|
142
|
+
if (this.closed) return;
|
|
143
|
+
if (typeof body !== "string" || tooLarge(body)) return this.close();
|
|
144
|
+
if (!this.events) {
|
|
145
|
+
this.early.push(body);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
this.step(body);
|
|
149
|
+
};
|
|
150
|
+
opened = () => {
|
|
151
|
+
for (const body of this.unsent.splice(0)) this.socket.send(body);
|
|
152
|
+
};
|
|
153
|
+
ended = () => this.close();
|
|
154
|
+
step(body) {
|
|
155
|
+
this.work = this.work.then(() => this.closed ? void 0 : this.read(body)).catch(() => this.close());
|
|
156
|
+
}
|
|
157
|
+
async read(body) {
|
|
158
|
+
if (this.key) return this.events.frame(body);
|
|
159
|
+
const said = JSON.parse(body);
|
|
160
|
+
if (!this.theirs) return this.hello(said);
|
|
161
|
+
const proof = typeof said["proof"] === "string" ? fromBase64(said["proof"]) : void 0;
|
|
162
|
+
if (!(proof !== void 0 && await crypto.subtle.verify({
|
|
163
|
+
name: "ECDSA",
|
|
164
|
+
hash: "SHA-256"
|
|
165
|
+
}, this.theirs.key, proof, proved(this.nonce, this.theirs.print)))) return this.close();
|
|
166
|
+
if (this.server?.authenticate) {
|
|
167
|
+
const data = await this.server.authenticate(this.theirs.token);
|
|
168
|
+
if (data === void 0) return this.close(REFUSED);
|
|
169
|
+
this.caller = Object.entries(data).filter(([k]) => k !== "key" && k !== "via").map(([k, v]) => [k, typeof v === "string" ? text(v) : v]);
|
|
170
|
+
}
|
|
171
|
+
if (this.closed) return;
|
|
172
|
+
this.key = this.theirs.print;
|
|
173
|
+
for (const frame of this.outbox.splice(0)) this.raw(frame);
|
|
174
|
+
this.events.keyed();
|
|
175
|
+
}
|
|
176
|
+
async hello(said) {
|
|
177
|
+
const { hello, key, nonce, token } = said;
|
|
178
|
+
const spki = typeof key === "string" ? fromBase64(key) : void 0;
|
|
179
|
+
if (typeof hello !== "string" || !fingerprint.test(hello) || typeof nonce !== "string" || !/^[\da-f]{32}$/.test(nonce) || !spki || await fingerprintOf(spki) !== hello || this.client && this.pin !== void 0 && this.pin !== hello) return this.close();
|
|
180
|
+
this.theirs = {
|
|
181
|
+
key: await crypto.subtle.importKey("spki", spki, {
|
|
182
|
+
name: "ECDSA",
|
|
183
|
+
namedCurve: "P-256"
|
|
184
|
+
}, false, ["verify"]),
|
|
185
|
+
nonce,
|
|
186
|
+
print: hello,
|
|
187
|
+
...typeof token === "string" ? { token } : {}
|
|
188
|
+
};
|
|
189
|
+
const signature = await this.signer.sign(proved(nonce, this.own));
|
|
190
|
+
this.raw(JSON.stringify({ proof: toBase64(signature) }));
|
|
191
|
+
}
|
|
192
|
+
open(events, own, signer) {
|
|
193
|
+
this.events = events;
|
|
194
|
+
if (this.closed) return events.close();
|
|
195
|
+
if (!signer) return this.close();
|
|
196
|
+
this.own = own;
|
|
197
|
+
this.signer = signer;
|
|
198
|
+
const token = this.client?.token;
|
|
199
|
+
this.raw(JSON.stringify({
|
|
200
|
+
hello: own,
|
|
201
|
+
key: toBase64(signer.spki),
|
|
202
|
+
nonce: this.nonce,
|
|
203
|
+
...token === void 0 ? {} : { token }
|
|
204
|
+
}));
|
|
205
|
+
for (const body of this.early.splice(0)) this.step(body);
|
|
206
|
+
}
|
|
207
|
+
raw(body) {
|
|
208
|
+
if (this.socket.readyState === 0) this.unsent.push(body);
|
|
209
|
+
else if (this.socket.readyState === 1) this.socket.send(body);
|
|
210
|
+
}
|
|
211
|
+
send(frame) {
|
|
212
|
+
if (this.closed || tooLarge(frame)) return false;
|
|
213
|
+
if (this.key) this.raw(frame);
|
|
214
|
+
else this.outbox.push(frame);
|
|
215
|
+
return true;
|
|
216
|
+
}
|
|
217
|
+
close(code) {
|
|
218
|
+
if (this.closed) return;
|
|
219
|
+
this.closed = true;
|
|
220
|
+
this.socket.removeEventListener("message", this.message);
|
|
221
|
+
this.socket.removeEventListener("open", this.opened);
|
|
222
|
+
this.socket.removeEventListener("close", this.ended);
|
|
223
|
+
this.socket.removeEventListener("error", this.ended);
|
|
224
|
+
if (this.socket.readyState < 2) this.socket.close(code ?? 1e3);
|
|
225
|
+
this.events?.close();
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
/**
|
|
229
|
+
* A link to the server at the other end of a WebSocket the caller opened,
|
|
230
|
+
* as a browser does. The server's key is proved, and checked against the
|
|
231
|
+
* pin when one is given.
|
|
232
|
+
*/
|
|
233
|
+
const webSocketLink = (socket, options = {}) => new WebSocketLink(socket, void 0, options);
|
|
234
|
+
/**
|
|
235
|
+
* A link to the Host that opened a WebSocket to this server, from the
|
|
236
|
+
* server's socket adapted to a `WebSocketEndpoint`. The other key is proved,
|
|
237
|
+
* and `authenticate` checks the token before any Remote Send arrives.
|
|
238
|
+
*/
|
|
239
|
+
const acceptWebSocket = (socket, options = {}) => new WebSocketLink(socket, options, void 0);
|
|
240
|
+
//#endregion
|
|
241
|
+
//#region src/peer.ts
|
|
242
|
+
const hex = (bytes) => Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
243
|
+
/** A key fingerprint: lowercase hex SHA-256 of the DER SubjectPublicKeyInfo. */
|
|
244
|
+
const fingerprintOf = async (spki) => hex(new Uint8Array(await crypto.subtle.digest("SHA-256", spki)));
|
|
245
|
+
/** A new ECDSA P-256 Host key and its fingerprint. */
|
|
246
|
+
const newHostKey = async () => {
|
|
247
|
+
const keys = await crypto.subtle.generateKey({
|
|
248
|
+
name: "ECDSA",
|
|
249
|
+
namedCurve: "P-256"
|
|
250
|
+
}, false, ["sign", "verify"]);
|
|
251
|
+
const spki = new Uint8Array(await crypto.subtle.exportKey("spki", keys.publicKey));
|
|
252
|
+
const signer = {
|
|
253
|
+
spki,
|
|
254
|
+
sign: async (data) => new Uint8Array(await crypto.subtle.sign({
|
|
255
|
+
name: "ECDSA",
|
|
256
|
+
hash: "SHA-256"
|
|
257
|
+
}, keys.privateKey, data))
|
|
258
|
+
};
|
|
259
|
+
return {
|
|
260
|
+
fingerprint: await fingerprintOf(spki),
|
|
261
|
+
keys,
|
|
262
|
+
signer
|
|
263
|
+
};
|
|
264
|
+
};
|
|
265
|
+
const secret = () => hex(crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16)));
|
|
266
|
+
const field = (frame, key) => {
|
|
267
|
+
const v = frame.kind === "map" ? frame.get(key) : void 0;
|
|
268
|
+
return v && v.kind !== "nothing" ? v : void 0;
|
|
269
|
+
};
|
|
270
|
+
const has = (frame, key) => frame.kind === "map" && frame.entries().some(([k]) => k === key);
|
|
271
|
+
const whole = (v) => {
|
|
272
|
+
const n = v?.kind === "number" ? Number(v.asDecimal().toString()) : NaN;
|
|
273
|
+
return Number.isSafeInteger(n) ? n : void 0;
|
|
274
|
+
};
|
|
275
|
+
const items = (v) => v?.kind === "list" ? Array.from({ length: v.length }, (_, i) => v.index(i + 1)) : [];
|
|
276
|
+
const failure = (code, data = []) => new ScriptError(code, code, map(data));
|
|
277
|
+
const sendFailed = (reason, error) => failure("send failed", [["reason", text(reason)], ...error ? [["error", error]] : []]);
|
|
278
|
+
const split = (id) => {
|
|
279
|
+
const m = /^([\da-f]{64})\/([\da-f]{32})$/.exec(id);
|
|
280
|
+
return m ? [m[1], m[2]] : void 0;
|
|
281
|
+
};
|
|
282
|
+
/** A link the router has opened, with the questions it waits on. */
|
|
283
|
+
var Wire = class {
|
|
284
|
+
link;
|
|
285
|
+
next = 1;
|
|
286
|
+
asked = /* @__PURE__ */ new Map();
|
|
287
|
+
constructor(link) {
|
|
288
|
+
this.link = link;
|
|
289
|
+
}
|
|
290
|
+
send(frame) {
|
|
291
|
+
let body;
|
|
292
|
+
try {
|
|
293
|
+
body = encodeValue(frame);
|
|
294
|
+
} catch {
|
|
295
|
+
return false;
|
|
296
|
+
}
|
|
297
|
+
return !this.link.closed && this.link.send(body);
|
|
298
|
+
}
|
|
299
|
+
/** Sends a question; `answer` hears its answer, or null when the link closes. */
|
|
300
|
+
ask(kind, rest, answer) {
|
|
301
|
+
const n = this.next++;
|
|
302
|
+
this.asked.set(n, answer);
|
|
303
|
+
if (!this.send(map([[kind, num(n)], ...rest]))) {
|
|
304
|
+
this.asked.delete(n);
|
|
305
|
+
answer(null);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
answered(n, frame) {
|
|
309
|
+
const answer = this.asked.get(n);
|
|
310
|
+
this.asked.delete(n);
|
|
311
|
+
answer?.(frame);
|
|
312
|
+
}
|
|
313
|
+
closed() {
|
|
314
|
+
const waiting = [...this.asked.values()];
|
|
315
|
+
this.asked.clear();
|
|
316
|
+
for (const answer of waiting) answer(null);
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
/** A Host's side of `peer`: its exposures and the links to other Hosts. */
|
|
320
|
+
var PeerRouter = class PeerRouter {
|
|
321
|
+
o;
|
|
322
|
+
key;
|
|
323
|
+
group;
|
|
324
|
+
exposures = /* @__PURE__ */ new Map();
|
|
325
|
+
names = /* @__PURE__ */ new Map();
|
|
326
|
+
peers = /* @__PURE__ */ new Map();
|
|
327
|
+
wires = /* @__PURE__ */ new Map();
|
|
328
|
+
byKey = /* @__PURE__ */ new Map();
|
|
329
|
+
callers = /* @__PURE__ */ new Map();
|
|
330
|
+
constructor(o) {
|
|
331
|
+
this.o = o;
|
|
332
|
+
this.key = o.key;
|
|
333
|
+
}
|
|
334
|
+
/** A router for the Group, with a new Host key unless the options give one. */
|
|
335
|
+
static async create(group, options = {}) {
|
|
336
|
+
const made = options.key === void 0 ? await newHostKey() : void 0;
|
|
337
|
+
const router = new PeerRouter({
|
|
338
|
+
...options,
|
|
339
|
+
key: options.key ?? made.fingerprint,
|
|
340
|
+
signer: options.signer ?? made?.signer
|
|
341
|
+
});
|
|
342
|
+
router.bind(group);
|
|
343
|
+
return router;
|
|
344
|
+
}
|
|
345
|
+
/** Gives the router its Group, whose Scripts its exposures name. */
|
|
346
|
+
bind(group) {
|
|
347
|
+
this.group = group;
|
|
348
|
+
}
|
|
349
|
+
/** Carries Remote Sends over a link. The newest link to a key carries what this Host sends there. */
|
|
350
|
+
connect(link) {
|
|
351
|
+
if (this.wires.has(link)) return;
|
|
352
|
+
const wire = new Wire(link);
|
|
353
|
+
this.wires.set(link, wire);
|
|
354
|
+
if (link.key) this.byKey.set(link.key, wire);
|
|
355
|
+
link.open({
|
|
356
|
+
keyed: () => {
|
|
357
|
+
if (this.wires.get(link) === wire) this.byKey.set(link.key, wire);
|
|
358
|
+
},
|
|
359
|
+
frame: (body) => {
|
|
360
|
+
let frame;
|
|
361
|
+
try {
|
|
362
|
+
frame = decodeValue(body, (kind, id) => kind === "peer" ? this.peerFor(id) : void 0);
|
|
363
|
+
} catch {
|
|
364
|
+
return link.close();
|
|
365
|
+
}
|
|
366
|
+
this.arrive(wire, frame);
|
|
367
|
+
},
|
|
368
|
+
close: () => {
|
|
369
|
+
this.wires.delete(link);
|
|
370
|
+
if (this.byKey.get(link.key) === wire) this.byKey.delete(link.key);
|
|
371
|
+
wire.closed();
|
|
372
|
+
}
|
|
373
|
+
}, this.key, this.o.signer);
|
|
374
|
+
}
|
|
375
|
+
/** Closes every link. */
|
|
376
|
+
close() {
|
|
377
|
+
for (const link of this.wires.keys()) link.close();
|
|
378
|
+
}
|
|
379
|
+
async wireFor(link) {
|
|
380
|
+
const got = await link;
|
|
381
|
+
if (!got || got.closed) return;
|
|
382
|
+
this.connect(got);
|
|
383
|
+
return this.wires.get(got);
|
|
384
|
+
}
|
|
385
|
+
/** A link to the Host with this key, dialled if the Host knows its address. */
|
|
386
|
+
wireTo(key) {
|
|
387
|
+
const open = this.byKey.get(key);
|
|
388
|
+
if (open && !open.link.closed) return Promise.resolve(open);
|
|
389
|
+
const address = this.o.addressOf?.(key);
|
|
390
|
+
return address === void 0 || !this.o.dial ? Promise.resolve(void 0) : this.wireFor(this.o.dial(address));
|
|
391
|
+
}
|
|
392
|
+
/** This Group's one Peer for an exposure id. */
|
|
393
|
+
peerFor(id) {
|
|
394
|
+
let peer = this.peers.get(id);
|
|
395
|
+
if (!peer) {
|
|
396
|
+
peer = this.group.object(peerKind, id, null);
|
|
397
|
+
this.peers.set(id, peer);
|
|
398
|
+
}
|
|
399
|
+
return peer.value;
|
|
400
|
+
}
|
|
401
|
+
local(identity, secret, message, args, waits) {
|
|
402
|
+
const held = this.exposures.get(secret);
|
|
403
|
+
const script = held && this.group.script(held.script);
|
|
404
|
+
if (!script) return waits ? "not exposed" : void 0;
|
|
405
|
+
try {
|
|
406
|
+
if (!waits) {
|
|
407
|
+
this.callers.set(script.deliver({
|
|
408
|
+
name: message,
|
|
409
|
+
args
|
|
410
|
+
}), identity);
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
const requested = script.request({
|
|
414
|
+
name: message,
|
|
415
|
+
args
|
|
416
|
+
});
|
|
417
|
+
this.callers.set(requested.id, identity);
|
|
418
|
+
return requested.result;
|
|
419
|
+
} catch (error) {
|
|
420
|
+
if (error.name === "MailboxFull") return waits ? "mailbox full" : void 0;
|
|
421
|
+
throw error;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
arrive(wire, frame) {
|
|
425
|
+
const reach = whole(field(frame, "reach"));
|
|
426
|
+
const call = whole(field(frame, "call"));
|
|
427
|
+
const post = field(frame, "post")?.asText();
|
|
428
|
+
const answer = whole(field(frame, "reached") ?? field(frame, "replied"));
|
|
429
|
+
if (answer !== void 0) return wire.answered(answer, frame);
|
|
430
|
+
if (reach !== void 0) {
|
|
431
|
+
const key = this.names.get(field(frame, "name")?.asText() ?? "");
|
|
432
|
+
wire.send(map([["reached", num(reach)], key === void 0 ? ["error", text("not exposed")] : ["peer", text(`${this.key}/${key}`)]]));
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
const to = post ?? field(frame, "to")?.asText() ?? "";
|
|
436
|
+
const parts = split(to);
|
|
437
|
+
const message = field(frame, "message")?.asText() ?? "";
|
|
438
|
+
const args = items(field(frame, "args"));
|
|
439
|
+
const mine = parts && parts[0] === this.key ? parts[1] : void 0;
|
|
440
|
+
const identity = wire.link.identity;
|
|
441
|
+
if (post !== void 0) {
|
|
442
|
+
if (mine) this.local(identity, mine, message, args, false);
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
if (call === void 0) return;
|
|
446
|
+
const reply = (rest) => wire.send(map([["replied", num(call)], rest]));
|
|
447
|
+
const failed = (reason, error) => reply(["failed", map([["reason", text(reason)], ...error ? [["error", error]] : []])]);
|
|
448
|
+
const result = mine ? this.local(identity, mine, message, args, true) : "not exposed";
|
|
449
|
+
if (typeof result === "string" || result === void 0) {
|
|
450
|
+
failed(result ?? "not exposed");
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
result.then((value) => {
|
|
454
|
+
if (!reply(["value", value])) failed("unreachable");
|
|
455
|
+
}, (error) => {
|
|
456
|
+
const e = error;
|
|
457
|
+
if (e.name === "ScriptError" && e.code === "send failed") {
|
|
458
|
+
const data = e.data;
|
|
459
|
+
failed(data?.get("reason").asText() ?? "errored", data?.kind === "map" && data.get("error").kind !== "nothing" ? data.get("error") : void 0);
|
|
460
|
+
} else failed("unreachable");
|
|
461
|
+
}).catch(() => failed("unreachable"));
|
|
462
|
+
}
|
|
463
|
+
/** The `peerCapability` implementation. */
|
|
464
|
+
impl = {
|
|
465
|
+
expose: (call, name) => this.expose(call, name),
|
|
466
|
+
unexpose: (call, exposure) => {
|
|
467
|
+
const key = typeof exposure === "string" ? this.names.get(exposure) : split(exposure.id)?.[1];
|
|
468
|
+
const held = key === void 0 ? void 0 : this.exposures.get(key);
|
|
469
|
+
if (held && held.script === call.scriptName) {
|
|
470
|
+
this.exposures.delete(key);
|
|
471
|
+
if (held.name !== void 0) this.names.delete(held.name);
|
|
472
|
+
}
|
|
473
|
+
},
|
|
474
|
+
reach: (call, address) => this.reach(call, address),
|
|
475
|
+
post: (call, peer, message, args) => this.send(call, peer, message, args, false),
|
|
476
|
+
call: (call, peer, message, args) => this.send(call, peer, message, args, true),
|
|
477
|
+
caller: (call) => (call.delivery === void 0 ? void 0 : this.callers.get(call.delivery)) ?? nothing
|
|
478
|
+
};
|
|
479
|
+
expose(call, name) {
|
|
480
|
+
if (name !== void 0 && this.names.has(name)) {
|
|
481
|
+
const key = this.names.get(name);
|
|
482
|
+
if (this.exposures.get(key).script !== call.scriptName) return call.fail(failure("name in use"));
|
|
483
|
+
return call.answer(this.peerFor(`${this.key}/${key}`));
|
|
484
|
+
}
|
|
485
|
+
const key = secret();
|
|
486
|
+
this.exposures.set(key, {
|
|
487
|
+
name,
|
|
488
|
+
script: call.scriptName
|
|
489
|
+
});
|
|
490
|
+
if (name !== void 0) this.names.set(name, key);
|
|
491
|
+
call.answer(this.peerFor(`${this.key}/${key}`));
|
|
492
|
+
}
|
|
493
|
+
reach(call, address) {
|
|
494
|
+
let named;
|
|
495
|
+
for (const link of this.wires.keys()) if (link.address === address && !link.closed) named = link;
|
|
496
|
+
const dialled = named ? Promise.resolve(named) : this.o.dial?.(address);
|
|
497
|
+
if (!dialled) return call.fail(failure("bad address"));
|
|
498
|
+
this.wireFor(dialled).then((wire) => {
|
|
499
|
+
if (!wire) return call.fail(failure("bad address"));
|
|
500
|
+
wire.ask("reach", [["name", text(address)]], (frame) => {
|
|
501
|
+
const id = frame && field(frame, "peer")?.asText();
|
|
502
|
+
if (id && wire.link.key && split(id)?.[0] === wire.link.key) call.answer(this.peerFor(id));
|
|
503
|
+
else call.fail(failure(frame ? "not exposed" : "bad address"));
|
|
504
|
+
});
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
send(call, peer, message, args, waits) {
|
|
508
|
+
const parts = split(peer.id);
|
|
509
|
+
if (!parts) return waits ? call.fail(sendFailed("unreachable")) : void 0;
|
|
510
|
+
const [key, secret] = parts;
|
|
511
|
+
if (key === this.key) {
|
|
512
|
+
const identity = map([["key", text(key)]]);
|
|
513
|
+
const result = this.local(identity, secret, message, args, waits);
|
|
514
|
+
if (!waits) return;
|
|
515
|
+
if (typeof result === "string" || result === void 0) return call.fail(sendFailed(result ?? "not exposed"));
|
|
516
|
+
result.then((value) => call.answer(value), (error) => {
|
|
517
|
+
const e = error;
|
|
518
|
+
call.fail(e.name === "ScriptError" && e.code === "send failed" ? failure("send failed", e.data?.kind === "map" ? [...e.data.entries()] : []) : sendFailed("unreachable"));
|
|
519
|
+
});
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
this.wireTo(key).then((wire) => {
|
|
523
|
+
const rest = [["message", text(message)], ["args", list(...args)]];
|
|
524
|
+
if (!waits) {
|
|
525
|
+
wire?.send(map([["post", text(peer.id)], ...rest]));
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
if (!wire) return call.fail(sendFailed("unreachable"));
|
|
529
|
+
wire.ask("call", [["to", text(peer.id)], ...rest], (frame) => {
|
|
530
|
+
const failed = frame && field(frame, "failed");
|
|
531
|
+
if (frame && has(frame, "value")) call.answer(frame.get("value"));
|
|
532
|
+
else if (failed) {
|
|
533
|
+
const reason = failed.get("reason").asText() ?? "unreachable";
|
|
534
|
+
const error = failed.get("error");
|
|
535
|
+
call.fail(sendFailed(reason, error.kind === "nothing" ? void 0 : error));
|
|
536
|
+
} else call.fail(sendFailed("unreachable"));
|
|
537
|
+
});
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
//#endregion
|
|
542
|
+
export { PeerRouter, acceptWebSocket, fingerprintOf, messagePortLink, newHostKey, webSocketLink };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference lib="esnext.disposable" />
|
|
2
|
-
import { J as Value } from "./debug
|
|
2
|
+
import { J as Value } from "./debug-C_Ob5rkl.js";
|
|
3
3
|
//#region src/readers.d.ts
|
|
4
4
|
/** Finds the Host Object a reader names by its kind and id, or none. */
|
|
5
5
|
type ObjectResolver = (kind: string, id: string) => {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
import { A as listValues,
|
|
2
|
+
import { A as listValues, H as quantity, Jt as parseInstant, L as nothing, N as map, T as instant, W as range, Zt as assertScalarText, et as text, f as bytesOf, l as Value, m as civilDate, o as invalidValue, u as bool, v as dec, w as hiddenCodePoint } from "./errors-P3ga1I-9.js";
|
|
3
3
|
import { t as fromBase64 } from "./base64-CDO-a06R.js";
|
|
4
4
|
//#region src/readers.ts
|
|
5
5
|
var Reader = class {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
|
|
2
|
-
import { A as listValues,
|
|
3
|
-
import {
|
|
4
|
-
import { _ as viewSource, t as checkSource } from "./checker-
|
|
2
|
+
import { A as listValues, H as quantity, Jt as parseInstant, L as nothing, N as map, R as num, S as functionValue, T as instant, Ut as formatInstant, V as patternValue, W as range, d as bytes, et as text, l as Value, m as civilDate, o as invalidValue, rn as normalizeNFC, u as bool, v as dec, z as objectValue } from "./errors-P3ga1I-9.js";
|
|
3
|
+
import { g as decodeUtf8, ot as patternSource } from "./operations-CGjDlkGU.js";
|
|
4
|
+
import { _ as viewSource, t as checkSource } from "./checker-Coml0Tf9.js";
|
|
5
5
|
import { n as toBase64 } from "./base64-CDO-a06R.js";
|
|
6
|
-
import { a as fromJson, n as JsonObject, o as isJsonNumber, s as isJsonObject, t as JsonNumber } from "./readers-
|
|
6
|
+
import { a as fromJson, n as JsonObject, o as isJsonNumber, s as isJsonObject, t as JsonNumber } from "./readers-yMS1eAOp.js";
|
|
7
7
|
//#region src/binary-value.ts
|
|
8
8
|
const coreErrors = /* @__PURE__ */ new WeakSet();
|
|
9
9
|
const max = 2n ** 64n;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
|
|
2
|
-
import { A as listValues, N as map,
|
|
3
|
-
import {
|
|
4
|
-
import { t as encodeValue } from "./encoding-
|
|
2
|
+
import { A as listValues, N as map, et as text, i as ScriptError, t as HostError, u as bool } from "./errors-P3ga1I-9.js";
|
|
3
|
+
import { Q as sizeOf, ut as compareText } from "./operations-CGjDlkGU.js";
|
|
4
|
+
import { t as encodeValue } from "./encoding-BL2ECJmg.js";
|
|
5
5
|
import { n as sha256 } from "./sha256-9CGf5BS_.js";
|
|
6
|
-
import { n as hostScriptError } from "./store-capability-
|
|
6
|
+
import { n as hostScriptError } from "./store-capability-DBLlWaNO.js";
|
|
7
7
|
//#region src/notify/router.ts
|
|
8
8
|
/**
|
|
9
9
|
* Whether a commit changed a key's value: compared by the bytes of the Value
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference lib="esnext.disposable" />
|
|
2
|
-
import { E as RestoreResult, I as TsRestoreOptions, J as Value, Mt as GrantContext, O as SaveOptions, P as TsGroup,
|
|
3
|
-
import { t as TsCore } from "./core-
|
|
2
|
+
import { $t as Group, E as RestoreResult, I as TsRestoreOptions, J as Value, Mt as GrantContext, O as SaveOptions, P as TsGroup, bt as Call } from "./debug-C_Ob5rkl.js";
|
|
3
|
+
import { t as TsCore } from "./core-D4qYURer.js";
|
|
4
4
|
//#region src/notify/router.d.ts
|
|
5
5
|
/** A Store's four limits on its watches (Store Notifications, Limits). */
|
|
6
6
|
type WatchLimits = {
|
package/session.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference lib="esnext.disposable" />
|
|
2
|
-
import { J as Value } from "./debug
|
|
3
|
-
import { C as Setup, _ as UserPrompt, a as ReplayOptions, c as parseTranscript, d as DebugAction, f as MOCK_ARGUMENTS, g as SourcePlacement, h as SessionHost, i as parseEnvelope, l as replayTranscript, m as SessionEnvironment, n as ObjectReference, o as Replayed, p as Mock, r as SessionObjects, s as TranscriptItem, t as Envelope, u as writeTranscript, v as Waiting } from "./objects-
|
|
2
|
+
import { J as Value } from "./debug-C_Ob5rkl.js";
|
|
3
|
+
import { C as Setup, _ as UserPrompt, a as ReplayOptions, c as parseTranscript, d as DebugAction, f as MOCK_ARGUMENTS, g as SourcePlacement, h as SessionHost, i as parseEnvelope, l as replayTranscript, m as SessionEnvironment, n as ObjectReference, o as Replayed, p as Mock, r as SessionObjects, s as TranscriptItem, t as Envelope, u as writeTranscript, v as Waiting } from "./objects-DVlpWsi0.js";
|
|
4
4
|
//#region src/session/describe.d.ts
|
|
5
5
|
/** One declaration of the session source, as `:describe` reads it. */
|
|
6
6
|
type DeclaredName = {
|