@harlan-zw/nuxt-cloudflare 0.0.10 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -5
- package/dist/bindings.d.mts +15 -8
- package/dist/bindings.mjs +12 -4
- package/dist/cli/index.mjs +1 -1
- package/dist/deploy.d.mts +1 -2
- package/dist/deploy.mjs +16 -18
- package/dist/module.json +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -184,14 +184,16 @@ D1 allows 100 bound parameters per statement, including each statement inside `d
|
|
|
184
184
|
### Bindings
|
|
185
185
|
|
|
186
186
|
```ts
|
|
187
|
-
import
|
|
188
|
-
import { createCloudflareBindings } from '@harlan-zw/nuxt-cloudflare/bindings'
|
|
187
|
+
import { createCloudflareBindings, resolveCloudflareBindings, runtimeConfigSource } from '@harlan-zw/nuxt-cloudflare/bindings'
|
|
189
188
|
|
|
190
189
|
const cloudflare = createCloudflareBindings()
|
|
191
190
|
|
|
192
|
-
function requireDatabase(source?:
|
|
191
|
+
function requireDatabase(source?: unknown) {
|
|
193
192
|
return cloudflare.require('DB', source)
|
|
194
193
|
}
|
|
194
|
+
|
|
195
|
+
const env = resolveCloudflareBindings(event)
|
|
196
|
+
const config = useRuntimeConfig(runtimeConfigSource(env ?? {}))
|
|
195
197
|
```
|
|
196
198
|
|
|
197
199
|
`nuxt prepare` runs Wrangler and writes exact, compatibility-aware types to `.nuxt/types/cloudflare-bindings.d.ts`. It merges root JSON, JSONC, or TOML bindings with `nitro.cloudflare.wrangler`. Nuxt and Nitro typechecks both reference the declaration, while bindings remain server runtime values. Production builds compare it with the final generated Wrangler config and fail on drift. Set `bindingTypes: false` only when another tool owns the declaration.
|
|
@@ -212,13 +214,12 @@ if (resolved._tag === 'missing')
|
|
|
212
214
|
throw new Error(`Missing Worker secrets: ${resolved.names.join(', ')}`)
|
|
213
215
|
|
|
214
216
|
await withWorkerSecretsFile({
|
|
215
|
-
path: '/secure/temp/secrets.json',
|
|
216
217
|
secrets: resolved.secrets,
|
|
217
218
|
use: path => exec('pnpm', ['wrangler', 'deploy', '--secrets-file', path]),
|
|
218
219
|
})
|
|
219
220
|
```
|
|
220
221
|
|
|
221
|
-
The
|
|
222
|
+
The helper owns a private temporary directory and creates the JSON file with mode 0600. It removes the directory after deployment succeeds or fails. Secret values may be strings or `null`; Wrangler treats `null` as a deletion marker.
|
|
222
223
|
|
|
223
224
|
## Deliberate boundaries
|
|
224
225
|
|
package/dist/bindings.d.mts
CHANGED
|
@@ -3,15 +3,22 @@ declare global {
|
|
|
3
3
|
interface CloudflareBindings {
|
|
4
4
|
}
|
|
5
5
|
}
|
|
6
|
-
interface
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
interface CloudflareRuntimeConfigSource<Environment extends object> {
|
|
7
|
+
context: {
|
|
8
|
+
nitro: Record<string, never>;
|
|
9
|
+
cloudflare: {
|
|
10
|
+
env: Environment;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
9
13
|
}
|
|
10
14
|
interface CloudflareBindingAccessor<Environment extends object> {
|
|
11
|
-
resolve: (source?:
|
|
12
|
-
get: <Name extends BindingName<Environment>>(name: Name, source?:
|
|
13
|
-
require: <Name extends BindingName<Environment>>(name: Name, source?:
|
|
15
|
+
resolve: (source?: unknown) => Environment | undefined;
|
|
16
|
+
get: <Name extends BindingName<Environment>>(name: Name, source?: unknown) => Environment[Name] | undefined;
|
|
17
|
+
require: <Name extends BindingName<Environment>>(name: Name, source?: unknown) => Exclude<Environment[Name], undefined>;
|
|
14
18
|
}
|
|
19
|
+
declare function resolveCloudflareBindings<Environment extends object = CloudflareBindings>(source?: unknown): Environment | undefined;
|
|
20
|
+
/** Creates the event-shaped source required by Nitro runtime config resolution. */
|
|
21
|
+
declare function runtimeConfigSource<Environment extends object>(env: Environment): CloudflareRuntimeConfigSource<Environment>;
|
|
15
22
|
/**
|
|
16
23
|
* Creates typed binding access for the generated `CloudflareBindings` environment.
|
|
17
24
|
*
|
|
@@ -20,5 +27,5 @@ interface CloudflareBindingAccessor<Environment extends object> {
|
|
|
20
27
|
*/
|
|
21
28
|
declare function createCloudflareBindings<Environment extends object = CloudflareBindings>(): CloudflareBindingAccessor<Environment>;
|
|
22
29
|
|
|
23
|
-
export { createCloudflareBindings };
|
|
24
|
-
export type { CloudflareBindingAccessor,
|
|
30
|
+
export { createCloudflareBindings, resolveCloudflareBindings, runtimeConfigSource };
|
|
31
|
+
export type { CloudflareBindingAccessor, CloudflareRuntimeConfigSource };
|
package/dist/bindings.mjs
CHANGED
|
@@ -5,15 +5,23 @@ function envFromContext(value) {
|
|
|
5
5
|
const cloudflare = asRecord(value)?.cloudflare;
|
|
6
6
|
return asRecord(asRecord(cloudflare)?.env);
|
|
7
7
|
}
|
|
8
|
-
function
|
|
8
|
+
function resolveCloudflareBindings(source) {
|
|
9
9
|
const sourceRecord = asRecord(source);
|
|
10
10
|
const sourceEnv = envFromContext(sourceRecord?.context) ?? envFromContext(sourceRecord);
|
|
11
11
|
const globalEnv = asRecord(globalThis.__env__);
|
|
12
12
|
return sourceEnv ?? globalEnv;
|
|
13
13
|
}
|
|
14
|
+
function runtimeConfigSource(env) {
|
|
15
|
+
return {
|
|
16
|
+
context: {
|
|
17
|
+
nitro: {},
|
|
18
|
+
cloudflare: { env }
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
14
22
|
function createCloudflareBindings() {
|
|
15
23
|
function get(name, source) {
|
|
16
|
-
const env =
|
|
24
|
+
const env = resolveCloudflareBindings(source);
|
|
17
25
|
return env && Object.hasOwn(env, name) ? env[name] : void 0;
|
|
18
26
|
}
|
|
19
27
|
function requireBinding(name, source) {
|
|
@@ -23,10 +31,10 @@ function createCloudflareBindings() {
|
|
|
23
31
|
return binding;
|
|
24
32
|
}
|
|
25
33
|
return {
|
|
26
|
-
resolve:
|
|
34
|
+
resolve: resolveCloudflareBindings,
|
|
27
35
|
get,
|
|
28
36
|
require: requireBinding
|
|
29
37
|
};
|
|
30
38
|
}
|
|
31
39
|
|
|
32
|
-
export { createCloudflareBindings };
|
|
40
|
+
export { createCloudflareBindings, resolveCloudflareBindings, runtimeConfigSource };
|
package/dist/cli/index.mjs
CHANGED
|
@@ -103,7 +103,7 @@ const doctor = defineCommand({
|
|
|
103
103
|
const main = defineCommand({
|
|
104
104
|
meta: {
|
|
105
105
|
name: "nuxt-cloudflare",
|
|
106
|
-
version: "0.0.
|
|
106
|
+
version: "0.0.11",
|
|
107
107
|
description: "Cloudflare deployment defaults and diagnostics for Nuxt"
|
|
108
108
|
},
|
|
109
109
|
subCommands: { doctor }
|
package/dist/deploy.d.mts
CHANGED
|
@@ -6,8 +6,7 @@ type WorkerSecretsResolution = {
|
|
|
6
6
|
secrets: Record<string, string>;
|
|
7
7
|
};
|
|
8
8
|
interface WorkerSecretsFileOptions<T> {
|
|
9
|
-
|
|
10
|
-
secrets: Readonly<Record<string, string>>;
|
|
9
|
+
secrets: Readonly<Record<string, string | null>>;
|
|
11
10
|
use: (path: string) => Promise<T> | T;
|
|
12
11
|
}
|
|
13
12
|
declare function resolveWorkerSecrets(requiredNames: readonly string[], environment: Readonly<Record<string, string | undefined>>): WorkerSecretsResolution;
|
package/dist/deploy.mjs
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { mkdtemp, rm, open } from 'node:fs/promises';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
2
4
|
|
|
3
5
|
function resolveWorkerSecrets(requiredNames, environment) {
|
|
4
6
|
const names = [...new Set(requiredNames)];
|
|
@@ -16,37 +18,33 @@ function settle(promise) {
|
|
|
16
18
|
(error) => ({ _tag: "error", error })
|
|
17
19
|
);
|
|
18
20
|
}
|
|
19
|
-
function isMissingFileError(error) {
|
|
20
|
-
return error !== null && typeof error === "object" && "code" in error && error.code === "ENOENT";
|
|
21
|
-
}
|
|
22
21
|
function throwFailures(errors, message) {
|
|
23
22
|
if (errors.length === 1)
|
|
24
23
|
throw errors[0];
|
|
25
24
|
throw new AggregateError(errors, message);
|
|
26
25
|
}
|
|
27
|
-
async function
|
|
28
|
-
const outcome = await settle(unlink(path));
|
|
29
|
-
if (outcome._tag === "error" && !isMissingFileError(outcome.error))
|
|
30
|
-
throw outcome.error;
|
|
31
|
-
}
|
|
32
|
-
async function writeWorkerSecretsFile(path, secrets) {
|
|
33
|
-
const payload = `${JSON.stringify(secrets)}
|
|
34
|
-
`;
|
|
26
|
+
async function writeWorkerSecretsFile(path, payload) {
|
|
35
27
|
const handle = await open(path, "wx", 384);
|
|
36
28
|
const writeOutcome = await settle(handle.writeFile(payload, "utf8"));
|
|
37
29
|
const closeOutcome = await settle(handle.close());
|
|
38
30
|
const failures = [writeOutcome, closeOutcome].filter((outcome) => outcome._tag === "error").map((outcome) => outcome.error);
|
|
39
31
|
if (failures.length === 0)
|
|
40
32
|
return;
|
|
41
|
-
const cleanupOutcome = await settle(removeWorkerSecretsFile(path));
|
|
42
|
-
if (cleanupOutcome._tag === "error")
|
|
43
|
-
failures.push(cleanupOutcome.error);
|
|
44
33
|
throwFailures(failures, "Failed to write and clean up the Worker secrets file");
|
|
45
34
|
}
|
|
46
35
|
async function withWorkerSecretsFile(options) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const
|
|
36
|
+
const payload = `${JSON.stringify(options.secrets)}
|
|
37
|
+
`;
|
|
38
|
+
const directory = await mkdtemp(join(tmpdir(), "nuxt-cloudflare-secrets-"));
|
|
39
|
+
const path = join(directory, "secrets.json");
|
|
40
|
+
const writeOutcome = await settle(writeWorkerSecretsFile(path, payload));
|
|
41
|
+
if (writeOutcome._tag === "error") {
|
|
42
|
+
const cleanupOutcome2 = await settle(rm(directory, { force: true, recursive: true }));
|
|
43
|
+
const failures2 = cleanupOutcome2._tag === "error" ? [writeOutcome.error, cleanupOutcome2.error] : [writeOutcome.error];
|
|
44
|
+
throwFailures(failures2, "Failed to write and clean up the Worker secrets file");
|
|
45
|
+
}
|
|
46
|
+
const useOutcome = await settle(Promise.resolve().then(() => options.use(path)));
|
|
47
|
+
const cleanupOutcome = await settle(rm(directory, { force: true, recursive: true }));
|
|
50
48
|
const failures = [useOutcome, cleanupOutcome].filter((outcome) => outcome._tag === "error").map((outcome) => outcome.error);
|
|
51
49
|
if (failures.length > 0)
|
|
52
50
|
throwFailures(failures, "Worker deployment and secrets file cleanup failed");
|
package/dist/module.json
CHANGED
package/package.json
CHANGED