express-session-rethinkdb-esm 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Robert Sirois
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
File without changes
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "express-session-rethinkdb-esm",
3
+ "version": "0.0.1",
4
+ "license": "MIT",
5
+ "author": "Robert Sirois",
6
+ "homepage": "https://github.com/rpsirois/express-session-rethinkdb-esm#readme",
7
+ "bugs": {
8
+ "url": "https://github.com/rpsirois/express-session-rethinkdb-esm/issues"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git@github.com:rpsirois/express-session-rethinkdb-esm.git"
13
+ },
14
+ "type": "module",
15
+ "exports": {
16
+ ".": "./rdb-session.js"
17
+ },
18
+ "dependencies": {},
19
+ "devDependencies": {},
20
+ "scripts": {}
21
+ }
package/rdb-session.js ADDED
@@ -0,0 +1,130 @@
1
+ const defaults = {
2
+ ttl: 1000 * 60 * 60 * 24 * 14 ,// 2 weeks
3
+ table: 'sessions',
4
+ }
5
+
6
+ export default cfg => (
7
+ class RethinkdbSessionStore extends cfg.session.Store {
8
+ constructor( opt ) {
9
+ super(opt)
10
+ Object.assign(this, defaults, opt)
11
+ if (! this.client) throw new Error('RethinkdbSessionStore requires a client in setup options.')
12
+ if (! this.conn) throw new Error('RethinkdbSessionStore requires a connection in setup options.')
13
+ this.initDb(this.client, this.conn)
14
+ }
15
+
16
+ getExpiry( cookie ) {
17
+ return cookie?.expires
18
+ ? new Date(cookie.expires)
19
+ : new Date(Date.now() + this.ttl)
20
+ }
21
+
22
+ async initDb( r, rc ) {
23
+ let { table } = this
24
+ let existing_tables = await r.tableList().run(rc)
25
+
26
+ if (existing_tables.indexOf( table ) < 0)
27
+ await r.tableCreate( table ).run(rc)
28
+ this._t = r.table(table)
29
+ }
30
+
31
+ async all( cb ) {
32
+ try {
33
+ let result = await this._t.run( this.conn ).toArray()
34
+ cb?.( null, result )
35
+ return result
36
+ } catch (err) {
37
+ if (!cb) throw err
38
+ cb(err)
39
+ }
40
+ }
41
+
42
+ async destroy( sid, cb ) {
43
+ try {
44
+ let result = await this._t.get( sid )
45
+ .delete()
46
+ .run(this.conn)
47
+ cb?.(null, result)
48
+ return result
49
+ } catch (err) {
50
+ cb(err)
51
+ }
52
+ }
53
+
54
+ async clear( cb ) {
55
+ try {
56
+ let result = await this._t
57
+ .delete()
58
+ .run(this.conn)
59
+ cb?.( null, result )
60
+ return result
61
+ } catch (err) {
62
+ if (!cb) throw err
63
+ cb(err)
64
+ }
65
+ }
66
+
67
+ async length( cb ) {
68
+ try {
69
+ let result = await this._t
70
+ .count()
71
+ .run(this.conn)
72
+ cb?.( null, result )
73
+ return result
74
+ } catch (err) {
75
+ if (!cb) throw err
76
+ cb(err)
77
+ }
78
+ }
79
+
80
+ async get( sid, cb ) {
81
+ try {
82
+ let result = await this._t.get( sid )
83
+ .run(this.conn)
84
+ result = !result ? null
85
+ : Date.now() > result.expires.getTime() ? null
86
+ : JSON.parse( result.session )
87
+ cb?.(null, result)
88
+ return result
89
+ } catch (err) {
90
+ if (!cb) throw err
91
+ cb(err)
92
+ }
93
+ }
94
+
95
+ async set( sid, session, cb ) {
96
+ try {
97
+ let obj = {
98
+ id: sid,
99
+ session: JSON.stringify(session),
100
+ expires: this.getExpiry(session?.cookie),
101
+ }
102
+
103
+ let ans = await this._t.get( sid )
104
+ .replace( obj )
105
+ .run( this.conn )
106
+
107
+ cb?.()
108
+ } catch (err) {
109
+ if (!cb) throw err
110
+ cb(err)
111
+ }
112
+ }
113
+
114
+ async touch( sid, session, cb ) {
115
+ try {
116
+ let obj = {
117
+ expires: this.getExpiry( session?.cookie ),
118
+ }
119
+
120
+ let ans = await this._t.get( sid )
121
+ .update( obj )
122
+ .run( this.conn )
123
+
124
+ cb?.()
125
+ } catch (err) {
126
+ if (!cb) throw err
127
+ cb(err)
128
+ }
129
+ }
130
+ })