@stainlessdev/xray-emitter 0.1.0-branch.pedro-unify.20f830a
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 +79 -0
- package/dist/chunk-2NR6RZEP.js +1339 -0
- package/dist/chunk-2NR6RZEP.js.map +1 -0
- package/dist/chunk-3MVVO5I7.cjs +1339 -0
- package/dist/chunk-3MVVO5I7.cjs.map +1 -0
- package/dist/chunk-6UH43LVD.js +281 -0
- package/dist/chunk-6UH43LVD.js.map +1 -0
- package/dist/chunk-AHXNYJ5A.cjs +621 -0
- package/dist/chunk-AHXNYJ5A.cjs.map +1 -0
- package/dist/chunk-GNSXLLEC.cjs +281 -0
- package/dist/chunk-GNSXLLEC.cjs.map +1 -0
- package/dist/chunk-JKW6E4L3.cjs +304 -0
- package/dist/chunk-JKW6E4L3.cjs.map +1 -0
- package/dist/chunk-MPQTI5AX.js +621 -0
- package/dist/chunk-MPQTI5AX.js.map +1 -0
- package/dist/chunk-QUH3LJ5M.cjs +634 -0
- package/dist/chunk-QUH3LJ5M.cjs.map +1 -0
- package/dist/chunk-VGRLHYDA.js +634 -0
- package/dist/chunk-VGRLHYDA.js.map +1 -0
- package/dist/chunk-YVMMCTXW.js +304 -0
- package/dist/chunk-YVMMCTXW.js.map +1 -0
- package/dist/express.cjs +45 -0
- package/dist/express.cjs.map +1 -0
- package/dist/express.d.cts +17 -0
- package/dist/express.d.ts +17 -0
- package/dist/express.js +45 -0
- package/dist/express.js.map +1 -0
- package/dist/fastify.cjs +64 -0
- package/dist/fastify.cjs.map +1 -0
- package/dist/fastify.d.cts +21 -0
- package/dist/fastify.d.ts +21 -0
- package/dist/fastify.js +64 -0
- package/dist/fastify.js.map +1 -0
- package/dist/fetch.cjs +16 -0
- package/dist/fetch.cjs.map +1 -0
- package/dist/fetch.d.cts +19 -0
- package/dist/fetch.d.ts +19 -0
- package/dist/fetch.js +16 -0
- package/dist/fetch.js.map +1 -0
- package/dist/hono.cjs +71 -0
- package/dist/hono.cjs.map +1 -0
- package/dist/hono.d.cts +30 -0
- package/dist/hono.d.ts +30 -0
- package/dist/hono.js +71 -0
- package/dist/hono.js.map +1 -0
- package/dist/index.cjs +12 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.cjs +45 -0
- package/dist/internal.cjs.map +1 -0
- package/dist/internal.d.cts +53 -0
- package/dist/internal.d.ts +53 -0
- package/dist/internal.js +45 -0
- package/dist/internal.js.map +1 -0
- package/dist/next.cjs +30 -0
- package/dist/next.cjs.map +1 -0
- package/dist/next.d.cts +18 -0
- package/dist/next.d.ts +18 -0
- package/dist/next.js +30 -0
- package/dist/next.js.map +1 -0
- package/dist/node.cjs +14 -0
- package/dist/node.cjs.map +1 -0
- package/dist/node.d.cts +20 -0
- package/dist/node.d.ts +20 -0
- package/dist/node.js +14 -0
- package/dist/node.js.map +1 -0
- package/dist/remix.cjs +30 -0
- package/dist/remix.cjs.map +1 -0
- package/dist/remix.d.cts +15 -0
- package/dist/remix.d.ts +15 -0
- package/dist/remix.js +30 -0
- package/dist/remix.js.map +1 -0
- package/dist/types-Z1nirh-F.d.cts +149 -0
- package/dist/types-Z1nirh-F.d.ts +149 -0
- package/package.json +94 -0
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunk3MVVO5I7cjs = require('./chunk-3MVVO5I7.cjs');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
var _chunkGNSXLLECcjs = require('./chunk-GNSXLLEC.cjs');
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
var _chunkJKW6E4L3cjs = require('./chunk-JKW6E4L3.cjs');
|
|
21
|
+
|
|
22
|
+
// src/fetch/emitter.ts
|
|
23
|
+
var _indexjs = require('@opentelemetry/exporter-trace-otlp-proto/build/src/platform/browser/index.js');
|
|
24
|
+
function createEmitter2(config) {
|
|
25
|
+
if (!_optionalChain([config, 'access', _ => _.exporter, 'optionalAccess', _2 => _2.instance])) {
|
|
26
|
+
const hasFetch = typeof globalThis !== "undefined" && typeof globalThis.fetch === "function";
|
|
27
|
+
if (!hasFetch) {
|
|
28
|
+
throw new Error(
|
|
29
|
+
"fetch is required to use the default @stainlessdev/xray-fetch exporter; provide exporter.instance or use @stainlessdev/xray-node instead."
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const resolved = _chunk3MVVO5I7cjs.normalizeConfig.call(void 0, config);
|
|
34
|
+
const exporter = _nullishCoalesce(_optionalChain([config, 'access', _3 => _3.exporter, 'optionalAccess', _4 => _4.instance]), () => ( new (0, _indexjs.OTLPTraceExporter)({
|
|
35
|
+
url: resolved.exporter.endpointUrl,
|
|
36
|
+
headers: _nullishCoalesce(resolved.exporter.headers, () => ( {})),
|
|
37
|
+
timeoutMillis: resolved.exporter.timeoutMs
|
|
38
|
+
})));
|
|
39
|
+
return _chunk3MVVO5I7cjs.createEmitter.call(void 0, config, exporter);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// src/fetch/adapter.ts
|
|
43
|
+
function wrapFetch(handler, xray, options) {
|
|
44
|
+
return async (req) => {
|
|
45
|
+
const normalizedRequest = {
|
|
46
|
+
method: req.method,
|
|
47
|
+
url: req.url,
|
|
48
|
+
route: _optionalChain([options, 'optionalAccess', _5 => _5.route]),
|
|
49
|
+
headers: _chunkGNSXLLECcjs.headerValuesFromFetchHeaders.call(void 0, req.headers),
|
|
50
|
+
requestId: _optionalChain([options, 'optionalAccess', _6 => _6.requestId]),
|
|
51
|
+
startTimeMs: Date.now()
|
|
52
|
+
};
|
|
53
|
+
const ctx = xray.startRequest(normalizedRequest);
|
|
54
|
+
_chunkGNSXLLECcjs.bindContextToObject.call(void 0, req, ctx);
|
|
55
|
+
if (_optionalChain([options, 'optionalAccess', _7 => _7.requestId])) {
|
|
56
|
+
_chunkGNSXLLECcjs.setContextRequestId.call(void 0, ctx, options.requestId);
|
|
57
|
+
}
|
|
58
|
+
if (_optionalChain([options, 'optionalAccess', _8 => _8.route])) {
|
|
59
|
+
_chunkGNSXLLECcjs.setContextRoute.call(void 0, ctx, options.route);
|
|
60
|
+
}
|
|
61
|
+
if (_optionalChain([options, 'optionalAccess', _9 => _9.capture])) {
|
|
62
|
+
_chunkGNSXLLECcjs.setCaptureOverride.call(void 0, ctx, options.capture);
|
|
63
|
+
}
|
|
64
|
+
if (_optionalChain([options, 'optionalAccess', _10 => _10.redaction])) {
|
|
65
|
+
_chunkGNSXLLECcjs.setRedactionOverride.call(void 0, ctx, options.redaction);
|
|
66
|
+
}
|
|
67
|
+
if (_optionalChain([options, 'optionalAccess', _11 => _11.onRequest])) {
|
|
68
|
+
try {
|
|
69
|
+
options.onRequest(ctx);
|
|
70
|
+
} catch (err) {
|
|
71
|
+
_chunkJKW6E4L3cjs.logWithLevel.call(void 0, xray.config.logger, "warn", xray.config.logLevel, "xray: onRequest failed", {
|
|
72
|
+
error: err instanceof Error ? err.message : String(err)
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
const capture = _optionalChain([options, 'optionalAccess', _12 => _12.capture]) ? { ...xray.config.capture, ...options.capture } : xray.config.capture;
|
|
77
|
+
const requestCapture = capture.requestBody === "none" ? null : wrapRequestBody(req, capture.maxBodyBytes);
|
|
78
|
+
const requestForHandler = _nullishCoalesce(_optionalChain([requestCapture, 'optionalAccess', _13 => _13.request]), () => ( req));
|
|
79
|
+
if (requestForHandler !== req) {
|
|
80
|
+
_chunkGNSXLLECcjs.bindContextToObject.call(void 0, requestForHandler, ctx);
|
|
81
|
+
}
|
|
82
|
+
let response;
|
|
83
|
+
try {
|
|
84
|
+
response = await handler(requestForHandler);
|
|
85
|
+
} catch (err) {
|
|
86
|
+
const log = xray.endRequest(
|
|
87
|
+
ctx,
|
|
88
|
+
{
|
|
89
|
+
statusCode: void 0,
|
|
90
|
+
headers: void 0,
|
|
91
|
+
body: void 0,
|
|
92
|
+
endTimeMs: Date.now()
|
|
93
|
+
},
|
|
94
|
+
err
|
|
95
|
+
);
|
|
96
|
+
if (_optionalChain([options, 'optionalAccess', _14 => _14.onError])) {
|
|
97
|
+
try {
|
|
98
|
+
options.onError(ctx, err);
|
|
99
|
+
} catch (errInner) {
|
|
100
|
+
_chunkJKW6E4L3cjs.logWithLevel.call(void 0, xray.config.logger, "warn", xray.config.logLevel, "xray: onError failed", {
|
|
101
|
+
error: errInner instanceof Error ? errInner.message : String(errInner)
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (_optionalChain([options, 'optionalAccess', _15 => _15.onResponse])) {
|
|
106
|
+
try {
|
|
107
|
+
options.onResponse(ctx, log);
|
|
108
|
+
} catch (errInner) {
|
|
109
|
+
_chunkJKW6E4L3cjs.logWithLevel.call(void 0,
|
|
110
|
+
xray.config.logger,
|
|
111
|
+
"warn",
|
|
112
|
+
xray.config.logLevel,
|
|
113
|
+
"xray: onResponse failed",
|
|
114
|
+
{
|
|
115
|
+
error: errInner instanceof Error ? errInner.message : String(errInner)
|
|
116
|
+
}
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
throw err;
|
|
121
|
+
}
|
|
122
|
+
if (!(response instanceof Response)) {
|
|
123
|
+
const log = xray.endRequest(ctx, {
|
|
124
|
+
statusCode: void 0,
|
|
125
|
+
headers: void 0,
|
|
126
|
+
body: void 0,
|
|
127
|
+
endTimeMs: Date.now()
|
|
128
|
+
});
|
|
129
|
+
if (_optionalChain([options, 'optionalAccess', _16 => _16.onResponse])) {
|
|
130
|
+
try {
|
|
131
|
+
options.onResponse(ctx, log);
|
|
132
|
+
} catch (err) {
|
|
133
|
+
_chunkJKW6E4L3cjs.logWithLevel.call(void 0,
|
|
134
|
+
xray.config.logger,
|
|
135
|
+
"warn",
|
|
136
|
+
xray.config.logLevel,
|
|
137
|
+
"xray: onResponse failed",
|
|
138
|
+
{
|
|
139
|
+
error: err instanceof Error ? err.message : String(err)
|
|
140
|
+
}
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return response;
|
|
145
|
+
}
|
|
146
|
+
const responseHeaders = new Headers(response.headers);
|
|
147
|
+
ensureResponseRequestIdHeaders(responseHeaders, ctx, xray);
|
|
148
|
+
const statusCode = response.status;
|
|
149
|
+
const isUpgrade = _chunkGNSXLLECcjs.isWebsocketUpgradeFetch.call(void 0, statusCode, req.headers, responseHeaders);
|
|
150
|
+
if (!response.body || isUpgrade || capture.responseBody === "none") {
|
|
151
|
+
const log = finalizeResponse(
|
|
152
|
+
ctx,
|
|
153
|
+
xray,
|
|
154
|
+
normalizedRequest,
|
|
155
|
+
capture,
|
|
156
|
+
_nullishCoalesce(_optionalChain([requestCapture, 'optionalAccess', _17 => _17.capture]), () => ( null)),
|
|
157
|
+
responseHeaders,
|
|
158
|
+
statusCode,
|
|
159
|
+
null
|
|
160
|
+
);
|
|
161
|
+
if (_optionalChain([options, 'optionalAccess', _18 => _18.onResponse])) {
|
|
162
|
+
try {
|
|
163
|
+
options.onResponse(ctx, log);
|
|
164
|
+
} catch (err) {
|
|
165
|
+
_chunkJKW6E4L3cjs.logWithLevel.call(void 0,
|
|
166
|
+
xray.config.logger,
|
|
167
|
+
"warn",
|
|
168
|
+
xray.config.logLevel,
|
|
169
|
+
"xray: onResponse failed",
|
|
170
|
+
{
|
|
171
|
+
error: err instanceof Error ? err.message : String(err)
|
|
172
|
+
}
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return new Response(response.body, {
|
|
177
|
+
headers: responseHeaders,
|
|
178
|
+
status: response.status,
|
|
179
|
+
statusText: response.statusText
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
const responseCapture = new (0, _chunkGNSXLLECcjs.LimitedBuffer)(capture.maxBodyBytes);
|
|
183
|
+
let finished = false;
|
|
184
|
+
const finalize = () => {
|
|
185
|
+
if (finished) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
finished = true;
|
|
189
|
+
const log = finalizeResponse(
|
|
190
|
+
ctx,
|
|
191
|
+
xray,
|
|
192
|
+
normalizedRequest,
|
|
193
|
+
capture,
|
|
194
|
+
_nullishCoalesce(_optionalChain([requestCapture, 'optionalAccess', _19 => _19.capture]), () => ( null)),
|
|
195
|
+
responseHeaders,
|
|
196
|
+
statusCode,
|
|
197
|
+
responseCapture
|
|
198
|
+
);
|
|
199
|
+
if (_optionalChain([options, 'optionalAccess', _20 => _20.onResponse])) {
|
|
200
|
+
try {
|
|
201
|
+
options.onResponse(ctx, log);
|
|
202
|
+
} catch (err) {
|
|
203
|
+
_chunkJKW6E4L3cjs.logWithLevel.call(void 0,
|
|
204
|
+
xray.config.logger,
|
|
205
|
+
"warn",
|
|
206
|
+
xray.config.logLevel,
|
|
207
|
+
"xray: onResponse failed",
|
|
208
|
+
{
|
|
209
|
+
error: err instanceof Error ? err.message : String(err)
|
|
210
|
+
}
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
const wrappedBody = wrapReadableStream(response.body, responseCapture, finalize, () => {
|
|
216
|
+
finalize();
|
|
217
|
+
});
|
|
218
|
+
return new Response(wrappedBody, {
|
|
219
|
+
headers: responseHeaders,
|
|
220
|
+
status: response.status,
|
|
221
|
+
statusText: response.statusText
|
|
222
|
+
});
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
function wrapFetchPreserve(handler, xray, options) {
|
|
226
|
+
return async (req) => {
|
|
227
|
+
const normalizedRequest = {
|
|
228
|
+
method: req.method,
|
|
229
|
+
url: req.url,
|
|
230
|
+
route: _optionalChain([options, 'optionalAccess', _21 => _21.route]),
|
|
231
|
+
headers: _chunkGNSXLLECcjs.headerValuesFromFetchHeaders.call(void 0, req.headers),
|
|
232
|
+
requestId: _optionalChain([options, 'optionalAccess', _22 => _22.requestId]),
|
|
233
|
+
startTimeMs: Date.now()
|
|
234
|
+
};
|
|
235
|
+
const ctx = xray.startRequest(normalizedRequest);
|
|
236
|
+
_chunkGNSXLLECcjs.bindContextToObject.call(void 0, req, ctx);
|
|
237
|
+
if (_optionalChain([options, 'optionalAccess', _23 => _23.requestId])) {
|
|
238
|
+
_chunkGNSXLLECcjs.setContextRequestId.call(void 0, ctx, options.requestId);
|
|
239
|
+
}
|
|
240
|
+
if (_optionalChain([options, 'optionalAccess', _24 => _24.route])) {
|
|
241
|
+
_chunkGNSXLLECcjs.setContextRoute.call(void 0, ctx, options.route);
|
|
242
|
+
}
|
|
243
|
+
if (_optionalChain([options, 'optionalAccess', _25 => _25.capture])) {
|
|
244
|
+
_chunkGNSXLLECcjs.setCaptureOverride.call(void 0, ctx, options.capture);
|
|
245
|
+
}
|
|
246
|
+
if (_optionalChain([options, 'optionalAccess', _26 => _26.redaction])) {
|
|
247
|
+
_chunkGNSXLLECcjs.setRedactionOverride.call(void 0, ctx, options.redaction);
|
|
248
|
+
}
|
|
249
|
+
if (_optionalChain([options, 'optionalAccess', _27 => _27.onRequest])) {
|
|
250
|
+
try {
|
|
251
|
+
options.onRequest(ctx);
|
|
252
|
+
} catch (err) {
|
|
253
|
+
_chunkJKW6E4L3cjs.logWithLevel.call(void 0, xray.config.logger, "warn", xray.config.logLevel, "xray: onRequest failed", {
|
|
254
|
+
error: err instanceof Error ? err.message : String(err)
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
const capture = _optionalChain([options, 'optionalAccess', _28 => _28.capture]) ? { ...xray.config.capture, ...options.capture } : xray.config.capture;
|
|
259
|
+
const requestCapturePromise = capture.requestBody === "none" ? Promise.resolve(null) : captureRequestClone(req, capture.maxBodyBytes, xray);
|
|
260
|
+
let response;
|
|
261
|
+
try {
|
|
262
|
+
response = await handler(req);
|
|
263
|
+
} catch (err) {
|
|
264
|
+
const log = xray.endRequest(
|
|
265
|
+
ctx,
|
|
266
|
+
{
|
|
267
|
+
statusCode: void 0,
|
|
268
|
+
headers: void 0,
|
|
269
|
+
body: void 0,
|
|
270
|
+
endTimeMs: Date.now()
|
|
271
|
+
},
|
|
272
|
+
err
|
|
273
|
+
);
|
|
274
|
+
if (_optionalChain([options, 'optionalAccess', _29 => _29.onError])) {
|
|
275
|
+
try {
|
|
276
|
+
options.onError(ctx, err);
|
|
277
|
+
} catch (errInner) {
|
|
278
|
+
_chunkJKW6E4L3cjs.logWithLevel.call(void 0, xray.config.logger, "warn", xray.config.logLevel, "xray: onError failed", {
|
|
279
|
+
error: errInner instanceof Error ? errInner.message : String(errInner)
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
if (_optionalChain([options, 'optionalAccess', _30 => _30.onResponse])) {
|
|
284
|
+
try {
|
|
285
|
+
options.onResponse(ctx, log);
|
|
286
|
+
} catch (errInner) {
|
|
287
|
+
_chunkJKW6E4L3cjs.logWithLevel.call(void 0,
|
|
288
|
+
xray.config.logger,
|
|
289
|
+
"warn",
|
|
290
|
+
xray.config.logLevel,
|
|
291
|
+
"xray: onResponse failed",
|
|
292
|
+
{
|
|
293
|
+
error: errInner instanceof Error ? errInner.message : String(errInner)
|
|
294
|
+
}
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
throw err;
|
|
299
|
+
}
|
|
300
|
+
if (!(response instanceof Response)) {
|
|
301
|
+
const log = xray.endRequest(ctx, {
|
|
302
|
+
statusCode: void 0,
|
|
303
|
+
headers: void 0,
|
|
304
|
+
body: void 0,
|
|
305
|
+
endTimeMs: Date.now()
|
|
306
|
+
});
|
|
307
|
+
if (_optionalChain([options, 'optionalAccess', _31 => _31.onResponse])) {
|
|
308
|
+
try {
|
|
309
|
+
options.onResponse(ctx, log);
|
|
310
|
+
} catch (err) {
|
|
311
|
+
_chunkJKW6E4L3cjs.logWithLevel.call(void 0,
|
|
312
|
+
xray.config.logger,
|
|
313
|
+
"warn",
|
|
314
|
+
xray.config.logLevel,
|
|
315
|
+
"xray: onResponse failed",
|
|
316
|
+
{
|
|
317
|
+
error: err instanceof Error ? err.message : String(err)
|
|
318
|
+
}
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return response;
|
|
323
|
+
}
|
|
324
|
+
const responseHeaders = new Headers(response.headers);
|
|
325
|
+
const ensured = ensureResponseRequestIdHeaders(responseHeaders, ctx, xray);
|
|
326
|
+
if (ensured.set) {
|
|
327
|
+
const headerName = canonicalHeaderName(xray.config.requestId.header);
|
|
328
|
+
try {
|
|
329
|
+
response.headers.set(headerName, _nullishCoalesce(ensured.value, () => ( "")));
|
|
330
|
+
} catch (e) {
|
|
331
|
+
response = new Response(response.body, {
|
|
332
|
+
headers: responseHeaders,
|
|
333
|
+
status: response.status,
|
|
334
|
+
statusText: response.statusText
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
const statusCode = response.status;
|
|
339
|
+
const isUpgrade = _chunkGNSXLLECcjs.isWebsocketUpgradeFetch.call(void 0, statusCode, req.headers, responseHeaders);
|
|
340
|
+
const responseCapturePromise = !response.body || isUpgrade || capture.responseBody === "none" ? Promise.resolve(null) : captureResponseClone(response, capture.maxBodyBytes, xray);
|
|
341
|
+
void (async () => {
|
|
342
|
+
const [requestCapture, responseCapture] = await Promise.all([
|
|
343
|
+
requestCapturePromise,
|
|
344
|
+
responseCapturePromise
|
|
345
|
+
]);
|
|
346
|
+
const log = finalizeResponse(
|
|
347
|
+
ctx,
|
|
348
|
+
xray,
|
|
349
|
+
normalizedRequest,
|
|
350
|
+
capture,
|
|
351
|
+
requestCapture,
|
|
352
|
+
responseHeaders,
|
|
353
|
+
statusCode,
|
|
354
|
+
responseCapture
|
|
355
|
+
);
|
|
356
|
+
if (_optionalChain([options, 'optionalAccess', _32 => _32.onResponse])) {
|
|
357
|
+
try {
|
|
358
|
+
options.onResponse(ctx, log);
|
|
359
|
+
} catch (err) {
|
|
360
|
+
_chunkJKW6E4L3cjs.logWithLevel.call(void 0,
|
|
361
|
+
xray.config.logger,
|
|
362
|
+
"warn",
|
|
363
|
+
xray.config.logLevel,
|
|
364
|
+
"xray: onResponse failed",
|
|
365
|
+
{
|
|
366
|
+
error: err instanceof Error ? err.message : String(err)
|
|
367
|
+
}
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
})().catch((err) => {
|
|
372
|
+
_chunkJKW6E4L3cjs.logWithLevel.call(void 0,
|
|
373
|
+
xray.config.logger,
|
|
374
|
+
"warn",
|
|
375
|
+
xray.config.logLevel,
|
|
376
|
+
"xray: response capture failed",
|
|
377
|
+
{
|
|
378
|
+
error: err instanceof Error ? err.message : String(err)
|
|
379
|
+
}
|
|
380
|
+
);
|
|
381
|
+
});
|
|
382
|
+
return response;
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
function getXrayContext(req) {
|
|
386
|
+
return _chunkGNSXLLECcjs.getXrayContextFromObject.call(void 0, req);
|
|
387
|
+
}
|
|
388
|
+
function wrapRequestBody(request, limit) {
|
|
389
|
+
if (limit <= 0 || !request.body || request.bodyUsed) {
|
|
390
|
+
return { capture: null, request };
|
|
391
|
+
}
|
|
392
|
+
const originalBody = request.body;
|
|
393
|
+
const capture = {
|
|
394
|
+
buffer: new (0, _chunkGNSXLLECcjs.LimitedBuffer)(limit),
|
|
395
|
+
read: false
|
|
396
|
+
};
|
|
397
|
+
let controller = null;
|
|
398
|
+
let started = false;
|
|
399
|
+
let reader = null;
|
|
400
|
+
const wrappedStream = new ReadableStream({
|
|
401
|
+
start(c) {
|
|
402
|
+
controller = c;
|
|
403
|
+
},
|
|
404
|
+
async cancel(reason) {
|
|
405
|
+
if (!started) {
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
try {
|
|
409
|
+
await _optionalChain([reader, 'optionalAccess', _33 => _33.cancel, 'call', _34 => _34(reason)]);
|
|
410
|
+
} finally {
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
const startPump = () => {
|
|
415
|
+
if (started) {
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
started = true;
|
|
419
|
+
reader = originalBody.getReader();
|
|
420
|
+
void (async () => {
|
|
421
|
+
try {
|
|
422
|
+
while (true) {
|
|
423
|
+
const { done, value } = await reader.read();
|
|
424
|
+
if (done) {
|
|
425
|
+
_optionalChain([controller, 'optionalAccess', _35 => _35.close, 'call', _36 => _36()]);
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
if (value) {
|
|
429
|
+
capture.read = true;
|
|
430
|
+
capture.buffer.write(value);
|
|
431
|
+
_optionalChain([controller, 'optionalAccess', _37 => _37.enqueue, 'call', _38 => _38(value)]);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
} catch (err) {
|
|
435
|
+
_optionalChain([controller, 'optionalAccess', _39 => _39.error, 'call', _40 => _40(err)]);
|
|
436
|
+
}
|
|
437
|
+
})();
|
|
438
|
+
};
|
|
439
|
+
const originalGetReader = wrappedStream.getReader.bind(wrappedStream);
|
|
440
|
+
wrappedStream.getReader = (...args) => {
|
|
441
|
+
startPump();
|
|
442
|
+
return originalGetReader(...args);
|
|
443
|
+
};
|
|
444
|
+
const init = {
|
|
445
|
+
body: wrappedStream,
|
|
446
|
+
cache: request.cache,
|
|
447
|
+
credentials: request.credentials,
|
|
448
|
+
headers: request.headers,
|
|
449
|
+
integrity: request.integrity,
|
|
450
|
+
keepalive: request.keepalive,
|
|
451
|
+
method: request.method,
|
|
452
|
+
mode: request.mode,
|
|
453
|
+
redirect: request.redirect,
|
|
454
|
+
referrer: request.referrer,
|
|
455
|
+
referrerPolicy: request.referrerPolicy,
|
|
456
|
+
signal: request.signal
|
|
457
|
+
};
|
|
458
|
+
if (isNodeRuntime()) {
|
|
459
|
+
init.duplex = "half";
|
|
460
|
+
}
|
|
461
|
+
const wrappedRequest = new Request(request.url, init);
|
|
462
|
+
return { capture, request: wrappedRequest };
|
|
463
|
+
}
|
|
464
|
+
async function captureRequestClone(request, limit, xray) {
|
|
465
|
+
if (limit <= 0 || !request.body || request.bodyUsed) {
|
|
466
|
+
return null;
|
|
467
|
+
}
|
|
468
|
+
let clone;
|
|
469
|
+
try {
|
|
470
|
+
clone = request.clone();
|
|
471
|
+
} catch (e2) {
|
|
472
|
+
_chunkJKW6E4L3cjs.logWithLevel.call(void 0, xray.config.logger, "warn", xray.config.logLevel, "xray: request clone failed");
|
|
473
|
+
return null;
|
|
474
|
+
}
|
|
475
|
+
const buffer = new (0, _chunkGNSXLLECcjs.LimitedBuffer)(limit);
|
|
476
|
+
await readStreamToBuffer(clone.body, buffer);
|
|
477
|
+
return buffer;
|
|
478
|
+
}
|
|
479
|
+
async function captureResponseClone(response, limit, xray) {
|
|
480
|
+
if (limit <= 0 || !response.body || response.bodyUsed) {
|
|
481
|
+
return null;
|
|
482
|
+
}
|
|
483
|
+
let clone;
|
|
484
|
+
try {
|
|
485
|
+
clone = response.clone();
|
|
486
|
+
} catch (e3) {
|
|
487
|
+
_chunkJKW6E4L3cjs.logWithLevel.call(void 0, xray.config.logger, "warn", xray.config.logLevel, "xray: response clone failed");
|
|
488
|
+
return null;
|
|
489
|
+
}
|
|
490
|
+
const buffer = new (0, _chunkGNSXLLECcjs.LimitedBuffer)(limit);
|
|
491
|
+
await readStreamToBuffer(clone.body, buffer);
|
|
492
|
+
return buffer;
|
|
493
|
+
}
|
|
494
|
+
async function readStreamToBuffer(stream, buffer) {
|
|
495
|
+
if (!stream) {
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
const reader = stream.getReader();
|
|
499
|
+
try {
|
|
500
|
+
while (true) {
|
|
501
|
+
const { done, value } = await reader.read();
|
|
502
|
+
if (done) {
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
if (value) {
|
|
506
|
+
buffer.write(value);
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
} finally {
|
|
510
|
+
try {
|
|
511
|
+
reader.releaseLock();
|
|
512
|
+
} catch (e4) {
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
function wrapReadableStream(stream, capture, onFinish, onError) {
|
|
517
|
+
const reader = stream.getReader();
|
|
518
|
+
return new ReadableStream({
|
|
519
|
+
async pull(controller) {
|
|
520
|
+
try {
|
|
521
|
+
const { done, value } = await reader.read();
|
|
522
|
+
if (done) {
|
|
523
|
+
controller.close();
|
|
524
|
+
onFinish();
|
|
525
|
+
try {
|
|
526
|
+
reader.releaseLock();
|
|
527
|
+
} catch (e5) {
|
|
528
|
+
}
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
if (value) {
|
|
532
|
+
capture.write(value);
|
|
533
|
+
controller.enqueue(value);
|
|
534
|
+
}
|
|
535
|
+
} catch (err) {
|
|
536
|
+
controller.error(err);
|
|
537
|
+
_optionalChain([onError, 'optionalCall', _41 => _41(err)]);
|
|
538
|
+
onFinish();
|
|
539
|
+
try {
|
|
540
|
+
reader.releaseLock();
|
|
541
|
+
} catch (e6) {
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
},
|
|
545
|
+
async cancel(reason) {
|
|
546
|
+
try {
|
|
547
|
+
await reader.cancel(reason);
|
|
548
|
+
} finally {
|
|
549
|
+
onFinish();
|
|
550
|
+
try {
|
|
551
|
+
reader.releaseLock();
|
|
552
|
+
} catch (e7) {
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
function finalizeResponse(ctx, xray, normalizedRequest, capture, requestCapture, responseHeaders, statusCode, responseCapture) {
|
|
559
|
+
if (requestCapture) {
|
|
560
|
+
const buffer = requestCapture instanceof _chunkGNSXLLECcjs.LimitedBuffer ? requestCapture : requestCapture.buffer;
|
|
561
|
+
const read = requestCapture instanceof _chunkGNSXLLECcjs.LimitedBuffer ? true : requestCapture.read;
|
|
562
|
+
if (read) {
|
|
563
|
+
normalizedRequest.body = _chunkJKW6E4L3cjs.makeCapturedBody.call(void 0,
|
|
564
|
+
buffer.bytes(),
|
|
565
|
+
buffer.totalBytes(),
|
|
566
|
+
buffer.truncated(),
|
|
567
|
+
capture.requestBody === "text" ? "text" : "base64"
|
|
568
|
+
);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
const responseBody = responseCapture ? _chunkJKW6E4L3cjs.makeCapturedBody.call(void 0,
|
|
572
|
+
responseCapture.bytes(),
|
|
573
|
+
responseCapture.totalBytes(),
|
|
574
|
+
responseCapture.truncated(),
|
|
575
|
+
capture.responseBody === "text" ? "text" : "base64"
|
|
576
|
+
) : void 0;
|
|
577
|
+
return xray.endRequest(ctx, {
|
|
578
|
+
statusCode,
|
|
579
|
+
headers: _chunkGNSXLLECcjs.headerValuesFromFetchHeaders.call(void 0, responseHeaders),
|
|
580
|
+
body: responseBody,
|
|
581
|
+
endTimeMs: Date.now()
|
|
582
|
+
});
|
|
583
|
+
}
|
|
584
|
+
function ensureResponseRequestIdHeaders(headers, ctx, xray) {
|
|
585
|
+
const headerName = xray.config.requestId.header;
|
|
586
|
+
const existing = normalizeRequestIdCandidate(_nullishCoalesce(headers.get(headerName), () => ( void 0)));
|
|
587
|
+
if (existing) {
|
|
588
|
+
return { value: existing, set: false };
|
|
589
|
+
}
|
|
590
|
+
const explicit = normalizeRequestIdCandidate(ctx.requestId);
|
|
591
|
+
if (explicit) {
|
|
592
|
+
headers.set(canonicalHeaderName(headerName), explicit);
|
|
593
|
+
return { value: explicit, set: true };
|
|
594
|
+
}
|
|
595
|
+
const generated = _chunkJKW6E4L3cjs.generateRequestId.call(void 0, );
|
|
596
|
+
headers.set(canonicalHeaderName(headerName), generated);
|
|
597
|
+
_chunkGNSXLLECcjs.setContextRequestId.call(void 0, ctx, generated);
|
|
598
|
+
return { value: generated, set: true };
|
|
599
|
+
}
|
|
600
|
+
function isNodeRuntime() {
|
|
601
|
+
const maybeProcess = globalThis.process;
|
|
602
|
+
return !!_optionalChain([maybeProcess, 'optionalAccess', _42 => _42.versions, 'optionalAccess', _43 => _43.node]);
|
|
603
|
+
}
|
|
604
|
+
function normalizeRequestIdCandidate(value) {
|
|
605
|
+
if (!value) {
|
|
606
|
+
return void 0;
|
|
607
|
+
}
|
|
608
|
+
const trimmed = value.trim();
|
|
609
|
+
return trimmed ? trimmed : void 0;
|
|
610
|
+
}
|
|
611
|
+
function canonicalHeaderName(headerName) {
|
|
612
|
+
return headerName.split("-").filter(Boolean).map((part) => part ? part[0].toUpperCase() + part.slice(1) : part).join("-");
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
|
|
616
|
+
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
exports.createEmitter = createEmitter2; exports.wrapFetch = wrapFetch; exports.wrapFetchPreserve = wrapFetchPreserve; exports.getXrayContext = getXrayContext;
|
|
621
|
+
//# sourceMappingURL=chunk-AHXNYJ5A.cjs.map
|