@celljs/ta-relay 4.13.1 → 4.15.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.
Files changed (72) hide show
  1. package/README.en.md +9 -1
  2. package/README.md +9 -1
  3. package/esm/common/index.d.ts +3 -0
  4. package/esm/common/index.js +3 -0
  5. package/esm/common/page-language.d.ts +14 -0
  6. package/esm/common/page-language.js +27 -0
  7. package/esm/common/relay-friends.js +29 -4
  8. package/esm/common/relay-host-calls.d.ts +34 -0
  9. package/esm/common/relay-host-calls.js +126 -0
  10. package/esm/common/relay-hubs-connection.d.ts +9 -1
  11. package/esm/common/relay-hubs-connection.js +33 -1
  12. package/esm/common/relay-hubs-protocol.d.ts +9 -1
  13. package/esm/common/relay-hubs.d.ts +7 -0
  14. package/esm/common/relay-hubs.js +20 -3
  15. package/esm/common/relay-identity.d.ts +11 -0
  16. package/esm/common/relay-identity.js +12 -1
  17. package/esm/common/relay-protocol.d.ts +1 -1
  18. package/esm/common/relay-protocol.js +2 -1
  19. package/esm/common/relay-team.d.ts +169 -0
  20. package/esm/common/relay-team.js +248 -0
  21. package/esm/common/relay-web.d.ts +13 -0
  22. package/esm/common/relay-web.js +61 -0
  23. package/esm/guest/guest-i18n.d.ts +6 -1
  24. package/esm/guest/guest-i18n.js +98 -5
  25. package/esm/guest/guest.js +1 -1
  26. package/esm/join/join-i18n.d.ts +65 -0
  27. package/esm/join/join-i18n.js +233 -0
  28. package/esm/join/join.d.ts +1 -0
  29. package/esm/join/join.js +433 -0
  30. package/esm/node/relay-server.d.ts +7 -0
  31. package/esm/node/relay-server.js +28 -3
  32. package/esm/node/relay-viewer.d.ts +2 -0
  33. package/esm/node/relay-viewer.js +9 -4
  34. package/esm/viewer/viewer-i18n.d.ts +6 -1
  35. package/esm/viewer/viewer-i18n.js +107 -3
  36. package/lib/common/index.d.ts +3 -0
  37. package/lib/common/index.js +3 -0
  38. package/lib/common/page-language.d.ts +15 -0
  39. package/lib/common/page-language.js +31 -0
  40. package/lib/common/relay-friends.js +29 -4
  41. package/lib/common/relay-host-calls.d.ts +35 -0
  42. package/lib/common/relay-host-calls.js +130 -0
  43. package/lib/common/relay-hubs-connection.d.ts +9 -1
  44. package/lib/common/relay-hubs-connection.js +33 -1
  45. package/lib/common/relay-hubs-protocol.d.ts +9 -1
  46. package/lib/common/relay-hubs.d.ts +7 -0
  47. package/lib/common/relay-hubs.js +20 -3
  48. package/lib/common/relay-identity.d.ts +11 -0
  49. package/lib/common/relay-identity.js +12 -1
  50. package/lib/common/relay-protocol.d.ts +1 -1
  51. package/lib/common/relay-protocol.js +2 -1
  52. package/lib/common/relay-team.d.ts +170 -0
  53. package/lib/common/relay-team.js +252 -0
  54. package/lib/common/relay-web.d.ts +14 -0
  55. package/lib/common/relay-web.js +64 -0
  56. package/lib/guest/guest-i18n.d.ts +6 -1
  57. package/lib/guest/guest-i18n.js +99 -6
  58. package/lib/guest/guest.js +1 -1
  59. package/lib/join/join-i18n.d.ts +66 -0
  60. package/lib/join/join-i18n.js +236 -0
  61. package/lib/join/join.d.ts +2 -0
  62. package/lib/join/join.js +435 -0
  63. package/lib/node/relay-server.d.ts +7 -0
  64. package/lib/node/relay-server.js +27 -2
  65. package/lib/node/relay-viewer.d.ts +2 -0
  66. package/lib/node/relay-viewer.js +6 -1
  67. package/lib/viewer/viewer-i18n.d.ts +6 -1
  68. package/lib/viewer/viewer-i18n.js +108 -4
  69. package/package.json +4 -4
  70. package/viewer/join.html +93 -0
  71. package/viewer/page.html +9 -9
  72. package/viewer/room.html +10 -10
@@ -0,0 +1,169 @@
1
+ import { RelayHostCalls } from './relay-host-calls.js';
2
+ import { type RelayIdentity } from './relay-identity.js';
3
+ /** The path of a link that invites into a Ta, on its relay: `<relay>/j#…`; opened in a browser, the page that asks to come in. */
4
+ export declare const RELAY_JOIN_PATH = "/j";
5
+ /** Where the relay serves Ta's web page, for one let in from a browser (docs/design/ta-team.md 8.3): `/w/#pair=<pairing link>`. */
6
+ export declare const RELAY_WEB_PATH = "/w/";
7
+ /** The package that lets people into a Ta: its host answers `ask` and `wait` of a link. */
8
+ export declare const RELAY_TEAM_PACKAGE = "@celljs/ta-team";
9
+ /** A link that invites into a Ta: its device (on the relay), whose Ta, the name of the role it gives. */
10
+ export interface RelayTeamLink {
11
+ relay: string;
12
+ host: string;
13
+ device: string;
14
+ key: string;
15
+ owner: string;
16
+ role: string;
17
+ /** It shares something on its own: the name shown (`设计师`). */
18
+ what?: string;
19
+ /** The language of who made it (`zh-CN`): the page of the link speaks it, unless the browser asks Chinese or Japanese. */
20
+ language?: string;
21
+ }
22
+ /** Where one asking from a browser comes from: the page of the relay (RELAY_JOIN_PATH), not the Ta app. */
23
+ export type RelayTeamVia = 'web';
24
+ /** The answer to asking to come in. */
25
+ export interface RelayTeamAnswer {
26
+ status: 'waiting' | 'accepted' | 'refused' | 'gone';
27
+ answer?: {
28
+ box: string;
29
+ sealed: string;
30
+ };
31
+ request?: string;
32
+ /**
33
+ * Refused at once: `app`, the role is one taken in the Ta app alone (it manages members, and the Ta has no provider to
34
+ * sign in with); `account`, sign in with an account first (from a browser, for a role that manages members).
35
+ */
36
+ reason?: 'app' | 'account';
37
+ }
38
+ /** How one asking by a link may say who they are (docs/design/ta-team.md 17). */
39
+ export interface RelayTeamWays {
40
+ /** The providers the Ta signs in with: GitHub, Google, WeChat, the operator's; `device`, by a code typed at it. */
41
+ providers: {
42
+ id: string;
43
+ name: string;
44
+ device?: true;
45
+ }[];
46
+ /** They sign in first: the role manages members and they ask from a browser; otherwise signing in is theirs to choose. */
47
+ required: boolean;
48
+ /** The role manages members. */
49
+ manages?: boolean;
50
+ }
51
+ /**
52
+ * A sign-in started: `url`, where the browser goes, to come back to the page of the link; `device`, a code to type at
53
+ * the provider's `uri` (GitHub's device flow), the Ta asking it whether it was ({@link RelayTeamJoining.typed}).
54
+ */
55
+ export interface RelayTeamSignInStart {
56
+ state: string;
57
+ url?: string;
58
+ device?: {
59
+ code: string;
60
+ uri: string;
61
+ expiresAt: number;
62
+ };
63
+ }
64
+ /**
65
+ * Signed in with a provider: as it came back to the page of the link (the code goes to the Ta, which alone can use it),
66
+ * or in a browser for the Ta app, by the handoff id the app gave it (RelayTeamLinkUtil.signInFor), or by a code typed,
67
+ * by the state of its sign-in as the handoff.
68
+ */
69
+ export type RelayTeamSignIn = {
70
+ state: string;
71
+ code: string;
72
+ } | {
73
+ handoff: string;
74
+ };
75
+ /** A sign-in the page of the link does in a browser for the Ta app (the fragment's `p` and `x`). */
76
+ export interface RelayTeamHandoff {
77
+ provider: string;
78
+ handoff: string;
79
+ }
80
+ /**
81
+ * What lets one asking by a link in, sealed for them alone (docs/design/ta-team.md 8.1): the link's key is shared by all
82
+ * who have the link, so the device of the one let in is sealed with ECDH between a key of the moment of each side.
83
+ */
84
+ export declare namespace RelayTeamSeal {
85
+ /** A pair of keys for one request: its public part goes with it. */
86
+ function keys(name: string): Promise<RelayIdentity>;
87
+ function seal(box: string, salt: string, text: string): Promise<{
88
+ box: string;
89
+ sealed: string;
90
+ }>;
91
+ /** Throws when it was not sealed for these keys. */
92
+ function open(keys: RelayIdentity, answer: {
93
+ box: string;
94
+ sealed: string;
95
+ }, salt: string): Promise<string>;
96
+ /** Whether a public key is one: what a request carries is checked before anything is sealed for it. */
97
+ function isKey(box: unknown): box is string;
98
+ }
99
+ /** A link that invites into a Ta: `<relay>/j#v=1&h=…&d=…&k=…&o=…&r=…`; all but the relay in the fragment, which is not sent to it. */
100
+ export declare namespace RelayTeamLinkUtil {
101
+ function format(link: RelayTeamLink): string;
102
+ /** Undefined for text that is not such a link. */
103
+ function parse(text: string): RelayTeamLink | undefined;
104
+ /**
105
+ * The page of a link that signs in with `provider` for the Ta app, in a browser (docs/design/ta-team.md 17): the app
106
+ * waits for the account under `handoff`, an id of its own; all of it in the fragment.
107
+ */
108
+ function signInFor(link: RelayTeamLink, provider: string, handoff: string): string;
109
+ /** The sign-in for the Ta app a link of {@link signInFor} asks, if it does. */
110
+ function handoffOf(text: string): RelayTeamHandoff | undefined;
111
+ /** Ta's web page of the relay, signing in with a pairing link (in the fragment, which the relay is not sent). */
112
+ function web(relay: string, pairing: string): string;
113
+ }
114
+ /**
115
+ * Asking to come into another Ta by its link (docs/design/ta-team.md 8.2, 8.3): a connection to it through the relay with
116
+ * the device of the link, which may only ask and wait for the answer; the device for the one let in comes sealed for a
117
+ * key of this request alone, then the page connects with it. The keys of a request may be kept (`keys`) to wait again
118
+ * after the page was opened again (`resume`).
119
+ */
120
+ export declare class RelayTeamJoining {
121
+ protected readonly calls: RelayHostCalls;
122
+ protected own?: RelayIdentity;
123
+ protected closed: boolean;
124
+ constructor(link: RelayTeamLink);
125
+ /** The keys of the request asked. */
126
+ get keys(): RelayIdentity | undefined;
127
+ /**
128
+ * How to say who one is. A Ta of before has no accounts for those asking (its link may not call `ways`): none needed.
129
+ * A Ta that cannot be reached throws, so that the page says so rather than hide the ways to sign in.
130
+ */
131
+ ways(via?: RelayTeamVia): Promise<RelayTeamWays>;
132
+ /** A call the relay or the Ta did not answer: the Ta is off, or the connection was lost. */
133
+ static unreachable(failure: unknown): boolean;
134
+ /** Where the browser goes to sign in with a provider, and the state it comes back with to the page of the link; for the app, its handoff. */
135
+ signIn(provider: string, handoff?: string): Promise<RelayTeamSignInStart>;
136
+ /** The browser came back signed in for the Ta app: the Ta verifies the account and keeps it for the app. */
137
+ signedIn(state: string, code: string): Promise<{
138
+ account: {
139
+ id: string;
140
+ name?: string;
141
+ };
142
+ }>;
143
+ /**
144
+ * Waits for the code of a sign-in (`device` of {@link signIn}) to be typed at the provider, asking again till it was
145
+ * (the account, kept by the Ta under the handoff of the sign-in, or its state), this stops or `until` passes. A call
146
+ * the relay or the Ta did not answer (a phone away while the code was typed) is asked again; one refused throws.
147
+ */
148
+ typed(state: string, until?: number): Promise<{
149
+ id: string;
150
+ name?: string;
151
+ } | undefined>;
152
+ /**
153
+ * Waits for the browser to sign in for the app, asking again till it did (the account), this stops or `until`
154
+ * passes; a call that fails (the app was away while the browser was in front) is asked again.
155
+ */
156
+ handedOff(handoff: string, until?: number): Promise<{
157
+ id: string;
158
+ name?: string;
159
+ } | undefined>;
160
+ /** Asks with a name (`via` a browser, maybe signed in): the answer, or that it waits. */
161
+ ask(name: string, via?: RelayTeamVia, signIn?: RelayTeamSignIn): Promise<RelayTeamAnswer>;
162
+ /** Goes on with a request asked before, with its keys. */
163
+ resume(keys: RelayIdentity): void;
164
+ /** Waits for the answer to a request, asking again till it comes or this stops. */
165
+ wait(request: string): Promise<RelayTeamAnswer>;
166
+ /** The pairing link of the device sealed in an answer. */
167
+ pairing(request: string, answer: NonNullable<RelayTeamAnswer['answer']>): Promise<string>;
168
+ close(): void;
169
+ }
@@ -0,0 +1,248 @@
1
+ import { RelayBytes } from './relay-bytes.js';
2
+ import { RelayHostCalls } from './relay-host-calls.js';
3
+ import { RelayIdentityUtil, RelayKeys } from './relay-identity.js';
4
+ import { RelayPairingUtil } from './relay-pairing.js';
5
+ /** The path of a link that invites into a Ta, on its relay: `<relay>/j#…`; opened in a browser, the page that asks to come in. */
6
+ export const RELAY_JOIN_PATH = '/j';
7
+ /** Where the relay serves Ta's web page, for one let in from a browser (docs/design/ta-team.md 8.3): `/w/#pair=<pairing link>`. */
8
+ export const RELAY_WEB_PATH = '/w/';
9
+ /** The package that lets people into a Ta: its host answers `ask` and `wait` of a link. */
10
+ export const RELAY_TEAM_PACKAGE = '@celljs/ta-team';
11
+ const SEAL_INFO = 'ta-team/1 device';
12
+ /**
13
+ * What lets one asking by a link in, sealed for them alone (docs/design/ta-team.md 8.1): the link's key is shared by all
14
+ * who have the link, so the device of the one let in is sealed with ECDH between a key of the moment of each side.
15
+ */
16
+ export var RelayTeamSeal;
17
+ (function (RelayTeamSeal) {
18
+ /** A pair of keys for one request: its public part goes with it. */
19
+ function keys(name) {
20
+ return RelayIdentityUtil.create(name);
21
+ }
22
+ RelayTeamSeal.keys = keys;
23
+ async function seal(box, salt, text) {
24
+ const own = await RelayIdentityUtil.create('ta');
25
+ const key = await RelayKeys.aesKey(await RelayIdentityUtil.agree(own, box, salt, SEAL_INFO));
26
+ return { box: own.box, sealed: await RelayKeys.seal(key, text) };
27
+ }
28
+ RelayTeamSeal.seal = seal;
29
+ /** Throws when it was not sealed for these keys. */
30
+ async function open(keys, answer, salt) {
31
+ const key = await RelayKeys.aesKey(await RelayIdentityUtil.agree(keys, answer.box, salt, SEAL_INFO));
32
+ return RelayKeys.open(key, answer.sealed);
33
+ }
34
+ RelayTeamSeal.open = open;
35
+ /** Whether a public key is one: what a request carries is checked before anything is sealed for it. */
36
+ function isKey(box) {
37
+ return typeof box === 'string' && /^[A-Za-z0-9_-]{80,100}$/.test(box) && RelayBytes.fromBase64(box).length === 65;
38
+ }
39
+ RelayTeamSeal.isKey = isKey;
40
+ })(RelayTeamSeal || (RelayTeamSeal = {}));
41
+ /** A link that invites into a Ta: `<relay>/j#v=1&h=…&d=…&k=…&o=…&r=…`; all but the relay in the fragment, which is not sent to it. */
42
+ export var RelayTeamLinkUtil;
43
+ (function (RelayTeamLinkUtil) {
44
+ const ID = /^[A-Za-z0-9_-]{16,64}$/;
45
+ function format(link) {
46
+ const fragment = new URLSearchParams({ v: '1', h: link.host, d: link.device, k: link.key, o: link.owner, r: link.role });
47
+ if (link.what) {
48
+ fragment.set('w', link.what);
49
+ }
50
+ if (link.language) {
51
+ fragment.set('l', link.language);
52
+ }
53
+ return `${link.relay.replace(/\/+$/, '')}${RELAY_JOIN_PATH}#${fragment}`;
54
+ }
55
+ RelayTeamLinkUtil.format = format;
56
+ /** Undefined for text that is not such a link. */
57
+ function parse(text) {
58
+ var _a;
59
+ var _b, _c, _d;
60
+ let url;
61
+ try {
62
+ url = new URL(text.trim());
63
+ }
64
+ catch (_e) {
65
+ return undefined;
66
+ }
67
+ const fragment = new URLSearchParams(url.hash.slice(1));
68
+ const [host, device, key] = ['h', 'd', 'k'].map(name => { var _a; return (_a = fragment.get(name)) !== null && _a !== void 0 ? _a : ''; });
69
+ if (!/^https?:$/.test(url.protocol) ||
70
+ url.pathname !== RELAY_JOIN_PATH ||
71
+ fragment.get('v') !== '1' ||
72
+ !ID.test(host) ||
73
+ !ID.test(device) ||
74
+ !/^[A-Za-z0-9_-]{43}$/.test(key)) {
75
+ return undefined;
76
+ }
77
+ const what = (_a = fragment.get('w')) === null || _a === void 0 ? void 0 : _a.slice(0, 80);
78
+ const language = (_b = fragment.get('l')) !== null && _b !== void 0 ? _b : '';
79
+ return {
80
+ relay: url.origin,
81
+ host,
82
+ device,
83
+ key,
84
+ owner: (_c = fragment.get('o')) !== null && _c !== void 0 ? _c : '',
85
+ role: (_d = fragment.get('r')) !== null && _d !== void 0 ? _d : '',
86
+ ...(what ? { what } : {}),
87
+ ...(/^[a-z]{2}(-[A-Za-z]{2,4})?$/.test(language) ? { language } : {}),
88
+ };
89
+ }
90
+ RelayTeamLinkUtil.parse = parse;
91
+ /**
92
+ * The page of a link that signs in with `provider` for the Ta app, in a browser (docs/design/ta-team.md 17): the app
93
+ * waits for the account under `handoff`, an id of its own; all of it in the fragment.
94
+ */
95
+ function signInFor(link, provider, handoff) {
96
+ return `${format(link)}&${new URLSearchParams({ p: provider, x: handoff })}`;
97
+ }
98
+ RelayTeamLinkUtil.signInFor = signInFor;
99
+ /** The sign-in for the Ta app a link of {@link signInFor} asks, if it does. */
100
+ function handoffOf(text) {
101
+ var _a, _b;
102
+ let url;
103
+ try {
104
+ url = new URL(text.trim());
105
+ }
106
+ catch (_c) {
107
+ return undefined;
108
+ }
109
+ const fragment = new URLSearchParams(url.hash.slice(1));
110
+ const provider = (_a = fragment.get('p')) !== null && _a !== void 0 ? _a : '';
111
+ const handoff = (_b = fragment.get('x')) !== null && _b !== void 0 ? _b : '';
112
+ return /^[a-z0-9_-]{1,32}$/.test(provider) && /^[A-Za-z0-9_-]{16,64}$/.test(handoff) ? { provider, handoff } : undefined;
113
+ }
114
+ RelayTeamLinkUtil.handoffOf = handoffOf;
115
+ /** Ta's web page of the relay, signing in with a pairing link (in the fragment, which the relay is not sent). */
116
+ function web(relay, pairing) {
117
+ return `${relay.replace(/\/+$/, '')}${RELAY_WEB_PATH}#${new URLSearchParams({ pair: pairing })}`;
118
+ }
119
+ RelayTeamLinkUtil.web = web;
120
+ })(RelayTeamLinkUtil || (RelayTeamLinkUtil = {}));
121
+ /**
122
+ * Asking to come into another Ta by its link (docs/design/ta-team.md 8.2, 8.3): a connection to it through the relay with
123
+ * the device of the link, which may only ask and wait for the answer; the device for the one let in comes sealed for a
124
+ * key of this request alone, then the page connects with it. The keys of a request may be kept (`keys`) to wait again
125
+ * after the page was opened again (`resume`).
126
+ */
127
+ export class RelayTeamJoining {
128
+ constructor(link) {
129
+ this.closed = false;
130
+ this.calls = new RelayHostCalls({ relay: link.relay, host: link.host, device: link.device, key: link.key, name: link.owner }, RELAY_TEAM_PACKAGE);
131
+ }
132
+ /** The keys of the request asked. */
133
+ get keys() {
134
+ return this.own;
135
+ }
136
+ /**
137
+ * How to say who one is. A Ta of before has no accounts for those asking (its link may not call `ways`): none needed.
138
+ * A Ta that cannot be reached throws, so that the page says so rather than hide the ways to sign in.
139
+ */
140
+ async ways(via) {
141
+ try {
142
+ return (await this.calls.callHost('ways', via ? { via } : {}));
143
+ }
144
+ catch (failure) {
145
+ if (RelayTeamJoining.unreachable(failure)) {
146
+ throw failure;
147
+ }
148
+ return { providers: [], required: false };
149
+ }
150
+ }
151
+ /** A call the relay or the Ta did not answer: the Ta is off, or the connection was lost. */
152
+ static unreachable(failure) {
153
+ return /did not answer|was lost/i.test(failure instanceof Error ? failure.message : String(failure));
154
+ }
155
+ /** Where the browser goes to sign in with a provider, and the state it comes back with to the page of the link; for the app, its handoff. */
156
+ async signIn(provider, handoff) {
157
+ return (await this.calls.callHost('signIn', { provider, ...(handoff ? { handoff } : {}) }));
158
+ }
159
+ /** The browser came back signed in for the Ta app: the Ta verifies the account and keeps it for the app. */
160
+ async signedIn(state, code) {
161
+ const answer = (await this.calls.callHost('signedIn', { state, code }));
162
+ return { account: answer.account };
163
+ }
164
+ /**
165
+ * Waits for the code of a sign-in (`device` of {@link signIn}) to be typed at the provider, asking again till it was
166
+ * (the account, kept by the Ta under the handoff of the sign-in, or its state), this stops or `until` passes. A call
167
+ * the relay or the Ta did not answer (a phone away while the code was typed) is asked again; one refused throws.
168
+ */
169
+ async typed(state, until = Date.now() + 15 * 60000) {
170
+ while (!this.closed && Date.now() < until) {
171
+ let answer;
172
+ try {
173
+ answer = (await this.calls.callHost('signedIn', { state }));
174
+ }
175
+ catch (failure) {
176
+ if (this.closed) {
177
+ return undefined;
178
+ }
179
+ if (!RelayTeamJoining.unreachable(failure)) {
180
+ throw failure;
181
+ }
182
+ await new Promise(resolve => setTimeout(resolve, 1000));
183
+ continue;
184
+ }
185
+ if (answer.status === 'done' && answer.account) {
186
+ return answer.account;
187
+ }
188
+ }
189
+ return undefined;
190
+ }
191
+ /**
192
+ * Waits for the browser to sign in for the app, asking again till it did (the account), this stops or `until`
193
+ * passes; a call that fails (the app was away while the browser was in front) is asked again.
194
+ */
195
+ async handedOff(handoff, until = Date.now() + 10 * 60000) {
196
+ while (!this.closed && Date.now() < until) {
197
+ try {
198
+ const answer = (await this.calls.callHost('handedOff', handoff));
199
+ if (answer.status === 'done' && answer.account) {
200
+ return answer.account;
201
+ }
202
+ }
203
+ catch (_a) {
204
+ await new Promise(resolve => setTimeout(resolve, 1000));
205
+ }
206
+ }
207
+ return undefined;
208
+ }
209
+ /** Asks with a name (`via` a browser, maybe signed in): the answer, or that it waits. */
210
+ async ask(name, via, signIn) {
211
+ const own = await RelayTeamSeal.keys(name || 'ta');
212
+ const answer = (await this.calls.callHost('ask', { name, box: own.box, ...(via ? { via } : {}), ...(signIn ? { signIn } : {}) }));
213
+ // The keys of a request asked: one refused, or that failed, leaves those of a request asked before.
214
+ if (answer.status !== 'refused') {
215
+ this.own = own;
216
+ }
217
+ return answer;
218
+ }
219
+ /** Goes on with a request asked before, with its keys. */
220
+ resume(keys) {
221
+ this.own = keys;
222
+ }
223
+ /** Waits for the answer to a request, asking again till it comes or this stops. */
224
+ async wait(request) {
225
+ for (;;) {
226
+ if (this.closed) {
227
+ return { status: 'gone' };
228
+ }
229
+ const answer = (await this.calls.callHost('wait', request));
230
+ if (answer.status !== 'waiting') {
231
+ return answer;
232
+ }
233
+ }
234
+ }
235
+ /** The pairing link of the device sealed in an answer. */
236
+ async pairing(request, answer) {
237
+ if (!this.own) {
238
+ throw new Error('Ask first.');
239
+ }
240
+ const device = JSON.parse(await RelayTeamSeal.open(this.own, answer, request));
241
+ return RelayPairingUtil.link(RelayPairingUtil.parse(RelayPairingUtil.link(device)));
242
+ }
243
+ close() {
244
+ this.closed = true;
245
+ this.calls.close();
246
+ }
247
+ }
248
+ //# sourceMappingURL=relay-team.js.map
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Ta's web page as the relay serves it under RELAY_WEB_PATH (docs/design/ta-team.md 8.3): the files of the page of
3
+ * `ta serve` (`apps/ta/dist/public`), its base made that path, for one let in from a browser; it signs in with the
4
+ * pairing link of its fragment and reaches the Ta through the relay, end to end, as the Ta app does.
5
+ */
6
+ export declare namespace RelayWebUtil {
7
+ /** The page, its base the path it is served at. */
8
+ function index(html: string): string;
9
+ /** The file of the page a path asks for, relative to its directory; undefined for none (outside it, or not a plain path). */
10
+ function file(path: string): string | undefined;
11
+ /** How a file of the page is answered: the page never framed nor cached old, the files with hashed names kept long. */
12
+ function headers(file: string): Record<string, string>;
13
+ }
@@ -0,0 +1,61 @@
1
+ import { RELAY_WEB_PATH } from './relay-team.js';
2
+ const TYPES = {
3
+ html: 'text/html; charset=utf-8',
4
+ js: 'text/javascript; charset=utf-8',
5
+ mjs: 'text/javascript; charset=utf-8',
6
+ css: 'text/css; charset=utf-8',
7
+ json: 'application/json',
8
+ webmanifest: 'application/manifest+json',
9
+ svg: 'image/svg+xml',
10
+ png: 'image/png',
11
+ jpg: 'image/jpeg',
12
+ webp: 'image/webp',
13
+ ico: 'image/x-icon',
14
+ woff2: 'font/woff2',
15
+ woff: 'font/woff',
16
+ ttf: 'font/ttf',
17
+ wasm: 'application/wasm',
18
+ txt: 'text/plain; charset=utf-8',
19
+ };
20
+ /**
21
+ * Ta's web page as the relay serves it under RELAY_WEB_PATH (docs/design/ta-team.md 8.3): the files of the page of
22
+ * `ta serve` (`apps/ta/dist/public`), its base made that path, for one let in from a browser; it signs in with the
23
+ * pairing link of its fragment and reaches the Ta through the relay, end to end, as the Ta app does.
24
+ */
25
+ export var RelayWebUtil;
26
+ (function (RelayWebUtil) {
27
+ /** The page, its base the path it is served at. */
28
+ function index(html) {
29
+ return html.replace(/<base href="\/">/, `<base href="${RELAY_WEB_PATH}">`);
30
+ }
31
+ RelayWebUtil.index = index;
32
+ /** The file of the page a path asks for, relative to its directory; undefined for none (outside it, or not a plain path). */
33
+ function file(path) {
34
+ if (!path.startsWith(RELAY_WEB_PATH)) {
35
+ return undefined;
36
+ }
37
+ const rest = path.slice(RELAY_WEB_PATH.length) || 'index.html';
38
+ const parts = rest.split('/');
39
+ if (parts.some(part => !part || part === '.' || part === '..' || !/^[\w.@-]+$/.test(part))) {
40
+ return undefined;
41
+ }
42
+ return rest;
43
+ }
44
+ RelayWebUtil.file = file;
45
+ /** How a file of the page is answered: the page never framed nor cached old, the files with hashed names kept long. */
46
+ function headers(file) {
47
+ var _a;
48
+ const type = (_a = TYPES[file.slice(file.lastIndexOf('.') + 1).toLowerCase()]) !== null && _a !== void 0 ? _a : 'application/octet-stream';
49
+ const page = file === 'index.html';
50
+ return {
51
+ 'content-type': type,
52
+ // no-transform: a CDN in front (Cloudflare's analytics) puts no script of its own into the page, which keeps keys.
53
+ 'cache-control': file.startsWith('assets/') ? 'public, max-age=31536000, immutable, no-transform' : 'no-cache, no-transform',
54
+ 'x-content-type-options': 'nosniff',
55
+ 'referrer-policy': 'no-referrer',
56
+ ...(page ? { 'x-frame-options': 'DENY' } : {}),
57
+ };
58
+ }
59
+ RelayWebUtil.headers = headers;
60
+ })(RelayWebUtil || (RelayWebUtil = {}));
61
+ //# sourceMappingURL=relay-web.js.map
@@ -1,3 +1,4 @@
1
+ import { type PageLanguage } from '../common/page-language.js';
1
2
  declare const zh: {
2
3
  title: string;
3
4
  invalid: string;
@@ -43,8 +44,12 @@ declare const zh: {
43
44
  close: string;
44
45
  };
45
46
  export type GuestText = keyof typeof zh;
47
+ /** The words of each language of Ta, checked by scripts/check-i18n.ts. */
48
+ export declare const GUEST_WORDS: Readonly<Record<PageLanguage, typeof zh>>;
46
49
  export declare namespace GuestI18n {
47
- const chinese: boolean;
50
+ const language: PageLanguage;
51
+ /** Chinese or Japanese: Latin among the words has a space on each side. */
52
+ const cjk: boolean;
48
53
  function t(id: GuestText, values?: Record<string, string | number>): string;
49
54
  /** The time of a line: when today, else the day too. */
50
55
  function time(at: number): string;
@@ -1,3 +1,4 @@
1
+ import { pageLanguage } from '../common/page-language.js';
1
2
  const zh = {
2
3
  title: 'Ta 房间',
3
4
  invalid: '这不是一个完整的 Ta 房间邀请链接,请检查是否复制完整。',
@@ -42,6 +43,50 @@ const zh = {
42
43
  secure: '端到端加密,中继看不到内容',
43
44
  close: '关闭',
44
45
  };
46
+ const zhTw = {
47
+ title: 'Ta 房間',
48
+ invalid: '這不是一個完整的 Ta 房間邀請連結,請檢查是否複製完整。',
49
+ inTa: '這是{by}在 Ta 裡的加好友連結,要在 Ta 裡開啟:用 Ta 掃碼,或把連結貼上到 Ta 的快捷開啟。',
50
+ invited: '{by}邀請你加入',
51
+ joinHint: '房主同意後,就能在這裡和大家聊天。',
52
+ name: '你的名字',
53
+ namePlaceholder: '大家看到的名字',
54
+ join: '申請加入',
55
+ joining: '正在申請…',
56
+ keep: '你的名字和一把只屬於這個瀏覽器的鑰匙儲存在本機;清除瀏覽器資料後要重新申請。',
57
+ haveTa: '裝了 Ta?在 Ta 應用裡掃碼或貼上這個連結,還能一起玩小鎮等遊戲。',
58
+ waiting: '已向{by}申請',
59
+ waitingHint: '等對方同意。可以先關掉頁面,之後再開啟這個連結。',
60
+ cancel: '取消申請',
61
+ refused: '{by}沒有同意你加入',
62
+ again: '再申請一次',
63
+ removed: '你已被移出這個房間',
64
+ closed: '這個房間已被房主解散',
65
+ inviteGone: '邀請已失效,請向房主要一個新連結',
66
+ own: '這是你自己的房間,請在 Ta 裡開啟。',
67
+ connecting: '連線中…',
68
+ online: '另有 {n} 人線上',
69
+ nobody: '其他人都不線上',
70
+ onlineNow: '線上',
71
+ members: '成員',
72
+ owner: '房主',
73
+ you: '我',
74
+ guest: '網頁訪客',
75
+ doubleOf: '{name}的分身',
76
+ double: '分身',
77
+ task: '任務',
78
+ agentOf: '{name}的智慧體',
79
+ rename: '修改我的名字',
80
+ leave: '退出房間',
81
+ leaveAsk: '退出「{title}」?退出後要有新的邀請才能再進來。',
82
+ game: '這個房間裡有 Ta 的遊戲,裝了 Ta 才能一起玩;在這裡可以聊天。',
83
+ empty: '還沒有人說話。只有房間裡的人能看到,中繼儲存的是密文。',
84
+ placeholder: '發訊息',
85
+ send: '傳送',
86
+ failed: '沒發出去:{reason}',
87
+ secure: '端到端加密,中繼看不到內容',
88
+ close: '關閉',
89
+ };
45
90
  const en = {
46
91
  title: 'A room of Ta',
47
92
  invalid: 'This is not a whole link that invites into a room of Ta: check it was copied in full.',
@@ -86,21 +131,69 @@ const en = {
86
131
  secure: 'End to end: the relay reads none of it',
87
132
  close: 'Close',
88
133
  };
134
+ const ja = {
135
+ title: 'Ta のルーム',
136
+ invalid: 'これは Ta ルームへの完全な招待リンクではありません。全体がコピーされているか確認してください。',
137
+ inTa: 'これは {by} さんが Ta で友だちを追加するリンクで、Ta で開きます。Ta でスキャンするか、Ta のクイックオープンにペーストしてください。',
138
+ invited: '{by}さんがあなたを招待しています',
139
+ joinHint: 'ルーム管理者が承認すると、ここでみんなとチャットできます。',
140
+ name: 'あなたの名前',
141
+ namePlaceholder: 'みんなに見える名前',
142
+ join: '参加をリクエスト',
143
+ joining: 'リクエスト中…',
144
+ keep: 'あなたの名前とこのブラウザ専用のキーはこのデバイスに保存されます。ブラウザデータをクリアすると再度リクエストが必要です。',
145
+ haveTa: 'Ta をインストール済みですか?Ta アプリでこのリンクをスキャンするかペーストして、タウンなどのゲームも一緒にプレイできます。',
146
+ waiting: '{by}さんにリクエスト済み',
147
+ waitingHint: '相手が承認するのを待っています。ページを閉じて、後でもう一度開くことができます。',
148
+ cancel: 'キャンセル',
149
+ refused: '{by}さんはあなたの参加を承認しませんでした',
150
+ again: 'もう一度リクエスト',
151
+ removed: 'このルームから削除されました',
152
+ closed: 'このルームはルーム管理者に閉じられました',
153
+ inviteGone: '招待が無効です。ルーム管理者に新しいリンクをリクエストしてください',
154
+ own: 'これはあなた自身のルームです。Ta で開いてください。',
155
+ connecting: '接続中…',
156
+ online: '他に {n} 人がオンライン',
157
+ nobody: '他に誰もオンラインではありません',
158
+ onlineNow: 'オンライン',
159
+ members: 'メンバー',
160
+ owner: 'ルーム管理者',
161
+ you: '私',
162
+ guest: 'ウェブゲスト',
163
+ doubleOf: '{name}の分身',
164
+ double: '分身',
165
+ task: 'タスク',
166
+ agentOf: '{name}のエージェント',
167
+ rename: '名前を変更',
168
+ leave: 'ルームを退出',
169
+ leaveAsk: '「{title}」を退出しますか?退出後は新しい招待でのみ戻ることができます。',
170
+ game: 'このルームには Ta のゲームがあり、プレイするには Ta が必要です。ここでチャットできます。',
171
+ empty: 'まだ誰も話していません。ルーム内の人だけが見ることができ、リレーは暗号化されて保存されます。',
172
+ placeholder: 'メッセージ',
173
+ send: '送信',
174
+ failed: '送信されませんでした:{reason}',
175
+ secure: 'エンドツーエンド暗号化。リレーは内容を読むことができません',
176
+ close: '閉じる',
177
+ };
178
+ /** The words of each language of Ta, checked by scripts/check-i18n.ts. */
179
+ export const GUEST_WORDS = { 'zh-CN': zh, 'zh-TW': zhTw, 'en-US': en, 'ja-JP': ja };
89
180
  export var GuestI18n;
90
181
  (function (GuestI18n) {
91
- GuestI18n.chinese = typeof navigator !== 'undefined' && /^zh\b/i.test(navigator.language);
182
+ GuestI18n.language = pageLanguage();
183
+ /** Chinese or Japanese: Latin among the words has a space on each side. */
184
+ GuestI18n.cjk = GuestI18n.language !== 'en-US';
185
+ const words = GUEST_WORDS[GuestI18n.language];
92
186
  function t(id, values = {}) {
93
- return (GuestI18n.chinese ? zh : en)[id].replace(/\{(\w+)\}/g, (_, name) => { var _a; return String((_a = values[name]) !== null && _a !== void 0 ? _a : ''); });
187
+ return words[id].replace(/\{(\w+)\}/g, (_, name) => { var _a; return String((_a = values[name]) !== null && _a !== void 0 ? _a : ''); });
94
188
  }
95
189
  GuestI18n.t = t;
96
190
  /** The time of a line: when today, else the day too. */
97
191
  function time(at) {
98
192
  const date = new Date(at);
99
- const language = GuestI18n.chinese ? 'zh-CN' : undefined;
100
- const clock = date.toLocaleTimeString(language, { hour: '2-digit', minute: '2-digit' });
193
+ const clock = date.toLocaleTimeString(GuestI18n.language, { hour: '2-digit', minute: '2-digit' });
101
194
  return date.toDateString() === new Date().toDateString()
102
195
  ? clock
103
- : `${date.toLocaleDateString(language, { month: 'numeric', day: 'numeric' })} ${clock}`;
196
+ : `${date.toLocaleDateString(GuestI18n.language, { month: 'numeric', day: 'numeric' })} ${clock}`;
104
197
  }
105
198
  GuestI18n.time = time;
106
199
  })(GuestI18n || (GuestI18n = {}));
@@ -83,7 +83,7 @@ class GuestPage {
83
83
  store: new BrowserStore(),
84
84
  relay: () => location.origin,
85
85
  guest: true,
86
- name: () => (GuestI18n.chinese ? '访客' : 'Guest'),
86
+ name: () => ({ 'zh-CN': '访客', 'zh-TW': '訪客', 'en-US': 'Guest', 'ja-JP': 'ゲスト' })[GuestI18n.language],
87
87
  });
88
88
  this.screen = '';
89
89
  this.stuck = true;