Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 59x 1x 1x 3x 3x 3x 1x 1x 3x 3x 4x 1x 1x 3x 3x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 82x 82x 82x 82x 28x 28x 82x 82x 82x 82x 82x 82x 82x 82x 82x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 3x 3x 3x 3x 79x 79x 82x 1x 1x 87x 87x 87x 1x 1x 1x 86x 86x 86x 86x 86x 87x 82x 3x 3x 3x 79x 79x 79x 79x 82x 51x 51x 33x 9x 33x 24x 24x 24x 24x 33x 33x 33x 18x 18x 18x 51x 82x 28x 28x 28x 82x 50x 50x 50x 50x 50x 87x 1x 1x 1x 87x 1x 1x 86x 6x 6x 6x 6x 82x 5x 1x 1x 5x 86x 1x 1x 11x 11x 11x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 11x 11x 11x 1x 1x 86x 83x 83x 83x 3x 86x 1x 1x 12x 12x 12x 3x 3x 3x 11x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 8x 8x 8x 11x 1x 1x 1x 11x 9x 9x 12x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 2x 1x 1x 19x 19x 1x 1x 61x 3x 3x 3x 61x 3x 3x 3x 61x 3x 3x 61x 61x 3x 3x 61x 3x 3x 61x 61x 61x 61x 61x 61x 1x | import type { SentryEvent, ErrorEvent, CountEvent } from '../types';
import { EventBus } from './EventBus';
import { now } from '../utils/helper';
import { logDebug } from '../utils/logger';
interface DedupeEntry {
count: number;
lastTime: number;
firstReported: boolean;
}
export class EventQueue {
private queue: SentryEvent[] = [];
private maxSize: number;
private flushInterval: number;
private timer: ReturnType<typeof setTimeout> | null = null;
private eventBus: EventBus;
private flushCallback: (events: SentryEvent[]) => Promise<void>;
private syncFlushCallback: (events: SentryEvent[]) => void;
private lastFlushTime: number = 0;
private isFlushing: boolean = false;
private onVisibilityChange?: () => void;
private onBeforeUnload?: () => void;
private onPagehide?: () => void;
private dedupeMap: Map<string, DedupeEntry> = new Map();
private pendingCounts: Map<string, { count: number; ts_start: number; ts_end: number }> = new Map();
private errorRateWindow: { [key: string]: number[] } = {};
private paused: boolean = false;
private pauseTimer: ReturnType<typeof setTimeout> | null = null;
constructor(
maxSize: number,
flushInterval: number,
eventBus: EventBus,
flushCallback: (events: SentryEvent[]) => Promise<void>,
syncFlushCallback: (events: SentryEvent[]) => void
) {
this.maxSize = maxSize;
this.flushInterval = flushInterval;
this.eventBus = eventBus;
this.flushCallback = flushCallback;
this.syncFlushCallback = syncFlushCallback;
}
start(): void {
this.startTimer();
this.setupVisibilityListener();
}
private startTimer(): void {
if (this.timer) return;
this.timer = setInterval(() => {
if (this.queue.length > 0 || this.pendingCounts.size > 0) {
this.flush();
}
}, this.flushInterval);
}
private setupVisibilityListener(): void {
this.onVisibilityChange = () => {
if (document.hidden && (this.queue.length > 0 || this.pendingCounts.size > 0)) {
this.flushSync();
}
};
if (typeof document !== 'undefined') {
document.addEventListener('visibilitychange', this.onVisibilityChange);
}
this.onBeforeUnload = () => {
if (this.queue.length > 0 || this.pendingCounts.size > 0) {
this.flushSync();
}
};
this.onPagehide = () => {
if (this.queue.length > 0 || this.pendingCounts.size > 0) {
this.flushSync();
}
};
if (typeof window !== 'undefined') {
window.addEventListener('beforeunload', this.onBeforeUnload);
window.addEventListener('pagehide', this.onPagehide);
}
}
private checkInfiniteLoop(fingerprint: string): boolean {
const currentTime = now();
const windowStart = currentTime - 1000;
if (!this.errorRateWindow[fingerprint]) {
this.errorRateWindow[fingerprint] = [];
}
const window = this.errorRateWindow[fingerprint];
window.push(currentTime);
while (window.length > 0 && window[0] < windowStart) {
window.shift();
}
if (window.length === 0) {
delete this.errorRateWindow[fingerprint];
return false;
}
if (window.length > 10) {
if (!this.paused) {
console.warn('[LightSDK] Infinite loop detected, pausing SDK for 60s', { fingerprint, count: window.length });
this.eventBus.emit('error', new Error('Infinite loop detected'));
this.paused = true;
if (this.pauseTimer) {
clearTimeout(this.pauseTimer);
}
this.pauseTimer = setTimeout(() => {
this.paused = false;
this.errorRateWindow = {};
console.info('[LightSDK] Resumed after infinite loop detection');
}, 60000);
}
return true;
}
return false;
}
enqueue(event: SentryEvent): void {
logDebug('EventQueue', 'enqueue called', { type: event.type, level: event.level, message: 'message' in event ? (event as { message: string }).message : undefined });
if (this.paused) {
logDebug('EventQueue', 'enqueue skipped: queue paused (infinite loop detected)');
return;
}
this.cleanupExpiredDedupeEntries();
const fingerprint = this.getDedupeKey(event);
if (fingerprint) {
if (this.checkInfiniteLoop(fingerprint)) {
logDebug('EventQueue', 'enqueue skipped: infinite loop detected', { fingerprint });
return;
}
const currentTime = now();
const existing = this.dedupeMap.get(fingerprint);
if (existing) {
if (currentTime - existing.lastTime < 60000) {
if (existing.count >= 3) {
if (!this.pendingCounts.has(fingerprint)) {
this.pendingCounts.set(fingerprint, { count: 1, ts_start: currentTime, ts_end: currentTime });
} else {
const pending = this.pendingCounts.get(fingerprint)!;
pending.count++;
pending.ts_end = currentTime;
}
logDebug('EventQueue', 'enqueue skipped: deduped (count >= 3)', { fingerprint, count: existing.count });
return;
}
existing.count++;
existing.lastTime = currentTime;
logDebug('EventQueue', 'Dedup count incremented', { fingerprint, count: existing.count });
} else {
this.dedupeMap.set(fingerprint, { count: 1, lastTime: currentTime, firstReported: true });
this.pendingCounts.delete(fingerprint);
logDebug('EventQueue', 'Dedup entry reset (expired)', { fingerprint });
}
} else {
this.dedupeMap.set(fingerprint, { count: 1, lastTime: currentTime, firstReported: false });
logDebug('EventQueue', 'New dedup entry created', { fingerprint });
}
}
this.queue.push(event);
logDebug('EventQueue', 'Event added to queue', { queueSize: this.queue.length });
this.eventBus.emit('event', event);
if (this.queue.length >= this.maxSize) {
logDebug('EventQueue', 'Queue reached maxSize, triggering flush', { maxSize: this.maxSize });
this.flush();
}
}
private cleanupExpiredDedupeEntries(): void {
if (Math.random() > 0.1) return;
const currentTime = now();
const expiryTime = 60000;
for (const [key, entry] of this.dedupeMap) {
if (currentTime - entry.lastTime > expiryTime) {
this.dedupeMap.delete(key);
}
}
}
private buildCountEvents(): SentryEvent[] {
const countEvents: SentryEvent[] = [];
for (const [fingerprint, data] of this.pendingCounts.entries()) {
const event: CountEvent = {
type: 'count',
fingerprint,
count: data.count,
ts_start: data.ts_start,
ts_end: data.ts_end,
timestamp: now(),
level: 'info',
};
countEvents.push(event);
}
return countEvents;
}
private getDedupeKey(event: SentryEvent): string | null {
if (event.type === 'error') {
const errEvent = event as ErrorEvent;
return errEvent.fingerprint || errEvent.message;
}
return null;
}
async flush(): Promise<void> {
logDebug('EventQueue', 'flush called', { queueSize: this.queue.length, pendingCounts: this.pendingCounts.size, isFlushing: this.isFlushing });
if (this.queue.length === 0 && this.pendingCounts.size === 0) {
logDebug('EventQueue', 'flush skipped: queue is empty');
return;
}
if (this.isFlushing) {
logDebug('EventQueue', 'flush skipped: already flushing');
return;
}
this.isFlushing = true;
const events = this.queue.splice(0, this.queue.length);
const countEvents = this.buildCountEvents();
const allEvents = [...events, ...countEvents];
logDebug('EventQueue', 'flush events to callback', { total: allEvents.length, events: events.length, countEvents: countEvents.length });
this.lastFlushTime = now();
this.eventBus.emit('report', allEvents);
try {
await this.flushCallback(allEvents);
this.pendingCounts.clear();
logDebug('EventQueue', 'flush completed successfully');
this.eventBus.emit('reported', allEvents);
} catch (e) {
logDebug('EventQueue', 'flush failed, returning events to queue', { error: e instanceof Error ? e.message : String(e) });
events.forEach(evt => this.queue.unshift(evt));
throw e;
} finally {
this.isFlushing = false;
}
}
flushSync(): void {
if (this.queue.length === 0 && this.pendingCounts.size === 0) return;
const events = this.queue.splice(0, this.queue.length);
const pendingCountsSnapshot = new Map(this.pendingCounts);
const countEvents = this.buildCountEvents();
const allEvents = [...events, ...countEvents];
this.lastFlushTime = now();
this.eventBus.emit('report', allEvents);
try {
this.syncFlushCallback(allEvents);
this.pendingCounts.clear();
this.eventBus.emit('reported', allEvents);
} catch (e) {
console.error('[LightSDK] Sync flush failed', e);
events.forEach(evt => this.queue.unshift(evt));
for (const [key, value] of pendingCountsSnapshot) {
if (!this.pendingCounts.has(key)) {
this.pendingCounts.set(key, value);
}
}
}
}
size(): number {
return this.queue.length;
}
destroy(): void {
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
if (this.pauseTimer) {
clearTimeout(this.pauseTimer);
this.pauseTimer = null;
}
if (typeof document !== 'undefined' && this.onVisibilityChange) {
document.removeEventListener('visibilitychange', this.onVisibilityChange);
}
if (typeof window !== 'undefined') {
if (this.onBeforeUnload) {
window.removeEventListener('beforeunload', this.onBeforeUnload);
}
if (this.onPagehide) {
window.removeEventListener('pagehide', this.onPagehide);
}
}
this.queue = [];
this.dedupeMap.clear();
this.pendingCounts.clear();
this.errorRateWindow = {};
}
}
|