]> git.ardis.lu - csvio/commitdiff
Add cleanup on abort
authorArdis Lu <ardisglu@gmail.com>
Sun, 9 Feb 2025 06:03:39 +0000 (22:03 -0800)
committerArdis Lu <ardisglu@gmail.com>
Sun, 9 Feb 2025 06:03:39 +0000 (22:03 -0800)
`close()` is NOT called if CSVWriter errors, only `abort()`. So need to call `close()` within `abort()` to finish cleanup.

src/core.js
test/core.test.js

index 03cac7f55db4dbd155ea85a23c728857fba651fd..4e09f5decd14022502465d67048683569d216da4 100644 (file)
@@ -375,6 +375,9 @@ export class CSVWriter extends WritableStream {
         await handle.close();
         status.elapsed = performance.now() - status.start;
         status.done = true;
+      },
+      async abort() {
+        await this.close();
       }
     });
 
index 283939767a49f4a69835cced61baea2f54673d8f..a02e4bd95fa6db80ae87f4be854dbb2efd71d0d7 100644 (file)
@@ -522,14 +522,16 @@ suite('CSVWriter', { concurrency: true }, () => {
   test('flushes progress to disk on uncaught error', { concurrency: true }, async (t) => {
     const temp = await createTempFile();
     t.after(async () => await unlink(temp));
+    const writer = new CSVWriter(temp);
     await rejects(async () => {
       await ReadableStream.from((function* () {
         yield '["header1","header2"]';
         yield '["1","1"]';
         throw new Error();
-      })()).pipeTo(new CSVWriter(temp))
+      })()).pipeTo(writer)
     });
-    await new CSVReader(temp).pipeTo(
+    deepStrictEqual(writer.status.done, true); // Confirm cleanup actions
+    await new CSVReader(temp).pipeTo( // Confirm flush to disk
       csvStreamEqualWritable([
         ['header1', 'header2'],
         ['1', '1'],
This page took 0.041393 seconds and 4 git commands to generate.