@pluv/persistence-cloudflare-transactional-storage 0.25.0
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/.eslintrc.js +4 -0
- package/.turbo/turbo-build.log +18 -0
- package/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +20 -0
- package/dist/index.d.mts +23 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +143 -0
- package/dist/index.mjs +117 -0
- package/package.json +37 -0
- package/src/PersistenceCloudflareTransactionalStorage.ts +99 -0
- package/src/index.ts +2 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/partitionByLength.ts +8 -0
- package/tsconfig.json +8 -0
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
> @pluv/persistence-cloudflare-transactional-storage@0.25.0 build /home/runner/work/pluv/pluv/packages/persistence-cloudflare-transactional-storage
|
|
3
|
+
> tsup src/index.ts --format esm,cjs --dts
|
|
4
|
+
|
|
5
|
+
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
+
[34mCLI[39m tsup v8.2.4
|
|
8
|
+
[34mCLI[39m Target: es6
|
|
9
|
+
[34mESM[39m Build start
|
|
10
|
+
[34mCJS[39m Build start
|
|
11
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m3.57 KB[39m
|
|
12
|
+
[32mESM[39m ⚡️ Build success in 44ms
|
|
13
|
+
[32mCJS[39m [1mdist/index.js [22m[32m4.63 KB[39m
|
|
14
|
+
[32mCJS[39m ⚡️ Build success in 45ms
|
|
15
|
+
[34mDTS[39m Build start
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 2228ms
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.mts [22m[32m1.07 KB[39m
|
|
18
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m1.07 KB[39m
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @pluv/persistence-cloudflare-transactional-storage
|
|
2
|
+
|
|
3
|
+
## 0.25.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 9db06ba: Added new package.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [4e078ca]
|
|
12
|
+
- Updated dependencies [f556d30]
|
|
13
|
+
- Updated dependencies [9db06ba]
|
|
14
|
+
- @pluv/io@0.25.0
|
|
15
|
+
- @pluv/types@0.25.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Shoubhit Dash
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
## `@pluv/persistence-cloudflare-transactional-storage`
|
|
2
|
+
|
|
3
|
+
> Crdt storage persistence for [@pluv/io](https://www.npmjs.com/package/@pluv/io) rooms on Cloudflare Workers.
|
|
4
|
+
|
|
5
|
+
**👉 See full documentation on [pluv.io](https://pluv.io/docs/introduction). 👈**
|
|
6
|
+
|
|
7
|
+
This is a dependency of `@pluv/platform-cloudflare`. You probably don't mean to install this yourself.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
# npm
|
|
13
|
+
npm install @pluv/persistence-cloudflare-transactional-storage
|
|
14
|
+
|
|
15
|
+
# yarn
|
|
16
|
+
yarn add @pluv/persistence-cloudflare-transactional-storage
|
|
17
|
+
|
|
18
|
+
# pnpm
|
|
19
|
+
pnpm add @pluv/persistence-cloudflare-transactional-storage
|
|
20
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { AbstractPersistence } from '@pluv/io';
|
|
2
|
+
import { JsonObject } from '@pluv/types';
|
|
3
|
+
|
|
4
|
+
interface PersistenceCloudflareTransactionalStorageConfig {
|
|
5
|
+
state: DurableObjectState;
|
|
6
|
+
}
|
|
7
|
+
declare class PersistenceCloudflareTransactionalStorage extends AbstractPersistence {
|
|
8
|
+
private _state;
|
|
9
|
+
constructor(config: PersistenceCloudflareTransactionalStorageConfig);
|
|
10
|
+
addUser(room: string, connectionId: string, user: JsonObject | null): Promise<void>;
|
|
11
|
+
deleteStorageState(room: string): Promise<void>;
|
|
12
|
+
deleteUser(room: string, connectionId: string): Promise<void>;
|
|
13
|
+
deleteUsers(room: string): Promise<void>;
|
|
14
|
+
getSize(room: string): Promise<number>;
|
|
15
|
+
getStorageState(room: string): Promise<string | null>;
|
|
16
|
+
getUser(room: string, connectionId: string): Promise<JsonObject | null>;
|
|
17
|
+
getUsers(room: string): Promise<readonly (JsonObject | null)[]>;
|
|
18
|
+
setStorageState(room: string, state: string): Promise<void>;
|
|
19
|
+
private _getStorageKey;
|
|
20
|
+
private _getUserKey;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { PersistenceCloudflareTransactionalStorage, type PersistenceCloudflareTransactionalStorageConfig };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { AbstractPersistence } from '@pluv/io';
|
|
2
|
+
import { JsonObject } from '@pluv/types';
|
|
3
|
+
|
|
4
|
+
interface PersistenceCloudflareTransactionalStorageConfig {
|
|
5
|
+
state: DurableObjectState;
|
|
6
|
+
}
|
|
7
|
+
declare class PersistenceCloudflareTransactionalStorage extends AbstractPersistence {
|
|
8
|
+
private _state;
|
|
9
|
+
constructor(config: PersistenceCloudflareTransactionalStorageConfig);
|
|
10
|
+
addUser(room: string, connectionId: string, user: JsonObject | null): Promise<void>;
|
|
11
|
+
deleteStorageState(room: string): Promise<void>;
|
|
12
|
+
deleteUser(room: string, connectionId: string): Promise<void>;
|
|
13
|
+
deleteUsers(room: string): Promise<void>;
|
|
14
|
+
getSize(room: string): Promise<number>;
|
|
15
|
+
getStorageState(room: string): Promise<string | null>;
|
|
16
|
+
getUser(room: string, connectionId: string): Promise<JsonObject | null>;
|
|
17
|
+
getUsers(room: string): Promise<readonly (JsonObject | null)[]>;
|
|
18
|
+
setStorageState(room: string, state: string): Promise<void>;
|
|
19
|
+
private _getStorageKey;
|
|
20
|
+
private _getUserKey;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { PersistenceCloudflareTransactionalStorage, type PersistenceCloudflareTransactionalStorageConfig };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var __async = (__this, __arguments, generator) => {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
var fulfilled = (value) => {
|
|
22
|
+
try {
|
|
23
|
+
step(generator.next(value));
|
|
24
|
+
} catch (e) {
|
|
25
|
+
reject(e);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
var rejected = (value) => {
|
|
29
|
+
try {
|
|
30
|
+
step(generator.throw(value));
|
|
31
|
+
} catch (e) {
|
|
32
|
+
reject(e);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
36
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// src/index.ts
|
|
41
|
+
var src_exports = {};
|
|
42
|
+
__export(src_exports, {
|
|
43
|
+
PersistenceCloudflareTransactionalStorage: () => PersistenceCloudflareTransactionalStorage
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(src_exports);
|
|
46
|
+
|
|
47
|
+
// src/PersistenceCloudflareTransactionalStorage.ts
|
|
48
|
+
var import_io = require("@pluv/io");
|
|
49
|
+
|
|
50
|
+
// src/utils/partitionByLength.ts
|
|
51
|
+
var partitionByLength = (arr, length) => {
|
|
52
|
+
if (!arr.length) return [];
|
|
53
|
+
const head = arr.slice(0, length);
|
|
54
|
+
const tail = arr.slice(length);
|
|
55
|
+
return [head, ...partitionByLength(tail, length)];
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// src/PersistenceCloudflareTransactionalStorage.ts
|
|
59
|
+
var CLOUDFLARE_DELETE_BATCH_LIMIT = 128;
|
|
60
|
+
var STORAGE_PREFIX = "$PLUV_STORAGE;";
|
|
61
|
+
var USER_PREFIX = "$PLUV_USER";
|
|
62
|
+
var PersistenceCloudflareTransactionalStorage = class extends import_io.AbstractPersistence {
|
|
63
|
+
constructor(config) {
|
|
64
|
+
super();
|
|
65
|
+
const { state } = config;
|
|
66
|
+
this._state = state;
|
|
67
|
+
}
|
|
68
|
+
addUser(room, connectionId, user) {
|
|
69
|
+
return __async(this, null, function* () {
|
|
70
|
+
yield this._state.storage.put(this._getUserKey(room, connectionId), user);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
deleteStorageState(room) {
|
|
74
|
+
return __async(this, null, function* () {
|
|
75
|
+
yield this._state.storage.delete(this._getStorageKey(room));
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
deleteUser(room, connectionId) {
|
|
79
|
+
return __async(this, null, function* () {
|
|
80
|
+
yield this._state.storage.delete(this._getUserKey(room, connectionId));
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
deleteUsers(room) {
|
|
84
|
+
return __async(this, null, function* () {
|
|
85
|
+
const map = yield this._state.storage.list({
|
|
86
|
+
allowConcurrency: true,
|
|
87
|
+
noCache: true,
|
|
88
|
+
prefix: USER_PREFIX
|
|
89
|
+
});
|
|
90
|
+
const partitions = partitionByLength(Array.from(map.keys()), CLOUDFLARE_DELETE_BATCH_LIMIT);
|
|
91
|
+
yield this._state.storage.transaction((tx) => __async(this, null, function* () {
|
|
92
|
+
yield partitions.reduce((promise, partition) => __async(this, null, function* () {
|
|
93
|
+
return yield promise.then(() => __async(this, null, function* () {
|
|
94
|
+
yield tx.delete(partition);
|
|
95
|
+
}));
|
|
96
|
+
}), Promise.resolve());
|
|
97
|
+
}));
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
getSize(room) {
|
|
101
|
+
return __async(this, null, function* () {
|
|
102
|
+
const storage = yield this._state.storage.list({ prefix: USER_PREFIX });
|
|
103
|
+
return storage.size;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
getStorageState(room) {
|
|
107
|
+
return __async(this, null, function* () {
|
|
108
|
+
const storage = yield this._state.storage.get(this._getStorageKey(room));
|
|
109
|
+
return storage != null ? storage : null;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
getUser(room, connectionId) {
|
|
113
|
+
return __async(this, null, function* () {
|
|
114
|
+
const user = yield this._state.storage.get(this._getUserKey(room, connectionId));
|
|
115
|
+
return user != null ? user : null;
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
getUsers(room) {
|
|
119
|
+
return __async(this, null, function* () {
|
|
120
|
+
const storage = yield this._state.storage.list({
|
|
121
|
+
prefix: USER_PREFIX
|
|
122
|
+
});
|
|
123
|
+
return Array.from(storage.values());
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
setStorageState(room, state) {
|
|
127
|
+
return __async(this, null, function* () {
|
|
128
|
+
yield this._state.storage.put(this._getStorageKey(room), state, {
|
|
129
|
+
allowConcurrency: true
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
_getStorageKey(room) {
|
|
134
|
+
return `${STORAGE_PREFIX}::${room}`;
|
|
135
|
+
}
|
|
136
|
+
_getUserKey(room, connectionId) {
|
|
137
|
+
return `${USER_PREFIX}::${room}::${connectionId}`;
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
141
|
+
0 && (module.exports = {
|
|
142
|
+
PersistenceCloudflareTransactionalStorage
|
|
143
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/PersistenceCloudflareTransactionalStorage.ts
|
|
23
|
+
import { AbstractPersistence } from "@pluv/io";
|
|
24
|
+
|
|
25
|
+
// src/utils/partitionByLength.ts
|
|
26
|
+
var partitionByLength = (arr, length) => {
|
|
27
|
+
if (!arr.length) return [];
|
|
28
|
+
const head = arr.slice(0, length);
|
|
29
|
+
const tail = arr.slice(length);
|
|
30
|
+
return [head, ...partitionByLength(tail, length)];
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// src/PersistenceCloudflareTransactionalStorage.ts
|
|
34
|
+
var CLOUDFLARE_DELETE_BATCH_LIMIT = 128;
|
|
35
|
+
var STORAGE_PREFIX = "$PLUV_STORAGE;";
|
|
36
|
+
var USER_PREFIX = "$PLUV_USER";
|
|
37
|
+
var PersistenceCloudflareTransactionalStorage = class extends AbstractPersistence {
|
|
38
|
+
constructor(config) {
|
|
39
|
+
super();
|
|
40
|
+
const { state } = config;
|
|
41
|
+
this._state = state;
|
|
42
|
+
}
|
|
43
|
+
addUser(room, connectionId, user) {
|
|
44
|
+
return __async(this, null, function* () {
|
|
45
|
+
yield this._state.storage.put(this._getUserKey(room, connectionId), user);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
deleteStorageState(room) {
|
|
49
|
+
return __async(this, null, function* () {
|
|
50
|
+
yield this._state.storage.delete(this._getStorageKey(room));
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
deleteUser(room, connectionId) {
|
|
54
|
+
return __async(this, null, function* () {
|
|
55
|
+
yield this._state.storage.delete(this._getUserKey(room, connectionId));
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
deleteUsers(room) {
|
|
59
|
+
return __async(this, null, function* () {
|
|
60
|
+
const map = yield this._state.storage.list({
|
|
61
|
+
allowConcurrency: true,
|
|
62
|
+
noCache: true,
|
|
63
|
+
prefix: USER_PREFIX
|
|
64
|
+
});
|
|
65
|
+
const partitions = partitionByLength(Array.from(map.keys()), CLOUDFLARE_DELETE_BATCH_LIMIT);
|
|
66
|
+
yield this._state.storage.transaction((tx) => __async(this, null, function* () {
|
|
67
|
+
yield partitions.reduce((promise, partition) => __async(this, null, function* () {
|
|
68
|
+
return yield promise.then(() => __async(this, null, function* () {
|
|
69
|
+
yield tx.delete(partition);
|
|
70
|
+
}));
|
|
71
|
+
}), Promise.resolve());
|
|
72
|
+
}));
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
getSize(room) {
|
|
76
|
+
return __async(this, null, function* () {
|
|
77
|
+
const storage = yield this._state.storage.list({ prefix: USER_PREFIX });
|
|
78
|
+
return storage.size;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
getStorageState(room) {
|
|
82
|
+
return __async(this, null, function* () {
|
|
83
|
+
const storage = yield this._state.storage.get(this._getStorageKey(room));
|
|
84
|
+
return storage != null ? storage : null;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
getUser(room, connectionId) {
|
|
88
|
+
return __async(this, null, function* () {
|
|
89
|
+
const user = yield this._state.storage.get(this._getUserKey(room, connectionId));
|
|
90
|
+
return user != null ? user : null;
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
getUsers(room) {
|
|
94
|
+
return __async(this, null, function* () {
|
|
95
|
+
const storage = yield this._state.storage.list({
|
|
96
|
+
prefix: USER_PREFIX
|
|
97
|
+
});
|
|
98
|
+
return Array.from(storage.values());
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
setStorageState(room, state) {
|
|
102
|
+
return __async(this, null, function* () {
|
|
103
|
+
yield this._state.storage.put(this._getStorageKey(room), state, {
|
|
104
|
+
allowConcurrency: true
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
_getStorageKey(room) {
|
|
109
|
+
return `${STORAGE_PREFIX}::${room}`;
|
|
110
|
+
}
|
|
111
|
+
_getUserKey(room, connectionId) {
|
|
112
|
+
return `${USER_PREFIX}::${room}::${connectionId}`;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
export {
|
|
116
|
+
PersistenceCloudflareTransactionalStorage
|
|
117
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pluv/persistence-cloudflare-transactional-storage",
|
|
3
|
+
"version": "0.25.0",
|
|
4
|
+
"description": "@pluv/io crdt storage persistence for cloudflare via transactional storage",
|
|
5
|
+
"author": "leedavidcs",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/pluv-io/pluv",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/pluv-io/pluv.git",
|
|
11
|
+
"directory": "packages/persistence-cloudflare-transactional-storage"
|
|
12
|
+
},
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"module": "./dist/index.mjs",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@pluv/io": "^0.25.0",
|
|
21
|
+
"@pluv/types": "^0.25.0"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@cloudflare/workers-types": "^4.20240821.1",
|
|
25
|
+
"eslint": "^8.57.0",
|
|
26
|
+
"tsup": "^8.2.4",
|
|
27
|
+
"typescript": "^5.5.4",
|
|
28
|
+
"@pluv/tsconfig": "^0.25.0",
|
|
29
|
+
"eslint-config-pluv": "^0.25.0"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
33
|
+
"dev": "tsup src/index.ts --format esm,cjs --watch --dts",
|
|
34
|
+
"lint": "eslint src/**/*.ts* --fix",
|
|
35
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { AbstractPersistence } from "@pluv/io";
|
|
2
|
+
import type { JsonObject } from "@pluv/types";
|
|
3
|
+
import { partitionByLength } 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 CLOUDFLARE_DELETE_BATCH_LIMIT = 128;
|
|
12
|
+
const STORAGE_PREFIX = "$PLUV_STORAGE;";
|
|
13
|
+
const USER_PREFIX = "$PLUV_USER";
|
|
14
|
+
|
|
15
|
+
export interface PersistenceCloudflareTransactionalStorageConfig {
|
|
16
|
+
state: DurableObjectState;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class PersistenceCloudflareTransactionalStorage extends AbstractPersistence {
|
|
20
|
+
private _state: DurableObjectState;
|
|
21
|
+
|
|
22
|
+
constructor(config: PersistenceCloudflareTransactionalStorageConfig) {
|
|
23
|
+
super();
|
|
24
|
+
|
|
25
|
+
const { state } = config;
|
|
26
|
+
|
|
27
|
+
this._state = state;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public async addUser(room: string, connectionId: string, user: JsonObject | null): Promise<void> {
|
|
31
|
+
await this._state.storage.put(this._getUserKey(room, connectionId), user);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
public async deleteStorageState(room: string): Promise<void> {
|
|
35
|
+
await this._state.storage.delete(this._getStorageKey(room));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
public async deleteUser(room: string, connectionId: string): Promise<void> {
|
|
39
|
+
await this._state.storage.delete(this._getUserKey(room, connectionId));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public async deleteUsers(room: string): Promise<void> {
|
|
43
|
+
const map = await this._state.storage.list({
|
|
44
|
+
allowConcurrency: true,
|
|
45
|
+
noCache: true,
|
|
46
|
+
prefix: USER_PREFIX,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const partitions = partitionByLength(Array.from(map.keys()), CLOUDFLARE_DELETE_BATCH_LIMIT);
|
|
50
|
+
|
|
51
|
+
await this._state.storage.transaction(async (tx) => {
|
|
52
|
+
await partitions.reduce(async (promise, partition) => {
|
|
53
|
+
return await promise.then(async () => {
|
|
54
|
+
await tx.delete(partition as string[]);
|
|
55
|
+
});
|
|
56
|
+
}, Promise.resolve());
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
public async getSize(room: string): Promise<number> {
|
|
61
|
+
const storage = await this._state.storage.list({ prefix: USER_PREFIX });
|
|
62
|
+
|
|
63
|
+
return storage.size;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
public async getStorageState(room: string): Promise<string | null> {
|
|
67
|
+
const storage = await this._state.storage.get<string>(this._getStorageKey(room));
|
|
68
|
+
|
|
69
|
+
return storage ?? null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
public async getUser(room: string, connectionId: string): Promise<JsonObject | null> {
|
|
73
|
+
const user = await this._state.storage.get<JsonObject | null>(this._getUserKey(room, connectionId));
|
|
74
|
+
|
|
75
|
+
return user ?? null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
public async getUsers(room: string): Promise<readonly (JsonObject | null)[]> {
|
|
79
|
+
const storage = await this._state.storage.list({
|
|
80
|
+
prefix: USER_PREFIX,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
return Array.from(storage.values()) as (JsonObject | null)[];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
public async setStorageState(room: string, state: string): Promise<void> {
|
|
87
|
+
await this._state.storage.put(this._getStorageKey(room), state, {
|
|
88
|
+
allowConcurrency: true,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
private _getStorageKey(room: string): string {
|
|
93
|
+
return `${STORAGE_PREFIX}::${room}`;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private _getUserKey(room: string, connectionId: string): string {
|
|
97
|
+
return `${USER_PREFIX}::${room}::${connectionId}`;
|
|
98
|
+
}
|
|
99
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { partitionByLength } from "./partitionByLength";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export const partitionByLength = <T>(arr: readonly T[] | T[], length: number): readonly (readonly T[])[] => {
|
|
2
|
+
if (!arr.length) return [];
|
|
3
|
+
|
|
4
|
+
const head = arr.slice(0, length);
|
|
5
|
+
const tail = arr.slice(length);
|
|
6
|
+
|
|
7
|
+
return [head, ...partitionByLength(tail, length)];
|
|
8
|
+
};
|