Skip to content

Commit 2d55c15

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

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

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

Lines changed: 1 addition & 2 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 {
@@ -105,7 +104,7 @@ export function patchLayer(options: ExpressPatchLayerOptions, maybeLayer?: Expre
105104
// prevent improper layer calculation in the case where there's a
106105
// middleware without a layerPath argument. It's unclear whether
107106
// that's possible, or if any existing code depends on that "bug".
108-
if (isLayerPathStored && type === ExpressLayerType_MIDDLEWARE) {
107+
if (isLayerPathStored) {
109108
storedLayers.pop();
110109
}
111110
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)