diff --git a/package.json b/package.json index 9dcebbf..c2003a1 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ ], "scripts": { "build": "concurrently 'npm:build:*'", - "build:js": "node ./src/scripts/build.mjs --entry='./src/main/ts/index.ts:./src/main/ts/util.ts:./src/main/ts/spawn.ts:./src/main/ts/zurk.ts'", + "build:js": "node ./src/scripts/build.mjs --entry='./src/main/ts/index.ts:./src/main/ts/util.ts:./src/main/ts/spawn.ts:./src/main/ts/zurk.ts:./src/main/ts/error.ts'", "build:dts": "tsc --emitDeclarationOnly --outDir target/dts", "build:docs": "typedoc --options src/main/typedoc", "build:stamp": "npx buildstamp", diff --git a/src/main/ts/error.ts b/src/main/ts/error.ts index 698be75..c8228ec 100644 --- a/src/main/ts/error.ts +++ b/src/main/ts/error.ts @@ -1,3 +1,16 @@ +/** + * @module zurk/error + * + * Zurk spawn error codes & handling utilities + * + * @example + * ```ts + * import {EXIT_CODES} from 'zurk/error' + * + * console.log(EXIT_CODES[2]) // 'Misuse of shell builtins' + * ``` + */ + export const EXIT_CODES = { 2: 'Misuse of shell builtins', 126: 'Invoked command cannot execute', diff --git a/src/main/ts/index.ts b/src/main/ts/index.ts index d7f9ac6..b9384be 100644 --- a/src/main/ts/index.ts +++ b/src/main/ts/index.ts @@ -6,3 +6,18 @@ export { invoke, exec, defaults } from './spawn.js' export { $ } from './x.js' export { zurk } from './zurk.js' export { type Promisified, buildCmd } from './util.js' + +/** + * @module zurk + * + * A generic process spawner + * + * @example + * ```ts + * 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` + * ``` + */ diff --git a/src/main/ts/spawn.ts b/src/main/ts/spawn.ts index 2d924e3..5b89f3e 100644 --- a/src/main/ts/spawn.ts +++ b/src/main/ts/spawn.ts @@ -4,6 +4,27 @@ import EventEmitter from 'node:events' import { Readable, Writable, Stream, Transform } from 'node:stream' import { assign, noop, randomId, g, immediate } from './util.js' +/** + * @module zurk/spawn + * + * Zurk internal child_process caller API + * + * @example + * ```ts + * import {invoke, normalizeCtx, TSpawnCtx} from 'zurk/spawn' + * + * const results: string[] = [] + * const callback: TSpawnCtx['callback'] = (_err, result) => results.push(result.stdout) + * + * invoke(normalizeCtx({ + * sync: true, + * cmd: 'echo', + * args: ['hello'], + * callback, + * })) + * ``` + */ + export * from './util.js' export type TSpawnError = any diff --git a/src/main/ts/util.ts b/src/main/ts/util.ts index 1c698e5..3bd8bff 100644 --- a/src/main/ts/util.ts +++ b/src/main/ts/util.ts @@ -2,6 +2,19 @@ import { Stream } from 'node:stream' import process from 'node:process' import { Buffer } from 'node:buffer' +/** + * @module zurk/util + * + * Zurk utility functions + * + * @example + * ```ts + * import {randomId} from 'zurk/util' + * + * randomId() // 'kdrx9bngrb' + * ``` + */ + export const g = (!process.versions.deno && global) || globalThis export const immediate = g.setImmediate || ((f: any): NodeJS.Timeout => g.setTimeout(f, 0)) diff --git a/src/main/ts/zurk.ts b/src/main/ts/zurk.ts index d9ea9e4..9b0d159 100644 --- a/src/main/ts/zurk.ts +++ b/src/main/ts/zurk.ts @@ -17,6 +17,21 @@ import { formatExitMessage } from './error.js' + +/** + * @module zurk/zurk + * + * Zurk process spawner + * + * @example + * ```ts + * import {zurk} from 'zurk/zurk' + * + * const r1 = zurk({ sync: true, cmd: 'echo', args: ['foo']}) + * const r2 = await zurk({ sync: false, cmd: 'echo', args: ['foo']}) + * ``` + */ + export const ZURK = Symbol('Zurk') export const ZURKPROXY = Symbol('ZurkProxy') diff --git a/src/scripts/build-jsr.mjs b/src/scripts/build-jsr.mjs index 3865f1c..48d9124 100644 --- a/src/scripts/build-jsr.mjs +++ b/src/scripts/build-jsr.mjs @@ -13,6 +13,9 @@ fs.writeFileSync(path.resolve(cwd, 'jsr.json'), JSON.stringify({ './zurk': './src/main/ts/zurk.ts' }, publish: { - include: ['src/main/ts'] + include: [ + 'src/main/ts', + 'README.md' + ] } }, null, 2)) diff --git a/src/test/ts/util.test.ts b/src/test/ts/util.test.ts index 177e7a1..fc3293c 100644 --- a/src/test/ts/util.test.ts +++ b/src/test/ts/util.test.ts @@ -1,6 +1,6 @@ import * as assert from 'node:assert' import {describe, it, test} from 'node:test' -import { assign, isStringLiteral } from '../../main/ts/util.js' +import { assign, isStringLiteral, randomId } from '../../main/ts/util.js' import tslib from 'tslib' describe('util', () => { @@ -9,6 +9,10 @@ describe('util', () => { assert.deepEqual(assign({a: 1}, {a: undefined}), {a: 1}) }) + it('randomId()', () => { + assert.match(randomId(), /^[\da-z]+$/) + }) + test('isStringLiteral()', () => { const bar = 'baz' assert.ok(isStringLiteral``) diff --git a/target/cjs/error.cjs b/target/cjs/error.cjs new file mode 100644 index 0000000..bfe1eff --- /dev/null +++ b/target/cjs/error.cjs @@ -0,0 +1,218 @@ +"use strict"; +const { + __export, + __toCommonJS +} = require('./cjslib.cjs'); + + +// src/main/ts/error.ts +var error_exports = {}; +__export(error_exports, { + ERRNO_CODES: () => ERRNO_CODES, + EXIT_CODES: () => EXIT_CODES, + formatErrorMessage: () => formatErrorMessage, + formatExitMessage: () => formatExitMessage, + getCallerLocation: () => getCallerLocation, + getCallerLocationFromString: () => getCallerLocationFromString, + getErrnoMessage: () => getErrnoMessage, + getExitCodeInfo: () => getExitCodeInfo +}); +module.exports = __toCommonJS(error_exports); +var EXIT_CODES = { + 2: "Misuse of shell builtins", + 126: "Invoked command cannot execute", + 127: "Command not found", + 128: "Invalid exit argument", + 129: "Hangup", + 130: "Interrupt", + 131: "Quit and dump core", + 132: "Illegal instruction", + 133: "Trace/breakpoint trap", + 134: "Process aborted", + 135: 'Bus error: "access to undefined portion of memory object"', + 136: 'Floating point exception: "erroneous arithmetic operation"', + 137: "Kill (terminate immediately)", + 138: "User-defined 1", + 139: "Segmentation violation", + 140: "User-defined 2", + 141: "Write to pipe with no one reading", + 142: "Signal raised by alarm", + 143: "Termination (request to terminate)", + 145: "Child process terminated, stopped (or continued*)", + 146: "Continue if stopped", + 147: "Stop executing temporarily", + 148: "Terminal stop signal", + 149: 'Background process attempting to read from tty ("in")', + 150: 'Background process attempting to write to tty ("out")', + 151: "Urgent data available on socket", + 152: "CPU time limit exceeded", + 153: "File size limit exceeded", + 154: 'Signal raised by timer counting virtual time: "virtual timer expired"', + 155: "Profiling timer expired", + 157: "Pollable event", + 159: "Bad syscall" +}; +var ERRNO_CODES = { + 0: "Success", + 1: "Not super-user", + 2: "No such file or directory", + 3: "No such process", + 4: "Interrupted system call", + 5: "I/O error", + 6: "No such device or address", + 7: "Arg list too long", + 8: "Exec format error", + 9: "Bad file number", + 10: "No children", + 11: "No more processes", + 12: "Not enough core", + 13: "Permission denied", + 14: "Bad address", + 15: "Block device required", + 16: "Mount device busy", + 17: "File exists", + 18: "Cross-device link", + 19: "No such device", + 20: "Not a directory", + 21: "Is a directory", + 22: "Invalid argument", + 23: "Too many open files in system", + 24: "Too many open files", + 25: "Not a typewriter", + 26: "Text file busy", + 27: "File too large", + 28: "No space left on device", + 29: "Illegal seek", + 30: "Read only file system", + 31: "Too many links", + 32: "Broken pipe", + 33: "Math arg out of domain of func", + 34: "Math result not representable", + 35: "File locking deadlock error", + 36: "File or path name too long", + 37: "No record locks available", + 38: "Function not implemented", + 39: "Directory not empty", + 40: "Too many symbolic links", + 42: "No message of desired type", + 43: "Identifier removed", + 44: "Channel number out of range", + 45: "Level 2 not synchronized", + 46: "Level 3 halted", + 47: "Level 3 reset", + 48: "Link number out of range", + 49: "Protocol driver not attached", + 50: "No CSI structure available", + 51: "Level 2 halted", + 52: "Invalid exchange", + 53: "Invalid request descriptor", + 54: "Exchange full", + 55: "No anode", + 56: "Invalid request code", + 57: "Invalid slot", + 59: "Bad font file fmt", + 60: "Device not a stream", + 61: "No data (for no delay io)", + 62: "Timer expired", + 63: "Out of streams resources", + 64: "Machine is not on the network", + 65: "Package not installed", + 66: "The object is remote", + 67: "The link has been severed", + 68: "Advertise error", + 69: "Srmount error", + 70: "Communication error on send", + 71: "Protocol error", + 72: "Multihop attempted", + 73: "Cross mount point (not really error)", + 74: "Trying to read unreadable message", + 75: "Value too large for defined data type", + 76: "Given log. name not unique", + 77: "f.d. invalid for this operation", + 78: "Remote address changed", + 79: "Can access a needed shared lib", + 80: "Accessing a corrupted shared lib", + 81: ".lib section in a.out corrupted", + 82: "Attempting to link in too many libs", + 83: "Attempting to exec a shared library", + 84: "Illegal byte sequence", + 86: "Streams pipe error", + 87: "Too many users", + 88: "Socket operation on non-socket", + 89: "Destination address required", + 90: "Message too long", + 91: "Protocol wrong type for socket", + 92: "Protocol not available", + 93: "Unknown protocol", + 94: "Socket type not supported", + 95: "Not supported", + 96: "Protocol family not supported", + 97: "Address family not supported by protocol family", + 98: "Address already in use", + 99: "Address not available", + 100: "Network interface is not configured", + 101: "Network is unreachable", + 102: "Connection reset by network", + 103: "Connection aborted", + 104: "Connection reset by peer", + 105: "No buffer space available", + 106: "Socket is already connected", + 107: "Socket is not connected", + 108: "Can't send after socket shutdown", + 109: "Too many references", + 110: "Connection timed out", + 111: "Connection refused", + 112: "Host is down", + 113: "Host is unreachable", + 114: "Socket already connected", + 115: "Connection already in progress", + 116: "Stale file handle", + 122: "Quota exceeded", + 123: "No medium (in tape drive)", + 125: "Operation canceled", + 130: "Previous owner died", + 131: "State not recoverable" +}; +function getErrnoMessage(errno) { + return ERRNO_CODES[-errno] || "Unknown error"; +} +function getExitCodeInfo(exitCode) { + return EXIT_CODES[exitCode]; +} +var formatExitMessage = (code, signal, stderr, from) => { + let message = `exit code: ${code}`; + if (code != 0 || signal != null) { + message = `${stderr || "\n"} at ${from}`; + message += ` + exit code: ${code}${getExitCodeInfo(code) ? " (" + getExitCodeInfo(code) + ")" : ""}`; + if (signal != null) { + message += ` + signal: ${signal}`; + } + } + return message; +}; +var formatErrorMessage = (err, from) => { + return `${err.message} + errno: ${err.errno} (${getErrnoMessage(err.errno)}) + code: ${err.code} + at ${from}`; +}; +function getCallerLocation(err = new Error("zurk error")) { + return getCallerLocationFromString(err.stack); +} +function getCallerLocationFromString(stackString = "unknown") { + var _a; + return ((_a = stackString.split(/^\s*(at\s)?/m).filter((s) => s == null ? void 0 : s.includes(":"))[2]) == null ? void 0 : _a.trim()) || stackString; +} +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + ERRNO_CODES, + EXIT_CODES, + formatErrorMessage, + formatExitMessage, + getCallerLocation, + getCallerLocationFromString, + getErrnoMessage, + getExitCodeInfo +}); \ No newline at end of file diff --git a/target/cjs/index.cjs b/target/cjs/index.cjs index bb55063..d421d30 100644 --- a/target/cjs/index.cjs +++ b/target/cjs/index.cjs @@ -22,15 +22,7 @@ var import_spawn2 = require("./spawn.cjs"); // src/main/ts/x.ts var import_zurk4 = require("./zurk.cjs"); var import_util4 = require("./util.cjs"); - -// src/main/ts/error.ts -function getCallerLocation(err = new Error("zurk error")) { - return getCallerLocationFromString(err.stack); -} -function getCallerLocationFromString(stackString = "unknown") { - var _a; - return ((_a = stackString.split(/^\s*(at\s)?/m).filter((s) => s == null ? void 0 : s.includes(":"))[2]) == null ? void 0 : _a.trim()) || stackString; -} +var import_error = require("./error.cjs"); // src/main/ts/mixin/pipe.ts var import_node_stream = require("stream"); @@ -135,7 +127,7 @@ var timeoutMixin = ($2, result, ctx) => { var $ = function(pieces, ...args) { const self = this !== import_util4.g && this; const preset = self || {}; - preset.stack = preset.stack || getCallerLocation(); + preset.stack = preset.stack || (0, import_error.getCallerLocation)(); if (pieces === void 0) return applyMixins($, preset); if ((0, import_util4.isStringLiteral)(pieces, ...args)) return ignite(preset, pieces, ...args); return (...args2) => $.apply(self ? (0, import_util4.assign)(self, pieces) : pieces, args2); diff --git a/target/cjs/zurk.cjs b/target/cjs/zurk.cjs index 0454caf..5b94dbf 100644 --- a/target/cjs/zurk.cjs +++ b/target/cjs/zurk.cjs @@ -25,190 +25,7 @@ __export(zurk_exports, { module.exports = __toCommonJS(zurk_exports); var import_spawn = require("./spawn.cjs"); var import_util = require("./util.cjs"); - -// src/main/ts/error.ts -var EXIT_CODES = { - 2: "Misuse of shell builtins", - 126: "Invoked command cannot execute", - 127: "Command not found", - 128: "Invalid exit argument", - 129: "Hangup", - 130: "Interrupt", - 131: "Quit and dump core", - 132: "Illegal instruction", - 133: "Trace/breakpoint trap", - 134: "Process aborted", - 135: 'Bus error: "access to undefined portion of memory object"', - 136: 'Floating point exception: "erroneous arithmetic operation"', - 137: "Kill (terminate immediately)", - 138: "User-defined 1", - 139: "Segmentation violation", - 140: "User-defined 2", - 141: "Write to pipe with no one reading", - 142: "Signal raised by alarm", - 143: "Termination (request to terminate)", - 145: "Child process terminated, stopped (or continued*)", - 146: "Continue if stopped", - 147: "Stop executing temporarily", - 148: "Terminal stop signal", - 149: 'Background process attempting to read from tty ("in")', - 150: 'Background process attempting to write to tty ("out")', - 151: "Urgent data available on socket", - 152: "CPU time limit exceeded", - 153: "File size limit exceeded", - 154: 'Signal raised by timer counting virtual time: "virtual timer expired"', - 155: "Profiling timer expired", - 157: "Pollable event", - 159: "Bad syscall" -}; -var ERRNO_CODES = { - 0: "Success", - 1: "Not super-user", - 2: "No such file or directory", - 3: "No such process", - 4: "Interrupted system call", - 5: "I/O error", - 6: "No such device or address", - 7: "Arg list too long", - 8: "Exec format error", - 9: "Bad file number", - 10: "No children", - 11: "No more processes", - 12: "Not enough core", - 13: "Permission denied", - 14: "Bad address", - 15: "Block device required", - 16: "Mount device busy", - 17: "File exists", - 18: "Cross-device link", - 19: "No such device", - 20: "Not a directory", - 21: "Is a directory", - 22: "Invalid argument", - 23: "Too many open files in system", - 24: "Too many open files", - 25: "Not a typewriter", - 26: "Text file busy", - 27: "File too large", - 28: "No space left on device", - 29: "Illegal seek", - 30: "Read only file system", - 31: "Too many links", - 32: "Broken pipe", - 33: "Math arg out of domain of func", - 34: "Math result not representable", - 35: "File locking deadlock error", - 36: "File or path name too long", - 37: "No record locks available", - 38: "Function not implemented", - 39: "Directory not empty", - 40: "Too many symbolic links", - 42: "No message of desired type", - 43: "Identifier removed", - 44: "Channel number out of range", - 45: "Level 2 not synchronized", - 46: "Level 3 halted", - 47: "Level 3 reset", - 48: "Link number out of range", - 49: "Protocol driver not attached", - 50: "No CSI structure available", - 51: "Level 2 halted", - 52: "Invalid exchange", - 53: "Invalid request descriptor", - 54: "Exchange full", - 55: "No anode", - 56: "Invalid request code", - 57: "Invalid slot", - 59: "Bad font file fmt", - 60: "Device not a stream", - 61: "No data (for no delay io)", - 62: "Timer expired", - 63: "Out of streams resources", - 64: "Machine is not on the network", - 65: "Package not installed", - 66: "The object is remote", - 67: "The link has been severed", - 68: "Advertise error", - 69: "Srmount error", - 70: "Communication error on send", - 71: "Protocol error", - 72: "Multihop attempted", - 73: "Cross mount point (not really error)", - 74: "Trying to read unreadable message", - 75: "Value too large for defined data type", - 76: "Given log. name not unique", - 77: "f.d. invalid for this operation", - 78: "Remote address changed", - 79: "Can access a needed shared lib", - 80: "Accessing a corrupted shared lib", - 81: ".lib section in a.out corrupted", - 82: "Attempting to link in too many libs", - 83: "Attempting to exec a shared library", - 84: "Illegal byte sequence", - 86: "Streams pipe error", - 87: "Too many users", - 88: "Socket operation on non-socket", - 89: "Destination address required", - 90: "Message too long", - 91: "Protocol wrong type for socket", - 92: "Protocol not available", - 93: "Unknown protocol", - 94: "Socket type not supported", - 95: "Not supported", - 96: "Protocol family not supported", - 97: "Address family not supported by protocol family", - 98: "Address already in use", - 99: "Address not available", - 100: "Network interface is not configured", - 101: "Network is unreachable", - 102: "Connection reset by network", - 103: "Connection aborted", - 104: "Connection reset by peer", - 105: "No buffer space available", - 106: "Socket is already connected", - 107: "Socket is not connected", - 108: "Can't send after socket shutdown", - 109: "Too many references", - 110: "Connection timed out", - 111: "Connection refused", - 112: "Host is down", - 113: "Host is unreachable", - 114: "Socket already connected", - 115: "Connection already in progress", - 116: "Stale file handle", - 122: "Quota exceeded", - 123: "No medium (in tape drive)", - 125: "Operation canceled", - 130: "Previous owner died", - 131: "State not recoverable" -}; -function getErrnoMessage(errno) { - return ERRNO_CODES[-errno] || "Unknown error"; -} -function getExitCodeInfo(exitCode) { - return EXIT_CODES[exitCode]; -} -var formatExitMessage = (code, signal, stderr, from) => { - let message = `exit code: ${code}`; - if (code != 0 || signal != null) { - message = `${stderr || "\n"} at ${from}`; - message += ` - exit code: ${code}${getExitCodeInfo(code) ? " (" + getExitCodeInfo(code) + ")" : ""}`; - if (signal != null) { - message += ` - signal: ${signal}`; - } - } - return message; -}; -var formatErrorMessage = (err, from) => { - return `${err.message} - errno: ${err.errno} (${getErrnoMessage(err.errno)}) - code: ${err.code} - at ${from}`; -}; - -// src/main/ts/zurk.ts +var import_error = require("./error.cjs"); var ZURK = Symbol("Zurk"); var ZURKPROXY = Symbol("ZurkProxy"); var zurk = (opts) => opts.sync ? zurkSync(opts) : zurkAsync(opts); @@ -262,9 +79,9 @@ var zurkifyPromise = (target, ctx) => { }; var getError = (spawnResult) => { if (spawnResult.error) - return new Error(formatErrorMessage(spawnResult.error, spawnResult.ctx.stack)); + return new Error((0, import_error.formatErrorMessage)(spawnResult.error, spawnResult.ctx.stack)); if (spawnResult.status || spawnResult.signal) - return new Error(formatExitMessage(spawnResult.status, spawnResult.signal, spawnResult.stderr, spawnResult.ctx.stack)); + return new Error((0, import_error.formatExitMessage)(spawnResult.status, spawnResult.signal, spawnResult.stderr, spawnResult.ctx.stack)); return null; }; var isZurkAny = (o) => (o == null ? void 0 : o[ZURK]) === ZURK; diff --git a/target/dts/error.d.ts b/target/dts/error.d.ts index 8c40b9d..129207c 100644 --- a/target/dts/error.d.ts +++ b/target/dts/error.d.ts @@ -1,3 +1,15 @@ +/** + * @module zurk/error + * + * Zurk spawn error codes & handling utilities + * + * @example + * ```ts + * import {EXIT_CODES} from 'zurk/error' + * + * console.log(EXIT_CODES[2]) // 'Misuse of shell builtins' + * ``` + */ export declare const EXIT_CODES: { 2: string; 126: string; diff --git a/target/dts/index.d.ts b/target/dts/index.d.ts index e9f0ca7..dd733b7 100644 --- a/target/dts/index.d.ts +++ b/target/dts/index.d.ts @@ -5,3 +5,17 @@ export { invoke, exec, defaults } from './spawn.js'; export { $ } from './x.js'; export { zurk } from './zurk.js'; export { type Promisified, buildCmd } from './util.js'; +/** + * @module zurk + * + * A generic process spawner + * + * @example + * ```ts + * 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` + * ``` + */ diff --git a/target/dts/spawn.d.ts b/target/dts/spawn.d.ts index c917689..06661b3 100644 --- a/target/dts/spawn.d.ts +++ b/target/dts/spawn.d.ts @@ -1,6 +1,26 @@ import * as cp from 'node:child_process'; import EventEmitter from 'node:events'; import { Readable, Writable, Stream, Transform } from 'node:stream'; +/** + * @module zurk/spawn + * + * Zurk internal child_process caller API + * + * @example + * ```ts + * import {invoke, normalizeCtx, TSpawnCtx} from 'zurk/spawn' + * + * const results: string[] = [] + * const callback: TSpawnCtx['callback'] = (_err, result) => results.push(result.stdout) + * + * invoke(normalizeCtx({ + * sync: true, + * cmd: 'echo', + * args: ['hello'], + * callback, + * })) + * ``` + */ export * from './util.js'; export type TSpawnError = any; export type TPushable = { diff --git a/target/dts/util.d.ts b/target/dts/util.d.ts index 37827fe..3b2d8a2 100644 --- a/target/dts/util.d.ts +++ b/target/dts/util.d.ts @@ -1,5 +1,17 @@ import { Stream } from 'node:stream'; import { Buffer } from 'node:buffer'; +/** + * @module zurk/util + * + * Zurk utility functions + * + * @example + * ```ts + * import {randomId} from 'zurk/util' + * + * randomId() // 'kdrx9bngrb' + * ``` + */ export declare const g: typeof globalThis; export declare const immediate: typeof setImmediate; export declare const noop: () => void; diff --git a/target/dts/zurk.d.ts b/target/dts/zurk.d.ts index c5476fe..23f0167 100644 --- a/target/dts/zurk.d.ts +++ b/target/dts/zurk.d.ts @@ -1,5 +1,18 @@ import { type TSpawnCtxNormalized, type TSpawnResult, type TSpawnListeners } from './spawn.js'; import { type Promisified } from './util.js'; +/** + * @module zurk/zurk + * + * Zurk process spawner + * + * @example + * ```ts + * import {zurk} from 'zurk/zurk' + * + * const r1 = zurk({ sync: true, cmd: 'echo', args: ['foo']}) + * const r2 = await zurk({ sync: false, cmd: 'echo', args: ['foo']}) + * ``` + */ export declare const ZURK: unique symbol; export declare const ZURKPROXY: unique symbol; export interface TZurkOn { diff --git a/target/esm/error.mjs b/target/esm/error.mjs new file mode 100644 index 0000000..16d4cdf --- /dev/null +++ b/target/esm/error.mjs @@ -0,0 +1,198 @@ +// src/main/ts/error.ts +var EXIT_CODES = { + 2: "Misuse of shell builtins", + 126: "Invoked command cannot execute", + 127: "Command not found", + 128: "Invalid exit argument", + 129: "Hangup", + 130: "Interrupt", + 131: "Quit and dump core", + 132: "Illegal instruction", + 133: "Trace/breakpoint trap", + 134: "Process aborted", + 135: 'Bus error: "access to undefined portion of memory object"', + 136: 'Floating point exception: "erroneous arithmetic operation"', + 137: "Kill (terminate immediately)", + 138: "User-defined 1", + 139: "Segmentation violation", + 140: "User-defined 2", + 141: "Write to pipe with no one reading", + 142: "Signal raised by alarm", + 143: "Termination (request to terminate)", + 145: "Child process terminated, stopped (or continued*)", + 146: "Continue if stopped", + 147: "Stop executing temporarily", + 148: "Terminal stop signal", + 149: 'Background process attempting to read from tty ("in")', + 150: 'Background process attempting to write to tty ("out")', + 151: "Urgent data available on socket", + 152: "CPU time limit exceeded", + 153: "File size limit exceeded", + 154: 'Signal raised by timer counting virtual time: "virtual timer expired"', + 155: "Profiling timer expired", + 157: "Pollable event", + 159: "Bad syscall" +}; +var ERRNO_CODES = { + 0: "Success", + 1: "Not super-user", + 2: "No such file or directory", + 3: "No such process", + 4: "Interrupted system call", + 5: "I/O error", + 6: "No such device or address", + 7: "Arg list too long", + 8: "Exec format error", + 9: "Bad file number", + 10: "No children", + 11: "No more processes", + 12: "Not enough core", + 13: "Permission denied", + 14: "Bad address", + 15: "Block device required", + 16: "Mount device busy", + 17: "File exists", + 18: "Cross-device link", + 19: "No such device", + 20: "Not a directory", + 21: "Is a directory", + 22: "Invalid argument", + 23: "Too many open files in system", + 24: "Too many open files", + 25: "Not a typewriter", + 26: "Text file busy", + 27: "File too large", + 28: "No space left on device", + 29: "Illegal seek", + 30: "Read only file system", + 31: "Too many links", + 32: "Broken pipe", + 33: "Math arg out of domain of func", + 34: "Math result not representable", + 35: "File locking deadlock error", + 36: "File or path name too long", + 37: "No record locks available", + 38: "Function not implemented", + 39: "Directory not empty", + 40: "Too many symbolic links", + 42: "No message of desired type", + 43: "Identifier removed", + 44: "Channel number out of range", + 45: "Level 2 not synchronized", + 46: "Level 3 halted", + 47: "Level 3 reset", + 48: "Link number out of range", + 49: "Protocol driver not attached", + 50: "No CSI structure available", + 51: "Level 2 halted", + 52: "Invalid exchange", + 53: "Invalid request descriptor", + 54: "Exchange full", + 55: "No anode", + 56: "Invalid request code", + 57: "Invalid slot", + 59: "Bad font file fmt", + 60: "Device not a stream", + 61: "No data (for no delay io)", + 62: "Timer expired", + 63: "Out of streams resources", + 64: "Machine is not on the network", + 65: "Package not installed", + 66: "The object is remote", + 67: "The link has been severed", + 68: "Advertise error", + 69: "Srmount error", + 70: "Communication error on send", + 71: "Protocol error", + 72: "Multihop attempted", + 73: "Cross mount point (not really error)", + 74: "Trying to read unreadable message", + 75: "Value too large for defined data type", + 76: "Given log. name not unique", + 77: "f.d. invalid for this operation", + 78: "Remote address changed", + 79: "Can access a needed shared lib", + 80: "Accessing a corrupted shared lib", + 81: ".lib section in a.out corrupted", + 82: "Attempting to link in too many libs", + 83: "Attempting to exec a shared library", + 84: "Illegal byte sequence", + 86: "Streams pipe error", + 87: "Too many users", + 88: "Socket operation on non-socket", + 89: "Destination address required", + 90: "Message too long", + 91: "Protocol wrong type for socket", + 92: "Protocol not available", + 93: "Unknown protocol", + 94: "Socket type not supported", + 95: "Not supported", + 96: "Protocol family not supported", + 97: "Address family not supported by protocol family", + 98: "Address already in use", + 99: "Address not available", + 100: "Network interface is not configured", + 101: "Network is unreachable", + 102: "Connection reset by network", + 103: "Connection aborted", + 104: "Connection reset by peer", + 105: "No buffer space available", + 106: "Socket is already connected", + 107: "Socket is not connected", + 108: "Can't send after socket shutdown", + 109: "Too many references", + 110: "Connection timed out", + 111: "Connection refused", + 112: "Host is down", + 113: "Host is unreachable", + 114: "Socket already connected", + 115: "Connection already in progress", + 116: "Stale file handle", + 122: "Quota exceeded", + 123: "No medium (in tape drive)", + 125: "Operation canceled", + 130: "Previous owner died", + 131: "State not recoverable" +}; +function getErrnoMessage(errno) { + return ERRNO_CODES[-errno] || "Unknown error"; +} +function getExitCodeInfo(exitCode) { + return EXIT_CODES[exitCode]; +} +var formatExitMessage = (code, signal, stderr, from) => { + let message = `exit code: ${code}`; + if (code != 0 || signal != null) { + message = `${stderr || "\n"} at ${from}`; + message += ` + exit code: ${code}${getExitCodeInfo(code) ? " (" + getExitCodeInfo(code) + ")" : ""}`; + if (signal != null) { + message += ` + signal: ${signal}`; + } + } + return message; +}; +var formatErrorMessage = (err, from) => { + return `${err.message} + errno: ${err.errno} (${getErrnoMessage(err.errno)}) + code: ${err.code} + at ${from}`; +}; +function getCallerLocation(err = new Error("zurk error")) { + return getCallerLocationFromString(err.stack); +} +function getCallerLocationFromString(stackString = "unknown") { + var _a; + return ((_a = stackString.split(/^\s*(at\s)?/m).filter((s) => s == null ? void 0 : s.includes(":"))[2]) == null ? void 0 : _a.trim()) || stackString; +} +export { + ERRNO_CODES, + EXIT_CODES, + formatErrorMessage, + formatExitMessage, + getCallerLocation, + getCallerLocationFromString, + getErrnoMessage, + getExitCodeInfo +}; diff --git a/target/esm/index.mjs b/target/esm/index.mjs index 92689f1..fb40d1d 100644 --- a/target/esm/index.mjs +++ b/target/esm/index.mjs @@ -17,15 +17,7 @@ import { g, immediate } from "./util.mjs"; - -// src/main/ts/error.ts -function getCallerLocation(err = new Error("zurk error")) { - return getCallerLocationFromString(err.stack); -} -function getCallerLocationFromString(stackString = "unknown") { - var _a; - return ((_a = stackString.split(/^\s*(at\s)?/m).filter((s) => s == null ? void 0 : s.includes(":"))[2]) == null ? void 0 : _a.trim()) || stackString; -} +import { getCallerLocation } from "./error.mjs"; // src/main/ts/mixin/pipe.ts import { Writable } from "node:stream"; diff --git a/target/esm/zurk.mjs b/target/esm/zurk.mjs index 0d74f7e..89153b9 100644 --- a/target/esm/zurk.mjs +++ b/target/esm/zurk.mjs @@ -12,190 +12,10 @@ import { isPromiseLike, makeDeferred } from "./util.mjs"; - -// src/main/ts/error.ts -var EXIT_CODES = { - 2: "Misuse of shell builtins", - 126: "Invoked command cannot execute", - 127: "Command not found", - 128: "Invalid exit argument", - 129: "Hangup", - 130: "Interrupt", - 131: "Quit and dump core", - 132: "Illegal instruction", - 133: "Trace/breakpoint trap", - 134: "Process aborted", - 135: 'Bus error: "access to undefined portion of memory object"', - 136: 'Floating point exception: "erroneous arithmetic operation"', - 137: "Kill (terminate immediately)", - 138: "User-defined 1", - 139: "Segmentation violation", - 140: "User-defined 2", - 141: "Write to pipe with no one reading", - 142: "Signal raised by alarm", - 143: "Termination (request to terminate)", - 145: "Child process terminated, stopped (or continued*)", - 146: "Continue if stopped", - 147: "Stop executing temporarily", - 148: "Terminal stop signal", - 149: 'Background process attempting to read from tty ("in")', - 150: 'Background process attempting to write to tty ("out")', - 151: "Urgent data available on socket", - 152: "CPU time limit exceeded", - 153: "File size limit exceeded", - 154: 'Signal raised by timer counting virtual time: "virtual timer expired"', - 155: "Profiling timer expired", - 157: "Pollable event", - 159: "Bad syscall" -}; -var ERRNO_CODES = { - 0: "Success", - 1: "Not super-user", - 2: "No such file or directory", - 3: "No such process", - 4: "Interrupted system call", - 5: "I/O error", - 6: "No such device or address", - 7: "Arg list too long", - 8: "Exec format error", - 9: "Bad file number", - 10: "No children", - 11: "No more processes", - 12: "Not enough core", - 13: "Permission denied", - 14: "Bad address", - 15: "Block device required", - 16: "Mount device busy", - 17: "File exists", - 18: "Cross-device link", - 19: "No such device", - 20: "Not a directory", - 21: "Is a directory", - 22: "Invalid argument", - 23: "Too many open files in system", - 24: "Too many open files", - 25: "Not a typewriter", - 26: "Text file busy", - 27: "File too large", - 28: "No space left on device", - 29: "Illegal seek", - 30: "Read only file system", - 31: "Too many links", - 32: "Broken pipe", - 33: "Math arg out of domain of func", - 34: "Math result not representable", - 35: "File locking deadlock error", - 36: "File or path name too long", - 37: "No record locks available", - 38: "Function not implemented", - 39: "Directory not empty", - 40: "Too many symbolic links", - 42: "No message of desired type", - 43: "Identifier removed", - 44: "Channel number out of range", - 45: "Level 2 not synchronized", - 46: "Level 3 halted", - 47: "Level 3 reset", - 48: "Link number out of range", - 49: "Protocol driver not attached", - 50: "No CSI structure available", - 51: "Level 2 halted", - 52: "Invalid exchange", - 53: "Invalid request descriptor", - 54: "Exchange full", - 55: "No anode", - 56: "Invalid request code", - 57: "Invalid slot", - 59: "Bad font file fmt", - 60: "Device not a stream", - 61: "No data (for no delay io)", - 62: "Timer expired", - 63: "Out of streams resources", - 64: "Machine is not on the network", - 65: "Package not installed", - 66: "The object is remote", - 67: "The link has been severed", - 68: "Advertise error", - 69: "Srmount error", - 70: "Communication error on send", - 71: "Protocol error", - 72: "Multihop attempted", - 73: "Cross mount point (not really error)", - 74: "Trying to read unreadable message", - 75: "Value too large for defined data type", - 76: "Given log. name not unique", - 77: "f.d. invalid for this operation", - 78: "Remote address changed", - 79: "Can access a needed shared lib", - 80: "Accessing a corrupted shared lib", - 81: ".lib section in a.out corrupted", - 82: "Attempting to link in too many libs", - 83: "Attempting to exec a shared library", - 84: "Illegal byte sequence", - 86: "Streams pipe error", - 87: "Too many users", - 88: "Socket operation on non-socket", - 89: "Destination address required", - 90: "Message too long", - 91: "Protocol wrong type for socket", - 92: "Protocol not available", - 93: "Unknown protocol", - 94: "Socket type not supported", - 95: "Not supported", - 96: "Protocol family not supported", - 97: "Address family not supported by protocol family", - 98: "Address already in use", - 99: "Address not available", - 100: "Network interface is not configured", - 101: "Network is unreachable", - 102: "Connection reset by network", - 103: "Connection aborted", - 104: "Connection reset by peer", - 105: "No buffer space available", - 106: "Socket is already connected", - 107: "Socket is not connected", - 108: "Can't send after socket shutdown", - 109: "Too many references", - 110: "Connection timed out", - 111: "Connection refused", - 112: "Host is down", - 113: "Host is unreachable", - 114: "Socket already connected", - 115: "Connection already in progress", - 116: "Stale file handle", - 122: "Quota exceeded", - 123: "No medium (in tape drive)", - 125: "Operation canceled", - 130: "Previous owner died", - 131: "State not recoverable" -}; -function getErrnoMessage(errno) { - return ERRNO_CODES[-errno] || "Unknown error"; -} -function getExitCodeInfo(exitCode) { - return EXIT_CODES[exitCode]; -} -var formatExitMessage = (code, signal, stderr, from) => { - let message = `exit code: ${code}`; - if (code != 0 || signal != null) { - message = `${stderr || "\n"} at ${from}`; - message += ` - exit code: ${code}${getExitCodeInfo(code) ? " (" + getExitCodeInfo(code) + ")" : ""}`; - if (signal != null) { - message += ` - signal: ${signal}`; - } - } - return message; -}; -var formatErrorMessage = (err, from) => { - return `${err.message} - errno: ${err.errno} (${getErrnoMessage(err.errno)}) - code: ${err.code} - at ${from}`; -}; - -// src/main/ts/zurk.ts +import { + formatErrorMessage, + formatExitMessage +} from "./error.mjs"; var ZURK = Symbol("Zurk"); var ZURKPROXY = Symbol("ZurkProxy"); var zurk = (opts) => opts.sync ? zurkSync(opts) : zurkAsync(opts);