@discord0/adapter-cloudflare 0.0.0-stage → 0.1.0-alpha.0
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/LICENSE +21 -0
- package/README.md +15 -2
- package/package.json +51 -4
- package/src/adapter.ts +60 -0
- package/src/index.ts +110 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nate Babyak
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @discord0/adapter-cloudflare
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Cloudflare Workers adapter for [discord0](https://www.npmjs.com/package/discord0).
|
|
4
|
+
|
|
5
|
+
> [!WARNING]
|
|
6
|
+
> discord0 is in alpha. APIs can change between releases without notice.
|
|
7
|
+
|
|
8
|
+
Create a project that uses it:
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
npx discord0 init my-bot --adapter cloudflare
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Docs: https://discord0.babyak.dev/adapters/cloudflare/
|
|
15
|
+
|
|
16
|
+
License: https://github.com/natebabyak/discord0/blob/main/LICENSE
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@discord0/adapter-cloudflare",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0-alpha.0",
|
|
4
|
+
"description": "Cloudflare Workers adapter for discord0.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cloudflare",
|
|
7
|
+
"discord",
|
|
8
|
+
"discord-bot",
|
|
9
|
+
"discord-interactions",
|
|
10
|
+
"serverless",
|
|
11
|
+
"slash-commands"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://discord0.babyak.dev/adapters/cloudflare/",
|
|
14
|
+
"bugs": "https://github.com/natebabyak/discord0/issues",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "Nate Babyak",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/natebabyak/discord0.git",
|
|
20
|
+
"directory": "packages/adapter-cloudflare"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"src",
|
|
24
|
+
"LICENSE",
|
|
25
|
+
"README.md"
|
|
26
|
+
],
|
|
27
|
+
"type": "module",
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"module": "src/index.ts",
|
|
30
|
+
"types": "src/index.ts",
|
|
31
|
+
"exports": {
|
|
32
|
+
".": "./src/index.ts"
|
|
33
|
+
},
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"typecheck": "bun check",
|
|
39
|
+
"test": "bun test ./tests"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/bun": "latest",
|
|
43
|
+
"typescript": "^7"
|
|
44
|
+
},
|
|
45
|
+
"peerDependencies": {
|
|
46
|
+
"discord0": "^0.1.0-alpha.0"
|
|
47
|
+
},
|
|
48
|
+
"peerDependenciesMeta": {
|
|
49
|
+
"discord0": {
|
|
50
|
+
"optional": true
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
package/src/adapter.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { HostedAdapter } from "discord0";
|
|
2
|
+
|
|
3
|
+
const json = (value: unknown) => `${JSON.stringify(value, null, 2)}\n`;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Cloudflare Workers deployment adapter for `discord0.config.ts`.
|
|
7
|
+
* `discord0 deploy` bundles the app with `cloudflare(app)` and deploys it with
|
|
8
|
+
* Wrangler.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* import adapter from "@discord0/adapter-cloudflare";
|
|
13
|
+
* import { defineConfig } from "discord0";
|
|
14
|
+
*
|
|
15
|
+
* export default defineConfig({ adapter: adapter() });
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @see [Cloudflare Workers adapter](https://discord0.babyak.dev/adapters/cloudflare/)
|
|
19
|
+
*/
|
|
20
|
+
export default function adapter(): HostedAdapter {
|
|
21
|
+
return {
|
|
22
|
+
name: "cloudflare",
|
|
23
|
+
runtime: {
|
|
24
|
+
module: import.meta.resolve("./index.ts"),
|
|
25
|
+
export: "cloudflare",
|
|
26
|
+
},
|
|
27
|
+
platform: "browser",
|
|
28
|
+
output: "worker.mjs",
|
|
29
|
+
files: ({ name, tasks }) => ({
|
|
30
|
+
"package.json": json({ name, private: true, type: "module" }),
|
|
31
|
+
"wrangler.json": json({
|
|
32
|
+
name,
|
|
33
|
+
main: "worker.mjs",
|
|
34
|
+
compatibility_date: "2026-11-03",
|
|
35
|
+
// Exposes bindings through process.env for the default credential lookup.
|
|
36
|
+
compatibility_flags: ["nodejs_compat"],
|
|
37
|
+
workers_dev: true,
|
|
38
|
+
// Always written, so removing every task also removes its triggers.
|
|
39
|
+
triggers: {
|
|
40
|
+
crons: [...new Set(tasks.map((task) => task.expression))],
|
|
41
|
+
},
|
|
42
|
+
}),
|
|
43
|
+
}),
|
|
44
|
+
// Cron Triggers fire in UTC; scheduled() runs every task on the trigger.
|
|
45
|
+
tasks: {
|
|
46
|
+
timezones: false,
|
|
47
|
+
notes: (tasks) => {
|
|
48
|
+
const crons = new Set(tasks.map((task) => task.expression)).size;
|
|
49
|
+
return [
|
|
50
|
+
`${crons === 1 ? "1 Cron Trigger" : `${crons} Cron Triggers`}. Workers Free allows 5 per account and 10 ms of CPU per run; Workers Paid allows 1,000. Trigger changes can take up to 15 minutes to apply.`,
|
|
51
|
+
];
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
// Workers always deploy live; there is no preview flag to add.
|
|
55
|
+
command: () => ["wrangler", "deploy"],
|
|
56
|
+
urlPatterns: [String.raw`https://[a-z0-9.-]+\.workers\.dev\b/?`],
|
|
57
|
+
urlHint:
|
|
58
|
+
"the *.workers.dev URL from Wrangler output or the Cloudflare dashboard",
|
|
59
|
+
};
|
|
60
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type AppExecutionContext,
|
|
3
|
+
applyHostDefaults,
|
|
4
|
+
Discord0Error,
|
|
5
|
+
type FetchApplication,
|
|
6
|
+
type TaskApplication,
|
|
7
|
+
} from "discord0";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The `controller` Cloudflare Workers passes to `scheduled()` when a Cron
|
|
11
|
+
* Trigger fires.
|
|
12
|
+
*
|
|
13
|
+
* @see [Cloudflare Workers scheduled tasks](https://discord0.babyak.dev/adapters/cloudflare/#scheduled-tasks)
|
|
14
|
+
*/
|
|
15
|
+
export interface ScheduledController {
|
|
16
|
+
/** The trigger's cron expression, as written in `triggers.crons`. */
|
|
17
|
+
cron: string;
|
|
18
|
+
/** When the trigger was scheduled to fire, in milliseconds since the epoch. */
|
|
19
|
+
scheduledTime: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Default `autoDeferAfterMs` (2000ms) applied on Cloudflare Workers when left
|
|
24
|
+
* unset on `new Discord()`.
|
|
25
|
+
*
|
|
26
|
+
* @see [Cloudflare Workers deferred work](https://discord0.babyak.dev/adapters/cloudflare/#deferred-work)
|
|
27
|
+
*/
|
|
28
|
+
export const defaultAutoDeferAfterMs = 2000;
|
|
29
|
+
|
|
30
|
+
const normalize = (expression: string) =>
|
|
31
|
+
expression.trim().split(/\s+/).join(" ");
|
|
32
|
+
|
|
33
|
+
function hasTasks(
|
|
34
|
+
app: FetchApplication,
|
|
35
|
+
): app is FetchApplication & TaskApplication {
|
|
36
|
+
const candidate = app as Partial<TaskApplication>;
|
|
37
|
+
return (
|
|
38
|
+
Array.isArray(candidate.tasks) && typeof candidate.runTask === "function"
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function prepare(app: FetchApplication) {
|
|
43
|
+
applyHostDefaults(app, { autoDeferAfterMs: defaultAutoDeferAfterMs });
|
|
44
|
+
return app;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Wraps a `Discord` app (or a `(env) => app` factory for explicit Worker
|
|
49
|
+
* bindings) into a Cloudflare Module Worker exporting `fetch` and `scheduled`.
|
|
50
|
+
* Use the factory form only in a Worker you deploy yourself with Wrangler.
|
|
51
|
+
* `discord0 deploy` wraps the entry point's default export again, so a
|
|
52
|
+
* factory there never gets the bindings.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { cloudflare } from "@discord0/adapter-cloudflare";
|
|
57
|
+
* import app from "./index";
|
|
58
|
+
*
|
|
59
|
+
* export default cloudflare(app);
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @see [Cloudflare Workers adapter](https://discord0.babyak.dev/adapters/cloudflare/)
|
|
63
|
+
*/
|
|
64
|
+
export function cloudflare<Env = unknown>(
|
|
65
|
+
application: FetchApplication | ((env: Env) => FetchApplication),
|
|
66
|
+
) {
|
|
67
|
+
const build = (env: Env) =>
|
|
68
|
+
prepare(typeof application === "function" ? application(env) : application);
|
|
69
|
+
return {
|
|
70
|
+
fetch(request: Request, env: Env, execution: AppExecutionContext) {
|
|
71
|
+
return build(env).fetch(request, {
|
|
72
|
+
waitUntil: (promise) => execution.waitUntil(promise),
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
async scheduled(controller: ScheduledController, env: Env) {
|
|
76
|
+
const app = build(env);
|
|
77
|
+
const cron = normalize(controller.cron);
|
|
78
|
+
if (!hasTasks(app))
|
|
79
|
+
throw new Discord0Error(
|
|
80
|
+
"NO_SCHEDULED_TASK",
|
|
81
|
+
`A Cron Trigger "${cron}" fired, but the app has no scheduled tasks to run`,
|
|
82
|
+
);
|
|
83
|
+
const due = app.tasks.filter((task) => task.expression === cron);
|
|
84
|
+
if (!due.length)
|
|
85
|
+
throw new Discord0Error(
|
|
86
|
+
"NO_SCHEDULED_TASK",
|
|
87
|
+
`No scheduled task matches the Cron Trigger "${cron}"`,
|
|
88
|
+
{
|
|
89
|
+
fix: `Run discord0 deploy so the Cron Triggers match the app's tasks again (${app.tasks.map((task) => `${task.name}: ${task.expression}`).join(", ")}).`,
|
|
90
|
+
},
|
|
91
|
+
);
|
|
92
|
+
const scheduledTime = new Date(controller.scheduledTime);
|
|
93
|
+
// Run every matching task even if one fails, then report the failures.
|
|
94
|
+
const results = await Promise.allSettled(
|
|
95
|
+
due.map((task) => app.runTask(task.name, { scheduledTime })),
|
|
96
|
+
);
|
|
97
|
+
const errors = results.flatMap((result) =>
|
|
98
|
+
result.status === "rejected" ? [result.reason] : [],
|
|
99
|
+
);
|
|
100
|
+
if (errors.length === 1) throw errors[0];
|
|
101
|
+
if (errors.length)
|
|
102
|
+
throw new AggregateError(
|
|
103
|
+
errors,
|
|
104
|
+
`${errors.length} scheduled tasks failed`,
|
|
105
|
+
);
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export { default } from "./adapter";
|