Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: decode url for middleware #4728

Merged
merged 14 commits into from Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -51,7 +51,7 @@
"prompts": "^2.4.1",
"rimraf": "^3.0.2",
"semver": "^7.3.5",
"sirv": "^1.0.14",
"sirv": "^1.0.16",
"ts-jest": "^27.0.4",
"ts-node": "^10.1.0",
"typescript": "^4.3.5",
Expand Down
13 changes: 13 additions & 0 deletions packages/playground/assets/__tests__/assets.spec.ts
Expand Up @@ -183,6 +183,19 @@ test('?url import', async () => {
)
})

describe('unicode url', () => {
test('from js import', async () => {
const src = readFile('テスト-測試-white space.js')
expect(await page.textContent('.unicode-url')).toMatch(
isBuild
? `data:application/javascript;base64,${Buffer.from(src).toString(
'base64'
)}`
: `/foo/テスト-測試-white space.js`
)
})
})

test('new URL(..., import.meta.url)', async () => {
expect(await page.textContent('.import-meta-url')).toMatch(assetMatch)
})
Expand Down
16 changes: 13 additions & 3 deletions packages/playground/assets/index.html
@@ -1,5 +1,9 @@
<!DOCTYPE html>

<head>
<meta charset="UTF-8" />
</head>

<link rel="icon" href="data:," />
<link rel="stylesheet" href="/raw.css" />

Expand All @@ -14,9 +18,6 @@ <h2>Raw References from publicDir</h2>
<li class="raw-css">
Raw CSS from publicDir should load (this should be red)
</li>
<li>
<img src="/white space.png" />
</li>
</ul>

<h2>Asset Imports from JS</h2>
Expand Down Expand Up @@ -70,6 +71,12 @@ <h2>CSS url references</h2>
<span style="background: #fff">CSS background (aliased)</span>
</div>

<h2>Unicode URL</h2>
<div>
<code class="unicode-url"></code>
<img src="./nested/テスト-測試-white space.png" />
</div>

<h2>Image Src Set</h2>
<div>
<img
Expand Down Expand Up @@ -158,6 +165,9 @@ <h2>new URL(`./${dynamic}`, import.meta.url)</h2>
import fooUrl from './foo.js?url'
text('.url', fooUrl)

import unicodeUrl from './テスト-測試-white space.js?url'
text('.unicode-url', unicodeUrl)

const metaUrl = new URL('./nested/asset.png', import.meta.url)
text('.import-meta-url', metaUrl)
document.querySelector('.import-meta-url-img').src = metaUrl
Expand Down
1 change: 1 addition & 0 deletions packages/playground/assets/テスト-測試-white space.js
@@ -0,0 +1 @@
console.log('test unicode')
2 changes: 1 addition & 1 deletion packages/vite/package.json
Expand Up @@ -113,7 +113,7 @@
"resolve.exports": "^1.0.2",
"rollup-plugin-license": "^2.5.0",
"selfsigned": "^1.10.11",
"sirv": "^1.0.14",
"sirv": "^1.0.16",
"source-map": "^0.6.1",
"source-map-support": "^0.5.19",
"strip-ansi": "^6.0.0",
Expand Down
9 changes: 7 additions & 2 deletions packages/vite/src/node/server/middlewares/static.ts
Expand Up @@ -38,6 +38,8 @@ export function servePublicMiddleware(dir: string): Connect.NextHandleFunction {
if (isImportRequest(req.url!)) {
return next()
}
// reset sirv decoded url
req._decoded = undefined
hannoeru marked this conversation as resolved.
Show resolved Hide resolved
serve(req, res, next)
}
}
Expand All @@ -50,7 +52,7 @@ export function serveStaticMiddleware(

// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
return function viteServeStaticMiddleware(req, res, next) {
const url = req.url!
const url = decodeURI(req.url!)

// only serve the file if it's not an html request
// so that html requests can fallthrough to our html middleware for
Expand All @@ -76,7 +78,8 @@ export function serveStaticMiddleware(
}
req.url = redirected
}

// reset sirv decoded url
req._decoded = undefined
hannoeru marked this conversation as resolved.
Show resolved Hide resolved
serve(req, res, next)
}
}
Expand All @@ -100,6 +103,8 @@ export function serveRawFsMiddleware(
if (isWindows) url = url.replace(/^[A-Z]:/i, '')

req.url = url
// reset sirv decoded url
req._decoded = undefined
hannoeru marked this conversation as resolved.
Show resolved Hide resolved
serveFromRoot(req, res, next)
} else {
next()
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/middlewares/transform.ts
Expand Up @@ -93,7 +93,7 @@ export function transformMiddleware(
return
}

let url = removeTimestampQuery(req.url!).replace(
let url = decodeURI(removeTimestampQuery(req.url!)).replace(
NULL_BYTE_PLACEHOLDER,
'\0'
)
Expand Down
1 change: 1 addition & 0 deletions packages/vite/types/connect.d.ts
Expand Up @@ -15,6 +15,7 @@ export namespace Connect {

export class IncomingMessage extends http.IncomingMessage {
originalUrl?: http.IncomingMessage['url']
_decoded?: string
}

export type NextFunction = (err?: any) => void
Expand Down
16 changes: 15 additions & 1 deletion yarn.lock
Expand Up @@ -877,6 +877,11 @@
resolved "https://registry.yarnpkg.com/@polka/url/-/url-1.0.0-next.17.tgz#25fdbdfd282c2f86ddf3fcefbd98be99cd2627e2"
integrity sha512-0p1rCgM3LLbAdwBnc7gqgnvjHg9KpbhcSphergHShlkWz8EdPawoMJ3/VbezI0mGC5eKCDzMaPgF9Yca6cKvrg==

"@polka/url@^1.0.0-next.19":
version "1.0.0-next.19"
resolved "https://registry.yarnpkg.com/@polka/url/-/url-1.0.0-next.19.tgz#2c94db828794aa53e7a420809dac870348819233"
integrity sha512-kHR9OHwP9WLpyC0i/WCAQCgf5hXkR9C+/21qxmrn+YwRlDRnBlqrcrFpXxhJTA9LDHJWa/FjoO2LJ12q8iWlEQ==

"@rollup/plugin-alias@^3.1.5":
version "3.1.5"
resolved "https://registry.yarnpkg.com/@rollup/plugin-alias/-/plugin-alias-3.1.5.tgz#73356a3a1eab2e1e2fd952f9f53cd89fc740d952"
Expand Down Expand Up @@ -6972,7 +6977,7 @@ simple-swizzle@^0.2.2:
dependencies:
is-arrayish "^0.3.1"

sirv@^1.0.12, sirv@^1.0.14:
sirv@^1.0.12:
hannoeru marked this conversation as resolved.
Show resolved Hide resolved
version "1.0.14"
resolved "https://registry.yarnpkg.com/sirv/-/sirv-1.0.14.tgz#b826343f573e12653c5b3c3080a3a2a6a06595cd"
integrity sha512-czTFDFjK9lXj0u9mJ3OmJoXFztoilYS+NdRPcJoT182w44wSEkHSiO7A2517GLJ8wKM4GjCm2OXE66Dhngbzjg==
Expand All @@ -6981,6 +6986,15 @@ sirv@^1.0.12, sirv@^1.0.14:
mime "^2.3.1"
totalist "^1.0.0"

sirv@^1.0.16:
version "1.0.16"
resolved "https://registry.yarnpkg.com/sirv/-/sirv-1.0.16.tgz#9caadfc46c264a38ad1c38c99259692fbf76ed10"
integrity sha512-x56DISeIgSUGVJrQS3mwu+UvtnzHenKDFBQL+UlAswxwk9b2Cpc0KGVvftoIJZgweOOXbMZzyXFYgVElOuSI1Q==
dependencies:
"@polka/url" "^1.0.0-next.19"
mime "^2.3.1"
totalist "^1.0.0"

sisteransi@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-1.0.5.tgz#134d681297756437cc05ca01370d3a7a571075ed"
Expand Down