diff --git a/.changeset/fast-suits-fry.md b/.changeset/fast-suits-fry.md new file mode 100644 index 0000000..84d4d92 --- /dev/null +++ b/.changeset/fast-suits-fry.md @@ -0,0 +1,5 @@ +--- +"next-ws": patch +--- + +Escape dist path for safe require() usage diff --git a/src/patches/helpers/next.ts b/src/patches/helpers/next.ts index ef9681f..798faac 100644 --- a/src/patches/helpers/next.ts +++ b/src/patches/helpers/next.ts @@ -10,7 +10,7 @@ export function getDistDirname() { const nextWsPackagePath = // require.resolve('next-ws/package.json', resolveOptions); const nextWsDirName = resolveDirname(nextWsPackagePath); - return `${nextWsDirName.replaceAll('\\', '/')}/dist`; + return `${nextWsDirName}/dist`.replace(/\\/g, '/').replace(/'/g, "\\'"); } /** diff --git a/src/server/helpers/route.ts b/src/server/helpers/route.ts index bc05e6e..7a78f1a 100644 --- a/src/server/helpers/route.ts +++ b/src/server/helpers/route.ts @@ -24,7 +24,7 @@ function createRouteRegex(routePattern: string) { */ function getRouteParams(routePattern: string, routePath: string) { const routeRegex = createRouteRegex(routePattern); - const match = routePath.match(routeRegex); + const match = routePath.replace(/\/+$/, '').match(routeRegex); if (!match) return null; if (!match.groups) return {}; @@ -58,13 +58,13 @@ export function resolvePathToRoute( ...nextServer.getAppPathRoutes(), }; + let pathToRoute = null; for (const [routePath, [filePath]] of Object.entries(routes)) { const realPath = `${basePath}${routePath}`; const routeParams = getRouteParams(realPath, requestPath); - if (routeParams) return { filePath: filePath!, routeParams }; + if (routeParams) pathToRoute = { filePath: filePath!, routeParams }; } - - return null; + return pathToRoute || null; } /**