fix(nuxt): guard res.end() calls with check if event is handled (#21665)

This commit is contained in:
Daniel Roe 2023-06-21 07:07:02 +01:00 committed by GitHub
parent aa2fd0112d
commit c3c56c14ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 35 additions and 30 deletions

View File

@ -58,7 +58,7 @@
"execa": "7.1.1", "execa": "7.1.1",
"fs-extra": "11.1.1", "fs-extra": "11.1.1",
"globby": "13.2.0", "globby": "13.2.0",
"h3": "1.6.6", "h3": "1.7.0",
"jiti": "1.18.2", "jiti": "1.18.2",
"markdownlint-cli": "^0.33.0", "markdownlint-cli": "^0.33.0",
"nuxi": "workspace:*", "nuxi": "workspace:*",

View File

@ -36,7 +36,7 @@
"execa": "7.1.1", "execa": "7.1.1",
"flat": "5.0.2", "flat": "5.0.2",
"giget": "1.1.2", "giget": "1.1.2",
"h3": "1.6.6", "h3": "1.7.0",
"jiti": "1.18.2", "jiti": "1.18.2",
"listhen": "1.0.4", "listhen": "1.0.4",
"mlly": "1.3.0", "mlly": "1.3.0",

View File

@ -72,7 +72,7 @@
"estree-walker": "^3.0.3", "estree-walker": "^3.0.3",
"fs-extra": "^11.1.1", "fs-extra": "^11.1.1",
"globby": "^13.2.0", "globby": "^13.2.0",
"h3": "^1.6.6", "h3": "^1.7.0",
"hookable": "^5.5.3", "hookable": "^5.5.3",
"jiti": "^1.18.2", "jiti": "^1.18.2",
"klona": "^2.0.6", "klona": "^2.0.6",

View File

@ -21,9 +21,6 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
data: error.data data: error.data
} }
// Set response code and message
setResponseStatus(event, (errorObject.statusCode !== 200 && errorObject.statusCode) as any as number || 500, errorObject.statusMessage)
// Console output // Console output
if (error.unhandled || error.fatal) { if (error.unhandled || error.fatal) {
const tags = [ const tags = [
@ -36,6 +33,11 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
console.error(tags, errorObject.message + '\n' + stack.map(l => ' ' + l.text).join(' \n')) console.error(tags, errorObject.message + '\n' + stack.map(l => ' ' + l.text).join(' \n'))
} }
if (event.handled) { return }
// Set response code and message
setResponseStatus(event, (errorObject.statusCode !== 200 && errorObject.statusCode) as any as number || 500, errorObject.statusMessage)
// JSON response // JSON response
if (isJsonRequest(event)) { if (isJsonRequest(event)) {
setResponseHeader(event, 'Content-Type', 'application/json') setResponseHeader(event, 'Content-Type', 'application/json')
@ -63,16 +65,19 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
// TODO: Support `message` in template // TODO: Support `message` in template
(errorObject as any).description = errorObject.message (errorObject as any).description = errorObject.message
} }
if (event.handled) { return }
setResponseHeader(event, 'Content-Type', 'text/html;charset=UTF-8') setResponseHeader(event, 'Content-Type', 'text/html;charset=UTF-8')
event.node.res.end(template(errorObject)) event.node.res.end(template(errorObject))
return return
} }
const html = await res.text()
if (event.handled) { return }
for (const [header, value] of res.headers.entries()) { for (const [header, value] of res.headers.entries()) {
setResponseHeader(event, header, value) setResponseHeader(event, header, value)
} }
setResponseStatus(event, res.status && res.status !== 200 ? res.status : undefined, res.statusText) setResponseStatus(event, res.status && res.status !== 200 ? res.status : undefined, res.statusText)
event.node.res.end(await res.text()) event.node.res.end(html)
} }

View File

@ -34,7 +34,7 @@
"@vitejs/plugin-vue": "4.2.3", "@vitejs/plugin-vue": "4.2.3",
"@vitejs/plugin-vue-jsx": "3.0.1", "@vitejs/plugin-vue-jsx": "3.0.1",
"esbuild-loader": "3.0.1", "esbuild-loader": "3.0.1",
"h3": "1.6.6", "h3": "1.7.0",
"ignore": "5.2.4", "ignore": "5.2.4",
"nitropack": "2.4.1", "nitropack": "2.4.1",
"unbuild": "latest", "unbuild": "latest",

View File

@ -40,7 +40,7 @@
"externality": "^1.0.2", "externality": "^1.0.2",
"fs-extra": "^11.1.1", "fs-extra": "^11.1.1",
"get-port-please": "^3.0.1", "get-port-please": "^3.0.1",
"h3": "^1.6.6", "h3": "^1.7.0",
"knitwork": "^1.0.0", "knitwork": "^1.0.0",
"magic-string": "^0.30.0", "magic-string": "^0.30.0",
"mlly": "^1.3.0", "mlly": "^1.3.0",

View File

@ -31,7 +31,7 @@
"file-loader": "^6.2.0", "file-loader": "^6.2.0",
"fork-ts-checker-webpack-plugin": "^8.0.0", "fork-ts-checker-webpack-plugin": "^8.0.0",
"fs-extra": "^11.1.1", "fs-extra": "^11.1.1",
"h3": "^1.6.6", "h3": "^1.7.0",
"hash-sum": "^2.0.0", "hash-sum": "^2.0.0",
"lodash-es": "^4.17.21", "lodash-es": "^4.17.21",
"magic-string": "^0.30.0", "magic-string": "^0.30.0",

View File

@ -81,8 +81,8 @@ importers:
specifier: 13.2.0 specifier: 13.2.0
version: 13.2.0 version: 13.2.0
h3: h3:
specifier: 1.6.6 specifier: 1.7.0
version: 1.6.6 version: 1.7.0
jiti: jiti:
specifier: 1.18.2 specifier: 1.18.2
version: 1.18.2 version: 1.18.2
@ -278,8 +278,8 @@ importers:
specifier: 1.1.2 specifier: 1.1.2
version: 1.1.2 version: 1.1.2
h3: h3:
specifier: 1.6.6 specifier: 1.7.0
version: 1.6.6 version: 1.7.0
jiti: jiti:
specifier: 1.18.2 specifier: 1.18.2
version: 1.18.2 version: 1.18.2
@ -383,8 +383,8 @@ importers:
specifier: ^13.2.0 specifier: ^13.2.0
version: 13.2.0 version: 13.2.0
h3: h3:
specifier: ^1.6.6 specifier: ^1.7.0
version: 1.6.6 version: 1.7.0
hookable: hookable:
specifier: ^5.5.3 specifier: ^5.5.3
version: 5.5.3 version: 5.5.3
@ -559,8 +559,8 @@ importers:
specifier: 3.0.1 specifier: 3.0.1
version: 3.0.1(webpack@5.87.0) version: 3.0.1(webpack@5.87.0)
h3: h3:
specifier: 1.6.6 specifier: 1.7.0
version: 1.6.6 version: 1.7.0
ignore: ignore:
specifier: 5.2.4 specifier: 5.2.4
version: 5.2.4 version: 5.2.4
@ -686,8 +686,8 @@ importers:
specifier: ^3.0.1 specifier: ^3.0.1
version: 3.0.1 version: 3.0.1
h3: h3:
specifier: ^1.6.6 specifier: ^1.7.0
version: 1.6.6 version: 1.7.0
knitwork: knitwork:
specifier: ^1.0.0 specifier: ^1.0.0
version: 1.0.0 version: 1.0.0
@ -801,8 +801,8 @@ importers:
specifier: ^11.1.1 specifier: ^11.1.1
version: 11.1.1 version: 11.1.1
h3: h3:
specifier: ^1.6.6 specifier: ^1.7.0
version: 1.6.6 version: 1.7.0
hash-sum: hash-sum:
specifier: ^2.0.0 specifier: ^2.0.0
version: 2.0.0 version: 2.0.0
@ -5385,12 +5385,12 @@ packages:
dependencies: dependencies:
duplexer: 0.1.2 duplexer: 0.1.2
/h3@1.6.6: /h3@1.7.0:
resolution: {integrity: sha512-DWu2s11OuuO9suEkX99dXaJoxd1RgPXiM4iDmLdrhGV63GLoav13f3Kdd5/Rw7xNKzhzn2+F2dleQjG66SnMPQ==} resolution: {integrity: sha512-iJJz2Pn2rC0j8CB3rkFMs0K269W7hDVOC7eL3qne5Joy4JZX1W7id7PBFV593GboHDOx0PzgO6ocqsynrIvdxw==}
dependencies: dependencies:
cookie-es: 1.0.0 cookie-es: 1.0.0
defu: 6.1.2 defu: 6.1.2
destr: 1.2.2 destr: 2.0.0
iron-webcrypto: 0.7.0 iron-webcrypto: 0.7.0
radix3: 1.0.1 radix3: 1.0.1
ufo: 1.1.2 ufo: 1.1.2
@ -6548,7 +6548,7 @@ packages:
fs-extra: 11.1.1 fs-extra: 11.1.1
globby: 13.2.0 globby: 13.2.0
gzip-size: 7.0.0 gzip-size: 7.0.0
h3: 1.6.6 h3: 1.7.0
hookable: 5.5.3 hookable: 5.5.3
http-proxy: 1.18.1 http-proxy: 1.18.1
is-primitive: 3.0.1 is-primitive: 3.0.1
@ -8606,7 +8606,7 @@ packages:
anymatch: 3.1.3 anymatch: 3.1.3
chokidar: 3.5.3 chokidar: 3.5.3
destr: 1.2.2 destr: 1.2.2
h3: 1.6.6 h3: 1.7.0
ioredis: 5.3.2 ioredis: 5.3.2
listhen: 1.0.4 listhen: 1.0.4
lru-cache: 9.1.1 lru-cache: 9.1.1

View File

@ -35,10 +35,10 @@ describe.skipIf(process.env.SKIP_BUNDLE_SIZE === 'true' || process.env.ECOSYSTEM
it('default server bundle size', async () => { it('default server bundle size', async () => {
stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir) stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir)
expect.soft(roundToKilobytes(stats.server.totalBytes)).toMatchInlineSnapshot('"62.1k"') expect.soft(roundToKilobytes(stats.server.totalBytes)).toMatchInlineSnapshot('"62.2k"')
const modules = await analyzeSizes('node_modules/**/*', serverDir) const modules = await analyzeSizes('node_modules/**/*', serverDir)
expect.soft(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2295k"') expect.soft(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2296k"')
const packages = modules.files const packages = modules.files
.filter(m => m.endsWith('package.json')) .filter(m => m.endsWith('package.json'))