@crossworks/share-ui 0.709.1 → 0.709.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crossworks/share-ui",
3
- "version": "0.709.1",
3
+ "version": "0.709.2",
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.709.1",
46
- "@mantle/content-core": "npm:@crossworks/content-core@0.709.1",
45
+ "@mantle/client-types": "npm:@crossworks/client-types@0.709.2",
46
+ "@mantle/content-core": "npm:@crossworks/content-core@0.709.2",
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",
@@ -1,6 +1,7 @@
1
1
  import { createContext, runInContext, runInNewContext } from 'node:vm';
2
2
  import { describe, expect, it } from 'vitest';
3
3
  import {
4
+ CANVAS_LOOK_DRAWER,
4
5
  buildCanvasEmbedCsp,
5
6
  buildCanvasFrameCsp,
6
7
  buildCanvasFrameHtml,
@@ -205,6 +206,8 @@ describe('the frame prelude: mantle.data (DWG phase 7a)', () => {
205
206
  win?: Record<string, unknown>;
206
207
  globals?: Record<string, unknown>;
207
208
  media?: boolean;
209
+ /** Stands in for the drawer's `import()` (a vm script has none). */
210
+ imp?: () => Promise<unknown>;
208
211
  } = {},
209
212
  ) {
210
213
  const html = buildCanvasFrameHtml({
@@ -220,6 +223,8 @@ describe('the frame prelude: mantle.data (DWG phase 7a)', () => {
220
223
  nonce,
221
224
  });
222
225
  const prelude = /<script>(\(function\(\)\{[\s\S]*?\}\)\(\);)<\/script>/.exec(html)![1]!;
226
+ const run = o.imp ? prelude.replace(`import('${CANVAS_LOOK_DRAWER}')`, '__imp()') : prelude;
227
+ if (o.imp && run === prelude) throw new Error('the prelude does not import the drawer');
223
228
  const toParent: Record<string, unknown>[] = [];
224
229
  const handlers: Record<string, Array<(e: unknown) => void>> = {};
225
230
  const options: Record<string, unknown[]> = {};
@@ -244,6 +249,7 @@ describe('the frame prelude: mantle.data (DWG phase 7a)', () => {
244
249
  };
245
250
  const ctx = {
246
251
  ...o.globals,
252
+ ...(o.imp ? { __imp: o.imp } : {}),
247
253
  window: win,
248
254
  document: { currentScript: null, documentElement: el },
249
255
  Object,
@@ -258,7 +264,7 @@ describe('the frame prelude: mantle.data (DWG phase 7a)', () => {
258
264
  };
259
265
  // One realm: on Bun runInNewContext would give the prelude one of its own.
260
266
  createContext(ctx);
261
- runInContext(prelude, ctx);
267
+ runInContext(run, ctx);
262
268
  // What a canvas runs after the prelude, in the same realm.
263
269
  if (canvas) runInContext(canvas, ctx);
264
270
  const api = (
@@ -296,6 +302,44 @@ describe('the frame prelude: mantle.data (DWG phase 7a)', () => {
296
302
  return PageTransitionEvent;
297
303
  }
298
304
 
305
+ it('look: draws itself for its parent only, once, at the asked scale, with no nonce', async () => {
306
+ const scales: number[] = [];
307
+ const f = frame('n-look', undefined, {
308
+ imp: () =>
309
+ Promise.resolve({
310
+ drawFrame: (s: number) => {
311
+ scales.push(s);
312
+ return Promise.resolve('data:image/png;base64,AAAA');
313
+ },
314
+ }),
315
+ });
316
+ const settle = () => new Promise((r) => setTimeout(r, 0));
317
+ // Not the parent: no picture.
318
+ f.reply({}, { v: 1, kind: 'look', id: 'x', scale: 2 });
319
+ f.reply(f.parent, { v: 1, kind: 'look', id: 'q1', scale: 1.5 });
320
+ await settle();
321
+ await settle();
322
+ expect(scales).toEqual([1.5]);
323
+ const res = f.toParent.filter((m) => m.kind === 'look.res');
324
+ expect(res).toEqual([
325
+ { ok: true, dataUrl: 'data:image/png;base64,AAAA', v: 1, kind: 'look.res', id: 'q1' },
326
+ ]);
327
+ expect(JSON.stringify(res)).not.toContain('n-look');
328
+ });
329
+
330
+ it('look: a drawer that fails answers why, cut short', async () => {
331
+ const f = frame('n1', undefined, {
332
+ imp: () => Promise.reject(new Error('x'.repeat(400))),
333
+ });
334
+ f.reply(f.parent, { v: 1, kind: 'look', id: 'q2', scale: 1 });
335
+ await new Promise((r) => setTimeout(r, 0));
336
+ await new Promise((r) => setTimeout(r, 0));
337
+ const res = f.toParent.filter((m) => m.kind === 'look.res');
338
+ expect(res).toHaveLength(1);
339
+ expect(res[0]).toMatchObject({ ok: false, id: 'q2' });
340
+ expect((res[0]!.error as string).length).toBe(300);
341
+ });
342
+
299
343
  it('runs in strict mode', () => {
300
344
  expect(frame('n0').prelude.startsWith("(function(){'use strict';")).toBe(true);
301
345
  });
@@ -637,6 +681,41 @@ describe('the frame prelude: mantle.data (DWG phase 7a)', () => {
637
681
  });
638
682
  });
639
683
 
684
+ describe('the drawer for look: no policy line moved', () => {
685
+ const scriptSrc = (csp: string) =>
686
+ csp
687
+ .split(';')
688
+ .map((d) => d.trim())
689
+ .find((d) => d.startsWith('script-src '));
690
+
691
+ it('the drawer is under the vendored runtime path that script-src already allows', () => {
692
+ expect(CANVAS_LOOK_DRAWER).toBe('/canvas-runtime/frame-look.js');
693
+ expect(scriptSrc(buildCanvasFrameCsp(ORIGIN, []))).toBe(
694
+ `script-src 'unsafe-inline' 'unsafe-eval' ${ORIGIN}/canvas-runtime/`,
695
+ );
696
+ });
697
+
698
+ it('the header and the parent embed policy allow the same scripts (an old client keeps its presents)', () => {
699
+ expect(scriptSrc(buildCanvasEmbedCsp(ORIGIN))).toBe(scriptSrc(buildCanvasFrameCsp(ORIGIN, [])));
700
+ });
701
+
702
+ it('the frame imports that one drawer and nothing else', () => {
703
+ const html = buildCanvasFrameHtml({
704
+ source: '<html><body>x</body></html>',
705
+ css: '',
706
+ importMapJson: '{"imports":{}}',
707
+ cls: '',
708
+ colorTheme: null,
709
+ size: 'fill',
710
+ theme: 'follow',
711
+ fontCss: '',
712
+ nonce: 'n',
713
+ });
714
+ expect(html.match(/import\(/g)).toEqual(['import(']);
715
+ expect(html).toContain(`import('${CANVAS_LOOK_DRAWER}')`);
716
+ });
717
+ });
718
+
640
719
  describe('buildCanvasEmbedCsp', () => {
641
720
  it('requires no network of whatever loads in the frame, and names only what an embedder may', () => {
642
721
  const csp = buildCanvasEmbedCsp('https://brain.example');
@@ -93,6 +93,12 @@ export function buildCanvasEmbedCsp(origin: string): string {
93
93
  );
94
94
  }
95
95
 
96
+ /** The present frame's drawer for `look` (a copy of the mini app frame's
97
+ * /share-runtime/frame-look.js, build-share-runtime.ts). Under
98
+ * /canvas-runtime/, which `script-src` already allows, so the frame policy
99
+ * and the parent's embed policy stay as they were. */
100
+ export const CANVAS_LOOK_DRAWER = '/canvas-runtime/frame-look.js';
101
+
96
102
  /** JSON for an inline script: `<` and the two JS line separators escaped, so
97
103
  * no value can close the tag. */
98
104
  function scriptJson(value: unknown): string {
@@ -155,6 +161,16 @@ function styleText(css: string): string {
155
161
  * a media message, so the call refuses at once instead of waiting, and
156
162
  * a render (no nonce) refuses at once too. The parent decides which
157
163
  * files: those the canvas declared, that the viewer may read.
164
+ * - Look (docs/agent-ui.md, "Look"; the area capture too): on
165
+ * `{v:1, kind:'look', id, scale}` from the parent only, the frame
166
+ * imports its drawer, `/canvas-runtime/frame-look.js` (the mini app
167
+ * frame's drawer, server/web/server/share-page/frame-look.ts, copied
168
+ * there by build-share-runtime.ts), and posts one
169
+ * `{v:1, kind:'look.res', id, ok, dataUrl | error}` back: its own
170
+ * viewport as a PNG. No policy line moved: `script-src` already allows
171
+ * `${origin}/canvas-runtime/`, in the header and in the parent's embed
172
+ * policy. The reply carries no nonce. A canvas could draw itself or
173
+ * answer for itself anyway; the picture shows only its own frame.
158
174
  * - Leaving: `{v:1, kind:'leaving', n}` as the document unloads (pagehide,
159
175
  * taken in the capture phase, registered before any canvas code runs),
160
176
  * so the parent takes the frame down before a navigated-to page runs.
@@ -220,7 +236,14 @@ function preludeScript(opts: {
220
236
  else if(m.kind==='media-reply'){var x=waits[m.id];if(!x||x.k!=='m')return;delete waits[m.id];clearTimeout(x.t);
221
237
  if(m.ok===true&&toStr.call(m.bytes)==='[object ArrayBuffer]'&&typeof m.mime==='string'){x.ok(m);}
222
238
  else{x.no(new Error(typeof m.error==='string'?m.error.slice(0,300):'the file could not be loaded'));}}
239
+ else if(m.kind==='look'&&typeof m.id==='string'){look(m.id,typeof m.scale==='number'?m.scale:1);}
223
240
  });
241
+ function look(id,scale){
242
+ function reply(r){r.v=1;r.kind='look.res';r.id=id;try{P.postMessage(r,'*');}catch(_){}}
243
+ import('${CANVAS_LOOK_DRAWER}')
244
+ .then(function(x){return x.drawFrame(scale);})
245
+ .then(function(u){reply({ok:true,dataUrl:u});},
246
+ function(err){reply({ok:false,error:String((err&&err.message)||err).slice(0,300)});});}
224
247
  function send(r){try{if(r.f){P.postMessage({v:1,kind:'media',n:nonce,id:r.id,file:r.f},'*');}
225
248
  else{P.postMessage({v:1,kind:'data',n:nonce,id:r.id,op:r.op,bucket:r.bucket,key:r.key,json:r.json,everyone:r.everyone,by:r.by},'*');}}
226
249
  catch(_){var w=waits[r.id];if(w){delete waits[r.id];clearTimeout(w.t);w.no(new Error(r.f?'the file could not be asked for':'the canvas data store is not available'));}}}