@revojs/cloudflare 0.2.59 → 0.2.61
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/bun.d.ts +1 -0
- package/dist/module/bun.js +26 -0
- package/dist/module/index.d.ts +2 -1
- package/dist/module/index.js +3 -1
- package/dist/package.js +2 -2
- package/dist/plugins/index.server.d.ts +4 -0
- package/dist/plugins/index.server.js +8 -0
- package/package.json +2 -2
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 };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import Module from "module";
|
|
2
|
+
import { Server } from "net";
|
|
3
|
+
import { networkInterfaces } from "os";
|
|
4
|
+
//#region src/module/bun.ts
|
|
5
|
+
var loader = Module;
|
|
6
|
+
var resolveFilename = loader._resolveFilename;
|
|
7
|
+
var listen = Object.getOwnPropertyDescriptor(Server.prototype, "listen")?.value;
|
|
8
|
+
if (process.versions.bun !== void 0) {
|
|
9
|
+
loader._resolveFilename = (request, ...options) => {
|
|
10
|
+
if (request === "undici") return resolveFilename.call(loader, "undici/index.js", ...options);
|
|
11
|
+
return resolveFilename.call(loader, request, ...options);
|
|
12
|
+
};
|
|
13
|
+
let ipv6 = false;
|
|
14
|
+
for (const addresses of Object.values(networkInterfaces())) for (const address of addresses ?? []) if (address.family === "IPv6") ipv6 = true;
|
|
15
|
+
if (!ipv6) Server.prototype.listen = function(...options) {
|
|
16
|
+
const [first, second] = options;
|
|
17
|
+
if (typeof first === "number" && typeof second !== "string") options.splice(1, 0, "0.0.0.0");
|
|
18
|
+
if (typeof first === "object" && first !== null && !("path" in first) && (!("host" in first) || first.host === void 0)) options[0] = {
|
|
19
|
+
...first,
|
|
20
|
+
host: "0.0.0.0"
|
|
21
|
+
};
|
|
22
|
+
return listen.apply(this, options);
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
//#endregion
|
|
26
|
+
export {};
|
package/dist/module/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "./bun.ts";
|
|
2
|
+
import type { PluginConfig } from "@cloudflare/vite-plugin";
|
|
2
3
|
export type CloudflareConfig = PluginConfig;
|
|
3
4
|
declare const _default: import("revojs/module").Module<"cloudflare", PluginConfig, readonly [import("revojs/module").Module<"server", Record<string, never>, []>]>;
|
|
4
5
|
export default _default;
|
package/dist/module/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import "./bun.js";
|
|
1
2
|
import package_default from "../package.js";
|
|
2
|
-
import { cloudflare } from "@cloudflare/vite-plugin";
|
|
3
3
|
import server, { SERVER } from "@revojs/server/module";
|
|
4
4
|
import { existsSync, readFileSync, writeFileSync } from "fs";
|
|
5
5
|
import { join } from "path";
|
|
@@ -11,6 +11,7 @@ var module_default = defineModule({
|
|
|
11
11
|
modules: [server],
|
|
12
12
|
config: () => ({ viteEnvironment: { name: SERVER } }),
|
|
13
13
|
async setup(config) {
|
|
14
|
+
const { cloudflare } = await import("@cloudflare/vite-plugin");
|
|
14
15
|
const { fromModule } = useModule(import.meta.url);
|
|
15
16
|
const crons = /* @__PURE__ */ new Set();
|
|
16
17
|
for (const path of resolvePaths(["./schedules/**/*.{js,ts}", "!./schedules/**/*.d.ts"])) {
|
|
@@ -71,6 +72,7 @@ var module_default = defineModule({
|
|
|
71
72
|
});
|
|
72
73
|
return {
|
|
73
74
|
sources: {
|
|
75
|
+
plugins: { from: [fromModule("../plugins")] },
|
|
74
76
|
"durable-objects": durableObjects,
|
|
75
77
|
entrypoints: {
|
|
76
78
|
from: ["./entrypoints"],
|
package/dist/package.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
//#region package.json
|
|
2
2
|
var name = "@revojs/cloudflare";
|
|
3
|
-
var version = "0.2.
|
|
3
|
+
var version = "0.2.61";
|
|
4
4
|
var repository = {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/tellua/revojs.git"
|
|
@@ -22,7 +22,7 @@ var exports = {
|
|
|
22
22
|
};
|
|
23
23
|
var scripts = {
|
|
24
24
|
"build": "vite build",
|
|
25
|
-
"typecheck": "
|
|
25
|
+
"typecheck": "bun check",
|
|
26
26
|
"watch": "vite build --watch"
|
|
27
27
|
};
|
|
28
28
|
var dependencies = {
|
|
@@ -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 };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@revojs/cloudflare",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.61",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
},
|
|
26
26
|
"scripts": {
|
|
27
27
|
"build": "vite build",
|
|
28
|
-
"typecheck": "
|
|
28
|
+
"typecheck": "bun check",
|
|
29
29
|
"watch": "vite build --watch"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|