@@ -114,7 +114,7 @@ export class MSCache {
114114 logger . debug ( `Building file cache from ${ path . join ( config . connection , `${ ns } .cache` ) } ` ) ;
115115
116116 try {
117- const [ keyvFile ] = initFileCache ( { ...config , cacheDir : config . connection , cacheId : `${ ns } .cache` } , logger ) ;
117+ const [ keyvFile ] = await initFileCache ( { ...config , cacheDir : config . connection , cacheId : `${ ns } .cache` } , logger ) ;
118118 secondaryCache = keyvFile ;
119119 } catch ( e ) {
120120 logger . warn ( e ) ;
@@ -177,38 +177,57 @@ export const flatCacheCreate = (opts: FlatCacheOptions) => {
177177 } ) ;
178178}
179179
180- export const flatCacheLoad = ( flatCache : FlatCache , logger : MaybeLogger ) : void => {
180+ export const flatCacheLoad = async ( flatCache : FlatCache , logger : MaybeLogger ) : Promise < void > => {
181181
182182 const cachePath = path . join ( flatCache . cacheDir , flatCache . cacheId ) ;
183183 try {
184184 fileOrDirectoryIsWriteable ( cachePath ) ;
185185 } catch ( e ) {
186- new Error ( `Unable to use path for file cache at ${ cachePath } ` , { cause : e } )
186+ throw new Error ( `Unable to use path for file cache at ${ cachePath } ` , { cause : e } )
187187 }
188188
189- let loadError : Error ;
189+ const streamPromise = new Promise ( ( resolve , reject ) => {
190+ flatCache . loadFileStream ( cachePath , ( progress : number , total : number ) => {
191+ logger . debug ( `Loading ${ progress } /${ total } chunks...` ) ;
192+ } , ( ) => {
193+ resolve ( true ) ;
194+ } , ( err : Error ) => {
195+ reject ( err ) ;
196+ } ) ;
197+ } ) ;
190198
191- const onlySaveError = ( e : Error ) => {
192- loadError = e ;
193- }
194- flatCache . on ( 'error' , onlySaveError ) ;
195199 try {
196- logger . debug ( 'Loading cache from file...' ) ;
197- flatCache . load ( ) ;
198- if ( loadError !== undefined ) {
199- throw loadError ;
200- }
200+ await streamPromise ;
201201 logger . debug ( `File cache loaded` ) ;
202- flatCache . off ( 'error' , onlySaveError ) ;
202+ return ;
203203 } catch ( e ) {
204- throw new Error ( `Unable to use file cache at ${ cachePath } ` , { cause : e } ) ;
204+ if ( null !== e . message . match ( / C a c h e f i l e .+ d o e s n o t e x i s t / ) ) {
205+ let loadError : Error ;
206+ try {
207+ const onlySaveError = ( e : Error ) => {
208+ loadError = e ;
209+ } ;
210+ flatCache . on ( 'error' , onlySaveError ) ;
211+ flatCache . load ( ) ;
212+ if ( loadError !== undefined ) {
213+ throw loadError ;
214+ }
215+ flatCache . off ( 'error' , onlySaveError ) ;
216+ logger . debug ( `File cache loaded` ) ;
217+ return ;
218+ } catch ( e ) {
219+ throw new Error ( `Unable to use file cache at ${ cachePath } ` , { cause : e } ) ;
220+ }
221+ } else {
222+ throw new Error ( `Unable to use file cache at ${ cachePath } ` , { cause : e } ) ;
223+ }
205224 }
206225}
207226
208- export const initFileCache = ( opts : FlatCacheOptions = { } , logger : MaybeLogger = new MaybeLogger ( ) ) : [ Keyv | KeyvStoreAdapter | undefined , FlatCache | undefined ] => {
227+ export const initFileCache = async ( opts : FlatCacheOptions = { } , logger : MaybeLogger = new MaybeLogger ( ) ) : Promise < [ Keyv | KeyvStoreAdapter | undefined , FlatCache | undefined ] > => {
209228 const flatCache = flatCacheCreate ( opts ) ;
210229 try {
211- flatCacheLoad ( flatCache , logger ) ;
230+ await flatCacheLoad ( flatCache , logger ) ;
212231 flatCache . on ( 'error' , ( e ) => {
213232 logger . warn ( e ) ;
214233 } ) ;
0 commit comments