repro-nest 0.0.200 → 0.0.201

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.js CHANGED
@@ -1149,6 +1149,7 @@ function reproMiddleware(cfg) {
1149
1149
  let flushed = false;
1150
1150
  let finished = false;
1151
1151
  let finishedAt = null;
1152
+ let lastEventAt = Date.now();
1152
1153
  let idleTimer = null;
1153
1154
  let hardStopTimer = null;
1154
1155
  let drainTimer = null;
@@ -1180,7 +1181,7 @@ function reproMiddleware(cfg) {
1180
1181
  }
1181
1182
  };
1182
1183
  const hasActiveWork = () => activeSpans.size > 0 || anonymousSpanDepth > 0;
1183
- const scheduleIdleFlush = () => {
1184
+ const scheduleIdleFlush = (delay = TRACE_IDLE_FLUSH_MS) => {
1184
1185
  if (!finished || flushed)
1185
1186
  return;
1186
1187
  if (hasActiveWork())
@@ -1191,20 +1192,27 @@ function reproMiddleware(cfg) {
1191
1192
  }
1192
1193
  catch { }
1193
1194
  }
1194
- idleTimer = setTimeout(() => doFlush(false), TRACE_IDLE_FLUSH_MS);
1195
+ idleTimer = setTimeout(() => doFlush(false), delay);
1195
1196
  };
1196
1197
  const doFlush = (force = false) => {
1197
1198
  if (flushed)
1198
1199
  return;
1200
+ const now = Date.now();
1199
1201
  const stillActive = hasActiveWork();
1202
+ const quietMs = now - lastEventAt;
1203
+ const waitedFinish = finishedAt === null ? 0 : now - finishedAt;
1204
+ // If work is still active and we haven't been quiet long enough, defer.
1200
1205
  if (stillActive && !force) {
1201
- scheduleIdleFlush();
1206
+ const remaining = Math.max(0, TRACE_LINGER_AFTER_FINISH_MS - quietMs);
1207
+ scheduleIdleFlush(Math.max(remaining, 10));
1202
1208
  return;
1203
1209
  }
1204
- if (stillActive && force && finishedAt !== null) {
1205
- const waited = Date.now() - finishedAt;
1206
- if (waited < ACTIVE_SPAN_FORCE_FLUSH_MS) {
1207
- scheduleIdleFlush();
1210
+ if (stillActive && force) {
1211
+ // Allow forced flush after either linger window of silence or max guard.
1212
+ if (quietMs < TRACE_LINGER_AFTER_FINISH_MS && waitedFinish < ACTIVE_SPAN_FORCE_FLUSH_MS) {
1213
+ const remainingQuiet = TRACE_LINGER_AFTER_FINISH_MS - quietMs;
1214
+ const remainingGuard = ACTIVE_SPAN_FORCE_FLUSH_MS - waitedFinish;
1215
+ scheduleIdleFlush(Math.max(10, Math.min(remainingQuiet, remainingGuard)));
1208
1216
  return;
1209
1217
  }
1210
1218
  }
@@ -1283,6 +1291,7 @@ function reproMiddleware(cfg) {
1283
1291
  };
1284
1292
  const spanKey = normalizeSpanId(evt.spanId);
1285
1293
  if (evt.type === 'enter') {
1294
+ lastEventAt = Date.now();
1286
1295
  if (spanKey) {
1287
1296
  activeSpans.add(spanKey);
1288
1297
  }
@@ -1291,6 +1300,7 @@ function reproMiddleware(cfg) {
1291
1300
  }
1292
1301
  }
1293
1302
  else if (evt.type === 'exit') {
1303
+ lastEventAt = Date.now();
1294
1304
  if (spanKey && activeSpans.has(spanKey)) {
1295
1305
  activeSpans.delete(spanKey);
1296
1306
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "repro-nest",
3
- "version": "0.0.200",
3
+ "version": "0.0.201",
4
4
  "description": "Repro Nest SDK",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/index.ts CHANGED
@@ -1373,6 +1373,7 @@ export function reproMiddleware(cfg: ReproMiddlewareConfig) {
1373
1373
  let flushed = false;
1374
1374
  let finished = false;
1375
1375
  let finishedAt: number | null = null;
1376
+ let lastEventAt: number = Date.now();
1376
1377
  let idleTimer: NodeJS.Timeout | null = null;
1377
1378
  let hardStopTimer: NodeJS.Timeout | null = null;
1378
1379
  let drainTimer: NodeJS.Timeout | null = null;
@@ -1398,26 +1399,34 @@ export function reproMiddleware(cfg: ReproMiddlewareConfig) {
1398
1399
 
1399
1400
  const hasActiveWork = () => activeSpans.size > 0 || anonymousSpanDepth > 0;
1400
1401
 
1401
- const scheduleIdleFlush = () => {
1402
+ const scheduleIdleFlush = (delay: number = TRACE_IDLE_FLUSH_MS) => {
1402
1403
  if (!finished || flushed) return;
1403
1404
  if (hasActiveWork()) return;
1404
1405
  if (idleTimer) {
1405
1406
  try { clearTimeout(idleTimer); } catch {}
1406
1407
  }
1407
- idleTimer = setTimeout(() => doFlush(false), TRACE_IDLE_FLUSH_MS);
1408
+ idleTimer = setTimeout(() => doFlush(false), delay);
1408
1409
  };
1409
1410
 
1410
1411
  const doFlush = (force: boolean = false) => {
1411
1412
  if (flushed) return;
1413
+ const now = Date.now();
1412
1414
  const stillActive = hasActiveWork();
1415
+ const quietMs = now - lastEventAt;
1416
+ const waitedFinish = finishedAt === null ? 0 : now - finishedAt;
1417
+
1418
+ // If work is still active and we haven't been quiet long enough, defer.
1413
1419
  if (stillActive && !force) {
1414
- scheduleIdleFlush();
1420
+ const remaining = Math.max(0, TRACE_LINGER_AFTER_FINISH_MS - quietMs);
1421
+ scheduleIdleFlush(Math.max(remaining, 10));
1415
1422
  return;
1416
1423
  }
1417
- if (stillActive && force && finishedAt !== null) {
1418
- const waited = Date.now() - finishedAt;
1419
- if (waited < ACTIVE_SPAN_FORCE_FLUSH_MS) {
1420
- scheduleIdleFlush();
1424
+ if (stillActive && force) {
1425
+ // Allow forced flush after either linger window of silence or max guard.
1426
+ if (quietMs < TRACE_LINGER_AFTER_FINISH_MS && waitedFinish < ACTIVE_SPAN_FORCE_FLUSH_MS) {
1427
+ const remainingQuiet = TRACE_LINGER_AFTER_FINISH_MS - quietMs;
1428
+ const remainingGuard = ACTIVE_SPAN_FORCE_FLUSH_MS - waitedFinish;
1429
+ scheduleIdleFlush(Math.max(10, Math.min(remainingQuiet, remainingGuard)));
1421
1430
  return;
1422
1431
  }
1423
1432
  }
@@ -1492,12 +1501,14 @@ export function reproMiddleware(cfg: ReproMiddlewareConfig) {
1492
1501
 
1493
1502
  const spanKey = normalizeSpanId(evt.spanId);
1494
1503
  if (evt.type === 'enter') {
1504
+ lastEventAt = Date.now();
1495
1505
  if (spanKey) {
1496
1506
  activeSpans.add(spanKey);
1497
1507
  } else {
1498
1508
  anonymousSpanDepth = Math.max(0, anonymousSpanDepth + 1);
1499
1509
  }
1500
1510
  } else if (evt.type === 'exit') {
1511
+ lastEventAt = Date.now();
1501
1512
  if (spanKey && activeSpans.has(spanKey)) {
1502
1513
  activeSpans.delete(spanKey);
1503
1514
  } else if (!spanKey && anonymousSpanDepth > 0) {