From fb05edaeb73b85fb2ac930c30e061c0c0078d6ca Mon Sep 17 00:00:00 2001 From: Ales Mraz Date: Mon, 12 Feb 2024 09:18:37 +0100 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=F0=9F=9A=80=20feat:=20add=20config?= =?UTF-8?q?=20for=20remote=20images=20download=20delays?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cli/external-images/index.ts | 16 +++++++++++++++- src/cli/index.ts | 11 ++++++++--- src/utils/getConfig.ts | 5 +++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/cli/external-images/index.ts b/src/cli/external-images/index.ts index 2d186b1c..ec177531 100644 --- a/src/cli/external-images/index.ts +++ b/src/cli/external-images/index.ts @@ -5,13 +5,23 @@ import got from 'got' import type { Manifest } from '../' +import type { Config } from './../../utils/getConfig' + type ExternalImagesDownloaderArgs = { terse?: boolean manifest: Manifest destDir: string + remoteImagesDownloadsDelay?: Config['remoteImagesDownloadsDelay'] } -const externalImagesDownloader = async ({ terse = false, manifest, destDir }: ExternalImagesDownloaderArgs) => { +const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) + +const externalImagesDownloader = async ({ + terse = false, + manifest, + destDir, + remoteImagesDownloadsDelay, +}: ExternalImagesDownloaderArgs) => { if (!terse) { // eslint-disable-next-line no-console console.log('\n- Download external images -') @@ -25,6 +35,10 @@ const externalImagesDownloader = async ({ terse = false, manifest, destDir }: Ex if (downloadedImages.some((image) => image === externalUrl)) continue + if (remoteImagesDownloadsDelay) { + await sleep(remoteImagesDownloadsDelay) + } + promises.push( (async () => { downloadedImages.push(externalUrl) diff --git a/src/cli/index.ts b/src/cli/index.ts index 831c5cf4..8336af98 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -174,8 +174,8 @@ export const optimizeImages = async ({ config.remoteImages === undefined ? [] : typeof config.remoteImages === 'function' - ? await config.remoteImages() - : config.remoteImages + ? await config.remoteImages() + : config.remoteImages if (remoteImages.length > 0) { const remoteImageList = new Set() @@ -216,7 +216,12 @@ export const optimizeImages = async ({ ) } if (manifest.some(({ externalUrl }) => externalUrl !== undefined)) { - await externalImagesDownloader({ terse, manifest, destDir }) + await externalImagesDownloader({ + terse, + manifest, + destDir, + remoteImagesDownloadsDelay: config.remoteImagesDownloadsDelay, + }) } const publicDir = path.resolve(cwd, 'public') diff --git a/src/utils/getConfig.ts b/src/utils/getConfig.ts index a4c8a330..73fe60e6 100644 --- a/src/utils/getConfig.ts +++ b/src/utils/getConfig.ts @@ -96,6 +96,11 @@ export type Config = { * @type {string[] | (() => string[] | Promise)} */ remoteImages?: string[] | (() => string[] | Promise) + + /** + * In case you need to download a large amount of images from an external CDN with a rate limit, this will add delays between downloading images. + */ + remoteImagesDownloadsDelay?: number } const getConfig = (): Config => { From 4978490d9c464af26cbd0539e5ec64e3f5d269d6 Mon Sep 17 00:00:00 2001 From: Ales Mraz Date: Mon, 12 Feb 2024 09:31:01 +0100 Subject: [PATCH 2/2] =?UTF-8?q?docs:=20=E2=9C=8F=EF=B8=8F=20add=20docs=20i?= =?UTF-8?q?nfo=20for=20remoteImagesDownloadsDelay=20config?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/docs/03-Features/01-external-images.md | 8 ++++++++ docs/docs/04-Configurations/01-basic-configuration.md | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/docs/docs/03-Features/01-external-images.md b/docs/docs/03-Features/01-external-images.md index 2f97508e..0d2f0d1d 100644 --- a/docs/docs/03-Features/01-external-images.md +++ b/docs/docs/03-Features/01-external-images.md @@ -43,3 +43,11 @@ Also, no downloading to local is performed. src="https://next-export-optimize-images.vercel.app/og.png?width=3840" /> ``` + +### `remoteImagesDownloadsDelay` + +- Type: number + +In case you need to download a large amount of images from an external CDN with a rate limit, this will add delays between downloading images. + +effectively this will add `sleep` function between downloads. diff --git a/docs/docs/04-Configurations/01-basic-configuration.md b/docs/docs/04-Configurations/01-basic-configuration.md index 8a631fda..889aee0b 100644 --- a/docs/docs/04-Configurations/01-basic-configuration.md +++ b/docs/docs/04-Configurations/01-basic-configuration.md @@ -216,3 +216,9 @@ You can directly specify the URL of an external image. This is useful in cases where it is not known what images will be used for the build using variables, for example. https://next-export-optimize-images.vercel.app/docs/Features/external-images#when-specifying-an-external-image-url-with-a-variable + +### `remoteImagesDownloadsDelay` + +- Type: number + +In case you need to download a large amount of images from an external CDN with a rate limit, this will add delays between downloading images.