@@ -28,7 +28,7 @@ export class Download {
28
28
private _finishedCallback : ( ) => void ;
29
29
private _finishedPromise : Promise < void > ;
30
30
private _saveAsRequests : { fulfill : ( ) => void ; reject : ( error ?: any ) => void ; path : string } [ ] = [ ] ;
31
- private _loaded : boolean = false ;
31
+ private _finished : boolean = false ;
32
32
private _page : Page ;
33
33
private _acceptDownloads : boolean ;
34
34
private _failure : string | null = null ;
@@ -75,23 +75,24 @@ export class Download {
75
75
}
76
76
77
77
async saveAs ( path : string ) {
78
- if ( this . _loaded ) {
78
+ if ( ! this . _acceptDownloads )
79
+ throw new Error ( 'Pass { acceptDownloads: true } when you are creating your browser context.' ) ;
80
+ if ( this . _deleted )
81
+ throw new Error ( 'Download already deleted. Save before deleting.' ) ;
82
+ if ( this . _failure )
83
+ throw new Error ( 'Download not found on disk. Check download.failure() for details.' ) ;
84
+
85
+ if ( this . _finished ) {
79
86
await this . _saveAs ( path ) ;
80
87
return ;
81
88
}
82
89
83
90
return new Promise ( ( fulfill , reject ) => this . _saveAsRequests . push ( { fulfill, reject, path} ) ) ;
84
91
}
85
92
86
- async _saveAs ( dlPath : string ) {
87
- if ( ! this . _acceptDownloads )
88
- throw new Error ( 'Pass { acceptDownloads: true } when you are creating your browser context.' ) ;
93
+ async _saveAs ( downloadPath : string ) {
89
94
const fileName = path . join ( this . _downloadsPath , this . _uuid ) ;
90
- if ( this . _failure )
91
- throw new Error ( 'Download not found on disk. Check download.failure() for details.' ) ;
92
- if ( this . _deleted )
93
- throw new Error ( 'Download already deleted. Save before deleting.' ) ;
94
- await util . promisify ( fs . copyFile ) ( fileName , dlPath ) ;
95
+ await util . promisify ( fs . copyFile ) ( fileName , downloadPath ) ;
95
96
}
96
97
97
98
async failure ( ) : Promise < string | null > {
@@ -118,13 +119,12 @@ export class Download {
118
119
}
119
120
120
121
async _reportFinished ( error ?: string ) {
122
+ this . _finished = true ;
123
+ this . _failure = error || null ;
124
+
121
125
if ( error ) {
122
- for ( const { reject } of this . _saveAsRequests ) {
123
- if ( ! this . _acceptDownloads )
124
- reject ( new Error ( 'Pass { acceptDownloads: true } when you are creating your browser context.' ) ) ;
125
- else
126
- reject ( error ) ;
127
- }
126
+ for ( const { reject } of this . _saveAsRequests )
127
+ reject ( error ) ;
128
128
} else {
129
129
for ( const { fulfill, reject, path } of this . _saveAsRequests ) {
130
130
try {
@@ -136,8 +136,6 @@ export class Download {
136
136
}
137
137
}
138
138
139
- this . _loaded = true ;
140
- this . _failure = error || null ;
141
139
this . _finishedCallback ( ) ;
142
140
}
143
141
}
0 commit comments