@devlens/core 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +139 -0
- package/dist/index.d.ts +139 -0
- package/dist/index.js +651 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +645 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +63 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,651 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/network/interceptor.ts
|
|
4
|
+
function matchesPattern(url, patterns) {
|
|
5
|
+
return patterns.some(
|
|
6
|
+
(p) => typeof p === "string" ? url.includes(p) : p.test(url)
|
|
7
|
+
);
|
|
8
|
+
}
|
|
9
|
+
function generateIssueId(method, url, status) {
|
|
10
|
+
return `network:${method}:${url}:${status}`;
|
|
11
|
+
}
|
|
12
|
+
function severityFromStatus(status) {
|
|
13
|
+
if (status >= 500) return "error";
|
|
14
|
+
if (status >= 400) return "warn";
|
|
15
|
+
return "info";
|
|
16
|
+
}
|
|
17
|
+
function buildSuggestion(method, url, status) {
|
|
18
|
+
if (status >= 500)
|
|
19
|
+
return `Server returned ${status} for ${method} ${url} \u2014 check server logs`;
|
|
20
|
+
if (status === 404)
|
|
21
|
+
return `Endpoint ${url} not found \u2014 verify the URL is correct`;
|
|
22
|
+
if (status === 401 || status === 403)
|
|
23
|
+
return `Authentication/authorization failed for ${url} \u2014 check credentials or permissions`;
|
|
24
|
+
if (status >= 400)
|
|
25
|
+
return `Client error ${status} for ${method} ${url} \u2014 check request parameters`;
|
|
26
|
+
return `Request to ${url} completed with status ${status}`;
|
|
27
|
+
}
|
|
28
|
+
function createNetworkInterceptor(engine, config) {
|
|
29
|
+
const resolvedConfig = typeof config === "boolean" || config === void 0 ? {} : config;
|
|
30
|
+
const interceptFetch = resolvedConfig.fetch !== false;
|
|
31
|
+
const interceptXhr = resolvedConfig.xhr !== false;
|
|
32
|
+
const ignoreUrls = resolvedConfig.ignoreUrls ?? [];
|
|
33
|
+
const logSuccess = resolvedConfig.logSuccess ?? false;
|
|
34
|
+
let originalFetch = null;
|
|
35
|
+
let originalXhrOpen = null;
|
|
36
|
+
let originalXhrSend = null;
|
|
37
|
+
function shouldIgnore(url) {
|
|
38
|
+
return ignoreUrls.length > 0 && matchesPattern(url, ignoreUrls);
|
|
39
|
+
}
|
|
40
|
+
function reportNetworkIssue(method, url, status, statusText, duration, error) {
|
|
41
|
+
if (!engine.isEnabled()) return;
|
|
42
|
+
const severity = error ? "error" : severityFromStatus(status);
|
|
43
|
+
if (severity === "info" && !logSuccess) return;
|
|
44
|
+
const issue = {
|
|
45
|
+
id: generateIssueId(method, url, status),
|
|
46
|
+
timestamp: Date.now(),
|
|
47
|
+
severity,
|
|
48
|
+
category: "network",
|
|
49
|
+
message: error ? `${method} ${url} failed: ${error.message}` : `${method} ${url} returned ${status} ${statusText}`,
|
|
50
|
+
details: {
|
|
51
|
+
url,
|
|
52
|
+
method,
|
|
53
|
+
status,
|
|
54
|
+
statusText,
|
|
55
|
+
duration: `${duration}ms`
|
|
56
|
+
},
|
|
57
|
+
suggestion: error ? `Network request to ${url} failed \u2014 check if the server is running and the URL is correct` : buildSuggestion(method, url, status),
|
|
58
|
+
stack: error?.stack,
|
|
59
|
+
source: "NetworkInterceptor"
|
|
60
|
+
};
|
|
61
|
+
engine.report(issue);
|
|
62
|
+
}
|
|
63
|
+
function installFetchInterceptor() {
|
|
64
|
+
if (typeof globalThis.fetch === "undefined") return;
|
|
65
|
+
originalFetch = globalThis.fetch;
|
|
66
|
+
const savedFetch = originalFetch;
|
|
67
|
+
globalThis.fetch = async function devlensFetch(input, init) {
|
|
68
|
+
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
|
|
69
|
+
const method = init?.method ?? "GET";
|
|
70
|
+
if (shouldIgnore(url)) {
|
|
71
|
+
return savedFetch.call(globalThis, input, init);
|
|
72
|
+
}
|
|
73
|
+
const start = performance.now();
|
|
74
|
+
try {
|
|
75
|
+
const response = await savedFetch.call(globalThis, input, init);
|
|
76
|
+
const duration = Math.round(performance.now() - start);
|
|
77
|
+
if (!response.ok || logSuccess) {
|
|
78
|
+
reportNetworkIssue(
|
|
79
|
+
method.toUpperCase(),
|
|
80
|
+
url,
|
|
81
|
+
response.status,
|
|
82
|
+
response.statusText,
|
|
83
|
+
duration
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
return response;
|
|
87
|
+
} catch (err) {
|
|
88
|
+
const duration = Math.round(performance.now() - start);
|
|
89
|
+
reportNetworkIssue(
|
|
90
|
+
method.toUpperCase(),
|
|
91
|
+
url,
|
|
92
|
+
0,
|
|
93
|
+
"Network Error",
|
|
94
|
+
duration,
|
|
95
|
+
err instanceof Error ? err : new Error(String(err))
|
|
96
|
+
);
|
|
97
|
+
throw err;
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
function installXhrInterceptor() {
|
|
102
|
+
if (typeof XMLHttpRequest === "undefined") return;
|
|
103
|
+
originalXhrOpen = XMLHttpRequest.prototype.open;
|
|
104
|
+
originalXhrSend = XMLHttpRequest.prototype.send;
|
|
105
|
+
const savedOpen = originalXhrOpen;
|
|
106
|
+
const savedSend = originalXhrSend;
|
|
107
|
+
XMLHttpRequest.prototype.open = function devlensXhrOpen(method, url, ...rest) {
|
|
108
|
+
this._devlens_method = method;
|
|
109
|
+
this._devlens_url = typeof url === "string" ? url : url.toString();
|
|
110
|
+
return savedOpen.apply(
|
|
111
|
+
this,
|
|
112
|
+
[method, url, ...rest]
|
|
113
|
+
);
|
|
114
|
+
};
|
|
115
|
+
XMLHttpRequest.prototype.send = function devlensXhrSend(body) {
|
|
116
|
+
const xhr = this;
|
|
117
|
+
const method = xhr._devlens_method ?? "GET";
|
|
118
|
+
const url = xhr._devlens_url ?? "";
|
|
119
|
+
if (shouldIgnore(url)) {
|
|
120
|
+
return savedSend.call(this, body);
|
|
121
|
+
}
|
|
122
|
+
const start = performance.now();
|
|
123
|
+
const onLoadEnd = () => {
|
|
124
|
+
const duration = Math.round(performance.now() - start);
|
|
125
|
+
if (xhr.status >= 400 || logSuccess) {
|
|
126
|
+
reportNetworkIssue(
|
|
127
|
+
method.toUpperCase(),
|
|
128
|
+
url,
|
|
129
|
+
xhr.status,
|
|
130
|
+
xhr.statusText,
|
|
131
|
+
duration
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
xhr.removeEventListener("loadend", onLoadEnd);
|
|
135
|
+
xhr.removeEventListener("error", onError);
|
|
136
|
+
};
|
|
137
|
+
const onError = () => {
|
|
138
|
+
const duration = Math.round(performance.now() - start);
|
|
139
|
+
reportNetworkIssue(
|
|
140
|
+
method.toUpperCase(),
|
|
141
|
+
url,
|
|
142
|
+
0,
|
|
143
|
+
"Network Error",
|
|
144
|
+
duration,
|
|
145
|
+
new Error("XMLHttpRequest network error")
|
|
146
|
+
);
|
|
147
|
+
xhr.removeEventListener("loadend", onLoadEnd);
|
|
148
|
+
xhr.removeEventListener("error", onError);
|
|
149
|
+
};
|
|
150
|
+
xhr.addEventListener("loadend", onLoadEnd);
|
|
151
|
+
xhr.addEventListener("error", onError);
|
|
152
|
+
return savedSend.call(this, body);
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
function install() {
|
|
156
|
+
if (typeof window === "undefined") return;
|
|
157
|
+
if (interceptFetch) installFetchInterceptor();
|
|
158
|
+
if (interceptXhr) installXhrInterceptor();
|
|
159
|
+
}
|
|
160
|
+
function uninstall() {
|
|
161
|
+
if (originalFetch) {
|
|
162
|
+
globalThis.fetch = originalFetch;
|
|
163
|
+
originalFetch = null;
|
|
164
|
+
}
|
|
165
|
+
if (originalXhrOpen) {
|
|
166
|
+
XMLHttpRequest.prototype.open = originalXhrOpen;
|
|
167
|
+
originalXhrOpen = null;
|
|
168
|
+
}
|
|
169
|
+
if (originalXhrSend) {
|
|
170
|
+
XMLHttpRequest.prototype.send = originalXhrSend;
|
|
171
|
+
originalXhrSend = null;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return { install, uninstall };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// src/guardian/data-guardian.ts
|
|
178
|
+
var SKIP_PROPERTIES = /* @__PURE__ */ new Set([
|
|
179
|
+
"constructor",
|
|
180
|
+
"prototype",
|
|
181
|
+
"__proto__",
|
|
182
|
+
"toJSON",
|
|
183
|
+
"toString",
|
|
184
|
+
"valueOf",
|
|
185
|
+
"length",
|
|
186
|
+
"then",
|
|
187
|
+
"catch",
|
|
188
|
+
"finally",
|
|
189
|
+
"$$typeof",
|
|
190
|
+
"_owner",
|
|
191
|
+
"_store",
|
|
192
|
+
"ref",
|
|
193
|
+
"key",
|
|
194
|
+
"_self",
|
|
195
|
+
"_source",
|
|
196
|
+
"displayName",
|
|
197
|
+
"nodeType"
|
|
198
|
+
]);
|
|
199
|
+
var NON_PROXYABLE_TYPES = /* @__PURE__ */ new Set([
|
|
200
|
+
"[object Date]",
|
|
201
|
+
"[object RegExp]",
|
|
202
|
+
"[object Map]",
|
|
203
|
+
"[object Set]",
|
|
204
|
+
"[object WeakMap]",
|
|
205
|
+
"[object WeakSet]",
|
|
206
|
+
"[object Error]",
|
|
207
|
+
"[object Promise]",
|
|
208
|
+
"[object ArrayBuffer]",
|
|
209
|
+
"[object DataView]",
|
|
210
|
+
"[object Int8Array]",
|
|
211
|
+
"[object Uint8Array]",
|
|
212
|
+
"[object Float32Array]",
|
|
213
|
+
"[object Float64Array]"
|
|
214
|
+
]);
|
|
215
|
+
function isProxyable(value) {
|
|
216
|
+
if (value === null || value === void 0) return false;
|
|
217
|
+
if (typeof value !== "object" && typeof value !== "function") return false;
|
|
218
|
+
const tag = Object.prototype.toString.call(value);
|
|
219
|
+
return !NON_PROXYABLE_TYPES.has(tag);
|
|
220
|
+
}
|
|
221
|
+
function createDataGuardian(engine, config) {
|
|
222
|
+
const resolvedConfig = typeof config === "boolean" || config === void 0 ? {} : config;
|
|
223
|
+
const maxDepth = resolvedConfig.maxDepth ?? 5;
|
|
224
|
+
const ignorePaths = new Set(resolvedConfig.ignorePaths ?? []);
|
|
225
|
+
const verbose = resolvedConfig.verbose ?? false;
|
|
226
|
+
const proxyCache = /* @__PURE__ */ new WeakMap();
|
|
227
|
+
function shouldIgnorePath(path) {
|
|
228
|
+
if (ignorePaths.size === 0) return false;
|
|
229
|
+
return ignorePaths.has(path);
|
|
230
|
+
}
|
|
231
|
+
function reportAccess(path, value, label) {
|
|
232
|
+
if (!engine.isEnabled()) return;
|
|
233
|
+
if (shouldIgnorePath(path)) return;
|
|
234
|
+
const isNull = value === null;
|
|
235
|
+
const category = isNull ? "null-access" : "undefined-data";
|
|
236
|
+
const prop = path.split(".").pop() ?? path;
|
|
237
|
+
const issue = {
|
|
238
|
+
id: `${category}:${path}`,
|
|
239
|
+
timestamp: Date.now(),
|
|
240
|
+
severity: "warn",
|
|
241
|
+
category,
|
|
242
|
+
message: `Property "${prop}" is ${isNull ? "null" : "undefined"} at path "${path}"`,
|
|
243
|
+
path,
|
|
244
|
+
foundValue: value,
|
|
245
|
+
expectedType: "non-nullish value",
|
|
246
|
+
source: label,
|
|
247
|
+
suggestion: `Check if "${prop}" is loaded/initialized before accessing. Full path: ${path}`
|
|
248
|
+
};
|
|
249
|
+
engine.report(issue);
|
|
250
|
+
}
|
|
251
|
+
function createProxy(target, label, parentPath, depth) {
|
|
252
|
+
if (depth > maxDepth) return target;
|
|
253
|
+
const cached = proxyCache.get(target);
|
|
254
|
+
if (cached) return cached;
|
|
255
|
+
const proxy = new Proxy(target, {
|
|
256
|
+
get(obj, prop, receiver) {
|
|
257
|
+
if (typeof prop === "symbol") {
|
|
258
|
+
return Reflect.get(obj, prop, receiver);
|
|
259
|
+
}
|
|
260
|
+
if (SKIP_PROPERTIES.has(prop)) {
|
|
261
|
+
return Reflect.get(obj, prop, receiver);
|
|
262
|
+
}
|
|
263
|
+
const currentPath = parentPath ? `${parentPath}.${prop}` : prop;
|
|
264
|
+
const value = Reflect.get(obj, prop, receiver);
|
|
265
|
+
if (value === null || value === void 0) {
|
|
266
|
+
const hasOwn = Object.prototype.hasOwnProperty.call(obj, prop) || prop in obj;
|
|
267
|
+
if (hasOwn) {
|
|
268
|
+
reportAccess(currentPath, value, label);
|
|
269
|
+
} else if (verbose) {
|
|
270
|
+
reportAccess(currentPath, value, label);
|
|
271
|
+
}
|
|
272
|
+
return value;
|
|
273
|
+
}
|
|
274
|
+
if (typeof value === "function") {
|
|
275
|
+
return value;
|
|
276
|
+
}
|
|
277
|
+
if (isProxyable(value)) {
|
|
278
|
+
return createProxy(
|
|
279
|
+
value,
|
|
280
|
+
label,
|
|
281
|
+
currentPath,
|
|
282
|
+
depth + 1
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
return value;
|
|
286
|
+
},
|
|
287
|
+
has(obj, prop) {
|
|
288
|
+
return Reflect.has(obj, prop);
|
|
289
|
+
},
|
|
290
|
+
ownKeys(obj) {
|
|
291
|
+
return Reflect.ownKeys(obj);
|
|
292
|
+
},
|
|
293
|
+
getOwnPropertyDescriptor(obj, prop) {
|
|
294
|
+
return Reflect.getOwnPropertyDescriptor(obj, prop);
|
|
295
|
+
},
|
|
296
|
+
set(obj, prop, value, receiver) {
|
|
297
|
+
return Reflect.set(obj, prop, value, receiver);
|
|
298
|
+
},
|
|
299
|
+
deleteProperty(obj, prop) {
|
|
300
|
+
return Reflect.deleteProperty(obj, prop);
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
proxyCache.set(target, proxy);
|
|
304
|
+
return proxy;
|
|
305
|
+
}
|
|
306
|
+
function guard(target, label) {
|
|
307
|
+
if (target === null || target === void 0) {
|
|
308
|
+
const nullLabel = label ?? "unknown";
|
|
309
|
+
const issue = {
|
|
310
|
+
id: `null-access:${nullLabel}:root`,
|
|
311
|
+
timestamp: Date.now(),
|
|
312
|
+
severity: "error",
|
|
313
|
+
category: target === null ? "null-access" : "undefined-data",
|
|
314
|
+
message: `Attempted to guard a ${target === null ? "null" : "undefined"} value (label: "${nullLabel}")`,
|
|
315
|
+
path: nullLabel,
|
|
316
|
+
foundValue: target,
|
|
317
|
+
source: nullLabel,
|
|
318
|
+
suggestion: `The value passed to guard() is ${target === null ? "null" : "undefined"}. Ensure data is loaded before guarding.`
|
|
319
|
+
};
|
|
320
|
+
engine.report(issue);
|
|
321
|
+
return target;
|
|
322
|
+
}
|
|
323
|
+
if (!isProxyable(target)) return target;
|
|
324
|
+
return createProxy(target, label ?? "guarded", "", 0);
|
|
325
|
+
}
|
|
326
|
+
function guardDeep(target, label) {
|
|
327
|
+
if (target === null || target === void 0) return guard(target, label);
|
|
328
|
+
if (!isProxyable(target)) return target;
|
|
329
|
+
const seen = /* @__PURE__ */ new WeakSet();
|
|
330
|
+
const resolvedLabel = label ?? "guarded";
|
|
331
|
+
function walkAndProxy(obj, path, depth) {
|
|
332
|
+
if (depth > maxDepth) return;
|
|
333
|
+
if (seen.has(obj)) return;
|
|
334
|
+
seen.add(obj);
|
|
335
|
+
const keys = Object.keys(obj);
|
|
336
|
+
for (const key of keys) {
|
|
337
|
+
const value = obj[key];
|
|
338
|
+
if (value !== null && value !== void 0 && isProxyable(value)) {
|
|
339
|
+
const childPath = path ? `${path}.${key}` : key;
|
|
340
|
+
createProxy(value, resolvedLabel, childPath, depth + 1);
|
|
341
|
+
walkAndProxy(value, childPath, depth + 1);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
walkAndProxy(target, "", 0);
|
|
346
|
+
return createProxy(target, resolvedLabel, "", 0);
|
|
347
|
+
}
|
|
348
|
+
function unguardAll() {
|
|
349
|
+
}
|
|
350
|
+
return { guard, guardDeep, unguardAll };
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// src/catcher/global-catcher.ts
|
|
354
|
+
function createGlobalCatcher(engine, config) {
|
|
355
|
+
const resolvedConfig = typeof config === "boolean" || config === void 0 ? {} : config;
|
|
356
|
+
const catchWindowErrors = resolvedConfig.windowErrors !== false;
|
|
357
|
+
const catchRejections = resolvedConfig.unhandledRejections !== false;
|
|
358
|
+
const catchConsoleErrors = resolvedConfig.consoleErrors === true;
|
|
359
|
+
let originalConsoleError = null;
|
|
360
|
+
function handleWindowError(event) {
|
|
361
|
+
if (!engine.isEnabled()) return;
|
|
362
|
+
const issue = {
|
|
363
|
+
id: `unhandled-error:${event.message}:${event.filename ?? ""}:${event.lineno ?? 0}`,
|
|
364
|
+
timestamp: Date.now(),
|
|
365
|
+
severity: "error",
|
|
366
|
+
category: "unhandled-error",
|
|
367
|
+
message: event.message || "Unknown error",
|
|
368
|
+
details: {
|
|
369
|
+
filename: event.filename,
|
|
370
|
+
lineno: event.lineno,
|
|
371
|
+
colno: event.colno
|
|
372
|
+
},
|
|
373
|
+
stack: event.error?.stack,
|
|
374
|
+
source: event.filename ? `${event.filename}:${event.lineno}:${event.colno}` : "unknown",
|
|
375
|
+
suggestion: event.filename ? `Unhandled error at ${event.filename}:${event.lineno} \u2014 wrap in try/catch or add error boundary` : "Unhandled error \u2014 add error handling to locate the source"
|
|
376
|
+
};
|
|
377
|
+
engine.report(issue);
|
|
378
|
+
}
|
|
379
|
+
function handleUnhandledRejection(event) {
|
|
380
|
+
if (!engine.isEnabled()) return;
|
|
381
|
+
const reason = event.reason;
|
|
382
|
+
const message = reason instanceof Error ? reason.message : typeof reason === "string" ? reason : "Unknown rejection reason";
|
|
383
|
+
const issue = {
|
|
384
|
+
id: `unhandled-rejection:${message}`,
|
|
385
|
+
timestamp: Date.now(),
|
|
386
|
+
severity: "error",
|
|
387
|
+
category: "unhandled-rejection",
|
|
388
|
+
message: `Unhandled Promise rejection: ${message}`,
|
|
389
|
+
details: {
|
|
390
|
+
reason: reason instanceof Error ? { name: reason.name, message: reason.message } : reason
|
|
391
|
+
},
|
|
392
|
+
stack: reason instanceof Error ? reason.stack : void 0,
|
|
393
|
+
source: "Promise",
|
|
394
|
+
suggestion: "Add .catch() to the Promise or use try/catch with await"
|
|
395
|
+
};
|
|
396
|
+
engine.report(issue);
|
|
397
|
+
}
|
|
398
|
+
function installConsoleInterceptor() {
|
|
399
|
+
if (typeof console === "undefined") return;
|
|
400
|
+
originalConsoleError = console.error;
|
|
401
|
+
const saved = originalConsoleError;
|
|
402
|
+
console.error = function devlensConsoleError(...args) {
|
|
403
|
+
saved.apply(console, args);
|
|
404
|
+
if (!engine.isEnabled()) return;
|
|
405
|
+
const message = args.map(
|
|
406
|
+
(arg) => typeof arg === "string" ? arg : JSON.stringify(arg)
|
|
407
|
+
).join(" ");
|
|
408
|
+
const issue = {
|
|
409
|
+
id: `unhandled-error:console:${message.slice(0, 100)}`,
|
|
410
|
+
timestamp: Date.now(),
|
|
411
|
+
severity: "error",
|
|
412
|
+
category: "unhandled-error",
|
|
413
|
+
message: `console.error: ${message}`,
|
|
414
|
+
details: { args },
|
|
415
|
+
source: "console.error",
|
|
416
|
+
suggestion: "A console.error was detected \u2014 investigate the error above"
|
|
417
|
+
};
|
|
418
|
+
engine.report(issue);
|
|
419
|
+
};
|
|
420
|
+
}
|
|
421
|
+
function install() {
|
|
422
|
+
if (typeof window === "undefined") return;
|
|
423
|
+
if (catchWindowErrors) {
|
|
424
|
+
window.addEventListener("error", handleWindowError);
|
|
425
|
+
}
|
|
426
|
+
if (catchRejections) {
|
|
427
|
+
window.addEventListener(
|
|
428
|
+
"unhandledrejection",
|
|
429
|
+
handleUnhandledRejection
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
if (catchConsoleErrors) {
|
|
433
|
+
installConsoleInterceptor();
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
function uninstall() {
|
|
437
|
+
if (typeof window !== "undefined") {
|
|
438
|
+
window.removeEventListener("error", handleWindowError);
|
|
439
|
+
window.removeEventListener(
|
|
440
|
+
"unhandledrejection",
|
|
441
|
+
handleUnhandledRejection
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
if (originalConsoleError) {
|
|
445
|
+
console.error = originalConsoleError;
|
|
446
|
+
originalConsoleError = null;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
return { install, uninstall };
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// src/reporter/console-reporter.ts
|
|
453
|
+
var CATEGORY_ICONS = {
|
|
454
|
+
"network": "\u{1F310}",
|
|
455
|
+
"null-access": "\u{1F480}",
|
|
456
|
+
"undefined-data": "\u{1F47B}",
|
|
457
|
+
"render-data": "\u{1F3A8}",
|
|
458
|
+
"unhandled-error": "\u{1F4A5}",
|
|
459
|
+
"unhandled-rejection": "\u26A1",
|
|
460
|
+
"type-mismatch": "\u{1F500}"
|
|
461
|
+
};
|
|
462
|
+
var SEVERITY_COLORS = {
|
|
463
|
+
error: "#ff4444",
|
|
464
|
+
warn: "#ffaa00",
|
|
465
|
+
info: "#4488ff"
|
|
466
|
+
};
|
|
467
|
+
var SEVERITY_LABELS = {
|
|
468
|
+
error: "ERROR",
|
|
469
|
+
warn: "WARN",
|
|
470
|
+
info: "INFO"
|
|
471
|
+
};
|
|
472
|
+
function formatDetails(issue) {
|
|
473
|
+
const lines = [];
|
|
474
|
+
if (issue.path) {
|
|
475
|
+
lines.push(` \u251C\u2500 Path: ${issue.path}`);
|
|
476
|
+
}
|
|
477
|
+
if (issue.foundValue !== void 0) {
|
|
478
|
+
lines.push(
|
|
479
|
+
` \u251C\u2500 Value: ${issue.foundValue === null ? "null" : String(issue.foundValue)}`
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
if (issue.details) {
|
|
483
|
+
const entries = Object.entries(issue.details);
|
|
484
|
+
for (const [key, value] of entries) {
|
|
485
|
+
lines.push(` \u251C\u2500 ${key}: ${typeof value === "string" ? value : JSON.stringify(value)}`);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
if (issue.source) {
|
|
489
|
+
lines.push(` \u251C\u2500 Source: ${issue.source}`);
|
|
490
|
+
}
|
|
491
|
+
if (issue.suggestion) {
|
|
492
|
+
lines.push(` \u251C\u2500 Suggestion: ${issue.suggestion}`);
|
|
493
|
+
}
|
|
494
|
+
if (issue.stack) {
|
|
495
|
+
const stackPreview = issue.stack.split("\n").slice(0, 3).join("\n");
|
|
496
|
+
lines.push(` \u2514\u2500 Stack:
|
|
497
|
+
${stackPreview}`);
|
|
498
|
+
} else if (lines.length > 0) {
|
|
499
|
+
const lastIdx = lines.length - 1;
|
|
500
|
+
lines[lastIdx] = lines[lastIdx].replace("\u251C\u2500", "\u2514\u2500");
|
|
501
|
+
}
|
|
502
|
+
return lines;
|
|
503
|
+
}
|
|
504
|
+
function createConsoleReporter() {
|
|
505
|
+
function report(issue) {
|
|
506
|
+
const icon = CATEGORY_ICONS[issue.category] ?? "\u{1F50D}";
|
|
507
|
+
const color = SEVERITY_COLORS[issue.severity];
|
|
508
|
+
const label = SEVERITY_LABELS[issue.severity];
|
|
509
|
+
const header = `${icon} DevLens [${label}] ${issue.category}: ${issue.message}`;
|
|
510
|
+
const details = formatDetails(issue);
|
|
511
|
+
const consoleFn = issue.severity === "error" ? console.error : issue.severity === "warn" ? console.warn : console.log;
|
|
512
|
+
console.groupCollapsed(
|
|
513
|
+
`%c${header}`,
|
|
514
|
+
`color: ${color}; font-weight: bold;`
|
|
515
|
+
);
|
|
516
|
+
for (const line of details) {
|
|
517
|
+
consoleFn(line);
|
|
518
|
+
}
|
|
519
|
+
consoleFn(
|
|
520
|
+
`%cTimestamp: ${new Date(issue.timestamp).toISOString()}`,
|
|
521
|
+
"color: #888;"
|
|
522
|
+
);
|
|
523
|
+
console.groupEnd();
|
|
524
|
+
}
|
|
525
|
+
function reportBatch(issues) {
|
|
526
|
+
if (issues.length === 0) return;
|
|
527
|
+
console.groupCollapsed(
|
|
528
|
+
`%c\u{1F50D} DevLens \u2014 ${issues.length} issue(s) detected`,
|
|
529
|
+
"color: #ff4444; font-weight: bold; font-size: 12px;"
|
|
530
|
+
);
|
|
531
|
+
for (const issue of issues) {
|
|
532
|
+
report(issue);
|
|
533
|
+
}
|
|
534
|
+
console.groupEnd();
|
|
535
|
+
}
|
|
536
|
+
function clear() {
|
|
537
|
+
console.clear();
|
|
538
|
+
}
|
|
539
|
+
return { report, reportBatch, clear };
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
// src/engine/detection-engine.ts
|
|
543
|
+
var SEVERITY_RANK = {
|
|
544
|
+
info: 0,
|
|
545
|
+
warn: 1,
|
|
546
|
+
error: 2
|
|
547
|
+
};
|
|
548
|
+
function matchesAnyPattern(value, patterns) {
|
|
549
|
+
if (!value || patterns.length === 0) return false;
|
|
550
|
+
return patterns.some(
|
|
551
|
+
(p) => typeof p === "string" ? value.includes(p) : p.test(value)
|
|
552
|
+
);
|
|
553
|
+
}
|
|
554
|
+
function createDetectionEngine(config = {}) {
|
|
555
|
+
const resolvedConfig = {
|
|
556
|
+
enabled: config.enabled ?? true,
|
|
557
|
+
minSeverity: config.minSeverity ?? "info",
|
|
558
|
+
throttleMs: config.throttleMs ?? 1e3,
|
|
559
|
+
maxIssues: config.maxIssues ?? 100,
|
|
560
|
+
...config
|
|
561
|
+
};
|
|
562
|
+
const reporter = resolvedConfig.reporter ?? createConsoleReporter();
|
|
563
|
+
const issues = [];
|
|
564
|
+
const subscribers = /* @__PURE__ */ new Set();
|
|
565
|
+
const lastReportedAt = /* @__PURE__ */ new Map();
|
|
566
|
+
function isEnabled() {
|
|
567
|
+
if (!resolvedConfig.enabled) return false;
|
|
568
|
+
if (typeof window === "undefined") return false;
|
|
569
|
+
try {
|
|
570
|
+
if (typeof process !== "undefined" && process.env?.NODE_ENV === "production") {
|
|
571
|
+
return false;
|
|
572
|
+
}
|
|
573
|
+
} catch {
|
|
574
|
+
}
|
|
575
|
+
return true;
|
|
576
|
+
}
|
|
577
|
+
function meetsMinSeverity(severity) {
|
|
578
|
+
return SEVERITY_RANK[severity] >= SEVERITY_RANK[resolvedConfig.minSeverity];
|
|
579
|
+
}
|
|
580
|
+
function isThrottled(id, now) {
|
|
581
|
+
const lastTime = lastReportedAt.get(id);
|
|
582
|
+
if (lastTime === void 0) return false;
|
|
583
|
+
return now - lastTime < resolvedConfig.throttleMs;
|
|
584
|
+
}
|
|
585
|
+
function isIgnored(issue) {
|
|
586
|
+
const ignore = resolvedConfig.ignore;
|
|
587
|
+
if (!ignore) return false;
|
|
588
|
+
if (ignore.urls && matchesAnyPattern(
|
|
589
|
+
issue.details?.url,
|
|
590
|
+
ignore.urls
|
|
591
|
+
)) {
|
|
592
|
+
return true;
|
|
593
|
+
}
|
|
594
|
+
if (ignore.paths && matchesAnyPattern(issue.path, ignore.paths)) {
|
|
595
|
+
return true;
|
|
596
|
+
}
|
|
597
|
+
if (ignore.messages && matchesAnyPattern(issue.message, ignore.messages)) {
|
|
598
|
+
return true;
|
|
599
|
+
}
|
|
600
|
+
return false;
|
|
601
|
+
}
|
|
602
|
+
function addToBuffer(issue) {
|
|
603
|
+
issues.push(issue);
|
|
604
|
+
if (issues.length > resolvedConfig.maxIssues) {
|
|
605
|
+
issues.shift();
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
function report(issue) {
|
|
609
|
+
if (!isEnabled()) return;
|
|
610
|
+
if (!meetsMinSeverity(issue.severity)) return;
|
|
611
|
+
const now = Date.now();
|
|
612
|
+
if (isThrottled(issue.id, now)) return;
|
|
613
|
+
if (isIgnored(issue)) return;
|
|
614
|
+
lastReportedAt.set(issue.id, now);
|
|
615
|
+
addToBuffer(issue);
|
|
616
|
+
reporter.report(issue);
|
|
617
|
+
for (const callback of subscribers) {
|
|
618
|
+
try {
|
|
619
|
+
callback(issue);
|
|
620
|
+
} catch {
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
function getConfig() {
|
|
625
|
+
return Object.freeze({ ...resolvedConfig });
|
|
626
|
+
}
|
|
627
|
+
function getIssues() {
|
|
628
|
+
return [...issues];
|
|
629
|
+
}
|
|
630
|
+
function subscribe(callback) {
|
|
631
|
+
subscribers.add(callback);
|
|
632
|
+
return () => {
|
|
633
|
+
subscribers.delete(callback);
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
return {
|
|
637
|
+
report,
|
|
638
|
+
getConfig,
|
|
639
|
+
getIssues,
|
|
640
|
+
subscribe,
|
|
641
|
+
isEnabled
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
exports.createConsoleReporter = createConsoleReporter;
|
|
646
|
+
exports.createDataGuardian = createDataGuardian;
|
|
647
|
+
exports.createDetectionEngine = createDetectionEngine;
|
|
648
|
+
exports.createGlobalCatcher = createGlobalCatcher;
|
|
649
|
+
exports.createNetworkInterceptor = createNetworkInterceptor;
|
|
650
|
+
//# sourceMappingURL=index.js.map
|
|
651
|
+
//# sourceMappingURL=index.js.map
|