-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdebug.ts
More file actions
148 lines (133 loc) · 4.13 KB
/
debug.ts
File metadata and controls
148 lines (133 loc) · 4.13 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
export type DebugCategory = 'schema' | 'registry' | 'expression' | 'action' | 'plugin' | 'render' | 'dashboard';
/**
* Fine-grained debug flags parsed from URL parameters.
*
* @example
* ```
* ?__debug → { enabled: true }
* ?__debug_schema → { enabled: true, schema: true }
* ?__debug_perf&__debug_data → { enabled: true, perf: true, data: true }
* ```
*/
export interface DebugFlags {
/** Master switch — true when any debug parameter is present */
enabled: boolean;
schema?: boolean;
perf?: boolean;
data?: boolean;
expr?: boolean;
events?: boolean;
registry?: boolean;
}
const DEBUG_PARAM_PREFIX = '__debug';
/**
* Parse debug flags from a URL search string (e.g. `?__debug&__debug_schema`).
* SSR-safe — returns `{ enabled: false }` when `window` is unavailable.
*
* @param search — Optional search string. Defaults to `window.location.search` when available.
*/
export function parseDebugFlags(search?: string): DebugFlags {
let qs: string | undefined = search;
if (qs === undefined) {
try {
qs = typeof window !== 'undefined' ? window.location.search : '';
} catch {
qs = '';
}
}
const params = new URLSearchParams(qs);
const hasMain = params.has(DEBUG_PARAM_PREFIX);
const schema = params.has(`${DEBUG_PARAM_PREFIX}_schema`);
const perf = params.has(`${DEBUG_PARAM_PREFIX}_perf`);
const data = params.has(`${DEBUG_PARAM_PREFIX}_data`);
const expr = params.has(`${DEBUG_PARAM_PREFIX}_expr`);
const events = params.has(`${DEBUG_PARAM_PREFIX}_events`);
const registry = params.has(`${DEBUG_PARAM_PREFIX}_registry`);
const anySub = schema || perf || data || expr || events || registry;
const enabled = hasMain || anySub;
return {
enabled,
...(schema && { schema }),
...(perf && { perf }),
...(data && { data }),
...(expr && { expr }),
...(events && { events }),
...(registry && { registry }),
};
}
/**
* Check whether debug mode is enabled.
*
* Resolution order (first truthy wins):
* 1. URL parameter `?__debug` (browser only)
* 2. `globalThis.OBJECTUI_DEBUG`
* 3. `process.env.OBJECTUI_DEBUG`
*/
export function isDebugEnabled(): boolean {
try {
// 1. URL parameter (browser only, SSR-safe)
if (typeof window !== 'undefined') {
try {
const flags = parseDebugFlags(window.location.search);
if (flags.enabled) return true;
} catch { /* ignore */ }
}
// 2. globalThis flag
const g = typeof globalThis !== 'undefined' && (globalThis as any).OBJECTUI_DEBUG;
if (g === true || g === 'true') return true;
// 3. process.env
const proc = (globalThis as any).process;
if (proc?.env?.OBJECTUI_DEBUG === 'true') return true;
return false;
} catch {
return false;
}
}
/**
* Log a debug message when OBJECTUI_DEBUG is enabled.
* No-op in production or when debug mode is off.
*
* @example
* ```ts
* // Enable debug mode
* globalThis.OBJECTUI_DEBUG = true;
*
* debugLog('schema', 'Resolving component', { type: 'Button' });
* // [ObjectUI Debug][schema] Resolving component { type: 'Button' }
* ```
*/
export function debugLog(category: DebugCategory, message: string, data?: unknown): void {
if (!isDebugEnabled()) return;
if (data !== undefined) {
console.log(`[ObjectUI Debug][${category}] ${message}`, data);
} else {
console.log(`[ObjectUI Debug][${category}] ${message}`);
}
}
const timers = new Map<string, number>();
/**
* Start a debug timer. Pair with {@link debugTimeEnd}.
*/
export function debugTime(label: string): void {
if (!isDebugEnabled()) return;
timers.set(label, performance.now());
}
/**
* End a debug timer and log the elapsed time.
*/
export function debugTimeEnd(label: string): void {
if (!isDebugEnabled()) return;
const start = timers.get(label);
if (start !== undefined) {
const elapsed = (performance.now() - start).toFixed(2);
console.log(`[ObjectUI Debug][perf] ${label}: ${elapsed}ms`);
timers.delete(label);
}
}