Commit 3f164e0

Anton Golub <antongolub@antongolub.com>
2024-11-08 12:24:05
fix: handle endless streams piping (#936)
closes #935
1 parent 6eb540f
Changed files (3)
src/core.ts
@@ -358,8 +358,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
       from.pipe(dest.stdin)
       return dest
     }
-
-    from.pipe(dest)
+    from.once('end', () => dest.emit('end-piped-from')).pipe(dest)
     return promisifyStream(dest)
   }
 
src/util.ts
@@ -462,6 +462,7 @@ export const promisifyStream = <S extends Writable>(
             target
               .once('error', (e) => _rej(rej(e)))
               .once('finish', () => _res(res(target)))
+              .once('end-piped-from', () => _res(res(target)))
           )
       }
       const value = Reflect.get(target, key)
test/core.test.js
@@ -427,6 +427,11 @@ describe('core', () => {
           assert.equal((await fs.readFile(file)).toString(), 'HELLO\n')
           await fs.rm(file)
         })
+
+        test('$ > stdout', async () => {
+          const p = $`echo 1`.pipe(process.stdout)
+          assert.equal(await p, process.stdout)
+        })
       })
 
       it('supports delayed piping', async () => {