@revojs/cloudflare 0.2.5 → 0.2.7
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 +10 -5
- package/dist/index.js +8 -68
- package/dist/module/index.js +64 -8
- package/dist/server/index.d.ts +9 -0
- package/dist/server/index.js +73 -0
- package/dist/types/index.d.ts +20 -0
- package/package.json +11 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import type { DurableObjectNamespace, Workflow } from "@cloudflare/workers-types";
|
|
2
|
+
import { type Scope } from "revojs";
|
|
3
|
+
export type ScreamingSnake<Value extends string> = Value extends `${infer Head}${infer Next}${infer Tail}` ? `${Uppercase<Head>}${Head extends Lowercase<Head> ? (Next extends Lowercase<Next> ? "" : "_") : ""}${ScreamingSnake<`${Next}${Tail}`>}` : Uppercase<Value>;
|
|
4
|
+
export type DurableObjects = {
|
|
5
|
+
[ClassName in keyof typeof import("#durable-objects") as ScreamingSnake<ClassName>]: DurableObjectNamespace<InstanceType<(typeof import("#durable-objects"))[ClassName]>>;
|
|
5
6
|
};
|
|
6
|
-
export
|
|
7
|
+
export type Workflows = {
|
|
8
|
+
[ClassName in keyof typeof import("#workflows") as ScreamingSnake<ClassName>]: Workflow;
|
|
9
|
+
};
|
|
10
|
+
export declare function useDurableObjects(scope: Scope): DurableObjects;
|
|
11
|
+
export declare function useWorkflows(scope: Scope): Workflows;
|
package/dist/index.js
CHANGED
|
@@ -1,70 +1,10 @@
|
|
|
1
|
-
import "
|
|
2
|
-
import { SCHEDULE_CONTEXT, SERVER_CONTEXT, isResponse, isWebSocket, sendResponse } from "@revojs/server";
|
|
3
|
-
import { VARIABLE_CONTEXT, createScope } from "revojs";
|
|
1
|
+
import { useVariables } from "revojs";
|
|
4
2
|
//#region src/index.ts
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
states: {},
|
|
12
|
-
request,
|
|
13
|
-
response: { headers: new Headers() }
|
|
14
|
-
}
|
|
15
|
-
} });
|
|
16
|
-
const result = await server.fetch(scope);
|
|
17
|
-
if (isResponse(result)) return result;
|
|
18
|
-
if (isWebSocket(result)) {
|
|
19
|
-
const [clientSocket, serverSocket] = new WebSocketPair();
|
|
20
|
-
const connection = {
|
|
21
|
-
async send(message) {
|
|
22
|
-
serverSocket.send(message);
|
|
23
|
-
},
|
|
24
|
-
async close(code, reason) {
|
|
25
|
-
serverSocket.close(code, reason);
|
|
26
|
-
}
|
|
27
|
-
};
|
|
28
|
-
serverSocket.addEventListener("open", async () => {
|
|
29
|
-
try {
|
|
30
|
-
await result.open(connection);
|
|
31
|
-
} catch (error) {
|
|
32
|
-
if (error instanceof Error) await result.error(connection, error);
|
|
33
|
-
else throw error;
|
|
34
|
-
}
|
|
35
|
-
});
|
|
36
|
-
serverSocket.addEventListener("message", async (event) => {
|
|
37
|
-
try {
|
|
38
|
-
await result.message(connection, event.data);
|
|
39
|
-
} catch (error) {
|
|
40
|
-
if (error instanceof Error) await result.error(connection, error);
|
|
41
|
-
else throw error;
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
serverSocket.addEventListener("error", async (event) => {
|
|
45
|
-
await result.error(connection, event.error);
|
|
46
|
-
});
|
|
47
|
-
serverSocket.addEventListener("close", async (event) => {
|
|
48
|
-
try {
|
|
49
|
-
await result.close(connection, event.code, event.reason);
|
|
50
|
-
} catch (error) {
|
|
51
|
-
if (error instanceof Error) await result.error(connection, error);
|
|
52
|
-
else throw error;
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
serverSocket.accept();
|
|
56
|
-
return sendResponse(scope, null, {
|
|
57
|
-
status: 101,
|
|
58
|
-
webSocket: clientSocket
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
},
|
|
62
|
-
async scheduled(event, variables) {
|
|
63
|
-
await (await import("#server").then((module) => module.default)).schedule(await createScope({ context: {
|
|
64
|
-
[VARIABLE_CONTEXT]: variables,
|
|
65
|
-
[SCHEDULE_CONTEXT]: { when: event.cron }
|
|
66
|
-
} }));
|
|
67
|
-
}
|
|
68
|
-
};
|
|
3
|
+
function useDurableObjects(scope) {
|
|
4
|
+
return useVariables(scope);
|
|
5
|
+
}
|
|
6
|
+
function useWorkflows(scope) {
|
|
7
|
+
return useVariables(scope);
|
|
8
|
+
}
|
|
69
9
|
//#endregion
|
|
70
|
-
export {
|
|
10
|
+
export { useDurableObjects, useWorkflows };
|
package/dist/module/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { cloudflare } from "@cloudflare/vite-plugin";
|
|
2
2
|
import server, { SERVER } from "@revojs/server/module";
|
|
3
3
|
import { readFileSync } from "fs";
|
|
4
|
-
import { addTypes, defineModule, resolvePaths, useModule } from "revojs/module";
|
|
5
|
-
import { mergeObjects, scheduleAliases } from "revojs/shared";
|
|
4
|
+
import { addTypes, defineModule, resolvePaths, resolveSource, useModule } from "revojs/module";
|
|
5
|
+
import { mergeObjects, scheduleAliases, toExportNames } from "revojs/shared";
|
|
6
6
|
//#region src/module/index.ts
|
|
7
7
|
var module_default = defineModule({
|
|
8
8
|
name: "cloudflare",
|
|
@@ -15,19 +15,75 @@ var module_default = defineModule({
|
|
|
15
15
|
const content = readFileSync(path, "utf8");
|
|
16
16
|
for (const match of content.matchAll(SCHEDULE_WHEN_MATCH)) crons.add(scheduleAliases[match[1].trim().toLowerCase()] ?? match[1].trim());
|
|
17
17
|
}
|
|
18
|
-
|
|
18
|
+
const durableObjects = {
|
|
19
|
+
match: [
|
|
20
|
+
"**/*.js",
|
|
21
|
+
"**/*.ts",
|
|
22
|
+
"!*.d.ts"
|
|
23
|
+
],
|
|
24
|
+
from: ["./durable-objects"],
|
|
25
|
+
format: "named"
|
|
26
|
+
};
|
|
27
|
+
const workflows = {
|
|
28
|
+
match: [
|
|
29
|
+
"**/*.js",
|
|
30
|
+
"**/*.ts",
|
|
31
|
+
"!*.d.ts"
|
|
32
|
+
],
|
|
33
|
+
from: ["./workflows"],
|
|
34
|
+
format: "named"
|
|
35
|
+
};
|
|
36
|
+
const durableObjectKeys = new Array();
|
|
37
|
+
for (const files of resolveSource(durableObjects).values()) for (const key of files.keys()) durableObjectKeys.push(key);
|
|
38
|
+
const workflowKeys = new Array();
|
|
39
|
+
for (const files of resolveSource(workflows).values()) for (const key of files.keys()) workflowKeys.push(key);
|
|
40
|
+
const durableObjectNames = toExportNames(durableObjectKeys);
|
|
41
|
+
const workflowNames = toExportNames(workflowKeys);
|
|
42
|
+
const durableObjectBindings = durableObjectNames.map((className) => ({
|
|
43
|
+
name: className.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toUpperCase(),
|
|
44
|
+
class_name: className
|
|
45
|
+
}));
|
|
46
|
+
const workflowBindings = workflowNames.map((className) => ({
|
|
47
|
+
binding: className.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toUpperCase(),
|
|
48
|
+
name: className.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase(),
|
|
49
|
+
class_name: className
|
|
50
|
+
}));
|
|
51
|
+
const exportsMap = {};
|
|
52
|
+
for (const className of durableObjectNames) exportsMap[className] = {
|
|
53
|
+
type: "durable-object",
|
|
54
|
+
storage: "sqlite"
|
|
55
|
+
};
|
|
56
|
+
addTypes(config, "cloudflare", () => `/// <reference types="@cloudflare/workers-types" />`);
|
|
19
57
|
const options = mergeObjects(config.cloudflare, {
|
|
20
58
|
viteEnvironment: { name: SERVER },
|
|
21
59
|
config: {
|
|
22
|
-
main: fromModule("../index.js"),
|
|
60
|
+
main: fromModule("../server/index.js"),
|
|
23
61
|
assets: { run_worker_first: ["/"] },
|
|
62
|
+
durable_objects: { bindings: durableObjectBindings },
|
|
63
|
+
workflows: workflowBindings,
|
|
64
|
+
exports: exportsMap,
|
|
24
65
|
triggers: { crons: [...crons] }
|
|
25
66
|
}
|
|
26
67
|
});
|
|
27
|
-
return {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
68
|
+
return {
|
|
69
|
+
sources: {
|
|
70
|
+
"durable-objects": durableObjects,
|
|
71
|
+
entrypoints: {
|
|
72
|
+
match: [
|
|
73
|
+
"**/*.js",
|
|
74
|
+
"**/*.ts",
|
|
75
|
+
"!*.d.ts"
|
|
76
|
+
],
|
|
77
|
+
from: ["./entrypoints"],
|
|
78
|
+
format: "named"
|
|
79
|
+
},
|
|
80
|
+
workflows
|
|
81
|
+
},
|
|
82
|
+
vite: {
|
|
83
|
+
server: { watch: { ignored: ["**/.wrangler/**"] } },
|
|
84
|
+
plugins: [cloudflare(options)]
|
|
85
|
+
}
|
|
86
|
+
};
|
|
31
87
|
}
|
|
32
88
|
});
|
|
33
89
|
var SCHEDULE_WHEN_MATCH = /when\s*:\s*["'`]([^"'`\n]+)["'`]/g;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ScheduledEvent } from "@cloudflare/workers-types";
|
|
2
|
+
export * from "#durable-objects";
|
|
3
|
+
export * from "#entrypoints";
|
|
4
|
+
export * from "#workflows";
|
|
5
|
+
declare const _default: {
|
|
6
|
+
fetch(request: Request, variables: Cloudflare.Env): Promise<Response | undefined>;
|
|
7
|
+
scheduled(event: ScheduledEvent, variables: Cloudflare.Env): Promise<void>;
|
|
8
|
+
};
|
|
9
|
+
export default _default;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { VARIABLE_CONTEXT, createScope } from "revojs";
|
|
2
|
+
import "@cloudflare/workers-types";
|
|
3
|
+
import { SCHEDULE_CONTEXT, SERVER_CONTEXT, isResponse, isWebSocket, sendResponse } from "@revojs/server";
|
|
4
|
+
export * from "#durable-objects";
|
|
5
|
+
export * from "#entrypoints";
|
|
6
|
+
export * from "#workflows";
|
|
7
|
+
//#region src/server/index.ts
|
|
8
|
+
var server_default = {
|
|
9
|
+
async fetch(request, variables) {
|
|
10
|
+
const server = await import("#server").then((module) => module.default);
|
|
11
|
+
const scope = await createScope({ context: {
|
|
12
|
+
[VARIABLE_CONTEXT]: variables,
|
|
13
|
+
[SERVER_CONTEXT]: {
|
|
14
|
+
states: {},
|
|
15
|
+
request,
|
|
16
|
+
response: { headers: new Headers() }
|
|
17
|
+
}
|
|
18
|
+
} });
|
|
19
|
+
const result = await server.fetch(scope);
|
|
20
|
+
if (isResponse(result)) return result;
|
|
21
|
+
if (isWebSocket(result)) {
|
|
22
|
+
const [clientSocket, serverSocket] = new WebSocketPair();
|
|
23
|
+
const connection = {
|
|
24
|
+
async send(message) {
|
|
25
|
+
serverSocket.send(message);
|
|
26
|
+
},
|
|
27
|
+
async close(code, reason) {
|
|
28
|
+
serverSocket.close(code, reason);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
serverSocket.addEventListener("open", async () => {
|
|
32
|
+
try {
|
|
33
|
+
await result.open(connection);
|
|
34
|
+
} catch (error) {
|
|
35
|
+
if (error instanceof Error) await result.error(connection, error);
|
|
36
|
+
else throw error;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
serverSocket.addEventListener("message", async (event) => {
|
|
40
|
+
try {
|
|
41
|
+
await result.message(connection, event.data);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
if (error instanceof Error) await result.error(connection, error);
|
|
44
|
+
else throw error;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
serverSocket.addEventListener("error", async (event) => {
|
|
48
|
+
await result.error(connection, event.error);
|
|
49
|
+
});
|
|
50
|
+
serverSocket.addEventListener("close", async (event) => {
|
|
51
|
+
try {
|
|
52
|
+
await result.close(connection, event.code, event.reason);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (error instanceof Error) await result.error(connection, error);
|
|
55
|
+
else throw error;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
serverSocket.accept();
|
|
59
|
+
return sendResponse(scope, null, {
|
|
60
|
+
status: 101,
|
|
61
|
+
webSocket: clientSocket
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
async scheduled(event, variables) {
|
|
66
|
+
await (await import("#server").then((module) => module.default)).schedule(await createScope({ context: {
|
|
67
|
+
[VARIABLE_CONTEXT]: variables,
|
|
68
|
+
[SCHEDULE_CONTEXT]: { when: event.cron }
|
|
69
|
+
} }));
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
//#endregion
|
|
73
|
+
export { server_default as default };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
declare module "#durable-objects" {
|
|
2
|
+
import type { DurableObject } from "cloudflare:workers";
|
|
3
|
+
|
|
4
|
+
const durableObject: typeof DurableObject;
|
|
5
|
+
export { durableObject };
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
declare module "#workflows" {
|
|
9
|
+
import type { WorkflowEntrypoint } from "cloudflare:workers";
|
|
10
|
+
|
|
11
|
+
const workflow: typeof WorkflowEntrypoint;
|
|
12
|
+
export { workflow };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
declare module "#entrypoints" {
|
|
16
|
+
import type { WorkerEntrypoint } from "cloudflare:workers";
|
|
17
|
+
|
|
18
|
+
const entrypoint: typeof WorkerEntrypoint;
|
|
19
|
+
export { entrypoint };
|
|
20
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@revojs/cloudflare",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.7",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -10,7 +10,14 @@
|
|
|
10
10
|
"dist"
|
|
11
11
|
],
|
|
12
12
|
"type": "module",
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"module": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
13
16
|
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.js"
|
|
20
|
+
},
|
|
14
21
|
"./module": {
|
|
15
22
|
"types": "./dist/module/index.d.ts",
|
|
16
23
|
"import": "./dist/module/index.js"
|
|
@@ -21,11 +28,11 @@
|
|
|
21
28
|
"watch": "vite build --watch"
|
|
22
29
|
},
|
|
23
30
|
"dependencies": {
|
|
24
|
-
"@cloudflare/vite-plugin": "^1.
|
|
25
|
-
"@cloudflare/workers-types": "^5.
|
|
31
|
+
"@cloudflare/vite-plugin": "^1.62.5",
|
|
32
|
+
"@cloudflare/workers-types": "^5.20261004.1",
|
|
26
33
|
"@revojs/server": "*",
|
|
27
34
|
"revojs": "*",
|
|
28
|
-
"wrangler": "^4.
|
|
35
|
+
"wrangler": "^4.147.0"
|
|
29
36
|
},
|
|
30
37
|
"devDependencies": {
|
|
31
38
|
"@revojs/tsconfig": "*"
|