Skip to content

Commit 9b02afb

Browse files
authored
fix(node): Allow ParseRequestOptions to be passed to request handler (#5287)
This fixes an issue introduced by #5257, which was designed to be backwards-compatible, but which missed a key use case: When users pass options to our Express middleware, they might be (and in fact, for now, almost certainly are) passing them in the form of `ParseRequestOptions`, not `AddRequestDataToEventOptions`. This allows for that possibility, as a backwards-compatibility measure until v8. It also adjusts a spot in the serverless SDK where a similar problem can occur. Fixes #5282.
1 parent 45818f3 commit 9b02afb

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

packages/node/src/handlers.ts

+20-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import * as domain from 'domain';
1414
import * as http from 'http';
1515

1616
import { NodeClient } from './client';
17+
// TODO (v8 / XXX) Remove these imports
18+
import type { ParseRequestOptions } from './requestDataDeprecated';
19+
import { parseRequest } from './requestDataDeprecated';
1720
import { addRequestDataToEvent, extractRequestData, flush, isAutoSessionTrackingEnabled } from './sdk';
1821

1922
/**
@@ -72,9 +75,12 @@ export function tracingHandler(): (
7275
};
7376
}
7477

75-
export type RequestHandlerOptions = AddRequestDataToEventOptions & {
76-
flushTimeout?: number;
77-
};
78+
export type RequestHandlerOptions =
79+
// TODO (v8 / XXX) Remove ParseRequestOptions type and eslint override
80+
// eslint-disable-next-line deprecation/deprecation
81+
| (ParseRequestOptions | AddRequestDataToEventOptions) & {
82+
flushTimeout?: number;
83+
};
7884

7985
/**
8086
* Express compatible request handler.
@@ -96,11 +102,21 @@ export function requestHandler(
96102
scope.setSession();
97103
}
98104
}
105+
99106
return function sentryRequestMiddleware(
100107
req: http.IncomingMessage,
101108
res: http.ServerResponse,
102109
next: (error?: any) => void,
103110
): void {
111+
// TODO (v8 / XXX) Remove this shim and just use `addRequestDataToEvent`
112+
let backwardsCompatibleEventProcessor: (event: Event) => Event;
113+
if (options && 'include' in options) {
114+
backwardsCompatibleEventProcessor = (event: Event) => addRequestDataToEvent(event, req, options);
115+
} else {
116+
// eslint-disable-next-line deprecation/deprecation
117+
backwardsCompatibleEventProcessor = (event: Event) => parseRequest(event, req, options as ParseRequestOptions);
118+
}
119+
104120
if (options && options.flushTimeout && options.flushTimeout > 0) {
105121
// eslint-disable-next-line @typescript-eslint/unbound-method
106122
const _end = res.end;
@@ -124,7 +140,7 @@ export function requestHandler(
124140
const currentHub = getCurrentHub();
125141

126142
currentHub.configureScope(scope => {
127-
scope.addEventProcessor((event: Event) => addRequestDataToEvent(event, req, options));
143+
scope.addEventProcessor(backwardsCompatibleEventProcessor);
128144
const client = currentHub.getClient<NodeClient>();
129145
if (isAutoSessionTrackingEnabled(client)) {
130146
const scope = currentHub.getScope();

packages/serverless/src/gcpfunction/http.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ import { domainify, getActiveDomain, proxyFunction } from './../utils';
1313
import { HttpFunction, WrapperOptions } from './general';
1414

1515
// TODO (v8 / #5257): Remove this whole old/new business and just use the new stuff
16+
type ParseRequestOptions = AddRequestDataToEventOptions['include'] & {
17+
serverName?: boolean;
18+
version?: boolean;
19+
};
20+
1621
interface OldHttpFunctionWrapperOptions extends WrapperOptions {
1722
/**
1823
* @deprecated Use `addRequestDataToEventOptions` instead.
1924
*/
20-
parseRequestOptions: AddRequestDataToEventOptions;
25+
parseRequestOptions: ParseRequestOptions;
2126
}
2227
interface NewHttpFunctionWrapperOptions extends WrapperOptions {
2328
addRequestDataToEventOptions: AddRequestDataToEventOptions;
@@ -58,7 +63,8 @@ function _wrapHttpFunction(fn: HttpFunction, wrapOptions: Partial<HttpFunctionWr
5863

5964
const options: HttpFunctionWrapperOptions = {
6065
flushTimeout: 2000,
61-
addRequestDataToEventOptions: parseRequestOptions ? parseRequestOptions : {},
66+
// TODO (v8 / xxx): Remove this line, since `addRequestDataToEventOptions` will be included in the spread of `wrapOptions`
67+
addRequestDataToEventOptions: parseRequestOptions ? { include: parseRequestOptions } : {},
6268
...wrapOptions,
6369
};
6470
return (req, res) => {

0 commit comments

Comments
 (0)