@@ -68,7 +68,13 @@ export class DownloadTask {
6868 }
6969
7070 if ( ! fileIo . accessSync ( path ) ) {
71- await fileIo . mkdir ( path ) ;
71+ try {
72+ await fileIo . mkdir ( path ) ;
73+ } catch ( error ) {
74+ if ( ! fileIo . accessSync ( path ) ) {
75+ throw error ;
76+ }
77+ }
7278 }
7379 }
7480
@@ -99,22 +105,14 @@ export class DownloadTask {
99105 }
100106
101107 private async listEntryNames ( directory : string ) : Promise < string [ ] > {
102- const entryNames : string [ ] = [ ] ;
103108 const files = await fileIo . listFile ( directory ) ;
109+ const validFiles = files . filter ( file => file !== '.' && file !== '..' ) ;
104110
105- for ( const file of files ) {
106- if ( file === '.' || file === '..' ) {
107- continue ;
108- }
109-
110- const filePath = `${ directory } /${ file } ` ;
111- const stat = await fileIo . stat ( filePath ) ;
112- if ( ! stat . isDirectory ( ) ) {
113- entryNames . push ( file ) ;
114- }
115- }
111+ const stats = await Promise . all (
112+ validFiles . map ( file => fileIo . stat ( `${ directory } /${ file } ` ) ) ,
113+ ) ;
116114
117- return entryNames ;
115+ return validFiles . filter ( ( _ , index ) => ! stats [ index ] . isDirectory ( ) ) ;
118116 }
119117
120118 private async writeFileContent (
@@ -432,11 +430,10 @@ export class DownloadTask {
432430 await this . recreateDirectory ( params . unzipDirectory ) ;
433431
434432 await zlib . decompressFile ( params . targetFile , params . unzipDirectory ) ;
435- const entryNames = await this . listEntryNames ( params . unzipDirectory ) ;
436- const manifestArrays = await this . readManifestArrays (
437- params . unzipDirectory ,
438- true ,
439- ) ;
433+ const [ entryNames , manifestArrays ] = await Promise . all ( [
434+ this . listEntryNames ( params . unzipDirectory ) ,
435+ this . readManifestArrays ( params . unzipDirectory , true ) ,
436+ ] ) ;
440437
441438 NativePatchCore . buildArchivePatchPlan (
442439 ARCHIVE_PATCH_TYPE_FROM_PACKAGE ,
@@ -475,11 +472,10 @@ export class DownloadTask {
475472 await this . recreateDirectory ( params . unzipDirectory ) ;
476473
477474 await zlib . decompressFile ( params . targetFile , params . unzipDirectory ) ;
478- const entryNames = await this . listEntryNames ( params . unzipDirectory ) ;
479- const manifestArrays = await this . readManifestArrays (
480- params . unzipDirectory ,
481- false ,
482- ) ;
475+ const [ entryNames , manifestArrays ] = await Promise . all ( [
476+ this . listEntryNames ( params . unzipDirectory ) ,
477+ this . readManifestArrays ( params . unzipDirectory , false ) ,
478+ ] ) ;
483479
484480 const plan = NativePatchCore . buildArchivePatchPlan (
485481 ARCHIVE_PATCH_TYPE_FROM_PPK ,
@@ -524,23 +520,36 @@ export class DownloadTask {
524520 . replace ( 'resources/base/media/' , '' )
525521 . split ( '.' ) [ 0 ] ;
526522 const mediaBuffer = await resourceManager . getMediaByName ( mediaName ) ;
527- const [ firstTarget , ...restTargets ] = targets ;
528- await this . writeFileContent ( firstTarget , mediaBuffer . buffer ) ;
529- for ( const target of restTargets ) {
530- await this . copySandboxFile ( firstTarget , target ) ;
523+ const parentDirs = [
524+ ...new Set (
525+ targets . map ( t => t . substring ( 0 , t . lastIndexOf ( '/' ) ) ) . filter ( Boolean ) ,
526+ ) ,
527+ ] ;
528+ for ( const dir of parentDirs ) {
529+ await this . ensureDirectory ( dir ) ;
531530 }
531+ await Promise . all (
532+ targets . map ( target => this . writeFileContent ( target , mediaBuffer . buffer ) ) ,
533+ ) ;
532534 continue ;
533535 }
534536 const fromContent = await resourceManager . getRawFd ( currentFrom ) ;
535537 const [ firstTarget , ...restTargets ] = targets ;
536- await this . ensureParentDirectory ( firstTarget ) ;
538+ const parentDirs = [
539+ ...new Set (
540+ targets . map ( t => t . substring ( 0 , t . lastIndexOf ( '/' ) ) ) . filter ( Boolean ) ,
541+ ) ,
542+ ] ;
543+ for ( const dir of parentDirs ) {
544+ await this . ensureDirectory ( dir ) ;
545+ }
537546 if ( fileIo . accessSync ( firstTarget ) ) {
538547 await fileIo . unlink ( firstTarget ) ;
539548 }
540549 saveFileToSandbox ( fromContent , firstTarget ) ;
541- for ( const target of restTargets ) {
542- await this . copySandboxFile ( firstTarget , target ) ;
543- }
550+ await Promise . all (
551+ restTargets . map ( target => this . copySandboxFile ( firstTarget , target ) ) ,
552+ ) ;
544553 }
545554 } catch ( error ) {
546555 error . message =
0 commit comments