@pluv/persistence-cloudflare-transactional-storage 5.0.0 → 5.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +0 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +5 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +12 -9
- package/.turbo/turbo-build.log +0 -16
- package/CHANGELOG.md +0 -859
- package/eslint.config.mjs +0 -4
- package/src/PersistenceCloudflareTransactionalStorage.ts +0 -383
- package/src/index.ts +0 -2
- package/src/utils/castNumber.ts +0 -7
- package/src/utils/index.ts +0 -3
- package/src/utils/partitionByLength.ts +0 -11
- package/src/utils/sql.ts +0 -10
- package/tsconfig.json +0 -8
- package/tsdown.config.ts +0 -15
package/eslint.config.mjs
DELETED
|
@@ -1,383 +0,0 @@
|
|
|
1
|
-
import { AbstractPersistence } from "@pluv/io";
|
|
2
|
-
import type { JsonObject } from "@pluv/types";
|
|
3
|
-
import { castNumber, partitionByLength, sql } from "./utils";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* !HACK
|
|
7
|
-
* @description The max amount of keys a Cloudflare transactional storage may delete at once
|
|
8
|
-
* @see https://developers.cloudflare.com/durable-objects/api/transactional-storage-api/#delete
|
|
9
|
-
* @date July 8, 2024
|
|
10
|
-
*/
|
|
11
|
-
const KV_CLOUDFLARE_DELETE_BATCH_LIMIT = 128;
|
|
12
|
-
const KV_STORAGE_PREFIX = "$PLUV_STORAGE;";
|
|
13
|
-
const KV_USER_PREFIX = "$PLUV_USER";
|
|
14
|
-
|
|
15
|
-
const SQLITE_STORAGE_TABLE = "__pluv_storage";
|
|
16
|
-
const SQLITE_USER_TABLE = "__pluv_user";
|
|
17
|
-
|
|
18
|
-
export interface PersistenceCloudflareTransactionalStorageConfig {
|
|
19
|
-
mode: "kv" | "sqlite";
|
|
20
|
-
}
|
|
21
|
-
export class PersistenceCloudflareTransactionalStorage extends AbstractPersistence {
|
|
22
|
-
private _mode: "kv" | "sqlite";
|
|
23
|
-
private _state: DurableObjectState | null = null;
|
|
24
|
-
private _initialized: Promise<true> | null = null;
|
|
25
|
-
|
|
26
|
-
constructor(config: PersistenceCloudflareTransactionalStorageConfig) {
|
|
27
|
-
super();
|
|
28
|
-
|
|
29
|
-
const { mode } = config;
|
|
30
|
-
|
|
31
|
-
this._initialized = (config as any)._initialized;
|
|
32
|
-
this._mode = mode;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
public async addUser(
|
|
36
|
-
room: string,
|
|
37
|
-
connectionId: string,
|
|
38
|
-
user: JsonObject | null,
|
|
39
|
-
): Promise<void> {
|
|
40
|
-
if (!this._initialized) return;
|
|
41
|
-
if (!this._state) return;
|
|
42
|
-
|
|
43
|
-
await this._initialized;
|
|
44
|
-
|
|
45
|
-
if (this._mode === "kv") {
|
|
46
|
-
await this._state.storage.put(this._getUserKey(room, connectionId), user);
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const result = this._state.storage.sql.exec<{ count: 0 }>(
|
|
51
|
-
sql`
|
|
52
|
-
SELECT id
|
|
53
|
-
FROM ${SQLITE_USER_TABLE}
|
|
54
|
-
WHERE id = ?;
|
|
55
|
-
`,
|
|
56
|
-
connectionId,
|
|
57
|
-
);
|
|
58
|
-
|
|
59
|
-
const existing = !!(result.toArray()[0]?.count ?? 0);
|
|
60
|
-
|
|
61
|
-
if (existing) return;
|
|
62
|
-
|
|
63
|
-
this._state.storage.sql.exec(
|
|
64
|
-
sql`
|
|
65
|
-
INSERT INTO ${SQLITE_USER_TABLE} VALUES (?, ?, ?);
|
|
66
|
-
`,
|
|
67
|
-
connectionId,
|
|
68
|
-
JSON.stringify(user),
|
|
69
|
-
room,
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
public async deleteStorageState(room: string): Promise<void> {
|
|
74
|
-
if (!this._initialized) return;
|
|
75
|
-
if (!this._state) return;
|
|
76
|
-
|
|
77
|
-
await this._initialized;
|
|
78
|
-
|
|
79
|
-
if (this._mode === "kv") {
|
|
80
|
-
await this._state.storage.delete(this._getStorageKey(room));
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
this._state.storage.sql.exec(
|
|
85
|
-
sql`
|
|
86
|
-
DELETE FROM ${SQLITE_STORAGE_TABLE}
|
|
87
|
-
WHERE room = ?;
|
|
88
|
-
`,
|
|
89
|
-
room,
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
public async deleteUser(room: string, connectionId: string): Promise<void> {
|
|
94
|
-
if (!this._initialized) return;
|
|
95
|
-
if (!this._state) return;
|
|
96
|
-
|
|
97
|
-
await this._initialized;
|
|
98
|
-
|
|
99
|
-
if (this._mode === "kv") {
|
|
100
|
-
await this._state.storage.delete(this._getUserKey(room, connectionId));
|
|
101
|
-
|
|
102
|
-
return;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
this._state.storage.sql.exec(
|
|
106
|
-
sql`
|
|
107
|
-
DELETE FROM ${SQLITE_USER_TABLE}
|
|
108
|
-
WHERE room = ?
|
|
109
|
-
AND id = ?;
|
|
110
|
-
`,
|
|
111
|
-
room,
|
|
112
|
-
connectionId,
|
|
113
|
-
);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
public async deleteUsers(room: string): Promise<void> {
|
|
117
|
-
if (!this._initialized) return;
|
|
118
|
-
if (!this._state) return;
|
|
119
|
-
|
|
120
|
-
await this._initialized;
|
|
121
|
-
|
|
122
|
-
if (this._mode === "kv") {
|
|
123
|
-
const map = await this._state.storage.list({
|
|
124
|
-
allowConcurrency: true,
|
|
125
|
-
noCache: true,
|
|
126
|
-
prefix: `${KV_USER_PREFIX}::${room}`,
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
const partitions = partitionByLength(
|
|
130
|
-
Array.from(map.keys()),
|
|
131
|
-
KV_CLOUDFLARE_DELETE_BATCH_LIMIT,
|
|
132
|
-
);
|
|
133
|
-
|
|
134
|
-
await this._state.storage.transaction(async (tx) => {
|
|
135
|
-
await partitions.reduce(async (promise, partition) => {
|
|
136
|
-
await promise;
|
|
137
|
-
await tx.delete(partition as string[]);
|
|
138
|
-
}, Promise.resolve());
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
this._state.storage.sql.exec(
|
|
145
|
-
sql`
|
|
146
|
-
DELETE FROM ${SQLITE_USER_TABLE}
|
|
147
|
-
WHERE room = ?;
|
|
148
|
-
`,
|
|
149
|
-
room,
|
|
150
|
-
);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
public async getStorageState(room: string): Promise<string | null> {
|
|
154
|
-
if (!this._initialized) return null;
|
|
155
|
-
if (!this._state) return null;
|
|
156
|
-
|
|
157
|
-
await this._initialized;
|
|
158
|
-
|
|
159
|
-
if (this._mode === "kv") {
|
|
160
|
-
const storage = await this._state.storage.get<string>(this._getStorageKey(room));
|
|
161
|
-
return storage ?? null;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const cursor = this._state.storage.sql.exec<{ room: string; data: string }>(
|
|
165
|
-
sql`
|
|
166
|
-
SELECT room, data
|
|
167
|
-
FROM ${SQLITE_STORAGE_TABLE}
|
|
168
|
-
WHERE room = ?;
|
|
169
|
-
`,
|
|
170
|
-
room,
|
|
171
|
-
);
|
|
172
|
-
|
|
173
|
-
try {
|
|
174
|
-
const result = cursor.one();
|
|
175
|
-
|
|
176
|
-
return result.data;
|
|
177
|
-
} catch {
|
|
178
|
-
return null;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
public async getUser(room: string, connectionId: string): Promise<JsonObject | null> {
|
|
183
|
-
if (!this._initialized) return null;
|
|
184
|
-
if (!this._state) return null;
|
|
185
|
-
|
|
186
|
-
await this._initialized;
|
|
187
|
-
|
|
188
|
-
if (this._mode === "kv") {
|
|
189
|
-
const user = await this._state.storage.get<JsonObject | null>(
|
|
190
|
-
this._getUserKey(room, connectionId),
|
|
191
|
-
);
|
|
192
|
-
|
|
193
|
-
return user ?? null;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
const cursor = this._state.storage.sql.exec<{ id: string; data: string; room: string }>(
|
|
197
|
-
sql`
|
|
198
|
-
SELECT id, data, room
|
|
199
|
-
FROM ${SQLITE_USER_TABLE}
|
|
200
|
-
WHERE room = ?
|
|
201
|
-
AND id = ?;
|
|
202
|
-
`,
|
|
203
|
-
room,
|
|
204
|
-
connectionId,
|
|
205
|
-
);
|
|
206
|
-
|
|
207
|
-
try {
|
|
208
|
-
const result = cursor.one();
|
|
209
|
-
const user = JSON.parse(result.data);
|
|
210
|
-
|
|
211
|
-
return user;
|
|
212
|
-
} catch {
|
|
213
|
-
return null;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
public async getUsers(
|
|
218
|
-
room: string,
|
|
219
|
-
): Promise<Map<[connectionId: string][0], JsonObject | null>> {
|
|
220
|
-
if (!this._initialized) return new Map();
|
|
221
|
-
if (!this._state) return new Map();
|
|
222
|
-
|
|
223
|
-
await this._initialized;
|
|
224
|
-
|
|
225
|
-
if (this._mode === "kv") {
|
|
226
|
-
const entries = await this._state.storage
|
|
227
|
-
.list<JsonObject | null>({
|
|
228
|
-
prefix: `${KV_USER_PREFIX}::${room}`,
|
|
229
|
-
})
|
|
230
|
-
.then((map) => Array.from(map.entries()));
|
|
231
|
-
|
|
232
|
-
return entries.reduce((map, [key, user]) => {
|
|
233
|
-
const connectionId = key.split("::").slice(-1)[0];
|
|
234
|
-
|
|
235
|
-
return !!connectionId ? map.set(connectionId, user) : map;
|
|
236
|
-
}, new Map<string, JsonObject | null>());
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
const cursor = this._state.storage.sql.exec<{ id: string; data: string; room: string }>(
|
|
240
|
-
sql`
|
|
241
|
-
SELECT id, room, data
|
|
242
|
-
FROM ${SQLITE_USER_TABLE}
|
|
243
|
-
WHERE room = ?;
|
|
244
|
-
`,
|
|
245
|
-
room,
|
|
246
|
-
);
|
|
247
|
-
|
|
248
|
-
return cursor.toArray().reduce((map, { id, data }) => {
|
|
249
|
-
try {
|
|
250
|
-
const user = JSON.parse(data) as JsonObject;
|
|
251
|
-
|
|
252
|
-
return map.set(id, user);
|
|
253
|
-
} catch {
|
|
254
|
-
return map;
|
|
255
|
-
}
|
|
256
|
-
}, new Map<string, JsonObject | null>());
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
public async getUsersSize(room: string): Promise<number> {
|
|
260
|
-
if (!this._initialized) return 0;
|
|
261
|
-
if (!this._state) return 0;
|
|
262
|
-
|
|
263
|
-
await this._initialized;
|
|
264
|
-
|
|
265
|
-
if (this._mode) {
|
|
266
|
-
const storage = await this._state.storage.list({
|
|
267
|
-
prefix: `${KV_USER_PREFIX}::${room}`,
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
return storage.size;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
const cursor = this._state.storage.sql.exec<{ count: number }>(
|
|
274
|
-
sql`
|
|
275
|
-
SELECT COUNT(*) count
|
|
276
|
-
FROM ${SQLITE_USER_TABLE}
|
|
277
|
-
WHERE room = ?;
|
|
278
|
-
`,
|
|
279
|
-
room,
|
|
280
|
-
);
|
|
281
|
-
|
|
282
|
-
try {
|
|
283
|
-
const result = cursor.one();
|
|
284
|
-
|
|
285
|
-
return castNumber(result.count);
|
|
286
|
-
} catch {
|
|
287
|
-
return 0;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
public initialize(roomContext: { state: DurableObjectState }): typeof this {
|
|
292
|
-
const initialized = (async () => {
|
|
293
|
-
const { state } = roomContext;
|
|
294
|
-
|
|
295
|
-
this._state = state;
|
|
296
|
-
|
|
297
|
-
if (this._mode === "sqlite") {
|
|
298
|
-
this._state.storage.sql.exec(sql`
|
|
299
|
-
CREATE TABLE IF NOT EXISTS ${SQLITE_STORAGE_TABLE}(
|
|
300
|
-
room TEXT PRIMARY KEY,
|
|
301
|
-
data TEXT NOT NULL
|
|
302
|
-
);
|
|
303
|
-
CREATE TABLE IF NOT EXISTS ${SQLITE_USER_TABLE}(
|
|
304
|
-
id TEXT PRIMARY KEY,
|
|
305
|
-
data TEXT NOT NULL,
|
|
306
|
-
room TEXT NOT NULL
|
|
307
|
-
);
|
|
308
|
-
CREATE INDEX IF NOT EXISTS ${SQLITE_USER_TABLE}_room ON ${SQLITE_USER_TABLE}(room);
|
|
309
|
-
`);
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
return true as const;
|
|
313
|
-
})();
|
|
314
|
-
|
|
315
|
-
return new PersistenceCloudflareTransactionalStorage({
|
|
316
|
-
mode: this._mode,
|
|
317
|
-
...{ _initialized: initialized },
|
|
318
|
-
}) as typeof this;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
public async setStorageState(room: string, state: string): Promise<void> {
|
|
322
|
-
if (!this._initialized) return;
|
|
323
|
-
if (!this._state) return;
|
|
324
|
-
|
|
325
|
-
await this._initialized;
|
|
326
|
-
|
|
327
|
-
if (this._mode === "kv") {
|
|
328
|
-
await this._state.storage.put(this._getStorageKey(room), state, {
|
|
329
|
-
allowConcurrency: true,
|
|
330
|
-
});
|
|
331
|
-
return;
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
const result = this._state.storage.sql.exec<{ count: number }>(
|
|
335
|
-
sql`
|
|
336
|
-
SELECT COUNT(*) as count
|
|
337
|
-
FROM ${SQLITE_STORAGE_TABLE}
|
|
338
|
-
WHERE room = ?;
|
|
339
|
-
`,
|
|
340
|
-
room,
|
|
341
|
-
);
|
|
342
|
-
|
|
343
|
-
const existing = !!(result.toArray()[0]?.count ?? 0);
|
|
344
|
-
|
|
345
|
-
if (existing) {
|
|
346
|
-
this._state.storage.sql.exec(
|
|
347
|
-
sql`
|
|
348
|
-
UPDATE ${SQLITE_STORAGE_TABLE}
|
|
349
|
-
SET data = ?
|
|
350
|
-
WHERE room = ?;
|
|
351
|
-
`,
|
|
352
|
-
state,
|
|
353
|
-
room,
|
|
354
|
-
);
|
|
355
|
-
|
|
356
|
-
return;
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
this._state.storage.sql.exec(
|
|
360
|
-
sql`
|
|
361
|
-
INSERT INTO ${SQLITE_STORAGE_TABLE} VALUES (?, ?);
|
|
362
|
-
`,
|
|
363
|
-
room,
|
|
364
|
-
state,
|
|
365
|
-
);
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
private _getStorageKey(room: string): string {
|
|
369
|
-
if (this._mode === "sqlite") {
|
|
370
|
-
throw new Error("Attempted to get kv key for sqlite storage");
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
return `${KV_STORAGE_PREFIX}::${room}`;
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
private _getUserKey(room: string, connectionId: string): string {
|
|
377
|
-
if (this._mode === "sqlite") {
|
|
378
|
-
throw new Error("Attempted to get kv key for sqlite storage");
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
return `${KV_USER_PREFIX}::${room}::${connectionId}`;
|
|
382
|
-
}
|
|
383
|
-
}
|
package/src/index.ts
DELETED
package/src/utils/castNumber.ts
DELETED
package/src/utils/index.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export const partitionByLength = <T>(
|
|
2
|
-
arr: readonly T[] | T[],
|
|
3
|
-
length: number,
|
|
4
|
-
): readonly (readonly T[])[] => {
|
|
5
|
-
if (!arr.length) return [];
|
|
6
|
-
|
|
7
|
-
const head = arr.slice(0, length);
|
|
8
|
-
const tail = arr.slice(length);
|
|
9
|
-
|
|
10
|
-
return [head, ...partitionByLength(tail, length)];
|
|
11
|
-
};
|
package/src/utils/sql.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export const sql = (strings: TemplateStringsArray, ...values: any[]): string => {
|
|
2
|
-
return strings
|
|
3
|
-
.reduce((result, str, i) => {
|
|
4
|
-
// Compresses the string to a single line (not including the interpolated values)
|
|
5
|
-
const cleaned = str.replace(/(\n[ \t]*)+/g, " ");
|
|
6
|
-
|
|
7
|
-
return result + cleaned + (i < values.length ? values[i] : "");
|
|
8
|
-
}, "")
|
|
9
|
-
.trim();
|
|
10
|
-
};
|
package/tsconfig.json
DELETED
package/tsdown.config.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { defineConfig } from "tsdown";
|
|
2
|
-
|
|
3
|
-
export default defineConfig({
|
|
4
|
-
format: ["esm"],
|
|
5
|
-
dts: true,
|
|
6
|
-
outDir: "dist",
|
|
7
|
-
entry: "src/index.ts",
|
|
8
|
-
target: "esnext",
|
|
9
|
-
sourcemap: true,
|
|
10
|
-
clean: true,
|
|
11
|
-
external: ["cloudflare:workers"],
|
|
12
|
-
exports: {
|
|
13
|
-
all: true,
|
|
14
|
-
},
|
|
15
|
-
});
|