| 510 | } |
| 511 | |
| 512 | async parse(onLine: any, onClose: any): Promise<void> { |
| 513 | const isFile = this.options.isFile; |
| 514 | const tmpFile = this.tempFile; |
| 515 | const outStream = this.output; |
| 516 | const isInput = this.options.isInput; |
| 517 | return new Promise((resolve, reject) => { |
| 518 | const rl = readline.createInterface({ |
| 519 | input: fs.createReadStream(tmpFile), |
| 520 | }); |
| 521 | let errored = false; |
| 522 | rl.on("line", (line) => { |
| 523 | const data = extractJSON(line, isInput); |
| 524 | if (!data) { |
| 525 | return; |
| 526 | } |
| 527 | onLine(data); |
| 528 | }); |
| 529 | rl.on("close", () => { |
| 530 | if (errored) { |
| 531 | reject(new FirebaseError("There was an error creating the report.")); |
| 532 | } else { |
| 533 | const result = onClose(); |
| 534 | if (isFile) { |
| 535 | // Only resolve once the data is flushed. |
| 536 | outStream.on("finish", () => { |
| 537 | resolve(result); |
| 538 | }); |
| 539 | outStream.end(); |
| 540 | } else { |
| 541 | resolve(result); |
| 542 | } |
| 543 | } |
| 544 | }); |
| 545 | rl.on("error", () => { |
| 546 | reject(); |
| 547 | }); |
| 548 | outStream.on("error", () => { |
| 549 | errored = true; |
| 550 | rl.close(); |
| 551 | }); |
| 552 | }); |
| 553 | } |
| 554 | |
| 555 | write(data: any) { |
| 556 | if (this.options.isFile) { |