fastify-session-better-sqlite3-store 1.0.6 → 1.0.9
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/.github/workflows/ci.yml +16 -27
- package/.github/workflows/standard.yml +1 -1
- package/README.md +7 -6
- package/index.js +9 -1
- package/package.json +3 -2
- package/test/first.test.js +12 -5
- package/test/second.test.js +11 -5
package/.github/workflows/ci.yml
CHANGED
|
@@ -2,36 +2,25 @@ name: ci
|
|
|
2
2
|
|
|
3
3
|
on:
|
|
4
4
|
push:
|
|
5
|
-
branches: [ main ]
|
|
5
|
+
branches: [ "main" ]
|
|
6
6
|
pull_request:
|
|
7
|
-
branches: [ main ]
|
|
7
|
+
branches: [ "main" ]
|
|
8
8
|
|
|
9
9
|
jobs:
|
|
10
|
+
ci:
|
|
11
|
+
runs-on: ${{ matrix.os }}
|
|
10
12
|
|
|
11
|
-
|
|
13
|
+
strategy:
|
|
14
|
+
matrix:
|
|
15
|
+
os: [ubuntu-latest, windows-latest, macOs-latest]
|
|
16
|
+
node-version: [14.x, 16.x, 18.x]
|
|
12
17
|
|
|
13
|
-
runs-on: ubuntu-latest
|
|
14
|
-
|
|
15
18
|
steps:
|
|
16
|
-
|
|
17
|
-
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
tests-windows:
|
|
26
|
-
|
|
27
|
-
runs-on: windows-latest
|
|
28
|
-
|
|
29
|
-
steps:
|
|
30
|
-
|
|
31
|
-
- uses: actions/checkout@v2
|
|
32
|
-
|
|
33
|
-
- name: Install Dependencies
|
|
34
|
-
run: npm install
|
|
35
|
-
|
|
36
|
-
- name: run tests
|
|
37
|
-
run: npm run test
|
|
19
|
+
- uses: actions/checkout@v3
|
|
20
|
+
- name: Use Node.js ${{ matrix.node-version }}
|
|
21
|
+
uses: actions/setup-node@v3
|
|
22
|
+
with:
|
|
23
|
+
node-version: ${{ matrix.node-version }}
|
|
24
|
+
cache: 'npm'
|
|
25
|
+
- run: npm ci
|
|
26
|
+
- run: npm test
|
package/README.md
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|

|
|
4
4
|

|
|
5
|
-
|
|
5
|
+

|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
7
|
|
|
7
|
-
|
|
8
|
+
A [better-sqlite3](https://github.com/WiseLibs/better-sqlite3) session store for [@fastify/session](https://github.com/fastify/session). By default [@fastify/session](https://github.com/fastify/session) uses in-memory storage to store sessions. With this small package you can store sessions on an **SQLite3** database instead.
|
|
8
9
|
|
|
9
10
|
## Installation
|
|
10
11
|
|
|
@@ -17,20 +18,20 @@ npm install fastify-session-better-sqlite3-store
|
|
|
17
18
|
Use with `fastify-session`'s `store` property.
|
|
18
19
|
|
|
19
20
|
```js
|
|
20
|
-
const fastify = require('fastify')({logger:true})
|
|
21
|
+
const fastify = require('fastify')({ logger: true })
|
|
21
22
|
const fastifyCookie = require('@fastify/cookie')
|
|
22
23
|
const fastifySession = require('@fastify/session')
|
|
23
|
-
const db = require('better-sqlite3')(
|
|
24
|
+
const db = require('better-sqlite3')('./sqlite.db')
|
|
24
25
|
|
|
25
26
|
// require module
|
|
26
27
|
const SqliteStore = require('fastify-session-better-sqlite3-store')
|
|
27
28
|
|
|
28
29
|
fastify.register(fastifyCookie)
|
|
29
|
-
fastify.register(fastifySession,{
|
|
30
|
+
fastify.register(fastifySession, {
|
|
31
|
+
store: new SqliteStore(db),
|
|
30
32
|
// ...
|
|
31
33
|
// other session options
|
|
32
34
|
// ...
|
|
33
|
-
store: new SqliteStore(db)
|
|
34
35
|
})
|
|
35
36
|
```
|
|
36
37
|
|
package/index.js
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
+
const NODE_VERSION_REQUIREMENT = 14
|
|
4
|
+
|
|
5
|
+
const nodeVersion = Number(process.version.match(/^v(\d+)/)[1])
|
|
6
|
+
if (nodeVersion < NODE_VERSION_REQUIREMENT) {
|
|
7
|
+
throw new Error(`node '${process.version}' not supported, needs 'v14.x' or greater`)
|
|
8
|
+
}
|
|
9
|
+
|
|
3
10
|
const EventEmitter = require('events')
|
|
4
11
|
|
|
5
12
|
class SqliteStore extends EventEmitter {
|
|
@@ -18,9 +25,10 @@ class SqliteStore extends EventEmitter {
|
|
|
18
25
|
)
|
|
19
26
|
} catch (err) {
|
|
20
27
|
if (err.toString() !== 'SqliteError: table session already exists') {
|
|
21
|
-
throw
|
|
28
|
+
throw err
|
|
22
29
|
}
|
|
23
30
|
}
|
|
31
|
+
|
|
24
32
|
super()
|
|
25
33
|
this.setSession = sqlite3db.prepare(`INSERT INTO ${table} (sid, expires, session) VALUES (?, ?, ?)`)
|
|
26
34
|
this.getSession = sqlite3db.prepare(`SELECT sid, expires, session FROM ${table} WHERE sid = ?`)
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fastify-session-better-sqlite3-store",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"description": "A simple session store for fastify-session using better-sqlite3",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"lint": "npx standard *.js",
|
|
7
|
+
"lint": "npx standard *.js test/*.js",
|
|
8
|
+
"lint-fix": "npx standard --fix *.js test/*.js",
|
|
8
9
|
"test": "node test/first.test && node test/second.test"
|
|
9
10
|
},
|
|
10
11
|
"repository": {
|
package/test/first.test.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const fastify = require('fastify')({ logger:
|
|
1
|
+
const fastify = require('fastify')({ logger: false })
|
|
2
2
|
const fastifyCookie = require('@fastify/cookie')
|
|
3
3
|
const fastifySession = require('@fastify/session')
|
|
4
4
|
const sqlite3db = require('better-sqlite3')('./test/sqlite.db')
|
|
@@ -53,20 +53,27 @@ async function start () {
|
|
|
53
53
|
|
|
54
54
|
const subject1 = await httpGetRequest(fastify, '/session')
|
|
55
55
|
const subject2 = await httpGetRequest(fastify, '/set/subject2')
|
|
56
|
+
|
|
56
57
|
const cookie1 = {
|
|
57
58
|
sessionId: subject2.cookies[0].value
|
|
58
59
|
}
|
|
60
|
+
|
|
59
61
|
const subject3 = await httpGetRequest(fastify, '/session', cookie1)
|
|
60
62
|
const subject4 = await httpGetRequest(fastify, '/destroy', cookie1)
|
|
61
63
|
const subject5 = await httpGetRequest(fastify, '/session', cookie1)
|
|
62
64
|
const subject6 = await httpGetRequest(fastify, '/set/subject6')
|
|
65
|
+
|
|
63
66
|
const cookie2 = {
|
|
64
67
|
sessionId: subject6.cookies[0].value
|
|
65
68
|
}
|
|
69
|
+
|
|
66
70
|
const subject7 = await httpGetRequest(fastify, '/session', cookie2)
|
|
67
|
-
|
|
71
|
+
|
|
72
|
+
await setTimeoutAsync(6000)
|
|
73
|
+
|
|
68
74
|
const subject8 = await httpGetRequest(fastify, '/session', cookie2)
|
|
69
75
|
const subject9 = await httpGetRequest(fastify, '/set/subject9')
|
|
76
|
+
|
|
70
77
|
const cookie3 = {
|
|
71
78
|
sessionId: subject9.cookies[0].value
|
|
72
79
|
}
|
|
@@ -85,13 +92,13 @@ async function start () {
|
|
|
85
92
|
await saveJsonFile('test/cookie.json', cookie3)
|
|
86
93
|
|
|
87
94
|
if (t.results()) {
|
|
88
|
-
console.log(t.testName, '=> PASSED ALL TEST\n')
|
|
95
|
+
console.log('\n', t.testName, '=> PASSED ALL TEST\n')
|
|
89
96
|
} else {
|
|
90
|
-
console.log(t.testName, '=> FAILED SOME TEST\n')
|
|
97
|
+
console.log('\n', t.testName, '=> FAILED SOME TEST\n')
|
|
91
98
|
process.exit(1)
|
|
92
99
|
}
|
|
93
100
|
} catch (err) {
|
|
94
|
-
|
|
101
|
+
console.error(err)
|
|
95
102
|
process.exit(1)
|
|
96
103
|
}
|
|
97
104
|
}
|
package/test/second.test.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const fastify = require('fastify')({ logger:
|
|
1
|
+
const fastify = require('fastify')({ logger: false })
|
|
2
2
|
const fastifyCookie = require('@fastify/cookie')
|
|
3
3
|
const fastifySession = require('@fastify/session')
|
|
4
4
|
const sqlite3db = require('better-sqlite3')('./test/sqlite.db')
|
|
@@ -24,7 +24,7 @@ fastify.get('/session', (request, reply) => {
|
|
|
24
24
|
}
|
|
25
25
|
})
|
|
26
26
|
|
|
27
|
-
const { SmallTest, httpGetRequest, readJsonFile } = require('./SmallTest')
|
|
27
|
+
const { SmallTest, httpGetRequest, readJsonFile, setTimeoutAsync } = require('./SmallTest')
|
|
28
28
|
|
|
29
29
|
async function start () {
|
|
30
30
|
const t = new SmallTest('TEST SET CASE 2')
|
|
@@ -33,18 +33,24 @@ async function start () {
|
|
|
33
33
|
|
|
34
34
|
const cookie = await readJsonFile('test/cookie.json')
|
|
35
35
|
const subject9 = await httpGetRequest(fastify, '/session', cookie)
|
|
36
|
+
|
|
37
|
+
await setTimeoutAsync(6000)
|
|
38
|
+
|
|
39
|
+
const subject10 = await httpGetRequest(fastify, '/session', cookie)
|
|
40
|
+
|
|
36
41
|
t.assertEqual('session existing after server restart', subject9.payload, 'session:subject9')
|
|
42
|
+
t.assertEqual('session expired', subject10.payload, 'session:no-user')
|
|
37
43
|
|
|
38
44
|
await fastify.close()
|
|
39
45
|
|
|
40
46
|
if (t.results()) {
|
|
41
|
-
console.log(t.testName, '=> PASSED ALL TEST\n')
|
|
47
|
+
console.log('\n', t.testName, '=> PASSED ALL TEST\n')
|
|
42
48
|
} else {
|
|
43
|
-
console.log(t.testName, '=> FAILED SOME TEST\n')
|
|
49
|
+
console.log('\n', t.testName, '=> FAILED SOME TEST\n')
|
|
44
50
|
process.exit(1)
|
|
45
51
|
}
|
|
46
52
|
} catch (err) {
|
|
47
|
-
|
|
53
|
+
console.error(err)
|
|
48
54
|
process.exit(1)
|
|
49
55
|
}
|
|
50
56
|
}
|