@orkestrel/process 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +28 -22
- package/dist/src/core/index.cjs +85 -8
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +330 -90
- package/dist/src/core/index.d.ts +330 -90
- package/dist/src/core/index.js +79 -9
- package/dist/src/core/index.js.map +1 -1
- package/dist/src/server/index.cjs +1099 -267
- package/dist/src/server/index.cjs.map +1 -1
- package/dist/src/server/index.d.cts +645 -101
- package/dist/src/server/index.d.ts +645 -101
- package/dist/src/server/index.js +1076 -265
- package/dist/src/server/index.js.map +1 -1
- package/package.json +17 -6
package/dist/src/core/index.js
CHANGED
|
@@ -1,11 +1,27 @@
|
|
|
1
|
-
import { holds } from "@orkestrel/contract";
|
|
1
|
+
import { holds, isError } from "@orkestrel/contract";
|
|
2
2
|
//#region src/core/constants.ts
|
|
3
|
-
/** The default cooperative window in milliseconds between `SIGTERM` and `SIGKILL` during termination. */
|
|
3
|
+
/** The default cooperative POSIX window in milliseconds between `SIGTERM` and `SIGKILL` during termination. */
|
|
4
4
|
var PROCESS_GRACE = 5e3;
|
|
5
|
+
/** The window in milliseconds a termination waits for the child's native exit after the final kill. */
|
|
6
|
+
var PROCESS_CONFIRMATION = 5e3;
|
|
5
7
|
/** The default maximum retained stderr tail in bytes for a supervised {@link ProcessInterface}. */
|
|
6
8
|
var PROCESS_EVIDENCE = 2048;
|
|
9
|
+
/** The default soft high-water mark in bytes for a supervised {@link ProcessInterface} line backlog. */
|
|
10
|
+
var PROCESS_BACKLOG = 10485760;
|
|
7
11
|
/** The default maximum captured bytes for a one-shot run's stdout and stderr, each. */
|
|
8
12
|
var PROCESS_OUTPUT = 10485760;
|
|
13
|
+
/** The largest timer delay in milliseconds the host schedules without truncating it to one. */
|
|
14
|
+
var PROCESS_TIMER = 2147483647;
|
|
15
|
+
/** The executable extensions a Windows lookup applies when the environment declares no `PATHEXT`. */
|
|
16
|
+
var PROCESS_PATHEXT = ".COM;.EXE;.BAT;.CMD";
|
|
17
|
+
/** The machine-readable failure categories a {@link ProcessError} carries, in declaration order. */
|
|
18
|
+
var PROCESS_ERROR_CODES = Object.freeze([
|
|
19
|
+
"spawn",
|
|
20
|
+
"timeout",
|
|
21
|
+
"duplicate",
|
|
22
|
+
"protocol",
|
|
23
|
+
"invalid"
|
|
24
|
+
]);
|
|
9
25
|
//#endregion
|
|
10
26
|
//#region src/core/errors.ts
|
|
11
27
|
/** A child-process failure with a stable machine-readable category. */
|
|
@@ -24,6 +40,7 @@ var ProcessError = class extends Error {
|
|
|
24
40
|
constructor(message, options) {
|
|
25
41
|
const cause = options.cause;
|
|
26
42
|
super(message, cause === void 0 ? void 0 : { cause });
|
|
43
|
+
Object.defineProperty(this, Symbol.for("@orkestrel/process.error"), { value: true });
|
|
27
44
|
this.code = options.code;
|
|
28
45
|
if (options.context !== void 0) this.context = options.context;
|
|
29
46
|
if (options.result !== void 0) this.result = options.result;
|
|
@@ -32,6 +49,13 @@ var ProcessError = class extends Error {
|
|
|
32
49
|
/**
|
|
33
50
|
* Checks whether an unknown value is a {@link ProcessError}.
|
|
34
51
|
*
|
|
52
|
+
* @remarks
|
|
53
|
+
* Recognition combines a global own-property brand with the native `Error` base, the subclass
|
|
54
|
+
* prototype, the fixed name, and a code {@link PROCESS_ERROR_CODES} declares. The brand is
|
|
55
|
+
* recognized across duplicate installations and ESM/CommonJS module copies at 0.0.4 or later. A copy
|
|
56
|
+
* earlier than 0.0.4 stamps no brand, so an error it throws stays outside the type, and so does a
|
|
57
|
+
* plain or property-only lookalike.
|
|
58
|
+
*
|
|
35
59
|
* @param value - The value to inspect
|
|
36
60
|
* @returns True only for a `ProcessError` instance; false otherwise
|
|
37
61
|
*
|
|
@@ -42,7 +66,14 @@ var ProcessError = class extends Error {
|
|
|
42
66
|
* ```
|
|
43
67
|
*/
|
|
44
68
|
function isProcessError(value) {
|
|
45
|
-
|
|
69
|
+
if (!isError(value)) return false;
|
|
70
|
+
return holds(() => {
|
|
71
|
+
if (Object.getPrototypeOf(value) === Error.prototype) return false;
|
|
72
|
+
if (value.name !== "ProcessError" || !("code" in value)) return false;
|
|
73
|
+
if (Object.getOwnPropertyDescriptor(value, Symbol.for("@orkestrel/process.error"))?.value !== true) return false;
|
|
74
|
+
const code = value.code;
|
|
75
|
+
return PROCESS_ERROR_CODES.some((declared) => declared === code);
|
|
76
|
+
});
|
|
46
77
|
}
|
|
47
78
|
/**
|
|
48
79
|
* Creates the failure raised when a manager launch reuses a live id.
|
|
@@ -57,16 +88,55 @@ function createDuplicateError(id) {
|
|
|
57
88
|
});
|
|
58
89
|
}
|
|
59
90
|
/**
|
|
91
|
+
* Creates the failure raised when a launch is attempted on a registry that is being destroyed.
|
|
92
|
+
*
|
|
93
|
+
* @param id - The id the refused launch asked for
|
|
94
|
+
* @returns A typed protocol failure
|
|
95
|
+
*/
|
|
96
|
+
function createProtocolError(id) {
|
|
97
|
+
return new ProcessError(`Process '${id}' cannot launch: the registry is destroyed`, {
|
|
98
|
+
code: "protocol",
|
|
99
|
+
context: { id }
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Creates the failure raised when a public input is refused before anything is spawned.
|
|
104
|
+
*
|
|
105
|
+
* @param subject - The rejected input named as the caller wrote it, such as `option 'grace'`
|
|
106
|
+
* @param value - The rejected value, carried as `context.value`
|
|
107
|
+
* @returns A typed validation failure
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* ```ts
|
|
111
|
+
* createInvalidError("option 'grace'", -1).code // 'invalid'
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
function createInvalidError(subject, value) {
|
|
115
|
+
return new ProcessError(`Invalid ${subject}`, {
|
|
116
|
+
code: "invalid",
|
|
117
|
+
context: { value }
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
60
121
|
* Creates the failure raised when a run does not complete successfully and rejection is requested.
|
|
61
122
|
*
|
|
123
|
+
* @remarks
|
|
124
|
+
* The category is `timeout` only when the run's own timeout elapsed; every other failure,
|
|
125
|
+
* including an abort and an output overflow, is a `spawn` failure carrying its
|
|
126
|
+
* {@link ExecuteResult}.
|
|
127
|
+
*
|
|
62
128
|
* @param result - The buffered run outcome that failed
|
|
63
|
-
* @param cause - The underlying
|
|
64
|
-
* @returns A typed run failure carrying its {@link
|
|
129
|
+
* @param cause - The underlying host fault, when one ended the run
|
|
130
|
+
* @returns A typed run failure carrying its {@link ExecuteResult}
|
|
65
131
|
*/
|
|
66
|
-
function
|
|
67
|
-
|
|
132
|
+
function createExecuteError(result, cause) {
|
|
133
|
+
let reason = `exited with code ${String(result.code)}`;
|
|
134
|
+
if (result.signal !== null) reason = `was killed by ${result.signal}`;
|
|
135
|
+
if (result.truncated) reason = "exceeded its output limit";
|
|
136
|
+
if (result.aborted) reason = "was aborted";
|
|
137
|
+
if (result.expired) reason = "timed out";
|
|
68
138
|
return new ProcessError(`Command '${result.command}' ${reason}`, {
|
|
69
|
-
code: result.
|
|
139
|
+
code: result.expired ? "timeout" : "spawn",
|
|
70
140
|
context: {
|
|
71
141
|
command: result.command,
|
|
72
142
|
code: result.code,
|
|
@@ -77,6 +147,6 @@ function createRunError(result, cause) {
|
|
|
77
147
|
});
|
|
78
148
|
}
|
|
79
149
|
//#endregion
|
|
80
|
-
export { PROCESS_EVIDENCE, PROCESS_GRACE, PROCESS_OUTPUT, ProcessError, createDuplicateError,
|
|
150
|
+
export { PROCESS_BACKLOG, PROCESS_CONFIRMATION, PROCESS_ERROR_CODES, PROCESS_EVIDENCE, PROCESS_GRACE, PROCESS_OUTPUT, PROCESS_PATHEXT, PROCESS_TIMER, ProcessError, createDuplicateError, createExecuteError, createInvalidError, createProtocolError, isProcessError };
|
|
81
151
|
|
|
82
152
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../../src/core/constants.ts","../../../src/core/errors.ts"],"sourcesContent":["/** The default cooperative window in milliseconds between `SIGTERM` and `SIGKILL` during termination. */\nexport const PROCESS_GRACE = 5_000\n\n/** The default maximum retained stderr tail in bytes for a supervised {@link ProcessInterface}. */\nexport const PROCESS_EVIDENCE = 2_048\n\n/** The default maximum captured bytes for a one-shot run's stdout and stderr, each. */\nexport const PROCESS_OUTPUT = 10_485_760\n
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../src/core/constants.ts","../../../src/core/errors.ts"],"sourcesContent":["/** The default cooperative POSIX window in milliseconds between `SIGTERM` and `SIGKILL` during termination. */\nexport const PROCESS_GRACE = 5_000\n\n/** The window in milliseconds a termination waits for the child's native exit after the final kill. */\nexport const PROCESS_CONFIRMATION = 5_000\n\n/** The default maximum retained stderr tail in bytes for a supervised {@link ProcessInterface}. */\nexport const PROCESS_EVIDENCE = 2_048\n\n/** The default soft high-water mark in bytes for a supervised {@link ProcessInterface} line backlog. */\nexport const PROCESS_BACKLOG = 10_485_760\n\n/** The default maximum captured bytes for a one-shot run's stdout and stderr, each. */\nexport const PROCESS_OUTPUT = 10_485_760\n\n/** The largest timer delay in milliseconds the host schedules without truncating it to one. */\nexport const PROCESS_TIMER = 2_147_483_647\n\n/** The executable extensions a Windows lookup applies when the environment declares no `PATHEXT`. */\nexport const PROCESS_PATHEXT = '.COM;.EXE;.BAT;.CMD'\n\n/** The machine-readable failure categories a {@link ProcessError} carries, in declaration order. */\nexport const PROCESS_ERROR_CODES = Object.freeze([\n\t'spawn',\n\t'timeout',\n\t'duplicate',\n\t'protocol',\n\t'invalid',\n] as const)\n","import type {\n\tProcessErrorCode,\n\tProcessErrorContext,\n\tProcessErrorOptions,\n\tExecuteResult,\n} from './types.js'\nimport { holds, isError } from '@orkestrel/contract'\nimport { PROCESS_ERROR_CODES } from './constants.js'\n\n/** A child-process failure with a stable machine-readable category. */\nexport class ProcessError extends Error {\n\toverride readonly name = 'ProcessError'\n\treadonly code: ProcessErrorCode\n\treadonly context?: ProcessErrorContext\n\t/** The buffered run outcome, present when a one-shot run produced the failure. */\n\treadonly result?: ExecuteResult\n\n\t/**\n\t * Create a process error.\n\t *\n\t * @param message - Human-readable failure description\n\t * @param options - Machine-readable category, optional context, optional cause, and optional run result\n\t */\n\tconstructor(message: string, options: ProcessErrorOptions) {\n\t\tconst cause = options.cause\n\t\tsuper(message, cause === undefined ? undefined : { cause })\n\t\tObject.defineProperty(this, Symbol.for('@orkestrel/process.error'), { value: true })\n\t\tthis.code = options.code\n\t\tif (options.context !== undefined) this.context = options.context\n\t\tif (options.result !== undefined) this.result = options.result\n\t}\n}\n\n/**\n * Checks whether an unknown value is a {@link ProcessError}.\n *\n * @remarks\n * Recognition combines a global own-property brand with the native `Error` base, the subclass\n * prototype, the fixed name, and a code {@link PROCESS_ERROR_CODES} declares. The brand is\n * recognized across duplicate installations and ESM/CommonJS module copies at 0.0.4 or later. A copy\n * earlier than 0.0.4 stamps no brand, so an error it throws stays outside the type, and so does a\n * plain or property-only lookalike.\n *\n * @param value - The value to inspect\n * @returns True only for a `ProcessError` instance; false otherwise\n *\n * @example\n * ```ts\n * isProcessError(new ProcessError('spawn failed', { code: 'spawn' })) // true\n * isProcessError(new Error('spawn failed')) // false\n * ```\n */\nexport function isProcessError(value: unknown): value is ProcessError {\n\tif (!isError(value)) return false\n\treturn holds(() => {\n\t\tif (Object.getPrototypeOf(value) === Error.prototype) return false\n\t\tif (value.name !== 'ProcessError' || !('code' in value)) return false\n\t\tconst descriptor = Object.getOwnPropertyDescriptor(\n\t\t\tvalue,\n\t\t\tSymbol.for('@orkestrel/process.error'),\n\t\t)\n\t\tif (descriptor?.value !== true) return false\n\t\tconst code: unknown = value.code\n\t\treturn PROCESS_ERROR_CODES.some((declared) => declared === code)\n\t})\n}\n\n/**\n * Creates the failure raised when a manager launch reuses a live id.\n *\n * @param id - The id already occupied by a live child\n * @returns A typed duplicate-id failure\n */\nexport function createDuplicateError(id: string): ProcessError {\n\treturn new ProcessError(`Process '${id}' is already live`, {\n\t\tcode: 'duplicate',\n\t\tcontext: { id },\n\t})\n}\n\n/**\n * Creates the failure raised when a launch is attempted on a registry that is being destroyed.\n *\n * @param id - The id the refused launch asked for\n * @returns A typed protocol failure\n */\nexport function createProtocolError(id: string): ProcessError {\n\treturn new ProcessError(`Process '${id}' cannot launch: the registry is destroyed`, {\n\t\tcode: 'protocol',\n\t\tcontext: { id },\n\t})\n}\n\n/**\n * Creates the failure raised when a public input is refused before anything is spawned.\n *\n * @param subject - The rejected input named as the caller wrote it, such as `option 'grace'`\n * @param value - The rejected value, carried as `context.value`\n * @returns A typed validation failure\n *\n * @example\n * ```ts\n * createInvalidError(\"option 'grace'\", -1).code // 'invalid'\n * ```\n */\nexport function createInvalidError(subject: string, value: unknown): ProcessError {\n\treturn new ProcessError(`Invalid ${subject}`, { code: 'invalid', context: { value } })\n}\n\n/**\n * Creates the failure raised when a run does not complete successfully and rejection is requested.\n *\n * @remarks\n * The category is `timeout` only when the run's own timeout elapsed; every other failure,\n * including an abort and an output overflow, is a `spawn` failure carrying its\n * {@link ExecuteResult}.\n *\n * @param result - The buffered run outcome that failed\n * @param cause - The underlying host fault, when one ended the run\n * @returns A typed run failure carrying its {@link ExecuteResult}\n */\nexport function createExecuteError(result: ExecuteResult, cause?: unknown): ProcessError {\n\tlet reason = `exited with code ${String(result.code)}`\n\tif (result.signal !== null) reason = `was killed by ${result.signal}`\n\tif (result.truncated) reason = 'exceeded its output limit'\n\tif (result.aborted) reason = 'was aborted'\n\tif (result.expired) reason = 'timed out'\n\treturn new ProcessError(`Command '${result.command}' ${reason}`, {\n\t\tcode: result.expired ? 'timeout' : 'spawn',\n\t\tcontext: { command: result.command, code: result.code, signal: result.signal },\n\t\t...(cause === undefined ? {} : { cause }),\n\t\tresult,\n\t})\n}\n"],"mappings":";;;AACA,IAAa,gBAAgB;;AAG7B,IAAa,uBAAuB;;AAGpC,IAAa,mBAAmB;;AAGhC,IAAa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