@pluv/persistence-redis 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 +43 -0
- package/LICENSE +21 -0
- package/README.md +18 -0
- package/dist/index.d.mts +27 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +131 -0
- package/dist/index.mjs +105 -0
- package/package.json +41 -0
- package/src/PersistenceRedis.ts +104 -0
- package/src/index.ts +2 -0
- package/tsconfig.json +8 -0
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
> @pluv/persistence-redis@0.25.0 build /home/runner/work/pluv/pluv/packages/persistence-redis
|
|
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
|
+
[32mCJS[39m [1mdist/index.js [22m[32m4.37 KB[39m
|
|
12
|
+
[32mCJS[39m ⚡️ Build success in 68ms
|
|
13
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m3.36 KB[39m
|
|
14
|
+
[32mESM[39m ⚡️ Build success in 68ms
|
|
15
|
+
[34mDTS[39m Build start
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 3673ms
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.mts [22m[32m1.08 KB[39m
|
|
18
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m1.08 KB[39m
|
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# @pluv/persistence-redis
|
|
2
|
+
|
|
3
|
+
## 0.25.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 9db06ba: **BREAKING **
|
|
8
|
+
|
|
9
|
+
Fixed typos `persistance` to `persistence`.
|
|
10
|
+
|
|
11
|
+
This does mean that all properties referencing `persistance` will need to be fixed. Examples below:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
# Re-install @pluv/persistence-redis
|
|
15
|
+
pnpm uninstall @pluv/persistance-redis
|
|
16
|
+
pnpm install @pluv/persistence-redis
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
// Before
|
|
21
|
+
createIO({
|
|
22
|
+
platform: platformNode({
|
|
23
|
+
persistance: new PersistanceRedis(/* ... */),
|
|
24
|
+
}),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// After
|
|
28
|
+
createIO({
|
|
29
|
+
platform: platformNode({
|
|
30
|
+
persistence: new PersistenceRedis(/* ... */),
|
|
31
|
+
}),
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`@pluv/persistance-redis` has been deprecated for `@pluv/persistence-redis`.
|
|
36
|
+
|
|
37
|
+
### Patch Changes
|
|
38
|
+
|
|
39
|
+
- Updated dependencies [4e078ca]
|
|
40
|
+
- Updated dependencies [f556d30]
|
|
41
|
+
- Updated dependencies [9db06ba]
|
|
42
|
+
- @pluv/io@0.25.0
|
|
43
|
+
- @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,18 @@
|
|
|
1
|
+
## `@pluv/persistence-redis`
|
|
2
|
+
|
|
3
|
+
> Crdt storage persistence for [@pluv/io](https://www.npmjs.com/package/@pluv/io) rooms on distributed systems. Available for [Node.js](https://nodejs.org/) only.
|
|
4
|
+
|
|
5
|
+
**👉 See full documentation on [pluv.io](https://pluv.io/docs/introduction). 👈**
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
# npm
|
|
11
|
+
npm install @pluv/persistence-redis ioredis
|
|
12
|
+
|
|
13
|
+
# yarn
|
|
14
|
+
yarn add @pluv/persistence-redis ioredis
|
|
15
|
+
|
|
16
|
+
# pnpm
|
|
17
|
+
pnpm add @pluv/persistence-redis ioredis
|
|
18
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { AbstractPersistence } from '@pluv/io';
|
|
2
|
+
import { JsonObject } from '@pluv/types';
|
|
3
|
+
import { Cluster, Redis } from 'ioredis';
|
|
4
|
+
|
|
5
|
+
type RedisClient = Cluster | Redis;
|
|
6
|
+
interface PersistenceRedisOptions {
|
|
7
|
+
client: RedisClient;
|
|
8
|
+
}
|
|
9
|
+
declare class PersistenceRedis extends AbstractPersistence {
|
|
10
|
+
private _client;
|
|
11
|
+
constructor(options: PersistenceRedisOptions);
|
|
12
|
+
addUser(room: string, connectionId: string, user: JsonObject | null): Promise<void>;
|
|
13
|
+
deleteStorageState(room: string): Promise<void>;
|
|
14
|
+
deleteUser(room: string, connectionId: string): Promise<void>;
|
|
15
|
+
deleteUsers(room: string): Promise<void>;
|
|
16
|
+
getSize(room: string): Promise<number>;
|
|
17
|
+
getStorageState(room: string): Promise<string | null>;
|
|
18
|
+
setStorageState(room: string, state: string): Promise<void>;
|
|
19
|
+
getUser(room: string, connectionId: string): Promise<JsonObject | null>;
|
|
20
|
+
getUsers(room: string): Promise<readonly (JsonObject | null)[]>;
|
|
21
|
+
private _getClusterKey;
|
|
22
|
+
private _getRoomStorageKey;
|
|
23
|
+
private _getRoomUsersKey;
|
|
24
|
+
private _getRoomUsersUserKey;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { PersistenceRedis, type RedisClient };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { AbstractPersistence } from '@pluv/io';
|
|
2
|
+
import { JsonObject } from '@pluv/types';
|
|
3
|
+
import { Cluster, Redis } from 'ioredis';
|
|
4
|
+
|
|
5
|
+
type RedisClient = Cluster | Redis;
|
|
6
|
+
interface PersistenceRedisOptions {
|
|
7
|
+
client: RedisClient;
|
|
8
|
+
}
|
|
9
|
+
declare class PersistenceRedis extends AbstractPersistence {
|
|
10
|
+
private _client;
|
|
11
|
+
constructor(options: PersistenceRedisOptions);
|
|
12
|
+
addUser(room: string, connectionId: string, user: JsonObject | null): Promise<void>;
|
|
13
|
+
deleteStorageState(room: string): Promise<void>;
|
|
14
|
+
deleteUser(room: string, connectionId: string): Promise<void>;
|
|
15
|
+
deleteUsers(room: string): Promise<void>;
|
|
16
|
+
getSize(room: string): Promise<number>;
|
|
17
|
+
getStorageState(room: string): Promise<string | null>;
|
|
18
|
+
setStorageState(room: string, state: string): Promise<void>;
|
|
19
|
+
getUser(room: string, connectionId: string): Promise<JsonObject | null>;
|
|
20
|
+
getUsers(room: string): Promise<readonly (JsonObject | null)[]>;
|
|
21
|
+
private _getClusterKey;
|
|
22
|
+
private _getRoomStorageKey;
|
|
23
|
+
private _getRoomUsersKey;
|
|
24
|
+
private _getRoomUsersUserKey;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { PersistenceRedis, type RedisClient };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
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
|
+
PersistenceRedis: () => PersistenceRedis
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(src_exports);
|
|
46
|
+
|
|
47
|
+
// src/PersistenceRedis.ts
|
|
48
|
+
var import_io = require("@pluv/io");
|
|
49
|
+
var DELIMITER = ":::";
|
|
50
|
+
var HASH_TAG = "{PluvPersistenceRedis}";
|
|
51
|
+
var PersistenceRedis = class extends import_io.AbstractPersistence {
|
|
52
|
+
constructor(options) {
|
|
53
|
+
super();
|
|
54
|
+
const { client } = options;
|
|
55
|
+
this._client = client;
|
|
56
|
+
}
|
|
57
|
+
addUser(room, connectionId, user) {
|
|
58
|
+
return __async(this, null, function* () {
|
|
59
|
+
yield this._client.multi().sadd(this._getRoomUsersKey(room), connectionId).set(this._getRoomUsersUserKey(room, connectionId), JSON.stringify(user)).exec();
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
deleteStorageState(room) {
|
|
63
|
+
return __async(this, null, function* () {
|
|
64
|
+
yield this._client.del(this._getRoomStorageKey(room));
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
deleteUser(room, connectionId) {
|
|
68
|
+
return __async(this, null, function* () {
|
|
69
|
+
const size = yield this.getSize(room);
|
|
70
|
+
let multi = this._client.multi().srem(this._getRoomUsersKey(room), connectionId).del(this._getRoomUsersUserKey(room, connectionId));
|
|
71
|
+
if (size === 1) {
|
|
72
|
+
multi = multi.del(this._getRoomStorageKey(room)).del(this._getRoomUsersKey(room));
|
|
73
|
+
}
|
|
74
|
+
yield multi.exec();
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
deleteUsers(room) {
|
|
78
|
+
return __async(this, null, function* () {
|
|
79
|
+
const members = yield this._client.smembers(this._getRoomUsersKey(room));
|
|
80
|
+
yield members.reduce(
|
|
81
|
+
(commands, connectionId) => {
|
|
82
|
+
return commands.del(this._getRoomUsersUserKey(room, connectionId));
|
|
83
|
+
},
|
|
84
|
+
this._client.multi().del(this._getRoomUsersKey(room))
|
|
85
|
+
).exec();
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
getSize(room) {
|
|
89
|
+
return __async(this, null, function* () {
|
|
90
|
+
return yield this._client.scard(this._getRoomUsersKey(room));
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
getStorageState(room) {
|
|
94
|
+
return __async(this, null, function* () {
|
|
95
|
+
return yield this._client.get(this._getRoomStorageKey(room));
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
setStorageState(room, state) {
|
|
99
|
+
return __async(this, null, function* () {
|
|
100
|
+
yield this._client.set(this._getRoomStorageKey(room), state);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
getUser(room, connectionId) {
|
|
104
|
+
return __async(this, null, function* () {
|
|
105
|
+
const data = yield this._client.get(this._getRoomUsersUserKey(room, connectionId));
|
|
106
|
+
return !data ? null : JSON.parse(data);
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
getUsers(room) {
|
|
110
|
+
return __async(this, null, function* () {
|
|
111
|
+
const members = yield this._client.smembers(this._getRoomUsersKey(room));
|
|
112
|
+
return yield Promise.all(members.map((connectionId) => this.getUser(room, connectionId)));
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
_getClusterKey(key) {
|
|
116
|
+
return `${HASH_TAG}${key}`;
|
|
117
|
+
}
|
|
118
|
+
_getRoomStorageKey(room) {
|
|
119
|
+
return this._getClusterKey(`room_storage${DELIMITER}${room}`);
|
|
120
|
+
}
|
|
121
|
+
_getRoomUsersKey(room) {
|
|
122
|
+
return this._getClusterKey(`room_users${DELIMITER}${room}`);
|
|
123
|
+
}
|
|
124
|
+
_getRoomUsersUserKey(room, connectionId) {
|
|
125
|
+
return this._getClusterKey(`${this._getRoomUsersKey(room)}${DELIMITER}${connectionId}`);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
129
|
+
0 && (module.exports = {
|
|
130
|
+
PersistenceRedis
|
|
131
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
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/PersistenceRedis.ts
|
|
23
|
+
import { AbstractPersistence } from "@pluv/io";
|
|
24
|
+
var DELIMITER = ":::";
|
|
25
|
+
var HASH_TAG = "{PluvPersistenceRedis}";
|
|
26
|
+
var PersistenceRedis = class extends AbstractPersistence {
|
|
27
|
+
constructor(options) {
|
|
28
|
+
super();
|
|
29
|
+
const { client } = options;
|
|
30
|
+
this._client = client;
|
|
31
|
+
}
|
|
32
|
+
addUser(room, connectionId, user) {
|
|
33
|
+
return __async(this, null, function* () {
|
|
34
|
+
yield this._client.multi().sadd(this._getRoomUsersKey(room), connectionId).set(this._getRoomUsersUserKey(room, connectionId), JSON.stringify(user)).exec();
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
deleteStorageState(room) {
|
|
38
|
+
return __async(this, null, function* () {
|
|
39
|
+
yield this._client.del(this._getRoomStorageKey(room));
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
deleteUser(room, connectionId) {
|
|
43
|
+
return __async(this, null, function* () {
|
|
44
|
+
const size = yield this.getSize(room);
|
|
45
|
+
let multi = this._client.multi().srem(this._getRoomUsersKey(room), connectionId).del(this._getRoomUsersUserKey(room, connectionId));
|
|
46
|
+
if (size === 1) {
|
|
47
|
+
multi = multi.del(this._getRoomStorageKey(room)).del(this._getRoomUsersKey(room));
|
|
48
|
+
}
|
|
49
|
+
yield multi.exec();
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
deleteUsers(room) {
|
|
53
|
+
return __async(this, null, function* () {
|
|
54
|
+
const members = yield this._client.smembers(this._getRoomUsersKey(room));
|
|
55
|
+
yield members.reduce(
|
|
56
|
+
(commands, connectionId) => {
|
|
57
|
+
return commands.del(this._getRoomUsersUserKey(room, connectionId));
|
|
58
|
+
},
|
|
59
|
+
this._client.multi().del(this._getRoomUsersKey(room))
|
|
60
|
+
).exec();
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
getSize(room) {
|
|
64
|
+
return __async(this, null, function* () {
|
|
65
|
+
return yield this._client.scard(this._getRoomUsersKey(room));
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
getStorageState(room) {
|
|
69
|
+
return __async(this, null, function* () {
|
|
70
|
+
return yield this._client.get(this._getRoomStorageKey(room));
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
setStorageState(room, state) {
|
|
74
|
+
return __async(this, null, function* () {
|
|
75
|
+
yield this._client.set(this._getRoomStorageKey(room), state);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
getUser(room, connectionId) {
|
|
79
|
+
return __async(this, null, function* () {
|
|
80
|
+
const data = yield this._client.get(this._getRoomUsersUserKey(room, connectionId));
|
|
81
|
+
return !data ? null : JSON.parse(data);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
getUsers(room) {
|
|
85
|
+
return __async(this, null, function* () {
|
|
86
|
+
const members = yield this._client.smembers(this._getRoomUsersKey(room));
|
|
87
|
+
return yield Promise.all(members.map((connectionId) => this.getUser(room, connectionId)));
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
_getClusterKey(key) {
|
|
91
|
+
return `${HASH_TAG}${key}`;
|
|
92
|
+
}
|
|
93
|
+
_getRoomStorageKey(room) {
|
|
94
|
+
return this._getClusterKey(`room_storage${DELIMITER}${room}`);
|
|
95
|
+
}
|
|
96
|
+
_getRoomUsersKey(room) {
|
|
97
|
+
return this._getClusterKey(`room_users${DELIMITER}${room}`);
|
|
98
|
+
}
|
|
99
|
+
_getRoomUsersUserKey(room, connectionId) {
|
|
100
|
+
return this._getClusterKey(`${this._getRoomUsersKey(room)}${DELIMITER}${connectionId}`);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
export {
|
|
104
|
+
PersistenceRedis
|
|
105
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pluv/persistence-redis",
|
|
3
|
+
"version": "0.25.0",
|
|
4
|
+
"description": "@pluv/io crdt storage persistence for node.js",
|
|
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-redis"
|
|
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
|
+
"@types/node": "^22.5.0",
|
|
21
|
+
"@pluv/io": "^0.25.0",
|
|
22
|
+
"@pluv/types": "^0.25.0"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"ioredis": "^5.0.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"eslint": "^8.57.0",
|
|
29
|
+
"ioredis": "^5.4.1",
|
|
30
|
+
"tsup": "^8.2.4",
|
|
31
|
+
"typescript": "^5.5.4",
|
|
32
|
+
"@pluv/tsconfig": "^0.25.0",
|
|
33
|
+
"eslint-config-pluv": "^0.25.0"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
37
|
+
"dev": "tsup src/index.ts --format esm,cjs --watch --dts",
|
|
38
|
+
"lint": "eslint src/**/*.ts* --fix",
|
|
39
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { AbstractPersistence } from "@pluv/io";
|
|
2
|
+
import type { JsonObject } from "@pluv/types";
|
|
3
|
+
import type { Cluster, Redis } from "ioredis";
|
|
4
|
+
|
|
5
|
+
const DELIMITER = ":::";
|
|
6
|
+
const HASH_TAG = "{PluvPersistenceRedis}";
|
|
7
|
+
|
|
8
|
+
export type RedisClient = Cluster | Redis;
|
|
9
|
+
|
|
10
|
+
export interface PersistenceRedisOptions {
|
|
11
|
+
client: RedisClient;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class PersistenceRedis extends AbstractPersistence {
|
|
15
|
+
private _client: RedisClient;
|
|
16
|
+
|
|
17
|
+
constructor(options: PersistenceRedisOptions) {
|
|
18
|
+
super();
|
|
19
|
+
|
|
20
|
+
const { client } = options;
|
|
21
|
+
|
|
22
|
+
this._client = client;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public async addUser(room: string, connectionId: string, user: JsonObject | null): Promise<void> {
|
|
26
|
+
await this._client
|
|
27
|
+
.multi()
|
|
28
|
+
.sadd(this._getRoomUsersKey(room), connectionId)
|
|
29
|
+
.set(this._getRoomUsersUserKey(room, connectionId), JSON.stringify(user))
|
|
30
|
+
.exec();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public async deleteStorageState(room: string): Promise<void> {
|
|
34
|
+
await this._client.del(this._getRoomStorageKey(room));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public async deleteUser(room: string, connectionId: string): Promise<void> {
|
|
38
|
+
const size = await this.getSize(room);
|
|
39
|
+
|
|
40
|
+
let multi = this._client
|
|
41
|
+
.multi()
|
|
42
|
+
.srem(this._getRoomUsersKey(room), connectionId)
|
|
43
|
+
.del(this._getRoomUsersUserKey(room, connectionId));
|
|
44
|
+
|
|
45
|
+
if (size === 1) {
|
|
46
|
+
multi = multi.del(this._getRoomStorageKey(room)).del(this._getRoomUsersKey(room));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
await multi.exec();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public async deleteUsers(room: string): Promise<void> {
|
|
53
|
+
const members = await this._client.smembers(this._getRoomUsersKey(room));
|
|
54
|
+
|
|
55
|
+
await members
|
|
56
|
+
.reduce(
|
|
57
|
+
(commands, connectionId) => {
|
|
58
|
+
return commands.del(this._getRoomUsersUserKey(room, connectionId));
|
|
59
|
+
},
|
|
60
|
+
this._client.multi().del(this._getRoomUsersKey(room)),
|
|
61
|
+
)
|
|
62
|
+
.exec();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
public async getSize(room: string): Promise<number> {
|
|
66
|
+
return await this._client.scard(this._getRoomUsersKey(room));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public async getStorageState(room: string): Promise<string | null> {
|
|
70
|
+
return await this._client.get(this._getRoomStorageKey(room));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public async setStorageState(room: string, state: string): Promise<void> {
|
|
74
|
+
await this._client.set(this._getRoomStorageKey(room), state);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public async getUser(room: string, connectionId: string): Promise<JsonObject | null> {
|
|
78
|
+
const data = await this._client.get(this._getRoomUsersUserKey(room, connectionId));
|
|
79
|
+
|
|
80
|
+
return !data ? null : JSON.parse(data);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
public async getUsers(room: string): Promise<readonly (JsonObject | null)[]> {
|
|
84
|
+
const members = await this._client.smembers(this._getRoomUsersKey(room));
|
|
85
|
+
|
|
86
|
+
return await Promise.all(members.map((connectionId) => this.getUser(room, connectionId)));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private _getClusterKey(key: string): string {
|
|
90
|
+
return `${HASH_TAG}${key}`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private _getRoomStorageKey(room: string): string {
|
|
94
|
+
return this._getClusterKey(`room_storage${DELIMITER}${room}`);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
private _getRoomUsersKey(room: string): string {
|
|
98
|
+
return this._getClusterKey(`room_users${DELIMITER}${room}`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private _getRoomUsersUserKey(room: string, connectionId: string): string {
|
|
102
|
+
return this._getClusterKey(`${this._getRoomUsersKey(room)}${DELIMITER}${connectionId}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
package/src/index.ts
ADDED