@@ -135,6 +135,17 @@ function extractUserData(
135
135
return extractedUser ;
136
136
}
137
137
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
+
138
149
/**
139
150
* Normalize data from the request object, accounting for framework differences.
140
151
*
@@ -162,23 +173,25 @@ export function extractRequestData(
162
173
} ;
163
174
// method:
164
175
// 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 ;
166
178
// host:
167
179
// express: req.hostname in > 4 and req.host in < 4
168
180
// koa: req.host
169
181
// node, nextjs: req.headers.host
182
+ // fastify: req.headers.host
170
183
// Express 4 mistakenly strips off port number from req.host / req.hostname so we can't rely on them
171
184
// See: https://github.com/expressjs/express/issues/3047#issuecomment-236653223
172
185
// Also: https://github.com/getsentry/sentry-javascript/issues/1917
173
186
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
+
178
190
// url (including path and query string):
179
191
// node, express: req.originalUrl
180
192
// 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 || '' ;
182
195
// absolute url
183
196
const absoluteUrl = originalUrl . startsWith ( protocol ) ? originalUrl : `${ protocol } ://${ host } ${ originalUrl } ` ;
184
197
include . forEach ( key => {
@@ -311,7 +324,8 @@ function extractQueryParams(req: PolymorphicRequest): string | Record<string, un
311
324
// url (including path and query string):
312
325
// node, express: req.originalUrl
313
326
// 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 ) || '' ;
315
329
316
330
if ( ! originalUrl ) {
317
331
return ;
0 commit comments