Code-first video & image, rendered natively on WebGPU.
fix(input-store): open Windows, relative and file:// media paths from disk
okanasle26b507604a0
diff --git a/packages/webgpu-renderers/src/input-store.test.ts b/packages/webgpu-renderers/src/input-store.test.tsindex c43a66b2..96d7d31f 100644--- a/packages/webgpu-renderers/src/input-store.test.ts+++ b/packages/webgpu-renderers/src/input-store.test.ts@@ -1,4 +1,8 @@-import { beforeEach, describe, expect, it, vi } from "vitest";+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";+import { tmpdir } from "node:os";+import { join, relative } from "node:path";+import { pathToFileURL } from "node:url";+import { afterAll, beforeEach, describe, expect, it, vi } from "vitest";// Variables referenced in vi.mock must be prefixed with "mock" in Vitestconst mockDispose = vi.fn();@@ -72,24 +76,51 @@ describe("InputStore", () => {(inputStore as any).store.clear();});- it("should acquire an Input using FilePathSource for local file paths in Node", async () => {- const filePath = "/path/to/video%20file.mp4";- const input = await inputStore.acquire(filePath);-- expect(mockState.filePathSourceConstructorCallCount).toBe(1);- expect(mockState.lastFilePathSourcePath).toBe("/path/to/video file.mp4");- expect(mockState.inputConstructorCallCount).toBe(1);- expect(input).toBeDefined();- });-- it("should acquire an Input using FilePathSource for file:// URLs in Node", async () => {- const fileUrl = "file:///path/to/video.mp4";- const input = await inputStore.acquire(fileUrl);-- expect(mockState.filePathSourceConstructorCallCount).toBe(1);- expect(mockState.lastFilePathSourcePath).toBe("/path/to/video.mp4");- expect(mockState.inputConstructorCallCount).toBe(1);- expect(input).toBeDefined();+ describe("local files in Node", () => {+ const dir = mkdtempSync(join(tmpdir(), "input-store-"));+ const spacedFile = join(dir, "video file.mp4");+ writeFileSync(spacedFile, "");+ mkdirSync(join(dir, "folder"));+ afterAll(() => rmSync(dir, { recursive: true, force: true }));++ it("opens an absolute path, percent-decoded", async () => {+ const input = await inputStore.acquire(join(dir, "video%20file.mp4"));++ expect(mockState.filePathSourceConstructorCallCount).toBe(1);+ expect(mockState.lastFilePathSourcePath).toBe(spacedFile);+ expect(mockState.urlSourceConstructorCallCount).toBe(0);+ expect(input).toBeDefined();+ });++ it("opens a file:// URL via fileURLToPath", async () => {+ await inputStore.acquire(pathToFileURL(spacedFile).href);++ expect(mockState.filePathSourceConstructorCallCount).toBe(1);+ expect(mockState.lastFilePathSourcePath).toBe(spacedFile);+ });++ it("opens a path relative to the working directory", async () => {+ await inputStore.acquire(relative(process.cwd(), spacedFile));++ expect(mockState.filePathSourceConstructorCallCount).toBe(1);+ expect(mockState.lastFilePathSourcePath).toBe(spacedFile);+ expect(mockState.urlSourceConstructorCallCount).toBe(0);+ });++ it("fails fast, naming the path, when the file is missing", async () => {+ const missing = join(dir, "missing.mp4");+ await expect(inputStore.acquire(missing)).rejects.toThrow(+ `Cannot open local media file "${missing}"`,+ );+ expect(mockState.urlSourceConstructorCallCount).toBe(0);+ expect((inputStore as any).store.has(missing)).toBe(false);+ });++ it("fails fast when the path is a directory", async () => {+ await expect(inputStore.acquire(join(dir, "folder"))).rejects.toThrow(+ "not a regular file",+ );+ });});it("should acquire an Input for a given URL", async () => {diff --git a/packages/webgpu-renderers/src/input-store.ts b/packages/webgpu-renderers/src/input-store.tsindex e5097aba..25b0fc02 100644--- a/packages/webgpu-renderers/src/input-store.ts+++ b/packages/webgpu-renderers/src/input-store.ts@@ -1,7 +1,5 @@import { ALL_FORMATS, FilePathSource, Input, UrlSource } from "mediabunny";--const isNodeRuntime = () =>- typeof process !== "undefined" && Boolean(process.versions?.node);+import { isNodeRuntime, toLocalPath } from "./local-path.js";/** In the browser, local paths are served through the dev server's `/@fs`. */function resolveInputUrl(url: string, isNode: boolean): string {@@ -21,6 +19,37 @@ function resolveInputUrl(url: string, isNode: boolean): string {return resolvedUrl;}+function safeDecode(path: string): string {+ try {+ return decodeURIComponent(path);+ } catch {+ return path;+ }+}++async function assertReadableFile(+ filePath: string,+ url: string,+): Promise<void> {+ const { stat } = await import(+ /* webpackIgnore: true */ /* @vite-ignore */ "node:fs/promises"+ );+ let isFile: boolean;+ try {+ isFile = (await stat(filePath)).isFile();+ } catch (err) {+ throw new Error(+ `Cannot open local media file "${filePath}" (from "${url}"): ${(err as Error).message}`,+ { cause: err },+ );+ }+ if (!isFile) {+ throw new Error(+ `Cannot open local media file "${filePath}" (from "${url}"): not a regular file`,+ );+ }+}+class InputStore {private store = new Map<string,@@ -35,15 +64,18 @@ class InputStore {let shared = this.store.get(resolvedUrl);if (!shared) {const promise = (async () => {- const isLocal = resolvedUrl.startsWith("/") || resolvedUrl.startsWith("file://");- let source: InstanceType<typeof FilePathSource> | InstanceType<typeof UrlSource>;- if (isNode && isLocal && typeof FilePathSource !== "undefined") {- let filePath = resolvedUrl.startsWith("file://")- ? resolvedUrl.slice(7)- : resolvedUrl;- try {- filePath = decodeURIComponent(filePath);- } catch (_) {}+ const filePath = await toLocalPath(+ resolvedUrl.startsWith("file:")+ ? resolvedUrl+ : safeDecode(resolvedUrl),+ );+ let source:+ | InstanceType<typeof FilePathSource>+ | InstanceType<typeof UrlSource>;+ if (filePath !== null) {+ // FilePathSource opens lazily; check now so a bad path fails+ // with its name instead of deep inside the first decode.+ await assertReadableFile(filePath, url);source = new FilePathSource(filePath);} else {source = new UrlSource(resolvedUrl);diff --git a/packages/webgpu-renderers/src/local-path.test.ts b/packages/webgpu-renderers/src/local-path.test.tsnew file mode 100644index 00000000..ec5fe967--- /dev/null+++ b/packages/webgpu-renderers/src/local-path.test.ts@@ -0,0 +1,32 @@+import { resolve } from "node:path";+import { pathToFileURL } from "node:url";+import { describe, expect, it } from "vitest";+import { toLocalPath } from "./local-path.js";++describe("toLocalPath (Node)", () => {+ it("returns null for URLs that should be fetched", async () => {+ for (const src of [+ "http://example.com/a.mp4",+ "https://example.com/a.mp4",+ "data:video/mp4;base64,AAAA",+ "blob:http://localhost/abc",+ ]) {+ expect(await toLocalPath(src)).toBeNull();+ }+ });++ it("converts file:// URLs with fileURLToPath", async () => {+ const abs = resolve("media", "clip one.mp4");+ expect(await toLocalPath(pathToFileURL(abs).href)).toBe(abs);+ });++ it("resolves relative paths against the working directory", async () => {+ expect(await toLocalPath("media/clip.mp4")).toBe(resolve("media/clip.mp4"));+ expect(await toLocalPath("./clip.mp4")).toBe(resolve("clip.mp4"));+ });++ it("treats a Windows drive path as local, not as a URL scheme", async () => {+ expect(await toLocalPath("D:\\media\\clip.mp4")).not.toBeNull();+ expect(await toLocalPath("D:/media/clip.mp4")).not.toBeNull();+ });+});diff --git a/packages/webgpu-renderers/src/local-path.ts b/packages/webgpu-renderers/src/local-path.tsnew file mode 100644index 00000000..72864bbd--- /dev/null+++ b/packages/webgpu-renderers/src/local-path.ts@@ -0,0 +1,29 @@+export const isNodeRuntime = () =>+ typeof process !== "undefined" && Boolean(process.versions?.node);++/**+ * A URL scheme of two or more characters (`http:`, `data:`, `blob:`, ...).+ * A single letter followed by a colon is a Windows drive (`D:\media`), not a scheme.+ */+const URL_SCHEME = /^[a-z][a-z0-9+.-]+:/i;++/**+ * The filesystem path a media source names under Node, or `null` when it+ * should be fetched. `file://` URLs go through `fileURLToPath` (so+ * `file:///D:/clip.mp4` becomes `D:\clip.mp4` on Windows), and absolute,+ * Windows and relative paths are resolved against the working directory.+ */+export async function toLocalPath(src: string): Promise<string | null> {+ if (!isNodeRuntime()) return null;+ if (src.startsWith("file:")) {+ const { fileURLToPath } = await import(+ /* webpackIgnore: true */ /* @vite-ignore */ "node:url"+ );+ return fileURLToPath(src);+ }+ if (URL_SCHEME.test(src)) return null;+ const { resolve } = await import(+ /* webpackIgnore: true */ /* @vite-ignore */ "node:path"+ );+ return resolve(src);+}diff --git a/packages/webgpu-renderers/src/lut-store.ts b/packages/webgpu-renderers/src/lut-store.tsindex 2d36c998..13011d81 100644--- a/packages/webgpu-renderers/src/lut-store.ts+++ b/packages/webgpu-renderers/src/lut-store.ts@@ -1,3 +1,5 @@+import { toLocalPath } from "./local-path.js";+export interface GPUModelLut {texture: GPUTexture;type: "1D" | "3D";@@ -523,18 +525,14 @@ export const lutStore: LutStore = (() => {})();/**- * A `.cube` file's text. Under Node a local path or `file://` URL is read from- * disk (fetch cannot open either), the same sources video layers accept;- * everything else is fetched.+ * A `.cube` file's text. Under Node a local path (absolute, relative or Windows)+ * or `file://` URL is read from disk (fetch cannot open either), the same+ * sources video layers accept; everything else is fetched.*/async function readLutText(src: string, signal?: AbortSignal): Promise<string> {- const isNode =- typeof process !== "undefined" && Boolean(process.versions?.node);- const isLocal = src.startsWith("/") || src.startsWith("file://");- if (isNode && isLocal) {+ const path = await toLocalPath(src);+ if (path !== null) {const { readFile } = await import("node:fs/promises");- const { fileURLToPath } = await import("node:url");- const path = src.startsWith("file://") ? fileURLToPath(src) : src;return readFile(path, { encoding: "utf8", signal });}const response = await fetch(src, { signal });diff --git a/packages/webgpu-renderers/src/nodes/image.ts b/packages/webgpu-renderers/src/nodes/image.tsindex 9fcb84e5..88038bcb 100644--- a/packages/webgpu-renderers/src/nodes/image.ts+++ b/packages/webgpu-renderers/src/nodes/image.ts@@ -1,3 +1,4 @@+import { toLocalPath } from "../local-path.js";import type { RenderContextValue } from "../render-context.js";import { textureCache } from "../texture-cache.js";import { createImageTexture } from "./image-texture.js";@@ -32,14 +33,7 @@ class ImageLoader {arrayBuffer = await response.arrayBuffer();} else {// Local filesystem file in headless Node.js- let filePath = src;- if (filePath.startsWith("file://")) {- try {- filePath = new URL(filePath).pathname;- } catch {- filePath = filePath.replace(/^file:\/\//, "");- }- }+ const filePath = (await toLocalPath(src)) ?? src;const fs = await import(/* webpackIgnore: true */ "fs/promises");const fileBuf = await fs.readFile(filePath);arrayBuffer = fileBuf.buffer.slice(