Skip to content

🎥 Make videos programmatically with React

7.1 KB
Raw Blame
import type {InvokeWithResponseStreamCommandOutput} from '@aws-sdk/client-lambda';
import {
	InvokeWithResponseStreamCommand,
	type InvokeWithResponseStreamResponseEvent,
} from '@aws-sdk/client-lambda';
import {makeStreamer} from '@remotion/streaming';
import {getLambdaClient} from './aws-clients';
import {getAwsRegionMetadata} from './aws-region-metadata';
import type {AwsRegion} from './regions';
/* eslint-disable no-console */
import type {ServerlessRoutines} from './shared-constants';
import {
	binaryPayloadSinkForStreamer,
	formatMap,
	messageTypeIdToMessageType,
	type GetBinaryPayloadSink,
	type MessageTypeId,
	type OnMessage,
	type StreamingMessage,
} from './streaming/streaming';
import type {CallFunctionOptions} from './types';
import type {RequestHandler} from './types';

const STREAM_STALL_TIMEOUT = 30000;
const LAMBDA_STREAM_STALL = `AWS did not invoke Lambda in ${STREAM_STALL_TIMEOUT}ms`;

export const parseJsonOrThrowSource = (data: Uint8Array, type: string) => {
	const asString = new TextDecoder('utf-8').decode(data);
	try {
		return JSON.parse(asString);
	} catch {
		throw new Error(`Invalid JSON (${type}): ${asString}`);
	}
};

const invokeStreamOrTimeout = async ({
	region,
	timeoutInTest,
	functionName,
	type,
	payload,
	requestHandler,
}: {
	region: AwsRegion;
	timeoutInTest: number;
	functionName: string;
	type: string;
	payload: Record<string, unknown>;
	requestHandler: RequestHandler | null;
}) => {
	const resProm = getLambdaClient(
		region as AwsRegion,
		timeoutInTest,
		(requestHandler ?? null) as RequestHandler | null,
	).send(
		new InvokeWithResponseStreamCommand({
			FunctionName: functionName,
			Payload: JSON.stringify({type, ...payload}),
		}),
	);

	let cleanup = () => undefined;

	const timeout = new Promise<InvokeWithResponseStreamCommandOutput>(
		(_resolve, reject) => {
			const int = setTimeout(() => {
				reject(new Error(LAMBDA_STREAM_STALL));
			}, STREAM_STALL_TIMEOUT);
			cleanup = () => {
				clearTimeout(int);
			};
		},
	);

	const res = await Promise.race([resProm, timeout]);

	cleanup();

	return res;
};

const INVALID_JSON_MESSAGE = 'Cannot parse Lambda response as JSON';

const callLambdaWithStreamingWithoutRetry = async <
	T extends ServerlessRoutines,
>({
	functionName,
	type,
	payload,
	region,
	timeoutInTest,
	receivedStreamingPayload,
	requestHandler,
	getBinaryPayloadSink,
}: CallFunctionOptions<T> & {
	receivedStreamingPayload: OnMessage;
	getBinaryPayloadSink: GetBinaryPayloadSink | null;
}): Promise<void> => {
	const res = await invokeStreamOrTimeout({
		functionName,
		payload,
		region,
		timeoutInTest,
		type,
		requestHandler,
	});

	const {onData, clear} = makeStreamer(
		(status, messageTypeId, data) => {
			const messageType = messageTypeIdToMessageType(
				messageTypeId as MessageTypeId,
			);
			const innerPayload =
				formatMap[messageType] === 'json'
					? parseJsonOrThrowSource(data, messageType)
					: data;

			const message: StreamingMessage = {
				successType: status,
				message: {
					type: messageType,
					payload: innerPayload,
				},
			};

			receivedStreamingPayload(message);
		},
		getBinaryPayloadSink
			? binaryPayloadSinkForStreamer(getBinaryPayloadSink)
			: null,
	);

	const dumpBuffers = () => {
		clear();
	};

	// @ts-expect-error - We are adding a listener to a global variable
	if (globalThis._dumpUnreleasedBuffers) {
		// @ts-expect-error - We are adding a listener to a global variable
		(globalThis._dumpUnreleasedBuffers as EventEmitter).addListener(
			'dump-unreleased-buffers',
			dumpBuffers,
		);
	}

	const events =
		res.EventStream as AsyncIterable<InvokeWithResponseStreamResponseEvent>;

	try {
		for await (const event of events) {
			// There are two types of events you can get on a stream.

			// `PayloadChunk`: These contain the actual raw bytes of the chunk
			// It has a single property: `Payload`
			if (event.PayloadChunk && event.PayloadChunk.Payload) {
				// Awaiting applies backpressure to the response stream when the
				// payload is being streamed to a sink
				await onData(event.PayloadChunk.Payload);
			}

			if (event.InvokeComplete) {
				if (event.InvokeComplete.ErrorCode) {
					const {consoleDomain} = getAwsRegionMetadata(region as AwsRegion);
					const logs = `https://${region}.${consoleDomain}/cloudwatch/home?region=${region}#logsV2:logs-insights$3FqueryDetail$3D~(end~0~start~-3600~timeType~'RELATIVE~unit~'seconds~editorString~'fields*20*40timestamp*2c*20*40requestId*2c*20*40message*0a*7c*20filter*20*40requestId*20like*20*${res.$metadata.requestId}*22*0a*7c*20sort*20*40timestamp*20asc~source~(~'*2faws*2flambda*2f${functionName}))`;
					if (event.InvokeComplete.ErrorCode === 'Unhandled') {
						throw new Error(
							`Lambda function ${functionName} failed with an unhandled error: ${
								event.InvokeComplete.ErrorDetails as string
							} See ${logs} to see the logs of this invocation.`,
						);
					}

					throw new Error(
						`Lambda function ${functionName} failed with error code ${event.InvokeComplete.ErrorCode}: ${event.InvokeComplete.ErrorDetails}. See ${logs} to see the logs of this invocation.`,
					);
				}
			}

			// Don't put a `break` statement here, as it will cause the socket to not properly exit.
		}
	} finally {
		// @ts-expect-error - We are adding a listener to a global variable
		if (globalThis._dumpUnreleasedBuffers) {
			// @ts-expect-error - We are adding a listener to a global variable
			(globalThis._dumpUnreleasedBuffers as EventEmitter).removeListener(
				'dump-unreleased-buffers',
				dumpBuffers,
			);
		}

		// Also closes the active payload sink if the stream aborted
		// mid-payload, so no file descriptor is leaked
		clear();
	}
};

export const callFunctionWithStreamingImplementation = async <
	T extends ServerlessRoutines,
>(
	options: CallFunctionOptions<T> & {
		receivedStreamingPayload: OnMessage;
		retriesRemaining: number;
		getBinaryPayloadSink: GetBinaryPayloadSink | null;
	},
): Promise<void> => {
	// As of August 2023, Lambda streaming sometimes misses parts of the JSON response.
	// Handling this for now by applying a retry mechanism.

	try {
		// Do not remove this await
		await callLambdaWithStreamingWithoutRetry<T>(options);
	} catch (err) {
		if (
			(err as Error).stack?.includes('TooManyRequestsException') ||
			(err as Error).message?.includes('ConcurrentInvocationLimitExceeded')
		) {
			throw new Error(
				`AWS Concurrency limit reached (Original Error: ${(err as Error).message}). See https://www.remotion.dev/docs/lambda/troubleshooting/rate-limit for tips to fix this.`,
			);
		}

		if (
			!(err as Error).message.includes(INVALID_JSON_MESSAGE) &&
			!(err as Error).message.includes(LAMBDA_STREAM_STALL) &&
			// https://discord.com/channels/809501355504959528/1332166561242288220/1332166561242288220
			!(err as Error).message.includes('Runtime.TruncatedResponse') &&
			!(err as Error).message.includes('aborted')
		) {
			throw err;
		}

		console.error('Retries remaining:', options.retriesRemaining);
		if (options.retriesRemaining === 0) {
			console.error('Throwing error:');
			throw err;
		}

		console.error(err);
		return callFunctionWithStreamingImplementation({
			...options,
			retriesRemaining: options.retriesRemaining - 1,
		});
	}
};