Skip to content

Commit 737028e

Browse files
committed
feat: getPlatformProxy
1 parent c974947 commit 737028e

File tree

7 files changed

+89
-20
lines changed

7 files changed

+89
-20
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
"h3": "^1.15.4",
7575
"mime": "^4.1.0",
7676
"nitro-cloudflare-dev": "^0.2.2",
77-
"nitropack": "link:../../../Library/pnpm/global/5/node_modules/nitropack",
7877
"nypm": "^0.6.2",
7978
"obuild": "^0.2.1",
8079
"ofetch": "^1.4.1",

pnpm-lock.yaml

Lines changed: 18 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/features/ai.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,19 @@ export async function setupAI(nuxt: Nuxt, hub: HubConfig, deps: Record<string, s
3030
const isCloudflareRuntime = nuxt.options.nitro.preset?.includes('cloudflare')
3131
const isAiBindingSet = !!(process.env.AI as { runtime: string } | undefined)?.runtime
3232

33-
if (isCloudflareRuntime && !isAiBindingSet) {
33+
if (nuxt.options.nitro.preset === 'cloudflare-dev') {
34+
// Fall back to env vars if binding isn't set
35+
if (!isAiBindingSet) {
36+
if (!process.env.CLOUDFLARE_ACCOUNT_ID || !process.env.CLOUDFLARE_API_KEY) {
37+
return logWhenReady(nuxt, `Set \`CLOUDFLARE_ACCOUNT_ID\` and \`CLOUDFLARE_API_KEY\` environment variables to enable \`hubAI()\` with ${providerName}`, 'error')
38+
}
39+
log.info('`hubAI()` configured with Cloudflare AI via environment variables during local development')
40+
} else {
41+
log.info('`hubAI()` configured with Cloudflare AI binding during local development')
42+
}
43+
} else if (isCloudflareRuntime && !isAiBindingSet) {
3444
return logWhenReady(nuxt, 'Ensure a `AI` binding is set in your Cloudflare Workers configuration', 'error')
35-
}
36-
37-
if (!process.env.CLOUDFLARE_ACCOUNT_ID || !process.env.CLOUDFLARE_API_KEY) {
45+
} else if (!isCloudflareRuntime && (!process.env.CLOUDFLARE_ACCOUNT_ID || !process.env.CLOUDFLARE_API_KEY)) {
3846
return logWhenReady(nuxt, `Set \`CLOUDFLARE_ACCOUNT_ID\` and \`CLOUDFLARE_API_KEY\` environment variables to enable \`hubAI()\` with ${providerName}`, 'error')
3947
}
4048
} else if (hub.ai === 'vercel') {

src/features/blob.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,25 @@ export function setupBlob(nuxt: Nuxt, hub: HubConfig, _deps: Record<string, stri
1515
nuxt.options.nitro.devStorage ||= {}
1616

1717
if (!nuxt.options.nitro.devStorage.blob) {
18-
nuxt.options.nitro.devStorage.blob = {
19-
driver: 'fs-lite',
20-
base: join(hub.dir!, 'blob')
18+
let devBlobConfig: NitroOptions['storage']['default']
19+
20+
// Cloudflare Dev
21+
if (nuxt.options.nitro.preset === 'cloudflare-dev') {
22+
log.info('`hubBlob()` configured with Cloudflare R2 during local development')
23+
devBlobConfig = {
24+
driver: 'cloudflare-r2-binding',
25+
bindingName: 'BLOB'
26+
}
27+
28+
// All other presets
29+
} else {
30+
devBlobConfig = {
31+
driver: 'fs-lite',
32+
base: join(hub.dir!, 'blob')
33+
}
2134
}
35+
36+
nuxt.options.nitro.devStorage.blob = devBlobConfig
2237
}
2338

2439
// Add Server scanning

src/features/cache.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,25 @@ const log = logger.withTag('nuxt:hub')
1515
export async function setupCache(nuxt: Nuxt, hub: HubConfig, _deps: Record<string, string>) {
1616
// Configure dev storage
1717
nuxt.options.nitro.devStorage ||= {}
18-
nuxt.options.nitro.devStorage.cache = defu(nuxt.options.nitro.devStorage.cache, {
19-
driver: 'fs-lite',
20-
base: join(hub.dir!, 'cache')
21-
})
18+
19+
let devCacheConfig: NitroOptions['storage']['default']
20+
21+
// Cloudflare Dev
22+
if (nuxt.options.nitro.preset === 'cloudflare-dev') {
23+
devCacheConfig = {
24+
driver: 'cloudflare-kv-binding',
25+
bindingName: 'CACHE'
26+
}
27+
28+
// All other presets
29+
} else {
30+
devCacheConfig = {
31+
driver: 'fs-lite',
32+
base: join(hub.dir!, 'cache')
33+
}
34+
}
35+
36+
nuxt.options.nitro.devStorage.cache = defu(nuxt.options.nitro.devStorage.cache, devCacheConfig)
2237

2338
// Add Server scanning
2439
addServerScanDir(resolve('runtime/cache/server'))

src/features/kv.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,26 @@ const log = logger.withTag('nuxt:hub')
1313
export function setupKV(nuxt: Nuxt, hub: HubConfig, _deps: Record<string, string>) {
1414
// Configure dev storage
1515
nuxt.options.nitro.devStorage ||= {}
16-
nuxt.options.nitro.devStorage.kv = defu(nuxt.options.nitro.devStorage.kv, {
17-
driver: 'fs',
18-
base: join(hub.dir!, 'kv')
19-
})
16+
17+
let devKVConfig: NitroOptions['storage']['default']
18+
19+
// Cloudflare Dev
20+
if (nuxt.options.nitro.preset === 'cloudflare-dev') {
21+
log.info('`hubKV()` configured with Cloudflare KV during local development')
22+
devKVConfig = {
23+
driver: 'cloudflare-kv-binding',
24+
bindingName: 'KV'
25+
}
26+
27+
// All other presets
28+
} else {
29+
devKVConfig = {
30+
driver: 'fs',
31+
base: join(hub.dir!, 'kv')
32+
}
33+
}
34+
35+
nuxt.options.nitro.devStorage.kv = defu(nuxt.options.nitro.devStorage.kv, devKVConfig)
2036

2137
// Add Server scanning
2238
addServerScanDir(resolve('runtime/kv/server'))

src/runtime/ai/server/utils/ai.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createWorkersAI, type WorkersAI } from 'workers-ai-provider'
44
import { useRuntimeConfig } from '#imports'
55
import { requireNuxtHubFeature } from '../../../utils/features'
66
import type { NuxtHubAIProvider } from '#build/types/nuxthub-ai'
7+
import type { Ai } from '@cloudflare/workers-types'
78

89
let _ai: WorkersAI | GatewayProvider
910

@@ -43,7 +44,7 @@ export function hubAI<T extends NuxtHubAI = NuxtHubAI>(model: Parameters<HubAIPr
4344
}
4445

4546
if (hub.ai === 'cloudflare') {
46-
const isAiBindingSet = !!(process.env.AI as { runtime: string } | undefined)?.runtime
47+
const isAiBindingSet = !!(process.env.AI as Ai | undefined)?.models
4748
_ai = createWorkersAI(isAiBindingSet
4849
? { binding: 'AI' }
4950
: {

0 commit comments

Comments
 (0)