Repository navigation
Expand file tree
/
Copy pathrelay-client.ts
More file actions
775 lines (726 loc) · 33.5 KB
/
Copy pathrelay-client.ts
File metadata and controls
775 lines (726 loc) · 33.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
/**
* The phone's half of the blind relay — `docs/relay.md` is the protocol.
*
* One WebSocket to `<relay>/v1/phone/<serverId>` carries everything the app
* says to a box it cannot reach directly: a hello in the clear, then sealed
* frames from `shared/src/e2e.ts` holding requests, their responses, and the
* same dashboard stream `/ws` pushes. The relay forwards bytes it cannot read.
*
* Nothing above `api.ts` knows this exists. `request()` builds the same
* method, path, headers and body it always did and hands them here when the
* connection is a relay one; `SessionSocket` subscribes to this link instead
* of opening its own. That is the whole point of the seam: a relay is a
* transport, and the screens should never learn transport.
*
* What the relay does not get to see or do is what shapes this file. The
* secret the frames are keyed from never travels — the hello names it by
* device id or by the hash of a pairing code — so a relay, or anyone who
* knows a `serverId`, derives different keys and the box refuses its first
* frame. Randomness for the ephemeral key comes from the platform CSPRNG, never
* `Math.random`: the private half is what makes the exchange forward-secret.
*/
import { sha256 } from "@noble/hashes/sha2.js";
import {
RELAY_CLOSE,
RELAY_LIMITS,
RELAY_PROTOCOL,
RELAY_WAIT_QUERY,
type BoxHello,
type BoxToPhone,
type PhoneHello,
type PhoneToBox,
type RelayResponse,
type RelayToPhone,
type SocketMessage,
} from "./index";
import { clientSession, ephemeral, open, seal, type Ephemeral, type Session } from "./e2e";
import { UnauthorizedError, UnreachableError, hostOf } from "./errors";
import { retryDelay } from "./retry";
/** What the keychain keeps for a box reached through a relay. */
export interface RelayIdentity {
/** The relay's base URL, from the pairing code. */
relay: string;
serverId: string;
deviceId: string;
/** base64url, 32 bytes: this phone's share of the E2E key for that box. */
deviceSecret: string;
}
/** Everything a link needs to open: where, as whom, and the secret to key from. */
export interface RelayTarget {
relay: string;
serverId: string;
auth: PhoneHello["auth"];
/** 32 bytes: the device secret, or the pairing secret before there is a device. */
secret: Uint8Array;
}
/** A paired phone's link. */
export function deviceTarget(identity: RelayIdentity): RelayTarget {
return {
relay: identity.relay,
serverId: identity.serverId,
auth: { kind: "device", deviceId: identity.deviceId },
secret: fromBase64Url(identity.deviceSecret),
};
}
/**
* A link for a phone that has only scanned a code. The box knows the code by
* the hash of its bytes; the secret itself stays here, mixed into the keys.
*/
export function pairingTarget(relay: string, serverId: string, secret: string): RelayTarget {
let bytes: Uint8Array;
try {
bytes = fromBase64Url(secret);
} catch {
bytes = new Uint8Array(0);
}
if (bytes.length !== 32) throw new Error("This code's secret is not one a Shahi server prints. Print a new code.");
return { relay, serverId, auth: { kind: "pairing", id: toBase64Url(sha256(bytes)) }, secret: bytes };
}
/** A request as `api.ts` builds it, before the transport decides how it travels. */
export interface OutgoingRequest {
method: string;
path: string;
headers: Record<string, string>;
body: Uint8Array | null;
}
/**
* What a request answers with, over either transport. The subset of `Response`
* that `api.ts` reads — the HTTP path hands a real one through, and the relay
* builds this from a `RelayResponse`.
*/
export interface Reply {
ok: boolean;
status: number;
headers: Headers;
json(): Promise<unknown>;
text(): Promise<string>;
bytes(): Promise<Uint8Array>;
}
export type LinkState = "connecting" | "live" | "lost";
/** What the app's socket wants to hear: the same three callbacks it has today. */
export interface LinkSubscriber {
onMessage(msg: SocketMessage): void;
onLink(state: LinkState): void;
/** The box or the relay refused this phone: it is no longer paired. */
onExpired?(): void;
}
const utf8 = { encode: (s: string) => new TextEncoder().encode(s), decode: (b: Uint8Array) => new TextDecoder().decode(b) };
/** How long the box may be silent before the link counts as dead — same as `/ws`. */
const SILENCE_LIMIT_MS = 70_000;
const WATCHDOG_INTERVAL_MS = 10_000;
const CONNECT_TIMEOUT_MS = 15_000;
/**
* How often a socket waiting for its box pings the relay (`docs/relay.md`,
* "Waiting for the box"). Nothing else crosses a waiting socket, and a NAT
* that forgets an idle route would swallow the relay's "box online" close; the
* box keeps its own route open at the same rate. The runtime answers without
* waking the relay's object, and SILENCE_LIMIT_MS without a pong drops it.
*/
const WAITING_PING_MS = 20_000;
/**
* How long a socket must have waited before "box online" redials at once.
* Anything quicker goes through the ordinary backoff, so a relay that holds
* and releases in a loop cannot turn this into a tight one.
*/
const WAITING_GRACE_MS = 1_000;
interface Pending {
id: number;
request: OutgoingRequest;
sent: boolean;
resolve: (reply: Reply) => void;
reject: (error: Error) => void;
timer: ReturnType<typeof setTimeout>;
}
/**
* One link to one box, reconnecting on its own.
*
* Requests are matched to responses by an incrementing id and rejected after
* the caller's timeout. A request is never resent: if the link closes while
* one is in flight it is rejected with why, and the caller — the reader's
* poll, a tap — retries as it would after a dropped HTTP request. Anything
* else would risk delivering a prompt twice.
*/
export class RelayLink {
#ws: WebSocket | undefined;
#self: Ephemeral | null = null;
#session: Session | null = null;
#confirmed = false;
#state: LinkState = "lost";
#pending = new Map<number, Pending>();
#receivedBytes = 0;
#liveSince = 0;
#nextId = 1;
#backoffMs = 500;
#timer: ReturnType<typeof setTimeout> | undefined;
#watchdog: ReturnType<typeof setInterval> | undefined;
#handshakeTimer: ReturnType<typeof setTimeout> | undefined;
#lastMessageAt = 0;
#closed = false;
#sendTimer: ReturnType<typeof setTimeout> | undefined;
#sendTokens = RELAY_LIMITS.phoneBurstBytes - 4096;
#sendRefilled = Date.now();
#watching: string | null = null;
/** Why the link last went down, until it is up again; see `request`'s timer. */
#dropped: Error | null = null;
/** When the relay began holding this socket for an offline box, or 0. */
#waitingSince = 0;
#pingedAt = 0;
#subscribers = new Set<LinkSubscriber>();
readonly host: string;
constructor(readonly target: RelayTarget, private readonly options: { randomBytes?: (length: number) => Uint8Array; retryRandom?: () => number } = {}) {
this.host = hostOf(target.relay);
}
get state(): LinkState {
return this.#state;
}
/** Hears the stream and the link state; told the current state at once. */
subscribe(sub: LinkSubscriber): void {
this.#subscribers.add(sub);
sub.onLink(this.#state);
}
unsubscribe(sub: LinkSubscriber): void {
this.#subscribers.delete(sub);
}
/** Opens the link now if it is not up — for a request, or for coming back to the app. */
ensureConnected(): void {
this.#closed = false;
this.#watchdog ??= setInterval(() => this.#checkAlive(), WATCHDOG_INTERVAL_MS);
if (this.#ws && this.#ws.readyState <= 1) {
if (this.#ws.readyState === 1) this.#checkAlive();
return;
}
if (this.#ws) {
this.#drop(this.#ws, this.#lost());
return;
}
// Polling must respect an already scheduled retry, including while offline.
if (this.#timer) return;
this.#open();
}
/** A real network/foreground transition may bypass the ordinary retry delay. */
reconnect(): void {
if (this.#closed) return;
this.#watchdog ??= setInterval(() => this.#checkAlive(), WATCHDOG_INTERVAL_MS);
if (this.#timer) clearTimeout(this.#timer);
this.#timer = undefined;
this.#backoffMs = 500;
if (this.#ws) this.#drop(this.#ws, this.#lost(), true);
else { this.#retry(true); this.#setState("lost"); }
}
/** Ends the link for good: a sign-out, or a target that changed under it. */
close(): void {
this.#closed = true;
if (this.#timer) clearTimeout(this.#timer);
if (this.#watchdog) clearInterval(this.#watchdog);
this.#timer = undefined;
this.#watchdog = undefined;
const socket = this.#ws;
if (socket) this.#discard(socket);
this.#rejectAll(new UnreachableError("lost", this.host, `The connection through ${this.host} was closed.`));
this.#setState("lost");
}
/** Which pane the box should push frames for, sent now and after every reconnect. */
watch(paneId: string | null): void {
this.#watching = paneId;
if (!this.#session) return;
this.#sendSealed({ t: "ws", data: paneId ? { type: "watch", paneId } : { type: "unwatch" } });
}
/**
* Sends a request and resolves with its answer. Opens the link if it is not
* up; the timeout runs from now, so waiting for the box counts against it
* exactly as connecting does on the HTTP path.
*/
request(request: OutgoingRequest, timeoutMs: number): Promise<Reply> {
// `close()` is for good. Reopening here, as a request once did through
// `ensureConnected()`, let whoever heard the close's "lost" — a dashboard
// refreshing on loss — revive a revoked or signed-out device's link, which
// then redialled the relay forever (pre-release review, 2026-09). Only an
// explicit `ensureConnected()` reopens a closed link.
if (this.#closed) return Promise.reject(new UnreachableError("lost", this.host, `The connection through ${this.host} was closed.`));
if (request.body && request.body.byteLength > RELAY_LIMITS.maxBodyBytes) {
return Promise.reject(new UnreachableError("relay", this.host, `That was too big to send through the relay: one message carries up to ${humanSize(RELAY_LIMITS.maxBodyBytes)}.`));
}
const heldBytes = [...this.#pending.values()].reduce((n, p) => n + (p.request.body?.byteLength ?? 0), 0);
if (this.#pending.size >= RELAY_LIMITS.maxPendingRequests || heldBytes + (request.body?.byteLength ?? 0) > RELAY_LIMITS.maxPendingBodyBytes) {
return Promise.reject(new UnreachableError("relay", this.host, "Too many requests are waiting for this computer. Try again shortly."));
}
return new Promise<Reply>((resolve, reject) => {
const id = this.#nextId++;
const timer = setTimeout(() => {
if (!this.#pending.delete(id)) return;
// "Connected to the relay" only when it was. A request made while a
// backoff retry is scheduled (up to 30 s apart) is never sent and runs
// into this timer, and the pre-release bug hunt found 7 to 9 of 30
// such requests to a computer that was offline saying it "may be busy
// or asleep". Those say why the link is down.
if (!pending.sent && this.#state !== "live") {
reject(this.#dropped ?? new UnreachableError(
"timeout",
this.host,
`Your computer didn't answer within ${Math.round(timeoutMs / 1000)} seconds. Shahi could not reach it through the relay yet. ${this.#next()}`,
));
return;
}
reject(
new UnreachableError(
"timeout",
this.host,
`Your computer didn't answer within ${Math.round(timeoutMs / 1000)} seconds. It is connected to the relay, so it may be busy or asleep.`,
),
);
}, timeoutMs);
const pending: Pending = { id, request, sent: false, resolve, reject, timer };
this.#pending.set(id, pending);
if (this.#session) this.#dispatch(pending);
else this.ensureConnected();
});
}
#open(): void {
if (this.#closed) return;
this.#setState("connecting");
// A paired device asks to be held while its box is away, so it hears the
// moment the box is back instead of on its next retry. A pairing link
// lives for one claim and is closed on its first failure: nothing to wait for.
const wait = this.target.auth.kind === "device" ? `?${RELAY_WAIT_QUERY}` : "";
const url = `${this.target.relay.replace(/^http/, "ws").replace(/\/+$/, "")}/v1/phone/${encodeURIComponent(this.target.serverId)}${wait}`;
let socket: WebSocket;
try { socket = new WebSocket(url); }
catch { this.#dropped = this.#lost(); this.#rejectAll(this.#dropped); this.#retry(); this.#setState("lost"); return; }
// Sealed frames are bytes. Left on the default, React Native hands each
// one over as a Blob that has to be read back asynchronously.
socket.binaryType = "arraybuffer";
this.#ws = socket;
this.#sendTokens = RELAY_LIMITS.phoneBurstBytes - 4096;
this.#sendRefilled = Date.now();
// Includes CONNECTING and the encrypted hello. Neither state is covered
// by the established-session heartbeat, and native may never emit close.
this.#armHandshake(socket);
socket.onopen = () => {
if (this.#ws !== socket || this.#closed) return;
// The backoff is reset on the box's hello, not here: a refused link
// (a ninth phone, a box that is offline) *opens* and then closes with
// a code, and resetting on open made the phone knock every ~0.8s for
// as long as the app was up — ~100k relay requests a day from one
// phone, the free plan's whole quota (measured, 2026-09-02).
this.#lastMessageAt = Date.now();
// A fresh key per connection is what makes a leaked secret useless
// against past sessions; the bytes come from the platform's CSPRNG.
try {
this.#self = ephemeral((this.options.randomBytes ?? secureRandomBytes)(32));
const hello: PhoneHello = { t: "hello", v: RELAY_PROTOCOL, pub: toBase64Url(this.#self.pub), auth: this.target.auth };
// Bytes, not text: the relay forwards data frames and drops text from
// phones, which is relay control. The box answers the same way.
socket.send(utf8.encode(JSON.stringify(hello)));
} catch { this.#drop(socket, this.#lost()); }
};
socket.onmessage = (event: { data: unknown }) => {
if (this.#ws !== socket || this.#closed) return;
this.#lastMessageAt = Date.now();
if (typeof event.data === "string") {
// The relay's only text to a phone: the box is away and this socket
// is held until it is back (`docs/relay.md`, "Waiting for the box").
// Its `pong` and anything else are not ours, beyond proving the route.
if (!this.#session && !this.#waitingSince && this.target.auth.kind === "device" && isWaiting(event.data)) this.#wait();
return;
}
if (!(event.data instanceof ArrayBuffer) || event.data.byteLength > RELAY_LIMITS.maxFrameBytes) {
this.#drop(socket, new UnreachableError("relay", this.host, "The relay sent an invalid or oversized frame."));
return;
}
if (!this.#session) {
// The first data frame is the box's hello, in the clear.
try {
if (event.data.byteLength > 4096) throw new Error("oversized hello");
this.#onHello(utf8.decode(new Uint8Array(event.data)));
} catch {
this.#drop(socket, new UnreachableError("relay", this.host, "The secure relay handshake was invalid."));
}
return;
}
let plain: Uint8Array;
try {
plain = open(this.#session, new Uint8Array(event.data as ArrayBuffer));
} catch {
// A box replacement can race a late frame from the old encrypted
// session. That says this connection is stale, not that the durable
// device credential was revoked. Only an authenticated sealed `bye`
// below is authority to erase a saved pairing.
this.#drop(socket, new UnreachableError("lost", this.host, `The secure connection through ${this.host} changed. ${this.#next()}`));
return;
}
this.#confirmed = true;
clearTimeout(this.#handshakeTimer); this.#handshakeTimer = undefined;
this.#receivedBytes += event.data.byteLength;
this.#receive(plain);
if (this.#receivedBytes >= RELAY_LIMITS.acknowledgeBytes && this.#session) {
const bytes = this.#receivedBytes;
this.#receivedBytes = 0;
this.#sendSealed({ t: "ack", bytes });
}
};
socket.onclose = (event: { code?: number; reason?: string }) => {
if (this.#ws !== socket) return;
const code = event?.code ?? 0;
const reason = event?.reason ?? "";
if (code === RELAY_CLOSE.boxOnline && this.#waitingSince) {
// The box is back. Requests made while waiting were never sent, so
// they stay to go out on the new link rather than failing.
const waited = Date.now() - this.#waitingSince;
this.#discard(socket);
if (waited >= WAITING_GRACE_MS) this.#open();
else this.#retry();
return;
}
if (
this.target.auth.kind === "pairing" &&
(code === RELAY_CLOSE.unauthorized || code === RELAY_CLOSE.forbidden ||
(!this.#confirmed && code === 1000 && reason === "closed by box"))
) {
// A one-time pairing link being refused is terminal: there is no
// saved identity yet, and retrying a spent/bad code cannot succeed.
this.#refuse("This pairing code could not be used. It may have expired or already been used. Show a new code on your computer and try again.");
return;
}
if (code === RELAY_CLOSE.unauthorized || code === RELAY_CLOSE.forbidden) {
// A transport close is not authenticated. During a sidecar restart a
// stale/new link race can produce either code, so retain the saved
// device and reconnect. Revocation travels as a sealed `bye` instead.
this.#drop(socket,
new UnreachableError("lost", this.host, `The computer rejected this connection through ${this.host}. Reconnecting…`),
);
return;
}
this.#drop(socket, closeError(code, reason, this.host));
};
socket.onerror = () => this.#drop(socket, this.#lost());
}
#onHello(text: string): void {
// Only a stable connection resets backoff; brief hello/close loops do not.
let hello: Partial<BoxHello> = {};
try {
hello = JSON.parse(text) as BoxHello;
} catch {
// Fall through: an unparseable hello is handled as a wrong one.
}
const pub = typeof hello.pub === "string" ? fromBase64Url(hello.pub) : new Uint8Array(0);
if (hello.t !== "hello" || hello.v !== RELAY_PROTOCOL || pub.length !== 32 || !this.#self) {
const version = typeof hello.v === "number" ? `protocol v${hello.v}` : "something this app does not recognise";
this.#rejectAll(
new UnreachableError(
"unknown",
this.host,
`The computer answered the relay with ${version}; this app speaks v${RELAY_PROTOCOL}. Update the app or Shahi on that computer.`,
),
);
this.close();
return;
}
this.#session = clientSession(this.#self, pub, this.target.secret);
// Waiting for the computer turned the deadline off. A hello alone proves
// nothing, so one arriving after that puts it back: otherwise a relay could
// hold the link on LIVE for as long as it liked, with no computer behind it
// (state-machine review, November 2026).
if (this.#ws && this.#handshakeTimer === undefined) this.#armHandshake(this.#ws);
this.#waitingSince = 0;
this.#receivedBytes = 0;
this.#liveSince = Date.now();
this.#self = null;
this.#dropped = null;
// A device must prove its secret even when it only wants the dashboard.
// A hello alone must never start a session or keep a phone slot alive.
if (this.target.auth.kind === "device" && !this.#sendSealed({ t: "ws", data: this.#watching ? { type: "watch", paneId: this.#watching } : { type: "unwatch" } })) return;
this.#setState("live");
for (const pending of this.#pending.values()) if (!pending.sent) this.#dispatch(pending);
}
#receive(plain: Uint8Array): void {
let msg: BoxToPhone;
try {
msg = JSON.parse(utf8.decode(plain)) as BoxToPhone;
} catch {
return; // A malformed frame is not worth tearing the link down for.
}
if (!msg || typeof msg !== "object") return;
if (msg.t === "res") {
const pending = this.#pending.get(msg.id);
if (!pending) return; // Answered after its timeout: nobody is waiting.
this.#refillSendTokens();
this.#pending.delete(msg.id);
clearTimeout(pending.timer);
try {
if (!Number.isInteger(msg.status) || msg.status < 100 || msg.status > 599 ||
(msg.body !== null && typeof msg.body !== "string") ||
!msg.headers || typeof msg.headers !== "object" || Array.isArray(msg.headers)) throw new Error("invalid response");
pending.resolve(reply(msg));
} catch {
pending.reject(new UnreachableError("relay", this.host, "The computer sent an invalid response."));
}
} else if (msg.t === "ws") {
if (!msg.data || typeof msg.data !== "object" || typeof msg.data.type !== "string") return;
if (msg.data.type === "ping") {
// A quiet dashboard may never reach the bulk ACK threshold. Answer
// heartbeats too, so a dead phone cannot retain a relay slot forever.
const bytes = this.#receivedBytes; this.#receivedBytes = 0;
if (bytes) this.#sendSealed({ t: "ack", bytes });
return;
}
this.#subscribers.forEach((s) => s.onMessage(msg.data as SocketMessage));
} else if (msg.t === "bye") {
// The box is ending this link because our session is gone — revoked in
// Settings, or expired. The relay could only close us with 1000, which
// we would retry; this sealed signal is how that reaches the app,
// exactly as a `/ws` close with 4001 does on a direct connection. Sign
// out and stop, rather than reconnecting into a refusal forever.
this.#refuse("This phone is no longer paired with that computer.");
}
}
#dispatch(pending: Pending): void {
const { request } = pending;
const message: PhoneToBox = {
t: "req",
id: pending.id,
method: request.method,
path: request.path,
headers: request.headers,
body: request.body ? toBase64Url(request.body) : null,
};
// A pair of valid attachments can exhaust the relay's one-MiB burst.
// Wait before sealing (which advances the nonce), never resend a write.
const frameBytes = utf8.encode(JSON.stringify(message)).byteLength + 24;
this.#refillSendTokens();
// Leave room for chat and control traffic while a file chunk waits.
const bulk = request.path.endsWith("/chunk") && request.path.startsWith("/api/uploads/transfers/");
const missing = frameBytes + (bulk ? 16 * 1024 : 1024) - this.#sendTokens;
if (missing > 0 && frameBytes <= RELAY_LIMITS.maxFrameBytes - 1024) {
if (!this.#sendTimer) this.#sendTimer = setTimeout(() => {
this.#sendTimer = undefined;
if (!this.#session) return;
for (const held of this.#pending.values()) if (!held.sent) this.#dispatch(held);
}, Math.ceil(missing / (RELAY_LIMITS.phoneBytesPerSecond * 0.9) * 1000) + 50);
return;
}
pending.sent = this.#sendSealed(message);
}
#refillSendTokens(): void {
const now = Date.now();
// A little headroom absorbs clock/network jitter and keeps control traffic responsive.
// The relay may receive a slow upload much later than socket.send(). Do
// not bank that transit time: the next upload could otherwise arrive in
// the same burst even though our local clock says capacity has recovered.
const uploading = [...this.#pending.values()].some(p => p.sent && (p.request.path === "/api/uploads" || p.request.path.startsWith("/api/uploads/transfers/") && p.request.path.endsWith("/chunk")));
if (!uploading) this.#sendTokens = Math.min(RELAY_LIMITS.phoneBurstBytes,
this.#sendTokens + Math.max(0, now - this.#sendRefilled) * RELAY_LIMITS.phoneBytesPerSecond * 0.9 / 1000);
this.#sendRefilled = now;
}
#sendSealed(msg: PhoneToBox): boolean {
const socket = this.#ws;
if (!this.#session || !socket) return false;
if (socket.readyState !== 1) { this.#drop(socket, this.#lost()); return false; }
// `seal` returns a fresh, exact-length array, so a view is the frame:
// React Native's `send` takes typed arrays as binary.
try {
const frame = seal(this.#session, utf8.encode(JSON.stringify(msg)));
if (frame.byteLength > RELAY_LIMITS.maxFrameBytes) {
this.#drop(socket, new UnreachableError("relay", this.host, "That message is too large for the relay."));
return false;
}
if (socket.bufferedAmount + frame.byteLength > RELAY_LIMITS.maxSocketBufferedBytes) {
this.#drop(socket, new UnreachableError("relay", this.host, `This connection is too slow. ${this.#next()}`));
return false;
}
this.#refillSendTokens();
this.#sendTokens -= frame.byteLength;
socket.send(new Uint8Array(frame));
return true;
} catch {
// Native WebSocket state can change between the readyState read and
// send while a relay connection is dropping. React Native throws
// INVALID_STATE_ERR in that race; let the close/retry path recover
// instead of taking down the React error boundary.
this.#drop(socket, this.#lost());
return false;
}
}
/** Not paired any more: stop for good and tell the app to sign out. */
#refuse(words: string): void {
this.#rejectAll(new UnauthorizedError(words));
this.close();
this.#subscribers.forEach((s) => s.onExpired?.());
}
#rejectAll(error: Error): void {
for (const pending of this.#pending.values()) {
clearTimeout(pending.timer);
pending.reject(error);
}
this.#pending.clear();
}
#setState(state: LinkState): void {
if (this.#state === state) return;
this.#state = state;
this.#subscribers.forEach((s) => s.onLink(state));
}
/**
* The relay is holding this socket until the box is back. Said now as the
* refusal it replaces, so the person sees "offline" and waiting requests
* fail with why, as a 4404 did; but nothing is scheduled, and the
* handshake deadline is off, since the box hello it waited for cannot come.
*/
#wait(): void {
this.#waitingSince = this.#pingedAt = Date.now();
clearTimeout(this.#handshakeTimer); this.#handshakeTimer = undefined;
this.#dropped = closeError(RELAY_CLOSE.boxOffline, "box offline", this.host);
this.#rejectAll(this.#dropped);
this.#setState("lost");
}
#checkAlive(): void {
if (this.#closed || !this.#ws) return;
if (this.#ws.readyState > 1) { this.#drop(this.#ws, this.#lost()); return; }
if (this.#waitingSince) {
const now = Date.now();
if (now - this.#lastMessageAt >= SILENCE_LIMIT_MS) { this.#drop(this.#ws, this.#lost()); return; }
if (now - this.#pingedAt >= WAITING_PING_MS) {
this.#pingedAt = now;
try { this.#ws.send("ping"); } catch { this.#drop(this.#ws, this.#lost()); }
}
return;
}
if (!this.#session) return;
if (Date.now() - this.#lastMessageAt < SILENCE_LIMIT_MS) return;
this.#drop(this.#ws, this.#lost());
}
#lost(): Error { return new UnreachableError("lost", this.host, `The connection through ${this.host} dropped. ${this.#next()}`); }
/**
* What a failed request says comes next. A pairing link lives for one claim,
* and whoever holds it closes it on the first failure, so it never
* reconnects: "Reconnecting…" under a pairing code that could not reach its
* relay was a promise nothing kept (pre-release review, September 2026).
*/
#next(): string { return this.target.auth.kind === "pairing" ? "Try again." : "Reconnecting…"; }
#armHandshake(socket: WebSocket): void {
this.#handshakeTimer = setTimeout(() => {
if (this.#ws === socket && !this.#confirmed) this.#drop(socket, this.#lost());
}, CONNECT_TIMEOUT_MS);
}
#discard(socket: WebSocket): void {
clearTimeout(this.#sendTimer); this.#sendTimer = undefined;
this.#ws = undefined; this.#session = null; this.#self = null; this.#confirmed = false; this.#waitingSince = 0;
clearTimeout(this.#handshakeTimer); this.#handshakeTimer = undefined;
socket.onopen = null; socket.onmessage = null; socket.onclose = null; socket.onerror = null;
try { socket.close(); } catch { /* Local recovery cannot depend on native close completing. */ }
}
#drop(socket: WebSocket, error: Error, immediate = false): void {
if (this.#ws !== socket) return;
if (this.#liveSince && Date.now() - this.#liveSince >= 30_000) this.#backoffMs = 500;
this.#liveSince = 0;
this.#discard(socket);
this.#dropped = error;
this.#rejectAll(error);
// Schedule before notifying: subscribers may immediately request a refresh.
this.#retry(immediate);
this.#setState("lost");
}
#retry(immediate = false): void {
if (this.#closed || this.#timer) return;
const delay = immediate ? 0 : retryDelay(this.#backoffMs, this.options.retryRandom);
// Capped at half a minute, like the box's own dial: a phone that keeps
// being refused is waiting for its box, and every attempt is a relay
// request the box's owner pays for.
this.#backoffMs = Math.min(this.#backoffMs * 2, 30_000);
this.#timer = setTimeout(() => {
this.#timer = undefined;
this.#open();
}, delay);
}
}
/**
* The relay's close codes, in words for the person holding the phone.
*
* 4429 covers three different refusals and the reason string tells them
* apart; before it was read, a photo too big for one frame was reported as
* "the relay is throttling this phone", and retrying failed the same way.
*/
function closeError(code: number, reason: string, host: string): UnreachableError {
if (code === RELAY_CLOSE.boxOffline) {
return new UnreachableError("box", host, "Your computer is offline — its Shahi service is not connected to the relay.");
}
if (code === RELAY_CLOSE.quota) {
if (reason === "frame too large") {
return new UnreachableError("relay", host, `That was too big to send through the relay: one message carries up to ${humanSize(RELAY_LIMITS.maxBodyBytes)}.`);
}
if (reason === "too many phones") {
return new UnreachableError("relay", host, "Too many connections are still open to this computer. Shahi will retry automatically.");
}
return new UnreachableError("relay", host, "The relay is throttling this phone. Wait a moment, then try again.");
}
return new UnreachableError("lost", host, `The connection through ${host} dropped. Try again.`);
}
function isWaiting(text: string): boolean {
try { return (JSON.parse(text) as Partial<RelayToPhone> | null)?.t === "waiting"; } catch { return false; }
}
/** Bytes as a person reads them: KB under a megabyte, so a cap of 783,360 is "765 KB" and not "0.7 MB". */
export function humanSize(bytes: number): string {
return bytes < 1024 * 1024 ? `${Math.round(bytes / 1024)} KB` : `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}
function reply(res: RelayResponse): Reply {
const bytes = res.body ? fromBase64Url(res.body) : new Uint8Array(0);
return {
ok: res.status >= 200 && res.status < 300,
status: res.status,
headers: new Headers(res.headers),
bytes: async () => bytes,
text: async () => utf8.decode(bytes),
json: async () => JSON.parse(utf8.decode(bytes)) as unknown,
};
}
/* ------------------------------------------------------------- base64url */
// Written out rather than borrowed from `atob`/`btoa`: Hermes has neither
// without a polyfill, and the frames are bytes either way.
const ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
const VALUES = new Int8Array(128).fill(-1);
for (let i = 0; i < ALPHABET.length; i++) VALUES[ALPHABET.charCodeAt(i)] = i;
VALUES["+".charCodeAt(0)] = 62;
VALUES["/".charCodeAt(0)] = 63;
export function toBase64Url(bytes: Uint8Array): string {
const parts: string[] = [];
// Chunked so a photo does not become a million one-character concatenations.
for (let start = 0; start < bytes.length; start += 3 * 1024) {
const codes: number[] = [];
const end = Math.min(bytes.length, start + 3 * 1024);
for (let i = start; i < end; i += 3) {
const a = bytes[i]!;
const b = bytes[i + 1];
const c = bytes[i + 2];
const triple = (a << 16) | ((b ?? 0) << 8) | (c ?? 0);
codes.push(ALPHABET.charCodeAt((triple >> 18) & 63), ALPHABET.charCodeAt((triple >> 12) & 63));
if (b !== undefined) codes.push(ALPHABET.charCodeAt((triple >> 6) & 63));
if (c !== undefined) codes.push(ALPHABET.charCodeAt(triple & 63));
}
parts.push(String.fromCharCode(...codes));
}
return parts.join("");
}
/** Accepts the standard alphabet and padding too, since a body may come from anywhere. */
export function fromBase64Url(text: string): Uint8Array {
if (!/^[A-Za-z0-9_+/-]*={0,2}$/.test(text)) throw new Error("relay: not base64url");
const clean = text.replace(/=+$/, "");
if (clean.length % 4 === 1 || (text.includes("=") && text.length % 4 !== 0)) throw new Error("relay: not base64url");
const out = new Uint8Array(Math.floor((clean.length * 3) / 4));
let acc = 0;
let bits = 0;
let at = 0;
for (let i = 0; i < clean.length; i++) {
const code = clean.charCodeAt(i);
const value = code < 128 ? VALUES[code]! : -1;
if (value < 0) throw new Error("relay: not base64url");
acc = (acc << 6) | value;
bits += 6;
if (bits >= 8) {
bits -= 8;
out[at++] = (acc >> bits) & 0xff;
}
}
if (bits && (acc & ((1 << bits) - 1)) !== 0) throw new Error("relay: not base64url");
return out;
}
function secureRandomBytes(length: number): Uint8Array {
return globalThis.crypto.getRandomValues(new Uint8Array(length));
}