@scriptdb/server 1.0.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/README.md +407 -0
- package/dist/index.js +5678 -0
- package/package.json +58 -0
package/README.md
ADDED
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
# ScriptDB Server
|
|
2
|
+
|
|
3
|
+
Server module for hosting and running ScriptDB instances with authentication, sandboxing, and command execution.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @scriptdb/server
|
|
9
|
+
# or
|
|
10
|
+
yarn add @scriptdb/server
|
|
11
|
+
# or
|
|
12
|
+
bun add @scriptdb/server
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { server } from '@scriptdb/server';
|
|
19
|
+
|
|
20
|
+
// Start the server with default configuration
|
|
21
|
+
server();
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Configuration
|
|
25
|
+
|
|
26
|
+
The server can be configured through:
|
|
27
|
+
|
|
28
|
+
1. **Configuration file** (`scriptdb.config.json`)
|
|
29
|
+
2. **Programmatic configuration**
|
|
30
|
+
|
|
31
|
+
### Configuration File
|
|
32
|
+
|
|
33
|
+
Create a `scriptdb.config.json` file in your project root:
|
|
34
|
+
|
|
35
|
+
```json
|
|
36
|
+
{
|
|
37
|
+
"host": "localhost",
|
|
38
|
+
"port": 1234,
|
|
39
|
+
"users": [
|
|
40
|
+
{
|
|
41
|
+
"username": "admin",
|
|
42
|
+
"password": "password123"
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"username": "guest",
|
|
46
|
+
"password": "guest123"
|
|
47
|
+
}
|
|
48
|
+
],
|
|
49
|
+
"folder": "databases"
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### Configuration Options
|
|
54
|
+
|
|
55
|
+
- `host` (string): Server host (default: "localhost")
|
|
56
|
+
- `port` (number): Server port (default: 1234)
|
|
57
|
+
- `users` (array): Array of user objects
|
|
58
|
+
- `folder` (string): Database storage folder (default: "databases")
|
|
59
|
+
|
|
60
|
+
### User Configuration
|
|
61
|
+
|
|
62
|
+
Each user object can contain:
|
|
63
|
+
|
|
64
|
+
```json
|
|
65
|
+
{
|
|
66
|
+
"username": "string",
|
|
67
|
+
"password": "string",
|
|
68
|
+
"passwordHash": "string", // Pre-hashed password (bcrypt)
|
|
69
|
+
"signingSecret": "string" // Secret for message signing
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Programmatic Usage
|
|
74
|
+
|
|
75
|
+
### Basic Server
|
|
76
|
+
|
|
77
|
+
```typescript
|
|
78
|
+
import { server } from '@scriptdb/server';
|
|
79
|
+
|
|
80
|
+
// Start server with default settings
|
|
81
|
+
server();
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Advanced Configuration
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
import { server } from '@scriptdb/server';
|
|
88
|
+
|
|
89
|
+
// The server reads from scriptdb.config.json if it exists
|
|
90
|
+
// You can also modify the configuration programmatically before starting
|
|
91
|
+
|
|
92
|
+
// Create a custom config file before starting
|
|
93
|
+
import { writeFileSync } from 'fs';
|
|
94
|
+
|
|
95
|
+
const config = {
|
|
96
|
+
host: "0.0.0.0",
|
|
97
|
+
port: 8080,
|
|
98
|
+
users: [
|
|
99
|
+
{
|
|
100
|
+
username: "admin",
|
|
101
|
+
passwordHash: "$2b$10$..." // bcrypt hash
|
|
102
|
+
}
|
|
103
|
+
],
|
|
104
|
+
folder: "./data"
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
writeFileSync('./scriptdb.config.json', JSON.stringify(config, null, 2));
|
|
108
|
+
|
|
109
|
+
// Start the server
|
|
110
|
+
server();
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## Security Features
|
|
114
|
+
|
|
115
|
+
### Authentication
|
|
116
|
+
|
|
117
|
+
The server supports username/password authentication with bcrypt password hashing:
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
// Using plain text passwords (server will hash them)
|
|
121
|
+
{
|
|
122
|
+
"username": "user",
|
|
123
|
+
"password": "plain-text-password"
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Using pre-hashed passwords (bcrypt)
|
|
127
|
+
{
|
|
128
|
+
"username": "user",
|
|
129
|
+
"passwordHash": "$2b$10$N9qo8uLOickgx2ZMRZoMye.IcnmVoKSj.9iECkZXod4HPnywpQqWu"
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### TLS/SSL Support
|
|
134
|
+
|
|
135
|
+
The server supports secure connections using TLS:
|
|
136
|
+
|
|
137
|
+
```json
|
|
138
|
+
{
|
|
139
|
+
"host": "localhost",
|
|
140
|
+
"port": 1234,
|
|
141
|
+
"secure": true,
|
|
142
|
+
"tlsOptions": {
|
|
143
|
+
"key": "./server-key.pem",
|
|
144
|
+
"cert": "./server-cert.pem",
|
|
145
|
+
"ca": "./ca-cert.pem",
|
|
146
|
+
"rejectUnauthorized": true
|
|
147
|
+
},
|
|
148
|
+
"users": [
|
|
149
|
+
{
|
|
150
|
+
"username": "admin",
|
|
151
|
+
"password": "password"
|
|
152
|
+
}
|
|
153
|
+
]
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Message Signing
|
|
158
|
+
|
|
159
|
+
Configure message signing for additional security:
|
|
160
|
+
|
|
161
|
+
```json
|
|
162
|
+
{
|
|
163
|
+
"users": [
|
|
164
|
+
{
|
|
165
|
+
"username": "admin",
|
|
166
|
+
"password": "password",
|
|
167
|
+
"signingSecret": "my-secret-key"
|
|
168
|
+
}
|
|
169
|
+
]
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## API
|
|
174
|
+
|
|
175
|
+
### server()
|
|
176
|
+
|
|
177
|
+
Starts the ScriptDB server using the configuration file.
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
import { server } from '@scriptdb/server';
|
|
181
|
+
|
|
182
|
+
server(): void
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
The function:
|
|
186
|
+
1. Reads `scriptdb.config.json` from the base path
|
|
187
|
+
2. Validates configuration
|
|
188
|
+
3. Creates the database folder if it doesn't exist
|
|
189
|
+
4. Initializes the VM and protocol handler
|
|
190
|
+
5. Starts the server
|
|
191
|
+
|
|
192
|
+
## Database Management
|
|
193
|
+
|
|
194
|
+
### Database Structure
|
|
195
|
+
|
|
196
|
+
```
|
|
197
|
+
databases/
|
|
198
|
+
├── mydb1/
|
|
199
|
+
│ ├── users.json
|
|
200
|
+
│ ├── products.json
|
|
201
|
+
│ └── ...
|
|
202
|
+
├── mydb2/
|
|
203
|
+
│ └── ...
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Command Execution
|
|
207
|
+
|
|
208
|
+
The server executes JavaScript commands in a sandboxed environment:
|
|
209
|
+
|
|
210
|
+
```javascript
|
|
211
|
+
// Client can send commands like:
|
|
212
|
+
const users = db.users.find({ status: 'active' });
|
|
213
|
+
return users;
|
|
214
|
+
|
|
215
|
+
// Or more complex operations:
|
|
216
|
+
const result = db.collection('products').aggregate([
|
|
217
|
+
{ $match: { category: 'electronics' } },
|
|
218
|
+
{ $group: { _id: '$brand', count: { $sum: 1 } } }
|
|
219
|
+
]);
|
|
220
|
+
return result;
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## Worker Threads
|
|
224
|
+
|
|
225
|
+
The server uses worker threads for:
|
|
226
|
+
|
|
227
|
+
- **bcrypt operations**: Password hashing and verification
|
|
228
|
+
- **VM execution**: Running user code in isolation
|
|
229
|
+
- **Payload validation**: Validating incoming requests
|
|
230
|
+
|
|
231
|
+
This ensures the main thread remains responsive and operations don't block each other.
|
|
232
|
+
|
|
233
|
+
## Rate Limiting
|
|
234
|
+
|
|
235
|
+
The server implements IP-based and username-based rate limiting:
|
|
236
|
+
|
|
237
|
+
```json
|
|
238
|
+
{
|
|
239
|
+
"rateLimiting": {
|
|
240
|
+
"ipFailWindowMs": 900000, // 15 minutes
|
|
241
|
+
"maxLoginAttempts": 5,
|
|
242
|
+
"lockDurationMs": 1800000 // 30 minutes
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
## Examples
|
|
248
|
+
|
|
249
|
+
### Production Server Setup
|
|
250
|
+
|
|
251
|
+
```typescript
|
|
252
|
+
import { server } from '@scriptdb/server';
|
|
253
|
+
import { writeFileSync } from 'fs';
|
|
254
|
+
|
|
255
|
+
// Production configuration
|
|
256
|
+
const productionConfig = {
|
|
257
|
+
host: "0.0.0.0",
|
|
258
|
+
port: 443,
|
|
259
|
+
secure: true,
|
|
260
|
+
tlsOptions: {
|
|
261
|
+
key: "/path/to/private.key",
|
|
262
|
+
cert: "/path/to/certificate.crt",
|
|
263
|
+
ca: "/path/to/ca_bundle.crt"
|
|
264
|
+
},
|
|
265
|
+
users: [
|
|
266
|
+
{
|
|
267
|
+
username: "api_user",
|
|
268
|
+
passwordHash: "$2b$10$...", // Pre-hashed password
|
|
269
|
+
signingSecret: "production-secret"
|
|
270
|
+
}
|
|
271
|
+
],
|
|
272
|
+
folder: "/var/lib/scriptdb",
|
|
273
|
+
rateLimiting: {
|
|
274
|
+
ipFailWindowMs: 900000,
|
|
275
|
+
maxLoginAttempts: 5,
|
|
276
|
+
lockDurationMs: 1800000
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
// Write configuration
|
|
281
|
+
writeFileSync('./scriptdb.config.json', JSON.stringify(productionConfig, null, 2));
|
|
282
|
+
|
|
283
|
+
// Start server
|
|
284
|
+
console.log("Starting ScriptDB server...");
|
|
285
|
+
server();
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
### Development Setup
|
|
289
|
+
|
|
290
|
+
```typescript
|
|
291
|
+
import { server } from '@scriptdb/server';
|
|
292
|
+
import { writeFileSync } from 'fs';
|
|
293
|
+
|
|
294
|
+
// Development configuration
|
|
295
|
+
const devConfig = {
|
|
296
|
+
host: "localhost",
|
|
297
|
+
port: 1234,
|
|
298
|
+
secure: false, // Disable TLS for development
|
|
299
|
+
users: [
|
|
300
|
+
{
|
|
301
|
+
username: "dev",
|
|
302
|
+
password: "dev123"
|
|
303
|
+
},
|
|
304
|
+
{
|
|
305
|
+
username: "test",
|
|
306
|
+
password: "test123"
|
|
307
|
+
}
|
|
308
|
+
],
|
|
309
|
+
folder: "./dev_databases"
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
writeFileSync('./scriptdb.config.json', JSON.stringify(devConfig, null, 2));
|
|
313
|
+
|
|
314
|
+
// Start server
|
|
315
|
+
console.log("Starting ScriptDB development server...");
|
|
316
|
+
server();
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
### Multi-User Setup
|
|
320
|
+
|
|
321
|
+
```typescript
|
|
322
|
+
// Create multiple users with different permissions
|
|
323
|
+
const multiUserConfig = {
|
|
324
|
+
host: "localhost",
|
|
325
|
+
port: 1234,
|
|
326
|
+
users: [
|
|
327
|
+
{
|
|
328
|
+
username: "admin",
|
|
329
|
+
password: "admin123",
|
|
330
|
+
signingSecret: "admin-signing-key"
|
|
331
|
+
},
|
|
332
|
+
{
|
|
333
|
+
username: "read_only",
|
|
334
|
+
password: "readonly123",
|
|
335
|
+
signingSecret: "readonly-signing-key"
|
|
336
|
+
},
|
|
337
|
+
{
|
|
338
|
+
username: "writer",
|
|
339
|
+
password: "writer123",
|
|
340
|
+
signingSecret: "writer-signing-key"
|
|
341
|
+
}
|
|
342
|
+
],
|
|
343
|
+
folder: "./shared_databases"
|
|
344
|
+
};
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
## Error Handling
|
|
348
|
+
|
|
349
|
+
The server handles various error conditions:
|
|
350
|
+
|
|
351
|
+
1. **Invalid Configuration**: Validates and sanitizes configuration values
|
|
352
|
+
2. **Authentication Failures**: Tracks failed attempts and implements lockouts
|
|
353
|
+
3. **File System Errors**: Creates directories and handles permission issues
|
|
354
|
+
4. **Network Errors**: Handles connection failures and timeouts
|
|
355
|
+
5. **VM Errors**: Isolates and reports script execution errors
|
|
356
|
+
|
|
357
|
+
## Logging
|
|
358
|
+
|
|
359
|
+
The server logs important events:
|
|
360
|
+
|
|
361
|
+
- Server startup and configuration
|
|
362
|
+
- Client connections and disconnections
|
|
363
|
+
- Authentication attempts and failures
|
|
364
|
+
- Command execution results
|
|
365
|
+
- Error conditions
|
|
366
|
+
|
|
367
|
+
Configure logging through environment variables or modify the server implementation.
|
|
368
|
+
|
|
369
|
+
## Environment Variables
|
|
370
|
+
|
|
371
|
+
- `NODE_ENV`: Set to 'production' for production mode
|
|
372
|
+
- `SCRIPTDB_CONFIG_PATH`: Custom path to configuration file
|
|
373
|
+
- `SCRIPTDB_LOG_LEVEL`: Logging level (debug, info, warn, error)
|
|
374
|
+
|
|
375
|
+
## Development
|
|
376
|
+
|
|
377
|
+
```bash
|
|
378
|
+
# Install dependencies
|
|
379
|
+
npm install
|
|
380
|
+
|
|
381
|
+
# Run in development mode
|
|
382
|
+
npm run dev
|
|
383
|
+
|
|
384
|
+
# Build the package
|
|
385
|
+
npm run build
|
|
386
|
+
|
|
387
|
+
# Run tests
|
|
388
|
+
npm test
|
|
389
|
+
|
|
390
|
+
# Type checking
|
|
391
|
+
npm run typecheck
|
|
392
|
+
|
|
393
|
+
# Lint code
|
|
394
|
+
npm run lint
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
## Security Considerations
|
|
398
|
+
|
|
399
|
+
1. **Use TLS in production**: Always enable secure connections
|
|
400
|
+
2. **Strong passwords**: Use bcrypt hashes for stored passwords
|
|
401
|
+
3. **Network security**: Configure firewalls and limit access
|
|
402
|
+
4. **Regular updates**: Keep dependencies updated
|
|
403
|
+
5. **Monitor logs**: Regularly check for suspicious activity
|
|
404
|
+
|
|
405
|
+
## License
|
|
406
|
+
|
|
407
|
+
MIT
|