Skip to content

Commit

Permalink
fix(cloudflare): normalize incoming & outgoing headers (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored May 2, 2022
1 parent 833d411 commit abda66b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/runtime/entries/aws-lambda.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { APIGatewayProxyEvent, APIGatewayProxyEventHeaders, APIGatewayProxyEventV2, APIGatewayProxyResult, APIGatewayProxyResultV2, Context } from 'aws-lambda'
import '#internal/nitro/virtual/polyfill'
import { withQuery } from 'ufo'
import type { HeadersObject } from 'unenv/runtime/_internal/types'
import { nitroApp } from '../app'

// Compatibility types that work with AWS v1, AWS v2 & Netlify
Expand Down Expand Up @@ -37,6 +36,6 @@ function normalizeIncomingHeaders (headers: APIGatewayProxyEventHeaders) {
return Object.fromEntries(Object.entries(headers).map(([key, value]) => [key.toLowerCase(), value!]))
}

function normalizeOutgoingHeaders (headers: HeadersObject) {
function normalizeOutgoingHeaders (headers: Record<string, string | string[] | undefined>) {
return Object.fromEntries(Object.entries(headers).map(([k, v]) => [k, Array.isArray(v) ? v.join(',') : v!]))
}
11 changes: 7 additions & 4 deletions src/runtime/entries/cloudflare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ addEventListener('fetch', (event: any) => {
event.respondWith(handleEvent(event))
})

async function handleEvent (event) {
async function handleEvent (event: FetchEvent) {
try {
return await getAssetFromKV(event, { cacheControl: assetsCacheControl, mapRequestToAsset: baseURLModifier })
} catch (_err) {
Expand All @@ -27,15 +27,14 @@ async function handleEvent (event) {
url: url.pathname + url.search,
host: url.hostname,
protocol: url.protocol,
headers: event.request.headers,
headers: Object.fromEntries(event.request.headers.entries()),
method: event.request.method,
redirect: event.request.redirect,
body
})

return new Response(r.body, {
// @ts-ignore
headers: r.headers,
headers: normalizeOutgoingHeaders(r.headers),
status: r.status,
statusText: r.statusText
})
Expand All @@ -56,3 +55,7 @@ const baseURLModifier = (request: Request) => {
const url = withoutBase(request.url, useRuntimeConfig().app.baseURL)
return mapRequestToAsset(new Request(url, request))
}

function normalizeOutgoingHeaders (headers: Record<string, string | string[] | undefined>) {
return Object.entries(headers).map(([k, v]) => [k, Array.isArray(v) ? v.join(',') : v])
}
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"lib": [
"WebWorker"
],
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Node",
Expand Down

0 comments on commit abda66b

Please sign in to comment.