Skip to content

Commit f3ade65

Browse files
committed
fixup! fixup! feat(core, node): portable Express integration
1 parent a762b60 commit f3ade65

6 files changed

Lines changed: 22 additions & 17 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function patchLayer(options: ExpressPatchLayerOptions, layer?: ExpressLay
6565
const isLayerPathStored = !!layerPath;
6666

6767
const constructedRoute = getConstructedRoute(req);
68-
const actualMatchedRoute = getActualMatchedRoute(req);
68+
const actualMatchedRoute = getActualMatchedRoute(req, constructedRoute);
6969

7070
options.onRouteResolved?.(actualMatchedRoute);
7171

‎packages/core/src/integrations/express/types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export interface ExpressRequest extends RequestEventData {
3434
export interface ExpressResponse {
3535
once(ev: string, listener: Function): this;
3636
removeListener(ev: string, listener?: Function): this;
37+
emit(ev: string, ...data: unknown[]): this;
3738
}
3839

3940
export interface NextFunction {

‎packages/core/src/integrations/express/utils.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ export const isLayerIgnored = (
159159
* Returns the route that should be used as the http.route attribute.
160160
*
161161
* @param req - The Express request object with layers store
162-
* @param layersStoreProperty - The property name where layer paths are stored
162+
* @param constructedRoute - The constructed route from `getConstructedRoute`
163163
* @returns The matched route string or undefined if no valid route is found
164164
*/
165-
export function getActualMatchedRoute(req: ExpressRequest): string | undefined {
165+
export function getActualMatchedRoute(req: ExpressRequest, constructedRoute: string): string | undefined {
166166
const layersStore = getStoredLayers(req);
167167

168168
// If no layers are stored, no route can be determined
@@ -176,7 +176,6 @@ export function getActualMatchedRoute(req: ExpressRequest): string | undefined {
176176
return req.originalUrl === '/' ? '/' : undefined;
177177
}
178178

179-
const constructedRoute = getConstructedRoute(req);
180179
if (constructedRoute === '*') {
181180
return constructedRoute;
182181
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ describe('patchLayer', () => {
121121
hasActiveSpan = false;
122122
const options: ExpressPatchLayerOptions = {};
123123
const req = Object.assign(new EventEmitter(), {
124-
originalUrl: '/',
124+
originalUrl: '/a/b/c',
125125
}) as unknown as ExpressRequest;
126126

127127
const layerHandleOriginal = vi.fn();
@@ -153,7 +153,7 @@ describe('patchLayer', () => {
153153
ignoreLayersType: ['middleware'],
154154
};
155155
const req = Object.assign(new EventEmitter(), {
156-
originalUrl: '/',
156+
originalUrl: '/a/b/c/layerPath'
157157
}) as unknown as ExpressRequest;
158158

159159
const layerHandleOriginal = vi.fn();
@@ -168,9 +168,9 @@ describe('patchLayer', () => {
168168
storeLayer(req, '/:boo');
169169
storeLayer(req, '/:car');
170170

171-
patchLayer(options, layer);
171+
patchLayer(options, layer, '/layerPath');
172172
layer.handle(req, res);
173-
expect(onRouteResolved).toHaveBeenCalledExactlyOnceWith('/a/:boo/:car');
173+
expect(onRouteResolved).toHaveBeenCalledExactlyOnceWith('/a/:boo/:car/layerPath');
174174
expect(layerHandleOriginal).toHaveBeenCalledOnce();
175175

176176
// should not have emitted any spans, it was ignored.
@@ -185,7 +185,7 @@ describe('patchLayer', () => {
185185
data: {
186186
'express.name': 'mw',
187187
'express.type': 'middleware',
188-
'http.route': '/a/:boo/:car',
188+
'http.route': '/a/:boo/:car/layerPath',
189189
'sentry.op': 'middleware.express',
190190
'sentry.origin': 'auto.http.express',
191191
},

‎packages/core/test/lib/integrations/express/utils.test.ts‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,48 +327,49 @@ describe('isLayerIgnored', () => {
327327

328328
describe('getActualMatchedRoute', () => {
329329
it('handles empty layersStore', () => {
330-
expect(getActualMatchedRoute({} as unknown as ExpressRequest)).toBe(undefined);
330+
const req = {} as unknown as ExpressRequest;
331+
expect(getActualMatchedRoute(req, getConstructedRoute(req))).toBe(undefined);
331332
});
332333

333334
it('handles case when all stored layers are /', () => {
334335
const req = { originalUrl: '/' } as unknown as ExpressRequest;
335336
storeLayer(req, '/');
336-
expect(getActualMatchedRoute(req)).toBe('/');
337+
expect(getActualMatchedRoute(req, getConstructedRoute(req))).toBe('/');
337338
req.originalUrl = '/other-thing';
338-
expect(getActualMatchedRoute(req)).toBe(undefined);
339+
expect(getActualMatchedRoute(req, getConstructedRoute(req))).toBe(undefined);
339340
});
340341

341342
it('returns constructed route if *', () => {
342343
const req = { originalUrl: '/xyz' } as unknown as ExpressRequest;
343344
storeLayer(req, '*');
344-
expect(getActualMatchedRoute(req)).toBe('*');
345+
expect(getActualMatchedRoute(req, getConstructedRoute(req))).toBe('*');
345346
});
346347

347348
it('returns constructed route when it looks regexp-ish', () => {
348349
const req = { originalUrl: '/xyz' } as unknown as ExpressRequest;
349350
storeLayer(req, '/\\,[*]/');
350-
expect(getActualMatchedRoute(req)).toBe('/\\,[*]/');
351+
expect(getActualMatchedRoute(req, getConstructedRoute(req))).toBe('/\\,[*]/');
351352
});
352353

353354
it('ensures constructed route starts with /', () => {
354355
const req = { originalUrl: '/a/b' } as unknown as ExpressRequest;
355356
storeLayer(req, 'a');
356357
storeLayer(req, '/b');
357-
expect(getActualMatchedRoute(req)).toBe('/a/b');
358+
expect(getActualMatchedRoute(req, getConstructedRoute(req))).toBe('/a/b');
358359
});
359360

360361
it('allows routes that contain *', () => {
361362
const req = { originalUrl: '/a/b' } as unknown as ExpressRequest;
362363
storeLayer(req, 'a');
363364
storeLayer(req, '/:boo');
364-
expect(getActualMatchedRoute(req)).toBe('/a/:boo');
365+
expect(getActualMatchedRoute(req, getConstructedRoute(req))).toBe('/a/:boo');
365366
});
366367

367368
it('returns undefined if invalid', () => {
368369
const req = { originalUrl: '/a/b' } as unknown as ExpressRequest;
369370
storeLayer(req, '/a');
370371
storeLayer(req, '/c');
371-
expect(getActualMatchedRoute(req)).toBe(undefined);
372+
expect(getActualMatchedRoute(req, getConstructedRoute(req))).toBe(undefined);
372373
});
373374
});
374375

‎packages/core/vite.config.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,9 @@ export default {
44
...baseConfig,
55
test: {
66
...baseConfig.test,
7+
coverage: {
8+
...baseConfig.test?.coverage,
9+
include: ['src/integrations/express/*'],
10+
},
711
},
812
};

0 commit comments

Comments
 (0)