-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathinspect.ts
More file actions
57 lines (47 loc) · 1.28 KB
/
inspect.ts
File metadata and controls
57 lines (47 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// compat/runtime/inspect.ts
// Object inspection abstraction for apps
declare const Deno: {
inspect(value: unknown, options?: { depth?: number; colors?: boolean }): string;
} | undefined;
const isDeno = typeof Deno !== "undefined";
// Cache the util module for Node.js
let nodeUtil: { inspect: (value: unknown, options?: object) => string } | null = null;
const getNodeUtil = async () => {
if (nodeUtil) return nodeUtil;
try {
nodeUtil = await import("node:util");
return nodeUtil;
} catch {
return null;
}
};
// Preload util module in Node.js
if (!isDeno) {
getNodeUtil();
}
export const inspect = (
value: unknown,
options?: { depth?: number; colors?: boolean },
): string => {
if (isDeno) {
return Deno!.inspect(value, options);
}
// Node.js / Bun - use cached module if available
if (nodeUtil?.inspect) {
return nodeUtil.inspect(value, {
depth: options?.depth ?? 4,
colors: options?.colors ?? false,
});
}
// Fallback for cases where module isn't loaded yet
// Handle errors specially
if (value instanceof Error) {
return `${value.name}: ${value.message}${value.stack ? `\n${value.stack}` : ""}`;
}
// Ultimate fallback
try {
return JSON.stringify(value, null, 2);
} catch {
return String(value);
}
};