Skip to content

Commit

Permalink
fix(bundles ans-104): use pipeline to improve error handling PE-4212
Browse files Browse the repository at this point in the history
Switching from 'pipe' to 'pipeline' because 'pipeline' has more
consistent and comprehensive error handling. With 'pipe' there was some
unhandled case that was causing promises to not get resolved/rejected
reliably when streams errored. No doubt that's resolvable by setting up
the correct handlers, but 'pipeline' is more fool proof.
  • Loading branch information
djwhitt committed Jul 25, 2023
1 parent d1b6301 commit 33eddea
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/lib/ans-104.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as EventEmitter from 'node:events';
import fs from 'node:fs';
import fsPromises from 'node:fs/promises';
import path from 'node:path';
import { pipeline } from 'node:stream';
import {
Worker,
isMainThread,
Expand Down Expand Up @@ -205,20 +206,25 @@ export class Ans104Parser {
`${parentId}`,
);
const writeStream = fs.createWriteStream(bundlePath);
data.stream.pipe(writeStream);
writeStream.on('error', (error) => {
log.error('Error writing ANS-104 bundle stream', error);
reject(error);
});
writeStream.on('finish', async () => {
log.info('Parsing ANS-104 bundle stream...');
this.worker.postMessage({
rootTxId,
parentId,
parentIndex,
bundlePath,
});
});
pipeline(
data.stream,
writeStream,
(error) => {
if (error !== undefined) {
this.unbundlePromiseReject?.(error);
this.resetUnbundlePromise();
log.error('Error writing ANS-104 bundle stream', error);
} else {
log.info('Parsing ANS-104 bundle stream...');
this.worker.postMessage({
rootTxId,
parentId,
parentIndex,
bundlePath,
});
}
}
);
} catch (error) {
reject(error);
}
Expand Down

0 comments on commit 33eddea

Please sign in to comment.