@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,634 @@
|
|
|
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/node/emitter.ts
|
|
23
|
+
var _exportertraceotlpproto = require('@opentelemetry/exporter-trace-otlp-proto');
|
|
24
|
+
function createEmitter2(config) {
|
|
25
|
+
const resolved = _chunk3MVVO5I7cjs.normalizeConfig.call(void 0, config);
|
|
26
|
+
const exporter = _nullishCoalesce(_optionalChain([config, 'access', _ => _.exporter, 'optionalAccess', _2 => _2.instance]), () => ( new (0, _exportertraceotlpproto.OTLPTraceExporter)({
|
|
27
|
+
url: resolved.exporter.endpointUrl,
|
|
28
|
+
headers: _nullishCoalesce(resolved.exporter.headers, () => ( {})),
|
|
29
|
+
timeoutMillis: resolved.exporter.timeoutMs
|
|
30
|
+
})));
|
|
31
|
+
return _chunk3MVVO5I7cjs.createEmitter.call(void 0, config, exporter);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/node/adapter.ts
|
|
35
|
+
var _tls = require('tls');
|
|
36
|
+
function wrapHttpHandler(handler, xray, options) {
|
|
37
|
+
return (req, res) => {
|
|
38
|
+
const normalizedRequest = {
|
|
39
|
+
method: _nullishCoalesce(req.method, () => ( "GET")),
|
|
40
|
+
url: fullUrl(req),
|
|
41
|
+
route: _optionalChain([options, 'optionalAccess', _3 => _3.route]),
|
|
42
|
+
headers: _chunkGNSXLLECcjs.headerValuesFromNodeHeaders.call(void 0,
|
|
43
|
+
req.headers
|
|
44
|
+
),
|
|
45
|
+
requestId: _optionalChain([options, 'optionalAccess', _4 => _4.requestId]),
|
|
46
|
+
remoteAddress: _optionalChain([req, 'access', _5 => _5.socket, 'optionalAccess', _6 => _6.remoteAddress]),
|
|
47
|
+
startTimeMs: Date.now()
|
|
48
|
+
};
|
|
49
|
+
trackExpressParams(req);
|
|
50
|
+
const ctx = xray.startRequest(normalizedRequest);
|
|
51
|
+
_chunkGNSXLLECcjs.bindContextToObject.call(void 0, req, ctx);
|
|
52
|
+
_chunkGNSXLLECcjs.bindContextToObject.call(void 0, res, ctx);
|
|
53
|
+
if (_optionalChain([options, 'optionalAccess', _7 => _7.requestId])) {
|
|
54
|
+
_chunkGNSXLLECcjs.setContextRequestId.call(void 0, ctx, options.requestId);
|
|
55
|
+
}
|
|
56
|
+
if (_optionalChain([options, 'optionalAccess', _8 => _8.route])) {
|
|
57
|
+
_chunkGNSXLLECcjs.setContextRoute.call(void 0, ctx, options.route);
|
|
58
|
+
}
|
|
59
|
+
if (_optionalChain([options, 'optionalAccess', _9 => _9.capture])) {
|
|
60
|
+
_chunkGNSXLLECcjs.setCaptureOverride.call(void 0, ctx, options.capture);
|
|
61
|
+
}
|
|
62
|
+
if (_optionalChain([options, 'optionalAccess', _10 => _10.redaction])) {
|
|
63
|
+
_chunkGNSXLLECcjs.setRedactionOverride.call(void 0, ctx, options.redaction);
|
|
64
|
+
}
|
|
65
|
+
if (_optionalChain([options, 'optionalAccess', _11 => _11.onRequest])) {
|
|
66
|
+
try {
|
|
67
|
+
options.onRequest(ctx);
|
|
68
|
+
} catch (err) {
|
|
69
|
+
_chunkJKW6E4L3cjs.logWithLevel.call(void 0, xray.config.logger, "warn", xray.config.logLevel, "xray: onRequest failed", {
|
|
70
|
+
error: err instanceof Error ? err.message : String(err)
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const capture = _optionalChain([options, 'optionalAccess', _12 => _12.capture]) ? { ...xray.config.capture, ...options.capture } : xray.config.capture;
|
|
75
|
+
const requestCapture = capture.requestBody === "none" ? null : wrapRequestBody(req, capture.maxBodyBytes);
|
|
76
|
+
const recorder = new ResponseRecorder(
|
|
77
|
+
capture.responseBody !== "none",
|
|
78
|
+
capture.maxBodyBytes,
|
|
79
|
+
(response) => {
|
|
80
|
+
ensureResponseRequestId(response, ctx, xray);
|
|
81
|
+
}
|
|
82
|
+
);
|
|
83
|
+
recorder.wrap(res);
|
|
84
|
+
let finished = false;
|
|
85
|
+
let capturedError;
|
|
86
|
+
let onErrorCalled = false;
|
|
87
|
+
const finish = () => {
|
|
88
|
+
if (finished) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
finished = true;
|
|
92
|
+
if (!normalizedRequest.route) {
|
|
93
|
+
const route = resolveExpressRoute(req);
|
|
94
|
+
if (route) {
|
|
95
|
+
normalizedRequest.route = route;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (requestCapture && requestCapture.read) {
|
|
99
|
+
normalizedRequest.body = _chunkJKW6E4L3cjs.makeCapturedBody.call(void 0,
|
|
100
|
+
requestCapture.buffer.bytes(),
|
|
101
|
+
requestCapture.buffer.totalBytes(),
|
|
102
|
+
requestCapture.buffer.truncated(),
|
|
103
|
+
capture.requestBody === "text" ? "text" : "base64"
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
const responseHeaders = recorder.headersSnapshot(res.getHeaders());
|
|
107
|
+
const recordedStatus = _nullishCoalesce(recorder.statusCode(), () => ( res.statusCode));
|
|
108
|
+
const statusCode = capturedError && !recorder.hasWrittenHeader() ? 500 : recordedStatus;
|
|
109
|
+
const isUpgrade = _chunkGNSXLLECcjs.isWebsocketUpgrade.call(void 0,
|
|
110
|
+
_nullishCoalesce(statusCode, () => ( 0)),
|
|
111
|
+
normalizedRequest.headers,
|
|
112
|
+
responseHeaders
|
|
113
|
+
);
|
|
114
|
+
const responseBody = recorder.bodyCaptured() && !isUpgrade ? _chunkJKW6E4L3cjs.makeCapturedBody.call(void 0,
|
|
115
|
+
recorder.body(),
|
|
116
|
+
recorder.totalBytes(),
|
|
117
|
+
recorder.truncated(),
|
|
118
|
+
capture.responseBody === "text" ? "text" : "base64"
|
|
119
|
+
) : void 0;
|
|
120
|
+
const normalizedResponse = {
|
|
121
|
+
statusCode: _nullishCoalesce(statusCode, () => ( void 0)),
|
|
122
|
+
headers: responseHeaders,
|
|
123
|
+
body: responseBody,
|
|
124
|
+
endTimeMs: Date.now()
|
|
125
|
+
};
|
|
126
|
+
const log = xray.endRequest(ctx, normalizedResponse, capturedError);
|
|
127
|
+
if (capturedError && _optionalChain([options, 'optionalAccess', _13 => _13.onError]) && !onErrorCalled) {
|
|
128
|
+
onErrorCalled = true;
|
|
129
|
+
try {
|
|
130
|
+
options.onError(ctx, capturedError);
|
|
131
|
+
} catch (err) {
|
|
132
|
+
_chunkJKW6E4L3cjs.logWithLevel.call(void 0, xray.config.logger, "warn", xray.config.logLevel, "xray: onError failed", {
|
|
133
|
+
error: err instanceof Error ? err.message : String(err)
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (_optionalChain([options, 'optionalAccess', _14 => _14.onResponse])) {
|
|
138
|
+
try {
|
|
139
|
+
options.onResponse(ctx, log);
|
|
140
|
+
} catch (err) {
|
|
141
|
+
_chunkJKW6E4L3cjs.logWithLevel.call(void 0,
|
|
142
|
+
xray.config.logger,
|
|
143
|
+
"warn",
|
|
144
|
+
xray.config.logLevel,
|
|
145
|
+
"xray: onResponse failed",
|
|
146
|
+
{
|
|
147
|
+
error: err instanceof Error ? err.message : String(err)
|
|
148
|
+
}
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
res.once("finish", finish);
|
|
154
|
+
res.once("close", finish);
|
|
155
|
+
try {
|
|
156
|
+
const result = handler(req, res);
|
|
157
|
+
if (result && typeof result.catch === "function") {
|
|
158
|
+
void result.catch((err) => {
|
|
159
|
+
capturedError = err;
|
|
160
|
+
if (_optionalChain([options, 'optionalAccess', _15 => _15.onError]) && !onErrorCalled) {
|
|
161
|
+
onErrorCalled = true;
|
|
162
|
+
try {
|
|
163
|
+
options.onError(ctx, err);
|
|
164
|
+
} catch (errInner) {
|
|
165
|
+
_chunkJKW6E4L3cjs.logWithLevel.call(void 0,
|
|
166
|
+
xray.config.logger,
|
|
167
|
+
"warn",
|
|
168
|
+
xray.config.logLevel,
|
|
169
|
+
"xray: onError failed",
|
|
170
|
+
{
|
|
171
|
+
error: errInner instanceof Error ? errInner.message : String(errInner)
|
|
172
|
+
}
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
} catch (err) {
|
|
179
|
+
capturedError = err;
|
|
180
|
+
if (_optionalChain([options, 'optionalAccess', _16 => _16.onError]) && !onErrorCalled) {
|
|
181
|
+
onErrorCalled = true;
|
|
182
|
+
try {
|
|
183
|
+
options.onError(ctx, err);
|
|
184
|
+
} catch (errInner) {
|
|
185
|
+
_chunkJKW6E4L3cjs.logWithLevel.call(void 0, xray.config.logger, "warn", xray.config.logLevel, "xray: onError failed", {
|
|
186
|
+
error: errInner instanceof Error ? errInner.message : String(errInner)
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
throw err;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
function getXrayContext(req) {
|
|
195
|
+
return _chunkGNSXLLECcjs.getXrayContextFromObject.call(void 0, req);
|
|
196
|
+
}
|
|
197
|
+
function wrapRequestBody(req, limit) {
|
|
198
|
+
if (limit <= 0) {
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
if (!hasRequestBody(req)) {
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
const capture = {
|
|
205
|
+
buffer: new (0, _chunkGNSXLLECcjs.LimitedBuffer)(limit),
|
|
206
|
+
read: false,
|
|
207
|
+
userConsuming: false
|
|
208
|
+
};
|
|
209
|
+
const originalPush = req.push;
|
|
210
|
+
req.push = function push(chunk, encoding) {
|
|
211
|
+
if (chunk != null) {
|
|
212
|
+
recordChunk(capture, chunk, encoding);
|
|
213
|
+
}
|
|
214
|
+
return originalPush.call(req, chunk, encoding);
|
|
215
|
+
};
|
|
216
|
+
const originalEmit = req.emit;
|
|
217
|
+
req.emit = function emit(event, ...args) {
|
|
218
|
+
if (event === "data" && capture.userConsuming && args[0] != null) {
|
|
219
|
+
capture.read = true;
|
|
220
|
+
}
|
|
221
|
+
if (event === "end" && capture.userConsuming) {
|
|
222
|
+
capture.read = true;
|
|
223
|
+
}
|
|
224
|
+
return originalEmit.call(req, event, ...args);
|
|
225
|
+
};
|
|
226
|
+
const originalOn = req.on;
|
|
227
|
+
req.on = function on(event, listener) {
|
|
228
|
+
if (event === "data" || event === "readable") {
|
|
229
|
+
capture.userConsuming = true;
|
|
230
|
+
}
|
|
231
|
+
return originalOn.call(req, event, listener);
|
|
232
|
+
};
|
|
233
|
+
const originalOnce = req.once;
|
|
234
|
+
req.once = function once(event, listener) {
|
|
235
|
+
if (event === "data" || event === "readable") {
|
|
236
|
+
capture.userConsuming = true;
|
|
237
|
+
}
|
|
238
|
+
return originalOnce.call(req, event, listener);
|
|
239
|
+
};
|
|
240
|
+
const originalAddListener = req.addListener;
|
|
241
|
+
req.addListener = function addListener(event, listener) {
|
|
242
|
+
if (event === "data" || event === "readable") {
|
|
243
|
+
capture.userConsuming = true;
|
|
244
|
+
}
|
|
245
|
+
return originalAddListener.call(req, event, listener);
|
|
246
|
+
};
|
|
247
|
+
const originalPipe = req.pipe;
|
|
248
|
+
req.pipe = function pipe(destination, options) {
|
|
249
|
+
capture.userConsuming = true;
|
|
250
|
+
return originalPipe.call(req, destination, options);
|
|
251
|
+
};
|
|
252
|
+
const originalRead = req.read;
|
|
253
|
+
req.read = function read(size) {
|
|
254
|
+
capture.userConsuming = true;
|
|
255
|
+
const chunk = originalRead.call(req, size);
|
|
256
|
+
const readableFlowing = req.readableFlowing;
|
|
257
|
+
const hasDataListeners = typeof req.listenerCount === "function" && req.listenerCount("data") > 0;
|
|
258
|
+
if (!hasDataListeners && readableFlowing !== true && chunk != null) {
|
|
259
|
+
capture.read = true;
|
|
260
|
+
}
|
|
261
|
+
return chunk;
|
|
262
|
+
};
|
|
263
|
+
return capture;
|
|
264
|
+
}
|
|
265
|
+
function hasRequestBody(req) {
|
|
266
|
+
if (req.headers["content-length"] != null) {
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
if (req.headers["transfer-encoding"] != null) {
|
|
270
|
+
return true;
|
|
271
|
+
}
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
function recordChunk(capture, chunk, encoding) {
|
|
275
|
+
const bytes = toBytes(chunk, encoding);
|
|
276
|
+
if (!bytes) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
capture.buffer.write(bytes);
|
|
280
|
+
}
|
|
281
|
+
function toBytes(chunk, encoding) {
|
|
282
|
+
if (chunk == null) {
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
if (typeof chunk === "string") {
|
|
286
|
+
return Buffer.from(chunk, encoding);
|
|
287
|
+
}
|
|
288
|
+
if (chunk instanceof Uint8Array) {
|
|
289
|
+
return chunk;
|
|
290
|
+
}
|
|
291
|
+
if (chunk instanceof ArrayBuffer) {
|
|
292
|
+
return new Uint8Array(chunk);
|
|
293
|
+
}
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
var ResponseRecorder = class {
|
|
297
|
+
constructor(captureBody, maxBodySize, onHeader) {
|
|
298
|
+
this.wroteHeader = false;
|
|
299
|
+
this.bytes = 0;
|
|
300
|
+
this.buffer = captureBody ? new (0, _chunkGNSXLLECcjs.LimitedBuffer)(maxBodySize) : null;
|
|
301
|
+
this.onHeader = onHeader;
|
|
302
|
+
}
|
|
303
|
+
body() {
|
|
304
|
+
return _nullishCoalesce(_optionalChain([this, 'access', _17 => _17.buffer, 'optionalAccess', _18 => _18.bytes, 'call', _19 => _19()]), () => ( new Uint8Array()));
|
|
305
|
+
}
|
|
306
|
+
totalBytes() {
|
|
307
|
+
return _nullishCoalesce(_optionalChain([this, 'access', _20 => _20.buffer, 'optionalAccess', _21 => _21.totalBytes, 'call', _22 => _22()]), () => ( 0));
|
|
308
|
+
}
|
|
309
|
+
bodyCaptured() {
|
|
310
|
+
return !!this.buffer && this.buffer.totalBytes() > 0;
|
|
311
|
+
}
|
|
312
|
+
bytesWritten() {
|
|
313
|
+
return this.bytes;
|
|
314
|
+
}
|
|
315
|
+
hasWrittenHeader() {
|
|
316
|
+
return this.wroteHeader;
|
|
317
|
+
}
|
|
318
|
+
headersSnapshot(defaultHeaders) {
|
|
319
|
+
if (this.headerSnapshot) {
|
|
320
|
+
return _chunkGNSXLLECcjs.headerValuesFromNodeHeaders.call(void 0, this.headerSnapshot);
|
|
321
|
+
}
|
|
322
|
+
return _chunkGNSXLLECcjs.headerValuesFromNodeHeaders.call(void 0, defaultHeaders);
|
|
323
|
+
}
|
|
324
|
+
statusCode() {
|
|
325
|
+
return this.status;
|
|
326
|
+
}
|
|
327
|
+
truncated() {
|
|
328
|
+
return _nullishCoalesce(_optionalChain([this, 'access', _23 => _23.buffer, 'optionalAccess', _24 => _24.truncated, 'call', _25 => _25()]), () => ( false));
|
|
329
|
+
}
|
|
330
|
+
wrap(res) {
|
|
331
|
+
const originalWriteHead = res.writeHead;
|
|
332
|
+
res.writeHead = ((statusCode, ...args) => {
|
|
333
|
+
if (!this.wroteHeader) {
|
|
334
|
+
this.applyWriteHeadHeaders(res, args);
|
|
335
|
+
}
|
|
336
|
+
this.recordHeader(res, statusCode);
|
|
337
|
+
return originalWriteHead.call(res, statusCode, ...args);
|
|
338
|
+
});
|
|
339
|
+
const originalWrite = res.write;
|
|
340
|
+
res.write = ((chunk, encoding, cb) => {
|
|
341
|
+
this.recordHeader(res, _nullishCoalesce(res.statusCode, () => ( 200)));
|
|
342
|
+
this.recordWrite(chunk, encoding);
|
|
343
|
+
return originalWrite.call(res, chunk, encoding, cb);
|
|
344
|
+
});
|
|
345
|
+
const originalEnd = res.end;
|
|
346
|
+
res.end = ((chunk, encoding, cb) => {
|
|
347
|
+
this.recordHeader(res, _nullishCoalesce(res.statusCode, () => ( 200)));
|
|
348
|
+
if (chunk) {
|
|
349
|
+
this.recordWrite(chunk, encoding);
|
|
350
|
+
}
|
|
351
|
+
return originalEnd.call(res, chunk, encoding, cb);
|
|
352
|
+
});
|
|
353
|
+
if (typeof res.flushHeaders === "function") {
|
|
354
|
+
const originalFlush = res.flushHeaders;
|
|
355
|
+
res.flushHeaders = (() => {
|
|
356
|
+
this.recordHeader(res, _nullishCoalesce(res.statusCode, () => ( 200)));
|
|
357
|
+
return originalFlush.call(res);
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
recordHeader(res, statusCode) {
|
|
362
|
+
if (this.wroteHeader) {
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
_optionalChain([this, 'access', _26 => _26.onHeader, 'optionalCall', _27 => _27(res)]);
|
|
366
|
+
this.wroteHeader = true;
|
|
367
|
+
this.status = statusCode;
|
|
368
|
+
this.headerSnapshot = { ...res.getHeaders() };
|
|
369
|
+
}
|
|
370
|
+
applyWriteHeadHeaders(res, args) {
|
|
371
|
+
if (args.length === 0) {
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
const headersArg = typeof args[0] === "string" ? args[1] : args[0];
|
|
375
|
+
if (!headersArg) {
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
if (Array.isArray(headersArg)) {
|
|
379
|
+
if (headersArg.length === 0) {
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
if (typeof headersArg[0] === "string") {
|
|
383
|
+
for (let i = 0; i < headersArg.length - 1; i += 2) {
|
|
384
|
+
const name = headersArg[i];
|
|
385
|
+
const value = headersArg[i + 1];
|
|
386
|
+
if (typeof name === "string" && value !== void 0 && value !== null) {
|
|
387
|
+
res.setHeader(name, value);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
for (const entry of headersArg) {
|
|
393
|
+
if (!Array.isArray(entry)) {
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
const [name, value] = entry;
|
|
397
|
+
if (typeof name === "string" && value !== void 0 && value !== null) {
|
|
398
|
+
res.setHeader(name, value);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
if (typeof headersArg === "object") {
|
|
404
|
+
for (const [name, value] of Object.entries(headersArg)) {
|
|
405
|
+
if (value !== void 0 && value !== null) {
|
|
406
|
+
res.setHeader(name, value);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
recordWrite(chunk, encoding) {
|
|
412
|
+
const bytes = toBytes(chunk, encoding);
|
|
413
|
+
if (!bytes) {
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
this.bytes += bytes.length;
|
|
417
|
+
if (this.buffer) {
|
|
418
|
+
this.buffer.write(bytes);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
function ensureResponseRequestId(res, ctx, xray) {
|
|
423
|
+
const headerName = xray.config.requestId.header;
|
|
424
|
+
const existing = headerValueFromNode(res.getHeader(headerName));
|
|
425
|
+
if (existing) {
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
const explicit = normalizeRequestIdCandidate(ctx.requestId);
|
|
429
|
+
if (explicit) {
|
|
430
|
+
res.setHeader(canonicalHeaderName(headerName), explicit);
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
const generated = _chunkJKW6E4L3cjs.generateRequestId.call(void 0, );
|
|
434
|
+
res.setHeader(canonicalHeaderName(headerName), generated);
|
|
435
|
+
_chunkGNSXLLECcjs.setContextRequestId.call(void 0, ctx, generated);
|
|
436
|
+
}
|
|
437
|
+
function fullUrl(req) {
|
|
438
|
+
const requestUrl = resolveRequestUrl(req);
|
|
439
|
+
if (!requestUrl) {
|
|
440
|
+
return "";
|
|
441
|
+
}
|
|
442
|
+
if (requestUrl.startsWith("http://") || requestUrl.startsWith("https://")) {
|
|
443
|
+
return requestUrl;
|
|
444
|
+
}
|
|
445
|
+
const host = req.headers["host"];
|
|
446
|
+
if (!host || typeof host !== "string") {
|
|
447
|
+
return requestUrl;
|
|
448
|
+
}
|
|
449
|
+
const scheme = req.socket instanceof _tls.TLSSocket ? "https" : "http";
|
|
450
|
+
return `${scheme}://${host}${requestUrl}`;
|
|
451
|
+
}
|
|
452
|
+
function resolveRequestUrl(req) {
|
|
453
|
+
const requestUrl = _nullishCoalesce(req.url, () => ( ""));
|
|
454
|
+
if (requestUrl.startsWith("http://") || requestUrl.startsWith("https://")) {
|
|
455
|
+
return requestUrl;
|
|
456
|
+
}
|
|
457
|
+
const expressReq = req;
|
|
458
|
+
if (typeof expressReq.originalUrl === "string" && expressReq.originalUrl.length > 0) {
|
|
459
|
+
return expressReq.originalUrl;
|
|
460
|
+
}
|
|
461
|
+
return requestUrl;
|
|
462
|
+
}
|
|
463
|
+
function headerValueFromNode(value) {
|
|
464
|
+
if (value == null) {
|
|
465
|
+
return void 0;
|
|
466
|
+
}
|
|
467
|
+
if (Array.isArray(value)) {
|
|
468
|
+
return normalizeRequestIdCandidate(value[0]);
|
|
469
|
+
}
|
|
470
|
+
return normalizeRequestIdCandidate(`${value}`);
|
|
471
|
+
}
|
|
472
|
+
function normalizeRequestIdCandidate(value) {
|
|
473
|
+
if (!value) {
|
|
474
|
+
return void 0;
|
|
475
|
+
}
|
|
476
|
+
const trimmed = value.trim();
|
|
477
|
+
return trimmed ? trimmed : void 0;
|
|
478
|
+
}
|
|
479
|
+
function canonicalHeaderName(headerName) {
|
|
480
|
+
return headerName.split("-").filter(Boolean).map((part) => part ? part[0].toUpperCase() + part.slice(1) : part).join("-");
|
|
481
|
+
}
|
|
482
|
+
var expressParamsHistory = /* @__PURE__ */ new WeakMap();
|
|
483
|
+
function trackExpressParams(req) {
|
|
484
|
+
if (expressParamsHistory.has(req)) {
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
const anyReq = req;
|
|
488
|
+
if (typeof anyReq.app !== "function") {
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
const history = [];
|
|
492
|
+
expressParamsHistory.set(req, history);
|
|
493
|
+
const descriptor = Object.getOwnPropertyDescriptor(req, "params");
|
|
494
|
+
if (descriptor && !descriptor.configurable) {
|
|
495
|
+
if (descriptor.value && typeof descriptor.value === "object") {
|
|
496
|
+
history.push({ ...descriptor.value });
|
|
497
|
+
}
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
let current = descriptor && "value" in descriptor ? descriptor.value : void 0;
|
|
501
|
+
if (current && typeof current === "object") {
|
|
502
|
+
history.push({ ...current });
|
|
503
|
+
}
|
|
504
|
+
Object.defineProperty(req, "params", {
|
|
505
|
+
configurable: true,
|
|
506
|
+
enumerable: _nullishCoalesce(_optionalChain([descriptor, 'optionalAccess', _28 => _28.enumerable]), () => ( true)),
|
|
507
|
+
get() {
|
|
508
|
+
return current;
|
|
509
|
+
},
|
|
510
|
+
set(value) {
|
|
511
|
+
current = value;
|
|
512
|
+
if (value && typeof value === "object") {
|
|
513
|
+
history.push({ ...value });
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
function resolveExpressRoute(req) {
|
|
519
|
+
const anyReq = req;
|
|
520
|
+
const routePath = extractExpressRoutePath(_optionalChain([anyReq, 'access', _29 => _29.route, 'optionalAccess', _30 => _30.path]));
|
|
521
|
+
const baseUrl = _nullishCoalesce(anyReq.baseUrl, () => ( ""));
|
|
522
|
+
if (!routePath && !baseUrl) {
|
|
523
|
+
return void 0;
|
|
524
|
+
}
|
|
525
|
+
const params = collectExpressParams(req, anyReq.params);
|
|
526
|
+
const resolvedBaseUrl = replaceBaseUrlParams(baseUrl, params, routePath);
|
|
527
|
+
return joinExpressRoute(resolvedBaseUrl, routePath);
|
|
528
|
+
}
|
|
529
|
+
function extractExpressRoutePath(path) {
|
|
530
|
+
if (typeof path === "string") {
|
|
531
|
+
return path;
|
|
532
|
+
}
|
|
533
|
+
if (Array.isArray(path)) {
|
|
534
|
+
for (const entry of path) {
|
|
535
|
+
if (typeof entry === "string") {
|
|
536
|
+
return entry;
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
return void 0;
|
|
541
|
+
}
|
|
542
|
+
function collectExpressParams(req, fallback) {
|
|
543
|
+
const history = expressParamsHistory.get(req);
|
|
544
|
+
if (!history || history.length === 0) {
|
|
545
|
+
return _nullishCoalesce(fallback, () => ( {}));
|
|
546
|
+
}
|
|
547
|
+
const merged = {};
|
|
548
|
+
for (const snapshot of history) {
|
|
549
|
+
for (const [key, value] of Object.entries(snapshot)) {
|
|
550
|
+
if (!(key in merged)) {
|
|
551
|
+
merged[key] = value;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
if (fallback) {
|
|
556
|
+
for (const [key, value] of Object.entries(fallback)) {
|
|
557
|
+
if (!(key in merged)) {
|
|
558
|
+
merged[key] = value;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
return merged;
|
|
563
|
+
}
|
|
564
|
+
function replaceBaseUrlParams(baseUrl, params, routePath) {
|
|
565
|
+
if (!baseUrl) {
|
|
566
|
+
return baseUrl;
|
|
567
|
+
}
|
|
568
|
+
const entries = Object.entries(params);
|
|
569
|
+
if (entries.length === 0) {
|
|
570
|
+
return baseUrl;
|
|
571
|
+
}
|
|
572
|
+
const excluded = new Set(routePath ? extractExpressParamNames(routePath) : []);
|
|
573
|
+
const replacements = entries.filter(([name]) => !excluded.has(name)).map(([name, value]) => ({ name, value: Array.isArray(value) ? value[0] : value })).filter((entry) => !!entry.value);
|
|
574
|
+
if (replacements.length === 0) {
|
|
575
|
+
return baseUrl;
|
|
576
|
+
}
|
|
577
|
+
const used = /* @__PURE__ */ new Set();
|
|
578
|
+
const encodedCache = /* @__PURE__ */ new Map();
|
|
579
|
+
const segments = baseUrl.split("/");
|
|
580
|
+
const updated = segments.map((segment) => {
|
|
581
|
+
if (!segment) {
|
|
582
|
+
return segment;
|
|
583
|
+
}
|
|
584
|
+
for (const { name, value } of replacements) {
|
|
585
|
+
if (used.has(name)) {
|
|
586
|
+
continue;
|
|
587
|
+
}
|
|
588
|
+
const encodedValue = _nullishCoalesce(encodedCache.get(value), () => ( encodeURIComponent(value)));
|
|
589
|
+
encodedCache.set(value, encodedValue);
|
|
590
|
+
if (segment === value || segment === encodedValue) {
|
|
591
|
+
used.add(name);
|
|
592
|
+
return `:${name}`;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
return segment;
|
|
596
|
+
});
|
|
597
|
+
return updated.join("/");
|
|
598
|
+
}
|
|
599
|
+
function extractExpressParamNames(path) {
|
|
600
|
+
const names = [];
|
|
601
|
+
const paramPattern = /:([A-Za-z0-9_]+)(?:\([^)]*\))?[?*+]?/g;
|
|
602
|
+
for (const match of path.matchAll(paramPattern)) {
|
|
603
|
+
const name = match[1];
|
|
604
|
+
if (name) {
|
|
605
|
+
names.push(name);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
return names;
|
|
609
|
+
}
|
|
610
|
+
function joinExpressRoute(baseUrl, routePath) {
|
|
611
|
+
const base = baseUrl ? ensureLeadingSlash(baseUrl) : "";
|
|
612
|
+
const route = routePath ? ensureLeadingSlash(routePath) : "";
|
|
613
|
+
if (!base) {
|
|
614
|
+
return route || "/";
|
|
615
|
+
}
|
|
616
|
+
if (!route || route === "/") {
|
|
617
|
+
return base;
|
|
618
|
+
}
|
|
619
|
+
const trimmedBase = base.endsWith("/") ? base.slice(0, -1) : base;
|
|
620
|
+
return `${trimmedBase}${route}`;
|
|
621
|
+
}
|
|
622
|
+
function ensureLeadingSlash(path) {
|
|
623
|
+
if (!path) {
|
|
624
|
+
return "/";
|
|
625
|
+
}
|
|
626
|
+
return path.startsWith("/") ? path : `/${path}`;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
exports.createEmitter = createEmitter2; exports.wrapHttpHandler = wrapHttpHandler; exports.getXrayContext = getXrayContext;
|
|
634
|
+
//# sourceMappingURL=chunk-QUH3LJ5M.cjs.map
|