Skip to content

🎥 Make videos programmatically with React

`@remotion/studio-server`: Read source files once per subscription batch

Jonny Burgere1846960de92
diff --git a/packages/studio-server/src/helpers/read-source-file.ts b/packages/studio-server/src/helpers/read-source-file.ts
new file mode 100644
index 0000000000..f9e8648f5c
--- /dev/null
+++ b/packages/studio-server/src/helpers/read-source-file.ts
@@ -0,0 +1,18 @@
+import {readFileSync} from 'node:fs';
+
+export const readSourceFile = ({
+ absolutePath,
+ fileContentsCache,
+}: {
+ absolutePath: string;
+ fileContentsCache: Map<string, string> | null;
+}): string => {
+ const cachedContents = fileContentsCache?.get(absolutePath);
+ if (cachedContents !== undefined) {
+ return cachedContents;
+ }
+
+ const fileContents = readFileSync(absolutePath, 'utf-8');
+ fileContentsCache?.set(absolutePath, fileContents);
+ return fileContents;
+};
diff --git a/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts b/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts
index fd1d9c8396..8cd324fc8b 100644
--- a/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts
+++ b/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts
@@ -1,4 +1,3 @@
-import {readFileSync} from 'node:fs';
import type {
CallExpression,
Expression,
@@ -32,6 +31,7 @@ import {NoReactInternals} from 'remotion/no-react';
import {getAstNodePath} from '../../helpers/get-ast-node-path';
import {toImportAgnosticNodePath} from '../../helpers/import-agnostic-node-path';
import {parseKeyframeEasingExpression} from '../../helpers/parse-keyframe-easing-expression';
+import {readSourceFile} from '../../helpers/read-source-file';
import {
FileOutsideProjectError,
resolveFileInsideProject,
@@ -1342,17 +1342,19 @@ export const resolveSequencePropsNodePathsFromFilename = ({
fileName,
targets,
remotionRoot,
+ fileContentsCache,
}: {
fileName: string;
targets: {line: number; column: number}[];
remotionRoot: string;
+ fileContentsCache: Map<string, string> | null;
}) => {
const {absolutePath} = resolveFileInsideProject({
remotionRoot,
fileName,
action: 'read',
});
- const fileContents = readFileSync(absolutePath, 'utf-8');
+ const fileContents = readSourceFile({absolutePath, fileContentsCache});
return CodemodsInternals.resolveNodePathsFromLocations({
fileContents,
targets,
@@ -1404,6 +1406,7 @@ export const computeSequencePropsStatus = ({
assetKeys = [],
effects,
remotionRoot,
+ fileContentsCache,
}: {
fileName: string;
nodePath: SequenceNodePath;
@@ -1412,6 +1415,7 @@ export const computeSequencePropsStatus = ({
assetKeys?: string[];
effects: string[][];
remotionRoot: string;
+ fileContentsCache: Map<string, string> | null;
}): CanUpdateSequencePropsResponseTrue => {
const {absolutePath} = resolveFileInsideProject({
remotionRoot,
@@ -1419,7 +1423,7 @@ export const computeSequencePropsStatus = ({
action: 'read',
});
- const fileContents = readFileSync(absolutePath, 'utf-8');
+ const fileContents = readSourceFile({absolutePath, fileContentsCache});
return computeSequencePropsStatusFromContent({
fileContents,
nodePath,
@@ -1440,6 +1444,7 @@ export const computeSequencePropsStatusFromFilenameByLocation = ({
effects,
remotionRoot,
logLevel,
+ fileContentsCache,
}: {
fileName: string;
line: number;
@@ -1450,6 +1455,7 @@ export const computeSequencePropsStatusFromFilenameByLocation = ({
effects: string[][];
remotionRoot: string;
logLevel: LogLevel;
+ fileContentsCache: Map<string, string> | null;
}): SubscribeToSequencePropsResponse => {
try {
const {absolutePath} = resolveFileInsideProject({
@@ -1462,6 +1468,7 @@ export const computeSequencePropsStatusFromFilenameByLocation = ({
fileName,
targets: [{line, column}],
remotionRoot,
+ fileContentsCache,
});
if (!resolvedNodePath) {
return {
@@ -1482,6 +1489,7 @@ export const computeSequencePropsStatusFromFilenameByLocation = ({
assetKeys,
effects,
remotionRoot,
+ fileContentsCache,
}),
nodePath: {
absolutePath,
diff --git a/packages/studio-server/src/preview-server/routes/subscribe-to-sequence-props.ts b/packages/studio-server/src/preview-server/routes/subscribe-to-sequence-props.ts
index 7fd787ca25..a35b5c83aa 100644
--- a/packages/studio-server/src/preview-server/routes/subscribe-to-sequence-props.ts
+++ b/packages/studio-server/src/preview-server/routes/subscribe-to-sequence-props.ts
@@ -11,6 +11,8 @@ export const subscribeToSequenceProps: ApiHandler<
SubscribeToSequencePropsBatchRequest,
SubscribeToSequencePropsBatchResponse
> = ({input, remotionRoot, logLevel}) => {
+ // Source contents are shared only within this synchronous subscription batch.
+ const fileContentsCache = new Map<string, string>();
const unresolvedByFile = new Map<
string,
{index: number; line: number; column: number}[]
@@ -32,6 +34,7 @@ export const subscribeToSequenceProps: ApiHandler<
fileName,
targets: unresolved,
remotionRoot,
+ fileContentsCache,
});
for (const [index, nodePath] of nodePaths.entries()) {
if (nodePath) {
@@ -72,6 +75,7 @@ export const subscribeToSequenceProps: ApiHandler<
clientId,
logLevel,
+ fileContentsCache,
}),
);
return Promise.resolve({results});
diff --git a/packages/studio-server/src/preview-server/sequence-props-watchers.ts b/packages/studio-server/src/preview-server/sequence-props-watchers.ts
index 4b1122a860..8e45c13022 100644
--- a/packages/studio-server/src/preview-server/sequence-props-watchers.ts
+++ b/packages/studio-server/src/preview-server/sequence-props-watchers.ts
@@ -50,6 +50,7 @@ const getSequencePropsStatus = ({
effects,
remotionRoot,
logLevel,
+ fileContentsCache,
}: {
fileName: string;
line: number;
@@ -62,6 +63,7 @@ const getSequencePropsStatus = ({
effects: string[][];
remotionRoot: string;
logLevel: LogLevel;
+ fileContentsCache: Map<string, string> | null;
}): SubscribeToSequencePropsResponse => {
if (preferredNodePath) {
try {
@@ -73,6 +75,7 @@ const getSequencePropsStatus = ({
assetKeys,
effects,
remotionRoot,
+ fileContentsCache,
});
return {
status: fromNodePath,
@@ -111,6 +114,7 @@ const getSequencePropsStatus = ({
assetKeys,
effects,
remotionRoot,
+ fileContentsCache,
});
} catch (error) {
if (
@@ -150,6 +154,7 @@ const getSequencePropsStatus = ({
assetKeys,
effects,
remotionRoot,
+ fileContentsCache,
}),
nodePath: {
absolutePath: path.resolve(remotionRoot, fileName),
@@ -182,6 +187,7 @@ const getSequencePropsStatus = ({
effects,
remotionRoot,
logLevel,
+ fileContentsCache,
});
return status;
@@ -200,6 +206,7 @@ export const subscribeToSequencePropsWatchers = ({
remotionRoot,
clientId,
logLevel,
+ fileContentsCache,
}: {
fileName: string;
line: number;
@@ -213,6 +220,7 @@ export const subscribeToSequencePropsWatchers = ({
remotionRoot: string;
clientId: string;
logLevel: LogLevel;
+ fileContentsCache: Map<string, string> | null;
}): SubscribeToSequencePropsResponse => {
const initialResult = getSequencePropsStatus({
fileName,
@@ -226,6 +234,7 @@ export const subscribeToSequencePropsWatchers = ({
effects,
remotionRoot,
logLevel,
+ fileContentsCache,
});
if (!initialResult.success) {
diff --git a/packages/studio-server/src/test/compute-sequence-props-status.test.ts b/packages/studio-server/src/test/compute-sequence-props-status.test.ts
index b9d18bedd7..c89619fb9a 100644
--- a/packages/studio-server/src/test/compute-sequence-props-status.test.ts
+++ b/packages/studio-server/src/test/compute-sequence-props-status.test.ts
@@ -75,6 +75,7 @@ export const Fallback = () => <><Sequence name="First" /><Sequence name="Fallbac
const location = getSourceLocation(input, search);
return computeSequencePropsStatusFromFilenameByLocation({
+ fileContentsCache: null,
fileName,
line: location.line,
column: location.column + columnOffset,
@@ -174,6 +175,7 @@ test('computeSequencePropsStatus should ignore source locations outside the proj
try {
expect(
computeSequencePropsStatusFromFilenameByLocation({
+ fileContentsCache: null,
fileName: '../studio/RefreshCompositionOverlay.js',
line: 1,
column: 1,
@@ -896,6 +898,7 @@ export const Example = () => {
test('canUpdateSequenceProps should flag computed props', () => {
const filePath = path.join(__dirname, 'snapshots', 'light-leak-computed.tsx');
const result = computeSequencePropsStatus({
+ fileContentsCache: null,
fileName: filePath,
nodePath: getNodePath(filePath, 8),
componentIdentity: null,
@@ -1570,6 +1573,7 @@ test('computeSequencePropsStatus should explain why outside-project file reads w
expect(() =>
computeSequencePropsStatus({
+ fileContentsCache: null,
fileName,
nodePath: [],
componentIdentity: null,
@@ -1585,6 +1589,7 @@ test('computeSequencePropsStatus should explain why outside-project file reads w
test('computeSequencePropsStatus should detect static nested props', () => {
const filePath = path.join(__dirname, 'snapshots', 'nested-props.tsx');
const result = computeSequencePropsStatus({
+ fileContentsCache: null,
fileName: filePath,
nodePath: getNodePath(filePath, 7),
componentIdentity: null,
@@ -1611,6 +1616,7 @@ test('computeSequencePropsStatus should detect static nested props', () => {
test('computeSequencePropsStatus should flag computed nested props', () => {
const filePath = path.join(__dirname, 'snapshots', 'nested-props.tsx');
const result = computeSequencePropsStatus({
+ fileContentsCache: null,
fileName: filePath,
nodePath: getNodePath(filePath, 8),
componentIdentity: null,
@@ -1671,6 +1677,7 @@ export const Example = () => {
test('computeSequencePropsStatus should flag computed when parent is not an object', () => {
const filePath = path.join(__dirname, 'snapshots', 'nested-props.tsx');
const result = computeSequencePropsStatus({
+ fileContentsCache: null,
fileName: filePath,
nodePath: getNodePath(filePath, 9),
componentIdentity: null,
@@ -1691,6 +1698,7 @@ test('computeSequencePropsStatus should flag computed when parent is not an obje
test('computeSequencePropsStatus should report unset nested props as undefined', () => {
const filePath = path.join(__dirname, 'snapshots', 'nested-props.tsx');
const result = computeSequencePropsStatus({
+ fileContentsCache: null,
fileName: filePath,
nodePath: getNodePath(filePath, 7),
componentIdentity: null,
@@ -1712,6 +1720,7 @@ test('computeSequencePropsStatus should report unset nested props as undefined',
test('computeSequencePropsStatus should report unset when parent attribute missing', () => {
const filePath = path.join(__dirname, 'snapshots', 'nested-props.tsx');
const result = computeSequencePropsStatus({
+ fileContentsCache: null,
fileName: filePath,
nodePath: getNodePath(filePath, 10),
componentIdentity: null,
@@ -1733,6 +1742,7 @@ test('computeSequencePropsStatus should report unset when parent attribute missi
test('computeSequencePropsStatus should return keyframes for interpolated style props', () => {
const filePath = path.join(__dirname, 'snapshots', 'keyframed-props.tsx');
const result = computeSequencePropsStatus({
+ fileContentsCache: null,
fileName: filePath,
nodePath: getNodePath(filePath, 8),
componentIdentity: null,