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 13 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
19 changes: 14 additions & 5 deletions packages/vite/src/node/server/middlewares/static.ts
Expand Up @@ -8,6 +8,7 @@ import {
ensureLeadingSlash,
fsPathFromId,
isImportRequest,
isInternalRequest,
isWindows,
slash
} from '../../utils'
Expand All @@ -34,8 +35,8 @@ export function servePublicMiddleware(dir: string): Connect.NextHandleFunction {

// Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...`
return function viteServePublicMiddleware(req, res, next) {
// skip import request
if (isImportRequest(req.url!)) {
// skip import request and internal requests `/@fs/ /@vite-client` etc...
if (isImportRequest(req.url!) || isInternalRequest(req.url!)) {
return next()
}
serve(req, res, next)
Expand All @@ -50,15 +51,19 @@ 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!

// only serve the file if it's not an html request
// so that html requests can fallthrough to our html middleware for
// special processing
if (path.extname(cleanUrl(url)) === '.html') {
// also skip internal requests `/@fs/ /@vite-client` etc...
if (
path.extname(cleanUrl(req.url!)) === '.html' ||
isInternalRequest(req.url!)
) {
return next()
}

const url = decodeURI(req.url!)

// apply aliases to static requests as well
let redirected: string | undefined
for (const { find, replacement } of config.resolve.alias) {
Expand All @@ -75,6 +80,8 @@ export function serveStaticMiddleware(
redirected = redirected.slice(dir.length)
}
req.url = redirected
// reset sirv decoded url
delete req._decoded
}

serve(req, res, next)
Expand All @@ -100,6 +107,8 @@ export function serveRawFsMiddleware(
if (isWindows) url = url.replace(/^[A-Z]:/i, '')

req.url = url
// reset sirv decoded url
delete req._decoded
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
17 changes: 16 additions & 1 deletion packages/vite/src/node/utils.ts
Expand Up @@ -4,7 +4,13 @@ import fs from 'fs'
import os from 'os'
import path from 'path'
import { pathToFileURL, URL } from 'url'
import { FS_PREFIX, DEFAULT_EXTENSIONS, VALID_ID_PREFIX } from './constants'
import {
FS_PREFIX,
DEFAULT_EXTENSIONS,
VALID_ID_PREFIX,
CLIENT_PUBLIC_PATH,
ENV_PUBLIC_PATH
} from './constants'
import resolve from 'resolve'
import builtins from 'builtin-modules'
import { FSWatcher } from 'chokidar'
Expand Down Expand Up @@ -122,8 +128,17 @@ export const isJSRequest = (url: string): boolean => {
}

const importQueryRE = /(\?|&)import=?(?:&|$)/
const internalPrefixes = [
FS_PREFIX,
VALID_ID_PREFIX,
CLIENT_PUBLIC_PATH,
ENV_PUBLIC_PATH
]
const InternalPrefixRE = new RegExp(`^(?:${internalPrefixes.join('|')})`)
const trailingSeparatorRE = /[\?&]$/
export const isImportRequest = (url: string): boolean => importQueryRE.test(url)
export const isInternalRequest = (url: string): boolean =>
InternalPrefixRE.test(url)

export function removeImportQuery(url: string): string {
return url.replace(importQueryRE, '$1').replace(trailingSeparatorRE, '')
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