soul-cli 0.8.1 → 0.9.3
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/.all-contributorsrc +9 -0
- package/.bruno/auth/folder.bru +8 -0
- package/.bruno/auth/obtain-access-token.bru +25 -0
- package/.bruno/bruno.json +6 -0
- package/.bruno/environments/local.bru +3 -0
- package/.bruno/rows/folder.bru +8 -0
- package/.bruno/rows/list-rows-with-ordering.bru +25 -0
- package/.bruno/tables/folder.bru +8 -0
- package/.bruno/tables/list-tables.bru +16 -0
- package/.env.sample +11 -2
- package/.github/dependabot.yml +9 -0
- package/.github/workflows/ci.yml +33 -0
- package/.github/workflows/npm-publish.yml +19 -6
- package/.husky/pre-commit +0 -3
- package/README.md +46 -7
- package/ROADMAP.md +154 -0
- package/assets/images/soul-RA-app.png +0 -0
- package/assets/images/soul-standalone.png +0 -0
- package/docs/extensions-examples.md +96 -16
- package/docs/self-hosting.md +54 -0
- package/docs/ws-examples.md +4 -0
- package/eslint.config.js +29 -0
- package/package.json +40 -20
- package/scripts/dev.sh +64 -0
- package/src/cli.test.js +128 -0
- package/src/config/index.js +10 -2
- package/src/config/index.test.js +120 -0
- package/src/controllers/auth/common.js +1 -16
- package/src/controllers/auth/common.test.js +23 -0
- package/src/controllers/auth/tables.js +1 -1
- package/src/controllers/auth/token.js +30 -7
- package/src/controllers/auth/user.js +20 -20
- package/src/controllers/auth/user.test.js +232 -0
- package/src/controllers/auth.test.js +445 -0
- package/src/controllers/rows.js +183 -120
- package/src/controllers/rows.test.js +119 -2
- package/src/controllers/tables.js +63 -17
- package/src/controllers/tables.test.js +185 -0
- package/src/db/index.test.js +21 -0
- package/src/extensions.js +105 -36
- package/src/extensions.test.js +340 -0
- package/src/index.js +29 -20
- package/src/index.test.js +118 -0
- package/src/middlewares/api.js +1 -1
- package/src/middlewares/api.test.js +29 -0
- package/src/middlewares/auth.js +11 -48
- package/src/middlewares/auth.test.js +105 -0
- package/src/middlewares/validation.test.js +60 -0
- package/src/schemas/index.js +3 -1
- package/src/schemas/rows.js +16 -6
- package/src/schemas/tables.js +28 -4
- package/src/server.js +83 -1
- package/src/services/authService.js +33 -3
- package/src/services/authService.test.js +115 -0
- package/src/services/rowService.js +39 -14
- package/src/services/rowService.test.js +153 -0
- package/src/services/tableService.js +15 -17
- package/src/services/tableService.test.js +190 -0
- package/src/swagger/swagger.json +4 -7
- package/src/tests/globalSetup.js +8 -1
- package/src/tests/index.js +23 -0
- package/src/utils/index.js +25 -2
- package/src/utils/index.test.js +135 -0
- package/src/utils/logger.js +15 -0
- package/src/utils/sql.js +112 -0
- package/src/utils/sql.test.js +115 -0
- package/src/websocket.js +46 -8
- package/src/websocket.test.js +121 -0
- package/.eslintignore +0 -2
- package/.eslintrc.js +0 -24
- package/src/middlewares/logger.js +0 -13
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# Hosting Static Content Using Soul
|
|
2
|
+
|
|
3
|
+
You can host static content using Soul by utilizing its extensions feature. This allows you to expose your static application through Soul, enabling users to access your content without hosting multiple applications.
|
|
4
|
+
|
|
5
|
+
Here are diagrams showing how to access Soul APIs when running standalone and how to expose a static client via extensions.
|
|
6
|
+
|
|
7
|
+
**Accessing Soul APIs**
|
|
8
|
+

|
|
9
|
+
|
|
10
|
+
**Accessing a React Admin Application via Soul**
|
|
11
|
+

|
|
12
|
+
|
|
13
|
+
## Steps
|
|
14
|
+
|
|
15
|
+
In this guide, we will demonstrate how to host a static `React Admin` application.
|
|
16
|
+
|
|
17
|
+
1. Create an `_extensions` folder and add an `api.js` file to it.:
|
|
18
|
+
```sh
|
|
19
|
+
mkdir _extensions && touch _extensions/api.js
|
|
20
|
+
```
|
|
21
|
+
2. Add the following code to the `api.js` file:
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
const reactAdminApp = {
|
|
25
|
+
method: "GET",
|
|
26
|
+
path: "/api/client",
|
|
27
|
+
handler: (req, res, db) => {
|
|
28
|
+
const clientPath = path.join(__dirname, "../dist", "index.html");
|
|
29
|
+
res.app.use(express.static(path.join(__dirname, "../dist")));
|
|
30
|
+
res.sendFile(clientPath);
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
3. Build your React Admin client:
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
npm run build
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
4. Copy the `dist` folder from your `React Admin` project to the `_extensions` folder:
|
|
42
|
+
|
|
43
|
+
```sh
|
|
44
|
+
cp -r dist <path/to/_extensions>
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
5. Run your Soul application:
|
|
48
|
+
```sh
|
|
49
|
+
soul -d foobar.db --extensions /path/to/_extensions/
|
|
50
|
+
```
|
|
51
|
+
6. To verify that the app is working, open the following URL in your browser:
|
|
52
|
+
```
|
|
53
|
+
http://localhost:<port>/api/client
|
|
54
|
+
```
|
package/docs/ws-examples.md
CHANGED
|
@@ -41,3 +41,7 @@ soul -d ./Chinook_Sqlite.sqlite -p 8000
|
|
|
41
41
|
## Namespaces
|
|
42
42
|
|
|
43
43
|
1. [/ws/tables](ws/rows-examples.md) Examples for Rows websockets
|
|
44
|
+
|
|
45
|
+
## Authentication
|
|
46
|
+
|
|
47
|
+
When Soul is running with `-a` (auth mode), WebSocket connections require the same `accessToken` cookie used by the REST API, and are subject to the same per-table read permission checks. Connect with the cookie set (e.g. `wscat --header "Cookie: accessToken=<token>"` or a browser session that already has it) -- connections without a valid, authorized token are closed with an error message.
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
const js = require('@eslint/js');
|
|
2
|
+
const globals = require('globals');
|
|
3
|
+
const eslintConfigPrettier = require('eslint-config-prettier');
|
|
4
|
+
|
|
5
|
+
module.exports = [
|
|
6
|
+
{
|
|
7
|
+
ignores: ['node_modules/**', '_extensions/**'],
|
|
8
|
+
},
|
|
9
|
+
js.configs.recommended,
|
|
10
|
+
{
|
|
11
|
+
languageOptions: {
|
|
12
|
+
ecmaVersion: 'latest',
|
|
13
|
+
sourceType: 'commonjs',
|
|
14
|
+
globals: {
|
|
15
|
+
...globals.node,
|
|
16
|
+
...globals.es2021,
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
files: ['**/*.test.js', 'src/tests/**'],
|
|
22
|
+
languageOptions: {
|
|
23
|
+
globals: {
|
|
24
|
+
...globals.jest,
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
eslintConfigPrettier,
|
|
29
|
+
];
|
package/package.json
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "soul-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.3",
|
|
4
4
|
"description": "A SQLite REST and Realtime server",
|
|
5
5
|
"main": "src/server.js",
|
|
6
6
|
"bin": {
|
|
7
7
|
"soul": "./src/server.js"
|
|
8
8
|
},
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": "22.x || 23.x || 24.x || 25.x || 26.x"
|
|
11
|
+
},
|
|
9
12
|
"scripts": {
|
|
10
13
|
"start": "node src/server.js",
|
|
11
14
|
"dev": "npm run swagger-autogen && cross-env NO_CLI=true nodemon src/server.js",
|
|
12
15
|
"cli": "nodemon src/server.js --database foobar.db",
|
|
13
16
|
"swagger-autogen": "cross-env NO_CLI=true node src/swagger/index.js",
|
|
14
|
-
"test": "cross-env CI=true NODE_ENV=test NO_CLI=true DB=test.db CORE_PORT=8001 jest --testTimeout=10000",
|
|
15
|
-
"
|
|
17
|
+
"test": "cross-env CI=true NODE_ENV=test NO_CLI=true DB=test.db CORE_PORT=8001 AUTH=true TOKEN_SECRET=test-only-ci-secret-0123456789 INITIAL_USER_USERNAME=admin INITIAL_USER_PASSWORD=Str0ngTestPw!1 jest --testTimeout=10000 --runInBand --forceExit",
|
|
18
|
+
"test:coverage": "npm test -- --coverage --coverageProvider=v8",
|
|
19
|
+
"prepare": "husky",
|
|
16
20
|
"lint": "eslint . --fix --max-warnings=0",
|
|
17
21
|
"format": "prettier . --write"
|
|
18
22
|
},
|
|
@@ -27,40 +31,56 @@
|
|
|
27
31
|
},
|
|
28
32
|
"homepage": "https://github.com/thevahidal/soul#readme",
|
|
29
33
|
"dependencies": {
|
|
30
|
-
"bcrypt": "^
|
|
31
|
-
"better-sqlite3": "^
|
|
34
|
+
"bcrypt": "^6.0.0",
|
|
35
|
+
"better-sqlite3": "^13.0.3",
|
|
32
36
|
"body-parser": "^1.20.2",
|
|
33
37
|
"check-password-strength": "^2.0.7",
|
|
38
|
+
"cookie": "^0.7.2",
|
|
34
39
|
"cookie-parser": "^1.4.6",
|
|
35
40
|
"cors": "^2.8.5",
|
|
36
|
-
"dotenv": "^
|
|
37
|
-
"express": "^
|
|
38
|
-
"express-rate-limit": "^6.
|
|
41
|
+
"dotenv": "^17.4.2",
|
|
42
|
+
"express": "^5.1.0",
|
|
43
|
+
"express-rate-limit": "^8.6.1",
|
|
39
44
|
"express-winston": "^4.2.0",
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
45
|
+
"helmet": "^8.3.0",
|
|
46
|
+
"joi": "^18.2.3",
|
|
47
|
+
"jsonwebtoken": "^9.0.3",
|
|
48
|
+
"soul-studio": "^0.0.3",
|
|
43
49
|
"swagger-ui-express": "^4.6.1",
|
|
44
|
-
"winston": "^3.
|
|
45
|
-
"ws": "^8.
|
|
50
|
+
"winston": "^3.19.0",
|
|
51
|
+
"ws": "^8.21.3",
|
|
46
52
|
"yargs": "^17.7.1"
|
|
47
53
|
},
|
|
48
54
|
"devDependencies": {
|
|
49
|
-
"
|
|
50
|
-
"
|
|
51
|
-
"eslint
|
|
52
|
-
"
|
|
53
|
-
"
|
|
55
|
+
"@eslint/js": "^10.0.1",
|
|
56
|
+
"cross-env": "^10.1.0",
|
|
57
|
+
"eslint": "^10.8.1",
|
|
58
|
+
"eslint-config-prettier": "^10.1.8",
|
|
59
|
+
"globals": "^17.9.0",
|
|
60
|
+
"husky": "^9.1.7",
|
|
61
|
+
"jest": "^30.4.2",
|
|
62
|
+
"lint-staged": "^17.3.0",
|
|
54
63
|
"nodemon": "^3.1.3",
|
|
55
64
|
"prettier": "3.1.0",
|
|
56
|
-
"supertest": "^
|
|
65
|
+
"supertest": "^7.1.4",
|
|
57
66
|
"swagger-autogen": "^2.23.1"
|
|
58
67
|
},
|
|
59
68
|
"jest": {
|
|
60
69
|
"testEnvironment": "node",
|
|
61
70
|
"coveragePathIgnorePatterns": [
|
|
62
|
-
"/node_modules/"
|
|
71
|
+
"/node_modules/",
|
|
72
|
+
"/src/tests/",
|
|
73
|
+
"/src/cli.js",
|
|
74
|
+
"/src/commands.js"
|
|
63
75
|
],
|
|
76
|
+
"coverageThreshold": {
|
|
77
|
+
"global": {
|
|
78
|
+
"statements": 92,
|
|
79
|
+
"branches": 85,
|
|
80
|
+
"functions": 100,
|
|
81
|
+
"lines": 92
|
|
82
|
+
}
|
|
83
|
+
},
|
|
64
84
|
"globalSetup": "./src/tests/globalSetup.js",
|
|
65
85
|
"globalTeardown": "./src/tests/globalTeardown.js"
|
|
66
86
|
},
|
package/scripts/dev.sh
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Boots a local Soul dev instance with a ready-to-use superuser.
|
|
3
|
+
#
|
|
4
|
+
# The initial user Soul creates is never a superuser by default -- there's
|
|
5
|
+
# no flag to create one directly, it has to be promoted via the
|
|
6
|
+
# `updatesuperuser` CLI command after the fact, against the same DB file
|
|
7
|
+
# (not :memory:, since a separate process can't reach another process's
|
|
8
|
+
# in-memory db). On first run this script does that dance once; on later
|
|
9
|
+
# runs the DB already has a superuser, so it just starts the server.
|
|
10
|
+
set -euo pipefail
|
|
11
|
+
|
|
12
|
+
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
13
|
+
cd "$ROOT"
|
|
14
|
+
|
|
15
|
+
DB_PATH="${SOUL_DEV_DB:-$ROOT/.dev.db}"
|
|
16
|
+
PORT="${SOUL_DEV_PORT:-8000}"
|
|
17
|
+
TOKEN_SECRET="${SOUL_DEV_TOKEN_SECRET:-dev-only-secret-not-for-production}"
|
|
18
|
+
USERNAME="${SOUL_DEV_USERNAME:-admin}"
|
|
19
|
+
PASSWORD="${SOUL_DEV_PASSWORD:-Str0ngTestPw!1}"
|
|
20
|
+
CORS_ORIGIN="${SOUL_DEV_CORS_ORIGIN:-http://localhost:3000}"
|
|
21
|
+
|
|
22
|
+
# Studio runs on a different port -- cross-origin cookie auth needs these
|
|
23
|
+
# (see COOKIE_SAMESITE/COOKIE_SECURE in src/config/index.js).
|
|
24
|
+
export COOKIE_SAMESITE="${SOUL_DEV_COOKIE_SAMESITE:-none}"
|
|
25
|
+
export COOKIE_SECURE="${SOUL_DEV_COOKIE_SECURE:-true}"
|
|
26
|
+
|
|
27
|
+
SERVER_ARGS=(
|
|
28
|
+
src/server.js
|
|
29
|
+
-d "$DB_PATH"
|
|
30
|
+
-p "$PORT"
|
|
31
|
+
-a
|
|
32
|
+
--ts "$TOKEN_SECRET"
|
|
33
|
+
--iuu "$USERNAME"
|
|
34
|
+
--iup "$PASSWORD"
|
|
35
|
+
--cors "$CORS_ORIGIN"
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
if [[ ! -f "$DB_PATH" ]]; then
|
|
39
|
+
echo "==> First run: creating $USERNAME and promoting to superuser..."
|
|
40
|
+
|
|
41
|
+
node "${SERVER_ARGS[@]}" &
|
|
42
|
+
SERVER_PID=$!
|
|
43
|
+
for i in $(seq 1 30); do
|
|
44
|
+
curl -sf "http://localhost:$PORT/api/health" >/dev/null 2>&1 && break
|
|
45
|
+
sleep 0.5
|
|
46
|
+
done
|
|
47
|
+
kill "$SERVER_PID" 2>/dev/null || true
|
|
48
|
+
wait "$SERVER_PID" 2>/dev/null || true
|
|
49
|
+
|
|
50
|
+
# updatesuperuser exits 1 on success (existing, harmless CLI quirk) --
|
|
51
|
+
# `|| true` so `set -e` doesn't treat that as a real failure. Checked by
|
|
52
|
+
# output text instead of exit code.
|
|
53
|
+
PROMOTE_OUTPUT="$(node src/server.js updatesuperuser --id=1 --is_superuser=true -d "$DB_PATH" 2>&1 || true)"
|
|
54
|
+
echo "$PROMOTE_OUTPUT"
|
|
55
|
+
if [[ "$PROMOTE_OUTPUT" != *"updated successfully"* ]]; then
|
|
56
|
+
echo "==> Failed to promote $USERNAME to superuser, aborting." >&2
|
|
57
|
+
rm -f "$DB_PATH"
|
|
58
|
+
exit 1
|
|
59
|
+
fi
|
|
60
|
+
|
|
61
|
+
echo "==> $USERNAME / $PASSWORD is now a superuser. Starting for real..."
|
|
62
|
+
fi
|
|
63
|
+
|
|
64
|
+
exec node "${SERVER_ARGS[@]}"
|
package/src/cli.test.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
const { spawnSync, spawn } = require('child_process');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
|
|
4
|
+
// src/cli.js and src/commands.js run their real logic based on
|
|
5
|
+
// process.argv/NO_CLI at module-load time (yargs' demandOption, the
|
|
6
|
+
// updatesuperuser command dispatch), so they can't be exercised by
|
|
7
|
+
// requiring them directly in-process the way the rest of the app is
|
|
8
|
+
// tested -- these spawn the actual `soul` entrypoint as a real
|
|
9
|
+
// subprocess and assert on its stdout/exit code, the same way a user
|
|
10
|
+
// would invoke the CLI.
|
|
11
|
+
//
|
|
12
|
+
// --env points at a file that doesn't exist so a developer's own local
|
|
13
|
+
// .env (which sets AUTH=true, a token secret, etc.) can't leak into
|
|
14
|
+
// these tests and make them depend on machine-specific state.
|
|
15
|
+
const SERVER_ENTRY = path.join(__dirname, 'server.js');
|
|
16
|
+
const NO_ENV_FILE = '/tmp/soul-cli-test-no-such-env-file';
|
|
17
|
+
|
|
18
|
+
const runCli = (args, options = {}) =>
|
|
19
|
+
spawnSync('node', [SERVER_ENTRY, '--env', NO_ENV_FILE, ...args], {
|
|
20
|
+
encoding: 'utf-8',
|
|
21
|
+
timeout: 8000,
|
|
22
|
+
env: { PATH: process.env.PATH },
|
|
23
|
+
...options,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe('CLI (spawned as a real subprocess)', () => {
|
|
27
|
+
it('exits non-zero with a usage error when --database is missing', () => {
|
|
28
|
+
const result = runCli([]);
|
|
29
|
+
|
|
30
|
+
expect(result.status).not.toBe(0);
|
|
31
|
+
expect(result.stdout + result.stderr).toContain(
|
|
32
|
+
'Missing required argument: d',
|
|
33
|
+
);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('prints help and exits 0 for --help', () => {
|
|
37
|
+
const result = runCli(['--help']);
|
|
38
|
+
|
|
39
|
+
expect(result.status).toBe(0);
|
|
40
|
+
expect(result.stdout).toContain('Usage: soul [options]');
|
|
41
|
+
expect(result.stdout).toContain('--database');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('updatesuperuser exits non-zero when neither --password nor --is_superuser is given', () => {
|
|
45
|
+
const result = runCli(['-d', ':memory:', 'updatesuperuser', '--id=1']);
|
|
46
|
+
|
|
47
|
+
expect(result.status).not.toBe(0);
|
|
48
|
+
expect(result.stdout).toContain(
|
|
49
|
+
'Please provide either the --password or --is_superuser flag',
|
|
50
|
+
);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('updatesuperuser reports a missing user and exits non-zero', () => {
|
|
54
|
+
const result = runCli([
|
|
55
|
+
'-d',
|
|
56
|
+
':memory:',
|
|
57
|
+
'-a',
|
|
58
|
+
'--ts=test-secret-0123456789',
|
|
59
|
+
'--iuu=admin',
|
|
60
|
+
'--iup=Str0ngTestPw!1',
|
|
61
|
+
'updatesuperuser',
|
|
62
|
+
'--id=999',
|
|
63
|
+
'--password=AnotherStr0ngPw!1',
|
|
64
|
+
]);
|
|
65
|
+
|
|
66
|
+
expect(result.status).not.toBe(0);
|
|
67
|
+
expect(result.stdout).toContain('User not found');
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('updatesuperuser --is_superuser=true actually promotes the user instead of crashing on the DB write', () => {
|
|
71
|
+
// regression test: --is_superuser is parsed by yargs as a native JS
|
|
72
|
+
// boolean, which better-sqlite3 can't bind directly -- this used to
|
|
73
|
+
// throw "SQLite3 can only bind numbers, strings, bigints, buffers, and
|
|
74
|
+
// null" from inside a catch block with no process.exit(), so the
|
|
75
|
+
// process never terminated and this spawnSync call would hang until
|
|
76
|
+
// its timeout instead of failing fast.
|
|
77
|
+
const result = runCli([
|
|
78
|
+
'-d',
|
|
79
|
+
':memory:',
|
|
80
|
+
'-a',
|
|
81
|
+
'--ts=test-secret-0123456789',
|
|
82
|
+
'--iuu=admin',
|
|
83
|
+
'--iup=Str0ngTestPw!1',
|
|
84
|
+
'updatesuperuser',
|
|
85
|
+
'--id=1',
|
|
86
|
+
'--is_superuser=true',
|
|
87
|
+
]);
|
|
88
|
+
|
|
89
|
+
expect(result.stdout).not.toContain('SQLite3 can only bind');
|
|
90
|
+
expect(result.stdout).toContain('updated successfully');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('starts the server and shuts down cleanly on SIGTERM', (done) => {
|
|
94
|
+
const child = spawn(
|
|
95
|
+
'node',
|
|
96
|
+
[SERVER_ENTRY, '--env', NO_ENV_FILE, '-d', ':memory:', '-p', '0'],
|
|
97
|
+
{ env: { PATH: process.env.PATH } },
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
let stdout = '';
|
|
101
|
+
const timer = setTimeout(() => {
|
|
102
|
+
child.kill('SIGKILL');
|
|
103
|
+
done(
|
|
104
|
+
new Error(`server did not start in time; stdout so far:\n${stdout}`),
|
|
105
|
+
);
|
|
106
|
+
}, 12000);
|
|
107
|
+
|
|
108
|
+
child.stdout.on('data', (chunk) => {
|
|
109
|
+
stdout += chunk.toString();
|
|
110
|
+
|
|
111
|
+
if (stdout.includes('Soul is running')) {
|
|
112
|
+
child.kill('SIGTERM');
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
child.on('exit', (code, signal) => {
|
|
117
|
+
clearTimeout(timer);
|
|
118
|
+
try {
|
|
119
|
+
expect(stdout).toContain('Soul is running');
|
|
120
|
+
// a clean shutdown exits on its own rather than being force-killed
|
|
121
|
+
expect(signal).not.toBe('SIGKILL');
|
|
122
|
+
done();
|
|
123
|
+
} catch (error) {
|
|
124
|
+
done(error);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}, 15000);
|
|
128
|
+
});
|
package/src/config/index.js
CHANGED
|
@@ -28,6 +28,9 @@ const envVarsSchema = Joi.object()
|
|
|
28
28
|
|
|
29
29
|
EXTENSIONS: Joi.string().default(null),
|
|
30
30
|
|
|
31
|
+
COOKIE_SAMESITE: Joi.string().valid('lax', 'strict', 'none').default('lax'),
|
|
32
|
+
COOKIE_SECURE: Joi.boolean().default(false),
|
|
33
|
+
|
|
31
34
|
START_WITH_STUDIO: Joi.boolean().default(false),
|
|
32
35
|
|
|
33
36
|
INITIAL_USER_USERNAME: Joi.string(),
|
|
@@ -131,13 +134,18 @@ module.exports = {
|
|
|
131
134
|
|
|
132
135
|
rateLimit: {
|
|
133
136
|
enabled: argv['rate-limit-enabled'] || envVars.RATE_LIMIT_ENABLED,
|
|
134
|
-
windowMs: envVars.
|
|
135
|
-
max: envVars.
|
|
137
|
+
windowMs: envVars.RATE_LIMIT_WINDOW_MS,
|
|
138
|
+
max: envVars.RATE_LIMIT_MAX_REQUESTS,
|
|
136
139
|
},
|
|
137
140
|
|
|
138
141
|
extensions: {
|
|
139
142
|
path: argv.extensions || envVars.EXTENSIONS,
|
|
140
143
|
},
|
|
141
144
|
|
|
145
|
+
cookie: {
|
|
146
|
+
sameSite: envVars.COOKIE_SAMESITE,
|
|
147
|
+
secure: envVars.COOKIE_SECURE,
|
|
148
|
+
},
|
|
149
|
+
|
|
142
150
|
startWithStudio: argv.studio || envVars.START_WITH_STUDIO,
|
|
143
151
|
};
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
describe('Config', () => {
|
|
2
|
+
const originalEnv = { ...process.env };
|
|
3
|
+
|
|
4
|
+
afterEach(() => {
|
|
5
|
+
process.env = { ...originalEnv };
|
|
6
|
+
jest.resetModules();
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it('reads rate limit window and max from RATE_LIMIT_WINDOW_MS and RATE_LIMIT_MAX_REQUESTS', () => {
|
|
10
|
+
process.env.RATE_LIMIT_ENABLED = 'true';
|
|
11
|
+
process.env.RATE_LIMIT_WINDOW_MS = '2000';
|
|
12
|
+
process.env.RATE_LIMIT_MAX_REQUESTS = '3';
|
|
13
|
+
|
|
14
|
+
jest.resetModules();
|
|
15
|
+
const config = require('./index');
|
|
16
|
+
|
|
17
|
+
expect(config.rateLimit.enabled).toBe(true);
|
|
18
|
+
expect(config.rateLimit.windowMs).toBe(2000);
|
|
19
|
+
expect(config.rateLimit.max).toBe(3);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('falls back to schema defaults when rate limit env vars are not set', () => {
|
|
23
|
+
delete process.env.RATE_LIMIT_WINDOW_MS;
|
|
24
|
+
delete process.env.RATE_LIMIT_MAX_REQUESTS;
|
|
25
|
+
|
|
26
|
+
jest.resetModules();
|
|
27
|
+
const config = require('./index');
|
|
28
|
+
|
|
29
|
+
expect(config.rateLimit.windowMs).toBe(1000);
|
|
30
|
+
expect(config.rateLimit.max).toBe(10);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('reads cookie SameSite/Secure from COOKIE_SAMESITE and COOKIE_SECURE', () => {
|
|
34
|
+
process.env.COOKIE_SAMESITE = 'none';
|
|
35
|
+
process.env.COOKIE_SECURE = 'true';
|
|
36
|
+
|
|
37
|
+
jest.resetModules();
|
|
38
|
+
const config = require('./index');
|
|
39
|
+
|
|
40
|
+
expect(config.cookie.sameSite).toBe('none');
|
|
41
|
+
expect(config.cookie.secure).toBe(true);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('falls back to lax/false when cookie env vars are not set', () => {
|
|
45
|
+
delete process.env.COOKIE_SAMESITE;
|
|
46
|
+
delete process.env.COOKIE_SECURE;
|
|
47
|
+
|
|
48
|
+
jest.resetModules();
|
|
49
|
+
const config = require('./index');
|
|
50
|
+
|
|
51
|
+
expect(config.cookie.sameSite).toBe('lax');
|
|
52
|
+
expect(config.cookie.secure).toBe(false);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('throws when the environment fails schema validation', () => {
|
|
56
|
+
process.env.NODE_ENV = 'not-a-real-environment';
|
|
57
|
+
|
|
58
|
+
jest.resetModules();
|
|
59
|
+
expect(() => require('./index')).toThrow('Config validation error');
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe('CLI argument overrides', () => {
|
|
63
|
+
afterEach(() => {
|
|
64
|
+
jest.dontMock('../cli');
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('prefers CLI args over env vars for every overridable setting', () => {
|
|
68
|
+
jest.doMock('../cli', () => ({
|
|
69
|
+
yargs: {
|
|
70
|
+
argv: {
|
|
71
|
+
port: 9999,
|
|
72
|
+
verbose: 'console',
|
|
73
|
+
database: 'cli.db',
|
|
74
|
+
cors: 'http://a.com,http://b.com',
|
|
75
|
+
auth: true,
|
|
76
|
+
'rate-limit-enabled': true,
|
|
77
|
+
tokensecret: 'cli-secret',
|
|
78
|
+
accesstokenexpirationtime: '1H',
|
|
79
|
+
refreshtokenexpirationtime: '2D',
|
|
80
|
+
initialuserusername: 'cliuser',
|
|
81
|
+
initialuserpassword: 'clipass',
|
|
82
|
+
studio: true,
|
|
83
|
+
extensions: '/some/path',
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
}));
|
|
87
|
+
|
|
88
|
+
jest.resetModules();
|
|
89
|
+
const config = require('./index');
|
|
90
|
+
|
|
91
|
+
expect(config.port).toBe(9999);
|
|
92
|
+
expect(config.verbose).toBe('console');
|
|
93
|
+
expect(config.db.filename).toBe('cli.db');
|
|
94
|
+
expect(config.cors.origin).toEqual(['http://a.com', 'http://b.com']);
|
|
95
|
+
expect(config.auth).toBe(true);
|
|
96
|
+
expect(config.rateLimit.enabled).toBe(true);
|
|
97
|
+
expect(config.tokenSecret).toBe('cli-secret');
|
|
98
|
+
expect(config.accessTokenExpirationTime).toBe('1H');
|
|
99
|
+
expect(config.refreshTokenExpirationTime).toBe('2D');
|
|
100
|
+
expect(config.initialUserUsername).toBe('cliuser');
|
|
101
|
+
expect(config.initialUserPassword).toBe('clipass');
|
|
102
|
+
expect(config.startWithStudio).toBe(true);
|
|
103
|
+
expect(config.extensions.path).toBe('/some/path');
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('falls back to a wildcard CORS origin when nothing is configured', () => {
|
|
107
|
+
// set explicitly (rather than deleting) so this doesn't depend on
|
|
108
|
+
// whether a local .env file happens to define this key -- dotenv
|
|
109
|
+
// only fills *missing* process.env keys, so deleting it here would
|
|
110
|
+
// let a developer's own .env leak into the test.
|
|
111
|
+
process.env.CORS_ORIGIN_WHITELIST = '*';
|
|
112
|
+
jest.doMock('../cli', () => ({ yargs: { argv: {} } }));
|
|
113
|
+
|
|
114
|
+
jest.resetModules();
|
|
115
|
+
const config = require('./index');
|
|
116
|
+
|
|
117
|
+
expect(config.cors.origin).toEqual(['*']);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
});
|
|
@@ -1,18 +1,3 @@
|
|
|
1
|
-
const { rowService } = require('../../services');
|
|
2
|
-
const { dbConstants } = require('../../constants');
|
|
3
|
-
|
|
4
|
-
const { USERS_TABLE } = dbConstants;
|
|
5
|
-
|
|
6
|
-
const isUsernameTaken = (username) => {
|
|
7
|
-
const users = rowService.get({
|
|
8
|
-
tableName: USERS_TABLE,
|
|
9
|
-
whereString: 'WHERE username=?',
|
|
10
|
-
whereStringValues: [username],
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
return users.length > 0;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
1
|
const checkAuthConfigs = ({ auth, tokenSecret }) => {
|
|
17
2
|
if (auth && !tokenSecret) {
|
|
18
3
|
throw new Error(
|
|
@@ -21,4 +6,4 @@ const checkAuthConfigs = ({ auth, tokenSecret }) => {
|
|
|
21
6
|
}
|
|
22
7
|
};
|
|
23
8
|
|
|
24
|
-
module.exports = {
|
|
9
|
+
module.exports = { checkAuthConfigs };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const { checkAuthConfigs } = require('./common');
|
|
2
|
+
|
|
3
|
+
describe('checkAuthConfigs', () => {
|
|
4
|
+
it('throws when auth is enabled but no token secret is provided', () => {
|
|
5
|
+
expect(() =>
|
|
6
|
+
checkAuthConfigs({ auth: true, tokenSecret: undefined }),
|
|
7
|
+
).toThrow(
|
|
8
|
+
'You need to provide a token secret either from the CLI or from your environment variables',
|
|
9
|
+
);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it('does not throw when auth is enabled and a token secret is provided', () => {
|
|
13
|
+
expect(() =>
|
|
14
|
+
checkAuthConfigs({ auth: true, tokenSecret: 'some-secret' }),
|
|
15
|
+
).not.toThrow();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('does not throw when auth is disabled, regardless of token secret', () => {
|
|
19
|
+
expect(() =>
|
|
20
|
+
checkAuthConfigs({ auth: false, tokenSecret: undefined }),
|
|
21
|
+
).not.toThrow();
|
|
22
|
+
});
|
|
23
|
+
});
|