Skip to content

🎥 Make videos programmatically with React

`@remotion/browser-studio`: Store public assets in OPFS (#10683)

Jonny Burger609457faa645
diff --git a/packages/browser-studio/e2e/app.tsx b/packages/browser-studio/e2e/app.tsx
index 5cec2a2362..6cef3cbca1 100644
--- a/packages/browser-studio/e2e/app.tsx
+++ b/packages/browser-studio/e2e/app.tsx
@@ -1,79 +1,94 @@
import {
BrowserStudio,
createBlankTemplateProject,
+ loadGitHubRepository,
type VirtualProject,
} from '@remotion/browser-studio';
import {StudioProtocolInternals} from '@remotion/studio-protocol';
import {createRoot} from 'react-dom/client';
import {VERSION} from 'remotion/version';
-const project = createBlankTemplateProject();
const initialElementPayload = StudioProtocolInternals.parseBrowserStudioHash(
window.location.hash,
);
-(
- window as typeof window & {
- __browserStudioProject: VirtualProject;
- __browserStudioRemotionVersion: string;
- }
-).__browserStudioProject = project;
(
window as typeof window & {__browserStudioRemotionVersion: string}
).__browserStudioRemotionVersion = VERSION;
-project.files['/project/src/index.ts'] =
- `import {fade} from '@remotion/transitions/fade';
+const render = async () => {
+ const project = new URLSearchParams(window.location.search).has('github')
+ ? await loadGitHubRepository({
+ repoUrl: 'https://github.com/remotion-dev/opfs-fixture',
+ })
+ : createBlankTemplateProject();
+ (
+ window as typeof window & {
+ __browserStudioProject: VirtualProject;
+ __browserStudioRemotionVersion: string;
+ }
+ ).__browserStudioProject = project;
+ if (!new URLSearchParams(window.location.search).has('github')) {
+ project.files['/project/src/index.ts'] =
+ `import {fade} from '@remotion/transitions/fade';
import {registerRoot} from 'remotion';
import {RemotionRoot} from './Root';
void fade;
registerRoot(RemotionRoot);
`;
+ }
+
+ const root = document.getElementById('root');
+ if (!root) {
+ throw new Error('Could not find root element');
+ }
-const root = document.getElementById('root');
-if (!root) {
- throw new Error('Could not find root element');
-}
+ const source = new URLSearchParams(window.location.search).get('source');
-const source = new URLSearchParams(window.location.search).get('source');
+ createRoot(root).render(
+ <BrowserStudio
+ dependencyResolver={
+ source === 'fallback'
+ ? ({name, version}) => (name === 'react' ? version : null)
+ : undefined
+ }
+ iframeSrc="/frame.html"
+ initialElement={
+ initialElementPayload === null
+ ? null
+ : {payload: initialElementPayload, sourceOrigin: null}
+ }
+ project={project}
+ readOnly={false}
+ onProjectChange={(nextProject) => {
+ (
+ window as typeof window & {__browserStudioProject: VirtualProject}
+ ).__browserStudioProject = nextProject;
+ }}
+ remotionPackageSource={
+ source === 'release'
+ ? {
+ baseUrl: new URL(
+ `/__remotion_browser_studio_release__/${VERSION}/`,
+ window.location.href,
+ ).href,
+ type: 'release',
+ version: VERSION,
+ }
+ : {
+ baseUrl: new URL(
+ '/__remotion_browser_studio_workspace__/commits/e2e/',
+ window.location.href,
+ ).href,
+ commit: 'e2e',
+ type: 'workspace',
+ }
+ }
+ />,
+ );
+};
-createRoot(root).render(
- <BrowserStudio
- dependencyResolver={
- source === 'fallback'
- ? ({name, version}) => (name === 'react' ? version : null)
- : undefined
- }
- iframeSrc="/frame.html"
- initialElement={
- initialElementPayload === null
- ? null
- : {payload: initialElementPayload, sourceOrigin: null}
- }
- project={project}
- readOnly={false}
- onProjectChange={(nextProject) => {
- (
- window as typeof window & {__browserStudioProject: VirtualProject}
- ).__browserStudioProject = nextProject;
- }}
- remotionPackageSource={
- source === 'release'
- ? {
- baseUrl: new URL(
- `/__remotion_browser_studio_release__/${VERSION}/`,
- window.location.href,
- ).href,
- type: 'release',
- version: VERSION,
- }
- : {
- baseUrl: new URL(
- '/__remotion_browser_studio_workspace__/commits/e2e/',
- window.location.href,
- ).href,
- commit: 'e2e',
- type: 'workspace',
- }
- }
- />,
-);
+render().catch((error) => {
+ setTimeout(() => {
+ throw error;
+ }, 0);
+});
diff --git a/packages/browser-studio/e2e/browser-studio.test.ts b/packages/browser-studio/e2e/browser-studio.test.ts
index 52bd7260aa..09054cf89d 100644
--- a/packages/browser-studio/e2e/browser-studio.test.ts
+++ b/packages/browser-studio/e2e/browser-studio.test.ts
@@ -10,10 +10,14 @@ import {
createElementPayload,
StudioProtocolInternals,
} from '@remotion/studio-protocol';
+import {strFromU8, unzipSync} from 'fflate';
const localImagePath = fileURLToPath(
new URL('../../codex-plugin/assets/logo.png', import.meta.url),
);
+const localVideoPath = fileURLToPath(
+ new URL('../../example/public/framer.webm', import.meta.url),
+);
const dropLocalFile = async ({
filePath,
@@ -297,6 +301,271 @@ test('loads Browser Studio from one immutable release artifact set', async ({
expect(remoteRemotionRequests).toEqual([]);
});
+test('stores, reclaims, ranges, and downloads imported OPFS files', async ({
+ page,
+}) => {
+ const rawFiles: Record<string, string> = {
+ 'package.json': JSON.stringify({
+ dependencies: {
+ react: '^19.0.0',
+ 'react-dom': '^19.0.0',
+ remotion: '^4.0.0',
+ },
+ }),
+ 'public/pixel.svg':
+ '<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64"><rect width="64" height="64" fill="red"/></svg>',
+ 'src/index.ts':
+ "import {registerRoot} from 'remotion'; import {Root} from './Root'; registerRoot(Root);",
+ 'src/Root.tsx': `import {AbsoluteFill, Composition, Img, staticFile} from 'remotion';
+
+const OpfsComposition = () => <AbsoluteFill><Img src={staticFile('pixel.svg')} /></AbsoluteFill>;
+
+export const Root = () => <Composition id="OpfsComp" component={OpfsComposition} durationInFrames={30} fps={30} width={64} height={64} />;
+`,
+ };
+ const encoder = new TextEncoder();
+ const tree = Object.entries(rawFiles).map(([path, contents]) => ({
+ path,
+ size: encoder.encode(contents).byteLength,
+ type: 'blob',
+ }));
+ const treeSha = 'ddc7ec42c2c9c06e84c9d5d2606e7ffc1394d900';
+
+ await page
+ .context()
+ .route(
+ 'https://api.github.com/repos/remotion-dev/opfs-fixture/git/trees/HEAD?recursive=1',
+ (route) =>
+ route.fulfill({
+ contentType: 'application/json',
+ body: JSON.stringify({sha: treeSha, tree, truncated: false}),
+ }),
+ );
+ await page
+ .context()
+ .route(
+ `https://raw.githubusercontent.com/remotion-dev/opfs-fixture/${treeSha}/**`,
+ (route) => {
+ const marker = `/${treeSha}/`;
+ const url = route.request().url();
+ const path = decodeURIComponent(
+ url.slice(url.indexOf(marker) + marker.length),
+ );
+ const contents = rawFiles[path];
+ return contents === undefined
+ ? route.fulfill({status: 404})
+ : route.fulfill({body: contents});
+ },
+ );
+
+ await page.goto('/?github=1');
+ const studio = page.frameLocator('iframe');
+ await expect(
+ studio.getByTitle('/project').getByText('OpfsComp'),
+ ).toBeVisible();
+ await studio.locator('[data-compname="OpfsComp"]').click();
+ await expect(
+ studio.locator('.remotion-studio-composition-container img'),
+ ).toBeVisible();
+
+ const projectStorage = await page.evaluate(() => {
+ const project = (
+ window as typeof window & {
+ __browserStudioProject: {
+ publicFileStorage?: {type: string};
+ publicFiles?: Record<string, {sizeInBytes?: number; type?: string}>;
+ };
+ }
+ ).__browserStudioProject;
+ return {
+ file: project.publicFiles?.['pixel.svg'],
+ storage: project.publicFileStorage,
+ };
+ });
+ expect(projectStorage).toEqual({
+ file: {
+ key: expect.any(String),
+ lastModified: expect.any(Number),
+ sizeInBytes: encoder.encode(rawFiles['public/pixel.svg']).byteLength,
+ type: 'stored',
+ },
+ storage: {
+ directoryName: expect.any(String),
+ type: 'opfs',
+ },
+ });
+
+ const studioFrame = page.frames().find((frame) => frame !== page.mainFrame());
+ if (!studioFrame) {
+ throw new Error('Could not find the Browser Studio frame');
+ }
+
+ const rangeResult = await studioFrame.evaluate(async () => {
+ const src = window.remotion_staticFiles.find(
+ (file) => file.name === 'pixel.svg',
+ )?.src;
+ if (!src) {
+ throw new Error('Could not find pixel.svg');
+ }
+
+ const response = await fetch(src, {headers: {Range: 'bytes=0-3'}});
+ return {body: await response.text(), status: response.status};
+ });
+ expect(rangeResult).toEqual({body: '<svg', status: 206});
+
+ await studioFrame.evaluate(async () => {
+ await window.remotion_browserStudio.writeStaticFile({
+ contents: new TextEncoder().encode('temporary').buffer,
+ filePath: 'temporary.txt',
+ });
+ });
+ await expect
+ .poll(() =>
+ page.evaluate(
+ () =>
+ (
+ window.__browserStudioProject.publicFiles?.['temporary.txt'] as
+ | {key?: string}
+ | undefined
+ )?.key,
+ ),
+ )
+ .toEqual(expect.any(String));
+ const temporaryKey = await page.evaluate(
+ () =>
+ (
+ window.__browserStudioProject.publicFiles?.['temporary.txt'] as
+ | {key: string}
+ | undefined
+ )?.key,
+ );
+ await studioFrame.evaluate(async () => {
+ await window.remotion_browserStudio.undo();
+ await window.remotion_browserStudio.redo();
+ });
+ await expect
+ .poll(() =>
+ page.evaluate(
+ () =>
+ (
+ window.__browserStudioProject.publicFiles?.['temporary.txt'] as
+ | {key?: string}
+ | undefined
+ )?.key,
+ ),
+ )
+ .toBe(temporaryKey);
+ await studioFrame.evaluate(async () => {
+ await window.remotion_browserStudio.undo();
+ await window.remotion_browserStudio.writeStaticFile({
+ contents: new TextEncoder().encode('kept').buffer,
+ filePath: 'kept.txt',
+ });
+ });
+ const retainedKeys = await page.evaluate(() => {
+ const publicFiles = window.__browserStudioProject.publicFiles as
+ | Record<string, {key?: string}>
+ | undefined;
+ return [publicFiles?.['kept.txt']?.key, publicFiles?.['pixel.svg']?.key];
+ });
+ expect(retainedKeys).not.toContain(undefined);
+ await expect
+ .poll(
+ () =>
+ page.evaluate(async () => {
+ const storage = window.__browserStudioProject.publicFileStorage;
+ if (!storage) {
+ throw new Error('Expected Browser Studio project storage');
+ }
+
+ const root = await navigator.storage.getDirectory();
+ const browserStudioDirectory = await root.getDirectoryHandle(
+ 'remotion-browser-studio',
+ );
+ const projectsDirectory =
+ await browserStudioDirectory.getDirectoryHandle('projects');
+ const projectDirectory = await projectsDirectory.getDirectoryHandle(
+ storage.directoryName,
+ );
+ const keys: string[] = [];
+ for await (const key of (
+ projectDirectory as FileSystemDirectoryHandle & {
+ keys: () => AsyncIterableIterator<string>;
+ }
+ ).keys()) {
+ keys.push(key);
+ }
+
+ return keys.sort();
+ }),
+ {timeout: 10_000},
+ )
+ .toEqual(retainedKeys.toSorted());
+ expect(retainedKeys).not.toContain(temporaryKey);
+
+ const archiveBytes = await studioFrame.evaluate(async () => {
+ const {data} = await window.remotion_browserStudio.downloadProject();
+ return Array.from(data);
+ });
+ const archive = unzipSync(new Uint8Array(archiveBytes));
+ expect(strFromU8(archive['public/pixel.svg'])).toBe(
+ rawFiles['public/pixel.svg'],
+ );
+ expect(strFromU8(archive['public/kept.txt'])).toBe('kept');
+ expect(archive['public/temporary.txt']).toBeUndefined();
+ const secondPage = await page.context().newPage();
+ await secondPage.goto('/?github=1');
+ await expect(
+ secondPage
+ .frameLocator('iframe')
+ .getByTitle('/project')
+ .getByText('OpfsComp'),
+ ).toBeVisible();
+ expect(
+ await studioFrame.evaluate(async () => {
+ const src = window.remotion_staticFiles.find(
+ (file) => file.name === 'pixel.svg',
+ )?.src;
+ if (!src) {
+ throw new Error('Could not find pixel.svg');
+ }
+
+ return (await fetch(src)).status;
+ }),
+ ).toBe(200);
+ await secondPage.close();
+
+ const previousDirectoryName = projectStorage.storage?.directoryName;
+ await page.reload();
+ await expect(
+ studio.getByTitle('/project').getByText('OpfsComp'),
+ ).toBeVisible();
+ const reloadedStorage = await page.evaluate(
+ () => window.__browserStudioProject.publicFileStorage,
+ );
+ expect(reloadedStorage?.directoryName).not.toBe(previousDirectoryName);
+ const remainingProjectDirectories = await page.evaluate(async () => {
+ const root = await navigator.storage.getDirectory();
+ const browserStudioDirectory = await root.getDirectoryHandle(
+ 'remotion-browser-studio',
+ );
+ const projectsDirectory =
+ await browserStudioDirectory.getDirectoryHandle('projects');
+ const directories: string[] = [];
+ for await (const name of (
+ projectsDirectory as FileSystemDirectoryHandle & {
+ keys: () => AsyncIterableIterator<string>;
+ }
+ ).keys()) {
+ directories.push(name);
+ }
+
+ return directories;
+ });
+ expect(remainingProjectDirectories).toContain(reloadedStorage?.directoryName);
+ expect(remainingProjectDirectories).not.toContain(previousDirectoryName);
+});
+
test('drops a local image onto the canvas and imports it into the virtual project', async ({
page,
}) => {
@@ -323,7 +592,6 @@ test('drops a local image onto the canvas and imports it into the virtual projec
page,
target: canvas,
});
- await expect(studio.getByText('logo.png', {exact: true})).toBeVisible();
await expect(studio.getByText('<CanvasImage>', {exact: true})).toBeVisible();
await expect
@@ -332,7 +600,10 @@ test('drops a local image onto the canvas and imports it into the virtual projec
const browserWindow = window as typeof window & {
__browserStudioProject: {
files: Record<string, string>;
- publicFiles?: Record<string, Uint8Array | string>;
+ publicFiles?: Record<
+ string,
+ Uint8Array | string | {sizeInBytes: number; type: 'stored'}
+ >;
};
};
const contents =
@@ -345,7 +616,9 @@ test('drops a local image onto the canvas and imports it into the virtual projec
publicFileSize:
typeof contents === 'string'
? new TextEncoder().encode(contents).byteLength
- : contents?.byteLength,
+ : contents instanceof Uint8Array
+ ? contents.byteLength
+ : contents?.sizeInBytes,
};
}),
)
@@ -353,6 +626,22 @@ test('drops a local image onto the canvas and imports it into the virtual projec
composition: expect.stringContaining('logo.png'),
publicFileSize: expect.any(Number),
});
+
+ await dropLocalFile({
+ filePath: localVideoPath,
+ page,
+ target: canvas,
+ });
+ await expect(studio.getByText('framer.webm', {exact: true})).toBeVisible();
+ await expect
+ .poll(() =>
+ page.evaluate(() =>
+ window.__browserStudioProject.files[
+ '/project/src/Composition.tsx'
+ ].includes("staticFile('framer.webm')"),
+ ),
+ )
+ .toBe(true);
expect(studioApiRequests).toEqual([]);
});
@@ -388,7 +677,10 @@ test('drops a local file into the virtual Assets folder', async ({page}) => {
const browserWindow = window as typeof window & {
__browserStudioProject: {
files: Record<string, string>;
- publicFiles?: Record<string, Uint8Array | string>;
+ publicFiles?: Record<
+ string,
+ Uint8Array | string | {sizeInBytes: number; type: 'stored'}
+ >;
};
};
const contents =
@@ -401,7 +693,9 @@ test('drops a local file into the virtual Assets folder', async ({page}) => {
publicFileSize:
typeof contents === 'string'
? new TextEncoder().encode(contents).byteLength
- : contents?.byteLength,
+ : contents instanceof Uint8Array
+ ? contents.byteLength
+ : contents?.sizeInBytes,
};
}),
)
diff --git a/packages/browser-studio/src/BrowserStudio.tsx b/packages/browser-studio/src/BrowserStudio.tsx
index 9b18ab398e..134d53c584 100644
--- a/packages/browser-studio/src/BrowserStudio.tsx
+++ b/packages/browser-studio/src/BrowserStudio.tsx
@@ -12,6 +12,7 @@ import {
} from './browser-studio-project-controller';
import {browserStudioDependencyVersions} from './dependency-versions';
import {studioRenderEntryExternal} from './dev/studio-render-entry-external';
+import {deleteBrowserStudioProjectStorage} from './opfs-public-files';
import {Spinner} from './Spinner';
import type {
BrowserStudioDependencyResolution,
@@ -201,6 +202,22 @@ export const BrowserStudio: React.FC<BrowserStudioProps> = ({
: project;
const activeProjectRef = useRef(activeProject);
activeProjectRef.current = activeProject;
+ const previousProjectStorageRef = useRef(activeProject.publicFileStorage);
+ useEffect(() => {
+ const previousStorage = previousProjectStorageRef.current;
+ const nextStorage = activeProject.publicFileStorage;
+ previousProjectStorageRef.current = nextStorage;
+ if (
+ previousStorage &&
+ previousStorage.directoryName !== nextStorage?.directoryName
+ ) {
+ deleteBrowserStudioProjectStorage(previousStorage).catch((error) => {
+ setTimeout(() => {
+ throw error;
+ }, 0);
+ });
+ }
+ }, [activeProject.publicFileStorage]);
const incomingProjectRef = useRef(project);
incomingProjectRef.current = project;
const onProjectChangeRef = useRef(onProjectChange);
@@ -365,7 +382,7 @@ export const BrowserStudio: React.FC<BrowserStudioProps> = ({
workerRef.current = worker;
lastSentProjectRef.current = activeProjectRef.current;
- worker.onmessage = (
+ worker.onmessage = async (
event: MessageEvent<BrowserStudioWorkerCompileResponse>,
) => {
if (didCancel) {
@@ -402,6 +419,14 @@ export const BrowserStudio: React.FC<BrowserStudioProps> = ({
new Blob([response.bundle], {type: 'text/javascript'}),
);
const currentProject = activeProjectRef.current;
+ const publicFiles = await publicFileManager.getStaticFiles({
+ lastModifiedByPath: null,
+ project: currentProject,
+ });
+ if (didCancel) {
+ return;
+ }
+
const bundleScriptUrl = useVendorBundle
? `${localVendorEntry}?projectBundleUrl=${encodeURIComponent(bundleUrlRef.current)}`
: bundleUrlRef.current;
@@ -423,10 +448,7 @@ export const BrowserStudio: React.FC<BrowserStudioProps> = ({
numberOfAudioTags: 0,
packageManager: 'unknown',
projectName: 'template-blank',
- publicFiles: publicFileManager.getStaticFiles({
- lastModifiedByPath: null,
- project: currentProject,
- }),
+ publicFiles,
publicFolderExists: null,
fileSystemPlatform: null,
publicPath: '',
@@ -509,14 +531,42 @@ export const BrowserStudio: React.FC<BrowserStudioProps> = ({
}, [activeProject]);
useEffect(() => {
- browserStudioOperations.emitEvent({
- files: publicFileManager.getStaticFiles({
+ let cancelled = false;
+ publicFileManager
+ .getStaticFiles({
lastModifiedByPath: null,
project: activeProject,
- }),
- folderExists: '/public',
- type: 'new-public-folder',
- });
+ })
+ .then((files) => {
+ if (cancelled) {
+ return;
+ }
+
+ browserStudioOperations.emitEvent({
+ files,
+ folderExists: '/public',
+ type: 'new-public-folder',
+ });
+ })
+ .catch((error) => {
+ if (cancelled) {
+ return;
+ }
+
+ const nextState: CompileState = {
+ status: 'error',
+ error: {
+ message: error instanceof Error ? error.message : String(error),
+ stack: error instanceof Error ? error.stack : undefined,
+ },
+ };
+ setState(nextState);
+ onCompileStateChangeRef.current?.(nextState);
+ });
+
+ return () => {
+ cancelled = true;
+ };
}, [activeProject, browserStudioOperations, publicFileManager]);
useEffect(() => {
diff --git a/packages/browser-studio/src/browser-studio-operations.ts b/packages/browser-studio/src/browser-studio-operations.ts
index 0a94b04b24..d9306f91fa 100644
--- a/packages/browser-studio/src/browser-studio-operations.ts
+++ b/packages/browser-studio/src/browser-studio-operations.ts
@@ -1873,12 +1873,10 @@ export const createBrowserStudioOperations = ({
deleteJsxNode,
deleteStaticFile: controller.deleteStaticFile,
downloadProject: () =>
- Promise.resolve(
- makeBrowserStudioProjectArchive({
- dependencyVersions,
- project: getProject(),
- }),
- ),
+ makeBrowserStudioProjectArchive({
+ dependencyVersions,
+ project: getProject(),
+ }),
duplicateComposition,
effects: effectOperations,
emitEvent: controller.emitEvent,
diff --git a/packages/browser-studio/src/browser-studio-project-controller.ts b/packages/browser-studio/src/browser-studio-project-controller.ts
index 1e0243aeac..959cf2c4a2 100644
--- a/packages/browser-studio/src/browser-studio-project-controller.ts
+++ b/packages/browser-studio/src/browser-studio-project-controller.ts
@@ -8,7 +8,18 @@ import type {
UndoResponse,
} from '@remotion/studio-shared';
import type {SequenceNodePath} from 'remotion';
-import type {VirtualProject} from './types';
+import {
+ collectBrowserStudioProjectStorageGarbage,
+ createBrowserStudioProjectStorage,
+ getBrowserStudioStoredPublicFile,
+ writeBrowserStudioStoredPublicFile,
+} from './opfs-public-files';
+import type {
+ BrowserStudioProjectStorage,
+ BrowserStudioStoredPublicFile,
+ VirtualProject,
+ VirtualProjectPublicFile,
+} from './types';
type ProjectNodePathMutationFiles = SequenceNodePathMutation['files'];
@@ -27,6 +38,13 @@ type ProjectMutation = {
const MAX_HISTORY_ENTRIES = 100;
+const isStoredPublicFile = (
+ contents: VirtualProjectPublicFile,
+): contents is BrowserStudioStoredPublicFile =>
+ typeof contents === 'object' &&
+ !(contents instanceof Uint8Array) &&
+ contents.type === 'stored';
+
const normalizePublicFilePath = (path: string) => {
const withoutLeadingSlash = path.startsWith('/') ? path.slice(1) : path;
@@ -45,7 +63,7 @@ const normalizePublicFilePath = (path: string) => {
};
const getCanonicalPublicFiles = (project: VirtualProject) => {
- const canonicalFiles: Record<string, Uint8Array | string> = {};
+ const canonicalFiles: Record<string, VirtualProjectPublicFile> = {};
for (const [path, contents] of Object.entries(project.publicFiles ?? {})) {
const canonicalPath = normalizePublicFilePath(path);
@@ -60,14 +78,27 @@ const getCanonicalPublicFiles = (project: VirtualProject) => {
};
const arePublicFileContentsEqual = (
- left: Uint8Array | string | undefined,
- right: Uint8Array | string | undefined,
+ left: VirtualProjectPublicFile | undefined,
+ right: VirtualProjectPublicFile | undefined,
) => {
if (left === right) {
return true;
}
- if (typeof left === 'string' || typeof right === 'string') {
+ if (left && right && isStoredPublicFile(left) && isStoredPublicFile(right)) {
+ return (
+ left.key === right.key &&
+ left.lastModified === right.lastModified &&
+ left.sizeInBytes === right.sizeInBytes
+ );
+ }
+
+ if (
+ typeof left === 'string' ||
+ typeof right === 'string' ||
+ !(left instanceof Uint8Array) ||
+ !(right instanceof Uint8Array)
+ ) {
return false;
}
@@ -91,8 +122,8 @@ const areStringRecordsEqual = (
};
const arePublicFileRecordsEqual = (
- left: Record<string, Uint8Array | string> | undefined,
- right: Record<string, Uint8Array | string> | undefined,
+ left: Record<string, VirtualProjectPublicFile> | undefined,
+ right: Record<string, VirtualProjectPublicFile> | undefined,
) => {
const leftEntries = Object.entries(left ?? {});
if (leftEntries.length !== Object.keys(right ?? {}).length) {
@@ -115,6 +146,8 @@ export const areBrowserStudioProjectsEqual = (
return (
left.entryPoint === right.entryPoint &&
left.rootDir === right.rootDir &&
+ left.publicFileStorage?.directoryName ===
+ right.publicFileStorage?.directoryName &&
areStringRecordsEqual(left.files, right.files) &&
arePublicFileRecordsEqual(left.publicFiles, right.publicFiles)
);
@@ -144,24 +177,38 @@ export const getBrowserStudioStaticFiles = ({
lastModifiedByPath,
project,
}: {
- getSrc: ((name: string, contents: Uint8Array | string) => string) | null;
+ getSrc:
+ | ((
+ name: string,
+ contents: VirtualProjectPublicFile,
+ project: VirtualProject,
+ ) => Promise<string> | string)
+ | null;
lastModifiedByPath: ReadonlyMap<string, number> | null;
project: VirtualProject;
}) => {
- return Object.entries(getCanonicalPublicFiles(project)).map(
- ([name, contents]) => ({
- lastModified:
- lastModifiedByPath === null ? 0 : (lastModifiedByPath.get(name) ?? 0),
- name,
- sizeInBytes:
- typeof contents === 'string'
- ? new TextEncoder().encode(contents).byteLength
- : contents.byteLength,
- src:
- getSrc === null
- ? `/${encodePublicFilePath(name)}`
- : getSrc(name, contents),
- }),
+ return Promise.all(
+ Object.entries(getCanonicalPublicFiles(project)).map(
+ async ([name, contents]) => ({
+ lastModified:
+ lastModifiedByPath === null
+ ? isStoredPublicFile(contents)
+ ? contents.lastModified
+ : 0
+ : (lastModifiedByPath.get(name) ?? 0),
+ name,
+ sizeInBytes:
+ typeof contents === 'string'
+ ? new TextEncoder().encode(contents).byteLength
+ : contents instanceof Uint8Array
+ ? contents.byteLength
+ : contents.sizeInBytes,
+ src:
+ getSrc === null
+ ? `/${encodePublicFilePath(name)}`
+ : await getSrc(name, contents, project),
+ }),
+ ),
);
};
@@ -182,10 +229,14 @@ export const createBrowserStudioPublicFileManager = ({
: revokeObjectUrl;
const objectUrls = new Map<
string,
- {contents: Uint8Array | string; url: string}
+ {contents: VirtualProjectPublicFile; url: string}
>();
- const getObjectUrl = (name: string, contents: Uint8Array | string) => {
+ const getObjectUrl = async (
+ name: string,
+ contents: VirtualProjectPublicFile,
+ project: VirtualProject,
+ ) => {
const existing = objectUrls.get(name);
if (arePublicFileContentsEqual(existing?.contents, contents)) {
return existing!.url;
@@ -195,11 +246,26 @@ export const createBrowserStudioPublicFileManager = ({
resolvedRevokeObjectUrl(existing.url);
}
- const blobContents =
- typeof contents === 'string' ? contents : contents.slice().buffer;
- const url = resolvedCreateObjectUrl(new Blob([blobContents]));
+ const blob = isStoredPublicFile(contents)
+ ? await getBrowserStudioStoredPublicFile({
+ file: contents,
+ storage:
+ project.publicFileStorage ??
+ (() => {
+ throw new Error(
+ `Stored public file ${name} has no project storage`,
+ );
+ })(),
+ })
+ : new Blob([
+ typeof contents === 'string' ? contents : contents.slice().buffer,
+ ]);
+ const url = resolvedCreateObjectUrl(blob);
objectUrls.set(name, {
- contents: typeof contents === 'string' ? contents : contents.slice(),
+ contents:
+ typeof contents === 'string' || isStoredPublicFile(contents)
+ ? contents
+ : contents.slice(),
url,
});
return url;
@@ -294,7 +360,7 @@ export type BrowserStudioProjectController = {
export type BrowserStudioStaticFilesGetter = (input: {
lastModifiedByPath: ReadonlyMap<string, number> | null;
project: VirtualProject;
-}) => ReturnType<typeof getBrowserStudioStaticFiles>;
+}) => Promise<Awaited<ReturnType<typeof getBrowserStudioStaticFiles>>>;
export const createBrowserStudioProjectController = ({
getStaticFiles,
@@ -325,6 +391,59 @@ export const createBrowserStudioProjectController = ({
let publicFileRevision = 0;
const nodePathMutationSessionId = `${Date.now()}:${Math.random()}`;
let nodePathMutationCounter = 0;
+ const reportAsyncError = (error: unknown) => {
+ setTimeout(() => {
+ throw error;
+ }, 0);
+ };
+
+ let garbageCollection = Promise.resolve();
+ const queueGarbageCollection = (storage: BrowserStudioProjectStorage) => {
+ garbageCollection = garbageCollection
+ .then(() => {
+ const referencedKeys = new Set<string>();
+ const projects = [
+ getProject(),
+ ...undoStack.flatMap((entry) => [entry.before, entry.after]),
+ ...redoStack.flatMap((entry) => [entry.before, entry.after]),
+ ];
+ for (const project of projects) {
+ if (
+ project.publicFileStorage?.directoryName !== storage.directoryName
+ ) {
+ continue;
+ }
+
+ for (const contents of Object.values(project.publicFiles ?? {})) {
+ if (isStoredPublicFile(contents)) {
+ referencedKeys.add(contents.key);
+ }
+ }
+ }
+
+ return collectBrowserStudioProjectStorageGarbage({
+ referencedKeys,
+ storage,
+ });
+ })
+ .catch(reportAsyncError);
+ };
+
+ const queueProjectGarbageCollections = (projects: VirtualProject[]) => {
+ const storages = new Map<string, BrowserStudioProjectStorage>();
+ for (const project of projects) {
+ if (project.publicFileStorage) {
+ storages.set(
+ project.publicFileStorage.directoryName,
+ project.publicFileStorage,
+ );
+ }
+ }
+
+ for (const storage of storages.values()) {
+ queueGarbageCollection(storage);
+ }
+ };
const getUndoRedoEvent = (): EventSourceEvent => ({
type: 'undo-redo-stack-changed',
@@ -332,9 +451,9 @@ export const createBrowserStudioProjectController = ({
redoFile: redoStack.at(-1)?.fileName ?? null,
});
- const getPublicFilesEvent = (): EventSourceEvent => ({
+ const getPublicFilesEvent = async (): Promise<EventSourceEvent> => ({
type: 'new-public-folder',
- files: resolvedGetStaticFiles({
+ files: await resolvedGetStaticFiles({
lastModifiedByPath,
project: getProject(),
}),
@@ -394,7 +513,7 @@ export const createBrowserStudioProjectController = ({
skipSequencePropsUpdate: nodePathMutationFiles !== null,
});
if (publicFilesChanged) {
- emit(getPublicFilesEvent());
+ getPublicFilesEvent().then(emit).catch(reportAsyncError);
}
emit(getUndoRedoEvent());
@@ -413,17 +532,28 @@ export const createBrowserStudioProjectController = ({
return null;
}
+ const discardedProjects: VirtualProject[] = [];
undoStack.push({before, after, fileName, nodePathMutationFiles});
if (undoStack.length > MAX_HISTORY_ENTRIES) {
- undoStack.shift();
+ const discarded = undoStack.shift();
+ if (discarded) {
+ discardedProjects.push(discarded.before, discarded.after);
+ }
+ }
+
+ for (const entry of redoStack) {
+ discardedProjects.push(entry.before, entry.after);
}
redoStack.length = 0;
- return commitProject({
+ const nodePathMutation = commitProject({
previousProject: before,
nextProject: after,
nodePathMutationFiles,
});
+ queueProjectGarbageCollections(discardedProjects);
+
+ return nodePathMutation;
};
const undo = (): Promise<UndoResponse> => {
@@ -515,9 +645,14 @@ export const createBrowserStudioProjectController = ({
Promise.resolve(getFileSource({fileName, project: getProject()})),
redo,
resetHistory: () => {
+ const discardedProjects = [
+ ...undoStack.flatMap((entry) => [entry.before, entry.after]),
+ ...redoStack.flatMap((entry) => [entry.before, entry.after]),
+ ];
undoStack.length = 0;
redoStack.length = 0;
emit(getUndoRedoEvent());
+ queueProjectGarbageCollections(discardedProjects);
},
renameStaticFile: ({oldRelativePath, newRelativePath}) => {
try {
@@ -559,7 +694,13 @@ export const createBrowserStudioProjectController = ({
undoFile: undoStack.at(-1)?.fileName ?? null,
redoFile: redoStack.at(-1)?.fileName ?? null,
});
- listener(getPublicFilesEvent());
+ getPublicFilesEvent()
+ .then((event) => {
+ if (listeners.has(listener)) {
+ listener(event);
+ }
+ })
+ .catch(reportAsyncError);
if (latestHmrEvent) {
listener(latestHmrEvent);
latestHmrEvent = null;
@@ -571,10 +712,15 @@ export const createBrowserStudioProjectController = ({
},
undo,
writeStaticFile: ({contents, filePath}) => {
- try {
+ return (async () => {
const canonicalPath = normalizePublicFilePath(filePath);
- const nextContents =
- typeof contents === 'string'
+ const currentProject = getProject();
+ const storage =
+ currentProject.publicFileStorage ??
+ (await createBrowserStudioProjectStorage());
+ const nextContents = storage
+ ? await writeBrowserStudioStoredPublicFile({contents, storage})
+ : typeof contents === 'string'
? contents
: new Uint8Array(contents.slice(0));
applyMutation({
@@ -582,16 +728,17 @@ export const createBrowserStudioProjectController = ({
nodePathMutationFiles: null,
mutate: (project) => ({
...project,
+ publicFileStorage: storage ?? project.publicFileStorage,
publicFiles: {
...getCanonicalPublicFiles(project),
[canonicalPath]: nextContents,
},
}),
});
- return Promise.resolve();
- } catch (error) {
- return Promise.reject(error);
- }
+ if (storage) {
+ queueGarbageCollection(storage);
+ }
+ })();
},
};
};
diff --git a/packages/browser-studio/src/download-project.ts b/packages/browser-studio/src/download-project.ts
index ba4013ee74..f5b0ee0ef5 100644
--- a/packages/browser-studio/src/download-project.ts
+++ b/packages/browser-studio/src/download-project.ts
@@ -1,5 +1,10 @@
import {strToU8, zipSync} from 'fflate';
-import type {VirtualProject} from './types';
+import {getBrowserStudioStoredPublicFile} from './opfs-public-files';
+import type {
+ BrowserStudioStoredPublicFile,
+ VirtualProject,
+ VirtualProjectPublicFile,
+} from './types';
type PackageJson = {
dependencies?: Record<string, string>;
@@ -86,6 +91,13 @@ const getPublicFileArchivePath = (path: string) => {
return `public/${validateRelativePath(relativePath)}`;
};
+const isStoredPublicFile = (
+ contents: VirtualProjectPublicFile,
+): contents is BrowserStudioStoredPublicFile =>
+ typeof contents === 'object' &&
+ !(contents instanceof Uint8Array) &&
+ contents.type === 'stored';
+
const getPublishedVersion = ({
name,
dependencyVersions,
@@ -209,7 +221,7 @@ const addArchiveFile = ({
typeof contents === 'string' ? strToU8(contents) : contents;
};
-export const makeBrowserStudioProjectArchive = ({
+export const makeBrowserStudioProjectArchive = async ({
dependencyVersions,
project,
}: {
@@ -228,9 +240,24 @@ export const makeBrowserStudioProjectArchive = ({
}
for (const [path, contents] of Object.entries(project.publicFiles ?? {})) {
+ let resolvedContents: Uint8Array | string;
+ if (isStoredPublicFile(contents)) {
+ if (!project.publicFileStorage) {
+ throw new Error(`Stored public file ${path} has no project storage`);
+ }
+
+ const file = await getBrowserStudioStoredPublicFile({
+ file: contents,
+ storage: project.publicFileStorage,
+ });
+ resolvedContents = new Uint8Array(await file.arrayBuffer());
+ } else {
+ resolvedContents = contents;
+ }
+
addArchiveFile({
archiveFiles,
- contents,
+ contents: resolvedContents,
path: getPublicFileArchivePath(path),
});
}
diff --git a/packages/browser-studio/src/index.tsx b/packages/browser-studio/src/index.tsx
index 9a0d1150e4..bfcebdbcff 100644
--- a/packages/browser-studio/src/index.tsx
+++ b/packages/browser-studio/src/index.tsx
@@ -7,8 +7,11 @@ export type {
BrowserStudioError,
BrowserStudioProps,
BrowserStudioRemotionPackageSource,
+ BrowserStudioProjectStorage,
+ BrowserStudioStoredPublicFile,
CompileState,
VirtualFileSystem,
VirtualProject,
+ VirtualProjectPublicFile,
} from './types';
export type {LoadGitHubRepositoryProgress} from './load-github-repository';
diff --git a/packages/browser-studio/src/load-github-repository.ts b/packages/browser-studio/src/load-github-repository.ts
index 00052d6a1e..74607160ec 100644
--- a/packages/browser-studio/src/load-github-repository.ts
+++ b/packages/browser-studio/src/load-github-repository.ts
@@ -1,4 +1,10 @@
+import {
+ createBrowserStudioProjectStorage,
+ deleteBrowserStudioProjectStorage,
+ writeBrowserStudioStoredPublicFile,
+} from './opfs-public-files';
import type {VirtualProject} from './types';
+import type {VirtualProjectPublicFile} from './types';
type GitHubTreeEntry = {
path: string;
@@ -185,7 +191,13 @@ export const loadGitHubRepository = async ({
let loadedBytes = 0;
let loadedFiles = 0;
const files: Record<string, string> = {};
- const publicFiles: Record<string, Uint8Array> = {};
+ const publicFiles: Record<string, VirtualProjectPublicFile> = {};
+ const hasPublicFiles = fileEntries.some((file) =>
+ file.path.startsWith('public/'),
+ );
+ const publicFileStorage = hasPublicFiles
+ ? await createBrowserStudioProjectStorage()
+ : null;
const totalFiles = fileEntries.length;
const reportDownloadProgress = () =>
onProgress?.({
@@ -198,45 +210,69 @@ export const loadGitHubRepository = async ({
reportDownloadProgress();
let nextFileIndex = 0;
- await Promise.all(
- Array.from(
- {length: Math.min(downloadConcurrency, fileEntries.length)},
- async () => {
- while (nextFileIndex < fileEntries.length) {
- const file = fileEntries[nextFileIndex++];
- const response = await fetch(
- `https://raw.githubusercontent.com/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/${encodeURIComponent(treeSha)}/${encodePath(file.path)}`,
- {signal},
- );
- if (!response.ok) {
- throw new Error(
- `Could not download ${file.path}: ${await getResponseError(response)}`,
+ try {
+ await Promise.all(
+ Array.from(
+ {length: Math.min(downloadConcurrency, fileEntries.length)},
+ async () => {
+ while (nextFileIndex < fileEntries.length) {
+ const file = fileEntries[nextFileIndex++];
+ const response = await fetch(
+ `https://raw.githubusercontent.com/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/${encodeURIComponent(treeSha)}/${encodePath(file.path)}`,
+ {signal},
);
- }
+ if (!response.ok) {
+ throw new Error(
+ `Could not download ${file.path}: ${await getResponseError(response)}`,
+ );
+ }
- const contents = new Uint8Array(await response.arrayBuffer());
- if (file.path.startsWith('public/')) {
- publicFiles[file.path.slice('public/'.length)] = contents;
- } else {
- const text = decodeTextFile(contents);
- if (text !== null) {
- files[`/project/${file.path}`] = text;
+ let loadedByteLength = file.size ?? 0;
+ if (file.path.startsWith('public/') && publicFileStorage) {
+ const contents = response.body ?? (await response.blob());
+ const storedFile = await writeBrowserStudioStoredPublicFile({
+ contents,
+ storage: publicFileStorage,
+ });
+ publicFiles[file.path.slice('public/'.length)] = storedFile;
+ loadedByteLength = storedFile.sizeInBytes;
+ } else {
+ const contents = new Uint8Array(await response.arrayBuffer());
+ loadedByteLength = contents.byteLength;
+ if (file.path.startsWith('public/')) {
+ publicFiles[file.path.slice('public/'.length)] = contents;
+ } else {
+ const text = decodeTextFile(contents);
+ if (text !== null) {
+ files[`/project/${file.path}`] = text;
+ }
+ }
}
+
+ loadedBytes += file.size ?? loadedByteLength;
+ loadedFiles++;
+ reportDownloadProgress();
}
+ },
+ ),
+ );
+ } catch (error) {
+ if (publicFileStorage) {
+ await deleteBrowserStudioProjectStorage(publicFileStorage);
+ }
- loadedBytes += file.size ?? contents.byteLength;
- loadedFiles++;
- reportDownloadProgress();
- }
- },
- ),
- );
+ throw error;
+ }
onProgress?.({phase: 'preparing-project'});
const entryPoint = entryPointCandidates.find(
(candidate) => files[`/project/${candidate}`] !== undefined,
);
if (!entryPoint) {
+ if (publicFileStorage) {
+ await deleteBrowserStudioProjectStorage(publicFileStorage);
+ }
+
throw new Error(
`${owner}/${repo} does not have a supported Remotion entry point. Expected src/index.ts or another standard Remotion entry point.`,
);
@@ -246,6 +282,7 @@ export const loadGitHubRepository = async ({
entryPoint: `/project/${entryPoint}`,
files,
publicFiles,
+ publicFileStorage: publicFileStorage ?? undefined,
rootDir: '/project',
};
};
diff --git a/packages/browser-studio/src/opfs-public-files.ts b/packages/browser-studio/src/opfs-public-files.ts
new file mode 100644
index 0000000000..bc978aab10
--- /dev/null
+++ b/packages/browser-studio/src/opfs-public-files.ts
@@ -0,0 +1,321 @@
+import type {
+ BrowserStudioProjectStorage,
+ BrowserStudioStoredPublicFile,
+} from './types';
+
+const browserStudioDirectoryName = 'remotion-browser-studio';
+const projectsDirectoryName = 'projects';
+const projectLockPrefix = 'remotion-browser-studio-project:';
+
+type IterableFileSystemDirectoryHandle = FileSystemDirectoryHandle & {
+ keys: () => AsyncIterableIterator<string>;
+};
+
+type ProjectStorageLease = {
+ finished: Promise<void>;
+ release: () => void;
+};
+
+const projectStorageLeases = new Map<string, ProjectStorageLease>();
+let abandonedProjectSweep: Promise<void> | null = null;
+let pageHideListenerInstalled = false;
+
+const isNotFoundError = (error: unknown) =>
+ error instanceof DOMException && error.name === 'NotFoundError';
+
+const getProjectLockName = (directoryName: string) =>
+ `${projectLockPrefix}${directoryName}`;
+
+const getProjectsDirectory = async ({create}: {create: boolean}) => {
+ const storage = globalThis.navigator?.storage;
+ if (!storage?.getDirectory) {
+ return null;
+ }
+
+ const root = await storage.getDirectory();
+ const browserStudioDirectory = await root.getDirectoryHandle(
+ browserStudioDirectoryName,
+ {create},
+ );
+ return browserStudioDirectory.getDirectoryHandle(projectsDirectoryName, {
+ create,
+ });
+};
+
+const getProjectDirectory = async ({
+ create,
+ storage,
+}: {
+ create: boolean;
+ storage: BrowserStudioProjectStorage;
+}) => {
+ const projectsDirectory = await getProjectsDirectory({create});
+ if (projectsDirectory === null) {
+ throw new Error('OPFS is not available in this browser');
+ }
+
+ return projectsDirectory.getDirectoryHandle(storage.directoryName, {create});
+};
+
+const releaseBrowserStudioProjectStorageLease = async (
+ storage: BrowserStudioProjectStorage,
+) => {
+ const lease = projectStorageLeases.get(storage.directoryName);
+ if (!lease) {
+ return;
+ }
+
+ projectStorageLeases.delete(storage.directoryName);
+ lease.release();
+ await lease.finished;
+};
+
+const removeBrowserStudioProjectStorage = async (
+ storage: BrowserStudioProjectStorage,
+) => {
+ await releaseBrowserStudioProjectStorageLease(storage);
+ try {
+ const projectsDirectory = await getProjectsDirectory({create: false});
+ if (projectsDirectory === null) {
+ return;
+ }
+
+ await projectsDirectory.removeEntry(storage.directoryName, {
+ recursive: true,
+ });
+ } catch (error) {
+ if (!isNotFoundError(error)) {
+ throw error;
+ }
+ }
+};
+
+const acquireBrowserStudioProjectStorageLease = async (
+ storage: BrowserStudioProjectStorage,
+) => {
+ if (projectStorageLeases.has(storage.directoryName)) {
+ return;
+ }
+
+ if (!pageHideListenerInstalled && typeof window !== 'undefined') {
+ pageHideListenerInstalled = true;
+ window.addEventListener('pagehide', (event) => {
+ if (event.persisted) {
+ return;
+ }
+
+ for (const directoryName of projectStorageLeases.keys()) {
+ removeBrowserStudioProjectStorage({
+ directoryName,
+ type: 'opfs',
+ }).catch((error) => {
+ setTimeout(() => {
+ throw error;
+ }, 0);
+ });
+ }
+ });
+ }
+
+ const lockManager = globalThis.navigator?.locks;
+ if (!lockManager) {
+ projectStorageLeases.set(storage.directoryName, {
+ finished: Promise.resolve(),
+ release: () => undefined,
+ });
+ return;
+ }
+
+ let release: () => void = () => undefined;
+ const released = new Promise<void>((resolve) => {
+ release = resolve;
+ });
+ let resolveAcquired: () => void = () => undefined;
+ let rejectAcquired: (error: unknown) => void = () => undefined;
+ const acquired = new Promise<void>((resolve, reject) => {
+ resolveAcquired = resolve;
+ rejectAcquired = reject;
+ });
+ const finished = lockManager.request(
+ getProjectLockName(storage.directoryName),
+ async () => {
+ resolveAcquired();
+ await released;
+ },
+ );
+ finished.catch(rejectAcquired);
+ await acquired;
+ projectStorageLeases.set(storage.directoryName, {finished, release});
+};
+
+const sweepAbandonedBrowserStudioProjectStorages = async () => {
+ const lockManager = globalThis.navigator?.locks;
+ if (!lockManager) {
+ return;
+ }
+
+ let projectsDirectory: FileSystemDirectoryHandle;
+ try {
+ const directory = await getProjectsDirectory({create: false});
+ if (directory === null) {
+ return;
+ }
+
+ projectsDirectory = directory;
+ } catch (error) {
+ if (isNotFoundError(error)) {
+ return;
+ }
+
+ throw error;
+ }
+
+ const names: string[] = [];
+ for await (const name of (
+ projectsDirectory as IterableFileSystemDirectoryHandle
+ ).keys()) {
+ names.push(name);
+ }
+
+ await Promise.all(
+ names.map((directoryName) =>
+ lockManager.request(
+ getProjectLockName(directoryName),
+ {ifAvailable: true},
+ async (lock) => {
+ if (lock === null) {
+ return;
+ }
+
+ try {
+ await projectsDirectory.removeEntry(directoryName, {
+ recursive: true,
+ });
+ } catch (error) {
+ if (!isNotFoundError(error)) {
+ throw error;
+ }
+ }
+ },
+ ),
+ ),
+ );
+};
+
+export const createBrowserStudioProjectStorage =
+ async (): Promise<BrowserStudioProjectStorage | null> => {
+ if (!globalThis.navigator?.storage?.getDirectory) {
+ return null;
+ }
+
+ abandonedProjectSweep ??= sweepAbandonedBrowserStudioProjectStorages();
+ await abandonedProjectSweep;
+
+ const storage: BrowserStudioProjectStorage = {
+ directoryName: crypto.randomUUID(),
+ type: 'opfs',
+ };
+ await acquireBrowserStudioProjectStorageLease(storage);
+ try {
+ await getProjectDirectory({create: true, storage});
+ return storage;
+ } catch (error) {
+ await releaseBrowserStudioProjectStorageLease(storage);
+ throw error;
+ }
+ };
+
+export const deleteBrowserStudioProjectStorage = async (
+ storage: BrowserStudioProjectStorage,
+) => {
+ await removeBrowserStudioProjectStorage(storage);
+};
+
+export const collectBrowserStudioProjectStorageGarbage = async ({
+ referencedKeys,
+ storage,
+}: {
+ referencedKeys: ReadonlySet<string>;
+ storage: BrowserStudioProjectStorage;
+}) => {
+ let projectDirectory: FileSystemDirectoryHandle;
+ try {
+ projectDirectory = await getProjectDirectory({create: false, storage});
+ } catch (error) {
+ if (isNotFoundError(error)) {
+ return;
+ }
+
+ throw error;
+ }
+
+ const keys: string[] = [];
+ for await (const key of (
+ projectDirectory as IterableFileSystemDirectoryHandle
+ ).keys()) {
+ keys.push(key);
+ }
+
+ await Promise.all(
+ keys.map(async (key) => {
+ if (referencedKeys.has(key)) {
+ return;
+ }
+
+ try {
+ await projectDirectory.removeEntry(key);
+ } catch (error) {
+ if (!isNotFoundError(error)) {
+ throw error;
+ }
+ }
+ }),
+ );
+};
+
+export const writeBrowserStudioStoredPublicFile = async ({
+ contents,
+ storage,
+}: {
+ contents:
+ | ArrayBuffer
+ | Blob
+ | ReadableStream<Uint8Array>
+ | Uint8Array
+ | string;
+ storage: BrowserStudioProjectStorage;
+}): Promise<BrowserStudioStoredPublicFile> => {
+ const projectDirectory = await getProjectDirectory({create: true, storage});
+ const key = crypto.randomUUID();
+ const handle = await projectDirectory.getFileHandle(key, {create: true});
+ const writable = await handle.createWritable();
+
+ if (contents instanceof ReadableStream) {
+ await contents.pipeTo(writable);
+ } else {
+ await writable.write(
+ contents instanceof Uint8Array ? contents.slice().buffer : contents,
+ );
+ await writable.close();
+ }
+
+ const file = await handle.getFile();
+ return {
+ key,
+ lastModified: file.lastModified,
+ sizeInBytes: file.size,
+ type: 'stored',
+ };
+};
+
+export const getBrowserStudioStoredPublicFile = async ({
+ file,
+ storage,
+}: {
+ file: BrowserStudioStoredPublicFile;
+ storage: BrowserStudioProjectStorage;
+}) => {
+ const projectDirectory = await getProjectDirectory({create: false, storage});
+ const handle = await projectDirectory.getFileHandle(file.key);
+ return handle.getFile();
+};
diff --git a/packages/browser-studio/src/test/browser-studio-project-operations.test.ts b/packages/browser-studio/src/test/browser-studio-project-operations.test.ts
index 182444573d..412016b85c 100644
--- a/packages/browser-studio/src/test/browser-studio-project-operations.test.ts
+++ b/packages/browser-studio/src/test/browser-studio-project-operations.test.ts
@@ -39,6 +39,7 @@ test('mutates virtual files, emits events, and preserves undo and redo history',
contentsAtMutation.push(project.files['/project/src/Composition.tsx']);
}
});
+ await new Promise((resolve) => setTimeout(resolve, 0));
expect(events.slice(0, 2).map((event) => event.type)).toEqual([
'init',
@@ -146,6 +147,7 @@ test('mutates virtual files, emits events, and preserves undo and redo history',
contents: new Uint8Array([0, 127, 128, 255]).buffer,
filePath: '/nested/upload.bin',
});
+ await new Promise((resolve) => setTimeout(resolve, 0));
expect(project.publicFiles?.['nested/upload.bin']).toEqual(
new Uint8Array([0, 127, 128, 255]),
);
@@ -578,7 +580,7 @@ test('rejects unsafe public paths and conflicting renames', async () => {
});
});
-test('refreshes object URLs if a supplied byte array is mutated', () => {
+test('refreshes object URLs if a supplied byte array is mutated', async () => {
const contents = new Uint8Array([1, 2, 3]);
const revokedUrls: string[] = [];
let nextObjectUrl = 0;
@@ -592,20 +594,28 @@ test('refreshes object URLs if a supplied byte array is mutated', () => {
};
expect(
- publicFileManager.getStaticFiles({lastModifiedByPath: null, project})[0]
- .src,
+ (
+ await publicFileManager.getStaticFiles({
+ lastModifiedByPath: null,
+ project,
+ })
+ )[0].src,
).toBe('blob:mutable-1');
contents[0] = 4;
expect(
- publicFileManager.getStaticFiles({lastModifiedByPath: null, project})[0]
- .src,
+ (
+ await publicFileManager.getStaticFiles({
+ lastModifiedByPath: null,
+ project,
+ })
+ )[0].src,
).toBe('blob:mutable-2');
expect(revokedUrls).toEqual(['blob:mutable-1']);
publicFileManager.dispose();
});
-test('uses the platform object URL implementation when overrides are null', () => {
+test('uses the platform object URL implementation when overrides are null', async () => {
const publicFileManager = createBrowserStudioPublicFileManager({
createObjectUrl: null,
revokeObjectUrl: null,
@@ -615,7 +625,7 @@ test('uses the platform object URL implementation when overrides are null', () =
publicFiles: {'default.txt': 'contents'},
};
- const [file] = publicFileManager.getStaticFiles({
+ const [file] = await publicFileManager.getStaticFiles({
lastModifiedByPath: null,
project,
});
diff --git a/packages/browser-studio/src/test/download-project.test.ts b/packages/browser-studio/src/test/download-project.test.ts
index 86b2e209b9..b6b5f2eda6 100644
--- a/packages/browser-studio/src/test/download-project.test.ts
+++ b/packages/browser-studio/src/test/download-project.test.ts
@@ -82,8 +82,8 @@ test('downloads the current Browser Studio project as a runnable archive', async
);
});
-test('adds a package.json if the virtual project has none', () => {
- const {data} = makeBrowserStudioProjectArchive({
+test('adds a package.json if the virtual project has none', async () => {
+ const {data} = await makeBrowserStudioProjectArchive({
dependencyVersions,
project: {
rootDir: '/project',
@@ -101,7 +101,7 @@ test('adds a package.json if the virtual project has none', () => {
expect(packageJson.scripts.dev).toBe('remotion studio');
});
-test('rejects unsafe paths and archive collisions', () => {
+test('rejects unsafe paths and archive collisions', async () => {
const invalidProjects: VirtualProject[] = [
{
rootDir: '/project',
@@ -122,8 +122,8 @@ test('rejects unsafe paths and archive collisions', () => {
];
for (const project of invalidProjects) {
- expect(() =>
+ await expect(
makeBrowserStudioProjectArchive({dependencyVersions, project}),
- ).toThrow();
+ ).rejects.toThrow();
}
});
diff --git a/packages/browser-studio/src/types.ts b/packages/browser-studio/src/types.ts
index 79c758dacb..2e0a8199e5 100644
--- a/packages/browser-studio/src/types.ts
+++ b/packages/browser-studio/src/types.ts
@@ -5,9 +5,27 @@ export type VirtualProject = {
rootDir: string;
entryPoint: string;
files: Record<string, string>;
- publicFiles?: Record<string, Uint8Array | string>;
+ publicFiles?: Record<string, VirtualProjectPublicFile>;
+ publicFileStorage?: BrowserStudioProjectStorage;
};
+export type BrowserStudioProjectStorage = {
+ directoryName: string;
+ type: 'opfs';
+};
+
+export type BrowserStudioStoredPublicFile = {
+ key: string;
+ lastModified: number;
+ sizeInBytes: number;
+ type: 'stored';
+};
+
+export type VirtualProjectPublicFile =
+ | Uint8Array
+ | string
+ | BrowserStudioStoredPublicFile;
+
export type VirtualFileSystem = {
readFile: (path: string) => Promise<string> | string;
writeFile: (path: string, contents: string) => Promise<void> | void;
diff --git a/packages/core/src/get-asset-file-name.ts b/packages/core/src/get-asset-file-name.ts
index 0141391ecb..07618ad6c8 100644
--- a/packages/core/src/get-asset-file-name.ts
+++ b/packages/core/src/get-asset-file-name.ts
@@ -1,8 +1,16 @@
export const getAssetDisplayName = (filename: string): string => {
- if (/data:|blob:/.test(filename.substring(0, 5))) {
+ if (filename.startsWith('data:')) {
return 'Data URL';
}
+ if (filename.startsWith('blob:')) {
+ const staticFile =
+ typeof window === 'undefined'
+ ? undefined
+ : window.remotion_staticFiles?.find((file) => file.src === filename);
+ return staticFile ? getAssetDisplayName(staticFile.name) : 'Blob URL';
+ }
+
const splitted = filename
.split('/')
.map((s) => s.split('\\'))
diff --git a/packages/core/src/input-props-serialization.ts b/packages/core/src/input-props-serialization.ts
index 3d0f15c226..a726650b3a 100644
--- a/packages/core/src/input-props-serialization.ts
+++ b/packages/core/src/input-props-serialization.ts
@@ -50,6 +50,7 @@ export const serializeJSONWithSpecialTypes = ({
if (
typeof item === 'string' &&
staticBase !== null &&
+ staticBase !== '' &&
item.startsWith(staticBase)
) {
customFileUsed = true;
@@ -69,6 +70,25 @@ export const serializeJSONWithSpecialTypes = ({
}
};
+export const resolveFileTokenToUrl = (value: string) => {
+ const encodedName = value.replace(FILE_TOKEN, '');
+ let name = encodedName;
+ try {
+ name = encodedName.split('/').map(decodeURIComponent).join('/');
+ } catch {
+ // Keep the encoded name if it contains an invalid escape sequence.
+ }
+
+ const matchingStaticFile = window.remotion_staticFiles?.find(
+ (file) => file.name === name,
+ );
+ if (matchingStaticFile) {
+ return matchingStaticFile.src;
+ }
+
+ return `${window.remotion_staticBase}/${encodedName}`;
+};
+
export const deserializeJSONWithSpecialTypes = <T = Record<string, unknown>>(
data: string,
): T => {
@@ -78,7 +98,7 @@ export const deserializeJSONWithSpecialTypes = <T = Record<string, unknown>>(
}
if (typeof value === 'string' && value.startsWith(FILE_TOKEN)) {
- return `${window.remotion_staticBase}/${value.replace(FILE_TOKEN, '')}`;
+ return resolveFileTokenToUrl(value);
}
return value;
diff --git a/packages/core/src/test/get-asset-file-name.test.ts b/packages/core/src/test/get-asset-file-name.test.ts
index 2f0c7eccf3..8b78923ce4 100644
--- a/packages/core/src/test/get-asset-file-name.test.ts
+++ b/packages/core/src/test/get-asset-file-name.test.ts
@@ -6,6 +6,8 @@ describe('get asset file name test', () => {
['assets/images/sample.png', 'sample.png'],
['assets\\images\\sample.png', 'sample.png'],
['sample.png', 'sample.png'],
+ ['data:video/mp4;base64,AAAA', 'Data URL'],
+ ['blob:https://remotion.dev/unknown', 'Blob URL'],
];
testStrings.forEach((entry) =>
@@ -13,4 +15,24 @@ describe('get asset file name test', () => {
expect(getAssetDisplayName(entry[0])).toEqual(entry[1]);
}),
);
+
+ test('resolves a blob URL to its public asset name', () => {
+ const previousStaticFiles = window.remotion_staticFiles;
+ window.remotion_staticFiles = [
+ {
+ lastModified: 0,
+ name: 'videos/intro.mp4',
+ sizeInBytes: 123,
+ src: 'blob:https://remotion.dev/intro',
+ },
+ ];
+
+ try {
+ expect(getAssetDisplayName('blob:https://remotion.dev/intro')).toEqual(
+ 'intro.mp4',
+ );
+ } finally {
+ window.remotion_staticFiles = previousStaticFiles;
+ }
+ });
});
diff --git a/packages/core/src/test/json-serialization.test.ts b/packages/core/src/test/json-serialization.test.ts
index 9985affa10..1eaeca73cc 100644
--- a/packages/core/src/test/json-serialization.test.ts
+++ b/packages/core/src/test/json-serialization.test.ts
@@ -41,3 +41,17 @@ test('Map used', () => {
});
expect(mapUsed).toEqual(true);
});
+
+test('an empty static base does not turn every string into a file', () => {
+ const result = serializeJSONWithSpecialTypes({
+ data: {color: '#91EAE4', title: 'Welcome to Remotion'},
+ indent: 2,
+ staticBase: '',
+ });
+
+ expect(result.customFileUsed).toBe(false);
+ expect(JSON.parse(result.serializedString)).toEqual({
+ color: '#91EAE4',
+ title: 'Welcome to Remotion',
+ });
+});
diff --git a/packages/core/src/use-schema.ts b/packages/core/src/use-schema.ts
index 7600d8902d..2bb1e0b4d7 100644
--- a/packages/core/src/use-schema.ts
+++ b/packages/core/src/use-schema.ts
@@ -4,7 +4,10 @@ import {
getEffectiveVisualModeValue,
resolveDragOverrideValue,
} from './get-effective-visual-mode-value.js';
-import {FILE_TOKEN} from './input-props-serialization.js';
+import {
+ FILE_TOKEN,
+ resolveFileTokenToUrl,
+} from './input-props-serialization.js';
import type {
InteractivitySchema,
InteractivitySchemaField,
@@ -348,7 +351,7 @@ export const computeEffectiveSchemaValuesDotNotation = ({
typeof value === 'string' &&
value.startsWith(FILE_TOKEN)
) {
- value = `${window.remotion_staticBase}/${value.slice(FILE_TOKEN.length)}`;
+ value = resolveFileTokenToUrl(value);
}
if (value === undefined) {
diff --git a/packages/studio/src/components/RenderModal/SchemaEditor/ZodSwitch.tsx b/packages/studio/src/components/RenderModal/SchemaEditor/ZodSwitch.tsx
index 27e3178384..469bac262e 100644
--- a/packages/studio/src/components/RenderModal/SchemaEditor/ZodSwitch.tsx
+++ b/packages/studio/src/components/RenderModal/SchemaEditor/ZodSwitch.tsx
@@ -76,7 +76,11 @@ export const ZodSwitch: React.FC<{
);
}
- if ((value as string).startsWith(window.remotion_staticBase)) {
+ if (
+ (window.remotion_staticBase !== '' &&
+ (value as string).startsWith(window.remotion_staticBase)) ||
+ window.remotion_staticFiles.some((file) => file.src === value)
+ ) {
return (
<ZodStaticFileEditor
setValue={setValue as UpdaterFunction<string>}