Package detail

zurk

webpod19.7kMIT0.11.3

A generic process spawner

readme

zurk

– cute sounds but never friendly.
– eats all kinds of materials.

🔬🧫

This subproject is a kind of experiment, addressed to the google/zx/issues/589. Just a testing ground for verifying ideas and approaches aimed at improve the zx architecture.

Concepts

  • Layered architecture:
    • spawn builds a configurable exec context around the node:child_process API.
    • zurk implements the API for sync and async executions.
    • x provides the basic template-string API.
  • Granularity: the package provides several entry points to help user to choose the right level of abstraction and/or to assist with tree-shaking.
  • Extensibility:
    • The context object at every layer is accessible fo modify.
    • Typings are mostly represented by interfaces, so it's easy to tweak up if necessary.

Requirements

  • OS
    • Linux
    • MacOS
    • Windows
  • Runtime
    • Node.js >= 6 (CJS)
    • Node.js >= 12 (ESM)
    • Bun >= 1.0.0
    • Deno >= 1.7.0, 2.x

Install

yarn add zurk

API

import {$, exec, zurk} from 'zurk'

const r1 = exec({sync: true, cmd: 'echo foo'})
const r2 = await zurk({sync: false, cmd: 'echo foo'})
const r3 = await $`echo foo`

Proposals

  • [x] Promises in cmd literals

    const foo = $`echo foo`
    const foobarbaz = (await $`echo ${foo} ${$`echo bar`} ${await $`echo baz`}`)
    
  • [x] Both sync and async executions ``ts const p1 = $echo fooconst p2 = $({sync: true})echo foo`

const o1 = (await p1).toString() // foo const o2 = await p1.stdout // foo const o3 = p2.stdout // foo


- [x] Configurable input
```ts
const input = '{"name": "foo"}'
const name = await $({input})`jq -r .name` // foo

const stdin = fs.createReadStream(path.join(fixtures, 'foo.json'))
const data = await $({stdin})`jq -r .data` // foo

const p = $`echo "5\\n3\\n1\\n4\\n2"`
const sorted = $({input: p})`sort`          // 1\n2\n3\n4\n5
  • [x] Pipe literals ``ts const result = $echo "5\n3\n1\n4\n2"`

const piped0 = result.pipesort | cat // '1\n2\n3\n4\n5' const piped1 = result.pipesort.pipecat // ... const piped2 = (await result).pipesort const piped3 = result.pipe($sort)


- [x] Pipe splitting
```ts
const result = $`echo 1; sleep 1; echo 2; sleep 1; echo 3`
const piped1 = result.pipe`cat`
let piped2: any

setTimeout(() => {
  piped2 = result.pipe`cat`
}, 1500)

await piped1
assert.equal((await piped1).toString(), '1\n2\n3')
assert.equal((await piped2).toString(), '1\n2\n3')
  • [x] Presets ```ts const $$ = $({sync: true, cmd: 'echo foo'}) const $$$ = $$({cmd: 'echo bar'})

const p1 = $$() // foo const p2 = $$$() // bar const p3 = $$echo baz // baz


- [x] AbortController
```ts
const ac = new AbortController()
const p = $({nothrow: true, ac})`sleep 10`
setTimeout(() => {
  ac.signal.abort() // or just `p.abort()`
}, 500)

const { error } = await p
error.message // 'The operation was aborted'
  • [x] Stdout limit
import {type TSpawnStore, $} from 'zurk'

const getFixedSizeArray = (size: number) => {
  const arr: any[] = []
  return new Proxy(arr, {
    get: (target: any, prop) =>
      prop === 'push' && arr.length >= size
        ? () => {}
        : target[prop]
  })
}
const store: TSpawnStore = {
  stdout: getFixedSizeArray(1),
  stderr: getFixedSizeArray(2),
  stdall: getFixedSizeArray(0)
}

const result = await $({store})`echo hello`
result.stdout // 'hello\n'
result.stdall // ''
  • [x] Built-in quote for bash and powershell ```ts import {quote, quotePwsh} from 'zurk'

const arg = 'foo bar' $({quote})echo ${arg} // "echo $'foo bar'" $({quote: quotePwsh})echo ${arg} // "echo 'foo bar'" ```

License

MIT

changelog

0.11.3 (2025-06-22)

Fixes & improvements

  • fix: pass subs fn for nested buildCmd calls (4beca8b)

0.11.2 (2025-04-01)

Fixes & improvements

0.11.1 (2025-03-31)

Fixes & improvements

  • refactor: use .ts ext in sources (2504eab)

0.11.0 (2025-03-27)

Features

  • feat: expose quote and quotePwsh (f5a1c61)

0.10.3 (2025-03-27)

Fixes & improvements

  • fix: strengthen spawn.input type (bef4919)

0.10.2 (2025-01-12)

Fixes & improvements

0.10.1 (2025-01-10)

Fixes & improvements

  • fix: make spawnChunkStore reducible (c04f309)

0.10.0 (2024-12-25)

Features

  • feat: introduce stdall event (13d8abd)

0.9.3 (2024-12-17)

Fixes & improvements

  • fix: apply forced stdio destroy for abort flow only (bbac4fe)
  • fix: force stdio destroy on abort/child.kill() (cd32992)

0.9.2 (2024-12-09)

Fixes & improvements

  • docs: improve jsr docs refs (770c846)
  • fix: build error chunk separately (556ef0f)

0.9.1 (2024-12-09)

Fixes & improvements

  • fix: relax isTemplateLiteral check for tslib compat (898f0d0)

0.9.0 (2024-12-08)

Features

  • feat: let shell be false (5b4ebdb)

0.8.0 (2024-12-06)

Features

  • feat: apply formatting to $ error messages (6b1eb65)
  • feat: provide error utils (0b6949b)

0.7.5 (2024-12-03)

Fixes & improvements

0.7.4 (2024-12-03)

Fixes & improvements

0.7.3 (2024-12-03)

Fixes & improvements

  • fix: move jsr publish to pkg postrelease script (4c353c5)

0.7.2 (2024-12-03)

Fixes & improvements

  • fix: fix jsr publish flow (240386b)

0.7.1 (2024-12-03)

Fixes & improvements

  • perf: enable publishing to jsr (044ea18)

0.7.0 (2024-12-03)

Features

  • feat: store target in git to support distribution via gh refs (d3b3c20)

0.6.3 (2024-11-10)

Fixes & improvements

  • fix: unhandledRejection on proxified finally (5877c68)

0.6.2 (2024-11-10)

Fixes & improvements

  • fix(spawn): handle nullable stdout/stderr (030decd)

0.6.1 (2024-10-30)

Fixes & improvements

  • fix: fix TShellOptions type (baa38f7)

0.6.0 (2024-10-17)

Features

  • feat: provide deno support (ec3addd)

0.5.0 (2024-10-02)

Fixes & improvements

  • docs: mention pipe split feature (439669d)
  • perf: make pipe chunked (4daae39)

Features

  • feat: provide multipiping (a4e0312)

0.4.4 (2024-09-25)

Fixes & improvements

  • fix: fill stdall store on sync spawn (c63c58d)

0.4.3 (2024-09-20)

Fixes & improvements

  • fix: apply events detach on process end (3d393be)

0.4.2 (2024-09-20)

Fixes & improvements

  • perf: release ee handlers on process end (49d89a9)

0.4.1 (2024-09-20)

Fixes & improvements

  • refactor: enhance internal zurk objects detectors (bcba34d)

0.4.0 (2024-09-20)

Features

  • feat: export spawn ctx defaults (d4466b0)

0.3.5 (2024-09-19)

Fixes & improvements

  • fix: enhance TemplateStringArray detection (10dc031)

0.3.4 (2024-09-18)

Fixes & improvements

  • fix(type): add index to TSpawnChunks type (c61538a)

0.3.3 (2024-09-18)

Fixes & improvements

  • fix(type): add length prop to TSpawnChunks type (93feadf)

0.3.2 (2024-09-11)

Fixes & improvements

  • fix: enhance abort handlers clean up (#13) (cf0211a)

0.3.1 (2024-09-05)

Fixes & improvements

  • fix: release on abort signal handler (c575cd4)

0.3.0 (2024-06-12)

Features

  • feat: provide chunks store customization (165b020)

0.2.0 (2024-06-01)

Features

  • feat: provide compat with nodejs 6+ (cjs) and nodejs 12+ (esm) (1606b98)

0.1.4 (2024-04-30)

Fixes & improvements

  • fix: replace Object.hasOwn with Object.hasOwnPrototype (1fb5262)

0.1.3 (2024-04-27)

Fixes & improvements

  • fix: let AbortController API be optional (a21e1b9)

0.1.2 (2024-04-07)

Fixes & improvements

0.1.1 (2024-04-07)

Fixes & improvements

  • fix: provide stdio customization (cbfc232)

0.1.0 (2024-04-06)

Features

  • feat: provide signal opt (dc2b7ea)

0.0.32 (2024-03-26)

Fixes & improvements

  • perf: migrate from yarn to npm (6c455a2)

0.0.31 (2024-03-17)

Fixes & improvements

  • fix: apply undefined-filter to assign (1463ca8)

0.0.30 (2024-03-17)

Fixes & improvements

  • fix: set detached false for win32 by default (4d4e3c0)
  • docs: formatting (c907044)
  • docs: md formatting (04a65a4)
  • refactor: move cmd builder to utils (60123a5)
  • docs: provide minimal usage example (ae22952)
  • docs(zurk): describe main concepts (8a9442b)
  • refactor: move cmd builder to utils (e73bb7a)
  • perf(spawn): compose normalize and invoke (4e07348)
  • refactor: move ee subscriber to spawn layer (c91ff33)
  • refactor: define TZurkOn handlers ifaces (86d19dd)
  • refactor(spawn): remove onStdout and onStderr handlers in favor or on('stdout', () => {...}) (7950a6c)

Features

  • feat: introduce listeners map, rename _ctx to ctx (616b091)
  • feat: pass reason arg to internal abortController (76441c5)