acebase-server 1.6.1 → 1.9.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.
@@ -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)
@@ -528,8 +434,25 @@ class AceBaseServer extends EventEmitter {
528
434
  };
529
435
  app.use(corsHandler);
530
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
+
531
449
  const server = options.https.enabled ? require('https').createServer(options.https, app) : require('http').createServer(app);
532
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
+
533
456
  // socket.io 2.x:
534
457
  handlePreflightRequest: corsHandler
535
458
  // socket.io 3+:
@@ -552,17 +475,18 @@ class AceBaseServer extends EventEmitter {
552
475
  }
553
476
  };
554
477
  this.url = this.config.url; // Is this used?
555
- this.debug = new DebugLogger(options.logLevel, `[${dbname}]`.colorize(ColorStyle.green)); //`« ${dbname} »`
478
+ this.debug = new DebugLogger(options.logLevel, `[${dbname}]`.colorize(ColorStyle.green));
556
479
 
557
480
  const dbOptions = {
558
481
  logLevel: options.logLevel,
559
482
  info: 'realtime database server',
560
- storage: {
561
- cluster: options.cluster,
483
+ // NEW: Allow storage setting like AceBaseLocalSettings - could allow using other db backend (typed, but undocumented)
484
+ storage: options.storage || {
562
485
  path: options.path,
563
486
  removeVoidProperties: true,
564
- transactions: options.transactions
565
- }
487
+ },
488
+ transactions: options.transactions,
489
+ ipc: options.ipc
566
490
  };
567
491
 
568
492
  const db = new AceBase(dbname, dbOptions);
@@ -617,7 +541,7 @@ class AceBaseServer extends EventEmitter {
617
541
 
618
542
  const setupRules = () => {
619
543
  // Check if there is a rules file, load it or generate default
620
- 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
621
545
 
622
546
  const defaultAccessRule = (def => {
623
547
  switch (def) {
@@ -713,10 +637,7 @@ class AceBaseServer extends EventEmitter {
713
637
  processRules('', accessRules.rules, []);
714
638
 
715
639
  // Watch file for changes
716
- fs.watchFile(rulesFilePath, (currStats, prevStats) => {
717
- // rules.json file changed, setup rules again
718
- setupRules();
719
- });
640
+ fs.watchFile(rulesFilePath, setupRules);
720
641
  }
721
642
  setupRules();
722
643
 
@@ -1094,6 +1015,7 @@ class AceBaseServer extends EventEmitter {
1094
1015
  catch(err) {
1095
1016
  this.debug.error(`Failed to parse AceBase-Context header: "${context}" in request at ${req.url} from client ${req.ip}`);
1096
1017
  }
1018
+ // console.log(`Received request on path ` + req.path);
1097
1019
  next();
1098
1020
  });
1099
1021
 
@@ -1332,11 +1254,11 @@ class AceBaseServer extends EventEmitter {
1332
1254
  if (typeof details !== 'object' || typeof details.uid !== 'string' || typeof details.password !== 'string' || typeof details.new_password !== 'string') {
1333
1255
  logRef.push({ action: 'change_password', success: false, code: 'invalid_details', ip: req.ip, date: new Date() });
1334
1256
  res.status(400).send('Bad Request'); // Bad Request
1335
- return;
1257
+ return;
1336
1258
  }
1337
- if (details.new_password.length < 8 || ~details.new_password.indexOf(' ') || !/[0-9]/.test(details.new_password) || !/[a-z]/.test(details.new_password) || !/[A-Z]/.test(details.new_password)) {
1259
+ if (details.new_password.length < 8 || details.new_password.includes(' ') || !/[0-9]/.test(details.new_password) || !/[a-z]/.test(details.new_password) || !/[A-Z]/.test(details.new_password)) {
1338
1260
  logRef.push({ action: 'change_password', success: false, code: 'new_password_denied', ip: req.ip, date: new Date(), uid: details.uid });
1339
- err = 'Invalid new password, must be at least 8 characters and contain a combination of numbers and letters (both lower and uppercase)';
1261
+ const err = 'Invalid new password, must be at least 8 characters and contain a combination of numbers and letters (both lower and uppercase)';
1340
1262
  res.status(422).send(err);// Unprocessable Entity
1341
1263
  return;
1342
1264
  }
@@ -1447,6 +1369,11 @@ class AceBaseServer extends EventEmitter {
1447
1369
 
1448
1370
  const details = req.body;
1449
1371
 
1372
+ if (typeof details.displayName === 'undefined' && typeof details.display_name === 'string') {
1373
+ // Allow display_name to be sent also (which is used in update endpoint)
1374
+ details.displayName = details.display_name;
1375
+ }
1376
+
1450
1377
  // Check if sent details are ok
1451
1378
  let err;
1452
1379
  if (!details.username && !details.email) {
@@ -1564,6 +1491,11 @@ class AceBaseServer extends EventEmitter {
1564
1491
  return sendUnauthorizedError(res, 'unauthorized_update', 'You are not authorized to perform this update. This attempt has been logged.');
1565
1492
  }
1566
1493
 
1494
+ if (typeof details.display_name === 'undefined' && typeof details.displayName === 'string') {
1495
+ // Allow displayName to be sent also (which is used in signup endpoint)
1496
+ details.display_name = details.displayName;
1497
+ }
1498
+
1567
1499
  // Check if sent details are ok
1568
1500
  let err;
1569
1501
  if (details.email && !isValid.email(details.email)) {
@@ -1658,13 +1590,13 @@ class AceBaseServer extends EventEmitter {
1658
1590
 
1659
1591
  if (req.user.uid !== 'admin' && (details.uid !== req.user.uid || typeof details.is_disabled === 'boolean')) {
1660
1592
  logRef.push({ action: 'delete', success: false, code: 'unauthorized_delete', auth_uid: req.user.uid, delete_uid: details.uid, ip: req.ip, date: new Date() });
1661
- return sendUnauthorizedError(res, 'unauthorized_update', 'You are not authorized to perform this operation, your attempt has been logged');
1593
+ return sendUnauthorizedError(res, 'unauthorized_delete', 'You are not authorized to perform this operation, your attempt has been logged');
1662
1594
  }
1663
1595
 
1664
1596
  const uid = details.uid || req.user.uid;
1665
1597
  if (uid === 'admin') {
1666
1598
  logRef.push({ action: 'delete', success: false, code: 'unauthorized_delete', auth_uid: req.user.uid, delete_uid: details.uid, ip: req.ip, date: new Date() });
1667
- return sendUnauthorizedError(res, 'unauthorized_update', 'The admin account cannot be deleted, your attempt has been logged');
1599
+ return sendUnauthorizedError(res, 'unauthorized_delete', 'The admin account cannot be deleted, your attempt has been logged');
1668
1600
  }
1669
1601
  return authRef.child(uid)
1670
1602
  .remove()
@@ -1941,7 +1873,11 @@ class AceBaseServer extends EventEmitter {
1941
1873
  expires_in: tokens.expires_in
1942
1874
  },
1943
1875
  access_token: createPublicAccessToken(user.uid, req.ip, user.access_token, _secureObjectsSalt),
1944
- user: getPublicAccountDetails(user)
1876
+
1877
+ // Disabled sending user details because:
1878
+ // 1) they might be too big to send as redirect header,
1879
+ // 2) client should verify the sent access token and get user details from the server
1880
+ // user: getPublicAccountDetails(user)
1945
1881
  };
1946
1882
 
1947
1883
  if (state.flow === 'socket') {
@@ -1988,6 +1924,13 @@ class AceBaseServer extends EventEmitter {
1988
1924
  });
1989
1925
  }
1990
1926
 
1927
+ // If environment is development, add API docs
1928
+ if (process.env.NODE_ENV === 'development') {
1929
+ this.debug.warn('DEVELOPMENT MODE: adding API docs endpoint at /docs');
1930
+ const addAPIDocsRoute = require('./routes/docs').default;
1931
+ addAPIDocsRoute(app);
1932
+ }
1933
+
1991
1934
  const webManagerDir = `/webmanager/`;
1992
1935
  app.get('/', (req, res) => {
1993
1936
  res.redirect(webManagerDir);
@@ -1999,32 +1942,19 @@ class AceBaseServer extends EventEmitter {
1999
1942
  // Send default file
2000
1943
  res.sendFile(__dirname + '/webmanager/index.html');
2001
1944
  }
2002
- // else if (filePath === 'TEST') {
2003
- // // apply test
2004
- // let test;
2005
- // if (req.query.type === 'update') {
2006
- // let val = req.query.val;
2007
- // if (val[0] === '{') { val = JSON.parse(val); }
2008
- // test = db.ref(req.query.path || '').update(val);
2009
- // }
2010
- // else {
2011
- // res.send('');
2012
- // }
2013
- // return test && test.then(() => {
2014
- // res.send('ok');
2015
- // })
2016
- // .catch(err => {
2017
- // res.send('fail: ' + err.message);
2018
- // });
2019
- // }
2020
1945
  else {
2021
1946
  res.sendFile(__dirname + '/webmanager/' + filePath);
2022
1947
  }
2023
1948
  });
2024
1949
 
1950
+ app.get(`/ping/${dbname}`, (req, res) => {
1951
+ // For simple connectivity check
1952
+ res.end('pong');
1953
+ });
1954
+
2025
1955
  app.get(`/info/${dbname}`, (req, res) => {
2026
1956
  const info = {
2027
- version: '1.5.1', // TODO: Load from package.json
1957
+ version: '1.9.0', // TODO: Load from package.json
2028
1958
  time: Date.now(),
2029
1959
  process: process.pid
2030
1960
  };
@@ -2195,43 +2125,77 @@ class AceBaseServer extends EventEmitter {
2195
2125
  });
2196
2126
  });
2197
2127
 
2198
- app.get(`/export/${dbname}/*`, (req, res) => {
2128
+ app.get(`/export/${dbname}/*`, async (req, res) => {
2199
2129
  // Export API
2200
- const path = req.path.substr(dbname.length + 9);
2130
+ const path = req.path.slice(dbname.length + 9);
2201
2131
  if (!userHasAccess(req.user, path, false, denyDetails => sendUnauthorizedError(res, denyDetails.code, denyDetails.message))) {
2202
2132
  return;
2203
2133
  }
2204
2134
  const format = req.query.format || 'json';
2135
+ const type_safe = req.query.type_safe !== '0';
2205
2136
 
2206
- const stream = {
2207
- write(chunk) {
2208
- res.write(chunk);
2209
-
2210
- // OLD: causes streaming issues
2211
- // return new Promise((resolve, reject) => {
2212
- // res.write(chunk, err => {
2213
- // if (err) { reject(err); }
2214
- // else { resolve(); }
2215
- // });
2216
- // });
2137
+ const write = async (chunk) => {
2138
+ let ok = res.write(chunk);
2139
+ if (!ok) {
2140
+ await new Promise(resolve => res.once('drain', resolve));
2217
2141
  }
2218
2142
  };
2219
2143
 
2220
2144
  const ref = db.ref(path);
2221
2145
  res.setHeader('Content-Disposition', `attachment; filename=${ref.key || 'export'}.json`); // Will be treated as a download in browser
2222
2146
 
2223
- ref.export(stream, { format })
2224
- .then(() => {
2147
+ try {
2148
+ await ref.export(write, { format, type_safe });
2149
+ }
2150
+ catch (err) {
2151
+ this.debug.error(`Error exporting data for path "/${path}": `, err);
2152
+ if (!res.headersSent) {
2153
+ res.statusCode = 500;
2154
+ res.send(err);
2155
+ }
2156
+ }
2157
+ finally {
2225
2158
  res.end();
2226
- })
2227
- .catch(err => {
2228
- res.statusCode = 500;
2229
- res.send(err);
2230
- })
2231
- .catch(err => {
2232
- // Previous catch raised another exception. Happens when streaming already started: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
2159
+ }
2160
+ });
2161
+
2162
+ app.post(`/import/${dbname}/*`, async (req, res) => {
2163
+ // Import API
2164
+ const path = req.path.slice(dbname.length + 9);
2165
+ if (!userHasAccess(req.user, path, false, denyDetails => sendUnauthorizedError(res, denyDetails.code, denyDetails.message))) {
2166
+ return;
2167
+ }
2168
+ const format = req.query.format || 'json';
2169
+ const suppress_events = req.query.suppress_events === '1';
2170
+
2171
+ req.pause(); // Switch to non-flowing mode so we can use .read() upon request
2172
+ let eof = false;
2173
+ req.once('end', () => { eof = true; });
2174
+ const read = async (length) => {
2175
+ let chunk = req.read();
2176
+ if (chunk === null && !eof) {
2177
+ await new Promise(resolve => req.once('readable', resolve));
2178
+ chunk = req.read();
2179
+ }
2180
+ console.log(`Received chunk: `, chunk);
2181
+ return chunk;
2182
+ };
2183
+
2184
+ const ref = db.ref(path);
2185
+ try {
2186
+ await ref.import(read, { format, suppress_events });
2187
+ res.send({ success: true });
2188
+ }
2189
+ catch (err) {
2190
+ this.debug.error(`Error importing data for path "/${path}": `, err);
2191
+ if (!res.headersSent) {
2192
+ res.statusCode = 500;
2193
+ res.send({ success: false, reason: err.message });
2194
+ }
2195
+ }
2196
+ finally {
2233
2197
  res.end();
2234
- });
2198
+ }
2235
2199
  });
2236
2200
 
2237
2201
  app.get(`/exists/${dbname}/*`, (req, res) => {
@@ -2289,12 +2253,12 @@ class AceBaseServer extends EventEmitter {
2289
2253
  }
2290
2254
  catch(err) {
2291
2255
  if (err instanceof SchemaValidationError) {
2292
- logRef.push({ action: 'update_data', success: false, code: 'schema_validation_failed', path, error: err.reason });
2256
+ logRef && logRef.push({ action: 'update_data', success: false, code: 'schema_validation_failed', path, error: err.reason });
2293
2257
  res.status(422).send({ code: 'schema_validation_failed', message: err.message });
2294
2258
  }
2295
2259
  else {
2296
2260
  this.debug.error(`failed to update "${path}":`, err);
2297
- logRef.push({ action: 'update_data', success: false, code: `unknown_error`, path, error: err.message });
2261
+ logRef && logRef.push({ action: 'update_data', success: false, code: `unknown_error`, path, error: err.message });
2298
2262
  sendError(res, err);
2299
2263
  }
2300
2264
  }
@@ -2697,7 +2661,7 @@ class AceBaseServer extends EventEmitter {
2697
2661
 
2698
2662
  this.shutdown = async () => {
2699
2663
  this.debug.warn('shutting down server');
2700
- fs.unwatchFile(rulesFilePath);
2664
+ fs.unwatchFile(rulesFilePath, setupRules);
2701
2665
  await new Promise((resolve) => {
2702
2666
  server.close(resolve);
2703
2667
  clients.list.slice().forEach(client => {
@@ -2707,9 +2671,43 @@ class AceBaseServer extends EventEmitter {
2707
2671
  this.debug.warn('closing database');
2708
2672
  await db.close(); // TODO: Check what happens if shutdown was triggered by SIGINT. Maybe already closed?
2709
2673
  this.debug.warn('shutdown complete');
2674
+
2675
+ // Emit events to let the outside world know we shut down.
2676
+ // This is especially important if this instance was running in a Node.js cluster: the process will
2677
+ // not exit automatically after this shutdown because Node.js' IPC channel between worker and master is still open.
2678
+ // By sending these events, the cluster manager can determine if it should (and when to) execute process.exit()
2679
+ this.emit('shutdown'); // Emit on AceBaseServer instance
2680
+ process.emit('acebase-server-shutdown'); // Emit on process
2681
+ try {
2682
+ process.send && process.send('acebase-server-shutdown'); // Send to master process when running in a Node.js cluster
2683
+ }
2684
+ catch(err) {
2685
+ // IPC Channel has apparently been closed already
2686
+ }
2710
2687
  };
2711
2688
  process.on('SIGINT', () => this.shutdown());
2712
2689
 
2690
+ let paused = false;
2691
+ this.pause = async () => {
2692
+ if (paused) { throw new Error('Server is already paused'); }
2693
+ server.close();
2694
+ this.debug.warn(`Paused "${dbname}" database server at ${this.config.url}`);
2695
+ this.emit('pause');
2696
+ paused = true;
2697
+ }
2698
+
2699
+ this.resume = async () => {
2700
+ if (!paused) { throw new Error('Server is not paused'); }
2701
+ return new Promise(resolve => {
2702
+ server.listen(this.config.port, this.config.hostname, () => {
2703
+ this.debug.warn(`Resumed "${dbname}" database server at ${this.config.url}`);
2704
+ this.emit('resume');
2705
+ paused = false;
2706
+ resolve();
2707
+ });
2708
+ })
2709
+ }
2710
+
2713
2711
  /** @type {Map<string, Client>} */
2714
2712
  // const clientsNew = new Map();
2715
2713
 
@@ -2848,6 +2846,7 @@ class AceBaseServer extends EventEmitter {
2848
2846
  previous: previousValue
2849
2847
  });
2850
2848
  this.debug.verbose(`Sending data event "${data.event}" for path "/${path}" to client ${socket.id}`.colorize([ColorStyle.bgWhite, ColorStyle.black]));
2849
+ // 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
2851
2850
  socket.emit('data-event', {
2852
2851
  subscr_path: subscriptionPath,
2853
2852
  path,
@@ -3050,7 +3049,6 @@ if (__filename.replace(/\\/g,"/").endsWith("/acebase-server.js") && process.argv
3050
3049
  module.exports = {
3051
3050
  AceBaseServer,
3052
3051
  AceBaseServerSettings,
3053
- AceBaseClusterSettings,
3054
3052
  AceBaseEmailRequest,
3055
3053
  AceBaseUserSignupEmailRequest,
3056
3054
  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(`Master ${process.pid} is running`);
14
-
15
- options.cluster = {
16
- enabled: true,
17
- isMaster: true,
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
- let worker = cluster.fork();
23
- worker.on("disconnect", (worker, code, signal) => {
29
+ const worker = cluster.fork();
30
+ worker.on('disconnect', () => {
24
31
  console.error(`worker ${worker.process.pid} disconnected`);
25
- // TODO: restart!
26
32
  });
27
- options.cluster.workers.push(worker);
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
- process.on("unhandledRejection", (reason, p) => {
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
- options.cluster = {
48
- enabled: true,
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
  }