Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: extended cloudflare context #677

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
"mlly": "^1.0.0",
"mri": "^1.2.0",
"node-fetch-native": "^1.0.1",
"ohash": "^1.0.0",
"ofetch": "^1.0.0",
"ohash": "^1.0.0",
"pathe": "^1.0.0",
"perfect-debounce": "^0.1.3",
"pkg-types": "^1.0.1",
Expand All @@ -102,6 +102,7 @@
"unstorage": "^1.0.1"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20221111.1",
"@nuxtjs/eslint-config-typescript": "^11.0.0",
"@types/aws-lambda": "^8.10.108",
"@types/etag": "^1.8.1",
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 33 additions & 1 deletion src/runtime/entries/cloudflare.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import '#internal/nitro/virtual/polyfill'
import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler'
import { withoutBase } from 'ufo'
import type { RequestInit } from '@cloudflare/workers-types'
import { createCall } from 'unenv/runtime/fetch/index'
import { NodeListener, App, createEvent, createError, isError, sendError, H3EventContext } from 'h3'
import { requestHasBody } from '../utils'
import { nitroApp } from '../app'
import { useRuntimeConfig } from '#internal/nitro'
Expand All @@ -22,7 +25,11 @@ async function handleEvent (event: FetchEvent) {
body = Buffer.from(await event.request.arrayBuffer())
}

const r = await nitroApp.localCall({
const localCall = createCall(toNodeListener(nitroApp.h3App, {
cf: (event.request as unknown as RequestInit)?.cf
}) as any)

const r = await localCall({
event,
url: url.pathname + url.search,
host: url.hostname,
Expand Down Expand Up @@ -60,3 +67,28 @@ const baseURLModifier = (request: Request) => {
function normalizeOutgoingHeaders (headers: Record<string, string | string[] | undefined>) {
return Object.entries(headers).map(([k, v]) => [k, Array.isArray(v) ? v.join(',') : v])
}

function toNodeListener (app: App, context: H3EventContext): NodeListener {
const toNodeHandle: NodeListener = async function (req, res) {
const event = createEvent(req, res)
event.context.cloudflare = context
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be:

Suggested change
event.context.cloudflare = context
event.context.cloudflare = context.cf

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the answer to this depends on where you'll want the fetch event context to live when module syntax support lands in #681.

try {
await app.handler(event)
} catch (_error: any) {
const error = createError(_error)
if (!isError(_error)) {
error.unhandled = true
}

if (app.options.onError) {
await app.options.onError(error, event)
} else {
if (error.unhandled || error.fatal) {
console.error('[h3]', error.fatal ? '[fatal]' : '[unhandled]', error) // eslint-disable-line no-console
}
await sendError(event, error, !!app.options.debug)
}
}
}
return toNodeHandle
}