@celljs/ta-relay 0.0.0-stage → 4.6.1

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.
Files changed (106) hide show
  1. package/README.md +34 -2
  2. package/esm/common/index.d.ts +6 -0
  3. package/esm/common/index.js +7 -0
  4. package/esm/common/index.js.map +1 -0
  5. package/esm/common/relay-bytes.d.ts +8 -0
  6. package/esm/common/relay-bytes.js +40 -0
  7. package/esm/common/relay-bytes.js.map +1 -0
  8. package/esm/common/relay-cipher.d.ts +22 -0
  9. package/esm/common/relay-cipher.js +55 -0
  10. package/esm/common/relay-cipher.js.map +1 -0
  11. package/esm/common/relay-pairing.d.ts +18 -0
  12. package/esm/common/relay-pairing.js +35 -0
  13. package/esm/common/relay-pairing.js.map +1 -0
  14. package/esm/common/relay-protocol.d.ts +56 -0
  15. package/esm/common/relay-protocol.js +29 -0
  16. package/esm/common/relay-protocol.js.map +1 -0
  17. package/esm/common/relay-room.d.ts +31 -0
  18. package/esm/common/relay-room.js +63 -0
  19. package/esm/common/relay-room.js.map +1 -0
  20. package/esm/common/relay-socket.d.ts +38 -0
  21. package/esm/common/relay-socket.js +98 -0
  22. package/esm/common/relay-socket.js.map +1 -0
  23. package/esm/node/index.d.ts +4 -0
  24. package/esm/node/index.js +5 -0
  25. package/esm/node/index.js.map +1 -0
  26. package/esm/node/relay-host.d.ts +61 -0
  27. package/esm/node/relay-host.js +208 -0
  28. package/esm/node/relay-host.js.map +1 -0
  29. package/esm/node/relay-server.d.ts +33 -0
  30. package/esm/node/relay-server.js +127 -0
  31. package/esm/node/relay-server.js.map +1 -0
  32. package/esm/node/relay-store.d.ts +35 -0
  33. package/esm/node/relay-store.js +59 -0
  34. package/esm/node/relay-store.js.map +1 -0
  35. package/esm/package.json +3 -0
  36. package/lib/common/index.d.ts +7 -0
  37. package/lib/common/index.d.ts.map +1 -0
  38. package/lib/common/index.js +23 -0
  39. package/lib/common/index.js.map +1 -0
  40. package/lib/common/relay-bytes.d.ts +9 -0
  41. package/lib/common/relay-bytes.d.ts.map +1 -0
  42. package/lib/common/relay-bytes.js +43 -0
  43. package/lib/common/relay-bytes.js.map +1 -0
  44. package/lib/common/relay-cipher.d.ts +23 -0
  45. package/lib/common/relay-cipher.d.ts.map +1 -0
  46. package/lib/common/relay-cipher.js +59 -0
  47. package/lib/common/relay-cipher.js.map +1 -0
  48. package/lib/common/relay-cipher.spec.d.ts +2 -0
  49. package/lib/common/relay-cipher.spec.d.ts.map +1 -0
  50. package/lib/common/relay-cipher.spec.js +43 -0
  51. package/lib/common/relay-cipher.spec.js.map +1 -0
  52. package/lib/common/relay-pairing.d.ts +19 -0
  53. package/lib/common/relay-pairing.d.ts.map +1 -0
  54. package/lib/common/relay-pairing.js +38 -0
  55. package/lib/common/relay-pairing.js.map +1 -0
  56. package/lib/common/relay-pairing.spec.d.ts +2 -0
  57. package/lib/common/relay-pairing.spec.d.ts.map +1 -0
  58. package/lib/common/relay-pairing.spec.js +25 -0
  59. package/lib/common/relay-pairing.spec.js.map +1 -0
  60. package/lib/common/relay-protocol.d.ts +57 -0
  61. package/lib/common/relay-protocol.d.ts.map +1 -0
  62. package/lib/common/relay-protocol.js +32 -0
  63. package/lib/common/relay-protocol.js.map +1 -0
  64. package/lib/common/relay-room.d.ts +32 -0
  65. package/lib/common/relay-room.d.ts.map +1 -0
  66. package/lib/common/relay-room.js +67 -0
  67. package/lib/common/relay-room.js.map +1 -0
  68. package/lib/common/relay-socket.d.ts +39 -0
  69. package/lib/common/relay-socket.d.ts.map +1 -0
  70. package/lib/common/relay-socket.js +102 -0
  71. package/lib/common/relay-socket.js.map +1 -0
  72. package/lib/node/index.d.ts +5 -0
  73. package/lib/node/index.d.ts.map +1 -0
  74. package/lib/node/index.js +21 -0
  75. package/lib/node/index.js.map +1 -0
  76. package/lib/node/relay-host.d.ts +62 -0
  77. package/lib/node/relay-host.d.ts.map +1 -0
  78. package/lib/node/relay-host.js +212 -0
  79. package/lib/node/relay-host.js.map +1 -0
  80. package/lib/node/relay-server.d.ts +34 -0
  81. package/lib/node/relay-server.d.ts.map +1 -0
  82. package/lib/node/relay-server.js +131 -0
  83. package/lib/node/relay-server.js.map +1 -0
  84. package/lib/node/relay-store.d.ts +36 -0
  85. package/lib/node/relay-store.d.ts.map +1 -0
  86. package/lib/node/relay-store.js +63 -0
  87. package/lib/node/relay-store.js.map +1 -0
  88. package/lib/node/relay.spec.d.ts +2 -0
  89. package/lib/node/relay.spec.d.ts.map +1 -0
  90. package/lib/node/relay.spec.js +141 -0
  91. package/lib/node/relay.spec.js.map +1 -0
  92. package/package.json +58 -4
  93. package/src/common/index.ts +6 -0
  94. package/src/common/relay-bytes.ts +37 -0
  95. package/src/common/relay-cipher.spec.ts +45 -0
  96. package/src/common/relay-cipher.ts +74 -0
  97. package/src/common/relay-pairing.spec.ts +26 -0
  98. package/src/common/relay-pairing.ts +46 -0
  99. package/src/common/relay-protocol.ts +50 -0
  100. package/src/common/relay-room.ts +79 -0
  101. package/src/common/relay-socket.ts +106 -0
  102. package/src/node/index.ts +4 -0
  103. package/src/node/relay-host.ts +230 -0
  104. package/src/node/relay-server.ts +143 -0
  105. package/src/node/relay-store.ts +84 -0
  106. package/src/node/relay.spec.ts +151 -0
package/README.md CHANGED
@@ -1,3 +1,35 @@
1
- # Temporary Holding Version
1
+ # Cell - Ta Relay
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ 从手机等其他设备连到自己电脑上的 `ta serve`:两边都向 Relay 发起连接,Relay 只转发,内容端到端加密,Relay 读不到。默认关闭:配对了设备,`ta serve` 才连接 Relay。
4
+
5
+ ```text
6
+ 手机(RelaySocket) ──wss──▶ Relay ◀──wss── ta serve(RelayHost)──ws──▶ 本机的 /api/ws
7
+ └──────── AES-256-GCM,密钥只在两端 ────────┘
8
+ ```
9
+
10
+ ## 配对
11
+
12
+ ```bash
13
+ ta pair https://<relay> # 新增一台设备,打印配对链接
14
+ ta pair --list # 已配对的设备
15
+ ta pair --remove <设备 id> # 取消配对,立即生效
16
+ ```
17
+
18
+ 配对链接形如 `https://<relay>/pair#v=1&h=<主机 id>&d=<设备 id>&k=<密钥>`。密钥在 `#` 之后,浏览器不会把它发给 Relay。在 Ta 应用里粘贴这个链接即可连接。设备记在 agent 目录的 `relay.json`(权限 0600),`ta serve` 运行时会自己读到改动,不用重启。
19
+
20
+ ## 协议
21
+
22
+ - `/v1/host/<主机 id>?secret=<主机密钥>`:`ta serve` 连这里。每个主机 id 归第一个带着它连上来的主机,Relay 只存密钥的哈希。
23
+ - `/v1/connect/<主机 id>`:设备连这里,Relay 给它分一个通道,转给主机(`{t:'open'|'data'|'close', ch, d}`)。
24
+ - 握手:设备先发 `{t:'hello', v:1, device, nonce}`;主机找到设备的密钥后回 `{t:'ready', nonce, proof}`。两边用 HKDF-SHA-256(设备密钥,盐是两个 nonce)给每个方向各派生一个 AES-256-GCM 密钥,所以每次连接的密钥都不同。`proof` 是主机加密的第一条消息,证明对方有这台设备的密钥;之后每条消息都加密,IV 就是消息序号,Relay 重放、丢弃或调换顺序的消息都会解密失败。
25
+ - 主机给每台设备单独开一条到本机 `/api/ws` 的连接,带上本机 token。设备拿不到 token,也不需要。
26
+
27
+ ## 部署 Relay
28
+
29
+ - Cloudflare Workers:[apps/ta-relay](../../apps/ta-relay/README.md)(Durable Objects,空闲时休眠)。
30
+ - 自己的机器:`ta relay [--host 0.0.0.0] [--port 8080]`,放在 HTTPS 反向代理后面。主机 id 的归属只存在内存里,重启后由先连上来的主机重新认领。
31
+
32
+ ## 已知限制
33
+
34
+ - 经 Relay 时,只有 WebSocket 上的调用可用;Web 客户端里本地文件的预览(`/api/files`)用不了。
35
+ - 设备密钥是长期的,没有前向保密:密钥泄露后,录下的旧流量也能解开。取消配对即可让它失效。
@@ -0,0 +1,6 @@
1
+ export * from './relay-bytes.js';
2
+ export * from './relay-cipher.js';
3
+ export * from './relay-pairing.js';
4
+ export * from './relay-protocol.js';
5
+ export * from './relay-room.js';
6
+ export * from './relay-socket.js';
@@ -0,0 +1,7 @@
1
+ export * from './relay-bytes.js';
2
+ export * from './relay-cipher.js';
3
+ export * from './relay-pairing.js';
4
+ export * from './relay-protocol.js';
5
+ export * from './relay-room.js';
6
+ export * from './relay-socket.js';
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/common/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC"}
@@ -0,0 +1,8 @@
1
+ /** Bytes as text and back, in base64url, with what every platform has (no Buffer in a browser). */
2
+ export declare namespace RelayBytes {
3
+ function random(length: number): Uint8Array;
4
+ function toBase64(bytes: Uint8Array): string;
5
+ /** Throws on what is not base64url. */
6
+ function fromBase64(text: string): Uint8Array;
7
+ function concat(...parts: Uint8Array[]): Uint8Array;
8
+ }
@@ -0,0 +1,40 @@
1
+ /** Bytes as text and back, in base64url, with what every platform has (no Buffer in a browser). */
2
+ export var RelayBytes;
3
+ (function (RelayBytes) {
4
+ function random(length) {
5
+ return globalThis.crypto.getRandomValues(new Uint8Array(length));
6
+ }
7
+ RelayBytes.random = random;
8
+ function toBase64(bytes) {
9
+ let binary = '';
10
+ for (const byte of bytes) {
11
+ binary += String.fromCharCode(byte);
12
+ }
13
+ return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
14
+ }
15
+ RelayBytes.toBase64 = toBase64;
16
+ /** Throws on what is not base64url. */
17
+ function fromBase64(text) {
18
+ if (!/^[A-Za-z0-9_-]*$/.test(text)) {
19
+ throw new Error('Not base64url.');
20
+ }
21
+ const binary = atob(text.replace(/-/g, '+').replace(/_/g, '/'));
22
+ const bytes = new Uint8Array(binary.length);
23
+ for (let i = 0; i < binary.length; i++) {
24
+ bytes[i] = binary.charCodeAt(i);
25
+ }
26
+ return bytes;
27
+ }
28
+ RelayBytes.fromBase64 = fromBase64;
29
+ function concat(...parts) {
30
+ const bytes = new Uint8Array(parts.reduce((length, part) => length + part.length, 0));
31
+ let offset = 0;
32
+ for (const part of parts) {
33
+ bytes.set(part, offset);
34
+ offset += part.length;
35
+ }
36
+ return bytes;
37
+ }
38
+ RelayBytes.concat = concat;
39
+ })(RelayBytes || (RelayBytes = {}));
40
+ //# sourceMappingURL=relay-bytes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"relay-bytes.js","sourceRoot":"","sources":["../../src/common/relay-bytes.ts"],"names":[],"mappings":"AAAA,mGAAmG;AACnG,MAAM,KAAW,UAAU,CAmC1B;AAnCD,WAAiB,UAAU;IACvB,SAAgB,MAAM,CAAC,MAAc;QACjC,OAAO,UAAU,CAAC,MAAM,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;IACrE,CAAC;IAFe,WAAA,MAAM,SAErB,CAAA;IAED,SAAgB,QAAQ,CAAC,KAAiB;QACtC,IAAI,MAAM,GAAG,EAAE,CAAC;QAChB,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACvB,MAAM,IAAI,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;QACxC,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IACnF,CAAC;IANe,WAAA,QAAQ,WAMvB,CAAA;IAED,uCAAuC;IACvC,SAAgB,UAAU,CAAC,IAAY;QACnC,IAAI,CAAC,kBAAkB,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;YACjC,MAAM,IAAI,KAAK,CAAC,gBAAgB,CAAC,CAAC;QACtC,CAAC;QACD,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,CAAC;QAChE,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAC5C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACrC,KAAK,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QACpC,CAAC;QACD,OAAO,KAAK,CAAC;IACjB,CAAC;IAVe,WAAA,UAAU,aAUzB,CAAA;IAED,SAAgB,MAAM,CAAC,GAAG,KAAmB;QACzC,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC;QACtF,IAAI,MAAM,GAAG,CAAC,CAAC;QACf,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACvB,KAAK,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;YACxB,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC;QAC1B,CAAC;QACD,OAAO,KAAK,CAAC;IACjB,CAAC;IARe,WAAA,MAAM,SAQrB,CAAA;AACL,CAAC,EAnCgB,UAAU,KAAV,UAAU,QAmC1B"}
@@ -0,0 +1,22 @@
1
+ export type RelaySide = 'client' | 'host';
2
+ /**
3
+ * The encryption of one connection between a paired device and ta serve: AES-256-GCM with a key for
4
+ * each direction, derived (HKDF-SHA-256) from the device's key and a nonce of each side, so that every
5
+ * connection has keys of its own. The IV is the number of the message, which must arrive in order: a
6
+ * message replayed, dropped or reordered by the relay fails to open.
7
+ */
8
+ export declare class RelayCipher {
9
+ protected readonly sendKey: CryptoKey;
10
+ protected readonly receiveKey: CryptoKey;
11
+ protected sent: number;
12
+ protected received: number;
13
+ protected sealing: Promise<unknown>;
14
+ protected opening: Promise<unknown>;
15
+ protected constructor(sendKey: CryptoKey, receiveKey: CryptoKey);
16
+ static derive(deviceKey: Uint8Array, clientNonce: Uint8Array, hostNonce: Uint8Array, side: RelaySide): Promise<RelayCipher>;
17
+ /** Resolves in the order of the calls, so that sending in `then` sends in order. */
18
+ seal(text: string): Promise<string>;
19
+ /** Rejects what this side's peer did not seal, or not as the next message. */
20
+ open(frame: string): Promise<string>;
21
+ protected static iv(counter: number): Uint8Array;
22
+ }
@@ -0,0 +1,55 @@
1
+ import { RelayBytes } from './relay-bytes.js';
2
+ const encoder = new TextEncoder();
3
+ const decoder = new TextDecoder();
4
+ const IV_LENGTH = 12;
5
+ /**
6
+ * The encryption of one connection between a paired device and ta serve: AES-256-GCM with a key for
7
+ * each direction, derived (HKDF-SHA-256) from the device's key and a nonce of each side, so that every
8
+ * connection has keys of its own. The IV is the number of the message, which must arrive in order: a
9
+ * message replayed, dropped or reordered by the relay fails to open.
10
+ */
11
+ export class RelayCipher {
12
+ constructor(sendKey, receiveKey) {
13
+ this.sendKey = sendKey;
14
+ this.receiveKey = receiveKey;
15
+ this.sent = 0;
16
+ this.received = 0;
17
+ this.sealing = Promise.resolve();
18
+ this.opening = Promise.resolve();
19
+ }
20
+ static async derive(deviceKey, clientNonce, hostNonce, side) {
21
+ const subtle = globalThis.crypto.subtle;
22
+ const secret = await subtle.importKey('raw', deviceKey, 'HKDF', false, ['deriveKey']);
23
+ const salt = RelayBytes.concat(clientNonce, hostNonce);
24
+ const key = (direction) => subtle.deriveKey({ name: 'HKDF', hash: 'SHA-256', salt: salt, info: encoder.encode(`ta-relay/1 ${direction}`) }, secret, { name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']);
25
+ const [toHost, toClient] = await Promise.all([key('client-to-host'), key('host-to-client')]);
26
+ return side === 'client' ? new RelayCipher(toHost, toClient) : new RelayCipher(toClient, toHost);
27
+ }
28
+ /** Resolves in the order of the calls, so that sending in `then` sends in order. */
29
+ seal(text) {
30
+ const iv = RelayCipher.iv(this.sent++);
31
+ const sealed = this.sealing.then(async () => {
32
+ const data = await globalThis.crypto.subtle.encrypt({ name: 'AES-GCM', iv: iv }, this.sendKey, encoder.encode(text));
33
+ return RelayBytes.toBase64(new Uint8Array(data));
34
+ });
35
+ this.sealing = sealed.catch(() => undefined);
36
+ return sealed;
37
+ }
38
+ /** Rejects what this side's peer did not seal, or not as the next message. */
39
+ open(frame) {
40
+ const iv = RelayCipher.iv(this.received++);
41
+ const opened = this.opening.then(async () => {
42
+ const data = await globalThis.crypto.subtle.decrypt({ name: 'AES-GCM', iv: iv }, this.receiveKey, RelayBytes.fromBase64(frame));
43
+ return decoder.decode(data);
44
+ });
45
+ this.opening = opened.catch(() => undefined);
46
+ return opened;
47
+ }
48
+ static iv(counter) {
49
+ const iv = new Uint8Array(IV_LENGTH);
50
+ new DataView(iv.buffer).setUint32(IV_LENGTH - 8, Math.floor(counter / 2 ** 32));
51
+ new DataView(iv.buffer).setUint32(IV_LENGTH - 4, counter >>> 0);
52
+ return iv;
53
+ }
54
+ }
55
+ //# sourceMappingURL=relay-cipher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"relay-cipher.js","sourceRoot":"","sources":["../../src/common/relay-cipher.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAClC,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC;AAClC,MAAM,SAAS,GAAG,EAAE,CAAC;AAIrB;;;;;GAKG;AACH,MAAM,OAAO,WAAW;IAMpB,YACuB,OAAkB,EAClB,UAAqB;QADrB,YAAO,GAAP,OAAO,CAAW;QAClB,eAAU,GAAV,UAAU,CAAW;QAPlC,SAAI,G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B,CAAC;IAES,MAAM,CAAC,EAAE,CAAC,OAAe;QAC/B,MAAM,EAAE,GAAG,IAAI,UAAU,CAAC,SAAS,CAAC,CAAC;QACrC,IAAI,QAAQ,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,SAAS,GAAG,CAAC,EAAE,IAAI,CAAC,KAAK,CAAC,OAAO,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;QAChF,IAAI,QAAQ,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,SAAS,CAAC,SAAS,GAAG,CAAC,EAAE,OAAO,KAAK,CAAC,CAAC,CAAC;QAChE,OAAO,EAAE,CAAC;IACd,CAAC;CACJ"}
@@ -0,0 +1,18 @@
1
+ /** What a device needs to reach a ta serve through a relay. The key never goes to the relay. */
2
+ export interface RelayPairing {
3
+ /** The relay, https or http, without a trailing slash. */
4
+ relay: string;
5
+ host: string;
6
+ device: string;
7
+ /** base64url, 32 bytes. */
8
+ key: string;
9
+ /** What the host is called, for the device to show. */
10
+ name?: string;
11
+ }
12
+ export declare namespace RelayPairingUtil {
13
+ /** A link to open on the device: all but the relay's address is in the fragment, which is not sent to it. */
14
+ function link(pairing: RelayPairing): string;
15
+ /** Throws on a link that is not one of {@link link}. */
16
+ function parse(link: string): RelayPairing;
17
+ function websocketUrl(relay: string, path: string): string;
18
+ }
@@ -0,0 +1,35 @@
1
+ import { RELAY_ID, RELAY_PAIR_PATH } from './relay-protocol.js';
2
+ export var RelayPairingUtil;
3
+ (function (RelayPairingUtil) {
4
+ /** A link to open on the device: all but the relay's address is in the fragment, which is not sent to it. */
5
+ function link(pairing) {
6
+ const fragment = new URLSearchParams({ v: '1', h: pairing.host, d: pairing.device, k: pairing.key });
7
+ if (pairing.name) {
8
+ fragment.set('n', pairing.name);
9
+ }
10
+ return `${pairing.relay}${RELAY_PAIR_PATH}#${fragment}`;
11
+ }
12
+ RelayPairingUtil.link = link;
13
+ /** Throws on a link that is not one of {@link link}. */
14
+ function parse(link) {
15
+ var _a;
16
+ const url = new URL(link.trim());
17
+ const fragment = new URLSearchParams(url.hash.slice(1));
18
+ const [host, device, key] = ['h', 'd', 'k'].map(name => { var _a; return (_a = fragment.get(name)) !== null && _a !== void 0 ? _a : ''; });
19
+ if (!/^https?:$/.test(url.protocol) ||
20
+ url.pathname !== RELAY_PAIR_PATH ||
21
+ fragment.get('v') !== '1' ||
22
+ !RELAY_ID.test(host) ||
23
+ !RELAY_ID.test(device) ||
24
+ !/^[A-Za-z0-9_-]{43}$/.test(key)) {
25
+ throw new Error('Not a pairing link of Ta.');
26
+ }
27
+ return { relay: url.origin, host, device, key, name: (_a = fragment.get('n')) !== null && _a !== void 0 ? _a : undefined };
28
+ }
29
+ RelayPairingUtil.parse = parse;
30
+ function websocketUrl(relay, path) {
31
+ return relay.replace(/^http/, 'ws') + path;
32
+ }
33
+ RelayPairingUtil.websocketUrl = websocketUrl;
34
+ })(RelayPairingUtil || (RelayPairingUtil = {}));
35
+ //# sourceMappingURL=relay-pairing.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"relay-pairing.js","sourceRoot":"","sources":["../../src/common/relay-pairing.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAc7D,MAAM,KAAW,gBAAgB,CA+BhC;AA/BD,WAAiB,gBAAgB;IAC7B,6GAA6G;IAC7G,SAAgB,IAAI,CAAC,OAAqB;QACtC,MAAM,QAAQ,GAAG,IAAI,eAAe,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,OAAO,CAAC,IAAI,EAAE,CAAC,EAAE,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;QACrG,IAAI,OAAO,CAAC,IAAI,EAAE,CAAC;YACf,QAAQ,CAAC,GAAG,CAAC,GAAG,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;QACpC,CAAC;QACD,OAAO,G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I,CAAC,GAAG,IAAI,CAAC;IAC/C,CAAC;IAFe,iBAAA,YAAY,eAE3B,CAAA;AACL,CAAC,EA/BgB,gBAAgB,KAAhB,gBAAgB,QA+BhC"}
@@ -0,0 +1,56 @@
1
+ /** The relay of Ta, on Cloudflare Workers (apps/ta-relay), when the user names none. */
2
+ export declare const RELAY_DEFAULT_URL = "https://relay.cellbang.com";
3
+ /** What the relay serves: hosts connect to the first, devices to the second. */
4
+ export declare const RELAY_HOST_PATH = "/v1/host/";
5
+ export declare const RELAY_CONNECT_PATH = "/v1/connect/";
6
+ export declare const RELAY_PAIR_PATH = "/pair";
7
+ /** The page a pairing link opens in a browser: the link is for the app, and the key in it stays in the fragment. */
8
+ export declare const RELAY_PAIR_PAGE = "<!doctype html><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width\"><title>Ta</title>\n<body style=\"font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em\">\n<h1>Ta</h1><p>Open this link in the Ta app, or paste it there, to reach your Ta from this device.</p>\n<p>\u6253\u5F00 Ta \u5E94\u7528\uFF0C\u7C98\u8D34\u8FD9\u4E2A\u94FE\u63A5\uFF0C\u5C31\u80FD\u4ECE\u8FD9\u53F0\u8BBE\u5907\u8FDE\u5230\u4F60\u7684 Ta\u3002</p></body>";
9
+ /** Hosts and devices: what the relay knows of them, and nothing it could read from. */
10
+ export declare const RELAY_ID: RegExp;
11
+ /** Close codes of the relay. */
12
+ export declare enum RelayCloseCode {
13
+ /** The host is not connected. */
14
+ HOST_OFFLINE = 4404,
15
+ /** Another host holds this id, or the secret is wrong. */
16
+ REFUSED = 4401,
17
+ /** The same host connected again; the older connection goes. */
18
+ REPLACED = 4409,
19
+ /** The host closed this channel, or refused the device. */
20
+ CLOSED_BY_HOST = 4410,
21
+ /** Too many devices are connected to this host. */
22
+ FULL = 4429
23
+ }
24
+ /**
25
+ * Between the relay and a host, which takes the connections of many devices over one WebSocket: each
26
+ * device connection is a channel. What a device sends goes to the host as `data`, and back; the relay
27
+ * cannot read it (RelayCipher).
28
+ */
29
+ export type RelayHostMessage = {
30
+ t: 'open';
31
+ ch: string;
32
+ } | {
33
+ t: 'data';
34
+ ch: string;
35
+ d: string;
36
+ } | {
37
+ t: 'close';
38
+ ch: string;
39
+ };
40
+ /** The first message of a device on a channel, in the clear; after it every message is sealed. */
41
+ export interface RelayHello {
42
+ t: 'hello';
43
+ v: 1;
44
+ device: string;
45
+ nonce: string;
46
+ }
47
+ /** The host's answer: its nonce, and a first sealed message that shows it has the device's key. */
48
+ export type RelayWelcome = {
49
+ t: 'ready';
50
+ nonce: string;
51
+ proof: string;
52
+ } | {
53
+ t: 'refused';
54
+ reason: string;
55
+ };
56
+ export declare const RELAY_PROOF = "ta-relay ready";
@@ -0,0 +1,29 @@
1
+ /** The relay of Ta, on Cloudflare Workers (apps/ta-relay), when the user names none. */
2
+ export const RELAY_DEFAULT_URL = 'https://relay.cellbang.com';
3
+ /** What the relay serves: hosts connect to the first, devices to the second. */
4
+ export const RELAY_HOST_PATH = '/v1/host/';
5
+ export const RELAY_CONNECT_PATH = '/v1/connect/';
6
+ export const RELAY_PAIR_PATH = '/pair';
7
+ /** The page a pairing link opens in a browser: the link is for the app, and the key in it stays in the fragment. */
8
+ export const RELAY_PAIR_PAGE = `<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Ta</title>
9
+ <body style="font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em">
10
+ <h1>Ta</h1><p>Open this link in the Ta app, or paste it there, to reach your Ta from this device.</p>
11
+ <p>打开 Ta 应用,粘贴这个链接,就能从这台设备连到你的 Ta。</p></body>`;
12
+ /** Hosts and devices: what the relay knows of them, and nothing it could read from. */
13
+ export const RELAY_ID = /^[A-Za-z0-9_-]{16,64}$/;
14
+ /** Close codes of the relay. */
15
+ export var RelayCloseCode;
16
+ (function (RelayCloseCode) {
17
+ /** The host is not connected. */
18
+ RelayCloseCode[RelayCloseCode["HOST_OFFLINE"] = 4404] = "HOST_OFFLINE";
19
+ /** Another host holds this id, or the secret is wrong. */
20
+ RelayCloseCode[RelayCloseCode["REFUSED"] = 4401] = "REFUSED";
21
+ /** The same host connected again; the older connection goes. */
22
+ RelayCloseCode[RelayCloseCode["REPLACED"] = 4409] = "REPLACED";
23
+ /** The host closed this channel, or refused the device. */
24
+ RelayCloseCode[RelayCloseCode["CLOSED_BY_HOST"] = 4410] = "CLOSED_BY_HOST";
25
+ /** Too many devices are connected to this host. */
26
+ RelayCloseCode[RelayCloseCode["FULL"] = 4429] = "FULL";
27
+ })(RelayCloseCode || (RelayCloseCode = {}));
28
+ export const RELAY_PROOF = 'ta-relay ready';
29
+ //# sourceMappingURL=relay-protocol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"relay-protocol.js","sourceRoot":"","sources":["../../src/common/relay-protocol.ts"],"names":[],"mappings":"AAAA,wFAAwF;AACxF,MAAM,CAAC,MAAM,iBAAiB,GAAG,4BAA4B,CAAC;AAE9D,gFAAgF;AAChF,MAAM,CAAC,MAAM,eAAe,GAAG,WAAW,CAAC;AAC3C,MAAM,CAAC,MAAM,kBAAkB,GAAG,cAAc,CAAC;AACjD,MAAM,CAAC,MAAM,eAAe,GAAG,OAAO,CAAC;AAEvC,oHAAoH;AACpH,MAAM,CAAC,MAAM,eAAe,GAAG;;;8CAGe,CAAC;AAE/C,uFAAuF;AACvF,MAAM,CAAC,MAAM,QAAQ,GAAG,wBAAwB,CAAC;AAEjD,gCAAgC;AAChC,MAAM,CAAN,IAAY,cAWX;AAXD,WAAY,cAAc;IACtB,iCAAiC;IACjC,sEAAmB,CAAA;IACnB,0DAA0D;IAC1D,4DAAc,CAAA;IACd,gEAAgE;IAChE,8DAAe,CAAA;IACf,2DAA2D;IAC3D,0EAAqB,CAAA;IACrB,mDAAmD;IACnD,sDAAW,CAAA;AACf,CAAC,EAXW,cAAc,KAAd,cAAc,QAWzB;AAoBD,MAAM,CAAC,MAAM,WAAW,GAAG,gBAAgB,CAAC"}
@@ -0,0 +1,31 @@
1
+ import { RelayCloseCode, type RelayHostMessage } from './relay-protocol.js';
2
+ /** One end of a WebSocket at the relay. */
3
+ export interface RelayPeer {
4
+ send(text: string): void;
5
+ close(code: number, reason: string): void;
6
+ }
7
+ /** Where the room finds its connections: kept in memory, or in a Durable Object that sleeps between messages. */
8
+ export interface RelayPeers {
9
+ host(): RelayPeer | undefined;
10
+ client(channel: string): RelayPeer | undefined;
11
+ clients(): RelayPeer[];
12
+ }
13
+ /** Devices at once on one host. */
14
+ export declare const RELAY_MAX_CLIENTS = 16;
15
+ /**
16
+ * The relay's whole job for one host: pass what a device sends to the host on the device's channel, and
17
+ * what the host sends on a channel to that device. It holds no state of its own, so that it runs the same
18
+ * in `ta relay` and in a Durable Object.
19
+ */
20
+ export declare class RelayRoom {
21
+ protected readonly peers: RelayPeers;
22
+ constructor(peers: RelayPeers);
23
+ /** False when the device cannot stay: the host is away or has too many devices; close it with the code. */
24
+ clientOpened(channel: string): RelayCloseCode | undefined;
25
+ fromClient(channel: string, text: string): void;
26
+ clientClosed(channel: string): void;
27
+ /** What the host sends that is not a message of the protocol is dropped. */
28
+ fromHost(text: string): void;
29
+ hostClosed(): void;
30
+ protected toHost(host: RelayPeer, message: RelayHostMessage): void;
31
+ }
@@ -0,0 +1,63 @@
1
+ import { RelayCloseCode } from './relay-protocol.js';
2
+ /** Devices at once on one host. */
3
+ export const RELAY_MAX_CLIENTS = 16;
4
+ /**
5
+ * The relay's whole job for one host: pass what a device sends to the host on the device's channel, and
6
+ * what the host sends on a channel to that device. It holds no state of its own, so that it runs the same
7
+ * in `ta relay` and in a Durable Object.
8
+ */
9
+ export class RelayRoom {
10
+ constructor(peers) {
11
+ this.peers = peers;
12
+ }
13
+ /** False when the device cannot stay: the host is away or has too many devices; close it with the code. */
14
+ clientOpened(channel) {
15
+ const host = this.peers.host();
16
+ if (!host) {
17
+ return RelayCloseCode.HOST_OFFLINE;
18
+ }
19
+ if (this.peers.clients().length > RELAY_MAX_CLIENTS) {
20
+ return RelayCloseCode.FULL;
21
+ }
22
+ this.toHost(host, { t: 'open', ch: channel });
23
+ return undefined;
24
+ }
25
+ fromClient(channel, text) {
26
+ const host = this.peers.host();
27
+ if (host) {
28
+ this.toHost(host, { t: 'data', ch: channel, d: text });
29
+ }
30
+ }
31
+ clientClosed(channel) {
32
+ const host = this.peers.host();
33
+ if (host) {
34
+ this.toHost(host, { t: 'close', ch: channel });
35
+ }
36
+ }
37
+ /** What the host sends that is not a message of the protocol is dropped. */
38
+ fromHost(text) {
39
+ let message;
40
+ try {
41
+ message = JSON.parse(text);
42
+ }
43
+ catch (_a) {
44
+ return;
45
+ }
46
+ const client = typeof message.ch === 'string' ? this.peers.client(message.ch) : undefined;
47
+ if (message.t === 'data' && typeof message.d === 'string') {
48
+ client === null || client === void 0 ? void 0 : client.send(message.d);
49
+ }
50
+ else if (message.t === 'close') {
51
+ client === null || client === void 0 ? void 0 : client.close(RelayCloseCode.CLOSED_BY_HOST, 'Closed by the host.');
52
+ }
53
+ }
54
+ hostClosed() {
55
+ for (const client of this.peers.clients()) {
56
+ client.close(RelayCloseCode.HOST_OFFLINE, 'The host went offline.');
57
+ }
58
+ }
59
+ toHost(host, message) {
60
+ host.send(JSON.stringify(message));
61
+ }
62
+ }
63
+ //# sourceMappingURL=relay-room.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"relay-room.js","sourceRoot":"","sources":["../../src/common/relay-room.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAyB,MAAM,kBAAkB,CAAC;AAezE,mCAAmC;AACnC,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAEpC;;;;GAIG;AACH,MAAM,OAAO,SAAS;IAClB,YAA+B,KAAiB;qBAAjB,KAAK;IAAe,CAAC;IAEpD,2GAA2G;IAC3G,YAAY,CAAC,OAAe;QACxB,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC;QAC/B,IAAI,CAAC,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AS,CAAC;QAC1F,IAAI,OAAO,CAAC,CAAC,KAAK,MAAM,IAAI,OAAO,OAAO,CAAC,CAAC,KAAK,QAAQ,EAAE,CAAC;YACxD,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;QAC5B,CAAC;aAAM,IAAI,OAAO,CAAC,CAAC,KAAK,OAAO,EAAE,CAAC;YAC/B,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,KAAK,CAAC,cAAc,CAAC,cAAc,EAAE,qBAAqB,CAAC,CAAC;QACxE,CAAC;IACL,CAAC;IAED,UAAU;QACN,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,EAAE,EAAE,CAAC;YACxC,MAAM,CAAC,KAAK,CAAC,cAAc,CAAC,YAAY,EAAE,wBAAwB,CAAC,CAAC;QACxE,CAAC;IACL,CAAC;IAES,MAAM,CAAC,IAAe,EAAE,OAAyB;QACvD,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC;IACvC,CAAC;CACJ"}
@@ -0,0 +1,38 @@
1
+ import { RelayCipher } from './relay-cipher.js';
2
+ import { type RelayPairing } from './relay-pairing.js';
3
+ /**
4
+ * A WebSocket to ta serve through a relay, for a paired device: it says hello, checks that the host has
5
+ * the device's key, and then seals and opens every message. It behaves as much like a WebSocket as
6
+ * TaConnection needs: `readyState`, `send`, `close` and the four handlers.
7
+ */
8
+ export declare class RelaySocket {
9
+ protected readonly pairing: RelayPairing;
10
+ static readonly CONNECTING = 0;
11
+ static readonly OPEN = 1;
12
+ static readonly CLOSING = 2;
13
+ static readonly CLOSED = 3;
14
+ /** The close code when the host does not know the device, or does not show its key. */
15
+ static readonly REFUSED = 4403;
16
+ onopen: (() => void) | null;
17
+ onmessage: ((message: {
18
+ data: string;
19
+ }) => void) | null;
20
+ onclose: ((event: {
21
+ code: number;
22
+ reason: string;
23
+ }) => void) | null;
24
+ onerror: ((event: unknown) => void) | null;
25
+ protected state: number;
26
+ protected code?: number;
27
+ protected cipher?: RelayCipher;
28
+ protected readonly socket: WebSocket;
29
+ protected readonly nonce: Uint8Array<ArrayBufferLike>;
30
+ constructor(pairing: RelayPairing);
31
+ get readyState(): number;
32
+ /** Why it closed, once it has. */
33
+ get closeCode(): number | undefined;
34
+ send(text: string): void;
35
+ close(code?: number, reason?: string): void;
36
+ protected receive(text: string): Promise<void>;
37
+ protected finish(code: number, reason: string): void;
38
+ }
@@ -0,0 +1,98 @@
1
+ import { RelayBytes } from './relay-bytes.js';
2
+ import { RelayCipher } from './relay-cipher.js';
3
+ import { RelayPairingUtil } from './relay-pairing.js';
4
+ import { RELAY_CONNECT_PATH, RELAY_PROOF } from './relay-protocol.js';
5
+ const CONNECTING = 0;
6
+ const OPEN = 1;
7
+ const CLOSING = 2;
8
+ const CLOSED = 3;
9
+ /**
10
+ * A WebSocket to ta serve through a relay, for a paired device: it says hello, checks that the host has
11
+ * the device's key, and then seals and opens every message. It behaves as much like a WebSocket as
12
+ * TaConnection needs: `readyState`, `send`, `close` and the four handlers.
13
+ */
14
+ export class RelaySocket {
15
+ constructor(pairing) {
16
+ this.pairing = pairing;
17
+ this.onopen = null;
18
+ this.onmessage = null;
19
+ this.onclose = null;
20
+ this.onerror = null;
21
+ this.state = CONNECTING;
22
+ this.nonce = RelayBytes.random(16);
23
+ this.socket = new WebSocket(RelayPairingUtil.websocketUrl(pairing.relay, RELAY_CONNECT_PATH + pairing.host));
24
+ this.socket.onopen = () => {
25
+ const hello = { t: 'hello', v: 1, device: pairing.device, nonce: RelayBytes.toBase64(this.nonce) };
26
+ this.socket.send(JSON.stringify(hello));
27
+ };
28
+ this.socket.onmessage = message => void this.receive(String(message.data));
29
+ this.socket.onerror = event => { var _a; return (_a = this.onerror) === null || _a === void 0 ? void 0 : _a.call(this, event); };
30
+ this.socket.onclose = event => this.finish(event.code, event.reason);
31
+ }
32
+ get readyState() {
33
+ return this.state;
34
+ }
35
+ /** Why it closed, once it has. */
36
+ get closeCode() {
37
+ return this.code;
38
+ }
39
+ send(text) {
40
+ if (this.state !== OPEN || !this.cipher) {
41
+ throw new Error('The connection is not open.');
42
+ }
43
+ this.cipher.seal(text).then(frame => this.socket.readyState === OPEN && this.socket.send(frame));
44
+ }
45
+ close(code = 1000, reason = '') {
46
+ if (this.state === CLOSED) {
47
+ return;
48
+ }
49
+ this.state = CLOSING;
50
+ this.socket.close(code, reason);
51
+ }
52
+ async receive(text) {
53
+ var _a, _b;
54
+ if (this.cipher) {
55
+ try {
56
+ const data = await this.cipher.open(text);
57
+ (_a = this.onmessage) === null || _a === void 0 ? void 0 : _a.call(this, { data });
58
+ }
59
+ catch (_c) {
60
+ this.close(4400, 'A message did not open.');
61
+ }
62
+ return;
63
+ }
64
+ try {
65
+ const welcome = JSON.parse(text);
66
+ if (welcome.t !== 'ready') {
67
+ throw new Error(welcome.reason);
68
+ }
69
+ const cipher = await RelayCipher.derive(RelayBytes.fromBase64(this.pairing.key), this.nonce, RelayBytes.fromBase64(welcome.nonce), 'client');
70
+ if ((await cipher.open(welcome.proof)) !== RELAY_PROOF) {
71
+ throw new Error('The host did not show the key.');
72
+ }
73
+ this.cipher = cipher;
74
+ this.state = OPEN;
75
+ (_b = this.onopen) === null || _b === void 0 ? void 0 : _b.call(this);
76
+ }
77
+ catch (_d) {
78
+ // A host without the key is not this device's host, whoever answered.
79
+ this.close(RelaySocket.REFUSED, 'The host refused this device.');
80
+ }
81
+ }
82
+ finish(code, reason) {
83
+ var _a;
84
+ if (this.state === CLOSED) {
85
+ return;
86
+ }
87
+ this.state = CLOSED;
88
+ this.code = code;
89
+ (_a = this.onclose) === null || _a === void 0 ? void 0 : _a.call(this, { code, reason });
90
+ }
91
+ }
92
+ RelaySocket.CONNECTING = CONNECTING;
93
+ RelaySocket.OPEN = OPEN;
94
+ RelaySocket.CLOSING = CLOSING;
95
+ RelaySocket.CLOSED = CLOSED;
96
+ /** The close code when the host does not know the device, or does not show its key. */
97
+ RelaySocket.REFUSED = 4403;
98
+ //# sourceMappingURL=relay-socket.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"relay-socket.js","sourceRoot":"","sources":["../../src/common/relay-socket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,EAAqB,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACtE,OAAO,EAAE,kBAAkB,EAAE,WAAW,EAAsC,MAAM,kBAAkB,CAAC;AAEvG,MAAM,UAAU,GAAG,CAAC,CAAC;AACrB,MAAM,IAAI,GAAG,CAAC,CAAC;AACf,MAAM,OAAO,GAAG,CAAC,CAAC;AAClB,MAAM,MAAM,GAAG,CAAC,CAAC;AAEjB;;;;GAIG;AACH,MAAM,OAAO,WAAW;IAmBpB,YAA+B,OAAqB;QAArB,YAAO,GAAP,OAAO,CAAc;QAXpD,WAAM,GAAwB,IAAI,CAAC;QACnC,cAAS,GAAiD,IAAI,CAAC;QAC/D,YAAO,GAA+D,IAAI,CAAC;QAC3E,YAAO,GAAsC,IAAI,CAAC;QAExC,UAAK,GAAG,UAAU,CAAC;QAIV,UAAK,GAAG,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QAG7C,IAAI,CAAC,MAAM,GAAG,IAAI,SAAS,CAAC,gBAAgB,CAAC,YAAY,CAAC,OAAO,CAAC,KAAK,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;QAC7G,IAAI,CAAC,MAAM,CAAC,MAAM,GAAG,GAAG,EAAE;YACtB,MAAM,KAAK,GAAe,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE,KAAK,EAAE,UAAU,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,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CAAC;mBAAO,CAAC;YACL,sEAAsE;YACtE,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,OAAO,EAAE,+BAA+B,CAAC,CAAC;QACrE,CAAC;IACL,CAAC;IAES,MAAM,CAAC,IAAY,EAAE,MAAc;;QACzC,IAAI,IAAI,CAAC,KAAK,KAAK,MAAM,EAAE,CAAC;YACxB,OAAO;QACX,CAAC;QACD,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC;QACpB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,MAAA,IAAI,CAAC,OAAO,+CAAZ,IAAI,EAAW,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,CAAC;IACrC,CAAC;CACJ;AAzFmB,sBAAU,GAAG,UAAU,AAAb,CAAc;AACxB,gBAAI,GAAG,IAAI,AAAP,CAAQ;AACZ,mBAAO,GAAG,OAAO,AAAV,CAAW;AAClB,kBAAM,GAAG,MAAM,AAAT,CAAU;AAChC,uFAAuF;AACvE,mBAAO,GAAG,IAAI,AAAP,CAAQ"}
@@ -0,0 +1,4 @@
1
+ export * from '../common/index.js';
2
+ export * from './relay-host.js';
3
+ export * from './relay-server.js';
4
+ export * from './relay-store.js';
@@ -0,0 +1,5 @@
1
+ export * from '../common/index.js';
2
+ export * from './relay-host.js';
3
+ export * from './relay-server.js';
4
+ export * from './relay-store.js';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/node/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC"}