@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.js CHANGED
@@ -2,6 +2,28 @@ import { __privateAdd, __privateGet, __privateSet, runWithTrace, getActiveTrace,
2
2
 
3
3
  // src/lib/http.ts
4
4
  var HTTP_METHODS = /* @__PURE__ */ new Set(["GET", "POST", "PUT", "PATCH", "DELETE"]);
5
+ var MAX_URL_LENGTH = 2048;
6
+ var MAX_HEADERS = 50;
7
+ var MAX_HEADER_NAME_LENGTH = 256;
8
+ var MAX_HEADER_VALUE_LENGTH = 1024;
9
+ var MAX_BODY_LENGTH = 65536;
10
+ var MAX_DESCRIPTION_LENGTH = 1e3;
11
+ var TRUNCATED = "\u2026[truncated]";
12
+ var OMITTED_JSON = `[JSON body omitted: over ${MAX_BODY_LENGTH} characters]`;
13
+ var BODY_READ_TIMEOUT_MS = 1e3;
14
+ var STILL_STREAMING = `[body not captured: still streaming after ${BODY_READ_TIMEOUT_MS}ms]`;
15
+ var UNCAPTURED_BODY_TYPES = [
16
+ "text/event-stream",
17
+ "application/x-ndjson",
18
+ "application/octet-stream",
19
+ "application/pdf",
20
+ "application/zip",
21
+ "application/gzip",
22
+ "image/",
23
+ "audio/",
24
+ "video/",
25
+ "font/"
26
+ ];
5
27
  function inferContentType(ct) {
6
28
  if (ct.includes("application/json")) {
7
29
  return "json";
@@ -14,11 +36,25 @@ function inferContentType(ct) {
14
36
  }
15
37
  return null;
16
38
  }
17
- function headersToRecord(headers) {
18
- const out = {};
19
- headers.forEach((v, k) => {
20
- out[k] = v;
39
+ function truncate(value, max) {
40
+ return value.length > max ? value.slice(0, max - TRUNCATED.length) + TRUNCATED : value;
41
+ }
42
+ function headerEntries(headers) {
43
+ const entries = [];
44
+ headers.forEach((value, name) => {
45
+ entries.push([name, value]);
21
46
  });
47
+ return entries;
48
+ }
49
+ function limitHeaders(entries) {
50
+ const out = {};
51
+ let count = 0;
52
+ for (const [name, value] of entries) {
53
+ if (count === MAX_HEADERS) break;
54
+ if (name.length > MAX_HEADER_NAME_LENGTH) continue;
55
+ out[name] = truncate(String(value), MAX_HEADER_VALUE_LENGTH);
56
+ count++;
57
+ }
22
58
  return out;
23
59
  }
24
60
  function initToRecord(headers) {
@@ -26,12 +62,34 @@ function initToRecord(headers) {
26
62
  return {};
27
63
  }
28
64
  if (headers instanceof Headers) {
29
- return headersToRecord(headers);
65
+ return limitHeaders(headerEntries(headers));
30
66
  }
31
67
  if (Array.isArray(headers)) {
32
- return Object.fromEntries(headers);
68
+ return limitHeaders(headers);
33
69
  }
34
- return { ...headers };
70
+ return limitHeaders(Object.entries(headers));
71
+ }
72
+ function findHeader(headers, name) {
73
+ for (const [key, value] of Object.entries(headers)) {
74
+ if (key.toLowerCase() === name) return value;
75
+ }
76
+ return void 0;
77
+ }
78
+ function parseBodyText(text) {
79
+ if (text === "") {
80
+ return null;
81
+ }
82
+ if (text.length > MAX_BODY_LENGTH) {
83
+ return /^\s*[[{]/.test(text) ? OMITTED_JSON : truncate(text, MAX_BODY_LENGTH);
84
+ }
85
+ try {
86
+ const parsed = JSON.parse(text);
87
+ if (typeof parsed === "object" && parsed !== null) {
88
+ return parsed;
89
+ }
90
+ } catch {
91
+ }
92
+ return text;
35
93
  }
36
94
  function parseRequestBody(body, contentType) {
37
95
  if (body == null) {
@@ -43,62 +101,128 @@ function parseRequestBody(body, contentType) {
43
101
  if (typeof body !== "string") {
44
102
  return null;
45
103
  }
46
- if (contentType === "json") {
47
- try {
48
- return JSON.parse(body);
49
- } catch {
50
- return body;
51
- }
52
- }
53
104
  if (contentType === "form") {
54
105
  return Object.fromEntries(new URLSearchParams(body));
55
106
  }
56
- return body;
107
+ return parseBodyText(body);
57
108
  }
58
- async function parseResponseBody(response, contentType) {
59
- if (contentType === "json") {
60
- try {
61
- return await response.json();
62
- } catch {
109
+ async function readResponseBody(response, contentType) {
110
+ if (UNCAPTURED_BODY_TYPES.some((t) => contentType.includes(t))) {
111
+ return null;
112
+ }
113
+ let timer;
114
+ try {
115
+ const body = response.clone().body;
116
+ if (!body) {
63
117
  return null;
64
118
  }
119
+ const reader = body.getReader();
120
+ const stopReading = () => {
121
+ reader.cancel().catch(() => {
122
+ });
123
+ };
124
+ const timedOut = new Promise((resolve) => {
125
+ timer = setTimeout(() => resolve("timeout"), BODY_READ_TIMEOUT_MS);
126
+ });
127
+ const decoder = new TextDecoder();
128
+ let text = "";
129
+ while (text.length <= MAX_BODY_LENGTH) {
130
+ const chunk = await Promise.race([reader.read(), timedOut]);
131
+ if (chunk === "timeout") {
132
+ stopReading();
133
+ return STILL_STREAMING;
134
+ }
135
+ if (chunk.done) {
136
+ return parseBodyText(text + decoder.decode());
137
+ }
138
+ text += decoder.decode(chunk.value, { stream: true });
139
+ }
140
+ stopReading();
141
+ return parseBodyText(text);
142
+ } catch {
143
+ return null;
144
+ } finally {
145
+ clearTimeout(timer);
65
146
  }
66
- if (contentType === "text" || contentType === "form") {
67
- return response.text();
147
+ }
148
+ function resolveRequest(input, init2) {
149
+ if (typeof Request !== "undefined" && input instanceof Request) {
150
+ return {
151
+ url: input.url,
152
+ method: init2?.method ?? input.method,
153
+ headers: init2?.headers ?? input.headers
154
+ };
68
155
  }
69
- return null;
156
+ return {
157
+ url: input.toString(),
158
+ method: init2?.method ?? "GET",
159
+ headers: init2?.headers
160
+ };
70
161
  }
71
- async function executeFetch(url, init2, recordSpan) {
72
- const method = init2?.method?.toUpperCase() ?? "GET";
73
- const reqHeaders = initToRecord(init2?.headers);
74
- const reqContentType = inferContentType(
75
- reqHeaders["content-type"] ?? reqHeaders["Content-Type"] ?? ""
162
+ function buildRequestInput(input, init2) {
163
+ const { url, method, headers: rawHeaders } = resolveRequest(input, init2);
164
+ const upperMethod = method.toUpperCase();
165
+ const headers = initToRecord(rawHeaders);
166
+ const contentType = inferContentType(
167
+ (findHeader(headers, "content-type") ?? "").toLowerCase()
76
168
  );
77
- const reqBody = parseRequestBody(init2?.body, reqContentType);
169
+ return {
170
+ // Ingest only accepts these methods; others (HEAD, OPTIONS) record as GET.
171
+ method: HTTP_METHODS.has(upperMethod) ? upperMethod : "GET",
172
+ url: truncate(url, MAX_URL_LENGTH),
173
+ headers,
174
+ content_type: contentType,
175
+ body: parseRequestBody(init2?.body, contentType)
176
+ };
177
+ }
178
+ async function buildResponseOutput(response) {
179
+ const { status } = response;
180
+ if (!Number.isInteger(status) || status < 100 || status > 599) {
181
+ return void 0;
182
+ }
183
+ const contentType = (response.headers?.get?.("content-type") ?? "").toLowerCase();
184
+ return {
185
+ status,
186
+ headers: response.headers ? limitHeaders(headerEntries(response.headers)) : {},
187
+ content_type: inferContentType(contentType),
188
+ body: await readResponseBody(response, contentType)
189
+ };
190
+ }
191
+ async function executeFetch(input, init2, recordSpan) {
192
+ let request;
193
+ try {
194
+ request = buildRequestInput(input, init2);
195
+ } catch {
196
+ }
197
+ function record(span) {
198
+ if (!request) return;
199
+ try {
200
+ recordSpan({ input: request, ...span });
201
+ } catch {
202
+ }
203
+ }
78
204
  const start = Date.now();
79
- const response = await globalThis.fetch(url, init2);
205
+ let response;
206
+ try {
207
+ response = await globalThis.fetch(input, init2);
208
+ } catch (err) {
209
+ record({
210
+ durationMs: Date.now() - start,
211
+ description: truncate(
212
+ `Request failed: ${err instanceof Error ? err.message : String(err)}`,
213
+ MAX_DESCRIPTION_LENGTH
214
+ )
215
+ });
216
+ throw err;
217
+ }
80
218
  const durationMs = Date.now() - start;
81
- const resContentType = inferContentType(
82
- response.headers.get("content-type") ?? ""
83
- );
84
- const resBody = await parseResponseBody(response, resContentType);
85
- recordSpan(
86
- {
87
- method: HTTP_METHODS.has(method) ? method : "GET",
88
- url: url.toString(),
89
- headers: reqHeaders,
90
- content_type: reqContentType,
91
- body: reqBody
92
- },
93
- {
94
- status: response.status,
95
- headers: headersToRecord(response.headers),
96
- content_type: resContentType,
97
- body: resBody
98
- },
99
- durationMs
100
- );
101
- return resBody;
219
+ let output;
220
+ try {
221
+ output = await buildResponseOutput(response);
222
+ } catch {
223
+ }
224
+ record({ output, durationMs });
225
+ return response;
102
226
  }
103
227
 
104
228
  // src/lib/redact.ts
@@ -287,14 +411,13 @@ var Trace = class {
287
411
  __privateSet(this, _environment, environment);
288
412
  return this;
289
413
  }
290
- fetch(name, url, init2, options) {
291
- return executeFetch(url, init2, (input, output, durationMs) => {
292
- this.span("http", name, {
293
- input,
294
- output,
295
- durationMs,
296
- redact: options?.redact
297
- });
414
+ /**
415
+ * Traced `fetch`: returns the Response untouched and records the request and
416
+ * response as an http span.
417
+ */
418
+ fetch(name, input, init2, options) {
419
+ return executeFetch(input, init2, (span) => {
420
+ this.span("http", name, { ...span, redact: options?.redact });
298
421
  });
299
422
  }
300
423
  async end() {
@@ -426,6 +549,13 @@ var NOOP_RESULT2 = { traceId: "" };
426
549
  var MAX_RETRIES = 2;
427
550
  var BASE_RETRY_MS = 1e3;
428
551
  var MAX_RETRY_DELAY_MS = 6e4;
552
+ var DEFAULT_TIMEOUT_MS = 3e4;
553
+ var MAX_TIMEOUT_MS = 2 ** 31 - 1;
554
+ function resolveTimeoutMs(value) {
555
+ if (typeof value !== "number" || isNaN(value) || value <= 0)
556
+ return DEFAULT_TIMEOUT_MS;
557
+ return Math.min(value, MAX_TIMEOUT_MS);
558
+ }
429
559
  function retryDelayMs(res, attempt) {
430
560
  const header = res.headers.get("retry-after");
431
561
  if (header) {
@@ -438,17 +568,19 @@ function retryDelayMs(res, attempt) {
438
568
  }
439
569
  return BASE_RETRY_MS * 2 ** (attempt - 1);
440
570
  }
441
- var _apiKey, _apiUrl, _environment2, _disabled2, _log2;
571
+ var _apiKey, _apiUrl, _environment2, _disabled2, _timeoutMs, _log2;
442
572
  var Recount = class {
443
573
  constructor(options = {}) {
444
574
  __privateAdd(this, _apiKey);
445
575
  __privateAdd(this, _apiUrl);
446
576
  __privateAdd(this, _environment2);
447
577
  __privateAdd(this, _disabled2);
578
+ __privateAdd(this, _timeoutMs);
448
579
  __privateAdd(this, _log2);
449
580
  const debug = options.debug ?? !!process.env.RECOUNT_DEBUG;
450
581
  __privateSet(this, _log2, debug ? (msg) => console.log(`[recount] ${msg}`) : () => {
451
582
  });
583
+ __privateSet(this, _timeoutMs, resolveTimeoutMs(options.timeoutMs));
452
584
  const apiKey = options.apiKey ?? process.env.RECOUNT_API_KEY;
453
585
  if (!apiKey) {
454
586
  console.warn("Recount: no API key found - tracing disabled.");
@@ -490,33 +622,61 @@ var Recount = class {
490
622
  "Content-Type": "application/json",
491
623
  "x-api-key": __privateGet(this, _apiKey)
492
624
  };
493
- for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
494
- try {
495
- const res = await globalThis.fetch(`${__privateGet(this, _apiUrl)}/ingest`, {
496
- method: "POST",
497
- headers,
498
- body
499
- });
500
- if (res.status === 429 && attempt < MAX_RETRIES) {
501
- const delay = retryDelayMs(res, attempt + 1);
502
- __privateGet(this, _log2).call(this, `429 rate limited attempt=${attempt + 1}/${MAX_RETRIES + 1} retrying in ${delay}ms`);
503
- await res.body?.cancel().catch(() => {
625
+ const deadline = Date.now() + __privateGet(this, _timeoutMs);
626
+ const controller = new AbortController();
627
+ const timer = setTimeout(() => controller.abort(), __privateGet(this, _timeoutMs));
628
+ try {
629
+ for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
630
+ try {
631
+ const res = await globalThis.fetch(`${__privateGet(this, _apiUrl)}/ingest`, {
632
+ method: "POST",
633
+ headers,
634
+ body,
635
+ signal: controller.signal
504
636
  });
505
- await new Promise((r) => setTimeout(r, delay));
506
- continue;
507
- }
508
- if (!res.ok) {
509
- const text = await res.text().catch(() => res.statusText);
510
- __privateGet(this, _log2).call(this, `ingest error status=${res.status} message=${text}`);
637
+ if (res.status === 429 && attempt < MAX_RETRIES) {
638
+ const delay = retryDelayMs(res, attempt + 1);
639
+ await res.body?.cancel().catch(() => {
640
+ });
641
+ if (Date.now() + delay >= deadline) {
642
+ __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`);
643
+ return NOOP_RESULT2;
644
+ }
645
+ __privateGet(this, _log2).call(this, `429 rate limited attempt=${attempt + 1}/${MAX_RETRIES + 1} retrying in ${delay}ms`);
646
+ await new Promise((r) => setTimeout(r, delay));
647
+ continue;
648
+ }
649
+ if (!res.ok) {
650
+ const text = await res.text().catch(() => res.statusText);
651
+ __privateGet(this, _log2).call(this, `ingest error status=${res.status} message=${text}`);
652
+ return NOOP_RESULT2;
653
+ }
654
+ let json;
655
+ try {
656
+ json = await res.json();
657
+ } catch (err) {
658
+ if (!(err instanceof SyntaxError)) throw err;
659
+ __privateGet(this, _log2).call(this, `ingest error: invalid JSON response (${err.message})`);
660
+ return NOOP_RESULT2;
661
+ }
662
+ const traceId = json?.data?.trace?.id;
663
+ if (typeof traceId !== "string") {
664
+ __privateGet(this, _log2).call(this, `ingest error: unexpected response ${JSON.stringify(json)?.slice(0, 200)}`);
665
+ return NOOP_RESULT2;
666
+ }
667
+ __privateGet(this, _log2).call(this, `ingested traceId=${traceId}`);
668
+ return { traceId };
669
+ } catch (err) {
670
+ if (controller.signal.aborted) {
671
+ __privateGet(this, _log2).call(this, `ingest timed out after ${__privateGet(this, _timeoutMs)}ms`);
672
+ } else {
673
+ __privateGet(this, _log2).call(this, `ingest failed (network error): ${err instanceof Error ? err.message : String(err)}`);
674
+ }
511
675
  return NOOP_RESULT2;
512
676
  }
513
- const json = await res.json();
514
- __privateGet(this, _log2).call(this, `ingested traceId=${json.data.trace.id}`);
515
- return { traceId: json.data.trace.id };
516
- } catch (err) {
517
- __privateGet(this, _log2).call(this, `ingest failed (network error): ${err instanceof Error ? err.message : String(err)}`);
518
- return NOOP_RESULT2;
519
677
  }
678
+ } finally {
679
+ clearTimeout(timer);
520
680
  }
521
681
  return NOOP_RESULT2;
522
682
  }
@@ -525,6 +685,7 @@ _apiKey = new WeakMap();
525
685
  _apiUrl = new WeakMap();
526
686
  _environment2 = new WeakMap();
527
687
  _disabled2 = new WeakMap();
688
+ _timeoutMs = new WeakMap();
528
689
  _log2 = new WeakMap();
529
690
 
530
691
  // src/lib/table.ts
@@ -948,15 +1109,13 @@ function named(name, fn) {
948
1109
  function addMetadata(data) {
949
1110
  getActiveTrace()?.addMetadata(data);
950
1111
  }
951
- function fetch(name, url, init2, options) {
1112
+ function fetch(name, input, init2, options) {
952
1113
  const active = getActiveTrace();
953
- return executeFetch(url, init2, (input, output, durationMs) => {
954
- active?.span("http", name, {
955
- input,
956
- output,
957
- durationMs,
958
- redact: options?.redact
959
- });
1114
+ if (!active) {
1115
+ return globalThis.fetch(input, init2);
1116
+ }
1117
+ return executeFetch(input, init2, (span) => {
1118
+ active.span("http", name, { ...span, redact: options?.redact });
960
1119
  });
961
1120
  }
962
1121
  var recount = {
@@ -986,6 +1145,6 @@ var recount = {
986
1145
  };
987
1146
  var index_default = recount;
988
1147
 
989
- export { SpanGroup, index_default as default };
1148
+ export { REDACTED, Recount, RecountError, SpanGroup, addAlert, addGroup, addMessage, addMetadata, addSpan, chart, chartFromRecords, index_default as default, fetch, init, named, spanCell, startTrace, table, tableFromObject, tableFromRecords, tableSections };
990
1149
  //# sourceMappingURL=index.js.map
991
1150
  //# sourceMappingURL=index.js.map