Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: do not hack stream behavior on promisify #923

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,10 +531,10 @@ export class ProcessPromise extends Promise<ProcessOutput> {
return super.catch(onrejected)
}

private static promisifyStream<D extends Writable>(
dest: D
): D & PromiseLike<void> {
return new Proxy(dest as D & PromiseLike<void>, {
private static promisifyStream<S extends Writable>(
stream: S
): S & PromiseLike<void> {
return new Proxy(stream as S & PromiseLike<void>, {
get(target, key) {
if (key === 'then') {
return (res: any = noop, rej: any = noop) =>
Expand All @@ -548,11 +548,9 @@ export class ProcessPromise extends Promise<ProcessOutput> {
const pipe = Reflect.get(target, key)
if (typeof pipe === 'function')
return function (...args: any) {
return (
args[0] instanceof ProcessPromise
? noop
: ProcessPromise.promisifyStream
)(pipe.apply(target, args) as Writable)
return ProcessPromise.promisifyStream(
pipe.apply(target, args) as S
)
}
}
return Reflect.get(target, key)
Expand Down
52 changes: 27 additions & 25 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,33 +397,35 @@ describe('core', () => {
)
})

test('is chainable ($)', async () => {
let { stdout: o1 } = await $`echo "hello"`
.pipe($`awk '{print $1" world"}'`)
.pipe($`tr '[a-z]' '[A-Z]'`)
assert.equal(o1, 'HELLO WORLD\n')

let { stdout: o2 } = await $`echo "hello"`
.pipe`awk '{print $1" world"}'`.pipe`tr '[a-z]' '[A-Z]'`
assert.equal(o2, 'HELLO WORLD\n')
})
describe('is chainable', () => {
test('$ to $', async () => {
let { stdout: o1 } = await $`echo "hello"`
.pipe($`awk '{print $1" world"}'`)
.pipe($`tr '[a-z]' '[A-Z]'`)
assert.equal(o1, 'HELLO WORLD\n')

let { stdout: o2 } = await $`echo "hello"`
.pipe`awk '{print $1" world"}'`.pipe`tr '[a-z]' '[A-Z]'`
assert.equal(o2, 'HELLO WORLD\n')
})

test('is chainable (Transform/Duplex)', async () => {
const file = tempfile()
const p = $`echo "hello"`
.pipe(
new Transform({
transform(chunk, encoding, callback) {
callback(null, String(chunk).toUpperCase())
},
})
)
.pipe(fs.createWriteStream(file))
test('Transform/Duplex', async () => {
const file = tempfile()
const p = $`echo "hello"`
.pipe(
new Transform({
transform(chunk, encoding, callback) {
callback(null, String(chunk).toUpperCase())
},
})
)
.pipe(fs.createWriteStream(file))

assert.ok(p instanceof WriteStream)
assert.equal(await p, undefined)
assert.equal((await fs.readFile(file)).toString(), 'HELLO\n')
await fs.rm(file)
assert.ok(p instanceof WriteStream)
assert.equal(await p, undefined)
assert.equal((await fs.readFile(file)).toString(), 'HELLO\n')
await fs.rm(file)
})
})

it('supports multipiping', async () => {
Expand Down