-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
139 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,3 @@ dist | |
.netlify | ||
db.sqlite-* | ||
.hub | ||
.wrangler/state/v3 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { defineNuxtModule, createResolver } from 'nuxt/kit' | ||
import { join } from 'pathe' | ||
import { defu } from 'defu' | ||
import { mkdir, writeFile, readFile } from 'node:fs/promises' | ||
|
||
export default defineNuxtModule({ | ||
meta: { | ||
name: 'hub' | ||
}, | ||
async setup (_options, nuxt) { | ||
const { resolve } = createResolver(import.meta.url) | ||
|
||
// Add Server utils based on environment | ||
// nuxt.options.nitro.imports = nuxt.options.nitro.imports || {} | ||
// nuxt.options.nitro.imports.dirs = nuxt.options.nitro.imports.dirs || [] | ||
// nuxt.options.nitro.imports.dirs.push(resolve(`../server/_utils/${nuxt.options.dev ? 'dev' : 'prod'}/`)) | ||
|
||
// Production mode | ||
if (!nuxt.options.dev) { | ||
return | ||
} | ||
|
||
if (process.env.NUXT_HUB_URL) { | ||
// TODO: check on hub.nuxt.com if the project is connected | ||
// return | ||
// return once we support Proxy for all providers (R2 missing now) | ||
} | ||
|
||
// Local development without remote connection | ||
// Create the .hub/ directory | ||
const hubDir = join(nuxt.options.rootDir, './.hub') | ||
try { | ||
await mkdir(hubDir) | ||
} catch (e: any) { | ||
if (e.errno === -17) { | ||
// File already exists | ||
} else { | ||
throw e | ||
} | ||
} | ||
// Add it to .gitignore | ||
const gitignorePath = join(nuxt.options.rootDir, './.gitignore') | ||
const gitignore = await readFile(gitignorePath, 'utf-8') | ||
if (!gitignore.includes('.hub')) { | ||
await writeFile(gitignorePath, gitignore + '\n.hub', 'utf-8') | ||
} | ||
|
||
// Generate the wrangler.toml file | ||
const wranglerPath = join(hubDir, './wrangler.toml') | ||
await writeFile(wranglerPath, DEFAULT_WRANGLER, 'utf-8') | ||
nuxt.options.runtimeConfig.wrangler = defu(nuxt.options.runtimeConfig.wrangler, { | ||
configPath: wranglerPath, | ||
persistDir: hubDir | ||
}) | ||
// Make sure runtime is transpiled | ||
// nuxt.options.nitro.externals.inline = nuxt.options.nitro.externals.inline || [] | ||
// nuxt.options.nitro.externals.inline.push(resolve('./runtime/server')) | ||
// Add server plugin | ||
nuxt.options.nitro.plugins = nuxt.options.nitro.plugins || [] | ||
nuxt.options.nitro.plugins.push(resolve('./runtime/server/plugins/cloudflare.dev')) | ||
} | ||
}) | ||
|
||
const DEFAULT_WRANGLER = `d1_databases = [ | ||
{ binding = "DB", database_name = "default", database_id = "default" }, | ||
] | ||
kv_namespaces = [ | ||
{ binding = "KV", id = "default" }, | ||
] | ||
r2_buckets = [ | ||
{ binding = "BUCKET", bucket_name = "default" }, | ||
]` |
52 changes: 52 additions & 0 deletions
52
_nuxthub/modules/hub/runtime/server/plugins/cloudflare.dev.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { NitroAppPlugin } from 'nitropack' | ||
// @ts-ignore | ||
import { useRuntimeConfig } from '#imports' | ||
|
||
export default <NitroAppPlugin>function (nitroApp) { | ||
let _proxy: ReturnType<typeof getBindingsProxy> | ||
|
||
nitroApp.hooks.hook('request', async (event) => { | ||
// Lazy initialize proxy when first request comes in | ||
if (!_proxy) { | ||
_proxy = getBindingsProxy().catch((error) => { | ||
console.error('Failed to initialize wrangler bindings proxy', error) | ||
return { bindings: {}, dispose: () => Promise.resolve() } | ||
}) | ||
} | ||
|
||
// Inject proxy bindings to the request context | ||
const bindings = (await _proxy).bindings | ||
Object.keys(bindings).forEach((key) => { | ||
if (!globalThis[key]) { | ||
globalThis[key] = bindings[key] | ||
} | ||
}) | ||
event.context.cloudflare = { | ||
...event.context.cloudflare, | ||
env: bindings, | ||
} | ||
}) | ||
|
||
// Dispose proxy when Nitro is closed | ||
nitroApp.hooks.hook('close', () => { | ||
return _proxy?.then((proxy) => proxy.dispose) | ||
}) | ||
} | ||
|
||
async function getBindingsProxy() { | ||
const _pkg = 'wrangler' // Bypass bundling! | ||
const { getBindingsProxy } = (await import( | ||
_pkg | ||
)) as typeof import('wrangler') | ||
|
||
const runtimeConfig: { | ||
wrangler: { configPath: string; persistDir: string }; | ||
} = useRuntimeConfig() | ||
|
||
const proxy = await getBindingsProxy({ | ||
configPath: runtimeConfig.wrangler.configPath, | ||
persist: { path: runtimeConfig.wrangler.persistDir }, | ||
}) | ||
|
||
return proxy | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export default defineNuxtConfig({ | ||
// ./modules are autoloaded | ||
modules: ['nitro-cloudflare-dev'] | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,22 @@ | ||
import type { H3Event } from 'h3' | ||
import type { R2Bucket } from '@cloudflare/workers-types/experimental' | ||
|
||
const _buckets: Record<string, R2Bucket> = {} | ||
|
||
export function useBucket (event: H3Event, name: string = '') { | ||
export function useBucket (name: string = '') { | ||
const bucketName = name ? `BUCKET_${name.toUpperCase()}` : 'BUCKET' | ||
if (_buckets[bucketName]) { | ||
return _buckets[bucketName] | ||
} | ||
|
||
if (process.env.NUXT_HUB_URL) { | ||
console.log('Using local bucket as proxy for useBucket() is not yet supported') | ||
console.log('Using R2 local (proxy for useBucket() is not yet supported)') | ||
} | ||
|
||
// TODO: move to globalThis.__env__ or process.env | ||
const cfEnv = event.context.cloudflare?.env | ||
if (!cfEnv) { | ||
console.log(event.context.cloudflare) | ||
throw createError('Missing Cloudflare env') | ||
} | ||
if (!cfEnv[bucketName]) { | ||
// @ts-ignore | ||
const binding = globalThis[bucketName] | ||
if (!binding) { | ||
throw createError(`Missing Cloudflare R2 binding ${bucketName}`) | ||
} | ||
_buckets[bucketName] = cfEnv[bucketName] as R2Bucket | ||
_buckets[bucketName] = binding as R2Bucket | ||
|
||
return _buckets[bucketName] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.