@celljs/ta-relay 4.10.0 → 4.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +13 -1
- package/README.md +13 -1
- package/esm/common/index.d.ts +9 -0
- package/esm/common/index.js +9 -0
- package/esm/common/relay-blobs.d.ts +54 -0
- package/esm/common/relay-blobs.js +153 -0
- package/esm/common/relay-card.d.ts +23 -0
- package/esm/common/relay-card.js +30 -0
- package/esm/common/relay-content.d.ts +104 -0
- package/esm/common/relay-content.js +316 -0
- package/esm/common/relay-conversation.d.ts +59 -0
- package/esm/common/relay-conversation.js +9 -0
- package/esm/common/relay-hub-util.d.ts +91 -0
- package/esm/common/relay-hub-util.js +164 -0
- package/esm/common/relay-hub.d.ts +125 -0
- package/esm/common/relay-hub.js +438 -0
- package/esm/common/relay-hubs.d.ts +271 -0
- package/esm/common/relay-hubs.js +844 -0
- package/esm/common/relay-identity.d.ts +46 -0
- package/esm/common/relay-identity.js +126 -0
- package/esm/common/relay-protocol.d.ts +211 -2
- package/esm/common/relay-protocol.js +53 -0
- package/esm/common/relay-room-chat.d.ts +80 -0
- package/esm/common/relay-room-chat.js +152 -0
- package/esm/common/relay-socket.d.ts +8 -0
- package/esm/common/relay-socket.js +23 -2
- package/esm/guest/guest-i18n.d.ts +47 -0
- package/esm/guest/guest-i18n.js +97 -0
- package/esm/guest/guest-style.d.ts +1 -0
- package/esm/guest/guest-style.js +78 -0
- package/esm/guest/guest.d.ts +1 -0
- package/esm/guest/guest.js +371 -0
- package/esm/node/index.d.ts +2 -0
- package/esm/node/index.js +2 -0
- package/esm/node/relay-host.d.ts +25 -3
- package/esm/node/relay-host.js +115 -4
- package/esm/node/relay-hub-files.d.ts +12 -0
- package/esm/node/relay-hub-files.js +39 -0
- package/esm/node/relay-server.d.ts +46 -1
- package/esm/node/relay-server.js +227 -1
- package/esm/node/relay-store.d.ts +72 -0
- package/esm/node/relay-store.js +123 -2
- package/esm/node/relay-viewer.d.ts +7 -0
- package/esm/node/relay-viewer.js +33 -0
- package/esm/viewer/viewer-i18n.d.ts +56 -0
- package/esm/viewer/viewer-i18n.js +117 -0
- package/esm/viewer/viewer-markdown.d.ts +7 -0
- package/esm/viewer/viewer-markdown.js +198 -0
- package/esm/viewer/viewer-style.d.ts +1 -0
- package/esm/viewer/viewer-style.js +72 -0
- package/esm/viewer/viewer.d.ts +8 -0
- package/esm/viewer/viewer.js +378 -0
- package/lib/common/index.d.ts +9 -0
- package/lib/common/index.js +9 -0
- package/lib/common/relay-blobs.d.ts +55 -0
- package/lib/common/relay-blobs.js +157 -0
- package/lib/common/relay-card.d.ts +24 -0
- package/lib/common/relay-card.js +33 -0
- package/lib/common/relay-content.d.ts +105 -0
- package/lib/common/relay-content.js +320 -0
- package/lib/common/relay-conversation.d.ts +60 -0
- package/lib/common/relay-conversation.js +12 -0
- package/lib/common/relay-hub-util.d.ts +92 -0
- package/lib/common/relay-hub-util.js +167 -0
- package/lib/common/relay-hub.d.ts +126 -0
- package/lib/common/relay-hub.js +442 -0
- package/lib/common/relay-hubs.d.ts +272 -0
- package/lib/common/relay-hubs.js +848 -0
- package/lib/common/relay-identity.d.ts +47 -0
- package/lib/common/relay-identity.js +129 -0
- package/lib/common/relay-protocol.d.ts +211 -2
- package/lib/common/relay-protocol.js +54 -1
- package/lib/common/relay-room-chat.d.ts +81 -0
- package/lib/common/relay-room-chat.js +156 -0
- package/lib/common/relay-socket.d.ts +8 -0
- package/lib/common/relay-socket.js +23 -2
- package/lib/guest/guest-i18n.d.ts +48 -0
- package/lib/guest/guest-i18n.js +100 -0
- package/lib/guest/guest-style.d.ts +2 -0
- package/lib/guest/guest-style.js +81 -0
- package/lib/guest/guest.d.ts +2 -0
- package/lib/guest/guest.js +373 -0
- package/lib/node/index.d.ts +2 -0
- package/lib/node/index.js +2 -0
- package/lib/node/relay-host.d.ts +25 -3
- package/lib/node/relay-host.js +115 -4
- package/lib/node/relay-hub-files.d.ts +13 -0
- package/lib/node/relay-hub-files.js +43 -0
- package/lib/node/relay-server.d.ts +46 -1
- package/lib/node/relay-server.js +226 -0
- package/lib/node/relay-store.d.ts +72 -0
- package/lib/node/relay-store.js +121 -0
- package/lib/node/relay-viewer.d.ts +8 -0
- package/lib/node/relay-viewer.js +32 -0
- package/lib/viewer/viewer-i18n.d.ts +57 -0
- package/lib/viewer/viewer-i18n.js +120 -0
- package/lib/viewer/viewer-markdown.d.ts +8 -0
- package/lib/viewer/viewer-markdown.js +201 -0
- package/lib/viewer/viewer-style.d.ts +2 -0
- package/lib/viewer/viewer-style.js +75 -0
- package/lib/viewer/viewer.d.ts +9 -0
- package/lib/viewer/viewer.js +381 -0
- package/package.json +5 -4
- package/viewer/frame.html +1 -0
- package/viewer/page.html +80 -0
- package/viewer/room.html +87 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/** What others know of a Ta (docs/design/ta-link.md 3.1): its two public keys, raw and base64url, and the name it gives. */
|
|
2
|
+
export interface RelayPublicIdentity {
|
|
3
|
+
/** RelayIdentityUtil.id of the two keys. */
|
|
4
|
+
id: string;
|
|
5
|
+
name: string;
|
|
6
|
+
/** ECDSA P-256: what it signs with. */
|
|
7
|
+
sign: string;
|
|
8
|
+
/** ECDH P-256: what keys for it are agreed with. */
|
|
9
|
+
box: string;
|
|
10
|
+
}
|
|
11
|
+
/** A Ta's own identity, its private keys as JWK; in `identity.json` of the agent directory. */
|
|
12
|
+
export interface RelayIdentity extends RelayPublicIdentity {
|
|
13
|
+
signKey: JsonWebKey;
|
|
14
|
+
boxKey: JsonWebKey;
|
|
15
|
+
createdAt: number;
|
|
16
|
+
}
|
|
17
|
+
/** Keys, sealing and signatures of rooms and identities, with WebCrypto only: the same in ta serve, a browser and a Worker. */
|
|
18
|
+
export declare namespace RelayKeys {
|
|
19
|
+
function hkdf(key: Uint8Array, salt: Uint8Array, info: string): Promise<Uint8Array>;
|
|
20
|
+
function sha256(bytes: Uint8Array | string): Promise<Uint8Array>;
|
|
21
|
+
/** base64url of SHA-256 of the text: what the relay keeps of a token. */
|
|
22
|
+
function hash(text: string): Promise<string>;
|
|
23
|
+
function hmac(key: Uint8Array, text: string): Promise<string>;
|
|
24
|
+
function aesKey(key: Uint8Array): Promise<CryptoKey>;
|
|
25
|
+
/** base64url of a random IV and AES-256-GCM of the text. */
|
|
26
|
+
function seal(key: CryptoKey, text: string): Promise<string>;
|
|
27
|
+
/** Throws when it was not sealed with this key. */
|
|
28
|
+
function open(key: CryptoKey, sealed: string): Promise<string>;
|
|
29
|
+
}
|
|
30
|
+
/** The identity of a Ta (docs/design/ta-link.md 3.1): made once, then what it signs with and agrees keys with. */
|
|
31
|
+
export declare namespace RelayIdentityUtil {
|
|
32
|
+
function create(name: string, now?: number): Promise<RelayIdentity>;
|
|
33
|
+
/** The first 16 bytes of SHA-256 of both public keys, base64url (22 characters): the same keys, the same id. */
|
|
34
|
+
function id(sign: string, box: string): Promise<string>;
|
|
35
|
+
/** Whether the keys are what the id says, and well formed. */
|
|
36
|
+
function check(identity: RelayPublicIdentity): Promise<boolean>;
|
|
37
|
+
function publicOf(identity: RelayPublicIdentity): RelayPublicIdentity;
|
|
38
|
+
function sign(identity: RelayIdentity, text: string): Promise<string>;
|
|
39
|
+
/** False for a signature that is not of this text by this key, or not a signature at all. */
|
|
40
|
+
function verify(signPublic: string, text: string, signature: string): Promise<boolean>;
|
|
41
|
+
/**
|
|
42
|
+
* A key only two identities can make, each with its own private key and the other's public one: HKDF of their
|
|
43
|
+
* ECDH, with `salt` and `info` saying what it is for.
|
|
44
|
+
*/
|
|
45
|
+
function agree(identity: RelayIdentity, otherBox: string, salt: string, info: string): Promise<Uint8Array>;
|
|
46
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { RelayBytes } from './relay-bytes.js';
|
|
2
|
+
const encoder = new TextEncoder();
|
|
3
|
+
const decoder = new TextDecoder();
|
|
4
|
+
const subtle = () => globalThis.crypto.subtle;
|
|
5
|
+
const SIGN = { name: 'ECDSA', namedCurve: 'P-256' };
|
|
6
|
+
const BOX = { name: 'ECDH', namedCurve: 'P-256' };
|
|
7
|
+
const SIGNATURE = { name: 'ECDSA', hash: 'SHA-256' };
|
|
8
|
+
/** Keys, sealing and signatures of rooms and identities, with WebCrypto only: the same in ta serve, a browser and a Worker. */
|
|
9
|
+
export var RelayKeys;
|
|
10
|
+
(function (RelayKeys) {
|
|
11
|
+
async function hkdf(key, salt, info) {
|
|
12
|
+
const secret = await subtle().importKey('raw', key, 'HKDF', false, ['deriveBits']);
|
|
13
|
+
const bits = await subtle().deriveBits({ name: 'HKDF', hash: 'SHA-256', salt: salt, info: encoder.encode(info) }, secret, 256);
|
|
14
|
+
return new Uint8Array(bits);
|
|
15
|
+
}
|
|
16
|
+
RelayKeys.hkdf = hkdf;
|
|
17
|
+
async function sha256(bytes) {
|
|
18
|
+
const data = typeof bytes === 'string' ? encoder.encode(bytes) : bytes;
|
|
19
|
+
return new Uint8Array(await subtle().digest('SHA-256', data));
|
|
20
|
+
}
|
|
21
|
+
RelayKeys.sha256 = sha256;
|
|
22
|
+
/** base64url of SHA-256 of the text: what the relay keeps of a token. */
|
|
23
|
+
async function hash(text) {
|
|
24
|
+
return RelayBytes.toBase64(await sha256(text));
|
|
25
|
+
}
|
|
26
|
+
RelayKeys.hash = hash;
|
|
27
|
+
async function hmac(key, text) {
|
|
28
|
+
const imported = await subtle().importKey('raw', key, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign']);
|
|
29
|
+
return RelayBytes.toBase64(new Uint8Array(await subtle().sign('HMAC', imported, encoder.encode(text))));
|
|
30
|
+
}
|
|
31
|
+
RelayKeys.hmac = hmac;
|
|
32
|
+
function aesKey(key) {
|
|
33
|
+
return subtle().importKey('raw', key, 'AES-GCM', false, ['encrypt', 'decrypt']);
|
|
34
|
+
}
|
|
35
|
+
RelayKeys.aesKey = aesKey;
|
|
36
|
+
/** base64url of a random IV and AES-256-GCM of the text. */
|
|
37
|
+
async function seal(key, text) {
|
|
38
|
+
const iv = RelayBytes.random(12);
|
|
39
|
+
const sealed = await subtle().encrypt({ name: 'AES-GCM', iv: iv }, key, encoder.encode(text));
|
|
40
|
+
return RelayBytes.toBase64(RelayBytes.concat(iv, new Uint8Array(sealed)));
|
|
41
|
+
}
|
|
42
|
+
RelayKeys.seal = seal;
|
|
43
|
+
/** Throws when it was not sealed with this key. */
|
|
44
|
+
async function open(key, sealed) {
|
|
45
|
+
const bytes = RelayBytes.fromBase64(sealed);
|
|
46
|
+
const opened = await subtle().decrypt({ name: 'AES-GCM', iv: bytes.subarray(0, 12) }, key, bytes.subarray(12));
|
|
47
|
+
return decoder.decode(opened);
|
|
48
|
+
}
|
|
49
|
+
RelayKeys.open = open;
|
|
50
|
+
})(RelayKeys || (RelayKeys = {}));
|
|
51
|
+
/** The identity of a Ta (docs/design/ta-link.md 3.1): made once, then what it signs with and agrees keys with. */
|
|
52
|
+
export var RelayIdentityUtil;
|
|
53
|
+
(function (RelayIdentityUtil) {
|
|
54
|
+
async function create(name, now = Date.now()) {
|
|
55
|
+
const sign = (await subtle().generateKey(SIGN, true, ['sign', 'verify']));
|
|
56
|
+
const box = (await subtle().generateKey(BOX, true, ['deriveBits']));
|
|
57
|
+
const raw = async (key) => RelayBytes.toBase64(new Uint8Array(await subtle().exportKey('raw', key)));
|
|
58
|
+
const [signPublic, boxPublic] = await Promise.all([raw(sign.publicKey), raw(box.publicKey)]);
|
|
59
|
+
return {
|
|
60
|
+
id: await id(signPublic, boxPublic),
|
|
61
|
+
name,
|
|
62
|
+
sign: signPublic,
|
|
63
|
+
box: boxPublic,
|
|
64
|
+
signKey: await subtle().exportKey('jwk', sign.privateKey),
|
|
65
|
+
boxKey: await subtle().exportKey('jwk', box.privateKey),
|
|
66
|
+
createdAt: now,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
RelayIdentityUtil.create = create;
|
|
70
|
+
/** The first 16 bytes of SHA-256 of both public keys, base64url (22 characters): the same keys, the same id. */
|
|
71
|
+
async function id(sign, box) {
|
|
72
|
+
const digest = await RelayKeys.sha256(RelayBytes.concat(RelayBytes.fromBase64(sign), RelayBytes.fromBase64(box)));
|
|
73
|
+
return RelayBytes.toBase64(digest.subarray(0, 16));
|
|
74
|
+
}
|
|
75
|
+
RelayIdentityUtil.id = id;
|
|
76
|
+
/** Whether the keys are what the id says, and well formed. */
|
|
77
|
+
async function check(identity) {
|
|
78
|
+
try {
|
|
79
|
+
if (identity.id !== (await id(identity.sign, identity.box))) {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
await Promise.all([importSign(identity.sign), importBox(identity.box)]);
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
catch (_a) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
RelayIdentityUtil.check = check;
|
|
90
|
+
function publicOf(identity) {
|
|
91
|
+
return { id: identity.id, name: identity.name, sign: identity.sign, box: identity.box };
|
|
92
|
+
}
|
|
93
|
+
RelayIdentityUtil.publicOf = publicOf;
|
|
94
|
+
async function sign(identity, text) {
|
|
95
|
+
const key = await subtle().importKey('jwk', identity.signKey, SIGN, false, ['sign']);
|
|
96
|
+
return RelayBytes.toBase64(new Uint8Array(await subtle().sign(SIGNATURE, key, encoder.encode(text))));
|
|
97
|
+
}
|
|
98
|
+
RelayIdentityUtil.sign = sign;
|
|
99
|
+
/** False for a signature that is not of this text by this key, or not a signature at all. */
|
|
100
|
+
async function verify(signPublic, text, signature) {
|
|
101
|
+
try {
|
|
102
|
+
return await subtle().verify(SIGNATURE, await importSign(signPublic), RelayBytes.fromBase64(signature), encoder.encode(text));
|
|
103
|
+
}
|
|
104
|
+
catch (_a) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
RelayIdentityUtil.verify = verify;
|
|
109
|
+
/**
|
|
110
|
+
* A key only two identities can make, each with its own private key and the other's public one: HKDF of their
|
|
111
|
+
* ECDH, with `salt` and `info` saying what it is for.
|
|
112
|
+
*/
|
|
113
|
+
async function agree(identity, otherBox, salt, info) {
|
|
114
|
+
const own = await subtle().importKey('jwk', identity.boxKey, BOX, false, ['deriveBits']);
|
|
115
|
+
const shared = await subtle().deriveBits({ name: 'ECDH', public: await importBox(otherBox) }, own, 256);
|
|
116
|
+
return RelayKeys.hkdf(new Uint8Array(shared), encoder.encode(salt), info);
|
|
117
|
+
}
|
|
118
|
+
RelayIdentityUtil.agree = agree;
|
|
119
|
+
function importSign(raw) {
|
|
120
|
+
return subtle().importKey('raw', RelayBytes.fromBase64(raw), SIGN, false, ['verify']);
|
|
121
|
+
}
|
|
122
|
+
function importBox(raw) {
|
|
123
|
+
return subtle().importKey('raw', RelayBytes.fromBase64(raw), BOX, false, []);
|
|
124
|
+
}
|
|
125
|
+
})(RelayIdentityUtil || (RelayIdentityUtil = {}));
|
|
126
|
+
//# sourceMappingURL=relay-identity.js.map
|
|
@@ -6,6 +6,34 @@ export declare const RELAY_CONNECT_PATH = "/v1/connect/";
|
|
|
6
6
|
export declare const RELAY_PAIR_PATH = "/pair";
|
|
7
7
|
/** A link of an extension handed on: its package comes from the computer of the one who shared it, through the relay. */
|
|
8
8
|
export declare const RELAY_SHARE_PATH = "/share";
|
|
9
|
+
/**
|
|
10
|
+
* Links of content shared (RelayContentUtil): a conversation, a page, a file, which the page of this path
|
|
11
|
+
* shows in a browser (the viewer of the relay, `viewer/`), and Ta opens itself.
|
|
12
|
+
*/
|
|
13
|
+
export declare const RELAY_CONTENT_PATH = "/s";
|
|
14
|
+
/** The frame the viewer runs a shared HTML page in: a page of its own, so that it has its own CSP. */
|
|
15
|
+
export declare const RELAY_FRAME_PATH = "/s/frame";
|
|
16
|
+
/**
|
|
17
|
+
* The policy of the frame a shared page runs in (sandboxed, of an opaque origin): its own scripts, styles and
|
|
18
|
+
* pictures, from itself or the web, but no request of its own (`connect-src`), no form sent.
|
|
19
|
+
*/
|
|
20
|
+
export declare const RELAY_FRAME_CSP = "default-src 'none'; script-src 'unsafe-inline' 'unsafe-eval' https: data: blob:; style-src 'unsafe-inline' https: data:; img-src https: data: blob:; font-src https: data:; media-src https: data: blob:; connect-src 'none'; form-action 'none'";
|
|
21
|
+
/**
|
|
22
|
+
* The policy the relay answers the frame with: that of its document, framed by the viewer alone and sandboxed by
|
|
23
|
+
* the relay itself. The `sandbox` of the viewer's iframe is not enough: a page of another site framing it without
|
|
24
|
+
* one would run what it writes in under the relay's origin, where guests of rooms keep their keys. A meta element
|
|
25
|
+
* cannot say `sandbox` or `frame-ancestors`, so they are in the header only.
|
|
26
|
+
*/
|
|
27
|
+
export declare const RELAY_FRAME_HEADER_CSP = "default-src 'none'; script-src 'unsafe-inline' 'unsafe-eval' https: data: blob:; style-src 'unsafe-inline' https: data:; img-src https: data: blob:; font-src https: data:; media-src https: data: blob:; connect-src 'none'; form-action 'none'; frame-ancestors 'self'; sandbox allow-scripts allow-popups allow-popups-to-escape-sandbox";
|
|
28
|
+
/** Headers of the viewer and its frame: never framed elsewhere, never a referrer (the key is in the address). */
|
|
29
|
+
export declare const RELAY_VIEWER_HEADERS: {
|
|
30
|
+
readonly 'content-type': 'text/html; charset=utf-8';
|
|
31
|
+
readonly 'cache-control': 'no-cache';
|
|
32
|
+
readonly 'referrer-policy': 'no-referrer';
|
|
33
|
+
readonly 'x-content-type-options': 'nosniff';
|
|
34
|
+
};
|
|
35
|
+
/** The copies of content kept on the relay, sealed, for when the computer that shared it is off (RelayBlobs). */
|
|
36
|
+
export declare const RELAY_BLOB_PATH = "/v1/blob/";
|
|
9
37
|
/** The page a pairing link opens in a browser: the link is for the app, and the key in it stays in the fragment. */
|
|
10
38
|
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>";
|
|
11
39
|
/** The page a link of an extension opens in a browser. */
|
|
@@ -34,6 +62,35 @@ export declare const RELAY_SHARE_MAX_BYTES: number;
|
|
|
34
62
|
export declare const RELAY_SHARE_CHUNK_BYTES: number;
|
|
35
63
|
/** How long a link of a package works. */
|
|
36
64
|
export declare const RELAY_SHARE_DAYS = 7;
|
|
65
|
+
/**
|
|
66
|
+
* Content handed on through the relay (RelayContentUtil): the viewer proves first that it has the key (a sealed
|
|
67
|
+
* RELAY_PROOF, the host's `ready` having none), then the host sends what it is, its bytes in chunks and the end.
|
|
68
|
+
*/
|
|
69
|
+
export type RelayContentMessage = {
|
|
70
|
+
t: 'content';
|
|
71
|
+
kind: string;
|
|
72
|
+
title: string;
|
|
73
|
+
size: number;
|
|
74
|
+
createdAt: number;
|
|
75
|
+
type?: string;
|
|
76
|
+
} | {
|
|
77
|
+
t: 'chunk';
|
|
78
|
+
d: string;
|
|
79
|
+
} | {
|
|
80
|
+
t: 'end';
|
|
81
|
+
} | {
|
|
82
|
+
t: 'error';
|
|
83
|
+
message: string;
|
|
84
|
+
};
|
|
85
|
+
/** The largest content shared: a conversation without its images, a page, a file. */
|
|
86
|
+
export declare const RELAY_CONTENT_MAX_BYTES: number;
|
|
87
|
+
/** How long a copy stays on the relay at most. */
|
|
88
|
+
export declare const RELAY_BLOB_MAX_DAYS = 30;
|
|
89
|
+
/**
|
|
90
|
+
* Why a host refused a viewer, in the clear `refused` it sends before it closes: `key` the key (a password)
|
|
91
|
+
* is wrong, `busy` too many wrong tries of late, `gone` no such share (stopped, or past its time).
|
|
92
|
+
*/
|
|
93
|
+
export type RelayRefusal = 'key' | 'busy' | 'gone';
|
|
37
94
|
/** Hosts and devices: what the relay knows of them, and nothing it could read from. */
|
|
38
95
|
export declare const RELAY_ID: RegExp;
|
|
39
96
|
/** Close codes of the relay. */
|
|
@@ -89,15 +146,167 @@ export interface RelayHello {
|
|
|
89
146
|
}
|
|
90
147
|
/**
|
|
91
148
|
* The host's answer: its nonce, and a first sealed message that shows it has the device's key; `z` when
|
|
92
|
-
* both compress what they seal after the proof (the device offered it, and the host can).
|
|
149
|
+
* both compress what they seal after the proof (the device offered it, and the host can). Without `proof`
|
|
150
|
+
* (content shared, RelayContentUtil), the device proves first, with a sealed RELAY_PROOF.
|
|
93
151
|
*/
|
|
94
152
|
export type RelayWelcome = {
|
|
95
153
|
t: 'ready';
|
|
96
154
|
nonce: string;
|
|
97
|
-
proof
|
|
155
|
+
proof?: string;
|
|
98
156
|
z?: 1;
|
|
99
157
|
} | {
|
|
100
158
|
t: 'refused';
|
|
101
159
|
reason: string;
|
|
102
160
|
};
|
|
103
161
|
export declare const RELAY_PROOF = "ta-relay ready";
|
|
162
|
+
/**
|
|
163
|
+
* Rooms of Ta (docs/design/ta-link.md 3): a hub at the relay for each, through which its members send to each
|
|
164
|
+
* other and keep a shared state, all sealed with the key of the room, which the relay never has (RelayHub).
|
|
165
|
+
*/
|
|
166
|
+
export declare const RELAY_HUB_PATH = "/v1/hub/";
|
|
167
|
+
/** Links that invite into a room (RelayHubLink): Ta opens them; a browser gets the page of guests (src/guest). */
|
|
168
|
+
export declare const RELAY_ROOM_PATH = "/r";
|
|
169
|
+
/** Members of a room, its owner among them. */
|
|
170
|
+
export declare const RELAY_HUB_MAX_MEMBERS = 16;
|
|
171
|
+
/** Characters of a sealed message or value of the state. */
|
|
172
|
+
export declare const RELAY_HUB_MAX_VALUE: number;
|
|
173
|
+
export declare const RELAY_HUB_MAX_KEYS = 512;
|
|
174
|
+
/** Characters of all of the state, sealed. */
|
|
175
|
+
export declare const RELAY_HUB_MAX_STATE: number;
|
|
176
|
+
/** Asks to join waiting for the owner, and for how long. */
|
|
177
|
+
export declare const RELAY_HUB_MAX_ASKS = 20;
|
|
178
|
+
export declare const RELAY_HUB_ASK_DAYS = 7;
|
|
179
|
+
/** A room nobody used for so long is deleted. */
|
|
180
|
+
export declare const RELAY_HUB_IDLE_DAYS = 90;
|
|
181
|
+
/** Messages of a connection in a second. */
|
|
182
|
+
export declare const RELAY_HUB_RATE = 40;
|
|
183
|
+
/** The sealed list of the members of a room, signed by its owner; `v` grows with each change. */
|
|
184
|
+
export interface RelayHubRoster {
|
|
185
|
+
v: number;
|
|
186
|
+
d: string;
|
|
187
|
+
}
|
|
188
|
+
/** A key of the shared state: `k` the HMAC of its name, `v` its version (from 1), `d` sealed, `by` the member who wrote it last. */
|
|
189
|
+
export interface RelayHubEntry {
|
|
190
|
+
k: string;
|
|
191
|
+
v: number;
|
|
192
|
+
d: string;
|
|
193
|
+
by: string;
|
|
194
|
+
}
|
|
195
|
+
/** An ask to join, for the owner: `d` sealed with the key of the invitation. */
|
|
196
|
+
export interface RelayHubAsk {
|
|
197
|
+
ask: string;
|
|
198
|
+
d: string;
|
|
199
|
+
at: number;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* What a connection to a hub sends. The first message says who it is: the owner making the room (`create`), a
|
|
203
|
+
* member (`auth`), one asking to join (`ask`) or come back for the answer (`wait`); `member` and `ask` are ids
|
|
204
|
+
* of the room alone, `token` what the relay knows them by (it keeps its SHA-256).
|
|
205
|
+
*/
|
|
206
|
+
export type RelayHubRequest = {
|
|
207
|
+
t: 'create';
|
|
208
|
+
member: string;
|
|
209
|
+
token: string;
|
|
210
|
+
} | {
|
|
211
|
+
t: 'auth';
|
|
212
|
+
member: string;
|
|
213
|
+
token: string;
|
|
214
|
+
} | {
|
|
215
|
+
t: 'ask';
|
|
216
|
+
ask: string;
|
|
217
|
+
token: string;
|
|
218
|
+
proof: string;
|
|
219
|
+
d: string;
|
|
220
|
+
} | {
|
|
221
|
+
t: 'wait';
|
|
222
|
+
ask: string;
|
|
223
|
+
token: string;
|
|
224
|
+
} | {
|
|
225
|
+
t: 'send';
|
|
226
|
+
d: string;
|
|
227
|
+
to?: string;
|
|
228
|
+
} | {
|
|
229
|
+
t: 'set';
|
|
230
|
+
id: number;
|
|
231
|
+
k: string;
|
|
232
|
+
d: string;
|
|
233
|
+
v?: number;
|
|
234
|
+
} | {
|
|
235
|
+
t: 'invite';
|
|
236
|
+
hash: string;
|
|
237
|
+
expiresAt: number;
|
|
238
|
+
} | {
|
|
239
|
+
t: 'admit';
|
|
240
|
+
ask: string;
|
|
241
|
+
answer: string;
|
|
242
|
+
roster: RelayHubRoster;
|
|
243
|
+
} | {
|
|
244
|
+
t: 'refuse';
|
|
245
|
+
ask: string;
|
|
246
|
+
} | {
|
|
247
|
+
t: 'roster';
|
|
248
|
+
roster: RelayHubRoster;
|
|
249
|
+
} | {
|
|
250
|
+
t: 'remove';
|
|
251
|
+
member: string;
|
|
252
|
+
roster: RelayHubRoster;
|
|
253
|
+
} | {
|
|
254
|
+
t: 'leave';
|
|
255
|
+
} | {
|
|
256
|
+
t: 'close';
|
|
257
|
+
};
|
|
258
|
+
/** Why the hub refused: `auth` not a member, `gone` no such room, `invite` the invitation is wrong or ran out. */
|
|
259
|
+
export type RelayHubFailure = 'auth' | 'gone' | 'invite' | 'full' | 'large' | 'limit' | 'rate' | 'owner' | 'exists' | 'invalid';
|
|
260
|
+
/**
|
|
261
|
+
* What a hub sends. A member is first `welcome`d (who is online, the roster, asks for the owner), then given the
|
|
262
|
+
* state in `entries` and `synced`.
|
|
263
|
+
*/
|
|
264
|
+
export type RelayHubEvent = {
|
|
265
|
+
t: 'welcome';
|
|
266
|
+
owner: string;
|
|
267
|
+
online: string[];
|
|
268
|
+
roster?: RelayHubRoster;
|
|
269
|
+
asks?: RelayHubAsk[];
|
|
270
|
+
} | {
|
|
271
|
+
t: 'entries';
|
|
272
|
+
entries: RelayHubEntry[];
|
|
273
|
+
} | {
|
|
274
|
+
t: 'synced';
|
|
275
|
+
} | {
|
|
276
|
+
t: 'msg';
|
|
277
|
+
from: string;
|
|
278
|
+
d: string;
|
|
279
|
+
} | {
|
|
280
|
+
t: 'state';
|
|
281
|
+
entry: RelayHubEntry;
|
|
282
|
+
} | {
|
|
283
|
+
t: 'done';
|
|
284
|
+
id: number;
|
|
285
|
+
ok: boolean;
|
|
286
|
+
v: number;
|
|
287
|
+
error?: RelayHubFailure;
|
|
288
|
+
} | {
|
|
289
|
+
t: 'presence';
|
|
290
|
+
member: string;
|
|
291
|
+
online: boolean;
|
|
292
|
+
} | {
|
|
293
|
+
t: 'roster';
|
|
294
|
+
roster: RelayHubRoster;
|
|
295
|
+
} | {
|
|
296
|
+
t: 'asked';
|
|
297
|
+
ask: RelayHubAsk;
|
|
298
|
+
} | {
|
|
299
|
+
t: 'waiting';
|
|
300
|
+
} | {
|
|
301
|
+
t: 'admitted';
|
|
302
|
+
answer: string;
|
|
303
|
+
} | {
|
|
304
|
+
t: 'refused';
|
|
305
|
+
} | {
|
|
306
|
+
t: 'left';
|
|
307
|
+
member: string;
|
|
308
|
+
} | {
|
|
309
|
+
t: 'error';
|
|
310
|
+
code: RelayHubFailure;
|
|
311
|
+
message: string;
|
|
312
|
+
};
|
|
@@ -6,6 +6,34 @@ export const RELAY_CONNECT_PATH = '/v1/connect/';
|
|
|
6
6
|
export const RELAY_PAIR_PATH = '/pair';
|
|
7
7
|
/** A link of an extension handed on: its package comes from the computer of the one who shared it, through the relay. */
|
|
8
8
|
export const RELAY_SHARE_PATH = '/share';
|
|
9
|
+
/**
|
|
10
|
+
* Links of content shared (RelayContentUtil): a conversation, a page, a file, which the page of this path
|
|
11
|
+
* shows in a browser (the viewer of the relay, `viewer/`), and Ta opens itself.
|
|
12
|
+
*/
|
|
13
|
+
export const RELAY_CONTENT_PATH = '/s';
|
|
14
|
+
/** The frame the viewer runs a shared HTML page in: a page of its own, so that it has its own CSP. */
|
|
15
|
+
export const RELAY_FRAME_PATH = '/s/frame';
|
|
16
|
+
/**
|
|
17
|
+
* The policy of the frame a shared page runs in (sandboxed, of an opaque origin): its own scripts, styles and
|
|
18
|
+
* pictures, from itself or the web, but no request of its own (`connect-src`), no form sent.
|
|
19
|
+
*/
|
|
20
|
+
export const RELAY_FRAME_CSP = "default-src 'none'; script-src 'unsafe-inline' 'unsafe-eval' https: data: blob:; style-src 'unsafe-inline' https: data:; img-src https: data: blob:; font-src https: data:; media-src https: data: blob:; connect-src 'none'; form-action 'none'";
|
|
21
|
+
/**
|
|
22
|
+
* The policy the relay answers the frame with: that of its document, framed by the viewer alone and sandboxed by
|
|
23
|
+
* the relay itself. The `sandbox` of the viewer's iframe is not enough: a page of another site framing it without
|
|
24
|
+
* one would run what it writes in under the relay's origin, where guests of rooms keep their keys. A meta element
|
|
25
|
+
* cannot say `sandbox` or `frame-ancestors`, so they are in the header only.
|
|
26
|
+
*/
|
|
27
|
+
export const RELAY_FRAME_HEADER_CSP = `${RELAY_FRAME_CSP}; frame-ancestors 'self'; sandbox allow-scripts allow-popups allow-popups-to-escape-sandbox`;
|
|
28
|
+
/** Headers of the viewer and its frame: never framed elsewhere, never a referrer (the key is in the address). */
|
|
29
|
+
export const RELAY_VIEWER_HEADERS = {
|
|
30
|
+
'content-type': 'text/html; charset=utf-8',
|
|
31
|
+
'cache-control': 'no-cache',
|
|
32
|
+
'referrer-policy': 'no-referrer',
|
|
33
|
+
'x-content-type-options': 'nosniff',
|
|
34
|
+
};
|
|
35
|
+
/** The copies of content kept on the relay, sealed, for when the computer that shared it is off (RelayBlobs). */
|
|
36
|
+
export const RELAY_BLOB_PATH = '/v1/blob/';
|
|
9
37
|
/** The page a pairing link opens in a browser: the link is for the app, and the key in it stays in the fragment. */
|
|
10
38
|
export const RELAY_PAIR_PAGE = `<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width"><title>Ta</title>
|
|
11
39
|
<body style="font:16px system-ui;max-width:32em;margin:3em auto;padding:0 1em">
|
|
@@ -22,6 +50,10 @@ export const RELAY_SHARE_MAX_BYTES = 8 * 1024 * 1024;
|
|
|
22
50
|
export const RELAY_SHARE_CHUNK_BYTES = 192 * 1024;
|
|
23
51
|
/** How long a link of a package works. */
|
|
24
52
|
export const RELAY_SHARE_DAYS = 7;
|
|
53
|
+
/** The largest content shared: a conversation without its images, a page, a file. */
|
|
54
|
+
export const RELAY_CONTENT_MAX_BYTES = 8 * 1024 * 1024;
|
|
55
|
+
/** How long a copy stays on the relay at most. */
|
|
56
|
+
export const RELAY_BLOB_MAX_DAYS = 30;
|
|
25
57
|
/** Hosts and devices: what the relay knows of them, and nothing it could read from. */
|
|
26
58
|
export const RELAY_ID = /^[A-Za-z0-9_-]{16,64}$/;
|
|
27
59
|
/** Close codes of the relay. */
|
|
@@ -52,4 +84,25 @@ export const RELAY_PONG = 'pong';
|
|
|
52
84
|
export const RELAY_PING_MS = 25000;
|
|
53
85
|
export const RELAY_STALE_MS = 80000;
|
|
54
86
|
export const RELAY_PROOF = 'ta-relay ready';
|
|
87
|
+
/**
|
|
88
|
+
* Rooms of Ta (docs/design/ta-link.md 3): a hub at the relay for each, through which its members send to each
|
|
89
|
+
* other and keep a shared state, all sealed with the key of the room, which the relay never has (RelayHub).
|
|
90
|
+
*/
|
|
91
|
+
export const RELAY_HUB_PATH = '/v1/hub/';
|
|
92
|
+
/** Links that invite into a room (RelayHubLink): Ta opens them; a browser gets the page of guests (src/guest). */
|
|
93
|
+
export const RELAY_ROOM_PATH = '/r';
|
|
94
|
+
/** Members of a room, its owner among them. */
|
|
95
|
+
export const RELAY_HUB_MAX_MEMBERS = 16;
|
|
96
|
+
/** Characters of a sealed message or value of the state. */
|
|
97
|
+
export const RELAY_HUB_MAX_VALUE = 96 * 1024;
|
|
98
|
+
export const RELAY_HUB_MAX_KEYS = 512;
|
|
99
|
+
/** Characters of all of the state, sealed. */
|
|
100
|
+
export const RELAY_HUB_MAX_STATE = 3 * 1024 * 1024;
|
|
101
|
+
/** Asks to join waiting for the owner, and for how long. */
|
|
102
|
+
export const RELAY_HUB_MAX_ASKS = 20;
|
|
103
|
+
export const RELAY_HUB_ASK_DAYS = 7;
|
|
104
|
+
/** A room nobody used for so long is deleted. */
|
|
105
|
+
export const RELAY_HUB_IDLE_DAYS = 90;
|
|
106
|
+
/** Messages of a connection in a second. */
|
|
107
|
+
export const RELAY_HUB_RATE = 40;
|
|
55
108
|
//# sourceMappingURL=relay-protocol.js.map
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/** A value of the shared state of a room, as RelayHubs and TaRoomServer read it. */
|
|
2
|
+
export interface RelayRoomChatValue {
|
|
3
|
+
value: unknown;
|
|
4
|
+
version: number;
|
|
5
|
+
by: string;
|
|
6
|
+
}
|
|
7
|
+
/** What a chat reads and sets: RelayHubs, or TaRoomServer of Ta. */
|
|
8
|
+
export interface RelayRoomChatRooms {
|
|
9
|
+
getState(room: string, prefix?: string): Promise<Record<string, RelayRoomChatValue>>;
|
|
10
|
+
setState(room: string, key: string, value: unknown, version?: number): Promise<{
|
|
11
|
+
ok: boolean;
|
|
12
|
+
version: number;
|
|
13
|
+
}>;
|
|
14
|
+
}
|
|
15
|
+
/** One thing said in a room. */
|
|
16
|
+
export interface RelayChatLine {
|
|
17
|
+
id: string;
|
|
18
|
+
/** The member who said it: that of the key it is under, never what the value says. */
|
|
19
|
+
from: string;
|
|
20
|
+
/** Its name as it said it, for one who left since. */
|
|
21
|
+
name: string;
|
|
22
|
+
text: string;
|
|
23
|
+
at: number;
|
|
24
|
+
}
|
|
25
|
+
/** What is kept of a line: `from` is the key's. */
|
|
26
|
+
type StoredLine = Omit<RelayChatLine, 'from'>;
|
|
27
|
+
/** The keys of what is said, `chat:<member>:<n>`: each member writes only its own, so none can write as another. */
|
|
28
|
+
export declare const RELAY_CHAT_KEY = "chat:";
|
|
29
|
+
/** The bytes of JSON of one part at most, well under what the relay takes of a value once sealed. */
|
|
30
|
+
export declare const RELAY_CHAT_PART_BYTES: number;
|
|
31
|
+
/** The parts of each member kept: the oldest is cleared as a new one starts. */
|
|
32
|
+
export declare const RELAY_CHAT_PARTS = 6;
|
|
33
|
+
export declare const RELAY_CHAT_TEXT_MAX = 2000;
|
|
34
|
+
export declare namespace RelayRoomChatUtil {
|
|
35
|
+
function key(member: string, part: number): string;
|
|
36
|
+
function parse(key: string): {
|
|
37
|
+
member: string;
|
|
38
|
+
part: number;
|
|
39
|
+
} | undefined;
|
|
40
|
+
/** The lines of a part, if its value is one and was written by the member of its key. */
|
|
41
|
+
function lines(member: string, value: unknown, by: string): RelayChatLine[] | undefined;
|
|
42
|
+
function stored(lines: RelayChatLine[]): StoredLine[];
|
|
43
|
+
function bytes(lines: StoredLine[]): number;
|
|
44
|
+
function id(): string;
|
|
45
|
+
}
|
|
46
|
+
interface Part {
|
|
47
|
+
member: string;
|
|
48
|
+
part: number;
|
|
49
|
+
lines: RelayChatLine[];
|
|
50
|
+
version: number;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The group chat of a room (docs/design/ta-link.md 3.7; the Rooms page of Ta and the page of guests), in its shared state: what each member said is in parts of its
|
|
54
|
+
* own (`chat:<member>:<n>`), the latest added to on its version, a new one once it is full, the oldest cleared beyond
|
|
55
|
+
* {@link RELAY_CHAT_PARTS}. So it is there for whoever comes later, sealed as all of the state, and nobody writes as another.
|
|
56
|
+
*/
|
|
57
|
+
export declare class RelayRoomChat {
|
|
58
|
+
protected readonly rooms: RelayRoomChatRooms;
|
|
59
|
+
readonly room: string;
|
|
60
|
+
protected readonly parts: Map<string, Part>;
|
|
61
|
+
protected readonly listeners: Set<() => void>;
|
|
62
|
+
protected all: readonly RelayChatLine[];
|
|
63
|
+
constructor(rooms: RelayRoomChatRooms, room: string);
|
|
64
|
+
/** All that is said, the earliest first. */
|
|
65
|
+
get lines(): readonly RelayChatLine[];
|
|
66
|
+
subscribe(listener: () => void): () => void;
|
|
67
|
+
load(): Promise<void>;
|
|
68
|
+
/** A key of the room set, as its event tells: whether it was of the chat. */
|
|
69
|
+
heard(key: string, value: RelayRoomChatValue): boolean;
|
|
70
|
+
/** Says `text` as `self` (its member id and name); set again on what another client of the same Ta wrote first. */
|
|
71
|
+
say(text: string, self: {
|
|
72
|
+
member: string;
|
|
73
|
+
name: string;
|
|
74
|
+
}): Promise<void>;
|
|
75
|
+
protected clear(key: string): Promise<void>;
|
|
76
|
+
protected latest(member: string): Part | undefined;
|
|
77
|
+
protected put(key: string, value: RelayRoomChatValue): void;
|
|
78
|
+
protected changed(): void;
|
|
79
|
+
}
|
|
80
|
+
export {};
|