@openartifactsai/harness-kit 0.0.0-stage → 0.23.0-rc.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.
- package/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +210 -2
- package/package.json +52 -4
- package/src/client.d.ts +160 -0
- package/src/client.js +409 -0
- package/src/context.d.ts +31 -0
- package/src/context.js +61 -0
- package/src/daemon.d.ts +177 -0
- package/src/daemon.js +293 -0
- package/src/embed/index.d.ts +230 -0
- package/src/embed/index.js +376 -0
- package/src/embed/remote.d.ts +39 -0
- package/src/embed/remote.js +51 -0
- package/src/harness.d.ts +383 -0
- package/src/harness.js +678 -0
- package/src/index.d.ts +28 -0
- package/src/index.js +20 -0
- package/src/legacy.d.ts +106 -0
- package/src/legacy.js +278 -0
- package/src/mcp.d.ts +39 -0
- package/src/mcp.js +85 -0
- package/src/runtimes.d.ts +174 -0
- package/src/runtimes.js +398 -0
- package/src/skill.d.ts +58 -0
- package/src/skill.js +88 -0
- package/src/testing-ws.d.ts +44 -0
- package/src/testing-ws.js +204 -0
- package/src/testing.d.ts +227 -0
- package/src/testing.js +564 -0
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@openartifactsai/harness-kit/embed`: browser code that puts an artifact's Shell page, or the library's home page,
|
|
3
|
+
* into an agent app's own UI. No Node imports: it runs in the app's renderer (a web page, an Electron or Tauri view).
|
|
4
|
+
*
|
|
5
|
+
* Two ways to show an artifact:
|
|
6
|
+
*
|
|
7
|
+
* - **Frame**: the host's page holds an `<iframe>` of the Shell (`viewUrl`). The Shell says `shell_hello` to its parent;
|
|
8
|
+
* this module answers `host_hello` to the Shell's origin only, and takes messages only from that iframe's window and
|
|
9
|
+
* the Shell's origin (the origin of the address it was given: loopback or not, `http` or `https`). The host's origin
|
|
10
|
+
* must be one its adapter declared (`caps.open.origins` in `hello`), or the Shell sends it no prompt or comment text,
|
|
11
|
+
* links, saved files or destinations.
|
|
12
|
+
*
|
|
13
|
+
* const view = embedArtifact(iframe, { url: viewUrl, agentName: 'My Agent', onNewSession: ({ prompt }) => … });
|
|
14
|
+
*
|
|
15
|
+
* - **Top-level view**: the host opens `host.open`'s `embed_url` in a view of its own (a tab, a webview, a window) with
|
|
16
|
+
* no parent frame; the Shell talks to the daemon, the daemon to the adapter (`shell.event` / `shell.command`). The
|
|
17
|
+
* host passes a transport that carries those both ways (in an Electron app, IPC to the process that holds the
|
|
18
|
+
* Harness connection):
|
|
19
|
+
*
|
|
20
|
+
* const view = embedArtifact({ send: (command) => harness.shellCommand(viewId, command), onEvent: (listener) => … }, { url: embedUrl, … });
|
|
21
|
+
*
|
|
22
|
+
* What the Shell sends is data about what the person did: prompts and comments are their words for the agent, never
|
|
23
|
+
* instructions to the host.
|
|
24
|
+
*
|
|
25
|
+
* Thumbnails: give `capture` (returns a JPEG of the view, as the host's own view can take one) and `saveThumbnail`
|
|
26
|
+
* (stores it, e.g. through the adapter's `Harness.saveThumbnail`; a browser page should not hold the daemon token).
|
|
27
|
+
* The kit captures {@link THUMBNAIL_DELAY_MS} after the Shell is ready, then after a place change at most once every
|
|
28
|
+
* {@link THUMBNAIL_INTERVAL_MS}.
|
|
29
|
+
*
|
|
30
|
+
* Model use: with the Shell bar hidden, `onModelUse` and `onModelNotice` hear what its AI mark and notice would show.
|
|
31
|
+
*
|
|
32
|
+
* Save state: `onSaved` hears the page's save state and whether leaving now loses work (the Shell's `saved`, after it
|
|
33
|
+
* is ready and on each change), and `saved` keeps the last one, so a host can ask before it closes its view.
|
|
34
|
+
*/
|
|
35
|
+
import { HOST_PROTOCOL, MAX_HOST_DESTINATIONS, adoptLegacyEnvelope, currentArtifactsUrl, shellToHostOf, withLegacyEnvelope, } from '@openartifactsai/protocol';
|
|
36
|
+
/** How long after the Shell is ready the first thumbnail is captured. */
|
|
37
|
+
export const THUMBNAIL_DELAY_MS = 2000;
|
|
38
|
+
/** The least time between two thumbnails of one view. */
|
|
39
|
+
export const THUMBNAIL_INTERVAL_MS = 60_000;
|
|
40
|
+
const isFrame = (target) => !('send' in target && typeof target.send === 'function');
|
|
41
|
+
function originOf(url) {
|
|
42
|
+
if (!url)
|
|
43
|
+
return null;
|
|
44
|
+
try {
|
|
45
|
+
const origin = new URL(url).origin;
|
|
46
|
+
return origin && origin !== 'null' ? origin : null;
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** The address without the view ticket (`oa_view`) and with `hash` as its fragment. */
|
|
53
|
+
function addressOf(url, hash) {
|
|
54
|
+
if (!url)
|
|
55
|
+
return null;
|
|
56
|
+
try {
|
|
57
|
+
const address = new URL(url);
|
|
58
|
+
address.searchParams.delete('oa_view');
|
|
59
|
+
if (hash)
|
|
60
|
+
address.hash = hash.startsWith('#') ? hash : `#${hash}`;
|
|
61
|
+
return address.href;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async function jpegOf(value) {
|
|
68
|
+
if (!value)
|
|
69
|
+
return null;
|
|
70
|
+
const bytes = value instanceof Uint8Array ? value : value instanceof ArrayBuffer ? new Uint8Array(value) : new Uint8Array(await value.arrayBuffer());
|
|
71
|
+
return bytes.length >= 4 && bytes[0] === 0xff && bytes[1] === 0xd8 ? bytes : null;
|
|
72
|
+
}
|
|
73
|
+
function defaultWindow() {
|
|
74
|
+
return globalThis.window ?? null;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Shows an artifact's Shell in the host's UI: `target` is the `<iframe>` (frame mode) or a transport to a top-level
|
|
78
|
+
* view (see the module's notes). Answers the Shell's hello and turns its messages into the callbacks.
|
|
79
|
+
*/
|
|
80
|
+
export function embedArtifact(target, options = {}) {
|
|
81
|
+
const win = options.window ?? defaultWindow();
|
|
82
|
+
const frame = isFrame(target) ? target : null;
|
|
83
|
+
const transport = frame ? null : target;
|
|
84
|
+
if (frame && options.url && frame.src !== options.url)
|
|
85
|
+
frame.src = options.url;
|
|
86
|
+
const shellUrl = options.url ?? frame?.src;
|
|
87
|
+
const shellOrigin = frame ? originOf(shellUrl) : null;
|
|
88
|
+
if (frame && !shellOrigin)
|
|
89
|
+
throw new Error('embedArtifact: the Shell address (options.url or the iframe src) is not an http(s) address.');
|
|
90
|
+
if (frame && !win)
|
|
91
|
+
throw new Error('embedArtifact: no window to listen on.');
|
|
92
|
+
let theme = options.theme;
|
|
93
|
+
let info = null;
|
|
94
|
+
let place = null;
|
|
95
|
+
let saved = null;
|
|
96
|
+
let helloed = false;
|
|
97
|
+
let stopped = false;
|
|
98
|
+
let resolveReady;
|
|
99
|
+
const ready = new Promise((resolve) => (resolveReady = resolve));
|
|
100
|
+
let thumbTimer = null;
|
|
101
|
+
let lastThumb = 0;
|
|
102
|
+
const report = (error) => {
|
|
103
|
+
try {
|
|
104
|
+
options.onError?.(error);
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
// The host's own error handler failed: nothing more to do.
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
const safely = (run) => {
|
|
111
|
+
try {
|
|
112
|
+
run();
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
report(error);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
const send = (command) => {
|
|
119
|
+
if (stopped)
|
|
120
|
+
return;
|
|
121
|
+
// Both envelope keys: a Shell (or, for a view, a daemon) from before the rename reads `openArtifact`.
|
|
122
|
+
const message = withLegacyEnvelope(command);
|
|
123
|
+
if (frame)
|
|
124
|
+
frame.contentWindow?.postMessage(message, shellOrigin);
|
|
125
|
+
else
|
|
126
|
+
void Promise.resolve(transport.send(message)).catch(report);
|
|
127
|
+
};
|
|
128
|
+
const hello = () => {
|
|
129
|
+
const caps = {};
|
|
130
|
+
if (options.onOpenSession || options.onNewSession)
|
|
131
|
+
caps.sessions = true;
|
|
132
|
+
if (options.toolbar)
|
|
133
|
+
caps.toolbar = true;
|
|
134
|
+
if (options.switcher)
|
|
135
|
+
caps.switcher = true;
|
|
136
|
+
return {
|
|
137
|
+
openArtifacts: 'host_hello',
|
|
138
|
+
caps,
|
|
139
|
+
protocol: HOST_PROTOCOL,
|
|
140
|
+
...(options.agentName ? { agentName: options.agentName } : {}),
|
|
141
|
+
...(options.harness ? { harness: options.harness } : {}),
|
|
142
|
+
...(theme ? { theme } : {}),
|
|
143
|
+
...(options.destinations?.length ? { destinations: options.destinations.slice(0, MAX_HOST_DESTINATIONS).map((d) => ({ id: d.id, label: d.label })) } : {}),
|
|
144
|
+
};
|
|
145
|
+
};
|
|
146
|
+
const thumbnail = () => {
|
|
147
|
+
thumbTimer = null;
|
|
148
|
+
if (stopped || !info || !options.capture || !options.saveThumbnail)
|
|
149
|
+
return;
|
|
150
|
+
lastThumb = Date.now();
|
|
151
|
+
const artifact = info.artifact;
|
|
152
|
+
void (async () => {
|
|
153
|
+
const jpeg = await jpegOf(await options.capture());
|
|
154
|
+
if (jpeg && !stopped)
|
|
155
|
+
await options.saveThumbnail(jpeg, artifact);
|
|
156
|
+
})().catch(report);
|
|
157
|
+
};
|
|
158
|
+
const scheduleThumbnail = (first) => {
|
|
159
|
+
if (stopped || thumbTimer || !options.capture || !options.saveThumbnail)
|
|
160
|
+
return;
|
|
161
|
+
const wait = first ? THUMBNAIL_DELAY_MS : Math.max(THUMBNAIL_DELAY_MS, lastThumb + THUMBNAIL_INTERVAL_MS - Date.now());
|
|
162
|
+
thumbTimer = setTimeout(thumbnail, wait);
|
|
163
|
+
};
|
|
164
|
+
const onShellMessage = (raw) => {
|
|
165
|
+
const message = shellToHostOf(raw);
|
|
166
|
+
if (!message || stopped)
|
|
167
|
+
return;
|
|
168
|
+
switch (message.openArtifacts) {
|
|
169
|
+
case 'shell_hello':
|
|
170
|
+
info = { artifact: message.artifact, id: message.id, title: message.title, type: message.type, version: message.version, protocol: message.protocol };
|
|
171
|
+
helloed = true;
|
|
172
|
+
send(hello());
|
|
173
|
+
break;
|
|
174
|
+
case 'shell_ready': {
|
|
175
|
+
// A top-level view's hello may have come before the host listened: what `shell_ready` says is enough.
|
|
176
|
+
// A Shell from before the rename says `open-artifact://artifact/<id>`.
|
|
177
|
+
info ??= { artifact: message.artifact, id: currentArtifactsUrl(message.artifact).replace(/^open-artifacts:\/\/artifact\//, ''), title: '', type: '', version: '', protocol: HOST_PROTOCOL };
|
|
178
|
+
const ready = info;
|
|
179
|
+
safely(() => options.onReady?.(ready));
|
|
180
|
+
resolveReady(ready);
|
|
181
|
+
if (!lastThumb)
|
|
182
|
+
scheduleThumbnail(true);
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
case 'place':
|
|
186
|
+
place = message.hash;
|
|
187
|
+
safely(() => options.onPlace?.({ artifact: message.artifact, hash: message.hash }));
|
|
188
|
+
if (lastThumb)
|
|
189
|
+
scheduleThumbnail(false);
|
|
190
|
+
break;
|
|
191
|
+
case 'new_session':
|
|
192
|
+
safely(() => options.onNewSession?.({ prompt: message.prompt, artifact: message.artifact }));
|
|
193
|
+
break;
|
|
194
|
+
case 'open_session':
|
|
195
|
+
safely(() => options.onOpenSession?.({ session: message.session, harness: message.harness, artifact: info?.artifact ?? null }));
|
|
196
|
+
break;
|
|
197
|
+
case 'comment_summon':
|
|
198
|
+
safely(() => options.onCommentSent?.({ text: message.text }));
|
|
199
|
+
break;
|
|
200
|
+
case 'close':
|
|
201
|
+
safely(() => options.onClose?.());
|
|
202
|
+
break;
|
|
203
|
+
case 'open_external':
|
|
204
|
+
safely(() => {
|
|
205
|
+
if (options.onOpenExternal)
|
|
206
|
+
options.onOpenExternal(message.url);
|
|
207
|
+
else
|
|
208
|
+
win?.open?.(message.url, '_blank', 'noopener');
|
|
209
|
+
});
|
|
210
|
+
break;
|
|
211
|
+
case 'saved_file':
|
|
212
|
+
safely(() => options.onSavedFile?.(message.path));
|
|
213
|
+
break;
|
|
214
|
+
case 'saved': {
|
|
215
|
+
const now = { state: message.state, leaveLosesWork: message.leaveLosesWork };
|
|
216
|
+
saved = now;
|
|
217
|
+
safely(() => options.onSaved?.({ ...now }));
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
case 'model_use': {
|
|
221
|
+
const { openArtifacts: _kind, ...use } = message;
|
|
222
|
+
safely(() => options.onModelUse?.(use));
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
225
|
+
case 'model_notice': {
|
|
226
|
+
const { openArtifacts: _kind, ...notice } = message;
|
|
227
|
+
safely(() => options.onModelNotice?.(notice));
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
// Anything else (a message of a newer Shell) is not this kit's to answer.
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
const onWindowMessage = (event) => {
|
|
234
|
+
// Only the Shell's own window, from the Shell's origin.
|
|
235
|
+
if (!frame || event.source !== frame.contentWindow || event.origin !== shellOrigin)
|
|
236
|
+
return;
|
|
237
|
+
onShellMessage(event.data);
|
|
238
|
+
};
|
|
239
|
+
let stopTransport = null;
|
|
240
|
+
if (frame)
|
|
241
|
+
win.addEventListener('message', onWindowMessage);
|
|
242
|
+
else {
|
|
243
|
+
stopTransport = transport.onEvent(onShellMessage);
|
|
244
|
+
// A top-level view's Shell may have said hello before the host listened: answer now; it takes the latest.
|
|
245
|
+
send(hello());
|
|
246
|
+
helloed = true;
|
|
247
|
+
}
|
|
248
|
+
return {
|
|
249
|
+
get artifact() {
|
|
250
|
+
return info?.artifact ?? null;
|
|
251
|
+
},
|
|
252
|
+
get place() {
|
|
253
|
+
return place;
|
|
254
|
+
},
|
|
255
|
+
get saved() {
|
|
256
|
+
return saved ? { ...saved } : null;
|
|
257
|
+
},
|
|
258
|
+
ready,
|
|
259
|
+
goto(hash) {
|
|
260
|
+
if (typeof hash === 'string' && hash)
|
|
261
|
+
send({ openArtifacts: 'goto', hash });
|
|
262
|
+
},
|
|
263
|
+
setTheme(next) {
|
|
264
|
+
theme = next;
|
|
265
|
+
if (helloed)
|
|
266
|
+
send(hello());
|
|
267
|
+
},
|
|
268
|
+
url: () => addressOf(shellUrl, place),
|
|
269
|
+
destroy() {
|
|
270
|
+
if (stopped)
|
|
271
|
+
return;
|
|
272
|
+
stopped = true;
|
|
273
|
+
if (thumbTimer)
|
|
274
|
+
clearTimeout(thumbTimer);
|
|
275
|
+
if (frame)
|
|
276
|
+
win?.removeEventListener('message', onWindowMessage);
|
|
277
|
+
stopTransport?.();
|
|
278
|
+
},
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
const text = (value, max) => (typeof value === 'string' ? value.slice(0, max) : '');
|
|
282
|
+
/** Shows the library's home page in an `<iframe>`, listing the host's own artifacts, and turns its messages into callbacks. */
|
|
283
|
+
export function embedLibrary(iframe, options = {}) {
|
|
284
|
+
const win = options.window ?? defaultWindow();
|
|
285
|
+
if (!win)
|
|
286
|
+
throw new Error('embedLibrary: no window to listen on.');
|
|
287
|
+
if (options.url && iframe.src !== options.url)
|
|
288
|
+
iframe.src = options.url;
|
|
289
|
+
const origin = originOf(options.url ?? iframe.src);
|
|
290
|
+
if (!origin)
|
|
291
|
+
throw new Error('embedLibrary: the home page address (options.url or the iframe src) is not an http(s) address.');
|
|
292
|
+
let theme = options.theme;
|
|
293
|
+
let helloed = false;
|
|
294
|
+
let stopped = false;
|
|
295
|
+
const hello = () => {
|
|
296
|
+
const caps = {};
|
|
297
|
+
if (options.onOpenSession || options.onNewSession)
|
|
298
|
+
caps.sessions = true;
|
|
299
|
+
if (options.onOpenArtifact)
|
|
300
|
+
caps.artifacts = true;
|
|
301
|
+
const library = {};
|
|
302
|
+
if (options.harness)
|
|
303
|
+
library.harness = options.harness;
|
|
304
|
+
if (options.workspace)
|
|
305
|
+
library.workspace = options.workspace;
|
|
306
|
+
return {
|
|
307
|
+
openArtifacts: 'host_hello',
|
|
308
|
+
caps,
|
|
309
|
+
protocol: HOST_PROTOCOL,
|
|
310
|
+
...(options.harness ? { harness: options.harness } : {}),
|
|
311
|
+
...(options.agentName ? { agentName: options.agentName } : {}),
|
|
312
|
+
...(theme ? { theme } : {}),
|
|
313
|
+
...(library.harness || library.workspace ? { library } : {}),
|
|
314
|
+
};
|
|
315
|
+
};
|
|
316
|
+
const send = (message) => {
|
|
317
|
+
// Both envelope keys: a home page from before the rename reads `openArtifact`.
|
|
318
|
+
if (!stopped)
|
|
319
|
+
iframe.contentWindow?.postMessage(withLegacyEnvelope(message), origin);
|
|
320
|
+
};
|
|
321
|
+
const safely = (run) => {
|
|
322
|
+
try {
|
|
323
|
+
run();
|
|
324
|
+
}
|
|
325
|
+
catch (error) {
|
|
326
|
+
try {
|
|
327
|
+
options.onError?.(error);
|
|
328
|
+
}
|
|
329
|
+
catch {
|
|
330
|
+
// Nothing more to do.
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
const onMessage = (event) => {
|
|
335
|
+
if (stopped || event.source !== iframe.contentWindow || event.origin !== origin)
|
|
336
|
+
return;
|
|
337
|
+
// A home page from before the rename says `openArtifact` only.
|
|
338
|
+
const data = adoptLegacyEnvelope(event.data);
|
|
339
|
+
if (!data || typeof data !== 'object')
|
|
340
|
+
return;
|
|
341
|
+
switch (data.openArtifacts) {
|
|
342
|
+
case 'home_hello':
|
|
343
|
+
helloed = true;
|
|
344
|
+
send(hello());
|
|
345
|
+
break;
|
|
346
|
+
case 'home_ready':
|
|
347
|
+
safely(() => options.onReady?.());
|
|
348
|
+
break;
|
|
349
|
+
case 'open_artifact': {
|
|
350
|
+
const artifact = { url: text(data.url, 200), id: text(data.id, 100), title: text(data.title, 500), type: text(data.type, 100) };
|
|
351
|
+
if (artifact.url)
|
|
352
|
+
safely(() => options.onOpenArtifact?.(artifact));
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
case 'open_session':
|
|
356
|
+
safely(() => options.onOpenSession?.({ session: typeof data.session === 'string' ? data.session.slice(0, 200) : null, harness: text(data.harness, 64) }));
|
|
357
|
+
break;
|
|
358
|
+
case 'new_session':
|
|
359
|
+
safely(() => options.onNewSession?.({ prompt: text(data.prompt, 8000), artifact: text(data.artifact, 200) }));
|
|
360
|
+
break;
|
|
361
|
+
}
|
|
362
|
+
};
|
|
363
|
+
win.addEventListener('message', onMessage);
|
|
364
|
+
return {
|
|
365
|
+
setTheme(next) {
|
|
366
|
+
theme = next;
|
|
367
|
+
if (helloed)
|
|
368
|
+
send(hello());
|
|
369
|
+
},
|
|
370
|
+
destroy() {
|
|
371
|
+
stopped = true;
|
|
372
|
+
win.removeEventListener('message', onMessage);
|
|
373
|
+
},
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
export { remoteGate, remotePolicyOf } from "./remote.js";
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a host shows a page that comes from another computer: a remote type's page (`host.open`'s `remote`).
|
|
3
|
+
*
|
|
4
|
+
* The host picks a policy (`HarnessOptions.embed.remote`, said in `hello` as `caps.open.remote`):
|
|
5
|
+
*
|
|
6
|
+
* - `allow`: show it, as any other page.
|
|
7
|
+
* - `ask` (the default): ask the person through the host's `allowRemote({ origin, type, title })`. A yes is kept per
|
|
8
|
+
* origin for the life of the process; a no (or a question that throws) is not, so the next open asks again. With no
|
|
9
|
+
* `allowRemote`, the answer is no.
|
|
10
|
+
* - `deny`: never show it; the daemon is told the page was not opened.
|
|
11
|
+
*
|
|
12
|
+
* No Node imports: an app may also run this in its renderer.
|
|
13
|
+
*/
|
|
14
|
+
import type { RemotePagePolicy } from '@openartifactsai/protocol';
|
|
15
|
+
export type { RemotePagePolicy };
|
|
16
|
+
/** What `allowRemote` is asked: may a page from `origin` (of type `type`) be shown? */
|
|
17
|
+
export interface RemotePageRequest {
|
|
18
|
+
/** The other computer's origin, e.g. `https://types.example.com`. */
|
|
19
|
+
origin: string;
|
|
20
|
+
/** The type id. */
|
|
21
|
+
type: string;
|
|
22
|
+
/** The artifact's title, when the daemon said it. */
|
|
23
|
+
title?: string;
|
|
24
|
+
}
|
|
25
|
+
/** The host's answer: `true` shows the page. */
|
|
26
|
+
export type AllowRemote = (request: RemotePageRequest) => boolean | Promise<boolean>;
|
|
27
|
+
/** The remote part of a `host.open`: the type and the origins its page comes from. */
|
|
28
|
+
export interface RemotePage {
|
|
29
|
+
type: string;
|
|
30
|
+
origins: string[];
|
|
31
|
+
}
|
|
32
|
+
/** The policy a host's option names, or the default (`ask`) for anything else. */
|
|
33
|
+
export declare function remotePolicyOf(value: unknown): RemotePagePolicy;
|
|
34
|
+
/**
|
|
35
|
+
* A gate for remote pages under one policy: `(remote, title) → shown?`. A page with no `remote` (a type on this
|
|
36
|
+
* computer) always passes. Under `ask`, a yes is kept per origin for the gate's life; a no, or a question that
|
|
37
|
+
* throws, is not kept: the person is asked again the next time.
|
|
38
|
+
*/
|
|
39
|
+
export declare function remoteGate(policy?: RemotePagePolicy, allowRemote?: AllowRemote): (remote: RemotePage | null | undefined, title?: string) => Promise<boolean>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a host shows a page that comes from another computer: a remote type's page (`host.open`'s `remote`).
|
|
3
|
+
*
|
|
4
|
+
* The host picks a policy (`HarnessOptions.embed.remote`, said in `hello` as `caps.open.remote`):
|
|
5
|
+
*
|
|
6
|
+
* - `allow`: show it, as any other page.
|
|
7
|
+
* - `ask` (the default): ask the person through the host's `allowRemote({ origin, type, title })`. A yes is kept per
|
|
8
|
+
* origin for the life of the process; a no (or a question that throws) is not, so the next open asks again. With no
|
|
9
|
+
* `allowRemote`, the answer is no.
|
|
10
|
+
* - `deny`: never show it; the daemon is told the page was not opened.
|
|
11
|
+
*
|
|
12
|
+
* No Node imports: an app may also run this in its renderer.
|
|
13
|
+
*/
|
|
14
|
+
/** The policy a host's option names, or the default (`ask`) for anything else. */
|
|
15
|
+
export function remotePolicyOf(value) {
|
|
16
|
+
return value === 'allow' || value === 'deny' || value === 'ask' ? value : 'ask';
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A gate for remote pages under one policy: `(remote, title) → shown?`. A page with no `remote` (a type on this
|
|
20
|
+
* computer) always passes. Under `ask`, a yes is kept per origin for the gate's life; a no, or a question that
|
|
21
|
+
* throws, is not kept: the person is asked again the next time.
|
|
22
|
+
*/
|
|
23
|
+
export function remoteGate(policy = 'ask', allowRemote) {
|
|
24
|
+
const answers = new Map();
|
|
25
|
+
const ask = (origin, type, title) => {
|
|
26
|
+
let answer = answers.get(origin);
|
|
27
|
+
if (!answer) {
|
|
28
|
+
answer = (async () => (await allowRemote({ origin, type, ...(title !== undefined ? { title } : {}) })) === true)();
|
|
29
|
+
// Shared while the question is open (two opens at once ask once); only a yes stays after it.
|
|
30
|
+
answers.set(origin, answer);
|
|
31
|
+
answer.then((yes) => {
|
|
32
|
+
if (!yes)
|
|
33
|
+
answers.delete(origin);
|
|
34
|
+
}, () => answers.delete(origin));
|
|
35
|
+
}
|
|
36
|
+
return answer.catch(() => false);
|
|
37
|
+
};
|
|
38
|
+
return async (remote, title) => {
|
|
39
|
+
if (!remote || !Array.isArray(remote.origins) || !remote.origins.length)
|
|
40
|
+
return true;
|
|
41
|
+
if (policy === 'allow')
|
|
42
|
+
return true;
|
|
43
|
+
if (policy === 'deny' || !allowRemote)
|
|
44
|
+
return false;
|
|
45
|
+
for (const origin of remote.origins) {
|
|
46
|
+
if (!(await ask(origin, remote.type, title)))
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
return true;
|
|
50
|
+
};
|
|
51
|
+
}
|