@crossworks/share-ui 0.500.35 → 0.500.36
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/package.json +3 -3
- package/src/app-bridge-protocol.ts +14 -2
- package/src/app-frame-html.test.ts +67 -0
- package/src/app-frame-html.ts +19 -0
- package/src/app-sandbox.tsx +3 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crossworks/share-ui",
|
|
3
|
-
"version": "0.500.
|
|
3
|
+
"version": "0.500.36",
|
|
4
4
|
"description": "The server-rendered share surface — the /s/<token> presenters, view-payload contract, mini-app sandbox, and the few primitives they need. Lives in MANTLE (the server renders these), published for jackdaw to consume; may depend only on @mantle/{client-types,content-core} (the jackdaw-repo-split boundary).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -42,8 +42,8 @@
|
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@dicebear/core": "^10.7.0",
|
|
44
44
|
"@dicebear/styles": "^10.6.0",
|
|
45
|
-
"@mantle/client-types": "npm:@crossworks/client-types@0.500.
|
|
46
|
-
"@mantle/content-core": "npm:@crossworks/content-core@0.500.
|
|
45
|
+
"@mantle/client-types": "npm:@crossworks/client-types@0.500.36",
|
|
46
|
+
"@mantle/content-core": "npm:@crossworks/content-core@0.500.36",
|
|
47
47
|
"@radix-ui/react-label": "^2.1.17",
|
|
48
48
|
"@radix-ui/react-slot": "^1.5.0",
|
|
49
49
|
"class-variance-authority": "^0.7.1",
|
|
@@ -69,7 +69,15 @@ export type BridgeEvt =
|
|
|
69
69
|
| { v: 1; kind: 'resize'; height: number }
|
|
70
70
|
| { v: 1; kind: 'error'; message: string; stack?: string }
|
|
71
71
|
// Team-hub surface only; ignored anywhere else.
|
|
72
|
-
| { v: 1; kind: 'hub.nav'; target: HubNavTarget }
|
|
72
|
+
| { v: 1; kind: 'hub.nav'; target: HubNavTarget }
|
|
73
|
+
// The frame's answer to a `look` (ui_look): its own picture, or why not.
|
|
74
|
+
// Sent by the frame document's listener (app-frame-html.ts), not the kit.
|
|
75
|
+
| LookRes;
|
|
76
|
+
|
|
77
|
+
/** A frame's answer to `look`: a PNG data URL of its visible viewport. */
|
|
78
|
+
export type LookRes =
|
|
79
|
+
| { v: 1; kind: 'look.res'; id: string; ok: true; dataUrl: string }
|
|
80
|
+
| { v: 1; kind: 'look.res'; id: string; ok: false; error: string };
|
|
73
81
|
|
|
74
82
|
/** Responses the host sends DOWN, correlated to a BridgeReq by `id`. */
|
|
75
83
|
export type BridgeRes =
|
|
@@ -85,7 +93,11 @@ export type BridgeCtl =
|
|
|
85
93
|
// Live theme: the host's <html> class + data-color-theme, mirrored onto the
|
|
86
94
|
// iframe's <html> so a dark/light or colour-theme switch restyles a RUNNING
|
|
87
95
|
// app (every theme's tokens are already in the linked stylesheet).
|
|
88
|
-
| { v: 1; kind: 'theme'; cls: string; colorTheme: string | null }
|
|
96
|
+
| { v: 1; kind: 'theme'; cls: string; colorTheme: string | null }
|
|
97
|
+
// ui_look (docs/agent-ui.md, "Look"): the host cannot read inside the
|
|
98
|
+
// frame, so it asks the frame to draw itself at `scale`; one `look.res`
|
|
99
|
+
// answers it.
|
|
100
|
+
| { v: 1; kind: 'look'; id: string; scale: number };
|
|
89
101
|
|
|
90
102
|
export type FromApp = BridgeReq | BridgeEvt;
|
|
91
103
|
export type FromHost = BridgeRes | BridgeCtl;
|
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { describe, expect, it } from 'vitest';
|
|
9
9
|
import {
|
|
10
|
+
LOOK_LISTENER,
|
|
10
11
|
appLibraryScripts,
|
|
11
12
|
buildAppFrameCsp,
|
|
12
13
|
buildAppFrameHtml,
|
|
@@ -209,3 +210,69 @@ describe('buildAppFrameCsp: the library files an app may load', () => {
|
|
|
209
210
|
expect(csp).toContain("connect-src 'none'");
|
|
210
211
|
});
|
|
211
212
|
});
|
|
213
|
+
|
|
214
|
+
describe('buildAppFrameHtml: the frame draws itself for ui_look', () => {
|
|
215
|
+
/** The listener run against a stand-in window, its dynamic import stood in. */
|
|
216
|
+
function listener(draw: (scale: number) => Promise<string>) {
|
|
217
|
+
const parent = {
|
|
218
|
+
sent: [] as unknown[],
|
|
219
|
+
postMessage(m: unknown) {
|
|
220
|
+
this.sent.push(m);
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
let onMessage: ((e: { source: unknown; data: unknown }) => void) | null = null;
|
|
224
|
+
const win = {
|
|
225
|
+
parent,
|
|
226
|
+
addEventListener: (_t: string, fn: typeof onMessage) => (onMessage = fn),
|
|
227
|
+
};
|
|
228
|
+
const loaded: string[] = [];
|
|
229
|
+
const code = LOOK_LISTENER.replace("import('/share-runtime/frame-look.js')", '__imp()');
|
|
230
|
+
expect(code).not.toBe(LOOK_LISTENER);
|
|
231
|
+
new Function('window', '__imp', code)(win, () => {
|
|
232
|
+
loaded.push('/share-runtime/frame-look.js');
|
|
233
|
+
return Promise.resolve({ drawFrame: draw });
|
|
234
|
+
});
|
|
235
|
+
const send = async (source: unknown, data: unknown) => {
|
|
236
|
+
onMessage!({ source, data });
|
|
237
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
238
|
+
};
|
|
239
|
+
return { parent, loaded, send };
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
it('is in every frame document, and the CSP lets it load its drawer', () => {
|
|
243
|
+
expect(frame('x')).toContain(LOOK_LISTENER);
|
|
244
|
+
expect(buildAppFrameCsp('https://b.example')).toContain('https://b.example/share-runtime/');
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it('answers its parent once, with the picture at the asked scale', async () => {
|
|
248
|
+
const scales: number[] = [];
|
|
249
|
+
const l = listener(async (s) => {
|
|
250
|
+
scales.push(s);
|
|
251
|
+
return 'data:image/png;base64,AAAA';
|
|
252
|
+
});
|
|
253
|
+
await l.send(l.parent, { v: 1, kind: 'look', id: 'q1', scale: 1.5 });
|
|
254
|
+
expect(scales).toEqual([1.5]);
|
|
255
|
+
expect(l.parent.sent).toEqual([
|
|
256
|
+
{ ok: true, dataUrl: 'data:image/png;base64,AAAA', v: 1, kind: 'look.res', id: 'q1' },
|
|
257
|
+
]);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('a failed draw says why', async () => {
|
|
261
|
+
const l = listener(async () => {
|
|
262
|
+
throw new Error('no canvas');
|
|
263
|
+
});
|
|
264
|
+
await l.send(l.parent, { v: 1, kind: 'look', id: 'q2', scale: 1 });
|
|
265
|
+
expect(l.parent.sent).toEqual([
|
|
266
|
+
{ ok: false, error: 'no canvas', v: 1, kind: 'look.res', id: 'q2' },
|
|
267
|
+
]);
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it('ignores anyone but its parent, and any other message: nothing is loaded', async () => {
|
|
271
|
+
const l = listener(async () => 'data:,');
|
|
272
|
+
await l.send({}, { v: 1, kind: 'look', id: 'q3', scale: 1 });
|
|
273
|
+
await l.send(l.parent, { v: 1, kind: 'theme', cls: 'dark' });
|
|
274
|
+
await l.send(l.parent, { v: 1, kind: 'look', scale: 1 });
|
|
275
|
+
expect(l.loaded).toEqual([]);
|
|
276
|
+
expect(l.parent.sent).toEqual([]);
|
|
277
|
+
});
|
|
278
|
+
});
|
package/src/app-frame-html.ts
CHANGED
|
@@ -89,6 +89,24 @@ const THEME_SYNC = `
|
|
|
89
89
|
})();
|
|
90
90
|
`;
|
|
91
91
|
|
|
92
|
+
// Host-injected too, for ui_look (docs/agent-ui.md, "Look"): on `look` from
|
|
93
|
+
// the parent, the frame loads its drawer (/share-runtime/frame-look.js, never
|
|
94
|
+
// part of an app bundle, so every app answers with no rebuild) and posts one
|
|
95
|
+
// `look.res` back: its visible viewport as a PNG data URL, or why not.
|
|
96
|
+
export const LOOK_LISTENER = `
|
|
97
|
+
(function(){
|
|
98
|
+
window.addEventListener('message', function(e){
|
|
99
|
+
if(e.source!==window.parent) return;
|
|
100
|
+
var m=e.data; if(!m||m.v!==1||m.kind!=='look'||typeof m.id!=='string') return;
|
|
101
|
+
function reply(r){ r.v=1; r.kind='look.res'; r.id=m.id; try{ window.parent.postMessage(r,'*'); }catch(_){} }
|
|
102
|
+
import('/share-runtime/frame-look.js')
|
|
103
|
+
.then(function(x){ return x.drawFrame(typeof m.scale==='number'?m.scale:1); })
|
|
104
|
+
.then(function(url){ reply({ok:true,dataUrl:url}); },
|
|
105
|
+
function(err){ reply({ok:false,error:String((err&&err.message)||err)}); });
|
|
106
|
+
});
|
|
107
|
+
})();
|
|
108
|
+
`;
|
|
109
|
+
|
|
92
110
|
/** HTML-attribute escape for the theme values baked into the <html> tag —
|
|
93
111
|
* they arrive as query params, so they are untrusted for MARKUP purposes
|
|
94
112
|
* even though a wrong theme is harmless. */
|
|
@@ -268,6 +286,7 @@ ${
|
|
|
268
286
|
<script>${ERROR_REPORTER}</script>
|
|
269
287
|
${viewerScript(opts.viewer)}<script type="module">${opts.bundleCode}</script>
|
|
270
288
|
<script>${THEME_SYNC}</script>
|
|
289
|
+
<script>${LOOK_LISTENER}</script>
|
|
271
290
|
${opts.neatSpec ? `<script type="module" src="/share-runtime/share-page.js"></script>\n` : ''}</body>
|
|
272
291
|
</html>`;
|
|
273
292
|
}
|
package/src/app-sandbox.tsx
CHANGED
|
@@ -363,6 +363,9 @@ export function AppSandbox({
|
|
|
363
363
|
}
|
|
364
364
|
return;
|
|
365
365
|
}
|
|
366
|
+
// A frame's picture for ui_look: the agent UI's own listener takes
|
|
367
|
+
// it; it is no request.
|
|
368
|
+
if (m.kind === 'look.res') return;
|
|
366
369
|
if (m.kind === 'hub.nav') {
|
|
367
370
|
// Validate before navigating — never act on a malformed message from a
|
|
368
371
|
// (possibly buggy) app bundle. Ignored off the /team surface.
|