nucleus-core-ts 0.10.183 → 0.10.185

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 (115) hide show
  1. package/dist/.build-ok +1 -1
  2. package/dist/fe/components/DemoFilm/_components/Caption/index.d.ts +11 -0
  3. package/dist/fe/components/DemoFilm/_components/Caption/index.js +42 -0
  4. package/dist/fe/components/DemoFilm/_components/DemoFilmRoot/index.d.ts +15 -0
  5. package/dist/fe/components/DemoFilm/_components/DemoFilmRoot/index.js +26 -0
  6. package/dist/fe/components/DemoFilm/_components/DeviceFrame/BrowserBar.d.ts +8 -0
  7. package/dist/fe/components/DemoFilm/_components/DeviceFrame/BrowserBar.js +50 -0
  8. package/dist/fe/components/DemoFilm/_components/DeviceFrame/Island.d.ts +5 -0
  9. package/dist/fe/components/DemoFilm/_components/DeviceFrame/Island.js +16 -0
  10. package/dist/fe/components/DemoFilm/_components/DeviceFrame/index.d.ts +11 -0
  11. package/dist/fe/components/DemoFilm/_components/DeviceFrame/index.js +46 -0
  12. package/dist/fe/components/DemoFilm/_components/FilmImage/index.d.ts +11 -0
  13. package/dist/fe/components/DemoFilm/_components/FilmImage/index.js +14 -0
  14. package/dist/fe/components/DemoFilm/_components/FilmPlayer/index.d.ts +25 -0
  15. package/dist/fe/components/DemoFilm/_components/FilmPlayer/index.js +86 -0
  16. package/dist/fe/components/DemoFilm/_components/Highlight/index.d.ts +13 -0
  17. package/dist/fe/components/DemoFilm/_components/Highlight/index.js +62 -0
  18. package/dist/fe/components/DemoFilm/_components/RoleStage/Body.d.ts +14 -0
  19. package/dist/fe/components/DemoFilm/_components/RoleStage/Body.js +67 -0
  20. package/dist/fe/components/DemoFilm/_components/RoleStage/RoleLabel.d.ts +10 -0
  21. package/dist/fe/components/DemoFilm/_components/RoleStage/RoleLabel.js +30 -0
  22. package/dist/fe/components/DemoFilm/_components/RoleStage/index.d.ts +8 -0
  23. package/dist/fe/components/DemoFilm/_components/RoleStage/index.js +36 -0
  24. package/dist/fe/components/DemoFilm/_components/ScreenImage/index.d.ts +11 -0
  25. package/dist/fe/components/DemoFilm/_components/ScreenImage/index.js +58 -0
  26. package/dist/fe/components/DemoFilm/_components/index.d.ts +8 -0
  27. package/dist/fe/components/DemoFilm/_components/index.js +8 -0
  28. package/dist/fe/components/DemoFilm/_hooks/index.d.ts +5 -0
  29. package/dist/fe/components/DemoFilm/_hooks/index.js +5 -0
  30. package/dist/fe/components/DemoFilm/_hooks/useDemoBeat.d.ts +3 -0
  31. package/dist/fe/components/DemoFilm/_hooks/useDemoBeat.js +24 -0
  32. package/dist/fe/components/DemoFilm/_hooks/useDemoFilm.d.ts +4 -0
  33. package/dist/fe/components/DemoFilm/_hooks/useDemoFilm.js +7 -0
  34. package/dist/fe/components/DemoFilm/_hooks/useFilmClock.d.ts +4 -0
  35. package/dist/fe/components/DemoFilm/_hooks/useFilmClock.js +7 -0
  36. package/dist/fe/components/DemoFilm/_hooks/usePlayback.d.ts +25 -0
  37. package/dist/fe/components/DemoFilm/_hooks/usePlayback.js +102 -0
  38. package/dist/fe/components/DemoFilm/_hooks/useReducedMotion.d.ts +2 -0
  39. package/dist/fe/components/DemoFilm/_hooks/useReducedMotion.js +14 -0
  40. package/dist/fe/components/DemoFilm/_host/index.d.ts +2 -0
  41. package/dist/fe/components/DemoFilm/_host/index.js +2 -0
  42. package/dist/fe/components/DemoFilm/_host/mountDemoFilmHost.d.ts +9 -0
  43. package/dist/fe/components/DemoFilm/_host/mountDemoFilmHost.js +60 -0
  44. package/dist/fe/components/DemoFilm/_host/types.d.ts +35 -0
  45. package/dist/fe/components/DemoFilm/_host/types.js +1 -0
  46. package/dist/fe/components/DemoFilm/_types/clock.d.ts +31 -0
  47. package/dist/fe/components/DemoFilm/_types/clock.js +1 -0
  48. package/dist/fe/components/DemoFilm/_types/device.d.ts +18 -0
  49. package/dist/fe/components/DemoFilm/_types/device.js +1 -0
  50. package/dist/fe/components/DemoFilm/_types/index.d.ts +6 -0
  51. package/dist/fe/components/DemoFilm/_types/index.js +6 -0
  52. package/dist/fe/components/DemoFilm/_types/player.d.ts +25 -0
  53. package/dist/fe/components/DemoFilm/_types/player.js +1 -0
  54. package/dist/fe/components/DemoFilm/_types/stage.d.ts +28 -0
  55. package/dist/fe/components/DemoFilm/_types/stage.js +1 -0
  56. package/dist/fe/components/DemoFilm/_types/theme.d.ts +38 -0
  57. package/dist/fe/components/DemoFilm/_types/theme.js +1 -0
  58. package/dist/fe/components/DemoFilm/_types/timeline.d.ts +49 -0
  59. package/dist/fe/components/DemoFilm/_types/timeline.js +5 -0
  60. package/dist/fe/components/DemoFilm/_utils/demoAssetUrl.d.ts +5 -0
  61. package/dist/fe/components/DemoFilm/_utils/demoAssetUrl.js +6 -0
  62. package/dist/fe/components/DemoFilm/_utils/device.d.ts +7 -0
  63. package/dist/fe/components/DemoFilm/_utils/device.js +50 -0
  64. package/dist/fe/components/DemoFilm/_utils/easing.d.ts +6 -0
  65. package/dist/fe/components/DemoFilm/_utils/easing.js +71 -0
  66. package/dist/fe/components/DemoFilm/_utils/index.d.ts +10 -0
  67. package/dist/fe/components/DemoFilm/_utils/index.js +10 -0
  68. package/dist/fe/components/DemoFilm/_utils/interpolate.d.ts +14 -0
  69. package/dist/fe/components/DemoFilm/_utils/interpolate.js +46 -0
  70. package/dist/fe/components/DemoFilm/_utils/motion.d.ts +6 -0
  71. package/dist/fe/components/DemoFilm/_utils/motion.js +19 -0
  72. package/dist/fe/components/DemoFilm/_utils/spring.d.ts +24 -0
  73. package/dist/fe/components/DemoFilm/_utils/spring.js +73 -0
  74. package/dist/fe/components/DemoFilm/_utils/stageSize.d.ts +9 -0
  75. package/dist/fe/components/DemoFilm/_utils/stageSize.js +19 -0
  76. package/dist/fe/components/DemoFilm/_utils/text.d.ts +3 -0
  77. package/dist/fe/components/DemoFilm/_utils/text.js +3 -0
  78. package/dist/fe/components/DemoFilm/_utils/theme.d.ts +7 -0
  79. package/dist/fe/components/DemoFilm/_utils/theme.js +137 -0
  80. package/dist/fe/components/DemoFilm/_utils/timeline.d.ts +13 -0
  81. package/dist/fe/components/DemoFilm/_utils/timeline.js +32 -0
  82. package/dist/fe/components/DemoFilm/index.d.ts +4 -0
  83. package/dist/fe/components/DemoFilm/index.js +4 -0
  84. package/dist/fe/index.d.ts +1 -0
  85. package/dist/fe/index.js +1 -0
  86. package/dist/index.js +8 -8
  87. package/dist/server.js +11 -11
  88. package/dist/src/Data/exprTypes.d.ts +29 -0
  89. package/dist/src/Data/index.d.ts +3 -2
  90. package/dist/src/Data/ruleContext.d.ts +3 -1
  91. package/dist/src/Data/ruleRefusals.d.ts +9 -0
  92. package/dist/src/Data/ruleTestKit.d.ts +2 -1
  93. package/dist/src/Data/ruleTypes.d.ts +12 -1
  94. package/dist/src/Data/unitBypass.d.ts +24 -0
  95. package/dist/src/Data/unitTypes.d.ts +8 -0
  96. package/dist/src/Data/viewAggTestKit.d.ts +1 -1
  97. package/dist/src/Nucleus/routes/auth/sessionPools.d.ts +16 -0
  98. package/dist/src/Nucleus/routes/tenant/selfSignupRateLimit.d.ts +19 -0
  99. package/dist/src/Nucleus/routes/tenant/types.d.ts +3 -0
  100. package/dist/src/Services/Auth/accountErasure.d.ts +6 -0
  101. package/dist/src/types.d.ts +2 -0
  102. package/package.json +21 -3
  103. package/scripts/demo-film/chrome.ts +75 -0
  104. package/scripts/demo-film/encoder.ts +99 -0
  105. package/scripts/demo-film/film.testkit.tsx +23 -0
  106. package/scripts/demo-film/frames.ts +47 -0
  107. package/scripts/demo-film/index.ts +17 -0
  108. package/scripts/demo-film/mp4.ts +62 -0
  109. package/scripts/demo-film/pageGlobals.d.ts +21 -0
  110. package/scripts/demo-film/render.test.ts +133 -0
  111. package/scripts/demo-film/renderFilm.ts +53 -0
  112. package/scripts/demo-film/renderStill.ts +30 -0
  113. package/scripts/demo-film/session.ts +66 -0
  114. package/scripts/demo-film/site.ts +97 -0
  115. package/scripts/demo-film/types.ts +63 -0
@@ -0,0 +1,62 @@
1
+ import type { Browser, Page } from 'puppeteer-core'
2
+
3
+ const READ_CHUNK = 3 * 1024 * 1024
4
+
5
+ export interface Mp4Options {
6
+ width: number
7
+ height: number
8
+ fps: number
9
+ bitrate: number
10
+ }
11
+
12
+ /**
13
+ * The Bun-side handle of the encoder page: frames go in as JPEGs, WebCodecs (H.264) and mp4-muxer
14
+ * inside Chrome turn them into an MP4, and `finish` streams the bytes back to disk in slices.
15
+ */
16
+ export class Mp4Encoder {
17
+ private constructor(private readonly page: Page) {}
18
+
19
+ static async open(browser: Browser, origin: string, options: Mp4Options): Promise<Mp4Encoder> {
20
+ const page = await (await browser.createBrowserContext()).newPage()
21
+ page.on('pageerror', (error) => {
22
+ console.error(`[encoder] ${error instanceof Error ? error.message : String(error)}`)
23
+ })
24
+ await page.goto(`${origin}/encoder.html`)
25
+ await page.waitForFunction(() => window.__encoder !== undefined)
26
+ await page.evaluate(async (settings) => {
27
+ if (window.__encoder === undefined) throw new Error('The encoder page is not ready')
28
+ await window.__encoder.start(settings)
29
+ }, options)
30
+ return new Mp4Encoder(page)
31
+ }
32
+
33
+ async addFrame(jpeg: Uint8Array, index: number): Promise<void> {
34
+ await this.page.evaluate(
35
+ async (data, at) => {
36
+ if (window.__encoder === undefined) throw new Error('The encoder page is not ready')
37
+ await window.__encoder.addFrame(data, at)
38
+ },
39
+ Buffer.from(jpeg).toString('base64'),
40
+ index
41
+ )
42
+ }
43
+
44
+ /** Finishes the file and writes it to `path`; returns its size in bytes. */
45
+ async finish(path: string): Promise<number> {
46
+ const size = await this.page.evaluate(async () => {
47
+ if (window.__encoder === undefined) throw new Error('The encoder page is not ready')
48
+ return window.__encoder.finish()
49
+ })
50
+ const writer = Bun.file(path).writer()
51
+ for (let offset = 0; offset < size; offset += READ_CHUNK) {
52
+ const base64 = await this.page.evaluate(
53
+ (from, length) => window.__encoder?.read(from, length) ?? '',
54
+ offset,
55
+ READ_CHUNK
56
+ )
57
+ writer.write(Buffer.from(base64, 'base64'))
58
+ }
59
+ await writer.end()
60
+ return size
61
+ }
62
+ }
@@ -0,0 +1,21 @@
1
+ /** What the encoder page offers the renderer (encoder.ts). */
2
+ interface EncoderStartOptions {
3
+ width: number
4
+ height: number
5
+ fps: number
6
+ bitrate: number
7
+ }
8
+
9
+ interface EncoderHost {
10
+ start: (options: EncoderStartOptions) => Promise<void>
11
+ /** Encodes one frame given as a base64 JPEG. Frames must arrive in order. */
12
+ addFrame: (jpegBase64: string, index: number) => Promise<void>
13
+ /** Flushes the encoder, writes the MP4 and returns its size in bytes. */
14
+ finish: () => Promise<number>
15
+ /** A slice of the finished MP4 as base64. */
16
+ read: (offset: number, length: number) => string
17
+ }
18
+
19
+ interface Window {
20
+ __encoder?: EncoderHost
21
+ }
@@ -0,0 +1,133 @@
1
+ import { afterAll, describe, expect, test } from 'bun:test'
2
+ import {
3
+ existsSync,
4
+ mkdirSync,
5
+ mkdtempSync,
6
+ readFileSync,
7
+ rmSync,
8
+ statSync,
9
+ writeFileSync,
10
+ } from 'node:fs'
11
+ import { tmpdir } from 'node:os'
12
+ import { join } from 'node:path'
13
+ import { renderFilm, renderStill, resolveChromePath, runAssetBase } from '.'
14
+ import { serveSite } from './site'
15
+
16
+ /** A 1x1 grey PNG: the capture every frame of the test run points at. */
17
+ const PNG = Buffer.from(
18
+ 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==',
19
+ 'base64'
20
+ )
21
+
22
+ const root = mkdtempSync(join(tmpdir(), 'nucleus-demo-film-test-'))
23
+ const publicDir = join(root, 'public')
24
+ const run = join(publicDir, 'run1')
25
+ mkdirSync(join(run, 'frames'), { recursive: true })
26
+ writeFileSync(join(run, 'frames', 'a.png'), PNG)
27
+ const timelinePath = join(run, 'timeline.json')
28
+ const view = { width: 390, height: 844, scale: 1 }
29
+ writeFileSync(
30
+ timelinePath,
31
+ JSON.stringify({
32
+ version: 1,
33
+ locale: 'en',
34
+ theme: 'dark',
35
+ recordedAt: '2026-11-14T00:00:00Z',
36
+ viewports: { guest: view, admin: view },
37
+ beats: [
38
+ {
39
+ id: 'b01',
40
+ caption: { en: 'One' },
41
+ frames: { guest: { image: 'frames/a.png', highlight: null } },
42
+ focus: 'guest',
43
+ seconds: 0.2,
44
+ },
45
+ { id: 'b02', caption: { en: 'Two' }, frames: {}, focus: 'guest', seconds: 0.2 },
46
+ ],
47
+ })
48
+ )
49
+ afterAll(() => rmSync(root, { recursive: true, force: true }))
50
+
51
+ describe('renderer helpers', () => {
52
+ test('the asset base is the timeline folder inside the public folder', () => {
53
+ expect(runAssetBase(publicDir, timelinePath)).toBe('/assets/run1')
54
+ expect(runAssetBase(run, timelinePath)).toBe('/assets')
55
+ expect(() => runAssetBase(join(publicDir, 'other'), timelinePath)).toThrow('not inside')
56
+ })
57
+
58
+ test('the Chrome path is the option, then CHROME_PATH, then the platform default', () => {
59
+ expect(resolveChromePath('/x/chrome')).toBe('/x/chrome')
60
+ const saved = process.env.CHROME_PATH
61
+ process.env.CHROME_PATH = '/env/chrome'
62
+ expect(resolveChromePath()).toBe('/env/chrome')
63
+ if (saved === undefined) delete process.env.CHROME_PATH
64
+ else process.env.CHROME_PATH = saved
65
+ })
66
+
67
+ test('the server serves the input and the runs, and refuses to leave either folder', async () => {
68
+ const siteDir = join(root, 'site')
69
+ mkdirSync(siteDir, { recursive: true })
70
+ writeFileSync(join(siteDir, 'composition.html'), '<p>page</p>')
71
+ writeFileSync(join(root, 'secret.txt'), 'secret')
72
+ const input = {
73
+ composition: 'Stage',
74
+ timeline: { version: 1, beats: [{ seconds: 1 }] },
75
+ assetBase: '/assets/run1',
76
+ fps: 30,
77
+ width: 640,
78
+ height: 360,
79
+ }
80
+ const site = serveSite(siteDir, publicDir, input)
81
+ try {
82
+ expect(await (await fetch(`${site.origin}/__input.json`)).json()).toEqual(input)
83
+ expect(await (await fetch(`${site.origin}/composition.html`)).text()).toBe('<p>page</p>')
84
+ expect((await fetch(`${site.origin}/assets/run1/frames/a.png`)).status).toBe(200)
85
+ expect((await fetch(`${site.origin}/assets/run1/nope.png`)).status).toBe(404)
86
+ expect((await fetch(`${site.origin}/assets/..%2Fsecret.txt`)).status).toBe(403)
87
+ expect((await fetch(`${site.origin}/..%2Fsecret.txt`)).status).toBe(403)
88
+ } finally {
89
+ await site.stop()
90
+ }
91
+ })
92
+ })
93
+
94
+ const chrome = (() => {
95
+ try {
96
+ return existsSync(resolveChromePath()) ? resolveChromePath() : null
97
+ } catch {
98
+ return null
99
+ }
100
+ })()
101
+
102
+ describe.skipIf(chrome === null)('render with real Chrome', () => {
103
+ const source = {
104
+ entry: join(import.meta.dir, 'film.testkit.tsx'),
105
+ timelinePath,
106
+ publicDir,
107
+ composition: 'Stage',
108
+ size: { width: 640, height: 360 },
109
+ reactRoot: import.meta.dir,
110
+ }
111
+
112
+ test('a still is a PNG of the composition', async () => {
113
+ const [still] = await renderStill({ ...source, outDir: join(root, 'stills'), seconds: [0.1] })
114
+ expect(still).toBeDefined()
115
+ const bytes = readFileSync(still ?? '')
116
+ expect(bytes.subarray(1, 4).toString()).toBe('PNG')
117
+ expect(bytes.byteLength).toBeGreaterThan(1000)
118
+ }, 120_000)
119
+
120
+ test('a film is an MP4 with one frame per 1/30 s', async () => {
121
+ const progress: number[] = []
122
+ const result = await renderFilm({
123
+ ...source,
124
+ outPath: join(root, 'out', 'film.mp4'),
125
+ concurrency: 2,
126
+ onProgress: (done) => progress.push(done),
127
+ })
128
+ expect(result.frames).toBe(12)
129
+ expect(progress.at(-1)).toBe(12)
130
+ expect(statSync(result.outPath).size).toBe(result.bytes)
131
+ expect(readFileSync(result.outPath).subarray(4, 8).toString()).toBe('ftyp')
132
+ }, 120_000)
133
+ })
@@ -0,0 +1,53 @@
1
+ import { mkdirSync } from 'node:fs'
2
+ import { dirname } from 'node:path'
3
+ import { filmLength } from './chrome'
4
+ import { captureFrames } from './frames'
5
+ import { Mp4Encoder } from './mp4'
6
+ import { openSession } from './session'
7
+ import type { RenderFilmOptions, RenderFilmResult } from './types'
8
+
9
+ const DEFAULT_BITRATE = 8_000_000
10
+ const DEFAULT_CONCURRENCY = 4
11
+
12
+ /**
13
+ * Renders a film to MP4 with no ffmpeg: bundles the app's entry, opens it in headless Chrome (several
14
+ * pages at once), screenshots every frame after `window.__film.seek(frame)` and feeds the JPEGs to an
15
+ * encoder page (WebCodecs H.264 and mp4-muxer). Why screenshots: Chrome has no stable way to rasterise
16
+ * live DOM into a canvas, while CDP screenshots are exactly what the compositor paints.
17
+ * Needs `puppeteer-core` and `mp4-muxer` installed in the app.
18
+ */
19
+ export async function renderFilm(options: RenderFilmOptions): Promise<RenderFilmResult> {
20
+ const started = performance.now()
21
+ const session = await openSession(options)
22
+ try {
23
+ const concurrency = Math.max(1, Math.floor(options.concurrency ?? DEFAULT_CONCURRENCY))
24
+ const pages = await Promise.all(Array.from({ length: concurrency }, () => session.openPage()))
25
+ const first = pages[0]
26
+ if (first === undefined) throw new Error('No composition page opened')
27
+ const total = await filmLength(first)
28
+ const encoder = await Mp4Encoder.open(session.browser, session.site.origin, {
29
+ ...options.size,
30
+ fps: session.fps,
31
+ bitrate: options.bitrate ?? DEFAULT_BITRATE,
32
+ })
33
+ const bundled = performance.now()
34
+ await captureFrames(
35
+ pages,
36
+ total,
37
+ (jpeg, index) => encoder.addFrame(jpeg, index),
38
+ (done) => options.onProgress?.(done, total)
39
+ )
40
+ mkdirSync(dirname(options.outPath), { recursive: true })
41
+ const bytes = await encoder.finish(options.outPath)
42
+ const finished = performance.now()
43
+ return {
44
+ outPath: options.outPath,
45
+ frames: total,
46
+ bytes,
47
+ bundleSeconds: (bundled - started) / 1000,
48
+ renderSeconds: (finished - bundled) / 1000,
49
+ }
50
+ } finally {
51
+ await session.close()
52
+ }
53
+ }
@@ -0,0 +1,30 @@
1
+ import { mkdirSync } from 'node:fs'
2
+ import { join } from 'node:path'
3
+ import { filmLength, seek } from './chrome'
4
+ import { openSession } from './session'
5
+ import type { RenderStillOptions } from './types'
6
+
7
+ /**
8
+ * Renders PNG stills of a film for a quick look, without the full MP4. Returns the written paths,
9
+ * one per requested second (`<outDir>/<name>-t<second>.png`).
10
+ */
11
+ export async function renderStill(options: RenderStillOptions): Promise<string[]> {
12
+ const session = await openSession(options)
13
+ try {
14
+ const page = await session.openPage()
15
+ const last = (await filmLength(page)) - 1
16
+ mkdirSync(options.outDir, { recursive: true })
17
+ const name = options.name ?? options.composition
18
+ const written: string[] = []
19
+ for (const second of options.seconds) {
20
+ const frame = Math.min(last, Math.max(0, Math.round(second * session.fps)))
21
+ await seek(page, frame)
22
+ const path = join(options.outDir, `${name}-t${second}.png`)
23
+ await page.screenshot({ type: 'png', path })
24
+ written.push(path)
25
+ }
26
+ return written
27
+ } finally {
28
+ await session.close()
29
+ }
30
+ }
@@ -0,0 +1,66 @@
1
+ import { rmSync } from 'node:fs'
2
+ import { dirname, relative, sep } from 'node:path'
3
+ import type { Browser, Page } from 'puppeteer-core'
4
+ import { launchChrome, openComposition, resolveChromePath } from './chrome'
5
+ import { ASSET_PREFIX, buildSite, type Site, serveSite } from './site'
6
+ import type { DemoFilmSource, TimelineHead } from './types'
7
+
8
+ export const DEFAULT_FPS = 30
9
+
10
+ /** A bundled, served, browser-ready film: everything a render or a still needs, and how to tear it down. */
11
+ export interface FilmSession {
12
+ site: Site
13
+ browser: Browser
14
+ fps: number
15
+ /** Opens one more composition page at the film's size. */
16
+ openPage: () => Promise<Page>
17
+ close: () => Promise<void>
18
+ }
19
+
20
+ /** The URL path of the run folder: the timeline's folder inside the public folder. */
21
+ export function runAssetBase(publicDir: string, timelinePath: string): string {
22
+ const inside = relative(publicDir, dirname(timelinePath)).split(sep).filter(Boolean)
23
+ if (inside[0] === '..' || inside.some((part) => part === '..'))
24
+ throw new Error(`${timelinePath} is not inside the public folder ${publicDir}`)
25
+ return [ASSET_PREFIX, ...inside].join('/')
26
+ }
27
+
28
+ async function readTimelineHead(timelinePath: string): Promise<TimelineHead> {
29
+ const timeline: TimelineHead = await Bun.file(timelinePath).json()
30
+ if (timeline.version !== 1 || !Array.isArray(timeline.beats) || timeline.beats.length === 0)
31
+ throw new Error(`${timelinePath} is not a usable timeline`)
32
+ return timeline
33
+ }
34
+
35
+ /** Bundles the entry, serves it with the run, and starts Chrome. */
36
+ export async function openSession(source: DemoFilmSource): Promise<FilmSession> {
37
+ const fps = source.fps ?? DEFAULT_FPS
38
+ const timeline = await readTimelineHead(source.timelinePath)
39
+ const siteDir = await buildSite(source)
40
+ const site = serveSite(siteDir, source.publicDir, {
41
+ composition: source.composition,
42
+ timeline,
43
+ assetBase: source.assetBase ?? runAssetBase(source.publicDir, source.timelinePath),
44
+ fps,
45
+ ...source.size,
46
+ })
47
+ let browser: Browser
48
+ try {
49
+ browser = await launchChrome(resolveChromePath(source.chromePath))
50
+ } catch (error) {
51
+ await site.stop()
52
+ rmSync(siteDir, { recursive: true, force: true })
53
+ throw error
54
+ }
55
+ return {
56
+ site,
57
+ browser,
58
+ fps,
59
+ openPage: () => openComposition(browser, site.origin, source.size),
60
+ close: async () => {
61
+ await browser.close().catch(() => undefined)
62
+ await site.stop()
63
+ rmSync(siteDir, { recursive: true, force: true })
64
+ },
65
+ }
66
+ }
@@ -0,0 +1,97 @@
1
+ import { mkdtempSync, rmSync } from 'node:fs'
2
+ import { tmpdir } from 'node:os'
3
+ import { dirname, join, resolve, sep } from 'node:path'
4
+ import type { BunPlugin } from 'bun'
5
+ import type { DemoFilmSource } from './types'
6
+
7
+ const ENCODER_ENTRY = join(import.meta.dir, 'encoder.ts')
8
+
9
+ /** URL prefix under which the runs folder is served. */
10
+ export const ASSET_PREFIX = '/assets'
11
+
12
+ /**
13
+ * The entry and the product's film code must share one React: hooks and context split in two
14
+ * otherwise. Every `react` and `react-dom` import in the bundle resolves from `root`.
15
+ */
16
+ function sharedReact(root: string): BunPlugin {
17
+ return {
18
+ name: 'shared-react',
19
+ setup(build) {
20
+ build.onResolve({ filter: /^react(-dom)?(\/.*)?$/ }, (args) => ({
21
+ path: Bun.resolveSync(args.path, root),
22
+ }))
23
+ },
24
+ }
25
+ }
26
+
27
+ const page = (title: string, entry: string) =>
28
+ `<!doctype html><html><head><meta charset="utf-8"><title>${title}</title>` +
29
+ `<style>html,body{margin:0;padding:0;background:#000;overflow:hidden}#root{position:absolute;inset:0}</style>` +
30
+ `</head><body><div id="root"></div><script type="module" src="/${entry}"></script></body></html>`
31
+
32
+ /** Bundles the product's entry and the encoder page into a fresh temporary folder (browser target). */
33
+ export async function buildSite(
34
+ source: Pick<DemoFilmSource, 'entry' | 'reactRoot'>
35
+ ): Promise<string> {
36
+ const dir = mkdtempSync(join(tmpdir(), 'nucleus-demo-film-'))
37
+ try {
38
+ const result = await Bun.build({
39
+ entrypoints: [source.entry, ENCODER_ENTRY],
40
+ outdir: dir,
41
+ target: 'browser',
42
+ publicPath: '/',
43
+ naming: { entry: '[name].js', asset: '[name].[ext]' },
44
+ loader: { '.woff2': 'file', '.woff': 'file', '.ttf': 'file' },
45
+ define: { 'process.env.NODE_ENV': '"production"' },
46
+ plugins: [sharedReact(source.reactRoot ?? dirname(source.entry))],
47
+ })
48
+ if (!result.success) throw new Error(`Bundle failed:\n${result.logs.map(String).join('\n')}`)
49
+ await Bun.write(join(dir, 'composition.html'), page('Film', entryFile(source.entry)))
50
+ await Bun.write(join(dir, 'encoder.html'), page('Encoder', 'encoder.js'))
51
+ return dir
52
+ } catch (error) {
53
+ rmSync(dir, { recursive: true, force: true })
54
+ throw error
55
+ }
56
+ }
57
+
58
+ /** The bundle name Bun gives an entry: its file name without extension, plus `.js`. */
59
+ function entryFile(entry: string): string {
60
+ const name = entry.split(/[\\/]/).pop() ?? entry
61
+ return `${name.replace(/\.[^.]+$/, '')}.js`
62
+ }
63
+
64
+ export interface Site {
65
+ origin: string
66
+ stop: () => Promise<void>
67
+ }
68
+
69
+ /** What the composition page fetches from `/__input.json`. */
70
+ export interface ServedInput {
71
+ composition: string
72
+ timeline: { version: number; beats: { seconds: number }[] }
73
+ assetBase: string
74
+ fps: number
75
+ width: number
76
+ height: number
77
+ }
78
+
79
+ /** Serves the bundle, the runs folder (`/assets/...`) and the composition input. */
80
+ export function serveSite(siteDir: string, publicDir: string, input: ServedInput): Site {
81
+ const server = Bun.serve({
82
+ hostname: '127.0.0.1',
83
+ port: 0,
84
+ async fetch(request) {
85
+ const path = decodeURIComponent(new URL(request.url).pathname)
86
+ if (path === '/__input.json') return Response.json(input)
87
+ const [base, tail] = path.startsWith(`${ASSET_PREFIX}/`)
88
+ ? [resolve(publicDir), path.slice(ASSET_PREFIX.length + 1)]
89
+ : [resolve(siteDir), path.slice(1)]
90
+ const file = resolve(base, tail)
91
+ if (!file.startsWith(base + sep)) return new Response('forbidden', { status: 403 })
92
+ const body = Bun.file(file)
93
+ return (await body.exists()) ? new Response(body) : new Response('not found', { status: 404 })
94
+ },
95
+ })
96
+ return { origin: `http://127.0.0.1:${server.port}`, stop: () => server.stop(true) }
97
+ }
@@ -0,0 +1,63 @@
1
+ /** What every render of a demo film needs. */
2
+ export interface DemoFilmSource {
3
+ /**
4
+ * Absolute path of the app's browser entry: a module that calls `mountDemoFilmHost` from
5
+ * `nucleus-core-ts/fe/demo-film-host` with the compositions it can render.
6
+ */
7
+ entry: string
8
+ /** Absolute path of the run's `timeline.json`. */
9
+ timelinePath: string
10
+ /**
11
+ * Absolute path of the folder holding the runs. It is served at `/assets`, and the composition's
12
+ * `assetBase` is the timeline's own folder inside it (`/assets/<run>`), so frame paths resolve the
13
+ * same way as on the public page.
14
+ */
15
+ publicDir: string
16
+ /** The key of the composition to render, as the entry registers it. */
17
+ composition: string
18
+ /** Composition size in px. */
19
+ size: { width: number; height: number }
20
+ /** Frames per second; 30 by default. */
21
+ fps?: number
22
+ /** Chrome executable; `CHROME_PATH`, else the usual install path of the platform. */
23
+ chromePath?: string
24
+ /** Replaces the `/assets/<run>` the composition receives, for entries that expect another layout. */
25
+ assetBase?: string
26
+ /** Resolve `react` and `react-dom` for the bundle from this folder (default: the entry's folder). */
27
+ reactRoot?: string
28
+ }
29
+
30
+ export interface RenderFilmOptions extends DemoFilmSource {
31
+ /** Absolute path of the MP4 to write. */
32
+ outPath: string
33
+ /** Pages capturing at once; 4 by default. */
34
+ concurrency?: number
35
+ /** H.264 bitrate in bits per second; 8 Mbps by default. */
36
+ bitrate?: number
37
+ /** Called after each batch of frames reaches the encoder. */
38
+ onProgress?: (done: number, total: number) => void
39
+ }
40
+
41
+ export interface RenderFilmResult {
42
+ outPath: string
43
+ frames: number
44
+ bytes: number
45
+ /** Seconds spent bundling and starting Chrome, and capturing and encoding. */
46
+ bundleSeconds: number
47
+ renderSeconds: number
48
+ }
49
+
50
+ export interface RenderStillOptions extends DemoFilmSource {
51
+ /** Folder the PNGs are written to. */
52
+ outDir: string
53
+ /** Film seconds to capture. */
54
+ seconds: number[]
55
+ /** File name prefix; the composition's name by default. */
56
+ name?: string
57
+ }
58
+
59
+ /** The timeline fields the renderer itself reads; the rest belongs to the product. */
60
+ export interface TimelineHead {
61
+ version: number
62
+ beats: { seconds: number }[]
63
+ }