From 742866c5669a2be4f8b5a4c861cadb933c381415 Mon Sep 17 00:00:00 2001 From: horo <143025439+horo-fox@users.noreply.github.com> Date: Thu, 11 Apr 2024 23:40:14 +0900 Subject: [PATCH] Limit imports in flight for `getCollection` (#10708) --- .changeset/four-zoos-taste.md | 5 +++++ packages/astro/src/content/runtime.ts | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) create mode 100644 .changeset/four-zoos-taste.md diff --git a/.changeset/four-zoos-taste.md b/.changeset/four-zoos-taste.md new file mode 100644 index 000000000000..6078bcdadb6e --- /dev/null +++ b/.changeset/four-zoos-taste.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Limits parallel imports within `getCollection()` to prevent EMFILE errors when accessing files diff --git a/packages/astro/src/content/runtime.ts b/packages/astro/src/content/runtime.ts index 8fcbc919104c..0ad74de3e537 100644 --- a/packages/astro/src/content/runtime.ts +++ b/packages/astro/src/content/runtime.ts @@ -1,4 +1,5 @@ import type { MarkdownHeading } from '@astrojs/markdown-remark'; +import pLimit from 'p-limit'; import { ZodIssueCode, string as zodString } from 'zod'; import { AstroError, AstroErrorData } from '../core/errors/index.js'; import { prependForwardSlash } from '../core/path.js'; @@ -80,8 +81,9 @@ export function createGetCollection({ // Always return a new instance so consumers can safely mutate it entries = [...cacheEntriesByCollection.get(collection)!]; } else { + const limit = pLimit(10); entries = await Promise.all( - lazyImports.map(async (lazyImport) => { + lazyImports.map((lazyImport) => limit(async () => { const entry = await lazyImport(); return type === 'content' ? { @@ -103,7 +105,7 @@ export function createGetCollection({ collection: entry.collection, data: entry.data, }; - }) + })) ); cacheEntriesByCollection.set(collection, entries); }