acebase-server 1.5.0 → 1.7.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 +243 -215
- package/src/cluster.js +33 -34
- package/src/index.d.ts +35 -2
- package/src/start.js +20 -4
- package/src/webmanager/index.html +2 -2
- 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.7.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.12.2",
|
|
48
|
+
"acebase-core": "^1.10.1",
|
|
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
|
|
|
@@ -1391,9 +1313,17 @@ class AceBaseServer extends EventEmitter {
|
|
|
1391
1313
|
email(email) {
|
|
1392
1314
|
return /[a-z0-9_.+]+@([a-z0-9\-]+\.)+[a-z]{2,}/i.test(email);
|
|
1393
1315
|
},
|
|
1316
|
+
async newEmailAddress(email) {
|
|
1317
|
+
const exists = await authRef.query().filter('email', '==', email).exists();
|
|
1318
|
+
return !exists;
|
|
1319
|
+
},
|
|
1394
1320
|
username(username) {
|
|
1395
1321
|
return username !== 'admin' && typeof username === 'string' && username.length >= 5 && /^[a-z0-9]+$/.test(username);
|
|
1396
1322
|
},
|
|
1323
|
+
async newUsername(username) {
|
|
1324
|
+
const exists = await authRef.query().filter('username', '==', username).exists();
|
|
1325
|
+
return !exists;
|
|
1326
|
+
},
|
|
1397
1327
|
displayName(displayName) {
|
|
1398
1328
|
return typeof displayName === 'string' && displayName.length >= 5;
|
|
1399
1329
|
},
|
|
@@ -1417,13 +1347,16 @@ class AceBaseServer extends EventEmitter {
|
|
|
1417
1347
|
const validationErrors = {
|
|
1418
1348
|
email: { code: 'invalid_email', message: 'Invalid email address' },
|
|
1419
1349
|
username: { code: 'invalid_username', message: 'Invalid username, must be at least 5 characters and can only contain lowercase characters a-z and 0-9' },
|
|
1350
|
+
emailExists: { code: 'email_conflict', message: 'Account with email address exists already' },
|
|
1351
|
+
usernameExists: { code: 'username_conflict', message: 'Account with username exists already' },
|
|
1352
|
+
emailOrUsernameExists: { code: 'conflict', message: `Account with username and/or email already exists` },
|
|
1420
1353
|
displayName: { code: 'invalid_display_name', message: 'Invalid display_name, must be at least 5 characters' },
|
|
1421
1354
|
password: { code: 'invalid_password', message: 'Invalid password, must be at least 8 characters and cannot contain spaces' },
|
|
1422
1355
|
picture: { code: 'invalid_picture', message: 'Invalid picture, must be an object with url, width and height properties'},
|
|
1423
1356
|
settings: { code: 'invalid_settings', message: 'Invalid settings, must be an object and contain only string, number and/or boolean values. Additionaly, string values can have a maximum length of 250, and a maximum of 100 settings can be added' }
|
|
1424
1357
|
};
|
|
1425
1358
|
|
|
1426
|
-
app.post(`/auth/${dbname}/signup`, (req, res) => {
|
|
1359
|
+
app.post(`/auth/${dbname}/signup`, async (req, res) => {
|
|
1427
1360
|
if (!this.config.authentication.allowUserSignup && (!req.user || req.user.username !== 'admin')) {
|
|
1428
1361
|
logRef.push({ action: 'signup', success: false, code: 'user_signup_disabled', ip: req.ip, date: new Date() });
|
|
1429
1362
|
res.statusCode = 403; // Forbidden
|
|
@@ -1443,9 +1376,15 @@ class AceBaseServer extends EventEmitter {
|
|
|
1443
1376
|
else if (details.email && !isValid.email(details.email)) {
|
|
1444
1377
|
err = validationErrors.email;
|
|
1445
1378
|
}
|
|
1446
|
-
else if (details.
|
|
1379
|
+
else if (details.email && !await isValid.newEmailAddress(details.email)) {
|
|
1380
|
+
err = validationErrors.emailExists;
|
|
1381
|
+
}
|
|
1382
|
+
else if (details.username && !isValid.username(details.username)) {
|
|
1447
1383
|
err = validationErrors.username;
|
|
1448
1384
|
}
|
|
1385
|
+
else if (details.username && !await isValid.newUsername(details.username)) {
|
|
1386
|
+
err = validationErrors.usernameExists;
|
|
1387
|
+
}
|
|
1449
1388
|
else if (!isValid.displayName(details.displayName)) {
|
|
1450
1389
|
err = validationErrors.displayName;
|
|
1451
1390
|
}
|
|
@@ -1455,36 +1394,20 @@ class AceBaseServer extends EventEmitter {
|
|
|
1455
1394
|
else if (!isValid.settings(details.settings)) {
|
|
1456
1395
|
err = validationErrors.settings;
|
|
1457
1396
|
}
|
|
1458
|
-
|
|
1397
|
+
|
|
1398
|
+
if (err && [validationErrors.emailExists, validationErrors.usernameExists].includes(err)) {
|
|
1399
|
+
logRef.push({ action: 'signup', success: false, code: 'conflict', ip: req.ip, date: new Date(), username: details.username, email: details.email });
|
|
1400
|
+
res.statusCode = 409; // conflict
|
|
1401
|
+
return res.send(validationErrors.emailOrUsernameExists);
|
|
1402
|
+
}
|
|
1403
|
+
else if (err) {
|
|
1459
1404
|
// Log failure
|
|
1460
1405
|
logRef.push({ action: 'signup', success: false, code: err.code, ip: req.ip, date: new Date() });
|
|
1461
|
-
|
|
1462
1406
|
res.statusCode = 422; // Unprocessable Entity
|
|
1463
|
-
res.send(err);
|
|
1464
|
-
return;
|
|
1407
|
+
return res.send(err);
|
|
1465
1408
|
}
|
|
1466
1409
|
|
|
1467
|
-
|
|
1468
|
-
const promises = [];
|
|
1469
|
-
if (details.username) {
|
|
1470
|
-
let promise = authRef.query().filter('username', '==', details.username).get();
|
|
1471
|
-
promises.push(promise);
|
|
1472
|
-
}
|
|
1473
|
-
if (details.email) {
|
|
1474
|
-
let promise = authRef.query().filter('email', '==', details.email).get();
|
|
1475
|
-
promises.push(promise);
|
|
1476
|
-
}
|
|
1477
|
-
return Promise.all(promises).then(arr => {
|
|
1478
|
-
return arr.reduce((n, snaps) => n + snaps.length, 0);
|
|
1479
|
-
})
|
|
1480
|
-
.then(userCount => {
|
|
1481
|
-
if (userCount > 0) {
|
|
1482
|
-
logRef.push({ action: 'signup', success: false, code: 'conflict', ip: req.ip, date: new Date(), username: details.username, email: details.email });
|
|
1483
|
-
res.statusCode = 409; // conflict
|
|
1484
|
-
res.send({ code: 'conflict', message: `Account with username and/or email already exists` });
|
|
1485
|
-
return;
|
|
1486
|
-
}
|
|
1487
|
-
|
|
1410
|
+
try {
|
|
1488
1411
|
// Ok, create user
|
|
1489
1412
|
let pwd = createPasswordHash(details.password);
|
|
1490
1413
|
/** @type {DbUserAccountDetails} */
|
|
@@ -1504,52 +1427,50 @@ class AceBaseServer extends EventEmitter {
|
|
|
1504
1427
|
settings: details.settings || {}
|
|
1505
1428
|
};
|
|
1506
1429
|
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
user.uid = uid;
|
|
1430
|
+
const userRef = await authRef.push(user);
|
|
1431
|
+
const uid = userRef.key;
|
|
1432
|
+
user.uid = uid;
|
|
1511
1433
|
|
|
1512
|
-
|
|
1513
|
-
|
|
1434
|
+
// Log success
|
|
1435
|
+
logRef.push({ action: 'signup', success: true, ip: req.ip, date: new Date(), uid });
|
|
1514
1436
|
|
|
1515
|
-
|
|
1516
|
-
|
|
1437
|
+
// Cache the user
|
|
1438
|
+
_authCache.set(user.uid, user);
|
|
1517
1439
|
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1440
|
+
// Request welcome e-mail to be sent
|
|
1441
|
+
const request = new AceBaseUserSignupEmailRequest({
|
|
1442
|
+
user: {
|
|
1443
|
+
uid: user.uid,
|
|
1444
|
+
username: user.username,
|
|
1445
|
+
email: user.email,
|
|
1446
|
+
displayName: user.display_name,
|
|
1447
|
+
settings: user.settings
|
|
1448
|
+
},
|
|
1449
|
+
date: user.created,
|
|
1450
|
+
ip: user.created_ip,
|
|
1451
|
+
provider: 'acebase',
|
|
1452
|
+
activationCode: createSignedPublicToken({ uid: user.uid }, _secureObjectsSalt), // , email: user.email, code: user.email_verification_code
|
|
1453
|
+
emailVerified: false
|
|
1454
|
+
});
|
|
1533
1455
|
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1456
|
+
this.config.email && this.config.email.send(request).catch(err => {
|
|
1457
|
+
logRef.push({ action: 'signup_email', success: false, code: 'unexpected', ip: req.ip, date: new Date(), error: err.message, request });
|
|
1458
|
+
});
|
|
1537
1459
|
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
});
|
|
1460
|
+
// Return the positive news
|
|
1461
|
+
const isAdmin = req.user && req.user.uid === 'admin';
|
|
1462
|
+
res.send({
|
|
1463
|
+
access_token: isAdmin ? '' : createPublicAccessToken(user.uid, req.ip, user.access_token, _secureObjectsSalt),
|
|
1464
|
+
user: getPublicAccountDetails(user)
|
|
1544
1465
|
});
|
|
1545
|
-
}
|
|
1546
|
-
|
|
1466
|
+
}
|
|
1467
|
+
catch(err) {
|
|
1547
1468
|
logRef.push({ action: 'signup', success: false, code: 'unexpected', ip: req.ip, date: new Date(), error: err.message, username: details.username, email: details.email });
|
|
1548
1469
|
sendError(res, err);
|
|
1549
|
-
}
|
|
1470
|
+
}
|
|
1550
1471
|
});
|
|
1551
1472
|
|
|
1552
|
-
app.post(`/auth/${dbname}/update`, (req, res) => {
|
|
1473
|
+
app.post(`/auth/${dbname}/update`, async (req, res) => {
|
|
1553
1474
|
let details = req.body;
|
|
1554
1475
|
|
|
1555
1476
|
if (!req.user) {
|
|
@@ -1569,9 +1490,15 @@ class AceBaseServer extends EventEmitter {
|
|
|
1569
1490
|
if (details.email && !isValid.email(details.email)) {
|
|
1570
1491
|
err = validationErrors.email;
|
|
1571
1492
|
}
|
|
1572
|
-
else if (details.
|
|
1493
|
+
else if (details.email && !await isValid.newEmailAddress(details.email)) {
|
|
1494
|
+
err = validationErrors.emailExists;
|
|
1495
|
+
}
|
|
1496
|
+
else if (details.username && !isValid.username(details.username)) {
|
|
1573
1497
|
err = validationErrors.username;
|
|
1574
1498
|
}
|
|
1499
|
+
else if (details.username && !await isValid.newUsername(details.username)) {
|
|
1500
|
+
err = validationErrors.usernameExists;
|
|
1501
|
+
}
|
|
1575
1502
|
else if (details.display_name && !isValid.displayName(details.display_name)) {
|
|
1576
1503
|
err = validationErrors.displayName;
|
|
1577
1504
|
}
|
|
@@ -1604,9 +1531,9 @@ class AceBaseServer extends EventEmitter {
|
|
|
1604
1531
|
user.email = details.email;
|
|
1605
1532
|
user.email_verified = false; // TODO: send verification email
|
|
1606
1533
|
}
|
|
1607
|
-
if (details.username) {
|
|
1608
|
-
if (details.display_name) {
|
|
1609
|
-
if (details.picture) {
|
|
1534
|
+
if (details.username) { user.username = details.username; }
|
|
1535
|
+
if (details.display_name) { user.display_name = details.display_name; }
|
|
1536
|
+
if (details.picture) { user.picture = details.picture; }
|
|
1610
1537
|
if (details.settings) {
|
|
1611
1538
|
if (typeof user.settings !== 'object') {
|
|
1612
1539
|
user.settings = {};
|
|
@@ -2016,9 +1943,14 @@ class AceBaseServer extends EventEmitter {
|
|
|
2016
1943
|
}
|
|
2017
1944
|
});
|
|
2018
1945
|
|
|
1946
|
+
app.get(`/ping/${dbname}`, (req, res) => {
|
|
1947
|
+
// For simple connectivity check
|
|
1948
|
+
res.end('pong');
|
|
1949
|
+
});
|
|
1950
|
+
|
|
2019
1951
|
app.get(`/info/${dbname}`, (req, res) => {
|
|
2020
1952
|
const info = {
|
|
2021
|
-
version: '1.
|
|
1953
|
+
version: '1.7.0', // TODO: Load from package.json
|
|
2022
1954
|
time: Date.now(),
|
|
2023
1955
|
process: process.pid
|
|
2024
1956
|
};
|
|
@@ -2071,7 +2003,7 @@ class AceBaseServer extends EventEmitter {
|
|
|
2071
2003
|
res.send(info);
|
|
2072
2004
|
});
|
|
2073
2005
|
|
|
2074
|
-
app.get(`/data/${dbname}/*`, (req, res) => {
|
|
2006
|
+
app.get(`/data/${dbname}/*`, async (req, res) => {
|
|
2075
2007
|
// Request data
|
|
2076
2008
|
const path = req.path.substr(dbname.length + 7); //.replace(/^\/+/g, '').replace(/\/+$/g, '');
|
|
2077
2009
|
if (!userHasAccess(req.user, path, false, denyDetails => sendUnauthorizedError(res, denyDetails.code, denyDetails.message))) {
|
|
@@ -2102,25 +2034,25 @@ class AceBaseServer extends EventEmitter {
|
|
|
2102
2034
|
options.exclude = [...options.exclude || [], '__auth__', '__log__'];
|
|
2103
2035
|
}
|
|
2104
2036
|
|
|
2105
|
-
|
|
2106
|
-
|
|
2037
|
+
try {
|
|
2038
|
+
// const snap = await db.ref(path).get(options);
|
|
2039
|
+
// const value = snap.val(), context = snap.context();
|
|
2040
|
+
const { value, context } = await db.api.get(path, options);
|
|
2041
|
+
if (!this.config.transactionLoggingEnabled) {
|
|
2042
|
+
delete context.acebase_cursor;
|
|
2043
|
+
}
|
|
2044
|
+
const serialized = Transport.serialize(value);
|
|
2045
|
+
|
|
2046
|
+
res.setHeader('AceBase-Context', JSON.stringify(context));
|
|
2047
|
+
res.send({
|
|
2048
|
+
exists: value !== null,
|
|
2049
|
+
val: serialized.val,
|
|
2050
|
+
map: serialized.map
|
|
2051
|
+
});
|
|
2052
|
+
}
|
|
2053
|
+
catch (err) {
|
|
2054
|
+
res.status(500).send(err);
|
|
2107
2055
|
}
|
|
2108
|
-
|
|
2109
|
-
db.ref(path)
|
|
2110
|
-
.get(options) //.once("value")
|
|
2111
|
-
.then(snap => {
|
|
2112
|
-
const ser = Transport.serialize(snap.val());
|
|
2113
|
-
const data = {
|
|
2114
|
-
exists: snap.exists(),
|
|
2115
|
-
val: ser.val,
|
|
2116
|
-
map: ser.map
|
|
2117
|
-
}
|
|
2118
|
-
res.send(data);
|
|
2119
|
-
})
|
|
2120
|
-
.catch(err => {
|
|
2121
|
-
res.statusCode = 500;
|
|
2122
|
-
res.send(err);
|
|
2123
|
-
});
|
|
2124
2056
|
});
|
|
2125
2057
|
|
|
2126
2058
|
app.get(`/reflect/${dbname}/*`, (req, res) => {
|
|
@@ -2330,7 +2262,7 @@ class AceBaseServer extends EventEmitter {
|
|
|
2330
2262
|
};
|
|
2331
2263
|
});
|
|
2332
2264
|
|
|
2333
|
-
app.post(`/query/${dbname}/*`, (req, res) => {
|
|
2265
|
+
app.post(`/query/${dbname}/*`, async (req, res) => {
|
|
2334
2266
|
// Execute query
|
|
2335
2267
|
const path = req.path.substr(dbname.length + 8);
|
|
2336
2268
|
if (!userHasAccess(req.user, path, false, denyDetails => sendUnauthorizedError(res, denyDetails.code, denyDetails.message))) {
|
|
@@ -2338,6 +2270,9 @@ class AceBaseServer extends EventEmitter {
|
|
|
2338
2270
|
}
|
|
2339
2271
|
|
|
2340
2272
|
const data = Transport.deserialize(req.body);
|
|
2273
|
+
if (typeof data !== 'object' || typeof data.query !== 'object' || typeof data.options !== 'object') {
|
|
2274
|
+
return sendError(res, { code: 'invalid_request', message: 'Invalid query request' });
|
|
2275
|
+
}
|
|
2341
2276
|
const query = data.query;
|
|
2342
2277
|
const options = data.options;
|
|
2343
2278
|
if (options.monitor === true) {
|
|
@@ -2361,14 +2296,21 @@ class AceBaseServer extends EventEmitter {
|
|
|
2361
2296
|
}
|
|
2362
2297
|
options.eventHandler = sendEvent;
|
|
2363
2298
|
}
|
|
2364
|
-
|
|
2365
|
-
|
|
2299
|
+
try {
|
|
2300
|
+
const { results, context } = await db.api.query(path, query, options);
|
|
2301
|
+
if (!this.config.transactionLoggingEnabled) {
|
|
2302
|
+
delete context.acebase_cursor;
|
|
2303
|
+
}
|
|
2366
2304
|
const response = {
|
|
2367
2305
|
count: results.length,
|
|
2368
2306
|
list: results // []
|
|
2369
2307
|
};
|
|
2308
|
+
res.setHeader('AceBase-Context', JSON.stringify(context));
|
|
2370
2309
|
res.send(Transport.serialize(response));
|
|
2371
|
-
}
|
|
2310
|
+
}
|
|
2311
|
+
catch(err) {
|
|
2312
|
+
sendError(res, { code: 'unknown', message: err.message });
|
|
2313
|
+
}
|
|
2372
2314
|
});
|
|
2373
2315
|
|
|
2374
2316
|
app.get(`/index/${dbname}`, async (req, res) => {
|
|
@@ -2472,9 +2414,59 @@ class AceBaseServer extends EventEmitter {
|
|
|
2472
2414
|
}
|
|
2473
2415
|
try {
|
|
2474
2416
|
const data = req.query;
|
|
2475
|
-
let targets =
|
|
2417
|
+
let targets = typeof data.path === 'string'
|
|
2418
|
+
? [{ path: data.path, events: ['value'] }]
|
|
2419
|
+
: typeof data.for === 'string'
|
|
2420
|
+
? JSON.parse(data.for)
|
|
2421
|
+
: null;
|
|
2422
|
+
if (targets === null) {
|
|
2423
|
+
return sendError(res, { code: 'invalid_request', message: 'Invalid mutations request' });
|
|
2424
|
+
}
|
|
2425
|
+
if (targets.length === 0) {
|
|
2426
|
+
targets.push({ path: '', events: ['value'] });
|
|
2427
|
+
}
|
|
2428
|
+
// Filter out any requested paths user does not have access to.
|
|
2429
|
+
targets = targets.filter(target => {
|
|
2430
|
+
let path = target.path;
|
|
2431
|
+
if (target.events.every(event => /^(?:notify_)?child_/.test(event))) { //if (!target.events.some(event => ['value','notify_value','mutations','mutated'].includes(event))) {
|
|
2432
|
+
// Only child_ events, check if they have access to children instead
|
|
2433
|
+
path = PathInfo.get(path).childPath('*');
|
|
2434
|
+
}
|
|
2435
|
+
return userHasAccess(req.user, path, false);
|
|
2436
|
+
});
|
|
2437
|
+
if (targets.length === 0) {
|
|
2438
|
+
return sendUnauthorizedError(res, 'not_authorized', 'User is not authorized to access this data');
|
|
2439
|
+
}
|
|
2440
|
+
|
|
2441
|
+
const { cursor, timestamp } = data;
|
|
2442
|
+
const result = await db.api.getMutations({ for: targets, cursor, timestamp });
|
|
2443
|
+
|
|
2444
|
+
res.setHeader('AceBase-Context', JSON.stringify({ acebase_cursor: result.new_cursor }));
|
|
2445
|
+
res.contentType('application/json');
|
|
2446
|
+
res.send(result.mutations);
|
|
2447
|
+
}
|
|
2448
|
+
catch(err) {
|
|
2449
|
+
sendError(res, err);
|
|
2450
|
+
}
|
|
2451
|
+
});
|
|
2452
|
+
|
|
2453
|
+
app.get(`/sync/changes/${dbname}`, async (req, res) => {
|
|
2454
|
+
// Gets effective changes for specific path(s) and event combinations since given cursor
|
|
2455
|
+
if (!this.config.transactionLoggingEnabled) {
|
|
2456
|
+
return sendError(res, { code: 'no_transaction_logging', message: 'Transaction logging not enabled' });
|
|
2457
|
+
}
|
|
2458
|
+
try {
|
|
2459
|
+
const data = req.query;
|
|
2460
|
+
let targets = typeof data.path === 'string'
|
|
2461
|
+
? [{ path: data.path, events: ['value'] }]
|
|
2462
|
+
: typeof data.for === 'string'
|
|
2463
|
+
? JSON.parse(data.for)
|
|
2464
|
+
: null;
|
|
2465
|
+
if (targets === null) {
|
|
2466
|
+
return sendError(res, { code: 'invalid_request', message: 'Invalid mutations request' });
|
|
2467
|
+
}
|
|
2476
2468
|
if (targets.length === 0) {
|
|
2477
|
-
targets.push({ path: '', events: '
|
|
2469
|
+
targets.push({ path: '', events: ['value'] });
|
|
2478
2470
|
}
|
|
2479
2471
|
// Filter out any requested paths user does not have access to.
|
|
2480
2472
|
targets = targets.filter(target => {
|
|
@@ -2489,10 +2481,12 @@ class AceBaseServer extends EventEmitter {
|
|
|
2489
2481
|
return sendUnauthorizedError(res, 'not_authorized', 'User is not authorized to access this data');
|
|
2490
2482
|
}
|
|
2491
2483
|
|
|
2492
|
-
const cursor = data
|
|
2493
|
-
const
|
|
2494
|
-
|
|
2495
|
-
res.
|
|
2484
|
+
const { cursor, timestamp } = data;
|
|
2485
|
+
const result = await db.api.getChanges({ for: targets, cursor, timestamp });
|
|
2486
|
+
|
|
2487
|
+
res.setHeader('AceBase-Context', JSON.stringify({ acebase_cursor: result.new_cursor }));
|
|
2488
|
+
res.contentType('application/json');
|
|
2489
|
+
res.send(result.changes);
|
|
2496
2490
|
}
|
|
2497
2491
|
catch(err) {
|
|
2498
2492
|
sendError(res, err);
|
|
@@ -2629,19 +2623,53 @@ class AceBaseServer extends EventEmitter {
|
|
|
2629
2623
|
|
|
2630
2624
|
this.shutdown = async () => {
|
|
2631
2625
|
this.debug.warn('shutting down server');
|
|
2632
|
-
fs.unwatchFile(rulesFilePath);
|
|
2626
|
+
fs.unwatchFile(rulesFilePath, setupRules);
|
|
2633
2627
|
await new Promise((resolve) => {
|
|
2634
2628
|
server.close(resolve);
|
|
2635
|
-
clients.list.forEach(client => {
|
|
2629
|
+
clients.list.slice().forEach(client => {
|
|
2636
2630
|
client.socket.disconnect(true);
|
|
2637
2631
|
});
|
|
2638
2632
|
});
|
|
2639
2633
|
this.debug.warn('closing database');
|
|
2640
2634
|
await db.close(); // TODO: Check what happens if shutdown was triggered by SIGINT. Maybe already closed?
|
|
2641
2635
|
this.debug.warn('shutdown complete');
|
|
2636
|
+
|
|
2637
|
+
// Emit events to let the outside world know we shut down.
|
|
2638
|
+
// This is especially important if this instance was running in a Node.js cluster: the process will
|
|
2639
|
+
// not exit automatically after this shutdown because Node.js' IPC channel between worker and master is still open.
|
|
2640
|
+
// By sending these events, the cluster manager can determine if it should (and when to) execute process.exit()
|
|
2641
|
+
this.emit('shutdown'); // Emit on AceBaseServer instance
|
|
2642
|
+
process.emit('acebase-server-shutdown'); // Emit on process
|
|
2643
|
+
try {
|
|
2644
|
+
process.send && process.send('acebase-server-shutdown'); // Send to master process when running in a Node.js cluster
|
|
2645
|
+
}
|
|
2646
|
+
catch(err) {
|
|
2647
|
+
// IPC Channel has apparently been closed already
|
|
2648
|
+
}
|
|
2642
2649
|
};
|
|
2643
2650
|
process.on('SIGINT', () => this.shutdown());
|
|
2644
2651
|
|
|
2652
|
+
let paused = false;
|
|
2653
|
+
this.pause = async () => {
|
|
2654
|
+
if (paused) { throw new Error('Server is already paused'); }
|
|
2655
|
+
server.close();
|
|
2656
|
+
this.debug.warn(`Paused "${dbname}" database server at ${this.config.url}`);
|
|
2657
|
+
this.emit('pause');
|
|
2658
|
+
paused = true;
|
|
2659
|
+
}
|
|
2660
|
+
|
|
2661
|
+
this.resume = async () => {
|
|
2662
|
+
if (!paused) { throw new Error('Server is not paused'); }
|
|
2663
|
+
return new Promise(resolve => {
|
|
2664
|
+
server.listen(this.config.port, this.config.hostname, () => {
|
|
2665
|
+
this.debug.warn(`Resumed "${dbname}" database server at ${this.config.url}`);
|
|
2666
|
+
this.emit('resume');
|
|
2667
|
+
paused = false;
|
|
2668
|
+
resolve();
|
|
2669
|
+
});
|
|
2670
|
+
})
|
|
2671
|
+
}
|
|
2672
|
+
|
|
2645
2673
|
/** @type {Map<string, Client>} */
|
|
2646
2674
|
// const clientsNew = new Map();
|
|
2647
2675
|
|
|
@@ -2780,6 +2808,7 @@ class AceBaseServer extends EventEmitter {
|
|
|
2780
2808
|
previous: previousValue
|
|
2781
2809
|
});
|
|
2782
2810
|
this.debug.verbose(`Sending data event "${data.event}" for path "/${path}" to client ${socket.id}`.colorize([ColorStyle.bgWhite, ColorStyle.black]));
|
|
2811
|
+
// 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
|
|
2783
2812
|
socket.emit('data-event', {
|
|
2784
2813
|
subscr_path: subscriptionPath,
|
|
2785
2814
|
path,
|
|
@@ -2982,7 +3011,6 @@ if (__filename.replace(/\\/g,"/").endsWith("/acebase-server.js") && process.argv
|
|
|
2982
3011
|
module.exports = {
|
|
2983
3012
|
AceBaseServer,
|
|
2984
3013
|
AceBaseServerSettings,
|
|
2985
|
-
AceBaseClusterSettings,
|
|
2986
3014
|
AceBaseEmailRequest,
|
|
2987
3015
|
AceBaseUserSignupEmailRequest,
|
|
2988
3016
|
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,8 +263,8 @@ 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.
|
|
267
|
-
<!-- <script type="text/javascript" src="
|
|
266
|
+
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/acebase-client@1.7.1/dist/browser.min.js"></script>
|
|
267
|
+
<!-- <script type="text/javascript" src="acebase-client-browser.min.js"></script> -->
|
|
268
268
|
|
|
269
269
|
<!-- AceBase Webmanager js -->
|
|
270
270
|
<script type="text/javascript" src="index.js"></script>
|
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) {
|