@webdecoy/node 0.31.0 → 0.33.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/README.md CHANGED
@@ -200,6 +200,8 @@ Local rules decide the request before the SDK reports their events. With an API
200
200
 
201
201
  For a long-lived SDK instance, call `getViolationDeliveryStats()` for counts of events acknowledged by ingest, failed batches, discarded events, and events waiting for an attempt. Call `await sdk.destroy()` during graceful shutdown to finish an in-flight send and attempt the remaining queue. Reporting has a fixed in-memory buffer. When the client pauses after a failure, unattempted events stay buffered. An attempted batch with an uncertain acknowledgment is discarded to avoid replaying its server-side effects.
202
202
 
203
+ Each event carries a unique `eventId`. Ingest stores an event once per id, so a batch that reaches it twice, for example through a proxy retry, adds no second violation and no second detection, and the repeat is still acknowledged. If the process exits without `destroy()`, the reporter keeps it open for at most one flush interval plus the request timeout to attempt the events it holds. It never keeps the process open while the client is paused after a failure. The Fastify plugin sends its remaining events when `fastify.close()` runs, and the Hono middleware hands each send to `executionCtx.waitUntil` on runtimes that provide one, such as Cloudflare Workers.
204
+
203
205
  ## Fetch runtimes (Bun, Deno, Workers, Astro, SvelteKit, Remix)
204
206
 
205
207
  `createFetchGuard()` wraps the SDK for any runtime that hands you a WHATWG `Request`. It is the same implementation `@webdecoy/hono` uses.
@@ -3224,6 +3224,7 @@ var ViolationReporter = class {
3224
3224
  this.maxBufferSize = config.maxBufferSize ?? 50;
3225
3225
  this.debug = config.debug ?? false;
3226
3226
  const flushInterval = config.flushInterval ?? 5e3;
3227
+ this.flushInterval = flushInterval;
3227
3228
  this.flushTimer = setInterval(() => void this.flush(), flushInterval);
3228
3229
  if (this.flushTimer.unref) {
3229
3230
  this.flushTimer.unref();
@@ -3233,7 +3234,9 @@ var ViolationReporter = class {
3233
3234
  * Add violations to the buffer. Auto-flushes when buffer reaches maxBufferSize.
3234
3235
  */
3235
3236
  report(violations) {
3236
- this.buffer.push(...violations);
3237
+ for (const violation of violations) {
3238
+ this.buffer.push(violation.eventId ? violation : { ...violation, eventId: crypto.randomUUID() });
3239
+ }
3237
3240
  if (this.buffer.length > MAX_BUFFERED_VIOLATIONS) {
3238
3241
  const overflow = this.buffer.length - MAX_BUFFERED_VIOLATIONS;
3239
3242
  this.buffer.splice(0, overflow);
@@ -3242,8 +3245,32 @@ var ViolationReporter = class {
3242
3245
  if (this.buffer.length >= this.maxBufferSize) {
3243
3246
  this.flush().catch(() => {
3244
3247
  });
3248
+ } else {
3249
+ this.scheduleDelivery();
3245
3250
  }
3246
3251
  }
3252
+ /**
3253
+ * Give buffered events one delivery attempt before the process can exit.
3254
+ *
3255
+ * The interval above is unref'd so an idle reporter never holds a process
3256
+ * open. On its own that meant a server shut down gracefully (Express after
3257
+ * server.close(), Fastify after close(), Hono on Node) exited with its last
3258
+ * events unsent and uncounted. This one-shot timer is left ref'd, so a
3259
+ * natural exit waits at most one flush interval plus the request timeout.
3260
+ * It is not re-armed while the client is paused, so an outage cannot keep
3261
+ * the process alive.
3262
+ */
3263
+ cancelDelivery() {
3264
+ clearTimeout(this.deliveryTimer);
3265
+ this.deliveryTimer = void 0;
3266
+ }
3267
+ scheduleDelivery() {
3268
+ if (this.deliveryTimer) return;
3269
+ this.deliveryTimer = setTimeout(() => {
3270
+ this.deliveryTimer = void 0;
3271
+ void this.flush();
3272
+ }, this.flushInterval);
3273
+ }
3247
3274
  /** Flush the buffer, coalescing requests that arrive during an active send. */
3248
3275
  flush() {
3249
3276
  if (this.queuedFlush) return this.queuedFlush;
@@ -3259,6 +3286,7 @@ var ViolationReporter = class {
3259
3286
  const size = this.buffer.length;
3260
3287
  this.flushPromise = Promise.resolve().then(() => this.flushBatches(size)).finally(() => {
3261
3288
  this.flushPromise = void 0;
3289
+ if (this.buffer.length === 0) this.cancelDelivery();
3262
3290
  });
3263
3291
  return this.flushPromise;
3264
3292
  }
@@ -3288,6 +3316,7 @@ var ViolationReporter = class {
3288
3316
  /** Stop the timer and give remaining events one final flush. */
3289
3317
  async destroy() {
3290
3318
  clearInterval(this.flushTimer);
3319
+ this.cancelDelivery();
3291
3320
  if (this.flushPromise) await this.flushPromise;
3292
3321
  await this.flush();
3293
3322
  this.droppedEvents += this.buffer.length;