runtime-requirements.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { readFile, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { createRequire } from "node:module";
  4. const require = createRequire(import.meta.url);
  5. export const SQLITE_MEMORY_MIN_NODE_MAJOR = 22;
  6. export const SQLITE_MEMORY_PIN_VERSION = String(SQLITE_MEMORY_MIN_NODE_MAJOR);
  7. export const SQLITE_MEMORY_PIN_FILES = [".nvmrc", ".node-version"] as const;
  8. export interface SqliteMemorySupportResult {
  9. readonly ok: boolean;
  10. readonly detail: string;
  11. }
  12. export interface NodeRuntimePinStatus {
  13. readonly ok: boolean;
  14. readonly detail: string;
  15. readonly missing: ReadonlyArray<string>;
  16. }
  17. export interface NodeRuntimePinRepairResult {
  18. readonly updated: boolean;
  19. readonly written: ReadonlyArray<string>;
  20. }
  21. export function formatSqliteMemorySupportWarning(options?: {
  22. readonly nodeVersion?: string;
  23. readonly hasNodeSqlite?: boolean;
  24. }): string | null {
  25. const nodeVersion = options?.nodeVersion ?? process.version;
  26. const result = evaluateSqliteMemorySupport({
  27. nodeVersion,
  28. hasNodeSqlite: options?.hasNodeSqlite,
  29. });
  30. if (result.ok) return null;
  31. return `Node ${nodeVersion} does not support SQLite memory index; memory.db live sync will fall back to Markdown. Use Node 22+ or run 'inkos doctor'.`;
  32. }
  33. export async function inspectNodeRuntimePinFiles(root: string): Promise<NodeRuntimePinStatus> {
  34. const missing: string[] = [];
  35. for (const file of SQLITE_MEMORY_PIN_FILES) {
  36. try {
  37. const content = await readFile(join(root, file), "utf-8");
  38. if (content.trim() !== SQLITE_MEMORY_PIN_VERSION) {
  39. missing.push(file);
  40. }
  41. } catch {
  42. missing.push(file);
  43. }
  44. }
  45. if (missing.length === 0) {
  46. return {
  47. ok: true,
  48. detail: `Pinned to Node ${SQLITE_MEMORY_PIN_VERSION} via ${SQLITE_MEMORY_PIN_FILES.join(", ")}.`,
  49. missing,
  50. };
  51. }
  52. return {
  53. ok: false,
  54. detail: `Missing or outdated: ${missing.join(", ")}. Run 'inkos doctor --repair-node-runtime'.`,
  55. missing,
  56. };
  57. }
  58. export async function ensureNodeRuntimePinFiles(root: string): Promise<NodeRuntimePinRepairResult> {
  59. const written: string[] = [];
  60. for (const file of SQLITE_MEMORY_PIN_FILES) {
  61. const path = join(root, file);
  62. let content = "";
  63. try {
  64. content = await readFile(path, "utf-8");
  65. } catch {
  66. content = "";
  67. }
  68. if (content.trim() === SQLITE_MEMORY_PIN_VERSION) {
  69. continue;
  70. }
  71. await writeFile(path, `${SQLITE_MEMORY_PIN_VERSION}\n`, "utf-8");
  72. written.push(file);
  73. }
  74. return {
  75. updated: written.length > 0,
  76. written,
  77. };
  78. }
  79. export function parseNodeMajor(version: string): number {
  80. return parseInt(version.replace(/^v/i, "").split(".")[0] ?? "0", 10);
  81. }
  82. function hasNodeSqliteBuiltin(): boolean {
  83. try {
  84. require("node:sqlite");
  85. return true;
  86. } catch {
  87. return false;
  88. }
  89. }
  90. export function evaluateSqliteMemorySupport(options?: {
  91. readonly nodeVersion?: string;
  92. readonly hasNodeSqlite?: boolean;
  93. }): SqliteMemorySupportResult {
  94. const nodeVersion = options?.nodeVersion ?? process.version;
  95. const major = parseNodeMajor(nodeVersion);
  96. if (major < SQLITE_MEMORY_MIN_NODE_MAJOR) {
  97. return {
  98. ok: false,
  99. detail: `Unavailable on ${nodeVersion}. Long-book memory.db acceleration requires Node ${SQLITE_MEMORY_MIN_NODE_MAJOR}+.`,
  100. };
  101. }
  102. const hasNodeSqlite = options?.hasNodeSqlite ?? hasNodeSqliteBuiltin();
  103. if (!hasNodeSqlite) {
  104. return {
  105. ok: false,
  106. detail: `${nodeVersion} detected, but node:sqlite is unavailable on this runtime. memory.db acceleration will stay disabled.`,
  107. };
  108. }
  109. return {
  110. ok: true,
  111. detail: `Available on ${nodeVersion}.`,
  112. };
  113. }