polystore 0.18.0 → 0.20.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/src/express.js ADDED
@@ -0,0 +1,47 @@
1
+ import session from "express-session";
2
+ import kv from "../index.js";
3
+
4
+ const ttlFromSession = (data) => {
5
+ const maxAge = data?.cookie?.originalMaxAge;
6
+ return typeof maxAge === "number" ? Math.ceil(maxAge / 1000) : null;
7
+ };
8
+
9
+ export class PolystoreSessionStore extends session.Store {
10
+ constructor(store) {
11
+ super();
12
+ this.store = store;
13
+ }
14
+
15
+ prefix(prefix = "") {
16
+ return new PolystoreSessionStore(this.store.prefix(prefix));
17
+ }
18
+
19
+ get(sid, cb) {
20
+ this.store.get(sid).then((data) => cb(null, data)).catch(cb);
21
+ }
22
+
23
+ set(sid, data, cb) {
24
+ this.store
25
+ .set(sid, data, ttlFromSession(data))
26
+ .then(() => cb && cb())
27
+ .catch((error) => cb && cb(error));
28
+ }
29
+
30
+ destroy(sid, cb) {
31
+ this.store
32
+ .del(sid)
33
+ .then(() => cb && cb())
34
+ .catch((error) => cb && cb(error));
35
+ }
36
+
37
+ touch(sid, data, cb) {
38
+ this.store
39
+ .set(sid, data, ttlFromSession(data))
40
+ .then(() => cb && cb())
41
+ .catch((error) => cb && cb(error));
42
+ }
43
+ }
44
+
45
+ export default function expressStore(client = new Map()) {
46
+ return new PolystoreSessionStore(kv(client));
47
+ }