Skip to content
Open
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
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- *Breaking:* The `env` middleware no longer calls `next()` after serving `/__/env.json` or `/__/env.js`.
- *Breaking:* Updated `mime-types` to v3; JavaScript files (`.js`, `.mjs`, `/__/env.js`) are now served as `text/javascript; charset=utf-8` (RFC 9239) instead of `application/javascript; charset=utf-8`, and `mime-db` v1.54+ updates several MIME mappings and marks `application/octet-stream` compressible.
- *Breaking:* Replaced `path-to-regexp` v1 with a built-in linear-time redirect path matcher and compiler. Undocumented inline regular expressions inside `redirects[].source` (e.g. `/:id(\d+)`) are no longer supported (use `redirects[].regex` instead), and ambiguous multi-parameter segments separated by non-`.` characters (e.g. `/:a-:b`) now match non-greedily from left to right.

15 changes: 0 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
"morgan": "^1.8.2",
"on-finished": "^2.2.0",
"on-headers": "^1.0.0",
"path-to-regexp": "^1.9.0",
"router": "^2.0.0",
"update-notifier-cjs": "^5.1.6"
},
Expand Down
72 changes: 39 additions & 33 deletions src/middleware/redirects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ import {
} from "connect";
import isUrl from "is-url";
import { ServerResponse } from "node:http";
import pathToRegexp from "path-to-regexp";

import { Configuration, Redirect as RedirectConfig } from "../config";
import { MiddlewareOptions } from "../options";
import {
createPathCompiler,
createPathMatcher,
PathCompiler,
PathMatcher,
} from "../utils/path-pattern";
import { slasher } from "../utils/pathutils";
import * as patterns from "../utils/patterns";

Expand Down Expand Up @@ -71,10 +75,9 @@ class Redirect {
private readonly glob?: string;
private readonly regex?: string;
private readonly destination: string;
private readonly captureKeys: pathToRegexp.Key[] = [];
private readonly engine?: "glob" | "pattern";
private readonly capture?: RegExp;
private readonly compileDestination?: pathToRegexp.PathFunction;
private readonly matchGlob?: PathMatcher;
private readonly captureRegex?: RegExp;
private readonly compileDestination?: PathCompiler;

constructor(redir: RedirectConfig) {
this.type = redir.type ?? 301;
Expand All @@ -84,40 +87,20 @@ class Redirect {
this.destination = redir.destination;

if (/(?:^|\/):/.test(this.destination)) {
if (this.glob) {
this.engine = "glob";
this.capture = pathToRegexp(this.glob, this.captureKeys);
}
if (this.regex) {
this.engine = "pattern";
this.capture = patterns.createRaw(this.regex);
this.captureRegex = patterns.createRaw(this.regex);
} else if (this.glob) {
this.matchGlob = createPathMatcher(this.glob);
}
this.compileDestination = pathToRegexp.compile(this.destination);
this.compileDestination = createPathCompiler(this.destination);
}
}

test(pathname: string, qs = ""): RedirectResult | undefined {
const match = this.capture?.exec(pathname);
if (match && this.compileDestination) {
const params: Record<string, string | string[] | undefined> = {};
if (this.engine === "glob") {
for (let i = 0; i < this.captureKeys.length; i++) {
const rawMatch = match[i + 1];
params[this.captureKeys[i].name] = rawMatch?.includes("/")
? rawMatch.split("/")
: rawMatch;
}
} else {
for (let j = 0; j < match.length; j++) {
params[j.toString()] = match[j];
}
if (match.groups) {
Object.assign(params, match.groups);
}
}

const params = this.extractParams(pathname);
if (params && this.compileDestination) {
try {
const dest = decodeURIComponent(this.compileDestination(params));
const dest = this.compileDestination(params);
return {
type: this.type,
destination: formatDestination(dest, qs),
Expand All @@ -134,6 +117,29 @@ class Redirect {
return undefined;
}

private extractParams(
pathname: string,
): Record<string, string | undefined> | undefined {
if (this.captureRegex) {
const match = this.captureRegex.exec(pathname);
if (!match) {
return undefined;
}
const params = Object.create(null) as Record<string, string | undefined>;
for (let j = 0; j < match.length; j++) {
params[j.toString()] = match[j];
}
if (match.groups) {
Object.assign(params, match.groups);
}
return params;
}
if (this.matchGlob) {
return this.matchGlob(pathname);
}
return undefined;
}

private matchesPattern(url: string): boolean {
if (this.glob) {
return patterns.configMatcher(url, { glob: this.glob });
Expand Down
Loading