Skip to content

Commit 79fe08c

Browse files
committed
Add fastify branches to requestdata parsing
1 parent fc09cb5 commit 79fe08c

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

packages/utils/src/requestdata.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,17 @@ function extractUserData(
135135
return extractedUser;
136136
}
137137

138+
/**
139+
* For express and koa - available at req.protocol.
140+
* For node, nextjs, and fastify - should be learned
141+
* via socket encryption status.
142+
*/
143+
function extractProto(req: PolymorphicRequest): string {
144+
if (req.protocol) return req.protocol;
145+
const sock = req.socket || (req.raw && req.raw.socket);
146+
return sock && sock.encrypted ? 'https' : 'http';
147+
}
148+
138149
/**
139150
* Normalize data from the request object, accounting for framework differences.
140151
*
@@ -162,23 +173,25 @@ export function extractRequestData(
162173
};
163174
// method:
164175
// node, express, koa, nextjs: req.method
165-
const method = req.method;
176+
// fastify: req.raw.method
177+
const method = req.raw ? req.raw.method : req.method;
166178
// host:
167179
// express: req.hostname in > 4 and req.host in < 4
168180
// koa: req.host
169181
// node, nextjs: req.headers.host
182+
// fastify: req.headers.host
170183
// Express 4 mistakenly strips off port number from req.host / req.hostname so we can't rely on them
171184
// See: https://github.com/expressjs/express/issues/3047#issuecomment-236653223
172185
// Also: https://github.com/getsentry/sentry-javascript/issues/1917
173186
const host = headers.host || req.hostname || req.host || '<no host>';
174-
// protocol:
175-
// node, nextjs: <n/a>
176-
// express, koa: req.protocol
177-
const protocol = req.protocol === 'https' || (req.socket && req.socket.encrypted) ? 'https' : 'http';
187+
188+
const protocol = extractProto(req);
189+
178190
// url (including path and query string):
179191
// node, express: req.originalUrl
180192
// koa, nextjs: req.url
181-
const originalUrl = req.originalUrl || req.url || '';
193+
// fastify: req.raw.url
194+
const originalUrl = (req.raw && req.raw.url) || req.originalUrl || req.url || '';
182195
// absolute url
183196
const absoluteUrl = originalUrl.startsWith(protocol) ? originalUrl : `${protocol}://${host}${originalUrl}`;
184197
include.forEach(key => {
@@ -311,7 +324,8 @@ function extractQueryParams(req: PolymorphicRequest): string | Record<string, un
311324
// url (including path and query string):
312325
// node, express: req.originalUrl
313326
// koa, nextjs: req.url
314-
let originalUrl = req.originalUrl || req.url || '';
327+
// fastify: req.raw.url
328+
let originalUrl = req.originalUrl || req.url || (req.raw && req.raw.url) || '';
315329

316330
if (!originalUrl) {
317331
return;

0 commit comments

Comments
 (0)