Skip to content

Commit

Permalink
fix: keep backward compatibility with event.node.req.url
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 31, 2023
1 parent 51ec2bb commit 44345e9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
14 changes: 12 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,18 @@ export function use(
export function createAppEventHandler(stack: Stack, options: AppOptions) {
const spacing = options.debug ? 2 : undefined;
return eventHandler(async (event) => {
const _reqPath = event.path;
// Keep original incoming url accessable
(event.node.req as { originalUrl?: string }).originalUrl =
(event.node.req as { originalUrl?: string }).originalUrl ||
event.node.req.url ||
"/";

Check warning on line 109 in src/app.ts

View check run for this annotation

Codecov / codecov/patch

src/app.ts#L109

Added line #L109 was not covered by tests

// Keep a copy of incoming url
const _reqPath = event._path || event.node.req.url || "/";

// Layer path is the path without the prefix
let _layerPath: string;

for (const layer of stack) {
// 1. Remove prefix from path
if (layer.route.length > 1) {
Expand All @@ -122,7 +132,7 @@ export function createAppEventHandler(stack: Stack, options: AppOptions) {

// 3. Update event path with layer path
event._path = _layerPath;
event.node.req.url = _layerPath; // Express compatibility
event.node.req.url = _layerPath;

// 4. Handle request
const val = await layer.handler(event);
Expand Down
15 changes: 1 addition & 14 deletions src/event/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import {
} from "../utils";
import { H3Response } from "./response";

// TODO: Dedup from request.ts
const DOUBLE_SLASH_RE = /[/\\]{2,}/g;

// TODO: Dedup from body.ts
const PayloadMethods: Set<HTTPMethod> = new Set([
"PATCH",
Expand Down Expand Up @@ -64,17 +61,7 @@ export class H3Event<
}

get path() {
if (!this._path) {
const hasQuery = this._originalPath.includes("?");

if (hasQuery) {
const [basePath, query] = this._originalPath.split("?");
this._path = basePath.replace(DOUBLE_SLASH_RE, "/") + "?" + query;
} else {
this._path = this._originalPath.replace(DOUBLE_SLASH_RE, "/");
}
}
return this._path;
return this._path || this.node.req.url || "/";
}

get url() {
Expand Down
13 changes: 10 additions & 3 deletions src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,16 @@ export function getRequestProtocol(
return (event.node.req.connection as any).encrypted ? "https" : "http";
}

/** @deprecated Use `event.path` directly */
const DOUBLE_SLASH_RE = /[/\\]{2,}/g;

export function getRequestPath(event: H3Event): string {
return event.path;
const path = event._originalPath.replace(DOUBLE_SLASH_RE, "/");
if (path.includes("?")) {
const [basePath, query] = path.split("?");
return basePath.replace(DOUBLE_SLASH_RE, "/") + "?" + query;

Check warning on line 136 in src/utils/request.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/request.ts#L135-L136

Added lines #L135 - L136 were not covered by tests
} else {
return path.replace(DOUBLE_SLASH_RE, "/");
}
}

export function getRequestURL(
Expand All @@ -138,6 +145,6 @@ export function getRequestURL(
) {
const host = getRequestHost(event, opts);
const protocol = getRequestProtocol(event);
const path = event.path;
const path = getRequestPath(event);
return new URL(path, `${protocol}://${host}`);
}
17 changes: 14 additions & 3 deletions src/utils/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,27 @@ import { eventHandler } from "../event";

export function useBase(base: string, handler: EventHandler): EventHandler {
base = withoutTrailingSlash(base);
if (!base) {

if (!base || base === "/") {
return handler;
}

return eventHandler(async (event) => {
const _path = event._path;
// Keep original incoming url accessable
(event.node.req as { originalUrl?: string }).originalUrl =
(event.node.req as { originalUrl?: string }).originalUrl ||
event.node.req.url ||
"/";

Check warning on line 17 in src/utils/route.ts

View check run for this annotation

Codecov / codecov/patch

src/utils/route.ts#L17

Added line #L17 was not covered by tests

const _path = event._path || event.node.req.url || "/";

event._path = withoutBase(event.path || "/", base);
event.node.req.url = event._path;

try {
return await handler(event);
} finally {
event._path = _path;
event._path = event.node.req.url = _path;
}
});
}

0 comments on commit 44345e9

Please sign in to comment.