Fixed durable objects example (#46)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 03d9dc8 · Browse files · 4 files·+14 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+14 added−11 removed
- other:+14 added−11 removed
Provenance
- Commit
a22a0eb- Tree
ce17aa9- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
03d9dc8
4 files changed+14 added−11 removed
modified:example/durable-objects/src/counter.ts+4 added-6 removed
123456678910111213141516151619171819202122252324252627282930313235333435import { Hono } from '../../../src/index'
export class Counter { value: number = 0 state: DurableObjectState app: Hono app: Hono = new Hono()
constructor(state: DurableObjectState) { this.state = state this.state.blockConcurrencyWhile(async () => { const stored = await this.state.storage?.get<number>('value') this.value = stored || 0 })
this.app = new Hono()
this.app.get('/increment', async (c) => { const currentValue = ++this.value this.state.storage?.put('value', this.value) await this.state.storage?.put('value', this.value) return c.text(currentValue.toString()) })
this.app.get('/decrement', async (c) => { const currentValue = --this.value this.state.storage?.put('value', this.value) await this.state.storage?.put('value', this.value) return c.text(currentValue.toString()) })
this.app.get('/', async (c) => { return c.text(this.value.toString()) }) }
async fetch(request: Request) { return this.app.dispatch(request) return this.app.fetch(request) }}modified:example/durable-objects/src/index.ts+7 added-2 removed
1234567894 unmodified lines1415161718192021222324252627282930312526import { Hono } from '../../../src/index'import type { Env } from '../../../src/index'
export { Counter } from './counter'
declare module '../../../src/index' { interface Env { COUNTER: DurableObjectNamespace }4 unmodified linesapp.get('*', async c => { const id = c.env.COUNTER.idFromName('A') const obj = c.env.COUNTER.get(id) const resp = await obj.fetch(c.req.url)
if(resp.status === 404) { return c.text('404 Not Found', 404) }
const count = parseInt(await resp.text()) return c.text(`Count is ${count}`)})
export default { fetch(request: Request, env: Env, event: FetchEvent) { return app.fetch(request, env, event) },}
modified:example/durable-objects/webpack.config.js+1 added-1 removed
12345667891020 unmodified linesconst path = require('path')
module.exports = { entry: './src/index.ts', output: { filename: 'dist/worker.mjs', filename: 'worker.mjs', libraryTarget: 'module', path: path.join(__dirname, 'dist'), }, devtool: 'cheap-module-source-map',20 unmodified linesmodified:example/durable-objects/wrangler.toml+2 added-2 removed
11 unmodified lines121314151616171819202122232311 unmodified lines[build.upload]# The "modules" upload format is required for all projects that export a Durable Objects classformat = "modules"dir = "dist"main = "./worker.mjs"main = "worker.mjs"
[durable_objects]bindings = [{name = "COUNTER", class_name = "Counter"}]
[[migrations]]tag = "v1" # Should be unique for each entrynew_classes = ["CounterTs"]new_classes = ["Counter"]