Skip to content

Commit d468f58

Browse files
fix(tracing-internal): Fix case when middleware contain array of routes with special chars as @ (#9375)
1 parent 677cb02 commit d468f58

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

packages/tracing-internal/src/node/integrations/express.ts

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ type Layer = {
6565
route?: { path: RouteType | RouteType[] };
6666
path?: string;
6767
regexp?: RegExp;
68-
keys?: { name: string; offset: number; optional: boolean }[];
68+
keys?: { name: string | number; offset: number; optional: boolean }[];
6969
};
7070

7171
type RouteType = string | RegExp;
@@ -433,30 +433,34 @@ export const extractOriginalRoute = (
433433
/**
434434
* iterate param matches from regexp.exec
435435
*/
436-
paramIndices.forEach(([startOffset, endOffset], index: number) => {
437-
/**
438-
* isolate part before param
439-
*/
440-
const substr1 = resultPath.substring(0, startOffset - indexShift);
441-
/**
442-
* define paramName as replacement in format :pathParam
443-
*/
444-
const replacement = `:${orderedKeys[index].name}`;
445-
446-
/**
447-
* isolate part after param
448-
*/
449-
const substr2 = resultPath.substring(endOffset - indexShift);
450-
451-
/**
452-
* recreate original path but with param replacement
453-
*/
454-
resultPath = substr1 + replacement + substr2;
455-
456-
/**
457-
* calculate new index shift after resultPath was modified
458-
*/
459-
indexShift = indexShift + (endOffset - startOffset - replacement.length);
436+
paramIndices.forEach((item: [number, number] | undefined, index: number) => {
437+
/** check if offsets is define because in some cases regex d flag returns undefined */
438+
if (item) {
439+
const [startOffset, endOffset] = item;
440+
/**
441+
* isolate part before param
442+
*/
443+
const substr1 = resultPath.substring(0, startOffset - indexShift);
444+
/**
445+
* define paramName as replacement in format :pathParam
446+
*/
447+
const replacement = `:${orderedKeys[index].name}`;
448+
449+
/**
450+
* isolate part after param
451+
*/
452+
const substr2 = resultPath.substring(endOffset - indexShift);
453+
454+
/**
455+
* recreate original path but with param replacement
456+
*/
457+
resultPath = substr1 + replacement + substr2;
458+
459+
/**
460+
* calculate new index shift after resultPath was modified
461+
*/
462+
indexShift = indexShift + (endOffset - startOffset - replacement.length);
463+
}
460464
});
461465

462466
return resultPath;

packages/tracing-internal/test/node/express.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,26 @@ if (major >= 16) {
8787
];
8888
expect(extractOriginalRoute(path, regex, keys)).toBe('/user/:userId/profile/:username');
8989
});
90+
91+
it('should handle complex regex scheme extract from array of routes', () => {
92+
const path1 = '/@fs/*';
93+
const path2 = '/@vite/client';
94+
const path3 = '/@react-refresh';
95+
const path4 = '/manifest.json';
96+
97+
const regex =
98+
/(?:^\/manifest\.json\/?(?=\/|$)|^\/@vite\/client\/?(?=\/|$)|^\/@react-refresh\/?(?=\/|$)|^\/src\/(.*)\/?(?=\/|$)|^\/vite\/(.*)\/?(?=\/|$)|^\/node_modules\/(.*)\/?(?=\/|$)|^\/@fs\/(.*)\/?(?=\/|$)|^\/@vite-plugin-checker-runtime\/?(?=\/|$)|^\/?$\/?(?=\/|$)|^\/home\/?$\/?(?=\/|$)|^\/login\/?(?=\/|$))/;
99+
const keys = [
100+
{ name: 0, offset: 8, optional: false },
101+
{ name: 0, offset: 8, optional: false },
102+
{ name: 0, offset: 9, optional: false },
103+
{ name: 0, offset: 17, optional: false },
104+
];
105+
106+
expect(extractOriginalRoute(path1, regex, keys)).toBe('/@fs/:0');
107+
expect(extractOriginalRoute(path2, regex, keys)).toBe('/@vite/client');
108+
expect(extractOriginalRoute(path3, regex, keys)).toBe('/@react-refresh');
109+
expect(extractOriginalRoute(path4, regex, keys)).toBe('/manifest.json');
110+
});
90111
});
91112
}

0 commit comments

Comments
 (0)