-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4daae39
commit a4e0312
Showing
3 changed files
with
56 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,55 @@ | ||
import { Writable } from 'node:stream' | ||
import { assign } from '../util.js' | ||
import { VoidWritable } from '../spawn.js' | ||
import EventEmitter from 'node:events' | ||
import { assign, isStringLiteral } from '../util.js' | ||
import { VoidStream } from '../spawn.js' | ||
import type { TShell, TMixin, TShellCtx } from '../x.js' | ||
import { type TZurk, type TZurkPromise, isZurkAny } from '../zurk.js' | ||
|
||
// eslint-disable-next-line sonarjs/cognitive-complexity | ||
export const pipeMixin: TMixin = <T extends TZurk | TZurkPromise >($: TShell, result: T, ctx: TShellCtx) => | ||
isZurkAny(result) | ||
? assign(result, { | ||
pipe(...args: any[]) { | ||
const target = args[0] | ||
const { fulfilled, stdout, store } = ctx | ||
if (isZurkAny(target)) { | ||
// stream.ctx.input = fulfilled.stdout | ||
const input = new VoidWritable() | ||
target.ctx.input = input | ||
pipe(...args: any) { | ||
const [target, ...rest] = args | ||
const { fulfilled, store, ee } = ctx | ||
const from = new VoidStream() | ||
const sync = !('then' in result) | ||
const input = fulfilled ? fulfilled.stdout : from | ||
const fill = () => { | ||
for (const chunk of store.stdout) { | ||
input.push(chunk) | ||
} | ||
if (fulfilled) { | ||
input.push(null) | ||
} else { | ||
stdout.pipe(input) | ||
from.write(chunk) | ||
} | ||
|
||
return target | ||
} | ||
let _result | ||
|
||
if (target instanceof Writable) { | ||
for (const chunk of store.stdout) { | ||
target.write(chunk) | ||
} | ||
if (fulfilled) { | ||
target.end() | ||
return target | ||
} | ||
if (isZurkAny(target)) { | ||
target.ctx.input = input | ||
_result = target | ||
} else if (target instanceof Writable) { | ||
_result = from.pipe(target) | ||
} else if (isStringLiteral(target, ...rest)) { | ||
_result = $.apply({ input: input, sync}, args) | ||
} else { | ||
throw new Error('Unsupported pipe argument') | ||
} | ||
|
||
return stdout.pipe(target) | ||
if (fulfilled) { | ||
fill() | ||
from.end() | ||
} else { | ||
const onStdout = (chunk: string | Buffer) => from.write(chunk) | ||
ee | ||
.once('stdout', () => { | ||
fill() | ||
ee.on('stdout', onStdout) | ||
}) | ||
.once('end', () => { | ||
ee.removeListener('stdout', onStdout) | ||
from.end() | ||
}) | ||
} | ||
|
||
return $.apply({input: fulfilled?.stdout || stdout, sync: !('then' in result)}, args as any) | ||
return _result | ||
} | ||
}) | ||
: result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters