@open-artifact/motion-stage 0.0.0-stage → 0.12.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.
Files changed (173) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +5 -0
  3. package/README.md +5 -2
  4. package/package.json +69 -4
  5. package/src/build.d.ts +18 -0
  6. package/src/build.js +73 -0
  7. package/src/host.d.ts +37 -0
  8. package/src/host.js +76 -0
  9. package/src/index.d.ts +11 -0
  10. package/src/index.js +6 -0
  11. package/src/layers.d.ts +30 -0
  12. package/src/layers.js +56 -0
  13. package/src/offline/audio.d.ts +19 -0
  14. package/src/offline/audio.js +20 -0
  15. package/src/offline/data.d.ts +26 -0
  16. package/src/offline/data.js +71 -0
  17. package/src/offline/main.d.ts +9 -0
  18. package/src/offline/main.js +52 -0
  19. package/src/offline/player.d.ts +23 -0
  20. package/src/offline/player.js +155 -0
  21. package/src/protocol/guards.d.ts +6 -0
  22. package/src/protocol/guards.js +208 -0
  23. package/src/protocol/layer-guards.d.ts +7 -0
  24. package/src/protocol/layer-guards.js +97 -0
  25. package/src/protocol/layers.d.ts +123 -0
  26. package/src/protocol/layers.js +1 -0
  27. package/src/protocol/types.d.ts +261 -0
  28. package/src/protocol/types.js +7 -0
  29. package/src/protocol.d.ts +2 -0
  30. package/src/protocol.js +2 -0
  31. package/src/raster.d.ts +4 -0
  32. package/src/raster.js +2 -0
  33. package/src/runtime/assets.d.ts +19 -0
  34. package/src/runtime/assets.js +63 -0
  35. package/src/runtime/captions.d.ts +26 -0
  36. package/src/runtime/captions.js +103 -0
  37. package/src/runtime/console.d.ts +13 -0
  38. package/src/runtime/console.js +99 -0
  39. package/src/runtime/cut.d.ts +7 -0
  40. package/src/runtime/cut.js +63 -0
  41. package/src/runtime/determinism.d.ts +12 -0
  42. package/src/runtime/determinism.js +47 -0
  43. package/src/runtime/film.d.ts +40 -0
  44. package/src/runtime/film.js +29 -0
  45. package/src/runtime/focus.d.ts +8 -0
  46. package/src/runtime/focus.js +71 -0
  47. package/src/runtime/fonts.d.ts +22 -0
  48. package/src/runtime/fonts.js +109 -0
  49. package/src/runtime/frame.d.ts +9 -0
  50. package/src/runtime/frame.js +123 -0
  51. package/src/runtime/kit/camera.d.ts +40 -0
  52. package/src/runtime/kit/camera.js +56 -0
  53. package/src/runtime/kit/chip.d.ts +25 -0
  54. package/src/runtime/kit/chip.js +75 -0
  55. package/src/runtime/kit/cursor.d.ts +46 -0
  56. package/src/runtime/kit/cursor.js +108 -0
  57. package/src/runtime/kit/dom.d.ts +14 -0
  58. package/src/runtime/kit/dom.js +46 -0
  59. package/src/runtime/kit/geometry.d.ts +22 -0
  60. package/src/runtime/kit/geometry.js +46 -0
  61. package/src/runtime/kit/index.d.ts +95 -0
  62. package/src/runtime/kit/index.js +149 -0
  63. package/src/runtime/kit/mark-strokes.d.ts +65 -0
  64. package/src/runtime/kit/mark-strokes.js +226 -0
  65. package/src/runtime/kit/marks.d.ts +21 -0
  66. package/src/runtime/kit/marks.js +165 -0
  67. package/src/runtime/kit/math.d.ts +23 -0
  68. package/src/runtime/kit/math.js +35 -0
  69. package/src/runtime/kit/paper.d.ts +7 -0
  70. package/src/runtime/kit/paper.js +27 -0
  71. package/src/runtime/kit/random.d.ts +8 -0
  72. package/src/runtime/kit/random.js +32 -0
  73. package/src/runtime/kit/text-fx.d.ts +18 -0
  74. package/src/runtime/kit/text-fx.js +55 -0
  75. package/src/runtime/kit/typing.d.ts +21 -0
  76. package/src/runtime/kit/typing.js +42 -0
  77. package/src/runtime/layers/describe.d.ts +41 -0
  78. package/src/runtime/layers/describe.js +327 -0
  79. package/src/runtime/layers/item.d.ts +121 -0
  80. package/src/runtime/layers/item.js +149 -0
  81. package/src/runtime/layers/parts.d.ts +50 -0
  82. package/src/runtime/layers/parts.js +155 -0
  83. package/src/runtime/layers/rows.d.ts +4 -0
  84. package/src/runtime/layers/rows.js +233 -0
  85. package/src/runtime/layers/scan.d.ts +24 -0
  86. package/src/runtime/layers/scan.js +202 -0
  87. package/src/runtime/layers/walk.d.ts +16 -0
  88. package/src/runtime/layers/walk.js +333 -0
  89. package/src/runtime/load.d.ts +10 -0
  90. package/src/runtime/load.js +228 -0
  91. package/src/runtime/main.d.ts +1 -0
  92. package/src/runtime/main.js +69 -0
  93. package/src/runtime/modules.d.ts +23 -0
  94. package/src/runtime/modules.js +53 -0
  95. package/src/runtime/port.d.ts +8 -0
  96. package/src/runtime/port.js +46 -0
  97. package/src/runtime/questions.d.ts +24 -0
  98. package/src/runtime/questions.js +162 -0
  99. package/src/runtime/recut.d.ts +35 -0
  100. package/src/runtime/recut.js +158 -0
  101. package/src/runtime/render.d.ts +80 -0
  102. package/src/runtime/render.js +342 -0
  103. package/src/runtime/retouch/ask.d.ts +17 -0
  104. package/src/runtime/retouch/ask.js +156 -0
  105. package/src/runtime/retouch/entry.d.ts +77 -0
  106. package/src/runtime/retouch/entry.js +1 -0
  107. package/src/runtime/retouch/judge.d.ts +25 -0
  108. package/src/runtime/retouch/judge.js +209 -0
  109. package/src/runtime/retouch/look.d.ts +39 -0
  110. package/src/runtime/retouch/look.js +135 -0
  111. package/src/runtime/retouch/parts.d.ts +22 -0
  112. package/src/runtime/retouch/parts.js +166 -0
  113. package/src/runtime/retouch/retouch.d.ts +37 -0
  114. package/src/runtime/retouch/retouch.js +238 -0
  115. package/src/runtime/retouch/seat.d.ts +40 -0
  116. package/src/runtime/retouch/seat.js +228 -0
  117. package/src/runtime/retouch/state.d.ts +37 -0
  118. package/src/runtime/retouch/state.js +153 -0
  119. package/src/runtime/retouch/type-style.d.ts +19 -0
  120. package/src/runtime/retouch/type-style.js +111 -0
  121. package/src/runtime/scan.d.ts +2 -0
  122. package/src/runtime/scan.js +49 -0
  123. package/src/runtime/scenes/card.d.ts +12 -0
  124. package/src/runtime/scenes/card.js +56 -0
  125. package/src/runtime/scenes/install.d.ts +47 -0
  126. package/src/runtime/scenes/install.js +387 -0
  127. package/src/runtime/scenes/read.d.ts +15 -0
  128. package/src/runtime/scenes/read.js +57 -0
  129. package/src/runtime/scenes/scoped.d.ts +13 -0
  130. package/src/runtime/scenes/scoped.js +40 -0
  131. package/src/runtime/score.d.ts +26 -0
  132. package/src/runtime/score.js +221 -0
  133. package/src/runtime/sketch/pen.d.ts +3 -0
  134. package/src/runtime/sketch/pen.js +371 -0
  135. package/src/runtime/sketch/strokes.d.ts +41 -0
  136. package/src/runtime/sketch/strokes.js +187 -0
  137. package/src/runtime/snapshot/draw.d.ts +14 -0
  138. package/src/runtime/snapshot/draw.js +125 -0
  139. package/src/runtime/snapshot/hold-first-draw.d.ts +12 -0
  140. package/src/runtime/snapshot/hold-first-draw.js +128 -0
  141. package/src/runtime/snapshot/plate.d.ts +20 -0
  142. package/src/runtime/snapshot/plate.js +79 -0
  143. package/src/runtime/snapshot/serialize.d.ts +49 -0
  144. package/src/runtime/snapshot/serialize.js +271 -0
  145. package/src/runtime/snapshot/styles.d.ts +8 -0
  146. package/src/runtime/snapshot/styles.js +109 -0
  147. package/src/runtime/snapshot/xml.d.ts +6 -0
  148. package/src/runtime/snapshot/xml.js +110 -0
  149. package/src/runtime/stage-size.d.ts +7 -0
  150. package/src/runtime/stage-size.js +3 -0
  151. package/src/runtime/stage.d.ts +27 -0
  152. package/src/runtime/stage.js +154 -0
  153. package/src/runtime/stretch.d.ts +13 -0
  154. package/src/runtime/stretch.js +16 -0
  155. package/src/runtime/texts.d.ts +24 -0
  156. package/src/runtime/texts.js +210 -0
  157. package/src/runtime/video-sync.d.ts +26 -0
  158. package/src/runtime/video-sync.js +126 -0
  159. package/src/stills.d.ts +41 -0
  160. package/src/stills.js +253 -0
  161. package/src/tools/film.d.ts +13 -0
  162. package/src/tools/film.js +116 -0
  163. package/src/tools/main.d.ts +1 -0
  164. package/src/tools/main.js +216 -0
  165. package/src/tools/score.d.ts +4 -0
  166. package/src/tools/score.js +29 -0
  167. package/src/tools/types.d.ts +69 -0
  168. package/src/tools/types.js +2 -0
  169. package/src/tools.d.ts +5 -0
  170. package/src/tools.js +3 -0
  171. package/src/virtual.d.ts +11 -0
  172. package/src/vite.d.ts +8 -0
  173. package/src/vite.js +43 -0
package/src/stills.js ADDED
@@ -0,0 +1,253 @@
1
+ /**
2
+ * Stills of a video artifact, drawn as its export draws them, by a headless Chromium driving the tools page (see
3
+ * `./tools`). It needs only Node (22 or later: it uses the built-in WebSocket) and a Chromium: `CHROME_PATH`, else one
4
+ * from Playwright's browser cache, else Chrome or Chromium where the system keeps it. It speaks the DevTools protocol
5
+ * itself, so it installs nothing. Shared by `scripts/stills.mjs` and the `open-artifact stills` command.
6
+ */
7
+ import { spawn } from 'node:child_process';
8
+ import { existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from 'node:fs';
9
+ import { homedir, tmpdir } from 'node:os';
10
+ import { join, resolve } from 'node:path';
11
+ import { pathToFileURL } from 'node:url';
12
+ /** A stills request that cannot be met: printed as it is, without a stack. */
13
+ export class StillsError extends Error {
14
+ }
15
+ export const STILLS_MAX_SCALE = 4;
16
+ /** The tools page's file name beside a built `open-artifact` command. */
17
+ export const STILLS_TOOLS_PAGE = 'motion-tools.html';
18
+ /** Played seconds, comma-separated (`0.8,2.4,5`); `null` when any is not a second of the film. */
19
+ export function parseStillTimes(text) {
20
+ const times = text.split(',').map((part) => (part.trim() === '' ? Number.NaN : Number(part)));
21
+ return times.length > 0 && times.every((t) => Number.isFinite(t) && t >= 0) ? times : null;
22
+ }
23
+ /** The scale a still is drawn at, or `null` outside (0, 4]. */
24
+ export function parseStillScale(text) {
25
+ if (text === undefined)
26
+ return 1;
27
+ const scale = Number(text);
28
+ return scale > 0 && scale <= STILLS_MAX_SCALE ? scale : null;
29
+ }
30
+ /** The PNG a time is written to. */
31
+ export const stillFileName = (t) => 't_' + t.toFixed(3) + '.png';
32
+ const plainFile = (path) => lstatSync(path, { throwIfNoEntry: false })?.isFile() === true;
33
+ /** The film's documents (film/, modules/, assets/; never people's own data/) and the uploads its assets name. */
34
+ export function readStillsFilm(root, warn = () => undefined) {
35
+ const db = join(root, 'db');
36
+ if (!plainFile(join(db, 'film', 'meta.json')))
37
+ throw new StillsError(root + ' is not a motion artifact: it has no db/film/meta.json.');
38
+ const documents = {};
39
+ for (const collection of ['film', 'modules', 'assets']) {
40
+ const folder = join(db, collection);
41
+ if (!existsSync(folder))
42
+ continue;
43
+ for (const name of readdirSync(folder)) {
44
+ if (!name.endsWith('.json'))
45
+ continue;
46
+ const path = join(folder, name);
47
+ if (!plainFile(path)) {
48
+ warn('skipped ' + path + ': not a plain file');
49
+ continue;
50
+ }
51
+ documents[collection + '/' + name.slice(0, -'.json'.length)] = readFileSync(path, 'utf8');
52
+ }
53
+ }
54
+ const blobs = {};
55
+ const uploads = join(root, '_blobs');
56
+ const stored = existsSync(uploads) ? readdirSync(uploads) : [];
57
+ for (const [path, text] of Object.entries(documents)) {
58
+ if (!path.startsWith('assets/'))
59
+ continue;
60
+ let ref = '';
61
+ try {
62
+ ref = String(JSON.parse(text).ref ?? '');
63
+ }
64
+ catch {
65
+ continue;
66
+ }
67
+ const id = /^\/?_blob\/([0-9a-f]{32})$/.exec(ref)?.[1];
68
+ if (id === undefined || id in blobs)
69
+ continue;
70
+ const file = stored.find((name) => name === id || name.startsWith(id + '.'));
71
+ if (file !== undefined && plainFile(join(uploads, file)))
72
+ blobs[id] = readFileSync(join(uploads, file)).toString('base64');
73
+ }
74
+ return { documents, blobs };
75
+ }
76
+ const safeList = (dir) => {
77
+ try {
78
+ return readdirSync(dir);
79
+ }
80
+ catch {
81
+ return [];
82
+ }
83
+ };
84
+ /** A Chrome or Chromium to draw with: `CHROME_PATH`, Playwright's cache, then the system's. */
85
+ export function findChromium(env = process.env) {
86
+ if (env.CHROME_PATH) {
87
+ if (existsSync(env.CHROME_PATH))
88
+ return env.CHROME_PATH;
89
+ throw new StillsError('CHROME_PATH does not exist: ' + env.CHROME_PATH);
90
+ }
91
+ const caches = [env.PLAYWRIGHT_BROWSERS_PATH, join(homedir(), 'Library', 'Caches', 'ms-playwright'), join(homedir(), '.cache', 'ms-playwright'), '/opt/pw-browsers'].filter((path) => path !== undefined && path !== '' && existsSync(path));
92
+ const found = [];
93
+ for (const cache of caches) {
94
+ for (const build of safeList(cache).sort().reverse()) {
95
+ const home = join(cache, build);
96
+ if (build.startsWith('chromium_headless_shell-'))
97
+ for (const sub of safeList(home))
98
+ found.push(join(home, sub, 'chrome-headless-shell'));
99
+ else if (build.startsWith('chromium-')) {
100
+ found.push(join(home, 'chrome-linux', 'chrome'), join(home, 'chrome-linux64', 'chrome'));
101
+ for (const sub of safeList(home))
102
+ found.push(join(home, sub, 'Chromium.app', 'Contents', 'MacOS', 'Chromium'));
103
+ }
104
+ }
105
+ }
106
+ found.push('/Applications/Google Chrome.app/Contents/MacOS/Google Chrome', '/Applications/Chromium.app/Contents/MacOS/Chromium', '/usr/bin/google-chrome', '/usr/bin/google-chrome-stable', '/usr/bin/chromium', '/usr/bin/chromium-browser');
107
+ const binary = found.find((path) => existsSync(path));
108
+ if (binary === undefined)
109
+ throw new StillsError('No Chromium found. Set CHROME_PATH to a Chrome or Chromium binary.');
110
+ return binary;
111
+ }
112
+ // The film's code gets no network: every host resolves to nothing, through a proxy that is not there.
113
+ const CHROMIUM_ARGS = [
114
+ '--headless=new',
115
+ '--remote-debugging-port=0',
116
+ '--no-first-run',
117
+ '--no-default-browser-check',
118
+ '--disable-extensions',
119
+ '--disable-background-networking',
120
+ '--disable-component-update',
121
+ '--mute-audio',
122
+ '--hide-scrollbars',
123
+ '--force-color-profile=srgb',
124
+ '--font-render-hinting=none',
125
+ '--disable-lcd-text',
126
+ '--window-size=1920,1080',
127
+ '--host-resolver-rules=MAP * ^NOTFOUND',
128
+ '--proxy-server=http://127.0.0.1:9',
129
+ '--proxy-bypass-list=<-loopback>',
130
+ ];
131
+ function launch(binary, profile) {
132
+ const child = spawn(binary, [...CHROMIUM_ARGS, '--user-data-dir=' + profile, 'about:blank'], { stdio: ['ignore', 'ignore', 'pipe'] });
133
+ return new Promise((done, fail) => {
134
+ let said = '';
135
+ const timer = setTimeout(() => fail(new Error('Chromium did not start within 30 s:\n' + said.slice(-2000))), 30000);
136
+ child.stderr?.on('data', (chunk) => {
137
+ said += String(chunk);
138
+ const url = /DevTools listening on (ws:\/\/\S+)/.exec(said)?.[1];
139
+ if (url === undefined)
140
+ return;
141
+ clearTimeout(timer);
142
+ done({ child, url });
143
+ });
144
+ child.on('error', (error) => {
145
+ clearTimeout(timer);
146
+ fail(error);
147
+ });
148
+ child.on('exit', (code) => fail(new Error('Chromium exited (' + String(code) + '):\n' + said.slice(-2000))));
149
+ });
150
+ }
151
+ /** The DevTools protocol over one WebSocket, commands sent to one page's session. */
152
+ async function connect(url) {
153
+ const socket = new WebSocket(url);
154
+ await new Promise((done, fail) => {
155
+ socket.onopen = () => done();
156
+ socket.onerror = () => fail(new Error('could not reach Chromium at ' + url));
157
+ });
158
+ let id = 0;
159
+ const pending = new Map();
160
+ socket.onmessage = (event) => {
161
+ const message = JSON.parse(String(event.data));
162
+ const waiter = message.id === undefined ? undefined : pending.get(message.id);
163
+ if (waiter === undefined || message.id === undefined)
164
+ return;
165
+ pending.delete(message.id);
166
+ if (message.error)
167
+ waiter.fail(new Error(message.error.message));
168
+ else
169
+ waiter.done(message.result);
170
+ };
171
+ const send = (method, params = {}, sessionId) => new Promise((done, fail) => {
172
+ pending.set(++id, { done: (value) => done(value), fail });
173
+ socket.send(JSON.stringify({ id, method, params, ...(sessionId ? { sessionId } : {}) }));
174
+ });
175
+ const { targetId } = await send('Target.createTarget', { url: 'about:blank' });
176
+ const { sessionId } = await send('Target.attachToTarget', { targetId, flatten: true });
177
+ return {
178
+ navigate: (to) => send('Page.navigate', { url: to }, sessionId),
179
+ async evaluate(expression) {
180
+ const { result, exceptionDetails } = await send('Runtime.evaluate', { expression, awaitPromise: true, returnByValue: true }, sessionId);
181
+ if (exceptionDetails)
182
+ throw new Error(exceptionDetails.exception?.description ?? exceptionDetails.text);
183
+ return result.value;
184
+ },
185
+ async close() {
186
+ await send('Browser.close').catch(() => undefined);
187
+ socket.close();
188
+ },
189
+ };
190
+ }
191
+ const messageOf = (error) => (error instanceof Error ? error.message : String(error));
192
+ /**
193
+ * Draws the stills asked for with the tools page's HTML (`buildToolsPage`, or the copy a build ships), printing
194
+ * through `say`: the film's problems, its size and length, each PNG's path, the score's counts and the film's own
195
+ * warnings and errors. Resolves to the exit code: 1 when the film did not load.
196
+ */
197
+ export async function drawStills(request, toolsPage, say = (line) => console.log(line)) {
198
+ const root = resolve(request.root);
199
+ const files = { ...readStillsFilm(root, (line) => console.warn(line)), mode: request.mode };
200
+ const binary = findChromium();
201
+ const work = mkdtempSync(join(tmpdir(), 'film-tools-'));
202
+ const out = request.out === null ? mkdtempSync(join(tmpdir(), 'film-stills-')) : resolve(request.out);
203
+ mkdirSync(out, { recursive: true });
204
+ const page = join(work, 'tools.html');
205
+ writeFileSync(page, toolsPage);
206
+ let child = null;
207
+ let browser = null;
208
+ try {
209
+ const launched = await launch(binary, join(work, 'profile'));
210
+ child = launched.child;
211
+ browser = await connect(launched.url);
212
+ await browser.navigate(pathToFileURL(page).href);
213
+ for (let i = 0; i < 100 && (await browser.evaluate('typeof window.filmTools')) !== 'object'; i++)
214
+ await new Promise((next) => setTimeout(next, 100));
215
+ const loaded = await browser.evaluate('window.filmTools.load(' + JSON.stringify(files) + ')');
216
+ for (const problem of loaded.problems)
217
+ say('problem: ' + problem);
218
+ if ('error' in loaded)
219
+ throw new Error('the film did not load: ' + loaded.error.message);
220
+ const { width, height, fps, duration, scenes } = loaded.meta;
221
+ say(JSON.stringify({ width, height, fps, duration, scenes }));
222
+ for (const t of request.times) {
223
+ try {
224
+ const png = await browser.evaluate('window.filmTools.frame(' + String(t) + ', { scale: ' + String(request.scale) + ' })');
225
+ const file = join(out, stillFileName(t));
226
+ writeFileSync(file, Buffer.from(png.slice(png.indexOf(',') + 1), 'base64'));
227
+ say(file);
228
+ }
229
+ catch (error) {
230
+ say('frame ' + String(t) + ' failed: ' + messageOf(error));
231
+ }
232
+ }
233
+ say('score: ' + JSON.stringify(await browser.evaluate('window.filmTools.scoreReport()')));
234
+ for (const line of await browser.evaluate('window.filmTools.consoleLog()'))
235
+ if (line.level !== 'log')
236
+ say('film ' + line.level + ': ' + line.text);
237
+ return 0;
238
+ }
239
+ catch (error) {
240
+ console.error(messageOf(error));
241
+ return 1;
242
+ }
243
+ finally {
244
+ await browser?.close();
245
+ const running = child;
246
+ if (running !== null && running.exitCode === null)
247
+ await new Promise((done) => {
248
+ running.once('exit', () => done());
249
+ running.kill();
250
+ });
251
+ rmSync(work, { recursive: true, force: true });
252
+ }
253
+ }
@@ -0,0 +1,13 @@
1
+ import type { StageFilm } from '../protocol.ts';
2
+ import type { ToolsFiles } from './types.ts';
3
+ export interface ToolsFilm {
4
+ /** null: there is nothing to load, and `problems` says why first. */
5
+ film: StageFilm | null;
6
+ title: string;
7
+ problems: string[];
8
+ }
9
+ /**
10
+ * The film the tools page loads, read from its files as the page reads its store. `retouched.json` stays among
11
+ * the modules: the stage lays it from there at load, as the page's own `retouch` message would.
12
+ */
13
+ export declare function toolsFilm(files: ToolsFiles): ToolsFilm;
@@ -0,0 +1,116 @@
1
+ import { BLOB_URL_PATTERN, EXPORT_SETTINGS, RECOLORED_MODULE, RETYPED_MODULE, VOICE_SETTINGS, applyRecolored, applyRetyped, decodeFilm, filmModule, parseRecoloredRecord, parseRetypedRecord, } from '@open-artifact/motion-core';
2
+ import { fromBase64 } from "../offline/data.js";
3
+ const streams = (mime) => mime.startsWith('video/') || mime.startsWith('audio/');
4
+ function documentsOf(given, problems) {
5
+ const documents = new Map();
6
+ for (const [path, value] of Object.entries(given)) {
7
+ if (typeof value !== 'string') {
8
+ documents.set(path, value);
9
+ continue;
10
+ }
11
+ try {
12
+ documents.set(path, JSON.parse(value));
13
+ }
14
+ catch {
15
+ problems.push(`db/${path}.json is not JSON; the player ignores it.`);
16
+ }
17
+ }
18
+ return documents;
19
+ }
20
+ /** The base64 of an asset's bytes: from its `data:` ref, or from the upload it names. null: not to be had. */
21
+ function base64Of(asset, blobs) {
22
+ if (asset.ref.startsWith('data:')) {
23
+ const comma = asset.ref.indexOf(',');
24
+ return comma < 0 ? null : asset.ref.slice(comma + 1);
25
+ }
26
+ const id = BLOB_URL_PATTERN.exec(asset.ref)?.[1];
27
+ return id === undefined ? null : (blobs[id] ?? null);
28
+ }
29
+ function stageAsset(asset, blobs) {
30
+ const base = { name: asset.name, mime: asset.mime, w: asset.w ?? 0, h: asset.h ?? 0 };
31
+ const data = base64Of(asset, blobs);
32
+ if (data === null)
33
+ return null;
34
+ if (streams(asset.mime))
35
+ return { ...base, url: `data:${asset.mime};base64,${data}` };
36
+ try {
37
+ return { ...base, bytes: fromBase64(data) };
38
+ }
39
+ catch {
40
+ return null;
41
+ }
42
+ }
43
+ function recordOf(film, name, parse) {
44
+ const module = filmModule(film, name);
45
+ if (module === undefined)
46
+ return null;
47
+ try {
48
+ return parse(JSON.parse(module.text));
49
+ }
50
+ catch {
51
+ return parse(null);
52
+ }
53
+ }
54
+ /** A person's retyped lines and colors written into the code, as the page does before the stage runs it. */
55
+ function applied(film, problems) {
56
+ let script = film.script;
57
+ let modules = [...film.modules];
58
+ const retyped = recordOf(film, RETYPED_MODULE, parseRetypedRecord);
59
+ if (retyped !== null) {
60
+ try {
61
+ ({ script, modules } = applyRetyped({ script, modules }, retyped));
62
+ }
63
+ catch {
64
+ problems.push(`A person's text (modules/${RETYPED_MODULE}) could not be swapped in, so the film shows its own.`);
65
+ }
66
+ }
67
+ const recolored = recordOf(film, RECOLORED_MODULE, parseRecoloredRecord);
68
+ if (recolored !== null && recolored.colors.length > 0) {
69
+ try {
70
+ ({ script, modules } = applyRecolored({ script, modules }, recolored));
71
+ }
72
+ catch {
73
+ problems.push(`A person's colors (modules/${RECOLORED_MODULE}) could not be swapped in, so the film shows its own.`);
74
+ }
75
+ }
76
+ return { script, modules };
77
+ }
78
+ /**
79
+ * The film the tools page loads, read from its files as the page reads its store. `retouched.json` stays among
80
+ * the modules: the stage lays it from there at load, as the page's own `retouch` message would.
81
+ */
82
+ export function toolsFilm(files) {
83
+ const problems = [];
84
+ const decoded = decodeFilm(documentsOf(files.documents, problems));
85
+ for (const problem of decoded.problems)
86
+ problems.push(`db/${problem.path}.json ${problem.message}`);
87
+ const title = decoded.meta?.title ?? 'Film';
88
+ if (decoded.script.trim() === '') {
89
+ problems.unshift('The film has no code yet (db/film/script.json has no "code").');
90
+ return { film: null, title, problems };
91
+ }
92
+ const blobs = files.blobs ?? {};
93
+ const assets = [];
94
+ for (const asset of decoded.assets) {
95
+ const loaded = stageAsset(asset, blobs);
96
+ if (loaded !== null)
97
+ assets.push(loaded);
98
+ else
99
+ problems.push(`The asset ${asset.name} could not be loaded (${asset.ref} was not given).`);
100
+ }
101
+ const code = applied(decoded, problems);
102
+ const mode = files.mode ?? 'final';
103
+ return {
104
+ film: {
105
+ script: code.script,
106
+ modules: code.modules.map((module) => ({ name: module.name, kind: module.kind, text: module.text })),
107
+ assets,
108
+ faces: null,
109
+ narration: { maxLines: VOICE_SETTINGS.maxLines, maxLineSeconds: VOICE_SETTINGS.maxLineSeconds },
110
+ clippedTextWaitMs: EXPORT_SETTINGS.clippedTextWaitMs,
111
+ mode,
112
+ },
113
+ title,
114
+ problems,
115
+ };
116
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,216 @@
1
+ import { SNAPSHOT_SETTINGS } from '@open-artifact/motion-core';
2
+ import { STAGE_SANDBOX, openStagePort, stageDocument } from "../host.js";
3
+ import { OFFLINE_RUNTIME_ID } from "../offline/data.js";
4
+ import { STAGE_PROTOCOL_VERSION } from "../protocol.js";
5
+ import { drawSnapshot, makeHoldFirstDraw, whyNoDecode } from "../raster.js";
6
+ import { toolsFilm } from "./film.js";
7
+ import { scoreReport } from "./score.js";
8
+ import { TOOLS_GLOBAL } from "./types.js";
9
+ const LOAD_TIMEOUT_MS = 60000;
10
+ /** The stage's own budget for a pull is 12 s, with its render; the page gives it 15. */
11
+ const FRAME_TIMEOUT_MS = 15000;
12
+ const SHOW_TIMEOUT_MS = 15000;
13
+ const holdFirstDraw = makeHoldFirstDraw();
14
+ let log = [];
15
+ let mounted = null;
16
+ let seq = 0;
17
+ const messageOf = (error) => (error instanceof Error ? error.message : String(error));
18
+ function wait(waiters, at, ms, late) {
19
+ return new Promise((resolve, reject) => {
20
+ const timer = setTimeout(() => {
21
+ waiters.delete(at);
22
+ reject(new Error(late));
23
+ }, ms);
24
+ waiters.set(at, { resolve, reject, timer });
25
+ });
26
+ }
27
+ function rejectAll(waiters, error) {
28
+ for (const waiter of waiters.values()) {
29
+ clearTimeout(waiter.timer);
30
+ waiter.reject(error);
31
+ }
32
+ waiters.clear();
33
+ }
34
+ function unmount(why) {
35
+ if (mounted === null)
36
+ return;
37
+ const gone = mounted;
38
+ mounted = null;
39
+ const error = new Error(why);
40
+ gone.settle.reject(error);
41
+ rejectAll(gone.shows, error);
42
+ rejectAll(gone.pulls, error);
43
+ gone.port?.close();
44
+ gone.frame.remove();
45
+ }
46
+ function hear(own, message) {
47
+ if (mounted !== own)
48
+ return;
49
+ switch (message.type) {
50
+ case 'hello':
51
+ if (message.version !== STAGE_PROTOCOL_VERSION)
52
+ own.settle.reject(new Error('the stage speaks version ' + message.version + ', not ' + STAGE_PROTOCOL_VERSION));
53
+ return;
54
+ case 'ready':
55
+ own.settle.resolve({ meta: message.meta, score: message.score, narration: message.narration });
56
+ return;
57
+ case 'presented':
58
+ for (const [at, waiter] of own.shows) {
59
+ if (at > message.seq)
60
+ continue;
61
+ clearTimeout(waiter.timer);
62
+ own.shows.delete(at);
63
+ waiter.resolve();
64
+ }
65
+ return;
66
+ case 'frame': {
67
+ const waiter = own.pulls.get(message.seq);
68
+ if (waiter === undefined)
69
+ return;
70
+ clearTimeout(waiter.timer);
71
+ own.pulls.delete(message.seq);
72
+ waiter.resolve(message);
73
+ return;
74
+ }
75
+ case 'frame-error': {
76
+ const waiter = own.pulls.get(message.seq);
77
+ if (waiter === undefined)
78
+ return;
79
+ clearTimeout(waiter.timer);
80
+ own.pulls.delete(message.seq);
81
+ waiter.reject(new Error(message.message));
82
+ return;
83
+ }
84
+ case 'console':
85
+ log.push({ level: message.level, text: message.text });
86
+ return;
87
+ case 'error':
88
+ log.push({ level: 'error', text: message.message + (message.stack ? '\n' + message.stack : ''), phase: message.phase });
89
+ if (message.phase === 'load' && own.loaded === null)
90
+ own.settle.reject(new Error(message.message));
91
+ return;
92
+ case 'navigating':
93
+ unmount('the stage navigated away, so the film is gone');
94
+ return;
95
+ case 'texts':
96
+ case 'layers':
97
+ case 'looked':
98
+ case 'link':
99
+ return;
100
+ default: {
101
+ const never = message;
102
+ void never;
103
+ }
104
+ }
105
+ }
106
+ function mount(film, runtime) {
107
+ const frame = document.createElement('iframe');
108
+ frame.setAttribute('sandbox', STAGE_SANDBOX);
109
+ frame.title = 'Film';
110
+ frame.style.cssText = 'position:fixed;inset:0;width:100%;height:100%;border:0;background:transparent';
111
+ frame.srcdoc = stageDocument(runtime);
112
+ let settle = { resolve: () => undefined, reject: () => undefined };
113
+ const ready = new Promise((resolve, reject) => {
114
+ settle = { resolve, reject };
115
+ });
116
+ const own = { frame, port: null, film, loaded: null, settle, shows: new Map(), pulls: new Map() };
117
+ mounted = own;
118
+ frame.addEventListener('load', () => {
119
+ own.port = openStagePort(frame, (message) => hear(own, message));
120
+ own.port?.send({ type: 'load', film });
121
+ }, { once: true });
122
+ document.body.appendChild(frame);
123
+ const timer = setTimeout(() => settle.reject(new Error('the film did not load within ' + LOAD_TIMEOUT_MS / 1000 + ' s')), LOAD_TIMEOUT_MS);
124
+ return { own, ready: ready.finally(() => clearTimeout(timer)) };
125
+ }
126
+ function ready() {
127
+ const own = mounted;
128
+ if (own === null || own.loaded === null)
129
+ throw new Error('no film is loaded: call load(files) first, and wait for it');
130
+ return { own, loaded: own.loaded };
131
+ }
132
+ const within = (t, meta) => Math.max(0, Math.min(meta.duration, Number.isFinite(t) ? t : 0));
133
+ /** The snapshot drawn as an export draws it, read back as a PNG. */
134
+ function raster(reply, waitMs) {
135
+ return new Promise((resolve, reject) => {
136
+ const picture = new Image();
137
+ picture.onload = () => {
138
+ holdFirstDraw(reply.svg, waitMs, () => {
139
+ drawSnapshot(picture, reply.pw, reply.ph)
140
+ .then((surface) => {
141
+ let canvas = surface;
142
+ if (!(canvas instanceof HTMLCanvasElement)) {
143
+ const copy = document.createElement('canvas');
144
+ copy.width = reply.pw;
145
+ copy.height = reply.ph;
146
+ copy.getContext('2d')?.drawImage(canvas, 0, 0);
147
+ canvas.close();
148
+ canvas = copy;
149
+ }
150
+ resolve(canvas.toDataURL('image/png'));
151
+ })
152
+ .catch((error) => reject(new Error('the snapshot could not be drawn: ' + messageOf(error))));
153
+ });
154
+ };
155
+ picture.onerror = () => reject(new Error('snapshot decode failed (foreignObject)' + whyNoDecode(reply.svg)));
156
+ picture.src = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(reply.svg);
157
+ });
158
+ }
159
+ const tools = {
160
+ version: 1,
161
+ async load(files) {
162
+ unmount('another film was loaded');
163
+ log = [];
164
+ const runtime = document.getElementById(OFFLINE_RUNTIME_ID)?.textContent ?? null;
165
+ if (runtime === null)
166
+ return { error: { message: 'this page has no stage runtime' }, problems: [] };
167
+ const read = toolsFilm(files);
168
+ if (read.film === null)
169
+ return { error: { message: read.problems[0] ?? 'the film cannot be read' }, problems: read.problems.slice(1) };
170
+ const { own, ready: loading } = mount(read.film, runtime);
171
+ try {
172
+ const loaded = await loading;
173
+ own.loaded = loaded;
174
+ return { meta: loaded.meta, problems: read.problems };
175
+ }
176
+ catch (error) {
177
+ if (mounted === own)
178
+ unmount('the film failed to load');
179
+ return { error: { message: messageOf(error) }, problems: read.problems };
180
+ }
181
+ },
182
+ seek(t) {
183
+ const { own, loaded } = ready();
184
+ const at = ++seq;
185
+ const shown = wait(own.shows, at, SHOW_TIMEOUT_MS, 'the stage did not show ' + t + ' s in time');
186
+ own.port?.send({ type: 'render', seq: at, t: within(t, loaded.meta), playing: false });
187
+ return shown;
188
+ },
189
+ async frame(t, options = {}) {
190
+ const { own, loaded } = ready();
191
+ const scale = options.scale ?? 1;
192
+ if (!(scale > 0 && scale <= 4))
193
+ throw new Error('scale must be above 0 and at most 4, not ' + String(options.scale));
194
+ const at = ++seq;
195
+ const reply = wait(own.pulls, at, FRAME_TIMEOUT_MS, 'the stage did not answer for the frame at ' + t + ' s in time');
196
+ const ask = {
197
+ type: 'frame',
198
+ seq: at,
199
+ t: within(t, loaded.meta),
200
+ scale,
201
+ smallMedia: false,
202
+ readFx: SNAPSHOT_SETTINGS.fx === 'read',
203
+ smooth: false,
204
+ };
205
+ own.port?.send(ask);
206
+ return raster(await reply, own.film.clippedTextWaitMs);
207
+ },
208
+ scoreReport() {
209
+ const loaded = mounted?.loaded ?? null;
210
+ return loaded === null ? null : scoreReport(loaded.score, loaded.narration);
211
+ },
212
+ consoleLog() {
213
+ return log.slice();
214
+ },
215
+ };
216
+ Reflect.set(window, TOOLS_GLOBAL, tools);
@@ -0,0 +1,4 @@
1
+ import type { NarrationReport, PlayedScore } from '../protocol.ts';
2
+ import type { ScoreReport } from './types.ts';
3
+ /** The score and narration a load reported, counted: enough to tell a silent or broken score at a glance. */
4
+ export declare function scoreReport(score: PlayedScore | null, narration: NarrationReport | null): ScoreReport;
@@ -0,0 +1,29 @@
1
+ import { DRUM_NAMES, FX_NAMES, PATCH_NAMES } from '@open-artifact/motion-core';
2
+ const VOICES = { note: PATCH_NAMES, drum: DRUM_NAMES, fx: FX_NAMES };
3
+ const played = (t) => t;
4
+ /** The score and narration a load reported, counted: enough to tell a silent or broken score at a glance. */
5
+ export function scoreReport(score, narration) {
6
+ const events = score?.events ?? [];
7
+ const unknown = new Set();
8
+ const counts = { note: 0, drum: 0, fx: 0 };
9
+ let first = Infinity;
10
+ let last = -Infinity;
11
+ for (const event of events) {
12
+ counts[event.kind]++;
13
+ if (!VOICES[event.kind].includes(event.patch))
14
+ unknown.add(event.kind + ':' + event.patch);
15
+ first = Math.min(first, event.t);
16
+ last = Math.max(last, event.t + event.dur);
17
+ }
18
+ return {
19
+ events: events.length,
20
+ notes: counts.note,
21
+ drums: counts.drum,
22
+ fx: counts.fx,
23
+ unknown: [...unknown],
24
+ first: played(Number.isFinite(first) ? first : 0),
25
+ last: played(Number.isFinite(last) ? last : 0),
26
+ tempo: score?.tempo ?? null,
27
+ narration: narration === null ? null : { lines: narration.lines.length, seconds: narration.lines.reduce((sum, line) => sum + line.dur, 0) },
28
+ };
29
+ }