Skip to content

Commit 7000dde

Browse files
committed
Initial commit (by create-cloudflare CLI)
Details: C3 = [email protected] project name = cloudflare-worker package manager = [email protected] wrangler = [email protected] git = 2.25.1
0 parents  commit 7000dde

9 files changed

+1893
-0
lines changed

.editorconfig

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# http://editorconfig.org
2+
root = true
3+
4+
[*]
5+
indent_style = tab
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.yml]
12+
indent_style = space

.gitignore

+172
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/

.prettierrc

+6
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+
}

package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "cloudflare-worker",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"deploy": "wrangler deploy",
7+
"dev": "wrangler dev",
8+
"start": "wrangler dev",
9+
"test": "vitest"
10+
},
11+
"devDependencies": {
12+
"wrangler": "^3.0.0",
13+
"vitest": "1.3.0",
14+
"@cloudflare/vitest-pool-workers": "^0.1.0"
15+
}
16+
}

src/index.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Welcome to Cloudflare Workers! This is your first worker.
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 worker in action
6+
* - Run `npm run deploy` to publish your worker
7+
*
8+
* Learn more at https://developers.cloudflare.com/workers/
9+
*/
10+
11+
export default {
12+
async fetch(request, env, ctx) {
13+
return new Response('Hello World!');
14+
},
15+
};

test/index.spec.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { env, createExecutionContext, waitOnExecutionContext, SELF } from 'cloudflare:test';
2+
import { describe, it, expect } from 'vitest';
3+
import worker from '../src';
4+
5+
describe('Hello World worker', () => {
6+
it('responds with Hello World! (unit style)', async () => {
7+
const request = new Request('http://example.com');
8+
// Create an empty context to pass to `worker.fetch()`.
9+
const ctx = createExecutionContext();
10+
const response = await worker.fetch(request, env, ctx);
11+
// Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions
12+
await waitOnExecutionContext(ctx);
13+
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
14+
});
15+
16+
it('responds with Hello World! (integration style)', async () => {
17+
const response = await SELF.fetch(request, env, ctx);
18+
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
19+
});
20+
});

vitest.config.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
2+
3+
export default defineWorkersConfig({
4+
test: {
5+
poolOptions: {
6+
workers: {
7+
wrangler: { configPath: "./wrangler.toml" },
8+
},
9+
},
10+
},
11+
});

wrangler.toml

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#:schema node_modules/wrangler/config-schema.json
2+
name = "cloudflare-worker"
3+
main = "src/index.js"
4+
compatibility_date = "2024-05-24"
5+
compatibility_flags = ["nodejs_compat"]
6+
7+
# Automatically place your workloads in an optimal location to minimize latency.
8+
# If you are running back-end logic in a Worker, running it closer to your back-end infrastructure
9+
# rather than the end user may result in better performance.
10+
# Docs: https://developers.cloudflare.com/workers/configuration/smart-placement/#smart-placement
11+
# [placement]
12+
# mode = "smart"
13+
14+
# Variable bindings. These are arbitrary, plaintext strings (similar to environment variables)
15+
# Docs:
16+
# - https://developers.cloudflare.com/workers/wrangler/configuration/#environment-variables
17+
# Note: Use secrets to store sensitive data.
18+
# - https://developers.cloudflare.com/workers/configuration/secrets/
19+
# [vars]
20+
# MY_VARIABLE = "production_value"
21+
22+
# Bind the Workers AI model catalog. Run machine learning models, powered by serverless GPUs, on Cloudflare’s global network
23+
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#workers-ai
24+
# [ai]
25+
# binding = "AI"
26+
27+
# Bind an Analytics Engine dataset. Use Analytics Engine to write analytics within your Pages Function.
28+
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#analytics-engine-datasets
29+
# [[analytics_engine_datasets]]
30+
# binding = "MY_DATASET"
31+
32+
# Bind a headless browser instance running on Cloudflare's global network.
33+
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#browser-rendering
34+
# [browser]
35+
# binding = "MY_BROWSER"
36+
37+
# Bind a D1 database. D1 is Cloudflare’s native serverless SQL database.
38+
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#d1-databases
39+
# [[d1_databases]]
40+
# binding = "MY_DB"
41+
# database_name = "my-database"
42+
# database_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
43+
44+
# Bind a dispatch namespace. Use Workers for Platforms to deploy serverless functions programmatically on behalf of your customers.
45+
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#dispatch-namespace-bindings-workers-for-platforms
46+
# [[dispatch_namespaces]]
47+
# binding = "MY_DISPATCHER"
48+
# namespace = "my-namespace"
49+
50+
# Bind a Durable Object. Durable objects are a scale-to-zero compute primitive based on the actor model.
51+
# Durable Objects can live for as long as needed. Use these when you need a long-running "server", such as in realtime apps.
52+
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#durable-objects
53+
# [[durable_objects.bindings]]
54+
# name = "MY_DURABLE_OBJECT"
55+
# class_name = "MyDurableObject"
56+
57+
# Durable Object migrations.
58+
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#migrations
59+
# [[migrations]]
60+
# tag = "v1"
61+
# new_classes = ["MyDurableObject"]
62+
63+
# Bind a Hyperdrive configuration. Use to accelerate access to your existing databases from Cloudflare Workers.
64+
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#hyperdrive
65+
# [[hyperdrive]]
66+
# binding = "MY_HYPERDRIVE"
67+
# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
68+
69+
# Bind a KV Namespace. Use KV as persistent storage for small key-value pairs.
70+
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#kv-namespaces
71+
# [[kv_namespaces]]
72+
# binding = "MY_KV_NAMESPACE"
73+
# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
74+
75+
# Bind an mTLS certificate. Use to present a client certificate when communicating with another service.
76+
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#mtls-certificates
77+
# [[mtls_certificates]]
78+
# binding = "MY_CERTIFICATE"
79+
# certificate_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
80+
81+
# Bind a Queue producer. Use this binding to schedule an arbitrary task that may be processed later by a Queue consumer.
82+
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#queues
83+
# [[queues.producers]]
84+
# binding = "MY_QUEUE"
85+
# queue = "my-queue"
86+
87+
# Bind a Queue consumer. Queue Consumers can retrieve tasks scheduled by Producers to act on them.
88+
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#queues
89+
# [[queues.consumers]]
90+
# queue = "my-queue"
91+
92+
# Bind an R2 Bucket. Use R2 to store arbitrarily large blobs of data, such as files.
93+
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#r2-buckets
94+
# [[r2_buckets]]
95+
# binding = "MY_BUCKET"
96+
# bucket_name = "my-bucket"
97+
98+
# Bind another Worker service. Use this binding to call another Worker without network overhead.
99+
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#service-bindings
100+
# [[services]]
101+
# binding = "MY_SERVICE"
102+
# service = "my-service"
103+
104+
# Bind a Vectorize index. Use to store and query vector embeddings for semantic search, classification and other vector search use-cases.
105+
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#vectorize-indexes
106+
# [[vectorize]]
107+
# binding = "MY_INDEX"
108+
# index_name = "my-index"

0 commit comments

Comments
 (0)