From cab53104748df7e61e85dfbc33e4c2cb485eff9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Bora?= Date: Sun, 20 Nov 2022 12:08:50 +0100 Subject: [PATCH 1/4] adding cloudflare context --- package.json | 3 ++- pnpm-lock.yaml | 6 +++++ src/runtime/entries/cloudflare.ts | 37 ++++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 7ad70ca37b..c53a81d304 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2f40f8136f..89d2a78f44 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,6 +8,7 @@ importers: .: specifiers: '@cloudflare/kv-asset-handler': ^0.2.0 + '@cloudflare/workers-types': ^4.20221111.1 '@netlify/functions': ^1.3.0 '@nuxtjs/eslint-config-typescript': ^11.0.0 '@rollup/plugin-alias': ^4.0.2 @@ -146,6 +147,7 @@ importers: unimport: 1.0.1_rollup@2.79.1 unstorage: 1.0.1 devDependencies: + '@cloudflare/workers-types': 4.20221111.1 '@nuxtjs/eslint-config-typescript': 11.0.0_hsf322ms6xhhd4b5ne6lb74y4a '@types/aws-lambda': 8.10.108 '@types/etag': 1.8.1 @@ -432,6 +434,10 @@ packages: mime: 3.0.0 dev: false + /@cloudflare/workers-types/4.20221111.1: + resolution: {integrity: sha512-BNV2wN8V6Zduvo7UzxcdjBbLQ906D2KhS804PDufLgx/sanGJCHVJMOIaLvS/b61JKtot1U7P/l1fjrjZ7/E3A==} + dev: true + /@edge-runtime/format/1.1.0: resolution: {integrity: sha512-MkLDDtPhXZIMx83NykdFmOpF7gVWIdd6GBHYb8V/E+PKWvD2pK/qWx9B30oN1iDJ2XBm0SGDjz02S8nDHI9lMQ==} dev: true diff --git a/src/runtime/entries/cloudflare.ts b/src/runtime/entries/cloudflare.ts index 42c53b9965..53dbd2c3b1 100644 --- a/src/runtime/entries/cloudflare.ts +++ b/src/runtime/entries/cloudflare.ts @@ -1,6 +1,9 @@ import '#internal/nitro/virtual/polyfill' import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler' import { withoutBase } from 'ufo' +import { 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' @@ -22,7 +25,14 @@ async function handleEvent (event: FetchEvent) { body = Buffer.from(await event.request.arrayBuffer()) } - const r = await nitroApp.localCall({ + const headers = Object.fromEntries(event.request.headers.entries()) + + const localCall = createCall(toNodeListener(nitroApp.h3App, { + headers, + cf: (event.request as unknown as RequestInit)?.cf + }) as any) + + const r = await localCall({ event, url: url.pathname + url.search, host: url.hostname, @@ -60,3 +70,28 @@ const baseURLModifier = (request: Request) => { function normalizeOutgoingHeaders (headers: Record) { 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 = { ...event.context, context } + 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 +} From 4e770d2fed18a7a77d10d0462243bf3d80d61a68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Bora?= Date: Sun, 20 Nov 2022 12:13:23 +0100 Subject: [PATCH 2/4] feat: import as type --- src/runtime/entries/cloudflare.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/entries/cloudflare.ts b/src/runtime/entries/cloudflare.ts index 53dbd2c3b1..c83aff3048 100644 --- a/src/runtime/entries/cloudflare.ts +++ b/src/runtime/entries/cloudflare.ts @@ -1,7 +1,7 @@ import '#internal/nitro/virtual/polyfill' import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler' import { withoutBase } from 'ufo' -import { RequestInit } from '@cloudflare/workers-types' +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' From f62e761081841bf7c5af8a6f22d2412bd472603b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Bora?= Date: Sun, 20 Nov 2022 13:24:36 +0100 Subject: [PATCH 3/4] fix: context object spread --- src/runtime/entries/cloudflare.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/runtime/entries/cloudflare.ts b/src/runtime/entries/cloudflare.ts index c83aff3048..5d014be385 100644 --- a/src/runtime/entries/cloudflare.ts +++ b/src/runtime/entries/cloudflare.ts @@ -25,10 +25,7 @@ async function handleEvent (event: FetchEvent) { body = Buffer.from(await event.request.arrayBuffer()) } - const headers = Object.fromEntries(event.request.headers.entries()) - const localCall = createCall(toNodeListener(nitroApp.h3App, { - headers, cf: (event.request as unknown as RequestInit)?.cf }) as any) @@ -74,7 +71,7 @@ function normalizeOutgoingHeaders (headers: Record Date: Fri, 20 Jan 2023 20:08:10 +0100 Subject: [PATCH 4/4] implement feedback --- src/runtime/entries/cloudflare.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/entries/cloudflare.ts b/src/runtime/entries/cloudflare.ts index 5d014be385..fbbb221ca3 100644 --- a/src/runtime/entries/cloudflare.ts +++ b/src/runtime/entries/cloudflare.ts @@ -71,7 +71,7 @@ function normalizeOutgoingHeaders (headers: Record