@@ -313,11 +313,8 @@ async function readHooks(
313313 collectHooks ( app , asObject ( inline . hooks ) ?? inline , hooks , unsupported ) ;
314314 }
315315 const seen = new Set < string > ( ) ;
316- let remainingJsonFiles = MAX_HOOK_JSON_FILES ;
317316 for ( const candidate of paths ) {
318- const discovered = await hookFiles ( root , candidate , remainingJsonFiles , seen ) ;
319- remainingJsonFiles -= discovered . length ;
320- for ( const file of discovered ) {
317+ for ( const file of await hookFiles ( root , candidate , seen ) ) {
321318 const raw = await readJson ( file ) ;
322319 const events = asObject ( raw ?. hooks ) ?? raw ;
323320 if ( events ) {
@@ -331,7 +328,6 @@ async function readHooks(
331328async function hookFiles (
332329 root : string ,
333330 candidate : string ,
334- remainingJsonFiles : number ,
335331 seen : Set < string > ,
336332) : Promise < string [ ] > {
337333 const target = underRoot ( root , candidate ) ;
@@ -347,43 +343,33 @@ async function hookFiles(
347343 if ( stats . isSymbolicLink ( ) ) {
348344 throw new Error ( `Plugin hook path uses a symlink: ${ candidate } ` ) ;
349345 }
350- if ( ! stats . isDirectory ( ) ) {
351- if ( seen . has ( target ) ) {
352- return [ ] ;
353- }
354- if ( remainingJsonFiles <= 0 ) {
346+ const files : string [ ] = [ ] ;
347+ function addFile ( file : string ) : void {
348+ if ( seen . has ( file ) ) return ;
349+ if ( seen . size >= MAX_HOOK_JSON_FILES ) {
355350 throw new Error ( `Plugin hook tree exceeded the ${ MAX_HOOK_JSON_FILES } -file limit` ) ;
356351 }
357- seen . add ( target ) ;
358- return [ target ] ;
352+ seen . add ( file ) ;
353+ files . push ( file ) ;
354+ }
355+ if ( ! stats . isDirectory ( ) ) {
356+ addFile ( target ) ;
357+ return files ;
359358 }
360- const files : string [ ] = [ ] ;
361359 async function walk ( directory : string , depth : number ) : Promise < void > {
362360 if ( depth > MAX_HOOK_WALK_DEPTH ) {
363361 throw new Error ( `Plugin hook tree exceeded the ${ MAX_HOOK_WALK_DEPTH } -directory depth limit` ) ;
364362 }
365- const dir = await fs . opendir ( directory ) ;
366- try {
367- for await ( const entry of dir ) {
368- const child = path . join ( directory , entry . name ) ;
369- if ( entry . isSymbolicLink ( ) ) {
370- throw new Error ( `Plugin hook path uses a symlink: ${ path . relative ( root , child ) } ` ) ;
371- }
372- if ( entry . isDirectory ( ) ) {
373- await walk ( child , depth + 1 ) ;
374- } else if ( entry . isFile ( ) && entry . name . endsWith ( ".json" ) ) {
375- if ( seen . has ( child ) ) {
376- continue ;
377- }
378- if ( files . length >= remainingJsonFiles ) {
379- throw new Error ( `Plugin hook tree exceeded the ${ MAX_HOOK_JSON_FILES } -file limit` ) ;
380- }
381- seen . add ( child ) ;
382- files . push ( child ) ;
383- }
363+ for await ( const entry of await fs . opendir ( directory ) ) {
364+ const child = path . join ( directory , entry . name ) ;
365+ if ( entry . isSymbolicLink ( ) ) {
366+ throw new Error ( `Plugin hook path uses a symlink: ${ path . relative ( root , child ) } ` ) ;
367+ }
368+ if ( entry . isDirectory ( ) ) {
369+ await walk ( child , depth + 1 ) ;
370+ } else if ( entry . isFile ( ) && entry . name . endsWith ( ".json" ) ) {
371+ addFile ( child ) ;
384372 }
385- } finally {
386- await dir . close ( ) . catch ( ( ) => undefined ) ;
387373 }
388374 }
389375 await walk ( target , 0 ) ;
0 commit comments