@blackbelt-technology/pi-dashboard-deck3d 0.0.0-stage → 0.9.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.
- package/.pi/skills/deck3d/SKILL.md +227 -0
- package/.pi/skills/deck3d/examples/deck.json +388 -0
- package/.pi/skills/deck3d/examples/fixture.md +18 -0
- package/.pi/skills/deck3d/examples/snapshot-after.png +0 -0
- package/.pi/skills/deck3d/examples/snapshot-before.png +0 -0
- package/.pi/skills/deck3d/reference/effects.agent.md +78 -0
- package/.pi/skills/deck3d/reference/effects.md +1217 -0
- package/.pi/skills/deck3d/reference/ir-fields.agent.md +30 -0
- package/.pi/skills/deck3d/reference/ir-fields.md +246 -0
- package/README.md +177 -2
- package/assets/LICENSE-Poppins.txt +99 -0
- package/assets/Poppins-Bold.ttf +0 -0
- package/assets/props/LICENSES.txt +4 -0
- package/assets/props/brain.glb +0 -0
- package/assets/props/cloud.glb +0 -0
- package/assets/props/cube.glb +0 -0
- package/assets/props/database.glb +0 -0
- package/assets/props/laptop.glb +0 -0
- package/assets/props/manifest.json +119 -0
- package/assets/props/robot.glb +0 -0
- package/assets/props/server.glb +0 -0
- package/assets/props/shield.glb +0 -0
- package/bin/deck3d +49 -0
- package/dist/cli.js +10228 -0
- package/dist/harvest/harness.js +185718 -0
- package/dist/runtime.js +64597 -0
- package/fixtures/business-2031/build-deck.mjs +60 -0
- package/fixtures/business-2031/build-overrides.mjs +154 -0
- package/fixtures/business-2031/deck.json +4220 -0
- package/fixtures/business-2031/deck.md +470 -0
- package/fixtures/business-2031/fx/closing-mark.js +43 -0
- package/fixtures/business-2031/fx/closing-mark.meta.json +26 -0
- package/fixtures/business-2031/overrides.json +903 -0
- package/fixtures/harvest.md +23 -0
- package/fixtures/strategy-lab.json +891 -0
- package/fixtures/strategy-lab.md +85 -0
- package/package.json +80 -4
- package/scripts/build-cli.mjs +29 -0
- package/scripts/build-harvest.ts +8 -0
- package/scripts/build-runtime.mjs +22 -0
- package/scripts/gen-effects.ts +14 -0
- package/scripts/gen-ir-fields.ts +34 -0
- package/src/check/index.ts +315 -0
- package/src/check/rules.ts +393 -0
- package/src/cli-io.ts +23 -0
- package/src/cli.ts +823 -0
- package/src/fx/accent-cycle.meta.json +26 -0
- package/src/fx/accent-cycle.ts +8 -0
- package/src/fx/agent-depth.meta.json +42 -0
- package/src/fx/agent-depth.ts +62 -0
- package/src/fx/ascii.meta.json +35 -0
- package/src/fx/ascii.ts +4 -0
- package/src/fx/aurora.meta.json +26 -0
- package/src/fx/aurora.ts +18 -0
- package/src/fx/billboards.meta.json +34 -0
- package/src/fx/billboards.ts +67 -0
- package/src/fx/bloom.meta.json +26 -0
- package/src/fx/bloom.ts +4 -0
- package/src/fx/camera-drift.meta.json +26 -0
- package/src/fx/camera-drift.ts +8 -0
- package/src/fx/capital-flows.meta.json +30 -0
- package/src/fx/capital-flows.ts +64 -0
- package/src/fx/catalogue.ts +89 -0
- package/src/fx/chromatic-aberration.meta.json +32 -0
- package/src/fx/chromatic-aberration.ts +4 -0
- package/src/fx/city-grid.meta.json +37 -0
- package/src/fx/city-grid.ts +51 -0
- package/src/fx/clipped-solids.meta.json +49 -0
- package/src/fx/clipped-solids.ts +171 -0
- package/src/fx/cohort-grid.meta.json +48 -0
- package/src/fx/cohort-grid.ts +63 -0
- package/src/fx/compose.ts +114 -0
- package/src/fx/constellation.meta.json +51 -0
- package/src/fx/constellation.ts +103 -0
- package/src/fx/curve-flow.meta.json +40 -0
- package/src/fx/curve-flow.ts +63 -0
- package/src/fx/dashed-flow.meta.json +27 -0
- package/src/fx/dashed-flow.ts +8 -0
- package/src/fx/data-columns.meta.json +30 -0
- package/src/fx/data-columns.ts +18 -0
- package/src/fx/defaults.ts +92 -0
- package/src/fx/depth-of-field.meta.json +40 -0
- package/src/fx/depth-of-field.ts +4 -0
- package/src/fx/dolly.meta.json +26 -0
- package/src/fx/dolly.ts +4 -0
- package/src/fx/dot-screen.meta.json +35 -0
- package/src/fx/dot-screen.ts +4 -0
- package/src/fx/dynamic-instances.meta.json +40 -0
- package/src/fx/dynamic-instances.ts +57 -0
- package/src/fx/emissive.meta.json +26 -0
- package/src/fx/emissive.ts +8 -0
- package/src/fx/extruded-shapes.meta.json +62 -0
- package/src/fx/extruded-shapes.ts +133 -0
- package/src/fx/fab-wafer.meta.json +42 -0
- package/src/fx/fab-wafer.ts +72 -0
- package/src/fx/fade.meta.json +26 -0
- package/src/fx/fade.ts +4 -0
- package/src/fx/film.meta.json +26 -0
- package/src/fx/film.ts +4 -0
- package/src/fx/float.meta.json +26 -0
- package/src/fx/float.ts +8 -0
- package/src/fx/flythrough.meta.json +26 -0
- package/src/fx/flythrough.ts +4 -0
- package/src/fx/fog.meta.json +26 -0
- package/src/fx/fog.ts +8 -0
- package/src/fx/geo-fragments.meta.json +48 -0
- package/src/fx/geo-fragments.ts +59 -0
- package/src/fx/glass.meta.json +26 -0
- package/src/fx/glass.ts +8 -0
- package/src/fx/globe-arcs.meta.json +36 -0
- package/src/fx/globe-arcs.ts +67 -0
- package/src/fx/glow-tube.meta.json +26 -0
- package/src/fx/glow-tube.ts +8 -0
- package/src/fx/glyph-rain.meta.json +30 -0
- package/src/fx/glyph-rain.ts +18 -0
- package/src/fx/god-rays.meta.json +40 -0
- package/src/fx/god-rays.ts +4 -0
- package/src/fx/grid-horizon.meta.json +30 -0
- package/src/fx/grid-horizon.ts +18 -0
- package/src/fx/hex-grid.meta.json +29 -0
- package/src/fx/hex-grid.ts +18 -0
- package/src/fx/holo-fresnel.meta.json +26 -0
- package/src/fx/holo-fresnel.ts +8 -0
- package/src/fx/horizon-gates.meta.json +36 -0
- package/src/fx/horizon-gates.ts +60 -0
- package/src/fx/index.ts +258 -0
- package/src/fx/iridescent.meta.json +26 -0
- package/src/fx/iridescent.ts +8 -0
- package/src/fx/iris.meta.json +26 -0
- package/src/fx/iris.ts +4 -0
- package/src/fx/lightformers.meta.json +26 -0
- package/src/fx/lightformers.ts +8 -0
- package/src/fx/local.ts +200 -0
- package/src/fx/market-tape.meta.json +53 -0
- package/src/fx/market-tape.ts +97 -0
- package/src/fx/matcap.meta.json +26 -0
- package/src/fx/matcap.ts +8 -0
- package/src/fx/meta.schema.json +49 -0
- package/src/fx/metal.meta.json +26 -0
- package/src/fx/metal.ts +8 -0
- package/src/fx/mirror-floor.meta.json +26 -0
- package/src/fx/mirror-floor.ts +8 -0
- package/src/fx/neural-mesh.meta.json +54 -0
- package/src/fx/neural-mesh.ts +110 -0
- package/src/fx/orbit-agents.meta.json +36 -0
- package/src/fx/orbit-agents.ts +63 -0
- package/src/fx/orbit.meta.json +27 -0
- package/src/fx/orbit.ts +8 -0
- package/src/fx/outline.meta.json +43 -0
- package/src/fx/outline.ts +4 -0
- package/src/fx/paper-stack.meta.json +36 -0
- package/src/fx/paper-stack.ts +61 -0
- package/src/fx/particle-stream.meta.json +27 -0
- package/src/fx/particle-stream.ts +8 -0
- package/src/fx/particles.meta.json +26 -0
- package/src/fx/particles.ts +8 -0
- package/src/fx/pixelate.meta.json +28 -0
- package/src/fx/pixelate.ts +4 -0
- package/src/fx/points-on-geometry.meta.json +61 -0
- package/src/fx/points-on-geometry.ts +71 -0
- package/src/fx/proof-gate.meta.json +30 -0
- package/src/fx/proof-gate.ts +67 -0
- package/src/fx/rings.meta.json +27 -0
- package/src/fx/rings.ts +8 -0
- package/src/fx/room-ibl.meta.json +26 -0
- package/src/fx/room-ibl.ts +8 -0
- package/src/fx/sao.meta.json +32 -0
- package/src/fx/sao.ts +4 -0
- package/src/fx/scaffold.ts +172 -0
- package/src/fx/scatter.meta.json +49 -0
- package/src/fx/scatter.ts +74 -0
- package/src/fx/selective-bloom.meta.json +36 -0
- package/src/fx/selective-bloom.ts +4 -0
- package/src/fx/server-racks.meta.json +36 -0
- package/src/fx/server-racks.ts +54 -0
- package/src/fx/shader-particles.meta.json +46 -0
- package/src/fx/shader-particles.ts +75 -0
- package/src/fx/signal-pulse.meta.json +27 -0
- package/src/fx/signal-pulse.ts +8 -0
- package/src/fx/smaa.meta.json +19 -0
- package/src/fx/smaa.ts +4 -0
- package/src/fx/sobel.meta.json +24 -0
- package/src/fx/sobel.ts +4 -0
- package/src/fx/soft-shadows.meta.json +26 -0
- package/src/fx/soft-shadows.ts +8 -0
- package/src/fx/sprites.meta.json +40 -0
- package/src/fx/sprites.ts +57 -0
- package/src/fx/stagger-reveal.meta.json +26 -0
- package/src/fx/stagger-reveal.ts +8 -0
- package/src/fx/starfield.meta.json +32 -0
- package/src/fx/starfield.ts +18 -0
- package/src/fx/swarm.meta.json +27 -0
- package/src/fx/swarm.ts +8 -0
- package/src/fx/tessellate.meta.json +39 -0
- package/src/fx/tessellate.ts +79 -0
- package/src/fx/tokens.meta.json +27 -0
- package/src/fx/tokens.ts +8 -0
- package/src/fx/trail.meta.json +27 -0
- package/src/fx/trail.ts +8 -0
- package/src/fx/trust-ledger.meta.json +30 -0
- package/src/fx/trust-ledger.ts +47 -0
- package/src/fx/types.ts +83 -0
- package/src/fx/util.ts +108 -0
- package/src/fx/vault-glyphs.meta.json +37 -0
- package/src/fx/vault-glyphs.ts +55 -0
- package/src/fx/video-screen.meta.json +57 -0
- package/src/fx/video-screen.ts +99 -0
- package/src/fx/vignette.meta.json +32 -0
- package/src/fx/vignette.ts +4 -0
- package/src/fx/volume-cloud.meta.json +58 -0
- package/src/fx/volume-cloud.ts +87 -0
- package/src/fx/volume-perlin.meta.json +46 -0
- package/src/fx/volume-perlin.ts +82 -0
- package/src/fx/volumetric-spot.meta.json +26 -0
- package/src/fx/volumetric-spot.ts +8 -0
- package/src/fx/wireframe-overlay.meta.json +27 -0
- package/src/fx/wireframe-overlay.ts +8 -0
- package/src/ir/defaults.ts +62 -0
- package/src/ir/field-reference.ts +96 -0
- package/src/ir/hash.ts +34 -0
- package/src/ir/ids.ts +65 -0
- package/src/ir/merge.ts +155 -0
- package/src/ir/schema.json +1211 -0
- package/src/ir/types.ts +333 -0
- package/src/ir/validate.ts +196 -0
- package/src/parse/derive.ts +205 -0
- package/src/parse/harvest/bundle.ts +56 -0
- package/src/parse/harvest/harness.html +22 -0
- package/src/parse/harvest/harness.ts +191 -0
- package/src/parse/harvest/index.ts +169 -0
- package/src/parse/harvest/shapes.ts +75 -0
- package/src/parse/markdown.ts +215 -0
- package/src/props/embed.ts +92 -0
- package/src/props/fetch.ts +97 -0
- package/src/props/generate.ts +152 -0
- package/src/props/key.ts +13 -0
- package/src/props/search.ts +180 -0
- package/src/render/embed-video.ts +80 -0
- package/src/render/font.ts +86 -0
- package/src/render/index.ts +149 -0
- package/src/render/template.html +87 -0
- package/src/runtime/backgrounds.ts +146 -0
- package/src/runtime/builders.ts +808 -0
- package/src/runtime/camera.ts +73 -0
- package/src/runtime/hud.ts +887 -0
- package/src/runtime/index.ts +1168 -0
- package/src/runtime/local-fx.ts +151 -0
- package/src/runtime/materials.ts +136 -0
- package/src/runtime/measure.ts +73 -0
- package/src/runtime/node-geometry.ts +26 -0
- package/src/runtime/palette.ts +111 -0
- package/src/runtime/post.ts +649 -0
- package/src/runtime/props.ts +292 -0
- package/src/runtime/quality.ts +20 -0
- package/src/runtime/rng.ts +8 -0
- package/src/runtime/scene.ts +644 -0
- package/src/runtime/text.ts +174 -0
- package/src/runtime/types.ts +118 -0
- package/src/serve/index.ts +376 -0
- package/src/util/paths.ts +35 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text: extruded titles (opentype.js TTF → ShapePath → ExtrudeGeometry) and
|
|
3
|
+
* canvas labels with an inverted background-colour outline (design D4).
|
|
4
|
+
*
|
|
5
|
+
* Finding ported from the lab: every three.js example `typeface.json` ships
|
|
6
|
+
* corrupt double-acute outlines (`ő ű Ő Ű`) → earcut streaks. A real TTF via
|
|
7
|
+
* opentype.js is the only clean path, so there is no typeface fallback.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { Font } from "opentype.js";
|
|
11
|
+
import opentype from "opentype.js";
|
|
12
|
+
import * as THREE from "three";
|
|
13
|
+
import type { PaletteColors } from "./palette.js";
|
|
14
|
+
|
|
15
|
+
const LABEL_FONT = "Poppins, sans-serif";
|
|
16
|
+
|
|
17
|
+
/** Parse the embedded TTF (base64) into an opentype Font. */
|
|
18
|
+
export function loadFont(base64: string): Font {
|
|
19
|
+
const bytes = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
|
|
20
|
+
return opentype.parse(bytes.buffer);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Greedy word-wrap. */
|
|
24
|
+
export function wrap(text: string, max = 18): string[] {
|
|
25
|
+
const out: string[] = [];
|
|
26
|
+
let line = "";
|
|
27
|
+
for (const w of text.split(" ")) {
|
|
28
|
+
if (`${line} ${w}`.trim().length > max) {
|
|
29
|
+
out.push(line.trim());
|
|
30
|
+
line = w;
|
|
31
|
+
} else {
|
|
32
|
+
line += ` ${w}`;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
out.push(line.trim());
|
|
36
|
+
return out;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** opentype outline → THREE.Shape[] (Y flipped: opentype is Y-down). */
|
|
40
|
+
export function ttfShapes(font: Font, text: string, size: number): THREE.Shape[] {
|
|
41
|
+
const sp = new THREE.ShapePath();
|
|
42
|
+
for (const c of font.getPath(text, 0, 0, size).commands) {
|
|
43
|
+
if (c.type === "M") sp.moveTo(c.x, -c.y);
|
|
44
|
+
else if (c.type === "L") sp.lineTo(c.x, -c.y);
|
|
45
|
+
else if (c.type === "Q") sp.quadraticCurveTo(c.x1, -c.y1, c.x, -c.y);
|
|
46
|
+
else if (c.type === "C") sp.bezierCurveTo(c.x1, -c.y1, c.x2, -c.y2, c.x, -c.y);
|
|
47
|
+
else if (c.type === "Z") sp.currentPath?.closePath();
|
|
48
|
+
}
|
|
49
|
+
return sp.toShapes(false);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface TitleResult {
|
|
53
|
+
group: THREE.Group;
|
|
54
|
+
missing: Set<string>;
|
|
55
|
+
lines: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Extruded hero text (multi-line, wrapped).
|
|
60
|
+
*
|
|
61
|
+
* `mat` may be a two-entry array: `ExtrudeGeometry` emits one group pair per
|
|
62
|
+
* glyph shape — material 0 = the front/back faces, material 1 = the extrusion
|
|
63
|
+
* walls plus the bevel — so `[face, edge]` draws a contour around every
|
|
64
|
+
* character (`defaults.titleEdge`).
|
|
65
|
+
*/
|
|
66
|
+
export function buildTitle(
|
|
67
|
+
font: Font,
|
|
68
|
+
text: string,
|
|
69
|
+
size: number,
|
|
70
|
+
depth: number,
|
|
71
|
+
mat: THREE.Material | THREE.Material[],
|
|
72
|
+
): TitleResult {
|
|
73
|
+
const group = new THREE.Group();
|
|
74
|
+
const missing = new Set<string>();
|
|
75
|
+
const ex = {
|
|
76
|
+
depth,
|
|
77
|
+
curveSegments: 8,
|
|
78
|
+
bevelEnabled: true,
|
|
79
|
+
bevelThickness: depth * 0.15,
|
|
80
|
+
bevelSize: size * 0.025,
|
|
81
|
+
bevelSegments: 3,
|
|
82
|
+
};
|
|
83
|
+
const lines = wrap(text, size > 0.3 ? 18 : 40);
|
|
84
|
+
lines.forEach((line, li) => {
|
|
85
|
+
for (const ch of line) if (ch !== " " && !font.hasChar(ch)) missing.add(ch);
|
|
86
|
+
const mesh = new THREE.Mesh(new THREE.ExtrudeGeometry(ttfShapes(font, line, size), ex), mat);
|
|
87
|
+
mesh.castShadow = true;
|
|
88
|
+
mesh.position.y = -li * size * 1.35;
|
|
89
|
+
group.add(mesh);
|
|
90
|
+
});
|
|
91
|
+
return { group, missing, lines: lines.length };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface LabelHandle {
|
|
95
|
+
group: THREE.Group;
|
|
96
|
+
/** World height of the label plane (for measurement). */
|
|
97
|
+
height: number;
|
|
98
|
+
canvasWidth: number;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Flat, unlit canvas label with a background-colour outline — readable at any
|
|
103
|
+
* size in both modes, never blooms (`toneMapped:false`).
|
|
104
|
+
*/
|
|
105
|
+
export function buildLabel(str: string, size: number, P: PaletteColors): LabelHandle {
|
|
106
|
+
const px = 96;
|
|
107
|
+
const pad = 18;
|
|
108
|
+
const canvas = document.createElement("canvas");
|
|
109
|
+
const ctx = canvas.getContext("2d") as CanvasRenderingContext2D;
|
|
110
|
+
const fontSpec = `700 ${px}px ${LABEL_FONT}`;
|
|
111
|
+
ctx.font = fontSpec;
|
|
112
|
+
const w = Math.ceil(ctx.measureText(str).width) + pad * 2;
|
|
113
|
+
canvas.width = w;
|
|
114
|
+
canvas.height = px + pad * 2;
|
|
115
|
+
ctx.font = fontSpec;
|
|
116
|
+
ctx.textBaseline = "middle";
|
|
117
|
+
ctx.lineJoin = "round";
|
|
118
|
+
ctx.lineWidth = px * 0.18;
|
|
119
|
+
ctx.strokeStyle = P.bg;
|
|
120
|
+
ctx.fillStyle = P.text;
|
|
121
|
+
ctx.strokeText(str, pad, canvas.height / 2);
|
|
122
|
+
ctx.fillText(str, pad, canvas.height / 2);
|
|
123
|
+
const tex = new THREE.CanvasTexture(canvas);
|
|
124
|
+
tex.colorSpace = THREE.SRGBColorSpace;
|
|
125
|
+
tex.anisotropy = 8;
|
|
126
|
+
const h = size * 1.15;
|
|
127
|
+
const plane = new THREE.Mesh(
|
|
128
|
+
new THREE.PlaneGeometry((h * canvas.width) / canvas.height, h),
|
|
129
|
+
// `depthTest: false`: a caption is an annotation, not scenery. With depth
|
|
130
|
+
// on, the plinth swallowed the bottom caption and the globe swallowed its
|
|
131
|
+
// own far-side ones. Every caller is a diagram label (builders.ts only),
|
|
132
|
+
// so this cannot lift chrome or body text out of the depth order.
|
|
133
|
+
new THREE.MeshBasicMaterial({ map: tex, transparent: true, depthWrite: false, depthTest: false, toneMapped: false }),
|
|
134
|
+
);
|
|
135
|
+
plane.renderOrder = 12;
|
|
136
|
+
const group = new THREE.Group();
|
|
137
|
+
group.add(plane);
|
|
138
|
+
return { group, height: h, canvasWidth: canvas.width };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Bullet panel texture (subtitle + accent bullets). */
|
|
142
|
+
export function bulletTexture(
|
|
143
|
+
slide: { subtitle?: string; bullets: string[] },
|
|
144
|
+
P: PaletteColors,
|
|
145
|
+
): THREE.CanvasTexture {
|
|
146
|
+
const c = document.createElement("canvas");
|
|
147
|
+
c.width = 1024;
|
|
148
|
+
c.height = 512;
|
|
149
|
+
const g = c.getContext("2d") as CanvasRenderingContext2D;
|
|
150
|
+
g.textBaseline = "top";
|
|
151
|
+
let y = 20;
|
|
152
|
+
if (slide.subtitle) {
|
|
153
|
+
g.globalAlpha = 0.8;
|
|
154
|
+
g.font = "400 40px sans-serif";
|
|
155
|
+
g.fillStyle = P.text;
|
|
156
|
+
g.fillText(slide.subtitle, 0, y);
|
|
157
|
+
g.globalAlpha = 1;
|
|
158
|
+
y += 80;
|
|
159
|
+
}
|
|
160
|
+
g.font = "500 38px sans-serif";
|
|
161
|
+
for (const b of slide.bullets) {
|
|
162
|
+
g.fillStyle = P.accent;
|
|
163
|
+
g.beginPath();
|
|
164
|
+
g.arc(14, y + 20, 8, 0, 7);
|
|
165
|
+
g.fill();
|
|
166
|
+
g.fillStyle = P.text;
|
|
167
|
+
g.fillText(b, 44, y);
|
|
168
|
+
y += 72;
|
|
169
|
+
}
|
|
170
|
+
const t = new THREE.CanvasTexture(c);
|
|
171
|
+
t.colorSpace = THREE.SRGBColorSpace;
|
|
172
|
+
t.anisotropy = 8;
|
|
173
|
+
return t;
|
|
174
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deck runtime config — the merged-IR view the browser engine reads.
|
|
3
|
+
*
|
|
4
|
+
* Structural copies of `DeckIR` types; the runtime bundle is compiled with
|
|
5
|
+
* esbuild from `src/runtime/index.ts` and receives `window.__DECK`.
|
|
6
|
+
*/
|
|
7
|
+
import type { CardOffset, Defaults, EffectRef, MergedSlide, PropOverride } from "../ir/types.js";
|
|
8
|
+
|
|
9
|
+
export interface RuntimeDeck {
|
|
10
|
+
defaults: Defaults;
|
|
11
|
+
slides: MergedSlide[];
|
|
12
|
+
/**
|
|
13
|
+
* Deck-level effect list. `applyOverrides` PREPENDS it to every slide's
|
|
14
|
+
* effects, so the panel needs it to tell an inherited slot from the slide's
|
|
15
|
+
* own. Already embedded (the whole `MergedDeck` is) — only undeclared.
|
|
16
|
+
*/
|
|
17
|
+
effects?: EffectRef[];
|
|
18
|
+
/** Prop placements from `overrides.props` (design D7). */
|
|
19
|
+
props?: PropOverride[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Deck defaults with one slide's overrides folded in (incl. slide-only knobs). */
|
|
23
|
+
export type SlideConfig = Defaults & { cardOffset?: CardOffset };
|
|
24
|
+
|
|
25
|
+
export interface Deck3dApi {
|
|
26
|
+
gotoSlide: (index1Based: number) => void;
|
|
27
|
+
setTime: (seconds: number) => void;
|
|
28
|
+
ready: () => Promise<void>;
|
|
29
|
+
measure: () => Measurement[];
|
|
30
|
+
peaks: () => number[];
|
|
31
|
+
effects: () => {
|
|
32
|
+
active: string[];
|
|
33
|
+
skipped: string[];
|
|
34
|
+
budget: { sum: number; limit: number; warning?: string };
|
|
35
|
+
/** Local-effect failures, `{ slide, effectId, phase }` (design D1). */
|
|
36
|
+
errors: Array<{ slide: string; effectId: string; phase: "create" | "tick" | "dispose" }>;
|
|
37
|
+
};
|
|
38
|
+
debug: {
|
|
39
|
+
titleGlyphs: () => number;
|
|
40
|
+
liftedMessage: () => string | null;
|
|
41
|
+
look: () => {
|
|
42
|
+
bg: string;
|
|
43
|
+
fog: string;
|
|
44
|
+
rim: string;
|
|
45
|
+
title: string;
|
|
46
|
+
/** Glyph side-wall contour colour; `""` when `titleEdge` is off. */
|
|
47
|
+
titleEdge: string;
|
|
48
|
+
camZ: number;
|
|
49
|
+
/** Full camera position + target: the rail runs on X, so `camZ` alone cannot tell a settled camera from a travelling one. */
|
|
50
|
+
cam: [number, number, number];
|
|
51
|
+
camTarget: [number, number, number];
|
|
52
|
+
anim: { mode: string; t: number; dur: number } | null;
|
|
53
|
+
/** Which floor surface is showing. */
|
|
54
|
+
floor: "mirror" | "water" | "none";
|
|
55
|
+
/** Whether backdrop geometry (backgrounds, local fx) is in the reflection. */
|
|
56
|
+
reflectBackdrop: boolean;
|
|
57
|
+
/** Floor roughness in the shader: 0 = mirror, 1 = fully scattered. */
|
|
58
|
+
floorMatte: number;
|
|
59
|
+
/** Reflection strength in the shader: 1 = full mirror, 0 = none. */
|
|
60
|
+
floorReflectivity: number;
|
|
61
|
+
/** Key-light target: the centre of the shadow frustum, which must track the slide. */
|
|
62
|
+
keyTarget: [number, number, number];
|
|
63
|
+
/** Rim-spot position, offset from the slide it lights. */
|
|
64
|
+
rimPos: [number, number, number];
|
|
65
|
+
};
|
|
66
|
+
/** Per diagram label: 1 = faces the camera head-on, 0 = edge-on, negative = facing away. */
|
|
67
|
+
labelFacing: () => number[];
|
|
68
|
+
/** Backdrop objects that leaked onto the content layer (checked by `check`). */
|
|
69
|
+
backdropLeaks: () => string[];
|
|
70
|
+
/**
|
|
71
|
+
* Shadow-pipeline state as the RENDERER sees it: whether the map is on,
|
|
72
|
+
* the key light's frustum, and the live caster/receiver counts with their
|
|
73
|
+
* layer masks. `castShadow` flags in the builders say nothing about
|
|
74
|
+
* whether a shadow reaches the frame.
|
|
75
|
+
*/
|
|
76
|
+
shadows: () => {
|
|
77
|
+
enabled: boolean;
|
|
78
|
+
autoUpdate: boolean;
|
|
79
|
+
camera: { left: number; right: number; top: number; bottom: number; near: number; far: number; pos: [number, number, number]; castShadow: boolean };
|
|
80
|
+
casters: string[];
|
|
81
|
+
receivers: string[];
|
|
82
|
+
casterCount: number;
|
|
83
|
+
receiverCount: number;
|
|
84
|
+
};
|
|
85
|
+
/** Fingerprint of the current slide's animated transforms. */
|
|
86
|
+
motion: () => string;
|
|
87
|
+
/** Every slide's live anchor, for rail/spacing assertions. */
|
|
88
|
+
localFx: () => Array<{ id: string; digest: string }>;
|
|
89
|
+
localFxAt: (index: number) => number;
|
|
90
|
+
sceneNodes: () => number;
|
|
91
|
+
anchors: () => Array<{ pos: [number, number, number]; rotY: number }>;
|
|
92
|
+
/** Every registered post pass: enabled for the current slide, and the params it was last applied with. */
|
|
93
|
+
post: () => Array<{ id: string; enabled: boolean; params: Record<string, number | string | boolean> }>;
|
|
94
|
+
};
|
|
95
|
+
current: () => number;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export interface Measurement {
|
|
99
|
+
kind: "title" | "label" | "node";
|
|
100
|
+
id: string;
|
|
101
|
+
text: string;
|
|
102
|
+
rect: { x: number; y: number; w: number; h: number };
|
|
103
|
+
capHeight: number;
|
|
104
|
+
/** Owner id of the first raycast hit between camera and label centre. */
|
|
105
|
+
hit?: string | null;
|
|
106
|
+
/** Label fill colour (canvas labels only). */
|
|
107
|
+
color?: string | null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
declare global {
|
|
111
|
+
interface Window {
|
|
112
|
+
__DECK: RuntimeDeck;
|
|
113
|
+
__DECK_FONT?: string;
|
|
114
|
+
/** Base64 GLB bytes keyed `<source>-<id>`. */
|
|
115
|
+
__DECK_PROPS?: Record<string, string>;
|
|
116
|
+
__deck3d?: Deck3dApi;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `deck3d serve` — the authoring server (Section 15).
|
|
3
|
+
*
|
|
4
|
+
* Closes the tune round trip: watch the deck's sources, rebuild on change,
|
|
5
|
+
* live-reload the open browser onto the slide it was already on, and let the
|
|
6
|
+
* configurator write its overrides straight to disk.
|
|
7
|
+
*
|
|
8
|
+
* SECURITY POSTURE. This server accepts browser-driven filesystem WRITES, so:
|
|
9
|
+
* it binds `127.0.0.1` only (never `0.0.0.0`), every write target is resolved
|
|
10
|
+
* and confined to the served deck's own directory, and every payload is
|
|
11
|
+
* IR-validated BEFORE anything is written — a rejected payload leaves the
|
|
12
|
+
* target byte-identical.
|
|
13
|
+
*
|
|
14
|
+
* The reload client is injected into the SERVED copy only. The on-disk build
|
|
15
|
+
* artifact stays self-contained and offline, so `serve` never changes what
|
|
16
|
+
* `build` produces.
|
|
17
|
+
*/
|
|
18
|
+
import { createHash } from "node:crypto";
|
|
19
|
+
import { existsSync, type FSWatcher, mkdtempSync, readFileSync, watch, writeFileSync } from "node:fs";
|
|
20
|
+
import { createServer, type Server, type ServerResponse } from "node:http";
|
|
21
|
+
import { tmpdir } from "node:os";
|
|
22
|
+
import { basename, dirname, extname, join, resolve, sep } from "node:path";
|
|
23
|
+
import type { CliIO, CliRun } from "../cli-io.js";
|
|
24
|
+
|
|
25
|
+
/** Media extensions the asset route will serve. */
|
|
26
|
+
const ASSET_TYPES: Record<string, string> = {
|
|
27
|
+
".mp4": "video/mp4",
|
|
28
|
+
".webm": "video/webm",
|
|
29
|
+
".png": "image/png",
|
|
30
|
+
".jpg": "image/jpeg",
|
|
31
|
+
".jpeg": "image/jpeg",
|
|
32
|
+
".webp": "image/webp",
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Serve a media file sitting beside the deck, same-origin.
|
|
37
|
+
*
|
|
38
|
+
* A `file://` clip taints the canvas and then cannot be uploaded as a WebGL
|
|
39
|
+
* texture, so a video layer needs its clip to come from the page's own
|
|
40
|
+
* origin. Returns true once the response is handled.
|
|
41
|
+
*
|
|
42
|
+
* Confinement is not optional: `new URL()` collapses plain dot-segments but
|
|
43
|
+
* leaves `%2f` encoded, so a path like `/a%2f..%2f..%2fsecret.png` still
|
|
44
|
+
* decodes into a real traversal. The resolved path is checked against the
|
|
45
|
+
* deck directory for exactly that case.
|
|
46
|
+
*/
|
|
47
|
+
function serveAsset(pathname: string, deckDir: string, res: ServerResponse): boolean {
|
|
48
|
+
const ext = extname(pathname).toLowerCase();
|
|
49
|
+
const type = ASSET_TYPES[ext];
|
|
50
|
+
if (!type) return false;
|
|
51
|
+
const target = resolve(deckDir, decodeURIComponent(pathname.replace(/^\/+/, "")));
|
|
52
|
+
if (!target.startsWith(deckDir + sep) || !existsSync(target)) {
|
|
53
|
+
res.writeHead(404, { "content-type": "text/plain" });
|
|
54
|
+
res.end("not found");
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
res.writeHead(200, { "content-type": type, "cache-control": "no-store" });
|
|
58
|
+
res.end(readFileSync(target));
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface ServeOptions {
|
|
63
|
+
/** 0 (the default) asks the OS for a free port. */
|
|
64
|
+
port?: number;
|
|
65
|
+
/** Run the fit check out of band after each rebuild. */
|
|
66
|
+
check?: boolean;
|
|
67
|
+
/** Debounce window for a burst of writes. */
|
|
68
|
+
debounceMs?: number;
|
|
69
|
+
io?: CliIO;
|
|
70
|
+
/**
|
|
71
|
+
* The CLI entry point, injected. `serve` drives `parse` and `render` through
|
|
72
|
+
* it but must NOT import `cli.ts`, which imports this module — see
|
|
73
|
+
* `cli-io.ts` for the cycle (and the exit-13 bug) that direction caused.
|
|
74
|
+
*/
|
|
75
|
+
run: CliRun;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface ServeHandle {
|
|
79
|
+
url: string;
|
|
80
|
+
host: string;
|
|
81
|
+
port: number;
|
|
82
|
+
/** Path of the built artifact (no reload client). */
|
|
83
|
+
htmlPath: string;
|
|
84
|
+
/** Resolves once any in-flight rebuild has settled. */
|
|
85
|
+
settled: () => Promise<void>;
|
|
86
|
+
/** Last rebuild error, or null when the last rebuild succeeded. */
|
|
87
|
+
lastError: () => string | null;
|
|
88
|
+
/** Findings from the last `--check` pass, by slide id. */
|
|
89
|
+
findings: () => Record<string, string[]>;
|
|
90
|
+
close: () => Promise<void>;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const HOST = "127.0.0.1";
|
|
94
|
+
|
|
95
|
+
/** How often `deck.json` is re-digested to catch an out-of-band override write. */
|
|
96
|
+
const JSON_POLL_MS = 150;
|
|
97
|
+
|
|
98
|
+
/** Injected only into the served HTML. Reloads in place, keeping the slide. */
|
|
99
|
+
const CLIENT = `<script>
|
|
100
|
+
(() => {
|
|
101
|
+
// Position survives the reload: the runtime keeps the slide in location.hash,
|
|
102
|
+
// so a plain reload lands where the author was. Staged configurator values
|
|
103
|
+
// live in localStorage and are replayed by the panel itself.
|
|
104
|
+
window.__deck3dServe = true;
|
|
105
|
+
const es = new EventSource("/__events");
|
|
106
|
+
es.addEventListener("rebuild", () => location.reload());
|
|
107
|
+
es.addEventListener("error-report", (e) => {
|
|
108
|
+
let box = document.getElementById("deck3d-serve-error");
|
|
109
|
+
if (!box) {
|
|
110
|
+
box = document.createElement("pre");
|
|
111
|
+
box.id = "deck3d-serve-error";
|
|
112
|
+
box.style.cssText = "position:fixed;left:0;right:0;bottom:0;z-index:9999;margin:0;padding:10px;max-height:40vh;overflow:auto;background:#3b0d0d;color:#fecaca;font:12px/1.5 ui-monospace,monospace;white-space:pre-wrap";
|
|
113
|
+
document.body.appendChild(box);
|
|
114
|
+
}
|
|
115
|
+
box.textContent = JSON.parse(e.data).message;
|
|
116
|
+
});
|
|
117
|
+
es.addEventListener("ok", () => document.getElementById("deck3d-serve-error")?.remove());
|
|
118
|
+
es.addEventListener("findings", (e) => {
|
|
119
|
+
window.__deck3dFindings = JSON.parse(e.data);
|
|
120
|
+
window.dispatchEvent(new CustomEvent("deck3d-findings"));
|
|
121
|
+
});
|
|
122
|
+
})();
|
|
123
|
+
</script>`;
|
|
124
|
+
|
|
125
|
+
const quietIO: CliIO = { stdout: () => {}, stderr: () => {} };
|
|
126
|
+
|
|
127
|
+
export async function startServe(mdPath: string, opts: ServeOptions): Promise<ServeHandle> {
|
|
128
|
+
const run = opts.run;
|
|
129
|
+
const md = resolve(mdPath);
|
|
130
|
+
const deckDir = dirname(md);
|
|
131
|
+
const jsonPath = join(deckDir, `${basename(md, ".md")}.json`);
|
|
132
|
+
const htmlPath = join(deckDir, `${basename(md, ".md")}.html`);
|
|
133
|
+
const io = opts.io ?? quietIO;
|
|
134
|
+
const debounceMs = opts.debounceMs ?? 120;
|
|
135
|
+
|
|
136
|
+
let html: string | null = null;
|
|
137
|
+
let lastError: string | null = null;
|
|
138
|
+
let findings: Record<string, string[]> = {};
|
|
139
|
+
let building: Promise<void> = Promise.resolve();
|
|
140
|
+
const clients = new Set<import("node:http").ServerResponse>();
|
|
141
|
+
|
|
142
|
+
function emit(event: string, data: unknown): void {
|
|
143
|
+
const payload = `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`;
|
|
144
|
+
for (const c of clients) c.write(payload);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Re-pin every local module's hash before rendering: the author is editing
|
|
149
|
+
* `fx/*.js` live, and the render refuses on a stale `sha256`. Serving is an
|
|
150
|
+
* authoring context, so the pin follows the file rather than gating it.
|
|
151
|
+
*/
|
|
152
|
+
function repinLocalFx(): void {
|
|
153
|
+
if (!existsSync(jsonPath)) return;
|
|
154
|
+
let ir: { overrides?: { slides?: Record<string, { effects?: Array<{ id: string; sha256?: string }> }>; effects?: Array<{ id: string; sha256?: string }> } };
|
|
155
|
+
try {
|
|
156
|
+
ir = JSON.parse(readFileSync(jsonPath, "utf8")) as typeof ir;
|
|
157
|
+
} catch {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
let touched = false;
|
|
161
|
+
const lists = [ir.overrides?.effects, ...Object.values(ir.overrides?.slides ?? {}).map((s) => s.effects)];
|
|
162
|
+
for (const list of lists) {
|
|
163
|
+
for (const ref of list ?? []) {
|
|
164
|
+
if (!ref.id?.startsWith("local:")) continue;
|
|
165
|
+
const file = join(deckDir, "fx", `${ref.id.slice("local:".length)}.js`);
|
|
166
|
+
if (!existsSync(file)) continue;
|
|
167
|
+
const sha = createHash("sha256").update(readFileSync(file)).digest("hex");
|
|
168
|
+
if (ref.sha256 !== sha) {
|
|
169
|
+
ref.sha256 = sha;
|
|
170
|
+
touched = true;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (touched) writeFileSync(jsonPath, `${JSON.stringify(ir, null, 2)}\n`);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Digest of `deck.json` as the last rebuild left it. Every rebuild rewrites
|
|
179
|
+
* that file (`parse -o`), so a bare watcher on it would re-trigger itself
|
|
180
|
+
* forever; comparing the digest tells a foreign write from our own.
|
|
181
|
+
*/
|
|
182
|
+
let jsonDigest = "";
|
|
183
|
+
const digestJson = (): string => (existsSync(jsonPath) ? createHash("sha256").update(readFileSync(jsonPath)).digest("hex") : "");
|
|
184
|
+
|
|
185
|
+
async function rebuild(): Promise<void> {
|
|
186
|
+
const errors: string[] = [];
|
|
187
|
+
const collect: CliIO = { stdout: () => {}, stderr: (m) => errors.push(m) };
|
|
188
|
+
try {
|
|
189
|
+
if ((await run(["parse", md, "-o", jsonPath], collect)) !== 0) throw new Error(errors.join("\n") || "parse failed");
|
|
190
|
+
repinLocalFx();
|
|
191
|
+
// Render to a scratch file: the served copy carries the reload client,
|
|
192
|
+
// and the deck-dir artifact must not.
|
|
193
|
+
const scratch = join(mkdtempSync(join(tmpdir(), "deck3d-serve-out-")), "deck.html");
|
|
194
|
+
if ((await run(["render", jsonPath, "-o", scratch], collect)) !== 0) throw new Error(errors.join("\n") || "render failed");
|
|
195
|
+
const built = readFileSync(scratch, "utf8");
|
|
196
|
+
writeFileSync(htmlPath, built);
|
|
197
|
+
html = built.replace("</body>", `${CLIENT}</body>`);
|
|
198
|
+
lastError = null;
|
|
199
|
+
emit("ok", {});
|
|
200
|
+
emit("rebuild", {});
|
|
201
|
+
if (opts.check) void runCheck();
|
|
202
|
+
} catch (err) {
|
|
203
|
+
// Last good deck stays served — an unparseable buffer mid-edit must not
|
|
204
|
+
// blank the author's screen.
|
|
205
|
+
lastError = (err as Error).message;
|
|
206
|
+
io.stderr(`deck3d serve: ${lastError}`);
|
|
207
|
+
emit("error-report", { message: lastError });
|
|
208
|
+
} finally {
|
|
209
|
+
jsonDigest = digestJson();
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Out of band by contract: the reload already happened. */
|
|
214
|
+
async function runCheck(): Promise<void> {
|
|
215
|
+
try {
|
|
216
|
+
const { runCheck: check, parseViewports } = await import("../check/index.js");
|
|
217
|
+
const report = await check(htmlPath, { viewports: parseViewports(undefined) });
|
|
218
|
+
const next: Record<string, string[]> = {};
|
|
219
|
+
for (const v of report.viewports) {
|
|
220
|
+
for (const f of v.findings) (next[f.slide] ??= []).push(`${v.viewport} ${f.rule}: ${f.detail ?? ""}`.trim());
|
|
221
|
+
}
|
|
222
|
+
findings = next;
|
|
223
|
+
emit("findings", findings);
|
|
224
|
+
} catch {
|
|
225
|
+
// Check is advisory here; a missing browser must not break serving.
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function queue(): void {
|
|
230
|
+
building = building.then(rebuild);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
await (building = rebuild());
|
|
234
|
+
|
|
235
|
+
let timer: NodeJS.Timeout | null = null;
|
|
236
|
+
const watchers: FSWatcher[] = [];
|
|
237
|
+
const onChange = (): void => {
|
|
238
|
+
if (timer) clearTimeout(timer);
|
|
239
|
+
timer = setTimeout(queue, debounceMs);
|
|
240
|
+
};
|
|
241
|
+
const watchPath = (p: string, recursive = false, handler: () => void = onChange): void => {
|
|
242
|
+
if (!existsSync(p)) return;
|
|
243
|
+
try {
|
|
244
|
+
watchers.push(watch(p, { recursive }, handler));
|
|
245
|
+
} catch {
|
|
246
|
+
// A platform without recursive watch still gets the deck.md watcher.
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
watchPath(md);
|
|
250
|
+
watchPath(join(deckDir, "fx"), true);
|
|
251
|
+
// `deck3d overrides apply` and a hand edit are writers too, not just the
|
|
252
|
+
// HTTP endpoints. Polled rather than watched: macOS delivers `fs.watch`
|
|
253
|
+
// events for this file seconds late, and the rebuild rewrites it anyway, so
|
|
254
|
+
// a digest compare is both the change detector and the self-write filter.
|
|
255
|
+
const jsonPoll = setInterval(() => {
|
|
256
|
+
if (digestJson() === jsonDigest) return;
|
|
257
|
+
onChange();
|
|
258
|
+
}, JSON_POLL_MS);
|
|
259
|
+
jsonPoll.unref();
|
|
260
|
+
|
|
261
|
+
function jsonBody(req: import("node:http").IncomingMessage): Promise<unknown> {
|
|
262
|
+
return new Promise((res, rej) => {
|
|
263
|
+
let raw = "";
|
|
264
|
+
req.on("data", (c) => {
|
|
265
|
+
raw += c;
|
|
266
|
+
// A write endpoint takes an overrides document, not a stream.
|
|
267
|
+
if (raw.length > 1_000_000) rej(new Error("payload too large"));
|
|
268
|
+
});
|
|
269
|
+
req.on("end", () => {
|
|
270
|
+
try {
|
|
271
|
+
res(JSON.parse(raw));
|
|
272
|
+
} catch (e) {
|
|
273
|
+
rej(e as Error);
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** Confines a caller-supplied name to the deck directory. */
|
|
280
|
+
function safeTarget(name: string): string | null {
|
|
281
|
+
const target = resolve(deckDir, name);
|
|
282
|
+
return target === deckDir || target.startsWith(deckDir + sep) ? target : null;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async function validPayload(patch: unknown): Promise<string | null> {
|
|
286
|
+
const { validate } = await import("../ir/validate.js");
|
|
287
|
+
const { deepMerge } = await import("../ir/merge.js");
|
|
288
|
+
const base = JSON.parse(readFileSync(jsonPath, "utf8")) as { overrides?: unknown };
|
|
289
|
+
const merged = { ...base, overrides: deepMerge((base.overrides ?? {}) as Record<string, unknown>, patch as Record<string, unknown>) };
|
|
290
|
+
const result = validate(merged, { deckDir });
|
|
291
|
+
return result.errors.length ? result.errors.map((e) => `${e.path}: ${e.message}`).join("; ") : null;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const server: Server = createServer((req, res) => {
|
|
295
|
+
const url = new URL(req.url ?? "/", `http://${HOST}`);
|
|
296
|
+
void (async () => {
|
|
297
|
+
if (serveAsset(url.pathname, deckDir, res)) return;
|
|
298
|
+
if (url.pathname === "/__events") {
|
|
299
|
+
res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache", connection: "keep-alive" });
|
|
300
|
+
res.write(": connected\n\n");
|
|
301
|
+
clients.add(res);
|
|
302
|
+
req.on("close", () => clients.delete(res));
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
if (req.method === "POST" && (url.pathname === "/__overrides" || url.pathname === "/__apply")) {
|
|
306
|
+
const target = safeTarget(url.searchParams.get("path") ?? (url.pathname === "/__apply" ? basename(jsonPath) : "overrides.json"));
|
|
307
|
+
if (!target) {
|
|
308
|
+
res.writeHead(400, { "content-type": "application/json" });
|
|
309
|
+
res.end(JSON.stringify({ error: "path escapes the deck directory" }));
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
let patch: unknown;
|
|
313
|
+
try {
|
|
314
|
+
patch = await jsonBody(req);
|
|
315
|
+
} catch (e) {
|
|
316
|
+
res.writeHead(400, { "content-type": "application/json" });
|
|
317
|
+
res.end(JSON.stringify({ error: (e as Error).message }));
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
// Validate BEFORE touching either file.
|
|
321
|
+
const invalid = await validPayload(patch);
|
|
322
|
+
if (invalid) {
|
|
323
|
+
res.writeHead(400, { "content-type": "application/json" });
|
|
324
|
+
res.end(JSON.stringify({ error: invalid }));
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
if (url.pathname === "/__apply") {
|
|
328
|
+
const { deepMerge } = await import("../ir/merge.js");
|
|
329
|
+
const ir = JSON.parse(readFileSync(jsonPath, "utf8")) as Record<string, unknown>;
|
|
330
|
+
ir.overrides = deepMerge((ir.overrides ?? {}) as Record<string, unknown>, patch as Record<string, unknown>);
|
|
331
|
+
writeFileSync(target, `${JSON.stringify(ir, null, 2)}\n`);
|
|
332
|
+
await (building = building.then(rebuild));
|
|
333
|
+
} else {
|
|
334
|
+
writeFileSync(target, `${JSON.stringify(patch, null, 2)}\n`);
|
|
335
|
+
}
|
|
336
|
+
res.writeHead(200, { "content-type": "application/json" });
|
|
337
|
+
res.end(JSON.stringify({ ok: true, wrote: basename(target) }));
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
if (html == null) {
|
|
341
|
+
res.writeHead(503, { "content-type": "text/plain" });
|
|
342
|
+
res.end(lastError ?? "building");
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
res.writeHead(200, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store" });
|
|
346
|
+
res.end(html);
|
|
347
|
+
})().catch((err: Error) => {
|
|
348
|
+
res.writeHead(500, { "content-type": "text/plain" });
|
|
349
|
+
res.end(err.message);
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
await new Promise<void>((done) => server.listen(opts.port ?? 0, HOST, done));
|
|
354
|
+
const port = (server.address() as { port: number }).port;
|
|
355
|
+
|
|
356
|
+
return {
|
|
357
|
+
url: `http://${HOST}:${port}`,
|
|
358
|
+
host: HOST,
|
|
359
|
+
port,
|
|
360
|
+
htmlPath,
|
|
361
|
+
settled: async () => {
|
|
362
|
+
// One debounce window plus the queued rebuild.
|
|
363
|
+
await new Promise((r) => setTimeout(r, debounceMs + 60));
|
|
364
|
+
await building;
|
|
365
|
+
},
|
|
366
|
+
lastError: () => lastError,
|
|
367
|
+
findings: () => findings,
|
|
368
|
+
close: async () => {
|
|
369
|
+
for (const w of watchers) w.close();
|
|
370
|
+
clearInterval(jsonPoll);
|
|
371
|
+
if (timer) clearTimeout(timer);
|
|
372
|
+
for (const c of clients) c.end();
|
|
373
|
+
await new Promise<void>((done) => server.close(() => done()));
|
|
374
|
+
},
|
|
375
|
+
};
|
|
376
|
+
}
|