@unieai/rabi-win32-process 0.0.0-stage → 0.2.1-rc.3
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/LICENSE +21 -0
- package/README.md +7 -2
- package/lib/index.js +761 -0
- package/lib/types/abi.d.ts +52 -0
- package/lib/types/control-stdio.d.ts +22 -0
- package/lib/types/errors.d.ts +9 -0
- package/lib/types/ffi.d.ts +160 -0
- package/lib/types/index.d.ts +8 -0
- package/lib/types/koffi.d.ts +49 -0
- package/lib/types/process.d.ts +143 -0
- package/package.json +30 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 DeepSeek
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @unieai/rabi-win32-process
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Shared low-level Win32 process, stdio, and Job Object primitives
|
|
4
|
+
|
|
5
|
+
Part of Rabi, the agent harness UnieAI Code runs on. Installed as a dependency of `@unieai/rabi`;
|
|
6
|
+
not meant to be used on its own.
|
|
7
|
+
|
|
8
|
+
See LICENSE for the license and copyright of this package.
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,761 @@
|
|
|
1
|
+
import { createLazyRequire } from "@unieai/rabi-lazy-require";
|
|
2
|
+
/** Win32 code reporting a caller-provided buffer is too small. */
|
|
3
|
+
const ERROR_INSUFFICIENT_BUFFER = 122;
|
|
4
|
+
/** Job limit that terminates every member when the final Job handle closes. */
|
|
5
|
+
const JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 8192;
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region lib/types/errors.js
|
|
8
|
+
/** Win32 call failure with the exact API name and error code. */
|
|
9
|
+
var Win32Error = class extends Error {
|
|
10
|
+
/** Win32 function whose checked result failed. */
|
|
11
|
+
api;
|
|
12
|
+
/** Exact GetLastError value or direct Win32 API error code. */
|
|
13
|
+
win32Code;
|
|
14
|
+
constructor(api, win32Code, detail) {
|
|
15
|
+
super(`${api} failed (Win32 ${win32Code})${detail === void 0 ? "" : `: ${detail}`}`);
|
|
16
|
+
this.name = "Win32Error";
|
|
17
|
+
this.api = api;
|
|
18
|
+
this.win32Code = win32Code;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region lib/types/koffi.js
|
|
23
|
+
/** Process-realm lazy access to Koffi's CommonJS entry. */
|
|
24
|
+
/** Load Koffi on the first Win32 native operation. */
|
|
25
|
+
const requireKoffi = createLazyRequire("koffi", import.meta.url);
|
|
26
|
+
//#endregion
|
|
27
|
+
//#region lib/types/ffi.js
|
|
28
|
+
/** Lazy Koffi bindings for generic Win32 process, stdio, and Job operations. */
|
|
29
|
+
/**
|
|
30
|
+
* Return whether a Koffi pointer represents NULL.
|
|
31
|
+
* @param value - pointer value returned by Koffi or a Win32 call.
|
|
32
|
+
* @returns true for null, undefined, or address zero.
|
|
33
|
+
*/
|
|
34
|
+
function isNullPtr(value) {
|
|
35
|
+
return value === null || value === void 0 || value === 0n;
|
|
36
|
+
}
|
|
37
|
+
let cachedTypes;
|
|
38
|
+
/** Resolve Koffi pointer and process layouts on the first native operation. */
|
|
39
|
+
function win32Types() {
|
|
40
|
+
if (cachedTypes !== void 0) return cachedTypes;
|
|
41
|
+
const koffi = requireKoffi();
|
|
42
|
+
const PVOID = koffi.pointer("void");
|
|
43
|
+
const PPVOID = koffi.pointer(PVOID);
|
|
44
|
+
const STARTUPINFOW = koffi.struct("RABI_STARTUPINFOW", {
|
|
45
|
+
cb: "uint32",
|
|
46
|
+
lpReserved: "str16",
|
|
47
|
+
lpDesktop: "str16",
|
|
48
|
+
lpTitle: "str16",
|
|
49
|
+
dwX: "uint32",
|
|
50
|
+
dwY: "uint32",
|
|
51
|
+
dwXSize: "uint32",
|
|
52
|
+
dwYSize: "uint32",
|
|
53
|
+
dwXCountChars: "uint32",
|
|
54
|
+
dwYCountChars: "uint32",
|
|
55
|
+
dwFillAttribute: "uint32",
|
|
56
|
+
dwFlags: "uint32",
|
|
57
|
+
wShowWindow: "uint16",
|
|
58
|
+
cbReserved2: "uint16",
|
|
59
|
+
lpReserved2: koffi.pointer("uint8"),
|
|
60
|
+
hStdInput: PVOID,
|
|
61
|
+
hStdOutput: PVOID,
|
|
62
|
+
hStdError: PVOID
|
|
63
|
+
});
|
|
64
|
+
const PROCESS_INFORMATION = koffi.struct("RABI_PROCESS_INFORMATION", {
|
|
65
|
+
hProcess: PVOID,
|
|
66
|
+
hThread: PVOID,
|
|
67
|
+
dwProcessId: "uint32",
|
|
68
|
+
dwThreadId: "uint32"
|
|
69
|
+
});
|
|
70
|
+
/* v8 ignore start -- ABI guards are pinned by native header probes. */
|
|
71
|
+
if (STARTUPINFOW.size !== 104) throw new Error(`STARTUPINFOW layout mismatch: koffi computed ${STARTUPINFOW.size}, expected 104`);
|
|
72
|
+
if (PROCESS_INFORMATION.size !== 24) throw new Error(`PROCESS_INFORMATION layout mismatch: koffi computed ${PROCESS_INFORMATION.size}, expected 24`);
|
|
73
|
+
/* v8 ignore stop */
|
|
74
|
+
return cachedTypes = {
|
|
75
|
+
PVOID,
|
|
76
|
+
PPVOID,
|
|
77
|
+
STARTUPINFOW,
|
|
78
|
+
PROCESS_INFORMATION
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Allocate a pointer-sized out-parameter slot.
|
|
83
|
+
* @returns allocated native slot.
|
|
84
|
+
*/
|
|
85
|
+
function allocPtrSlot() {
|
|
86
|
+
const { PVOID } = win32Types();
|
|
87
|
+
return requireKoffi().alloc(PVOID, 1);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Allocate a uint32 out-parameter slot.
|
|
91
|
+
* @returns allocated native slot.
|
|
92
|
+
*/
|
|
93
|
+
function allocUint32() {
|
|
94
|
+
return requireKoffi().alloc("uint32", 1);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Decode a pointer out-parameter.
|
|
98
|
+
* @param slot - pointer-sized slot filled by Win32.
|
|
99
|
+
* @returns decoded pointer, or null for address zero.
|
|
100
|
+
*/
|
|
101
|
+
function decodePtr(slot) {
|
|
102
|
+
const value = requireKoffi().decode(slot, win32Types().PVOID);
|
|
103
|
+
return isNullPtr(value) ? null : value;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Decode a uint32 out-parameter.
|
|
107
|
+
* @param slot - uint32 slot filled by Win32.
|
|
108
|
+
* @returns decoded unsigned value.
|
|
109
|
+
*/
|
|
110
|
+
function decodeUint32(slot) {
|
|
111
|
+
return requireKoffi().decode(slot, "uint32");
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Allocate a zeroed STARTUPINFOW.
|
|
115
|
+
* @returns allocated struct pointer.
|
|
116
|
+
*/
|
|
117
|
+
function allocStartupInfo() {
|
|
118
|
+
return requireKoffi().alloc(win32Types().STARTUPINFOW, 1);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Encode the stdio-bearing STARTUPINFOW fields.
|
|
122
|
+
* @param startupInfo - allocated STARTUPINFOW pointer.
|
|
123
|
+
* @param fields - fields required for inherited stdio.
|
|
124
|
+
*/
|
|
125
|
+
function encodeStartupInfo(startupInfo, fields) {
|
|
126
|
+
requireKoffi().encode(startupInfo, win32Types().STARTUPINFOW, fields);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Allocate a zeroed PROCESS_INFORMATION.
|
|
130
|
+
* @returns allocated struct pointer.
|
|
131
|
+
*/
|
|
132
|
+
function allocProcessInfo() {
|
|
133
|
+
return requireKoffi().alloc(win32Types().PROCESS_INFORMATION, 1);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Decode PROCESS_INFORMATION.
|
|
137
|
+
* @param processInfo - struct pointer filled by CreateProcess.
|
|
138
|
+
* @returns process/thread handles and ids.
|
|
139
|
+
*/
|
|
140
|
+
function decodeProcessInfo(processInfo) {
|
|
141
|
+
return requireKoffi().decode(processInfo, win32Types().PROCESS_INFORMATION);
|
|
142
|
+
}
|
|
143
|
+
let cachedContext;
|
|
144
|
+
let cached;
|
|
145
|
+
/* v8 ignore start -- exercised by native Windows ABI and sandbox jobs. */
|
|
146
|
+
function bindingContext() {
|
|
147
|
+
if (cachedContext !== void 0) return cachedContext;
|
|
148
|
+
const koffi = requireKoffi();
|
|
149
|
+
const kernel32 = koffi.load("kernel32.dll");
|
|
150
|
+
const advapi32 = koffi.load("advapi32.dll");
|
|
151
|
+
const bind = (lib, name, result, args) => lib.func("__stdcall", name, result, args);
|
|
152
|
+
cachedContext = {
|
|
153
|
+
kernel32,
|
|
154
|
+
advapi32,
|
|
155
|
+
bind
|
|
156
|
+
};
|
|
157
|
+
return cachedContext;
|
|
158
|
+
}
|
|
159
|
+
function bindings() {
|
|
160
|
+
if (cached !== void 0) return cached;
|
|
161
|
+
const koffi = requireKoffi();
|
|
162
|
+
const { PVOID, PPVOID, STARTUPINFOW, PROCESS_INFORMATION } = win32Types();
|
|
163
|
+
const { kernel32, advapi32, bind } = bindingContext();
|
|
164
|
+
const node = koffi.load(null);
|
|
165
|
+
cached = {
|
|
166
|
+
closeHandle: bind(kernel32, "CloseHandle", "int", [PVOID]),
|
|
167
|
+
getLastError: bind(kernel32, "GetLastError", "uint32", []),
|
|
168
|
+
getFileType: bind(kernel32, "GetFileType", "uint32", [PVOID]),
|
|
169
|
+
formatMessageW: bind(kernel32, "FormatMessageW", "uint32", [
|
|
170
|
+
"uint32",
|
|
171
|
+
PVOID,
|
|
172
|
+
"uint32",
|
|
173
|
+
"uint32",
|
|
174
|
+
PVOID,
|
|
175
|
+
"uint32",
|
|
176
|
+
PVOID
|
|
177
|
+
]),
|
|
178
|
+
createPipe: bind(kernel32, "CreatePipe", "int", [
|
|
179
|
+
PPVOID,
|
|
180
|
+
PPVOID,
|
|
181
|
+
PVOID,
|
|
182
|
+
"uint32"
|
|
183
|
+
]),
|
|
184
|
+
setHandleInformation: bind(kernel32, "SetHandleInformation", "int", [
|
|
185
|
+
PVOID,
|
|
186
|
+
"uint32",
|
|
187
|
+
"uint32"
|
|
188
|
+
]),
|
|
189
|
+
createProcessAsUserW: bind(advapi32, "CreateProcessAsUserW", "int", [
|
|
190
|
+
PVOID,
|
|
191
|
+
"str16",
|
|
192
|
+
"str16",
|
|
193
|
+
PVOID,
|
|
194
|
+
PVOID,
|
|
195
|
+
"int",
|
|
196
|
+
"uint32",
|
|
197
|
+
PVOID,
|
|
198
|
+
"str16",
|
|
199
|
+
koffi.pointer(STARTUPINFOW),
|
|
200
|
+
koffi.pointer(PROCESS_INFORMATION)
|
|
201
|
+
]),
|
|
202
|
+
createProcessW: bind(kernel32, "CreateProcessW", "int", [
|
|
203
|
+
"str16",
|
|
204
|
+
"str16",
|
|
205
|
+
PVOID,
|
|
206
|
+
PVOID,
|
|
207
|
+
"int",
|
|
208
|
+
"uint32",
|
|
209
|
+
PVOID,
|
|
210
|
+
"str16",
|
|
211
|
+
koffi.pointer(STARTUPINFOW),
|
|
212
|
+
koffi.pointer(PROCESS_INFORMATION)
|
|
213
|
+
]),
|
|
214
|
+
readFile: bind(kernel32, "ReadFile", "int", [
|
|
215
|
+
PVOID,
|
|
216
|
+
PVOID,
|
|
217
|
+
"uint32",
|
|
218
|
+
koffi.pointer("uint32"),
|
|
219
|
+
PVOID
|
|
220
|
+
]),
|
|
221
|
+
peekNamedPipe: bind(kernel32, "PeekNamedPipe", "int", [
|
|
222
|
+
PVOID,
|
|
223
|
+
PVOID,
|
|
224
|
+
"uint32",
|
|
225
|
+
koffi.pointer("uint32"),
|
|
226
|
+
koffi.pointer("uint32"),
|
|
227
|
+
koffi.pointer("uint32")
|
|
228
|
+
]),
|
|
229
|
+
waitForSingleObject: bind(kernel32, "WaitForSingleObject", "uint32", [PVOID, "uint32"]),
|
|
230
|
+
getExitCodeProcess: bind(kernel32, "GetExitCodeProcess", "int", [PVOID, koffi.pointer("uint32")]),
|
|
231
|
+
createJobObjectW: bind(kernel32, "CreateJobObjectW", PVOID, [PVOID, "str16"]),
|
|
232
|
+
setInformationJobObject: bind(kernel32, "SetInformationJobObject", "int", [
|
|
233
|
+
PVOID,
|
|
234
|
+
"int",
|
|
235
|
+
PVOID,
|
|
236
|
+
"uint32"
|
|
237
|
+
]),
|
|
238
|
+
queryInformationJobObject: bind(kernel32, "QueryInformationJobObject", "int", [
|
|
239
|
+
PVOID,
|
|
240
|
+
"int",
|
|
241
|
+
PVOID,
|
|
242
|
+
"uint32",
|
|
243
|
+
PVOID
|
|
244
|
+
]),
|
|
245
|
+
assignProcessToJobObject: bind(kernel32, "AssignProcessToJobObject", "int", [PVOID, PVOID]),
|
|
246
|
+
resumeThread: bind(kernel32, "ResumeThread", "uint32", [PVOID]),
|
|
247
|
+
terminateProcess: bind(kernel32, "TerminateProcess", "int", [PVOID, "uint32"]),
|
|
248
|
+
terminateJobObject: bind(kernel32, "TerminateJobObject", "int", [PVOID, "uint32"]),
|
|
249
|
+
getStdHandle: bind(kernel32, "GetStdHandle", PVOID, ["int"]),
|
|
250
|
+
uvGetOsfhandle: node.func("uv_get_osfhandle", PVOID, ["int"])
|
|
251
|
+
};
|
|
252
|
+
return cached;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Extend the shared process table with caller-owned Win32 API families.
|
|
256
|
+
* @param create - binds only the caller-specific operations from the shared libraries.
|
|
257
|
+
* @returns generic process bindings combined with the caller-specific operations.
|
|
258
|
+
*/
|
|
259
|
+
function extendWin32ProcessBindings(create) {
|
|
260
|
+
return {
|
|
261
|
+
...bindings(),
|
|
262
|
+
...create(bindingContext())
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Load the generic process binding table without policy-specific extensions.
|
|
267
|
+
* @returns shared Win32 process, stdio, and Job operations.
|
|
268
|
+
*/
|
|
269
|
+
function loadWin32ProcessBindings() {
|
|
270
|
+
return bindings();
|
|
271
|
+
}
|
|
272
|
+
/* v8 ignore stop */
|
|
273
|
+
/**
|
|
274
|
+
* Format a Win32 error code through FormatMessageW.
|
|
275
|
+
* @param api - active binding table.
|
|
276
|
+
* @param win32Code - captured GetLastError value.
|
|
277
|
+
* @returns trimmed system message, or an empty string when unavailable.
|
|
278
|
+
*/
|
|
279
|
+
function errorText(api, win32Code) {
|
|
280
|
+
const buffer = Buffer.alloc(1024);
|
|
281
|
+
const length = api.formatMessageW(4608, null, win32Code, 0, buffer, buffer.length / 2, null);
|
|
282
|
+
return length === 0 ? "" : buffer.subarray(0, length * 2).toString("utf16le").trim();
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Throw the current GetLastError value.
|
|
286
|
+
* @param api - active binding table.
|
|
287
|
+
* @param name - failing Win32 operation.
|
|
288
|
+
* @param detail - optional operation context.
|
|
289
|
+
* @returns never; always throws Win32Error.
|
|
290
|
+
*/
|
|
291
|
+
function throwLastError(api, name, detail) {
|
|
292
|
+
const win32Code = api.getLastError();
|
|
293
|
+
throw new Win32Error(name, win32Code, detail ?? errorText(api, win32Code));
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Throw an explicitly captured Win32 error code.
|
|
297
|
+
* @param api - active binding table.
|
|
298
|
+
* @param name - failing Win32 operation.
|
|
299
|
+
* @param win32Code - error captured before cleanup.
|
|
300
|
+
* @param detail - optional operation context.
|
|
301
|
+
* @returns never; always throws Win32Error.
|
|
302
|
+
*/
|
|
303
|
+
function throwWin32(api, name, win32Code, detail) {
|
|
304
|
+
throw new Win32Error(name, win32Code, detail ?? errorText(api, win32Code));
|
|
305
|
+
}
|
|
306
|
+
//#endregion
|
|
307
|
+
//#region lib/types/control-stdio.js
|
|
308
|
+
/** Windows CRT startup descriptors for a Node payload with one inherited control pipe. */
|
|
309
|
+
const HANDLE_BYTES = 8;
|
|
310
|
+
const INVALID_HANDLE = 18446744073709551615n;
|
|
311
|
+
const FOPEN = 1;
|
|
312
|
+
const FPIPE = 8;
|
|
313
|
+
const FDEV = 64;
|
|
314
|
+
const FILE_TYPE_CHAR = 2;
|
|
315
|
+
const FILE_TYPE_PIPE = 3;
|
|
316
|
+
/**
|
|
317
|
+
* Encode the CRT's descriptor table before the child runtime allocates descriptors.
|
|
318
|
+
* Supported Windows targets use 64-bit handles. Empty slots remain closed, and the
|
|
319
|
+
* backing Buffer must remain alive until CreateProcess returns.
|
|
320
|
+
* @param api - native file-type inspection for inherited handles.
|
|
321
|
+
* @param stdio - standard handles and the provider-owned fd-7 control pipe.
|
|
322
|
+
* @returns descriptor count, flag bytes, and handle values for STARTUPINFO's reserved CRT fields.
|
|
323
|
+
*/
|
|
324
|
+
function inheritedControlStdio(api, stdio) {
|
|
325
|
+
const count = stdio.control.fileDescriptor + 1;
|
|
326
|
+
const handleOffset = 4 + count;
|
|
327
|
+
const bytes = Buffer.alloc(handleOffset + count * HANDLE_BYTES);
|
|
328
|
+
bytes.writeUInt32LE(count, 0);
|
|
329
|
+
for (let index = 0; index < count; index++) bytes.writeBigUInt64LE(INVALID_HANDLE, handleOffset + index * HANDLE_BYTES);
|
|
330
|
+
const entries = [
|
|
331
|
+
[0, stdio.stdin],
|
|
332
|
+
[1, stdio.stdout],
|
|
333
|
+
[2, stdio.stderr],
|
|
334
|
+
[stdio.control.fileDescriptor, stdio.control.handle]
|
|
335
|
+
];
|
|
336
|
+
for (const [fd, handle] of entries) {
|
|
337
|
+
const kind = api.getFileType(handle);
|
|
338
|
+
if (fd === stdio.control.fileDescriptor && kind !== FILE_TYPE_PIPE) throw new Error("subprocess control descriptor is not a Windows pipe");
|
|
339
|
+
bytes[4 + fd] = FOPEN | (kind === FILE_TYPE_PIPE ? FPIPE : kind === FILE_TYPE_CHAR ? FDEV : 0);
|
|
340
|
+
bytes.writeBigUInt64LE(handle, handleOffset + fd * HANDLE_BYTES);
|
|
341
|
+
}
|
|
342
|
+
return bytes;
|
|
343
|
+
}
|
|
344
|
+
//#endregion
|
|
345
|
+
//#region lib/types/process.js
|
|
346
|
+
/** Typed Win32 process operations over the shared binding table. */
|
|
347
|
+
/**
|
|
348
|
+
* Quote one argument according to CommandLineToArgvW parsing.
|
|
349
|
+
* @param argument - one argv entry.
|
|
350
|
+
* @returns bare or quoted command-line segment.
|
|
351
|
+
*/
|
|
352
|
+
function quoteArg(argument) {
|
|
353
|
+
if (argument === "") return "\"\"";
|
|
354
|
+
if (!/[\s"]/u.test(argument)) return argument;
|
|
355
|
+
let quoted = "\"";
|
|
356
|
+
for (let index = 0; index < argument.length; index++) {
|
|
357
|
+
let backslashes = 0;
|
|
358
|
+
while (index < argument.length && argument.charAt(index) === "\\") {
|
|
359
|
+
backslashes += 1;
|
|
360
|
+
index += 1;
|
|
361
|
+
}
|
|
362
|
+
if (index === argument.length) quoted += "\\".repeat(backslashes * 2);
|
|
363
|
+
else if (argument.charAt(index) === "\"") quoted += "\\".repeat(backslashes * 2 + 1) + "\"";
|
|
364
|
+
else quoted += "\\".repeat(backslashes) + argument.charAt(index);
|
|
365
|
+
}
|
|
366
|
+
return quoted + "\"";
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Build the mutable command line accepted by CreateProcessAsUserW.
|
|
370
|
+
* @param program - executable argv entry.
|
|
371
|
+
* @param args - remaining argv entries.
|
|
372
|
+
* @returns joined Win32 command line.
|
|
373
|
+
*/
|
|
374
|
+
function buildCommandLine(program, args) {
|
|
375
|
+
return [program, ...args].map(quoteArg).join(" ");
|
|
376
|
+
}
|
|
377
|
+
function compareWindowsEnvironmentKeys([left], [right]) {
|
|
378
|
+
const foldedLeft = left.toUpperCase();
|
|
379
|
+
const foldedRight = right.toUpperCase();
|
|
380
|
+
return foldedLeft < foldedRight ? -1 : foldedLeft > foldedRight ? 1 : 0;
|
|
381
|
+
}
|
|
382
|
+
function encodeWindowsEnvironment(env) {
|
|
383
|
+
const strings = Object.entries(env).sort(compareWindowsEnvironmentKeys).map(([key, value]) => `${key}=${value}`);
|
|
384
|
+
return Buffer.from(`${strings.join("\0")}\0\0`, "utf16le");
|
|
385
|
+
}
|
|
386
|
+
function freeNative(pointer) {
|
|
387
|
+
if (pointer !== void 0) requireKoffi().free(pointer);
|
|
388
|
+
}
|
|
389
|
+
function closeBestEffort(api, handle) {
|
|
390
|
+
if (!isNullPtr(handle)) api.closeHandle(handle);
|
|
391
|
+
}
|
|
392
|
+
function createPipe(api, owned) {
|
|
393
|
+
const readSlot = allocPtrSlot();
|
|
394
|
+
let writeSlot;
|
|
395
|
+
try {
|
|
396
|
+
writeSlot = allocPtrSlot();
|
|
397
|
+
if (api.createPipe(readSlot, writeSlot, null, 0) === 0) throwLastError(api, "CreatePipe");
|
|
398
|
+
const read = decodePtr(readSlot);
|
|
399
|
+
const write = decodePtr(writeSlot);
|
|
400
|
+
if (read === null || write === null) {
|
|
401
|
+
closeBestEffort(api, read);
|
|
402
|
+
closeBestEffort(api, write);
|
|
403
|
+
throwLastError(api, "CreatePipe", "null pipe handle");
|
|
404
|
+
}
|
|
405
|
+
owned.add(read);
|
|
406
|
+
owned.add(write);
|
|
407
|
+
return {
|
|
408
|
+
read,
|
|
409
|
+
write
|
|
410
|
+
};
|
|
411
|
+
} finally {
|
|
412
|
+
freeNative(writeSlot);
|
|
413
|
+
requireKoffi().free(readSlot);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
function closeOwned(api, owned, handle) {
|
|
417
|
+
/* v8 ignore next -- each successfully decoded pipe end is uniquely owned. */
|
|
418
|
+
if (!owned.delete(handle)) return;
|
|
419
|
+
api.closeHandle(handle);
|
|
420
|
+
}
|
|
421
|
+
function closeAllOwned(api, owned) {
|
|
422
|
+
for (const handle of owned) api.closeHandle(handle);
|
|
423
|
+
owned.clear();
|
|
424
|
+
}
|
|
425
|
+
function createRestrictedProcess(api, options, commandLine, creationFlags, startupInfo, processInfo) {
|
|
426
|
+
return api.createProcessAsUserW(options.token, null, commandLine, null, null, 1, creationFlags, null, options.cwd, startupInfo, processInfo);
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Spawn a process with anonymous-pipe stdout/stderr and immediate stdin EOF.
|
|
430
|
+
* New console windows start hidden without changing console inheritance.
|
|
431
|
+
* @param api - active binding table.
|
|
432
|
+
* @param options - command, cwd, args, and restricted primary token.
|
|
433
|
+
* @returns caller-owned process and pipe read handles.
|
|
434
|
+
*/
|
|
435
|
+
function spawnPipedProcess(api, options) {
|
|
436
|
+
const owned = /* @__PURE__ */ new Set();
|
|
437
|
+
let startupInfo;
|
|
438
|
+
let processInfo;
|
|
439
|
+
try {
|
|
440
|
+
const stdIn = createPipe(api, owned);
|
|
441
|
+
const stdOut = createPipe(api, owned);
|
|
442
|
+
const stdErr = createPipe(api, owned);
|
|
443
|
+
for (const [handle, label] of [
|
|
444
|
+
[stdIn.read, "stdin read end"],
|
|
445
|
+
[stdOut.write, "stdout write end"],
|
|
446
|
+
[stdErr.write, "stderr write end"]
|
|
447
|
+
]) if (api.setHandleInformation(handle, 1, 1) === 0) throwLastError(api, "SetHandleInformation", label);
|
|
448
|
+
startupInfo = allocStartupInfo();
|
|
449
|
+
encodeStartupInfo(startupInfo, {
|
|
450
|
+
cb: 104,
|
|
451
|
+
dwFlags: 257,
|
|
452
|
+
wShowWindow: 0,
|
|
453
|
+
hStdInput: stdIn.read,
|
|
454
|
+
hStdOutput: stdOut.write,
|
|
455
|
+
hStdError: stdErr.write
|
|
456
|
+
});
|
|
457
|
+
processInfo = allocProcessInfo();
|
|
458
|
+
if (createRestrictedProcess(api, options, buildCommandLine(options.command, options.args), 0, startupInfo, processInfo) === 0) throwWin32(api, "CreateProcessAsUserW", api.getLastError(), `command: ${options.command}, cwd: ${options.cwd}`);
|
|
459
|
+
const info = decodeProcessInfo(processInfo);
|
|
460
|
+
if (info.hProcess === null || info.hThread === null) {
|
|
461
|
+
if (info.hProcess !== null) api.terminateProcess(info.hProcess, 1);
|
|
462
|
+
closeBestEffort(api, info.hThread);
|
|
463
|
+
closeBestEffort(api, info.hProcess);
|
|
464
|
+
throw new Error(`CreateProcessAsUserW succeeded but returned null process/thread handles (pid ${info.dwProcessId})`);
|
|
465
|
+
}
|
|
466
|
+
closeOwned(api, owned, stdIn.read);
|
|
467
|
+
closeOwned(api, owned, stdIn.write);
|
|
468
|
+
closeOwned(api, owned, stdOut.write);
|
|
469
|
+
closeOwned(api, owned, stdErr.write);
|
|
470
|
+
closeBestEffort(api, info.hThread);
|
|
471
|
+
owned.delete(stdOut.read);
|
|
472
|
+
owned.delete(stdErr.read);
|
|
473
|
+
return {
|
|
474
|
+
pid: info.dwProcessId,
|
|
475
|
+
process: info.hProcess,
|
|
476
|
+
stdoutRead: stdOut.read,
|
|
477
|
+
stderrRead: stdErr.read
|
|
478
|
+
};
|
|
479
|
+
} catch (error) {
|
|
480
|
+
closeAllOwned(api, owned);
|
|
481
|
+
throw error;
|
|
482
|
+
} finally {
|
|
483
|
+
freeNative(processInfo);
|
|
484
|
+
freeNative(startupInfo);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Drain one anonymous pipe until the writer closes it.
|
|
489
|
+
* @param api - active binding table.
|
|
490
|
+
* @param handle - caller-owned pipe read end.
|
|
491
|
+
* @returns complete bytes read before EOF; the handle is always closed.
|
|
492
|
+
* @throws when a Win32 pipe operation fails.
|
|
493
|
+
*/
|
|
494
|
+
async function drainPipe(api, handle) {
|
|
495
|
+
const chunks = [];
|
|
496
|
+
let countSlot;
|
|
497
|
+
try {
|
|
498
|
+
countSlot = allocUint32();
|
|
499
|
+
for (;;) {
|
|
500
|
+
if (api.peekNamedPipe(handle, null, 0, null, countSlot, null) === 0) {
|
|
501
|
+
const win32Code = api.getLastError();
|
|
502
|
+
if (win32Code === 109 || win32Code === 232) break;
|
|
503
|
+
throwLastError(api, "PeekNamedPipe", `drain failure after ${chunks.length} chunk(s)`);
|
|
504
|
+
}
|
|
505
|
+
const available = decodeUint32(countSlot);
|
|
506
|
+
if (available > 0) {
|
|
507
|
+
const chunk = Buffer.alloc(available);
|
|
508
|
+
if (api.readFile(handle, chunk, chunk.length, countSlot, null) === 0) throwLastError(api, "ReadFile", `drain failure after ${chunks.length} chunk(s)`);
|
|
509
|
+
chunks.push(chunk.subarray(0, decodeUint32(countSlot)));
|
|
510
|
+
}
|
|
511
|
+
await new Promise((resolve) => setTimeout(resolve, 1));
|
|
512
|
+
}
|
|
513
|
+
return Buffer.concat(chunks);
|
|
514
|
+
} finally {
|
|
515
|
+
freeNative(countSlot);
|
|
516
|
+
api.closeHandle(handle);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Wait for a process and always close its handle.
|
|
521
|
+
* @param api - active binding table.
|
|
522
|
+
* @param process - caller-owned process handle.
|
|
523
|
+
* @returns direct process exit code.
|
|
524
|
+
*/
|
|
525
|
+
function waitForProcessExit(api, process) {
|
|
526
|
+
let exitCodeSlot;
|
|
527
|
+
try {
|
|
528
|
+
if (api.waitForSingleObject(process, 4294967295) === 4294967295) throwLastError(api, "WaitForSingleObject");
|
|
529
|
+
exitCodeSlot = allocUint32();
|
|
530
|
+
if (api.getExitCodeProcess(process, exitCodeSlot) === 0) throwLastError(api, "GetExitCodeProcess");
|
|
531
|
+
return decodeUint32(exitCodeSlot);
|
|
532
|
+
} finally {
|
|
533
|
+
freeNative(exitCodeSlot);
|
|
534
|
+
api.closeHandle(process);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
function createKillOnCloseJob(api) {
|
|
538
|
+
const job = api.createJobObjectW(null, null);
|
|
539
|
+
if (isNullPtr(job)) throwLastError(api, "CreateJobObjectW");
|
|
540
|
+
const information = Buffer.alloc(144);
|
|
541
|
+
information.writeUInt32LE(JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, 16);
|
|
542
|
+
if (api.setInformationJobObject(job, 9, information, information.length) === 0) {
|
|
543
|
+
const win32Code = api.getLastError();
|
|
544
|
+
api.closeHandle(job);
|
|
545
|
+
throwWin32(api, "SetInformationJobObject", win32Code);
|
|
546
|
+
}
|
|
547
|
+
return job;
|
|
548
|
+
}
|
|
549
|
+
const UV_INVALID_OS_FILE_HANDLE = 18446744073709551615n;
|
|
550
|
+
const UV_INVALID_FILE_DESCRIPTOR = 18446744073709551614n;
|
|
551
|
+
function inheritedStandardHandles(api, controlFileDescriptor) {
|
|
552
|
+
const get = (selector, label) => {
|
|
553
|
+
const handle = api.getStdHandle(selector);
|
|
554
|
+
if (!isNullPtr(handle)) return handle;
|
|
555
|
+
throwLastError(api, "GetStdHandle", `null ${label} handle`);
|
|
556
|
+
};
|
|
557
|
+
return {
|
|
558
|
+
stdin: get(-10, "stdin"),
|
|
559
|
+
stdout: get(-11, "stdout"),
|
|
560
|
+
stderr: get(-12, "stderr"),
|
|
561
|
+
...controlFileDescriptor === void 0 ? {} : { control: {
|
|
562
|
+
fileDescriptor: controlFileDescriptor,
|
|
563
|
+
handle: descriptorHandle(api, controlFileDescriptor, "control")
|
|
564
|
+
} }
|
|
565
|
+
};
|
|
566
|
+
}
|
|
567
|
+
function descriptorHandle(api, fileDescriptor, label) {
|
|
568
|
+
const handle = api.uvGetOsfhandle(fileDescriptor);
|
|
569
|
+
if (isNullPtr(handle) || handle === UV_INVALID_OS_FILE_HANDLE || handle === UV_INVALID_FILE_DESCRIPTOR) throw new Error(`uv_get_osfhandle returned an invalid handle for target ${label} fd ${String(fileDescriptor)}`);
|
|
570
|
+
return handle;
|
|
571
|
+
}
|
|
572
|
+
function targetCarrierHandles(api, descriptors) {
|
|
573
|
+
return {
|
|
574
|
+
stdin: descriptorHandle(api, descriptors.stdin, "stdin"),
|
|
575
|
+
stdout: descriptorHandle(api, descriptors.stdout, "stdout"),
|
|
576
|
+
stderr: descriptorHandle(api, descriptors.stderr, "stderr"),
|
|
577
|
+
...descriptors.control === void 0 ? {} : { control: {
|
|
578
|
+
fileDescriptor: descriptors.control,
|
|
579
|
+
handle: descriptorHandle(api, descriptors.control, "control")
|
|
580
|
+
} }
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
/** Shared suspended-create, Job-assignment, and resume lifecycle. */
|
|
584
|
+
function spawnJobProcess(api, options, resolveStdio, createName, create) {
|
|
585
|
+
const job = createKillOnCloseJob(api);
|
|
586
|
+
const enabled = [];
|
|
587
|
+
let startupInfo;
|
|
588
|
+
let processInfo;
|
|
589
|
+
let controlDescriptorBlock;
|
|
590
|
+
let created = 0;
|
|
591
|
+
let createFailureCode = 0;
|
|
592
|
+
try {
|
|
593
|
+
const stdio = resolveStdio();
|
|
594
|
+
const inherited = [
|
|
595
|
+
[stdio.stdin, "stdin"],
|
|
596
|
+
[stdio.stdout, "stdout"],
|
|
597
|
+
[stdio.stderr, "stderr"]
|
|
598
|
+
];
|
|
599
|
+
if (stdio.control !== void 0) inherited.push([stdio.control.handle, "control"]);
|
|
600
|
+
for (const [handle, label] of inherited) {
|
|
601
|
+
if (api.setHandleInformation(handle, 1, 1) === 0) throwLastError(api, "SetHandleInformation", `${label} (enable inherit)`);
|
|
602
|
+
enabled.push(handle);
|
|
603
|
+
}
|
|
604
|
+
const controlBytes = stdio.control === void 0 ? void 0 : inheritedControlStdio(api, {
|
|
605
|
+
...stdio,
|
|
606
|
+
control: stdio.control
|
|
607
|
+
});
|
|
608
|
+
if (controlBytes !== void 0) {
|
|
609
|
+
const koffi = requireKoffi();
|
|
610
|
+
controlDescriptorBlock = {
|
|
611
|
+
pointer: koffi.alloc("uint8", controlBytes.length),
|
|
612
|
+
length: controlBytes.length
|
|
613
|
+
};
|
|
614
|
+
koffi.encode(controlDescriptorBlock.pointer, "uint8", controlBytes, controlBytes.length);
|
|
615
|
+
}
|
|
616
|
+
startupInfo = allocStartupInfo();
|
|
617
|
+
encodeStartupInfo(startupInfo, {
|
|
618
|
+
cb: 104,
|
|
619
|
+
dwFlags: 257,
|
|
620
|
+
wShowWindow: 0,
|
|
621
|
+
hStdInput: stdio.stdin,
|
|
622
|
+
hStdOutput: stdio.stdout,
|
|
623
|
+
hStdError: stdio.stderr,
|
|
624
|
+
...controlDescriptorBlock === void 0 ? {} : {
|
|
625
|
+
cbReserved2: controlDescriptorBlock.length,
|
|
626
|
+
lpReserved2: controlDescriptorBlock.pointer
|
|
627
|
+
}
|
|
628
|
+
});
|
|
629
|
+
processInfo = allocProcessInfo();
|
|
630
|
+
created = create(startupInfo, processInfo);
|
|
631
|
+
if (created === 0) createFailureCode = api.getLastError();
|
|
632
|
+
} catch (error) {
|
|
633
|
+
freeNative(processInfo);
|
|
634
|
+
api.closeHandle(job);
|
|
635
|
+
throw error;
|
|
636
|
+
} finally {
|
|
637
|
+
freeNative(startupInfo);
|
|
638
|
+
freeNative(controlDescriptorBlock?.pointer);
|
|
639
|
+
for (const handle of enabled) api.setHandleInformation(handle, 1, 0);
|
|
640
|
+
}
|
|
641
|
+
if (created === 0) {
|
|
642
|
+
freeNative(processInfo);
|
|
643
|
+
api.closeHandle(job);
|
|
644
|
+
throwWin32(api, createName, createFailureCode, `command: ${options.command}, cwd: ${options.cwd}`);
|
|
645
|
+
}
|
|
646
|
+
let info;
|
|
647
|
+
try {
|
|
648
|
+
info = decodeProcessInfo(processInfo);
|
|
649
|
+
} finally {
|
|
650
|
+
freeNative(processInfo);
|
|
651
|
+
}
|
|
652
|
+
if (info.hProcess === null || info.hThread === null) {
|
|
653
|
+
if (info.hProcess !== null) api.terminateProcess(info.hProcess, 1);
|
|
654
|
+
api.closeHandle(job);
|
|
655
|
+
closeBestEffort(api, info.hThread);
|
|
656
|
+
closeBestEffort(api, info.hProcess);
|
|
657
|
+
throw new Error(`${createName} succeeded but returned null process/thread handles (pid ${info.dwProcessId})`);
|
|
658
|
+
}
|
|
659
|
+
if (api.assignProcessToJobObject(job, info.hProcess) === 0) {
|
|
660
|
+
const win32Code = api.getLastError();
|
|
661
|
+
api.terminateProcess(info.hProcess, 1);
|
|
662
|
+
closeBestEffort(api, info.hThread);
|
|
663
|
+
closeBestEffort(api, info.hProcess);
|
|
664
|
+
api.closeHandle(job);
|
|
665
|
+
throwWin32(api, "AssignProcessToJobObject", win32Code, `pid ${info.dwProcessId}`);
|
|
666
|
+
}
|
|
667
|
+
if (api.resumeThread(info.hThread) === 4294967295) {
|
|
668
|
+
const win32Code = api.getLastError();
|
|
669
|
+
closeBestEffort(api, info.hThread);
|
|
670
|
+
closeBestEffort(api, info.hProcess);
|
|
671
|
+
api.closeHandle(job);
|
|
672
|
+
throwWin32(api, "ResumeThread", win32Code, `pid ${info.dwProcessId}`);
|
|
673
|
+
}
|
|
674
|
+
closeBestEffort(api, info.hThread);
|
|
675
|
+
return {
|
|
676
|
+
pid: info.dwProcessId,
|
|
677
|
+
process: info.hProcess,
|
|
678
|
+
job
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* Spawn a restricted-token process suspended with hidden initial windows, assign its Job, then resume it.
|
|
683
|
+
* @param api - active binding table.
|
|
684
|
+
* @param options - command, cwd, args, and restricted primary token.
|
|
685
|
+
* @returns caller-owned process and Job handles after successful resume.
|
|
686
|
+
* @remarks Node clears stdio handle inheritability at startup through
|
|
687
|
+
* uv_disable_stdio_inheritance. This operation temporarily restores the bits
|
|
688
|
+
* required by STARTF_USESTDHANDLES. Restoring them afterward is best-effort:
|
|
689
|
+
* failure must not replace the already-created child's outcome.
|
|
690
|
+
*/
|
|
691
|
+
function spawnInheritedJobProcess(api, options) {
|
|
692
|
+
const commandLine = buildCommandLine(options.command, options.args);
|
|
693
|
+
return spawnJobProcess(api, options, () => inheritedStandardHandles(api, options.controlFileDescriptor), "CreateProcessAsUserW", (startupInfo, processInfo) => createRestrictedProcess(api, options, commandLine, 4, startupInfo, processInfo));
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* Spawn an ordinary process suspended with hidden initial windows, assign its Job, then resume it.
|
|
697
|
+
* @param api - active binding table.
|
|
698
|
+
* @param options - command, cwd, argv, and target carrier descriptors.
|
|
699
|
+
* @returns caller-owned process and Job handles after successful resume.
|
|
700
|
+
*/
|
|
701
|
+
function spawnCurrentTokenJobProcess(api, options) {
|
|
702
|
+
const commandLine = buildCommandLine(options.command, options.args);
|
|
703
|
+
const environment = encodeWindowsEnvironment(options.env);
|
|
704
|
+
return spawnJobProcess(api, options, () => targetCarrierHandles(api, options.stdio), "CreateProcessW", (startupInfo, processInfo) => api.createProcessW(options.applicationName, commandLine, null, null, 1, 1028, environment, options.cwd, startupInfo, processInfo));
|
|
705
|
+
}
|
|
706
|
+
/**
|
|
707
|
+
* Verify that an unnamed kill-on-close Job can be created and released now.
|
|
708
|
+
* @param api - active binding table.
|
|
709
|
+
*/
|
|
710
|
+
function probeCurrentTokenJobSupport(api) {
|
|
711
|
+
closeHandleChecked(api, createKillOnCloseJob(api), "current-token Job capability probe");
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Poll one process handle without blocking the runner event loop.
|
|
715
|
+
* @param api - active binding table.
|
|
716
|
+
* @param process - caller-owned process handle.
|
|
717
|
+
* @returns the direct exit code when signalled, or undefined while running.
|
|
718
|
+
*/
|
|
719
|
+
function pollProcessExit(api, process) {
|
|
720
|
+
const waitResult = api.waitForSingleObject(process, 0);
|
|
721
|
+
if (waitResult === 258) return void 0;
|
|
722
|
+
if (waitResult === 4294967295) throwLastError(api, "WaitForSingleObject");
|
|
723
|
+
const exitCodeSlot = allocUint32();
|
|
724
|
+
try {
|
|
725
|
+
if (api.getExitCodeProcess(process, exitCodeSlot) === 0) throwLastError(api, "GetExitCodeProcess");
|
|
726
|
+
return decodeUint32(exitCodeSlot);
|
|
727
|
+
} finally {
|
|
728
|
+
requireKoffi().free(exitCodeSlot);
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* Return whether a Job has no active processes.
|
|
733
|
+
* @param api - active binding table.
|
|
734
|
+
* @param job - caller-owned Job handle.
|
|
735
|
+
* @returns true once the Job reports zero active processes.
|
|
736
|
+
*/
|
|
737
|
+
function isJobEmpty(api, job) {
|
|
738
|
+
const information = Buffer.alloc(48);
|
|
739
|
+
if (api.queryInformationJobObject(job, 1, information, information.length, null) === 0) throwLastError(api, "QueryInformationJobObject", "active process count");
|
|
740
|
+
return information.readUInt32LE(40) === 0;
|
|
741
|
+
}
|
|
742
|
+
/**
|
|
743
|
+
* Terminate every process in a Job.
|
|
744
|
+
* @param api - active binding table.
|
|
745
|
+
* @param job - caller-owned Job handle.
|
|
746
|
+
* @param exitCode - direct Windows exit code assigned to members.
|
|
747
|
+
*/
|
|
748
|
+
function terminateJob(api, job, exitCode) {
|
|
749
|
+
if (api.terminateJobObject(job, exitCode) === 0) throwLastError(api, "TerminateJobObject");
|
|
750
|
+
}
|
|
751
|
+
/**
|
|
752
|
+
* Close a caller-owned handle and report a labelled Win32 failure.
|
|
753
|
+
* @param api - active binding table.
|
|
754
|
+
* @param handle - handle to close.
|
|
755
|
+
* @param detail - lifecycle label for diagnostics.
|
|
756
|
+
*/
|
|
757
|
+
function closeHandleChecked(api, handle, detail) {
|
|
758
|
+
if (api.closeHandle(handle) === 0) throwLastError(api, "CloseHandle", detail);
|
|
759
|
+
}
|
|
760
|
+
//#endregion
|
|
761
|
+
export { ERROR_INSUFFICIENT_BUFFER, Win32Error, allocPtrSlot, allocUint32, closeHandleChecked, decodePtr, decodeUint32, drainPipe, extendWin32ProcessBindings, isJobEmpty, isNullPtr, loadWin32ProcessBindings, pollProcessExit, probeCurrentTokenJobSupport, spawnCurrentTokenJobProcess, spawnInheritedJobProcess, spawnPipedProcess, terminateJob, throwLastError, throwWin32, waitForProcessExit };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/** Generic Win32 process, stdio, and Job Object constants verified on x64. */
|
|
2
|
+
/** STARTUPINFOW uses the standard input, output, and error handles. */
|
|
3
|
+
export declare const STARTF_USESTDHANDLES = 256;
|
|
4
|
+
/** STARTUPINFOW applies wShowWindow when creating a console window. */
|
|
5
|
+
export declare const STARTF_USESHOWWINDOW = 1;
|
|
6
|
+
/** Initial window visibility that preserves the child's console attachment. */
|
|
7
|
+
export declare const SW_HIDE = 0;
|
|
8
|
+
/** HandleInformation flag that permits child inheritance. */
|
|
9
|
+
export declare const HANDLE_FLAG_INHERIT = 1;
|
|
10
|
+
/** Infinite WaitForSingleObject timeout. */
|
|
11
|
+
export declare const INFINITE = 4294967295;
|
|
12
|
+
/** WaitForSingleObject returned because a zero-time probe is not signalled. */
|
|
13
|
+
export declare const WAIT_TIMEOUT = 258;
|
|
14
|
+
/** CreateProcess flag that prevents user code from running before resume. */
|
|
15
|
+
export declare const CREATE_SUSPENDED = 4;
|
|
16
|
+
/** CreateProcess flag selecting a UTF-16 environment block. */
|
|
17
|
+
export declare const CREATE_UNICODE_ENVIRONMENT = 1024;
|
|
18
|
+
/** GetStdHandle selector for standard input. */
|
|
19
|
+
export declare const STD_INPUT_HANDLE = -10;
|
|
20
|
+
/** GetStdHandle selector for standard output. */
|
|
21
|
+
export declare const STD_OUTPUT_HANDLE = -11;
|
|
22
|
+
/** GetStdHandle selector for standard error. */
|
|
23
|
+
export declare const STD_ERROR_HANDLE = -12;
|
|
24
|
+
/** FormatMessage reads the operating system message table. */
|
|
25
|
+
export declare const FORMAT_MESSAGE_FROM_SYSTEM = 4096;
|
|
26
|
+
/** FormatMessage leaves insertion placeholders uninterpreted. */
|
|
27
|
+
export declare const FORMAT_MESSAGE_IGNORE_INSERTS = 512;
|
|
28
|
+
/** Win32 code reporting a caller-provided buffer is too small. */
|
|
29
|
+
export declare const ERROR_INSUFFICIENT_BUFFER = 122;
|
|
30
|
+
/** Win32 code reporting that the other pipe end closed. */
|
|
31
|
+
export declare const ERROR_BROKEN_PIPE = 109;
|
|
32
|
+
/** Win32 code reporting that a pipe has no remaining data. */
|
|
33
|
+
export declare const ERROR_NO_DATA = 232;
|
|
34
|
+
/** Job limit that terminates every member when the final Job handle closes. */
|
|
35
|
+
export declare const JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 8192;
|
|
36
|
+
/** QueryInformationJobObject class for basic accounting and active-process count. */
|
|
37
|
+
export declare const JobObjectBasicAccountingInformation = 1;
|
|
38
|
+
/** SetInformationJobObject class for JOBOBJECT_EXTENDED_LIMIT_INFORMATION. */
|
|
39
|
+
export declare const JobObjectExtendedLimitInformation = 9;
|
|
40
|
+
/** x64 JOBOBJECT_BASIC_ACCOUNTING_INFORMATION byte size. */
|
|
41
|
+
export declare const JOBOBJECT_BASIC_ACCOUNTING_SIZE = 48;
|
|
42
|
+
/** Byte offset of ActiveProcesses in JOBOBJECT_BASIC_ACCOUNTING_INFORMATION. */
|
|
43
|
+
export declare const JOBOBJECT_BASIC_ACCOUNTING_ACTIVE_PROCESSES_OFFSET = 40;
|
|
44
|
+
/** x64 JOBOBJECT_EXTENDED_LIMIT_INFORMATION byte size. */
|
|
45
|
+
export declare const JOBOBJECT_EXTENDED_LIMIT_SIZE = 144;
|
|
46
|
+
/** Byte offset of BasicLimitInformation.LimitFlags in the extended Job record. */
|
|
47
|
+
export declare const JOBOBJECT_EXTENDED_LIMIT_FLAGS_OFFSET = 16;
|
|
48
|
+
/** x64 STARTUPINFOW byte size verified by the native probe. */
|
|
49
|
+
export declare const STARTUPINFOW_SIZE = 104;
|
|
50
|
+
/** x64 PROCESS_INFORMATION byte size verified by the native probe. */
|
|
51
|
+
export declare const PROCESS_INFORMATION_SIZE = 24;
|
|
52
|
+
//# sourceMappingURL=abi.d.ts.map
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Windows CRT startup descriptors for a Node payload with one inherited control pipe. */
|
|
2
|
+
import type { NativePtr, Win32ProcessBindings } from './ffi.ts';
|
|
3
|
+
/** Native stdio handles plus the single additional descriptor selected by the launcher. */
|
|
4
|
+
export interface InheritedControlStdio {
|
|
5
|
+
stdin: NativePtr;
|
|
6
|
+
stdout: NativePtr;
|
|
7
|
+
stderr: NativePtr;
|
|
8
|
+
control: {
|
|
9
|
+
fileDescriptor: 7;
|
|
10
|
+
handle: NativePtr;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Encode the CRT's descriptor table before the child runtime allocates descriptors.
|
|
15
|
+
* Supported Windows targets use 64-bit handles. Empty slots remain closed, and the
|
|
16
|
+
* backing Buffer must remain alive until CreateProcess returns.
|
|
17
|
+
* @param api - native file-type inspection for inherited handles.
|
|
18
|
+
* @param stdio - standard handles and the provider-owned fd-7 control pipe.
|
|
19
|
+
* @returns descriptor count, flag bytes, and handle values for STARTUPINFO's reserved CRT fields.
|
|
20
|
+
*/
|
|
21
|
+
export declare function inheritedControlStdio(api: Pick<Win32ProcessBindings, 'getFileType'>, stdio: InheritedControlStdio): Buffer;
|
|
22
|
+
//# sourceMappingURL=control-stdio.d.ts.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Win32 call failure with the exact API name and error code. */
|
|
2
|
+
export declare class Win32Error extends Error {
|
|
3
|
+
/** Win32 function whose checked result failed. */
|
|
4
|
+
readonly api: string;
|
|
5
|
+
/** Exact GetLastError value or direct Win32 API error code. */
|
|
6
|
+
readonly win32Code: number;
|
|
7
|
+
constructor(api: string, win32Code: number, detail?: string);
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/** Lazy Koffi bindings for generic Win32 process, stdio, and Job operations. */
|
|
2
|
+
import { type Koffi } from './koffi.ts';
|
|
3
|
+
declare const nativePtr: unique symbol;
|
|
4
|
+
/** Koffi native pointer branded against accidental numeric use. */
|
|
5
|
+
export type NativePtr = bigint & {
|
|
6
|
+
readonly [nativePtr]: true;
|
|
7
|
+
};
|
|
8
|
+
type Ptr = ReturnType<Koffi['pointer']>;
|
|
9
|
+
/** Loaded Win32 libraries and the shared stdcall binder used by process extensions. */
|
|
10
|
+
export interface Win32BindingContext {
|
|
11
|
+
/** Kernel process, handle, pipe, and Job APIs. */
|
|
12
|
+
readonly kernel32: ReturnType<Koffi['load']>;
|
|
13
|
+
/** Token and security APIs. */
|
|
14
|
+
readonly advapi32: ReturnType<Koffi['load']>;
|
|
15
|
+
/** Bind one stdcall function from a loaded Win32 library. */
|
|
16
|
+
readonly bind: (library: ReturnType<Koffi['load']>, name: string, result: Ptr | string, args: Array<Ptr | string>) => unknown;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Return whether a Koffi pointer represents NULL.
|
|
20
|
+
* @param value - pointer value returned by Koffi or a Win32 call.
|
|
21
|
+
* @returns true for null, undefined, or address zero.
|
|
22
|
+
*/
|
|
23
|
+
export declare function isNullPtr(value: NativePtr | null | undefined): value is null | undefined;
|
|
24
|
+
/** STARTUPINFOW fields used by inherited or piped stdio launches. */
|
|
25
|
+
export interface StartupInfoInput {
|
|
26
|
+
cb: number;
|
|
27
|
+
dwFlags: number;
|
|
28
|
+
wShowWindow: number;
|
|
29
|
+
hStdInput: NativePtr;
|
|
30
|
+
hStdOutput: NativePtr;
|
|
31
|
+
hStdError: NativePtr;
|
|
32
|
+
cbReserved2?: number;
|
|
33
|
+
lpReserved2?: NativePtr;
|
|
34
|
+
}
|
|
35
|
+
/** Decoded PROCESS_INFORMATION result. */
|
|
36
|
+
export interface ProcessInfoOutput {
|
|
37
|
+
hProcess: NativePtr | null;
|
|
38
|
+
hThread: NativePtr | null;
|
|
39
|
+
dwProcessId: number;
|
|
40
|
+
dwThreadId: number;
|
|
41
|
+
}
|
|
42
|
+
/** Generic Win32 calls consumed by restricted-token sandbox process operations. */
|
|
43
|
+
export interface Win32ProcessBindings {
|
|
44
|
+
closeHandle(handle: NativePtr): number;
|
|
45
|
+
getLastError(): number;
|
|
46
|
+
getFileType(handle: NativePtr): number;
|
|
47
|
+
uvGetOsfhandle(fileDescriptor: number): NativePtr | null;
|
|
48
|
+
formatMessageW(flags: number, source: null, messageId: number, languageId: number, buffer: Buffer, size: number, args: null): number;
|
|
49
|
+
createPipe(readHandle: NativePtr, writeHandle: NativePtr, attributes: null, size: number): number;
|
|
50
|
+
setHandleInformation(handle: NativePtr, mask: number, flags: number): number;
|
|
51
|
+
createProcessAsUserW(token: NativePtr, applicationName: string | null, commandLine: string, processAttributes: null, threadAttributes: null, inheritHandles: number, creationFlags: number, environment: null, currentDirectory: string | null, startupInfo: NativePtr, processInfo: NativePtr): number;
|
|
52
|
+
createProcessW(applicationName: string | null, commandLine: string, processAttributes: null, threadAttributes: null, inheritHandles: number, creationFlags: number, environment: Buffer | null, currentDirectory: string | null, startupInfo: NativePtr, processInfo: NativePtr): number;
|
|
53
|
+
readFile(file: NativePtr, buffer: Buffer, count: number, bytesRead: NativePtr, overlapped: null): number;
|
|
54
|
+
peekNamedPipe(pipe: NativePtr, buffer: null, size: number, bytesRead: NativePtr | null, totalAvail: NativePtr, leftThisMessage: NativePtr | null): number;
|
|
55
|
+
waitForSingleObject(handle: NativePtr, milliseconds: number): number;
|
|
56
|
+
getExitCodeProcess(process: NativePtr, exitCode: NativePtr): number;
|
|
57
|
+
createJobObjectW(attributes: null, name: null): NativePtr;
|
|
58
|
+
setInformationJobObject(job: NativePtr, cls: number, information: Buffer, length: number): number;
|
|
59
|
+
queryInformationJobObject(job: NativePtr, cls: number, information: Buffer, length: number, returnLength: null): number;
|
|
60
|
+
assignProcessToJobObject(job: NativePtr, process: NativePtr): number;
|
|
61
|
+
resumeThread(thread: NativePtr): number;
|
|
62
|
+
terminateProcess(process: NativePtr, exitCode: number): number;
|
|
63
|
+
terminateJobObject(job: NativePtr, exitCode: number): number;
|
|
64
|
+
getStdHandle(stdHandle: number): NativePtr;
|
|
65
|
+
}
|
|
66
|
+
/** Generic Win32 calls plus Node's libuv descriptor-to-handle bridge. */
|
|
67
|
+
export interface CurrentTokenProcessBindings extends Win32ProcessBindings {
|
|
68
|
+
uvGetOsfhandle(fileDescriptor: number): NativePtr | null;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Materialize the Koffi STARTUPINFOW layout on first native use.
|
|
72
|
+
* @returns the cached native struct type.
|
|
73
|
+
*/
|
|
74
|
+
export declare function startupInfoType(): ReturnType<Koffi['struct']>;
|
|
75
|
+
/**
|
|
76
|
+
* Materialize the Koffi PROCESS_INFORMATION layout on first native use.
|
|
77
|
+
* @returns the cached native struct type.
|
|
78
|
+
*/
|
|
79
|
+
export declare function processInformationType(): ReturnType<Koffi['struct']>;
|
|
80
|
+
/**
|
|
81
|
+
* Allocate a pointer-sized out-parameter slot.
|
|
82
|
+
* @returns allocated native slot.
|
|
83
|
+
*/
|
|
84
|
+
export declare function allocPtrSlot(): NativePtr;
|
|
85
|
+
/**
|
|
86
|
+
* Allocate a uint32 out-parameter slot.
|
|
87
|
+
* @returns allocated native slot.
|
|
88
|
+
*/
|
|
89
|
+
export declare function allocUint32(): NativePtr;
|
|
90
|
+
/**
|
|
91
|
+
* Decode a pointer out-parameter.
|
|
92
|
+
* @param slot - pointer-sized slot filled by Win32.
|
|
93
|
+
* @returns decoded pointer, or null for address zero.
|
|
94
|
+
*/
|
|
95
|
+
export declare function decodePtr(slot: NativePtr): NativePtr | null;
|
|
96
|
+
/**
|
|
97
|
+
* Decode a uint32 out-parameter.
|
|
98
|
+
* @param slot - uint32 slot filled by Win32.
|
|
99
|
+
* @returns decoded unsigned value.
|
|
100
|
+
*/
|
|
101
|
+
export declare function decodeUint32(slot: NativePtr): number;
|
|
102
|
+
/**
|
|
103
|
+
* Allocate a zeroed STARTUPINFOW.
|
|
104
|
+
* @returns allocated struct pointer.
|
|
105
|
+
*/
|
|
106
|
+
export declare function allocStartupInfo(): NativePtr;
|
|
107
|
+
/**
|
|
108
|
+
* Encode the stdio-bearing STARTUPINFOW fields.
|
|
109
|
+
* @param startupInfo - allocated STARTUPINFOW pointer.
|
|
110
|
+
* @param fields - fields required for inherited stdio.
|
|
111
|
+
*/
|
|
112
|
+
export declare function encodeStartupInfo(startupInfo: NativePtr, fields: StartupInfoInput): void;
|
|
113
|
+
/**
|
|
114
|
+
* Allocate a zeroed PROCESS_INFORMATION.
|
|
115
|
+
* @returns allocated struct pointer.
|
|
116
|
+
*/
|
|
117
|
+
export declare function allocProcessInfo(): NativePtr;
|
|
118
|
+
/**
|
|
119
|
+
* Decode PROCESS_INFORMATION.
|
|
120
|
+
* @param processInfo - struct pointer filled by CreateProcess.
|
|
121
|
+
* @returns process/thread handles and ids.
|
|
122
|
+
*/
|
|
123
|
+
export declare function decodeProcessInfo(processInfo: NativePtr): ProcessInfoOutput;
|
|
124
|
+
/**
|
|
125
|
+
* Extend the shared process table with caller-owned Win32 API families.
|
|
126
|
+
* @param create - binds only the caller-specific operations from the shared libraries.
|
|
127
|
+
* @returns generic process bindings combined with the caller-specific operations.
|
|
128
|
+
*/
|
|
129
|
+
export declare function extendWin32ProcessBindings<Extension extends object>(create: (context: Win32BindingContext) => Extension): CurrentTokenProcessBindings & Extension;
|
|
130
|
+
/**
|
|
131
|
+
* Load the generic process binding table without policy-specific extensions.
|
|
132
|
+
* @returns shared Win32 process, stdio, and Job operations.
|
|
133
|
+
*/
|
|
134
|
+
export declare function loadWin32ProcessBindings(): CurrentTokenProcessBindings;
|
|
135
|
+
/**
|
|
136
|
+
* Format a Win32 error code through FormatMessageW.
|
|
137
|
+
* @param api - active binding table.
|
|
138
|
+
* @param win32Code - captured GetLastError value.
|
|
139
|
+
* @returns trimmed system message, or an empty string when unavailable.
|
|
140
|
+
*/
|
|
141
|
+
export declare function errorText(api: Win32ProcessBindings, win32Code: number): string;
|
|
142
|
+
/**
|
|
143
|
+
* Throw the current GetLastError value.
|
|
144
|
+
* @param api - active binding table.
|
|
145
|
+
* @param name - failing Win32 operation.
|
|
146
|
+
* @param detail - optional operation context.
|
|
147
|
+
* @returns never; always throws Win32Error.
|
|
148
|
+
*/
|
|
149
|
+
export declare function throwLastError(api: Win32ProcessBindings, name: string, detail?: string): never;
|
|
150
|
+
/**
|
|
151
|
+
* Throw an explicitly captured Win32 error code.
|
|
152
|
+
* @param api - active binding table.
|
|
153
|
+
* @param name - failing Win32 operation.
|
|
154
|
+
* @param win32Code - error captured before cleanup.
|
|
155
|
+
* @param detail - optional operation context.
|
|
156
|
+
* @returns never; always throws Win32Error.
|
|
157
|
+
*/
|
|
158
|
+
export declare function throwWin32(api: Win32ProcessBindings, name: string, win32Code: number, detail?: string): never;
|
|
159
|
+
export {};
|
|
160
|
+
//# sourceMappingURL=ffi.d.ts.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Shared low-level Win32 process, stdio, and Job Object primitives. */
|
|
2
|
+
export { ERROR_INSUFFICIENT_BUFFER } from './abi.ts';
|
|
3
|
+
export * from './errors.ts';
|
|
4
|
+
export { allocPtrSlot, allocUint32, decodePtr, decodeUint32, extendWin32ProcessBindings, isNullPtr, loadWin32ProcessBindings, throwLastError, throwWin32, } from './ffi.ts';
|
|
5
|
+
export type { CurrentTokenProcessBindings, NativePtr, Win32ProcessBindings, } from './ffi.ts';
|
|
6
|
+
export { closeHandleChecked, drainPipe, isJobEmpty, pollProcessExit, probeCurrentTokenJobSupport, spawnInheritedJobProcess, spawnCurrentTokenJobProcess, spawnPipedProcess, terminateJob, waitForProcessExit, } from './process.ts';
|
|
7
|
+
export type { CurrentTokenStdioFileDescriptors, CurrentTokenProcessSpawnOptions, SpawnedJobProcess, SpawnedPipedProcess, } from './process.ts';
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/** Process-realm lazy access to Koffi's CommonJS entry. */
|
|
2
|
+
import type koffi from 'koffi';
|
|
3
|
+
/** Koffi runtime export type. */
|
|
4
|
+
export type Koffi = typeof koffi;
|
|
5
|
+
/** Load Koffi on the first Win32 native operation. */
|
|
6
|
+
export declare const requireKoffi: () => {
|
|
7
|
+
LibraryHandle: import("koffi").LibraryHandle;
|
|
8
|
+
TypeObject: import("koffi").TypeObject;
|
|
9
|
+
Union: import("koffi").Union;
|
|
10
|
+
address: typeof import("koffi").address;
|
|
11
|
+
alias: typeof import("koffi").alias;
|
|
12
|
+
alignof: typeof import("koffi").alignof;
|
|
13
|
+
alloc: typeof import("koffi").alloc;
|
|
14
|
+
array: typeof import("koffi").array;
|
|
15
|
+
as: typeof import("koffi").as;
|
|
16
|
+
call: typeof import("koffi").call;
|
|
17
|
+
config: typeof import("koffi").config;
|
|
18
|
+
decode: typeof import("koffi").decode;
|
|
19
|
+
disposable: typeof import("koffi").disposable;
|
|
20
|
+
encode: typeof import("koffi").encode;
|
|
21
|
+
enumeration: typeof import("koffi").enumeration;
|
|
22
|
+
errno: typeof import("koffi").errno;
|
|
23
|
+
extension: typeof import("koffi").extension;
|
|
24
|
+
free: typeof import("koffi").free;
|
|
25
|
+
inout: typeof import("koffi").inout;
|
|
26
|
+
introspect: typeof import("koffi").introspect;
|
|
27
|
+
load: typeof import("koffi").load;
|
|
28
|
+
node: typeof import("koffi").node;
|
|
29
|
+
offsetof: typeof import("koffi").offsetof;
|
|
30
|
+
opaque: typeof import("koffi").opaque;
|
|
31
|
+
os: typeof import("koffi").os;
|
|
32
|
+
out: typeof import("koffi").out;
|
|
33
|
+
pack: typeof import("koffi").pack;
|
|
34
|
+
pointer: typeof import("koffi").pointer;
|
|
35
|
+
proto: typeof import("koffi").proto;
|
|
36
|
+
register: typeof import("koffi").register;
|
|
37
|
+
reset: typeof import("koffi").reset;
|
|
38
|
+
resolve: typeof import("koffi").resolve;
|
|
39
|
+
sizeof: typeof import("koffi").sizeof;
|
|
40
|
+
stats: typeof import("koffi").stats;
|
|
41
|
+
struct: typeof import("koffi").struct;
|
|
42
|
+
type: typeof import("koffi").type;
|
|
43
|
+
types: typeof import("koffi").types;
|
|
44
|
+
union: typeof import("koffi").union;
|
|
45
|
+
unregister: typeof import("koffi").unregister;
|
|
46
|
+
version: typeof import("koffi").version;
|
|
47
|
+
view: typeof import("koffi").view;
|
|
48
|
+
};
|
|
49
|
+
//# sourceMappingURL=koffi.d.ts.map
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/** Typed Win32 process operations over the shared binding table. */
|
|
2
|
+
import type { CurrentTokenProcessBindings, NativePtr, Win32ProcessBindings } from './ffi.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Quote one argument according to CommandLineToArgvW parsing.
|
|
5
|
+
* @param argument - one argv entry.
|
|
6
|
+
* @returns bare or quoted command-line segment.
|
|
7
|
+
*/
|
|
8
|
+
export declare function quoteArg(argument: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* Build the mutable command line accepted by CreateProcessAsUserW.
|
|
11
|
+
* @param program - executable argv entry.
|
|
12
|
+
* @param args - remaining argv entries.
|
|
13
|
+
* @returns joined Win32 command line.
|
|
14
|
+
*/
|
|
15
|
+
export declare function buildCommandLine(program: string, args: readonly string[]): string;
|
|
16
|
+
interface ProcessSpawnOptions {
|
|
17
|
+
/** Executable argv entry passed through CreateProcess. */
|
|
18
|
+
command: string;
|
|
19
|
+
/** Arguments excluding the executable. */
|
|
20
|
+
args: readonly string[];
|
|
21
|
+
/** Existing child working directory. */
|
|
22
|
+
cwd: string;
|
|
23
|
+
}
|
|
24
|
+
/** Ordinary process creation inputs used by the local Win32 runner. */
|
|
25
|
+
export interface CurrentTokenProcessSpawnOptions extends ProcessSpawnOptions {
|
|
26
|
+
/** Resolved executable path passed separately from the preserved argv entry. */
|
|
27
|
+
applicationName: string;
|
|
28
|
+
/** Complete target environment passed without mutating the runner. */
|
|
29
|
+
env: Readonly<Record<string, string>>;
|
|
30
|
+
/** Runner CRT descriptors carrying target stdin, stdout, and stderr. */
|
|
31
|
+
stdio: CurrentTokenStdioFileDescriptors;
|
|
32
|
+
}
|
|
33
|
+
/** Runner CRT descriptors whose OS handles become the target standard handles. */
|
|
34
|
+
export interface CurrentTokenStdioFileDescriptors {
|
|
35
|
+
stdin: number;
|
|
36
|
+
stdout: number;
|
|
37
|
+
stderr: number;
|
|
38
|
+
/** Optional carrier and target descriptor for the inherited control pipe. */
|
|
39
|
+
control?: 7;
|
|
40
|
+
}
|
|
41
|
+
/** Restricted-token process creation inputs owned by the Windows ACL sandbox. */
|
|
42
|
+
export interface RestrictedProcessSpawnOptions extends ProcessSpawnOptions {
|
|
43
|
+
/** Restricted primary token supplied by sandbox policy. */
|
|
44
|
+
token: NativePtr;
|
|
45
|
+
/** Optional control pipe inherited at the same descriptor in the payload. */
|
|
46
|
+
controlFileDescriptor?: 7;
|
|
47
|
+
}
|
|
48
|
+
/** Piped child resources whose process and read handles remain caller-owned. */
|
|
49
|
+
export interface SpawnedPipedProcess {
|
|
50
|
+
/** Direct child process id. */
|
|
51
|
+
pid: number;
|
|
52
|
+
/** Process handle closed by waitForProcessExit. */
|
|
53
|
+
process: NativePtr;
|
|
54
|
+
/** Stdout pipe read end closed by drainPipe. */
|
|
55
|
+
stdoutRead: NativePtr;
|
|
56
|
+
/** Stderr pipe read end closed by drainPipe. */
|
|
57
|
+
stderrRead: NativePtr;
|
|
58
|
+
}
|
|
59
|
+
/** Suspended child assigned to one caller-owned kill-on-close Job before resume. */
|
|
60
|
+
export interface SpawnedJobProcess {
|
|
61
|
+
/** Direct child process id. */
|
|
62
|
+
pid: number;
|
|
63
|
+
/** Process handle closed by waitForProcessExit. */
|
|
64
|
+
process: NativePtr;
|
|
65
|
+
/** Job handle closed by the lifecycle owner. */
|
|
66
|
+
job: NativePtr;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Spawn a process with anonymous-pipe stdout/stderr and immediate stdin EOF.
|
|
70
|
+
* New console windows start hidden without changing console inheritance.
|
|
71
|
+
* @param api - active binding table.
|
|
72
|
+
* @param options - command, cwd, args, and restricted primary token.
|
|
73
|
+
* @returns caller-owned process and pipe read handles.
|
|
74
|
+
*/
|
|
75
|
+
export declare function spawnPipedProcess(api: Win32ProcessBindings, options: RestrictedProcessSpawnOptions): SpawnedPipedProcess;
|
|
76
|
+
/**
|
|
77
|
+
* Drain one anonymous pipe until the writer closes it.
|
|
78
|
+
* @param api - active binding table.
|
|
79
|
+
* @param handle - caller-owned pipe read end.
|
|
80
|
+
* @returns complete bytes read before EOF; the handle is always closed.
|
|
81
|
+
* @throws when a Win32 pipe operation fails.
|
|
82
|
+
*/
|
|
83
|
+
export declare function drainPipe(api: Win32ProcessBindings, handle: NativePtr): Promise<Buffer>;
|
|
84
|
+
/**
|
|
85
|
+
* Wait for a process and always close its handle.
|
|
86
|
+
* @param api - active binding table.
|
|
87
|
+
* @param process - caller-owned process handle.
|
|
88
|
+
* @returns direct process exit code.
|
|
89
|
+
*/
|
|
90
|
+
export declare function waitForProcessExit(api: Win32ProcessBindings, process: NativePtr): number;
|
|
91
|
+
/**
|
|
92
|
+
* Spawn a restricted-token process suspended with hidden initial windows, assign its Job, then resume it.
|
|
93
|
+
* @param api - active binding table.
|
|
94
|
+
* @param options - command, cwd, args, and restricted primary token.
|
|
95
|
+
* @returns caller-owned process and Job handles after successful resume.
|
|
96
|
+
* @remarks Node clears stdio handle inheritability at startup through
|
|
97
|
+
* uv_disable_stdio_inheritance. This operation temporarily restores the bits
|
|
98
|
+
* required by STARTF_USESTDHANDLES. Restoring them afterward is best-effort:
|
|
99
|
+
* failure must not replace the already-created child's outcome.
|
|
100
|
+
*/
|
|
101
|
+
export declare function spawnInheritedJobProcess(api: Win32ProcessBindings, options: RestrictedProcessSpawnOptions): SpawnedJobProcess;
|
|
102
|
+
/**
|
|
103
|
+
* Spawn an ordinary process suspended with hidden initial windows, assign its Job, then resume it.
|
|
104
|
+
* @param api - active binding table.
|
|
105
|
+
* @param options - command, cwd, argv, and target carrier descriptors.
|
|
106
|
+
* @returns caller-owned process and Job handles after successful resume.
|
|
107
|
+
*/
|
|
108
|
+
export declare function spawnCurrentTokenJobProcess(api: CurrentTokenProcessBindings, options: CurrentTokenProcessSpawnOptions): SpawnedJobProcess;
|
|
109
|
+
/**
|
|
110
|
+
* Verify that an unnamed kill-on-close Job can be created and released now.
|
|
111
|
+
* @param api - active binding table.
|
|
112
|
+
*/
|
|
113
|
+
export declare function probeCurrentTokenJobSupport(api: CurrentTokenProcessBindings): void;
|
|
114
|
+
/**
|
|
115
|
+
* Poll one process handle without blocking the runner event loop.
|
|
116
|
+
* @param api - active binding table.
|
|
117
|
+
* @param process - caller-owned process handle.
|
|
118
|
+
* @returns the direct exit code when signalled, or undefined while running.
|
|
119
|
+
*/
|
|
120
|
+
export declare function pollProcessExit(api: Win32ProcessBindings, process: NativePtr): number | undefined;
|
|
121
|
+
/**
|
|
122
|
+
* Return whether a Job has no active processes.
|
|
123
|
+
* @param api - active binding table.
|
|
124
|
+
* @param job - caller-owned Job handle.
|
|
125
|
+
* @returns true once the Job reports zero active processes.
|
|
126
|
+
*/
|
|
127
|
+
export declare function isJobEmpty(api: Win32ProcessBindings, job: NativePtr): boolean;
|
|
128
|
+
/**
|
|
129
|
+
* Terminate every process in a Job.
|
|
130
|
+
* @param api - active binding table.
|
|
131
|
+
* @param job - caller-owned Job handle.
|
|
132
|
+
* @param exitCode - direct Windows exit code assigned to members.
|
|
133
|
+
*/
|
|
134
|
+
export declare function terminateJob(api: Win32ProcessBindings, job: NativePtr, exitCode: number): void;
|
|
135
|
+
/**
|
|
136
|
+
* Close a caller-owned handle and report a labelled Win32 failure.
|
|
137
|
+
* @param api - active binding table.
|
|
138
|
+
* @param handle - handle to close.
|
|
139
|
+
* @param detail - lifecycle label for diagnostics.
|
|
140
|
+
*/
|
|
141
|
+
export declare function closeHandleChecked(api: Win32ProcessBindings, handle: NativePtr, detail: string): void;
|
|
142
|
+
export {};
|
|
143
|
+
//# sourceMappingURL=process.d.ts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unieai/rabi-win32-process",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"description": "Shared low-level Win32 process, stdio, and Job Object primitives",
|
|
4
|
+
"version": "0.2.1-rc.3",
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "lib/index.js",
|
|
10
|
+
"types": "lib/types/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./lib/types/index.d.ts",
|
|
14
|
+
"default": "./lib/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./src/*": "./src/*",
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"lib/index.js",
|
|
21
|
+
"lib/types/**/*.d.ts",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@unieai/cordis": "4.0.5"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@unieai/rabi-lazy-require": "0.2.1-rc.3",
|
|
30
|
+
"koffi": "3.1.1"
|
|
31
|
+
}
|
|
32
|
+
}
|