🎥 Make videos programmatically with React
new options system
Jonny Burgerba8391732359
diff --git a/packages/docs/docs/lambda/webhooks.md b/packages/docs/docs/lambda/webhooks.mdindex a911d12d5c..454b5d8b19 100644--- a/packages/docs/docs/lambda/webhooks.md+++ b/packages/docs/docs/lambda/webhooks.md@@ -78,7 +78,9 @@ type WebhookPayload =};```-The fields [`renderId`](/docs/lambda/rendermediaonlambda#renderid), [`bucketName`](/docs/lambda/rendermediaonlambda#bucketname) will be returned [just like they are returned by `renderMediaOnLambda()` itself](/docs/lambda/rendermediaonlambda#return-value). The field `customData` can be set to a JSON-serializable object and it's useful to pass on custom data to the webhook endpoint.+The fields [`renderId`](/docs/lambda/rendermediaonlambda#renderid), [`bucketName`](/docs/lambda/rendermediaonlambda#bucketname) will be returned [just like they are returned by `renderMediaOnLambda()` itself](/docs/lambda/rendermediaonlambda#return-value).++You can use the field `customData` to set a JSON-serializable object, which is useful to pass on custom data to the webhook endpoint. **The `customData` field must be less than 1KB (1024 bytes) when serialized, otherwise an error is thrown**. Store larger data in `inputProps` and retrieve it back by calling [`getRenderProgress()`](/docs/lambda/get-render-progress) and reading `progress.renderMetadata.inputProps`.If the render process times out, the reponse body will not contain any other fields.@@ -229,14 +231,14 @@ const ENABLE_TESTING = true;export default async function handler(req: NextApiRequest,- res: NextApiResponse+ res: NextApiResponse,) {if (ENABLE_TESTING) {res.setHeader("Access-Control-Allow-Origin", "https://www.remotion.dev");res.setHeader("Access-Control-Allow-Methods", "OPTIONS,POST");res.setHeader("Access-Control-Allow-Headers",- "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, X-Remotion-Status, X-Remotion-Signature, X-Remotion-Mode"+ "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version, X-Remotion-Status, X-Remotion-Signature, X-Remotion-Mode",);}if (req.method === "OPTIONS") {diff --git a/packages/lambda/src/api/render-media-on-lambda.ts b/packages/lambda/src/api/render-media-on-lambda.tsindex 452a27587a..4712c05805 100644--- a/packages/lambda/src/api/render-media-on-lambda.ts+++ b/packages/lambda/src/api/render-media-on-lambda.ts@@ -12,19 +12,13 @@ import type {import type {BrowserSafeApis} from '@remotion/renderer/client';import type {AwsRegion} from '../pricing/aws-regions';import {callLambda} from '../shared/call-lambda';-import type {OutNameInput, Privacy} from '../shared/constants';+import type {OutNameInput, Privacy, WebhookOption} from '../shared/constants';import {LambdaRoutines} from '../shared/constants';import type {DownloadBehavior} from '../shared/content-disposition-header';import {getCloudwatchRendererUrl, getS3RenderUrl} from '../shared/get-aws-urls';import type {LambdaCodec} from '../shared/validate-lambda-codec';import {makeLambdaRenderMediaPayload} from './make-lambda-payload';-export type LambdaWebhookOption = {- url: string;- secret: string | null;- customData?: Record<string, unknown>;-};-export type RenderMediaOnLambdaInput = {region: AwsRegion;functionName: string;@@ -60,7 +54,7 @@ export type RenderMediaOnLambdaInput = {overwrite?: boolean;audioBitrate?: string | null;videoBitrate?: string | null;- webhook?: LambdaWebhookOption | null;+ webhook?: WebhookOption | null;forceWidth?: number | null;forceHeight?: number | null;rendererFunctionName?: string | null;diff --git a/packages/lambda/src/cli/args.ts b/packages/lambda/src/cli/args.tsindex 5da1143374..839d48b07c 100644--- a/packages/lambda/src/cli/args.ts+++ b/packages/lambda/src/cli/args.ts@@ -1,4 +1,5 @@import {CliInternals} from '@remotion/cli';+import type {BrowserSafeApis} from '@remotion/renderer/client';import type {AwsRegion} from '../pricing/aws-regions';import type {Privacy} from '../shared/constants';@@ -28,7 +29,7 @@ type LambdaCommandLineOptions = {privacy: Privacy;webhook: string | undefined;['webhook-secret']: string | undefined;- ['webhook-custom-data']: string | undefined;+ [BrowserSafeApis.options.webhookCustomDataOption.cliFlag]: string | undefined;['renderer-function-name']: string | undefined;['function-name']: string | undefined;['force-bucket-name']: string | undefined;diff --git a/packages/lambda/src/cli/commands/render/render.ts b/packages/lambda/src/cli/commands/render/render.tsindex a31ccbd647..7a4a264545 100644--- a/packages/lambda/src/cli/commands/render/render.ts+++ b/packages/lambda/src/cli/commands/render/render.ts@@ -21,6 +21,7 @@ import {validateServeUrl} from '../../../shared/validate-serveurl';import {parsedLambdaCli} from '../../args';import {getAwsRegion} from '../../get-aws-region';import {findFunctionName} from '../../helpers/find-function-name';+import {getWebhookCustomData} from '../../helpers/get-webhook-custom-data';import {quit} from '../../helpers/quit';import {Log} from '../../log';import {makeMultiProgressFromStatus, makeProgressString} from './progress';@@ -146,10 +147,7 @@ export const renderCommand = async (args: string[], remotionRoot: string) => {const framesPerLambda = parsedLambdaCli['frames-per-lambda'] ?? undefined;validateFramesPerLambda({framesPerLambda, durationInFrames: 1});- // TODO: validate and parse webhook custom data elsewhere- const webhookCustomData = parsedLambdaCli['webhook-custom-data']- ? JSON.parse(parsedLambdaCli['webhook-custom-data'])- : null;+ const webhookCustomData = getWebhookCustomData();const res = await renderMediaOnLambda({functionName,diff --git a/packages/lambda/src/cli/helpers/get-webhook-custom-data.ts b/packages/lambda/src/cli/helpers/get-webhook-custom-data.tsnew file mode 100644index 0000000000..a664dca51e--- /dev/null+++ b/packages/lambda/src/cli/helpers/get-webhook-custom-data.ts@@ -0,0 +1,47 @@+import {BrowserSafeApis} from '@remotion/renderer/client';+import fs from 'node:fs';+import os from 'node:os';+import path from 'node:path';+import {parsedLambdaCli} from '../args';+import {Log} from '../log';++export const getWebhookCustomData = (): Record<string, unknown> | null => {+ const flagName = BrowserSafeApis.options.webhookCustomDataOption.cliFlag;+ const webhookFlag = parsedLambdaCli[flagName];++ if (!webhookFlag) {+ return null;+ }++ const jsonFile = path.resolve(process.cwd(), webhookFlag);+ try {+ if (fs.existsSync(jsonFile)) {+ const rawJsonData = fs.readFileSync(jsonFile, 'utf-8');++ return JSON.parse(rawJsonData);+ }++ return JSON.parse(webhookFlag);+ } catch (err) {+ Log.error(+ `You passed --${flagName} but it was neither valid JSON nor a file path to a valid JSON file. Provided value: ${webhookFlag}`,+ );+ Log.info('Got the following value:', webhookFlag);+ Log.error(+ 'Check that your input is parseable using `JSON.parse` and try again.',+ );+ if (os.platform() === 'win32') {+ Log.warn(+ 'Note: Windows handles escaping of quotes very weirdly in the command line.',+ );+ Log.warn('This might have led to you having this problem.');+ Log.warn(+ 'Consider using the alternative API for --props which is to pass',+ );+ Log.warn('a path to a JSON file:');+ Log.warn(` --${flagName}=path/to/props.json`);+ }++ process.exit(1);+ }+};diff --git a/packages/lambda/src/shared/constants.ts b/packages/lambda/src/shared/constants.tsindex 683c5d3c41..4bd2ab3278 100644--- a/packages/lambda/src/shared/constants.ts+++ b/packages/lambda/src/shared/constants.ts@@ -7,9 +7,11 @@ import type {PixelFormat,ProResProfile,StillImageFormat,+ ToOptions,VideoImageFormat,X264Preset,} from '@remotion/renderer';+import type {BrowserSafeApis} from '@remotion/renderer/client';import type {VideoConfig} from 'remotion';import type {ChunkRetry} from '../functions/helpers/get-retry-stats';import type {EnhancedErrorInfo} from '../functions/helpers/write-lambda-error';@@ -207,11 +209,19 @@ export enum LambdaRoutines {compositions = 'compositions',}-type WebhookOption = null | {- url: string;- secret: string | null;- customData?: Record<string, unknown>;-};+type Prettify<T> = {+ [K in keyof T]: T[K];+} & {};++export type WebhookOption = Prettify<+ | null+ | ({+ url: string;+ secret: string | null;+ } & Partial<+ ToOptions<[typeof BrowserSafeApis.options.webhookCustomDataOption]>+ >)+>;export type SerializedInputProps =| {diff --git a/packages/renderer/src/client.ts b/packages/renderer/src/client.tsindex 881e0e0edb..4e486291a2 100644--- a/packages/renderer/src/client.ts+++ b/packages/renderer/src/client.ts@@ -25,6 +25,7 @@ import {optionsMap} from './options/options-map';import {scaleOption} from './options/scale';import {videoBitrate} from './options/video-bitrate';import {videoCodecOption} from './options/video-codec';+import {webhookCustomDataOption} from './options/webhook-custom-data';import {DEFAULT_PIXEL_FORMAT, validPixelFormats} from './pixel-format';import {proResProfileOptions} from './prores-profile';import {validateOutputFilename} from './validate-output-filename';@@ -56,6 +57,7 @@ export const BrowserSafeApis = {muteOption,videoCodecOption,offthreadVideoCacheSizeInBytesOption,+ webhookCustomDataOption,},optionsMap,codecSupportsCrf,diff --git a/packages/renderer/src/options/webhook-custom-data.tsx b/packages/renderer/src/options/webhook-custom-data.tsxnew file mode 100644index 0000000000..b8081552a6--- /dev/null+++ b/packages/renderer/src/options/webhook-custom-data.tsx@@ -0,0 +1,15 @@+import {AnyRemotionOption} from './option';++export const webhookCustomDataOption = {+ name: 'Webhook custom data',+ cliFlag: 'webhook-custom-data' as const,+ description: (+ <>+ Pass up to 1,024 bytes of a JSON-serializable object to the webhook. This+ data will be included in the webhook payload.+ </>+ ),+ ssrName: 'customData' as const,+ docLink: 'https://www.remotion.dev/docs/lambda/webhooks',+ type: {} as Record<string, unknown> | null,+} satisfies AnyRemotionOption;