@@ -7,6 +7,7 @@ import { setupServer } from "msw/node"
77import { Writable } from "stream"
88import got from "got"
99import fs from "fs-extra"
10+ import { slash } from "gatsby-core-utils/path"
1011import { fetchRemoteFile } from "../fetch-remote-file"
1112import * as storage from "../utils/get-storage"
1213
@@ -132,6 +133,7 @@ const server = setupServer(
132133 ctx . body ( content )
133134 )
134135 } ) ,
136+
135137 rest . get ( `http://external.com/dog` , async ( req , res , ctx ) => {
136138 const { content, contentLength } = await getFileContent (
137139 path . join ( __dirname , `./fixtures/dog-thumbnail.jpg` ) ,
@@ -175,6 +177,19 @@ const server = setupServer(
175177 ctx . body ( content )
176178 )
177179 } ) ,
180+ rest . get ( `http://external.com/dog-*.jpg` , async ( req , res , ctx ) => {
181+ const { content, contentLength } = await getFileContent (
182+ path . join ( __dirname , `./fixtures/dog-thumbnail.jpg` ) ,
183+ req
184+ )
185+
186+ return res (
187+ ctx . set ( `Content-Type` , `image/jpg` ) ,
188+ ctx . set ( `Content-Length` , contentLength ) ,
189+ ctx . status ( 200 ) ,
190+ ctx . body ( content )
191+ )
192+ } ) ,
178193 rest . get ( `http://external.com/404.jpg` , async ( req , res , ctx ) => {
179194 const content = `Page not found`
180195
@@ -456,96 +471,171 @@ Fetch details:
456471` )
457472 } )
458473
459- it ( `should not re-download file if cache is set` , async ( ) => {
460- const filePath = await fetchRemoteFile ( {
461- url : `http://external.com/dog.jpg` ,
462- cache,
463- cacheKey : `1` ,
464- } )
465- const cachedFilePath = await fetchRemoteFile ( {
466- url : `http://external.com/dog.jpg` ,
467- cache,
468- cacheKey : `1` ,
469- } )
474+ let cacheVersion = 0
475+ describe . each ( [ false , true ] ) ( `with excludeDigest %s` , excludeDigest => {
476+ function getExternalUrl ( cacheVersion ) {
477+ return `http://external.com/dog-${ cacheVersion } .jpg?v=${ cacheVersion } `
478+ }
470479
471- expect ( filePath ) . toBe ( cachedFilePath )
472- expect ( gotStream ) . toBeCalledTimes ( 1 )
473- expect ( fs . pathExists ) . toBeCalledTimes ( 1 )
474- expect ( fs . copy ) . not . toBeCalled ( )
475- } )
480+ it ( `should not re-download file if cache is set` , async ( ) => {
481+ const filePath = await fetchRemoteFile ( {
482+ url : getExternalUrl ( ++ cacheVersion ) ,
483+ cache,
484+ cacheKey : `${ cacheVersion } ` ,
485+ excludeDigest,
486+ } )
487+ const cachedFilePath = await fetchRemoteFile ( {
488+ url : getExternalUrl ( cacheVersion ) ,
489+ cache,
490+ cacheKey : `${ cacheVersion } ` ,
491+ excludeDigest,
492+ } )
476493
477- it ( `should not re-download and use same path if ouputDir is not inside public folder` , async ( ) => {
478- const filePath = await fetchRemoteFile ( {
479- url : `http://external.com/dog.jpg` ,
480- directory : cache . directory ,
481- cacheKey : `2` ,
482- } )
483- const cachedFilePath = await fetchRemoteFile ( {
484- url : `http://external.com/dog.jpg` ,
485- directory : path . join ( cache . directory , `diff` ) ,
486- cacheKey : `2` ,
494+ expect ( filePath ) . toBe ( cachedFilePath )
495+ expect ( gotStream ) . toBeCalledTimes ( 1 )
496+ expect ( fs . pathExists ) . toBeCalledTimes ( 1 )
497+ expect ( fs . copy ) . not . toBeCalled ( )
487498 } )
488499
489- expect ( filePath ) . toBe ( cachedFilePath )
490- expect ( gotStream ) . toBeCalledTimes ( 1 )
491- expect ( fs . pathExists ) . toBeCalledTimes ( 1 )
492- expect ( fs . copy ) . not . toBeCalled ( )
493- } )
500+ it ( `should not re-download and use same path if ouputDir is not inside public folder` , async ( ) => {
501+ const filePath = await fetchRemoteFile ( {
502+ url : getExternalUrl ( ++ cacheVersion ) ,
503+ directory : cache . directory ,
504+ cacheKey : `${ cacheVersion } ` ,
505+ excludeDigest,
506+ } )
507+ const cachedFilePath = await fetchRemoteFile ( {
508+ url : getExternalUrl ( cacheVersion ) ,
509+ directory : path . join ( cache . directory , `diff` ) ,
510+ cacheKey : `${ cacheVersion } ` ,
511+ excludeDigest,
512+ } )
494513
495- it ( `should not re-download but copy file to public folder` , async ( ) => {
496- const currentGlobal = global . __GATSBY
497- global . __GATSBY = {
498- root : cache . directory ,
499- }
500- await fs . ensureDir ( path . join ( cache . directory , `public` ) )
501- const filePath = await fetchRemoteFile ( {
502- url : `http://external.com/dog.jpg` ,
503- directory : cache . directory ,
504- cacheKey : `3` ,
514+ expect ( filePath ) . toBe ( cachedFilePath )
515+ expect ( gotStream ) . toBeCalledTimes ( 1 )
516+ expect ( fs . pathExists ) . toBeCalledTimes ( 1 )
517+ expect ( fs . copy ) . not . toBeCalled ( )
505518 } )
506- const cachedFilePath = await fetchRemoteFile ( {
507- url : `http://external.com/dog.jpg` ,
508- directory : path . join ( cache . directory , `public` ) ,
509- cacheKey : `3` ,
519+
520+ it ( `should not re-download but copy file to public folder` , async ( ) => {
521+ const currentGlobal = global . __GATSBY
522+ global . __GATSBY = {
523+ root : cache . directory ,
524+ }
525+ await fs . ensureDir ( path . join ( cache . directory , `public` ) )
526+ const filePath = await fetchRemoteFile ( {
527+ url : getExternalUrl ( ++ cacheVersion ) ,
528+ directory : cache . directory ,
529+ cacheKey : `${ cacheVersion } ` ,
530+ excludeDigest,
531+ } )
532+ const cachedFilePath = await fetchRemoteFile ( {
533+ url : getExternalUrl ( cacheVersion ) ,
534+ directory : path . join ( cache . directory , `public` ) ,
535+ cacheKey : `${ cacheVersion } ` ,
536+ excludeDigest,
537+ } )
538+
539+ expect ( filePath ) . not . toBe ( cachedFilePath )
540+ expect ( cachedFilePath ) . toStartWith ( path . join ( cache . directory , `public` ) )
541+ expect ( gotStream ) . toBeCalledTimes ( 1 )
542+ expect ( fs . pathExists ) . toBeCalledTimes ( 1 )
543+ expect ( fs . copy ) . toBeCalledTimes ( 1 )
544+ expect ( await fs . pathExists ( cachedFilePath ) ) . toBe ( true )
545+ global . __GATSBY = currentGlobal
510546 } )
511547
512- expect ( filePath ) . not . toBe ( cachedFilePath )
513- expect ( cachedFilePath ) . toStartWith ( path . join ( cache . directory , `public` ) )
514- expect ( gotStream ) . toBeCalledTimes ( 1 )
515- expect ( fs . pathExists ) . toBeCalledTimes ( 1 )
516- expect ( fs . copy ) . toBeCalledTimes ( 1 )
517- expect ( await fs . pathExists ( cachedFilePath ) ) . toBe ( true )
518- global . __GATSBY = currentGlobal
519- } )
548+ it ( `should not re-download but copy file to public folder (with slashes)` , async ( ) => {
549+ const currentGlobal = global . __GATSBY
550+ global . __GATSBY = {
551+ root : cache . directory ,
552+ }
553+ await fs . ensureDir ( path . join ( cache . directory , `public` ) )
554+ const filePath = await fetchRemoteFile ( {
555+ url : getExternalUrl ( ++ cacheVersion ) ,
556+ directory : slash ( cache . directory ) ,
557+ cacheKey : `${ cacheVersion } ` ,
558+ excludeDigest,
559+ } )
560+ const cachedFilePath = await fetchRemoteFile ( {
561+ url : getExternalUrl ( cacheVersion ) ,
562+ directory : slash ( path . join ( cache . directory , `public` ) ) ,
563+ cacheKey : `${ cacheVersion } ` ,
564+ excludeDigest,
565+ } )
520566
521- it ( `should not re-download but copy file to public folder when the same url is requested` , async ( ) => {
522- const currentGlobal = global . __GATSBY
523- global . __GATSBY = {
524- root : cache . directory ,
525- }
526- await fs . ensureDir ( path . join ( cache . directory , `public` ) )
527- const filePathPromise = fetchRemoteFile ( {
528- url : `http://external.com/dog.jpg?v=4` ,
529- directory : cache . directory ,
530- cacheKey : `4` ,
567+ expect ( filePath ) . not . toBe ( cachedFilePath )
568+ expect ( cachedFilePath ) . toStartWith ( path . join ( cache . directory , `public` ) )
569+ expect ( gotStream ) . toBeCalledTimes ( 1 )
570+ expect ( fs . pathExists ) . toBeCalledTimes ( 1 )
571+ expect ( fs . copy ) . toBeCalledTimes ( 1 )
572+ expect ( await fs . pathExists ( cachedFilePath ) ) . toBe ( true )
573+ global . __GATSBY = currentGlobal
531574 } )
532- const cachedFilePathPromise = fetchRemoteFile ( {
533- url : `http://external.com/dog.jpg?v=4` ,
534- directory : path . join ( cache . directory , `public` ) ,
535- cacheKey : `4` ,
575+
576+ it ( `should not re-download but copy file to public folder when the same url is requested` , async ( ) => {
577+ const currentGlobal = global . __GATSBY
578+ global . __GATSBY = {
579+ root : cache . directory ,
580+ }
581+ await fs . ensureDir ( path . join ( cache . directory , `public` ) )
582+ const filePathPromise = fetchRemoteFile ( {
583+ url : getExternalUrl ( ++ cacheVersion ) ,
584+ directory : cache . directory ,
585+ cacheKey : `${ cacheVersion } ` ,
586+ excludeDigest,
587+ } )
588+ const cachedFilePathPromise = fetchRemoteFile ( {
589+ url : getExternalUrl ( cacheVersion ) ,
590+ directory : path . join ( cache . directory , `public` ) ,
591+ cacheKey : `${ cacheVersion } ` ,
592+ excludeDigest,
593+ } )
594+
595+ const [ filePath , cachedFilePath ] = await Promise . all ( [
596+ filePathPromise ,
597+ cachedFilePathPromise ,
598+ ] )
599+
600+ expect ( filePath ) . not . toBe ( cachedFilePath )
601+ expect ( cachedFilePath ) . toStartWith ( path . join ( cache . directory , `public` ) )
602+ expect ( gotStream ) . toBeCalledTimes ( 1 )
603+ expect ( fs . pathExists ) . toBeCalledTimes ( 0 )
604+ expect ( fs . copy ) . toBeCalledTimes ( 1 )
605+ global . __GATSBY = currentGlobal
536606 } )
537607
538- const [ filePath , cachedFilePath ] = await Promise . all ( [
539- filePathPromise ,
540- cachedFilePathPromise ,
541- ] )
608+ it ( `should not re-download but copy file to public folder when the same url is requested (with slashes)` , async ( ) => {
609+ const currentGlobal = global . __GATSBY
610+ global . __GATSBY = {
611+ root : cache . directory ,
612+ }
613+ await fs . ensureDir ( path . join ( cache . directory , `public` ) )
614+ const filePathPromise = fetchRemoteFile ( {
615+ url : getExternalUrl ( ++ cacheVersion ) ,
616+ directory : slash ( cache . directory ) ,
617+ cacheKey : `${ cacheVersion } ` ,
618+ excludeDigest,
619+ } )
620+ const cachedFilePathPromise = fetchRemoteFile ( {
621+ url : getExternalUrl ( cacheVersion ) ,
622+ directory : slash ( path . join ( cache . directory , `public` ) ) ,
623+ cacheKey : `${ cacheVersion } ` ,
624+ excludeDigest,
625+ } )
542626
543- expect ( filePath ) . not . toBe ( cachedFilePath )
544- expect ( cachedFilePath ) . toStartWith ( path . join ( cache . directory , `public` ) )
545- expect ( gotStream ) . toBeCalledTimes ( 1 )
546- expect ( fs . pathExists ) . toBeCalledTimes ( 0 )
547- expect ( fs . copy ) . toBeCalledTimes ( 1 )
548- global . __GATSBY = currentGlobal
627+ const [ filePath , cachedFilePath ] = await Promise . all ( [
628+ filePathPromise ,
629+ cachedFilePathPromise ,
630+ ] )
631+
632+ expect ( filePath ) . not . toBe ( cachedFilePath )
633+ expect ( cachedFilePath ) . toStartWith ( path . join ( cache . directory , `public` ) )
634+ expect ( gotStream ) . toBeCalledTimes ( 1 )
635+ expect ( fs . pathExists ) . toBeCalledTimes ( 0 )
636+ expect ( fs . copy ) . toBeCalledTimes ( 1 )
637+ global . __GATSBY = currentGlobal
638+ } )
549639 } )
550640
551641 describe ( `retries the download` , ( ) => {
0 commit comments