poe-code 3.0.307 → 3.0.308

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "poe-code",
3
- "version": "3.0.307",
3
+ "version": "3.0.308",
4
4
  "description": "CLI tool to configure Poe API for developer workflows.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -2,8 +2,10 @@ import net from "node:net";
2
2
  export async function waitForReady(check, options) {
3
3
  assertValidTimeout(options.timeoutMs, "readiness timeout");
4
4
  if (check.kind === "log-pattern") {
5
+ assertValidLogPattern(check.pattern);
5
6
  return waitForLogPattern(check.pattern, options);
6
7
  }
8
+ assertValidTcpPort(check.port);
7
9
  return waitForTcp(check, options);
8
10
  }
9
11
  function waitForLogPattern(pattern, options) {
@@ -124,3 +126,13 @@ function assertValidTimeout(value, description) {
124
126
  throw new Error(`Invalid ${description}: ${value}`);
125
127
  }
126
128
  }
129
+ function assertValidLogPattern(value) {
130
+ if (value.trim().length === 0) {
131
+ throw new Error("Invalid log pattern readiness check: pattern must not be blank.");
132
+ }
133
+ }
134
+ function assertValidTcpPort(value) {
135
+ if (!Number.isSafeInteger(value) || value <= 0 || value > 65_535) {
136
+ throw new Error(`Invalid TCP readiness port: ${value}`);
137
+ }
138
+ }
@@ -6,7 +6,7 @@ import { hasOwnErrorCode } from "./errors.js";
6
6
  import { createLogWriter } from "./logs/log-writer.js";
7
7
  import { assertPathHasNoSymbolicLinks } from "./path-safety.js";
8
8
  import { assertValidManagedProcessId } from "./process-id.js";
9
- import { createStateStore } from "./state/state-store.js";
9
+ import { assertValidProcessStateDocument, createStateStore } from "./state/state-store.js";
10
10
  import { createSupervisor } from "./supervisor/supervisor.js";
11
11
  const DEFAULT_POLL_INTERVAL_MS = 100;
12
12
  const DEFAULT_STARTUP_TIMEOUT_MS = 30_000;
@@ -14,6 +14,7 @@ const DEFAULT_STOP_TIMEOUT_MS = 5_000;
14
14
  const TEMP_WRITE_MAX_ATTEMPTS = 3;
15
15
  export async function startManagedProcess(options) {
16
16
  assertOptionalFiniteDuration(options.startupTimeoutMs, "startup timeout");
17
+ assertOptionalFiniteDuration(options.pollIntervalMs, "poll interval");
17
18
  const fs = options.fs ?? defaultFs();
18
19
  const spec = normalizeSpec(options.spec);
19
20
  const existing = await readManagedProcess({
@@ -56,6 +57,8 @@ export async function startManagedProcess(options) {
56
57
  }
57
58
  }
58
59
  export async function stopManagedProcess(options) {
60
+ assertOptionalFiniteDuration(options.stopTimeoutMs, "stop timeout");
61
+ assertOptionalFiniteDuration(options.pollIntervalMs, "poll interval");
59
62
  const fs = options.fs ?? defaultFs();
60
63
  const record = await readManagedProcess({
61
64
  baseDir: options.baseDir,
@@ -310,6 +313,7 @@ export async function runManagedProcess(options) {
310
313
  if (options.signal?.aborted) {
311
314
  return;
312
315
  }
316
+ assertOptionalFiniteDuration(options.pollIntervalMs, "poll interval");
313
317
  const fs = options.fs ?? defaultFs();
314
318
  await assertProcessDirectorySafe(fs, options.baseDir, options.id);
315
319
  await assertPathNotSymbolicLink(fs, resolveLogDir(options.baseDir, options.id));
@@ -523,13 +527,94 @@ async function readSpec(fs, baseDir, id) {
523
527
  if (!isRecord(spec) || typeof spec.id !== "string" || spec.id !== id) {
524
528
  throw new Error(`Invalid managed process specification for "${id}".`);
525
529
  }
526
- return spec;
530
+ return assertValidProcessSpec(spec, id);
531
+ }
532
+ function assertValidProcessSpec(value, id) {
533
+ if (value.id !== id ||
534
+ !isNonEmptyString(value.command) ||
535
+ !isOptionalStringArray(value.args) ||
536
+ !isOptionalString(value.cwd) ||
537
+ !isOptionalStringRecord(value.env) ||
538
+ !isRestartPolicy(value.restart) ||
539
+ !isOptionalNonNegativeSafeInteger(value.maxRestarts) ||
540
+ !isOptionalFiniteDurationValue(value.backoffMs) ||
541
+ !isOptionalFiniteDurationValue(value.maxBackoffMs) ||
542
+ !isOptionalPositiveSafeInteger(value.logRetainCount) ||
543
+ !isOptionalReadyCheck(value.readyCheck) ||
544
+ !isOptionalRecord(value.docker)) {
545
+ throw new Error(`Invalid managed process specification for "${id}".`);
546
+ }
547
+ return value;
548
+ }
549
+ function isNonEmptyString(value) {
550
+ return typeof value === "string" && value.trim().length > 0;
551
+ }
552
+ function isOptionalString(value) {
553
+ return value === undefined || typeof value === "string";
554
+ }
555
+ function isOptionalStringArray(value) {
556
+ return value === undefined || (Array.isArray(value) &&
557
+ value.every((entry) => typeof entry === "string"));
558
+ }
559
+ function isOptionalStringRecord(value) {
560
+ if (value === undefined) {
561
+ return true;
562
+ }
563
+ if (!isPlainRecord(value)) {
564
+ return false;
565
+ }
566
+ return Object.values(value).every((entry) => typeof entry === "string");
567
+ }
568
+ function isRestartPolicy(value) {
569
+ return value === "never" || value === "on-failure" || value === "always";
570
+ }
571
+ function isOptionalNonNegativeSafeInteger(value) {
572
+ return value === undefined || (typeof value === "number" &&
573
+ Number.isSafeInteger(value) &&
574
+ value >= 0);
575
+ }
576
+ function isOptionalPositiveSafeInteger(value) {
577
+ return value === undefined || (typeof value === "number" &&
578
+ Number.isSafeInteger(value) &&
579
+ value > 0);
580
+ }
581
+ function isOptionalFiniteDurationValue(value) {
582
+ return value === undefined || (typeof value === "number" &&
583
+ Number.isFinite(value) &&
584
+ value >= 0);
585
+ }
586
+ function isOptionalReadyCheck(value) {
587
+ if (value === undefined) {
588
+ return true;
589
+ }
590
+ if (!isPlainRecord(value)) {
591
+ return false;
592
+ }
593
+ if (value.kind === "log-pattern") {
594
+ return isNonEmptyString(value.pattern);
595
+ }
596
+ if (value.kind === "tcp") {
597
+ return (typeof value.port === "number" &&
598
+ Number.isSafeInteger(value.port) &&
599
+ value.port > 0 &&
600
+ value.port <= 65_535 &&
601
+ isOptionalString(value.host) &&
602
+ isOptionalFiniteDurationValue(value.timeoutMs));
603
+ }
604
+ return false;
605
+ }
606
+ function isOptionalRecord(value) {
607
+ return value === undefined || isPlainRecord(value);
608
+ }
609
+ function isPlainRecord(value) {
610
+ return typeof value === "object" && value !== null && !Array.isArray(value);
527
611
  }
528
612
  async function writeSpec(fs, baseDir, spec) {
529
613
  await writeJsonFile(fs, resolveSpecPath(baseDir, spec.id), spec);
530
614
  }
531
615
  async function readState(fs, baseDir, id) {
532
- return await readJsonFile(fs, resolveStatePath(baseDir, id));
616
+ const state = await readJsonFile(fs, resolveStatePath(baseDir, id));
617
+ return state === null ? null : assertValidProcessStateDocument(state, id);
533
618
  }
534
619
  async function writeState(fs, baseDir, state) {
535
620
  await writeJsonFile(fs, resolveStatePath(baseDir, state.id), state);
@@ -1,6 +1,20 @@
1
1
  import path from "node:path";
2
2
  export function assertValidManagedProcessId(id) {
3
- if (id.length === 0 || id === "." || id === ".." || path.basename(id) !== id) {
3
+ if (id.length === 0 ||
4
+ id !== id.trim() ||
5
+ id === "." ||
6
+ id === ".." ||
7
+ path.basename(id) !== id ||
8
+ hasControlCharacter(id)) {
4
9
  throw new Error(`Invalid managed process id: ${id}`);
5
10
  }
6
11
  }
12
+ function hasControlCharacter(value) {
13
+ for (const char of value) {
14
+ const code = char.charCodeAt(0);
15
+ if (code <= 31 || code === 127) {
16
+ return true;
17
+ }
18
+ }
19
+ return false;
20
+ }
@@ -1,2 +1,3 @@
1
- import type { LauncherFileSystem, StateStore } from "../types.js";
1
+ import type { LauncherFileSystem, ProcessState, StateStore } from "../types.js";
2
2
  export declare function createStateStore(stateDir: string, fs?: LauncherFileSystem): StateStore;
3
+ export declare function assertValidProcessStateDocument(value: unknown, id: string): ProcessState;
@@ -78,10 +78,7 @@ export function createStateStore(stateDir, fs = nodeFs) {
78
78
  await assertPathHasNoSymbolicLinks(fs, statePath);
79
79
  const content = await fs.readFile(statePath, "utf8");
80
80
  const parsed = JSON.parse(content);
81
- if (!isProcessState(parsed, id)) {
82
- throw new Error(`Invalid process state document: ${id}`);
83
- }
84
- return parsed;
81
+ return assertValidProcessStateDocument(parsed, id);
85
82
  }
86
83
  catch (error) {
87
84
  if (isNotFoundError(error)) {
@@ -169,6 +166,12 @@ export function createStateStore(stateDir, fs = nodeFs) {
169
166
  }
170
167
  return { read, write, list, remove };
171
168
  }
169
+ export function assertValidProcessStateDocument(value, id) {
170
+ if (!isProcessState(value, id)) {
171
+ throw new Error(`Invalid process state document: ${id}`);
172
+ }
173
+ return value;
174
+ }
172
175
  function isProcessState(value, id) {
173
176
  if (typeof value !== "object" || value === null || Array.isArray(value)) {
174
177
  return false;
@@ -181,11 +184,20 @@ function isProcessState(value, id) {
181
184
  state.status === "crashed" ||
182
185
  state.status === "restarting") &&
183
186
  (state.runtime === "host" || state.runtime === "docker") &&
184
- typeof state.restartCount === "number" &&
185
- (state.lastExitCode === null || typeof state.lastExitCode === "number") &&
187
+ isPositiveSafeIntegerOrNull(state.pid) &&
188
+ isNonNegativeSafeInteger(state.restartCount) &&
189
+ (state.lastExitCode === null || isNonNegativeSafeInteger(state.lastExitCode)) &&
186
190
  (state.lastStartedAt === null || typeof state.lastStartedAt === "string") &&
187
191
  (state.lastStoppedAt === null || typeof state.lastStoppedAt === "string") &&
188
192
  typeof state.command === "string" &&
189
193
  Array.isArray(state.args) &&
190
194
  state.args.every((argument) => typeof argument === "string"));
191
195
  }
196
+ function isPositiveSafeIntegerOrNull(value) {
197
+ return value === null || (typeof value === "number" &&
198
+ Number.isSafeInteger(value) &&
199
+ value > 0);
200
+ }
201
+ function isNonNegativeSafeInteger(value) {
202
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
203
+ }