Skip to content

Commit 55c94dc

Browse files
committed
move request data functions back to node
1 parent 903addf commit 55c94dc

File tree

8 files changed

+339
-108
lines changed

8 files changed

+339
-108
lines changed

packages/node/src/handlers.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import * as domain from 'domain';
1414
import * as http from 'http';
1515

1616
import { NodeClient } from './client';
17+
import { addRequestDataToEvent, extractRequestData } from './requestdata';
1718
// TODO (v8 / XXX) Remove these imports
1819
import type { ParseRequestOptions } from './requestDataDeprecated';
1920
import { parseRequest } from './requestDataDeprecated';
20-
import { addRequestDataToEvent, extractRequestData, flush, isAutoSessionTrackingEnabled } from './sdk';
21+
import { flush, isAutoSessionTrackingEnabled } from './sdk';
2122

2223
/**
2324
* Express-compatible tracing handler.

packages/node/src/index.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,8 @@ export {
4646

4747
export { NodeClient } from './client';
4848
export { makeNodeTransport } from './transports';
49-
export {
50-
addRequestDataToEvent,
51-
extractRequestData,
52-
defaultIntegrations,
53-
init,
54-
defaultStackParser,
55-
lastEventId,
56-
flush,
57-
close,
58-
getSentryRelease,
59-
} from './sdk';
49+
export { defaultIntegrations, init, defaultStackParser, lastEventId, flush, close, getSentryRelease } from './sdk';
50+
export { addRequestDataToEvent, extractRequestData } from './requestdata';
6051
export { deepReadDirSync } from './utils';
6152

6253
import { Integrations as CoreIntegrations } from '@sentry/core';

packages/node/src/requestDataDeprecated.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
/* eslint-disable deprecation/deprecation */
88
/* eslint-disable @typescript-eslint/no-explicit-any */
99
import { Event, ExtractedNodeRequestData, PolymorphicRequest } from '@sentry/types';
10+
1011
import {
1112
addRequestDataToEvent,
1213
AddRequestDataToEventOptions,
1314
extractRequestData as _extractRequestData,
14-
} from '@sentry/utils';
15-
import * as cookie from 'cookie';
16-
import * as url from 'url';
15+
} from './requestdata';
1716

1817
/**
1918
* @deprecated `Handlers.ExpressRequest` is deprecated and will be removed in v8. Use `PolymorphicRequest` instead.
@@ -30,7 +29,7 @@ export type ExpressRequest = PolymorphicRequest;
3029
* @returns An object containing normalized request data
3130
*/
3231
export function extractRequestData(req: { [key: string]: any }, keys?: string[]): ExtractedNodeRequestData {
33-
return _extractRequestData(req, { include: keys, deps: { cookie, url } });
32+
return _extractRequestData(req, { include: keys });
3433
}
3534

3635
/**
@@ -55,5 +54,5 @@ export type ParseRequestOptions = AddRequestDataToEventOptions['include'] & {
5554
* @hidden
5655
*/
5756
export function parseRequest(event: Event, req: ExpressRequest, options: ParseRequestOptions = {}): Event {
58-
return addRequestDataToEvent(event, req, { include: options, deps: { cookie, url } });
57+
return addRequestDataToEvent(event, req, { include: options });
5958
}

packages/node/src/requestdata.ts

+318
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
import { Event, ExtractedNodeRequestData, PolymorphicRequest, Transaction, TransactionSource } from '@sentry/types';
2+
import { isPlainObject, isString, normalize, stripUrlQueryAndFragment } from '@sentry/utils/';
3+
import * as cookie from 'cookie';
4+
import * as url from 'url';
5+
6+
const DEFAULT_INCLUDES = {
7+
ip: false,
8+
request: true,
9+
transaction: true,
10+
user: true,
11+
};
12+
const DEFAULT_REQUEST_INCLUDES = ['cookies', 'data', 'headers', 'method', 'query_string', 'url'];
13+
const DEFAULT_USER_INCLUDES = ['id', 'username', 'email'];
14+
15+
/**
16+
* Sets parameterized route as transaction name e.g.: `GET /users/:id`
17+
* Also adds more context data on the transaction from the request
18+
*/
19+
export function addRequestDataToTransaction(transaction: Transaction | undefined, req: PolymorphicRequest): void {
20+
if (!transaction) return;
21+
if (!transaction.metadata.source || transaction.metadata.source === 'url') {
22+
// Attempt to grab a parameterized route off of the request
23+
transaction.setName(...extractPathForTransaction(req, { path: true, method: true }));
24+
}
25+
transaction.setData('url', req.originalUrl || req.url);
26+
if (req.baseUrl) {
27+
transaction.setData('baseUrl', req.baseUrl);
28+
}
29+
transaction.setData('query', extractQueryParams(req));
30+
}
31+
32+
/**
33+
* Extracts a complete and parameterized path from the request object and uses it to construct transaction name.
34+
* If the parameterized transaction name cannot be extracted, we fall back to the raw URL.
35+
*
36+
* Additionally, this function determines and returns the transaction name source
37+
*
38+
* eg. GET /mountpoint/user/:id
39+
*
40+
* @param req A request object
41+
* @param options What to include in the transaction name (method, path, or a custom route name to be
42+
* used instead of the request's route)
43+
*
44+
* @returns A tuple of the fully constructed transaction name [0] and its source [1] (can be either 'route' or 'url')
45+
*/
46+
export function extractPathForTransaction(
47+
req: PolymorphicRequest,
48+
options: { path?: boolean; method?: boolean; customRoute?: string } = {},
49+
): [string, TransactionSource] {
50+
const method = req.method && req.method.toUpperCase();
51+
52+
let path = '';
53+
let source: TransactionSource = 'url';
54+
55+
// Check to see if there's a parameterized route we can use (as there is in Express)
56+
if (options.customRoute || req.route) {
57+
path = options.customRoute || `${req.baseUrl || ''}${req.route && req.route.path}`;
58+
source = 'route';
59+
}
60+
61+
// Otherwise, just take the original URL
62+
else if (req.originalUrl || req.url) {
63+
path = stripUrlQueryAndFragment(req.originalUrl || req.url || '');
64+
}
65+
66+
let name = '';
67+
if (options.method && method) {
68+
name += method;
69+
}
70+
if (options.method && options.path) {
71+
name += ' ';
72+
}
73+
if (options.path && path) {
74+
name += path;
75+
}
76+
77+
return [name, source];
78+
}
79+
80+
type TransactionNamingScheme = 'path' | 'methodPath' | 'handler';
81+
82+
/** JSDoc */
83+
function extractTransaction(req: PolymorphicRequest, type: boolean | TransactionNamingScheme): string {
84+
switch (type) {
85+
case 'path': {
86+
return extractPathForTransaction(req, { path: true })[0];
87+
}
88+
case 'handler': {
89+
return (req.route && req.route.stack && req.route.stack[0] && req.route.stack[0].name) || '<anonymous>';
90+
}
91+
case 'methodPath':
92+
default: {
93+
return extractPathForTransaction(req, { path: true, method: true })[0];
94+
}
95+
}
96+
}
97+
98+
/** JSDoc */
99+
function extractUserData(
100+
user: {
101+
[key: string]: unknown;
102+
},
103+
keys: boolean | string[],
104+
): { [key: string]: unknown } {
105+
const extractedUser: { [key: string]: unknown } = {};
106+
const attributes = Array.isArray(keys) ? keys : DEFAULT_USER_INCLUDES;
107+
108+
attributes.forEach(key => {
109+
if (user && key in user) {
110+
extractedUser[key] = user[key];
111+
}
112+
});
113+
114+
return extractedUser;
115+
}
116+
117+
/**
118+
* Normalize data from the request object
119+
*
120+
* @param req The request object from which to extract data
121+
* @param options.include An optional array of keys to include in the normalized data. Defaults to
122+
* DEFAULT_REQUEST_INCLUDES if not provided.
123+
* @param options.deps Injected, platform-specific dependencies
124+
*
125+
* @returns An object containing normalized request data
126+
*/
127+
export function extractRequestData(
128+
req: PolymorphicRequest,
129+
options?: {
130+
include?: string[];
131+
},
132+
): ExtractedNodeRequestData {
133+
const { include = DEFAULT_REQUEST_INCLUDES } = options || {};
134+
const requestData: { [key: string]: unknown } = {};
135+
136+
// headers:
137+
// node, express, koa, nextjs: req.headers
138+
const headers = (req.headers || {}) as {
139+
host?: string;
140+
cookie?: string;
141+
};
142+
// method:
143+
// node, express, koa, nextjs: req.method
144+
const method = req.method;
145+
// host:
146+
// express: req.hostname in > 4 and req.host in < 4
147+
// koa: req.host
148+
// node, nextjs: req.headers.host
149+
const host = req.hostname || req.host || headers.host || '<no host>';
150+
// protocol:
151+
// node, nextjs: <n/a>
152+
// express, koa: req.protocol
153+
const protocol = req.protocol === 'https' || (req.socket && req.socket.encrypted) ? 'https' : 'http';
154+
// url (including path and query string):
155+
// node, express: req.originalUrl
156+
// koa, nextjs: req.url
157+
const originalUrl = req.originalUrl || req.url || '';
158+
// absolute url
159+
const absoluteUrl = `${protocol}://${host}${originalUrl}`;
160+
include.forEach(key => {
161+
switch (key) {
162+
case 'headers': {
163+
requestData.headers = headers;
164+
break;
165+
}
166+
case 'method': {
167+
requestData.method = method;
168+
break;
169+
}
170+
case 'url': {
171+
requestData.url = absoluteUrl;
172+
break;
173+
}
174+
case 'cookies': {
175+
// cookies:
176+
// node, express, koa: req.headers.cookie
177+
// vercel, sails.js, express (w/ cookie middleware), nextjs: req.cookies
178+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
179+
requestData.cookies =
180+
// TODO (v8 / #5257): We're only sending the empty object for backwards compatibility, so the last bit can
181+
// come off in v8
182+
req.cookies || (headers.cookie && cookie.parse(headers.cookie)) || {};
183+
break;
184+
}
185+
case 'query_string': {
186+
// query string:
187+
// node: req.url (raw)
188+
// express, koa, nextjs: req.query
189+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
190+
requestData.query_string = extractQueryParams(req);
191+
break;
192+
}
193+
case 'data': {
194+
if (method === 'GET' || method === 'HEAD') {
195+
break;
196+
}
197+
// body data:
198+
// express, koa, nextjs: req.body
199+
//
200+
// when using node by itself, you have to read the incoming stream(see
201+
// https://nodejs.dev/learn/get-http-request-body-data-using-nodejs); if a user is doing that, we can't know
202+
// where they're going to store the final result, so they'll have to capture this data themselves
203+
if (req.body !== undefined) {
204+
requestData.data = isString(req.body) ? req.body : JSON.stringify(normalize(req.body));
205+
}
206+
break;
207+
}
208+
default: {
209+
if ({}.hasOwnProperty.call(req, key)) {
210+
requestData[key] = (req as { [key: string]: unknown })[key];
211+
}
212+
}
213+
}
214+
});
215+
216+
return requestData;
217+
}
218+
219+
/**
220+
* Options deciding what parts of the request to use when enhancing an event
221+
*/
222+
export interface AddRequestDataToEventOptions {
223+
/** Flags controlling whether each type of data should be added to the event */
224+
include?: {
225+
ip?: boolean;
226+
request?: boolean | string[];
227+
transaction?: boolean | TransactionNamingScheme;
228+
user?: boolean | string[];
229+
};
230+
}
231+
232+
/**
233+
* Add data from the given request to the given event
234+
*
235+
* @param event The event to which the request data will be added
236+
* @param req Request object
237+
* @param options.include Flags to control what data is included
238+
*
239+
* @returns The mutated `Event` object
240+
*/
241+
export function addRequestDataToEvent(
242+
event: Event,
243+
req: PolymorphicRequest,
244+
options?: AddRequestDataToEventOptions,
245+
): Event {
246+
const include = {
247+
...DEFAULT_INCLUDES,
248+
...options?.include,
249+
};
250+
251+
if (include.request) {
252+
const extractedRequestData = Array.isArray(include.request)
253+
? extractRequestData(req, { include: include.request })
254+
: extractRequestData(req);
255+
256+
event.request = {
257+
...event.request,
258+
...extractedRequestData,
259+
};
260+
}
261+
262+
if (include.user) {
263+
const extractedUser = req.user && isPlainObject(req.user) ? extractUserData(req.user, include.user) : {};
264+
265+
if (Object.keys(extractedUser).length) {
266+
event.user = {
267+
...event.user,
268+
...extractedUser,
269+
};
270+
}
271+
}
272+
273+
// client ip:
274+
// node, nextjs: req.socket.remoteAddress
275+
// express, koa: req.ip
276+
if (include.ip) {
277+
const ip = req.ip || (req.socket && req.socket.remoteAddress);
278+
if (ip) {
279+
event.user = {
280+
...event.user,
281+
ip_address: ip,
282+
};
283+
}
284+
}
285+
286+
if (include.transaction && !event.transaction) {
287+
// TODO do we even need this anymore?
288+
// TODO make this work for nextjs
289+
event.transaction = extractTransaction(req, include.transaction);
290+
}
291+
292+
return event;
293+
}
294+
295+
function extractQueryParams(req: PolymorphicRequest): string | Record<string, unknown> | undefined {
296+
// url (including path and query string):
297+
// node, express: req.originalUrl
298+
// koa, nextjs: req.url
299+
let originalUrl = req.originalUrl || req.url || '';
300+
301+
if (!originalUrl) {
302+
return;
303+
}
304+
305+
// The `URL` constructor can't handle internal URLs of the form `/some/path/here`, so stick a dummy protocol and
306+
// hostname on the beginning. Since the point here is just to grab the query string, it doesn't matter what we use.
307+
if (originalUrl.startsWith('/')) {
308+
originalUrl = `http://dogs.are.great${originalUrl}`;
309+
}
310+
311+
return (
312+
req.query ||
313+
(typeof URL !== undefined && new URL(originalUrl).search.replace('?', '')) ||
314+
// In Node 8, `URL` isn't in the global scope, so we have to use the built-in module from Node
315+
url.parse(originalUrl).query ||
316+
undefined
317+
);
318+
}

0 commit comments

Comments
 (0)