@@ -27,6 +27,7 @@ interface CommonExportData {
2727interface ReadyExport extends CommonExportData {
2828 exportStatus : "ready" ;
2929 exportCreatedAt : number ;
30+ docsTransformed : number ;
3031}
3132
3233interface InProgressExport extends CommonExportData {
@@ -124,7 +125,7 @@ export class ExportsManager extends EventEmitter<ExportsManagerEvents> {
124125 }
125126 }
126127
127- public async readExport ( exportName : string ) : Promise < string > {
128+ public async readExport ( exportName : string ) : Promise < { content : string ; docsTransformed : number } > {
128129 try {
129130 this . assertIsNotShuttingDown ( ) ;
130131 exportName = decodeAndNormalize ( exportName ) ;
@@ -137,9 +138,12 @@ export class ExportsManager extends EventEmitter<ExportsManagerEvents> {
137138 throw new Error ( "Requested export is still being generated. Try again later." ) ;
138139 }
139140
140- const { exportPath } = exportHandle ;
141+ const { exportPath, docsTransformed } = exportHandle ;
141142
142- return fs . readFile ( exportPath , { encoding : "utf8" , signal : this . shutdownController . signal } ) ;
143+ return {
144+ content : await fs . readFile ( exportPath , { encoding : "utf8" , signal : this . shutdownController . signal } ) ,
145+ docsTransformed,
146+ } ;
143147 } catch ( error ) {
144148 this . logger . error ( {
145149 id : LogId . exportReadError ,
@@ -202,17 +206,15 @@ export class ExportsManager extends EventEmitter<ExportsManagerEvents> {
202206 } ) : Promise < void > {
203207 try {
204208 let pipeSuccessful = false ;
209+ let docsTransformed = 0 ;
205210 try {
206211 await fs . mkdir ( this . exportsDirectoryPath , { recursive : true } ) ;
207212 const outputStream = createWriteStream ( inProgressExport . exportPath ) ;
208- await pipeline (
209- [
210- input . stream ( ) ,
211- this . docToEJSONStream ( this . getEJSONOptionsForFormat ( jsonExportFormat ) ) ,
212- outputStream ,
213- ] ,
214- { signal : this . shutdownController . signal }
215- ) ;
213+ const ejsonTransofrm = this . docToEJSONStream ( this . getEJSONOptionsForFormat ( jsonExportFormat ) ) ;
214+ await pipeline ( [ input . stream ( ) , ejsonTransofrm , outputStream ] , {
215+ signal : this . shutdownController . signal ,
216+ } ) ;
217+ docsTransformed = ejsonTransofrm . docsTransformed ;
216218 pipeSuccessful = true ;
217219 } catch ( error ) {
218220 // If the pipeline errors out then we might end up with
@@ -231,6 +233,7 @@ export class ExportsManager extends EventEmitter<ExportsManagerEvents> {
231233 ...inProgressExport ,
232234 exportCreatedAt : Date . now ( ) ,
233235 exportStatus : "ready" ,
236+ docsTransformed,
234237 } ;
235238 this . emit ( "export-available" , inProgressExport . exportURI ) ;
236239 }
@@ -256,33 +259,39 @@ export class ExportsManager extends EventEmitter<ExportsManagerEvents> {
256259 }
257260 }
258261
259- private docToEJSONStream ( ejsonOptions : EJSONOptions | undefined ) : Transform {
262+ private docToEJSONStream ( ejsonOptions : EJSONOptions | undefined ) : Transform & { docsTransformed : number } {
260263 let docsTransformed = 0 ;
261- return new Transform ( {
262- objectMode : true ,
263- transform ( chunk : unknown , encoding , callback ) : void {
264- try {
265- const doc = EJSON . stringify ( chunk , undefined , undefined , ejsonOptions ) ;
264+ const result = Object . assign (
265+ new Transform ( {
266+ objectMode : true ,
267+ transform ( chunk : unknown , encoding , callback ) : void {
268+ try {
269+ const doc = EJSON . stringify ( chunk , undefined , undefined , ejsonOptions ) ;
270+ if ( docsTransformed === 0 ) {
271+ this . push ( "[" + doc ) ;
272+ } else {
273+ this . push ( ",\n" + doc ) ;
274+ }
275+ docsTransformed ++ ;
276+ callback ( ) ;
277+ } catch ( err ) {
278+ callback ( err as Error ) ;
279+ }
280+ } ,
281+ flush ( callback ) : void {
266282 if ( docsTransformed === 0 ) {
267- this . push ( "[" + doc ) ;
283+ this . push ( "[]" ) ;
268284 } else {
269- this . push ( ",\n" + doc ) ;
285+ this . push ( "]" ) ;
270286 }
271- docsTransformed ++ ;
287+ result . docsTransformed = docsTransformed ;
272288 callback ( ) ;
273- } catch ( err ) {
274- callback ( err as Error ) ;
275- }
276- } ,
277- flush ( callback ) : void {
278- if ( docsTransformed === 0 ) {
279- this . push ( "[]" ) ;
280- } else {
281- this . push ( "]" ) ;
282- }
283- callback ( ) ;
284- } ,
285- } ) ;
289+ } ,
290+ } ) ,
291+ { docsTransformed }
292+ ) ;
293+
294+ return result ;
286295 }
287296
288297 private async cleanupExpiredExports ( ) : Promise < void > {
0 commit comments