glitchgrab 1.68.0 → 1.68.2

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/server.mjs CHANGED
@@ -1,6 +1,16 @@
1
1
  // src/server.ts
2
2
  import { createHmac } from "crypto";
3
3
 
4
+ // src/redact.ts
5
+ var EMAIL_RE = /[\w.+-]+@[\w-]+\.[\w.-]+/g;
6
+ var JWT_RE = /\beyJ[\w-]{8,}\.[\w-]{8,}\.[\w-]{8,}\b/g;
7
+ var BEARER_RE = /\b(bearer\s+)[\w-.~+/]{12,}=*/gi;
8
+ var KEY_PREFIX_RE = /\b(?:(?:sk|rk|pk)_(?:live|test)_[A-Za-z0-9]{10,}|sk-[A-Za-z0-9_-]{16,}|gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|AKIA[0-9A-Z]{16}|xox[abposr]-[A-Za-z0-9-]{10,}|gg[cswi]?_[A-Za-z0-9]{20,})\b/g;
9
+ var URL_CREDENTIALS_RE = /\b([a-z][a-z0-9+.-]*:\/\/[^\s:@/]+):[^\s@/]+@/gi;
10
+ function redactString(value) {
11
+ return value.replace(URL_CREDENTIALS_RE, "$1:[REDACTED]@").replace(KEY_PREFIX_RE, "[REDACTED]").replace(JWT_RE, "[REDACTED]").replace(BEARER_RE, "$1[REDACTED]").replace(EMAIL_RE, "[REDACTED_EMAIL]");
12
+ }
13
+
4
14
  // src/runtime.ts
5
15
  var pageLoadedAt = Date.now();
6
16
 
@@ -63,6 +73,71 @@ async function sendReport(payload, baseUrl) {
63
73
  }
64
74
  }
65
75
 
76
+ // src/dedup.ts
77
+ var DEFAULT_WINDOW_MS = 5 * 60 * 1e3;
78
+ var MAX_ENTRIES = 100;
79
+ var seen = /* @__PURE__ */ new Map();
80
+ function hashString(input) {
81
+ let h1 = 3735928559 ^ 0;
82
+ let h2 = 1103547991 ^ 0;
83
+ for (let i = 0; i < input.length; i++) {
84
+ const ch = input.charCodeAt(i);
85
+ h1 = Math.imul(h1 ^ ch, 2654435761);
86
+ h2 = Math.imul(h2 ^ ch, 1597334677);
87
+ }
88
+ h1 = Math.imul(h1 ^ h1 >>> 16, 2246822507) ^ Math.imul(h2 ^ h2 >>> 13, 3266489909);
89
+ h2 = Math.imul(h2 ^ h2 >>> 16, 2246822507) ^ Math.imul(h1 ^ h1 >>> 13, 3266489909);
90
+ return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36);
91
+ }
92
+ function topStackFrame(stack) {
93
+ if (!stack) return "";
94
+ const lines = stack.split("\n");
95
+ for (const line of lines) {
96
+ const t = line.trim();
97
+ if (t.startsWith("at ") || t.includes("@")) {
98
+ return t;
99
+ }
100
+ }
101
+ return "";
102
+ }
103
+ function stripUrlQuery(url) {
104
+ if (!url) return "";
105
+ try {
106
+ const u = new URL(url);
107
+ return `${u.origin}${u.pathname}`;
108
+ } catch (e) {
109
+ const q = url.indexOf("?");
110
+ return q === -1 ? url : url.slice(0, q);
111
+ }
112
+ }
113
+ function computeSignature(params) {
114
+ var _a, _b, _c, _d;
115
+ const msg = (_b = (_a = params.errorMessage) == null ? void 0 : _a.trim()) != null ? _b : "";
116
+ const page = stripUrlQuery(params.pageUrl);
117
+ const frame = topStackFrame(params.errorStack);
118
+ const digest = (_d = (_c = params.digest) == null ? void 0 : _c.trim()) != null ? _d : "";
119
+ return hashString(digest ? `${msg}|${page}|${frame}|${digest}` : `${msg}|${page}|${frame}`);
120
+ }
121
+ function shouldSkipDuplicate(signature, windowMs = DEFAULT_WINDOW_MS, now = Date.now()) {
122
+ const last = seen.get(signature);
123
+ if (last !== void 0 && now - last < windowMs) {
124
+ return true;
125
+ }
126
+ seen.set(signature, now);
127
+ if (seen.size > MAX_ENTRIES) {
128
+ const cutoff = now - windowMs;
129
+ for (const [sig, ts] of seen) {
130
+ if (ts < cutoff) seen.delete(sig);
131
+ }
132
+ if (seen.size > MAX_ENTRIES) {
133
+ const excess = seen.size - MAX_ENTRIES;
134
+ const keys = Array.from(seen.keys()).slice(0, excess);
135
+ for (const k of keys) seen.delete(k);
136
+ }
137
+ }
138
+ return false;
139
+ }
140
+
66
141
  // src/server.ts
67
142
  var MAX_LOG_CHARS = 2e5;
68
143
  var DEV_BLOCKED_MESSAGE = "development";
@@ -157,7 +232,9 @@ async function reportServerError(error, options = {}) {
157
232
  if (!token) return null;
158
233
  const enableInDevelopment = (_d = (_c = options.enableInDevelopment) != null ? _c : defaults.enableInDevelopment) != null ? _d : false;
159
234
  if (!enableInDevelopment && readEnv("NODE_ENV") === DEV_BLOCKED_MESSAGE) return null;
160
- const { message, stack } = describeError(error);
235
+ const described = describeError(error);
236
+ const message = redactString(described.message) || "Unknown server error";
237
+ const stack = described.stack ? redactString(described.stack) : void 0;
161
238
  const context = (_e = options.context) != null ? _e : defaults.context;
162
239
  const reporter = { ...defaults.reporter, ...options.reporter };
163
240
  const payload = {
@@ -210,12 +287,15 @@ function captureServerErrors(options = {}) {
210
287
  }
211
288
  };
212
289
  proc.on("uncaughtExceptionMonitor", onException);
290
+ const consoleLevels = options.console === void 0 ? DEFAULT_SERVER_CONSOLE : options.console;
291
+ const restoreConsole = consoleLevels && consoleLevels.length ? captureServerConsole({ ...options, levels: consoleLevels, context: options.context }) : null;
213
292
  const captureRejections = (_a = options.captureUnhandledRejections) != null ? _a : true;
214
293
  if (captureRejections) proc.on("unhandledRejection", onRejection);
215
294
  return () => {
216
295
  var _a2, _b;
217
296
  try {
218
297
  (_a2 = proc.off) == null ? void 0 : _a2.call(proc, "uncaughtExceptionMonitor", onException);
298
+ restoreConsole == null ? void 0 : restoreConsole();
219
299
  if (captureRejections) {
220
300
  (_b = proc.off) == null ? void 0 : _b.call(proc, "unhandledRejection", onRejection);
221
301
  }
@@ -227,6 +307,155 @@ function captureServerErrors(options = {}) {
227
307
  };
228
308
  }
229
309
  }
310
+ var reportedServerErrors = /* @__PURE__ */ new WeakSet();
311
+ function markServerErrorReported(error) {
312
+ try {
313
+ if (error && typeof error === "object") reportedServerErrors.add(error);
314
+ } catch (e) {
315
+ }
316
+ }
317
+ function isClientAbort(error) {
318
+ try {
319
+ const e = error;
320
+ if ((e == null ? void 0 : e.name) === "ResponseAborted") return true;
321
+ return (e == null ? void 0 : e.code) === "ECONNRESET" && typeof (e == null ? void 0 : e.message) === "string" && e.message.trim() === "aborted";
322
+ } catch (e) {
323
+ return false;
324
+ }
325
+ }
326
+ function isClientAbortLine(text) {
327
+ return /\bError: aborted\b/.test(text) && /\bECONNRESET\b/.test(text);
328
+ }
329
+ function keepAlive(work) {
330
+ var _a, _b, _c;
331
+ try {
332
+ const store = globalThis[/* @__PURE__ */ Symbol.for("@vercel/request-context")];
333
+ (_c = (_b = (_a = store == null ? void 0 : store.get) == null ? void 0 : _a.call(store)) == null ? void 0 : _b.waitUntil) == null ? void 0 : _c.call(_b, work.catch(() => {
334
+ }));
335
+ } catch (e) {
336
+ }
337
+ }
338
+ async function reportRequestError(error, request, context = {}, options = {}) {
339
+ var _a, _b, _c, _d, _e, _f, _g, _h;
340
+ try {
341
+ if (isClientAbort(error)) return null;
342
+ markServerErrorReported(error);
343
+ const method = ((_a = request == null ? void 0 : request.method) != null ? _a : "GET").toUpperCase();
344
+ const route = (_d = (_c = context.routePath) != null ? _c : (_b = request == null ? void 0 : request.path) == null ? void 0 : _b.split("?")[0]) != null ? _d : "unknown";
345
+ const digest = typeof (error == null ? void 0 : error.digest) === "string" ? error.digest : void 0;
346
+ return await reportServerError(error, {
347
+ ...options,
348
+ context: (_e = options.context) != null ? _e : `route${route.startsWith("/") ? "" : "/"}${route}`,
349
+ description: (_h = options.description) != null ? _h : `${method} ${(_g = (_f = request == null ? void 0 : request.path) == null ? void 0 : _f.split("?")[0]) != null ? _g : route} failed on the server${context.routeType ? ` (${context.routeType}${context.routerKind ? `, ${context.routerKind}` : ""})` : ""}.`,
350
+ metadata: {
351
+ ...options.metadata,
352
+ capturedBy: "onRequestError",
353
+ requestMethod: method,
354
+ ...(request == null ? void 0 : request.path) ? { requestPath: request.path.split("?")[0] } : {},
355
+ ...context.routePath ? { routePath: context.routePath } : {},
356
+ ...context.routeType ? { routeType: context.routeType } : {},
357
+ ...context.routerKind ? { routerKind: context.routerKind } : {},
358
+ ...context.renderSource ? { renderSource: context.renderSource } : {},
359
+ ...digest ? { digest } : {}
360
+ }
361
+ });
362
+ } catch (e) {
363
+ return null;
364
+ }
365
+ }
366
+ var DEFAULT_SERVER_CONSOLE = ["error", "warn"];
367
+ var SERVER_CONSOLE_NOISE = [/^\[glitchgrab\]/i, /ExperimentalWarning/, /DeprecationWarning/];
368
+ var SERVER_CONSOLE_MAX = 2e3;
369
+ var SERVER_ERROR_ARG_DELAY_MS = 1500;
370
+ function consoleText(args) {
371
+ try {
372
+ return args.map((a) => {
373
+ if (typeof a === "string") return a;
374
+ if (a instanceof Error) return a.message;
375
+ try {
376
+ return JSON.stringify(a);
377
+ } catch (e) {
378
+ return String(a);
379
+ }
380
+ }).join(" ").slice(0, SERVER_CONSOLE_MAX);
381
+ } catch (e) {
382
+ return "";
383
+ }
384
+ }
385
+ var restoreServerConsole = null;
386
+ function captureServerConsole(options = {}) {
387
+ var _a;
388
+ try {
389
+ if (restoreServerConsole) restoreServerConsole();
390
+ const levels = new Set((_a = options.levels) != null ? _a : ["error"]);
391
+ const original = { error: console.error, warn: console.warn };
392
+ let filing = false;
393
+ const file = (level, args) => {
394
+ var _a2, _b, _c;
395
+ try {
396
+ const err = args.find((a) => a instanceof Error);
397
+ if (err && (reportedServerErrors.has(err) || isClientAbort(err))) return Promise.resolve();
398
+ const text = redactString(consoleText(args)).trim();
399
+ if (!text) return Promise.resolve();
400
+ if (isClientAbortLine(text)) return Promise.resolve();
401
+ if (SERVER_CONSOLE_NOISE.some((re) => re.test(text))) return Promise.resolve();
402
+ if ((_a2 = options.ignore) == null ? void 0 : _a2.some((p) => p instanceof RegExp ? p.test(text) : text.includes(p))) {
403
+ return Promise.resolve();
404
+ }
405
+ const context = (_c = (_b = options.context) != null ? _b : defaults.context) != null ? _c : `console.${level}`;
406
+ const errorMessage = `console.${level}: ${err && args.length === 1 ? err.message : text}`;
407
+ const sig = computeSignature({ errorMessage, pageUrl: contextUrl(context), errorStack: err == null ? void 0 : err.stack });
408
+ if (shouldSkipDuplicate(sig)) return Promise.resolve();
409
+ if (err) markServerErrorReported(err);
410
+ filing = true;
411
+ try {
412
+ return reportServerError(err && args.length === 1 ? err : errorMessage, {
413
+ ...options,
414
+ context,
415
+ metadata: {
416
+ ...options.metadata,
417
+ capturedBy: `console.${level}`,
418
+ autoKind: "console",
419
+ consoleLevel: level
420
+ },
421
+ ...err && args.length > 1 ? { description: text } : {}
422
+ });
423
+ } finally {
424
+ filing = false;
425
+ }
426
+ } catch (e) {
427
+ return Promise.resolve();
428
+ }
429
+ };
430
+ const wrap = (level) => function(...args) {
431
+ original[level].apply(console, args);
432
+ if (filing || !levels.has(level)) return;
433
+ try {
434
+ keepAlive(
435
+ args.some((a) => a instanceof Error) ? new Promise((resolve) => setTimeout(resolve, SERVER_ERROR_ARG_DELAY_MS)).then(
436
+ () => file(level, args)
437
+ ) : file(level, args)
438
+ );
439
+ } catch (e) {
440
+ }
441
+ };
442
+ console.error = wrap("error");
443
+ console.warn = wrap("warn");
444
+ const restore = () => {
445
+ try {
446
+ console.error = original.error;
447
+ console.warn = original.warn;
448
+ } catch (e) {
449
+ }
450
+ if (restoreServerConsole === restore) restoreServerConsole = null;
451
+ };
452
+ restoreServerConsole = restore;
453
+ return restore;
454
+ } catch (e) {
455
+ return () => {
456
+ };
457
+ }
458
+ }
230
459
  function signSession(options) {
231
460
  var _a, _b, _c;
232
461
  const userId = options == null ? void 0 : options.userId;
@@ -1146,6 +1375,7 @@ export {
1146
1375
  addLineCaller,
1147
1376
  addRingNumber,
1148
1377
  cancelBooking,
1378
+ captureServerConsole,
1149
1379
  captureServerErrors,
1150
1380
  changeLineNumber,
1151
1381
  configureServerReporter,
@@ -1176,6 +1406,7 @@ export {
1176
1406
  removeLineCaller,
1177
1407
  removeRingNumber,
1178
1408
  removeWebhook,
1409
+ reportRequestError,
1179
1410
  reportServerError,
1180
1411
  rescheduleBooking,
1181
1412
  resetServerReporter,