pid-file.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Shared PID file format: parse and format runtime pid files.
  3. *
  4. * Two formats are supported:
  5. * - Legacy: plain number string, e.g. "12345"
  6. * - JSON: {"pid":12345,"owner":"electron"|"cli"|"unknown","startedAt":1710000000000}
  7. *
  8. * All readers must handle both. Writers should use the new JSON format.
  9. *
  10. * @module
  11. */
  12. /** Owner of the runtime process — determines who manages its lifecycle. */
  13. export type PidOwner = 'electron' | 'cli' | 'unknown';
  14. export interface PidInfo {
  15. pid: number;
  16. owner: PidOwner;
  17. startedAt?: number;
  18. mode?: string;
  19. startupToken?: string;
  20. }
  21. /**
  22. * Parse a runtime pid file content string into structured PidInfo.
  23. *
  24. * Handles both legacy (plain number) and new JSON formats.
  25. * Returns null if the content is unparseable.
  26. */
  27. export function parsePidFile(raw: string): PidInfo | null {
  28. const trimmed = raw.trim();
  29. if (!trimmed) return null;
  30. // Try JSON format first (starts with '{')
  31. if (trimmed.startsWith('{')) {
  32. try {
  33. const obj = JSON.parse(trimmed) as Record<string, unknown>;
  34. const pid = typeof obj.pid === 'number' ? obj.pid : parseInt(String(obj.pid), 10);
  35. if (isNaN(pid) || pid <= 0) return null;
  36. const owner = isValidOwner(obj.owner) ? obj.owner : 'unknown';
  37. const startedAt = parseStartedAt(obj.startedAt);
  38. return {
  39. pid,
  40. owner,
  41. ...(startedAt !== undefined ? { startedAt } : {}),
  42. ...(typeof obj.mode === 'string' ? { mode: obj.mode } : {}),
  43. ...(typeof obj.startupToken === 'string' ? { startupToken: obj.startupToken } : {}),
  44. };
  45. } catch {
  46. return null;
  47. }
  48. }
  49. // Legacy format: plain number
  50. const pid = parseInt(trimmed, 10);
  51. if (isNaN(pid) || pid <= 0) return null;
  52. return { pid, owner: 'unknown' };
  53. }
  54. function isValidOwner(value: unknown): value is PidOwner {
  55. return value === 'electron' || value === 'cli' || value === 'unknown';
  56. }
  57. function parseStartedAt(value: unknown): number | undefined {
  58. if (typeof value === 'number' && Number.isFinite(value)) return value;
  59. if (typeof value !== 'string') return undefined;
  60. const parsed = new Date(value).getTime();
  61. return Number.isFinite(parsed) ? parsed : undefined;
  62. }
  63. /**
  64. * Format a PID info struct into the JSON file content.
  65. */
  66. export function formatPidFile(pid: number, owner: PidOwner): string {
  67. const info: PidInfo = {
  68. pid,
  69. owner,
  70. startedAt: Date.now(),
  71. };
  72. return JSON.stringify(info);
  73. }
  74. /**
  75. * Determine the runtime owner based on runtime environment.
  76. *
  77. * - --owner electron indicates a supervisor manages runtime lifecycle.
  78. * - Otherwise the runtime was started by CLI / npm directly.
  79. */
  80. export function detectPidOwner(): PidOwner {
  81. const args = process.argv.slice(2);
  82. for (let i = 0; i < args.length; i++) {
  83. const arg = args[i];
  84. const value =
  85. arg === '--owner' ? args[i + 1] : arg?.startsWith('--owner=') ? arg.slice(8) : undefined;
  86. if (value === 'electron') return 'electron';
  87. if (value === 'service' || value === 'cli') return 'cli';
  88. }
  89. return 'cli';
  90. }