@refactorlabs/recount 0.7.1 → 0.8.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);
69
+ }
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 {
33
91
  }
34
- return { ...headers };
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() {
@@ -948,15 +1071,13 @@ function named(name, fn) {
948
1071
  function addMetadata(data) {
949
1072
  getActiveTrace()?.addMetadata(data);
950
1073
  }
951
- function fetch(name, url, init2, options) {
1074
+ function fetch(name, input, init2, options) {
952
1075
  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
- });
1076
+ if (!active) {
1077
+ return globalThis.fetch(input, init2);
1078
+ }
1079
+ return executeFetch(input, init2, (span) => {
1080
+ active.span("http", name, { ...span, redact: options?.redact });
960
1081
  });
961
1082
  }
962
1083
  var recount = {