@celljs/ta-host 4.17.0 → 4.19.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 (153) hide show
  1. package/esm/node/account-service.d.ts +73 -0
  2. package/esm/node/account-service.js +304 -0
  3. package/esm/node/app-commands.d.ts +1 -1
  4. package/esm/node/app-commands.js +46 -29
  5. package/esm/node/credentials.d.ts +12 -0
  6. package/esm/node/credentials.js +35 -0
  7. package/esm/node/host-protocol.d.ts +2 -0
  8. package/esm/node/host-protocol.js +2 -0
  9. package/esm/node/index.d.ts +3 -0
  10. package/esm/node/index.js +3 -0
  11. package/esm/node/keep-awake.d.ts +3 -0
  12. package/esm/node/keep-awake.js +6 -1
  13. package/esm/node/package-hosts.d.ts +4 -0
  14. package/esm/node/package-hosts.js +18 -1
  15. package/esm/node/relay-bridge.js +1 -1
  16. package/esm/node/rooms-bridge.d.ts +10 -1
  17. package/esm/node/rooms-bridge.js +25 -0
  18. package/esm/node/screen-share/desktop-picture.d.ts +51 -0
  19. package/esm/node/screen-share/desktop-picture.js +133 -0
  20. package/esm/node/screen-share/desktop-session.d.ts +128 -0
  21. package/esm/node/screen-share/desktop-session.js +437 -0
  22. package/esm/node/screen-share/desktop-targets.d.ts +24 -0
  23. package/esm/node/screen-share/desktop-targets.js +80 -0
  24. package/esm/node/screen-share/desktop-viewer.d.ts +146 -0
  25. package/esm/node/screen-share/desktop-viewer.js +339 -0
  26. package/esm/node/screen-share/index.d.ts +8 -0
  27. package/esm/node/screen-share/index.js +9 -0
  28. package/esm/node/screen-share/rfb-client.d.ts +54 -0
  29. package/esm/node/screen-share/rfb-client.js +179 -0
  30. package/esm/node/screen-share/rfb-des.d.ts +11 -0
  31. package/esm/node/screen-share/rfb-des.js +143 -0
  32. package/esm/node/screen-share/rfb-encodings.d.ts +71 -0
  33. package/esm/node/screen-share/rfb-encodings.js +288 -0
  34. package/esm/node/screen-share/rfb-framebuffer.d.ts +16 -0
  35. package/esm/node/screen-share/rfb-framebuffer.js +53 -0
  36. package/esm/node/screen-share/rfb-protocol.d.ts +90 -0
  37. package/esm/node/screen-share/rfb-protocol.js +36 -0
  38. package/esm/node/screen-share/rfb-reader.d.ts +34 -0
  39. package/esm/node/screen-share/rfb-reader.js +110 -0
  40. package/esm/node/screen-share/rfb-security.d.ts +30 -0
  41. package/esm/node/screen-share/rfb-security.js +136 -0
  42. package/esm/node/screen-share/rfb-test-server.d.ts +37 -0
  43. package/esm/node/screen-share/rfb-test-server.js +116 -0
  44. package/esm/node/screen-share/screen-share-server-impl.d.ts +37 -0
  45. package/esm/node/screen-share/screen-share-server-impl.js +76 -0
  46. package/esm/node/screen-share/screen-shares.d.ts +123 -0
  47. package/esm/node/screen-share/screen-shares.js +463 -0
  48. package/esm/node/session-info-util.d.ts +10 -11
  49. package/esm/node/session-info-util.js +16 -94
  50. package/esm/node/settings-store.d.ts +1 -1
  51. package/esm/node/settings-store.js +7 -4
  52. package/esm/node/sign-in/access-store.d.ts +1 -1
  53. package/esm/node/sign-in/access-store.js +4 -1
  54. package/esm/node/sign-in/index.d.ts +2 -0
  55. package/esm/node/sign-in/index.js +2 -0
  56. package/esm/node/sign-in/polled-sign-in.d.ts +50 -0
  57. package/esm/node/sign-in/polled-sign-in.js +83 -0
  58. package/esm/node/sign-in/sign-in-devices.d.ts +55 -0
  59. package/esm/node/sign-in/sign-in-devices.js +113 -0
  60. package/esm/node/sign-in/sign-in-flow.d.ts +38 -4
  61. package/esm/node/sign-in/sign-in-flow.js +79 -4
  62. package/esm/node/sign-in/sign-in-pages.d.ts +8 -0
  63. package/esm/node/sign-in/sign-in-pages.js +137 -8
  64. package/esm/node/sign-in/sign-in-provider.d.ts +6 -0
  65. package/esm/node/sign-in/sign-in-provider.js +15 -0
  66. package/esm/node/sources-extension.d.ts +10 -0
  67. package/esm/node/sources-extension.js +18 -0
  68. package/esm/node/ta-server-impl.js +3 -3
  69. package/esm/node/ta-session-factory.js +2 -0
  70. package/esm/node/team/team-calls.js +35 -0
  71. package/esm/node/voice-bridge.d.ts +3 -1
  72. package/esm/node/voice-bridge.js +10 -3
  73. package/esm/node/voices-file.d.ts +5 -0
  74. package/esm/node/voices-file.js +14 -3
  75. package/esm/node/working-directory.d.ts +8 -2
  76. package/esm/node/working-directory.js +7 -0
  77. package/lib/node/account-service.d.ts +74 -0
  78. package/lib/node/account-service.js +307 -0
  79. package/lib/node/app-commands.d.ts +1 -1
  80. package/lib/node/app-commands.js +46 -29
  81. package/lib/node/credentials.d.ts +13 -0
  82. package/lib/node/credentials.js +38 -0
  83. package/lib/node/host-protocol.d.ts +2 -0
  84. package/lib/node/host-protocol.js +3 -1
  85. package/lib/node/index.d.ts +3 -0
  86. package/lib/node/index.js +3 -0
  87. package/lib/node/keep-awake.d.ts +3 -0
  88. package/lib/node/keep-awake.js +6 -1
  89. package/lib/node/package-hosts.d.ts +4 -0
  90. package/lib/node/package-hosts.js +17 -0
  91. package/lib/node/relay-bridge.js +1 -1
  92. package/lib/node/rooms-bridge.d.ts +10 -1
  93. package/lib/node/rooms-bridge.js +25 -0
  94. package/lib/node/screen-share/desktop-picture.d.ts +52 -0
  95. package/lib/node/screen-share/desktop-picture.js +136 -0
  96. package/lib/node/screen-share/desktop-session.d.ts +129 -0
  97. package/lib/node/screen-share/desktop-session.js +442 -0
  98. package/lib/node/screen-share/desktop-targets.d.ts +25 -0
  99. package/lib/node/screen-share/desktop-targets.js +84 -0
  100. package/lib/node/screen-share/desktop-viewer.d.ts +147 -0
  101. package/lib/node/screen-share/desktop-viewer.js +343 -0
  102. package/lib/node/screen-share/index.d.ts +9 -0
  103. package/lib/node/screen-share/index.js +25 -0
  104. package/lib/node/screen-share/rfb-client.d.ts +55 -0
  105. package/lib/node/screen-share/rfb-client.js +183 -0
  106. package/lib/node/screen-share/rfb-des.d.ts +12 -0
  107. package/lib/node/screen-share/rfb-des.js +146 -0
  108. package/lib/node/screen-share/rfb-encodings.d.ts +72 -0
  109. package/lib/node/screen-share/rfb-encodings.js +295 -0
  110. package/lib/node/screen-share/rfb-framebuffer.d.ts +17 -0
  111. package/lib/node/screen-share/rfb-framebuffer.js +57 -0
  112. package/lib/node/screen-share/rfb-protocol.d.ts +91 -0
  113. package/lib/node/screen-share/rfb-protocol.js +39 -0
  114. package/lib/node/screen-share/rfb-reader.d.ts +35 -0
  115. package/lib/node/screen-share/rfb-reader.js +115 -0
  116. package/lib/node/screen-share/rfb-security.d.ts +31 -0
  117. package/lib/node/screen-share/rfb-security.js +141 -0
  118. package/lib/node/screen-share/rfb-test-server.d.ts +38 -0
  119. package/lib/node/screen-share/rfb-test-server.js +120 -0
  120. package/lib/node/screen-share/screen-share-server-impl.d.ts +38 -0
  121. package/lib/node/screen-share/screen-share-server-impl.js +79 -0
  122. package/lib/node/screen-share/screen-shares.d.ts +124 -0
  123. package/lib/node/screen-share/screen-shares.js +464 -0
  124. package/lib/node/session-info-util.d.ts +10 -11
  125. package/lib/node/session-info-util.js +15 -93
  126. package/lib/node/settings-store.d.ts +1 -1
  127. package/lib/node/settings-store.js +6 -3
  128. package/lib/node/sign-in/access-store.d.ts +1 -1
  129. package/lib/node/sign-in/access-store.js +4 -1
  130. package/lib/node/sign-in/index.d.ts +2 -0
  131. package/lib/node/sign-in/index.js +2 -0
  132. package/lib/node/sign-in/polled-sign-in.d.ts +51 -0
  133. package/lib/node/sign-in/polled-sign-in.js +87 -0
  134. package/lib/node/sign-in/sign-in-devices.d.ts +56 -0
  135. package/lib/node/sign-in/sign-in-devices.js +117 -0
  136. package/lib/node/sign-in/sign-in-flow.d.ts +38 -4
  137. package/lib/node/sign-in/sign-in-flow.js +80 -5
  138. package/lib/node/sign-in/sign-in-pages.d.ts +8 -0
  139. package/lib/node/sign-in/sign-in-pages.js +136 -7
  140. package/lib/node/sign-in/sign-in-provider.d.ts +6 -0
  141. package/lib/node/sign-in/sign-in-provider.js +15 -0
  142. package/lib/node/sources-extension.d.ts +11 -0
  143. package/lib/node/sources-extension.js +22 -0
  144. package/lib/node/ta-server-impl.js +2 -2
  145. package/lib/node/ta-session-factory.js +2 -0
  146. package/lib/node/team/team-calls.js +35 -0
  147. package/lib/node/voice-bridge.d.ts +3 -1
  148. package/lib/node/voice-bridge.js +9 -2
  149. package/lib/node/voices-file.d.ts +5 -0
  150. package/lib/node/voices-file.js +14 -3
  151. package/lib/node/working-directory.d.ts +8 -2
  152. package/lib/node/working-directory.js +7 -0
  153. package/package.json +14 -10
@@ -17,7 +17,11 @@ export class VoicesFile {
17
17
  const services = Array.isArray(config === null || config === void 0 ? void 0 : config.services)
18
18
  ? config.services.filter(service => service && typeof service.id === 'string' && typeof service.type === 'string' && typeof service.options === 'object')
19
19
  : [];
20
- return { services, ...(typeof (config === null || config === void 0 ? void 0 : config.use) === 'string' ? { use: config.use } : {}) };
20
+ return {
21
+ services,
22
+ ...(typeof (config === null || config === void 0 ? void 0 : config.use) === 'string' ? { use: config.use } : {}),
23
+ ...((config === null || config === void 0 ? void 0 : config.bargeIn) && typeof config.bargeIn === 'object' ? { bargeIn: config.bargeIn } : {}),
24
+ };
21
25
  }
22
26
  /** What reads, or nothing with why it does not parse. */
23
27
  safeRead() {
@@ -64,11 +68,17 @@ export class VoicesFile {
64
68
  services[at] = { ...kept, ...(name ? { name } : {}), options };
65
69
  this.write({ ...config, services });
66
70
  }
71
+ /** Sets keys of bargeIn, the others kept. */
72
+ setBargeIn(bargeIn) {
73
+ const config = this.read();
74
+ this.write({ ...config, bargeIn: { ...config.bargeIn, ...bargeIn } });
75
+ }
67
76
  remove(id) {
68
77
  var _a;
69
78
  const config = this.read();
70
79
  const services = ((_a = config.services) !== null && _a !== void 0 ? _a : []).filter(service => service.id !== id);
71
- this.write({ services, ...(config.use && config.use !== id ? { use: config.use } : {}) });
80
+ const { use, ...rest } = config;
81
+ this.write({ ...rest, services, ...(use && use !== id ? { use } : {}) });
72
82
  }
73
83
  use(id) {
74
84
  var _a, _b;
@@ -76,7 +86,8 @@ export class VoicesFile {
76
86
  if (id !== undefined && !((_a = config.services) !== null && _a !== void 0 ? _a : []).some(service => service.id === id)) {
77
87
  throw new Error(`There is no voice service ${id} in voices.json.`);
78
88
  }
79
- this.write({ services: (_b = config.services) !== null && _b !== void 0 ? _b : [], ...(id ? { use: id } : {}) });
89
+ const { use: _, ...rest } = config;
90
+ this.write({ ...rest, services: (_b = config.services) !== null && _b !== void 0 ? _b : [], ...(id ? { use: id } : {}) });
80
91
  }
81
92
  write(config) {
82
93
  mkdirSync(dirname(this.file), { recursive: true });
@@ -1,7 +1,8 @@
1
1
  export declare const TaWorkingDirectory: unique symbol;
2
2
  /**
3
- * The working directory the user chose for ta serve (TaServer.setWorkingDirectory), and the hosts of the
4
- * remote directories opened lately (TaRemoteServer), kept across restarts.
3
+ * The working directory the user chose for ta serve (TaServer.setWorkingDirectory), the hosts of the
4
+ * remote directories opened lately (TaRemoteServer) and the desktops added (TaScreenShareServer), kept across
5
+ * restarts.
5
6
  */
6
7
  export interface TaWorkingDirectory {
7
8
  get(): string | undefined;
@@ -10,6 +11,9 @@ export interface TaWorkingDirectory {
10
11
  recentHosts(): string[];
11
12
  addRecentHost(host: string): void;
12
13
  removeRecentHost(host: string): void;
14
+ /** The desktops the user added, as kept: checked by whoever reads them. */
15
+ desktops(): unknown[];
16
+ setDesktops(desktops: unknown[]): void;
13
17
  }
14
18
  /** In ta.json of the agent directory, beside Ta's other settings of its own; the file keeps its other keys. */
15
19
  export declare class TaWorkingDirectoryImpl implements TaWorkingDirectory {
@@ -19,6 +23,8 @@ export declare class TaWorkingDirectoryImpl implements TaWorkingDirectory {
19
23
  recentHosts(): string[];
20
24
  addRecentHost(host: string): void;
21
25
  removeRecentHost(host: string): void;
26
+ desktops(): unknown[];
27
+ setDesktops(desktops: unknown[]): void;
22
28
  protected write(changes: Record<string, unknown>): void;
23
29
  protected read(): Record<string, unknown>;
24
30
  }
@@ -40,6 +40,13 @@ let TaWorkingDirectoryImpl = class TaWorkingDirectoryImpl {
40
40
  this.write({ remoteHosts: hosts.filter(other => other !== host) });
41
41
  }
42
42
  }
43
+ desktops() {
44
+ const { desktops } = this.read();
45
+ return Array.isArray(desktops) ? desktops : [];
46
+ }
47
+ setDesktops(desktops) {
48
+ this.write({ desktops });
49
+ }
43
50
  write(changes) {
44
51
  writeFileSync(`${this.file}.tmp`, `${JSON.stringify({ ...this.read(), ...changes }, null, 2)}\n`);
45
52
  renameSync(`${this.file}.tmp`, this.file);
@@ -0,0 +1,74 @@
1
+ import { type Application, ApplicationLifecycle } from '@celljs/core';
2
+ import { type TaAccountInfo, TaAccountServer } from '@celljs/ta-protocol';
3
+ import { AccountAgent, type AccountKept, type AccountView, type AccountWay } from '@celljs/ta-relay';
4
+ import { type RelayBridge } from './relay-bridge';
5
+ /** What a package hears from the same package on another computer of the account (TaAccountService.tell). */
6
+ export type TaAccountListener = (from: {
7
+ id: string;
8
+ name: string;
9
+ }, value: unknown) => void;
10
+ /**
11
+ * The account of this computer (docs/design/ta-account.md): signed in, it is in the user's list of devices; it lets
12
+ * in the devices the user lets in, pairs each with this ta serve through the relay, and unpairs those removed. What it
13
+ * keeps (its own keys, whom it trusts, its token) is in `account.json` of the agent directory, for the user alone.
14
+ */
15
+ export declare class TaAccountService implements TaAccountServer, ApplicationLifecycle<Application> {
16
+ protected readonly bridge: RelayBridge;
17
+ protected agent?: AccountAgent;
18
+ protected view?: AccountView;
19
+ protected signingIn?: {
20
+ way: AccountWay;
21
+ handle: string;
22
+ code: string;
23
+ url: string;
24
+ interval: number;
25
+ expiresAt: number;
26
+ };
27
+ protected error?: string;
28
+ protected timer?: ReturnType<typeof setTimeout>;
29
+ protected running?: Promise<void>;
30
+ protected stopped: boolean;
31
+ protected readonly listeners: Map<string, Set<TaAccountListener>>;
32
+ get file(): string;
33
+ /** The name this computer goes by in the account. */
34
+ static computerName(): string;
35
+ onStart(): void;
36
+ onStop(): void;
37
+ protected read(): AccountKept | undefined;
38
+ protected write(kept: AccountKept): void;
39
+ protected service(): string;
40
+ /** The gateways whose accounts this computer may sign in with: of the deployment joined, and of this build. */
41
+ protected gateways(): string[];
42
+ protected agentOf(kept: AccountKept): AccountAgent;
43
+ /** Signed out by the service (removed, or the token no longer works): the devices it paired lose their way in. */
44
+ protected forget(): void;
45
+ protected schedule(after: number): void;
46
+ /** One round at a time: a sign-in polled, or the account synced. */
47
+ protected round(): Promise<void>;
48
+ protected pollSignIn(): Promise<number>;
49
+ getAccount(): Promise<TaAccountInfo>;
50
+ info(): Promise<TaAccountInfo>;
51
+ signIn(provider: string, service?: string): Promise<TaAccountInfo>;
52
+ protected begin(provider: string, service?: string): Promise<TaAccountInfo>;
53
+ cancelSignIn(): Promise<TaAccountInfo>;
54
+ protected act(run: (agent: AccountAgent) => Promise<AccountView>): Promise<TaAccountInfo>;
55
+ approve(id: string): Promise<TaAccountInfo>;
56
+ remove(id: string): Promise<TaAccountInfo>;
57
+ signOut(): Promise<TaAccountInfo>;
58
+ /** This computer in the account, once let in, for packages (TaExtensionHost.account). */
59
+ self(): {
60
+ id: string;
61
+ name: string;
62
+ } | undefined;
63
+ /** The other computers of the account this one trusts, for packages (TaExtensionHost.account). */
64
+ computers(): {
65
+ id: string;
66
+ name: string;
67
+ online: boolean;
68
+ }[];
69
+ /** Says `value` to a package on another computer of the account, sealed for it: it hears it on its next look. */
70
+ tell(to: string, topic: string, value: unknown): Promise<void>;
71
+ /** Hears what the same package on other computers of the account tells, by `topic`. */
72
+ onTold(topic: string, listener: TaAccountListener): () => void;
73
+ }
74
+ //# sourceMappingURL=account-service.d.ts.map
@@ -0,0 +1,307 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var TaAccountService_1;
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.TaAccountService = void 0;
14
+ const node_fs_1 = require("node:fs");
15
+ const node_os_1 = require("node:os");
16
+ const node_path_1 = require("node:path");
17
+ const core_1 = require("@celljs/core");
18
+ const rpc_1 = require("@celljs/rpc");
19
+ const ta_brand_1 = require("@celljs/ta-harness/lib/node/ta-brand");
20
+ const ta_protocol_1 = require("@celljs/ta-protocol");
21
+ const ta_relay_1 = require("@celljs/ta-relay");
22
+ const host_protocol_1 = require("./host-protocol");
23
+ const relay_bridge_1 = require("./relay-bridge");
24
+ const ta_errors_1 = require("./ta-errors");
25
+ /** How often a computer says it is there and looks at its account; quicker while a device waits to be let in. */
26
+ const SYNC_MS = 20000;
27
+ const WAITING_SYNC_MS = 4000;
28
+ /**
29
+ * The account of this computer (docs/design/ta-account.md): signed in, it is in the user's list of devices; it lets
30
+ * in the devices the user lets in, pairs each with this ta serve through the relay, and unpairs those removed. What it
31
+ * keeps (its own keys, whom it trusts, its token) is in `account.json` of the agent directory, for the user alone.
32
+ */
33
+ let TaAccountService = TaAccountService_1 = class TaAccountService {
34
+ constructor() {
35
+ this.stopped = false;
36
+ this.listeners = new Map();
37
+ }
38
+ get file() {
39
+ return (0, node_path_1.join)(ta_brand_1.TaBrand.agentDir(), 'account.json');
40
+ }
41
+ /** The name this computer goes by in the account. */
42
+ static computerName() {
43
+ return (0, node_os_1.hostname)().replace(/\.local$/, '');
44
+ }
45
+ onStart() {
46
+ const kept = this.read();
47
+ if (kept) {
48
+ this.agent = this.agentOf(kept);
49
+ this.schedule(0);
50
+ }
51
+ }
52
+ onStop() {
53
+ this.stopped = true;
54
+ clearTimeout(this.timer);
55
+ }
56
+ read() {
57
+ var _a;
58
+ try {
59
+ const kept = JSON.parse((0, node_fs_1.readFileSync)(this.file, 'utf8'));
60
+ return (kept === null || kept === void 0 ? void 0 : kept.token) && ((_a = kept.self) === null || _a === void 0 ? void 0 : _a.signKey) ? kept : undefined;
61
+ }
62
+ catch (_b) {
63
+ return undefined;
64
+ }
65
+ }
66
+ write(kept) {
67
+ const temp = `${this.file}.${process.pid}.tmp`;
68
+ (0, node_fs_1.writeFileSync)(temp, `${JSON.stringify(kept, undefined, 2)}\n`, { mode: 0o600 });
69
+ (0, node_fs_1.chmodSync)(temp, 0o600);
70
+ (0, node_fs_1.renameSync)(temp, this.file);
71
+ }
72
+ service() {
73
+ return this.bridge.relayFor('devices');
74
+ }
75
+ /** The gateways whose accounts this computer may sign in with: of the deployment joined, and of this build. */
76
+ gateways() {
77
+ var _a, _b;
78
+ const joined = (_b = (_a = this.bridge.relays.safeRead().config) === null || _a === void 0 ? void 0 : _a.deployment) === null || _b === void 0 ? void 0 : _b.gateway;
79
+ return [joined, process.env.TA_GATEWAY_URL].filter((one) => !!one);
80
+ }
81
+ agentOf(kept) {
82
+ const store = this.bridge.store;
83
+ return new ta_relay_1.AccountAgent(kept, {
84
+ save: next => this.write(next),
85
+ name: () => TaAccountService_1.computerName(),
86
+ host: () => {
87
+ var _a;
88
+ const config = store.read();
89
+ return config.host ? { relay: (_a = config.url) !== null && _a !== void 0 ? _a : this.service(), id: config.host.id } : undefined;
90
+ },
91
+ pair: async (device) => {
92
+ const pairing = store.pair(this.service(), device.name, { account: device.id });
93
+ this.bridge.refresh();
94
+ return ta_relay_1.RelayPairingUtil.link({ ...pairing, name: TaAccountService_1.computerName() });
95
+ },
96
+ paired: () => store.read().devices.flatMap(device => (device.account ? [device.account] : [])),
97
+ unpair: id => {
98
+ for (const device of store.read().devices.filter(one => one.account === id)) {
99
+ store.remove(device.id);
100
+ }
101
+ this.bridge.refresh();
102
+ },
103
+ told: (from, topic, value) => {
104
+ var _a;
105
+ for (const listener of (_a = this.listeners.get(topic)) !== null && _a !== void 0 ? _a : []) {
106
+ try {
107
+ listener({ id: from.id, name: from.name }, value);
108
+ }
109
+ catch (_b) {
110
+ // A package's listener is its own.
111
+ }
112
+ }
113
+ },
114
+ signedOut: () => this.forget(),
115
+ });
116
+ }
117
+ /** Signed out by the service (removed, or the token no longer works): the devices it paired lose their way in. */
118
+ forget() {
119
+ const store = this.bridge.store;
120
+ for (const device of store.read().devices.filter(one => one.account)) {
121
+ store.remove(device.id);
122
+ }
123
+ this.bridge.refresh();
124
+ (0, node_fs_1.rmSync)(this.file, { force: true });
125
+ this.agent = undefined;
126
+ this.view = undefined;
127
+ }
128
+ schedule(after) {
129
+ var _a, _b;
130
+ clearTimeout(this.timer);
131
+ if (this.stopped || (!this.agent && !this.signingIn)) {
132
+ return;
133
+ }
134
+ this.timer = setTimeout(() => void this.round(), after);
135
+ (_b = (_a = this.timer).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
136
+ }
137
+ /** One round at a time: a sign-in polled, or the account synced. */
138
+ round() {
139
+ var _a;
140
+ (_a = this.running) !== null && _a !== void 0 ? _a : (this.running = (async () => {
141
+ let next = SYNC_MS;
142
+ try {
143
+ if (this.signingIn) {
144
+ next = await this.pollSignIn();
145
+ }
146
+ else if (this.agent) {
147
+ this.view = await this.agent.sync();
148
+ this.error = undefined;
149
+ if (this.view.waiting || this.view.devices.some(one => one.waiting)) {
150
+ next = WAITING_SYNC_MS;
151
+ }
152
+ }
153
+ }
154
+ catch (error) {
155
+ this.error = error instanceof Error ? error.message : String(error);
156
+ }
157
+ finally {
158
+ this.running = undefined;
159
+ }
160
+ this.schedule(next);
161
+ })());
162
+ return this.running;
163
+ }
164
+ async pollSignIn() {
165
+ const signing = this.signingIn;
166
+ if (Date.now() > signing.expiresAt) {
167
+ this.signingIn = undefined;
168
+ this.error = 'The sign-in ran out of time: begin it again.';
169
+ return SYNC_MS;
170
+ }
171
+ const answer = await ta_relay_1.AccountService.check(signing.way, signing.handle);
172
+ if (answer.status === 'pending') {
173
+ return signing.interval * 1000;
174
+ }
175
+ this.signingIn = undefined;
176
+ if (answer.status === 'failed') {
177
+ this.error = answer.error;
178
+ return SYNC_MS;
179
+ }
180
+ const kept = await ta_relay_1.AccountAgent.create(signing.way.service, answer.token, answer.account, 'computer', TaAccountService_1.computerName());
181
+ this.write(kept);
182
+ this.agent = this.agentOf(kept);
183
+ this.view = await this.agent.sync();
184
+ this.error = undefined;
185
+ return this.view.waiting ? WAITING_SYNC_MS : SYNC_MS;
186
+ }
187
+ getAccount() {
188
+ return ta_errors_1.TaErrors.guard(() => this.info());
189
+ }
190
+ async info() {
191
+ var _a, _b;
192
+ var _c, _d;
193
+ const service = (_d = (_c = (_a = this.agent) === null || _a === void 0 ? void 0 : _a.state.service) !== null && _c !== void 0 ? _c : (_b = this.signingIn) === null || _b === void 0 ? void 0 : _b.way.service) !== null && _d !== void 0 ? _d : this.service();
194
+ const error = this.error ? { error: this.error } : {};
195
+ if (!this.agent) {
196
+ const providers = await ta_relay_1.AccountService.ways(service, this.gateways()).catch(() => []);
197
+ const signing = this.signingIn;
198
+ return {
199
+ service,
200
+ providers,
201
+ devices: [],
202
+ ...(signing ? { signingIn: { provider: signing.way.id, code: signing.code, url: signing.url, expiresAt: signing.expiresAt } } : {}),
203
+ ...error,
204
+ };
205
+ }
206
+ if (!this.view) {
207
+ await this.round();
208
+ }
209
+ const view = this.view;
210
+ if (!view) {
211
+ return { service, providers: [], account: this.agent.state.account, devices: [], ...error };
212
+ }
213
+ const pairings = this.agent.state.pairings;
214
+ const devices = view.devices.map(({ trusted: _trusted, ...device }) => ({
215
+ ...device,
216
+ ...(device.kind === 'computer' && pairings[device.id] ? { pairing: pairings[device.id] } : {}),
217
+ }));
218
+ return { service, providers: [], account: view.account, waiting: view.waiting, code: view.code, devices, ...error };
219
+ }
220
+ signIn(provider, service) {
221
+ return ta_errors_1.TaErrors.guard(() => this.begin(String(provider !== null && provider !== void 0 ? provider : ''), typeof service === 'string' ? service : undefined));
222
+ }
223
+ async begin(provider, service) {
224
+ if (this.agent) {
225
+ return this.info();
226
+ }
227
+ const ways = await ta_relay_1.AccountService.ways(this.service(), this.gateways());
228
+ const way = ways.find(one => one.id === provider && (service === undefined || one.service === service));
229
+ if (!way) {
230
+ throw new ta_relay_1.AccountCallError(404, `No way to sign in with ${provider} here.`);
231
+ }
232
+ const start = await ta_relay_1.AccountService.begin(way, TaAccountService_1.computerName());
233
+ this.signingIn = { way, ...start, expiresAt: Date.now() + start.expiresIn * 1000 };
234
+ this.error = undefined;
235
+ this.schedule(start.interval * 1000);
236
+ return this.info();
237
+ }
238
+ async cancelSignIn() {
239
+ this.signingIn = undefined;
240
+ return this.info();
241
+ }
242
+ act(run) {
243
+ return ta_errors_1.TaErrors.guard(async () => {
244
+ if (!this.agent) {
245
+ throw ta_errors_1.TaErrors.invalidArgument('This computer is not signed in to an account.', 'Sign in first.');
246
+ }
247
+ this.view = await run(this.agent);
248
+ this.schedule(this.view.devices.some(one => one.waiting) ? WAITING_SYNC_MS : SYNC_MS);
249
+ return this.info();
250
+ });
251
+ }
252
+ approve(id) {
253
+ return this.act(agent => agent.approve(String(id !== null && id !== void 0 ? id : '')));
254
+ }
255
+ remove(id) {
256
+ return this.act(agent => agent.remove(String(id !== null && id !== void 0 ? id : '')));
257
+ }
258
+ signOut() {
259
+ return ta_errors_1.TaErrors.guard(async () => {
260
+ if (this.agent) {
261
+ await this.agent.signOut();
262
+ this.forget();
263
+ }
264
+ this.error = undefined;
265
+ return this.info();
266
+ });
267
+ }
268
+ /** This computer in the account, once let in, for packages (TaExtensionHost.account). */
269
+ self() {
270
+ const me = this.view && !this.view.waiting ? this.view.devices.find(one => one.self) : undefined;
271
+ return me ? { id: me.id, name: me.name } : undefined;
272
+ }
273
+ /** The other computers of the account this one trusts, for packages (TaExtensionHost.account). */
274
+ computers() {
275
+ var _a;
276
+ var _b;
277
+ return ((_b = (_a = this.view) === null || _a === void 0 ? void 0 : _a.devices) !== null && _b !== void 0 ? _b : [])
278
+ .filter(one => one.kind === 'computer' && one.trusted && !one.self)
279
+ .map(one => ({ id: one.id, name: one.name, online: one.online }));
280
+ }
281
+ /** Says `value` to a package on another computer of the account, sealed for it: it hears it on its next look. */
282
+ async tell(to, topic, value) {
283
+ if (!this.agent) {
284
+ throw new ta_relay_1.AccountCallError(401, 'Sign in to an account first.');
285
+ }
286
+ await this.agent.tell(to, topic, value);
287
+ }
288
+ /** Hears what the same package on other computers of the account tells, by `topic`. */
289
+ onTold(topic, listener) {
290
+ let set = this.listeners.get(topic);
291
+ if (!set) {
292
+ set = new Set();
293
+ this.listeners.set(topic, set);
294
+ }
295
+ set.add(listener);
296
+ return () => set.delete(listener);
297
+ }
298
+ };
299
+ exports.TaAccountService = TaAccountService;
300
+ __decorate([
301
+ (0, core_1.Autowired)(relay_bridge_1.RELAY_BRIDGE),
302
+ __metadata("design:type", Function)
303
+ ], TaAccountService.prototype, "bridge", void 0);
304
+ exports.TaAccountService = TaAccountService = TaAccountService_1 = __decorate([
305
+ (0, rpc_1.Rpc)([ta_protocol_1.TaAccountServer, host_protocol_1.TA_ACCOUNT, core_1.ApplicationLifecycle])
306
+ ], TaAccountService);
307
+ //# sourceMappingURL=account-service.js.map
@@ -21,7 +21,7 @@ export interface AppCommandAccess {
21
21
  * Whether `principal` agrees, on a screen of theirs, to a danger action asked for by the run of `sessionFile`;
22
22
  * undefined when they have no page open to ask on.
23
23
  */
24
- confirm?(principal: string, what: string, sessionFile?: string): Promise<boolean | undefined>;
24
+ confirm?(principal: string, what: string, sessionFile?: string, detail?: string): Promise<boolean | undefined>;
25
25
  }
26
26
  type Json = Record<string, unknown>;
27
27
  /** The actions by id, and who may run them: pure, for TaAppCommandsImpl and its tests. */
@@ -64,8 +64,8 @@ class AppCommandRegistry {
64
64
  }
65
65
  }
66
66
  async run(id, input = {}, options = {}) {
67
- var _a, _b, _c;
68
- var _d, _e;
67
+ var _a, _b, _c, _d;
68
+ var _e, _f;
69
69
  const command = (_a = this.commands.get(id)) === null || _a === void 0 ? void 0 : _a.command;
70
70
  if (!command) {
71
71
  throw ta_errors_1.TaErrors.invalidArgument(`There is no action ${JSON.stringify(id)}.`, 'See app_commands for the actions there are.');
@@ -78,10 +78,12 @@ class AppCommandRegistry {
78
78
  if (refused) {
79
79
  throw ta_errors_1.TaErrors.invalidArgument(`Who asked this may not do ${id} (${command.action}).`, 'Ask the owner of this Ta to let them, or to do it.');
80
80
  }
81
- const principal = (_d = principals.find(one => one !== ta_protocol_1.TA_OWNER)) !== null && _d !== void 0 ? _d : ta_protocol_1.TA_OWNER;
81
+ const principal = (_e = principals.find(one => one !== ta_protocol_1.TA_OWNER)) !== null && _e !== void 0 ? _e : ta_protocol_1.TA_OWNER;
82
+ const context = { principal, language: this.language(), ...(options.sessionFile ? { sessionFile: options.sessionFile } : {}) };
82
83
  // What cannot be undone, or acts for the user, waits for them to agree on their screen; a page's tap is theirs already.
83
84
  if (command.risk === 'danger' && !options.principal) {
84
- const agreed = await ((_c = (_b = this.access).confirm) === null || _c === void 0 ? void 0 : _c.call(_b, principal, (_e = command.title[this.language()]) !== null && _e !== void 0 ? _e : command.title['en-US'], options.sessionFile));
85
+ const detail = await ((_b = command.detail) === null || _b === void 0 ? void 0 : _b.call(command, input, context));
86
+ const agreed = await ((_d = (_c = this.access).confirm) === null || _d === void 0 ? void 0 : _d.call(_c, principal, (_f = command.title[context.language]) !== null && _f !== void 0 ? _f : command.title['en-US'], options.sessionFile, detail));
85
87
  if (agreed === undefined) {
86
88
  throw ta_errors_1.TaErrors.invalidArgument(`${id} needs the user to agree on their screen, and no page of Ta is open.`, 'Tell the user to open Ta, or to do it themselves.');
87
89
  }
@@ -89,11 +91,7 @@ class AppCommandRegistry {
89
91
  throw ta_errors_1.TaErrors.invalidArgument(`The user did not agree to ${id}.`, 'Do not do it another way; ask them what they want instead.');
90
92
  }
91
93
  }
92
- return command.run(input, {
93
- principal,
94
- language: this.language(),
95
- ...(options.sessionFile ? { sessionFile: options.sessionFile } : {}),
96
- });
94
+ return command.run(input, context);
97
95
  }
98
96
  /** Each who asked: the principal given, or those of the run of the conversation; the owner for a run nobody asked. */
99
97
  principals(options) {
@@ -435,34 +433,53 @@ var CoreAppCommands;
435
433
  return {
436
434
  id: 'asks.answer',
437
435
  title: words('Answer for you', '替你回答', '替你回答', 'あなたの代わりに回答'),
438
- description: "Answers what waits for the user (asks.list) with what the user said: file of the conversation; answer: one of a dialog's choices (or its number), yes or no for a confirmation, any text for an input or a question. Only what the user said: never decide for them.",
436
+ description: "Answers what waits for the user (asks.list) with what the user said: file of the conversation; answer: one of a dialog's choices (or its number), yes or no for a confirmation, any text for an input or a question. Only what the user said: never decide for them. The user agrees on their screen before it is answered.",
439
437
  input: { type: 'object', properties: { file: { type: 'string' }, answer: { type: 'string' } }, required: ['file', 'answer'] },
440
- risk: 'act',
438
+ // It may answer a confirmation of another conversation (delete, publish): the user agrees on their screen first.
439
+ risk: 'danger',
441
440
  action: 'chat.send',
441
+ detail: async (input, context) => {
442
+ const { ask, said } = await answering(deps, input, context.language);
443
+ return `${ask.name}:${ask.question} → ${said}`;
444
+ },
442
445
  run: async (input, context) => {
443
- const ask = (await asks(deps)).find(one => one.file === input.file);
444
- if (!ask) {
445
- throw ta_errors_1.TaErrors.invalidArgument('That conversation waits for nothing.', 'See asks.list for what waits.');
446
- }
446
+ const { ask, response, said } = await answering(deps, input, context.language);
447
447
  const notice = ta_protocol_1.TaLanguageUtil.pick(context.language, words('Answered for you', '替你回答了', '替你回答了', 'あなたの代わりに回答しました'));
448
- if (ask.dialog && ask.sessionId) {
449
- const response = answerOf(ask.dialog, input.answer);
450
- if (!response) {
451
- throw ta_errors_1.TaErrors.invalidArgument(`"${String(input.answer)}" does not answer it${ask.choices ? `: it takes one of ${ask.choices.join(', ')}` : ''}.`, 'Ask the user again.');
452
- }
448
+ if (response && ask.dialog && ask.sessionId) {
453
449
  await deps.respondUi(ask.sessionId, { type: 'extension_ui_response', id: ask.dialog.id, ...response });
454
- const said = 'value' in response ? response.value : ta_protocol_1.TaAppUtil.shown(response.confirmed, context.language);
455
450
  return { text: `Answered ${ask.name}: ${said}.`, notice, changes: [{ label: ask.question, after: said }] };
456
451
  }
457
- const text = typeof input.answer === 'string' ? input.answer.trim() : '';
458
- if (!text) {
459
- throw ta_errors_1.TaErrors.invalidArgument('answer is what the user said.', 'Pass their words.');
460
- }
461
- await deps.send(ask.file, ask.cwd, text);
462
- return { text: `Told ${ask.name}: ${text}`, notice, changes: [{ label: ask.question, after: text }] };
452
+ await deps.send(ask.file, ask.cwd, said);
453
+ return { text: `Told ${ask.name}: ${said}`, notice, changes: [{ label: ask.question, after: said }] };
463
454
  },
464
455
  };
465
456
  }
457
+ /** What asks.answer would answer, and how, as the user reads it; throws for what waits for nothing or no answer of it. */
458
+ async function answering(deps, input, language) {
459
+ const ask = (await asks(deps)).find(one => one.file === input.file);
460
+ if (!ask) {
461
+ throw ta_errors_1.TaErrors.invalidArgument('That conversation waits for nothing.', 'See asks.list for what waits.');
462
+ }
463
+ if (ask.dialog && ask.sessionId) {
464
+ const response = answerOf(ask.dialog, input.answer);
465
+ if (!response) {
466
+ throw ta_errors_1.TaErrors.invalidArgument(`"${String(input.answer)}" does not answer it${ask.choices ? `: it takes one of ${ask.choices.join(', ')}` : ''}.`, 'Ask the user again.');
467
+ }
468
+ if ('value' in response) {
469
+ return { ask, response, said: response.value };
470
+ }
471
+ return {
472
+ ask,
473
+ response,
474
+ said: ta_protocol_1.TaLanguageUtil.pick(language, response.confirmed ? words('yes', '是', '是', 'はい') : words('no', '否', '否', 'いいえ')),
475
+ };
476
+ }
477
+ const text = typeof input.answer === 'string' ? input.answer.trim() : '';
478
+ if (!text) {
479
+ throw ta_errors_1.TaErrors.invalidArgument('answer is what the user said.', 'Pass their words.');
480
+ }
481
+ return { ask, said: text };
482
+ }
466
483
  function onOff(on, language) {
467
484
  return ta_protocol_1.TaAppUtil.shown(on, language);
468
485
  }
@@ -561,12 +578,12 @@ let TaAppCommandsImpl = class TaAppCommandsImpl {
561
578
  can: (principal, action) => team().can(principal, action),
562
579
  actorsOf: file => team().actorsOf(file),
563
580
  current: () => team().current(),
564
- confirm: async (principal, what, sessionFile) => {
581
+ confirm: async (principal, what, sessionFile, detail) => {
565
582
  if (!this.pages.has(principal, ta_protocol_1.TA_PAGE_CONFIRM)) {
566
583
  return undefined;
567
584
  }
568
585
  const session = sessionFile ? (await this.server.listSessions()).find(one => one.sessionFile === sessionFile) : undefined;
569
- const ask = { who: (session === null || session === void 0 ? void 0 : session.sessionName) || (session === null || session === void 0 ? void 0 : session.firstMessage) || 'Ta', what };
586
+ const ask = { who: (session === null || session === void 0 ? void 0 : session.sessionName) || (session === null || session === void 0 ? void 0 : session.firstMessage) || 'Ta', what, ...(detail ? { detail } : {}) };
570
587
  return (await this.pages.ask(principal, ta_protocol_1.TA_PAGE_CONFIRM, ask, CONFIRM_MS)) === true;
571
588
  },
572
589
  });
@@ -0,0 +1,13 @@
1
+ import type { TaCredentialStore, TaExtensionCredentials } from '@celljs/ta-protocol';
2
+ /**
3
+ * The store of the sign-ins Ta gives by itself (TaExtensionHost.credentials): a package registers it (the vault of
4
+ * ta-secrets); without one, nothing is kept and every sign-in is asked.
5
+ */
6
+ export declare class TaCredentials {
7
+ protected registered?: TaCredentialStore;
8
+ /** The store there is now; undefined while no package keeps sign-ins. */
9
+ get store(): TaCredentialStore | undefined;
10
+ /** For the server side of packages (TaExtensionHost.credentials). */
11
+ readonly extension: TaExtensionCredentials;
12
+ }
13
+ //# sourceMappingURL=credentials.d.ts.map
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.TaCredentials = void 0;
10
+ const core_1 = require("@celljs/core");
11
+ /**
12
+ * The store of the sign-ins Ta gives by itself (TaExtensionHost.credentials): a package registers it (the vault of
13
+ * ta-secrets); without one, nothing is kept and every sign-in is asked.
14
+ */
15
+ let TaCredentials = class TaCredentials {
16
+ constructor() {
17
+ /** For the server side of packages (TaExtensionHost.credentials). */
18
+ this.extension = {
19
+ register: store => {
20
+ this.registered = store;
21
+ return () => {
22
+ if (this.registered === store) {
23
+ this.registered = undefined;
24
+ }
25
+ };
26
+ },
27
+ };
28
+ }
29
+ /** The store there is now; undefined while no package keeps sign-ins. */
30
+ get store() {
31
+ return this.registered;
32
+ }
33
+ };
34
+ exports.TaCredentials = TaCredentials;
35
+ exports.TaCredentials = TaCredentials = __decorate([
36
+ (0, core_1.Component)()
37
+ ], TaCredentials);
38
+ //# sourceMappingURL=credentials.js.map