@celljs/ta-relay 4.12.0 → 4.13.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.
- package/README.en.md +26 -1
- package/README.md +26 -1
- package/esm/common/index.d.ts +3 -0
- package/esm/common/index.js +3 -0
- package/esm/common/relay-friends.d.ts +154 -0
- package/esm/common/relay-friends.js +482 -0
- package/esm/common/relay-hub-util.d.ts +9 -0
- package/esm/common/relay-hub-util.js +19 -2
- package/esm/common/relay-hubs-connection.d.ts +17 -5
- package/esm/common/relay-hubs-connection.js +40 -37
- package/esm/common/relay-hubs-protocol.d.ts +51 -7
- package/esm/common/relay-hubs.d.ts +67 -5
- package/esm/common/relay-hubs.js +305 -16
- package/esm/common/relay-identity.d.ts +2 -0
- package/esm/common/relay-info.d.ts +44 -0
- package/esm/common/relay-info.js +94 -0
- package/esm/common/relay-protocol.d.ts +7 -3
- package/esm/common/relay-protocol.js +7 -3
- package/esm/common/relay-room-chat.d.ts +6 -1
- package/esm/common/relay-room-chat.js +1 -0
- package/esm/common/relay-spot.d.ts +122 -0
- package/esm/common/relay-spot.js +179 -0
- package/esm/guest/guest-i18n.d.ts +2 -0
- package/esm/guest/guest-i18n.js +4 -0
- package/esm/guest/guest.js +9 -1
- package/esm/node/index.d.ts +2 -0
- package/esm/node/index.js +2 -0
- package/esm/node/relay-file-storage.d.ts +32 -0
- package/esm/node/relay-file-storage.js +101 -0
- package/esm/node/relay-host.d.ts +63 -28
- package/esm/node/relay-host.js +145 -52
- package/esm/node/relay-server.d.ts +37 -3
- package/esm/node/relay-server.js +193 -37
- package/esm/node/relay-store.d.ts +34 -6
- package/esm/node/relay-store.js +54 -22
- package/esm/node/relays-file.d.ts +139 -0
- package/esm/node/relays-file.js +373 -0
- package/lib/common/index.d.ts +3 -0
- package/lib/common/index.js +3 -0
- package/lib/common/relay-friends.d.ts +155 -0
- package/lib/common/relay-friends.js +486 -0
- package/lib/common/relay-hub-util.d.ts +9 -0
- package/lib/common/relay-hub-util.js +19 -2
- package/lib/common/relay-hubs-connection.d.ts +17 -5
- package/lib/common/relay-hubs-connection.js +40 -37
- package/lib/common/relay-hubs-protocol.d.ts +51 -7
- package/lib/common/relay-hubs.d.ts +67 -5
- package/lib/common/relay-hubs.js +306 -17
- package/lib/common/relay-identity.d.ts +2 -0
- package/lib/common/relay-info.d.ts +45 -0
- package/lib/common/relay-info.js +97 -0
- package/lib/common/relay-protocol.d.ts +7 -3
- package/lib/common/relay-protocol.js +8 -4
- package/lib/common/relay-room-chat.d.ts +6 -1
- package/lib/common/relay-room-chat.js +1 -0
- package/lib/common/relay-spot.d.ts +123 -0
- package/lib/common/relay-spot.js +183 -0
- package/lib/guest/guest-i18n.d.ts +2 -0
- package/lib/guest/guest-i18n.js +4 -0
- package/lib/guest/guest.js +9 -1
- package/lib/node/index.d.ts +2 -0
- package/lib/node/index.js +2 -0
- package/lib/node/relay-file-storage.d.ts +33 -0
- package/lib/node/relay-file-storage.js +105 -0
- package/lib/node/relay-host.d.ts +63 -28
- package/lib/node/relay-host.js +146 -53
- package/lib/node/relay-server.d.ts +37 -3
- package/lib/node/relay-server.js +193 -37
- package/lib/node/relay-store.d.ts +34 -6
- package/lib/node/relay-store.js +54 -22
- package/lib/node/relays-file.d.ts +140 -0
- package/lib/node/relays-file.js +378 -0
- package/package.json +2 -2
- package/viewer/room.html +10 -10
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A spot (docs/design/ta-nearby.md 2.2): the Ta that typed the same code of six digits, on networks that keep them
|
|
3
|
+
* apart, hear each other's beacons through the relay, and pass sealed messages by their temporary ids. The id of a spot
|
|
4
|
+
* is a hash of its code; the relay keeps nothing of it but who is connected now.
|
|
5
|
+
*/
|
|
6
|
+
export declare const RELAY_SPOT_PATH = "/v1/spot/";
|
|
7
|
+
/** Who may come into a spot this long after it was opened; those in stay. */
|
|
8
|
+
export declare const RELAY_SPOT_JOIN_MS: number;
|
|
9
|
+
/** Those in a spot at most. */
|
|
10
|
+
export declare const RELAY_SPOT_MAX = 50;
|
|
11
|
+
/** A message at most. */
|
|
12
|
+
export declare const RELAY_SPOT_MESSAGE = 900000;
|
|
13
|
+
/** Codes nobody opened that one address may try in {@link RELAY_SPOT_TRIES_MS}; then it waits. */
|
|
14
|
+
export declare const RELAY_SPOT_TRIES = 10;
|
|
15
|
+
export declare const RELAY_SPOT_TRIES_MS: number;
|
|
16
|
+
export declare enum RelaySpotCloseCode {
|
|
17
|
+
/** Not understood. */
|
|
18
|
+
BAD = 4400,
|
|
19
|
+
/** No one opened a spot of this code. */
|
|
20
|
+
NONE = 4404,
|
|
21
|
+
/** Opened too long ago to come in. */
|
|
22
|
+
LATE = 4410,
|
|
23
|
+
FULL = 4429,
|
|
24
|
+
/** This address tried too many codes nobody opened: it waits (RelaySpotTries). */
|
|
25
|
+
TRIES = 4428
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The codes nobody opened that one address tried lately (docs/design/ta-nearby.md 2.2): six digits are a million
|
|
29
|
+
* codes, so who tries them is held to {@link RELAY_SPOT_TRIES} in {@link RELAY_SPOT_TRIES_MS}. Only those that fail
|
|
30
|
+
* count, so that a whole office behind one address may come into one spot at once.
|
|
31
|
+
*/
|
|
32
|
+
export declare namespace RelaySpotTries {
|
|
33
|
+
/** What an address is counted by: an IPv4 address, or the /64 of an IPv6 one, which its owner has all of. */
|
|
34
|
+
function keyOf(address: string | null | undefined): string;
|
|
35
|
+
/** The failures still counted. */
|
|
36
|
+
function recent(fails: readonly number[], now: number): number[];
|
|
37
|
+
function blocked(fails: readonly number[], now: number): boolean;
|
|
38
|
+
/** When it may try again. */
|
|
39
|
+
function until(fails: readonly number[], now: number): number;
|
|
40
|
+
}
|
|
41
|
+
/** What the relay knows of one in a spot: its temporary ids, and when the spot was opened. */
|
|
42
|
+
export interface RelaySpotWho {
|
|
43
|
+
e?: string;
|
|
44
|
+
/** Its id of before, for what was sealed for it just before it changed. */
|
|
45
|
+
was?: string;
|
|
46
|
+
opened: number;
|
|
47
|
+
}
|
|
48
|
+
/** What one sends to its spot. */
|
|
49
|
+
export type RelaySpotIn =
|
|
50
|
+
/** First: opens it, or comes in if it is open. */
|
|
51
|
+
{
|
|
52
|
+
t: 'open';
|
|
53
|
+
}
|
|
54
|
+
/** First: comes in; refused when nobody opened it. */
|
|
55
|
+
| {
|
|
56
|
+
t: 'join';
|
|
57
|
+
}
|
|
58
|
+
/** Its beacon, to every other one. */
|
|
59
|
+
| {
|
|
60
|
+
t: 'here';
|
|
61
|
+
b: {
|
|
62
|
+
e: string;
|
|
63
|
+
} & Record<string, unknown>;
|
|
64
|
+
}
|
|
65
|
+
/** A sealed message to one (`re`: the answer to one of its). */
|
|
66
|
+
| {
|
|
67
|
+
t: 'to';
|
|
68
|
+
to: string;
|
|
69
|
+
id: string;
|
|
70
|
+
f: unknown;
|
|
71
|
+
re?: true;
|
|
72
|
+
} | {
|
|
73
|
+
t: 'bye';
|
|
74
|
+
};
|
|
75
|
+
/** What the relay sends to one in a spot. */
|
|
76
|
+
export type RelaySpotOut = {
|
|
77
|
+
t: 'ready';
|
|
78
|
+
opened: number;
|
|
79
|
+
} | {
|
|
80
|
+
t: 'here';
|
|
81
|
+
b: {
|
|
82
|
+
e: string;
|
|
83
|
+
} & Record<string, unknown>;
|
|
84
|
+
} | {
|
|
85
|
+
t: 'frame';
|
|
86
|
+
from: string;
|
|
87
|
+
id: string;
|
|
88
|
+
f: unknown;
|
|
89
|
+
re?: true;
|
|
90
|
+
}
|
|
91
|
+
/** Nobody of that id is there. */
|
|
92
|
+
| {
|
|
93
|
+
t: 'miss';
|
|
94
|
+
id: string;
|
|
95
|
+
} | {
|
|
96
|
+
t: 'gone';
|
|
97
|
+
e: string;
|
|
98
|
+
};
|
|
99
|
+
export interface RelaySpotSocket {
|
|
100
|
+
/** The same for the same connection. */
|
|
101
|
+
readonly key: unknown;
|
|
102
|
+
who: RelaySpotWho | undefined;
|
|
103
|
+
send(text: string): void;
|
|
104
|
+
close(code: number, reason: string): void;
|
|
105
|
+
}
|
|
106
|
+
/** One spot, on a Durable Object of the Worker or in `ta relay`: the same rules. */
|
|
107
|
+
export declare class RelaySpot {
|
|
108
|
+
protected readonly sockets: () => RelaySpotSocket[];
|
|
109
|
+
protected readonly now: () => number;
|
|
110
|
+
/** One asked to join a code nobody opened: counted against its address (RelaySpotTries). */
|
|
111
|
+
protected readonly missed?: ((socket: RelaySpotSocket) => void) | undefined;
|
|
112
|
+
constructor(sockets: () => RelaySpotSocket[], now?: () => number,
|
|
113
|
+
/** One asked to join a code nobody opened: counted against its address (RelaySpotTries). */
|
|
114
|
+
missed?: ((socket: RelaySpotSocket) => void) | undefined);
|
|
115
|
+
receive(socket: RelaySpotSocket, text: string): void;
|
|
116
|
+
/** One left: the others forget it at once. */
|
|
117
|
+
closed(socket: RelaySpotSocket): void;
|
|
118
|
+
protected enter(socket: RelaySpotSocket, message: RelaySpotIn): void;
|
|
119
|
+
/** The others in the spot. */
|
|
120
|
+
protected others(socket: RelaySpotSocket): RelaySpotSocket[];
|
|
121
|
+
static parse(text: string): RelaySpotIn | undefined;
|
|
122
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { RELAY_PING, RELAY_PONG } from './relay-protocol.js';
|
|
2
|
+
/**
|
|
3
|
+
* A spot (docs/design/ta-nearby.md 2.2): the Ta that typed the same code of six digits, on networks that keep them
|
|
4
|
+
* apart, hear each other's beacons through the relay, and pass sealed messages by their temporary ids. The id of a spot
|
|
5
|
+
* is a hash of its code; the relay keeps nothing of it but who is connected now.
|
|
6
|
+
*/
|
|
7
|
+
export const RELAY_SPOT_PATH = '/v1/spot/';
|
|
8
|
+
/** Who may come into a spot this long after it was opened; those in stay. */
|
|
9
|
+
export const RELAY_SPOT_JOIN_MS = 10 * 60000;
|
|
10
|
+
/** Those in a spot at most. */
|
|
11
|
+
export const RELAY_SPOT_MAX = 50;
|
|
12
|
+
/** A message at most. */
|
|
13
|
+
export const RELAY_SPOT_MESSAGE = 900000;
|
|
14
|
+
/** Codes nobody opened that one address may try in {@link RELAY_SPOT_TRIES_MS}; then it waits. */
|
|
15
|
+
export const RELAY_SPOT_TRIES = 10;
|
|
16
|
+
export const RELAY_SPOT_TRIES_MS = 10 * 60000;
|
|
17
|
+
export var RelaySpotCloseCode;
|
|
18
|
+
(function (RelaySpotCloseCode) {
|
|
19
|
+
/** Not understood. */
|
|
20
|
+
RelaySpotCloseCode[RelaySpotCloseCode["BAD"] = 4400] = "BAD";
|
|
21
|
+
/** No one opened a spot of this code. */
|
|
22
|
+
RelaySpotCloseCode[RelaySpotCloseCode["NONE"] = 4404] = "NONE";
|
|
23
|
+
/** Opened too long ago to come in. */
|
|
24
|
+
RelaySpotCloseCode[RelaySpotCloseCode["LATE"] = 4410] = "LATE";
|
|
25
|
+
RelaySpotCloseCode[RelaySpotCloseCode["FULL"] = 4429] = "FULL";
|
|
26
|
+
/** This address tried too many codes nobody opened: it waits (RelaySpotTries). */
|
|
27
|
+
RelaySpotCloseCode[RelaySpotCloseCode["TRIES"] = 4428] = "TRIES";
|
|
28
|
+
})(RelaySpotCloseCode || (RelaySpotCloseCode = {}));
|
|
29
|
+
/**
|
|
30
|
+
* The codes nobody opened that one address tried lately (docs/design/ta-nearby.md 2.2): six digits are a million
|
|
31
|
+
* codes, so who tries them is held to {@link RELAY_SPOT_TRIES} in {@link RELAY_SPOT_TRIES_MS}. Only those that fail
|
|
32
|
+
* count, so that a whole office behind one address may come into one spot at once.
|
|
33
|
+
*/
|
|
34
|
+
export var RelaySpotTries;
|
|
35
|
+
(function (RelaySpotTries) {
|
|
36
|
+
/** What an address is counted by: an IPv4 address, or the /64 of an IPv6 one, which its owner has all of. */
|
|
37
|
+
function keyOf(address) {
|
|
38
|
+
const ip = (address !== null && address !== void 0 ? address : '').trim().replace(/^::ffff:/i, '');
|
|
39
|
+
if (!ip.includes(':')) {
|
|
40
|
+
return ip || 'unknown';
|
|
41
|
+
}
|
|
42
|
+
const [head, tail = ''] = ip.toLowerCase().split('::');
|
|
43
|
+
const groups = head ? head.split(':') : [];
|
|
44
|
+
const rest = tail ? tail.split(':') : [];
|
|
45
|
+
const full = [...groups, ...Array(Math.max(0, 8 - groups.length - rest.length)).fill('0'), ...rest];
|
|
46
|
+
return `${full
|
|
47
|
+
.slice(0, 4)
|
|
48
|
+
.map(group => group.replace(/^0+(?=.)/, ''))
|
|
49
|
+
.join(':')}::/64`;
|
|
50
|
+
}
|
|
51
|
+
RelaySpotTries.keyOf = keyOf;
|
|
52
|
+
/** The failures still counted. */
|
|
53
|
+
function recent(fails, now) {
|
|
54
|
+
return fails.filter(at => now - at < RELAY_SPOT_TRIES_MS);
|
|
55
|
+
}
|
|
56
|
+
RelaySpotTries.recent = recent;
|
|
57
|
+
function blocked(fails, now) {
|
|
58
|
+
return recent(fails, now).length >= RELAY_SPOT_TRIES;
|
|
59
|
+
}
|
|
60
|
+
RelaySpotTries.blocked = blocked;
|
|
61
|
+
/** When it may try again. */
|
|
62
|
+
function until(fails, now) {
|
|
63
|
+
const kept = recent(fails, now).sort((a, b) => a - b);
|
|
64
|
+
return kept.length >= RELAY_SPOT_TRIES ? kept[kept.length - RELAY_SPOT_TRIES] + RELAY_SPOT_TRIES_MS : now;
|
|
65
|
+
}
|
|
66
|
+
RelaySpotTries.until = until;
|
|
67
|
+
})(RelaySpotTries || (RelaySpotTries = {}));
|
|
68
|
+
/** One spot, on a Durable Object of the Worker or in `ta relay`: the same rules. */
|
|
69
|
+
export class RelaySpot {
|
|
70
|
+
constructor(sockets, now = Date.now,
|
|
71
|
+
/** One asked to join a code nobody opened: counted against its address (RelaySpotTries). */
|
|
72
|
+
missed) {
|
|
73
|
+
this.sockets = sockets;
|
|
74
|
+
this.now = now;
|
|
75
|
+
this.missed = missed;
|
|
76
|
+
}
|
|
77
|
+
receive(socket, text) {
|
|
78
|
+
var _a;
|
|
79
|
+
var _b;
|
|
80
|
+
if (text === RELAY_PING) {
|
|
81
|
+
socket.send(RELAY_PONG);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const message = text.length <= RELAY_SPOT_MESSAGE ? RelaySpot.parse(text) : undefined;
|
|
85
|
+
if (!message) {
|
|
86
|
+
socket.close(RelaySpotCloseCode.BAD, 'Not understood.');
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const who = socket.who;
|
|
90
|
+
if (!who) {
|
|
91
|
+
this.enter(socket, message);
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (message.t === 'here') {
|
|
95
|
+
if (who.e !== message.b.e) {
|
|
96
|
+
socket.who = { ...who, ...(who.e ? { was: who.e } : {}), e: message.b.e };
|
|
97
|
+
}
|
|
98
|
+
const text = JSON.stringify({ t: 'here', b: message.b });
|
|
99
|
+
for (const other of this.others(socket)) {
|
|
100
|
+
other.send(text);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
else if (message.t === 'to') {
|
|
104
|
+
const target = this.others(socket).find(other => { var _a, _b; return ((_a = other.who) === null || _a === void 0 ? void 0 : _a.e) === message.to || ((_b = other.who) === null || _b === void 0 ? void 0 : _b.was) === message.to; });
|
|
105
|
+
const out = target
|
|
106
|
+
? { t: 'frame', from: (_b = (_a = socket.who) === null || _a === void 0 ? void 0 : _a.e) !== null && _b !== void 0 ? _b : '', id: message.id, f: message.f, ...(message.re ? { re: true } : {}) }
|
|
107
|
+
: { t: 'miss', id: message.id };
|
|
108
|
+
(target !== null && target !== void 0 ? target : socket).send(JSON.stringify(out));
|
|
109
|
+
}
|
|
110
|
+
else if (message.t === 'bye') {
|
|
111
|
+
socket.close(1000, 'Bye.');
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/** One left: the others forget it at once. */
|
|
115
|
+
closed(socket) {
|
|
116
|
+
var _a;
|
|
117
|
+
const e = (_a = socket.who) === null || _a === void 0 ? void 0 : _a.e;
|
|
118
|
+
if (e) {
|
|
119
|
+
const text = JSON.stringify({ t: 'gone', e });
|
|
120
|
+
for (const other of this.others(socket)) {
|
|
121
|
+
other.send(text);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
enter(socket, message) {
|
|
126
|
+
var _a;
|
|
127
|
+
if (message.t !== 'open' && message.t !== 'join') {
|
|
128
|
+
socket.close(RelaySpotCloseCode.BAD, 'Open or join first.');
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const others = this.others(socket);
|
|
132
|
+
const now = this.now();
|
|
133
|
+
const opened = others.length ? Math.min(...others.map(other => { var _a; var _b; return (_b = (_a = other.who) === null || _a === void 0 ? void 0 : _a.opened) !== null && _b !== void 0 ? _b : now; })) : now;
|
|
134
|
+
if (!others.length && message.t === 'join') {
|
|
135
|
+
(_a = this.missed) === null || _a === void 0 ? void 0 : _a.call(this, socket);
|
|
136
|
+
socket.close(RelaySpotCloseCode.NONE, 'No such spot.');
|
|
137
|
+
}
|
|
138
|
+
else if (now - opened > RELAY_SPOT_JOIN_MS) {
|
|
139
|
+
socket.close(RelaySpotCloseCode.LATE, 'Too late to come in.');
|
|
140
|
+
}
|
|
141
|
+
else if (others.length >= RELAY_SPOT_MAX) {
|
|
142
|
+
socket.close(RelaySpotCloseCode.FULL, 'Full.');
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
socket.who = { opened };
|
|
146
|
+
socket.send(JSON.stringify({ t: 'ready', opened }));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/** The others in the spot. */
|
|
150
|
+
others(socket) {
|
|
151
|
+
return this.sockets().filter(other => other.key !== socket.key && other.who);
|
|
152
|
+
}
|
|
153
|
+
static parse(text) {
|
|
154
|
+
try {
|
|
155
|
+
const data = JSON.parse(text);
|
|
156
|
+
const id = (value) => typeof value === 'string' && value.length > 0 && value.length <= 64;
|
|
157
|
+
switch (data.t) {
|
|
158
|
+
case 'open':
|
|
159
|
+
case 'join':
|
|
160
|
+
case 'bye':
|
|
161
|
+
return { t: data.t };
|
|
162
|
+
case 'here': {
|
|
163
|
+
const b = data.b;
|
|
164
|
+
return b && typeof b === 'object' && id(b.e) ? { t: 'here', b: b } : undefined;
|
|
165
|
+
}
|
|
166
|
+
case 'to':
|
|
167
|
+
return id(data.to) && id(data.id)
|
|
168
|
+
? { t: 'to', to: data.to, id: data.id, f: data.f, ...(data.re === true ? { re: true } : {}) }
|
|
169
|
+
: undefined;
|
|
170
|
+
default:
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
catch (_a) {
|
|
175
|
+
return undefined;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
//# sourceMappingURL=relay-spot.js.map
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
declare const zh: {
|
|
2
2
|
title: string;
|
|
3
3
|
invalid: string;
|
|
4
|
+
inTa: string;
|
|
4
5
|
invited: string;
|
|
5
6
|
joinHint: string;
|
|
6
7
|
name: string;
|
|
@@ -28,6 +29,7 @@ declare const zh: {
|
|
|
28
29
|
guest: string;
|
|
29
30
|
doubleOf: string;
|
|
30
31
|
double: string;
|
|
32
|
+
task: string;
|
|
31
33
|
agentOf: string;
|
|
32
34
|
rename: string;
|
|
33
35
|
leave: string;
|
package/esm/guest/guest-i18n.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const zh = {
|
|
2
2
|
title: 'Ta 房间',
|
|
3
3
|
invalid: '这不是一个完整的 Ta 房间邀请链接,请检查是否复制完整。',
|
|
4
|
+
inTa: '这是{by}在 Ta 里的加好友链接,要在 Ta 里打开:用 Ta 扫码,或把链接粘贴到 Ta 的快捷打开。',
|
|
4
5
|
invited: '{by}邀请你加入',
|
|
5
6
|
joinHint: '房主同意后,就能在这里和大家聊天。',
|
|
6
7
|
name: '你的名字',
|
|
@@ -28,6 +29,7 @@ const zh = {
|
|
|
28
29
|
guest: '网页访客',
|
|
29
30
|
doubleOf: '{name}的分身',
|
|
30
31
|
double: '分身',
|
|
32
|
+
task: '任务',
|
|
31
33
|
agentOf: '{name}的智能体',
|
|
32
34
|
rename: '修改我的名字',
|
|
33
35
|
leave: '退出房间',
|
|
@@ -43,6 +45,7 @@ const zh = {
|
|
|
43
45
|
const en = {
|
|
44
46
|
title: 'A room of Ta',
|
|
45
47
|
invalid: 'This is not a whole link that invites into a room of Ta: check it was copied in full.',
|
|
48
|
+
inTa: 'This link of {by} adds them as a friend in Ta, and opens in Ta: scan it with Ta, or paste it in Quick open of Ta.',
|
|
46
49
|
invited: '{by} invites you to join',
|
|
47
50
|
joinHint: 'Once the owner lets you in, you chat with everyone here.',
|
|
48
51
|
name: 'Your name',
|
|
@@ -70,6 +73,7 @@ const en = {
|
|
|
70
73
|
guest: 'Web guest',
|
|
71
74
|
doubleOf: "{name}'s double",
|
|
72
75
|
double: 'Double',
|
|
76
|
+
task: 'Task',
|
|
73
77
|
agentOf: "{name}'s agent",
|
|
74
78
|
rename: 'Change my name',
|
|
75
79
|
leave: 'Leave the room',
|
package/esm/guest/guest.js
CHANGED
|
@@ -290,7 +290,9 @@ class GuestPage {
|
|
|
290
290
|
const personOf = (line) => { var _a; var _b; return (_b = (_a = room.members.find(member => member.member === line.from)) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : line.name; };
|
|
291
291
|
// What a double says (docs/design/ta-link.md 4) is its own: `X's double`, a guest having none; an agent's, its name.
|
|
292
292
|
const nameOf = (line) => (line.agent ? line.agent.name : line.double ? t('doubleOf').replace('{name}', personOf(line)) : personOf(line));
|
|
293
|
-
const tagOf = (line) =>
|
|
293
|
+
const tagOf = (line) => [line.agent ? t('agentOf').replace('{name}', personOf(line)) : line.double ? t('double') : '', line.task ? t('task') : '']
|
|
294
|
+
.filter(Boolean)
|
|
295
|
+
.join(' · ');
|
|
294
296
|
const speaker = (line) => { var _a; var _b; return `${line.from}:${line.double ? 'd' : ''}:${(_b = (_a = line.agent) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : ''}`; };
|
|
295
297
|
const nodes = [];
|
|
296
298
|
if (!lines.length) {
|
|
@@ -377,6 +379,12 @@ function main() {
|
|
|
377
379
|
document.body.replaceChildren(div('app', div('top', logo), div('center', Object.assign(el('p', t('invalid')), { className: 'hint' }))));
|
|
378
380
|
return;
|
|
379
381
|
}
|
|
382
|
+
if (link.kind) {
|
|
383
|
+
// A room a package keeps for itself (friends of Moments): only a Ta joins it.
|
|
384
|
+
const logo = Object.assign(el('span', 'Ta'), { className: 'logo' });
|
|
385
|
+
document.body.replaceChildren(div('app', div('top', logo), div('center', Object.assign(el('p', t('inTa', { by: link.by })), { className: 'hint' }))));
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
380
388
|
new GuestPage(link).start();
|
|
381
389
|
}
|
|
382
390
|
addEventListener('hashchange', () => location.reload());
|
package/esm/node/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export * from '../common/index.js';
|
|
2
|
+
export * from './relay-file-storage.js';
|
|
2
3
|
export * from './relay-files-host.js';
|
|
3
4
|
export * from './relay-host.js';
|
|
4
5
|
export * from './relay-hub-files.js';
|
|
5
6
|
export * from './relay-server.js';
|
|
6
7
|
export * from './relay-store.js';
|
|
7
8
|
export * from './relay-viewer.js';
|
|
9
|
+
export * from './relays-file.js';
|
package/esm/node/index.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
export * from '../common/index.js';
|
|
2
|
+
export * from './relay-file-storage.js';
|
|
2
3
|
export * from './relay-files-host.js';
|
|
3
4
|
export * from './relay-host.js';
|
|
4
5
|
export * from './relay-hub-files.js';
|
|
5
6
|
export * from './relay-server.js';
|
|
6
7
|
export * from './relay-store.js';
|
|
7
8
|
export * from './relay-viewer.js';
|
|
9
|
+
export * from './relays-file.js';
|
|
8
10
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { RelayBlobStorage } from '../common/relay-blobs.js';
|
|
2
|
+
import type { RelayHubStorage } from '../common/relay-hub.js';
|
|
3
|
+
/**
|
|
4
|
+
* What `ta relay --data <dir>` keeps of a room or a copy, as a Durable Object keeps it: one directory each, the values in
|
|
5
|
+
* memory and in one file, written whole after each change (rooms hold at most a few megabytes, copies eight).
|
|
6
|
+
*/
|
|
7
|
+
export declare class FileRelayStorage implements RelayHubStorage, RelayBlobStorage {
|
|
8
|
+
protected readonly dir: string;
|
|
9
|
+
/** Its alarm ran: what it held went with it (a copy that ran out, a room unused for long). */
|
|
10
|
+
protected readonly onAlarm?: (() => void) | undefined;
|
|
11
|
+
protected readonly values: Map<string, unknown>;
|
|
12
|
+
protected readonly file: string;
|
|
13
|
+
protected timer?: ReturnType<typeof setTimeout>;
|
|
14
|
+
constructor(dir: string,
|
|
15
|
+
/** Its alarm ran: what it held went with it (a copy that ran out, a room unused for long). */
|
|
16
|
+
onAlarm?: (() => void) | undefined);
|
|
17
|
+
/** The ids of what a directory of the relay holds. */
|
|
18
|
+
static ids(root: string): string[];
|
|
19
|
+
get empty(): boolean;
|
|
20
|
+
get<T>(key: string): Promise<T | undefined>;
|
|
21
|
+
put(key: string, value: unknown): Promise<void>;
|
|
22
|
+
delete(key: string): Promise<boolean>;
|
|
23
|
+
list<T>(options: {
|
|
24
|
+
prefix: string;
|
|
25
|
+
}): Promise<Map<string, T>>;
|
|
26
|
+
deleteAll(): Promise<void>;
|
|
27
|
+
setAlarm(at: number): Promise<void>;
|
|
28
|
+
close(): void;
|
|
29
|
+
protected schedule(at: number): void;
|
|
30
|
+
protected save(): void;
|
|
31
|
+
protected static load(file: string): Map<string, unknown>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { mkdirSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { RelayBytes } from '../common/relay-bytes.js';
|
|
4
|
+
const encode = (value) => (value instanceof Uint8Array ? { b: RelayBytes.toBase64(value) } : { j: value });
|
|
5
|
+
const decode = (stored) => ('b' in stored ? RelayBytes.fromBase64(stored.b) : stored.j);
|
|
6
|
+
/**
|
|
7
|
+
* What `ta relay --data <dir>` keeps of a room or a copy, as a Durable Object keeps it: one directory each, the values in
|
|
8
|
+
* memory and in one file, written whole after each change (rooms hold at most a few megabytes, copies eight).
|
|
9
|
+
*/
|
|
10
|
+
export class FileRelayStorage {
|
|
11
|
+
constructor(dir,
|
|
12
|
+
/** Its alarm ran: what it held went with it (a copy that ran out, a room unused for long). */
|
|
13
|
+
onAlarm) {
|
|
14
|
+
this.dir = dir;
|
|
15
|
+
this.onAlarm = onAlarm;
|
|
16
|
+
this.file = join(dir, 'values.json');
|
|
17
|
+
this.values = FileRelayStorage.load(this.file);
|
|
18
|
+
const alarm = this.values.get(ALARM);
|
|
19
|
+
if (typeof alarm === 'number') {
|
|
20
|
+
this.schedule(alarm);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** The ids of what a directory of the relay holds. */
|
|
24
|
+
static ids(root) {
|
|
25
|
+
try {
|
|
26
|
+
return readdirSync(root, { withFileTypes: true })
|
|
27
|
+
.filter(entry => entry.isDirectory())
|
|
28
|
+
.map(entry => entry.name);
|
|
29
|
+
}
|
|
30
|
+
catch (_a) {
|
|
31
|
+
return [];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
get empty() {
|
|
35
|
+
return [...this.values.keys()].every(key => key === ALARM);
|
|
36
|
+
}
|
|
37
|
+
async get(key) {
|
|
38
|
+
const value = this.values.get(key);
|
|
39
|
+
return value === undefined ? undefined : structuredClone(value);
|
|
40
|
+
}
|
|
41
|
+
async put(key, value) {
|
|
42
|
+
this.values.set(key, structuredClone(value));
|
|
43
|
+
this.save();
|
|
44
|
+
}
|
|
45
|
+
async delete(key) {
|
|
46
|
+
const had = this.values.delete(key);
|
|
47
|
+
this.save();
|
|
48
|
+
return had;
|
|
49
|
+
}
|
|
50
|
+
async list(options) {
|
|
51
|
+
const found = new Map();
|
|
52
|
+
for (const [key, value] of [...this.values].sort(([a], [b]) => a.localeCompare(b))) {
|
|
53
|
+
if (key !== ALARM && key.startsWith(options.prefix)) {
|
|
54
|
+
found.set(key, structuredClone(value));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return found;
|
|
58
|
+
}
|
|
59
|
+
async deleteAll() {
|
|
60
|
+
this.values.clear();
|
|
61
|
+
clearTimeout(this.timer);
|
|
62
|
+
rmSync(this.dir, { recursive: true, force: true });
|
|
63
|
+
}
|
|
64
|
+
async setAlarm(at) {
|
|
65
|
+
this.values.set(ALARM, at);
|
|
66
|
+
this.save();
|
|
67
|
+
this.schedule(at);
|
|
68
|
+
}
|
|
69
|
+
close() {
|
|
70
|
+
clearTimeout(this.timer);
|
|
71
|
+
}
|
|
72
|
+
schedule(at) {
|
|
73
|
+
var _a, _b;
|
|
74
|
+
clearTimeout(this.timer);
|
|
75
|
+
// Past the 24 days of a timer, it is set again then.
|
|
76
|
+
const wait = Math.max(at - Date.now(), 0);
|
|
77
|
+
this.timer = setTimeout(() => { var _a; return (wait > 2 ** 31 - 1 ? this.schedule(at) : (_a = this.onAlarm) === null || _a === void 0 ? void 0 : _a.call(this)); }, Math.min(wait, 2 ** 31 - 1));
|
|
78
|
+
(_b = (_a = this.timer).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
79
|
+
}
|
|
80
|
+
save() {
|
|
81
|
+
if (this.empty) {
|
|
82
|
+
rmSync(this.dir, { recursive: true, force: true });
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
mkdirSync(this.dir, { recursive: true, mode: 0o700 });
|
|
86
|
+
const temporary = `${this.file}.${process.pid}.tmp`;
|
|
87
|
+
writeFileSync(temporary, JSON.stringify(Object.fromEntries([...this.values].map(([key, value]) => [key, encode(value)]))), { mode: 0o600 });
|
|
88
|
+
renameSync(temporary, this.file);
|
|
89
|
+
}
|
|
90
|
+
static load(file) {
|
|
91
|
+
try {
|
|
92
|
+
const data = JSON.parse(readFileSync(file, 'utf8'));
|
|
93
|
+
return new Map(Object.entries(data).map(([key, stored]) => [key, decode(stored)]));
|
|
94
|
+
}
|
|
95
|
+
catch (_a) {
|
|
96
|
+
return new Map();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const ALARM = '\u0000alarm';
|
|
101
|
+
//# sourceMappingURL=relay-file-storage.js.map
|