Commit 3188694

Anton Golub <antongolub@antongolub.com>
2025-08-06 13:05:44
refactor: simplify `ProcessPromise[asyncIterator]` inners (#1307)
* refactor: simplify `ProcessPromise[asyncIterator]` inners * fix: handle `stdio: ignore` case in `ProcessPromise[asyncIterator]`
1 parent 820a85c
build/core.cjs
@@ -855,7 +855,7 @@ var _ProcessPromise = class _ProcessPromise extends Promise {
         yield* __yieldStar((0, import_util.getLines)(chunk, memo, dlmtr));
       }
       try {
-        for (var iter = __forAwait(this.stdout[Symbol.asyncIterator] ? this.stdout : import_vendor_core2.VoidStream.from(this.stdout)), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
+        for (var iter = __forAwait(this.stdout || []), more, temp, error; more = !(temp = yield new __await(iter.next())).done; more = false) {
           const chunk = temp.value;
           yield* __yieldStar((0, import_util.getLines)(chunk, memo, dlmtr));
         }
src/core.ts
@@ -777,9 +777,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
       yield* getLines(chunk, memo, dlmtr)
     }
 
-    for await (const chunk of this.stdout[Symbol.asyncIterator]
-      ? this.stdout
-      : VoidStream.from(this.stdout)) {
+    for await (const chunk of this.stdout || []) {
       yield* getLines(chunk, memo, dlmtr)
     }
 
test/core.test.js
@@ -1207,7 +1207,7 @@ describe('core', () => {
         const process = $`sleep 0.1; echo Chunk1; sleep 0.1; echo Chunk2; sleep 0.2; echo Chunk3; sleep 0.1; echo Chunk4;`
         const chunks = []
 
-        await new Promise((resolve) => setTimeout(resolve, 250))
+        await sleep(250)
         for await (const chunk of process) {
           chunks.push(chunk)
         }
@@ -1217,6 +1217,35 @@ describe('core', () => {
         assert.equal(chunks[3], 'Chunk4', 'Second chunk should be "Chunk4"')
       })
 
+      it('handles ignored stdio', async () => {
+        const p = $({
+          stdio: 'ignore',
+        })`sleep 0.1; echo Chunk1; sleep 0.1; echo Chunk2`
+        const chunks = []
+        for await (const chunk of p) {
+          chunks.push(chunk)
+        }
+
+        assert.equal(chunks.length, 0)
+        assert.equal((await p).stdout, '')
+      })
+
+      it('handles non-iterable stdio', async () => {
+        const file = tempfile()
+        const fd = fs.openSync(file, 'w')
+        const p = $({
+          stdio: ['ignore', fd, 'ignore'],
+        })`sleep 0.1; echo Chunk1; sleep 0.1; echo Chunk2`
+        const chunks = []
+        for await (const chunk of p) {
+          chunks.push(chunk)
+        }
+
+        assert.equal(chunks.length, 0)
+        assert.equal((await p).stdout, '')
+        assert.equal(fs.readFileSync(file, 'utf-8'), `Chunk1\nChunk2\n`)
+      })
+
       it('should process all output before handling a non-zero exit code', async () => {
         const process = $`sleep 0.1; echo foo; sleep 0.1; echo bar; sleep 0.1; exit 1;`
         const chunks = []
.size-limit.json
@@ -15,7 +15,7 @@
       "README.md",
       "LICENSE"
     ],
-    "limit": "125.00 kB",
+    "limit": "124.85 kB",
     "brotli": false,
     "gzip": false
   },
@@ -29,7 +29,7 @@
       "build/globals.js",
       "build/deno.js"
     ],
-    "limit": "815.003 kB",
+    "limit": "814.95 kB",
     "brotli": false,
     "gzip": false
   },
@@ -62,7 +62,7 @@
       "README.md",
       "LICENSE"
     ],
-    "limit": "872.50 kB",
+    "limit": "872.25 kB",
     "brotli": false,
     "gzip": false
   }