Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: keep backward compatibility with event.node.req.url #471

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 @@

// 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 @@
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 @@
) {
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 @@

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;
}
});
}