acebase-server 1.5.1 → 1.8.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 +55 -37
- package/package.json +3 -3
- package/src/acebase-server.js +231 -173
- package/src/cluster.js +33 -34
- package/src/index.d.ts +35 -2
- package/src/start.js +20 -4
- package/src/webmanager/index.html +1 -1
- package/src/webmanager/index.js +3 -2
package/README.md
CHANGED
|
@@ -41,6 +41,23 @@ const server = new AceBaseServer(dbname, settings);
|
|
|
41
41
|
|
|
42
42
|
You can either pass ```certPath``` and ```keyPath```, or ```pfxPath``` and ```passphrase``` - depending on the type of certificate files you have.
|
|
43
43
|
|
|
44
|
+
### Enable transaction logging
|
|
45
|
+
(NEW, BETA)
|
|
46
|
+
|
|
47
|
+
AceBase now supports transaction logging to facilitate sophisticated synchronization options and custom data recovery. Using cursors that indicate certain points in time, this allows for fast and easy synchronization of data between an AceBase server and multiple clients, or other server instances. This functionality is currently in BETA stage and will be tested extensively in the coming weeks.
|
|
48
|
+
|
|
49
|
+
To enable transaction logging on your database, add the `transactions` setting:
|
|
50
|
+
```js
|
|
51
|
+
const server = new AceBaseServer(dbname, {
|
|
52
|
+
host: 'localhost', port: 443,
|
|
53
|
+
transactions: {
|
|
54
|
+
log: true, // Enable
|
|
55
|
+
maxAge: 30, // Keep logs of last 30 days
|
|
56
|
+
noWait: false // Data changes wait for log to be written
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
```
|
|
60
|
+
|
|
44
61
|
### Enable authentication
|
|
45
62
|
|
|
46
63
|
Authentication settings determine who is allowed access to your server. By enabling authentication, the server will allow users to sign in (and signup) and authorization rules to be defined for data being read/written to the database.
|
|
@@ -58,22 +75,22 @@ const settings = {
|
|
|
58
75
|
}
|
|
59
76
|
```
|
|
60
77
|
|
|
61
|
-
The following
|
|
62
|
-
-
|
|
63
|
-
-
|
|
64
|
-
-
|
|
65
|
-
-
|
|
78
|
+
The following `authentication` settings are available:
|
|
79
|
+
- `enabled`: whether to enable user authentication (default: `true`)
|
|
80
|
+
- `allowUserSignup`: whether users can sign up for an account themselves, or if the admin has to (default: `false`)
|
|
81
|
+
- `defaultAccessRule`: default authorization rule for read/write operations. Either `deny`, `allow` or `auth`. The first time the server is launched with authentication enabled, it will create a *rules.json* file that contains this access rule for any path. `deny` will deny all users (except admin) read/write access, `allow` will grant access to anyone, `auth` will grant access only to authenticated users (default: `auth`)
|
|
82
|
+
- `defaultAdminPassword`: supply a default password for the admin account that will be used when it is created. If you do not supply this, a generated password will be used instead and displayed only once in the console output.
|
|
66
83
|
|
|
67
84
|
### Setup authorization rules
|
|
68
85
|
|
|
69
|
-
If you enabled authentication, you can also define access rules for your data. Using rules, you can allow or deny specific (or anonymous) users read and/or write access to your data. These rules are identical to those used by [Firebase](https://firebase.google.com/docs/database/security/) (Note: ".read" and ".write" only, ".validate" might be implemented in the future, ".schema" implemented as alternative) and are saved in a file called _rules.json_ in your database directory. The default rules written to the file are determined by the
|
|
86
|
+
If you enabled authentication, you can also define access rules for your data. Using rules, you can allow or deny specific (or anonymous) users read and/or write access to your data. These rules are identical to those used by [Firebase](https://firebase.google.com/docs/database/security/) (Note: ".read" and ".write" only, ".validate" might be implemented in the future, ".schema" implemented as alternative) and are saved in a file called _rules.json_ in your database directory. The default rules written to the file are determined by the `defaultAccessRule` authentication setting at the first server launch with `authentication` enabled.
|
|
70
87
|
|
|
71
|
-
The default _rules.json_ file content is based on the value of the
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
88
|
+
The default _rules.json_ file content is based on the value of the `defaultAccessRule` setting, possible values are:
|
|
89
|
+
* `"auth"`: Only allow authenticated users read/write access to the database
|
|
90
|
+
* `"allow"`: Allow anyone (including anonymous users) read/write access to the database
|
|
91
|
+
* `"deny"`: Deny anyone (except admin user) read/write access to the database
|
|
75
92
|
|
|
76
|
-
When
|
|
93
|
+
When `defaultAccessRule: "auth"` is used, it will generate the following _rules.json_ file:
|
|
77
94
|
```json
|
|
78
95
|
{
|
|
79
96
|
"rules": {
|
|
@@ -83,7 +100,7 @@ When ```defaultAccessRule: "auth"``` is used, it will generate the following _ru
|
|
|
83
100
|
}
|
|
84
101
|
```
|
|
85
102
|
|
|
86
|
-
When
|
|
103
|
+
When `"allow"` or `"deny"` is used, the `".read"` and `".write"` properties will be set to `true` or `false` respectively.
|
|
87
104
|
|
|
88
105
|
If you want to further restrict what data users can access and/or write to (RECOMMENDED!), you could edit the file as such, granting users read/write access to their own user node:
|
|
89
106
|
```json
|
|
@@ -117,8 +134,8 @@ NOTE: Just like Firebase, access is denied by default when no rule is found for
|
|
|
117
134
|
```
|
|
118
135
|
Above rules enforces:
|
|
119
136
|
* No read or write access to the root node or any child for anyone. (No rule has been set for those nodes, access will be denied)
|
|
120
|
-
* Read access to all reviews for specific shops ('shop_reviews/shop1', 'shop_reviews/shop2') for anyone, including unauthenticated clients (
|
|
121
|
-
* Write access to an authenticated user's own review for any shop. (
|
|
137
|
+
* Read access to all reviews for specific shops ('shop_reviews/shop1', 'shop_reviews/shop2') for anyone, including unauthenticated clients (`".read"` rule is set to `true`)
|
|
138
|
+
* Write access to an authenticated user's own review for any shop. (`".write"` rule is set to `"auth.uid === $uid"`)
|
|
122
139
|
|
|
123
140
|
### Schema validation
|
|
124
141
|
|
|
@@ -128,7 +145,7 @@ There are 2 ways to can add schemas:
|
|
|
128
145
|
- In your `rules.json` file, see below.
|
|
129
146
|
- Programmatically through `db.schema.set`. See the [AceBase documentation](https://github.com/appy-one/acebase#adding-schemas-to-enforce-data-rules) for more info.
|
|
130
147
|
|
|
131
|
-
To ensure all users have a
|
|
148
|
+
To ensure all users have a `name` (string), `email` (string) and `language` (either Dutch, English, German, French or Spanish), optionally a `birthdate` (Date) and `address` (custom object definition), add the following to your _rules.json_ file:
|
|
132
149
|
```json
|
|
133
150
|
{
|
|
134
151
|
"rules": {
|
|
@@ -197,11 +214,11 @@ You can also decide to split the schema up into multiple levels:
|
|
|
197
214
|
|
|
198
215
|
And, if you prefer, schema definitions can be defined as strings instead:
|
|
199
216
|
```json
|
|
200
|
-
|
|
217
|
+
{
|
|
201
218
|
"address": {
|
|
202
219
|
".schema": "{ street: string, city: string, country: string, geo?: { lat: number, lon: number } }"
|
|
203
220
|
}
|
|
204
|
-
|
|
221
|
+
}
|
|
205
222
|
```
|
|
206
223
|
|
|
207
224
|
## Sending user e-mails [NEW]
|
|
@@ -225,39 +242,36 @@ function sendRequestedEmail(request) {
|
|
|
225
242
|
}
|
|
226
243
|
```
|
|
227
244
|
|
|
228
|
-
Any links you would add to the e-mails you send out should point to your own website/app, where you would handle account verification / password resets through an
|
|
245
|
+
Any links you would add to the e-mails you send out should point to your own website/app, where you would handle account verification / password resets through an `AceBaseClient` connected to your server:
|
|
229
246
|
|
|
230
|
-
```
|
|
247
|
+
```js
|
|
231
248
|
const resetCode = 'weydgaed7gjsdhfjadbsfadsfasq3w7dtuqwebd'; // eg from your_reset_url?code=weydga...
|
|
232
249
|
const newPassword = 'MyNewPassword'; // from user input on your page
|
|
233
|
-
|
|
234
|
-
.
|
|
250
|
+
try {
|
|
251
|
+
await client.auth.resetPassword(resetCode, newPassword);
|
|
235
252
|
// User can now sign in with their new password
|
|
236
|
-
}
|
|
237
|
-
|
|
253
|
+
}
|
|
254
|
+
catch(err) {
|
|
238
255
|
// Something went wrong
|
|
239
|
-
}
|
|
256
|
+
}
|
|
240
257
|
```
|
|
241
258
|
|
|
242
259
|
OR, if you have direct access to your AceBaseServer instance from your website:
|
|
243
260
|
|
|
244
261
|
```javascript
|
|
245
|
-
server.resetPassword(req.ip, resetCode, newPassword)
|
|
246
|
-
.then(() => { ... });
|
|
262
|
+
await server.resetPassword(req.ip, resetCode, newPassword);
|
|
247
263
|
```
|
|
248
264
|
|
|
249
265
|
To verify a user's email address:
|
|
250
266
|
```javascript
|
|
251
267
|
const verificationCode = 'weydgaed7gjsdhfjadbsfadsfasq3w7dtuqwebd'; // eg from your_verify_url?code=weydga...
|
|
252
|
-
client.auth.verifyEmailAddress(verificationCode)
|
|
253
|
-
.then(() => { ... })
|
|
268
|
+
await client.auth.verifyEmailAddress(verificationCode);
|
|
254
269
|
```
|
|
255
270
|
|
|
256
271
|
OR, directly on your server:
|
|
257
272
|
|
|
258
273
|
```javascript
|
|
259
|
-
server.verifyEmailAddress(req.ip, verificationCode)
|
|
260
|
-
.then(() => { ... });
|
|
274
|
+
await server.verifyEmailAddress(req.ip, verificationCode);
|
|
261
275
|
```
|
|
262
276
|
|
|
263
277
|
### E-mail request examples
|
|
@@ -317,11 +331,11 @@ Request type *user_reset_password_success*:
|
|
|
317
331
|
|
|
318
332
|
You can enable users to sign into your app through a third party login provider, such as Facebook, Google, Twitter etc. To enable this, follow these steps for each provider:
|
|
319
333
|
|
|
320
|
-
* First, get
|
|
334
|
+
* First, get `client_id` and `client_secret` API keys from the auth providers' developer environment. This will allow your app to use with the provider's auth API.
|
|
321
335
|
|
|
322
|
-
* Most OAuth providers restrict authentication callback uri's to a predefined set of uris, so make sure you add AceBase's callback URL in the provider's API settings:
|
|
336
|
+
* Most OAuth providers restrict authentication callback uri's to a predefined set of uris, so make sure you add AceBase's callback URL in the provider's API settings: `"https://your.acebase.server/oauth2/dbname/signin"` (replace hostname and dbname to your server Url)
|
|
323
337
|
|
|
324
|
-
* Then, add your API keys to your
|
|
338
|
+
* Then, add your API keys to your `AceBaseServer` config with `configAuthProvider`:
|
|
325
339
|
|
|
326
340
|
```javascript
|
|
327
341
|
server.configAuthProvider('facebook', { client_id: '[your fb app_id]', client_secret: '[your fb app_secret]', scopes: [/* Any additional scopes, such as 'user_birthday' */] });
|
|
@@ -347,7 +361,7 @@ client.auth.finishAuthProviderSignIn(callbackResult)
|
|
|
347
361
|
})
|
|
348
362
|
```
|
|
349
363
|
|
|
350
|
-
The
|
|
364
|
+
The `result` object will also contain the provider's `access_token` and `refresh_token` in case you want to make custom calls to the provider's API. If you want to keep the provider's access_token active, you will have to call `client.auth.refreshAuthProviderToken` before it expires:
|
|
351
365
|
|
|
352
366
|
```javascript
|
|
353
367
|
const keepAlive = (provider) => {
|
|
@@ -373,13 +387,13 @@ Currently implemented auth providers are:
|
|
|
373
387
|
* Facebook*
|
|
374
388
|
* Spotify
|
|
375
389
|
|
|
376
|
-
*NOTE: Facebook access tokens are short-lived by default, but will be exchanged for a long-lived (60 day) access token upon refresh. If you need to keep the Facebook access token active, execute
|
|
390
|
+
*NOTE: Facebook access tokens are short-lived by default, but will be exchanged for a long-lived (60 day) access token upon refresh. If you need to keep the Facebook access token active, execute `refreshAuthProviderToken` immediately after `finishAuthProviderSignIn`, and keep refreshing every time the user starts your app. Once a Facebook access token has expired, it cannot be refreshed and the user will have to sign in again.
|
|
377
391
|
|
|
378
392
|
## Add cloud functions to handle data changes
|
|
379
393
|
|
|
380
394
|
You can add "cloud functions" to perform custom tasks upon data changes. You can do this in 2 ways:
|
|
381
395
|
|
|
382
|
-
* In the same process you are running your
|
|
396
|
+
* In the same process you are running your `AceBaseServer` (requires server v1.1+). Make sure you run cpu heavy code in a separate worker thread to keep the server thread available for core tasks:
|
|
383
397
|
```js
|
|
384
398
|
const server = new AceBaseServer(dbname, settings);
|
|
385
399
|
await server.ready();
|
|
@@ -411,7 +425,7 @@ db.ref('uploads/images').on('child_added', async snap => {
|
|
|
411
425
|
|
|
412
426
|
## Extending the server API
|
|
413
427
|
|
|
414
|
-
You can add your own custom API functions to the server with the
|
|
428
|
+
You can add your own custom API functions to the server with the `server.extend(method, path, handler)` method:
|
|
415
429
|
|
|
416
430
|
```javascript
|
|
417
431
|
const server = new AceBaseServer(...);
|
|
@@ -459,6 +473,10 @@ client.callExtension('post', 'quotes/add', {
|
|
|
459
473
|
});
|
|
460
474
|
```
|
|
461
475
|
|
|
476
|
+
## Running in a cluster
|
|
477
|
+
|
|
478
|
+
It is possible to run your *AceBaseServer* in a cluster so you can utilize more than just the 1 CPU a Node.js application typically uses. To make sure all instances can read and write to the same database files safely they will have to be able to communicate with each other. This is done using a technique called interprocess communication (IPC). Node.js' built-in cluster functionality provides IPC channels to communicate between master and worker processes, but if you want to run a `pm2` cluster or want multiple instances running in the cloud, you'll have to use an external IPC server. See [AceBase IPC Server](https://github.com/appy-one/acebase-ipc-server) for more information about the different clustering solutions and code examples.
|
|
479
|
+
|
|
462
480
|
## Connecting to a server
|
|
463
481
|
|
|
464
482
|
See *acebase-client* on [npm](https://www.npmjs.com/package/acebase-client) or [github](https://github.com/appy-one/acebase-client)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "acebase-server",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.0",
|
|
4
4
|
"description": "AceBase realtime database server (webserver endpoint to allow remote connections)",
|
|
5
5
|
"main": "./src/index.js",
|
|
6
6
|
"private": false,
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
"author": "Ewout Stortenbeker <me@appy.one> (http://appy.one)",
|
|
45
45
|
"license": "MIT",
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"acebase": "^1.
|
|
48
|
-
"acebase-core": "^1.
|
|
47
|
+
"acebase": "^1.14.0",
|
|
48
|
+
"acebase-core": "^1.12.0",
|
|
49
49
|
"express": "^4.17.1",
|
|
50
50
|
"socket.io": "^2.4.1"
|
|
51
51
|
},
|
package/src/acebase-server.js
CHANGED
|
@@ -5,22 +5,6 @@ const { ID, Transport, DataSnapshot, PathInfo, Utils, DebugLogger, ColorStyle }
|
|
|
5
5
|
const fs = require('fs');
|
|
6
6
|
const crypto = require('crypto');
|
|
7
7
|
|
|
8
|
-
class AceBaseClusterSettings {
|
|
9
|
-
constructor(settings) {
|
|
10
|
-
this.enabled = typeof settings === "object";
|
|
11
|
-
this.isMaster = this.enabled && settings.isMaster;
|
|
12
|
-
this.master = this.enabled ? settings.master : process;
|
|
13
|
-
this.workers = this.enabled ? settings.workers : [process];
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// class AceBaseClusterSettings {
|
|
18
|
-
// constructor(settings) {
|
|
19
|
-
// this.enabled = typeof settings === "object";
|
|
20
|
-
// this.port = this.enabled ? settings.port : 0;
|
|
21
|
-
// }
|
|
22
|
-
// }
|
|
23
|
-
|
|
24
8
|
class AceBaseServerHttpsSettings {
|
|
25
9
|
/**
|
|
26
10
|
*
|
|
@@ -206,6 +190,15 @@ class AceBaseServerTransactionSettings {
|
|
|
206
190
|
}
|
|
207
191
|
}
|
|
208
192
|
|
|
193
|
+
/**
|
|
194
|
+
* @typedef IPCClientSettings
|
|
195
|
+
* @property {string} [host='localhost'] IPC Server host to connect to. Default is `"localhost"`
|
|
196
|
+
* @property {number} port IPC Server port number
|
|
197
|
+
* @property {boolean} [ssl=false] Whether to use a secure connection to the server. Strongly recommended if `host` is not `"localhost"`. Default is `false`
|
|
198
|
+
* @property {string} [token] Token used in the IPC Server configuration (optional). The server will refuse connections using the wrong token.
|
|
199
|
+
* @property {'master'|'worker'} role Determines the role of this IPC client. Only 1 process can be assigned the 'master' role, all other processes must use the role 'worker'
|
|
200
|
+
*/
|
|
201
|
+
|
|
209
202
|
class AceBaseServerSettings {
|
|
210
203
|
|
|
211
204
|
/**
|
|
@@ -221,6 +214,7 @@ class AceBaseServerSettings {
|
|
|
221
214
|
* @param {AceBaseServerAuthenticationSettings} [settings.auth]
|
|
222
215
|
* @param {AceBaseServerEmailSettings} [settings.email]
|
|
223
216
|
* @param {AceBaseServerTransactionSettings} [settings.transactions]
|
|
217
|
+
* @param {IPCClientSettings} [settings.ipc]
|
|
224
218
|
*
|
|
225
219
|
*/
|
|
226
220
|
constructor(settings) {
|
|
@@ -229,105 +223,17 @@ class AceBaseServerSettings {
|
|
|
229
223
|
this.host = settings.host || "localhost";
|
|
230
224
|
this.port = settings.port || 3000;
|
|
231
225
|
this.path = settings.path || '.';
|
|
232
|
-
this.cluster = new AceBaseClusterSettings(settings.cluster);
|
|
233
226
|
this.https = new AceBaseServerHttpsSettings(settings.https);
|
|
234
227
|
this.authentication = new AceBaseServerAuthenticationSettings(settings.authentication);
|
|
235
228
|
this.maxPayloadSize = settings.maxPayloadSize || '10mb';
|
|
236
229
|
this.allowOrigin = settings.allowOrigin || '*';
|
|
237
230
|
this.email = typeof settings.email === 'object' ? new AceBaseServerEmailSettings(settings.email) : null;
|
|
238
231
|
this.transactions = new AceBaseServerTransactionSettings(settings.transactions);
|
|
232
|
+
this.ipc = settings.ipc;
|
|
239
233
|
}
|
|
240
234
|
}
|
|
241
235
|
|
|
242
236
|
class AccessDeniedError extends Error { }
|
|
243
|
-
|
|
244
|
-
class ClientSubscription {
|
|
245
|
-
constructor(obj) {
|
|
246
|
-
this.callback = obj.callback;
|
|
247
|
-
this.disconnectedCallback = null;
|
|
248
|
-
this.missedEvents = [];
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
class MissedClientEvent {
|
|
252
|
-
constructor(obj) {
|
|
253
|
-
this.time = Date.now();
|
|
254
|
-
this.event = obj.event;
|
|
255
|
-
this.subscriptionPath = obj.subscriptionPath;
|
|
256
|
-
this.path = obj.path;
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
class Client {
|
|
260
|
-
/**
|
|
261
|
-
* Not used - yet
|
|
262
|
-
* @param {object} obj
|
|
263
|
-
* @param {AceBaseServer} obj.server
|
|
264
|
-
* @param {SocketIO.Socket} obj.socket
|
|
265
|
-
* @param {DbUserAccountDetails} obj.user
|
|
266
|
-
*/
|
|
267
|
-
constructor(obj) {
|
|
268
|
-
this.server = obj.server;
|
|
269
|
-
this.socket = obj.socket;
|
|
270
|
-
this.id = this.socket.id;
|
|
271
|
-
this.user = obj.user;
|
|
272
|
-
|
|
273
|
-
/** @type {{ [path: string]: ClientSubscription }} */
|
|
274
|
-
this.subscriptions = {};
|
|
275
|
-
/** @type {MissedClientEvent[]} */
|
|
276
|
-
this.missedEvents = [];
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
subscribe(requestId, event, subscriptionPath) {
|
|
280
|
-
// Check if user has access
|
|
281
|
-
if (!this.server.userHasAccess(this.user, subscriptionPath, false)) {
|
|
282
|
-
logRef.push({ action: `subscribe`, success: false, code: `access_denied`, uid: this.user ? this.user.uid : '-', path: subscriptionPath });
|
|
283
|
-
this.socket.emit('result', {
|
|
284
|
-
success: false,
|
|
285
|
-
reason: `access_denied`,
|
|
286
|
-
req_id: requestId
|
|
287
|
-
});
|
|
288
|
-
return;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
const callback = (err, path, currentValue, previousValue) => {
|
|
292
|
-
if (err) {
|
|
293
|
-
return;
|
|
294
|
-
}
|
|
295
|
-
if (!this.socket) {
|
|
296
|
-
// Client is currently disconnected. Keep track of missed events in case
|
|
297
|
-
// the client comes back again.
|
|
298
|
-
// TODO: Move from memory to eventsDb after a while
|
|
299
|
-
this.missedEvents.push(new MissedClientEvent({ event, subscriptionPath, path, currentValue, previousValue }));
|
|
300
|
-
return;
|
|
301
|
-
}
|
|
302
|
-
if (!this.server.userHasAccess(this.user, path, false)) {
|
|
303
|
-
if (subscriptionPath.indexOf('*') < 0 && subscriptionPath.indexOf('$') < 0) {
|
|
304
|
-
// Could potentially be very many callbacks, so
|
|
305
|
-
// DISABLED: logRef.push({ action: `access_revoked`, uid: client.user ? client.user.uid : '-', path: subscriptionPath });
|
|
306
|
-
// Only log when user subscribes again
|
|
307
|
-
this.socket.emit('result', {
|
|
308
|
-
success: false,
|
|
309
|
-
reason: `access_denied`,
|
|
310
|
-
req_id: requestId
|
|
311
|
-
});
|
|
312
|
-
}
|
|
313
|
-
return;
|
|
314
|
-
}
|
|
315
|
-
let val = Transport.serialize({
|
|
316
|
-
current: currentValue,
|
|
317
|
-
previous: previousValue
|
|
318
|
-
});
|
|
319
|
-
this.server.verbose(`Sending data event "${event}" for path "/${path}" to client ${this.id}`);
|
|
320
|
-
this.socket.emit("data-event", {
|
|
321
|
-
subscr_path: subscriptionPath,
|
|
322
|
-
path,
|
|
323
|
-
event,
|
|
324
|
-
val
|
|
325
|
-
});
|
|
326
|
-
};
|
|
327
|
-
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
|
|
331
237
|
/**
|
|
332
238
|
* @typedef {object} DbUserAccountDetails
|
|
333
239
|
* @property {string} [uid] uid, not stored in database object (uid is the node's key)
|
|
@@ -518,6 +424,7 @@ class AceBaseServer extends EventEmitter {
|
|
|
518
424
|
res.header('Access-Control-Allow-Origin', options.allowOrigin !== '*' ? options.allowOrigin : (req.headers.origin || '*'));
|
|
519
425
|
res.header('Access-Control-Allow-Methods', corsOptions.methods);
|
|
520
426
|
res.header('Access-Control-Allow-Headers', corsOptions.allowedHeaders);
|
|
427
|
+
res.header('Access-Control-Expose-Headers', 'Date, AceBase-Context'); // Prevent browsers from stripping these headers from the response for programmatic access in cross-origin requests
|
|
521
428
|
if (req.method === 'OPTIONS') {
|
|
522
429
|
res.status(200).end();
|
|
523
430
|
}
|
|
@@ -527,8 +434,25 @@ class AceBaseServer extends EventEmitter {
|
|
|
527
434
|
};
|
|
528
435
|
app.use(corsHandler);
|
|
529
436
|
|
|
437
|
+
// TODO: determine max socket payload using options.maxPayloadSize which is now only used for json POST data
|
|
438
|
+
// const maxPayloadBytes = ((payloadStr) => {
|
|
439
|
+
// const match = payloadStr.match(/^([0-9]+)(?:mb|kb|b)$/i);
|
|
440
|
+
// if (!match) { return 10e7; } // Socket.IO 2.x default (100MB), 3.x default is 1MB (1e6)
|
|
441
|
+
// const nr = +match[0], unit = match[1].toLowerCase();
|
|
442
|
+
// switch (unit) {
|
|
443
|
+
// case 'mb': return nr * 1e6;
|
|
444
|
+
// case 'kb': return nr * 1e3;
|
|
445
|
+
// case 'b': return nr;
|
|
446
|
+
// }
|
|
447
|
+
// }, options.maxPayloadSize);
|
|
448
|
+
|
|
530
449
|
const server = options.https.enabled ? require('https').createServer(options.https, app) : require('http').createServer(app);
|
|
531
450
|
const io = require('socket.io')(server, {
|
|
451
|
+
// See https://socket.io/docs/v2/server-initialization/ and https://socket.io/docs/v3/server-initialization/
|
|
452
|
+
pingInterval: 5000, // socket.io 2.x default is 25000
|
|
453
|
+
pingTimeout: 5000, // socket.io 2.x default is 5000, 3.x default = 20000
|
|
454
|
+
maxHttpBufferSize: 10e7, // Socket is closed if sent message exceeds this. Socket.io 2.x default is 10e7 (100MB)
|
|
455
|
+
|
|
532
456
|
// socket.io 2.x:
|
|
533
457
|
handlePreflightRequest: corsHandler
|
|
534
458
|
// socket.io 3+:
|
|
@@ -551,17 +475,18 @@ class AceBaseServer extends EventEmitter {
|
|
|
551
475
|
}
|
|
552
476
|
};
|
|
553
477
|
this.url = this.config.url; // Is this used?
|
|
554
|
-
this.debug = new DebugLogger(options.logLevel, `[${dbname}]`.colorize(ColorStyle.green));
|
|
478
|
+
this.debug = new DebugLogger(options.logLevel, `[${dbname}]`.colorize(ColorStyle.green));
|
|
555
479
|
|
|
556
480
|
const dbOptions = {
|
|
557
481
|
logLevel: options.logLevel,
|
|
558
482
|
info: 'realtime database server',
|
|
559
|
-
|
|
560
|
-
|
|
483
|
+
// NEW: Allow storage setting like AceBaseLocalSettings - could allow using other db backend (typed, but undocumented)
|
|
484
|
+
storage: options.storage || {
|
|
561
485
|
path: options.path,
|
|
562
486
|
removeVoidProperties: true,
|
|
563
|
-
|
|
564
|
-
|
|
487
|
+
},
|
|
488
|
+
transactions: options.transactions,
|
|
489
|
+
ipc: options.ipc
|
|
565
490
|
};
|
|
566
491
|
|
|
567
492
|
const db = new AceBase(dbname, dbOptions);
|
|
@@ -616,7 +541,7 @@ class AceBaseServer extends EventEmitter {
|
|
|
616
541
|
|
|
617
542
|
const setupRules = () => {
|
|
618
543
|
// Check if there is a rules file, load it or generate default
|
|
619
|
-
fs.unwatchFile(rulesFilePath); // If function was triggered because of file change, stop listening now. Listener will be setup again at end of function
|
|
544
|
+
fs.unwatchFile(rulesFilePath, setupRules); // If function was triggered because of file change, stop listening now. Listener will be setup again at end of function
|
|
620
545
|
|
|
621
546
|
const defaultAccessRule = (def => {
|
|
622
547
|
switch (def) {
|
|
@@ -712,10 +637,7 @@ class AceBaseServer extends EventEmitter {
|
|
|
712
637
|
processRules('', accessRules.rules, []);
|
|
713
638
|
|
|
714
639
|
// Watch file for changes
|
|
715
|
-
fs.watchFile(rulesFilePath,
|
|
716
|
-
// rules.json file changed, setup rules again
|
|
717
|
-
setupRules();
|
|
718
|
-
});
|
|
640
|
+
fs.watchFile(rulesFilePath, setupRules);
|
|
719
641
|
}
|
|
720
642
|
setupRules();
|
|
721
643
|
|
|
@@ -1093,6 +1015,7 @@ class AceBaseServer extends EventEmitter {
|
|
|
1093
1015
|
catch(err) {
|
|
1094
1016
|
this.debug.error(`Failed to parse AceBase-Context header: "${context}" in request at ${req.url} from client ${req.ip}`);
|
|
1095
1017
|
}
|
|
1018
|
+
// console.log(`Received request on path ` + req.path);
|
|
1096
1019
|
next();
|
|
1097
1020
|
});
|
|
1098
1021
|
|
|
@@ -2021,9 +1944,14 @@ class AceBaseServer extends EventEmitter {
|
|
|
2021
1944
|
}
|
|
2022
1945
|
});
|
|
2023
1946
|
|
|
1947
|
+
app.get(`/ping/${dbname}`, (req, res) => {
|
|
1948
|
+
// For simple connectivity check
|
|
1949
|
+
res.end('pong');
|
|
1950
|
+
});
|
|
1951
|
+
|
|
2024
1952
|
app.get(`/info/${dbname}`, (req, res) => {
|
|
2025
1953
|
const info = {
|
|
2026
|
-
version: '1.
|
|
1954
|
+
version: '1.8.0', // TODO: Load from package.json
|
|
2027
1955
|
time: Date.now(),
|
|
2028
1956
|
process: process.pid
|
|
2029
1957
|
};
|
|
@@ -2076,7 +2004,7 @@ class AceBaseServer extends EventEmitter {
|
|
|
2076
2004
|
res.send(info);
|
|
2077
2005
|
});
|
|
2078
2006
|
|
|
2079
|
-
app.get(`/data/${dbname}/*`, (req, res) => {
|
|
2007
|
+
app.get(`/data/${dbname}/*`, async (req, res) => {
|
|
2080
2008
|
// Request data
|
|
2081
2009
|
const path = req.path.substr(dbname.length + 7); //.replace(/^\/+/g, '').replace(/\/+$/g, '');
|
|
2082
2010
|
if (!userHasAccess(req.user, path, false, denyDetails => sendUnauthorizedError(res, denyDetails.code, denyDetails.message))) {
|
|
@@ -2107,25 +2035,25 @@ class AceBaseServer extends EventEmitter {
|
|
|
2107
2035
|
options.exclude = [...options.exclude || [], '__auth__', '__log__'];
|
|
2108
2036
|
}
|
|
2109
2037
|
|
|
2110
|
-
|
|
2111
|
-
|
|
2038
|
+
try {
|
|
2039
|
+
// const snap = await db.ref(path).get(options);
|
|
2040
|
+
// const value = snap.val(), context = snap.context();
|
|
2041
|
+
const { value, context } = await db.api.get(path, options);
|
|
2042
|
+
if (!this.config.transactionLoggingEnabled) {
|
|
2043
|
+
delete context.acebase_cursor;
|
|
2044
|
+
}
|
|
2045
|
+
const serialized = Transport.serialize(value);
|
|
2046
|
+
|
|
2047
|
+
res.setHeader('AceBase-Context', JSON.stringify(context));
|
|
2048
|
+
res.send({
|
|
2049
|
+
exists: value !== null,
|
|
2050
|
+
val: serialized.val,
|
|
2051
|
+
map: serialized.map
|
|
2052
|
+
});
|
|
2053
|
+
}
|
|
2054
|
+
catch (err) {
|
|
2055
|
+
res.status(500).send(err);
|
|
2112
2056
|
}
|
|
2113
|
-
|
|
2114
|
-
db.ref(path)
|
|
2115
|
-
.get(options) //.once("value")
|
|
2116
|
-
.then(snap => {
|
|
2117
|
-
const ser = Transport.serialize(snap.val());
|
|
2118
|
-
const data = {
|
|
2119
|
-
exists: snap.exists(),
|
|
2120
|
-
val: ser.val,
|
|
2121
|
-
map: ser.map
|
|
2122
|
-
}
|
|
2123
|
-
res.send(data);
|
|
2124
|
-
})
|
|
2125
|
-
.catch(err => {
|
|
2126
|
-
res.statusCode = 500;
|
|
2127
|
-
res.send(err);
|
|
2128
|
-
});
|
|
2129
2057
|
});
|
|
2130
2058
|
|
|
2131
2059
|
app.get(`/reflect/${dbname}/*`, (req, res) => {
|
|
@@ -2194,43 +2122,77 @@ class AceBaseServer extends EventEmitter {
|
|
|
2194
2122
|
});
|
|
2195
2123
|
});
|
|
2196
2124
|
|
|
2197
|
-
app.get(`/export/${dbname}/*`, (req, res) => {
|
|
2125
|
+
app.get(`/export/${dbname}/*`, async (req, res) => {
|
|
2198
2126
|
// Export API
|
|
2199
|
-
const path = req.path.
|
|
2127
|
+
const path = req.path.slice(dbname.length + 9);
|
|
2200
2128
|
if (!userHasAccess(req.user, path, false, denyDetails => sendUnauthorizedError(res, denyDetails.code, denyDetails.message))) {
|
|
2201
2129
|
return;
|
|
2202
2130
|
}
|
|
2203
2131
|
const format = req.query.format || 'json';
|
|
2132
|
+
const type_safe = req.query.type_safe !== '0';
|
|
2204
2133
|
|
|
2205
|
-
const
|
|
2206
|
-
write(chunk)
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
// OLD: causes streaming issues
|
|
2210
|
-
// return new Promise((resolve, reject) => {
|
|
2211
|
-
// res.write(chunk, err => {
|
|
2212
|
-
// if (err) { reject(err); }
|
|
2213
|
-
// else { resolve(); }
|
|
2214
|
-
// });
|
|
2215
|
-
// });
|
|
2134
|
+
const write = async (chunk) => {
|
|
2135
|
+
let ok = res.write(chunk);
|
|
2136
|
+
if (!ok) {
|
|
2137
|
+
await new Promise(resolve => res.once('drain', resolve));
|
|
2216
2138
|
}
|
|
2217
2139
|
};
|
|
2218
2140
|
|
|
2219
2141
|
const ref = db.ref(path);
|
|
2220
2142
|
res.setHeader('Content-Disposition', `attachment; filename=${ref.key || 'export'}.json`); // Will be treated as a download in browser
|
|
2221
2143
|
|
|
2222
|
-
|
|
2223
|
-
|
|
2144
|
+
try {
|
|
2145
|
+
await ref.export(write, { format, type_safe });
|
|
2146
|
+
}
|
|
2147
|
+
catch (err) {
|
|
2148
|
+
this.debug.error(`Error exporting data for path "/${path}": `, err);
|
|
2149
|
+
if (!res.headersSent) {
|
|
2150
|
+
res.statusCode = 500;
|
|
2151
|
+
res.send(err);
|
|
2152
|
+
}
|
|
2153
|
+
}
|
|
2154
|
+
finally {
|
|
2224
2155
|
res.end();
|
|
2225
|
-
}
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
.
|
|
2231
|
-
|
|
2156
|
+
}
|
|
2157
|
+
});
|
|
2158
|
+
|
|
2159
|
+
app.post(`/import/${dbname}/*`, async (req, res) => {
|
|
2160
|
+
// Import API
|
|
2161
|
+
const path = req.path.slice(dbname.length + 9);
|
|
2162
|
+
if (!userHasAccess(req.user, path, false, denyDetails => sendUnauthorizedError(res, denyDetails.code, denyDetails.message))) {
|
|
2163
|
+
return;
|
|
2164
|
+
}
|
|
2165
|
+
const format = req.query.format || 'json';
|
|
2166
|
+
const suppress_events = req.query.suppress_events === '1';
|
|
2167
|
+
|
|
2168
|
+
req.pause(); // Switch to non-flowing mode so we can use .read() upon request
|
|
2169
|
+
let eof = false;
|
|
2170
|
+
req.once('end', () => { eof = true; });
|
|
2171
|
+
const read = async (length) => {
|
|
2172
|
+
let chunk = req.read();
|
|
2173
|
+
if (chunk === null && !eof) {
|
|
2174
|
+
await new Promise(resolve => req.once('readable', resolve));
|
|
2175
|
+
chunk = req.read();
|
|
2176
|
+
}
|
|
2177
|
+
console.log(`Received chunk: `, chunk);
|
|
2178
|
+
return chunk;
|
|
2179
|
+
};
|
|
2180
|
+
|
|
2181
|
+
const ref = db.ref(path);
|
|
2182
|
+
try {
|
|
2183
|
+
await ref.import(read, { format, suppress_events });
|
|
2184
|
+
res.send({ success: true });
|
|
2185
|
+
}
|
|
2186
|
+
catch (err) {
|
|
2187
|
+
this.debug.error(`Error importing data for path "/${path}": `, err);
|
|
2188
|
+
if (!res.headersSent) {
|
|
2189
|
+
res.statusCode = 500;
|
|
2190
|
+
res.send({ success: false, reason: err.message });
|
|
2191
|
+
}
|
|
2192
|
+
}
|
|
2193
|
+
finally {
|
|
2232
2194
|
res.end();
|
|
2233
|
-
}
|
|
2195
|
+
}
|
|
2234
2196
|
});
|
|
2235
2197
|
|
|
2236
2198
|
app.get(`/exists/${dbname}/*`, (req, res) => {
|
|
@@ -2288,12 +2250,12 @@ class AceBaseServer extends EventEmitter {
|
|
|
2288
2250
|
}
|
|
2289
2251
|
catch(err) {
|
|
2290
2252
|
if (err instanceof SchemaValidationError) {
|
|
2291
|
-
logRef.push({ action: 'update_data', success: false, code: 'schema_validation_failed', path, error: err.reason });
|
|
2253
|
+
logRef && logRef.push({ action: 'update_data', success: false, code: 'schema_validation_failed', path, error: err.reason });
|
|
2292
2254
|
res.status(422).send({ code: 'schema_validation_failed', message: err.message });
|
|
2293
2255
|
}
|
|
2294
2256
|
else {
|
|
2295
2257
|
this.debug.error(`failed to update "${path}":`, err);
|
|
2296
|
-
logRef.push({ action: 'update_data', success: false, code: `unknown_error`, path, error: err.message });
|
|
2258
|
+
logRef && logRef.push({ action: 'update_data', success: false, code: `unknown_error`, path, error: err.message });
|
|
2297
2259
|
sendError(res, err);
|
|
2298
2260
|
}
|
|
2299
2261
|
}
|
|
@@ -2335,7 +2297,7 @@ class AceBaseServer extends EventEmitter {
|
|
|
2335
2297
|
};
|
|
2336
2298
|
});
|
|
2337
2299
|
|
|
2338
|
-
app.post(`/query/${dbname}/*`, (req, res) => {
|
|
2300
|
+
app.post(`/query/${dbname}/*`, async (req, res) => {
|
|
2339
2301
|
// Execute query
|
|
2340
2302
|
const path = req.path.substr(dbname.length + 8);
|
|
2341
2303
|
if (!userHasAccess(req.user, path, false, denyDetails => sendUnauthorizedError(res, denyDetails.code, denyDetails.message))) {
|
|
@@ -2343,6 +2305,9 @@ class AceBaseServer extends EventEmitter {
|
|
|
2343
2305
|
}
|
|
2344
2306
|
|
|
2345
2307
|
const data = Transport.deserialize(req.body);
|
|
2308
|
+
if (typeof data !== 'object' || typeof data.query !== 'object' || typeof data.options !== 'object') {
|
|
2309
|
+
return sendError(res, { code: 'invalid_request', message: 'Invalid query request' });
|
|
2310
|
+
}
|
|
2346
2311
|
const query = data.query;
|
|
2347
2312
|
const options = data.options;
|
|
2348
2313
|
if (options.monitor === true) {
|
|
@@ -2366,14 +2331,21 @@ class AceBaseServer extends EventEmitter {
|
|
|
2366
2331
|
}
|
|
2367
2332
|
options.eventHandler = sendEvent;
|
|
2368
2333
|
}
|
|
2369
|
-
|
|
2370
|
-
|
|
2334
|
+
try {
|
|
2335
|
+
const { results, context } = await db.api.query(path, query, options);
|
|
2336
|
+
if (!this.config.transactionLoggingEnabled) {
|
|
2337
|
+
delete context.acebase_cursor;
|
|
2338
|
+
}
|
|
2371
2339
|
const response = {
|
|
2372
2340
|
count: results.length,
|
|
2373
2341
|
list: results // []
|
|
2374
2342
|
};
|
|
2343
|
+
res.setHeader('AceBase-Context', JSON.stringify(context));
|
|
2375
2344
|
res.send(Transport.serialize(response));
|
|
2376
|
-
}
|
|
2345
|
+
}
|
|
2346
|
+
catch(err) {
|
|
2347
|
+
sendError(res, { code: 'unknown', message: err.message });
|
|
2348
|
+
}
|
|
2377
2349
|
});
|
|
2378
2350
|
|
|
2379
2351
|
app.get(`/index/${dbname}`, async (req, res) => {
|
|
@@ -2477,9 +2449,16 @@ class AceBaseServer extends EventEmitter {
|
|
|
2477
2449
|
}
|
|
2478
2450
|
try {
|
|
2479
2451
|
const data = req.query;
|
|
2480
|
-
let targets =
|
|
2452
|
+
let targets = typeof data.path === 'string'
|
|
2453
|
+
? [{ path: data.path, events: ['value'] }]
|
|
2454
|
+
: typeof data.for === 'string'
|
|
2455
|
+
? JSON.parse(data.for)
|
|
2456
|
+
: null;
|
|
2457
|
+
if (targets === null) {
|
|
2458
|
+
return sendError(res, { code: 'invalid_request', message: 'Invalid mutations request' });
|
|
2459
|
+
}
|
|
2481
2460
|
if (targets.length === 0) {
|
|
2482
|
-
targets.push({ path: '', events: '
|
|
2461
|
+
targets.push({ path: '', events: ['value'] });
|
|
2483
2462
|
}
|
|
2484
2463
|
// Filter out any requested paths user does not have access to.
|
|
2485
2464
|
targets = targets.filter(target => {
|
|
@@ -2494,10 +2473,55 @@ class AceBaseServer extends EventEmitter {
|
|
|
2494
2473
|
return sendUnauthorizedError(res, 'not_authorized', 'User is not authorized to access this data');
|
|
2495
2474
|
}
|
|
2496
2475
|
|
|
2497
|
-
const cursor = data
|
|
2498
|
-
const
|
|
2499
|
-
|
|
2500
|
-
res.
|
|
2476
|
+
const { cursor, timestamp } = data;
|
|
2477
|
+
const result = await db.api.getMutations({ for: targets, cursor, timestamp });
|
|
2478
|
+
|
|
2479
|
+
res.setHeader('AceBase-Context', JSON.stringify({ acebase_cursor: result.new_cursor }));
|
|
2480
|
+
res.contentType('application/json');
|
|
2481
|
+
res.send(result.mutations);
|
|
2482
|
+
}
|
|
2483
|
+
catch(err) {
|
|
2484
|
+
sendError(res, err);
|
|
2485
|
+
}
|
|
2486
|
+
});
|
|
2487
|
+
|
|
2488
|
+
app.get(`/sync/changes/${dbname}`, async (req, res) => {
|
|
2489
|
+
// Gets effective changes for specific path(s) and event combinations since given cursor
|
|
2490
|
+
if (!this.config.transactionLoggingEnabled) {
|
|
2491
|
+
return sendError(res, { code: 'no_transaction_logging', message: 'Transaction logging not enabled' });
|
|
2492
|
+
}
|
|
2493
|
+
try {
|
|
2494
|
+
const data = req.query;
|
|
2495
|
+
let targets = typeof data.path === 'string'
|
|
2496
|
+
? [{ path: data.path, events: ['value'] }]
|
|
2497
|
+
: typeof data.for === 'string'
|
|
2498
|
+
? JSON.parse(data.for)
|
|
2499
|
+
: null;
|
|
2500
|
+
if (targets === null) {
|
|
2501
|
+
return sendError(res, { code: 'invalid_request', message: 'Invalid mutations request' });
|
|
2502
|
+
}
|
|
2503
|
+
if (targets.length === 0) {
|
|
2504
|
+
targets.push({ path: '', events: ['value'] });
|
|
2505
|
+
}
|
|
2506
|
+
// Filter out any requested paths user does not have access to.
|
|
2507
|
+
targets = targets.filter(target => {
|
|
2508
|
+
let path = target.path;
|
|
2509
|
+
if (target.events.every(event => /^(?:notify_)?child_/.test(event))) { //if (!target.events.some(event => ['value','notify_value','mutations','mutated'].includes(event))) {
|
|
2510
|
+
// Only child_ events, check if they have access to children instead
|
|
2511
|
+
path = PathInfo.get(path).childPath('*');
|
|
2512
|
+
}
|
|
2513
|
+
return userHasAccess(req.user, path, false);
|
|
2514
|
+
});
|
|
2515
|
+
if (targets.length === 0) {
|
|
2516
|
+
return sendUnauthorizedError(res, 'not_authorized', 'User is not authorized to access this data');
|
|
2517
|
+
}
|
|
2518
|
+
|
|
2519
|
+
const { cursor, timestamp } = data;
|
|
2520
|
+
const result = await db.api.getChanges({ for: targets, cursor, timestamp });
|
|
2521
|
+
|
|
2522
|
+
res.setHeader('AceBase-Context', JSON.stringify({ acebase_cursor: result.new_cursor }));
|
|
2523
|
+
res.contentType('application/json');
|
|
2524
|
+
res.send(result.changes);
|
|
2501
2525
|
}
|
|
2502
2526
|
catch(err) {
|
|
2503
2527
|
sendError(res, err);
|
|
@@ -2634,19 +2658,53 @@ class AceBaseServer extends EventEmitter {
|
|
|
2634
2658
|
|
|
2635
2659
|
this.shutdown = async () => {
|
|
2636
2660
|
this.debug.warn('shutting down server');
|
|
2637
|
-
fs.unwatchFile(rulesFilePath);
|
|
2661
|
+
fs.unwatchFile(rulesFilePath, setupRules);
|
|
2638
2662
|
await new Promise((resolve) => {
|
|
2639
2663
|
server.close(resolve);
|
|
2640
|
-
clients.list.forEach(client => {
|
|
2664
|
+
clients.list.slice().forEach(client => {
|
|
2641
2665
|
client.socket.disconnect(true);
|
|
2642
2666
|
});
|
|
2643
2667
|
});
|
|
2644
2668
|
this.debug.warn('closing database');
|
|
2645
2669
|
await db.close(); // TODO: Check what happens if shutdown was triggered by SIGINT. Maybe already closed?
|
|
2646
2670
|
this.debug.warn('shutdown complete');
|
|
2671
|
+
|
|
2672
|
+
// Emit events to let the outside world know we shut down.
|
|
2673
|
+
// This is especially important if this instance was running in a Node.js cluster: the process will
|
|
2674
|
+
// not exit automatically after this shutdown because Node.js' IPC channel between worker and master is still open.
|
|
2675
|
+
// By sending these events, the cluster manager can determine if it should (and when to) execute process.exit()
|
|
2676
|
+
this.emit('shutdown'); // Emit on AceBaseServer instance
|
|
2677
|
+
process.emit('acebase-server-shutdown'); // Emit on process
|
|
2678
|
+
try {
|
|
2679
|
+
process.send && process.send('acebase-server-shutdown'); // Send to master process when running in a Node.js cluster
|
|
2680
|
+
}
|
|
2681
|
+
catch(err) {
|
|
2682
|
+
// IPC Channel has apparently been closed already
|
|
2683
|
+
}
|
|
2647
2684
|
};
|
|
2648
2685
|
process.on('SIGINT', () => this.shutdown());
|
|
2649
2686
|
|
|
2687
|
+
let paused = false;
|
|
2688
|
+
this.pause = async () => {
|
|
2689
|
+
if (paused) { throw new Error('Server is already paused'); }
|
|
2690
|
+
server.close();
|
|
2691
|
+
this.debug.warn(`Paused "${dbname}" database server at ${this.config.url}`);
|
|
2692
|
+
this.emit('pause');
|
|
2693
|
+
paused = true;
|
|
2694
|
+
}
|
|
2695
|
+
|
|
2696
|
+
this.resume = async () => {
|
|
2697
|
+
if (!paused) { throw new Error('Server is not paused'); }
|
|
2698
|
+
return new Promise(resolve => {
|
|
2699
|
+
server.listen(this.config.port, this.config.hostname, () => {
|
|
2700
|
+
this.debug.warn(`Resumed "${dbname}" database server at ${this.config.url}`);
|
|
2701
|
+
this.emit('resume');
|
|
2702
|
+
paused = false;
|
|
2703
|
+
resolve();
|
|
2704
|
+
});
|
|
2705
|
+
})
|
|
2706
|
+
}
|
|
2707
|
+
|
|
2650
2708
|
/** @type {Map<string, Client>} */
|
|
2651
2709
|
// const clientsNew = new Map();
|
|
2652
2710
|
|
|
@@ -2785,6 +2843,7 @@ class AceBaseServer extends EventEmitter {
|
|
|
2785
2843
|
previous: previousValue
|
|
2786
2844
|
});
|
|
2787
2845
|
this.debug.verbose(`Sending data event "${data.event}" for path "/${path}" to client ${socket.id}`.colorize([ColorStyle.bgWhite, ColorStyle.black]));
|
|
2846
|
+
// TODO: let large data events notify the client, then let them download the data manually so it doesn't have to be transmitted through the websocket
|
|
2788
2847
|
socket.emit('data-event', {
|
|
2789
2848
|
subscr_path: subscriptionPath,
|
|
2790
2849
|
path,
|
|
@@ -2987,7 +3046,6 @@ if (__filename.replace(/\\/g,"/").endsWith("/acebase-server.js") && process.argv
|
|
|
2987
3046
|
module.exports = {
|
|
2988
3047
|
AceBaseServer,
|
|
2989
3048
|
AceBaseServerSettings,
|
|
2990
|
-
AceBaseClusterSettings,
|
|
2991
3049
|
AceBaseEmailRequest,
|
|
2992
3050
|
AceBaseUserSignupEmailRequest,
|
|
2993
3051
|
AceBaseUserResetPasswordEmailRequest,
|
package/src/cluster.js
CHANGED
|
@@ -3,6 +3,15 @@ const { AceBaseServer } = require('./acebase-server');
|
|
|
3
3
|
const cluster = require('cluster');
|
|
4
4
|
const numCPUs = 2; //require('os').cpus().length;
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Node.js cluster example. You can use this as a reference how to create an AceBaseServer cluster using Node.js'
|
|
8
|
+
* built-in cluster functionality.
|
|
9
|
+
*
|
|
10
|
+
* If you want to run a pm2 or cloud-based cluster instead, do NOT use this code.
|
|
11
|
+
* To do that, see https://github.com/appy-one/acebase-ipc-server
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
|
|
6
15
|
let dbname = "default";
|
|
7
16
|
let options = { /* default options */ }; // Load from cluster.config.js!
|
|
8
17
|
options.authentication = { enabled: false };
|
|
@@ -10,52 +19,42 @@ options.https = { enabled: false };
|
|
|
10
19
|
|
|
11
20
|
if (cluster.isMaster) {
|
|
12
21
|
// Startup master
|
|
13
|
-
console.log(`
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
workers: []
|
|
19
|
-
};
|
|
22
|
+
console.log(`Starting database server cluster with ${numCPUs} workers`);
|
|
23
|
+
const master = new AceBase(dbname, options);
|
|
24
|
+
master.once('ready', () => {
|
|
25
|
+
console.log(`Master database server started on process ${process.pid}`);
|
|
26
|
+
});
|
|
20
27
|
|
|
21
28
|
for (let i = 0; i < numCPUs; i++) {
|
|
22
|
-
|
|
23
|
-
worker.on(
|
|
29
|
+
const worker = cluster.fork();
|
|
30
|
+
worker.on('disconnect', () => {
|
|
24
31
|
console.error(`worker ${worker.process.pid} disconnected`);
|
|
25
|
-
// TODO: restart!
|
|
26
32
|
});
|
|
27
|
-
|
|
33
|
+
worker.on('message', (msg) => {
|
|
34
|
+
if (msg === 'acebase-server-shutdown') {
|
|
35
|
+
// We could handle shutdown message here to kill the worker safely, but we'll do it in the worker threads instead: see below
|
|
36
|
+
// worker.kill();
|
|
37
|
+
}
|
|
38
|
+
});
|
|
28
39
|
}
|
|
29
40
|
|
|
30
|
-
|
|
31
|
-
console.log("Unhandled Rejection in master ", process.pid, " at: ", reason.stack);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
console.log(`Starting database server with ${options.cluster.workers.length} workers`);
|
|
35
|
-
const master = new AceBase(dbname, options);
|
|
36
|
-
master.once("ready", () => {
|
|
37
|
-
console.log(`Master database server started on process ${process.pid}`);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
cluster.on("exit", (worker, code, signal) => {
|
|
41
|
+
cluster.on('exit', (worker, code, signal) => {
|
|
41
42
|
console.error(`worker ${worker.process.pid} died`);
|
|
42
43
|
});
|
|
43
44
|
}
|
|
44
45
|
else {
|
|
45
46
|
console.log(`Worker ${process.pid} is running`);
|
|
46
47
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
isMaster: false,
|
|
50
|
-
master: process
|
|
51
|
-
};
|
|
52
|
-
process.on("unhandledRejection", (reason, p) => {
|
|
53
|
-
console.log("Unhandled Rejection in worker ", process.pid, " at: ", reason.stack);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
const workerServer = new AceBaseServer(dbname, options);
|
|
57
|
-
workerServer.ready(() => {
|
|
48
|
+
const server = new AceBaseServer(dbname, options);
|
|
49
|
+
server.ready(() => {
|
|
58
50
|
console.log(`Worker database server started on process ${process.pid}`);
|
|
59
51
|
});
|
|
60
|
-
|
|
52
|
+
// Handle shutdown event to exit the worker process safely
|
|
53
|
+
server.on('shutdown', () => {
|
|
54
|
+
process.exit();
|
|
55
|
+
});
|
|
56
|
+
// Other option:
|
|
57
|
+
// process.on('acebase-server-shutdown', () => {
|
|
58
|
+
// process.exit();
|
|
59
|
+
// });
|
|
61
60
|
}
|
package/src/index.d.ts
CHANGED
|
@@ -4,7 +4,14 @@ import { AceBase } from 'acebase';
|
|
|
4
4
|
declare namespace acebaseserver {
|
|
5
5
|
class AceBaseServer {
|
|
6
6
|
constructor(dbname: string, options?: AceBaseServerSettings)
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Wait for the server to be ready to accept incoming connections
|
|
10
|
+
* @param callback (optional) callback function that is called when ready. You can also use the returned promise
|
|
11
|
+
* @returns returns a promise that resolves when ready
|
|
12
|
+
*/
|
|
13
|
+
ready(callback?: () => void): Promise<void>
|
|
14
|
+
|
|
8
15
|
get url(): string
|
|
9
16
|
|
|
10
17
|
/**
|
|
@@ -67,8 +74,17 @@ declare namespace acebaseserver {
|
|
|
67
74
|
|
|
68
75
|
/**
|
|
69
76
|
* Shuts down the server. Stops listening for incoming connections, breaks current connections and closes the database.
|
|
77
|
+
* Is automatically executed when a "SIGINT" process event is received.
|
|
78
|
+
*
|
|
79
|
+
* Once the shutdown procedure is completed, it emits a "shutdown" event on the server instance, "acebase-server-shutdown" event on the `process`, and sends an 'acebase-server-shutdown' IPC message if Node.js clustering is used.
|
|
80
|
+
* These events can be handled by cluster managing code to `kill` or `exit` the process safely.
|
|
70
81
|
*/
|
|
71
82
|
shutdown(): Promise<void>
|
|
83
|
+
|
|
84
|
+
/** Temporarily stops the server from handling incoming connections, but keeps existing connections open */
|
|
85
|
+
pause(): Promise<void>
|
|
86
|
+
/** Resumes handling incoming connections */
|
|
87
|
+
resume(): Promise<void>
|
|
72
88
|
}
|
|
73
89
|
|
|
74
90
|
interface IOAuthProviderSettings {
|
|
@@ -102,6 +118,10 @@ declare namespace acebaseserver {
|
|
|
102
118
|
email?: AceBaseServerEmailSettings
|
|
103
119
|
/** Transaction logging settings. Warning: BETA stage, do NOT use in production yet */
|
|
104
120
|
transactions?: AceBaseServerTransactionLogSettings
|
|
121
|
+
/** IPC settings for pm2 or cloud-based clusters. BETA stage, see https://github.com/appy-one/acebase-ipc-server */
|
|
122
|
+
ipc?: IPCClientSettings
|
|
123
|
+
/** Allows overriding of default storage settings used by the database. ALPHA stage */
|
|
124
|
+
storage?: any
|
|
105
125
|
}
|
|
106
126
|
|
|
107
127
|
interface AceBaseServerHttpsSettings {
|
|
@@ -118,6 +138,8 @@ declare namespace acebaseserver {
|
|
|
118
138
|
/** 'allow', 'deny' or 'auth' */
|
|
119
139
|
defaultAccessRule?: string
|
|
120
140
|
defaultAdminPassword?: string
|
|
141
|
+
separateDb?: boolean
|
|
142
|
+
/** @deprecated Misspelled, use separateDb instead */
|
|
121
143
|
seperateDb?: boolean
|
|
122
144
|
}
|
|
123
145
|
|
|
@@ -145,7 +167,18 @@ declare namespace acebaseserver {
|
|
|
145
167
|
maxAge?: number;
|
|
146
168
|
noWait?: boolean;
|
|
147
169
|
}
|
|
148
|
-
|
|
170
|
+
interface IPCClientSettings {
|
|
171
|
+
/** IPC Server hostname. Default is "localhost" */
|
|
172
|
+
host?: string
|
|
173
|
+
/** IPC Server port */
|
|
174
|
+
port: number
|
|
175
|
+
/** IPC Server token needed to access the server. Only needed if the server does not use a token */
|
|
176
|
+
token?: string
|
|
177
|
+
/** Whether to use a secure connection to the IPC server, default is `false` */
|
|
178
|
+
ssl?: boolean
|
|
179
|
+
/** Role of the IPC Client. There can only be 1 `master`, all other need to be a `worker`. */
|
|
180
|
+
role: 'master'|'worker'
|
|
181
|
+
}
|
|
149
182
|
interface AceBaseEmailRequest {
|
|
150
183
|
readonly type: string
|
|
151
184
|
readonly ip: string
|
package/src/start.js
CHANGED
|
@@ -15,11 +15,27 @@ function getVariable(name, defaultValue) {
|
|
|
15
15
|
const path = getVariable('DBPATH', '.');
|
|
16
16
|
const dbname = getVariable('DBNAME', 'default');
|
|
17
17
|
const host = getVariable('HOST', '0.0.0.0');
|
|
18
|
-
const port =
|
|
19
|
-
const
|
|
18
|
+
const port = +getVariable('PORT', 3000);
|
|
19
|
+
const ipcPort = +getVariable('IPC_PORT', 0);
|
|
20
20
|
const options = { host, port, path };
|
|
21
|
-
if (
|
|
22
|
-
|
|
21
|
+
if (ipcPort > 0) {
|
|
22
|
+
const role = getVariable('IPC_ROLE');
|
|
23
|
+
if (!['master','worker'].includes(role)) {
|
|
24
|
+
throw new Error('IPC_ROLE must be either "master" or "worker"');
|
|
25
|
+
}
|
|
26
|
+
options.ipc = {
|
|
27
|
+
host: getVariable('IPC_HOST'),
|
|
28
|
+
port: ipcPort,
|
|
29
|
+
ssl: +getVariable('IPC_SSL', 0) === 1,
|
|
30
|
+
token: getVariable('IPC_TOKEN'),
|
|
31
|
+
role
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
if (+getVariable('TXLOG', 0) === 1) {
|
|
35
|
+
options.transactions = {
|
|
36
|
+
log: true,
|
|
37
|
+
maxAge: +getVariable('TXDAYS', 30)
|
|
38
|
+
};
|
|
23
39
|
}
|
|
24
40
|
const server = new AceBaseServer(dbname, options);
|
|
25
41
|
server.once("ready", () => {
|
|
@@ -263,7 +263,7 @@ M.AutoInit();
|
|
|
263
263
|
</script>
|
|
264
264
|
|
|
265
265
|
<!-- AceBase client js -->
|
|
266
|
-
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/acebase-client@1.
|
|
266
|
+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/acebase-client@1.11.0/dist/browser.min.js"></script>
|
|
267
267
|
<!-- <script type="text/javascript" src="acebase-client-browser.min.js"></script> -->
|
|
268
268
|
|
|
269
269
|
<!-- AceBase Webmanager js -->
|
package/src/webmanager/index.js
CHANGED
|
@@ -10,7 +10,8 @@ function connect(dbname, username, password) {
|
|
|
10
10
|
let createNew = connection.db === null || connection.dbname !== dbname;
|
|
11
11
|
if (createNew) {
|
|
12
12
|
connection.db && connection.db.disconnect();
|
|
13
|
-
|
|
13
|
+
let host = location.hostname, port = location.port, https = location.protocol === 'https:', autoConnect = true;
|
|
14
|
+
connection.db = new AceBaseClient({ dbname, host, port, https, autoConnect });
|
|
14
15
|
}
|
|
15
16
|
return connection.db.ready()
|
|
16
17
|
.then(() => {
|
|
@@ -275,7 +276,7 @@ function updateBrowsePath(path = '') {
|
|
|
275
276
|
return showError('node does not exist');
|
|
276
277
|
}
|
|
277
278
|
else if (info.children.list.length === 0) {
|
|
278
|
-
|
|
279
|
+
showError('node has no children');
|
|
279
280
|
}
|
|
280
281
|
|
|
281
282
|
if (skip === 0 && !info.children.more) {
|