@crossworks/share-ui 0.500.23 → 0.500.30

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.500.23",
3
+ "version": "0.500.30",
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.23",
46
- "@mantle/content-core": "npm:@crossworks/content-core@0.500.23",
45
+ "@mantle/client-types": "npm:@crossworks/client-types@0.500.30",
46
+ "@mantle/content-core": "npm:@crossworks/content-core@0.500.30",
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",
@@ -200,9 +200,15 @@ describe('the frame prelude: mantle.data (DWG phase 7a)', () => {
200
200
  function frame(
201
201
  nonce: string,
202
202
  canvas?: string,
203
- o: { source?: string; win?: Record<string, unknown>; globals?: Record<string, unknown> } = {},
203
+ o: {
204
+ source?: string;
205
+ win?: Record<string, unknown>;
206
+ globals?: Record<string, unknown>;
207
+ media?: boolean;
208
+ } = {},
204
209
  ) {
205
210
  const html = buildCanvasFrameHtml({
211
+ media: o.media,
206
212
  source: o.source ?? '<html><body>x</body></html>',
207
213
  css: '',
208
214
  importMapJson: '{"imports":{}}',
@@ -502,6 +508,133 @@ describe('the frame prelude: mantle.data (DWG phase 7a)', () => {
502
508
  await expect(f.api.clear!()).rejects.toThrow('picture');
503
509
  expect(f.toParent.filter((m) => m.kind === 'data')).toEqual([]);
504
510
  });
511
+
512
+ describe('mantle.media (task a265178d)', () => {
513
+ const A = '11111111-1111-4111-8111-111111111111';
514
+ /** A frame whose host answers media, with stand-in Blob and URL. */
515
+ function mediaFrame(nonce: string, media = true) {
516
+ const made: string[] = [];
517
+ const revoked: string[] = [];
518
+ class Blob {
519
+ constructor(
520
+ readonly parts: unknown[],
521
+ readonly opts: { type: string },
522
+ ) {}
523
+ }
524
+ // A constructor with the two statics, as the browser's URL.
525
+ const URL = Object.assign(function URL() {}, {
526
+ createObjectURL: (b: { opts: { type: string } }) => {
527
+ const u = `blob:x/${made.length}-${b.opts.type}`;
528
+ made.push(u);
529
+ return u;
530
+ },
531
+ revokeObjectURL: (u: string) => revoked.push(u),
532
+ });
533
+ const f = frame(nonce, undefined, { media, globals: { Blob, URL } });
534
+ const api = (f.win.mantle as { media: Record<string, (id: unknown) => Promise<string>> })
535
+ .media;
536
+ return { ...f, media: api, made, revoked };
537
+ }
538
+
539
+ it('holds a call until ready, sends the file id with the nonce, and makes a blob URL', async () => {
540
+ const f = mediaFrame('m1');
541
+ const got = f.media.url!(A.toUpperCase());
542
+ expect(f.toParent).toEqual([]);
543
+ f.load();
544
+ expect(f.toParent[1]).toEqual({ v: 1, kind: 'media', n: 'm1', id: 1, file: A });
545
+ f.reply(f.parent, {
546
+ v: 1,
547
+ kind: 'media-reply',
548
+ id: 1,
549
+ ok: true,
550
+ mime: 'audio/mpeg',
551
+ bytes: new ArrayBuffer(3),
552
+ });
553
+ await expect(got).resolves.toBe('blob:x/0-audio/mpeg');
554
+ // Cached: the same URL, no second call.
555
+ await expect(f.media.url!(A)).resolves.toBe('blob:x/0-audio/mpeg');
556
+ expect(f.toParent.filter((m) => m.kind === 'media')).toHaveLength(1);
557
+ // image is the same call.
558
+ expect(f.media.image).toBe(f.media.url);
559
+ });
560
+
561
+ it('release revokes the URL and the next call asks again; pagehide revokes all', async () => {
562
+ const f = mediaFrame('m2');
563
+ f.load();
564
+ const got = f.media.url!(A);
565
+ f.reply(f.parent, {
566
+ v: 1,
567
+ kind: 'media-reply',
568
+ id: 1,
569
+ ok: true,
570
+ mime: 'audio/mpeg',
571
+ bytes: new ArrayBuffer(1),
572
+ });
573
+ const u = await got;
574
+ void f.media.release!(A);
575
+ expect(f.revoked).toEqual([u]);
576
+ void f.media.url!(A);
577
+ expect(f.toParent.filter((m) => m.kind === 'media')).toHaveLength(2);
578
+ f.reply(f.parent, {
579
+ v: 1,
580
+ kind: 'media-reply',
581
+ id: 2,
582
+ ok: true,
583
+ mime: 'audio/mpeg',
584
+ bytes: new ArrayBuffer(1),
585
+ });
586
+ await Promise.resolve();
587
+ await Promise.resolve();
588
+ f.pagehide(false);
589
+ expect(f.revoked).toHaveLength(2);
590
+ });
591
+
592
+ it('a refusal is not cached; a forged or wrong-kind reply resolves nothing', async () => {
593
+ const f = mediaFrame('m3');
594
+ f.load();
595
+ const first = f.media.url!(A);
596
+ // A data reply with the media call's id, and a reply from a stranger.
597
+ f.reply(f.parent, { v: 1, kind: 'data-reply', id: 1, ok: true, result: 'x' });
598
+ f.reply(
599
+ { postMessage() {} },
600
+ {
601
+ v: 1,
602
+ kind: 'media-reply',
603
+ id: 1,
604
+ ok: true,
605
+ mime: 'audio/mpeg',
606
+ bytes: new ArrayBuffer(1),
607
+ },
608
+ );
609
+ // Bytes that are not an ArrayBuffer are a refusal.
610
+ f.reply(f.parent, {
611
+ v: 1,
612
+ kind: 'media-reply',
613
+ id: 1,
614
+ ok: true,
615
+ mime: 'audio/mpeg',
616
+ bytes: 'x',
617
+ });
618
+ await expect(first).rejects.toThrow('could not be loaded');
619
+ void f.media.url!(A).catch(() => undefined);
620
+ expect(f.toParent.filter((m) => m.kind === 'media')).toHaveLength(2);
621
+ });
622
+
623
+ it('refuses a non-uuid, an older host (no media) and a render at once, sending nothing', async () => {
624
+ const f = mediaFrame('m4');
625
+ f.load();
626
+ await expect(f.media.url!('../x')).rejects.toThrow('uuid');
627
+ const old = mediaFrame('m5', false);
628
+ old.load();
629
+ await expect(old.media.url!(A)).rejects.toThrow('not available');
630
+ const render = mediaFrame('', true);
631
+ render.load();
632
+ await expect(render.media.url!(A)).rejects.toThrow('not available');
633
+ expect(
634
+ [...f.toParent, ...old.toParent, ...render.toParent].filter((m) => m.kind === 'media'),
635
+ ).toEqual([]);
636
+ });
637
+ });
505
638
  });
506
639
 
507
640
  describe('buildCanvasEmbedCsp', () => {
@@ -20,6 +20,7 @@
20
20
  */
21
21
  import type { CanvasSize } from '@mantle/content-core/canvas';
22
22
  import { CANVAS_DATA_CALL_TIMEOUT_MS } from '@mantle/content-core/canvas-data';
23
+ import { CANVAS_MEDIA_CALL_TIMEOUT_MS } from '@mantle/content-core/canvas-media';
23
24
 
24
25
  /**
25
26
  * The canvas frame CSP, sent as a HEADER (section 5.2). Every source names
@@ -141,6 +142,19 @@ function styleText(css: string): string {
141
142
  * parent binds both, and checks every field (canvas-sandbox.tsx). A
142
143
  * canvas that never calls it sends nothing. With no parent (a render: no
143
144
  * nonce) reads answer empty and writes refuse, at once.
145
+ * - Media (task a265178d): `window.mantle.media` (url, image, release),
146
+ * beside data and on the same terms (not on a canvas that names an
147
+ * element mantle). `url(fileId)` asks the parent for a file's bytes,
148
+ * `{v:1, kind:'media', n, id, file}`, held until `ready` like a data
149
+ * call, and resolves to a blob: URL made here from the parent's
150
+ * `{v:1, kind:'media-reply', id, ok, mime, bytes}` (bytes an
151
+ * ArrayBuffer, transferred). One URL per file, cached until `release`
152
+ * or the page goes; a refusal is not cached. `image` is the same call
153
+ * (the DWG pictures' name). Only when the host asked for media (`m=1`
154
+ * on the frame URL, so `media: true` here): an older host never answers
155
+ * a media message, so the call refuses at once instead of waiting, and
156
+ * a render (no nonce) refuses at once too. The parent decides which
157
+ * files: those the canvas declared, that the viewer may read.
144
158
  * - Leaving: `{v:1, kind:'leaving', n}` as the document unloads (pagehide,
145
159
  * taken in the capture phase, registered before any canvas code runs),
146
160
  * so the parent takes the frame down before a navigated-to page runs.
@@ -156,6 +170,8 @@ function preludeScript(opts: {
156
170
  nonce: string;
157
171
  /** The canvas names an element mantle (namesMantle): window.mantle stays its. */
158
172
  named: boolean;
173
+ /** The host answers `mantle.media` calls (it asked with `m=1`). */
174
+ media: boolean;
159
175
  }): string {
160
176
  const cfg = scriptJson({
161
177
  cls: opts.cls,
@@ -164,6 +180,7 @@ function preludeScript(opts: {
164
180
  size: opts.size === 'fill' ? null : opts.size,
165
181
  n: opts.nonce,
166
182
  named: opts.named,
183
+ m: opts.media,
167
184
  });
168
185
  // The script takes itself out of the document as it runs: the nonce lives
169
186
  // on only in this closure, out of the canvas's reach (it runs later, in the
@@ -185,16 +202,28 @@ function preludeScript(opts: {
185
202
  function theme(cls,ct){h.className=cls||'';if(ct){h.setAttribute('data-color-theme',ct);}else{h.removeAttribute('data-color-theme');}}
186
203
  theme(cfg.cls,cfg.ct);
187
204
  var waits=Object.create(null),seq=0,up=false,queue=[],qn=0,noHost=!nonce;
205
+ var toStr=Object.prototype.toString,BlobC=typeof Blob==='function'?Blob:null,
206
+ mkUrl=typeof URL==='function'&&URL.createObjectURL?URL.createObjectURL.bind(URL):null,
207
+ rmUrl=typeof URL==='function'&&URL.revokeObjectURL?URL.revokeObjectURL.bind(URL):null;
208
+ var mcache=Object.create(null),murls=Object.create(null),stray=[];
209
+ function revokeAll(){var all=stray;stray=[];for(var f in murls){all.push(murls[f]);}
210
+ murls=Object.create(null);mcache=Object.create(null);
211
+ for(var i=0;i<all.length;i++){try{if(rmUrl)rmUrl(all[i]);}catch(_){}}}
188
212
  window.addEventListener('pagehide',function(e){
189
- if(nonce&&!kept(e)){try{P.postMessage({v:1,kind:'leaving',n:nonce},'*');}catch(_){}}},{capture:true});
213
+ if(kept(e))return;revokeAll();
214
+ if(nonce){try{P.postMessage({v:1,kind:'leaving',n:nonce},'*');}catch(_){}}},{capture:true});
190
215
  window.addEventListener('message',function(e){
191
216
  if(e.source!==P)return;var m=e.data;if(!m||m.v!==1)return;
192
217
  if(m.kind==='theme'&&cfg.follow){theme(m.cls,m.colorTheme);}
193
- else if(m.kind==='data-reply'){var w=waits[m.id];if(!w)return;delete waits[m.id];clearTimeout(w.t);
218
+ else if(m.kind==='data-reply'){var w=waits[m.id];if(!w||w.k!=='d')return;delete waits[m.id];clearTimeout(w.t);
194
219
  if(m.ok===true){w.ok(m.result);}else{w.no(new Error(typeof m.error==='string'?m.error.slice(0,300):'the canvas data store refused'));}}
220
+ 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
+ if(m.ok===true&&toStr.call(m.bytes)==='[object ArrayBuffer]'&&typeof m.mime==='string'){x.ok(m);}
222
+ else{x.no(new Error(typeof m.error==='string'?m.error.slice(0,300):'the file could not be loaded'));}}
195
223
  });
196
- function send(r){try{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},'*');}
197
- catch(_){var w=waits[r.id];if(w){delete waits[r.id];clearTimeout(w.t);w.no(new Error('the canvas data store is not available'));}}}
224
+ function send(r){try{if(r.f){P.postMessage({v:1,kind:'media',n:nonce,id:r.id,file:r.f},'*');}
225
+ 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
+ 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'));}}}
198
227
  function call(op,a){return new Promise(function(ok,no){
199
228
  if(noHost){
200
229
  if(op==='get'){ok(null);}else if(op==='list'){ok([]);}
@@ -202,7 +231,7 @@ function preludeScript(opts: {
202
231
  else{no(new Error('nothing is saved here: this is a picture of the canvas'));}
203
232
  return;}
204
233
  var id=++seq;var t=setTimeout(function(){if(waits[id]){delete waits[id];no(new Error('the canvas data store did not answer'));}},${CANVAS_DATA_CALL_TIMEOUT_MS + 5_000});
205
- waits[id]={ok:ok,no:no,t:t};
234
+ waits[id]={k:'d',ok:ok,no:no,t:t};
206
235
  var r={id:id,op:op,bucket:a.bucket,key:a.key,json:a.json,everyone:a.everyone,by:a.by};
207
236
  if(up){send(r);}else{queue[qn++]=r;}
208
237
  });}
@@ -217,7 +246,26 @@ function preludeScript(opts: {
217
246
  info:function(){return call('info',{});},
218
247
  clear:function(){return call('clear',{}).then(function(r){return r&&typeof r.removed==='number'?r.removed:0;});}
219
248
  };
220
- if(!cfg.named&&!('mantle' in window)){try{window.mantle=Object.freeze({data:Object.freeze(data)});}catch(_){}}
249
+ function murl(f){
250
+ if(typeof f!=='string'||!/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(f)){
251
+ return Promise.reject(new TypeError('mantle.media takes a file id (a uuid) listed in canvas:assets'));}
252
+ f=f.toLowerCase();
253
+ if(noHost||!cfg.m||!BlobC||!mkUrl){return Promise.reject(new Error('media is not available here'));}
254
+ if(mcache[f])return mcache[f];
255
+ var p=new Promise(function(ok,no){
256
+ var id=++seq;var t=setTimeout(function(){if(waits[id]){delete waits[id];no(new Error('the file did not arrive'));}},${CANVAS_MEDIA_CALL_TIMEOUT_MS + 5_000});
257
+ waits[id]={k:'m',t:t,no:no,ok:function(m){
258
+ try{var u=mkUrl(new BlobC([m.bytes],{type:m.mime}));if(mcache[f]===p){murls[f]=u;}else{stray.push(u);}ok(u);}catch(err){no(err);}}};
259
+ var r={id:id,f:f};if(up){send(r);}else{queue[qn++]=r;}
260
+ });
261
+ mcache[f]=p;
262
+ p.then(null,function(){if(mcache[f]===p){delete mcache[f];}});
263
+ return p;
264
+ }
265
+ function mrel(f){if(typeof f!=='string')return;f=f.toLowerCase();var u=murls[f];
266
+ delete murls[f];delete mcache[f];if(u){try{if(rmUrl)rmUrl(u);}catch(_){}}}
267
+ var media={url:murl,image:murl,release:mrel};
268
+ if(!cfg.named&&!('mantle' in window)){try{window.mantle=Object.freeze({data:Object.freeze(data),media:Object.freeze(media)});}catch(_){}}
221
269
  if(cfg.size){
222
270
  var fit=function(){var W=cfg.size.w,H=cfg.size.h,iw=window.innerWidth||W,ih=window.innerHeight||H;
223
271
  var s=Math.min(iw/W,ih/H);if(!(s>0))s=1;
@@ -310,6 +358,9 @@ export function buildCanvasFrameHtml(opts: {
310
358
  /** The frame's proof for its parent (canvasFrameNonce); '' for a render
311
359
  * with no parent. */
312
360
  nonce?: string;
361
+ /** The host answers `mantle.media` (its frame URL said `m=1`). Never in a
362
+ * render: with no nonce, media refuses whatever this says. */
363
+ media?: boolean;
313
364
  }): string {
314
365
  const follow = opts.theme !== 'fixed';
315
366
  // A fixed canvas wears the light baseline: no dark class, no colour theme.
@@ -325,7 +376,7 @@ export function buildCanvasFrameHtml(opts: {
325
376
  `<style>${styleText(opts.fontCss)}</style>`,
326
377
  `<style>${styleText(opts.css)}</style>`,
327
378
  `<style>${layoutCss(opts.size)}</style>`,
328
- `<script>${preludeScript({ cls, colorTheme, followTheme: follow, size: opts.size, nonce: opts.nonce ?? '', named: namesMantle(source) })}</script>`,
379
+ `<script>${preludeScript({ cls, colorTheme, followTheme: follow, size: opts.size, nonce: opts.nonce ?? '', named: namesMantle(source), media: opts.media === true && !!opts.nonce })}</script>`,
329
380
  ].join('\n');
330
381
  return `<!doctype html>\n${prelude}\n${source}`;
331
382
  }
@@ -0,0 +1,94 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { CANVAS_MEDIA_MAX_BYTES, CANVAS_MEDIA_NO_ACCESS } from '@mantle/content-core/canvas-media';
3
+ import { fetchCanvasMedia, ownerCanvasMediaSource, readTicketAssets } from './canvas-media-bridge';
4
+
5
+ const A = '11111111-1111-4111-8111-111111111111';
6
+
7
+ describe('ownerCanvasMediaSource', () => {
8
+ it("is the brain's file route beside the canvas API, relative or absolute", () => {
9
+ expect(ownerCanvasMediaSource(`/api/presents/${A}`)!(A)).toBe(`/api/files/files/${A}?raw=1`);
10
+ expect(ownerCanvasMediaSource(`https://brain.example/api/presents/${A}`)!(A)).toBe(
11
+ `https://brain.example/api/files/files/${A}?raw=1`,
12
+ );
13
+ });
14
+
15
+ it('a base of another shape (an open link) has none', () => {
16
+ expect(ownerCanvasMediaSource('/s/tok/canvas')).toBeNull();
17
+ });
18
+ });
19
+
20
+ describe('readTicketAssets', () => {
21
+ it('keeps uuids only, lower case', () => {
22
+ expect([...readTicketAssets([A.toUpperCase(), 'x', 7, null])]).toEqual([A]);
23
+ expect(readTicketAssets('nope').size).toBe(0);
24
+ });
25
+ });
26
+
27
+ describe('fetchCanvasMedia', () => {
28
+ const answer = (res: Response) => async () => res;
29
+
30
+ it('hands over the bytes and their type', async () => {
31
+ const r = await fetchCanvasMedia(
32
+ answer(
33
+ new Response(new Uint8Array([1, 2, 3]), { headers: { 'content-type': 'audio/mpeg' } }),
34
+ ),
35
+ 'u',
36
+ );
37
+ expect(r.ok).toBe(true);
38
+ if (r.ok) {
39
+ expect(r.mime).toBe('audio/mpeg');
40
+ expect([...new Uint8Array(r.bytes)]).toEqual([1, 2, 3]);
41
+ }
42
+ });
43
+
44
+ it('any refusal of the route is the one "no access"', async () => {
45
+ for (const status of [401, 403, 404, 500]) {
46
+ const r = await fetchCanvasMedia(answer(new Response('x', { status })), 'u');
47
+ expect(r).toEqual({ ok: false, error: CANVAS_MEDIA_NO_ACCESS });
48
+ }
49
+ });
50
+
51
+ it('refuses a type outside the list (SVG, HTML, anything sniffable)', async () => {
52
+ for (const type of ['image/svg+xml', 'text/html', 'application/octet-stream']) {
53
+ const r = await fetchCanvasMedia(
54
+ answer(new Response('<svg/>', { headers: { 'content-type': type } })),
55
+ 'u',
56
+ );
57
+ expect(r.ok).toBe(false);
58
+ }
59
+ });
60
+
61
+ it('refuses a file over the cap, by its length or as it streams', async () => {
62
+ const big = await fetchCanvasMedia(
63
+ answer(
64
+ new Response('x', {
65
+ headers: {
66
+ 'content-type': 'audio/mpeg',
67
+ 'content-length': String(CANVAS_MEDIA_MAX_BYTES + 1),
68
+ },
69
+ }),
70
+ ),
71
+ 'u',
72
+ );
73
+ expect(big.ok).toBe(false);
74
+ const stream = new ReadableStream<Uint8Array>({
75
+ start(c) {
76
+ c.enqueue(new Uint8Array(CANVAS_MEDIA_MAX_BYTES));
77
+ c.enqueue(new Uint8Array(1));
78
+ c.close();
79
+ },
80
+ });
81
+ const streamed = await fetchCanvasMedia(
82
+ answer(new Response(stream, { headers: { 'content-type': 'audio/mpeg' } })),
83
+ 'u',
84
+ );
85
+ expect(streamed.ok).toBe(false);
86
+ });
87
+
88
+ it('a failed fetch is said, never thrown', async () => {
89
+ const r = await fetchCanvasMedia(async () => {
90
+ throw new Error('down');
91
+ }, 'u');
92
+ expect(r).toEqual({ ok: false, error: 'the file could not be reached' });
93
+ });
94
+ });
@@ -0,0 +1,126 @@
1
+ /**
2
+ * The host half of a canvas's `mantle.media` (task a265178d): what
3
+ * CanvasSandbox does with a `{v:1, kind:'media', n, id, file}` message from
4
+ * its frame, and where it gets the bytes. Pure but for the fetch it is
5
+ * handed, so every check is tested without a browser
6
+ * (canvas-media-bridge.test.ts).
7
+ *
8
+ * The frame is hostile. Its message is taken only from the proven document
9
+ * with its nonce (acceptsDataMessage, as a data call), and only for a file
10
+ * id the canvas DECLARED (the `assets` its frame ticket returned: the saved
11
+ * `meta.assets`). The bytes come through the host's own credential from a
12
+ * route that checks the reader again: the owner side's file route (the
13
+ * viewer's own read right), the open link's asset route (a declared file of
14
+ * the canvas's own workspace, decision 7). Any miss is one answer, CANVAS_MEDIA_NO_ACCESS. Only
15
+ * an allowed type (CANVAS_MEDIA_MIMES) of at most CANVAS_MEDIA_MAX_BYTES is
16
+ * handed over, as an ArrayBuffer the frame turns into a blob: URL.
17
+ */
18
+ import {
19
+ CANVAS_MEDIA_CALL_TIMEOUT_MS,
20
+ CANVAS_MEDIA_MAX_BYTES,
21
+ CANVAS_MEDIA_NO_ACCESS,
22
+ canvasMediaMime,
23
+ isCanvasAssetId,
24
+ } from '@mantle/content-core/canvas-media';
25
+
26
+ /** Where the bytes of a declared file come from, by file id. */
27
+ export type CanvasMediaSource = (fileId: string) => string;
28
+
29
+ /**
30
+ * The owner side's media source for a canvas API base: the brain's own file
31
+ * route (`/api/files/files/<id>?raw=1`, the viewer's read right), next to
32
+ * `/api/presents/<id>` (relative on a one-origin box, absolute on a split
33
+ * client). Null for a base of another shape: no media.
34
+ */
35
+ export function ownerCanvasMediaSource(apiBase: string): CanvasMediaSource | null {
36
+ const m = /^(.*)\/api\/presents\/[^/?#]+$/.exec(apiBase);
37
+ if (!m) return null;
38
+ const root = m[1]!;
39
+ return (fileId) => `${root}/api/files/files/${encodeURIComponent(fileId)}?raw=1`;
40
+ }
41
+
42
+ /** The declared ids a frame-ticket answer carries (`assets`), lower case;
43
+ * anything that is not a list of uuids reads as none. */
44
+ export function readTicketAssets(raw: unknown): Set<string> {
45
+ const out = new Set<string>();
46
+ if (!Array.isArray(raw)) return out;
47
+ for (const v of raw) if (isCanvasAssetId(v)) out.add(v.toLowerCase());
48
+ return out;
49
+ }
50
+
51
+ export type CanvasMediaFetched =
52
+ { ok: true; mime: string; bytes: ArrayBuffer } | { ok: false; error: string };
53
+
54
+ const TOO_LARGE = `this file is over ${CANVAS_MEDIA_MAX_BYTES / (1024 * 1024)} MB`;
55
+
56
+ /** The body, read to at most `max` bytes; null when it is longer (the read
57
+ * is stopped there, never buffered whole). */
58
+ async function readCapped(res: Response, max: number): Promise<ArrayBuffer | null> {
59
+ if (!res.body) {
60
+ const all = await res.arrayBuffer();
61
+ return all.byteLength > max ? null : all;
62
+ }
63
+ const reader = res.body.getReader();
64
+ const parts: Uint8Array[] = [];
65
+ let size = 0;
66
+ for (;;) {
67
+ const { done, value } = await reader.read();
68
+ if (done) break;
69
+ size += value.byteLength;
70
+ if (size > max) {
71
+ await reader.cancel().catch(() => undefined);
72
+ return null;
73
+ }
74
+ parts.push(value);
75
+ }
76
+ const out = new Uint8Array(size);
77
+ let at = 0;
78
+ for (const p of parts) {
79
+ out.set(p, at);
80
+ at += p.byteLength;
81
+ }
82
+ return out.buffer;
83
+ }
84
+
85
+ /**
86
+ * Fetch one declared file for the frame: the bytes and their type, or the
87
+ * words the frame hears. A route's refusal (404, 403, 401) is "no access",
88
+ * whatever it was; the size and the type are looked at only after the route
89
+ * said yes, so they tell nothing about a file the reader may not have.
90
+ */
91
+ export async function fetchCanvasMedia(
92
+ fetcher: (input: string, init?: RequestInit) => Promise<Response>,
93
+ url: string,
94
+ ): Promise<CanvasMediaFetched> {
95
+ let res: Response;
96
+ try {
97
+ res = await fetcher(url, { signal: AbortSignal.timeout(CANVAS_MEDIA_CALL_TIMEOUT_MS) });
98
+ } catch {
99
+ return { ok: false, error: 'the file could not be reached' };
100
+ }
101
+ if (!res.ok) {
102
+ await res.body?.cancel().catch(() => undefined);
103
+ if (res.status === 429) return { ok: false, error: 'too many files asked for: wait a moment' };
104
+ return { ok: false, error: CANVAS_MEDIA_NO_ACCESS };
105
+ }
106
+ const mime = canvasMediaMime(res.headers.get('content-type'));
107
+ if (!mime) {
108
+ await res.body?.cancel().catch(() => undefined);
109
+ return {
110
+ ok: false,
111
+ error: 'a canvas plays mp3 or ogg sound and shows png, jpeg or webp pictures only',
112
+ };
113
+ }
114
+ const length = Number(res.headers.get('content-length'));
115
+ if (Number.isFinite(length) && length > CANVAS_MEDIA_MAX_BYTES) {
116
+ await res.body?.cancel().catch(() => undefined);
117
+ return { ok: false, error: TOO_LARGE };
118
+ }
119
+ try {
120
+ const bytes = await readCapped(res, CANVAS_MEDIA_MAX_BYTES);
121
+ if (!bytes) return { ok: false, error: TOO_LARGE };
122
+ return { ok: true, mime, bytes };
123
+ } catch {
124
+ return { ok: false, error: 'the file could not be read' };
125
+ }
126
+ }
@@ -34,6 +34,18 @@
34
34
  * and the Navigation API does not fire for an opaque frame's navigations);
35
35
  * the desktop app refuses it before it leaves (will-frame-navigate).
36
36
  *
37
+ * Media (task a265178d): a canvas's `mantle.media` calls arrive as `media`
38
+ * messages, taken on the same terms as data (own frame, proven, nonce),
39
+ * and only for a file the canvas declared (the `assets` its ticket
40
+ * returned). The bytes come from `media` (the owner side: the brain's file
41
+ * route, the viewer's own read right; an open link: its asset route) with
42
+ * the host's fetcher, and go back as a transferred ArrayBuffer
43
+ * (./canvas-media-bridge.ts). At most CANVAS_MEDIA_IN_FLIGHT_MAX at once,
44
+ * and CANVAS_MEDIA_CALLS_PER_LOAD calls or CANVAS_MEDIA_BYTES_PER_LOAD bytes
45
+ * per load.
46
+ * The frame URL carries `m=1` when this host answers media, so a frame of an
47
+ * older host refuses at once instead of waiting.
48
+ *
37
49
  * Replay: bump `reloadKey`; a new ticket is minted and the frame reloads, so
38
50
  * an animation starts again from its first frame. Full screen: the host
39
51
  * calls `requestFullscreen()` on the element `frameRef` hands back.
@@ -47,6 +59,19 @@ import {
47
59
  readCanvasDataMessage,
48
60
  } from './canvas-data-bridge';
49
61
  import { CANVAS_DATA_CALL_TIMEOUT_MS } from '@mantle/content-core/canvas-data';
62
+ import {
63
+ CANVAS_MEDIA_BYTES_PER_LOAD,
64
+ CANVAS_MEDIA_CALLS_PER_LOAD,
65
+ CANVAS_MEDIA_IN_FLIGHT_MAX,
66
+ CANVAS_MEDIA_NO_ACCESS,
67
+ readCanvasMediaMessage,
68
+ } from '@mantle/content-core/canvas-media';
69
+ import {
70
+ fetchCanvasMedia,
71
+ ownerCanvasMediaSource,
72
+ readTicketAssets,
73
+ type CanvasMediaSource,
74
+ } from './canvas-media-bridge';
50
75
 
51
76
  type Status = 'loading' | 'ready' | 'error' | 'missing' | 'left';
52
77
 
@@ -74,6 +99,7 @@ export function CanvasSandbox({
74
99
  onDown,
75
100
  onError,
76
101
  dataApi = true,
102
+ media,
77
103
  }: {
78
104
  presentId: string;
79
105
  /** The canvas's API base. Absent: the page-relative `/api/presents/<id>`
@@ -102,8 +128,13 @@ export function CanvasSandbox({
102
128
  * False (an open link): each call is answered here, refused, and no
103
129
  * request is sent. */
104
130
  dataApi?: boolean;
131
+ /** Where a declared file's bytes come from, by file id. Absent: the
132
+ * brain's file route beside `apiBase` (ownerCanvasMediaSource); null: no
133
+ * media (each call is refused, `m=1` is not sent). */
134
+ media?: CanvasMediaSource | null;
105
135
  }) {
106
136
  const apiBase = apiBaseOverride ?? `/api/presents/${presentId}`;
137
+ const mediaSource = media === undefined ? ownerCanvasMediaSource(apiBase) : media;
107
138
  const iframeRef = useRef<HTMLIFrameElement | null>(null);
108
139
  const [src, setSrc] = useState<string | null>(null);
109
140
  const [status, setStatus] = useState<Status>('loading');
@@ -115,17 +146,28 @@ export function CanvasSandbox({
115
146
  const proven = useRef(false);
116
147
  const proofWait = useRef<ReturnType<typeof setTimeout> | null>(null);
117
148
  // The latest callbacks, read by the effects without re-running them.
118
- const handlers = useRef({ onReady, onDown, onError, fetcher, dataApi });
119
- handlers.current = { onReady, onDown, onError, fetcher, dataApi };
149
+ const handlers = useRef({ onReady, onDown, onError, fetcher, dataApi, mediaSource });
150
+ handlers.current = { onReady, onDown, onError, fetcher, dataApi, mediaSource };
120
151
  // The frame load each data call belongs to: a reply to an earlier load
121
152
  // (a reload, Replay, a take-down) is dropped, never sent to the new one,
122
153
  // and the in-flight count starts again with each load.
123
154
  const frameLoad = useRef(0);
124
155
  const dataInFlight = useRef(0);
156
+ const mediaInFlight = useRef(0);
157
+ // What this load asked for in all (CANVAS_MEDIA_CALLS_PER_LOAD and
158
+ // CANVAS_MEDIA_BYTES_PER_LOAD).
159
+ const mediaUsed = useRef({ calls: 0, bytes: 0 });
160
+ // The files this load's canvas declared (its ticket's `assets`): the only
161
+ // ids a media call may name.
162
+ const assets = useRef<Set<string>>(new Set());
125
163
  const nextLoad = () => {
126
164
  frameLoad.current += 1;
127
165
  dataInFlight.current = 0;
166
+ mediaInFlight.current = 0;
167
+ mediaUsed.current = { calls: 0, bytes: 0 };
168
+ assets.current = new Set();
128
169
  };
170
+ const withMedia = mediaSource !== null;
129
171
  const apiBaseRef = useRef(apiBase);
130
172
  apiBaseRef.current = apiBase;
131
173
 
@@ -150,13 +192,19 @@ export function CanvasSandbox({
150
192
  return;
151
193
  }
152
194
  if (!res.ok) throw new Error(`frame ticket: ${res.status}`);
153
- const { ticket, nonce: n } = (await res.json()) as { ticket: string; nonce?: string };
195
+ const {
196
+ ticket,
197
+ nonce: n,
198
+ assets: declared,
199
+ } = (await res.json()) as { ticket: string; nonce?: string; assets?: unknown };
154
200
  if (cancelled) return;
155
201
  nonce.current = typeof n === 'string' && n ? n : null;
156
202
  proven.current = false;
203
+ assets.current = withMedia ? readTicketAssets(declared) : new Set();
157
204
  const { cls, colorTheme } = hostTheme();
158
205
  const qs = new URLSearchParams({ t: ticket, cls });
159
206
  if (colorTheme) qs.set('ct', colorTheme);
207
+ if (withMedia) qs.set('m', '1');
160
208
  setSrc(`${apiBase}/frame?${qs.toString()}`);
161
209
  } catch {
162
210
  if (!cancelled) setStatus('error');
@@ -165,7 +213,7 @@ export function CanvasSandbox({
165
213
  return () => {
166
214
  cancelled = true;
167
215
  };
168
- }, [apiBase, reloadKey]);
216
+ }, [apiBase, reloadKey, withMedia]);
169
217
 
170
218
  // Take the frame down: whatever it shows now is not the canvas.
171
219
  const takeDown = (why: string) => {
@@ -175,6 +223,9 @@ export function CanvasSandbox({
175
223
  nonce.current = null;
176
224
  frameLoad.current += 1;
177
225
  dataInFlight.current = 0;
226
+ mediaInFlight.current = 0;
227
+ mediaUsed.current = { calls: 0, bytes: 0 };
228
+ assets.current = new Set();
178
229
  setSrc(null);
179
230
  setStatus('left');
180
231
  handlers.current.onDown?.();
@@ -221,6 +272,65 @@ export function CanvasSandbox({
221
272
  // window may hold a page the canvas navigated to.
222
273
  if (!acceptsDataMessage(m, { proven: proven.current, nonce: nonce.current })) return;
223
274
  void forwardData(frame, m);
275
+ } else if (m.kind === 'media') {
276
+ // The same proof as a data call, then the canvas's own declaration.
277
+ if (!acceptsDataMessage(m, { proven: proven.current, nonce: nonce.current })) return;
278
+ void forwardMedia(frame, m);
279
+ }
280
+ }
281
+
282
+ /** One `mantle.media` call: a declared file, fetched with the host's
283
+ * credential from a route that checks the reader, handed over as an
284
+ * ArrayBuffer. The load is checked again before every reply. */
285
+ async function forwardMedia(frame: HTMLIFrameElement, raw: unknown) {
286
+ const load = frameLoad.current;
287
+ const answer = (
288
+ id: number | null,
289
+ reply: Record<string, unknown>,
290
+ transfer: Transferable[] = [],
291
+ ) => {
292
+ if (id === null || load !== frameLoad.current || !proven.current) return;
293
+ frame.contentWindow?.postMessage(
294
+ { v: 1, kind: 'media-reply', id, ...reply },
295
+ '*',
296
+ transfer,
297
+ );
298
+ };
299
+ const read = readCanvasMediaMessage(raw);
300
+ if (!read.ok) {
301
+ answer(read.id, { ok: false, error: 'mantle.media takes a file id (a uuid)' });
302
+ return;
303
+ }
304
+ const { req } = read;
305
+ const source = handlers.current.mediaSource;
306
+ // Not declared, or no media here: the one answer for any file the
307
+ // canvas was not given.
308
+ if (!source || !assets.current.has(req.file)) {
309
+ answer(req.id, { ok: false, error: CANVAS_MEDIA_NO_ACCESS });
310
+ return;
311
+ }
312
+ if (mediaInFlight.current >= CANVAS_MEDIA_IN_FLIGHT_MAX) {
313
+ answer(req.id, { ok: false, error: 'too many files at once: wait for the ones asked for' });
314
+ return;
315
+ }
316
+ const used = mediaUsed.current;
317
+ if (used.calls >= CANVAS_MEDIA_CALLS_PER_LOAD || used.bytes >= CANVAS_MEDIA_BYTES_PER_LOAD) {
318
+ answer(req.id, {
319
+ ok: false,
320
+ error: 'this canvas asked for too much media: Replay to start again',
321
+ });
322
+ return;
323
+ }
324
+ used.calls += 1;
325
+ mediaInFlight.current += 1;
326
+ try {
327
+ const got = await fetchCanvasMedia(handlers.current.fetcher, source(req.file));
328
+ // Counted in this load's budget only (a reply of an earlier load is dropped).
329
+ if (got.ok && load === frameLoad.current) used.bytes += got.bytes.byteLength;
330
+ if (got.ok) answer(req.id, { ok: true, mime: got.mime, bytes: got.bytes }, [got.bytes]);
331
+ else answer(req.id, { ok: false, error: got.error });
332
+ } finally {
333
+ if (load === frameLoad.current) mediaInFlight.current -= 1;
224
334
  }
225
335
  }
226
336
 
@@ -8,7 +8,10 @@
8
8
  * on screen in full screen). The frame
9
9
  * ticket and the frame document come from /s/<token>/canvas, which re-checks
10
10
  * the link on every load. The canvas's `mantle.data` calls are refused here:
11
- * a link reads or writes no saved data.
11
+ * a link reads or writes no saved data. Its `mantle.media` calls fetch from
12
+ * the link's asset route (/s/<token>/a/<fileId>), which serves a declared
13
+ * file of the canvas's own workspace (decision 7, checked there per
14
+ * request); any other call hears "no access".
12
15
  */
13
16
  import { useRef, useState } from 'react';
14
17
  import { Maximize, RotateCcw } from 'lucide-react';
@@ -22,6 +25,12 @@ export function shareCanvasApiBase(token: string): string {
22
25
  return `/s/${encodeURIComponent(token)}/canvas`;
23
26
  }
24
27
 
28
+ /** Where a canvas on an open link gets a declared file's bytes: the link's
29
+ * own asset route. */
30
+ export function shareCanvasMediaSource(token: string): (fileId: string) => string {
31
+ return (fileId) => `/s/${encodeURIComponent(token)}/a/${encodeURIComponent(fileId)}`;
32
+ }
33
+
25
34
  export function PresentPresenter({
26
35
  view,
27
36
  token,
@@ -43,6 +52,7 @@ export function PresentPresenter({
43
52
  title={view.title || 'Shared canvas'}
44
53
  reloadKey={reloadKey}
45
54
  dataApi={false}
55
+ media={shareCanvasMediaSource(token)}
46
56
  />
47
57
  <div className="absolute bottom-3 right-3 z-10 flex items-center gap-1 rounded-full border border-border bg-background/85 py-1 pl-3 pr-1 text-xs text-muted-foreground shadow-sm backdrop-blur">
48
58
  <span className="max-w-[40vw] truncate">Shared canvas</span>