Skip to content

Commit 7728586

Browse files
committed
chore: up
1 parent ef38aa0 commit 7728586

File tree

14 files changed

+139
-84
lines changed

14 files changed

+139
-84
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@ dist
1111
.netlify
1212
db.sqlite-*
1313
.hub
14-
.wrangler/state/v3

_nuxthub/modules/hub.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

_nuxthub/modules/hub/index.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { defineNuxtModule, createResolver } from 'nuxt/kit'
2+
import { join } from 'pathe'
3+
import { defu } from 'defu'
4+
import { mkdir, writeFile, readFile } from 'node:fs/promises'
5+
6+
export default defineNuxtModule({
7+
meta: {
8+
name: 'hub'
9+
},
10+
async setup (_options, nuxt) {
11+
const { resolve } = createResolver(import.meta.url)
12+
13+
// Add Server utils based on environment
14+
// nuxt.options.nitro.imports = nuxt.options.nitro.imports || {}
15+
// nuxt.options.nitro.imports.dirs = nuxt.options.nitro.imports.dirs || []
16+
// nuxt.options.nitro.imports.dirs.push(resolve(`../server/_utils/${nuxt.options.dev ? 'dev' : 'prod'}/`))
17+
18+
// Production mode
19+
if (!nuxt.options.dev) {
20+
return
21+
}
22+
23+
if (process.env.NUXT_HUB_URL) {
24+
// TODO: check on hub.nuxt.com if the project is connected
25+
// return
26+
// return once we support Proxy for all providers (R2 missing now)
27+
}
28+
29+
// Local development without remote connection
30+
// Create the .hub/ directory
31+
const hubDir = join(nuxt.options.rootDir, './.hub')
32+
try {
33+
await mkdir(hubDir)
34+
} catch (e: any) {
35+
if (e.errno === -17) {
36+
// File already exists
37+
} else {
38+
throw e
39+
}
40+
}
41+
// Add it to .gitignore
42+
const gitignorePath = join(nuxt.options.rootDir, './.gitignore')
43+
const gitignore = await readFile(gitignorePath, 'utf-8')
44+
if (!gitignore.includes('.hub')) {
45+
await writeFile(gitignorePath, gitignore + '\n.hub', 'utf-8')
46+
}
47+
48+
// Generate the wrangler.toml file
49+
const wranglerPath = join(hubDir, './wrangler.toml')
50+
await writeFile(wranglerPath, DEFAULT_WRANGLER, 'utf-8')
51+
nuxt.options.runtimeConfig.wrangler = defu(nuxt.options.runtimeConfig.wrangler, {
52+
configPath: wranglerPath,
53+
persistDir: hubDir
54+
})
55+
// Make sure runtime is transpiled
56+
// nuxt.options.nitro.externals.inline = nuxt.options.nitro.externals.inline || []
57+
// nuxt.options.nitro.externals.inline.push(resolve('./runtime/server'))
58+
// Add server plugin
59+
nuxt.options.nitro.plugins = nuxt.options.nitro.plugins || []
60+
nuxt.options.nitro.plugins.push(resolve('./runtime/server/plugins/cloudflare.dev'))
61+
}
62+
})
63+
64+
const DEFAULT_WRANGLER = `d1_databases = [
65+
{ binding = "DB", database_name = "default", database_id = "default" },
66+
]
67+
kv_namespaces = [
68+
{ binding = "KV", id = "default" },
69+
]
70+
r2_buckets = [
71+
{ binding = "BUCKET", bucket_name = "default" },
72+
]`
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import type { NitroAppPlugin } from 'nitropack'
2+
// @ts-ignore
3+
import { useRuntimeConfig } from '#imports'
4+
5+
export default <NitroAppPlugin>function (nitroApp) {
6+
let _proxy: ReturnType<typeof getBindingsProxy>
7+
8+
nitroApp.hooks.hook('request', async (event) => {
9+
// Lazy initialize proxy when first request comes in
10+
if (!_proxy) {
11+
_proxy = getBindingsProxy().catch((error) => {
12+
console.error('Failed to initialize wrangler bindings proxy', error)
13+
return { bindings: {}, dispose: () => Promise.resolve() }
14+
})
15+
}
16+
17+
// Inject proxy bindings to the request context
18+
const bindings = (await _proxy).bindings
19+
Object.keys(bindings).forEach((key) => {
20+
if (!globalThis[key]) {
21+
globalThis[key] = bindings[key]
22+
}
23+
})
24+
event.context.cloudflare = {
25+
...event.context.cloudflare,
26+
env: bindings,
27+
}
28+
})
29+
30+
// Dispose proxy when Nitro is closed
31+
nitroApp.hooks.hook('close', () => {
32+
return _proxy?.then((proxy) => proxy.dispose)
33+
})
34+
}
35+
36+
async function getBindingsProxy() {
37+
const _pkg = 'wrangler' // Bypass bundling!
38+
const { getBindingsProxy } = (await import(
39+
_pkg
40+
)) as typeof import('wrangler')
41+
42+
const runtimeConfig: {
43+
wrangler: { configPath: string; persistDir: string };
44+
} = useRuntimeConfig()
45+
46+
const proxy = await getBindingsProxy({
47+
configPath: runtimeConfig.wrangler.configPath,
48+
persist: { path: runtimeConfig.wrangler.persistDir },
49+
})
50+
51+
return proxy
52+
}

_nuxthub/nuxt.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export default defineNuxtConfig({
22
// ./modules are autoloaded
3-
modules: ['nitro-cloudflare-dev']
43
})

_nuxthub/server/utils/bucket.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
1-
import type { H3Event } from 'h3'
21
import type { R2Bucket } from '@cloudflare/workers-types/experimental'
32

43
const _buckets: Record<string, R2Bucket> = {}
54

6-
export function useBucket (event: H3Event, name: string = '') {
5+
export function useBucket (name: string = '') {
76
const bucketName = name ? `BUCKET_${name.toUpperCase()}` : 'BUCKET'
87
if (_buckets[bucketName]) {
98
return _buckets[bucketName]
109
}
1110

1211
if (process.env.NUXT_HUB_URL) {
13-
console.log('Using local bucket as proxy for useBucket() is not yet supported')
12+
console.log('Using R2 local (proxy for useBucket() is not yet supported)')
1413
}
15-
16-
// TODO: move to globalThis.__env__ or process.env
17-
const cfEnv = event.context.cloudflare?.env
18-
if (!cfEnv) {
19-
console.log(event.context.cloudflare)
20-
throw createError('Missing Cloudflare env')
21-
}
22-
if (!cfEnv[bucketName]) {
14+
// @ts-ignore
15+
const binding = globalThis[bucketName]
16+
if (!binding) {
2317
throw createError(`Missing Cloudflare R2 binding ${bucketName}`)
2418
}
25-
_buckets[bucketName] = cfEnv[bucketName] as R2Bucket
19+
_buckets[bucketName] = binding as R2Bucket
2620

2721
return _buckets[bucketName]
2822
}

_nuxthub/server/utils/database.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function useDatabase () {
4444
})
4545
} else if (import.meta.dev) {
4646
// local sqlite in development
47-
console.log('Using local database...')
47+
console.log('Using D1 local database...')
4848
_client = new Database(join(process.cwd(), './.hub/db.sqlite'))
4949
_db = drizzle(_client)
5050
} else {

_nuxthub/server/utils/kv.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function useKV (prefix?: string) {
1818
})
1919
})
2020
} else if (import.meta.dev && process.env.NUXT_HUB_URL) {
21-
console.log('Using remote KV...')
21+
console.log('Using remote KV namespace...')
2222
// Use https://unstorage.unjs.io/drivers/http
2323
_kv = createStorage({
2424
driver: httpDriver({
@@ -30,7 +30,7 @@ export function useKV (prefix?: string) {
3030
})
3131
} else if (import.meta.dev) {
3232
// local kv in development
33-
console.log('Using local KV...')
33+
console.log('Using local KV namespace...')
3434
_kv = createStorage({
3535
driver: fsDriver({ base: join(process.cwd(), './.hub/kv') })
3636
})

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"drizzle-kit": "^0.20.13",
2121
"drizzle-orm": "^0.29.3",
2222
"h3-zod": "^0.5.3",
23-
"nitro-cloudflare-dev": "^0.0.7",
2423
"nuxt": "^3.9.3",
2524
"nuxt-auth-utils": "^0.0.14",
2625
"utf-8-validate": "^6.0.3",

pages/storage.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const items = [[{
115115
:key="index"
116116
class="flex items-center gap-4 py-2"
117117
>
118-
<span class="flex-1 font-medium">{{ file }}</span>
118+
<span class="flex-1 font-medium">{{ file.key }}</span>
119119
</li>
120120
</ul>
121121
</UCard>

0 commit comments

Comments
 (0)