Skip to content

Commit 4adacd5

Browse files
committed
fixup! fixup! fixup! feat(core, node): portable Express integration
1 parent fd870be commit 4adacd5

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

packages/core/src/integrations/express/patch-layer.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
ATTR_HTTP_ROUTE,
1414
ATTR_EXPRESS_TYPE,
1515
ATTR_EXPRESS_NAME,
16-
ExpressLayerType_MIDDLEWARE,
1716
ExpressLayerType_ROUTER,
1817
} from './types';
1918
import {
@@ -33,9 +32,6 @@ export type ExpressPatchLayerOptions = Pick<
3332
'onRouteResolved' | 'ignoreLayers' | 'ignoreLayersType'
3433
>;
3534

36-
// exported for testing
37-
export const kDidSetSDKMetadata = Symbol('sentry.DidSetSDKMetadata');
38-
3935
export function patchLayer(options: ExpressPatchLayerOptions, maybeLayer?: ExpressLayer, layerPath?: string): void {
4036
if (!maybeLayer?.handle) {
4137
return;
@@ -56,7 +52,7 @@ export function patchLayer(options: ExpressPatchLayerOptions, maybeLayer?: Expre
5652

5753
function layerHandlePatched(
5854
this: ExpressLayer,
59-
req: ExpressRequest & { [kDidSetSDKMetadata]?: boolean },
55+
req: ExpressRequest,
6056
res: ExpressResponse,
6157
//oxlint-disable-next-line no-explicit-any
6258
...otherArgs: any[]
@@ -105,7 +101,7 @@ export function patchLayer(options: ExpressPatchLayerOptions, maybeLayer?: Expre
105101
// prevent improper layer calculation in the case where there's a
106102
// middleware without a layerPath argument. It's unclear whether
107103
// that's possible, or if any existing code depends on that "bug".
108-
if (isLayerPathStored && type === ExpressLayerType_MIDDLEWARE) {
104+
if (isLayerPathStored) {
109105
storedLayers.pop();
110106
}
111107
return layerHandleOriginal.apply(this, [req, res, ...otherArgs]);

packages/core/test/lib/integrations/express/patch-layer.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,34 @@ describe('patchLayer', () => {
264264
checkSpans([]);
265265
});
266266

267+
it('pops storedLayers when ignoring router or request_handler type layers', () => {
268+
for (const type of ['router', 'request_handler'] as const) {
269+
const options: ExpressPatchLayerOptions = { ignoreLayersType: [type] };
270+
const req = Object.assign(new EventEmitter(), {
271+
originalUrl: '/a/b/c/layerPath',
272+
}) as unknown as ExpressRequest;
273+
274+
// simulate layers already stored for previous path segments
275+
storeLayer(req, '/a');
276+
storeLayer(req, '/b');
277+
278+
// patch a layer of the ignored type with a layerPath
279+
const layerHandleOriginal = vi.fn();
280+
// layer.name must match what getLayerMetadata uses to classify each type:
281+
// 'router' → router, 'bound dispatch' → request_handler, other → middleware
282+
const layerName = type === 'router' ? 'router' : 'bound dispatch';
283+
const layer = { name: layerName, handle: layerHandleOriginal } as unknown as ExpressLayer;
284+
patchLayer(options, layer, '/c');
285+
286+
// storeLayer('/c') happens inside the patched handle, before being popped
287+
// after handle returns, storedLayers should be back to ['/a', '/b']
288+
layer.handle(req, Object.assign(new EventEmitter(), {}) as unknown as ExpressResponse);
289+
290+
// the ignored layer's path must be cleaned up so subsequent layers see the correct route
291+
expect(getStoredLayers(req)).toStrictEqual(['/a', '/b']);
292+
}
293+
});
294+
267295
it('warns about not setting name in default isolation scope', async () => {
268296
inDefaultIsolationScope = true;
269297
DEBUG_BUILD = true;

0 commit comments

Comments
 (0)