@bakers-build/core 0.14.1-next.1 → 0.14.1-next.2

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.
@@ -65,16 +65,18 @@ export type VerifyOutcome = {
65
65
  */
66
66
  export declare function verifyConductorProcess(input: VerifyProcessInput): VerifyOutcome;
67
67
  export declare function signalVerifiedProcess(input: SignalVerifiedInput): SignalOutcome;
68
+ /** Runs a command and returns its stdout; throws on a non-zero exit. */
69
+ export type RunCommand = (file: string, args: string[]) => string;
68
70
  /**
69
71
  * The real probe: `/proc/<pid>/cmdline` and the owning uid of `/proc/<pid>`
70
- * itself on Linux.
72
+ * on Linux; one `ps -ww -o uid= -o command= -p <pid>` line on macOS (`-ww`
73
+ * keeps the command untruncated, so the markers are always in it).
71
74
  *
72
- * Only Linux is claimed as supported. macOS could shell out to `ps`, but a
73
- * wrong answer here signals the wrong process, so an unimplemented platform
74
- * reports `supported: false` and stop degrades with a message rather than
75
- * guessing. Start is unaffected everywhere.
75
+ * Any other platform reports `supported: false`: a wrong answer here signals
76
+ * the wrong process, so stop degrades with a message rather than guessing.
77
+ * Start is unaffected everywhere.
76
78
  */
77
- export declare function defaultProcProbe(): ProcProbe;
79
+ export declare function defaultProcProbe(platform?: NodeJS.Platform, run?: RunCommand): ProcProbe;
78
80
  /** What `killRunProcesses` needs from the platform, injected for tests. */
79
81
  export interface RunProcessProbe {
80
82
  readonly supported: boolean;
@@ -85,7 +87,8 @@ export interface RunProcessProbe {
85
87
  uidOf(pid: number): number | undefined;
86
88
  send(pid: number, signal: NodeJS.Signals): void;
87
89
  }
88
- export declare function defaultRunProcessProbe(): RunProcessProbe;
90
+ /** Linux only: it lists and reads `/proc`. */
91
+ export declare function defaultRunProcessProbe(platform?: NodeJS.Platform): RunProcessProbe;
89
92
  /**
90
93
  * Ends every process of this OS user that carries `BAKERS_RUN_UUID=<runUuid>`:
91
94
  * the run's agent, its sandbox and background commands that outlive the
@@ -1,3 +1,4 @@
1
+ import { execFileSync } from 'node:child_process';
1
2
  import { readdirSync, readFileSync, statSync } from 'node:fs';
2
3
  /**
3
4
  * Substrings a live bakers-conductor foreground process's command line must all
@@ -113,17 +114,52 @@ export function signalVerifiedProcess(input) {
113
114
  }
114
115
  return { ok: true, pid: identified.pid, signal: input.signal };
115
116
  }
117
+ const runCommand = (file, args) => execFileSync(file, args, {
118
+ encoding: 'utf8',
119
+ stdio: ['ignore', 'pipe', 'ignore'],
120
+ });
116
121
  /**
117
122
  * The real probe: `/proc/<pid>/cmdline` and the owning uid of `/proc/<pid>`
118
- * itself on Linux.
123
+ * on Linux; one `ps -ww -o uid= -o command= -p <pid>` line on macOS (`-ww`
124
+ * keeps the command untruncated, so the markers are always in it).
119
125
  *
120
- * Only Linux is claimed as supported. macOS could shell out to `ps`, but a
121
- * wrong answer here signals the wrong process, so an unimplemented platform
122
- * reports `supported: false` and stop degrades with a message rather than
123
- * guessing. Start is unaffected everywhere.
126
+ * Any other platform reports `supported: false`: a wrong answer here signals
127
+ * the wrong process, so stop degrades with a message rather than guessing.
128
+ * Start is unaffected everywhere.
124
129
  */
125
- export function defaultProcProbe() {
126
- const supported = process.platform === 'linux';
130
+ export function defaultProcProbe(platform = process.platform, run = runCommand) {
131
+ const send = (pid, signal) => {
132
+ process.kill(pid, signal);
133
+ };
134
+ if (platform === 'darwin') {
135
+ const psOf = (pid) => {
136
+ try {
137
+ const match = /^\s*(\d+)\s+(\S.*)$/.exec(run('ps', [
138
+ '-ww',
139
+ '-o',
140
+ 'uid=',
141
+ '-o',
142
+ 'command=',
143
+ '-p',
144
+ String(pid),
145
+ ]).trim());
146
+ return match
147
+ ? { uid: Number(match[1]), command: match[2] }
148
+ : undefined;
149
+ }
150
+ catch {
151
+ // ps exits non-zero when the pid is gone.
152
+ return undefined;
153
+ }
154
+ };
155
+ return {
156
+ supported: true,
157
+ cmdlineOf: (pid) => psOf(pid)?.command,
158
+ uidOf: (pid) => psOf(pid)?.uid,
159
+ send,
160
+ };
161
+ }
162
+ const supported = platform === 'linux';
127
163
  return {
128
164
  supported,
129
165
  cmdlineOf(pid) {
@@ -152,17 +188,17 @@ export function defaultProcProbe() {
152
188
  return undefined;
153
189
  }
154
190
  },
155
- send(pid, signal) {
156
- process.kill(pid, signal);
157
- },
191
+ send,
158
192
  };
159
193
  }
160
- export function defaultRunProcessProbe() {
161
- const base = defaultProcProbe();
194
+ /** Linux only: it lists and reads `/proc`. */
195
+ export function defaultRunProcessProbe(platform = process.platform) {
196
+ const base = defaultProcProbe(platform);
197
+ const supported = platform === 'linux';
162
198
  return {
163
- supported: base.supported,
199
+ supported,
164
200
  pids() {
165
- if (!base.supported)
201
+ if (!supported)
166
202
  return [];
167
203
  return readdirSync('/proc')
168
204
  .filter((name) => /^\d+$/.test(name))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bakers-build/core",
3
- "version": "0.14.1-next.1",
3
+ "version": "0.14.1-next.2",
4
4
  "description": "bakers.build surface-agnostic kernel: host contract, split-config helpers, addon preset/discovery machinery, shared primitives.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -40,7 +40,7 @@
40
40
  "yaml": "^2.7.0"
41
41
  },
42
42
  "bakersBuild": {
43
- "commit": "e6b3d200ba14c56b2fb9d3695f273ea349a97f65",
43
+ "commit": "3f4e6a91f380b3a2aaa69324ba3400e4cadf3e17",
44
44
  "channel": "next"
45
45
  }
46
46
  }