Skip to content

🎥 Make videos programmatically with React

inline mimetypes and p-limit

Jonny Burger9664a4df5c57
diff --git a/packages/bundler/package.json b/packages/bundler/package.json
index 0e736766a7..b4f7c776cb 100644
--- a/packages/bundler/package.json
+++ b/packages/bundler/package.json
@@ -46,7 +46,7 @@
"@jonny/eslint-config": "3.0.259",
"@types/express": "^4.17.7",
"@types/jest": "^27.4.0",
- "@types/mime-types": "^2.1.1",
+ "@types/mime-types": "2.1.1",
"@types/node": "^16.7.5",
"@types/react": "18.0.1",
"@types/react-dom": "18.0.0",
diff --git a/packages/bundler/src/index.ts b/packages/bundler/src/index.ts
index 7942cb09c9..a3c9305b7f 100644
--- a/packages/bundler/src/index.ts
+++ b/packages/bundler/src/index.ts
@@ -1,4 +1,3 @@
-import mimeTypes from 'mime-types';
import {getLatestRemotionVersion} from './get-latest-remotion-version';
import {getPackageManager, lockFilePaths} from './get-package-manager';
import {startServer} from './start-server';
@@ -13,7 +12,6 @@ export const BundlerInternals = {
getPackageManager,
lockFilePaths,
esbuild,
- mimeTypes,
};
export {bundle} from './bundler';
diff --git a/packages/lambda/package.json b/packages/lambda/package.json
index 5afc8e90fb..80c0f2578a 100644
--- a/packages/lambda/package.json
+++ b/packages/lambda/package.json
@@ -37,7 +37,7 @@
"aws-policies": "^1.0.1",
"dotenv": "^10.0.0",
"minimist": "1.2.6",
- "p-limit": "^3.1.0",
+ "mime-types": "2.1.34",
"remotion": "3.0.7"
},
"peerDependencies": {
@@ -50,6 +50,7 @@
"@types/minimist": "1.2.2",
"@types/node": "^14.14.14",
"@types/prompt": "^1.1.0",
+ "@types/mime-types": "2.1.1",
"eslint": "8.13.0",
"execa": "5.1.1",
"jest": "^27.2.4",
diff --git a/packages/lambda/src/api/clean-items.ts b/packages/lambda/src/api/clean-items.ts
index ec568265da..bb1a8f7112 100644
--- a/packages/lambda/src/api/clean-items.ts
+++ b/packages/lambda/src/api/clean-items.ts
@@ -1,7 +1,7 @@
import {DeleteObjectCommand} from '@aws-sdk/client-s3';
-import pLimit from 'p-limit';
import {AwsRegion} from '../pricing/aws-regions';
import {getS3Client} from '../shared/aws-clients';
+import {pLimit} from '../shared/p-limit';
const limit = pLimit(10);
diff --git a/packages/lambda/src/api/upload-dir.ts b/packages/lambda/src/api/upload-dir.ts
index 07b33e9b84..858a7c2540 100644
--- a/packages/lambda/src/api/upload-dir.ts
+++ b/packages/lambda/src/api/upload-dir.ts
@@ -1,7 +1,7 @@
import {PutObjectCommand} from '@aws-sdk/client-s3';
import {Upload} from '@aws-sdk/lib-storage';
-import {BundlerInternals} from '@remotion/bundler';
import {createReadStream, promises as fs} from 'fs';
+import mimeTypes from 'mime-types';
import path from 'path';
import {Privacy} from '../defaults';
import {AwsRegion} from '../pricing/aws-regions';
@@ -75,8 +75,7 @@ export const uploadDir = async ({
const uploads = files.map(async (filePath) => {
const Key = `${folder}/${path.relative(dir, filePath.name)}`;
const Body = createReadStream(filePath.name);
- const ContentType =
- BundlerInternals.mimeTypes.lookup(Key) || 'application/octet-stream';
+ const ContentType = mimeTypes.lookup(Key) || 'application/octet-stream';
const ACL = privacy === 'private' ? 'private' : 'public-read';
if (filePath.size > 5 * 1024 * 1024) {
const paralellUploads3 = new Upload({
diff --git a/packages/lambda/src/functions/helpers/io.ts b/packages/lambda/src/functions/helpers/io.ts
index 31ec66cd65..04a87f584f 100644
--- a/packages/lambda/src/functions/helpers/io.ts
+++ b/packages/lambda/src/functions/helpers/io.ts
@@ -4,8 +4,8 @@ import {
PutObjectCommand,
_Object,
} from '@aws-sdk/client-s3';
-import {BundlerInternals} from '@remotion/bundler';
import {ReadStream} from 'fs';
+import mimeTypes from 'mime-types';
import {Readable} from 'stream';
import {AwsRegion} from '../../pricing/aws-regions';
import {getS3Client} from '../../shared/aws-clients';
@@ -94,8 +94,7 @@ export const lambdaWriteFile = async ({
Body: body,
ACL: privacy === 'private' ? 'private' : 'public-read',
ExpectedBucketOwner: expectedBucketOwner ?? undefined,
- ContentType:
- BundlerInternals.mimeTypes.lookup(key) || 'application/octet-stream',
+ ContentType: mimeTypes.lookup(key) || 'application/octet-stream',
})
);
};
diff --git a/packages/lambda/src/shared/p-limit.ts b/packages/lambda/src/shared/p-limit.ts
new file mode 100644
index 0000000000..a56ab84de9
--- /dev/null
+++ b/packages/lambda/src/shared/p-limit.ts
@@ -0,0 +1,75 @@
+export const pLimit = (concurrency: number) => {
+ const queue: Function[] = [];
+ let activeCount = 0;
+
+ const next = () => {
+ activeCount--;
+
+ if (queue.length > 0) {
+ queue.shift()?.();
+ }
+ };
+
+ const run = async <Arguments extends unknown[], ReturnType>(
+ fn: (..._arguments: Arguments) => PromiseLike<ReturnType> | ReturnType,
+ resolve: (res: Promise<ReturnType>) => void,
+ ...args: Arguments
+ ) => {
+ activeCount++;
+
+ // eslint-disable-next-line require-await
+ const result = (async () => fn(...args))();
+
+ resolve(result);
+
+ try {
+ await result;
+ } catch {}
+
+ next();
+ };
+
+ const enqueue = <Arguments extends unknown[], ReturnType>(
+ fn: (..._arguments: Arguments) => PromiseLike<ReturnType> | ReturnType,
+ resolve: (res: Promise<ReturnType>) => void,
+ ...args: Arguments
+ ) => {
+ queue.push(() => run(fn, resolve, ...args));
+
+ (async () => {
+ // This function needs to wait until the next microtask before comparing
+ // `activeCount` to `concurrency`, because `activeCount` is updated asynchronously
+ // when the run function is dequeued and called. The comparison in the if-statement
+ // needs to happen asynchronously as well to get an up-to-date value for `activeCount`.
+ await Promise.resolve();
+
+ if (activeCount < concurrency && queue.length > 0) {
+ queue.shift()?.();
+ }
+ })();
+ };
+
+ const generator = <Arguments extends unknown[], ReturnType>(
+ fn: (..._arguments: Arguments) => PromiseLike<ReturnType> | ReturnType,
+ ...args: Arguments
+ ) =>
+ new Promise<ReturnType>((resolve) => {
+ enqueue(fn, resolve, ...args);
+ });
+
+ Object.defineProperties(generator, {
+ activeCount: {
+ get: () => activeCount,
+ },
+ pendingCount: {
+ get: () => queue.length,
+ },
+ clearQueue: {
+ value: () => {
+ queue.length = 0;
+ },
+ },
+ });
+
+ return generator;
+};
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index b1c9a98875..f5b32999ae 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -55,7 +55,7 @@ importers:
'@jonny/eslint-config': 3.0.259
'@types/express': ^4.17.7
'@types/jest': ^27.4.0
- '@types/mime-types': ^2.1.1
+ '@types/mime-types': 2.1.1
'@types/node': ^16.7.5
'@types/react': 18.0.1
'@types/react-dom': 18.0.0
@@ -559,6 +559,7 @@ importers:
'@remotion/cli': 3.0.7
'@remotion/renderer': 3.0.7
'@types/jest': ^27.0.2
+ '@types/mime-types': 2.1.1
'@types/minimist': 1.2.2
'@types/node': ^14.14.14
'@types/prompt': ^1.1.0
@@ -567,8 +568,8 @@ importers:
eslint: 8.13.0
execa: 5.1.1
jest: ^27.2.4
+ mime-types: 2.1.34
minimist: 1.2.6
- p-limit: ^3.1.0
prettier: ^2.4.1
prettier-plugin-organize-imports: ^2.3.4
remotion: 3.0.7
@@ -590,12 +591,13 @@ importers:
'@remotion/renderer': link:../renderer
aws-policies: 1.0.1
dotenv: 10.0.0
+ mime-types: 2.1.34
minimist: 1.2.6
- p-limit: 3.1.0
remotion: link:../core
devDependencies:
'@jonny/eslint-config': [email protected][email protected]
'@types/jest': 27.4.0
+ '@types/mime-types': 2.1.1
'@types/minimist': 1.2.2
'@types/node': 14.17.20
'@types/prompt': 1.1.2