修复路径中存在特殊符号的问题

This commit is contained in:
2025-04-27 14:10:21 +08:00
parent fd364fb393
commit f494ca4652
3 changed files with 11 additions and 6 deletions

View File

@ -1,9 +1,12 @@
{
"name": "@yizhi/koa-router",
"version": "1.0.3",
"version": "1.0.4",
"main": "dist/index.js",
"types": "typing/index.d.ts",
"scripts": {},
"scripts": {
"watch": "rm -rf dist typing tsconfig.tsbuildinfo && tsc -w --inlineSourceMap",
"build": "rm -rf dist typing tsconfig.tsbuildinfo && tsc"
},
"author": "",
"license": "ISC",
"description": "",
@ -13,4 +16,4 @@
"devDependencies": {
"@types/koa": "^2.15.0"
}
}
}

View File

@ -128,6 +128,8 @@ export class KoaRouter {
const res = pathname.matchAll(/\{([a-zA-Z][a-zA-Z0-9_]*)(:([a-zA-Z][a-zA-Z0-9_]*))?\}/g);
const parsers: IKoaRouteConfig["parsers"] = {};
const resolveRegexp = (str: string) => str.replace(/\\/g, "\\\\").replace(/\./g, "\\.")
//处理参数,并生成正则表达式
const regexpItems: string[] = [];
let offset = 0;
@ -135,7 +137,7 @@ export class KoaRouter {
const name = match[1];
const type = match[3];
//放入路由前部分
regexpItems.push(pathname.slice(offset, match.index));
regexpItems.push(resolveRegexp(pathname.slice(offset, match.index)));
//放入路由参数部分
if (type) {
@ -150,7 +152,7 @@ export class KoaRouter {
}
//基础正则表达式
let regexpStr = "^" + regexpItems.join("") + pathname.slice(offset);
let regexpStr = "^" + regexpItems.join("") + resolveRegexp(pathname.slice(offset));
//严格模式
if (option?.strict ?? true) regexpStr += "$";

View File

@ -48,7 +48,7 @@
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
"inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
"outDir": "./dist", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */