@celljs/ta-relay 4.9.1 → 4.10.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.
@@ -0,0 +1,130 @@
1
+ import { RelayBytes } from '../common/relay-bytes.js';
2
+ import { RELAY_FILE_CHUNK_BYTES, RELAY_FILE_MAX_BYTES, RELAY_FILES_MAX_PARALLEL, RELAY_FILES_PREFIX, RELAY_FILES_TIMEOUT_MS, } from '../common/relay-files.js';
3
+ /**
4
+ * The files of the workspace on a channel of a device (RelayFiles): asks them of the ta serve of this
5
+ * machine and sends them back sealed. It asks for nothing but a path of {@link RELAY_FILES_PREFIX}, with
6
+ * GET, at the loopback port of ta serve and never at another host: a device has the whole of ta serve
7
+ * already, and what a device cannot be trusted with is not this.
8
+ */
9
+ export class RelayFilesHost {
10
+ constructor(options) {
11
+ this.options = options;
12
+ this.reading = 0;
13
+ this.running = new Set();
14
+ // Only ever loopback, whatever the address of the socket says: what this reaches is one server.
15
+ this.base = `http://127.0.0.1:${new URL(options.localUrl).port}`;
16
+ }
17
+ /** A frame of the device: a file to read, or something of a version it does not know. */
18
+ async receive(text) {
19
+ let frame;
20
+ try {
21
+ frame = JSON.parse(text);
22
+ }
23
+ catch (_a) {
24
+ return;
25
+ }
26
+ if (!frame || typeof frame !== 'object' || frame.t !== 'get' || typeof frame.url !== 'string' || typeof frame.id !== 'number') {
27
+ return;
28
+ }
29
+ await this.read(frame.id, frame.url);
30
+ }
31
+ /** The device went: what it asked for is not read to the end. */
32
+ close() {
33
+ for (const controller of this.running) {
34
+ controller.abort();
35
+ }
36
+ this.running.clear();
37
+ }
38
+ /** What to fetch for what a device asked, or undefined: a path of {@link RELAY_FILES_PREFIX} of this ta serve alone. */
39
+ static target(base, url) {
40
+ let parsed;
41
+ try {
42
+ // Parsed, never taken as text: a device that sends another host or a path out of the prefix is refused.
43
+ parsed = new URL(url, base);
44
+ }
45
+ catch (_a) {
46
+ return undefined;
47
+ }
48
+ if (parsed.origin !== base || !parsed.pathname.startsWith(RELAY_FILES_PREFIX)) {
49
+ return undefined;
50
+ }
51
+ return base + parsed.pathname + parsed.search;
52
+ }
53
+ async read(id, url) {
54
+ var _a, _b;
55
+ const target = RelayFilesHost.target(this.base, url);
56
+ if (!target) {
57
+ this.options.send({ t: 'error', id, message: 'Ta reads only the files it offers by URL.' });
58
+ return;
59
+ }
60
+ if (this.reading >= RELAY_FILES_MAX_PARALLEL) {
61
+ this.options.send({ t: 'error', id, message: 'Too many files are loading at once.' });
62
+ return;
63
+ }
64
+ const controller = new AbortController();
65
+ this.running.add(controller);
66
+ this.reading++;
67
+ const timer = setTimeout(() => controller.abort(), RELAY_FILES_TIMEOUT_MS);
68
+ try {
69
+ const response = await fetch(target, { signal: controller.signal, redirect: 'error', headers: { accept: '*/*' } });
70
+ if (!response.ok) {
71
+ this.options.send({ t: 'error', id, message: `The file is not there (${response.status}).` });
72
+ return;
73
+ }
74
+ const length = Number((_a = response.headers.get('content-length')) !== null && _a !== void 0 ? _a : 0);
75
+ if (length > RELAY_FILE_MAX_BYTES) {
76
+ this.options.send({ t: 'error', id, message: 'The file is too large to show here.' });
77
+ return;
78
+ }
79
+ this.options.send({
80
+ t: 'head',
81
+ id,
82
+ status: response.status,
83
+ type: (_b = response.headers.get('content-type')) !== null && _b !== void 0 ? _b : 'application/octet-stream',
84
+ size: length || undefined,
85
+ });
86
+ if (!response.body) {
87
+ this.options.send({ t: 'end', id });
88
+ return;
89
+ }
90
+ let sent = 0;
91
+ let parts = [];
92
+ let held = 0;
93
+ const flush = () => {
94
+ if (held > 0) {
95
+ this.options.send({ t: 'chunk', id, d: RelayBytes.toBase64(RelayBytes.concat(...parts)) });
96
+ parts = [];
97
+ held = 0;
98
+ }
99
+ };
100
+ for await (const piece of response.body) {
101
+ const chunk = piece;
102
+ sent += chunk.length;
103
+ if (sent > RELAY_FILE_MAX_BYTES) {
104
+ this.options.send({ t: 'error', id, message: 'The file is too large to show here.' });
105
+ return;
106
+ }
107
+ parts.push(chunk);
108
+ held += chunk.length;
109
+ if (held >= RELAY_FILE_CHUNK_BYTES) {
110
+ flush();
111
+ }
112
+ }
113
+ flush();
114
+ this.options.send({ t: 'end', id });
115
+ }
116
+ catch (error) {
117
+ this.options.send({
118
+ t: 'error',
119
+ id,
120
+ message: controller.signal.aborted ? 'The file took too long to come.' : `The file could not be read: ${error.message}`,
121
+ });
122
+ }
123
+ finally {
124
+ clearTimeout(timer);
125
+ this.running.delete(controller);
126
+ this.reading--;
127
+ }
128
+ }
129
+ }
130
+ //# sourceMappingURL=relay-files-host.js.map
@@ -1,5 +1,7 @@
1
1
  import { RelayCipher } from '../common/relay-cipher.js';
2
+ import { type RelayFilesFrame } from '../common/relay-files.js';
2
3
  import { RELAY_PING, type RelayHostMessage, type RelayWelcome } from '../common/relay-protocol.js';
4
+ import { RelayFilesHost } from './relay-files-host.js';
3
5
  import type { RelayShare, RelayStore } from './relay-store.js';
4
6
  export interface RelayHostOptions {
5
7
  store: RelayStore;
@@ -12,19 +14,28 @@ export interface RelayHostOptions {
12
14
  /** The bytes of a package handed on (a share of the store), for one who has its link; without it, shares are refused. */
13
15
  sharePackage?(share: RelayShare): Promise<Uint8Array>;
14
16
  }
15
- /** A device on a channel: the handshake, then its connection to ta serve. */
17
+ /**
18
+ * A device on a channel: the handshake, then its connection to ta serve — or, for a channel that asked
19
+ * for files (RelayFiles, RelayFilesHost), the file it asks for. What a channel is, it says in its first
20
+ * sealed message; until then nothing of ta serve is opened for it.
21
+ */
16
22
  declare class RelayChannel {
17
23
  readonly id: string;
18
24
  protected readonly host: RelayHost;
19
25
  protected readonly options: RelayHostOptions;
20
26
  protected cipher?: RelayCipher;
21
27
  protected local?: WebSocket;
28
+ /** What this channel does, once its first sealed message said it. */
29
+ protected mode?: 'rpc' | 'files';
30
+ protected files?: RelayFilesHost;
22
31
  protected readonly early: string[];
23
32
  protected readonly timer: NodeJS.Timeout;
24
33
  constructor(id: string, host: RelayHost, options: RelayHostOptions);
25
34
  receive(text: string): Promise<void>;
26
35
  /** From the relay: the device is gone. */
27
36
  closed(): void;
37
+ /** A frame of the files of the workspace, sealed as the rest. */
38
+ protected sendFrame(frame: RelayFilesFrame): void;
28
39
  protected hello(text: string): Promise<void>;
29
40
  /** A package handed on: what it is, its bytes in chunks, the end; then the channel closes. It never reaches ta serve's socket. */
30
41
  protected sendShare(cipher: RelayCipher, share: RelayShare): Promise<void>;
@@ -1,10 +1,16 @@
1
1
  import { RelayBytes } from '../common/relay-bytes.js';
2
- import { RelayCipher } from '../common/relay-cipher.js';
2
+ import { RelayCipher, RelayDeflate } from '../common/relay-cipher.js';
3
+ import { RelayFilesUtil } from '../common/relay-files.js';
3
4
  import { RelayPairingUtil } from '../common/relay-pairing.js';
4
5
  import { RELAY_HOST_PATH, RELAY_PING, RELAY_PING_MS, RELAY_PONG, RELAY_PROOF, RELAY_SHARE_CHUNK_BYTES, RELAY_SHARE_MAX_BYTES, RelayCloseCode, } from '../common/relay-protocol.js';
6
+ import { RelayFilesHost } from './relay-files-host.js';
5
7
  const MAX_RECONNECT_DELAY_MS = 30000;
6
8
  const HELLO_TIMEOUT_MS = 10000;
7
- /** A device on a channel: the handshake, then its connection to ta serve. */
9
+ /**
10
+ * A device on a channel: the handshake, then its connection to ta serve — or, for a channel that asked
11
+ * for files (RelayFiles, RelayFilesHost), the file it asks for. What a channel is, it says in its first
12
+ * sealed message; until then nothing of ta serve is opened for it.
13
+ */
8
14
  class RelayChannel {
9
15
  constructor(id, host, options) {
10
16
  this.id = id;
@@ -14,7 +20,7 @@ class RelayChannel {
14
20
  this.timer = setTimeout(() => this.close(), HELLO_TIMEOUT_MS);
15
21
  }
16
22
  async receive(text) {
17
- var _a;
23
+ var _a, _b;
18
24
  if (!this.cipher) {
19
25
  await this.hello(text);
20
26
  return;
@@ -23,11 +29,24 @@ class RelayChannel {
23
29
  try {
24
30
  data = await this.cipher.open(text);
25
31
  }
26
- catch (_b) {
32
+ catch (_c) {
27
33
  this.close();
28
34
  return;
29
35
  }
30
- if (((_a = this.local) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
36
+ if (!this.mode) {
37
+ this.mode = RelayFilesUtil.isHello(data) ? 'files' : 'rpc';
38
+ if (this.mode === 'files') {
39
+ this.files = new RelayFilesHost({ localUrl: this.options.localUrl, send: frame => this.sendFrame(frame) });
40
+ }
41
+ else {
42
+ this.connectLocal(this.cipher);
43
+ }
44
+ }
45
+ if (this.mode === 'files') {
46
+ await ((_a = this.files) === null || _a === void 0 ? void 0 : _a.receive(data));
47
+ return;
48
+ }
49
+ if (((_b = this.local) === null || _b === void 0 ? void 0 : _b.readyState) === WebSocket.OPEN) {
31
50
  this.local.send(data);
32
51
  }
33
52
  else {
@@ -36,9 +55,15 @@ class RelayChannel {
36
55
  }
37
56
  /** From the relay: the device is gone. */
38
57
  closed() {
39
- var _a;
58
+ var _a, _b;
40
59
  clearTimeout(this.timer);
41
- (_a = this.local) === null || _a === void 0 ? void 0 : _a.close();
60
+ (_a = this.files) === null || _a === void 0 ? void 0 : _a.close();
61
+ (_b = this.local) === null || _b === void 0 ? void 0 : _b.close();
62
+ }
63
+ /** A frame of the files of the workspace, sealed as the rest. */
64
+ sendFrame(frame) {
65
+ var _a;
66
+ (_a = this.cipher) === null || _a === void 0 ? void 0 : _a.seal(JSON.stringify(frame)).then(sealed => this.host.send({ t: 'data', ch: this.id, d: sealed }));
42
67
  }
43
68
  async hello(text) {
44
69
  var _a;
@@ -63,13 +88,14 @@ class RelayChannel {
63
88
  const nonce = RelayBytes.random(16);
64
89
  const cipher = await RelayCipher.derive(RelayBytes.fromBase64(key), RelayBytes.fromBase64(hello.nonce), nonce, 'host');
65
90
  this.cipher = cipher;
66
- this.answer({ t: 'ready', nonce: RelayBytes.toBase64(nonce), proof: await cipher.seal(RELAY_PROOF) });
91
+ const compress = hello.z === 1 && RelayDeflate.supported();
92
+ const proof = await cipher.seal(RELAY_PROOF);
93
+ // The messages after the proof, both ways: the device sets it once it opened the proof.
94
+ cipher.compress = compress;
95
+ this.answer({ t: 'ready', nonce: RelayBytes.toBase64(nonce), proof, ...(compress ? { z: 1 } : {}) });
67
96
  if (share) {
68
97
  await this.sendShare(cipher, share);
69
98
  }
70
- else {
71
- this.connectLocal(cipher);
72
- }
73
99
  }
74
100
  /** A package handed on: what it is, its bytes in chunks, the end; then the channel closes. It never reaches ta serve's socket. */
75
101
  async sendShare(cipher, share) {
@@ -1,6 +1,7 @@
1
1
  export * from './qr-code';
2
2
  export * from './relay-bytes';
3
3
  export * from './relay-cipher';
4
+ export * from './relay-files';
4
5
  export * from './relay-pairing';
5
6
  export * from './relay-protocol';
6
7
  export * from './relay-room';
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./qr-code"), exports);
18
18
  __exportStar(require("./relay-bytes"), exports);
19
19
  __exportStar(require("./relay-cipher"), exports);
20
+ __exportStar(require("./relay-files"), exports);
20
21
  __exportStar(require("./relay-pairing"), exports);
21
22
  __exportStar(require("./relay-protocol"), exports);
22
23
  __exportStar(require("./relay-room"), exports);
@@ -4,10 +4,16 @@ export type RelaySide = 'client' | 'host';
4
4
  * each direction, derived (HKDF-SHA-256) from the device's key and a nonce of each side, so that every
5
5
  * connection has keys of its own. The IV is the number of the message, which must arrive in order: a
6
6
  * message replayed, dropped or reordered by the relay fails to open.
7
+ *
8
+ * Encrypted bytes do not compress, so a message is compressed before it is sealed, once both sides said
9
+ * they can (`RelayHello.z`, `RelayWelcome.z`, then `compress`): JSON shrinks to a third or less, which
10
+ * is what a phone on a slow network waits for.
7
11
  */
8
12
  export declare class RelayCipher {
9
13
  protected readonly sendKey: CryptoKey;
10
14
  protected readonly receiveKey: CryptoKey;
15
+ /** Messages sealed from now are compressed when large, and those opened may be: both sides set it at the same message. */
16
+ compress: boolean;
11
17
  protected sent: number;
12
18
  protected received: number;
13
19
  protected sealing: Promise<unknown>;
@@ -18,6 +24,15 @@ export declare class RelayCipher {
18
24
  seal(text: string): Promise<string>;
19
25
  /** Rejects what this side's peer did not seal, or not as the next message. */
20
26
  open(frame: string): Promise<string>;
27
+ /** The bytes of a message to seal: as they are until compression is agreed, then behind the byte that says how. */
28
+ protected pack(text: string): Promise<Uint8Array>;
29
+ protected unpack(bytes: Uint8Array): Promise<string>;
21
30
  protected static iv(counter: number): Uint8Array;
22
31
  }
32
+ /** deflate-raw of the platform (CompressionStream), where it has it: browsers, Node.js 21.2 and later, Bun, Deno. */
33
+ export declare namespace RelayDeflate {
34
+ function supported(): boolean;
35
+ function compress(bytes: Uint8Array): Promise<Uint8Array>;
36
+ function decompress(bytes: Uint8Array): Promise<Uint8Array>;
37
+ }
23
38
  //# sourceMappingURL=relay-cipher.d.ts.map
@@ -1,20 +1,31 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.RelayCipher = void 0;
3
+ exports.RelayDeflate = exports.RelayCipher = void 0;
4
4
  const relay_bytes_1 = require("./relay-bytes");
5
5
  const encoder = new TextEncoder();
6
6
  const decoder = new TextDecoder();
7
7
  const IV_LENGTH = 12;
8
+ /** The first byte of a message once compression is agreed: what follows. */
9
+ const PLAIN = 0;
10
+ const DEFLATED = 1;
11
+ /** Smaller messages go as they are: they are about as small compressed. */
12
+ const DEFLATE_MIN_BYTES = 1024;
8
13
  /**
9
14
  * The encryption of one connection between a paired device and ta serve: AES-256-GCM with a key for
10
15
  * each direction, derived (HKDF-SHA-256) from the device's key and a nonce of each side, so that every
11
16
  * connection has keys of its own. The IV is the number of the message, which must arrive in order: a
12
17
  * message replayed, dropped or reordered by the relay fails to open.
18
+ *
19
+ * Encrypted bytes do not compress, so a message is compressed before it is sealed, once both sides said
20
+ * they can (`RelayHello.z`, `RelayWelcome.z`, then `compress`): JSON shrinks to a third or less, which
21
+ * is what a phone on a slow network waits for.
13
22
  */
14
23
  class RelayCipher {
15
24
  constructor(sendKey, receiveKey) {
16
25
  this.sendKey = sendKey;
17
26
  this.receiveKey = receiveKey;
27
+ /** Messages sealed from now are compressed when large, and those opened may be: both sides set it at the same message. */
28
+ this.compress = false;
18
29
  this.sent = 0;
19
30
  this.received = 0;
20
31
  this.sealing = Promise.resolve();
@@ -32,7 +43,7 @@ class RelayCipher {
32
43
  seal(text) {
33
44
  const iv = RelayCipher.iv(this.sent++);
34
45
  const sealed = this.sealing.then(async () => {
35
- const data = await globalThis.crypto.subtle.encrypt({ name: 'AES-GCM', iv: iv }, this.sendKey, encoder.encode(text));
46
+ const data = await globalThis.crypto.subtle.encrypt({ name: 'AES-GCM', iv: iv }, this.sendKey, (await this.pack(text)));
36
47
  return relay_bytes_1.RelayBytes.toBase64(new Uint8Array(data));
37
48
  });
38
49
  this.sealing = sealed.catch(() => undefined);
@@ -43,11 +54,29 @@ class RelayCipher {
43
54
  const iv = RelayCipher.iv(this.received++);
44
55
  const opened = this.opening.then(async () => {
45
56
  const data = await globalThis.crypto.subtle.decrypt({ name: 'AES-GCM', iv: iv }, this.receiveKey, relay_bytes_1.RelayBytes.fromBase64(frame));
46
- return decoder.decode(data);
57
+ return this.unpack(new Uint8Array(data));
47
58
  });
48
59
  this.opening = opened.catch(() => undefined);
49
60
  return opened;
50
61
  }
62
+ /** The bytes of a message to seal: as they are until compression is agreed, then behind the byte that says how. */
63
+ async pack(text) {
64
+ const bytes = encoder.encode(text);
65
+ if (!this.compress) {
66
+ return bytes;
67
+ }
68
+ const deflated = bytes.length >= DEFLATE_MIN_BYTES ? await RelayDeflate.compress(bytes) : undefined;
69
+ return deflated && deflated.length < bytes.length
70
+ ? relay_bytes_1.RelayBytes.concat(Uint8Array.of(DEFLATED), deflated)
71
+ : relay_bytes_1.RelayBytes.concat(Uint8Array.of(PLAIN), bytes);
72
+ }
73
+ async unpack(bytes) {
74
+ if (!this.compress) {
75
+ return decoder.decode(bytes);
76
+ }
77
+ const body = bytes.subarray(1);
78
+ return decoder.decode(bytes[0] === DEFLATED ? await RelayDeflate.decompress(body) : body);
79
+ }
51
80
  static iv(counter) {
52
81
  const iv = new Uint8Array(IV_LENGTH);
53
82
  new DataView(iv.buffer).setUint32(IV_LENGTH - 8, Math.floor(counter / 2 ** 32));
@@ -56,4 +85,35 @@ class RelayCipher {
56
85
  }
57
86
  }
58
87
  exports.RelayCipher = RelayCipher;
88
+ /** deflate-raw of the platform (CompressionStream), where it has it: browsers, Node.js 21.2 and later, Bun, Deno. */
89
+ var RelayDeflate;
90
+ (function (RelayDeflate) {
91
+ let support;
92
+ function supported() {
93
+ if (support === undefined) {
94
+ try {
95
+ new CompressionStream('deflate-raw');
96
+ new DecompressionStream('deflate-raw');
97
+ support = true;
98
+ }
99
+ catch (_a) {
100
+ support = false;
101
+ }
102
+ }
103
+ return support;
104
+ }
105
+ RelayDeflate.supported = supported;
106
+ function compress(bytes) {
107
+ return through(bytes, new CompressionStream('deflate-raw'));
108
+ }
109
+ RelayDeflate.compress = compress;
110
+ function decompress(bytes) {
111
+ return through(bytes, new DecompressionStream('deflate-raw'));
112
+ }
113
+ RelayDeflate.decompress = decompress;
114
+ async function through(bytes, stream) {
115
+ const piped = new Blob([bytes]).stream().pipeThrough(stream);
116
+ return new Uint8Array(await new Response(piped).arrayBuffer());
117
+ }
118
+ })(RelayDeflate || (exports.RelayDeflate = RelayDeflate = {}));
59
119
  //# sourceMappingURL=relay-cipher.js.map
@@ -0,0 +1,109 @@
1
+ import type { RelayPairing } from './relay-pairing';
2
+ import { RelaySocket } from './relay-socket';
3
+ /**
4
+ * The files of the workspace through the relay, for a page of a device that cannot load them by URL: behind
5
+ * a relay, and in the app on a phone, only the calls on the WebSocket reach ta serve, so `/api/files` is
6
+ * asked for here instead, on a channel of its own. The frames below are what that channel carries in place
7
+ * of the JSON-RPC of `/api/ws`; the relay forwards them sealed as it does the rest, and reads neither.
8
+ *
9
+ * The host answers only a path of {@link RELAY_FILES_PREFIX}, with GET, at the loopback address of its own
10
+ * ta serve: a device has the whole of ta serve already, and a relay that could be made to ask for anything
11
+ * else would be a way into the machine.
12
+ */
13
+ export type RelayFilesFrame =
14
+ /** The first frame of a device on a channel: this one asks for files, not for `/api/ws`. */
15
+ {
16
+ t: 'files';
17
+ v: 1;
18
+ }
19
+ /** A file to read; a URL of {@link RELAY_FILES_PREFIX}. */
20
+ | {
21
+ t: 'get';
22
+ id: number;
23
+ url: string;
24
+ }
25
+ /** What ta serve answers before the bytes. */
26
+ | {
27
+ t: 'head';
28
+ id: number;
29
+ status: number;
30
+ type?: string;
31
+ size?: number;
32
+ } | {
33
+ t: 'chunk';
34
+ id: number;
35
+ d: string;
36
+ } | {
37
+ t: 'end';
38
+ id: number;
39
+ } | {
40
+ t: 'error';
41
+ id: number;
42
+ message: string;
43
+ };
44
+ /** The only paths of ta serve a device may ask for: the ones answered without its token. */
45
+ export declare const RELAY_FILES_PREFIX = "/api/files/";
46
+ /** The bytes of a chunk, base64 in a sealed frame: well under the 1 MiB of a message of Cloudflare. */
47
+ export declare const RELAY_FILE_CHUNK_BYTES: number;
48
+ /** The largest file a device reads this way: a video of a workspace is previewed by its first bytes, not all of it. */
49
+ export declare const RELAY_FILE_MAX_BYTES: number;
50
+ /** Files read at once on one channel; a page that loads its pictures asks for several. */
51
+ export declare const RELAY_FILES_MAX_PARALLEL = 4;
52
+ /** For one file, all of it. */
53
+ export declare const RELAY_FILES_TIMEOUT_MS = 30000;
54
+ /** A file as the page gets it: its type as ta serve answers it, and the bytes. */
55
+ export interface RelayFileBytes {
56
+ type: string;
57
+ size: number;
58
+ data: Uint8Array;
59
+ }
60
+ export declare namespace RelayFilesUtil {
61
+ /** The frame that turns a channel into a channel of files. */
62
+ function hello(): RelayFilesFrame;
63
+ /** Whether a frame of a device asks for files, rather than for ta serve's socket (`{t:'files', v:1}`). */
64
+ function isHello(text: string): boolean;
65
+ }
66
+ /** One file asked for, and what came of it so far. */
67
+ interface Pending {
68
+ readonly chunks: Uint8Array[];
69
+ received: number;
70
+ head?: {
71
+ type: string;
72
+ size: number;
73
+ };
74
+ readonly resolve: (bytes: RelayFileBytes) => void;
75
+ readonly reject: (error: Error) => void;
76
+ readonly timer: ReturnType<typeof setTimeout>;
77
+ }
78
+ export interface RelayFilesOptions {
79
+ /** For one file. Default RELAY_FILES_TIMEOUT_MS. */
80
+ timeoutMs?: number;
81
+ }
82
+ /**
83
+ * The files of the ta serve of a pairing, through the relay: a channel of its own, kept open for the page,
84
+ * sealed end to end as the connection of a device is. It behaves for the page as loading the URL does.
85
+ */
86
+ export declare class RelayFiles {
87
+ protected readonly pairing: RelayPairing;
88
+ protected readonly options: RelayFilesOptions;
89
+ protected socket?: RelaySocket;
90
+ protected opening?: Promise<void>;
91
+ protected called: number;
92
+ protected readonly pending: Map<number, Pending>;
93
+ constructor(pairing: RelayPairing, options?: RelayFilesOptions);
94
+ /** The bytes of a URL of {@link RELAY_FILES_PREFIX}, as ta serve answers them. */
95
+ get(url: string): Promise<RelayFileBytes>;
96
+ /** Whether the channel is open: the page may keep one for as long as it shows files. */
97
+ get open(): boolean;
98
+ close(): void;
99
+ protected send(frame: RelayFilesFrame): void;
100
+ /** Opens the channel, once: the first file asked for opens it. */
101
+ protected connect(): Promise<void>;
102
+ protected receive(text: string): void;
103
+ protected settle(id: number, error: Error): void;
104
+ protected finish(id: number, bytes: RelayFileBytes): void;
105
+ /** The channel went: what was asked for is not coming. */
106
+ protected fail(error: Error): void;
107
+ }
108
+ export {};
109
+ //# sourceMappingURL=relay-files.d.ts.map