🎥 Make videos programmatically with React
server side part
Jonny Burgerebc756b55425
diff --git a/packages/studio-server/src/file-watcher.ts b/packages/studio-server/src/file-watcher.tsindex 4c5b2ecac6..530fb51642 100644--- a/packages/studio-server/src/file-watcher.ts+++ b/packages/studio-server/src/file-watcher.ts@@ -1,9 +1,9 @@import fs, {readFileSync, writeFileSync} from 'node:fs';export type FileChangeEvent =- | {type: 'created'; content: string}+ | {type: 'created'; content: string; originatorClientId: string | undefined}| {type: 'deleted'}- | {type: 'changed'; content: string};+ | {type: 'changed'; content: string; originatorClientId: string | undefined};type OnChange = (event: FileChangeEvent) => void;@@ -32,7 +32,11 @@ export type FileWatcherRegistry = {exists: boolean;unwatch: () => void;};- writeFileAndNotifyFileWatchers: (file: string, content: string) => void;+ writeFileAndNotifyFileWatchers: (+ file: string,+ content: string,+ originatorClientId: string | undefined,+ ) => void;};export const createFileWatcherRegistry = (): FileWatcherRegistry => {@@ -84,7 +88,11 @@ export const createFileWatcherRegistry = (): FileWatcherRegistry => {if (existenceOnly) {if (!shared.existedBefore && existsNow) {shared.existedBefore = true;- event = {type: 'created', content: ''};+ event = {+ type: 'created',+ content: '',+ originatorClientId: undefined,+ };} else if (shared.existedBefore && !existsNow) {shared.existedBefore = false;event = {type: 'deleted'};@@ -93,7 +101,7 @@ export const createFileWatcherRegistry = (): FileWatcherRegistry => {const content = readFileSync(file, 'utf-8');shared.existedBefore = true;shared.lastKnownContent = content;- event = {type: 'created', content};+ event = {type: 'created', content, originatorClientId: undefined};} else if (shared.existedBefore && !existsNow) {shared.existedBefore = false;shared.lastKnownContent = null;@@ -113,7 +121,7 @@ export const createFileWatcherRegistry = (): FileWatcherRegistry => {}shared.lastKnownContent = content;- event = {type: 'changed', content};+ event = {type: 'changed', content, originatorClientId: undefined};}if (!event) {@@ -140,7 +148,11 @@ export const createFileWatcherRegistry = (): FileWatcherRegistry => {};};- const _writeFileAndNotifyFileWatchers = (file: string, content: string) => {+ const _writeFileAndNotifyFileWatchers = (+ file: string,+ content: string,+ originatorClientId: string | undefined,+ ) => {writeFileSync(file, content);const shared = sharedWatchers.get(getRegistryKey(file, false));@@ -152,7 +164,7 @@ export const createFileWatcherRegistry = (): FileWatcherRegistry => {shared.existedBefore = true;for (const subscriber of shared.subscribers) {- subscriber({type: 'changed', content});+ subscriber({type: 'changed', content, originatorClientId});}};@@ -195,10 +207,14 @@ export const installFileWatcher: FileWatcherRegistry['installFileWatcher'] = (};export const writeFileAndNotifyFileWatchers: FileWatcherRegistry['writeFileAndNotifyFileWatchers'] =- (file, content) => {+ (file, content, originatorClientId) => {if (!currentRegistry) {return;}- getFileWatcherRegistry().writeFileAndNotifyFileWatchers(file, content);+ getFileWatcherRegistry().writeFileAndNotifyFileWatchers(+ file,+ content,+ originatorClientId,+ );};diff --git a/packages/studio-server/src/preview-server/routes/apply-codemod.ts b/packages/studio-server/src/preview-server/routes/apply-codemod.tsindex ffb3f03974..9af8d9cd8c 100644--- a/packages/studio-server/src/preview-server/routes/apply-codemod.ts+++ b/packages/studio-server/src/preview-server/routes/apply-codemod.ts@@ -41,7 +41,11 @@ export const applyCodemodHandler: ApiHandler<});if (!dryRun) {- writeFileAndNotifyFileWatchers(projectInfo.rootFile, formatted);+ writeFileAndNotifyFileWatchers(+ projectInfo.rootFile,+ formatted,+ undefined,+ );const end = Date.now() - time;RenderInternals.Log.info({indent: false, logLevel},diff --git a/packages/studio-server/src/preview-server/routes/apply-visual-control-change.ts b/packages/studio-server/src/preview-server/routes/apply-visual-control-change.tsindex 7aff6a5d10..4e97edd599 100644--- a/packages/studio-server/src/preview-server/routes/apply-visual-control-change.ts+++ b/packages/studio-server/src/preview-server/routes/apply-visual-control-change.ts@@ -117,7 +117,7 @@ export const applyVisualControlHandler: ApiHandler<});suppressUndoStackInvalidation(absolutePath);suppressBundlerUpdateForFile(absolutePath);- writeFileAndNotifyFileWatchers(absolutePath, output);+ writeFileAndNotifyFileWatchers(absolutePath, output, undefined);waitForLiveEventsListener().then((listener) => {listener.sendEventToClient({diff --git a/packages/studio-server/src/preview-server/routes/delete-jsx-node.ts b/packages/studio-server/src/preview-server/routes/delete-jsx-node.tsindex 7eb07a2dc8..0904c792a6 100644--- a/packages/studio-server/src/preview-server/routes/delete-jsx-node.ts+++ b/packages/studio-server/src/preview-server/routes/delete-jsx-node.ts@@ -52,7 +52,7 @@ export const deleteJsxNodeHandler: ApiHandler<suppressHmrOnFileRestore: false,});suppressUndoStackInvalidation(absolutePath);- writeFileAndNotifyFileWatchers(absolutePath, output);+ writeFileAndNotifyFileWatchers(absolutePath, output, undefined);const locationLabel = formatLogFileLocation({remotionRoot,diff --git a/packages/studio-server/src/preview-server/routes/duplicate-jsx-node.ts b/packages/studio-server/src/preview-server/routes/duplicate-jsx-node.tsindex a5e16254b3..59ac93cf68 100644--- a/packages/studio-server/src/preview-server/routes/duplicate-jsx-node.ts+++ b/packages/studio-server/src/preview-server/routes/duplicate-jsx-node.ts@@ -52,7 +52,7 @@ export const duplicateJsxNodeHandler: ApiHandler<suppressHmrOnFileRestore: false,});suppressUndoStackInvalidation(absolutePath);- writeFileAndNotifyFileWatchers(absolutePath, output);+ writeFileAndNotifyFileWatchers(absolutePath, output, undefined);const locationLabel = formatLogFileLocation({remotionRoot,diff --git a/packages/studio-server/src/preview-server/routes/save-effect-props.ts b/packages/studio-server/src/preview-server/routes/save-effect-props.tsindex 82f93e319f..1ebc1992d2 100644--- a/packages/studio-server/src/preview-server/routes/save-effect-props.ts+++ b/packages/studio-server/src/preview-server/routes/save-effect-props.ts@@ -35,6 +35,7 @@ export const saveEffectPropsHandler: ApiHandler<value,defaultValue,schema,+ clientId,},remotionRoot,logLevel,@@ -109,7 +110,7 @@ export const saveEffectPropsHandler: ApiHandler<});suppressUndoStackInvalidation(absolutePath);suppressBundlerUpdateForFile(absolutePath);- writeFileAndNotifyFileWatchers(absolutePath, output);+ writeFileAndNotifyFileWatchers(absolutePath, output, clientId);logEffectUpdate({fileRelativeToRoot,diff --git a/packages/studio-server/src/preview-server/routes/save-sequence-props.ts b/packages/studio-server/src/preview-server/routes/save-sequence-props.tsindex 498d7862c1..6e1a1a8d11 100644--- a/packages/studio-server/src/preview-server/routes/save-sequence-props.ts+++ b/packages/studio-server/src/preview-server/routes/save-sequence-props.ts@@ -25,7 +25,7 @@ export const saveSequencePropsHandler: ApiHandler<SaveSequencePropsRequest,SaveSequencePropsResponse> = ({- input: {fileName, nodePath, key, value, defaultValue, schema},+ input: {fileName, nodePath, key, value, defaultValue, schema, clientId},remotionRoot,logLevel,}) =>@@ -101,7 +101,7 @@ export const saveSequencePropsHandler: ApiHandler<});suppressUndoStackInvalidation(absolutePath);suppressBundlerUpdateForFile(absolutePath);- writeFileAndNotifyFileWatchers(absolutePath, output);+ writeFileAndNotifyFileWatchers(absolutePath, output, clientId);logUpdate({fileRelativeToRoot,diff --git a/packages/studio-server/src/preview-server/routes/update-default-props.ts b/packages/studio-server/src/preview-server/routes/update-default-props.tsindex 6adc14394f..2a3e6517dd 100644--- a/packages/studio-server/src/preview-server/routes/update-default-props.ts+++ b/packages/studio-server/src/preview-server/routes/update-default-props.ts@@ -69,7 +69,7 @@ export const updateDefaultPropsHandler: ApiHandler<});suppressUndoStackInvalidation(projectInfo.rootFile);suppressBundlerUpdateForFile(projectInfo.rootFile);- writeFileAndNotifyFileWatchers(projectInfo.rootFile, output);+ writeFileAndNotifyFileWatchers(projectInfo.rootFile, output, undefined);const locationLabel = formatLogFileLocation({remotionRoot,diff --git a/packages/studio-server/src/preview-server/sequence-props-watchers.ts b/packages/studio-server/src/preview-server/sequence-props-watchers.tsindex e8c82592ad..db9e163431 100644--- a/packages/studio-server/src/preview-server/sequence-props-watchers.ts+++ b/packages/studio-server/src/preview-server/sequence-props-watchers.ts@@ -133,6 +133,10 @@ export const subscribeToSequencePropsWatchers = ({return;}+ if (event.originatorClientId === clientId) {+ return;+ }+try {const result = computeSequencePropsStatusFromContent({fileContents: event.content,diff --git a/packages/studio-server/src/preview-server/undo-stack.ts b/packages/studio-server/src/preview-server/undo-stack.tsindex c6b281ae0f..0bf03fabba 100644--- a/packages/studio-server/src/preview-server/undo-stack.ts+++ b/packages/studio-server/src/preview-server/undo-stack.ts@@ -307,7 +307,7 @@ export function popUndo(): {success: true} | {success: false; reason: string} {suppressBundlerUpdateForFile(entry.filePath);}- writeFileAndNotifyFileWatchers(entry.filePath, entry.oldContents);+ writeFileAndNotifyFileWatchers(entry.filePath, entry.oldContents, undefined);RenderInternals.Log.verbose({indent: false, logLevel: storedLogLevel},@@ -347,7 +347,7 @@ export function popRedo(): {success: true} | {success: false; reason: string} {suppressBundlerUpdateForFile(entry.filePath);}- writeFileAndNotifyFileWatchers(entry.filePath, entry.oldContents);+ writeFileAndNotifyFileWatchers(entry.filePath, entry.oldContents, undefined);RenderInternals.Log.verbose({indent: false, logLevel: storedLogLevel},diff --git a/packages/studio-server/src/test/file-watcher.test.ts b/packages/studio-server/src/test/file-watcher.test.tsindex 1c2be3c3e4..e801f6008e 100644--- a/packages/studio-server/src/test/file-watcher.test.ts+++ b/packages/studio-server/src/test/file-watcher.test.ts@@ -46,12 +46,20 @@ test('multiple watchers on the same file share a single OS watcher', () => {expect(w2.exists).toBe(true);// writeFileAndNotifyFileWatchers should notify both- registry.writeFileAndNotifyFileWatchers(tmpFile, 'updated');+ registry.writeFileAndNotifyFileWatchers(tmpFile, 'updated', undefined);expect(cb1).toHaveBeenCalledTimes(1);- expect(cb1).toHaveBeenCalledWith({type: 'changed', content: 'updated'});+ expect(cb1).toHaveBeenCalledWith({+ type: 'changed',+ content: 'updated',+ originatorClientId: undefined,+ });expect(cb2).toHaveBeenCalledTimes(1);- expect(cb2).toHaveBeenCalledWith({type: 'changed', content: 'updated'});+ expect(cb2).toHaveBeenCalledWith({+ type: 'changed',+ content: 'updated',+ originatorClientId: undefined,+ });w1.unwatch();w2.unwatch();@@ -103,11 +111,15 @@ test('unwatching one subscriber does not affect the other', () => {w1.unwatch();- registry.writeFileAndNotifyFileWatchers(tmpFile, 'after-unwatch');+ registry.writeFileAndNotifyFileWatchers(tmpFile, 'after-unwatch', undefined);expect(cb1).toHaveBeenCalledTimes(0);expect(cb2).toHaveBeenCalledTimes(1);- expect(cb2).toHaveBeenCalledWith({type: 'changed', content: 'after-unwatch'});+ expect(cb2).toHaveBeenCalledWith({+ type: 'changed',+ content: 'after-unwatch',+ originatorClientId: undefined,+ });w2.unwatch();});@@ -123,7 +135,7 @@ test('writeFileAndNotifyFileWatchers passes content to subscribers', () => {},});- registry.writeFileAndNotifyFileWatchers(tmpFile, 'hello world');+ registry.writeFileAndNotifyFileWatchers(tmpFile, 'hello world', undefined);const event = receivedEvent!;expect(event.type).toBe('changed');@@ -143,7 +155,7 @@ test('writeFileAndNotifyFileWatchers writes the file to disk', () => {onChange: () => {},});- registry.writeFileAndNotifyFileWatchers(tmpFile, 'disk content');+ registry.writeFileAndNotifyFileWatchers(tmpFile, 'disk content', undefined);expect(readFileSync(tmpFile, 'utf-8')).toBe('disk content');@@ -152,7 +164,7 @@ test('writeFileAndNotifyFileWatchers writes the file to disk', () => {test('writeFileAndNotifyFileWatchers works even without watchers', () => {// Should not throw- registry.writeFileAndNotifyFileWatchers(tmpFile, 'no watchers');+ registry.writeFileAndNotifyFileWatchers(tmpFile, 'no watchers', undefined);expect(readFileSync(tmpFile, 'utf-8')).toBe('no watchers');});@@ -167,7 +179,7 @@ test('duplicate content from fs.watchFile is suppressed', async () => {});// Simulate a write via our API — sets lastKnownContent- registry.writeFileAndNotifyFileWatchers(tmpFile, 'new content');+ registry.writeFileAndNotifyFileWatchers(tmpFile, 'new content', undefined);expect(cb).toHaveBeenCalledTimes(1);// Wait for fs.watchFile to poll (100ms interval + generous buffer)@@ -196,7 +208,11 @@ test('registries are isolated from each other', () => {onChange: cb2,});- registry.writeFileAndNotifyFileWatchers(tmpFile, 'from registry 1');+ registry.writeFileAndNotifyFileWatchers(+ tmpFile,+ 'from registry 1',+ undefined,+ );expect(cb1).toHaveBeenCalledTimes(1);expect(cb2).toHaveBeenCalledTimes(0);@@ -268,7 +284,11 @@ test('existenceOnly emits created with empty content when file appears', () => {writeFileSync(newFile, 'hello');capturedListener!();- expect(cb).toHaveBeenCalledWith({type: 'created', content: ''});+ expect(cb).toHaveBeenCalledWith({+ type: 'created',+ content: '',+ originatorClientId: undefined,+ });w.unwatch();unlinkSync(newFile);diff --git a/packages/studio-shared/src/api-requests.ts b/packages/studio-shared/src/api-requests.tsindex 1d64ff3587..a3d1f0de01 100644--- a/packages/studio-shared/src/api-requests.ts+++ b/packages/studio-shared/src/api-requests.ts@@ -252,6 +252,7 @@ export type SaveSequencePropsRequest = {value: string;defaultValue: string | null;schema: SequenceSchema;+ clientId: string;};export type SaveSequencePropsResponse =@@ -272,6 +273,7 @@ export type SaveEffectPropsRequest = {value: string;defaultValue: string | null;schema: SequenceSchema;+ clientId: string;};export type SaveEffectPropsResponse = CanUpdateEffectPropsResponse;diff --git a/packages/studio/src/components/Timeline/TimelineEffectFieldRow.tsx b/packages/studio/src/components/Timeline/TimelineEffectFieldRow.tsxindex b3a0969440..4f5bedb8af 100644--- a/packages/studio/src/components/Timeline/TimelineEffectFieldRow.tsx+++ b/packages/studio/src/components/Timeline/TimelineEffectFieldRow.tsx@@ -3,6 +3,7 @@ import React, {useCallback, useContext, useMemo} from 'react';import type {SequencePropsSubscriptionKey} from 'remotion';import {Internals} from 'remotion';import type {CodePosition} from '../../error-overlay/react-overlay/utils/get-source-map';+import {StudioServerConnectionCtx} from '../../helpers/client-id';import type {EffectSchemaFieldInfo} from '../../helpers/timeline-layout';import {EXPANDED_SECTION_PADDING_RIGHT} from '../../helpers/timeline-layout';import {callApi} from '../call-api';@@ -47,6 +48,12 @@ const Value: React.FC<{Internals.VisualModeCodeValuesContext,);+ const {previewServerState} = useContext(StudioServerConnectionCtx);+ const clientId =+ previewServerState.type === 'connected'+ ? previewServerState.clientId+ : null;+const effectStatus = Internals.getEffectCodeValuesCtx({codeValues: visualModeCodeValues,nodePath,@@ -88,6 +95,10 @@ const Value: React.FC<{return Promise.reject(new Error('Cannot save'));}+ if (!clientId) {+ return Promise.reject(new Error('Not connected to studio server'));+ }+const defaultValue =field.fieldSchema.default !== undefined? JSON.stringify(field.fieldSchema.default)@@ -126,11 +137,13 @@ const Value: React.FC<{value: stringifiedValue,defaultValue,schema: field.effectSchema,+ clientId,}),errorLabel: 'Could not save effect prop',});},[+ clientId,field.effectIndex,field.effectSchema,field.fieldSchema.default,diff --git a/packages/studio/src/components/Timeline/TimelineEffectGroupRow.tsx b/packages/studio/src/components/Timeline/TimelineEffectGroupRow.tsxindex 8dddb32379..67b6a62d9c 100644--- a/packages/studio/src/components/Timeline/TimelineEffectGroupRow.tsx+++ b/packages/studio/src/components/Timeline/TimelineEffectGroupRow.tsx@@ -79,7 +79,7 @@ export const TimelineEffectGroupRow: React.FC<{const onToggle = useCallback((type: 'enable' | 'disable') => {- if (!canToggle) {+ if (!canToggle || previewServerState.type !== 'connected') {return;}@@ -99,6 +99,7 @@ export const TimelineEffectGroupRow: React.FC<{defaultValue,schema: effectSchema,setCodeValues,+ clientId: previewServerState.clientId,});},[@@ -106,6 +107,7 @@ export const TimelineEffectGroupRow: React.FC<{effectIndex,effectSchema,nodePath,+ previewServerState,setCodeValues,validatedLocation.source,],diff --git a/packages/studio/src/components/Timeline/TimelineFieldRow.tsx b/packages/studio/src/components/Timeline/TimelineFieldRow.tsxindex bb529989d2..b018ee0876 100644--- a/packages/studio/src/components/Timeline/TimelineFieldRow.tsx+++ b/packages/studio/src/components/Timeline/TimelineFieldRow.tsx@@ -6,6 +6,7 @@ import type {import type {SequenceSchema} from 'remotion';import {Internals} from 'remotion';import type {CodePosition} from '../../error-overlay/react-overlay/utils/get-source-map';+import {StudioServerConnectionCtx} from '../../helpers/client-id';import type {SchemaFieldInfo,TimelineFieldOnDragValueChange,@@ -67,6 +68,11 @@ const Value: React.FC<{});const {setCodeValues} = useContext(Internals.VisualModeSettersContext);+ const {previewServerState} = useContext(StudioServerConnectionCtx);+ const clientId =+ previewServerState.type === 'connected'+ ? previewServerState.clientId+ : null;const onSave = useCallback<TimelineFieldOnSave>((value) => {@@ -74,6 +80,10 @@ const Value: React.FC<{return Promise.reject(new Error('Cannot save'));}+ if (!clientId) {+ return Promise.reject(new Error('Not connected to studio server'));+ }+const defaultValue =field.fieldSchema.default !== undefined? JSON.stringify(field.fieldSchema.default)@@ -100,10 +110,12 @@ const Value: React.FC<{defaultValue,schema,setCodeValues,+ clientId,});},[codeValue,+ clientId,field.fieldSchema.default,field.key,nodePath,diff --git a/packages/studio/src/components/Timeline/TimelineListItem.tsx b/packages/studio/src/components/Timeline/TimelineListItem.tsxindex 2b550a23a5..bad5a55c24 100644--- a/packages/studio/src/components/Timeline/TimelineListItem.tsx+++ b/packages/studio/src/components/Timeline/TimelineListItem.tsx@@ -220,7 +220,8 @@ export const TimelineListItem: React.FC<{!validatedLocation ||!codeValuesForOverride ||!codeHiddenStatus ||- !codeHiddenStatus.canUpdate+ !codeHiddenStatus.canUpdate ||+ previewServerState.type !== 'connected') {return;}@@ -242,12 +243,14 @@ export const TimelineListItem: React.FC<{defaultValue,schema,setCodeValues,+ clientId: previewServerState.clientId,});},[codeHiddenStatus,codeValuesForOverride,nodePath,+ previewServerState,sequence.controls,setCodeValues,validatedLocation,diff --git a/packages/studio/src/components/Timeline/save-effect-prop.ts b/packages/studio/src/components/Timeline/save-effect-prop.tsindex 95b28c74fb..9dd76502b0 100644--- a/packages/studio/src/components/Timeline/save-effect-prop.ts+++ b/packages/studio/src/components/Timeline/save-effect-prop.ts@@ -13,6 +13,7 @@ export const saveEffectProp = ({defaultValue,schema,setCodeValues,+ clientId,}: {fileName: string;nodePath: SequencePropsSubscriptionKey;@@ -22,6 +23,7 @@ export const saveEffectProp = ({defaultValue: string | null;schema: SequenceSchema;setCodeValues: SetCodeValues;+ clientId: string;}): Promise<void> => {return enqueueSavePropChange({nodePath,@@ -43,6 +45,7 @@ export const saveEffectProp = ({value: JSON.stringify(value),defaultValue,schema,+ clientId,}),errorLabel: 'Could not save effect prop',});diff --git a/packages/studio/src/components/Timeline/save-sequence-prop.ts b/packages/studio/src/components/Timeline/save-sequence-prop.tsindex 9e172bf995..968fefe646 100644--- a/packages/studio/src/components/Timeline/save-sequence-prop.ts+++ b/packages/studio/src/components/Timeline/save-sequence-prop.ts@@ -22,6 +22,7 @@ export const saveSequenceProp = ({defaultValue,schema,setCodeValues,+ clientId,}: {fileName: string;nodePath: SequencePropsSubscriptionKey;@@ -30,6 +31,7 @@ export const saveSequenceProp = ({defaultValue: string | null;schema: SequenceSchema;setCodeValues: SetCodeValues;+ clientId: string;}): Promise<void> => {return enqueueSavePropChange({nodePath,@@ -49,6 +51,7 @@ export const saveSequenceProp = ({value: JSON.stringify(value),defaultValue,schema,+ clientId,}),errorLabel: 'Could not save sequence prop',});