pillarjs / path-to-regexp

Turn a path string such as `/user/:name` into a regular expression
MIT License
8.15k stars 383 forks source link

path.replace is not a function #333

Closed wll8 closed 6 days ago

wll8 commented 1 week ago

The new version of path-to-regexp is an incompatible update

TypeError: path must be a string, array of strings, or regular expression
    at pathToRegexp (C:\git2\mockm\server\node_modules\express\node_modules\path-to-regexp\index.js:69:11)
    at new Layer (C:\git2\mockm\server\node_modules\express\lib\router\layer.js:45:17)
    at Function.route (C:\git2\mockm\server\node_modules\express\lib\router\index.js:505:15)
    at app.<computed> [as bind] (C:\git2\mockm\server\node_modules\express\lib\application.js:498:30)
    at new Server (C:\git2\mockm\server\node_modules\@httptoolkit\httpolyglot\dist\index.js:36:47)
    at Object.createServer (C:\git2\mockm\server\node_modules\@httptoolkit\httpolyglot\dist\index.js:159:12)
    at Object.getHttpServer (C:\git2\mockm\server\util\business.js:68:56)
    at serverProxy (C:\git2\mockm\server\proxy.js:34:49)
    at C:\git2\mockm\server\server.js:46:24

image

GrahamCampbell commented 1 week ago

path-to-regexp@0.2.5 is not a new version.

wll8 commented 6 days ago

The version that found the problem was 0.1.10, but this error was thrown in 0.1.11.

  if (typeof path !== 'string') {
    throw new TypeError('path must be a string, array of strings, or regular expression');
  }

imgA:

image

imgB:

image

In imgA, httpolyglot passed an object, which was concatenated into a string in path-to-regexp 0.1.7, and the program had no errors. However, in later versions of path-to-regexp, my use case ran into errors because the object could not be handled "friendly".

Unfortunately, httpolyglot does not accept questions. What should I do? Can path-to-regexp retain the old behavior and ignore it without reporting an error when a non-string is passed in, or something else?

blakeembrey commented 6 days ago

I won't be fixing this in path-to-regexp because this library was never intended to accept random objects as input. This appears to be a bug in httpolyglot, since bind has always been a HTTP method on that function and was not the native JS bind.

blakeembrey commented 6 days ago

It's a regrettable situation, but it appears that line never did what the authors intended (when combined with Express) and could be removed. I'd maybe suggest a PR removing this line: https://github.com/httptoolkit/httpolyglot/blob/8f726e1fce599b7a1444444cab396e818306c5b0/src/index.ts#L81

Alternatively if you write a wrapper function that has a real bind method, that would work too. E.g. (req, res) => app(req, res);.