@@ -102,18 +102,28 @@ class RemoteAuth extends BaseAuthStrategy {
102102 }
103103
104104 async storeRemoteSession ( options ) {
105- /* Compress & Store Session */
106105 const pathExists = await this . isValidPath ( this . userDataDir ) ;
107- if ( pathExists ) {
108- await this . compressSession ( ) ;
109- await this . store . save ( { session : path . join ( this . dataPath , this . sessionName ) } ) ;
110- await fs . promises . unlink ( path . join ( this . dataPath , `${ this . sessionName } .zip` ) ) ;
111- await fs . promises . rm ( `${ this . tempDir } ` , {
112- recursive : true ,
113- force : true ,
114- maxRetries : this . rmMaxRetries ,
115- } ) . catch ( ( ) => { } ) ;
106+ if ( ! pathExists ) return ;
107+
108+ let compressedSessionPath ;
109+ try {
110+ compressedSessionPath = await this . compressSession ( ) ;
111+ await this . store . save ( { session : path . join ( this . dataPath , this . sessionName ) } ) ;
116112 if ( options && options . emit ) this . client . emit ( Events . REMOTE_SESSION_SAVED ) ;
113+ } finally {
114+ const paths = [
115+ this . tempDir ,
116+ ...( compressedSessionPath ? [ compressedSessionPath ] : [ ] )
117+ ] ;
118+ await Promise . allSettled (
119+ paths . map ( ( p ) =>
120+ fs . promises . rm ( p , {
121+ recursive : true ,
122+ force : true ,
123+ maxRetries : this . rmMaxRetries ,
124+ } )
125+ )
126+ ) ;
117127 }
118128 }
119129
@@ -142,20 +152,26 @@ class RemoteAuth extends BaseAuthStrategy {
142152 }
143153
144154 async compressSession ( ) {
145- const archive = archiver ( 'zip ') ;
146- const stream = fs . createWriteStream ( path . join ( this . dataPath , ` ${ this . sessionName } .zip` ) ) ;
155+ const stageDefaultPath = path . join ( this . tempDir , 'Default ') ;
156+ const userDataDefaultPath = path . join ( this . userDataDir , 'Default' ) ;
147157
148- await fs . copy ( this . userDataDir , this . tempDir ) . catch ( ( ) => { } ) ;
149- await this . deleteMetadata ( ) ;
150- return new Promise ( ( resolve , reject ) => {
151- archive
152- . directory ( this . tempDir , false )
153- . on ( 'error' , err => reject ( err ) )
154- . pipe ( stream ) ;
158+ await fs . emptyDir ( stageDefaultPath ) ;
159+ await this . copyByRequiredDirs ( userDataDefaultPath , stageDefaultPath ) ;
155160
156- stream . on ( 'close' , ( ) => resolve ( ) ) ;
161+ const archive = archiver ( 'zip' ) ;
162+ const outPath = path . join ( this . dataPath , `${ this . sessionName } .zip` ) ;
163+ const out = fs . createWriteStream ( outPath ) ;
164+
165+ await new Promise ( ( resolve , reject ) => {
166+ out . once ( 'close' , resolve ) ;
167+ out . once ( 'error' , reject ) ;
168+ archive . once ( 'error' , reject ) ;
169+
170+ archive . pipe ( out ) ;
171+ archive . directory ( this . tempDir , false ) ;
157172 archive . finalize ( ) ;
158173 } ) ;
174+ return outPath ;
159175 }
160176
161177 async unCompressSession ( compressedSessionPath ) {
@@ -171,25 +187,16 @@ class RemoteAuth extends BaseAuthStrategy {
171187 await fs . promises . unlink ( compressedSessionPath ) ;
172188 }
173189
174- async deleteMetadata ( ) {
175- const sessionDirs = [ this . tempDir , path . join ( this . tempDir , 'Default' ) ] ;
176- for ( const dir of sessionDirs ) {
177- const sessionFiles = await fs . promises . readdir ( dir ) ;
178- for ( const element of sessionFiles ) {
179- if ( ! this . requiredDirs . includes ( element ) ) {
180- const dirElement = path . join ( dir , element ) ;
181- const stats = await fs . promises . lstat ( dirElement ) ;
182-
183- if ( stats . isDirectory ( ) ) {
184- await fs . promises . rm ( dirElement , {
185- recursive : true ,
186- force : true ,
187- maxRetries : this . rmMaxRetries ,
188- } ) . catch ( ( ) => { } ) ;
189- } else {
190- await fs . promises . unlink ( dirElement ) . catch ( ( ) => { } ) ;
191- }
192- }
190+ async copyByRequiredDirs ( from , to ) {
191+ for ( const d of this . requiredDirs ) {
192+ const src = path . join ( from , d ) ;
193+ if ( await this . isValidPath ( src ) ) {
194+ const dest = path . join ( to , path . basename ( src ) ) ;
195+ await fs . promises . cp ( src , dest , {
196+ recursive : true ,
197+ force : true ,
198+ errorOnExist : false
199+ } ) ;
193200 }
194201 }
195202 }
0 commit comments