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

feat: protect ProcessPromise from inappropriate instantiation effects #1097

Merged
merged 2 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{
"name": "zx/index",
"path": "build/*.{js,cjs}",
"limit": "809 kB",
"limit": "809.1 kB",
"brotli": false,
"gzip": false
},
Expand Down
60 changes: 34 additions & 26 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export interface Shell<
(opts: Partial<Omit<Options, 'sync'>>): Shell<true>
}
}
const bound: [boolean, string, string, Options][] = []
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe object is better?


export const $: Shell & Options = new Proxy<Shell & Options>(
function (pieces: TemplateStringsArray | Partial<Options>, ...args: any) {
Expand All @@ -164,25 +165,14 @@ export const $: Shell & Options = new Proxy<Shell & Options>(
checkShell()
checkQuote()

let resolve: Resolve, reject: Resolve
const process = new ProcessPromise((...args) => ([resolve, reject] = args))
const cmd = buildCmd(
$.quote as typeof quote,
pieces as TemplateStringsArray,
args
) as string
const sync = snapshot[SYNC]

process._bind(
cmd,
from,
resolve!,
(v: ProcessOutput) => {
reject!(v)
if (sync) throw v
},
snapshot
)
bound.push([sync, cmd, from, snapshot])
const process = new ProcessPromise(noop)

if (!process.isHalted() || sync) process.run()

Expand Down Expand Up @@ -237,19 +227,26 @@ export class ProcessPromise extends Promise<ProcessOutput> {
private _reject: Resolve = noop
private _resolve: Resolve = noop

_bind(
cmd: string,
from: string,
resolve: Resolve,
reject: Resolve,
options: Options
) {
this._command = cmd
this._from = from
this._resolve = resolve
this._reject = reject
this._snapshot = { ac: new AbortController(), ...options }
if (this._snapshot.halt) this._stage = 'halted'
constructor(executor: (resolve: Resolve, reject: Resolve) => void) {
let resolve: Resolve
let reject: Resolve
super((...args) => {
;[resolve, reject] = args
executor?.(...args)
})

if (executor === noop) {
const [sync, cmd, from, snapshot] = bound.pop()!
this._command = cmd
this._from = from
this._resolve = resolve!
this._reject = (v: ProcessOutput) => {
reject!(v)
if (sync) throw v
}
this._snapshot = { ac: new AbortController(), ...snapshot }
if (this._snapshot.halt) this._stage = 'halted'
} else ProcessPromise.disarm(this)
}

run(): ProcessPromise {
Expand Down Expand Up @@ -653,6 +650,17 @@ export class ProcessPromise extends Promise<ProcessOutput> {
this._stdin.removeListener(event, cb)
return this
}

// prettier-ignore
private static disarm(p: ProcessPromise, toggle = true): void {
Object.getOwnPropertyNames(ProcessPromise.prototype).forEach(k => {
if (k in Promise.prototype) return
if (!toggle) { Reflect.deleteProperty(p, k); return }
Object.defineProperty(p, k, { configurable: true, get() {
throw new Error('Inappropriate usage. Apply $ instead of direct instantiation.')
}})
})
}
}

type ProcessDto = {
Expand Down
22 changes: 17 additions & 5 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,12 +456,17 @@ describe('core', () => {

it('all transitions', async () => {
const { promise, resolve, reject } = Promise.withResolvers()
const p = new ProcessPromise(noop, noop)
const p = new ProcessPromise(() => {})
ProcessPromise.disarm(p, false)
assert.equal(p.stage, 'initial')
p._bind('echo foo', 'test', resolve, reject, {
...defaults,
halt: true,
})

p._command = 'echo foo'
p._from = 'test'
p._resolve = resolve
p._reject = reject
p._snapshot = { ...defaults }
p._stage = 'halted'

assert.equal(p.stage, 'halted')
p.run()
assert.equal(p.stage, 'running')
Expand Down Expand Up @@ -490,6 +495,13 @@ describe('core', () => {
assert.ok(p5 !== p1)
})

test('asserts self instantiation', async () => {
const p = new ProcessPromise(() => {})

assert(typeof p.then === 'function')
assert.throws(() => p.stage, /Inappropriate usage/)
})

test('resolves with ProcessOutput', async () => {
const o = await $`echo foo`
assert.ok(o instanceof ProcessOutput)
Expand Down