Skip to content

vite server: escape colon in path#6364

Closed
milahu wants to merge 1 commit into
vitejs:mainfrom
milahu:patch-3
Closed

vite server: escape colon in path#6364
milahu wants to merge 1 commit into
vitejs:mainfrom
milahu:patch-3

Conversation

@milahu
Copy link
Copy Markdown
Contributor

@milahu milahu commented Jan 3, 2022

part of issue #5398 (comment)

module node:stream is cached as node_modules/.vite/node_stream.js

but importing node:stream returns http 404 not found

solution: rewrite the url path: escape colon to underscore

-/node_modules/.vite/node:stream.js?v=xxxxxxxx
+/node_modules/.vite/node_stream.js?v=xxxxxxxx

todo

replace only /node_modules/.vite/xxxx paths
but not /@id/__vite-xxxx etc

todo: test production mode
only tested in development mode (npx vite)

todo: maybe parse req.url to only replace the path
avoid false replacements like

-http://localhost:1234/node_modules/.vite/node:stream.js?v=xxxxxxxx
+http_//localhost_1234/node_modules/.vite/node_stream.js?v=xxxxxxxx

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@patak-cat
Copy link
Copy Markdown
Member

Why is node:stream being cached here or requested from the browser? Dependencies that uses node builtins should be fixed upstream, or maybe a project like https://github.com/unjs/unenv could be used as a temporal patch until they are fixed

@bluwy
Copy link
Copy Markdown
Member

bluwy commented Jan 3, 2022

My quick guess is that during the prebundle scan, since node:stream is aliased as stream-browserify, it detects it as a dependency for prebundling. So it prebundles as node_stream.js that contains stream-browserify, which is a bit weird or maybe it makes sense?

But either way if we do want a quick fix, we should update flattenId to handle colons instead, but I don't think this is a sustainable change and fix the question above instead.

middlewares.use(function escapeColonMiddleware(req, res, next) {
if (req.url) {
// example url: /node_modules/.vite/node:stream.js?v=xxxxxxxx
req.url = req.url.replace(/:/g, '_')
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regex doesn't look safe at all 👀
Is it really wanted to find all colons? Also that one in query params?

Beside that, I think this would need a test

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we want to do this, shouldn't it only happen for node:?

@milahu
Copy link
Copy Markdown
Contributor Author

milahu commented Jan 3, 2022

this is just a quick hack, obviously ...

Why is node:stream being cached here or requested from the browser?

not relevant, but if you want to reproduce, see #5398 (comment)

the problem is the mismatch between
requested filename node:stream.js and
cached filename node_stream.js

we should update flattenId to handle colons instead

wdym? colon would break on windows, see valid filenames

@bluwy bluwy mentioned this pull request Feb 20, 2022
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants