@celljs/ta-web 4.34.0 → 4.35.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 (221) hide show
  1. package/README.en.md +1 -1
  2. package/README.md +1 -1
  3. package/esm/browser/apps/app-icon.d.ts +23 -0
  4. package/esm/browser/apps/app-icon.js +98 -0
  5. package/esm/browser/apps/app-settings.d.ts +6 -0
  6. package/esm/browser/apps/app-settings.js +67 -0
  7. package/esm/browser/apps/app-window-util.d.ts +23 -0
  8. package/esm/browser/apps/app-window-util.js +27 -0
  9. package/esm/browser/apps/app-window.d.ts +9 -0
  10. package/esm/browser/apps/app-window.js +90 -0
  11. package/esm/browser/apps/web-apps.d.ts +29 -0
  12. package/esm/browser/apps/web-apps.js +57 -0
  13. package/esm/browser/assistant/call-keeper.d.ts +2 -5
  14. package/esm/browser/assistant/call-keeper.js +7 -11
  15. package/esm/browser/components/access-settings.d.ts +6 -0
  16. package/esm/browser/components/access-settings.js +52 -0
  17. package/esm/browser/components/app-root.d.ts +6 -0
  18. package/esm/browser/components/app-root.js +34 -0
  19. package/esm/browser/components/app-view.d.ts +8 -2
  20. package/esm/browser/components/app-view.js +1 -25
  21. package/esm/browser/components/cloud-sign-in-view.d.ts +1 -1
  22. package/esm/browser/components/host-view.d.ts +1 -1
  23. package/esm/browser/components/settings-more.js +6 -0
  24. package/esm/browser/device/screen-awake.d.ts +16 -0
  25. package/esm/browser/device/screen-awake.js +37 -0
  26. package/esm/browser/device/sheet-swipe.js +4 -0
  27. package/esm/browser/extension/icons.js +2 -1
  28. package/esm/browser/extension/web-access.d.ts +16 -0
  29. package/esm/browser/extension/web-access.js +75 -0
  30. package/esm/browser/extension/web-api-desktop.d.ts +23 -0
  31. package/esm/browser/extension/web-api-spec.d.ts +70 -0
  32. package/esm/browser/extension/web-api-spec.js +223 -0
  33. package/esm/browser/extension/web-extension-api.d.ts +6 -0
  34. package/esm/browser/extension/web-extension-context.js +11 -2
  35. package/esm/browser/extension/web-extension-run.d.ts +6 -1
  36. package/esm/browser/extension/web-extension-run.js +25 -0
  37. package/esm/browser/extension/web-extensions-running.d.ts +18 -1
  38. package/esm/browser/extension/web-extensions-running.js +102 -9
  39. package/esm/browser/extension/web-pages.d.ts +5 -0
  40. package/esm/browser/extension/web-pages.js +3 -0
  41. package/esm/browser/i18n/en/common.d.ts +1 -0
  42. package/esm/browser/i18n/en/common.js +1 -0
  43. package/esm/browser/i18n/en/discover.d.ts +11 -0
  44. package/esm/browser/i18n/en/discover.js +11 -0
  45. package/esm/browser/i18n/en/settings.d.ts +28 -0
  46. package/esm/browser/i18n/en/settings.js +28 -0
  47. package/esm/browser/i18n/ja/common.js +1 -0
  48. package/esm/browser/i18n/ja/discover.js +11 -0
  49. package/esm/browser/i18n/ja/settings.js +28 -0
  50. package/esm/browser/i18n/locales.d.ts +41 -3
  51. package/esm/browser/i18n/messages.d.ts +40 -2
  52. package/esm/browser/i18n/zh/common.js +1 -0
  53. package/esm/browser/i18n/zh/discover.js +11 -0
  54. package/esm/browser/i18n/zh/settings.js +28 -0
  55. package/esm/browser/i18n/zh-tw/common.js +1 -0
  56. package/esm/browser/i18n/zh-tw/discover.js +11 -0
  57. package/esm/browser/i18n/zh-tw/settings.js +28 -0
  58. package/esm/browser/index.d.ts +1 -0
  59. package/esm/browser/index.js +1 -0
  60. package/esm/browser/notices/notices.js +1 -0
  61. package/esm/browser/sandbox/channel/channel.d.ts +56 -0
  62. package/esm/browser/sandbox/channel/channel.js +124 -0
  63. package/esm/browser/sandbox/channel/codec.d.ts +80 -0
  64. package/esm/browser/sandbox/channel/codec.js +251 -0
  65. package/esm/browser/sandbox/hook-host.d.ts +23 -0
  66. package/esm/browser/sandbox/hook-host.js +61 -0
  67. package/esm/browser/sandbox/isolated-host.d.ts +105 -0
  68. package/esm/browser/sandbox/isolated-host.js +395 -0
  69. package/esm/browser/sandbox/remote/remote-copy.d.ts +56 -0
  70. package/esm/browser/sandbox/remote/remote-copy.js +302 -0
  71. package/esm/browser/sandbox/remote/remote-mirror.d.ts +61 -0
  72. package/esm/browser/sandbox/remote/remote-mirror.js +341 -0
  73. package/esm/browser/sandbox/remote/remote-protocol.d.ts +79 -0
  74. package/esm/browser/sandbox/remote/remote-protocol.js +54 -0
  75. package/esm/browser/sandbox/remote/remote-styles.d.ts +10 -0
  76. package/esm/browser/sandbox/remote/remote-styles.js +113 -0
  77. package/esm/browser/sandbox/remote-root.d.ts +11 -0
  78. package/esm/browser/sandbox/remote-root.js +81 -0
  79. package/esm/browser/sandbox/runtime/frame-api.d.ts +28 -0
  80. package/esm/browser/sandbox/runtime/frame-api.js +147 -0
  81. package/esm/browser/sandbox/runtime/frame-features.d.ts +6 -0
  82. package/esm/browser/sandbox/runtime/frame-features.js +50 -0
  83. package/esm/browser/sandbox/runtime/frame-parts.d.ts +36 -0
  84. package/esm/browser/sandbox/runtime/frame-parts.js +69 -0
  85. package/esm/browser/sandbox/runtime/frame-roots.d.ts +26 -0
  86. package/esm/browser/sandbox/runtime/frame-roots.js +76 -0
  87. package/esm/browser/sandbox/runtime/frame-store.d.ts +9 -0
  88. package/esm/browser/sandbox/runtime/frame-store.js +18 -0
  89. package/esm/browser/sandbox/runtime/sandbox-runtime.d.ts +11 -0
  90. package/esm/browser/sandbox/runtime/sandbox-runtime.js +45 -0
  91. package/esm/browser/sandbox/sandbox-features.d.ts +25 -0
  92. package/esm/browser/sandbox/sandbox-features.js +32 -0
  93. package/esm/browser/sandbox/sandbox-frame.d.ts +21 -0
  94. package/esm/browser/sandbox/sandbox-frame.js +75 -0
  95. package/esm/browser/sandbox/sandbox-guards.d.ts +19 -0
  96. package/esm/browser/sandbox/sandbox-guards.js +74 -0
  97. package/esm/browser/sandbox/sandbox-protocol.d.ts +57 -0
  98. package/esm/browser/sandbox/sandbox-protocol.js +49 -0
  99. package/esm/browser/sandbox/sandbox-runtime-code.d.ts +1 -0
  100. package/esm/browser/sandbox/sandbox-runtime-code.js +3 -0
  101. package/esm/browser/sandbox/web-sandbox.d.ts +28 -0
  102. package/esm/browser/sandbox/web-sandbox.js +44 -0
  103. package/esm/browser/settings/settings-protocol.d.ts +2 -0
  104. package/esm/browser/settings/settings-protocol.js +2 -0
  105. package/esm/browser/style/surfaces.d.ts +1 -1
  106. package/esm/browser/style/surfaces.js +5 -0
  107. package/esm/browser/style/tints.d.ts +2 -0
  108. package/esm/browser/style/tints.js +10 -0
  109. package/esm/browser/util/desktop.d.ts +31 -1
  110. package/esm/browser/util/desktop.js +20 -0
  111. package/esm/browser/view.js +1 -1
  112. package/lib/browser/apps/app-icon.d.ts +24 -0
  113. package/lib/browser/apps/app-icon.js +134 -0
  114. package/lib/browser/apps/app-settings.d.ts +7 -0
  115. package/lib/browser/apps/app-settings.js +103 -0
  116. package/lib/browser/apps/app-window-util.d.ts +24 -0
  117. package/lib/browser/apps/app-window-util.js +30 -0
  118. package/lib/browser/apps/app-window.d.ts +10 -0
  119. package/lib/browser/apps/app-window.js +126 -0
  120. package/lib/browser/apps/web-apps.d.ts +30 -0
  121. package/lib/browser/apps/web-apps.js +61 -0
  122. package/lib/browser/assistant/call-keeper.d.ts +2 -5
  123. package/lib/browser/assistant/call-keeper.js +6 -10
  124. package/lib/browser/components/access-settings.d.ts +7 -0
  125. package/lib/browser/components/access-settings.js +88 -0
  126. package/lib/browser/components/app-root.d.ts +7 -0
  127. package/lib/browser/components/app-root.js +70 -0
  128. package/lib/browser/components/app-view.d.ts +8 -2
  129. package/lib/browser/components/app-view.js +1 -25
  130. package/lib/browser/components/cloud-sign-in-view.d.ts +1 -1
  131. package/lib/browser/components/host-view.d.ts +1 -1
  132. package/lib/browser/components/settings-more.js +6 -0
  133. package/lib/browser/device/screen-awake.d.ts +16 -0
  134. package/lib/browser/device/screen-awake.js +39 -1
  135. package/lib/browser/device/sheet-swipe.js +4 -0
  136. package/lib/browser/extension/icons.js +1 -0
  137. package/lib/browser/extension/web-access.d.ts +17 -0
  138. package/lib/browser/extension/web-access.js +78 -0
  139. package/lib/browser/extension/web-api-desktop.d.ts +23 -0
  140. package/lib/browser/extension/web-api-spec.d.ts +71 -0
  141. package/lib/browser/extension/web-api-spec.js +226 -0
  142. package/lib/browser/extension/web-extension-api.d.ts +6 -0
  143. package/lib/browser/extension/web-extension-context.js +11 -2
  144. package/lib/browser/extension/web-extension-run.d.ts +6 -1
  145. package/lib/browser/extension/web-extension-run.js +25 -0
  146. package/lib/browser/extension/web-extensions-running.d.ts +18 -1
  147. package/lib/browser/extension/web-extensions-running.js +102 -9
  148. package/lib/browser/extension/web-pages.d.ts +5 -0
  149. package/lib/browser/extension/web-pages.js +3 -0
  150. package/lib/browser/i18n/en/common.d.ts +1 -0
  151. package/lib/browser/i18n/en/common.js +1 -0
  152. package/lib/browser/i18n/en/discover.d.ts +11 -0
  153. package/lib/browser/i18n/en/discover.js +11 -0
  154. package/lib/browser/i18n/en/settings.d.ts +28 -0
  155. package/lib/browser/i18n/en/settings.js +28 -0
  156. package/lib/browser/i18n/ja/common.js +1 -0
  157. package/lib/browser/i18n/ja/discover.js +11 -0
  158. package/lib/browser/i18n/ja/settings.js +28 -0
  159. package/lib/browser/i18n/locales.d.ts +41 -3
  160. package/lib/browser/i18n/messages.d.ts +40 -2
  161. package/lib/browser/i18n/zh/common.js +1 -0
  162. package/lib/browser/i18n/zh/discover.js +11 -0
  163. package/lib/browser/i18n/zh/settings.js +28 -0
  164. package/lib/browser/i18n/zh-tw/common.js +1 -0
  165. package/lib/browser/i18n/zh-tw/discover.js +11 -0
  166. package/lib/browser/i18n/zh-tw/settings.js +28 -0
  167. package/lib/browser/index.d.ts +1 -0
  168. package/lib/browser/index.js +1 -0
  169. package/lib/browser/notices/notices.js +1 -0
  170. package/lib/browser/sandbox/channel/channel.d.ts +57 -0
  171. package/lib/browser/sandbox/channel/channel.js +128 -0
  172. package/lib/browser/sandbox/channel/codec.d.ts +81 -0
  173. package/lib/browser/sandbox/channel/codec.js +256 -0
  174. package/lib/browser/sandbox/hook-host.d.ts +24 -0
  175. package/lib/browser/sandbox/hook-host.js +98 -0
  176. package/lib/browser/sandbox/isolated-host.d.ts +106 -0
  177. package/lib/browser/sandbox/isolated-host.js +432 -0
  178. package/lib/browser/sandbox/remote/remote-copy.d.ts +57 -0
  179. package/lib/browser/sandbox/remote/remote-copy.js +306 -0
  180. package/lib/browser/sandbox/remote/remote-mirror.d.ts +62 -0
  181. package/lib/browser/sandbox/remote/remote-mirror.js +345 -0
  182. package/lib/browser/sandbox/remote/remote-protocol.d.ts +80 -0
  183. package/lib/browser/sandbox/remote/remote-protocol.js +57 -0
  184. package/lib/browser/sandbox/remote/remote-styles.d.ts +11 -0
  185. package/lib/browser/sandbox/remote/remote-styles.js +116 -0
  186. package/lib/browser/sandbox/remote-root.d.ts +12 -0
  187. package/lib/browser/sandbox/remote-root.js +117 -0
  188. package/lib/browser/sandbox/runtime/frame-api.d.ts +29 -0
  189. package/lib/browser/sandbox/runtime/frame-api.js +183 -0
  190. package/lib/browser/sandbox/runtime/frame-features.d.ts +7 -0
  191. package/lib/browser/sandbox/runtime/frame-features.js +53 -0
  192. package/lib/browser/sandbox/runtime/frame-parts.d.ts +37 -0
  193. package/lib/browser/sandbox/runtime/frame-parts.js +106 -0
  194. package/lib/browser/sandbox/runtime/frame-roots.d.ts +27 -0
  195. package/lib/browser/sandbox/runtime/frame-roots.js +113 -0
  196. package/lib/browser/sandbox/runtime/frame-store.d.ts +10 -0
  197. package/lib/browser/sandbox/runtime/frame-store.js +22 -0
  198. package/lib/browser/sandbox/runtime/sandbox-runtime.d.ts +12 -0
  199. package/lib/browser/sandbox/runtime/sandbox-runtime.js +47 -0
  200. package/lib/browser/sandbox/sandbox-features.d.ts +26 -0
  201. package/lib/browser/sandbox/sandbox-features.js +35 -0
  202. package/lib/browser/sandbox/sandbox-frame.d.ts +22 -0
  203. package/lib/browser/sandbox/sandbox-frame.js +79 -0
  204. package/lib/browser/sandbox/sandbox-guards.d.ts +20 -0
  205. package/lib/browser/sandbox/sandbox-guards.js +77 -0
  206. package/lib/browser/sandbox/sandbox-protocol.d.ts +58 -0
  207. package/lib/browser/sandbox/sandbox-protocol.js +52 -0
  208. package/lib/browser/sandbox/sandbox-runtime-code.d.ts +2 -0
  209. package/lib/browser/sandbox/sandbox-runtime-code.js +6 -0
  210. package/lib/browser/sandbox/web-sandbox.d.ts +29 -0
  211. package/lib/browser/sandbox/web-sandbox.js +47 -0
  212. package/lib/browser/settings/settings-protocol.d.ts +2 -0
  213. package/lib/browser/settings/settings-protocol.js +2 -0
  214. package/lib/browser/style/surfaces.d.ts +1 -1
  215. package/lib/browser/style/surfaces.js +5 -0
  216. package/lib/browser/style/tints.d.ts +2 -0
  217. package/lib/browser/style/tints.js +10 -0
  218. package/lib/browser/util/desktop.d.ts +31 -1
  219. package/lib/browser/util/desktop.js +20 -0
  220. package/lib/browser/view.js +2 -2
  221. package/package.json +8 -8
@@ -0,0 +1,106 @@
1
+ import type { TaExtensionAccess } from '@celljs/ta-protocol';
2
+ import * as React from 'react';
3
+ import type { WebExtensionAPI } from '../extension/web-extension-api';
4
+ import { Channel } from './channel/channel';
5
+ import { Codec, type CodecScope } from './channel/codec';
6
+ import { HookHost } from './hook-host';
7
+ import { RemoteCopy } from './remote/remote-copy';
8
+ import type { RemoteEvent } from './remote/remote-protocol';
9
+ import { SandboxFrame } from './sandbox-frame';
10
+ import { type SandboxOptions } from './sandbox-protocol';
11
+ /** What the copy of a root tells the view that draws it. */
12
+ export interface RemoteRootEvents {
13
+ part(element: HTMLElement): void;
14
+ partGone(element: HTMLElement): void;
15
+ failed(message: string): void;
16
+ }
17
+ /** What an isolated host is given besides the API: the code, the options of the device, and how the page changes. */
18
+ export interface IsolatedHostSource {
19
+ readonly code: string;
20
+ readonly name: string;
21
+ readonly access: TaExtensionAccess;
22
+ readonly options: SandboxOptions;
23
+ /** Something the values of the API read may have changed (the settings): they are read again. */
24
+ changes(listener: () => void): () => void;
25
+ }
26
+ /** The parts of Ta in the copies: their props as the sandbox sent them, by the id the sandbox gave each. */
27
+ declare class PartBoard {
28
+ protected readonly kept: Map<string, Record<string, unknown>>;
29
+ protected readonly listeners: Set<() => void>;
30
+ protected readonly keys: WeakMap<HTMLElement, number>;
31
+ protected nextKey: number;
32
+ protected current: number;
33
+ readonly subscribe: (listener: () => void) => () => undefined;
34
+ readonly version: () => number;
35
+ props(id: string): Record<string, unknown> | undefined;
36
+ set(id: string, props: Record<string, unknown> | undefined): void;
37
+ /** The slots of a part (`<ta-slot n>`), by the name of their prop. */
38
+ slots(element: HTMLElement): [string, HTMLElement][];
39
+ keyOf(element: HTMLElement): number;
40
+ changed(): void;
41
+ }
42
+ /**
43
+ * The page's side of an extension in a sandbox (docs/design/ta-extension-access.md 3.3): what it asks answered by the
44
+ * API, cut to its access, by the entries of WEB_API_SPEC and the checks of SANDBOX_GUARDS; the hooks it uses run here
45
+ * (HookHost) and their values sent; what it draws copied into the page (RemoteRoot, RemoteCopy). One for the extension,
46
+ * however many places show it. It knows no member of the API by name.
47
+ */
48
+ export declare class IsolatedHost {
49
+ protected readonly pi: WebExtensionAPI;
50
+ protected readonly source: IsolatedHostSource;
51
+ readonly className: string;
52
+ readonly parts: PartBoard;
53
+ protected readonly frame: SandboxFrame;
54
+ protected channel?: Channel;
55
+ protected codec?: Codec;
56
+ protected hooks?: HookHost;
57
+ protected readonly hookScopes: Map<string, CodecScope>;
58
+ protected readonly holds: Map<number, () => void>;
59
+ protected nextHold: number;
60
+ protected readonly copies: Map<number, {
61
+ copy: RemoteCopy;
62
+ events: RemoteRootEvents;
63
+ scope: CodecScope;
64
+ }>;
65
+ protected nextRoot: number;
66
+ protected readonly components: Map<number, React.ComponentType<Record<string, unknown>>>;
67
+ protected values: Record<string, unknown>;
68
+ protected readonly style: HTMLStyleElement;
69
+ protected readonly stops: (() => void)[];
70
+ protected absent: Set<string>;
71
+ protected stopped: boolean;
72
+ constructor(pi: WebExtensionAPI, source: IsolatedHostSource);
73
+ /** Starts the sandbox; resolves once the extension ran and registered what it has. */
74
+ start(): Promise<void>;
75
+ stop(): void;
76
+ /** Copies a component of the sandbox into `element`; its root, to unmount it with. */
77
+ mount(element: HTMLElement, component: number, props: Record<string, unknown>, events: RemoteRootEvents): number;
78
+ props(root: number, props: Record<string, unknown>): void;
79
+ unmount(root: number): void;
80
+ /** The component of Ta of a path (`ui.Markdown`, `ui.icons.Star`) as far as the extension's access reaches. */
81
+ partComponent(path: string): unknown;
82
+ protected component(id: number): React.ComponentType<Record<string, unknown>>;
83
+ protected event(event: RemoteEvent): void;
84
+ protected reaches(path: string): boolean;
85
+ /** A member of the API cut to the extension's access, by its path: what reading it throws, it throws. */
86
+ protected member(path: string): {
87
+ value: unknown;
88
+ call: (...args: unknown[]) => unknown;
89
+ };
90
+ protected decodeArgs(args: unknown, _scope?: CodecScope): unknown[];
91
+ protected answer({ path, args }: {
92
+ path: string;
93
+ args: unknown[];
94
+ }, _signal?: AbortSignal): Promise<unknown>;
95
+ /** What an address of the page holds, for a sandbox, which cannot load it. */
96
+ static held(url: string): Promise<Blob>;
97
+ protected release(id: number): void;
98
+ protected watchHook(key: string, path: string, args: unknown): void;
99
+ protected heardHook(key: string, value: unknown): void;
100
+ /** The values and the methods of no arguments, read now (`value`, `read`). */
101
+ protected read(): Record<string, unknown>;
102
+ protected sendValues(reread?: boolean): void;
103
+ protected encoded(values: Record<string, unknown>): Record<string, unknown>;
104
+ }
105
+ export {};
106
+ //# sourceMappingURL=isolated-host.d.ts.map
@@ -0,0 +1,432 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.IsolatedHost = void 0;
37
+ const React = __importStar(require("react"));
38
+ const web_api_spec_1 = require("../extension/web-api-spec");
39
+ const channel_1 = require("./channel/channel");
40
+ const codec_1 = require("./channel/codec");
41
+ const hook_host_1 = require("./hook-host");
42
+ const remote_copy_1 = require("./remote/remote-copy");
43
+ const remote_styles_1 = require("./remote/remote-styles");
44
+ const remote_root_1 = require("./remote-root");
45
+ const sandbox_features_1 = require("./sandbox-features");
46
+ const sandbox_frame_1 = require("./sandbox-frame");
47
+ const sandbox_guards_1 = require("./sandbox-guards");
48
+ const sandbox_protocol_1 = require("./sandbox-protocol");
49
+ let hosts = 0;
50
+ /** The parts of Ta in the copies: their props as the sandbox sent them, by the id the sandbox gave each. */
51
+ class PartBoard {
52
+ constructor() {
53
+ this.kept = new Map();
54
+ this.listeners = new Set();
55
+ this.keys = new WeakMap();
56
+ this.nextKey = 1;
57
+ this.current = 0;
58
+ this.subscribe = (listener) => {
59
+ this.listeners.add(listener);
60
+ return () => void this.listeners.delete(listener);
61
+ };
62
+ this.version = () => this.current;
63
+ }
64
+ props(id) {
65
+ return this.kept.get(id);
66
+ }
67
+ set(id, props) {
68
+ if (props) {
69
+ this.kept.set(id, props);
70
+ }
71
+ else {
72
+ this.kept.delete(id);
73
+ }
74
+ this.changed();
75
+ }
76
+ /** The slots of a part (`<ta-slot n>`), by the name of their prop. */
77
+ slots(element) {
78
+ return Array.from(element.children)
79
+ .filter((child) => child.localName === 'ta-slot')
80
+ .map(slot => { var _a; return [(_a = slot.getAttribute('n')) !== null && _a !== void 0 ? _a : 'children', slot]; });
81
+ }
82
+ keyOf(element) {
83
+ let key = this.keys.get(element);
84
+ if (key === undefined) {
85
+ key = this.nextKey++;
86
+ this.keys.set(element, key);
87
+ }
88
+ return key;
89
+ }
90
+ changed() {
91
+ this.current++;
92
+ for (const listener of this.listeners)
93
+ listener();
94
+ }
95
+ }
96
+ /**
97
+ * The page's side of an extension in a sandbox (docs/design/ta-extension-access.md 3.3): what it asks answered by the
98
+ * API, cut to its access, by the entries of WEB_API_SPEC and the checks of SANDBOX_GUARDS; the hooks it uses run here
99
+ * (HookHost) and their values sent; what it draws copied into the page (RemoteRoot, RemoteCopy). One for the extension,
100
+ * however many places show it. It knows no member of the API by name.
101
+ */
102
+ class IsolatedHost {
103
+ constructor(pi, source) {
104
+ this.pi = pi;
105
+ this.source = source;
106
+ this.className = `ta-sbx-${++hosts}`;
107
+ this.parts = new PartBoard();
108
+ this.frame = new sandbox_frame_1.SandboxFrame();
109
+ this.hookScopes = new Map();
110
+ this.holds = new Map();
111
+ this.nextHold = 1;
112
+ this.copies = new Map();
113
+ this.nextRoot = 1;
114
+ this.components = new Map();
115
+ this.values = {};
116
+ this.style = document.createElement('style');
117
+ this.stops = [];
118
+ this.absent = new Set();
119
+ this.stopped = false;
120
+ }
121
+ /** Starts the sandbox; resolves once the extension ran and registered what it has. */
122
+ async start() {
123
+ var _a;
124
+ const { pi, source } = this;
125
+ if (!remote_styles_1.RemoteStyles.supported()) {
126
+ throw new Error('This browser cannot keep the styles of a sandbox to its own place.');
127
+ }
128
+ const { features, absent } = await sandbox_features_1.SandboxFeatureUtil.of(source.options, pi);
129
+ this.absent = new Set(absent);
130
+ this.hooks = new hook_host_1.HookHost((key, value) => this.heardHook(key, value));
131
+ // The hooks read at once, watched as long as it runs: their values go with the others.
132
+ web_api_spec_1.WebApiSpecUtil.each((path, entry) => {
133
+ var _a;
134
+ if (entry.form === 'hook' && entry.peek && !this.absent.has(path) && this.reaches(path)) {
135
+ (_a = this.hooks) === null || _a === void 0 ? void 0 : _a.watch(`peek:${path}`, this.member(path).call, []);
136
+ }
137
+ });
138
+ this.values = { ...this.values, ...this.read() };
139
+ let icons = [];
140
+ try {
141
+ icons = Object.keys((_a = pi.ui.icons) !== null && _a !== void 0 ? _a : {});
142
+ }
143
+ catch (_b) { }
144
+ const port = await this.frame.start({
145
+ code: source.code,
146
+ name: source.name,
147
+ access: source.access,
148
+ scope: `.${this.className}`,
149
+ absent,
150
+ values: this.encoded(this.values),
151
+ icons,
152
+ features,
153
+ });
154
+ if (this.stopped) {
155
+ port.close();
156
+ return;
157
+ }
158
+ this.style.dataset.sandbox = source.name;
159
+ document.head.appendChild(this.style);
160
+ const channel = new channel_1.Channel(port);
161
+ const codec = new codec_1.Codec(channel, { component: id => this.component(id), part: path => this.member(path).value });
162
+ this.channel = channel;
163
+ this.codec = codec;
164
+ this.stops.push(source.changes(() => this.sendValues()));
165
+ let ready;
166
+ let failed;
167
+ const started = new Promise((resolve, reject) => {
168
+ ready = resolve;
169
+ failed = reject;
170
+ });
171
+ channel.handle('api', (body, signal) => this.answer(body, signal));
172
+ channel.handle('release', body => this.release(Number(body)));
173
+ channel.handle('ready', () => ready());
174
+ channel.handle('failed', body => failed(new Error(String(body))));
175
+ channel.on('hook.watch', body => {
176
+ const { key, path, args } = body;
177
+ this.watchHook(String(key), String(path), args);
178
+ });
179
+ channel.on('hook.args', body => {
180
+ var _a;
181
+ const { key, args } = body;
182
+ const scope = this.hookScopes.get(String(key));
183
+ (_a = this.hooks) === null || _a === void 0 ? void 0 : _a.args(String(key), this.decodeArgs(args, scope));
184
+ });
185
+ channel.on('hook.unwatch', body => {
186
+ var _a, _b;
187
+ const key = String(body.key);
188
+ (_a = this.hooks) === null || _a === void 0 ? void 0 : _a.unwatch(key);
189
+ (_b = this.hookScopes.get(key)) === null || _b === void 0 ? void 0 : _b.release();
190
+ this.hookScopes.delete(key);
191
+ });
192
+ channel.on('ops', body => {
193
+ var _a;
194
+ const { root, ops } = body;
195
+ (_a = this.copies.get(root)) === null || _a === void 0 ? void 0 : _a.copy.apply(Array.isArray(ops) ? ops : []);
196
+ });
197
+ channel.on('failed', body => {
198
+ var _a;
199
+ const { root, message } = body;
200
+ (_a = this.copies.get(root)) === null || _a === void 0 ? void 0 : _a.events.failed(String(message));
201
+ });
202
+ channel.on('part', body => {
203
+ const { id, props } = body;
204
+ this.parts.set(String(id), props === undefined ? undefined : codec.decode(props));
205
+ });
206
+ channel.on('styles', body => {
207
+ this.style.textContent = remote_styles_1.RemoteStyles.scope(String(body !== null && body !== void 0 ? body : ''), `.${this.className}`, this.className);
208
+ });
209
+ // A link the extension did not handle opens as pi.openLink would: its guard and the extension's access apply.
210
+ channel.on('link', body => void this.answer({ path: 'openLink', args: [body] }).catch(() => undefined));
211
+ await started;
212
+ }
213
+ stop() {
214
+ var _a, _b;
215
+ this.stopped = true;
216
+ for (const stop of this.stops.splice(0))
217
+ stop();
218
+ for (const release of this.holds.values())
219
+ release();
220
+ this.holds.clear();
221
+ for (const root of [...this.copies.keys()])
222
+ this.unmount(root);
223
+ (_a = this.hooks) === null || _a === void 0 ? void 0 : _a.dispose();
224
+ (_b = this.channel) === null || _b === void 0 ? void 0 : _b.close();
225
+ this.frame.stop();
226
+ this.style.remove();
227
+ }
228
+ /** Copies a component of the sandbox into `element`; its root, to unmount it with. */
229
+ mount(element, component, props, events) {
230
+ const root = this.nextRoot++;
231
+ const { codec, channel } = this;
232
+ if (!codec || !channel) {
233
+ events.failed('The sandbox has stopped.');
234
+ return root;
235
+ }
236
+ const scope = codec.scope();
237
+ const copy = new remote_copy_1.RemoteCopy(root, element, { event: event => this.event(event), part: events.part, partGone: events.partGone }, {
238
+ scope: `.${this.className}`,
239
+ suffix: this.className,
240
+ });
241
+ this.copies.set(root, { copy, events, scope });
242
+ channel
243
+ .request('mount', { root, component, props: codec.encode(props, undefined, scope) })
244
+ .catch(error => { var _a; return events.failed(String((_a = error === null || error === void 0 ? void 0 : error.message) !== null && _a !== void 0 ? _a : error)); });
245
+ return root;
246
+ }
247
+ props(root, props) {
248
+ var _a;
249
+ const found = this.copies.get(root);
250
+ if (found && this.codec) {
251
+ (_a = this.channel) === null || _a === void 0 ? void 0 : _a.notify('props', { root, props: this.codec.encode(props, undefined, found.scope) });
252
+ }
253
+ }
254
+ unmount(root) {
255
+ var _a;
256
+ const found = this.copies.get(root);
257
+ if (!found) {
258
+ return;
259
+ }
260
+ this.copies.delete(root);
261
+ (_a = this.channel) === null || _a === void 0 ? void 0 : _a.notify('unmount', { root });
262
+ found.copy.dispose();
263
+ found.scope.release();
264
+ }
265
+ /** The component of Ta of a path (`ui.Markdown`, `ui.icons.Star`) as far as the extension's access reaches. */
266
+ partComponent(path) {
267
+ try {
268
+ return this.member(path).value;
269
+ }
270
+ catch (_a) {
271
+ return undefined;
272
+ }
273
+ }
274
+ component(id) {
275
+ let found = this.components.get(id);
276
+ if (!found) {
277
+ const host = this;
278
+ found = function Remote(props) {
279
+ return React.createElement(remote_root_1.RemoteRoot, { host, component: id, props });
280
+ };
281
+ this.components.set(id, found);
282
+ }
283
+ return found;
284
+ }
285
+ event(event) {
286
+ var _a;
287
+ (_a = this.channel) === null || _a === void 0 ? void 0 : _a.notify('event', event);
288
+ }
289
+ reaches(path) {
290
+ try {
291
+ this.member(path);
292
+ return true;
293
+ }
294
+ catch (_a) {
295
+ return false;
296
+ }
297
+ }
298
+ /** A member of the API cut to the extension's access, by its path: what reading it throws, it throws. */
299
+ member(path) {
300
+ let owner;
301
+ let value = this.pi;
302
+ for (const key of path.split('.')) {
303
+ if (!value || (typeof value !== 'object' && typeof value !== 'function')) {
304
+ throw new Error(`pi.${path} is not there.`);
305
+ }
306
+ owner = value;
307
+ value = value[key];
308
+ }
309
+ return {
310
+ value,
311
+ call: (...args) => {
312
+ if (typeof value !== 'function') {
313
+ throw new Error(`pi.${path} is not there in this Ta.`);
314
+ }
315
+ return value.apply(owner, args);
316
+ },
317
+ };
318
+ }
319
+ decodeArgs(args, _scope) {
320
+ return Array.isArray(args) ? args.map(arg => { var _a; return (_a = this.codec) === null || _a === void 0 ? void 0 : _a.decode(arg); }) : [];
321
+ }
322
+ async answer({ path, args }, _signal) {
323
+ var _a;
324
+ const entry = web_api_spec_1.WebApiSpecUtil.find(String(path));
325
+ if (!entry || this.absent.has(path) || !['call', 'fire', 'hold', 'read'].includes(entry.form)) {
326
+ throw new Error(`A sandbox cannot ask for pi.${String(path)}.`);
327
+ }
328
+ const context = { name: this.source.name };
329
+ const guard = sandbox_guards_1.SANDBOX_GUARDS[path];
330
+ const decoded = this.decodeArgs(args);
331
+ const result = this.member(path).call(...((guard === null || guard === void 0 ? void 0 : guard.args) ? guard.args(decoded, context) : decoded));
332
+ switch (entry.form) {
333
+ case 'fire':
334
+ return undefined;
335
+ case 'hold': {
336
+ const id = this.nextHold++;
337
+ if (typeof result === 'function') {
338
+ this.holds.set(id, result);
339
+ }
340
+ return id;
341
+ }
342
+ default: {
343
+ let value = await result;
344
+ value = (guard === null || guard === void 0 ? void 0 : guard.result) ? await guard.result(value, context) : value;
345
+ if (entry.form === 'call' && entry.returns === 'url') {
346
+ value = await IsolatedHost.held(String(value));
347
+ }
348
+ return (_a = this.codec) === null || _a === void 0 ? void 0 : _a.encode(value);
349
+ }
350
+ }
351
+ }
352
+ /** What an address of the page holds, for a sandbox, which cannot load it. */
353
+ static async held(url) {
354
+ const blob = await (await fetch(url)).blob();
355
+ if (blob.size > sandbox_protocol_1.SandboxLimits.FILE_BYTES) {
356
+ throw new Error('The file is larger than a sandbox is given.');
357
+ }
358
+ return blob;
359
+ }
360
+ release(id) {
361
+ const release = this.holds.get(id);
362
+ this.holds.delete(id);
363
+ release === null || release === void 0 ? void 0 : release();
364
+ }
365
+ watchHook(key, path, args) {
366
+ var _a, _b, _c;
367
+ const entry = web_api_spec_1.WebApiSpecUtil.find(path);
368
+ if ((entry === null || entry === void 0 ? void 0 : entry.form) !== 'hook' || this.absent.has(path) || !this.codec) {
369
+ (_a = this.channel) === null || _a === void 0 ? void 0 : _a.notify('hook', { key, error: `A sandbox cannot use pi.${path}.` });
370
+ return;
371
+ }
372
+ try {
373
+ const { call } = this.member(path);
374
+ this.hookScopes.set(key, this.codec.scope());
375
+ (_b = this.hooks) === null || _b === void 0 ? void 0 : _b.watch(key, call, this.decodeArgs(args));
376
+ }
377
+ catch (error) {
378
+ (_c = this.channel) === null || _c === void 0 ? void 0 : _c.notify('hook', { key, error: error instanceof Error ? error.message : String(error) });
379
+ }
380
+ }
381
+ heardHook(key, value) {
382
+ var _a;
383
+ if (key.startsWith('peek:')) {
384
+ const path = key.slice('peek:'.length);
385
+ if (this.values[path] !== value) {
386
+ this.values = { ...this.values, [path]: value };
387
+ this.sendValues(false);
388
+ }
389
+ return;
390
+ }
391
+ const codec = this.codec;
392
+ if (!codec) {
393
+ return;
394
+ }
395
+ // What it gave out for the value before is let go with it.
396
+ const previous = this.hookScopes.get(`value:${key}`);
397
+ const scope = codec.scope();
398
+ this.hookScopes.set(`value:${key}`, scope);
399
+ (_a = this.channel) === null || _a === void 0 ? void 0 : _a.notify('hook', { key, value: codec.encode(value, undefined, scope) });
400
+ previous === null || previous === void 0 ? void 0 : previous.release();
401
+ }
402
+ /** The values and the methods of no arguments, read now (`value`, `read`). */
403
+ read() {
404
+ const values = {};
405
+ web_api_spec_1.WebApiSpecUtil.each((path, entry) => {
406
+ if ((entry.form === 'value' || entry.form === 'read') && !this.absent.has(path)) {
407
+ try {
408
+ const { value, call } = this.member(path);
409
+ values[path] = entry.form === 'read' ? (typeof value === 'function' ? call() : undefined) : value;
410
+ }
411
+ catch (_a) { }
412
+ }
413
+ });
414
+ return values;
415
+ }
416
+ sendValues(reread = true) {
417
+ var _a;
418
+ if (reread) {
419
+ this.values = { ...this.values, ...this.read() };
420
+ }
421
+ (_a = this.channel) === null || _a === void 0 ? void 0 : _a.notify('values', this.encoded(this.values));
422
+ }
423
+ encoded(values) {
424
+ // Before the channel: data alone (what it holds of functions is left out).
425
+ if (!this.codec) {
426
+ return JSON.parse(JSON.stringify(values !== null && values !== void 0 ? values : {}));
427
+ }
428
+ return this.codec.encode(values, undefined, this.codec.forever);
429
+ }
430
+ }
431
+ exports.IsolatedHost = IsolatedHost;
432
+ //# sourceMappingURL=isolated-host.js.map
@@ -0,0 +1,57 @@
1
+ import { type RemoteEvent, type RemoteEventType, type RemoteNode, type RemoteOp } from './remote-protocol';
2
+ /** What a copy asks of where it is drawn: to tell the sandbox what the user did, and to draw the parts of Ta in it. */
3
+ export interface RemoteCopyHost {
4
+ event(event: RemoteEvent): void;
5
+ /** A part of Ta came or changed, or a slot of it (`ta-slot`, what the extension gives it to draw) came. */
6
+ part(element: HTMLElement): void;
7
+ /** A part of Ta went. */
8
+ partGone(element: HTMLElement): void;
9
+ }
10
+ /**
11
+ * The copy of one root of a sandbox in the page (docs/design/ta-extension-access.md 3.4): the operations of the sandbox
12
+ * applied to real DOM under `container`, through the rules of RemoteRules, and what the user does to it sent back. It
13
+ * knows nothing of the API nor of React.
14
+ */
15
+ export declare class RemoteCopy {
16
+ readonly root: number;
17
+ protected readonly container: HTMLElement;
18
+ protected readonly host: RemoteCopyHost;
19
+ /** What its styles are scoped to (RemoteStyles), and what renames its keyframes. */
20
+ protected readonly styles: {
21
+ scope: string;
22
+ suffix: string;
23
+ };
24
+ protected readonly nodes: Map<number, Node>;
25
+ protected readonly ids: WeakMap<Node, number>;
26
+ protected readonly urls: Map<string, string>;
27
+ protected moved?: {
28
+ event: PointerEvent;
29
+ frame: number;
30
+ };
31
+ protected seq: number;
32
+ protected readonly typed: Map<number, number>;
33
+ protected readonly stops: (() => void)[];
34
+ constructor(root: number, container: HTMLElement, host: RemoteCopyHost,
35
+ /** What its styles are scoped to (RemoteStyles), and what renames its keyframes. */
36
+ styles: {
37
+ scope: string;
38
+ suffix: string;
39
+ });
40
+ apply(ops: readonly RemoteOp[]): void;
41
+ dispose(): void;
42
+ protected one(op: RemoteOp): void;
43
+ protected make(remote: RemoteNode, svg: boolean): Node | undefined;
44
+ protected scoped(css: string): string;
45
+ protected attribute(element: Element, name: string, value: string | null): void;
46
+ protected keep(id: number, node: Node): void;
47
+ protected forget(node: Node): void;
48
+ protected gone(node: Node): void;
49
+ /**
50
+ * The node of the sandbox an event is for: the nearest of this copy; none when that is a part of Ta, whose own
51
+ * elements are the page's (its callbacks reach the sandbox themselves).
52
+ */
53
+ protected idOf(target: EventTarget | null): number | undefined;
54
+ protected heard(type: RemoteEventType, event: Event): void;
55
+ protected send(type: RemoteEventType, event: Event, id: number, target?: EventTarget | null): void;
56
+ }
57
+ //# sourceMappingURL=remote-copy.d.ts.map