🎥 Make videos programmatically with React
remove need for Internals.isInLambda()
Jonny Burger1ba62c48dbbf
diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.tsindex ab68ad7789..992712c936 100644--- a/packages/core/src/internals.ts+++ b/packages/core/src/internals.ts@@ -147,7 +147,6 @@ import {} from './config/chromium-flags';import {validateOpenGlRenderer} from './validation/validate-opengl-renderer';import {OpenGlRenderer} from './validation/validate-opengl-renderer';-import {isInLambda} from './is-in-lambda';import {getPreviewDomElement} from './get-preview-dom-element';const Timeline = {...TimelinePosition, ...TimelineInOutPosition};@@ -231,7 +230,6 @@ export const Internals = {invalidCompositionErrorMessage,isCompositionIdValid,DEFAULT_OVERWRITE,- isInLambda,AssetCompression,defaultOverrideFunction,DEFAULT_PUPPETEER_TIMEOUT,diff --git a/packages/lambda/src/cli/get-aws-region.ts b/packages/lambda/src/cli/get-aws-region.tsindex 85f6275ffc..fed63de592 100644--- a/packages/lambda/src/cli/get-aws-region.ts+++ b/packages/lambda/src/cli/get-aws-region.ts@@ -1,11 +1,11 @@-import {Internals} from 'remotion';import {AwsRegion} from '../pricing/aws-regions';import {DEFAULT_REGION} from '../shared/constants';+import {isInLambda} from '../shared/is-in-lambda';import {validateAwsRegion} from '../shared/validate-aws-region';import {parsedLambdaCli} from './args';export const getAwsRegion = (): AwsRegion => {- if (Internals.isInLambda()) {+ if (isInLambda()) {throw new Error('Should not call getAwsRegion() if in lambda');}diff --git a/packages/lambda/src/functions/helpers/get-current-region.ts b/packages/lambda/src/functions/helpers/get-current-region.tsindex 15bf55da1f..5120cc8584 100644--- a/packages/lambda/src/functions/helpers/get-current-region.ts+++ b/packages/lambda/src/functions/helpers/get-current-region.ts@@ -1,8 +1,8 @@-import {Internals} from 'remotion';import {AwsRegion} from '../../pricing/aws-regions';+import {isInLambda} from '../../shared/is-in-lambda';export const getCurrentRegionInFunction = () => {- if (!Internals.isInLambda()) {+ if (!isInLambda()) {throw new Error('Should not call getCurrentRegion() if not inside a lambda function');diff --git a/packages/lambda/src/shared/aws-clients.ts b/packages/lambda/src/shared/aws-clients.tsindex cfacb191a1..0cfde10572 100644--- a/packages/lambda/src/shared/aws-clients.ts+++ b/packages/lambda/src/shared/aws-clients.ts@@ -2,9 +2,9 @@ import {CloudWatchLogsClient} from '@aws-sdk/client-cloudwatch-logs';import {IAMClient} from '@aws-sdk/client-iam';import {LambdaClient} from '@aws-sdk/client-lambda';import {S3Client} from '@aws-sdk/client-s3';-import {Internals} from 'remotion';import {AwsRegion} from '../pricing/aws-regions';import {checkCredentials} from './check-credentials';+import {isInLambda} from './is-in-lambda';const _clients: Partial<Record<@@ -23,7 +23,7 @@ const getCredentials = () => {accessKeyId: process.env.REMOTION_AWS_ACCESS_KEY_ID,secretAccessKey: process.env.REMOTION_AWS_SECRET_ACCESS_KEY,}- : Internals.isInLambda()+ : isInLambda()? undefined: process.env.AWS_ACCESS_KEY_ID && process.env.AWS_SECRET_ACCESS_KEY? {diff --git a/packages/lambda/src/shared/is-in-lambda.ts b/packages/lambda/src/shared/is-in-lambda.tsnew file mode 100644index 0000000000..1616c3261d--- /dev/null+++ b/packages/lambda/src/shared/is-in-lambda.ts@@ -0,0 +1,2 @@+export const isInLambda = () =>+ Boolean(typeof process !== 'undefined' && process?.env?.LAMBDA_TASK_ROOT);diff --git a/packages/renderer/src/tmp-dir.ts b/packages/renderer/src/tmp-dir.tsindex 78b9be5db2..cd32cddf5f 100644--- a/packages/renderer/src/tmp-dir.ts+++ b/packages/renderer/src/tmp-dir.ts@@ -16,21 +16,15 @@ const randomHash = (): string => {};export const tmpDir = (str: string) => {- if (Internals.isInLambda()) {- const dir = '/tmp/' + str + randomHash();-- if (fs.existsSync(dir)) {- (fs.rmSync ?? fs.rmdirSync)(dir, {- recursive: true,- force: true,- });- }+ const newDir = path.join(os.tmpdir(), str + randomHash());- mkdirSync(dir);- return dir;+ if (fs.existsSync(newDir)) {+ (fs.rmSync ?? fs.rmdirSync)(newDir, {+ recursive: true,+ force: true,+ });}- const newDir = path.join(os.tmpdir(), str + randomHash());mkdirSync(newDir);return newDir;};diff --git a/tests/lambda/lambda-browser-bundle.test.ts b/tests/lambda/lambda-browser-bundle.test.tsindex 2770af47c1..a58d7b2af6 100644--- a/tests/lambda/lambda-browser-bundle.test.ts+++ b/tests/lambda/lambda-browser-bundle.test.ts@@ -1,8 +1,7 @@import path from "path";-import esbuild = require("esbuild");import { tmpdir } from "os";import fs from "fs";-+import esbuild = require("esbuild");test("Should not be able to bundle @remotion/lambda directly", async () => {expect(() =>esbuild.build({@@ -15,16 +14,38 @@ test("Should not be able to bundle @remotion/lambda directly", async () => {).rejects.toThrow(/Could not resolve "pnpapi"/);});-test("Should be able to bundle @remotion/lambda/client with ESBuild", async () => {+describe("Should be able to bundle @remotion/lambda/client with ESBuild", () => {const outfile = path.join(tmpdir(), "esbuild-test.js");- const { errors } = await esbuild.build({- platform: "node",- target: "node14",- bundle: true,- logLevel: "silent",- outfile,- entryPoints: [require.resolve("@remotion/lambda/client")],++ test("Should build without errors", async () => {+ const { errors } = await esbuild.build({+ platform: "node",+ target: "node14",+ bundle: true,+ logLevel: "silent",+ outfile,+ entryPoints: [require.resolve("@remotion/lambda/client")],+ });+ expect(errors.length).toBe(0);+ });++ test("Bundle should be below 6MB", async () => {+ const file = await fs.promises.readFile(outfile, "utf-8");+ expect(file.length).toBeGreaterThan(10000);+ expect(file.length).toBeLessThanOrEqual(6000000);+ });++ test("Bundle should not include core Remotion", async () => {+ const file = await fs.promises.readFile(outfile, "utf-8");+ expect(file).not.toContain("AbsoluteFill");+ });++ test("Bundle should not include Renderer", async () => {+ const file = await fs.promises.readFile(outfile, "utf-8");+ expect(file).not.toContain("@remotion/renderer");+ });++ test("Should be able to delete it", () => {+ fs.unlinkSync(outfile);});- expect(errors.length).toBe(0);- fs.unlinkSync(outfile);});