| 15 | |
| 16 | // DEBUG: Safe deep-inspect that handles circular refs + truncates big strings |
| 17 | function inspect(value: unknown, maxStringLen = 500, maxDepth = 6): string { |
| 18 | const seen = new WeakSet(); |
| 19 | function walk(v: unknown, depth: number): unknown { |
| 20 | if (depth > maxDepth) return "[max depth]"; |
| 21 | if (v === null || v === undefined) return v; |
| 22 | if (typeof v === "string") |
| 23 | return v.length > maxStringLen |
| 24 | ? v.substring(0, maxStringLen) + `...[${v.length} chars]` |
| 25 | : v; |
| 26 | if (typeof v === "number" || typeof v === "boolean") return v; |
| 27 | if (typeof v === "function") return `[function ${v.name || "anonymous"}]`; |
| 28 | if (typeof v === "symbol") return v.toString(); |
| 29 | if (typeof v === "bigint") return v.toString(); |
| 30 | if (v instanceof Date) return v.toISOString(); |
| 31 | if (v instanceof RegExp) return v.toString(); |
| 32 | if (v instanceof Error) |
| 33 | return { name: v.name, message: v.message, stack: v.stack }; |
| 34 | if (typeof v === "object") { |
| 35 | if (seen.has(v as object)) return "[circular]"; |
| 36 | seen.add(v as object); |
| 37 | if (Array.isArray(v)) |
| 38 | return v.length > 50 |
| 39 | ? [ |
| 40 | ...v.slice(0, 50).map((x) => walk(x, depth + 1)), |
| 41 | `...[${v.length} items]`, |
| 42 | ] |
| 43 | : v.map((x) => walk(x, depth + 1)); |
| 44 | const out: Record<string, unknown> = {}; |
| 45 | for (const key of Object.keys(v as Record<string, unknown>)) { |
| 46 | out[key] = walk((v as Record<string, unknown>)[key], depth + 1); |
| 47 | } |
| 48 | return out; |
| 49 | } |
| 50 | return String(v); |
| 51 | } |
| 52 | try { |
| 53 | return JSON.stringify(walk(value, 0), null, 2); |
| 54 | } catch { |
| 55 | return `[inspect error: ${typeof value}]`; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | export const AtomicHooksPlugin: Plugin = async ({ $, directory }) => { |
| 60 | const ATOMIC = "atomic"; |