Skip to content

Commit cfa892c

Browse files
authored
Allow prefix on createWorkersKVSessionStorare
1 parent 6ab22a7 commit cfa892c

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

packages/react-router-cloudflare/sessions/workersKVStorage.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ interface WorkersKVSessionStorageOptions {
1616
* The KVNamespace used to store the sessions.
1717
*/
1818
kv: KVNamespace;
19+
20+
/**
21+
* Optional prefix for the session keys in the KV store.
22+
*/
23+
prefix?: string;
1924
}
2025

2126
/**
@@ -30,6 +35,7 @@ export function createWorkersKVSessionStorage<
3035
>({
3136
cookie,
3237
kv,
38+
prefix,
3339
}: WorkersKVSessionStorageOptions): SessionStorage<Data, FlashData> {
3440
return createSessionStorage({
3541
cookie,
@@ -44,11 +50,11 @@ export function createWorkersKVSessionStorage<
4450
.map((x) => x.toString(16).padStart(2, "0"))
4551
.join("");
4652

47-
if (await kv.get(id, "json")) {
53+
if (await kv.get(`${prefix}${id}`, "json")) {
4854
continue;
4955
}
5056

51-
await kv.put(id, JSON.stringify(data), {
57+
await kv.put(`${prefix}${id}`, JSON.stringify(data), {
5258
expiration: expires
5359
? Math.round(expires.getTime() / 1000)
5460
: undefined,
@@ -58,7 +64,7 @@ export function createWorkersKVSessionStorage<
5864
}
5965
},
6066
async readData(id) {
61-
let session = await kv.get(id);
67+
let session = await kv.get(`${prefix}${id}`);
6268

6369
if (!session) {
6470
return null;
@@ -67,12 +73,12 @@ export function createWorkersKVSessionStorage<
6773
return JSON.parse(session);
6874
},
6975
async updateData(id, data, expires) {
70-
await kv.put(id, JSON.stringify(data), {
76+
await kv.put(`${prefix}${id}`, JSON.stringify(data), {
7177
expiration: expires ? Math.round(expires.getTime() / 1000) : undefined,
7278
});
7379
},
7480
async deleteData(id) {
75-
await kv.delete(id);
81+
await kv.delete(`${prefix}${id}`);
7682
},
7783
});
7884
}

0 commit comments

Comments
 (0)