File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -113,18 +113,21 @@ async function trySaveBackup(
113113 writeBackup : WriteBackup ,
114114) : Promise < void > {
115115 const timeoutObject : { timeout : ?TimeoutID } = { timeout : null } ;
116+ const writeStream = await storageAdapter . createWriteStream ( filename ) ;
116117 const setBackupTimeout = ( alreadyWaited : number ) => {
117118 timeoutObject . timeout = setTimeout ( ( ) => {
118119 const nowWaited = alreadyWaited + backupWatchFrequency ;
120+ const uploadedByteCount = writeStream . getUploadedByteCount ( ) ;
121+ const uploadedMiB = Math . floor ( uploadedByteCount / ( 1024 * 1024 ) ) ;
119122 console . log (
120- `writing backup for ${ filename } has taken ${ nowWaited } ms so far` ,
123+ `writing backup for ${ filename } has taken ${ nowWaited } ms, ` +
124+ `uploaded ${ uploadedMiB } MiB so far` ,
121125 ) ;
122126 setBackupTimeout ( nowWaited ) ;
123127 } , backupWatchFrequency ) ;
124128 } ;
125129 setBackupTimeout ( 0 ) ;
126130
127- const writeStream = await storageAdapter . createWriteStream ( filename ) ;
128131 writeStream . on ( 'error' , ( error : Error ) => {
129132 console . warn ( `write stream emitted error for ${ filename } ` , error ) ;
130133 } ) ;
Original file line number Diff line number Diff line change @@ -33,7 +33,11 @@ class BackupStorageSpaceExceededError extends Error {
3333export interface BackupStorageAdapter {
3434 listFiles ( ) : Promise < $ReadOnlyArray < StoredFileInfo >> ;
3535 deleteFile ( filename : string ) : Promise < void > ;
36- createWriteStream ( filename : string ) : Promise < stream$Writable > ;
36+ createWriteStream ( filename : string ) : Promise < BackupWriteStream > ;
37+ }
38+
39+ export interface BackupWriteStream extends stream$Writable {
40+ getUploadedByteCount ( ) : number ;
3741}
3842
3943function createBackupStorageAdapter (
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import { Writable } from 'stream';
66
77import {
88 BackupStorageSpaceExceededError ,
9+ type BackupWriteStream ,
910 type DropboxStorageConfig ,
1011 type StoredFileInfo ,
1112} from './backup-storage.js' ;
@@ -82,7 +83,7 @@ class DropboxBackupStorageAdapter {
8283 }
8384 }
8485
85- async createWriteStream ( filename : string ) : Promise < stream$Writable > {
86+ async createWriteStream ( filename : string ) : Promise < BackupWriteStream > {
8687 const accessToken = await this . refreshAccessToken ( ) ;
8788 return new DropboxUploadWriteStream ( this , accessToken , filename ) ;
8889 }
@@ -310,6 +311,10 @@ class DropboxUploadWriteStream extends Writable {
310311 ) ;
311312 this . bufferedChunk = Buffer . alloc ( 0 ) ;
312313 }
314+
315+ getUploadedByteCount ( ) : number {
316+ return this . offset ;
317+ }
313318}
314319
315320function verifyBufferEncoding ( encoding : string ) : buffer$Encoding {
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ import { promisify } from 'util';
77
88import {
99 BackupStorageSpaceExceededError ,
10+ type BackupWriteStream ,
1011 type LocalStorageConfig ,
1112 type StoredFileInfo ,
1213} from './backup-storage.js' ;
@@ -50,8 +51,8 @@ class LocalBackupStorageAdapter {
5051 }
5152 }
5253
53- async createWriteStream ( filename : string ) : Promise < stream$Writable > {
54- const writeStream = new PassThrough ( ) ;
54+ async createWriteStream ( filename : string ) : Promise < BackupWriteStream > {
55+ const writeStream = new LocalBackupWriteStream ( ) ;
5556 const fileWriteStream = fs . createWriteStream (
5657 path . join ( this . directory , filename ) ,
5758 ) ;
@@ -67,4 +68,20 @@ class LocalBackupStorageAdapter {
6768 }
6869}
6970
71+ class LocalBackupWriteStream extends PassThrough {
72+ uploadedByteCount : number ;
73+
74+ constructor ( ) {
75+ super ( ) ;
76+ this . uploadedByteCount = 0 ;
77+ this . on ( 'data' , chunk => {
78+ this . uploadedByteCount += chunk . length ;
79+ } ) ;
80+ }
81+
82+ getUploadedByteCount ( ) : number {
83+ return this . uploadedByteCount ;
84+ }
85+ }
86+
7087export { LocalBackupStorageAdapter } ;
You can’t perform that action at this time.
0 commit comments