@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.cjs +188 -67
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -4
- package/dist/index.d.ts +8 -4
- package/dist/index.js +188 -67
- package/dist/index.js.map +1 -1
- package/dist/integrations/mongodb.cjs +59 -21
- package/dist/integrations/mongodb.cjs.map +1 -1
- package/dist/integrations/mongodb.js +59 -21
- package/dist/integrations/mongodb.js.map +1 -1
- package/llms.md +7 -3
- package/package.json +1 -1
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
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
|
78
|
+
return limitHeaders(headerEntries(headers));
|
|
43
79
|
}
|
|
44
80
|
if (Array.isArray(headers)) {
|
|
45
|
-
return
|
|
81
|
+
return limitHeaders(headers);
|
|
82
|
+
}
|
|
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 {
|
|
46
104
|
}
|
|
47
|
-
return
|
|
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
|
|
72
|
-
if (
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
80
|
-
|
|
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
|
|
169
|
+
return {
|
|
170
|
+
url: input.toString(),
|
|
171
|
+
method: init2?.method ?? "GET",
|
|
172
|
+
headers: init2?.headers
|
|
173
|
+
};
|
|
83
174
|
}
|
|
84
|
-
|
|
85
|
-
const method = init2
|
|
86
|
-
const
|
|
87
|
-
const
|
|
88
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
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() {
|
|
@@ -977,15 +1100,13 @@ function named(name, fn) {
|
|
|
977
1100
|
function addMetadata(data) {
|
|
978
1101
|
getActiveTrace()?.addMetadata(data);
|
|
979
1102
|
}
|
|
980
|
-
function fetch(name,
|
|
1103
|
+
function fetch(name, input, init2, options) {
|
|
981
1104
|
const active = getActiveTrace();
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
redact: options?.redact
|
|
988
|
-
});
|
|
1105
|
+
if (!active) {
|
|
1106
|
+
return globalThis.fetch(input, init2);
|
|
1107
|
+
}
|
|
1108
|
+
return executeFetch(input, init2, (span) => {
|
|
1109
|
+
active.span("http", name, { ...span, redact: options?.redact });
|
|
989
1110
|
});
|
|
990
1111
|
}
|
|
991
1112
|
var recount = {
|