kenton
09/23/2021, 3:01 PMwes
09/23/2021, 3:46 PMworkers.api.error.durable_object_namespace_name_taken [API code: 10065]
brett
09/23/2021, 3:55 PMhttps://api.cloudflare.com/client/v4/accounts/$ACCOUNT/workers/durable_objects/namespaces
to list your namespaces -- you must have one with the same name from before? You can delete it via that API too by sending a DELETE
to https://api.cloudflare.com/client/v4/accounts/$ACCOUNT/workers/durable_objects/namespaces/$NAMESPACE_ID
wes
09/23/2021, 4:33 PMkalepail
09/23/2021, 4:49 PMwes
09/23/2021, 5:05 PMconstructor(state, env) {
this.state = state
// `blockConcurrencyWhile()` ensures no requests are delivered until
// initialization completes.
this.state.blockConcurrencyWhile(async () => {
let stored = await this.state.storage.get('value')
this.value = stored || 10
})
}
brett
09/23/2021, 5:11 PMbrett
09/23/2021, 5:11 PMwes
09/23/2021, 5:15 PMmodule.exports = class Counter {
constructor(state, env) {
this.state = state
// `blockConcurrencyWhile()` ensures no requests are delivered until
// initialization completes.
this.state.blockConcurrencyWhile(async () => {
let stored = await this.state.storage.get('value')
this.value = stored || 10
})
}
// Handle HTTP requests from clients.
async fetch(request, options) {
// Apply requested action.
let url = new URL(request.url)
let currentValue = this.value
const pathname = url.pathname.split('/')
const path = pathname[1]
const amount = pathname[2]
switch (path) {
case 'increment':
this.value += amount
currentValue = this.value
await this.state.storage.put('value', this.value)
break
case 'decrement':
this.value -= amount
currentValue = this.value
await this.state.storage.put('value', this.value)
break
case '':
// Just serve the current value. No storage calls needed!
break
default:
return new Response('Not found', { status: 404 })
}
// Return `currentValue`. Note that `this.value` may have been
// incremented or decremented by a concurrent request when we
// yielded the event loop to `await` the `storage.put` above!
// That's why we stored the counter value created by this
// request in `currentValue` before we used `await`.
return new Response(currentValue)
}
}
wes
09/23/2021, 5:18 PMbrett
09/23/2021, 5:20 PMbrett
09/23/2021, 5:20 PMwes
09/23/2021, 5:27 PMwes
09/23/2021, 5:27 PMasync function handleCount(url, env) {
let id = env.COUNTER.idFromName('A');
let obj = env.COUNTER.get(id);
let resp = await obj.fetch(url);
let count = parseInt(await resp.text());
return count;
}
wes
09/23/2021, 5:27 PMwes
09/23/2021, 5:28 PMbrett
09/23/2021, 5:49 PMbrett
09/23/2021, 5:55 PMvalue
is undefined because you don't send a proper number. It seems like you depend on JS converting a string to a number, but 3 + undefined
gives NaN
which could be persisted to storage, and then on the next constructor call you might fall into the || 10
, I think? Or at least it feels like something fuzzy around that. I would start by being more strict about what value
iswes
09/23/2021, 6:04 PMHardAtWork
09/23/2021, 6:09 PMwes
09/23/2021, 6:10 PMwes
09/23/2021, 6:10 PMwes
09/23/2021, 6:18 PMDeleted User
09/23/2021, 7:41 PMDeleted User
09/23/2021, 7:41 PMHardAtWork
09/23/2021, 7:47 PMDeleted User
09/23/2021, 8:05 PMHardAtWork
09/23/2021, 8:51 PMDeleted User
09/23/2021, 8:51 PMronan(wighawag)
09/24/2021, 9:15 AM