@@ -17,14 +17,27 @@ const limiter = new Bottleneck({
1717const limitedRequest = limiter . wrap ( request )
1818const limitedRead = limiter . wrap ( util . promisify ( fs . readFile ) )
1919
20- async function fetchChildren ( node , basePath ) {
20+ function getSelfRef ( node ) {
21+ let ref
2122 const self = node . links . find ( ( link ) => ( link . rel === 'self' ) )
23+ if ( self && self . href ) {
24+ ref = self . href
25+ }
26+ return ref
27+ }
28+
29+ function getChildLinks ( node ) {
2230 const links =
2331 node . links . filter ( ( link ) => ( link . rel === 'child' || link . rel === 'item' ) )
32+ return links
33+ }
34+
35+ async function fetchChildren ( node , links , basePath ) {
36+ const selfHref = getSelfRef ( node )
2437 const linkPromises = links . map ( ( link ) => {
2538 let urlPath
2639 let returnPromise
27- if ( ! self || ! self . href || ! link . href ) {
40+ if ( ! selfHref || ! link . href ) {
2841 return Promise . reject ( new Error ( `${ node . id } has invalid links` ) )
2942 }
3043 if ( path . isAbsolute ( link . href ) ) {
@@ -34,7 +47,7 @@ async function fetchChildren(node, basePath) {
3447 if ( basePath ) {
3548 urlPath = `${ path . dirname ( basePath ) } /${ link . href } `
3649 } else {
37- urlPath = `${ path . dirname ( self . href ) } /${ link . href } `
50+ urlPath = `${ path . dirname ( selfHref ) } /${ link . href } `
3851 }
3952 }
4053 if ( isUrl ( urlPath ) ) {
@@ -61,31 +74,26 @@ async function fetchChildren(node, basePath) {
6174 return children
6275}
6376
64- function getSelfRef ( node ) {
65- let ref
66- const self = node . links . find ( ( link ) => ( link . rel === 'self' ) )
67- if ( self && self . href ) {
68- ref = self . href
69- }
70- return ref
71- }
72-
7377// Mutates stack and visited
7478async function visitChildren ( node , stack , visited , basePath ) {
7579 let children
80+ const nodeLinks = getChildLinks ( node )
7681 try {
77- children = await fetchChildren ( node , basePath )
82+ children = await fetchChildren ( node , nodeLinks , basePath )
7883 } catch ( error ) {
7984 logger . error ( error )
8085 }
8186 if ( children ) {
8287 // eslint-disable-next-line
8388 for ( const child of children ) {
8489 const key = getSelfRef ( child )
90+ const childLinks = getChildLinks ( child )
8591 if ( key ) {
8692 if ( ! visited [ key ] ) {
87- visited [ key ] = true
8893 stack . push ( child )
94+ if ( childLinks . length ) {
95+ visited [ key ] = true
96+ }
8997 }
9098 } else {
9199 logger . error ( `${ node . id } has invalid self link` )
0 commit comments