Skip to content

Commit dcd1e38

Browse files
committed
chore: up
1 parent 3cfc682 commit dcd1e38

File tree

13 files changed

+90
-607
lines changed

13 files changed

+90
-607
lines changed
Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,23 @@
1-
import type { NitroAppPlugin } from 'nitropack'
21
// @ts-ignore
3-
import { useRuntimeConfig } from '#imports'
2+
import { defineNitroPlugin, useRuntimeConfig } from '#imports'
43

5-
export default <NitroAppPlugin>function (nitroApp) {
6-
let _proxy: ReturnType<typeof getBindingsProxy>
4+
export default defineNitroPlugin(async (nitroApp) => {
5+
const proxyPromise = getBindingsProxy()
76

87
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-
8+
const proxy = await proxyPromise
179
// 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-
})
2410
event.context.cloudflare = {
2511
...event.context.cloudflare,
26-
env: bindings,
12+
env: proxy.bindings,
2713
}
2814
})
2915

3016
// Dispose proxy when Nitro is closed
3117
nitroApp.hooks.hook('close', () => {
32-
return _proxy?.then((proxy) => proxy.dispose)
18+
return proxyPromise.then((proxy) => proxy.dispose())
3319
})
34-
}
20+
})
3521

3622
async function getBindingsProxy() {
3723
const _pkg = 'wrangler' // Bypass bundling!
@@ -48,5 +34,14 @@ async function getBindingsProxy() {
4834
persist: { path: runtimeConfig.wrangler.persistDir },
4935
})
5036

37+
Object.keys(proxy.bindings).forEach((key) => {
38+
if (!globalThis[key]) {
39+
globalThis[key] = proxy.bindings[key]
40+
}
41+
})
42+
43+
// @ts-ignore
44+
await hubHooks.callHook('bindings:ready')
45+
5146
return proxy
52-
}
47+
}

_nuxthub/server/api/_hub/database/query.post.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,6 @@ export default eventHandler(async (event) => {
88
// Make sure the client is initialized first
99
const client = useDatabaseClient()
1010

11-
if (process.dev) {
12-
try {
13-
if (method === 'run')
14-
return client.prepare(sql).run(params)
15-
if (method === 'all' || method === 'values')
16-
return client.prepare(sql).raw().all(params)
17-
// method = 'get'
18-
return client.prepare(sql).raw().get(params)
19-
} catch (e: any) {
20-
throw createError({
21-
statusCode: 500,
22-
message: `Database error: ${e.message}`
23-
})
24-
}
25-
}
26-
27-
// await verifyHubSecretKey(event)
28-
// TODO: Validate header for authorization through hub.nuxt.com
29-
// const [, secretKey] = (getHeader(event, 'authorization') || '').split(' ')
30-
3111
try {
3212
if (method === 'run') {
3313
const { meta } = await client.prepare(sql).bind(...params).run()

_nuxthub/server/utils/blob.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ export function useBlob () {
3030
const proxyURL = import.meta.dev && process.env.NUXT_HUB_URL
3131
let bucket: R2Bucket
3232
if (!proxyURL) {
33+
import.meta.dev && console.log('Using Blob local storage...')
3334
bucket = _useBucket()
35+
} else {
36+
import.meta.dev && console.log('Using Blob remote storage...')
3437
}
3538

3639
return {

_nuxthub/server/utils/database.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { D1Database } from '@cloudflare/workers-types/experimental'
12
import { drizzle as drizzleD1, DrizzleD1Database } from 'drizzle-orm/d1'
23
import { drizzle } from 'drizzle-orm/better-sqlite3'
34
import type { BetterSQLite3Database } from 'drizzle-orm/better-sqlite3'
@@ -10,16 +11,12 @@ import { join } from 'pathe'
1011

1112
export * as tables from '~/server/database/schema'
1213

13-
let _db: DrizzleD1Database | BetterSQLite3Database | SqliteRemoteDatabase | null = null
14-
let _client: any = null
14+
let _db: DrizzleD1Database | BetterSQLite3Database | SqliteRemoteDatabase
15+
let _client: D1Database
1516

1617
export function useDatabase () {
1718
if (!_db) {
18-
if (process.env.DB) {
19-
// d1 in production
20-
_client = process.env.DB
21-
_db = drizzleD1(_client)
22-
} else if (import.meta.dev && process.env.NUXT_HUB_URL) {
19+
if (import.meta.dev && process.env.NUXT_HUB_URL) {
2320
console.log('Using D1 remote database...')
2421
_db = drizzleHTTP(async (sql, params, method) => {
2522
// https://orm.drizzle.team/docs/get-started-sqlite#http-proxy
@@ -42,13 +39,15 @@ export function useDatabase () {
4239
return { rows: [] }
4340
}
4441
})
45-
} else if (import.meta.dev) {
46-
// local sqlite in development
47-
console.log('Using D1 local database...')
48-
_client = new Database(join(process.cwd(), './.hub/db.sqlite'))
49-
_db = drizzle(_client)
5042
} else {
51-
throw new Error('No database configured for production')
43+
const binding = process.env.DB || globalThis.__env__?.DB || globalThis.DB
44+
if (binding) {
45+
_client = binding as D1Database
46+
_db = drizzleD1(_client)
47+
import.meta.dev && console.log('Using D1 local database...')
48+
} else {
49+
throw createError('Missing Cloudflare D1 binding DB')
50+
}
5251
}
5352
}
5453
return _db

_nuxthub/server/utils/hooks.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { createHooks } from 'hookable'
2+
3+
export interface HubHooks {
4+
'bindings:ready': () => any | void
5+
}
6+
7+
export const hubHooks = createHooks<HubHooks>()
8+
9+
export function onHubReady (cb: HubHooks['bindings:ready']) {
10+
if (import.meta.dev) {
11+
return hubHooks.hookOnce('bindings:ready', cb)
12+
}
13+
cb()
14+
}

_nuxthub/server/utils/kv.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ let _kv: Storage
1010

1111
export function useKV () {
1212
if (!_kv) {
13+
// TODO: same as database
1314
if (process.env.KV) {
1415
// kv in production
1516
_kv = createStorage({
@@ -18,7 +19,7 @@ export function useKV () {
1819
})
1920
})
2021
} else if (import.meta.dev && process.env.NUXT_HUB_URL) {
21-
console.log('Using remote KV namespace...')
22+
console.log('Using KV remote namespace...')
2223
// Use https://unstorage.unjs.io/drivers/http
2324
_kv = createStorage({
2425
driver: httpDriver({
@@ -30,7 +31,7 @@ export function useKV () {
3031
})
3132
} else if (import.meta.dev) {
3233
// local kv in development
33-
console.log('Using local KV namespace...')
34+
console.log('Using KV local namespace...')
3435
_kv = createStorage({
3536
driver: fsDriver({ base: join(process.cwd(), './.hub/kv') })
3637
})

drizzle.config.ts

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

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"dev": "nuxi dev",
77
"build": "nuxi build --preset cloudflare_pages",
88
"preview": "nuxt preview",
9-
"migrate": "drizzle-kit generate:sqlite",
109
"lint": "eslint .",
1110
"postinstall": "nuxt prepare",
1211
"logs": "wrangler pages deployment tail"
@@ -16,16 +15,14 @@
1615
"@iconify-json/simple-icons": "^1.1.90",
1716
"@libsql/client": "^0.4.3",
1817
"@nuxt/ui": "^2.13.0",
19-
"better-sqlite3": "^9.3.0",
2018
"bufferutil": "^4.0.8",
21-
"drizzle-kit": "^0.20.13",
2219
"drizzle-orm": "^0.29.3",
2320
"image-meta": "^0.2.0",
2421
"mime": "^4.0.1",
2522
"nuxt": "^3.10.0",
2623
"nuxt-auth-utils": "^0.0.15",
2724
"utf-8-validate": "^6.0.3",
28-
"wrangler": "^3.25.0",
25+
"wrangler": "^3.26.0",
2926
"zod": "^3.22.4"
3027
},
3128
"devDependencies": {

0 commit comments

Comments
 (0)