ezlogs-nextjs 0.4.2 → 0.4.3

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
@@ -317,8 +317,14 @@ The agent is designed to be **invisible** to your application.
317
317
 
318
318
  - **Never throws** into the host — every public surface wraps its work
319
319
  in `try/catch`.
320
- - **Never blocks** requests — sending is asynchronous on a background
321
- flush scheduler.
320
+ - **Doesn't hold up responses** — on a long-running server, events are
321
+ sent by a background flush scheduler, never on the request path. On
322
+ serverless hosts (Vercel, AWS Lambda, Netlify, Cloudflare Pages) the
323
+ function can freeze once the response is sent, so each request's
324
+ events are sent as it finishes: through the platform's `waitUntil`
325
+ when there is one (Vercel, and Next.js runtimes that provide it), after the response; without
326
+ it, the request waits at most 2 seconds, and events not sent by then
327
+ are best-effort (sent only if the function stays warm).
322
328
  - **Build-safe** — the build-time loader wraps every transform in
323
329
  try/catch and returns source unchanged on any error, so a regression
324
330
  in the loader can't break `next build`.
package/dist/index.cjs CHANGED
@@ -123,7 +123,7 @@ var GLOBAL_KEY = /* @__PURE__ */ Symbol.for("ezlogs-nextjs:flush-scheduler-state
123
123
  function state() {
124
124
  const g = globalThis;
125
125
  if (!g[GLOBAL_KEY]) {
126
- g[GLOBAL_KEY] = { timer: null, inFlight: false };
126
+ g[GLOBAL_KEY] = { timer: null, inFlight: false, current: null, followUp: null };
127
127
  }
128
128
  return g[GLOBAL_KEY];
129
129
  }
@@ -151,15 +151,37 @@ var flushScheduler = {
151
151
  isRunning() {
152
152
  return state().timer !== null;
153
153
  },
154
- /** Force a single drain-and-send cycle. Used by serverless flush + tests. */
155
- async flushNow() {
156
- await tick();
154
+ /**
155
+ * Drain and send now; resolves once this caller's events are sent (or
156
+ * dropped). If a send is already running, one more drain is queued after
157
+ * it, so events buffered since that send started are covered too: on
158
+ * serverless, overlapping requests each hand this promise to waitUntil.
159
+ * Concurrent callers share that single follow-up, so a slow server never
160
+ * builds a queue.
161
+ */
162
+ flushNow() {
163
+ const s = state();
164
+ if (!s.inFlight || !s.current) return tick();
165
+ if (!s.followUp) {
166
+ s.followUp = s.current.then(() => {
167
+ s.followUp = null;
168
+ return tick();
169
+ });
170
+ }
171
+ return s.followUp;
157
172
  }
158
173
  };
159
- async function tick() {
174
+ function tick() {
160
175
  const s = state();
161
- if (s.inFlight) return;
176
+ if (s.inFlight && s.current) return s.current;
162
177
  s.inFlight = true;
178
+ s.current = runTick().finally(() => {
179
+ s.inFlight = false;
180
+ s.current = null;
181
+ });
182
+ return s.current;
183
+ }
184
+ async function runTick() {
163
185
  try {
164
186
  const events = chunkYXZZNZG6_cjs.drainEvents();
165
187
  if (events.length > 0) await retrySender.send(events);
@@ -170,8 +192,6 @@ async function tick() {
170
192
  if (swept.length > 0) await retrySender.send(swept);
171
193
  } catch (error) {
172
194
  chunkTWTG5ILJ_cjs.logger.error(`FlushScheduler tick failed: ${describeError3(error)}`);
173
- } finally {
174
- s.inFlight = false;
175
195
  }
176
196
  }
177
197
  function sendInterval() {
@@ -799,10 +819,7 @@ function captureRoute(handler, context = {}) {
799
819
  durationMs: handlerMs ?? Date.now() - startMs,
800
820
  startedAt: new Date(startMs)
801
821
  });
802
- if (isServerless()) {
803
- await flushScheduler.flushNow().catch(() => {
804
- });
805
- }
822
+ await flushOnServerless();
806
823
  chunkMMLQXSSD_cjs.endCorrelationScope();
807
824
  };
808
825
  try {
@@ -1032,9 +1049,7 @@ function capturePagesApi(handler, context = {}) {
1032
1049
  durationMs: Date.now() - startMs,
1033
1050
  startedAt: new Date(startMs)
1034
1051
  });
1035
- if (isServerless()) {
1036
- await flushScheduler.flushNow().catch(() => void 0);
1037
- }
1052
+ await flushOnServerless();
1038
1053
  chunkMMLQXSSD_cjs.endCorrelationScope();
1039
1054
  }
1040
1055
  })
@@ -1113,9 +1128,7 @@ function captureServerAction(action, actionName) {
1113
1128
  });
1114
1129
  throw e;
1115
1130
  } finally {
1116
- if (isServerless()) {
1117
- await flushScheduler.flushNow().catch(() => void 0);
1118
- }
1131
+ await flushOnServerless();
1119
1132
  chunkMMLQXSSD_cjs.endCorrelationScope();
1120
1133
  }
1121
1134
  })
@@ -1929,6 +1942,31 @@ function followBody(response, method, onDone, onChunk) {
1929
1942
  return null;
1930
1943
  }
1931
1944
  }
1945
+ var SERVERLESS_INLINE_FLUSH_MS = 2e3;
1946
+ async function flushOnServerless() {
1947
+ if (!isServerless()) return;
1948
+ try {
1949
+ const waitUntil = platformWaitUntil();
1950
+ if (waitUntil) {
1951
+ waitUntil(flushScheduler.flushNow());
1952
+ return;
1953
+ }
1954
+ } catch (e) {
1955
+ chunkTWTG5ILJ_cjs.logger.debug(`waitUntil unavailable, flushing inline: ${describeError5(e)}`);
1956
+ }
1957
+ let timer;
1958
+ try {
1959
+ await Promise.race([
1960
+ flushScheduler.flushNow(),
1961
+ new Promise((resolve) => {
1962
+ timer = setTimeout(resolve, SERVERLESS_INLINE_FLUSH_MS);
1963
+ })
1964
+ ]);
1965
+ } catch {
1966
+ } finally {
1967
+ clearTimeout(timer);
1968
+ }
1969
+ }
1932
1970
  function platformWaitUntil() {
1933
1971
  for (const key of ["@next/request-context", "@vercel/request-context"]) {
1934
1972
  try {
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { patchPgClient } from './chunk-Z2TM3LGG.js';
2
2
  import { flushAuditLog, runWithEmittedAuditIdsScope, auditLogInstalled, checkAuditLogAccess, detectTriggerTrackedTables, maintainAuditLogAfterRequests } from './chunk-MHBS5FEP.js';
3
- import { build, pushEvent, maskAuthParams, isAuthActionName, isAuthPath, scrubSecretsInText, buffer_exports, sanitizeRequestParams, passesLuhn, drainEvents, isSensitiveKey } from './chunk-NINIAL7M.js';
3
+ import { build, pushEvent, maskAuthParams, isAuthActionName, isAuthPath, scrubSecretsInText, buffer_exports, sanitizeRequestParams, passesLuhn, isSensitiveKey, drainEvents } from './chunk-NINIAL7M.js';
4
4
  export { ArgError, build as buildEvent } from './chunk-NINIAL7M.js';
5
5
  import { configuration, resolveCorrelation, runWithCorrelation, maybeExtractSupabaseActor, endCorrelationScope, getCurrentActor, current, makeAsyncContextCapture, runWithActorScope, setActor, DEFAULT_EXCLUDED_EXTENSIONS } from './chunk-GI6BM4MR.js';
6
6
  import { logger, setLogLevel } from './chunk-FCFVIRQF.js';
@@ -122,7 +122,7 @@ var GLOBAL_KEY = /* @__PURE__ */ Symbol.for("ezlogs-nextjs:flush-scheduler-state
122
122
  function state() {
123
123
  const g = globalThis;
124
124
  if (!g[GLOBAL_KEY]) {
125
- g[GLOBAL_KEY] = { timer: null, inFlight: false };
125
+ g[GLOBAL_KEY] = { timer: null, inFlight: false, current: null, followUp: null };
126
126
  }
127
127
  return g[GLOBAL_KEY];
128
128
  }
@@ -150,15 +150,37 @@ var flushScheduler = {
150
150
  isRunning() {
151
151
  return state().timer !== null;
152
152
  },
153
- /** Force a single drain-and-send cycle. Used by serverless flush + tests. */
154
- async flushNow() {
155
- await tick();
153
+ /**
154
+ * Drain and send now; resolves once this caller's events are sent (or
155
+ * dropped). If a send is already running, one more drain is queued after
156
+ * it, so events buffered since that send started are covered too: on
157
+ * serverless, overlapping requests each hand this promise to waitUntil.
158
+ * Concurrent callers share that single follow-up, so a slow server never
159
+ * builds a queue.
160
+ */
161
+ flushNow() {
162
+ const s = state();
163
+ if (!s.inFlight || !s.current) return tick();
164
+ if (!s.followUp) {
165
+ s.followUp = s.current.then(() => {
166
+ s.followUp = null;
167
+ return tick();
168
+ });
169
+ }
170
+ return s.followUp;
156
171
  }
157
172
  };
158
- async function tick() {
173
+ function tick() {
159
174
  const s = state();
160
- if (s.inFlight) return;
175
+ if (s.inFlight && s.current) return s.current;
161
176
  s.inFlight = true;
177
+ s.current = runTick().finally(() => {
178
+ s.inFlight = false;
179
+ s.current = null;
180
+ });
181
+ return s.current;
182
+ }
183
+ async function runTick() {
162
184
  try {
163
185
  const events = drainEvents();
164
186
  if (events.length > 0) await retrySender.send(events);
@@ -169,8 +191,6 @@ async function tick() {
169
191
  if (swept.length > 0) await retrySender.send(swept);
170
192
  } catch (error) {
171
193
  logger.error(`FlushScheduler tick failed: ${describeError3(error)}`);
172
- } finally {
173
- s.inFlight = false;
174
194
  }
175
195
  }
176
196
  function sendInterval() {
@@ -798,10 +818,7 @@ function captureRoute(handler, context = {}) {
798
818
  durationMs: handlerMs ?? Date.now() - startMs,
799
819
  startedAt: new Date(startMs)
800
820
  });
801
- if (isServerless()) {
802
- await flushScheduler.flushNow().catch(() => {
803
- });
804
- }
821
+ await flushOnServerless();
805
822
  endCorrelationScope();
806
823
  };
807
824
  try {
@@ -1031,9 +1048,7 @@ function capturePagesApi(handler, context = {}) {
1031
1048
  durationMs: Date.now() - startMs,
1032
1049
  startedAt: new Date(startMs)
1033
1050
  });
1034
- if (isServerless()) {
1035
- await flushScheduler.flushNow().catch(() => void 0);
1036
- }
1051
+ await flushOnServerless();
1037
1052
  endCorrelationScope();
1038
1053
  }
1039
1054
  })
@@ -1112,9 +1127,7 @@ function captureServerAction(action, actionName) {
1112
1127
  });
1113
1128
  throw e;
1114
1129
  } finally {
1115
- if (isServerless()) {
1116
- await flushScheduler.flushNow().catch(() => void 0);
1117
- }
1130
+ await flushOnServerless();
1118
1131
  endCorrelationScope();
1119
1132
  }
1120
1133
  })
@@ -1928,6 +1941,31 @@ function followBody(response, method, onDone, onChunk) {
1928
1941
  return null;
1929
1942
  }
1930
1943
  }
1944
+ var SERVERLESS_INLINE_FLUSH_MS = 2e3;
1945
+ async function flushOnServerless() {
1946
+ if (!isServerless()) return;
1947
+ try {
1948
+ const waitUntil = platformWaitUntil();
1949
+ if (waitUntil) {
1950
+ waitUntil(flushScheduler.flushNow());
1951
+ return;
1952
+ }
1953
+ } catch (e) {
1954
+ logger.debug(`waitUntil unavailable, flushing inline: ${describeError5(e)}`);
1955
+ }
1956
+ let timer;
1957
+ try {
1958
+ await Promise.race([
1959
+ flushScheduler.flushNow(),
1960
+ new Promise((resolve) => {
1961
+ timer = setTimeout(resolve, SERVERLESS_INLINE_FLUSH_MS);
1962
+ })
1963
+ ]);
1964
+ } catch {
1965
+ } finally {
1966
+ clearTimeout(timer);
1967
+ }
1968
+ }
1931
1969
  function platformWaitUntil() {
1932
1970
  for (const key of ["@next/request-context", "@vercel/request-context"]) {
1933
1971
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ezlogs-nextjs",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "EZLogs agent for Next.js — capture HTTP, database, and background-job events as plain-English Action cards.",
5
5
  "license": "MIT",
6
6
  "author": "EZLogs",