create-cloudflare 2.27.2 → 2.28.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/dist/cli.js +28242 -27304
- package/package.json +4 -3
- package/templates/hello-world-durable-object/ts/src/index.ts +0 -21
- package/templates/hello-world-durable-object/ts/worker-configuration.d.ts +3 -4
- package/templates/next/c3.ts +6 -7
- package/templates/pre-existing/c3.ts +2 -2
- package/templates/qwik/c3.ts +3 -3
- package/templates/qwik/templates/wrangler.toml +1 -0
- package/templates/react/c3.ts +2 -2
- package/templates-experimental/hello-world-assets-only/c3.ts +15 -0
- package/templates-experimental/hello-world-assets-only/templates/package.json +13 -0
- package/templates-experimental/hello-world-assets-only/templates/public/index.html +11 -0
- package/templates-experimental/hello-world-assets-only/templates/wrangler.toml +4 -0
- package/templates-experimental/hello-world-durable-object-with-assets/c3.ts +23 -0
- package/templates-experimental/hello-world-durable-object-with-assets/js/.editorconfig +12 -0
- package/templates-experimental/hello-world-durable-object-with-assets/js/.prettierrc +6 -0
- package/templates-experimental/hello-world-durable-object-with-assets/js/__dot__gitignore +172 -0
- package/templates-experimental/hello-world-durable-object-with-assets/js/package.json +13 -0
- package/templates-experimental/hello-world-durable-object-with-assets/js/public/index.html +19 -0
- package/templates-experimental/hello-world-durable-object-with-assets/js/src/index.js +69 -0
- package/templates-experimental/hello-world-durable-object-with-assets/js/wrangler.toml +19 -0
- package/templates-experimental/hello-world-durable-object-with-assets/ts/.editorconfig +12 -0
- package/templates-experimental/hello-world-durable-object-with-assets/ts/.prettierrc +6 -0
- package/templates-experimental/hello-world-durable-object-with-assets/ts/__dot__gitignore +172 -0
- package/templates-experimental/hello-world-durable-object-with-assets/ts/package.json +15 -0
- package/templates-experimental/hello-world-durable-object-with-assets/ts/public/index.html +19 -0
- package/templates-experimental/hello-world-durable-object-with-assets/ts/src/index.ts +66 -0
- package/templates-experimental/hello-world-durable-object-with-assets/ts/tsconfig.json +101 -0
- package/templates-experimental/hello-world-durable-object-with-assets/ts/worker-configuration.d.ts +5 -0
- package/templates-experimental/hello-world-durable-object-with-assets/ts/wrangler.toml +19 -0
- package/templates-experimental/hello-world-with-assets/c3.ts +2 -2
- package/templates-experimental/hello-world-with-assets/js/public/index.html +13 -1
- package/templates-experimental/hello-world-with-assets/js/src/index.js +9 -1
- package/templates-experimental/hello-world-with-assets/js/test/index.spec.js +34 -13
- package/templates-experimental/hello-world-with-assets/py/public/index.html +14 -2
- package/templates-experimental/hello-world-with-assets/py/src/entry.py +7 -2
- package/templates-experimental/hello-world-with-assets/ts/public/index.html +13 -1
- package/templates-experimental/hello-world-with-assets/ts/src/index.ts +10 -2
- package/templates-experimental/hello-world-with-assets/ts/test/index.spec.ts +34 -18
- package/templates-experimental/qwik/c3.ts +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "create-cloudflare",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.28.0",
|
|
4
4
|
"description": "A CLI for creating and deploying new applications to Cloudflare.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
@@ -64,11 +64,12 @@
|
|
|
64
64
|
"vite-tsconfig-paths": "^4.0.8",
|
|
65
65
|
"which-pm-runs": "^1.1.0",
|
|
66
66
|
"wrap-ansi": "^9.0.0",
|
|
67
|
+
"xdg-app-paths": "^8.3.0",
|
|
67
68
|
"yargs": "^17.7.2",
|
|
69
|
+
"@cloudflare/cli": "1.1.1",
|
|
68
70
|
"@cloudflare/eslint-config-worker": "1.1.0",
|
|
69
71
|
"@cloudflare/workers-tsconfig": "0.0.0",
|
|
70
|
-
"wrangler": "3.78.
|
|
71
|
-
"@cloudflare/cli": "1.1.1"
|
|
72
|
+
"wrangler": "3.78.8"
|
|
72
73
|
},
|
|
73
74
|
"engines": {
|
|
74
75
|
"node": ">=18.14.1"
|
|
@@ -13,27 +13,6 @@ import { DurableObject } from "cloudflare:workers";
|
|
|
13
13
|
* Learn more at https://developers.cloudflare.com/durable-objects
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Associate bindings declared in wrangler.toml with the TypeScript type system
|
|
19
|
-
*/
|
|
20
|
-
export interface Env {
|
|
21
|
-
// Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/
|
|
22
|
-
// MY_KV_NAMESPACE: KVNamespace;
|
|
23
|
-
//
|
|
24
|
-
// Example binding to Durable Object. Learn more at https://developers.cloudflare.com/workers/runtime-apis/durable-objects/
|
|
25
|
-
MY_DURABLE_OBJECT: DurableObjectNamespace<MyDurableObject>;
|
|
26
|
-
//
|
|
27
|
-
// Example binding to R2. Learn more at https://developers.cloudflare.com/workers/runtime-apis/r2/
|
|
28
|
-
// MY_BUCKET: R2Bucket;
|
|
29
|
-
//
|
|
30
|
-
// Example binding to a Service. Learn more at https://developers.cloudflare.com/workers/runtime-apis/service-bindings/
|
|
31
|
-
// MY_SERVICE: Fetcher;
|
|
32
|
-
//
|
|
33
|
-
// Example binding to a Queue. Learn more at https://developers.cloudflare.com/queues/javascript-apis/
|
|
34
|
-
// MY_QUEUE: Queue;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
16
|
/** A Durable Object's behavior is defined in an exported Javascript class */
|
|
38
17
|
export class MyDurableObject extends DurableObject {
|
|
39
18
|
/**
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
// Generated by Wrangler
|
|
2
|
-
|
|
1
|
+
// Generated by Wrangler by running `wrangler types`
|
|
2
|
+
|
|
3
3
|
interface Env {
|
|
4
|
-
MY_DURABLE_OBJECT: DurableObjectNamespace
|
|
4
|
+
MY_DURABLE_OBJECT: DurableObjectNamespace<import("./src/index").MyDurableObject>;
|
|
5
5
|
}
|
|
6
|
-
|
package/templates/next/c3.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { join } from "path";
|
|
2
|
-
import {
|
|
3
|
-
import { processArgument } from "@cloudflare/cli/args";
|
|
2
|
+
import { updateStatus, warn } from "@cloudflare/cli";
|
|
4
3
|
import { brandColor, dim } from "@cloudflare/cli/colors";
|
|
5
|
-
import { spinner } from "@cloudflare/cli/interactive";
|
|
4
|
+
import { inputPrompt, spinner } from "@cloudflare/cli/interactive";
|
|
6
5
|
import { runFrameworkGenerator } from "frameworks/index";
|
|
7
6
|
import {
|
|
8
7
|
copyFile,
|
|
@@ -18,7 +17,7 @@ import { detectPackageManager } from "helpers/packageManagers";
|
|
|
18
17
|
import { installPackages } from "helpers/packages";
|
|
19
18
|
import { getTemplatePath } from "../../src/templates";
|
|
20
19
|
import type { TemplateConfig } from "../../src/templates";
|
|
21
|
-
import type {
|
|
20
|
+
import type { C3Context } from "types";
|
|
22
21
|
|
|
23
22
|
const { npm, npx } = detectPackageManager();
|
|
24
23
|
|
|
@@ -42,7 +41,7 @@ const generate = async (ctx: C3Context) => {
|
|
|
42
41
|
// This should never happen to users, it is a check mostly so that
|
|
43
42
|
// if the toml file is changed in a way that breaks the "KV Example" addition
|
|
44
43
|
// the C3 Next.js e2e runs will fail with this
|
|
45
|
-
|
|
44
|
+
throw new Error("Failed to properly generate the wrangler.toml file");
|
|
46
45
|
}
|
|
47
46
|
|
|
48
47
|
writeFile(join(ctx.project.path, "wrangler.toml"), newTomlContent);
|
|
@@ -89,7 +88,7 @@ const configure = async (ctx: C3Context) => {
|
|
|
89
88
|
]);
|
|
90
89
|
|
|
91
90
|
if (!path) {
|
|
92
|
-
|
|
91
|
+
throw new Error("Could not find the `/api` or `/app` directory");
|
|
93
92
|
}
|
|
94
93
|
|
|
95
94
|
const usesTs = usesTypescript(ctx);
|
|
@@ -135,7 +134,7 @@ export const shouldInstallNextOnPagesEslintPlugin = async (
|
|
|
135
134
|
return false;
|
|
136
135
|
}
|
|
137
136
|
|
|
138
|
-
return await
|
|
137
|
+
return await inputPrompt({
|
|
139
138
|
type: "confirm",
|
|
140
139
|
question: "Do you want to use the next-on-pages eslint-plugin?",
|
|
141
140
|
label: "eslint-plugin",
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { cp, mkdtemp } from "fs/promises";
|
|
2
2
|
import { tmpdir } from "os";
|
|
3
3
|
import { join } from "path";
|
|
4
|
-
import { processArgument } from "@cloudflare/cli/args";
|
|
5
4
|
import { brandColor, dim } from "@cloudflare/cli/colors";
|
|
5
|
+
import { processArgument } from "helpers/args";
|
|
6
6
|
import { runCommand } from "helpers/command";
|
|
7
7
|
import { detectPackageManager } from "helpers/packageManagers";
|
|
8
8
|
import { chooseAccount } from "../../src/wrangler/accounts";
|
|
@@ -14,7 +14,7 @@ export async function copyExistingWorkerFiles(ctx: C3Context) {
|
|
|
14
14
|
await chooseAccount(ctx);
|
|
15
15
|
|
|
16
16
|
if (ctx.args.existingScript === undefined) {
|
|
17
|
-
ctx.args.existingScript = await processArgument
|
|
17
|
+
ctx.args.existingScript = await processArgument(
|
|
18
18
|
ctx.args,
|
|
19
19
|
"existingScript",
|
|
20
20
|
{
|
package/templates/qwik/c3.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { endSection } from "@cloudflare/cli";
|
|
2
2
|
import { brandColor } from "@cloudflare/cli/colors";
|
|
3
3
|
import { spinner } from "@cloudflare/cli/interactive";
|
|
4
4
|
import { runFrameworkGenerator } from "frameworks/index";
|
|
@@ -13,7 +13,7 @@ import type { C3Context } from "types";
|
|
|
13
13
|
const { npm, npx } = detectPackageManager();
|
|
14
14
|
|
|
15
15
|
const generate = async (ctx: C3Context) => {
|
|
16
|
-
await runFrameworkGenerator(ctx, ["
|
|
16
|
+
await runFrameworkGenerator(ctx, ["playground", ctx.project.name]);
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
const configure = async (ctx: C3Context) => {
|
|
@@ -75,7 +75,7 @@ const addBindingsProxy = (ctx: C3Context) => {
|
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
if (configArgument.type !== "ObjectExpression") {
|
|
78
|
-
|
|
78
|
+
throw new Error("Failed to update `vite.config.ts`");
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
// Add the `platform` object to the object
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
name = "<TBD>"
|
|
3
3
|
compatibility_date = "<TBD>"
|
|
4
4
|
pages_build_output_dir = "./dist"
|
|
5
|
+
compatibility_flags = ["nodejs_compat"]
|
|
5
6
|
|
|
6
7
|
# Automatically place your workloads in an optimal location to minimize latency.
|
|
7
8
|
# If you are running back-end logic in a Pages Function, running it closer to your back-end infrastructure
|
package/templates/react/c3.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { logRaw } from "@cloudflare/cli";
|
|
2
|
-
import {
|
|
2
|
+
import { inputPrompt } from "@cloudflare/cli/interactive";
|
|
3
3
|
import { runFrameworkGenerator } from "frameworks/index";
|
|
4
4
|
import { detectPackageManager } from "helpers/packageManagers";
|
|
5
5
|
import type { TemplateConfig } from "../../src/templates";
|
|
@@ -8,7 +8,7 @@ import type { C3Context } from "types";
|
|
|
8
8
|
const { npm } = detectPackageManager();
|
|
9
9
|
|
|
10
10
|
const generate = async (ctx: C3Context) => {
|
|
11
|
-
const variant = await
|
|
11
|
+
const variant = await inputPrompt({
|
|
12
12
|
type: "select",
|
|
13
13
|
question: "Select a variant:",
|
|
14
14
|
label: "variant",
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { TemplateConfig } from "../../src/templates";
|
|
2
|
+
|
|
3
|
+
const config: TemplateConfig = {
|
|
4
|
+
configVersion: 1,
|
|
5
|
+
id: "hello-world-assets-only",
|
|
6
|
+
path: "templates-experimental/hello-world-assets-only",
|
|
7
|
+
displayName: "Hello World - Assets-only",
|
|
8
|
+
description: "Get started with a basic Worker that only serves static assets",
|
|
9
|
+
platform: "workers",
|
|
10
|
+
copyFiles: {
|
|
11
|
+
path: "./templates",
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export default config;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { TemplateConfig } from "../../src/templates";
|
|
2
|
+
|
|
3
|
+
const config: TemplateConfig = {
|
|
4
|
+
configVersion: 1,
|
|
5
|
+
id: "hello-world-durable-object-with-assets",
|
|
6
|
+
path: "templates-experimental/hello-world-durable-object-with-assets",
|
|
7
|
+
displayName: "Hello World - Worker Using Durable Objects with Assets",
|
|
8
|
+
description:
|
|
9
|
+
"Get started with a basic stateful app to build projects like real-time chats, collaborative apps, and multiplayer games, which hosts assets",
|
|
10
|
+
platform: "workers",
|
|
11
|
+
copyFiles: {
|
|
12
|
+
variants: {
|
|
13
|
+
js: {
|
|
14
|
+
path: "./js",
|
|
15
|
+
},
|
|
16
|
+
ts: {
|
|
17
|
+
path: "./ts",
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export default config;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# Logs
|
|
2
|
+
|
|
3
|
+
logs
|
|
4
|
+
_.log
|
|
5
|
+
npm-debug.log_
|
|
6
|
+
yarn-debug.log*
|
|
7
|
+
yarn-error.log*
|
|
8
|
+
lerna-debug.log*
|
|
9
|
+
.pnpm-debug.log*
|
|
10
|
+
|
|
11
|
+
# Diagnostic reports (https://nodejs.org/api/report.html)
|
|
12
|
+
|
|
13
|
+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
|
14
|
+
|
|
15
|
+
# Runtime data
|
|
16
|
+
|
|
17
|
+
pids
|
|
18
|
+
_.pid
|
|
19
|
+
_.seed
|
|
20
|
+
\*.pid.lock
|
|
21
|
+
|
|
22
|
+
# Directory for instrumented libs generated by jscoverage/JSCover
|
|
23
|
+
|
|
24
|
+
lib-cov
|
|
25
|
+
|
|
26
|
+
# Coverage directory used by tools like istanbul
|
|
27
|
+
|
|
28
|
+
coverage
|
|
29
|
+
\*.lcov
|
|
30
|
+
|
|
31
|
+
# nyc test coverage
|
|
32
|
+
|
|
33
|
+
.nyc_output
|
|
34
|
+
|
|
35
|
+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
|
36
|
+
|
|
37
|
+
.grunt
|
|
38
|
+
|
|
39
|
+
# Bower dependency directory (https://bower.io/)
|
|
40
|
+
|
|
41
|
+
bower_components
|
|
42
|
+
|
|
43
|
+
# node-waf configuration
|
|
44
|
+
|
|
45
|
+
.lock-wscript
|
|
46
|
+
|
|
47
|
+
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
|
48
|
+
|
|
49
|
+
build/Release
|
|
50
|
+
|
|
51
|
+
# Dependency directories
|
|
52
|
+
|
|
53
|
+
node_modules/
|
|
54
|
+
jspm_packages/
|
|
55
|
+
|
|
56
|
+
# Snowpack dependency directory (https://snowpack.dev/)
|
|
57
|
+
|
|
58
|
+
web_modules/
|
|
59
|
+
|
|
60
|
+
# TypeScript cache
|
|
61
|
+
|
|
62
|
+
\*.tsbuildinfo
|
|
63
|
+
|
|
64
|
+
# Optional npm cache directory
|
|
65
|
+
|
|
66
|
+
.npm
|
|
67
|
+
|
|
68
|
+
# Optional eslint cache
|
|
69
|
+
|
|
70
|
+
.eslintcache
|
|
71
|
+
|
|
72
|
+
# Optional stylelint cache
|
|
73
|
+
|
|
74
|
+
.stylelintcache
|
|
75
|
+
|
|
76
|
+
# Microbundle cache
|
|
77
|
+
|
|
78
|
+
.rpt2_cache/
|
|
79
|
+
.rts2_cache_cjs/
|
|
80
|
+
.rts2_cache_es/
|
|
81
|
+
.rts2_cache_umd/
|
|
82
|
+
|
|
83
|
+
# Optional REPL history
|
|
84
|
+
|
|
85
|
+
.node_repl_history
|
|
86
|
+
|
|
87
|
+
# Output of 'npm pack'
|
|
88
|
+
|
|
89
|
+
\*.tgz
|
|
90
|
+
|
|
91
|
+
# Yarn Integrity file
|
|
92
|
+
|
|
93
|
+
.yarn-integrity
|
|
94
|
+
|
|
95
|
+
# dotenv environment variable files
|
|
96
|
+
|
|
97
|
+
.env
|
|
98
|
+
.env.development.local
|
|
99
|
+
.env.test.local
|
|
100
|
+
.env.production.local
|
|
101
|
+
.env.local
|
|
102
|
+
|
|
103
|
+
# parcel-bundler cache (https://parceljs.org/)
|
|
104
|
+
|
|
105
|
+
.cache
|
|
106
|
+
.parcel-cache
|
|
107
|
+
|
|
108
|
+
# Next.js build output
|
|
109
|
+
|
|
110
|
+
.next
|
|
111
|
+
out
|
|
112
|
+
|
|
113
|
+
# Nuxt.js build / generate output
|
|
114
|
+
|
|
115
|
+
.nuxt
|
|
116
|
+
dist
|
|
117
|
+
|
|
118
|
+
# Gatsby files
|
|
119
|
+
|
|
120
|
+
.cache/
|
|
121
|
+
|
|
122
|
+
# Comment in the public line in if your project uses Gatsby and not Next.js
|
|
123
|
+
|
|
124
|
+
# https://nextjs.org/blog/next-9-1#public-directory-support
|
|
125
|
+
|
|
126
|
+
# public
|
|
127
|
+
|
|
128
|
+
# vuepress build output
|
|
129
|
+
|
|
130
|
+
.vuepress/dist
|
|
131
|
+
|
|
132
|
+
# vuepress v2.x temp and cache directory
|
|
133
|
+
|
|
134
|
+
.temp
|
|
135
|
+
.cache
|
|
136
|
+
|
|
137
|
+
# Docusaurus cache and generated files
|
|
138
|
+
|
|
139
|
+
.docusaurus
|
|
140
|
+
|
|
141
|
+
# Serverless directories
|
|
142
|
+
|
|
143
|
+
.serverless/
|
|
144
|
+
|
|
145
|
+
# FuseBox cache
|
|
146
|
+
|
|
147
|
+
.fusebox/
|
|
148
|
+
|
|
149
|
+
# DynamoDB Local files
|
|
150
|
+
|
|
151
|
+
.dynamodb/
|
|
152
|
+
|
|
153
|
+
# TernJS port file
|
|
154
|
+
|
|
155
|
+
.tern-port
|
|
156
|
+
|
|
157
|
+
# Stores VSCode versions used for testing VSCode extensions
|
|
158
|
+
|
|
159
|
+
.vscode-test
|
|
160
|
+
|
|
161
|
+
# yarn v2
|
|
162
|
+
|
|
163
|
+
.yarn/cache
|
|
164
|
+
.yarn/unplugged
|
|
165
|
+
.yarn/build-state.yml
|
|
166
|
+
.yarn/install-state.gz
|
|
167
|
+
.pnp.\*
|
|
168
|
+
|
|
169
|
+
# wrangler project
|
|
170
|
+
|
|
171
|
+
.dev.vars
|
|
172
|
+
.wrangler/
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>Hello, World!</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<h1 id="heading"></h1>
|
|
10
|
+
<script>
|
|
11
|
+
fetch('/message')
|
|
12
|
+
.then((resp) => resp.text())
|
|
13
|
+
.then((text) => {
|
|
14
|
+
const h1 = document.getElementById('heading');
|
|
15
|
+
h1.textContent = text;
|
|
16
|
+
});
|
|
17
|
+
</script>
|
|
18
|
+
</body>
|
|
19
|
+
</html>
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { DurableObject } from "cloudflare:workers";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Welcome to Cloudflare Workers! This is your first Durable Objects application.
|
|
5
|
+
*
|
|
6
|
+
* - Run `npm run dev` in your terminal to start a development server
|
|
7
|
+
* - Open a browser tab at http://localhost:8787/ to see your Durable Object in action
|
|
8
|
+
* - Run `npm run deploy` to publish your application
|
|
9
|
+
*
|
|
10
|
+
* Learn more at https://developers.cloudflare.com/durable-objects
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Env provides a mechanism to reference bindings declared in wrangler.toml within JavaScript
|
|
15
|
+
*
|
|
16
|
+
* @typedef {Object} Env
|
|
17
|
+
* @property {DurableObjectNamespace} MY_DURABLE_OBJECT - The Durable Object namespace binding
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** A Durable Object's behavior is defined in an exported Javascript class */
|
|
21
|
+
export class MyDurableObject extends DurableObject {
|
|
22
|
+
/**
|
|
23
|
+
* The constructor is invoked once upon creation of the Durable Object, i.e. the first call to
|
|
24
|
+
* `DurableObjectStub::get` for a given identifier (no-op constructors can be omitted)
|
|
25
|
+
*
|
|
26
|
+
* @param {DurableObjectState} ctx - The interface for interacting with Durable Object state
|
|
27
|
+
* @param {Env} env - The interface to reference bindings declared in wrangler.toml
|
|
28
|
+
*/
|
|
29
|
+
constructor(ctx, env) {
|
|
30
|
+
super(ctx, env);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The Durable Object exposes an RPC method sayHello which will be invoked when when a Durable
|
|
35
|
+
* Object instance receives a request from a Worker via the same method invocation on the stub
|
|
36
|
+
*
|
|
37
|
+
* @param {string} name - The name provided to a Durable Object instance from a Worker
|
|
38
|
+
* @returns {Promise<string>} The greeting to be sent back to the Worker
|
|
39
|
+
*/
|
|
40
|
+
async sayHello(name) {
|
|
41
|
+
return `Hello, ${name}!`;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export default {
|
|
46
|
+
/**
|
|
47
|
+
* This is the standard fetch handler for a Cloudflare Worker
|
|
48
|
+
*
|
|
49
|
+
* @param {Request} request - The request submitted to the Worker from the client
|
|
50
|
+
* @param {Env} env - The interface to reference bindings declared in wrangler.toml
|
|
51
|
+
* @param {ExecutionContext} ctx - The execution context of the Worker
|
|
52
|
+
* @returns {Promise<Response>} The response to be sent back to the client
|
|
53
|
+
*/
|
|
54
|
+
async fetch(request, env, ctx) {
|
|
55
|
+
// We will create a `DurableObjectId` using the pathname from the Worker request
|
|
56
|
+
// This id refers to a unique instance of our 'MyDurableObject' class above
|
|
57
|
+
let id = env.MY_DURABLE_OBJECT.idFromName(new URL(request.url).pathname);
|
|
58
|
+
|
|
59
|
+
// This stub creates a communication channel with the Durable Object instance
|
|
60
|
+
// The Durable Object constructor will be invoked upon the first call for a given id
|
|
61
|
+
let stub = env.MY_DURABLE_OBJECT.get(id);
|
|
62
|
+
|
|
63
|
+
// We call the `sayHello()` RPC method on the stub to invoke the method on the remote
|
|
64
|
+
// Durable Object instance
|
|
65
|
+
let greeting = await stub.sayHello("world");
|
|
66
|
+
|
|
67
|
+
return new Response(greeting);
|
|
68
|
+
},
|
|
69
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
#:schema node_modules/wrangler/config-schema.json
|
|
2
|
+
name = "<TBD>"
|
|
3
|
+
main = "src/index.js"
|
|
4
|
+
compatibility_date = "<TBD>"
|
|
5
|
+
experimental_assets = { directory = "./public", binding = "ASSETS" }
|
|
6
|
+
|
|
7
|
+
# Bind a Durable Object. Durable objects are a scale-to-zero compute primitive based on the actor model.
|
|
8
|
+
# Durable Objects can live for as long as needed. Use these when you need a long-running "server", such as in realtime apps.
|
|
9
|
+
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects
|
|
10
|
+
[[durable_objects.bindings]]
|
|
11
|
+
name = "MY_DURABLE_OBJECT"
|
|
12
|
+
class_name = "MyDurableObject"
|
|
13
|
+
|
|
14
|
+
# Durable Object migrations.
|
|
15
|
+
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#migrations
|
|
16
|
+
[[migrations]]
|
|
17
|
+
tag = "v1"
|
|
18
|
+
new_classes = ["MyDurableObject"]
|
|
19
|
+
|