forked from cloudflare/templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (63 loc) · 1.65 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* @typedef Bindings
* @property {DurableObjectNamespace} COUNTER
*/
/**
* Worker
*/
export default {
/**
* @param {Request} req
* @param {Bindings} env
* @returns {Promise<Response>}
*/
async fetch(req, env) {
let id = env.COUNTER.idFromName('A');
let obj = env.COUNTER.get(id);
let resp = await obj.fetch(req.url);
let count = await resp.text();
return new Response("Durable Object 'A' count: " + count);
},
};
/**
* Durable Object
*/
export class Counter {
/**
* @param {DurableObjectState} state
*/
constructor(state) {
this.state = state;
}
/**
* Handle HTTP requests from clients.
* @param {Request} request
*/
async fetch(request) {
// Apply requested action.
let url = new URL(request.url);
// Durable Object storage is automatically cached in-memory, so reading the same key every request is fast.
// (That said, you could also store the value in a class member if you prefer.)
/** @type {number} */
let value = (await this.state.storage.get('value')) || 0;
switch (url.pathname) {
case '/increment':
++value;
break;
case '/decrement':
--value;
break;
case '/':
// Just serve the current value.
break;
default:
return new Response('Not found', { status: 404 });
}
// We don't have to worry about a concurrent request having modified the
// value in storage because "input gates" will automatically protect against
// unwanted concurrency. So, read-modify-write is safe. For more details,
// see: https://blog.cloudflare.com/durable-objects-easy-fast-correct-choose-three/
await this.state.storage.put('value', value);
return new Response('' + value);
}
}