/durable-objects-webpack-commonjs

Template for durable objects, webpack, commonjs

Primary LanguageJavaScriptApache License 2.0Apache-2.0

👷 Durable Objects Counter template

Note: You must use wrangler 2.0.0 or newer to use this template.

Please read the Durable Object documentation before using this template.

A template for kick starting a Cloudflare Workers project using:

  • Durable Objects
  • Modules (commonjs modules to be specific)
  • Webpack
  • Wrangler

Worker code is in src/. The Durable Object Counter class is in src/counter.js, and the eyeball script is in index.js.

Webpack is configured to output a bundled CommonJS Module to dist/index.js.

This project uses a shim ES module at src/shim.mjs that imports the commonjs bundle, and re-exports it. This is necessary because commonjs does not support named exports, and the only way to export a durable object class is using named exports.

Once you have published the worker, you can interact with it as follows:

bash-3.2$ curl worker.your-account-name.workers.dev/
Select a Durable Object to contact by using the `name` URL query string parameter. e.g. ?name=A
bash-3.2$ curl worker.your-account-name.workers.dev/?name=A
Durable Object 'A' 0 is even
bash-3.2$ curl worker.your-account-name.workers.dev/increment?name=A
Durable Object 'A' 1 is odd
bash-3.2$ curl worker.your-account-name.workers.dev/increment?name=A
Durable Object 'A' 2 is even
bash-3.2$ curl worker.your-account-name.workers.dev/decrement?name=A
Durable Object 'A' 1 is odd