-
-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the pulicMiddleware (#1673)
* feat: add the pulicMiddleware * feat: add the logger for the publicMiddleware --------- Co-authored-by: zengwenjie.paq <zengwenjie.paq@bytedance.com> Co-authored-by: ADNY <66500121+ErKeLost@users.noreply.github.com>
- Loading branch information
1 parent
3ffa686
commit 69391da
Showing
12 changed files
with
189 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 99 additions & 8 deletions
107
packages/core/src/newServer/middlewares/publicMiddleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,107 @@ | ||
import { Compiler } from '../../compiler/index.js'; | ||
import sirv from 'sirv'; | ||
import { ResolvedUserConfig } from '../../config/types.js'; | ||
import { HttpServer } from '../index.js'; | ||
import { colors } from '../../utils/color.js'; | ||
import { Logger } from '../../utils/logger.js'; | ||
import { removeHashFromPath, withTrailingSlash } from '../../utils/path.js'; | ||
import { normalizePath } from '../../utils/share.js'; | ||
import { | ||
cleanUrl, | ||
isImportRequest, | ||
knownJavascriptExtensionRE, | ||
removeImportQuery, | ||
urlRE | ||
} from '../../utils/url.js'; | ||
|
||
function warnAboutPublicDir(url: string, publicPath: string) { | ||
let warning: string; | ||
if (isImportRequest(url)) { | ||
const rawUrl = removeImportQuery(url); | ||
if (urlRE.test(url)) { | ||
warning = | ||
`Assets in the public directory are directly accessible at the root path.\n` + | ||
`Use ${colors.brandColor( | ||
rawUrl.replace(publicPath, '/') | ||
)} instead of the explicit ${colors.brandColor(rawUrl)}.`; | ||
} else { | ||
warning = | ||
'Assets in the public directory should not be imported directly in JavaScript.\n' + | ||
`To import an asset, place it inside the src directory. Use ${colors.brandColor( | ||
rawUrl.replace(publicPath, '/src/') | ||
)} instead of ${colors.cyan(rawUrl)}.\n` + | ||
`For referencing the asset's path, use ${colors.brandColor( | ||
rawUrl.replace(publicPath, '/') | ||
)}.`; | ||
} | ||
} else { | ||
warning = | ||
`Public directory files are accessible directly at the root path.\n` + | ||
`Use ${colors.brandColor( | ||
url.replace(publicPath, '/') | ||
)} directly, rather than ${colors.brandColor(`${publicPath}${url}`)}.`; | ||
} | ||
|
||
return warning; | ||
} | ||
|
||
export function publicMiddleware( | ||
server: HttpServer, | ||
compiler: Compiler, | ||
publicPath: string, | ||
config: ResolvedUserConfig | ||
logger: Logger, | ||
config: ResolvedUserConfig, | ||
publicFiles?: Set<string> | ||
) { | ||
return async function handlerPublicMiddleware( | ||
const { publicDir, root } = config; | ||
const publicPath = `${publicDir.slice(root.length)}`; | ||
const headers = config.server.headers; | ||
const serve = sirv(publicDir, { | ||
dev: true, | ||
etag: true, | ||
extensions: [], | ||
setHeaders: (res, path) => { | ||
if (knownJavascriptExtensionRE.test(path)) { | ||
res.setHeader('Content-Type', 'text/javascript'); | ||
} | ||
if (headers) { | ||
for (const name in headers) { | ||
res.setHeader(name, headers[name]!); | ||
} | ||
} | ||
} | ||
}); | ||
const toFilePath = (url: string) => { | ||
let filePath = cleanUrl(url); | ||
if (filePath.indexOf('%') !== -1) { | ||
try { | ||
filePath = decodeURI(filePath); | ||
} catch (err) { | ||
// ignore | ||
} | ||
} | ||
return normalizePath(filePath); | ||
}; | ||
|
||
return async function farmHandlerPublicMiddleware( | ||
req: any, | ||
res: any, | ||
next: () => void | ||
) {}; | ||
) { | ||
const url = removeHashFromPath(req.url!); | ||
const filePath = toFilePath(url); | ||
|
||
// If it is not equal, it means that it is recognized as a module | ||
if ( | ||
publicDir.startsWith(withTrailingSlash(root)) && | ||
publicFiles.has(url) && | ||
req.url !== url | ||
) { | ||
const publicDirWarning = warnAboutPublicDir(url, publicPath); | ||
if (publicDirWarning) { | ||
logger.warn(publicDirWarning); | ||
} | ||
} | ||
|
||
if (publicFiles && !publicFiles.has(filePath)) { | ||
return next(); | ||
} | ||
|
||
serve(req, res, next); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.