Skip to content

Commit 6a8d70f

Browse files
feat: Concurrency (#89)
# Description Enable a concurrency parameter to allow users to concurrently upload assets ## Issue Ticket Number Closes #57 ## Type of change <!-- Please select all options that are applicable. --> - [ ] Bug fix (non-breaking change which fixes an issue) - [X] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [X] This change requires a documentation update # Checklist <!-- These must all be followed and checked. --> - [ ] I have followed the contributing guidelines of this project as mentioned in [CONTRIBUTING.md](/CONTRIBUTING.md) - [ ] I have created an [issue](https://github.com/colbyfayock/netlify-plugin-cloudinary/issues) ticket for this PR - [ ] I have checked to ensure there aren't other open [Pull Requests](https://github.com/colbyfayock/netlify-plugin-cloudinary/pulls) for the same update/change? - [ ] I have performed a self-review of my own code - [x] I have run tests locally to ensure they all pass - [ ] I have commented my code, particularly in hard-to-understand areas - [x] I have made corresponding changes needed to the documentation --------- Co-authored-by: Colby Fayock <[email protected]>
1 parent bedd68f commit 6a8d70f

File tree

7 files changed

+44
-26
lines changed

7 files changed

+44
-26
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ npm install netlify-plugin-cloudinary
9797
| maxSize | object | No | eager | See Below. |
9898
| privateCdn | boolean | No | true | Enables Private CDN Distribution (Advanced Plan Users) |
9999
| uploadPreset | string | No | my-preset | Defined set of asset upload defaults in Cloudinary |
100-
100+
| uploadConcurrency | number | No | 10 | Maximum value of concurrent uploads |
101101
*Cloud Name must be set as an environment variable if not as an input
102102

103103
#### Max Size
@@ -322,4 +322,4 @@ npm run test
322322
<!-- markdownlint-restore -->
323323
<!-- prettier-ignore-end -->
324324

325-
<!-- ALL-CONTRIBUTORS-LIST:END -->
325+
<!-- ALL-CONTRIBUTORS-LIST:END -->

netlify-plugin-cloudinary/manifest.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ inputs:
3030
- name: uploadPreset
3131
required: false
3232
description: "Defined set of asset upload defaults in Cloudinary"
33+
- name: uploadConcurrency
34+
required: false
35+
description: "Maximum value of concurrent uploads"
36+
default: 10

netlify-plugin-cloudinary/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"cloudinary": "^1.40.0",
2323
"glob": "^10.3.3",
2424
"jsdom": "21",
25-
"node-fetch": "2"
25+
"node-fetch": "2",
26+
"p-limit": "3.1.0"
2627
},
2728
"devDependencies": {
2829
"@netlify/config": "20.8.1",

netlify-plugin-cloudinary/src/index.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs from 'node:fs/promises';
22
import path from 'node:path';
33
import { glob } from 'glob';
4+
import pLimit from 'p-limit';
45

56
import { Inputs } from './types/integration';
67

@@ -101,6 +102,8 @@ const CLOUDINARY_ASSET_DIRECTORIES = [
101102
},
102103
];
103104

105+
const DEFAULT_CONCURRENCY = 10;
106+
104107
/**
105108
* TODO
106109
* - Handle srcset
@@ -130,13 +133,14 @@ export async function onBuild({
130133
const {
131134
cname,
132135
deliveryType,
133-
folder = process.env.SITE_NAME,
136+
folder = process.env.SITE_NAME || '',
134137
imagesPath = CLOUDINARY_ASSET_DIRECTORIES.find(
135138
({ inputKey }) => inputKey === 'imagesPath',
136139
)?.path,
137140
maxSize,
138141
privateCdn,
139142
uploadPreset,
143+
uploadConcurrency = DEFAULT_CONCURRENCY,
140144
} = inputs;
141145

142146
if (!folder) {
@@ -201,26 +205,31 @@ export async function onBuild({
201205
}
202206

203207
try {
204-
_cloudinaryAssets.images = await Promise.all(
205-
imagesFiles.map(async image => {
206-
const publishPath = image.replace(PUBLISH_DIR, '');
207-
208-
const cloudinary = await getCloudinaryUrl({
209-
deliveryType,
210-
folder,
211-
path: publishPath,
212-
localDir: PUBLISH_DIR,
213-
uploadPreset,
214-
remoteHost: host,
215-
transformations
216-
});
217-
218-
return {
219-
publishPath,
220-
...cloudinary,
221-
};
222-
}),
223-
);
208+
const limitUploadFiles = pLimit(uploadConcurrency);
209+
const uploadsQueue = imagesFiles.map(image => {
210+
const publishPath = image.replace(PUBLISH_DIR, '');
211+
return limitUploadFiles(() => {
212+
async function uploadFile() {
213+
const cloudinary = await getCloudinaryUrl({
214+
deliveryType,
215+
folder,
216+
path: publishPath,
217+
localDir: PUBLISH_DIR,
218+
uploadPreset,
219+
remoteHost: host,
220+
transformations
221+
});
222+
223+
return {
224+
publishPath,
225+
...cloudinary,
226+
};
227+
}
228+
return uploadFile();
229+
})
230+
})
231+
232+
_cloudinaryAssets.images = await Promise.all(uploadsQueue);
224233
} catch (e) {
225234
globalErrors.push(e)
226235
}

netlify-plugin-cloudinary/src/types/integration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ export type Inputs = {
1616
};
1717
privateCdn: boolean;
1818
uploadPreset: string;
19-
};
19+
uploadConcurrency: number;
20+
};

netlify.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
[plugins.inputs]
1616
cloudName = "netlify-cloudinary"
17-
# deliveryType = "upload"
17+
deliveryType = "upload"
1818
imagesPath = [ "images", "screenshots" ]
1919

2020
[plugins.inputs.maxSize]

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)