11import os from "node:os" ;
22import path from "node:path" ;
3+ import pLimit from "p-limit" ;
34import lockfile from "proper-lockfile" ;
45import fs from "fs-extra" ;
56import { getMD5 } from "../../../utils/crypto" ;
@@ -16,13 +17,16 @@ type CacheTypeValue = (typeof CacheType)[keyof typeof CacheType];
1617const processStartTime = Number ( new Date ( ) ) ;
1718const tmpDir = path . join ( os . tmpdir ( ) , SELECTIVITY_CACHE_DIRECTIRY ) ;
1819
20+ // https://nodejs.org/api/cli.html#uv_threadpool_sizesize
21+ const libUVLimited = pLimit ( ( process . env . UV_THREADPOOL_SIZE && Number ( process . env . UV_THREADPOOL_SIZE ) ) || 16 ) ;
22+
1923const ensureSelectivityCacheDirectory = async ( ) : Promise < void > => {
20- await fs . ensureDir ( tmpDir ) ;
24+ await libUVLimited ( ( ) => fs . ensureDir ( tmpDir ) ) ;
2125} ;
2226
2327const wasModifiedAfterProcessStart = async ( flagFilePath : string ) : Promise < boolean > => {
2428 try {
25- const stats = await fs . stat ( flagFilePath ) ;
29+ const stats = await libUVLimited ( ( ) => fs . stat ( flagFilePath ) ) ;
2630 return stats . mtimeMs >= processStartTime ;
2731 } catch {
2832 return false ;
@@ -51,7 +55,7 @@ export const getCachedSelectivityFile = async (cacheType: CacheTypeValue, key: s
5155 const flagFilePath = cacheFilePath + SELECTIVITY_CACHE_READY_SUFFIX ;
5256
5357 if ( await wasModifiedAfterProcessStart ( flagFilePath ) ) {
54- const cacheContents = await fs . readFile ( cacheFilePath , "utf8" ) . catch ( ( ) => null ) ;
58+ const cacheContents = await libUVLimited ( ( ) => fs . readFile ( cacheFilePath , "utf8" ) ) . catch ( ( ) => null ) ;
5559
5660 return cacheContents ;
5761 }
@@ -60,7 +64,7 @@ export const getCachedSelectivityFile = async (cacheType: CacheTypeValue, key: s
6064 if ( await wasModifiedAfterProcessStart ( cacheFilePath ) ) {
6165 for ( let i = 0 ; i < 10 ; i ++ ) {
6266 if ( await wasModifiedAfterProcessStart ( flagFilePath ) ) {
63- const cacheContents = await fs . readFile ( cacheFilePath , "utf8" ) . catch ( ( ) => null ) ;
67+ const cacheContents = await libUVLimited ( ( ) => fs . readFile ( cacheFilePath , "utf8" ) ) . catch ( ( ) => null ) ;
6468
6569 return cacheContents ;
6670 }
@@ -113,12 +117,12 @@ export const setCachedSelectivityFile = async (
113117 }
114118
115119 try {
116- await fs . writeFile ( cacheFilePath , utf8Contents , { encoding : "utf8" } ) . catch ( cause => {
120+ await libUVLimited ( ( ) => fs . writeFile ( cacheFilePath , utf8Contents , { encoding : "utf8" } ) ) . catch ( cause => {
117121 throw new Error ( `Couldn't write cache to "${ cacheFilePath } "` , { cause } ) ;
118122 } ) ;
119123
120124 // Using "writeFile" to trigger "mtime" update even if file exists
121- await fs . writeFile ( flagFilePath , "" ) . catch ( cause => {
125+ await libUVLimited ( ( ) => fs . writeFile ( flagFilePath , "" ) ) . catch ( cause => {
122126 throw new Error ( `Couldn't mark cache as fresh at "${ cacheFilePath } "` , { cause } ) ;
123127 } ) ;
124128 } finally {
0 commit comments