Skip to content

Commit

Permalink
fix: readBody can return null
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 3, 2020
1 parent 5efb82d commit a83637e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/node/serverPluginModuleRewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ export const moduleRewritePlugin: Plugin = ({ app, watcher, resolver }) => {

if (ctx.path === '/index.html') {
const html = await readBody(ctx.body)
if (rewriteCache.has(html)) {
if (html && rewriteCache.has(html)) {
debug('/index.html: serving from cache')
ctx.body = rewriteCache.get(html)
} else if (ctx.body) {
await initLexer
let hasInjectedDevFlag = false
ctx.body = html.replace(
ctx.body = html!.replace(
/(<script\b[^>]*>)([\s\S]*?)<\/script>/gm,
(_, openTag, script) => {
// also inject __DEV__ flag
Expand All @@ -78,6 +78,7 @@ export const moduleRewritePlugin: Plugin = ({ app, watcher, resolver }) => {
// this allows us to post-process javascript produced by user middlewares
// regardless of the extension of the original files.
if (
ctx.body &&
ctx.response.is('js') &&
!ctx.url.endsWith('.map') &&
// skip internal client
Expand All @@ -92,7 +93,7 @@ export const moduleRewritePlugin: Plugin = ({ app, watcher, resolver }) => {
} else {
await initLexer
ctx.body = rewriteImports(
content,
content!,
ctx.url.replace(/(&|\?)t=\d+/, ''),
resolver,
ctx.query.t
Expand Down
6 changes: 3 additions & 3 deletions src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ export async function cachedRead(
* Useful in post-processing middlewares.
*/
export async function readBody(
stream: Readable | Buffer | string
): Promise<string> {
stream: Readable | Buffer | string | null
): Promise<string | null> {
if (stream instanceof Readable) {
return new Promise((resolve, reject) => {
let res = ''
Expand All @@ -97,6 +97,6 @@ export async function readBody(
})
})
} else {
return typeof stream === 'string' ? stream : stream.toString()
return !stream || typeof stream === 'string' ? stream : stream.toString()
}
}

0 comments on commit a83637e

Please sign in to comment.