Skip to content

Commit 69bc7d5

Browse files
authored
Add hello world example for Durable Objects (#4747)
1 parent 9f96f28 commit 69bc7d5

File tree

15 files changed

+761
-1
lines changed

15 files changed

+761
-1
lines changed

packages/create-cloudflare/e2e-tests/cli.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ describe.skipIf(frameworkToTest || isQuarantineMode())(
107107
},
108108
{
109109
matcher: /What type of application do you want to create/,
110-
input: [keys.down, keys.down, keys.enter],
110+
input: [keys.down, keys.down, keys.down, keys.enter],
111111
},
112112
{
113113
matcher: /Do you want to use TypeScript/,

packages/create-cloudflare/src/templateMap.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ export const templateMap: Record<string, TemplateConfig> = {
1313
label: `"Hello World" Worker`,
1414
handler: runWorkersGenerator,
1515
},
16+
"hello-world-durable-object": {
17+
label: `"Hello World" Durable Object`,
18+
handler: runWorkersGenerator,
19+
},
1620
webFramework: {
1721
label: "Website or web app",
1822
handler: runPagesGenerator,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = tab
6+
tab_width = 2
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.yml]
13+
indent_style = space
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"printWidth": 140,
3+
"singleQuote": true,
4+
"semi": true,
5+
"useTabs": true
6+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "<TBD>",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"deploy": "wrangler deploy",
7+
"dev": "wrangler dev",
8+
"start": "wrangler dev"
9+
},
10+
"devDependencies": {
11+
"wrangler": "^3.0.0"
12+
}
13+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Welcome to Cloudflare Workers! This is your first Durable Objects application.
3+
*
4+
* - Run `npm run dev` in your terminal to start a development server
5+
* - Open a browser tab at http://localhost:8787/ to see your Durable Object in action
6+
* - Run `npm run deploy` to publish your application
7+
*
8+
* Learn more at https://developers.cloudflare.com/durable-objects
9+
*/
10+
11+
/**
12+
* Env provides a mechanism to reference bindings declared in wrangler.toml within JavaScript
13+
*
14+
* @typedef {Object} Env
15+
* @property {DurableObjectNamespace} MY_DURABLE_OBJECT - The Durable Object namespace binding
16+
*/
17+
18+
/** A Durable Object's behavior is defined in an exported Javascript class */
19+
export class MyDurableObject {
20+
/**
21+
* The constructor is invoked once upon creation of the Durable Object, i.e. the first call to
22+
* `DurableObjectStub::get` for a given identifier
23+
*
24+
* @param {DurableObjectState} state - The interface for interacting with Durable Object state
25+
* @param {Env} env - The interface to reference bindings declared in wrangler.toml
26+
*/
27+
constructor(state, env) {}
28+
29+
/**
30+
* The Durable Object fetch handler will be invoked when a Durable Object instance receives a
31+
* request from a Worker via an associated stub
32+
*
33+
* @param {Request} request - The request submitted to a Durable Object instance from a Worker
34+
* @returns {Promise<Response>} The response to be sent back to the Worker
35+
*/
36+
async fetch(request) {
37+
return new Response('Hello World');
38+
}
39+
}
40+
41+
export default {
42+
/**
43+
* This is the standard fetch handler for a Cloudflare Worker
44+
*
45+
* @param {Request} request - The request submitted to the Worker from the client
46+
* @param {Env} env - The interface to reference bindings declared in wrangler.toml
47+
* @param {ExecutionContext} ctx - The execution context of the Worker
48+
* @returns {Promise<Response>} The response to be sent back to the client
49+
*/
50+
async fetch(request, env, ctx) {
51+
// We will create a `DurableObjectId` using the pathname from the Worker request
52+
// This id refers to a unique instance of our 'MyDurableObject' class above
53+
let id = env.MY_DURABLE_OBJECT.idFromName(new URL(request.url).pathname);
54+
55+
// This stub creates a communication channel with the Durable Object instance
56+
// The Durable Object constructor will be invoked upon the first call for a given id
57+
let stub = env.MY_DURABLE_OBJECT.get(id);
58+
59+
// We call `fetch()` on the stub to send a request to the Durable Object instance
60+
// The Durable Object instance will invoke its fetch handler to handle the request
61+
let response = await stub.fetch(request);
62+
63+
return response;
64+
},
65+
};
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name = "<TBD>"
2+
main = "src/index.js"
3+
compatibility_date = "<TBD>"
4+
5+
# Variable bindings. These are arbitrary, plaintext strings (similar to environment variables)
6+
# Note: Use secrets to store sensitive data.
7+
# Docs: https://developers.cloudflare.com/workers/platform/environment-variables
8+
# [vars]
9+
# MY_VARIABLE = "production_value"
10+
11+
# Bind a KV Namespace. Use KV as persistent storage for small key-value pairs.
12+
# Docs: https://developers.cloudflare.com/workers/runtime-apis/kv
13+
# [[kv_namespaces]]
14+
# binding = "MY_KV_NAMESPACE"
15+
# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
16+
17+
# Bind an R2 Bucket. Use R2 to store arbitrarily large blobs of data, such as files.
18+
# Docs: https://developers.cloudflare.com/r2/api/workers/workers-api-usage/
19+
# [[r2_buckets]]
20+
# binding = "MY_BUCKET"
21+
# bucket_name = "my-bucket"
22+
23+
# Bind a Queue producer. Use this binding to schedule an arbitrary task that may be processed later by a Queue consumer.
24+
# Docs: https://developers.cloudflare.com/queues/get-started
25+
# [[queues.producers]]
26+
# binding = "MY_QUEUE"
27+
# queue = "my-queue"
28+
29+
# Bind a Queue consumer. Queue Consumers can retrieve tasks scheduled by Producers to act on them.
30+
# Docs: https://developers.cloudflare.com/queues/get-started
31+
# [[queues.consumers]]
32+
# queue = "my-queue"
33+
34+
# Bind another Worker service. Use this binding to call another Worker without network overhead.
35+
# Docs: https://developers.cloudflare.com/workers/platform/services
36+
# [[services]]
37+
# binding = "MY_SERVICE"
38+
# service = "my-service"
39+
40+
# Bind a Durable Object. Durable objects are a scale-to-zero compute primitive based on the actor model.
41+
# Durable Objects can live for as long as needed. Use these when you need a long-running "server", such as in realtime apps.
42+
# Docs: https://developers.cloudflare.com/workers/runtime-apis/durable-objects
43+
[[durable_objects.bindings]]
44+
name = "MY_DURABLE_OBJECT"
45+
class_name = "MyDurableObject"
46+
47+
# Durable Object migrations.
48+
# Docs: https://developers.cloudflare.com/workers/learning/using-durable-objects#configure-durable-object-classes-with-migrations
49+
[[migrations]]
50+
tag = "v1"
51+
new_classes = ["MyDurableObject"]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = tab
6+
tab_width = 2
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
12+
[*.yml]
13+
indent_style = space

0 commit comments

Comments
 (0)