Skip to content

Commit 72e8c2e

Browse files
committed
extract pathname without hostname
1 parent 29b0f0f commit 72e8c2e

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

packages/hono/src/shared/patchAppRequest.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ const INTERNAL_REQUEST_ORIGIN = 'auto.http.hono.internal_request';
1717
// Widened type to allow forwarding opaque args (env bindings, execution context)
1818
type LooseRequestFn = (input: string | Request | URL, requestInit?: RequestInit, ...rest: unknown[]) => unknown;
1919

20+
function extractPathname(input: string | Request | URL): string {
21+
if (typeof input === 'string') {
22+
return /^https?:\/\//.test(input) ? new URL(input).pathname : input;
23+
}
24+
25+
return input instanceof Request ? new URL(input.url).pathname : input.pathname;
26+
}
27+
2028
/**
2129
* Patches `app.request()` on a Hono instance so that each internal dispatch
2230
* is traced as a `hono.request` span — child of whatever span is active at
@@ -41,8 +49,7 @@ export function patchAppRequest<E extends Env>(app: Hono<E>): void {
4149
let method = requestInit?.method ?? (input instanceof Request ? input.method : 'GET');
4250
method = method.toUpperCase();
4351

44-
const path =
45-
typeof input === 'string' ? input : input instanceof Request ? new URL(input.url).pathname : input.pathname;
52+
const path = extractPathname(input);
4653

4754
return startSpan(
4855
{

packages/hono/test/shared/patchAppRequest.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,29 @@ describe('patchAppRequest', () => {
119119
expect(sentryOriginal).toBe(originalRequest);
120120
});
121121

122+
it('extracts pathname from a full URL string instead of using the raw string', async () => {
123+
const app = new Hono();
124+
app.get('/api/hello', c => c.text('world'));
125+
patchAppRequest(app);
126+
127+
await app.request('http://localhost/api/hello');
128+
129+
expect(startSpanMock).toHaveBeenCalledWith(
130+
expect.objectContaining({ name: 'GET /api/hello' }),
131+
expect.any(Function),
132+
);
133+
});
134+
135+
it('extracts pathname from an https URL string', async () => {
136+
const app = new Hono();
137+
app.get('/secure', c => c.text('ok'));
138+
patchAppRequest(app);
139+
140+
await app.request('https://example.com/secure');
141+
142+
expect(startSpanMock).toHaveBeenCalledWith(expect.objectContaining({ name: 'GET /secure' }), expect.any(Function));
143+
});
144+
122145
it('extracts pathname from a Request object input', async () => {
123146
const app = new Hono();
124147
app.get('/items/abc', c => c.text('found'));

0 commit comments

Comments
 (0)