@crossworks/content-core 0.500.24 → 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 +3 -2
- package/src/canvas-media.test.ts +116 -0
- package/src/canvas-media.ts +279 -0
- package/src/canvas.test.ts +1 -1
- package/src/canvas.ts +18 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crossworks/content-core",
|
|
3
|
-
"version": "0.500.
|
|
3
|
+
"version": "0.500.30",
|
|
4
4
|
"description": "Browser-safe content logic — markdown, blocks, pages, tables, formulas — shared by the server and any client. Zero server deps by design: nothing here may reach @mantle/db, node-only APIs, or the network (the jackdaw-repo-split P0 boundary).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
"./block-list": "./src/block-list.ts",
|
|
11
11
|
"./canvas": "./src/canvas.ts",
|
|
12
12
|
"./canvas-data": "./src/canvas-data.ts",
|
|
13
|
+
"./canvas-media": "./src/canvas-media.ts",
|
|
13
14
|
"./contacts-format": "./src/contacts-format.ts",
|
|
14
15
|
"./doc-to-markdown": "./src/doc-to-markdown.ts",
|
|
15
16
|
"./drift-spec": "./src/drift-spec.ts",
|
|
@@ -37,7 +38,7 @@
|
|
|
37
38
|
"./tree": "./src/tree.ts"
|
|
38
39
|
},
|
|
39
40
|
"dependencies": {
|
|
40
|
-
"@mantle/client-types": "npm:@crossworks/client-types@0.500.
|
|
41
|
+
"@mantle/client-types": "npm:@crossworks/client-types@0.500.30",
|
|
41
42
|
"marked": "^18.1.2",
|
|
42
43
|
"mathjs": "15.2.0"
|
|
43
44
|
},
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { canvasAssets, canvasLoadProblems } from './canvas';
|
|
3
|
+
import {
|
|
4
|
+
CANVAS_ASSETS_MAX,
|
|
5
|
+
canvasAssetProblems,
|
|
6
|
+
canvasMediaMime,
|
|
7
|
+
readCanvasMediaMessage,
|
|
8
|
+
} from './canvas-media';
|
|
9
|
+
|
|
10
|
+
const A = '11111111-1111-4111-8111-111111111111';
|
|
11
|
+
const B = '22222222-2222-4222-8222-222222222222';
|
|
12
|
+
|
|
13
|
+
const doc = (assets: string, body = '') =>
|
|
14
|
+
`<!doctype html><html><head><title>t</title><meta name="canvas:assets" content="${assets}"></head><body>${body}</body></html>`;
|
|
15
|
+
|
|
16
|
+
describe('canvasAssets', () => {
|
|
17
|
+
it('reads the head tag: lower case, de-duplicated, in order', () => {
|
|
18
|
+
expect(canvasAssets(doc(`${B} ${A.toUpperCase()}\n${B}`)).ids).toEqual([B, A]);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('a canvas with no tag declares nothing', () => {
|
|
22
|
+
expect(canvasAssets('<html><head><title>t</title></head></html>')).toEqual({
|
|
23
|
+
ids: [],
|
|
24
|
+
bad: [],
|
|
25
|
+
over: false,
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('keeps what is not a file id apart, for the save to refuse', () => {
|
|
30
|
+
const d = canvasAssets(doc(`${A} nope ../x`));
|
|
31
|
+
expect(d.ids).toEqual([A]);
|
|
32
|
+
expect(d.bad).toEqual(['nope', '../x']);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('caps the list, and says it went over', () => {
|
|
36
|
+
const many = Array.from(
|
|
37
|
+
{ length: CANVAS_ASSETS_MAX + 1 },
|
|
38
|
+
(_, i) => `00000000-0000-4000-8000-${String(i).padStart(12, '0')}`,
|
|
39
|
+
);
|
|
40
|
+
const d = canvasAssets(doc(many.join(' ')));
|
|
41
|
+
expect(d.ids).toHaveLength(CANVAS_ASSETS_MAX);
|
|
42
|
+
expect(d.over).toBe(true);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("reads a template canvas's data fileId fields (the D5 pictures)", () => {
|
|
46
|
+
const html =
|
|
47
|
+
'<html><head><meta name="diagram:template" content="flow"></head><body>' +
|
|
48
|
+
`<script type="application/json" id="diagram-data">{"template":"flow","nodes":[{"picture":{"fileId":"${A}"}},{"fileId":"x"}]}</script>` +
|
|
49
|
+
'</body></html>';
|
|
50
|
+
const d = canvasAssets(html);
|
|
51
|
+
expect(d.ids).toEqual([A]);
|
|
52
|
+
expect(d.bad).toEqual(['x']);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('canvasAssetProblems', () => {
|
|
57
|
+
it('a bad token or too many is a problem; an undeclared literal id a warning', () => {
|
|
58
|
+
const html = doc(
|
|
59
|
+
`${A} nope`,
|
|
60
|
+
`<script>mantle.media.url('${B}');mantle.media.url("${A}")</script>`,
|
|
61
|
+
);
|
|
62
|
+
const r = canvasAssetProblems(html, canvasAssets(html));
|
|
63
|
+
expect(r.problems).toHaveLength(1);
|
|
64
|
+
expect(r.problems[0]).toContain("'nope'");
|
|
65
|
+
expect(r.warnings).toHaveLength(1);
|
|
66
|
+
expect(r.warnings[0]).toContain(B);
|
|
67
|
+
expect(r.warnings[0]).not.toContain(A);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('a clean declaration has neither', () => {
|
|
71
|
+
const html = doc(A, `<script>mantle.media.url('${A}')</script>`);
|
|
72
|
+
expect(canvasAssetProblems(html, canvasAssets(html))).toEqual({ problems: [], warnings: [] });
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
describe('canvasLoadProblems and file URLs', () => {
|
|
77
|
+
it('a file URL is refused with the way to play it instead', () => {
|
|
78
|
+
for (const url of [`/api/files/files/${A}?raw=1`, `/s/tok/a/${A}`]) {
|
|
79
|
+
const p = canvasLoadProblems(`<html><body><audio src="${url}"></audio></body></html>`, []);
|
|
80
|
+
expect(p).toHaveLength(1);
|
|
81
|
+
expect(p[0]).toContain("mantle.media.url('<file id>')");
|
|
82
|
+
expect(p[0]).toContain('canvas:assets');
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
describe('canvasMediaMime', () => {
|
|
88
|
+
it('takes mp3, ogg, png, jpeg and webp, parameters dropped; nothing else', () => {
|
|
89
|
+
expect(canvasMediaMime('audio/mpeg')).toBe('audio/mpeg');
|
|
90
|
+
expect(canvasMediaMime('Audio/Ogg; codecs=opus')).toBe('audio/ogg');
|
|
91
|
+
expect(canvasMediaMime('image/webp')).toBe('image/webp');
|
|
92
|
+
for (const t of ['image/svg+xml', 'text/html', 'application/octet-stream', '', null]) {
|
|
93
|
+
expect(canvasMediaMime(t)).toBeNull();
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
describe('readCanvasMediaMessage', () => {
|
|
99
|
+
it('takes a call id and a file id, lower case', () => {
|
|
100
|
+
expect(readCanvasMediaMessage({ id: 3, file: A.toUpperCase() })).toEqual({
|
|
101
|
+
ok: true,
|
|
102
|
+
req: { id: 3, file: A },
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('refuses a bad file with its call id, and drops what has no call id', () => {
|
|
107
|
+
expect(readCanvasMediaMessage({ id: 4, file: '../etc' })).toEqual({ ok: false, id: 4 });
|
|
108
|
+
expect(readCanvasMediaMessage({ id: 4, file: { toString: () => A } })).toEqual({
|
|
109
|
+
ok: false,
|
|
110
|
+
id: 4,
|
|
111
|
+
});
|
|
112
|
+
for (const raw of [null, [], { file: A }, { id: 0, file: A }, { id: 1.5, file: A }]) {
|
|
113
|
+
expect(readCanvasMediaMessage(raw)).toEqual({ ok: false, id: null });
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
});
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The canvas media contract (task a265178d): which Files a Present canvas
|
|
3
|
+
* may play or show through `mantle.media`, shared by the save step (the
|
|
4
|
+
* content layer derives `meta.assets` here), the frame's helper (share-ui
|
|
5
|
+
* canvas-frame-html), the host that answers its calls (share-ui
|
|
6
|
+
* canvas-sandbox) and the open link's asset check. Pure: no server or
|
|
7
|
+
* browser dependency.
|
|
8
|
+
*
|
|
9
|
+
* A canvas runs sandboxed with no network. It asks its PARENT for a file's
|
|
10
|
+
* bytes by id; the parent fetches them with its own credential and hands
|
|
11
|
+
* them over, and the frame makes a blob: URL of them. One rule decides
|
|
12
|
+
* which ids it may ask for: those its saved source DECLARES, in a head tag
|
|
13
|
+
* (`<meta name="canvas:assets" content="id id ...">`) or, for a diagram
|
|
14
|
+
* template canvas, in its data's `fileId` fields. The save keeps only the
|
|
15
|
+
* declared files the saver reads (the content layer); every play is
|
|
16
|
+
* checked again as the viewer (their own file read) or, on an open link,
|
|
17
|
+
* by the link's asset route (a declared file of the canvas's workspace).
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { UUID_RE } from './uuid';
|
|
21
|
+
|
|
22
|
+
/** The most file ids one canvas declares. */
|
|
23
|
+
export const CANVAS_ASSETS_MAX = 500;
|
|
24
|
+
|
|
25
|
+
/** The largest file the host hands a canvas. */
|
|
26
|
+
export const CANVAS_MEDIA_MAX_BYTES = 10 * 1024 * 1024;
|
|
27
|
+
|
|
28
|
+
/** How many media calls of one frame may wait at once. */
|
|
29
|
+
export const CANVAS_MEDIA_IN_FLIGHT_MAX = 4;
|
|
30
|
+
|
|
31
|
+
/** What one frame load may ask for in all: calls, and bytes handed over. A
|
|
32
|
+
* canvas that keeps asking (a url/release loop) is refused past these, not
|
|
33
|
+
* served without end; Replay starts a new load. A 118-line narrated tour
|
|
34
|
+
* is about 120 calls and 7 MB. */
|
|
35
|
+
export const CANVAS_MEDIA_CALLS_PER_LOAD = 400;
|
|
36
|
+
export const CANVAS_MEDIA_BYTES_PER_LOAD = 300 * 1024 * 1024;
|
|
37
|
+
|
|
38
|
+
/** How long the host waits for one file, and the frame for the host. */
|
|
39
|
+
export const CANVAS_MEDIA_CALL_TIMEOUT_MS = 60_000;
|
|
40
|
+
|
|
41
|
+
/** The types the host hands a canvas: mp3 and ogg sound, and raster
|
|
42
|
+
* pictures. Never SVG or HTML: a canvas gets bytes it can only play or
|
|
43
|
+
* draw, never a document that would run. */
|
|
44
|
+
export const CANVAS_MEDIA_MIMES = [
|
|
45
|
+
'audio/mpeg',
|
|
46
|
+
'audio/ogg',
|
|
47
|
+
'image/png',
|
|
48
|
+
'image/jpeg',
|
|
49
|
+
'image/webp',
|
|
50
|
+
] as const;
|
|
51
|
+
|
|
52
|
+
/** A file type the host hands a canvas, from a response's content-type
|
|
53
|
+
* (parameters dropped), or null. */
|
|
54
|
+
export function canvasMediaMime(contentType: string | null | undefined): string | null {
|
|
55
|
+
const base = (contentType ?? '').split(';')[0]!.trim().toLowerCase();
|
|
56
|
+
return (CANVAS_MEDIA_MIMES as readonly string[]).includes(base) ? base : null;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The words a frame hears for any file it may not have: one answer for
|
|
60
|
+
* "not declared", "not readable" and "gone", so a canvas learns nothing
|
|
61
|
+
* about a file it was not given. */
|
|
62
|
+
export const CANVAS_MEDIA_NO_ACCESS = 'no access to this file';
|
|
63
|
+
|
|
64
|
+
/** A file id as a canvas names one: a uuid, either case. */
|
|
65
|
+
export function isCanvasAssetId(v: unknown): v is string {
|
|
66
|
+
return typeof v === 'string' && UUID_RE.test(v);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The meta tag a canvas declares its files in. */
|
|
70
|
+
export const CANVAS_ASSETS_META = 'canvas:assets';
|
|
71
|
+
|
|
72
|
+
/** The opening tag of a diagram template canvas's data block (diagram-
|
|
73
|
+
* templates canvas.ts DATA_ID, 'diagram-data'), and its head marker. */
|
|
74
|
+
const TEMPLATE_DATA_OPEN = '<script type="application/json" id="diagram-data">';
|
|
75
|
+
const TEMPLATE_MARKER = '<meta name="diagram:template"';
|
|
76
|
+
|
|
77
|
+
/** How far into a template's data the `fileId` fields are looked for. */
|
|
78
|
+
const TEMPLATE_DATA_DEPTH = 8;
|
|
79
|
+
|
|
80
|
+
/** What a canvas declares: the valid ids (lower case, de-duplicated, in
|
|
81
|
+
* order, at most CANVAS_ASSETS_MAX), the tokens that are not uuids, and
|
|
82
|
+
* whether it declared more than the cap. */
|
|
83
|
+
export type CanvasAssetDeclaration = { ids: string[]; bad: string[]; over: boolean };
|
|
84
|
+
|
|
85
|
+
/** The `fileId` values (uuids) of a template canvas's data, at any depth
|
|
86
|
+
* up to TEMPLATE_DATA_DEPTH: the D5 picture ids. */
|
|
87
|
+
function templateFileIds(html: string): string[] {
|
|
88
|
+
if (!html.includes(TEMPLATE_MARKER)) return [];
|
|
89
|
+
const at = html.indexOf(TEMPLATE_DATA_OPEN);
|
|
90
|
+
const end = at < 0 ? -1 : html.indexOf('</script>', at + TEMPLATE_DATA_OPEN.length);
|
|
91
|
+
if (end < 0) return [];
|
|
92
|
+
let data: unknown;
|
|
93
|
+
try {
|
|
94
|
+
data = JSON.parse(html.slice(at + TEMPLATE_DATA_OPEN.length, end));
|
|
95
|
+
} catch {
|
|
96
|
+
return [];
|
|
97
|
+
}
|
|
98
|
+
const out: string[] = [];
|
|
99
|
+
const walk = (v: unknown, depth: number) => {
|
|
100
|
+
if (depth > TEMPLATE_DATA_DEPTH || out.length > CANVAS_ASSETS_MAX || !v) return;
|
|
101
|
+
if (Array.isArray(v)) {
|
|
102
|
+
for (const x of v) walk(x, depth + 1);
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (typeof v !== 'object') return;
|
|
106
|
+
for (const [k, x] of Object.entries(v as Record<string, unknown>)) {
|
|
107
|
+
if (k === 'fileId' && typeof x === 'string') out.push(x);
|
|
108
|
+
else walk(x, depth + 1);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
walk(data, 0);
|
|
112
|
+
return out;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** The file ids a canvas declares: its `canvas:assets` head tag (`tag`,
|
|
116
|
+
* the meta's content as parseCanvasHead reads it, or null) and a template
|
|
117
|
+
* canvas's data `fileId` fields. */
|
|
118
|
+
export function canvasAssetDeclaration(html: string, tag: string | null): CanvasAssetDeclaration {
|
|
119
|
+
const tokens = [
|
|
120
|
+
...(tag ?? '')
|
|
121
|
+
.split(/[\s,]+/)
|
|
122
|
+
.map((t) => t.trim())
|
|
123
|
+
.filter(Boolean),
|
|
124
|
+
...templateFileIds(html),
|
|
125
|
+
];
|
|
126
|
+
const ids: string[] = [];
|
|
127
|
+
const bad: string[] = [];
|
|
128
|
+
const seen = new Set<string>();
|
|
129
|
+
for (const t of tokens) {
|
|
130
|
+
if (!UUID_RE.test(t)) {
|
|
131
|
+
if (bad.length < 20) bad.push(t.slice(0, 60));
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
const id = t.toLowerCase();
|
|
135
|
+
if (!seen.has(id)) {
|
|
136
|
+
seen.add(id);
|
|
137
|
+
ids.push(id);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return { ids: ids.slice(0, CANVAS_ASSETS_MAX), bad, over: ids.length > CANVAS_ASSETS_MAX };
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** Literal ids a canvas passes to `mantle.media.url/image/release('<id>')`
|
|
144
|
+
* (a bounded scan per call site). */
|
|
145
|
+
function literalMediaIds(html: string): string[] {
|
|
146
|
+
const out = new Set<string>();
|
|
147
|
+
for (const m of html.matchAll(
|
|
148
|
+
/mantle\.media\.(?:url|image|release)\(\s*["'`]([0-9a-fA-F-]{36})["'`]/g,
|
|
149
|
+
)) {
|
|
150
|
+
if (UUID_RE.test(m[1]!)) out.add(m[1]!.toLowerCase());
|
|
151
|
+
}
|
|
152
|
+
return [...out];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* What is wrong with a canvas's declaration, found before it is saved:
|
|
157
|
+
* `problems` refuse the save (a token that is not a file id, more than the
|
|
158
|
+
* cap), `warnings` do not (an id the canvas passes to `mantle.media` by
|
|
159
|
+
* name that it never declared: that call will be refused). Each names the
|
|
160
|
+
* fix. The read check (the saver reads every declared file) is the content
|
|
161
|
+
* layer's, not this.
|
|
162
|
+
*/
|
|
163
|
+
export function canvasAssetProblems(
|
|
164
|
+
html: string,
|
|
165
|
+
decl: CanvasAssetDeclaration,
|
|
166
|
+
): { problems: string[]; warnings: string[] } {
|
|
167
|
+
const problems: string[] = [];
|
|
168
|
+
if (decl.bad.length) {
|
|
169
|
+
problems.push(
|
|
170
|
+
`${CANVAS_ASSETS_META} holds ${decl.bad.map((b) => `'${b}'`).join(', ')}: list file ids only (uuids, from file_list), separated by spaces.`,
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
if (decl.over) {
|
|
174
|
+
problems.push(
|
|
175
|
+
`${CANVAS_ASSETS_META} declares more than ${CANVAS_ASSETS_MAX} files. Split the canvas, or declare fewer.`,
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
const declared = new Set(decl.ids);
|
|
179
|
+
const missing = literalMediaIds(html).filter((id) => !declared.has(id));
|
|
180
|
+
const warnings = missing.length
|
|
181
|
+
? [
|
|
182
|
+
`mantle.media is called with ${missing.slice(0, 10).join(', ')}${missing.length > 10 ? ' and more' : ''}, not in ${CANVAS_ASSETS_META}: those calls will be refused. Add the ids to <meta name="${CANVAS_ASSETS_META}" content="id id ...">.`,
|
|
183
|
+
]
|
|
184
|
+
: [];
|
|
185
|
+
return { problems, warnings };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** A media call the host takes: its call id and the file it asks for
|
|
189
|
+
* (lower case). */
|
|
190
|
+
export type CanvasMediaRequest = { id: number; file: string };
|
|
191
|
+
|
|
192
|
+
/** Read a frame's `{v:1, kind:'media', n, id, file}` message: the request,
|
|
193
|
+
* or the call id to refuse (null: drop it, nothing to answer to). The
|
|
194
|
+
* nonce and the frame are checked before (acceptsDataMessage). */
|
|
195
|
+
export function readCanvasMediaMessage(
|
|
196
|
+
raw: unknown,
|
|
197
|
+
): { ok: true; req: CanvasMediaRequest } | { ok: false; id: number | null } {
|
|
198
|
+
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return { ok: false, id: null };
|
|
199
|
+
const m = raw as Record<string, unknown>;
|
|
200
|
+
const id = typeof m.id === 'number' && Number.isSafeInteger(m.id) && m.id > 0 ? m.id : null;
|
|
201
|
+
if (id === null) return { ok: false, id: null };
|
|
202
|
+
if (!isCanvasAssetId(m.file)) return { ok: false, id };
|
|
203
|
+
return { ok: true, req: { id, file: m.file.toLowerCase() } };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** The example clips the guide's canvas declares (placeholders: an agent
|
|
207
|
+
* puts its own file ids in). */
|
|
208
|
+
export const CANVAS_MEDIA_EXAMPLE_IDS = [
|
|
209
|
+
'11111111-1111-4111-8111-111111111111',
|
|
210
|
+
'22222222-2222-4222-8222-222222222222',
|
|
211
|
+
] as const;
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* The how-to for `mantle.media`, one text for every reader: the
|
|
215
|
+
* canvas_authoring skill (in-app agents) sets it in as a section, and
|
|
216
|
+
* canvas_library 'media' (what an MCP client reads) answers it with a full
|
|
217
|
+
* example canvas. Markdown, no heading of its own. The caps come from the
|
|
218
|
+
* constants above, so the words never drift from the checks.
|
|
219
|
+
*/
|
|
220
|
+
export const CANVAS_MEDIA_HOWTO = `Only when a canvas plays sound or shows a picture kept in Files (narration clips, a photo). Anything small stays inline (a data: URL, inline SVG).
|
|
221
|
+
|
|
222
|
+
The frame gives every canvas \`mantle.media\` (no import, no network: the page around the canvas fetches the file as the viewer and hands the bytes in):
|
|
223
|
+
|
|
224
|
+
- \`await mantle.media.url(fileId)\`: a \`blob:\` URL of the file, for an \`<audio>\` or \`<img>\` \`src\`. One URL per file, kept until released.
|
|
225
|
+
- \`await mantle.media.image(fileId)\`: the same call, for pictures.
|
|
226
|
+
- \`mantle.media.release(fileId)\`: free the file's URL once it has played.
|
|
227
|
+
|
|
228
|
+
Rules:
|
|
229
|
+
|
|
230
|
+
- Declare every file in the head: \`<meta name="${CANVAS_ASSETS_META}" content="<file id> <file id> ...">\`, ids from \`file_list\` or \`speech_file_create\`, at most ${CANVAS_ASSETS_MAX}. A call for a file not declared is refused. The save keeps only the files you can read, and its reply says which it left out and who cannot play them.
|
|
231
|
+
- mp3 or ogg sound, png, jpeg or webp pictures, at most ${CANVAS_MEDIA_MAX_BYTES / (1024 * 1024)} MB each. Never a file URL in the source: it is refused.
|
|
232
|
+
- Every call can reject: a viewer who cannot read the file, an open link whose media is off, a picture of the canvas (poster, export: no sound there). Always keep a way on without it: captions, \`speechSynthesis\`, or the next slide.
|
|
233
|
+
- Browsers play sound only from a click, and iPhone and iPad allow it per player. Use ONE shared player: ask for the first clip's URL before the click, set its \`src\` and call \`play()\` inside the click handler itself (no \`await\` before it), then change \`src\` and \`play()\` again on \`ended\` for each next clip. A new \`<audio>\` per clip stops after the first one there.
|
|
234
|
+
- Ask for the next clip while one plays; release the one that finished.
|
|
235
|
+
|
|
236
|
+
\`\`\`html
|
|
237
|
+
<button id="start" class="rounded-md bg-primary px-8 py-4 text-2xl text-primary-foreground">Start</button>
|
|
238
|
+
<p id="caption" class="text-3xl" aria-live="polite"></p>
|
|
239
|
+
<script type="module">
|
|
240
|
+
// Each line's caption and clip; the same ids are in <meta name="${CANVAS_ASSETS_META}">.
|
|
241
|
+
const lines = [
|
|
242
|
+
{ text: 'Welcome to the tour.', fileId: '${CANVAS_MEDIA_EXAMPLE_IDS[0]}' },
|
|
243
|
+
{ text: 'Here is the second stop.', fileId: '${CANVAS_MEDIA_EXAMPLE_IDS[1]}' },
|
|
244
|
+
];
|
|
245
|
+
const player = new Audio();
|
|
246
|
+
const caption = document.getElementById('caption');
|
|
247
|
+
const asked = new Map();
|
|
248
|
+
const urlOf = (line) => {
|
|
249
|
+
if (!asked.has(line.fileId)) asked.set(line.fileId, mantle.media.url(line.fileId).catch(() => null));
|
|
250
|
+
return asked.get(line.fileId);
|
|
251
|
+
};
|
|
252
|
+
let at = 0;
|
|
253
|
+
const first = await urlOf(lines[0]); // fetched before the click: no gesture needed for that
|
|
254
|
+
function speak(text) {
|
|
255
|
+
if (!('speechSynthesis' in window)) return void setTimeout(next, 2500);
|
|
256
|
+
const u = new SpeechSynthesisUtterance(text);
|
|
257
|
+
u.onend = next;
|
|
258
|
+
speechSynthesis.speak(u);
|
|
259
|
+
}
|
|
260
|
+
function show(url) {
|
|
261
|
+
const line = lines[at];
|
|
262
|
+
caption.textContent = line.text;
|
|
263
|
+
if (lines[at + 1]) urlOf(lines[at + 1]);
|
|
264
|
+
if (!url) return speak(line.text);
|
|
265
|
+
player.src = url;
|
|
266
|
+
player.play().catch(() => speak(line.text));
|
|
267
|
+
}
|
|
268
|
+
async function next() {
|
|
269
|
+
const done = lines[at];
|
|
270
|
+
mantle.media.release(done.fileId);
|
|
271
|
+
asked.delete(done.fileId);
|
|
272
|
+
at += 1;
|
|
273
|
+
if (at < lines.length) show(await urlOf(lines[at]));
|
|
274
|
+
}
|
|
275
|
+
player.addEventListener('ended', next);
|
|
276
|
+
// play() inside the click itself: this is what unlocks the one player.
|
|
277
|
+
document.getElementById('start').addEventListener('click', () => show(first), { once: true });
|
|
278
|
+
</script>
|
|
279
|
+
\`\`\``;
|
package/src/canvas.test.ts
CHANGED
|
@@ -142,7 +142,7 @@ describe('canvasLoadProblems', () => {
|
|
|
142
142
|
|
|
143
143
|
it('names an unknown library, a CDN and an outside file, with the fix', () => {
|
|
144
144
|
const bad = `<html><body><script src="https://cdn.example.test/x.js"></script>
|
|
145
|
-
<img src="/
|
|
145
|
+
<img src="/img/1.png"><script type="module">import gsap from 'gsap'; import x from 'https://esm.example.test/y';</script></body></html>`;
|
|
146
146
|
const p = canvasLoadProblems(bad, MODULES);
|
|
147
147
|
expect(p).toHaveLength(4);
|
|
148
148
|
expect(p.join('\n')).toMatch(
|
package/src/canvas.ts
CHANGED
|
@@ -10,6 +10,12 @@
|
|
|
10
10
|
* canvas safe to run. The sandboxed frame does that (section 5.2).
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
+
import {
|
|
14
|
+
CANVAS_ASSETS_META,
|
|
15
|
+
canvasAssetDeclaration,
|
|
16
|
+
type CanvasAssetDeclaration,
|
|
17
|
+
} from './canvas-media';
|
|
18
|
+
|
|
13
19
|
/** The largest canvas source the brain stores (section 4.1). */
|
|
14
20
|
export const CANVAS_MAX_BYTES = 1_000_000;
|
|
15
21
|
|
|
@@ -222,6 +228,12 @@ export function parseCanvasHead(html: string): CanvasHead {
|
|
|
222
228
|
};
|
|
223
229
|
}
|
|
224
230
|
|
|
231
|
+
/** The files a canvas declares for `mantle.media` (canvas-media.ts): its
|
|
232
|
+
* `canvas:assets` head tag, and a template canvas's data `fileId` fields. */
|
|
233
|
+
export function canvasAssets(html: string): CanvasAssetDeclaration {
|
|
234
|
+
return canvasAssetDeclaration(html, metaTags(headOf(html)).get(CANVAS_ASSETS_META) ?? null);
|
|
235
|
+
}
|
|
236
|
+
|
|
225
237
|
/** The most text a canvas contributes to search and the extractor. */
|
|
226
238
|
export const CANVAS_TEXT_MAX = 20_000;
|
|
227
239
|
|
|
@@ -360,6 +372,12 @@ export function canvasLoadProblems(html: string, modules: readonly string[]): st
|
|
|
360
372
|
continue;
|
|
361
373
|
}
|
|
362
374
|
if (url.startsWith(CANVAS_RUNTIME_PREFIX) || url.startsWith('/share-runtime/')) continue;
|
|
375
|
+
if (/^\/(?:api\/files\/|s\/[^/]+\/a\/)/.test(url)) {
|
|
376
|
+
problems.push(
|
|
377
|
+
`'${url.slice(0, 120)}': a canvas loads no file by URL. Play or show a file with mantle.media.url('<file id>') and list the id in <meta name="${CANVAS_ASSETS_META}" content="id id ...">.`,
|
|
378
|
+
);
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
363
381
|
problems.push(
|
|
364
382
|
`'${url.slice(0, 120)}': only files under ${CANVAS_RUNTIME_PREFIX} (for example ${CANVAS_RUNTIME_PREFIX}plotly/plotly.min.js) load in a canvas. Inline anything else.`,
|
|
365
383
|
);
|