Skip to content

🎥 Make videos programmatically with React

use http client to make request

Jonny Burger8f50dce85bc2
diff --git a/packages/docs/docs/lambda/validate-webhooks.md b/packages/docs/docs/lambda/validate-webhooks.md
index f4504d3ef9..365a74ee1c 100644
--- a/packages/docs/docs/lambda/validate-webhooks.md
+++ b/packages/docs/docs/lambda/validate-webhooks.md
@@ -14,7 +14,7 @@ router.post("/my-remotion-webhook-endpoint", (req, res) => {
.update(JSON.stringify(req.body))
.digest("hex")}`;
- if (signature !== req.header("X-REMOTION-SIGNATURE")) {
+ if (signature !== req.header("X-Remotion-Signature")) {
// Request wasn't sent by Remotion
// ...
} else {
diff --git a/packages/lambda/src/shared/invoke-webhook.ts b/packages/lambda/src/shared/invoke-webhook.ts
index 26b0ebd179..75214c4998 100644
--- a/packages/lambda/src/shared/invoke-webhook.ts
+++ b/packages/lambda/src/shared/invoke-webhook.ts
@@ -1,4 +1,6 @@
import * as Crypto from 'crypto';
+import http from 'http';
+import https from 'https';
/**
* @description Calculates cryptographically secure signature for webhooks using Hmac.
@@ -18,26 +20,62 @@ export type InvokeWebhookInput = {
renderId: string;
};
+const getWebhookClient = (url: string) => {
+ if (url.startsWith('https://')) {
+ return mockableHttpClients.https;
+ }
+
+ if (url.startsWith('http://')) {
+ return mockableHttpClients.http;
+ }
+
+ throw new Error('Can only request URLs starting with http:// or https://');
+};
+
+export const mockableHttpClients = {
+ http: http.request,
+ https: https.request,
+};
+
/**
* @description Calls a webhook.
* @link https://remotion.dev/docs/lambda/rendermediaonlambda#webhook
* @param params.url URL of webhook to call.
*/
-export async function invokeWebhook({url, type, renderId}: InvokeWebhookInput) {
+export function invokeWebhook({url, type, renderId}: InvokeWebhookInput) {
const payload = JSON.stringify({result: type, renderId});
- try {
- const response = await fetch(url, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'X-REMOTION-SIGNATURE': calculateSignature(payload),
+
+ return new Promise<void>((resolve, reject) => {
+ const req = getWebhookClient(url)(
+ url,
+ {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'X-Remotion-Signature': calculateSignature(payload),
+ 'X-Remotion-Status': type,
+ },
},
- body: payload,
+ (res) => {
+ if (res.statusCode && res.statusCode > 299) {
+ reject(
+ new Error(
+ `Sent a webhook but got a status code of ${res.statusCode}`
+ )
+ );
+ return;
+ }
+
+ resolve();
+ }
+ );
+
+ req.on('error', (err) => {
+ reject(err);
});
- if (response.status > 299) {
- // do we want to log webhook delivery failures?
- }
- } catch {
- // network error ocurred. Do we log this?
- }
+
+ req.write(payload);
+
+ req.end();
+ });
}
diff --git a/packages/lambda/src/test/integration/webhooks.test.ts b/packages/lambda/src/test/integration/webhooks.test.ts
index b203d16ac8..46cc8a3715 100644
--- a/packages/lambda/src/test/integration/webhooks.test.ts
+++ b/packages/lambda/src/test/integration/webhooks.test.ts
@@ -2,6 +2,7 @@ import {RenderInternals} from '@remotion/renderer';
import {VERSION} from 'remotion/version';
import {LambdaRoutines} from '../../defaults';
import {handler} from '../../functions';
+import {mockableHttpClients} from '../../shared/invoke-webhook';
import type {LambdaReturnValues} from '../../shared/return-values';
import {disableLogs, enableLogs} from '../disable-logs';
@@ -14,21 +15,28 @@ const extraContext = {
type Await<T> = T extends PromiseLike<infer U> ? U : T;
-let originalFetch = global.fetch;
+const originalFetch = mockableHttpClients.http;
beforeEach(() => {
- originalFetch = global.fetch;
- global.fetch = jest.fn(() =>
- Promise.resolve({
- statusText: 'OK',
- ok: true,
- status: 201,
- json: () => Promise.resolve({}),
- })
- ) as jest.Mock;
+ // @ts-expect-error
+ mockableHttpClients.http = jest.fn(
+ (
+ _url: string,
+ _options: unknown,
+ cb: (a: {statusCode: number}) => void
+ ) => {
+ cb({
+ statusCode: 201,
+ });
+ return {
+ on: () => undefined,
+ end: () => undefined,
+ };
+ }
+ );
});
afterEach(() => {
- global.fetch = originalFetch;
+ mockableHttpClients.http = originalFetch;
});
beforeAll(() => {
@@ -94,15 +102,19 @@ describe('Webhooks', () => {
extraContext
)) as Await<LambdaReturnValues[LambdaRoutines.status]>;
- expect(fetch).toHaveBeenCalledTimes(1);
- expect(fetch).toHaveBeenCalledWith(TEST_URL, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'X-REMOTION-SIGNATURE': expect.stringContaining('sha1='),
+ expect(mockableHttpClients.http).toHaveBeenCalledTimes(1);
+ expect(mockableHttpClients.http).toHaveBeenCalledWith(
+ TEST_URL,
+ {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'X-Remotion-Signature': expect.stringContaining('sha1='),
+ 'X-Remotion-Status': 'success',
+ },
},
- body: expect.stringContaining('success'),
- });
+ expect.anything()
+ );
});
test('Should call webhook upon timeout', async () => {
@@ -155,14 +167,18 @@ describe('Webhooks', () => {
extraContext
)) as Await<LambdaReturnValues[LambdaRoutines.status]>;
- expect(fetch).toHaveBeenCalledTimes(1);
- expect(fetch).toHaveBeenCalledWith(TEST_URL, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'X-REMOTION-SIGNATURE': expect.stringContaining('sha1='),
+ expect(mockableHttpClients.http).toHaveBeenCalledTimes(1);
+ expect(mockableHttpClients.http).toHaveBeenCalledWith(
+ TEST_URL,
+ {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'X-Remotion-Signature': expect.stringContaining('sha1='),
+ 'X-Remotion-Status': 'timeout',
+ },
},
- body: expect.stringContaining('timeout'),
- });
+ expect.anything()
+ );
});
});