Gathering detailed insights and metrics for itty-durable
Gathering detailed insights and metrics for itty-durable
Gathering detailed insights and metrics for itty-durable
Gathering detailed insights and metrics for itty-durable
npm install itty-durable
Typescript
Module System
JavaScript (98.79%)
TypeScript (1.21%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
269 Stars
91 Commits
20 Forks
6 Watchers
6 Branches
5 Contributors
Updated on Jul 08, 2025
Latest Version
2.4.1
Package Id
itty-durable@2.4.1
Unpacked Size
38.00 kB
Size
10.97 kB
File Count
21
Published on
Apr 02, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Simplifies usage of Cloudflare Durable Objects, allowing lightweight object definitions and direct access to object methods from within Workers (no need for request building/handling).
npm install itty-router itty-durable
1import { createDurable } from 'itty-durable' 2 3export class Counter extends createDurable({ autoReturn: true }) { 4 constructor(state, env) { 5 super(state, env) 6 7 // anything defined here is only used for initialization (if not loaded from storage) 8 this.counter = 0 9 } 10 11 // Because this function does not return anything, it will return the entire contents 12 // Example: { counter: 1 } 13 increment() { 14 this.counter++ 15 } 16 17 // Any explicit return will honored, despite the autoReturn flag. 18 // Note that any serializable params can passed through from the Worker without issue. 19 add(a, b) { 20 return a + b 21 } 22}
1import { ThrowableRouter, missing, withParams } from 'itty-router-extras' 2import { withDurables } from 'itty-durable' 3 4// export the durable class, per spec 5export { Counter } from './Counter' 6 7const router = ThrowableRouter({ base: '/counter' }) 8 9router 10 // add upstream middleware, allowing Durable access off the request 11 .all('*', withDurables()) 12 13 // get the durable itself... returns json response, so no need to wrap 14 .get('/', ({ Counter }) => Counter.get('test').toJSON()) 15 16 // By using { autoReturn: true } in createDurable(), this method returns the contents 17 .get('/increment', ({ Counter }) => Counter.get('test').increment()) 18 19 // you can pass any serializable params to a method... (e.g. /counter/add/3/4 => 7) 20 .get('/add/:a?/:b?', withParams, 21 ({ Counter, a, b }) => Counter.get('test').add(Number(a), Number(b)) 22 ) 23 24 // reset the durable 25 .get('/reset', ({ Counter }) => Counter.get('test').reset()) 26 27 // 404 for everything else 28 .all('*', () => missing('Are you sure about that?')) 29 30// with itty, and using ES6 module syntax (required for DO), this is all you need 31export default { 32 fetch: router.handle 33} 34 35/* 36Example Interactions: 37 38GET /counter => { counter: 0 } 39GET /counter/increment => { counter: 1 } 40GET /counter/increment => { counter: 2 } 41GET /counter/increment => { counter: 3 } 42GET /counter/reset => { counter: 0 } 43GET /counter/add/20/3 => 23 44*/
This library works via a two part process:
First of all, we create a base class for your Durable Objects to extend (through createDurable()
). This embeds the persistance layer, a few convenience functions, and most importantly, a tiny internal itty-router to handle fetch requests. Using this removes the boilerplate from your objects themselves, allowing them to be only business logic.
Next, we expose the withDurables()
middleware for use within your Workers (it is designed for drop-in use with itty-router, but should work with virtually any typical Worker router). This embeds proxied stubs (translation: "magic stubs") into the Request. Using these stubs, you can call methods on the Durable Object directly, rather than manually creating fetch requests to do so (that's all handled internally, communicating with the embedded router within the Durable Objects themselves).
npm install itty-durable
createDurable(options?: object): class
Factory function to create the IttyDurable class (with options) for your Durable Object to extend.
Option | Type(s) | Default | Description |
---|---|---|---|
autoPersist | boolean | false | By default, all contents are stored in-memory only, and are cleared when the DO evacuates from memory (unless explicitly asked to persist). By setting this to true , each request to the DO through the stub will persist the contents automatically. |
autoReturn | boolean | false | If set to true , methods without an explicit return will return the contents of the object itself (as controlled through the toJSON() method). This method is overridable for custom payload shaping. |
withDurables(options?: object): function
Highly-recommended middleware to embed itty-durable stubs into the request. Using these stubs allows you to skip manually creating/sending requests or handling response parsing.
Option | Type(s) | Default | Description |
---|---|---|---|
parse | boolean | false | By default, the stub methods return a Promise to the Response from the Durable Object itself. This is great if you're just passing the response along and don't want to modify it. To take more control, setting this to true will instead return a Promise to the parsed JSON contents instead. To then respond to requests, you would have to handle building of a JSON Response yourself (e.g. json() within itty-router-extras). |
Big time thanks to all the fantastic developers on the Cloudflare Workers discord group, for their putting up with my constant questions, code snippets, and guiding me off the dangerous[ly flawed] path of async setters ;)
Let's face it, in open source, these are the real heroes... improving the quality of libraries out there for everyone!
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 2/13 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
32 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More