Skip to content

🎥 Make videos programmatically with React

write output files

Jonny Burger98cec0a9d16c
diff --git a/packages/cli/src/preview-server/routes.ts b/packages/cli/src/preview-server/routes.ts
index c5d39c3f32..1373e06da5 100644
--- a/packages/cli/src/preview-server/routes.ts
+++ b/packages/cli/src/preview-server/routes.ts
@@ -2,7 +2,7 @@ import {BundlerInternals} from '@remotion/bundler';
import {RenderInternals} from '@remotion/renderer';
import {createReadStream, existsSync, statSync} from 'node:fs';
import type {IncomingMessage, ServerResponse} from 'node:http';
-import path from 'node:path';
+import path, {join} from 'node:path';
import {URLSearchParams} from 'node:url';
import {ConfigInternals} from '../config';
import {getNumberOfSharedAudioTags} from '../config/number-of-shared-audio-tags';
@@ -34,6 +34,13 @@ const static404 = (response: ServerResponse) => {
);
};
+const output404 = (response: ServerResponse) => {
+ response.writeHead(404);
+ response.end(
+ 'The output/ prefix has been changed, this URL is no longer valid.',
+ );
+};
+
const handleFallback = async ({
remotionRoot,
hash,
@@ -247,8 +254,10 @@ const handleFavicon = (_: IncomingMessage, response: ServerResponse) => {
};
export const handleRoutes = ({
- hash,
- hashPrefix,
+ staticHash,
+ staticHashPrefix,
+ outputHash,
+ outputHashPrefix,
request,
response,
liveEventsServer,
@@ -258,8 +267,10 @@ export const handleRoutes = ({
entryPoint,
publicDir,
}: {
- hash: string;
- hashPrefix: string;
+ staticHash: string;
+ staticHashPrefix: string;
+ outputHash: string;
+ outputHashPrefix: string;
request: IncomingMessage;
response: ServerResponse;
liveEventsServer: LiveEventsServer;
@@ -311,17 +322,47 @@ export const handleRoutes = ({
return liveEventsServer.router(request, response);
}
- if (url.pathname.startsWith(hash)) {
- return serveStatic(publicDir, hash, request, response);
+ if (url.pathname.startsWith(staticHash)) {
+ const filename = new URL(
+ request.url as string,
+ 'http://localhost',
+ ).pathname.replace(new RegExp(`^${staticHash}`), '');
+ const filePath = join(publicDir, decodeURIComponent(filename));
+
+ return serveStatic({
+ root: publicDir,
+ path: filePath,
+ req: request,
+ res: response,
+ });
}
- if (url.pathname.startsWith(hashPrefix)) {
+ if (url.pathname.startsWith(staticHashPrefix)) {
return static404(response);
}
+ if (url.pathname.startsWith(outputHash)) {
+ const filename = new URL(
+ request.url as string,
+ 'http://localhost',
+ ).pathname.replace(new RegExp(`^${outputHash}`), '');
+ const filePath = join(remotionRoot, decodeURIComponent(filename));
+
+ return serveStatic({
+ root: remotionRoot,
+ path: filePath,
+ req: request,
+ res: response,
+ });
+ }
+
+ if (url.pathname.startsWith(outputHashPrefix)) {
+ return output404(response);
+ }
+
return handleFallback({
remotionRoot,
- hash,
+ hash: staticHash,
response,
getCurrentInputProps,
getEnvVariables,
diff --git a/packages/cli/src/preview-server/serve-static.ts b/packages/cli/src/preview-server/serve-static.ts
index 8da9f0199d..9590f71c6f 100644
--- a/packages/cli/src/preview-server/serve-static.ts
+++ b/packages/cli/src/preview-server/serve-static.ts
@@ -9,16 +9,20 @@
import {RenderInternals} from '@remotion/renderer';
import {createReadStream, existsSync, promises} from 'node:fs';
import type {IncomingMessage, ServerResponse} from 'node:http';
-import {join} from 'node:path';
import {getValueContentRangeHeader} from './dev-middleware/middleware';
import {parseRange} from './dev-middleware/range-parser';
-export const serveStatic = async function (
- root: string,
- hash: string,
- req: IncomingMessage,
- res: ServerResponse,
-) {
+export const serveStatic = async function ({
+ root,
+ path,
+ req,
+ res,
+}: {
+ root: string;
+ path: string;
+ req: IncomingMessage;
+ res: ServerResponse;
+}) {
if (req.method !== 'GET' && req.method !== 'HEAD') {
// method not allowed
res.statusCode = 405;
@@ -28,12 +32,6 @@ export const serveStatic = async function (
return;
}
- const filename = new URL(
- req.url as string,
- 'http://localhost',
- ).pathname.replace(new RegExp(`^${hash}`), '');
- const path = join(root, decodeURIComponent(filename));
-
if (!RenderInternals.isPathInside(path, root)) {
res.writeHead(500);
res.write('Not allowed to read');
diff --git a/packages/cli/src/preview-server/start-server.ts b/packages/cli/src/preview-server/start-server.ts
index cf4ebab61a..507a5554f7 100644
--- a/packages/cli/src/preview-server/start-server.ts
+++ b/packages/cli/src/preview-server/start-server.ts
@@ -25,8 +25,10 @@ export const startServer = async (options: {
publicDir: string;
userPassedPublicDir: string | null;
poll: number | null;
- hash: string;
- hashPrefix: string;
+ staticHash: string;
+ staticHashPrefix: string;
+ outputHash: string;
+ outputHashPrefix: string;
}): Promise<{
port: number;
liveEventsServer: LiveEventsServer;
@@ -70,8 +72,10 @@ export const startServer = async (options: {
})
.then(() => {
handleRoutes({
- hash: options.hash,
- hashPrefix: options.hashPrefix,
+ staticHash: options.staticHash,
+ staticHashPrefix: options.staticHashPrefix,
+ outputHash: options.outputHash,
+ outputHashPrefix: options.outputHashPrefix,
request: request as IncomingMessage,
response,
liveEventsServer,
diff --git a/packages/cli/src/studio.ts b/packages/cli/src/studio.ts
index 72af7f09b9..c198c12359 100644
--- a/packages/cli/src/studio.ts
+++ b/packages/cli/src/studio.ts
@@ -108,8 +108,13 @@ export const studioCommand = async (remotionRoot: string, args: string[]) => {
remotionRoot,
});
- const hashPrefix = '/static-';
- const staticHash = `${hashPrefix}${crypto.randomBytes(6).toString('hex')}`;
+ const hash = crypto.randomBytes(6).toString('hex');
+
+ const outputHashPrefix = '/outputs-';
+ const outputHash = `${outputHashPrefix}${hash}`;
+
+ const staticHashPrefix = '/static-';
+ const staticHash = `${staticHashPrefix}${hash}`;
initPublicFolderWatch({
publicDir,
@@ -129,7 +134,6 @@ export const studioCommand = async (remotionRoot: string, args: string[]) => {
});
});
},
-
staticHash,
});
@@ -148,8 +152,10 @@ export const studioCommand = async (remotionRoot: string, args: string[]) => {
webpackOverride: ConfigInternals.getWebpackOverrideFn(),
poll: ConfigInternals.getWebpackPolling(),
userPassedPublicDir: ConfigInternals.getPublicDir(),
- hash: staticHash,
- hashPrefix,
+ staticHash,
+ staticHashPrefix,
+ outputHash,
+ outputHashPrefix,
});
setLiveEventsListener(liveEventsServer);