@refactorlabs/recount 0.7.1 → 0.9.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/dist/index.cjs CHANGED
@@ -15,6 +15,28 @@ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "acce
15
15
 
16
16
  // src/lib/http.ts
17
17
  var HTTP_METHODS = /* @__PURE__ */ new Set(["GET", "POST", "PUT", "PATCH", "DELETE"]);
18
+ var MAX_URL_LENGTH = 2048;
19
+ var MAX_HEADERS = 50;
20
+ var MAX_HEADER_NAME_LENGTH = 256;
21
+ var MAX_HEADER_VALUE_LENGTH = 1024;
22
+ var MAX_BODY_LENGTH = 65536;
23
+ var MAX_DESCRIPTION_LENGTH = 1e3;
24
+ var TRUNCATED = "\u2026[truncated]";
25
+ var OMITTED_JSON = `[JSON body omitted: over ${MAX_BODY_LENGTH} characters]`;
26
+ var BODY_READ_TIMEOUT_MS = 1e3;
27
+ var STILL_STREAMING = `[body not captured: still streaming after ${BODY_READ_TIMEOUT_MS}ms]`;
28
+ var UNCAPTURED_BODY_TYPES = [
29
+ "text/event-stream",
30
+ "application/x-ndjson",
31
+ "application/octet-stream",
32
+ "application/pdf",
33
+ "application/zip",
34
+ "application/gzip",
35
+ "image/",
36
+ "audio/",
37
+ "video/",
38
+ "font/"
39
+ ];
18
40
  function inferContentType(ct) {
19
41
  if (ct.includes("application/json")) {
20
42
  return "json";
@@ -27,11 +49,25 @@ function inferContentType(ct) {
27
49
  }
28
50
  return null;
29
51
  }
30
- function headersToRecord(headers) {
31
- const out = {};
32
- headers.forEach((v, k) => {
33
- out[k] = v;
52
+ function truncate(value, max) {
53
+ return value.length > max ? value.slice(0, max - TRUNCATED.length) + TRUNCATED : value;
54
+ }
55
+ function headerEntries(headers) {
56
+ const entries = [];
57
+ headers.forEach((value, name) => {
58
+ entries.push([name, value]);
34
59
  });
60
+ return entries;
61
+ }
62
+ function limitHeaders(entries) {
63
+ const out = {};
64
+ let count = 0;
65
+ for (const [name, value] of entries) {
66
+ if (count === MAX_HEADERS) break;
67
+ if (name.length > MAX_HEADER_NAME_LENGTH) continue;
68
+ out[name] = truncate(String(value), MAX_HEADER_VALUE_LENGTH);
69
+ count++;
70
+ }
35
71
  return out;
36
72
  }
37
73
  function initToRecord(headers) {
@@ -39,12 +75,34 @@ function initToRecord(headers) {
39
75
  return {};
40
76
  }
41
77
  if (headers instanceof Headers) {
42
- return headersToRecord(headers);
78
+ return limitHeaders(headerEntries(headers));
43
79
  }
44
80
  if (Array.isArray(headers)) {
45
- return Object.fromEntries(headers);
81
+ return limitHeaders(headers);
46
82
  }
47
- return { ...headers };
83
+ return limitHeaders(Object.entries(headers));
84
+ }
85
+ function findHeader(headers, name) {
86
+ for (const [key, value] of Object.entries(headers)) {
87
+ if (key.toLowerCase() === name) return value;
88
+ }
89
+ return void 0;
90
+ }
91
+ function parseBodyText(text) {
92
+ if (text === "") {
93
+ return null;
94
+ }
95
+ if (text.length > MAX_BODY_LENGTH) {
96
+ return /^\s*[[{]/.test(text) ? OMITTED_JSON : truncate(text, MAX_BODY_LENGTH);
97
+ }
98
+ try {
99
+ const parsed = JSON.parse(text);
100
+ if (typeof parsed === "object" && parsed !== null) {
101
+ return parsed;
102
+ }
103
+ } catch {
104
+ }
105
+ return text;
48
106
  }
49
107
  function parseRequestBody(body, contentType) {
50
108
  if (body == null) {
@@ -56,62 +114,128 @@ function parseRequestBody(body, contentType) {
56
114
  if (typeof body !== "string") {
57
115
  return null;
58
116
  }
59
- if (contentType === "json") {
60
- try {
61
- return JSON.parse(body);
62
- } catch {
63
- return body;
64
- }
65
- }
66
117
  if (contentType === "form") {
67
118
  return Object.fromEntries(new URLSearchParams(body));
68
119
  }
69
- return body;
120
+ return parseBodyText(body);
70
121
  }
71
- async function parseResponseBody(response, contentType) {
72
- if (contentType === "json") {
73
- try {
74
- return await response.json();
75
- } catch {
122
+ async function readResponseBody(response, contentType) {
123
+ if (UNCAPTURED_BODY_TYPES.some((t) => contentType.includes(t))) {
124
+ return null;
125
+ }
126
+ let timer;
127
+ try {
128
+ const body = response.clone().body;
129
+ if (!body) {
76
130
  return null;
77
131
  }
132
+ const reader = body.getReader();
133
+ const stopReading = () => {
134
+ reader.cancel().catch(() => {
135
+ });
136
+ };
137
+ const timedOut = new Promise((resolve) => {
138
+ timer = setTimeout(() => resolve("timeout"), BODY_READ_TIMEOUT_MS);
139
+ });
140
+ const decoder = new TextDecoder();
141
+ let text = "";
142
+ while (text.length <= MAX_BODY_LENGTH) {
143
+ const chunk = await Promise.race([reader.read(), timedOut]);
144
+ if (chunk === "timeout") {
145
+ stopReading();
146
+ return STILL_STREAMING;
147
+ }
148
+ if (chunk.done) {
149
+ return parseBodyText(text + decoder.decode());
150
+ }
151
+ text += decoder.decode(chunk.value, { stream: true });
152
+ }
153
+ stopReading();
154
+ return parseBodyText(text);
155
+ } catch {
156
+ return null;
157
+ } finally {
158
+ clearTimeout(timer);
78
159
  }
79
- if (contentType === "text" || contentType === "form") {
80
- return response.text();
160
+ }
161
+ function resolveRequest(input, init2) {
162
+ if (typeof Request !== "undefined" && input instanceof Request) {
163
+ return {
164
+ url: input.url,
165
+ method: init2?.method ?? input.method,
166
+ headers: init2?.headers ?? input.headers
167
+ };
81
168
  }
82
- return null;
169
+ return {
170
+ url: input.toString(),
171
+ method: init2?.method ?? "GET",
172
+ headers: init2?.headers
173
+ };
83
174
  }
84
- async function executeFetch(url, init2, recordSpan) {
85
- const method = init2?.method?.toUpperCase() ?? "GET";
86
- const reqHeaders = initToRecord(init2?.headers);
87
- const reqContentType = inferContentType(
88
- reqHeaders["content-type"] ?? reqHeaders["Content-Type"] ?? ""
175
+ function buildRequestInput(input, init2) {
176
+ const { url, method, headers: rawHeaders } = resolveRequest(input, init2);
177
+ const upperMethod = method.toUpperCase();
178
+ const headers = initToRecord(rawHeaders);
179
+ const contentType = inferContentType(
180
+ (findHeader(headers, "content-type") ?? "").toLowerCase()
89
181
  );
90
- const reqBody = parseRequestBody(init2?.body, reqContentType);
182
+ return {
183
+ // Ingest only accepts these methods; others (HEAD, OPTIONS) record as GET.
184
+ method: HTTP_METHODS.has(upperMethod) ? upperMethod : "GET",
185
+ url: truncate(url, MAX_URL_LENGTH),
186
+ headers,
187
+ content_type: contentType,
188
+ body: parseRequestBody(init2?.body, contentType)
189
+ };
190
+ }
191
+ async function buildResponseOutput(response) {
192
+ const { status } = response;
193
+ if (!Number.isInteger(status) || status < 100 || status > 599) {
194
+ return void 0;
195
+ }
196
+ const contentType = (response.headers?.get?.("content-type") ?? "").toLowerCase();
197
+ return {
198
+ status,
199
+ headers: response.headers ? limitHeaders(headerEntries(response.headers)) : {},
200
+ content_type: inferContentType(contentType),
201
+ body: await readResponseBody(response, contentType)
202
+ };
203
+ }
204
+ async function executeFetch(input, init2, recordSpan) {
205
+ let request;
206
+ try {
207
+ request = buildRequestInput(input, init2);
208
+ } catch {
209
+ }
210
+ function record(span) {
211
+ if (!request) return;
212
+ try {
213
+ recordSpan({ input: request, ...span });
214
+ } catch {
215
+ }
216
+ }
91
217
  const start = Date.now();
92
- const response = await globalThis.fetch(url, init2);
218
+ let response;
219
+ try {
220
+ response = await globalThis.fetch(input, init2);
221
+ } catch (err) {
222
+ record({
223
+ durationMs: Date.now() - start,
224
+ description: truncate(
225
+ `Request failed: ${err instanceof Error ? err.message : String(err)}`,
226
+ MAX_DESCRIPTION_LENGTH
227
+ )
228
+ });
229
+ throw err;
230
+ }
93
231
  const durationMs = Date.now() - start;
94
- const resContentType = inferContentType(
95
- response.headers.get("content-type") ?? ""
96
- );
97
- const resBody = await parseResponseBody(response, resContentType);
98
- recordSpan(
99
- {
100
- method: HTTP_METHODS.has(method) ? method : "GET",
101
- url: url.toString(),
102
- headers: reqHeaders,
103
- content_type: reqContentType,
104
- body: reqBody
105
- },
106
- {
107
- status: response.status,
108
- headers: headersToRecord(response.headers),
109
- content_type: resContentType,
110
- body: resBody
111
- },
112
- durationMs
113
- );
114
- return resBody;
232
+ let output;
233
+ try {
234
+ output = await buildResponseOutput(response);
235
+ } catch {
236
+ }
237
+ record({ output, durationMs });
238
+ return response;
115
239
  }
116
240
 
117
241
  // src/lib/redact.ts
@@ -300,14 +424,13 @@ var Trace = class {
300
424
  __privateSet(this, _environment, environment);
301
425
  return this;
302
426
  }
303
- fetch(name, url, init2, options) {
304
- return executeFetch(url, init2, (input, output, durationMs) => {
305
- this.span("http", name, {
306
- input,
307
- output,
308
- durationMs,
309
- redact: options?.redact
310
- });
427
+ /**
428
+ * Traced `fetch`: returns the Response untouched and records the request and
429
+ * response as an http span.
430
+ */
431
+ fetch(name, input, init2, options) {
432
+ return executeFetch(input, init2, (span) => {
433
+ this.span("http", name, { ...span, redact: options?.redact });
311
434
  });
312
435
  }
313
436
  async end() {
@@ -439,6 +562,13 @@ var NOOP_RESULT2 = { traceId: "" };
439
562
  var MAX_RETRIES = 2;
440
563
  var BASE_RETRY_MS = 1e3;
441
564
  var MAX_RETRY_DELAY_MS = 6e4;
565
+ var DEFAULT_TIMEOUT_MS = 3e4;
566
+ var MAX_TIMEOUT_MS = 2 ** 31 - 1;
567
+ function resolveTimeoutMs(value) {
568
+ if (typeof value !== "number" || isNaN(value) || value <= 0)
569
+ return DEFAULT_TIMEOUT_MS;
570
+ return Math.min(value, MAX_TIMEOUT_MS);
571
+ }
442
572
  function retryDelayMs(res, attempt) {
443
573
  const header = res.headers.get("retry-after");
444
574
  if (header) {
@@ -451,17 +581,19 @@ function retryDelayMs(res, attempt) {
451
581
  }
452
582
  return BASE_RETRY_MS * 2 ** (attempt - 1);
453
583
  }
454
- var _apiKey, _apiUrl, _environment2, _disabled2, _log2;
584
+ var _apiKey, _apiUrl, _environment2, _disabled2, _timeoutMs, _log2;
455
585
  var Recount = class {
456
586
  constructor(options = {}) {
457
587
  __privateAdd(this, _apiKey);
458
588
  __privateAdd(this, _apiUrl);
459
589
  __privateAdd(this, _environment2);
460
590
  __privateAdd(this, _disabled2);
591
+ __privateAdd(this, _timeoutMs);
461
592
  __privateAdd(this, _log2);
462
593
  const debug = options.debug ?? !!process.env.RECOUNT_DEBUG;
463
594
  __privateSet(this, _log2, debug ? (msg) => console.log(`[recount] ${msg}`) : () => {
464
595
  });
596
+ __privateSet(this, _timeoutMs, resolveTimeoutMs(options.timeoutMs));
465
597
  const apiKey = options.apiKey ?? process.env.RECOUNT_API_KEY;
466
598
  if (!apiKey) {
467
599
  console.warn("Recount: no API key found - tracing disabled.");
@@ -503,33 +635,61 @@ var Recount = class {
503
635
  "Content-Type": "application/json",
504
636
  "x-api-key": __privateGet(this, _apiKey)
505
637
  };
506
- for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
507
- try {
508
- const res = await globalThis.fetch(`${__privateGet(this, _apiUrl)}/ingest`, {
509
- method: "POST",
510
- headers,
511
- body
512
- });
513
- if (res.status === 429 && attempt < MAX_RETRIES) {
514
- const delay = retryDelayMs(res, attempt + 1);
515
- __privateGet(this, _log2).call(this, `429 rate limited attempt=${attempt + 1}/${MAX_RETRIES + 1} retrying in ${delay}ms`);
516
- await res.body?.cancel().catch(() => {
638
+ const deadline = Date.now() + __privateGet(this, _timeoutMs);
639
+ const controller = new AbortController();
640
+ const timer = setTimeout(() => controller.abort(), __privateGet(this, _timeoutMs));
641
+ try {
642
+ for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
643
+ try {
644
+ const res = await globalThis.fetch(`${__privateGet(this, _apiUrl)}/ingest`, {
645
+ method: "POST",
646
+ headers,
647
+ body,
648
+ signal: controller.signal
517
649
  });
518
- await new Promise((r) => setTimeout(r, delay));
519
- continue;
520
- }
521
- if (!res.ok) {
522
- const text = await res.text().catch(() => res.statusText);
523
- __privateGet(this, _log2).call(this, `ingest error status=${res.status} message=${text}`);
650
+ if (res.status === 429 && attempt < MAX_RETRIES) {
651
+ const delay = retryDelayMs(res, attempt + 1);
652
+ await res.body?.cancel().catch(() => {
653
+ });
654
+ if (Date.now() + delay >= deadline) {
655
+ __privateGet(this, _log2).call(this, `429 rate limited attempt=${attempt + 1}/${MAX_RETRIES + 1} retry in ${delay}ms would exceed timeout=${__privateGet(this, _timeoutMs)}ms, dropping trace`);
656
+ return NOOP_RESULT2;
657
+ }
658
+ __privateGet(this, _log2).call(this, `429 rate limited attempt=${attempt + 1}/${MAX_RETRIES + 1} retrying in ${delay}ms`);
659
+ await new Promise((r) => setTimeout(r, delay));
660
+ continue;
661
+ }
662
+ if (!res.ok) {
663
+ const text = await res.text().catch(() => res.statusText);
664
+ __privateGet(this, _log2).call(this, `ingest error status=${res.status} message=${text}`);
665
+ return NOOP_RESULT2;
666
+ }
667
+ let json;
668
+ try {
669
+ json = await res.json();
670
+ } catch (err) {
671
+ if (!(err instanceof SyntaxError)) throw err;
672
+ __privateGet(this, _log2).call(this, `ingest error: invalid JSON response (${err.message})`);
673
+ return NOOP_RESULT2;
674
+ }
675
+ const traceId = json?.data?.trace?.id;
676
+ if (typeof traceId !== "string") {
677
+ __privateGet(this, _log2).call(this, `ingest error: unexpected response ${JSON.stringify(json)?.slice(0, 200)}`);
678
+ return NOOP_RESULT2;
679
+ }
680
+ __privateGet(this, _log2).call(this, `ingested traceId=${traceId}`);
681
+ return { traceId };
682
+ } catch (err) {
683
+ if (controller.signal.aborted) {
684
+ __privateGet(this, _log2).call(this, `ingest timed out after ${__privateGet(this, _timeoutMs)}ms`);
685
+ } else {
686
+ __privateGet(this, _log2).call(this, `ingest failed (network error): ${err instanceof Error ? err.message : String(err)}`);
687
+ }
524
688
  return NOOP_RESULT2;
525
689
  }
526
- const json = await res.json();
527
- __privateGet(this, _log2).call(this, `ingested traceId=${json.data.trace.id}`);
528
- return { traceId: json.data.trace.id };
529
- } catch (err) {
530
- __privateGet(this, _log2).call(this, `ingest failed (network error): ${err instanceof Error ? err.message : String(err)}`);
531
- return NOOP_RESULT2;
532
690
  }
691
+ } finally {
692
+ clearTimeout(timer);
533
693
  }
534
694
  return NOOP_RESULT2;
535
695
  }
@@ -538,6 +698,7 @@ _apiKey = new WeakMap();
538
698
  _apiUrl = new WeakMap();
539
699
  _environment2 = new WeakMap();
540
700
  _disabled2 = new WeakMap();
701
+ _timeoutMs = new WeakMap();
541
702
  _log2 = new WeakMap();
542
703
  var _key = /* @__PURE__ */ Symbol.for("@refactorlabs/recount.storage");
543
704
  var _nameKey = /* @__PURE__ */ Symbol.for("@refactorlabs/recount.spanName");
@@ -977,15 +1138,13 @@ function named(name, fn) {
977
1138
  function addMetadata(data) {
978
1139
  getActiveTrace()?.addMetadata(data);
979
1140
  }
980
- function fetch(name, url, init2, options) {
1141
+ function fetch(name, input, init2, options) {
981
1142
  const active = getActiveTrace();
982
- return executeFetch(url, init2, (input, output, durationMs) => {
983
- active?.span("http", name, {
984
- input,
985
- output,
986
- durationMs,
987
- redact: options?.redact
988
- });
1143
+ if (!active) {
1144
+ return globalThis.fetch(input, init2);
1145
+ }
1146
+ return executeFetch(input, init2, (span) => {
1147
+ active.span("http", name, { ...span, redact: options?.redact });
989
1148
  });
990
1149
  }
991
1150
  var recount = {
@@ -1015,7 +1174,26 @@ var recount = {
1015
1174
  };
1016
1175
  var index_default = recount;
1017
1176
 
1177
+ exports.REDACTED = REDACTED;
1178
+ exports.Recount = Recount;
1179
+ exports.RecountError = RecountError;
1018
1180
  exports.SpanGroup = SpanGroup;
1181
+ exports.addAlert = addAlert;
1182
+ exports.addGroup = addGroup;
1183
+ exports.addMessage = addMessage;
1184
+ exports.addMetadata = addMetadata;
1185
+ exports.addSpan = addSpan;
1186
+ exports.chart = chart;
1187
+ exports.chartFromRecords = chartFromRecords;
1019
1188
  exports.default = index_default;
1189
+ exports.fetch = fetch;
1190
+ exports.init = init;
1191
+ exports.named = named;
1192
+ exports.spanCell = spanCell;
1193
+ exports.startTrace = startTrace;
1194
+ exports.table = table;
1195
+ exports.tableFromObject = tableFromObject;
1196
+ exports.tableFromRecords = tableFromRecords;
1197
+ exports.tableSections = tableSections;
1020
1198
  //# sourceMappingURL=index.cjs.map
1021
1199
  //# sourceMappingURL=index.cjs.map