Skip to content

Commit 5f2c464

Browse files
committed
fix: fix for cloudflare workers
1 parent 115b408 commit 5f2c464

4 files changed

Lines changed: 1696 additions & 973 deletions

File tree

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ npm i xid-ts --save
2727

2828
## Examples
2929

30-
```js
30+
### Create and Parse Xid
31+
32+
```ts
3133
import { Xid } from 'xid-ts'
3234

3335
const defaultXid = Xid.default()
@@ -51,7 +53,7 @@ assert.equal(xid.equals(Xid.fromValue(Buffer.from([77, 136, 225, 91, 96, 244, 13
5153
assert.equal(xid.equals(Xid.fromValue(new Uint8Array([77, 136, 225, 91, 96, 244, 134, 228, 40, 65, 45, 201]))), true)
5254
```
5355

54-
## Encode & Decode With JSON and CBOR
56+
### Encode & Decode With JSON and CBOR
5557

5658
https://github.com/yiwen-ai/xid-ts/blob/main/src/index.test.ts#L70
5759

@@ -78,6 +80,35 @@ const obj2 = decode(data)
7880
assert.isTrue(xid.equals(Xid.fromValue(obj2.id)))
7981
```
8082

83+
### Cloudflare Workers
84+
85+
```ts
86+
import { DurableObject } from "cloudflare:workers"
87+
import { Xid, newState, type XidState } from 'xid-ts'
88+
89+
export class GlobalState extends DurableObject {
90+
#xidState: XidState = newState()
91+
92+
constructor(ctx: DurableObjectState, env: Env) {
93+
super(ctx, env)
94+
}
95+
96+
xid(): Xid {
97+
return new Xid(undefined, this.#xidState)
98+
}
99+
}
100+
101+
export default {
102+
async fetch(request, env, ctx): Promise<Response> {
103+
const stub = env.GLOBAL_STATE.getByName("global")
104+
// _id is not Xid instance because it comes from DurableObject RPC serialization
105+
const _id = await stub.xid()
106+
const id = new Xid(_id)
107+
return new Response(JSON.stringify({id}), { status: 200 })
108+
// {"id":"d4amdocsodcmnao0bddg"}
109+
},
110+
} satisfies ExportedHandler<Env>
111+
```
81112

82113
[`xid`]: https://github.com/rs/xid
83114
[object-id]: https://docs.mongodb.org/manual/reference/object-id/

package.json

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "xid-ts",
33
"type": "module",
4-
"version": "1.2.0",
4+
"version": "1.2.1",
55
"author": "0xZensh <txr1883@gmail.com>",
66
"description": "xid is a globally unique id generator thought for the web. A Typescript port of https://github.com/rs/xid.",
77
"license": "MIT",
@@ -42,18 +42,18 @@
4242
"test": "vitest src --coverage --run"
4343
},
4444
"devDependencies": {
45-
"@types/node": "^22.5.1",
46-
"@typescript-eslint/eslint-plugin": "^8.3.0",
47-
"@typescript-eslint/parser": "^8.3.0",
48-
"@vitest/coverage-v8": "^2.0.5",
49-
"cborg": "^4.2.3",
50-
"eslint": "^9.9.1",
51-
"eslint-plugin-import": "^2.29.1",
52-
"eslint-config-prettier": "^9.1.0",
53-
"eslint-plugin-prettier": "^5.2.1",
54-
"prettier": "^3.3.3",
55-
"typescript": "^5.5.4",
56-
"vitest": "^2.0.5"
45+
"@types/node": "^24.10.1",
46+
"@typescript-eslint/eslint-plugin": "^8.46.4",
47+
"@typescript-eslint/parser": "^8.46.4",
48+
"@vitest/coverage-v8": "^4.0.8",
49+
"cborg": "^4.3.0",
50+
"eslint": "^9.39.1",
51+
"eslint-plugin-import": "^2.32.0",
52+
"eslint-config-prettier": "^10.1.8",
53+
"eslint-plugin-prettier": "^5.5.4",
54+
"prettier": "^3.6.2",
55+
"typescript": "^5.9.3",
56+
"vitest": "^4.0.8"
5757
},
5858
"dependencies": {},
5959
"keywords": [

0 commit comments

Comments
 (0)