Skip to content

Commit b87b8f9

Browse files
committed
rejection on saveZip
1 parent e23376c commit b87b8f9

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@transcend-io/penumbra",
3-
"version": "8.0.0",
3+
"version": "8.1.0",
44
"description": "Crypto streams for the browser.",
55
"type": "module",
66
"main": "./dist/main.penumbra.umd.cjs",

src/zip.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ export class PenumbraZipWriter extends EventTarget {
8989
/** Current zip archive size */
9090
private bytesWritten = 0;
9191

92+
/** Promise representing completion of the zip stream piping to the file sink */
93+
private pipePromise?: Promise<void>;
94+
9295
/**
9396
* Penumbra zip writer constructor
9497
* @param options - ZipOptions
@@ -150,13 +153,10 @@ export class PenumbraZipWriter extends EventTarget {
150153
ReadableStream | null,
151154
] = saveBuffer ? readable.tee() : [readable, null];
152155

153-
/**
154-
* This is intentionally not awaited because it does not complete until the entire stream has finished.
155-
* We want the main caller to be able to error-handle the function for any errors during stream setup.
156-
*
157-
* The consumer can handle the event via the streams interface.
158-
*/
159-
zipStream.pipeTo(saveStream, { signal }).catch((error: unknown) => {
156+
this.pipePromise = zipStream.pipeTo(saveStream, { signal });
157+
// Attach a rejection handler for logging to avoid unhandledrejection, but
158+
// keep the original promise's rejection for callers that await done().
159+
this.pipePromise.catch((error: unknown) => {
160160
const finalError =
161161
error instanceof Error
162162
? error
@@ -371,6 +371,16 @@ export class PenumbraZipWriter extends EventTarget {
371371
return size;
372372
}
373373

374+
/**
375+
* Await completion of the underlying zip stream being written to the sink
376+
* @returns Promise that resolves when the sink write completes
377+
*/
378+
async done(): Promise<void> {
379+
if (this.pipePromise) {
380+
await this.pipePromise;
381+
}
382+
}
383+
374384
/** Cancel Penumbra zip writer */
375385
abort(): void {
376386
if (!this.controller.signal.aborted) {

0 commit comments

Comments
 (0)