@celljs/ta-relay 4.15.2 → 4.17.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 (58) hide show
  1. package/esm/common/index.d.ts +1 -0
  2. package/esm/common/index.js +1 -0
  3. package/esm/common/relay-content.d.ts +13 -0
  4. package/esm/common/relay-content.js +42 -0
  5. package/esm/common/relay-conversation.d.ts +14 -0
  6. package/esm/common/relay-hub.js +2 -2
  7. package/esm/common/relay-hubs-identity.d.ts +36 -0
  8. package/esm/common/relay-hubs-identity.js +156 -0
  9. package/esm/common/relay-hubs-protocol.d.ts +4 -0
  10. package/esm/common/relay-hubs-util.d.ts +11 -0
  11. package/esm/common/relay-hubs-util.js +74 -0
  12. package/esm/common/relay-hubs.d.ts +17 -44
  13. package/esm/common/relay-hubs.js +37 -235
  14. package/esm/common/relay-protocol.d.ts +9 -0
  15. package/esm/common/relay-protocol.js +13 -0
  16. package/esm/common/relay-spot.d.ts +15 -3
  17. package/esm/common/relay-spot.js +17 -5
  18. package/esm/node/index.d.ts +1 -0
  19. package/esm/node/index.js +1 -0
  20. package/esm/node/relay-memory-storage.d.ts +26 -0
  21. package/esm/node/relay-memory-storage.js +60 -0
  22. package/esm/node/relay-server.d.ts +14 -26
  23. package/esm/node/relay-server.js +33 -60
  24. package/esm/node/relay-store.js +4 -4
  25. package/esm/node/relays-file.d.ts +27 -0
  26. package/esm/node/relays-file.js +59 -4
  27. package/esm/viewer/viewer.js +33 -1
  28. package/lib/common/index.d.ts +1 -0
  29. package/lib/common/index.js +1 -0
  30. package/lib/common/relay-content.d.ts +13 -0
  31. package/lib/common/relay-content.js +43 -1
  32. package/lib/common/relay-conversation.d.ts +14 -0
  33. package/lib/common/relay-hub.js +1 -1
  34. package/lib/common/relay-hubs-identity.d.ts +37 -0
  35. package/lib/common/relay-hubs-identity.js +160 -0
  36. package/lib/common/relay-hubs-protocol.d.ts +4 -0
  37. package/lib/common/relay-hubs-util.d.ts +12 -0
  38. package/lib/common/relay-hubs-util.js +77 -0
  39. package/lib/common/relay-hubs.d.ts +17 -44
  40. package/lib/common/relay-hubs.js +36 -234
  41. package/lib/common/relay-protocol.d.ts +9 -0
  42. package/lib/common/relay-protocol.js +15 -1
  43. package/lib/common/relay-spot.d.ts +15 -3
  44. package/lib/common/relay-spot.js +17 -5
  45. package/lib/node/index.d.ts +1 -0
  46. package/lib/node/index.js +1 -0
  47. package/lib/node/relay-memory-storage.d.ts +27 -0
  48. package/lib/node/relay-memory-storage.js +65 -0
  49. package/lib/node/relay-server.d.ts +14 -26
  50. package/lib/node/relay-server.js +34 -61
  51. package/lib/node/relay-store.js +3 -3
  52. package/lib/node/relays-file.d.ts +27 -0
  53. package/lib/node/relays-file.js +58 -3
  54. package/lib/viewer/viewer.js +32 -0
  55. package/package.json +3 -2
  56. package/viewer/join.html +3 -3
  57. package/viewer/page.html +9 -9
  58. package/viewer/room.html +10 -10
@@ -1,11 +1,13 @@
1
1
  import { type IncomingMessage, type Server, type ServerResponse } from 'node:http';
2
2
  import type { Duplex } from 'node:stream';
3
3
  import { WebSocketConnection } from '@celljs/rpc/lib/node/websocket/websocket-connection';
4
- import { type RelayBlobStorage, RelayBlobs } from '../common/relay-blobs';
5
- import { RelayHub, type RelayHubStorage, type RelayHubWho } from '../common/relay-hub';
4
+ import { type TaDeploymentEnv } from '@celljs/ta-protocol/lib/common/deployment-protocol';
5
+ import { RelayBlobs } from '../common/relay-blobs';
6
+ import { RelayHub, type RelayHubWho } from '../common/relay-hub';
6
7
  import { RelayRoom } from '../common/relay-room';
7
8
  import { RelaySpot, type RelaySpotSocket, type RelaySpotWho } from '../common/relay-spot';
8
9
  import { FileRelayStorage } from './relay-file-storage';
10
+ import { MemoryBlobStorage, MemoryHubStorage } from './relay-memory-storage';
9
11
  export interface RelayServerOptions {
10
12
  host?: string;
11
13
  port?: number;
@@ -19,6 +21,14 @@ export interface RelayServerOptions {
19
21
  * (`ta relay` gives its own); without it, a link that invites still asks from a browser, but leads nowhere.
20
22
  */
21
23
  web?: string;
24
+ /**
25
+ * What it says of the deployment it is part of (TA_WELL_KNOWN_PATH, docs/design/ta-deploy.md 2): its name, its
26
+ * gateway, and its own address (TA_PUBLIC_URL; else the one it was reached at).
27
+ */
28
+ deployment?: TaDeploymentEnv & {
29
+ TA_PUBLIC_URL?: string;
30
+ };
31
+ version?: string;
22
32
  }
23
33
  interface Room {
24
34
  secretHash: string;
@@ -26,30 +36,6 @@ interface Room {
26
36
  clients: Map<string, WebSocketConnection>;
27
37
  relay: RelayRoom;
28
38
  }
29
- /** A copy of content in memory, deleted when it runs out. */
30
- declare class MemoryBlobStorage implements RelayBlobStorage {
31
- protected readonly values: Map<string, unknown>;
32
- protected timer?: ReturnType<typeof setTimeout>;
33
- get<T>(key: string): Promise<T | undefined>;
34
- put(key: string, value: unknown): Promise<void>;
35
- deleteAll(): Promise<void>;
36
- /** No copy is kept: nothing of it need stay in the relay's memory. */
37
- get empty(): boolean;
38
- setAlarm(at: number): Promise<void>;
39
- }
40
- /** A room in memory, as a Durable Object keeps it. */
41
- declare class MemoryHubStorage implements RelayHubStorage {
42
- protected readonly values: Map<string, unknown>;
43
- get<T>(key: string): Promise<T | undefined>;
44
- put(key: string, value: unknown): Promise<void>;
45
- delete(key: string): Promise<boolean>;
46
- list<T>(options: {
47
- prefix: string;
48
- }): Promise<Map<string, T>>;
49
- deleteAll(): Promise<void>;
50
- /** No room was made, or it was closed. */
51
- get empty(): boolean;
52
- }
53
39
  interface Hub {
54
40
  hub: RelayHub;
55
41
  storage: MemoryHubStorage | FileRelayStorage;
@@ -86,6 +72,8 @@ export declare class RelayServer {
86
72
  protected readonly spotAddresses: WeakMap<WebSocketConnection, string>;
87
73
  protected server?: Server;
88
74
  constructor(options?: RelayServerOptions);
75
+ /** What it is part of, and whether it runs and can keep what it holds: for anyone, and the probes of a platform. */
76
+ protected describe(path: string, request: IncomingMessage, response: ServerResponse): void;
89
77
  listen(): Promise<string>;
90
78
  /** A file of Ta's web page (RelayWebUtil), from the directory of the options. */
91
79
  protected web(path: string, response: ServerResponse): void;
@@ -6,6 +6,7 @@ const node_fs_1 = require("node:fs");
6
6
  const node_http_1 = require("node:http");
7
7
  const node_path_1 = require("node:path");
8
8
  const websocket_connection_1 = require("@celljs/rpc/lib/node/websocket/websocket-connection");
9
+ const deployment_protocol_1 = require("@celljs/ta-protocol/lib/common/deployment-protocol");
9
10
  const relay_blobs_1 = require("../common/relay-blobs");
10
11
  const relay_hub_1 = require("../common/relay-hub");
11
12
  const relay_info_1 = require("../common/relay-info");
@@ -15,67 +16,9 @@ const relay_spot_1 = require("../common/relay-spot");
15
16
  const relay_team_1 = require("../common/relay-team");
16
17
  const relay_web_1 = require("../common/relay-web");
17
18
  const relay_file_storage_1 = require("./relay-file-storage");
19
+ const relay_memory_storage_1 = require("./relay-memory-storage");
18
20
  const relay_viewer_1 = require("./relay-viewer");
19
21
  const WEBSOCKET = { maxPayload: 32 * 1024 * 1024, heartbeat: 30000 };
20
- /** A copy of content in memory, deleted when it runs out. */
21
- class MemoryBlobStorage {
22
- constructor() {
23
- this.values = new Map();
24
- }
25
- async get(key) {
26
- return this.values.get(key);
27
- }
28
- async put(key, value) {
29
- this.values.set(key, value);
30
- }
31
- async deleteAll() {
32
- this.values.clear();
33
- clearTimeout(this.timer);
34
- }
35
- /** No copy is kept: nothing of it need stay in the relay's memory. */
36
- get empty() {
37
- return this.values.size === 0;
38
- }
39
- async setAlarm(at) {
40
- var _a, _b;
41
- clearTimeout(this.timer);
42
- // Past the 24 days of a timer, it is found run out on the next read.
43
- this.timer = setTimeout(() => this.values.clear(), Math.min(Math.max(at - Date.now(), 0), 2 ** 31 - 1));
44
- (_b = (_a = this.timer).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
45
- }
46
- }
47
- /** A room in memory, as a Durable Object keeps it. */
48
- class MemoryHubStorage {
49
- constructor() {
50
- this.values = new Map();
51
- }
52
- async get(key) {
53
- const value = this.values.get(key);
54
- return value === undefined ? undefined : structuredClone(value);
55
- }
56
- async put(key, value) {
57
- this.values.set(key, structuredClone(value));
58
- }
59
- async delete(key) {
60
- return this.values.delete(key);
61
- }
62
- async list(options) {
63
- const found = new Map();
64
- for (const [key, value] of [...this.values].sort(([a], [b]) => a.localeCompare(b))) {
65
- if (key.startsWith(options.prefix)) {
66
- found.set(key, structuredClone(value));
67
- }
68
- }
69
- return found;
70
- }
71
- async deleteAll() {
72
- this.values.clear();
73
- }
74
- /** No room was made, or it was closed. */
75
- get empty() {
76
- return this.values.size === 0;
77
- }
78
- }
79
22
  const peer = (connection) => ({ send: text => connection.send(text), close: (code, reason) => connection.close(code, reason) });
80
23
  /**
81
24
  * `ta relay`: the relay on a machine of one's own, as the Cloudflare Worker is in the cloud. It forwards,
@@ -114,10 +57,40 @@ class RelayServer {
114
57
  }
115
58
  }
116
59
  }
60
+ /** What it is part of, and whether it runs and can keep what it holds: for anyone, and the probes of a platform. */
61
+ describe(path, request, response) {
62
+ var _a, _b;
63
+ const version = this.options.version;
64
+ let body = { ok: true, ...(version ? { version } : {}) };
65
+ let status = 200;
66
+ if (path === deployment_protocol_1.TA_WELL_KNOWN_PATH) {
67
+ const env = (_a = this.options.deployment) !== null && _a !== void 0 ? _a : {};
68
+ const forwarded = String((_b = request.headers['x-forwarded-proto']) !== null && _b !== void 0 ? _b : '')
69
+ .split(',')[0]
70
+ .trim();
71
+ const origin = request.headers.host ? `${forwarded === 'https' ? 'https' : 'http'}://${request.headers.host}` : undefined;
72
+ body = deployment_protocol_1.TaDeploymentUtil.describe(env, { relay: env.TA_PUBLIC_URL || origin }, version);
73
+ }
74
+ else if (path === '/readyz' && this.options.data) {
75
+ try {
76
+ (0, node_fs_1.accessSync)(this.options.data, node_fs_1.constants.W_OK);
77
+ }
78
+ catch (error) {
79
+ status = 503;
80
+ body = { ok: false, error: `${this.options.data} cannot be written: ${error.message}` };
81
+ }
82
+ }
83
+ response.writeHead(status, deployment_protocol_1.TaDeploymentUtil.HEADERS);
84
+ response.end(JSON.stringify(body));
85
+ }
117
86
  listen() {
118
87
  const server = (0, node_http_1.createServer)((request, response) => {
119
88
  var _a;
120
89
  const path = ((_a = request.url) !== null && _a !== void 0 ? _a : '/').split(/[?#]/)[0];
90
+ if (path === deployment_protocol_1.TA_WELL_KNOWN_PATH || path === '/healthz' || path === '/readyz') {
91
+ this.describe(path, request, response);
92
+ return;
93
+ }
121
94
  if (path === relay_info_1.RELAY_INFO_PATH) {
122
95
  // Without a directory for its data, rooms and copies live in memory: gone when the relay restarts.
123
96
  const info = relay_info_1.RelayInfoUtil.of(!!this.options.data);
@@ -263,7 +236,7 @@ class RelayServer {
263
236
  var _a;
264
237
  void ((_a = made.storage) === null || _a === void 0 ? void 0 : _a.deleteAll().then(() => this.blobs.get(id) === copy && this.blobs.delete(id)));
265
238
  })
266
- : new MemoryBlobStorage();
239
+ : new relay_memory_storage_1.MemoryBlobStorage();
267
240
  made.storage = storage;
268
241
  made.blobs = new relay_blobs_1.RelayBlobs(storage);
269
242
  copy = made;
@@ -503,7 +476,7 @@ class RelayServer {
503
476
  const made = { sockets };
504
477
  const storage = this.options.data
505
478
  ? new relay_file_storage_1.FileRelayStorage((0, node_path_1.join)(this.options.data, 'hubs', id), () => { var _a; return void ((_a = made.hub) === null || _a === void 0 ? void 0 : _a.alarm().then(() => this.dropHub(id, made))); })
506
- : new MemoryHubStorage();
479
+ : new relay_memory_storage_1.MemoryHubStorage();
507
480
  made.storage = storage;
508
481
  made.hub = new relay_hub_1.RelayHub(storage, () => [...sockets.keys()].map(socketOf));
509
482
  hub = made;
@@ -41,7 +41,7 @@ class RelayStore {
41
41
  /** The relay of a device, a link or content shared: its own, else the one of relay.json, else the official one. */
42
42
  static relayOf(item, config) {
43
43
  var _a, _b;
44
- return (_b = (_a = item.relay) !== null && _a !== void 0 ? _a : config.url) !== null && _b !== void 0 ? _b : relay_protocol_1.RELAY_DEFAULT_URL;
44
+ return (_b = (_a = item.relay) !== null && _a !== void 0 ? _a : config.url) !== null && _b !== void 0 ? _b : (0, relay_protocol_1.relayDefaultUrl)();
45
45
  }
46
46
  /** A share still on, by its id. */
47
47
  share(shareId, now = Date.now()) {
@@ -89,7 +89,7 @@ class RelayStore {
89
89
  const expiresAt = days ? now + days * 24 * 60 * 60 * 1000 : undefined;
90
90
  const offline = input.offline && input.access !== 'self' && input.access !== 'people';
91
91
  const before = this.read();
92
- const relay = ((_b = (_a = input.relay) !== null && _a !== void 0 ? _a : before.url) !== null && _b !== void 0 ? _b : relay_protocol_1.RELAY_DEFAULT_URL).replace(/\/+$/, '');
92
+ const relay = ((_b = (_a = input.relay) !== null && _a !== void 0 ? _a : before.url) !== null && _b !== void 0 ? _b : (0, relay_protocol_1.relayDefaultUrl)()).replace(/\/+$/, '');
93
93
  const content = {
94
94
  id,
95
95
  relay,
@@ -170,7 +170,7 @@ class RelayStore {
170
170
  shareRoot(root, name, now = Date.now(), relay) {
171
171
  var _a, _b, _c, _d;
172
172
  const before = this.read();
173
- const wanted = ((_a = relay !== null && relay !== void 0 ? relay : before.url) !== null && _a !== void 0 ? _a : relay_protocol_1.RELAY_DEFAULT_URL).replace(/\/+$/, '');
173
+ const wanted = ((_a = relay !== null && relay !== void 0 ? relay : before.url) !== null && _a !== void 0 ? _a : (0, relay_protocol_1.relayDefaultUrl)()).replace(/\/+$/, '');
174
174
  const url = (_b = before.url) !== null && _b !== void 0 ? _b : wanted;
175
175
  const host = (_c = before.host) !== null && _c !== void 0 ? _c : { id: id(), secret: (0, node_crypto_1.randomBytes)(32).toString('base64url') };
176
176
  const live = ((_d = before.shares) !== null && _d !== void 0 ? _d : []).filter(share => share.expiresAt > now);
@@ -49,12 +49,23 @@ export interface RelayStoreServiceEntry {
49
49
  options: Record<string, unknown>;
50
50
  builtin: false;
51
51
  }
52
+ /**
53
+ * A deployment of Ta the user joined (docs/design/ta-deploy.md 8): where it was found, what it said of itself, and the id
54
+ * of its relay in relays.json, which is then the default of new pairings, rooms and links.
55
+ */
56
+ export interface RelaysDeployment {
57
+ url: string;
58
+ name?: string;
59
+ gateway?: string;
60
+ relay?: string;
61
+ }
52
62
  /** relays.json in the agent directory: the relays, and which one each feature uses. Written by hand or by Ta. */
53
63
  export interface RelaysConfig {
54
64
  relays?: Record<string, RelayService>;
55
65
  use?: Record<string, string>;
56
66
  /** What a feature not in `use` takes. */
57
67
  default?: string;
68
+ deployment?: RelaysDeployment;
58
69
  }
59
70
  export type RelayServiceEntry = (({
60
71
  type: 'ta-relay';
@@ -131,6 +142,22 @@ export declare class RelaysFile {
131
142
  addStore(type: string, options: Record<string, unknown>, name?: string): string;
132
143
  /** Removes a relay that no feature uses; the official one stays. What is on it (devices, rooms, links) stays on it. */
133
144
  remove(id: string): void;
145
+ /**
146
+ * Joins a deployment, as it said of itself: its relay added and made the default (what a feature not in `use` takes),
147
+ * the deployment kept. What was made before (devices, rooms, links) stays on its relay; a choice in `use` stays too.
148
+ */
149
+ join(url: string, deployment: {
150
+ name?: string;
151
+ gateway?: string;
152
+ relay?: string;
153
+ }): RelaysDeployment;
154
+ /**
155
+ * Leaves the deployment joined: the default is the relay of this build again, and its relay goes from the list when
156
+ * no feature names it. What is on it (rooms, links) stays there, each remembering its relay.
157
+ */
158
+ leave(): void;
159
+ /** The deployment joined, as relays.json holds it: none when a hand wrote something else there. */
160
+ deployment(config?: RelaysConfig): RelaysDeployment | undefined;
134
161
  /** Which relay a feature uses from now on; `undefined` leaves it to the default. */
135
162
  use(feature: RelayFeature, id: string | undefined): void;
136
163
  /** Written whole, its other keys as they were, through a temporary file. */
@@ -170,7 +170,7 @@ class RelaysFile {
170
170
  const entries = [];
171
171
  const own = config.relays && typeof config.relays === 'object' ? config.relays : {};
172
172
  if (!own[exports.OFFICIAL_RELAY]) {
173
- entries.push({ id: exports.OFFICIAL_RELAY, type: 'ta-relay', url: relay_protocol_1.RELAY_DEFAULT_URL, name: 'relay.cellbang.com', builtin: true });
173
+ entries.push({ id: exports.OFFICIAL_RELAY, type: 'ta-relay', url: (0, relay_protocol_1.relayDefaultUrl)(), name: new URL((0, relay_protocol_1.relayDefaultUrl)()).host, builtin: true });
174
174
  }
175
175
  for (const [id, service] of Object.entries(own)) {
176
176
  if (!ID.test(id) || !service || typeof service !== 'object') {
@@ -204,7 +204,7 @@ class RelaysFile {
204
204
  if (service) {
205
205
  return service;
206
206
  }
207
- const url = fallback !== null && fallback !== void 0 ? fallback : relay_protocol_1.RELAY_DEFAULT_URL;
207
+ const url = fallback !== null && fallback !== void 0 ? fallback : (0, relay_protocol_1.relayDefaultUrl)();
208
208
  return (_a = this.services(config).find(entry => RelayServiceUtil.isRelay(entry) && entry.url === url)) !== null && _a !== void 0 ? _a : { id: '', type: 'ta-relay', url, builtin: false };
209
209
  }
210
210
  /**
@@ -222,7 +222,7 @@ class RelaysFile {
222
222
  /** The address a feature uses: its service, else `fallback` (the relay of relay.json), else the official one. */
223
223
  urlFor(feature, fallback) {
224
224
  const service = this.serviceFor(feature, fallback);
225
- return RelayServiceUtil.isRelay(service) ? service.url : (fallback !== null && fallback !== void 0 ? fallback : relay_protocol_1.RELAY_DEFAULT_URL);
225
+ return RelayServiceUtil.isRelay(service) ? service.url : (fallback !== null && fallback !== void 0 ? fallback : (0, relay_protocol_1.relayDefaultUrl)());
226
226
  }
227
227
  /** Adds a relay by its address: its id from its host name. The id, and the one before for the same address. */
228
228
  add(url, name) {
@@ -330,6 +330,61 @@ class RelaysFile {
330
330
  delete relays[id];
331
331
  this.write({ ...config, relays });
332
332
  }
333
+ /**
334
+ * Joins a deployment, as it said of itself: its relay added and made the default (what a feature not in `use` takes),
335
+ * the deployment kept. What was made before (devices, rooms, links) stays on its relay; a choice in `use` stays too.
336
+ */
337
+ join(url, deployment) {
338
+ var _a, _b;
339
+ const relay = deployment.relay ? this.add(deployment.relay, deployment.name) : undefined;
340
+ const config = this.read();
341
+ const name = (_a = deployment.name) === null || _a === void 0 ? void 0 : _a.trim().slice(0, 80);
342
+ const joined = {
343
+ url,
344
+ ...(name ? { name } : {}),
345
+ ...(deployment.gateway ? { gateway: deployment.gateway } : {}),
346
+ ...(relay ? { relay } : {}),
347
+ };
348
+ const { default: before, ...rest } = config;
349
+ const fallback = relay !== null && relay !== void 0 ? relay : (before === ((_b = config.deployment) === null || _b === void 0 ? void 0 : _b.relay) ? undefined : before);
350
+ this.write({ ...rest, ...(fallback ? { default: fallback } : {}), deployment: joined });
351
+ return joined;
352
+ }
353
+ /**
354
+ * Leaves the deployment joined: the default is the relay of this build again, and its relay goes from the list when
355
+ * no feature names it. What is on it (rooms, links) stays there, each remembering its relay.
356
+ */
357
+ leave() {
358
+ var _a, _b;
359
+ const config = this.read();
360
+ const joined = config.deployment;
361
+ if (!joined) {
362
+ return;
363
+ }
364
+ const { deployment: _left, default: before, ...rest } = config;
365
+ const relays = { ...((_a = rest.relays) !== null && _a !== void 0 ? _a : {}) };
366
+ const used = Object.values((_b = rest.use) !== null && _b !== void 0 ? _b : {});
367
+ if (joined.relay && joined.relay !== exports.OFFICIAL_RELAY && !used.includes(joined.relay)) {
368
+ delete relays[joined.relay];
369
+ }
370
+ const kept = before && before !== joined.relay ? before : undefined;
371
+ this.write({ ...rest, relays, ...(kept ? { default: kept } : {}) });
372
+ }
373
+ /** The deployment joined, as relays.json holds it: none when a hand wrote something else there. */
374
+ deployment(config = this.safeRead().config) {
375
+ const joined = config.deployment;
376
+ if (!joined || typeof joined !== 'object' || typeof joined.url !== 'string') {
377
+ return undefined;
378
+ }
379
+ const text = (value) => (typeof value === 'string' && value ? value : undefined);
380
+ const relay = text(joined.relay);
381
+ return {
382
+ url: joined.url,
383
+ ...(text(joined.name) ? { name: text(joined.name) } : {}),
384
+ ...(text(joined.gateway) ? { gateway: text(joined.gateway) } : {}),
385
+ ...(relay && this.services(config).some(service => service.id === relay) ? { relay } : {}),
386
+ };
387
+ }
333
388
  /** Which relay a feature uses from now on; `undefined` leaves it to the default. */
334
389
  use(feature, id) {
335
390
  var _a;
@@ -5,6 +5,7 @@ const relay_card_1 = require("../common/relay-card");
5
5
  const relay_content_1 = require("../common/relay-content");
6
6
  const relay_conversation_1 = require("../common/relay-conversation");
7
7
  const relay_protocol_1 = require("../common/relay-protocol");
8
+ const relay_team_1 = require("../common/relay-team");
8
9
  const viewer_i18n_1 = require("./viewer-i18n");
9
10
  const viewer_markdown_1 = require("./viewer-markdown");
10
11
  const viewer_style_1 = require("./viewer-style");
@@ -108,6 +109,19 @@ function conversation(main, item) {
108
109
  row.append(Object.assign(el('div', said), { className: 'bubble' }));
109
110
  main.append(row);
110
111
  }
112
+ else if (message.role === 'custom') {
113
+ // A card of an extension, as its words: Ta's page draws the card itself.
114
+ const { words } = text(message.content);
115
+ if (words.trim()) {
116
+ const row = el('div');
117
+ row.className = 'msg ta';
118
+ const block = el('div');
119
+ block.className = 'text';
120
+ block.append(viewer_markdown_1.ViewerMarkdown.render(words));
121
+ row.append(block);
122
+ main.append(row);
123
+ }
124
+ }
111
125
  else if (message.role === 'assistant') {
112
126
  const row = el('div');
113
127
  row.className = 'msg ta';
@@ -317,6 +331,17 @@ function footer(card) {
317
331
  foot.append(...(card ? [] : [el('span', t('footer'))]), el('span', t('secure')));
318
332
  document.body.append(foot);
319
333
  }
334
+ /** Whether the relay serves Ta's web page (apps/ta-relay with its assets, `ta relay` given its files). */
335
+ async function richPage() {
336
+ var _a;
337
+ try {
338
+ const response = await fetch(relay_team_1.RELAY_WEB_PATH, { method: 'HEAD', cache: 'no-store' });
339
+ return response.ok && ((_a = response.headers.get('content-type')) !== null && _a !== void 0 ? _a : '').startsWith('text/html');
340
+ }
341
+ catch (_b) {
342
+ return false;
343
+ }
344
+ }
320
345
  async function show() {
321
346
  var _a;
322
347
  const main = chrome(t('title'));
@@ -332,6 +357,8 @@ async function show() {
332
357
  card(main, 'self', t(link.access));
333
358
  return;
334
359
  }
360
+ // Ta's own page, where the relay has it (RELAY_WEB_PATH), draws a conversation as Ta does: its cards, plots and 3D.
361
+ const rich = new URLSearchParams(location.search).has('plain') ? Promise.resolve(false) : richPage();
335
362
  let password;
336
363
  let error;
337
364
  for (;;) {
@@ -344,6 +371,11 @@ async function show() {
344
371
  }
345
372
  try {
346
373
  const item = await relay_content_1.RelayContentUtil.fetch(link, { password });
374
+ if (item.kind === relay_conversation_1.RELAY_CONVERSATION_KIND && (await rich)) {
375
+ relay_content_1.RelayContentHandoff.keep(link.share, item, password);
376
+ location.replace(relay_team_1.RELAY_WEB_PATH + location.hash);
377
+ return;
378
+ }
347
379
  main.replaceChildren();
348
380
  const name = document.querySelector('.top .name');
349
381
  if (name) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-relay",
3
- "version": "4.15.2",
3
+ "version": "4.17.0",
4
4
  "description": "Ta from anywhere: a relay that only forwards, and end-to-end encryption between a paired device and ta serve",
5
5
  "homepage": "https://github.com/cellbang/ta",
6
6
  "bugs": {
@@ -30,7 +30,8 @@
30
30
  "watch": "tsc -b -w --preserveWatchOutput"
31
31
  },
32
32
  "dependencies": {
33
- "@celljs/rpc": "4.15.2"
33
+ "@celljs/rpc": "4.17.0",
34
+ "@celljs/ta-protocol": "4.17.0"
34
35
  },
35
36
  "publishConfig": {
36
37
  "access": "public"