mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-29 09:02:03 +00:00
feat(nitro): handle request body in workers (#537)
This commit is contained in:
parent
3e0e24f80d
commit
2b63578a84
@ -1,6 +1,7 @@
|
|||||||
import '#polyfill'
|
import '#polyfill'
|
||||||
import { getAssetFromKV } from '@cloudflare/kv-asset-handler'
|
import { getAssetFromKV } from '@cloudflare/kv-asset-handler'
|
||||||
import { localCall } from '../server'
|
import { localCall } from '../server'
|
||||||
|
import { requestHasBody, useRequestBody } from '../server/utils'
|
||||||
|
|
||||||
const PUBLIC_PATH = process.env.PUBLIC_PATH // Default: /_nuxt/
|
const PUBLIC_PATH = process.env.PUBLIC_PATH // Default: /_nuxt/
|
||||||
|
|
||||||
@ -17,6 +18,10 @@ async function handleEvent (event) {
|
|||||||
|
|
||||||
const url = new URL(event.request.url)
|
const url = new URL(event.request.url)
|
||||||
|
|
||||||
|
if (requestHasBody(event.request)) {
|
||||||
|
event.request.body = await useRequestBody(event.request)
|
||||||
|
}
|
||||||
|
|
||||||
const r = await localCall({
|
const r = await localCall({
|
||||||
event,
|
event,
|
||||||
url: url.pathname + url.search,
|
url: url.pathname + url.search,
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
// @ts-nocheck
|
// @ts-nocheck
|
||||||
import '#polyfill'
|
import '#polyfill'
|
||||||
import { localCall } from '../server'
|
import { localCall } from '../server'
|
||||||
|
import { requestHasBody, useRequestBody } from '../server/utils'
|
||||||
|
|
||||||
const STATIC_ASSETS_BASE = process.env.NUXT_STATIC_BASE + '/' + process.env.NUXT_STATIC_VERSION
|
const STATIC_ASSETS_BASE = process.env.NUXT_STATIC_BASE + '/' + process.env.NUXT_STATIC_VERSION
|
||||||
const METHODS_WITH_BODY = ['POST', 'PUT', 'PATCH']
|
|
||||||
|
|
||||||
addEventListener('fetch', (event: any) => {
|
addEventListener('fetch', (event: any) => {
|
||||||
const url = new URL(event.request.url)
|
const url = new URL(event.request.url)
|
||||||
@ -16,9 +16,10 @@ addEventListener('fetch', (event: any) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
async function handleEvent (url, event) {
|
async function handleEvent (url, event) {
|
||||||
if (METHODS_WITH_BODY.includes(event.request.method.toUpperCase()) && !event.request.body) {
|
if (requestHasBody(event.request)) {
|
||||||
event.request.body = await event.request.text()
|
event.request.body = await useRequestBody(event.request)
|
||||||
}
|
}
|
||||||
|
|
||||||
const r = await localCall({
|
const r = await localCall({
|
||||||
event,
|
event,
|
||||||
url: url.pathname + url.search,
|
url: url.pathname + url.search,
|
||||||
|
26
packages/nitro/src/runtime/server/utils.ts
Normal file
26
packages/nitro/src/runtime/server/utils.ts
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
const METHOD_WITH_BODY_RE = /post|put|patch/i
|
||||||
|
const TEXT_MIME_RE = /application\/text|text\/html/
|
||||||
|
const JSON_MIME_RE = /application\/json/
|
||||||
|
|
||||||
|
export function requestHasBody (request: globalThis.Request) : boolean {
|
||||||
|
return METHOD_WITH_BODY_RE.test(request.method)
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function useRequestBody (request: globalThis.Request): Promise<any> {
|
||||||
|
const contentType = request.headers.get('content-type') || ''
|
||||||
|
if (contentType.includes('form')) {
|
||||||
|
const formData = await request.formData()
|
||||||
|
const body = Object.create(null)
|
||||||
|
for (const entry of formData.entries()) {
|
||||||
|
body[entry[0]] = entry[1]
|
||||||
|
}
|
||||||
|
return body
|
||||||
|
} else if (JSON_MIME_RE.test(contentType)) {
|
||||||
|
return request.json()
|
||||||
|
} else if (TEXT_MIME_RE.test(contentType)) {
|
||||||
|
return request.text()
|
||||||
|
} else {
|
||||||
|
const blob = await request.blob()
|
||||||
|
return URL.createObjectURL(blob)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user