@revojs/cloudflare 0.2.58 → 0.2.60
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/dist/index.d.ts +13 -0
- package/dist/index.js +105 -3
- package/dist/module/index.js +1 -0
- package/dist/package.js +1 -1
- package/dist/plugins/index.server.d.ts +4 -0
- package/dist/plugins/index.server.js +8 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -26,3 +26,16 @@ export declare const SECRETS: {
|
|
|
26
26
|
store_id: string;
|
|
27
27
|
secret_name: string;
|
|
28
28
|
}[];
|
|
29
|
+
export interface LocalPlatform {
|
|
30
|
+
env: Record<string, unknown>;
|
|
31
|
+
dispose: () => Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
export interface LocalMigrationOptions {
|
|
34
|
+
reset?: boolean;
|
|
35
|
+
}
|
|
36
|
+
export declare const PLATFORM_CONTEXT: import("revojs").Descriptor<Partial<LocalPlatform>>;
|
|
37
|
+
export declare const LOCAL_DIRECTORY = ".wrangler/revojs";
|
|
38
|
+
export declare const LOCAL_STATE = ".wrangler/state";
|
|
39
|
+
export declare function isLocalRuntime(): boolean;
|
|
40
|
+
export declare function startLocalPlatform(scope: Scope): Promise<LocalPlatform>;
|
|
41
|
+
export declare function migrateLocalDatabases(scope: Scope, options?: LocalMigrationOptions): Promise<void>;
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import config from "#config/cloudflare";
|
|
2
|
-
import { defineHook, useVariables } from "revojs";
|
|
2
|
+
import { VARIABLE_CONTEXT, defineContext, defineHook, getContext, setContext, useVariables } from "revojs";
|
|
3
3
|
//#region src/index.ts
|
|
4
4
|
function useDurableObjects(scope) {
|
|
5
5
|
return useVariables(scope);
|
|
@@ -11,7 +11,11 @@ async function resolveSecrets(variables) {
|
|
|
11
11
|
const resolved = { ...variables };
|
|
12
12
|
for (const { binding } of SECRETS) {
|
|
13
13
|
const secret = resolved[binding];
|
|
14
|
-
if (isSecretsStoreSecret(secret))
|
|
14
|
+
if (isSecretsStoreSecret(secret)) try {
|
|
15
|
+
resolved[binding] = await secret.get();
|
|
16
|
+
} catch {
|
|
17
|
+
resolved[binding] = void 0;
|
|
18
|
+
}
|
|
15
19
|
}
|
|
16
20
|
return resolved;
|
|
17
21
|
}
|
|
@@ -20,5 +24,103 @@ function isSecretsStoreSecret(value) {
|
|
|
20
24
|
}
|
|
21
25
|
var EMAIL_RECEIVED_HOOK = defineHook("EMAIL_RECEIVED_HOOK");
|
|
22
26
|
var SECRETS = (typeof config.config === "object" ? config.config.secrets_store_secrets : void 0) ?? [];
|
|
27
|
+
var PLATFORM_CONTEXT = defineContext("PLATFORM_CONTEXT");
|
|
28
|
+
var LOCAL_DIRECTORY = ".wrangler/revojs";
|
|
29
|
+
var LOCAL_STATE = ".wrangler/state";
|
|
30
|
+
function isLocalRuntime() {
|
|
31
|
+
return globalThis.navigator?.userAgent !== "Cloudflare-Workers" && typeof localRuntime()?.getBuiltinModule === "function";
|
|
32
|
+
}
|
|
33
|
+
async function startLocalPlatform(scope) {
|
|
34
|
+
const { getPlatformProxy } = await import(
|
|
35
|
+
/* @vite-ignore */
|
|
36
|
+
"wrangler"
|
|
37
|
+
);
|
|
38
|
+
const platform = await getPlatformProxy({
|
|
39
|
+
configPath: writeLocalConfig(),
|
|
40
|
+
persist: { path: `${LOCAL_STATE}/v3` }
|
|
41
|
+
});
|
|
42
|
+
setContext(scope, PLATFORM_CONTEXT, platform);
|
|
43
|
+
setContext(scope, VARIABLE_CONTEXT, {
|
|
44
|
+
...await resolveSecrets(platform.env),
|
|
45
|
+
...getContext(scope, VARIABLE_CONTEXT)
|
|
46
|
+
});
|
|
47
|
+
return platform;
|
|
48
|
+
}
|
|
49
|
+
async function migrateLocalDatabases(scope, options = {}) {
|
|
50
|
+
const runtime = requireLocalRuntime();
|
|
51
|
+
const { rmSync } = runtime.getBuiltinModule("node:fs");
|
|
52
|
+
const { resolve } = runtime.getBuiltinModule("node:path");
|
|
53
|
+
const { spawnSync } = runtime.getBuiltinModule("node:child_process");
|
|
54
|
+
await getContext(scope, PLATFORM_CONTEXT).dispose?.();
|
|
55
|
+
setContext(scope, PLATFORM_CONTEXT, {});
|
|
56
|
+
const path = writeLocalConfig();
|
|
57
|
+
if (options.reset) rmSync(resolve(LOCAL_STATE, "v3", "d1"), {
|
|
58
|
+
recursive: true,
|
|
59
|
+
force: true
|
|
60
|
+
});
|
|
61
|
+
for (const database of localDatabases()) {
|
|
62
|
+
if (!database.migrations_dir) continue;
|
|
63
|
+
if (spawnSync("npx", [
|
|
64
|
+
"wrangler",
|
|
65
|
+
"d1",
|
|
66
|
+
"migrations",
|
|
67
|
+
"apply",
|
|
68
|
+
database.binding,
|
|
69
|
+
"--local",
|
|
70
|
+
"--config",
|
|
71
|
+
path,
|
|
72
|
+
"--persist-to",
|
|
73
|
+
".wrangler/state"
|
|
74
|
+
], {
|
|
75
|
+
stdio: "inherit",
|
|
76
|
+
env: {
|
|
77
|
+
...runtime.env,
|
|
78
|
+
CI: "1"
|
|
79
|
+
}
|
|
80
|
+
}).status !== 0) throw new Error(`Applying the migrations of ${database.binding} failed.`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
var LOCAL_EXCLUDED = /* @__PURE__ */ new Set([
|
|
84
|
+
"main",
|
|
85
|
+
"assets",
|
|
86
|
+
"durable_objects",
|
|
87
|
+
"workflows",
|
|
88
|
+
"exports",
|
|
89
|
+
"migrations",
|
|
90
|
+
"triggers",
|
|
91
|
+
"containers",
|
|
92
|
+
"previews",
|
|
93
|
+
"routes"
|
|
94
|
+
]);
|
|
95
|
+
function localRuntime() {
|
|
96
|
+
return globalThis.process;
|
|
97
|
+
}
|
|
98
|
+
function requireLocalRuntime() {
|
|
99
|
+
const runtime = localRuntime();
|
|
100
|
+
if (!runtime || !isLocalRuntime()) throw new Error("Local Cloudflare bindings are only available in commands running on Node.js.");
|
|
101
|
+
return runtime;
|
|
102
|
+
}
|
|
103
|
+
function localDatabases() {
|
|
104
|
+
return (typeof config.config === "object" ? config.config : {}).d1_databases ?? [];
|
|
105
|
+
}
|
|
106
|
+
function writeLocalConfig() {
|
|
107
|
+
const runtime = requireLocalRuntime();
|
|
108
|
+
const { mkdirSync, writeFileSync } = runtime.getBuiltinModule("node:fs");
|
|
109
|
+
const { resolve } = runtime.getBuiltinModule("node:path");
|
|
110
|
+
const local = {};
|
|
111
|
+
for (const [key, value] of Object.entries(typeof config.config === "object" ? config.config : {})) if (!LOCAL_EXCLUDED.has(key)) local[key] = value;
|
|
112
|
+
const databases = new Array();
|
|
113
|
+
for (const database of localDatabases()) {
|
|
114
|
+
const entry = { ...database };
|
|
115
|
+
if (entry.migrations_dir) entry.migrations_dir = resolve(entry.migrations_dir);
|
|
116
|
+
if (entry.migrations_pattern) entry.migrations_pattern = resolve(entry.migrations_pattern);
|
|
117
|
+
databases.push(entry);
|
|
118
|
+
}
|
|
119
|
+
local.d1_databases = databases;
|
|
120
|
+
const path = resolve(LOCAL_DIRECTORY, "wrangler.json");
|
|
121
|
+
mkdirSync(resolve(LOCAL_DIRECTORY), { recursive: true });
|
|
122
|
+
writeFileSync(path, JSON.stringify(local, null, 4));
|
|
123
|
+
return path;
|
|
124
|
+
}
|
|
23
125
|
//#endregion
|
|
24
|
-
export { EMAIL_RECEIVED_HOOK, SECRETS, isSecretsStoreSecret, resolveSecrets, useDurableObjects, useWorkflows };
|
|
126
|
+
export { EMAIL_RECEIVED_HOOK, LOCAL_DIRECTORY, LOCAL_STATE, PLATFORM_CONTEXT, SECRETS, isLocalRuntime, isSecretsStoreSecret, migrateLocalDatabases, resolveSecrets, startLocalPlatform, useDurableObjects, useWorkflows };
|
package/dist/module/index.js
CHANGED
package/dist/package.js
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { isLocalRuntime, startLocalPlatform } from "../index.js";
|
|
2
|
+
import { definePlugin } from "revojs";
|
|
3
|
+
//#region src/plugins/index.server.ts
|
|
4
|
+
var index_server_default = definePlugin({ async setup(scope) {
|
|
5
|
+
if (isLocalRuntime()) await startLocalPlatform(scope);
|
|
6
|
+
} });
|
|
7
|
+
//#endregion
|
|
8
|
+
export { index_server_default as default };
|