Skip to content

🎥 Make videos programmatically with React

`@remotion/lambda-client`: honor custom `requestHandler` when invoking functions (#10418)

Noah83c52bd9cf1e
diff --git a/packages/lambda-client/src/call-lambda-async.ts b/packages/lambda-client/src/call-lambda-async.ts
index fa09d66498..e05c106d2d 100644
--- a/packages/lambda-client/src/call-lambda-async.ts
+++ b/packages/lambda-client/src/call-lambda-async.ts
@@ -6,6 +6,7 @@ import type {
} from '@remotion/serverless-client';
import {getLambdaClient} from './aws-clients';
import type {AwsRegion} from './regions';
+import type {RequestHandler} from './types';
export const callFunctionAsyncImplementation = async <
T extends ServerlessRoutines,
@@ -15,6 +16,7 @@ export const callFunctionAsyncImplementation = async <
payload,
region,
timeoutInTest,
+ requestHandler,
}: CallFunctionOptions<T, Provider>): Promise<void> => {
const stringifiedPayload = JSON.stringify(payload);
if (stringifiedPayload.length > 256 * 1024) {
@@ -26,7 +28,7 @@ export const callFunctionAsyncImplementation = async <
const result = await getLambdaClient(
region as AwsRegion,
timeoutInTest,
- null,
+ (requestHandler ?? null) as RequestHandler | null,
).send(
new InvokeCommand({
FunctionName: functionName,
diff --git a/packages/lambda-client/src/call-lambda-streaming.ts b/packages/lambda-client/src/call-lambda-streaming.ts
index f54fadd5c8..97c2da8b05 100644
--- a/packages/lambda-client/src/call-lambda-streaming.ts
+++ b/packages/lambda-client/src/call-lambda-streaming.ts
@@ -20,6 +20,7 @@ import {
import {getLambdaClient} from './aws-clients';
import {getAwsRegionMetadata} from './aws-region-metadata';
import type {AwsRegion} from './regions';
+import type {RequestHandler} from './types';
const STREAM_STALL_TIMEOUT = 30000;
const LAMBDA_STREAM_STALL = `AWS did not invoke Lambda in ${STREAM_STALL_TIMEOUT}ms`;
@@ -39,17 +40,19 @@ const invokeStreamOrTimeout = async <Provider extends CloudProvider>({
functionName,
type,
payload,
+ requestHandler,
}: {
region: Provider['region'];
timeoutInTest: number;
functionName: string;
type: string;
payload: Record<string, unknown>;
+ requestHandler: Provider['requestHandler'] | null;
}) => {
const resProm = getLambdaClient(
region as AwsRegion,
timeoutInTest,
- null,
+ (requestHandler ?? null) as RequestHandler | null,
).send(
new InvokeWithResponseStreamCommand({
FunctionName: functionName,
@@ -89,6 +92,7 @@ const callLambdaWithStreamingWithoutRetry = async <
region,
timeoutInTest,
receivedStreamingPayload,
+ requestHandler,
}: CallFunctionOptions<T, Provider> & {
receivedStreamingPayload: OnMessage<Provider>;
}): Promise<void> => {
@@ -98,6 +102,7 @@ const callLambdaWithStreamingWithoutRetry = async <
region,
timeoutInTest,
type,
+ requestHandler,
});
const {onData, clear} = makeStreamer((status, messageTypeId, data) => {
diff --git a/packages/lambda-client/src/call-lambda-sync.ts b/packages/lambda-client/src/call-lambda-sync.ts
index b7a3dfb5b2..5c0bcf27c7 100644
--- a/packages/lambda-client/src/call-lambda-sync.ts
+++ b/packages/lambda-client/src/call-lambda-sync.ts
@@ -8,6 +8,7 @@ import type {
} from '@remotion/serverless-client';
import {getLambdaClient} from './aws-clients';
import type {AwsRegion} from './regions';
+import type {RequestHandler} from './types';
const callLambdaSyncWithoutRetry = async <
T extends ServerlessRoutines,
@@ -17,6 +18,7 @@ const callLambdaSyncWithoutRetry = async <
payload,
region,
timeoutInTest,
+ requestHandler,
}: CallFunctionOptions<T, Provider>): Promise<
OrError<ServerlessReturnValues<Provider>[T]>
> => {
@@ -24,7 +26,7 @@ const callLambdaSyncWithoutRetry = async <
const res = await getLambdaClient(
region as AwsRegion,
timeoutInTest,
- null,
+ (requestHandler ?? null) as RequestHandler | null,
).send(
new InvokeCommand({
FunctionName: functionName,
diff --git a/packages/lambda-client/src/get-service-client.ts b/packages/lambda-client/src/get-service-client.ts
index 1cee68f4a2..8f233f65c0 100644
--- a/packages/lambda-client/src/get-service-client.ts
+++ b/packages/lambda-client/src/get-service-client.ts
@@ -26,13 +26,11 @@ const getCredentialsHash = ({
region,
service,
forcePathStyle,
- requestHandler,
}: {
region: string;
customCredentials: CustomCredentials<AwsProvider> | null;
service: keyof ServiceMapping;
forcePathStyle: boolean;
- requestHandler: RequestHandler | null;
}): string => {
const hashComponents: {[key: string]: unknown} = {};
@@ -70,7 +68,6 @@ const getCredentialsHash = ({
hashComponents.region = region;
hashComponents.service = service;
hashComponents.forcePathStyle = forcePathStyle;
- hashComponents.requestHandler = requestHandler;
return random(JSON.stringify(hashComponents)).toString().replace('0.', '');
};
@@ -87,6 +84,24 @@ const _clients: Partial<
>
> = {};
+// Custom handlers cannot be serialized into the cache key; key by identity instead
+const _clientsWithCustomHandler = new WeakMap<
+ object,
+ Partial<Record<string, ServiceMapping[keyof ServiceMapping]>>
+>();
+
+const getCustomHandlerClientCache = (requestHandler: object) => {
+ const existing = _clientsWithCustomHandler.get(requestHandler);
+ if (existing) {
+ return existing;
+ }
+
+ const created: Partial<Record<string, ServiceMapping[keyof ServiceMapping]>> =
+ {};
+ _clientsWithCustomHandler.set(requestHandler, created);
+ return created;
+};
+
export function getServiceClient<T extends keyof ServiceMapping>({
region,
service,
@@ -133,10 +148,13 @@ export function getServiceClient<T extends keyof ServiceMapping>({
customCredentials,
service,
forcePathStyle,
- requestHandler,
});
- if (!_clients[key]) {
+ const cache = requestHandler
+ ? getCustomHandlerClientCache(requestHandler)
+ : _clients;
+
+ if (!cache[key]) {
checkCredentials();
const lambdaOptions =
@@ -148,12 +166,8 @@ export function getServiceClient<T extends keyof ServiceMapping>({
}
: undefined;
- // Merge custom requestHandler with lambda options
- const finalRequestHandler = requestHandler
- ? lambdaOptions
- ? {...requestHandler, ...lambdaOptions}
- : requestHandler
- : lambdaOptions;
+ // Spreading a handler instance (e.g. NodeHttpHandler) would strip its prototype
+ const finalRequestHandler = requestHandler ?? lambdaOptions;
const client = customCredentials
? new Client({
@@ -187,8 +201,8 @@ export function getServiceClient<T extends keyof ServiceMapping>({
return client as ServiceMapping[T];
}
- _clients[key] = client;
+ cache[key] = client;
}
- return _clients[key] as ServiceMapping[T];
+ return cache[key] as ServiceMapping[T];
}