@celljs/ta-relay 4.10.0 → 4.12.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 +11 -0
- package/esm/common/index.js +11 -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-faces.d.ts +32 -0
- package/esm/common/relay-faces.js +208 -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-connection.d.ts +98 -0
- package/esm/common/relay-hubs-connection.js +505 -0
- package/esm/common/relay-hubs-protocol.d.ts +130 -0
- package/esm/common/relay-hubs-protocol.js +2 -0
- package/esm/common/relay-hubs.d.ts +61 -0
- package/esm/common/relay-hubs.js +351 -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 +109 -0
- package/esm/common/relay-room-chat.js +176 -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 +50 -0
- package/esm/guest/guest-i18n.js +103 -0
- package/esm/guest/guest-style.d.ts +1 -0
- package/esm/guest/guest-style.js +85 -0
- package/esm/guest/guest.d.ts +1 -0
- package/esm/guest/guest.js +384 -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 +11 -0
- package/lib/common/index.js +11 -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-faces.d.ts +33 -0
- package/lib/common/relay-faces.js +211 -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-connection.d.ts +99 -0
- package/lib/common/relay-hubs-connection.js +510 -0
- package/lib/common/relay-hubs-protocol.d.ts +131 -0
- package/lib/common/relay-hubs-protocol.js +3 -0
- package/lib/common/relay-hubs.d.ts +62 -0
- package/lib/common/relay-hubs.js +355 -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 +110 -0
- package/lib/common/relay-room-chat.js +180 -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 +51 -0
- package/lib/guest/guest-i18n.js +106 -0
- package/lib/guest/guest-style.d.ts +2 -0
- package/lib/guest/guest-style.js +88 -0
- package/lib/guest/guest.d.ts +2 -0
- package/lib/guest/guest.js +386 -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 +94 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RelayBlobClient = exports.RelayBlobs = void 0;
|
|
4
|
+
const relay_bytes_1 = require("./relay-bytes");
|
|
5
|
+
const relay_protocol_1 = require("./relay-protocol");
|
|
6
|
+
/** Below the 2 MiB of a value of a Durable Object. */
|
|
7
|
+
const CHUNK_BYTES = 1024 * 1024;
|
|
8
|
+
/** The sealing adds an IV, a tag and the header of the content. */
|
|
9
|
+
const MAX_BYTES = relay_protocol_1.RELAY_CONTENT_MAX_BYTES + 64 * 1024;
|
|
10
|
+
const FAIL_WINDOW_MS = 10 * 60 * 1000;
|
|
11
|
+
const MAX_FAILS = 20;
|
|
12
|
+
const DAY_MS = 24 * 60 * 60 * 1000;
|
|
13
|
+
const READ_HEADERS = { 'access-control-allow-origin': '*', 'access-control-allow-headers': 'x-ta-auth', 'access-control-allow-methods': 'GET, OPTIONS' };
|
|
14
|
+
/**
|
|
15
|
+
* One copy of content shared, sealed, on the relay (docs/design/ta-link.md 2.5): the host that shared it puts and
|
|
16
|
+
* deletes it with its token; a viewer reads it with the token of its key. The relay can read neither the copy
|
|
17
|
+
* nor what opens it. Runs the same in a Durable Object (one for each share) and in `ta relay`.
|
|
18
|
+
*/
|
|
19
|
+
class RelayBlobs {
|
|
20
|
+
constructor(storage, now = Date.now) {
|
|
21
|
+
this.storage = storage;
|
|
22
|
+
this.now = now;
|
|
23
|
+
}
|
|
24
|
+
async handle(request) {
|
|
25
|
+
switch (request.method) {
|
|
26
|
+
case 'OPTIONS':
|
|
27
|
+
return new Response(null, { status: 204, headers: READ_HEADERS });
|
|
28
|
+
case 'GET':
|
|
29
|
+
return this.read(request);
|
|
30
|
+
case 'PUT':
|
|
31
|
+
return this.write(request);
|
|
32
|
+
case 'DELETE':
|
|
33
|
+
return this.remove(request);
|
|
34
|
+
default:
|
|
35
|
+
return RelayBlobs.answer(405, 'Not allowed.');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async read(request) {
|
|
39
|
+
var _a, _b;
|
|
40
|
+
const meta = await this.meta();
|
|
41
|
+
if (!meta) {
|
|
42
|
+
return RelayBlobs.answer(404, 'No copy.', READ_HEADERS);
|
|
43
|
+
}
|
|
44
|
+
const fails = (_a = (await this.storage.get('fails'))) !== null && _a !== void 0 ? _a : { count: 0, since: 0 };
|
|
45
|
+
const now = this.now();
|
|
46
|
+
const recent = now - fails.since < FAIL_WINDOW_MS ? fails : { count: 0, since: now };
|
|
47
|
+
if (recent.count >= MAX_FAILS) {
|
|
48
|
+
return RelayBlobs.answer(429, 'Too many tries.', READ_HEADERS);
|
|
49
|
+
}
|
|
50
|
+
if ((await RelayBlobs.hash((_b = request.headers.get('x-ta-auth')) !== null && _b !== void 0 ? _b : '')) !== meta.authHash) {
|
|
51
|
+
await this.storage.put('fails', { count: recent.count + 1, since: recent.since });
|
|
52
|
+
return RelayBlobs.answer(403, 'Refused.', READ_HEADERS);
|
|
53
|
+
}
|
|
54
|
+
const parts = [];
|
|
55
|
+
for (let index = 0; index < meta.chunks; index++) {
|
|
56
|
+
const part = await this.storage.get(`c${index}`);
|
|
57
|
+
if (!part) {
|
|
58
|
+
return RelayBlobs.answer(404, 'No copy.', READ_HEADERS);
|
|
59
|
+
}
|
|
60
|
+
parts.push(new Uint8Array(part));
|
|
61
|
+
}
|
|
62
|
+
return new Response(relay_bytes_1.RelayBytes.concat(...parts), {
|
|
63
|
+
headers: { ...READ_HEADERS, 'content-type': 'application/octet-stream', 'cache-control': 'no-store' },
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
async write(request) {
|
|
67
|
+
var _a, _b;
|
|
68
|
+
var _c;
|
|
69
|
+
const writeHash = await RelayBlobs.hash(RelayBlobs.bearer(request));
|
|
70
|
+
const before = await this.storage.get('meta');
|
|
71
|
+
if (before && before.writeHash !== writeHash && before.expiresAt > this.now()) {
|
|
72
|
+
return RelayBlobs.answer(403, 'Another host keeps this copy.');
|
|
73
|
+
}
|
|
74
|
+
const authHash = (_c = request.headers.get('x-ta-auth-hash')) !== null && _c !== void 0 ? _c : '';
|
|
75
|
+
const expiresAt = Number(request.headers.get('x-ta-expires'));
|
|
76
|
+
const now = this.now();
|
|
77
|
+
if (RelayBlobs.bearer(request).length < 32 ||
|
|
78
|
+
!/^[A-Za-z0-9_-]{43}$/.test(authHash) ||
|
|
79
|
+
!(expiresAt > now) ||
|
|
80
|
+
expiresAt > now + relay_protocol_1.RELAY_BLOB_MAX_DAYS * DAY_MS + DAY_MS) {
|
|
81
|
+
return RelayBlobs.answer(400, 'A token, the hash of what reads it and when it runs out are needed.');
|
|
82
|
+
}
|
|
83
|
+
const length = Number(request.headers.get('content-length'));
|
|
84
|
+
if (length > MAX_BYTES) {
|
|
85
|
+
return RelayBlobs.answer(413, 'Too large.');
|
|
86
|
+
}
|
|
87
|
+
const bytes = new Uint8Array(await request.arrayBuffer());
|
|
88
|
+
if (bytes.length === 0 || bytes.length > MAX_BYTES) {
|
|
89
|
+
return RelayBlobs.answer(bytes.length === 0 ? 400 : 413, bytes.length === 0 ? 'Empty.' : 'Too large.');
|
|
90
|
+
}
|
|
91
|
+
await this.storage.deleteAll();
|
|
92
|
+
const chunks = Math.ceil(bytes.length / CHUNK_BYTES);
|
|
93
|
+
for (let index = 0; index < chunks; index++) {
|
|
94
|
+
await this.storage.put(`c${index}`, bytes.slice(index * CHUNK_BYTES, (index + 1) * CHUNK_BYTES));
|
|
95
|
+
}
|
|
96
|
+
await this.storage.put('meta', { writeHash, authHash, expiresAt, size: bytes.length, chunks });
|
|
97
|
+
await ((_b = (_a = this.storage).setAlarm) === null || _b === void 0 ? void 0 : _b.call(_a, expiresAt));
|
|
98
|
+
return RelayBlobs.answer(201, 'Kept.');
|
|
99
|
+
}
|
|
100
|
+
async remove(request) {
|
|
101
|
+
const meta = await this.storage.get('meta');
|
|
102
|
+
if (meta && meta.writeHash !== (await RelayBlobs.hash(RelayBlobs.bearer(request)))) {
|
|
103
|
+
return RelayBlobs.answer(403, 'Refused.');
|
|
104
|
+
}
|
|
105
|
+
await this.storage.deleteAll();
|
|
106
|
+
return RelayBlobs.answer(200, 'Deleted.');
|
|
107
|
+
}
|
|
108
|
+
/** The copy while it lasts; past its time it is deleted, should the alarm not have run. */
|
|
109
|
+
async meta() {
|
|
110
|
+
const meta = await this.storage.get('meta');
|
|
111
|
+
if (meta && meta.expiresAt <= this.now()) {
|
|
112
|
+
await this.storage.deleteAll();
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
return meta;
|
|
116
|
+
}
|
|
117
|
+
static bearer(request) {
|
|
118
|
+
var _a;
|
|
119
|
+
var _b, _c;
|
|
120
|
+
return (_c = (_a = /^Bearer (\S+)$/.exec((_b = request.headers.get('authorization')) !== null && _b !== void 0 ? _b : '')) === null || _a === void 0 ? void 0 : _a[1]) !== null && _c !== void 0 ? _c : '';
|
|
121
|
+
}
|
|
122
|
+
static async hash(text) {
|
|
123
|
+
const digest = await globalThis.crypto.subtle.digest('SHA-256', new TextEncoder().encode(text));
|
|
124
|
+
return relay_bytes_1.RelayBytes.toBase64(new Uint8Array(digest));
|
|
125
|
+
}
|
|
126
|
+
static answer(status, text, headers = {}) {
|
|
127
|
+
return new Response(`${text}\n`, { status, headers: { ...headers, 'content-type': 'text/plain; charset=utf-8' } });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
exports.RelayBlobs = RelayBlobs;
|
|
131
|
+
/** The bytes of a copy at most, sealed. */
|
|
132
|
+
RelayBlobs.MAX_BYTES = MAX_BYTES;
|
|
133
|
+
/** What the host that shared content does with its copy on the relay. */
|
|
134
|
+
var RelayBlobClient;
|
|
135
|
+
(function (RelayBlobClient) {
|
|
136
|
+
async function put(copy) {
|
|
137
|
+
const response = await fetch(`${copy.relay}${relay_protocol_1.RELAY_BLOB_PATH}${copy.share}`, {
|
|
138
|
+
method: 'PUT',
|
|
139
|
+
headers: {
|
|
140
|
+
authorization: `Bearer ${copy.token}`,
|
|
141
|
+
'x-ta-auth-hash': copy.authHash,
|
|
142
|
+
'x-ta-expires': String(copy.expiresAt),
|
|
143
|
+
'content-type': 'application/octet-stream',
|
|
144
|
+
},
|
|
145
|
+
body: copy.bytes,
|
|
146
|
+
});
|
|
147
|
+
if (!response.ok) {
|
|
148
|
+
throw new Error(`The relay did not keep the copy: ${response.status} ${(await response.text()).trim()}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
RelayBlobClient.put = put;
|
|
152
|
+
async function remove(relay, share, token) {
|
|
153
|
+
await fetch(`${relay}${relay_protocol_1.RELAY_BLOB_PATH}${share}`, { method: 'DELETE', headers: { authorization: `Bearer ${token}` } });
|
|
154
|
+
}
|
|
155
|
+
RelayBlobClient.remove = remove;
|
|
156
|
+
})(RelayBlobClient || (exports.RelayBlobClient = RelayBlobClient = {}));
|
|
157
|
+
//# sourceMappingURL=relay-blobs.js.map
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** The kind of an agent's card shared by a link (docs/design/ta-social.md): JSON of {@link RelayAgentCard}. */
|
|
2
|
+
export declare const RELAY_AGENT_KIND = "ta-agent";
|
|
3
|
+
/**
|
|
4
|
+
* An agent of Ta as its card shares it: who it is and its role, what its secrets taken out (Redactor), not where it
|
|
5
|
+
* works nor what it said. The Ta that adds it fits the role to the project it is added for.
|
|
6
|
+
*/
|
|
7
|
+
export interface RelayAgentCard {
|
|
8
|
+
v: 1;
|
|
9
|
+
name: string;
|
|
10
|
+
role: string;
|
|
11
|
+
/** One or two characters on its avatar. */
|
|
12
|
+
mark: string;
|
|
13
|
+
/** Which of the faces of ta-channels it has. */
|
|
14
|
+
face?: number;
|
|
15
|
+
/** The colour of its face, for a page that does not draw the faces (the viewer of the relay). */
|
|
16
|
+
color?: string;
|
|
17
|
+
/** How many places of the role were hidden as secrets. */
|
|
18
|
+
redacted?: number;
|
|
19
|
+
}
|
|
20
|
+
export declare namespace RelayAgentCards {
|
|
21
|
+
/** A card read from what a link shares; undefined for what is none. */
|
|
22
|
+
function parse(data: Uint8Array | string): RelayAgentCard | undefined;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=relay-card.d.ts.map
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RelayAgentCards = exports.RELAY_AGENT_KIND = void 0;
|
|
4
|
+
/** The kind of an agent's card shared by a link (docs/design/ta-social.md): JSON of {@link RelayAgentCard}. */
|
|
5
|
+
exports.RELAY_AGENT_KIND = 'ta-agent';
|
|
6
|
+
var RelayAgentCards;
|
|
7
|
+
(function (RelayAgentCards) {
|
|
8
|
+
const MAX_ROLE = 8000;
|
|
9
|
+
/** A card read from what a link shares; undefined for what is none. */
|
|
10
|
+
function parse(data) {
|
|
11
|
+
try {
|
|
12
|
+
const card = JSON.parse(typeof data === 'string' ? data : new TextDecoder().decode(data));
|
|
13
|
+
if ((card === null || card === void 0 ? void 0 : card.v) !== 1 || typeof card.name !== 'string' || !card.name.trim() || typeof card.role !== 'string' || !card.role.trim()) {
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
const color = typeof card.color === 'string' && /^#[0-9a-fA-F]{6}$/.test(card.color) ? card.color : undefined;
|
|
17
|
+
return {
|
|
18
|
+
v: 1,
|
|
19
|
+
name: card.name.trim().slice(0, 32),
|
|
20
|
+
role: card.role.trim().slice(0, MAX_ROLE),
|
|
21
|
+
mark: typeof card.mark === 'string' && card.mark.trim() ? card.mark.trim().slice(0, 2) : card.name.trim().slice(0, 1).toUpperCase(),
|
|
22
|
+
...(Number.isInteger(card.face) ? { face: card.face } : {}),
|
|
23
|
+
...(color ? { color } : {}),
|
|
24
|
+
...(typeof card.redacted === 'number' && card.redacted > 0 ? { redacted: card.redacted } : {}),
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
catch (_a) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
RelayAgentCards.parse = parse;
|
|
32
|
+
})(RelayAgentCards || (exports.RelayAgentCards = RelayAgentCards = {}));
|
|
33
|
+
//# sourceMappingURL=relay-card.js.map
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { type RelayIdentity } from './relay-identity';
|
|
2
|
+
/**
|
|
3
|
+
* Who may open a link of content: anyone with it, anyone with it and its password, the one who shared it, or Ta friends
|
|
4
|
+
* the one who shared it chose (`people`), in their own Ta (docs/design/ta-link.md 3.6).
|
|
5
|
+
*/
|
|
6
|
+
export type RelayContentAccess = 'link' | 'password' | 'self' | 'people';
|
|
7
|
+
/** What a link of content shared names. The key is in the fragment, which a browser does not send. */
|
|
8
|
+
export interface RelayContentLink {
|
|
9
|
+
relay: string;
|
|
10
|
+
host: string;
|
|
11
|
+
/** The id of the share, as a device's on the relay. */
|
|
12
|
+
share: string;
|
|
13
|
+
/** base64url, 32 bytes; none for `self` and `people`. */
|
|
14
|
+
key?: string;
|
|
15
|
+
access: RelayContentAccess;
|
|
16
|
+
/** For `people`: the identity of the one who shared it, whose key the key of a friend is agreed with. */
|
|
17
|
+
owner?: string;
|
|
18
|
+
/** A copy is kept on the relay, sealed: it opens while the computer that shared it is off. */
|
|
19
|
+
offline?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* What it shares, in the link (`c`) for kinds other than a conversation or a file (an agent's card), so that the
|
|
22
|
+
* package that opens it claims the link before it is fetched. Not secret.
|
|
23
|
+
*/
|
|
24
|
+
kind?: string;
|
|
25
|
+
}
|
|
26
|
+
/** What is shared: a conversation (`ta-conversation`, JSON), a file or a page (its bytes). */
|
|
27
|
+
export interface RelayContentHeader {
|
|
28
|
+
kind: string;
|
|
29
|
+
title: string;
|
|
30
|
+
createdAt: number;
|
|
31
|
+
/** The media type of a file. */
|
|
32
|
+
type?: string;
|
|
33
|
+
}
|
|
34
|
+
export interface RelayContentItem extends RelayContentHeader {
|
|
35
|
+
data: Uint8Array;
|
|
36
|
+
}
|
|
37
|
+
export type RelayContentFailure =
|
|
38
|
+
/** A password is needed, or the one given is wrong (`wrong`). */
|
|
39
|
+
'password' | 'wrong'
|
|
40
|
+
/** Too many wrong passwords of late: try again later. */
|
|
41
|
+
| 'busy'
|
|
42
|
+
/** Stopped, or past its time. */
|
|
43
|
+
| 'gone'
|
|
44
|
+
/** The computer that shared it does not run Ta now, and there is no copy on the relay. */
|
|
45
|
+
| 'offline'
|
|
46
|
+
/** Only the one who shared it opens it, in their own Ta. */
|
|
47
|
+
| 'self'
|
|
48
|
+
/** Only the Ta friends it was shared with open it, in their own Ta. */
|
|
49
|
+
| 'people' | 'invalid' | 'timeout' | 'large';
|
|
50
|
+
export declare class RelayContentError extends Error {
|
|
51
|
+
readonly reason: RelayContentFailure;
|
|
52
|
+
constructor(reason: RelayContentFailure, message: string);
|
|
53
|
+
}
|
|
54
|
+
export interface RelayContentFetchOptions {
|
|
55
|
+
password?: string;
|
|
56
|
+
/** For `people`: this Ta, and the key the one who shared it agrees keys with (of its friends). */
|
|
57
|
+
people?: {
|
|
58
|
+
identity: RelayIdentity;
|
|
59
|
+
ownerBox: string;
|
|
60
|
+
};
|
|
61
|
+
maxBytes?: number;
|
|
62
|
+
/** For all of it. Default 60 seconds. */
|
|
63
|
+
timeoutMs?: number;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Content shared by a link (docs/design/ta-link.md): its link, its keys, and how it is fetched, from the
|
|
67
|
+
* computer that shared it through the relay, or from the copy the relay keeps sealed. Runs the same in a
|
|
68
|
+
* browser (the viewer of the relay), in ta serve and in a Worker: WebCrypto, fetch and WebSocket only.
|
|
69
|
+
*/
|
|
70
|
+
export declare namespace RelayContentUtil {
|
|
71
|
+
/**
|
|
72
|
+
* `https://<relay>/s#v=1&h=<host>&s=<share>&k=<key>`, with `p=1` for a password, `m=self`, `o=1` for a copy on the relay,
|
|
73
|
+
* `c=<kind>` for what is no conversation or file.
|
|
74
|
+
*/
|
|
75
|
+
function link(content: RelayContentLink): string;
|
|
76
|
+
function is(text: string): boolean;
|
|
77
|
+
/** Throws on what is not a link of {@link link}. */
|
|
78
|
+
function parse(text: string): RelayContentLink;
|
|
79
|
+
/**
|
|
80
|
+
* The key the content is sealed with: the key of the link, or with a password, one derived from both
|
|
81
|
+
* (PBKDF2 of the password, then HKDF with the key of the link), so that neither alone opens it.
|
|
82
|
+
*/
|
|
83
|
+
function contentKey(linkKey: Uint8Array, share: string, password?: string): Promise<Uint8Array>;
|
|
84
|
+
/**
|
|
85
|
+
* The key of content shared with a Ta friend (`people`): only the two make it, each with its own private key and the
|
|
86
|
+
* other's public one, for this share alone.
|
|
87
|
+
*/
|
|
88
|
+
function peopleKey(identity: RelayIdentity, otherBox: string, share: string): Promise<Uint8Array>;
|
|
89
|
+
/** What reads the copy on the relay, which keeps only its SHA-256 (`x-ta-auth`). */
|
|
90
|
+
function authToken(key: Uint8Array): Promise<string>;
|
|
91
|
+
function sha256(text: string): Promise<string>;
|
|
92
|
+
/** The bytes of the copy on the relay: a random IV, then AES-256-GCM of {@link encode}. */
|
|
93
|
+
function seal(key: Uint8Array, item: RelayContentItem): Promise<Uint8Array>;
|
|
94
|
+
/** Throws when it was not sealed with this key. */
|
|
95
|
+
function open(key: Uint8Array, bytes: Uint8Array): Promise<RelayContentItem>;
|
|
96
|
+
/** The header as JSON behind its length (4 bytes), then the data. */
|
|
97
|
+
function encode(item: RelayContentItem): Uint8Array;
|
|
98
|
+
function decode(bytes: Uint8Array): RelayContentItem;
|
|
99
|
+
/**
|
|
100
|
+
* What a link shares: from the copy on the relay when it has one, else from the computer that shared it,
|
|
101
|
+
* end to end through the relay. Rejects with a RelayContentError that says why it cannot.
|
|
102
|
+
*/
|
|
103
|
+
function fetch(text: string | RelayContentLink, options?: RelayContentFetchOptions): Promise<RelayContentItem>;
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=relay-content.d.ts.map
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RelayContentUtil = exports.RelayContentError = void 0;
|
|
4
|
+
const relay_bytes_1 = require("./relay-bytes");
|
|
5
|
+
const relay_identity_1 = require("./relay-identity");
|
|
6
|
+
const relay_protocol_1 = require("./relay-protocol");
|
|
7
|
+
const relay_socket_1 = require("./relay-socket");
|
|
8
|
+
const encoder = new TextEncoder();
|
|
9
|
+
const decoder = new TextDecoder();
|
|
10
|
+
class RelayContentError extends Error {
|
|
11
|
+
constructor(reason, message) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.reason = reason;
|
|
14
|
+
this.name = 'RelayContentError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
exports.RelayContentError = RelayContentError;
|
|
18
|
+
const PBKDF2_ITERATIONS = 600000;
|
|
19
|
+
/** Kinds the links of before carry no `c` for. */
|
|
20
|
+
const PLAIN_KINDS = new Set(['ta-conversation', 'file']);
|
|
21
|
+
const KIND = /^[a-z0-9][a-z0-9-]{0,31}$/;
|
|
22
|
+
/**
|
|
23
|
+
* Content shared by a link (docs/design/ta-link.md): its link, its keys, and how it is fetched, from the
|
|
24
|
+
* computer that shared it through the relay, or from the copy the relay keeps sealed. Runs the same in a
|
|
25
|
+
* browser (the viewer of the relay), in ta serve and in a Worker: WebCrypto, fetch and WebSocket only.
|
|
26
|
+
*/
|
|
27
|
+
var RelayContentUtil;
|
|
28
|
+
(function (RelayContentUtil) {
|
|
29
|
+
/**
|
|
30
|
+
* `https://<relay>/s#v=1&h=<host>&s=<share>&k=<key>`, with `p=1` for a password, `m=self`, `o=1` for a copy on the relay,
|
|
31
|
+
* `c=<kind>` for what is no conversation or file.
|
|
32
|
+
*/
|
|
33
|
+
function link(content) {
|
|
34
|
+
var _a;
|
|
35
|
+
const fragment = new URLSearchParams({ v: '1', h: content.host, s: content.share });
|
|
36
|
+
if (content.access === 'self') {
|
|
37
|
+
fragment.set('m', 'self');
|
|
38
|
+
}
|
|
39
|
+
else if (content.access === 'people') {
|
|
40
|
+
fragment.set('m', 'people');
|
|
41
|
+
fragment.set('o', (_a = content.owner) !== null && _a !== void 0 ? _a : '');
|
|
42
|
+
}
|
|
43
|
+
else if (content.key) {
|
|
44
|
+
fragment.set('k', content.key);
|
|
45
|
+
}
|
|
46
|
+
if (content.access === 'password') {
|
|
47
|
+
fragment.set('p', '1');
|
|
48
|
+
}
|
|
49
|
+
if (content.offline && content.access !== 'self' && content.access !== 'people') {
|
|
50
|
+
fragment.set('o', '1');
|
|
51
|
+
}
|
|
52
|
+
if (content.kind && !PLAIN_KINDS.has(content.kind) && KIND.test(content.kind)) {
|
|
53
|
+
fragment.set('c', content.kind);
|
|
54
|
+
}
|
|
55
|
+
return `${content.relay}${relay_protocol_1.RELAY_CONTENT_PATH}#${fragment}`;
|
|
56
|
+
}
|
|
57
|
+
RelayContentUtil.link = link;
|
|
58
|
+
function is(text) {
|
|
59
|
+
try {
|
|
60
|
+
parse(text);
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
catch (_a) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
RelayContentUtil.is = is;
|
|
68
|
+
/** Throws on what is not a link of {@link link}. */
|
|
69
|
+
function parse(text) {
|
|
70
|
+
var _a, _b;
|
|
71
|
+
const url = new URL(text.trim());
|
|
72
|
+
const fragment = new URLSearchParams(url.hash.slice(1));
|
|
73
|
+
const [host, share, key] = ['h', 's', 'k'].map(name => { var _a; return (_a = fragment.get(name)) !== null && _a !== void 0 ? _a : ''; });
|
|
74
|
+
const self = fragment.get('m') === 'self';
|
|
75
|
+
const people = fragment.get('m') === 'people';
|
|
76
|
+
const owner = (_a = fragment.get('o')) !== null && _a !== void 0 ? _a : '';
|
|
77
|
+
if (!/^https?:$/.test(url.protocol) ||
|
|
78
|
+
url.pathname !== relay_protocol_1.RELAY_CONTENT_PATH ||
|
|
79
|
+
fragment.get('v') !== '1' ||
|
|
80
|
+
!relay_protocol_1.RELAY_ID.test(host) ||
|
|
81
|
+
!relay_protocol_1.RELAY_ID.test(share) ||
|
|
82
|
+
(people ? !/^[A-Za-z0-9_-]{22}$/.test(owner) : !self && !/^[A-Za-z0-9_-]{43}$/.test(key))) {
|
|
83
|
+
throw new RelayContentError('invalid', 'Not a link of something shared from Ta.');
|
|
84
|
+
}
|
|
85
|
+
const kind = (_b = fragment.get('c')) !== null && _b !== void 0 ? _b : '';
|
|
86
|
+
if (people) {
|
|
87
|
+
return { relay: url.origin, host, share, access: 'people', owner, offline: false, ...(KIND.test(kind) ? { kind } : {}) };
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
relay: url.origin,
|
|
91
|
+
host,
|
|
92
|
+
share,
|
|
93
|
+
key: self ? undefined : key,
|
|
94
|
+
access: self ? 'self' : fragment.get('p') === '1' ? 'password' : 'link',
|
|
95
|
+
offline: !self && fragment.get('o') === '1',
|
|
96
|
+
...(KIND.test(kind) ? { kind } : {}),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
RelayContentUtil.parse = parse;
|
|
100
|
+
/**
|
|
101
|
+
* The key the content is sealed with: the key of the link, or with a password, one derived from both
|
|
102
|
+
* (PBKDF2 of the password, then HKDF with the key of the link), so that neither alone opens it.
|
|
103
|
+
*/
|
|
104
|
+
async function contentKey(linkKey, share, password) {
|
|
105
|
+
if (password === undefined) {
|
|
106
|
+
return linkKey;
|
|
107
|
+
}
|
|
108
|
+
const subtle = globalThis.crypto.subtle;
|
|
109
|
+
const secret = await subtle.importKey('raw', encoder.encode(password.normalize('NFC')), 'PBKDF2', false, ['deriveBits']);
|
|
110
|
+
const stretched = await subtle.deriveBits({ name: 'PBKDF2', hash: 'SHA-256', salt: encoder.encode(`ta-share:${share}`), iterations: PBKDF2_ITERATIONS }, secret, 256);
|
|
111
|
+
return hkdf(linkKey, new Uint8Array(stretched), 'ta-share/1 password');
|
|
112
|
+
}
|
|
113
|
+
RelayContentUtil.contentKey = contentKey;
|
|
114
|
+
/**
|
|
115
|
+
* The key of content shared with a Ta friend (`people`): only the two make it, each with its own private key and the
|
|
116
|
+
* other's public one, for this share alone.
|
|
117
|
+
*/
|
|
118
|
+
function peopleKey(identity, otherBox, share) {
|
|
119
|
+
return relay_identity_1.RelayIdentityUtil.agree(identity, otherBox, share, 'ta-share/1 people');
|
|
120
|
+
}
|
|
121
|
+
RelayContentUtil.peopleKey = peopleKey;
|
|
122
|
+
/** What reads the copy on the relay, which keeps only its SHA-256 (`x-ta-auth`). */
|
|
123
|
+
async function authToken(key) {
|
|
124
|
+
return relay_bytes_1.RelayBytes.toBase64(await hkdf(key, new Uint8Array(0), 'ta-share/1 auth'));
|
|
125
|
+
}
|
|
126
|
+
RelayContentUtil.authToken = authToken;
|
|
127
|
+
async function sha256(text) {
|
|
128
|
+
return relay_bytes_1.RelayBytes.toBase64(new Uint8Array(await globalThis.crypto.subtle.digest('SHA-256', encoder.encode(text))));
|
|
129
|
+
}
|
|
130
|
+
RelayContentUtil.sha256 = sha256;
|
|
131
|
+
/** The bytes of the copy on the relay: a random IV, then AES-256-GCM of {@link encode}. */
|
|
132
|
+
async function seal(key, item) {
|
|
133
|
+
const iv = relay_bytes_1.RelayBytes.random(12);
|
|
134
|
+
const sealed = await globalThis.crypto.subtle.encrypt({ name: 'AES-GCM', iv: iv }, await blobKey(key), encode(item));
|
|
135
|
+
return relay_bytes_1.RelayBytes.concat(iv, new Uint8Array(sealed));
|
|
136
|
+
}
|
|
137
|
+
RelayContentUtil.seal = seal;
|
|
138
|
+
/** Throws when it was not sealed with this key. */
|
|
139
|
+
async function open(key, bytes) {
|
|
140
|
+
const opened = await globalThis.crypto.subtle.decrypt({ name: 'AES-GCM', iv: bytes.subarray(0, 12) }, await blobKey(key), bytes.subarray(12));
|
|
141
|
+
return decode(new Uint8Array(opened));
|
|
142
|
+
}
|
|
143
|
+
RelayContentUtil.open = open;
|
|
144
|
+
/** The header as JSON behind its length (4 bytes), then the data. */
|
|
145
|
+
function encode(item) {
|
|
146
|
+
const { data, ...header } = item;
|
|
147
|
+
const head = encoder.encode(JSON.stringify(header));
|
|
148
|
+
const length = new Uint8Array(4);
|
|
149
|
+
new DataView(length.buffer).setUint32(0, head.length);
|
|
150
|
+
return relay_bytes_1.RelayBytes.concat(length, head, data);
|
|
151
|
+
}
|
|
152
|
+
RelayContentUtil.encode = encode;
|
|
153
|
+
function decode(bytes) {
|
|
154
|
+
const length = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getUint32(0);
|
|
155
|
+
const header = JSON.parse(decoder.decode(bytes.subarray(4, 4 + length)));
|
|
156
|
+
return {
|
|
157
|
+
kind: String(header.kind),
|
|
158
|
+
title: String(header.title),
|
|
159
|
+
createdAt: Number(header.createdAt),
|
|
160
|
+
type: header.type,
|
|
161
|
+
data: bytes.slice(4 + length),
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
RelayContentUtil.decode = decode;
|
|
165
|
+
/**
|
|
166
|
+
* What a link shares: from the copy on the relay when it has one, else from the computer that shared it,
|
|
167
|
+
* end to end through the relay. Rejects with a RelayContentError that says why it cannot.
|
|
168
|
+
*/
|
|
169
|
+
async function fetch(text, options = {}) {
|
|
170
|
+
const link = typeof text === 'string' ? parse(text) : text;
|
|
171
|
+
if (link.access === 'people') {
|
|
172
|
+
if (!options.people) {
|
|
173
|
+
throw new RelayContentError('people', 'Only the Ta friends it was shared with open it, in their own Ta.');
|
|
174
|
+
}
|
|
175
|
+
return fromHost(link, await peopleKey(options.people.identity, options.people.ownerBox, link.share), options);
|
|
176
|
+
}
|
|
177
|
+
if (link.access === 'self' || !link.key) {
|
|
178
|
+
throw new RelayContentError('self', 'Only the one who shared it opens it, in their own Ta.');
|
|
179
|
+
}
|
|
180
|
+
if (link.access === 'password' && !options.password) {
|
|
181
|
+
throw new RelayContentError('password', 'It is shared with a password.');
|
|
182
|
+
}
|
|
183
|
+
const key = await contentKey(relay_bytes_1.RelayBytes.fromBase64(link.key), link.share, link.access === 'password' ? options.password : undefined);
|
|
184
|
+
if (link.offline) {
|
|
185
|
+
const copy = await fromRelay(link, key);
|
|
186
|
+
if (copy) {
|
|
187
|
+
return copy;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return fromHost(link, key, options);
|
|
191
|
+
}
|
|
192
|
+
RelayContentUtil.fetch = fetch;
|
|
193
|
+
/** The copy on the relay; undefined when there is none (any more), to ask the computer instead. */
|
|
194
|
+
async function fromRelay(link, key) {
|
|
195
|
+
let response;
|
|
196
|
+
try {
|
|
197
|
+
response = await globalThis.fetch(`${link.relay}${relay_protocol_1.RELAY_BLOB_PATH}${link.share}`, { headers: { 'x-ta-auth': await authToken(key) } });
|
|
198
|
+
}
|
|
199
|
+
catch (_a) {
|
|
200
|
+
return undefined;
|
|
201
|
+
}
|
|
202
|
+
if (response.status === 403) {
|
|
203
|
+
throw link.access === 'password'
|
|
204
|
+
? new RelayContentError('wrong', 'The password is wrong.')
|
|
205
|
+
: new RelayContentError('gone', 'This link no longer works.');
|
|
206
|
+
}
|
|
207
|
+
if (response.status === 429) {
|
|
208
|
+
throw new RelayContentError('busy', 'Too many wrong passwords: try again in a few minutes.');
|
|
209
|
+
}
|
|
210
|
+
if (!response.ok) {
|
|
211
|
+
return undefined;
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
return await open(key, new Uint8Array(await response.arrayBuffer()));
|
|
215
|
+
}
|
|
216
|
+
catch (_b) {
|
|
217
|
+
throw new RelayContentError('wrong', 'The password is wrong.');
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
function fromHost(link, key, options) {
|
|
221
|
+
var _a;
|
|
222
|
+
const max = (_a = options.maxBytes) !== null && _a !== void 0 ? _a : relay_protocol_1.RELAY_CONTENT_MAX_BYTES;
|
|
223
|
+
const pairing = { relay: link.relay, host: link.host, device: link.share, key: relay_bytes_1.RelayBytes.toBase64(key) };
|
|
224
|
+
return new Promise((resolve, reject) => {
|
|
225
|
+
var _a;
|
|
226
|
+
const socket = new relay_socket_1.RelaySocket(pairing, { proveFirst: true });
|
|
227
|
+
const chunks = [];
|
|
228
|
+
let received = 0;
|
|
229
|
+
let header;
|
|
230
|
+
let done = false;
|
|
231
|
+
const finish = (error, item) => {
|
|
232
|
+
if (done) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
done = true;
|
|
236
|
+
clearTimeout(timer);
|
|
237
|
+
socket.onclose = null;
|
|
238
|
+
socket.close();
|
|
239
|
+
if (item) {
|
|
240
|
+
resolve(item);
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
reject(error);
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
const timer = setTimeout(() => finish(new RelayContentError('timeout', 'The computer that shared it took too long to send it.')), (_a = options.timeoutMs) !== null && _a !== void 0 ? _a : 60000);
|
|
247
|
+
socket.onmessage = ({ data }) => {
|
|
248
|
+
let message;
|
|
249
|
+
try {
|
|
250
|
+
message = JSON.parse(data);
|
|
251
|
+
}
|
|
252
|
+
catch (_a) {
|
|
253
|
+
finish(new RelayContentError('invalid', 'The computer that shared it sent what is not content.'));
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
if (message.t === 'error') {
|
|
257
|
+
finish(new RelayContentError('gone', message.message));
|
|
258
|
+
}
|
|
259
|
+
else if (message.t === 'content') {
|
|
260
|
+
if (!(message.size >= 0) || message.size > max) {
|
|
261
|
+
finish(new RelayContentError('large', `It is larger than ${Math.round(max / 1024 / 1024)} MB.`));
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
header = {
|
|
265
|
+
kind: String(message.kind),
|
|
266
|
+
title: String(message.title),
|
|
267
|
+
createdAt: Number(message.createdAt),
|
|
268
|
+
type: message.type,
|
|
269
|
+
size: message.size,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
else if (message.t === 'chunk' && header) {
|
|
273
|
+
const bytes = relay_bytes_1.RelayBytes.fromBase64(message.d);
|
|
274
|
+
received += bytes.length;
|
|
275
|
+
if (received > header.size) {
|
|
276
|
+
finish(new RelayContentError('invalid', 'It is larger than it said.'));
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
chunks.push(bytes);
|
|
280
|
+
}
|
|
281
|
+
else if (message.t === 'end' && header) {
|
|
282
|
+
if (received !== header.size) {
|
|
283
|
+
finish(new RelayContentError('invalid', 'It came short.'));
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
const { size: _size, ...rest } = header;
|
|
287
|
+
finish(undefined, { ...rest, data: relay_bytes_1.RelayBytes.concat(...chunks) });
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
socket.onclose = ({ code, reason }) => finish(closed(code, reason, link));
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
function closed(code, reason, link) {
|
|
294
|
+
if (code === relay_socket_1.RelaySocket.REFUSED) {
|
|
295
|
+
if (reason === 'key') {
|
|
296
|
+
return link.access === 'password'
|
|
297
|
+
? new RelayContentError('wrong', 'The password is wrong.')
|
|
298
|
+
: new RelayContentError('gone', 'This link no longer works.');
|
|
299
|
+
}
|
|
300
|
+
if (reason === 'busy') {
|
|
301
|
+
return new RelayContentError('busy', 'Too many wrong passwords: try again in a few minutes.');
|
|
302
|
+
}
|
|
303
|
+
return new RelayContentError('gone', 'This link no longer works: the share was stopped or ran out.');
|
|
304
|
+
}
|
|
305
|
+
return code === relay_protocol_1.RelayCloseCode.HOST_OFFLINE || code === relay_protocol_1.RelayCloseCode.TIMEOUT
|
|
306
|
+
? new RelayContentError('offline', 'The computer that shared it is not running Ta now. Try again later.')
|
|
307
|
+
: new RelayContentError('offline', 'The computer that shared it cannot be reached now. Try again later.');
|
|
308
|
+
}
|
|
309
|
+
async function blobKey(key) {
|
|
310
|
+
const bytes = await hkdf(key, new Uint8Array(0), 'ta-share/1 blob');
|
|
311
|
+
return globalThis.crypto.subtle.importKey('raw', bytes, 'AES-GCM', false, ['encrypt', 'decrypt']);
|
|
312
|
+
}
|
|
313
|
+
async function hkdf(key, salt, info) {
|
|
314
|
+
const subtle = globalThis.crypto.subtle;
|
|
315
|
+
const secret = await subtle.importKey('raw', key, 'HKDF', false, ['deriveBits']);
|
|
316
|
+
const bits = await subtle.deriveBits({ name: 'HKDF', hash: 'SHA-256', salt: salt, info: encoder.encode(info) }, secret, 256);
|
|
317
|
+
return new Uint8Array(bits);
|
|
318
|
+
}
|
|
319
|
+
})(RelayContentUtil || (exports.RelayContentUtil = RelayContentUtil = {}));
|
|
320
|
+
//# sourceMappingURL=relay-content.js.map
|