Skip to content

🎥 Make videos programmatically with React

`@remotion/lambda`: Support redirects for webhooks

Jonny Burger1ce5618bdc8b
diff --git a/packages/lambda/src/functions/launch.ts b/packages/lambda/src/functions/launch.ts
index 19fe078b0f..6ecf775295 100644
--- a/packages/lambda/src/functions/launch.ts
+++ b/packages/lambda/src/functions/launch.ts
@@ -555,6 +555,7 @@ export const launchHandler = async (
bucketName: params.bucketName,
customData: params.webhook.customData ?? null,
},
+ redirectsSoFar: 0,
},
params.logLevel,
);
@@ -652,6 +653,7 @@ export const launchHandler = async (
timeToFinish: postRenderData.timeToFinish,
costs: postRenderData.cost,
},
+ redirectsSoFar: 0,
},
params.logLevel,
);
@@ -744,6 +746,7 @@ export const launchHandler = async (
stack: e.stack as string,
})),
},
+ redirectsSoFar: 0,
},
params.logLevel,
);
diff --git a/packages/lambda/src/shared/invoke-webhook.ts b/packages/lambda/src/shared/invoke-webhook.ts
index be2bbd3ba6..3aab3635a9 100644
--- a/packages/lambda/src/shared/invoke-webhook.ts
+++ b/packages/lambda/src/shared/invoke-webhook.ts
@@ -56,6 +56,10 @@ export const mockableHttpClients = {
https: https.request,
};
+// Don't handle 304 status code (Not Modified) as a redirect,
+// since the browser will display the right page.
+const redirectStatusCodes = [301, 302, 303, 307, 308];
+
const getWebhookClient = (url: string) => {
if (url.startsWith('https://')) {
return mockableHttpClients.https;
@@ -72,12 +76,14 @@ type InvokeWebhookOptions = {
payload: WebhookPayload;
url: string;
secret: string | null;
+ redirectsSoFar: number;
};
function invokeWebhookRaw({
payload,
secret,
url,
+ redirectsSoFar,
}: InvokeWebhookOptions): Promise<void> {
const jsonPayload = JSON.stringify(payload);
@@ -97,6 +103,32 @@ function invokeWebhookRaw({
},
(res) => {
if (res.statusCode && res.statusCode > 299) {
+ if (redirectStatusCodes.includes(res.statusCode)) {
+ if (!res.headers.location) {
+ reject(
+ new Error(
+ `Received a status code ${res.statusCode} but no "Location" header while calling ${res.headers.location}`,
+ ),
+ );
+ return;
+ }
+
+ if (redirectsSoFar > 10) {
+ reject(new Error(`Too many redirects while downloading ${url}`));
+ return;
+ }
+
+ invokeWebhookRaw({
+ payload,
+ secret,
+ url: res.headers.location,
+ redirectsSoFar: redirectsSoFar + 1,
+ })
+ .then(resolve)
+ .catch(reject);
+ return;
+ }
+
reject(
new Error(
`Sent a webhook to ${url} but got a status code of ${res.statusCode} with message '${res.statusMessage}'`,