@@ -55,22 +55,31 @@ function ensureTestDataDir() {
55
55
}
56
56
57
57
async function getZippedTestDataset ( url , filename , unpackDirectory ) {
58
- var dir = ensureTestDataDir ( ) ;
59
- var targetPath = path . join ( dir , filename ) ;
60
- var unpackPath = path . join ( dir , unpackDirectory ) ;
58
+ const dir = ensureTestDataDir ( ) ;
59
+ const targetPath = path . join ( dir , filename ) ;
60
+ const unpackPath = path . join ( dir , unpackDirectory ) ;
61
61
if ( ! fs . existsSync ( unpackPath ) ) {
62
62
await downloadToFile ( url , targetPath ) ;
63
63
await unzip ( targetPath , unpackPath ) ;
64
64
}
65
65
return unpackPath ;
66
66
}
67
67
68
+ /**
69
+ * Stores the required downloads to prevent async reading before download completed.
70
+ */
71
+ const asyncDownloadMap = new Map ( ) ;
72
+
68
73
async function getTestDataset ( url , filename ) {
69
- var dir = ensureTestDataDir ( ) ;
70
- var targetPath = path . join ( dir , filename ) ;
71
- if ( ! fs . existsSync ( targetPath ) ) {
72
- await downloadToFile ( url , targetPath ) ;
74
+ const dir = ensureTestDataDir ( ) ;
75
+ const targetPath = path . join ( dir , filename ) ;
76
+ let filePromise = asyncDownloadMap . get ( targetPath ) ;
77
+ if ( ! filePromise && ! fs . existsSync ( targetPath ) ) {
78
+ filePromise = downloadToFile ( url , targetPath ) ;
79
+ asyncDownloadMap . set ( targetPath , filePromise ) ;
73
80
}
81
+ // This returns immediately if filePromise is undefined - eg if the file already downloaded.
82
+ await filePromise ;
74
83
return targetPath ;
75
84
}
76
85
0 commit comments