acebase-server 1.4.0 → 1.6.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "acebase-server",
3
- "version": "1.4.0",
3
+ "version": "1.6.1",
4
4
  "description": "AceBase realtime database server (webserver endpoint to allow remote connections)",
5
5
  "main": "./src/index.js",
6
6
  "private": false,
@@ -8,7 +8,7 @@
8
8
  "scripts": {
9
9
  "start": "node src/acebase-server.js start default 0.0.0.0 3000",
10
10
  "test": "echo \"Error: no test specified\" && exit 1",
11
- "compile": "tsc --outDir ./src"
11
+ "compile": "tsc"
12
12
  },
13
13
  "keywords": [
14
14
  "database",
@@ -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.8.3",
48
- "acebase-core": "^1.6.2",
47
+ "acebase": "^1.11.1",
48
+ "acebase-core": "^1.10.1",
49
49
  "express": "^4.17.1",
50
50
  "socket.io": "^2.4.1"
51
51
  },
@@ -50,7 +50,7 @@ class AceBaseServerAuthenticationSettings {
50
50
  * @param {number} [settings.tokensExpire=0] how many minutes before access tokens expire. 0 for no expiration. not implemented yet
51
51
  * @param {string} [settings.defaultAccessRule='auth'] when the server runs for the first time, what defaults to use to generate the rules.json file with. Options are: 'auth' (only authenticated access to db, default), 'deny' (deny access to anyone except admin user), 'allow' (allow access to anyone)
52
52
  * @param {boolean} [settings.defaultAdminPassword] when the server runs for the first time, what password to use for the admin user. If not supplied, a generated password will be used and shown ONCE in the console output.
53
- * @param {boolean} [settings.seperateDb=false] whether to use a seperate database for auth and logging
53
+ * @param {boolean} [settings.separateDb=false] whether to use a separate database for auth and logging
54
54
  */
55
55
  constructor(settings) {
56
56
  if (typeof settings !== "object") { settings = {}; }
@@ -60,7 +60,9 @@ class AceBaseServerAuthenticationSettings {
60
60
  this.tokensExpire = typeof settings.tokensExpire === 'number' ? settings.tokensExpire : 0;
61
61
  this.defaultAccessRule = settings.defaultAccessRule || AceBaseServerAuthenticationSettings.ACCESS_DEFAULT.ALLOW_AUTHENTICATED;
62
62
  this.defaultAdminPassword = typeof settings.defaultAdminPassword === 'string' ? settings.defaultAdminPassword : undefined;
63
- this.seperateDb = typeof settings.seperateDb === 'boolean' ? settings.seperateDb : false;
63
+ this.separateDb = typeof settings.separateDb === 'boolean'
64
+ ? settings.separateDb
65
+ : typeof settings.seperateDb === 'boolean' ? seperateDb : false; // Handle previous _wrong_ spelling
64
66
  }
65
67
 
66
68
  static get ACCESS_DEFAULT() {
@@ -191,6 +193,19 @@ class AceBaseServerEmailSettings {
191
193
  }
192
194
  }
193
195
 
196
+ class AceBaseServerTransactionSettings {
197
+ /**
198
+ * TODO: Use AceBaseTransactionLogSettings from 'acebase/src/storage-acebase.js'
199
+ * @param {AceBaseServerTransactionSettings} settings
200
+ */
201
+ constructor(settings) {
202
+ settings = settings || { log: false }; // Keep default false until transaction logging is out of alpha/beta
203
+ this.log = settings.log === true;
204
+ this.maxAge = typeof settings.maxAge === 'number' ? settings.maxAge : 30; // 30 days
205
+ this.noWait = settings.noWait === true;
206
+ }
207
+ }
208
+
194
209
  class AceBaseServerSettings {
195
210
 
196
211
  /**
@@ -205,6 +220,7 @@ class AceBaseServerSettings {
205
220
  * @param {AceBaseServerHttpsSettings} [settings.https] { keyPath:string, certPath:string} | { pfxPath:string, passphrase:string }
206
221
  * @param {AceBaseServerAuthenticationSettings} [settings.auth]
207
222
  * @param {AceBaseServerEmailSettings} [settings.email]
223
+ * @param {AceBaseServerTransactionSettings} [settings.transactions]
208
224
  *
209
225
  */
210
226
  constructor(settings) {
@@ -219,6 +235,7 @@ class AceBaseServerSettings {
219
235
  this.maxPayloadSize = settings.maxPayloadSize || '10mb';
220
236
  this.allowOrigin = settings.allowOrigin || '*';
221
237
  this.email = typeof settings.email === 'object' ? new AceBaseServerEmailSettings(settings.email) : null;
238
+ this.transactions = new AceBaseServerTransactionSettings(settings.transactions);
222
239
  }
223
240
  }
224
241
 
@@ -501,6 +518,7 @@ class AceBaseServer extends EventEmitter {
501
518
  res.header('Access-Control-Allow-Origin', options.allowOrigin !== '*' ? options.allowOrigin : (req.headers.origin || '*'));
502
519
  res.header('Access-Control-Allow-Methods', corsOptions.methods);
503
520
  res.header('Access-Control-Allow-Headers', corsOptions.allowedHeaders);
521
+ 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
504
522
  if (req.method === 'OPTIONS') {
505
523
  res.status(200).end();
506
524
  }
@@ -528,7 +546,10 @@ class AceBaseServer extends EventEmitter {
528
546
  },
529
547
  https: options.https,
530
548
  authentication: options.authentication,
531
- email: options.email
549
+ email: options.email,
550
+ get transactionLoggingEnabled() {
551
+ return options.transactions && options.transactions.log === true;
552
+ }
532
553
  };
533
554
  this.url = this.config.url; // Is this used?
534
555
  this.debug = new DebugLogger(options.logLevel, `[${dbname}]`.colorize(ColorStyle.green)); //`« ${dbname} »`
@@ -539,7 +560,8 @@ class AceBaseServer extends EventEmitter {
539
560
  storage: {
540
561
  cluster: options.cluster,
541
562
  path: options.path,
542
- removeVoidProperties: true
563
+ removeVoidProperties: true,
564
+ transactions: options.transactions
543
565
  }
544
566
  };
545
567
 
@@ -562,12 +584,16 @@ class AceBaseServer extends EventEmitter {
562
584
  const otherDbsPath = `${options.path}/${dbname}.acebase`;
563
585
  // const eventsDb = new AceBase('events', { logLevel: dbOptions.logLevel, storage: { path: otherDbsPath, removeVoidProperties: true } })
564
586
  // const logsDb = new AceBase('logs', { logLevel: dbOptions.logLevel, storage: { path: otherDbsPath, removeVoidProperties: true } })
565
- const authDb = options.authentication.enabled
566
- ?
567
- options.authentication.seperateDb === true // NEW
568
- ? new AceBase('auth', { logLevel: dbOptions.logLevel, storage: { path: otherDbsPath, removeVoidProperties: true, info: `${dbname} auth database` } })
569
- : db
570
- : null;
587
+ const authDb = (() => {
588
+ if (!options.authentication.enabled) {
589
+ return null;
590
+ }
591
+ switch(options.authentication.separateDb) {
592
+ case true: return new AceBase('auth', { logLevel: dbOptions.logLevel, storage: { path: otherDbsPath, removeVoidProperties: true, info: `${dbname} auth database` } });
593
+ case 'v2': /*NOT TESTED YET*/return new AceBase(dbname, { logLevel: dbOptions.logLevel, storage: { type: 'auth', path: options.path, removeVoidProperties: true, info: `${dbname} auth database` } });
594
+ default: return db;
595
+ }
596
+ })();
571
597
 
572
598
  // To handle unhandled promise rejections so process will not die in future versions of node:
573
599
  // process.on("unhandledRejection", (reason, p) => {
@@ -587,11 +613,10 @@ class AceBaseServer extends EventEmitter {
587
613
  const securityRef = authDb ? authDb === db ? db.ref('__auth__/security') : authDb.ref('security') : null;
588
614
  const authRef = authDb ? authDb === db ? db.ref('__auth__/accounts') : authDb.ref('accounts') : null;
589
615
  const logRef = authDb ? authDb === db ? db.ref('__log__') : authDb.ref('log') : null;
616
+ const rulesFilePath = `${options.path}/${dbname}.acebase/rules.json`;
590
617
 
591
618
  const setupRules = () => {
592
619
  // Check if there is a rules file, load it or generate default
593
- const rulesFilePath = `${options.path}/${dbname}.acebase/rules.json`;
594
-
595
620
  fs.unwatchFile(rulesFilePath); // If function was triggered because of file change, stop listening now. Listener will be setup again at end of function
596
621
 
597
622
  const defaultAccessRule = (def => {
@@ -691,7 +716,7 @@ class AceBaseServer extends EventEmitter {
691
716
  fs.watchFile(rulesFilePath, (currStats, prevStats) => {
692
717
  // rules.json file changed, setup rules again
693
718
  setupRules();
694
- })
719
+ });
695
720
  }
696
721
  setupRules();
697
722
 
@@ -879,7 +904,7 @@ class AceBaseServer extends EventEmitter {
879
904
  return false;
880
905
  }
881
906
 
882
- const env = { now: Date.now(), auth: user || null };
907
+ const env = { now: Date.now(), auth: user || null }; // IDEA: Add functions like "exists" and "value". These will be async (so that requires refactoring) and can be used like "await exists('./shared/' + auth.uid)" and "await value('./writable') === true"
883
908
  const pathKeys = PathInfo.getPathKeys(path);
884
909
  let rule = accessRules.rules;
885
910
  let rulePath = [];
@@ -1367,9 +1392,17 @@ class AceBaseServer extends EventEmitter {
1367
1392
  email(email) {
1368
1393
  return /[a-z0-9_.+]+@([a-z0-9\-]+\.)+[a-z]{2,}/i.test(email);
1369
1394
  },
1395
+ async newEmailAddress(email) {
1396
+ const exists = await authRef.query().filter('email', '==', email).exists();
1397
+ return !exists;
1398
+ },
1370
1399
  username(username) {
1371
1400
  return username !== 'admin' && typeof username === 'string' && username.length >= 5 && /^[a-z0-9]+$/.test(username);
1372
1401
  },
1402
+ async newUsername(username) {
1403
+ const exists = await authRef.query().filter('username', '==', username).exists();
1404
+ return !exists;
1405
+ },
1373
1406
  displayName(displayName) {
1374
1407
  return typeof displayName === 'string' && displayName.length >= 5;
1375
1408
  },
@@ -1377,6 +1410,9 @@ class AceBaseServer extends EventEmitter {
1377
1410
  // return typeof password === 'string' && password.length >= 8 && password.indexOf(' ') < 0 && /[0-9]/.test(password) && /[a-z]/.test(password) && /[A-Z]/.test(password);
1378
1411
  return typeof password === 'string' && password.length >= 8 && password.indexOf(' ') < 0; // Let client application set their own password rules. Keep minimum length of 8 and no spaces requirement.
1379
1412
  },
1413
+ picture(picture) {
1414
+ return picture === null || (typeof picture === 'object' && typeof picture.url === 'string' && typeof picture.width === 'number' && typeof picture.height === 'number');
1415
+ },
1380
1416
  settings(settings) {
1381
1417
  return typeof settings === 'undefined'
1382
1418
  || (
@@ -1390,12 +1426,16 @@ class AceBaseServer extends EventEmitter {
1390
1426
  const validationErrors = {
1391
1427
  email: { code: 'invalid_email', message: 'Invalid email address' },
1392
1428
  username: { code: 'invalid_username', message: 'Invalid username, must be at least 5 characters and can only contain lowercase characters a-z and 0-9' },
1429
+ emailExists: { code: 'email_conflict', message: 'Account with email address exists already' },
1430
+ usernameExists: { code: 'username_conflict', message: 'Account with username exists already' },
1431
+ emailOrUsernameExists: { code: 'conflict', message: `Account with username and/or email already exists` },
1393
1432
  displayName: { code: 'invalid_display_name', message: 'Invalid display_name, must be at least 5 characters' },
1394
1433
  password: { code: 'invalid_password', message: 'Invalid password, must be at least 8 characters and cannot contain spaces' },
1434
+ picture: { code: 'invalid_picture', message: 'Invalid picture, must be an object with url, width and height properties'},
1395
1435
  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' }
1396
1436
  };
1397
1437
 
1398
- app.post(`/auth/${dbname}/signup`, (req, res) => {
1438
+ app.post(`/auth/${dbname}/signup`, async (req, res) => {
1399
1439
  if (!this.config.authentication.allowUserSignup && (!req.user || req.user.username !== 'admin')) {
1400
1440
  logRef.push({ action: 'signup', success: false, code: 'user_signup_disabled', ip: req.ip, date: new Date() });
1401
1441
  res.statusCode = 403; // Forbidden
@@ -1415,9 +1455,15 @@ class AceBaseServer extends EventEmitter {
1415
1455
  else if (details.email && !isValid.email(details.email)) {
1416
1456
  err = validationErrors.email;
1417
1457
  }
1418
- else if (details.username && !isValid.username) {
1458
+ else if (details.email && !await isValid.newEmailAddress(details.email)) {
1459
+ err = validationErrors.emailExists;
1460
+ }
1461
+ else if (details.username && !isValid.username(details.username)) {
1419
1462
  err = validationErrors.username;
1420
1463
  }
1464
+ else if (details.username && !await isValid.newUsername(details.username)) {
1465
+ err = validationErrors.usernameExists;
1466
+ }
1421
1467
  else if (!isValid.displayName(details.displayName)) {
1422
1468
  err = validationErrors.displayName;
1423
1469
  }
@@ -1427,36 +1473,20 @@ class AceBaseServer extends EventEmitter {
1427
1473
  else if (!isValid.settings(details.settings)) {
1428
1474
  err = validationErrors.settings;
1429
1475
  }
1430
- if (err) {
1476
+
1477
+ if (err && [validationErrors.emailExists, validationErrors.usernameExists].includes(err)) {
1478
+ logRef.push({ action: 'signup', success: false, code: 'conflict', ip: req.ip, date: new Date(), username: details.username, email: details.email });
1479
+ res.statusCode = 409; // conflict
1480
+ return res.send(validationErrors.emailOrUsernameExists);
1481
+ }
1482
+ else if (err) {
1431
1483
  // Log failure
1432
1484
  logRef.push({ action: 'signup', success: false, code: err.code, ip: req.ip, date: new Date() });
1433
-
1434
1485
  res.statusCode = 422; // Unprocessable Entity
1435
- res.send(err);
1436
- return;
1437
- }
1438
-
1439
- // Check if user(s) with username and/or email don't already exist
1440
- const promises = [];
1441
- if (details.username) {
1442
- let promise = authRef.query().filter('username', '==', details.username).get();
1443
- promises.push(promise);
1486
+ return res.send(err);
1444
1487
  }
1445
- if (details.email) {
1446
- let promise = authRef.query().filter('email', '==', details.email).get();
1447
- promises.push(promise);
1448
- }
1449
- return Promise.all(promises).then(arr => {
1450
- return arr.reduce((n, snaps) => n + snaps.length, 0);
1451
- })
1452
- .then(userCount => {
1453
- if (userCount > 0) {
1454
- logRef.push({ action: 'signup', success: false, code: 'conflict', ip: req.ip, date: new Date(), username: details.username, email: details.email });
1455
- res.statusCode = 409; // conflict
1456
- res.send({ code: 'conflict', message: `Account with username and/or email already exists` });
1457
- return;
1458
- }
1459
1488
 
1489
+ try {
1460
1490
  // Ok, create user
1461
1491
  let pwd = createPasswordHash(details.password);
1462
1492
  /** @type {DbUserAccountDetails} */
@@ -1476,52 +1506,50 @@ class AceBaseServer extends EventEmitter {
1476
1506
  settings: details.settings || {}
1477
1507
  };
1478
1508
 
1479
- return authRef.push(user)
1480
- .then(ref => {
1481
- const uid = ref.key;
1482
- user.uid = uid;
1509
+ const userRef = await authRef.push(user);
1510
+ const uid = userRef.key;
1511
+ user.uid = uid;
1483
1512
 
1484
- // Log success
1485
- logRef.push({ action: 'signup', success: true, ip: req.ip, date: new Date(), uid });
1513
+ // Log success
1514
+ logRef.push({ action: 'signup', success: true, ip: req.ip, date: new Date(), uid });
1486
1515
 
1487
- // Cache the user
1488
- _authCache.set(user.uid, user);
1516
+ // Cache the user
1517
+ _authCache.set(user.uid, user);
1489
1518
 
1490
- // Request welcome e-mail to be sent
1491
- const request = new AceBaseUserSignupEmailRequest({
1492
- user: {
1493
- uid: user.uid,
1494
- username: user.username,
1495
- email: user.email,
1496
- displayName: user.display_name,
1497
- settings: user.settings
1498
- },
1499
- date: user.created,
1500
- ip: user.created_ip,
1501
- provider: 'acebase',
1502
- activationCode: createSignedPublicToken({ uid: user.uid }, _secureObjectsSalt), // , email: user.email, code: user.email_verification_code
1503
- emailVerified: false
1504
- });
1519
+ // Request welcome e-mail to be sent
1520
+ const request = new AceBaseUserSignupEmailRequest({
1521
+ user: {
1522
+ uid: user.uid,
1523
+ username: user.username,
1524
+ email: user.email,
1525
+ displayName: user.display_name,
1526
+ settings: user.settings
1527
+ },
1528
+ date: user.created,
1529
+ ip: user.created_ip,
1530
+ provider: 'acebase',
1531
+ activationCode: createSignedPublicToken({ uid: user.uid }, _secureObjectsSalt), // , email: user.email, code: user.email_verification_code
1532
+ emailVerified: false
1533
+ });
1505
1534
 
1506
- this.config.email && this.config.email.send(request).catch(err => {
1507
- logRef.push({ action: 'signup_email', success: false, code: 'unexpected', ip: req.ip, date: new Date(), error: err.message, request });
1508
- });
1535
+ this.config.email && this.config.email.send(request).catch(err => {
1536
+ logRef.push({ action: 'signup_email', success: false, code: 'unexpected', ip: req.ip, date: new Date(), error: err.message, request });
1537
+ });
1509
1538
 
1510
- // Return the positive news
1511
- const isAdmin = req.user && req.user.uid === 'admin';
1512
- res.send({
1513
- access_token: isAdmin ? '' : createPublicAccessToken(user.uid, req.ip, user.access_token, _secureObjectsSalt),
1514
- user: getPublicAccountDetails(user)
1515
- });
1539
+ // Return the positive news
1540
+ const isAdmin = req.user && req.user.uid === 'admin';
1541
+ res.send({
1542
+ access_token: isAdmin ? '' : createPublicAccessToken(user.uid, req.ip, user.access_token, _secureObjectsSalt),
1543
+ user: getPublicAccountDetails(user)
1516
1544
  });
1517
- })
1518
- .catch(err => {
1545
+ }
1546
+ catch(err) {
1519
1547
  logRef.push({ action: 'signup', success: false, code: 'unexpected', ip: req.ip, date: new Date(), error: err.message, username: details.username, email: details.email });
1520
1548
  sendError(res, err);
1521
- });
1549
+ }
1522
1550
  });
1523
1551
 
1524
- app.post(`/auth/${dbname}/update`, (req, res) => {
1552
+ app.post(`/auth/${dbname}/update`, async (req, res) => {
1525
1553
  let details = req.body;
1526
1554
 
1527
1555
  if (!req.user) {
@@ -1541,12 +1569,21 @@ class AceBaseServer extends EventEmitter {
1541
1569
  if (details.email && !isValid.email(details.email)) {
1542
1570
  err = validationErrors.email;
1543
1571
  }
1544
- else if (details.username && !isValid.username) {
1572
+ else if (details.email && !await isValid.newEmailAddress(details.email)) {
1573
+ err = validationErrors.emailExists;
1574
+ }
1575
+ else if (details.username && !isValid.username(details.username)) {
1545
1576
  err = validationErrors.username;
1546
1577
  }
1578
+ else if (details.username && !await isValid.newUsername(details.username)) {
1579
+ err = validationErrors.usernameExists;
1580
+ }
1547
1581
  else if (details.display_name && !isValid.displayName(details.display_name)) {
1548
1582
  err = validationErrors.displayName;
1549
1583
  }
1584
+ else if (details.picture && !isValid.picture(details.picture)) {
1585
+ err = validationErrors.picture;
1586
+ }
1550
1587
  else if (!isValid.settings(details.settings)) {
1551
1588
  err = validationErrors.settings;
1552
1589
  }
@@ -1569,12 +1606,13 @@ class AceBaseServer extends EventEmitter {
1569
1606
  return;
1570
1607
  }
1571
1608
  user = snap.val();
1572
- if (details.email !== user.email) {
1609
+ if (details.email && details.email !== user.email) {
1573
1610
  user.email = details.email;
1574
1611
  user.email_verified = false; // TODO: send verification email
1575
1612
  }
1576
- if (details.username) { updates.username = details.username; }
1577
- if (details.display_name) { updates.display_name = details.display_name; }
1613
+ if (details.username) { user.username = details.username; }
1614
+ if (details.display_name) { user.display_name = details.display_name; }
1615
+ if (details.picture) { user.picture = details.picture; }
1578
1616
  if (details.settings) {
1579
1617
  if (typeof user.settings !== 'object') {
1580
1618
  user.settings = {};
@@ -1645,6 +1683,10 @@ class AceBaseServer extends EventEmitter {
1645
1683
  try {
1646
1684
  const providerName = req.query.provider;
1647
1685
  const callbackUrl = req.query.callbackUrl;
1686
+ const options = Object.keys(req.query).filter(key => key.startsWith('option_')).reduce((options, key) => {
1687
+ const name = key.slice(7);
1688
+ options[name] = req.query[key];
1689
+ }, {});
1648
1690
  const signedInUid = req.user && req.user.uid;
1649
1691
  const provider = this.authProviders[providerName];
1650
1692
  if (!provider) {
@@ -1653,7 +1695,7 @@ class AceBaseServer extends EventEmitter {
1653
1695
  // Create secure state so it cannot be tampered with. hash it with a server-only known salt: the generated admin password salt
1654
1696
  await this.ready();
1655
1697
  const state = createSignedPublicToken({ flow: 'redirect', provider: providerName, uid: signedInUid, callbackUrl }, _secureObjectsSalt);
1656
- const clientAuthUrl = await provider.init({ redirect_url: `${req.protocol}://${req.headers.host}/oauth2/${dbname}/signin`, state });
1698
+ const clientAuthUrl = await provider.init({ redirect_url: `${req.protocol}://${req.headers.host}/oauth2/${dbname}/signin`, state, options });
1657
1699
  res.send({ redirectUrl: clientAuthUrl });
1658
1700
  }
1659
1701
  catch(err) {
@@ -1982,7 +2024,7 @@ class AceBaseServer extends EventEmitter {
1982
2024
 
1983
2025
  app.get(`/info/${dbname}`, (req, res) => {
1984
2026
  const info = {
1985
- version: '0.9',
2027
+ version: '1.5.1', // TODO: Load from package.json
1986
2028
  time: Date.now(),
1987
2029
  process: process.pid
1988
2030
  };
@@ -2035,7 +2077,7 @@ class AceBaseServer extends EventEmitter {
2035
2077
  res.send(info);
2036
2078
  });
2037
2079
 
2038
- app.get(`/data/${dbname}/*`, (req, res) => {
2080
+ app.get(`/data/${dbname}/*`, async (req, res) => {
2039
2081
  // Request data
2040
2082
  const path = req.path.substr(dbname.length + 7); //.replace(/^\/+/g, '').replace(/\/+$/g, '');
2041
2083
  if (!userHasAccess(req.user, path, false, denyDetails => sendUnauthorizedError(res, denyDetails.code, denyDetails.message))) {
@@ -2066,21 +2108,25 @@ class AceBaseServer extends EventEmitter {
2066
2108
  options.exclude = [...options.exclude || [], '__auth__', '__log__'];
2067
2109
  }
2068
2110
 
2069
- db.ref(path)
2070
- .get(options) //.once("value")
2071
- .then(snap => {
2072
- const ser = Transport.serialize(snap.val());
2073
- const data = {
2074
- exists: snap.exists(),
2075
- val: ser.val,
2076
- map: ser.map
2077
- }
2078
- res.send(data);
2079
- })
2080
- .catch(err => {
2081
- res.statusCode = 500;
2082
- res.send(err);
2083
- });
2111
+ try {
2112
+ // const snap = await db.ref(path).get(options);
2113
+ // const value = snap.val(), context = snap.context();
2114
+ const { value, context } = await db.api.get(path, options);
2115
+ if (!this.config.transactionLoggingEnabled) {
2116
+ delete context.acebase_cursor;
2117
+ }
2118
+ const serialized = Transport.serialize(value);
2119
+
2120
+ res.setHeader('AceBase-Context', JSON.stringify(context));
2121
+ res.send({
2122
+ exists: value !== null,
2123
+ val: serialized.val,
2124
+ map: serialized.map
2125
+ });
2126
+ }
2127
+ catch (err) {
2128
+ res.status(500).send(err);
2129
+ }
2084
2130
  });
2085
2131
 
2086
2132
  app.get(`/reflect/${dbname}/*`, (req, res) => {
@@ -2279,18 +2325,18 @@ class AceBaseServer extends EventEmitter {
2279
2325
  }
2280
2326
  catch(err) {
2281
2327
  if (err instanceof SchemaValidationError) {
2282
- logRef.push({ action: 'set_data', success: false, code: 'schema_validation_failed', path, error: err.reason });
2328
+ logRef && logRef.push({ action: 'set_data', success: false, code: 'schema_validation_failed', path, error: err.reason });
2283
2329
  res.status(422).send({ code: 'schema_validation_failed', message: err.message });
2284
2330
  }
2285
2331
  else {
2286
2332
  this.debug.error(`failed to set "${path}":`, err);
2287
- logRef.push({ action: 'set_data', success: false, code: 'unknown_error', path, error: err.message });
2333
+ logRef && logRef.push({ action: 'set_data', success: false, code: 'unknown_error', path, error: err.message });
2288
2334
  sendError(res, err);
2289
2335
  }
2290
2336
  };
2291
2337
  });
2292
2338
 
2293
- app.post(`/query/${dbname}/*`, (req, res) => {
2339
+ app.post(`/query/${dbname}/*`, async (req, res) => {
2294
2340
  // Execute query
2295
2341
  const path = req.path.substr(dbname.length + 8);
2296
2342
  if (!userHasAccess(req.user, path, false, denyDetails => sendUnauthorizedError(res, denyDetails.code, denyDetails.message))) {
@@ -2298,6 +2344,9 @@ class AceBaseServer extends EventEmitter {
2298
2344
  }
2299
2345
 
2300
2346
  const data = Transport.deserialize(req.body);
2347
+ if (typeof data !== 'object' || typeof data.query !== 'object' || typeof data.options !== 'object') {
2348
+ return sendError(res, { code: 'invalid_request', message: 'Invalid query request' });
2349
+ }
2301
2350
  const query = data.query;
2302
2351
  const options = data.options;
2303
2352
  if (options.monitor === true) {
@@ -2321,14 +2370,21 @@ class AceBaseServer extends EventEmitter {
2321
2370
  }
2322
2371
  options.eventHandler = sendEvent;
2323
2372
  }
2324
- return db.api.query(path, query, options) //query.get(options)
2325
- .then(results => {
2373
+ try {
2374
+ const { results, context } = await db.api.query(path, query, options);
2375
+ if (!this.config.transactionLoggingEnabled) {
2376
+ delete context.acebase_cursor;
2377
+ }
2326
2378
  const response = {
2327
2379
  count: results.length,
2328
2380
  list: results // []
2329
2381
  };
2382
+ res.setHeader('AceBase-Context', JSON.stringify(context));
2330
2383
  res.send(Transport.serialize(response));
2331
- });
2384
+ }
2385
+ catch(err) {
2386
+ sendError(res, { code: 'unknown', message: err.message });
2387
+ }
2332
2388
  });
2333
2389
 
2334
2390
  app.get(`/index/${dbname}`, async (req, res) => {
@@ -2425,6 +2481,91 @@ class AceBaseServer extends EventEmitter {
2425
2481
  }
2426
2482
  });
2427
2483
 
2484
+ app.get(`/sync/mutations/${dbname}`, async (req, res) => {
2485
+ // Gets mutations for specific path(s) and event combinations since given cursor
2486
+ if (!this.config.transactionLoggingEnabled) {
2487
+ return sendError(res, { code: 'no_transaction_logging', message: 'Transaction logging not enabled' });
2488
+ }
2489
+ try {
2490
+ const data = req.query;
2491
+ let targets = typeof data.path === 'string'
2492
+ ? [{ path: data.path, events: ['value'] }]
2493
+ : typeof data.for === 'string'
2494
+ ? JSON.parse(data.for)
2495
+ : null;
2496
+ if (targets === null) {
2497
+ return sendError(res, { code: 'invalid_request', message: 'Invalid mutations request' });
2498
+ }
2499
+ if (targets.length === 0) {
2500
+ targets.push({ path: '', events: ['value'] });
2501
+ }
2502
+ // Filter out any requested paths user does not have access to.
2503
+ targets = targets.filter(target => {
2504
+ let path = target.path;
2505
+ if (target.events.every(event => /^(?:notify_)?child_/.test(event))) { //if (!target.events.some(event => ['value','notify_value','mutations','mutated'].includes(event))) {
2506
+ // Only child_ events, check if they have access to children instead
2507
+ path = PathInfo.get(path).childPath('*');
2508
+ }
2509
+ return userHasAccess(req.user, path, false);
2510
+ });
2511
+ if (targets.length === 0) {
2512
+ return sendUnauthorizedError(res, 'not_authorized', 'User is not authorized to access this data');
2513
+ }
2514
+
2515
+ const { cursor, timestamp } = data;
2516
+ const result = await db.api.getMutations({ for: targets, cursor, timestamp });
2517
+
2518
+ res.setHeader('AceBase-Context', JSON.stringify({ acebase_cursor: result.new_cursor }));
2519
+ res.contentType('application/json');
2520
+ res.send(result.mutations);
2521
+ }
2522
+ catch(err) {
2523
+ sendError(res, err);
2524
+ }
2525
+ });
2526
+
2527
+ app.get(`/sync/changes/${dbname}`, async (req, res) => {
2528
+ // Gets effective changes for specific path(s) and event combinations since given cursor
2529
+ if (!this.config.transactionLoggingEnabled) {
2530
+ return sendError(res, { code: 'no_transaction_logging', message: 'Transaction logging not enabled' });
2531
+ }
2532
+ try {
2533
+ const data = req.query;
2534
+ let targets = typeof data.path === 'string'
2535
+ ? [{ path: data.path, events: ['value'] }]
2536
+ : typeof data.for === 'string'
2537
+ ? JSON.parse(data.for)
2538
+ : null;
2539
+ if (targets === null) {
2540
+ return sendError(res, { code: 'invalid_request', message: 'Invalid mutations request' });
2541
+ }
2542
+ if (targets.length === 0) {
2543
+ targets.push({ path: '', events: ['value'] });
2544
+ }
2545
+ // Filter out any requested paths user does not have access to.
2546
+ targets = targets.filter(target => {
2547
+ let path = target.path;
2548
+ if (target.events.every(event => /^(?:notify_)?child_/.test(event))) { //if (!target.events.some(event => ['value','notify_value','mutations','mutated'].includes(event))) {
2549
+ // Only child_ events, check if they have access to children instead
2550
+ path = PathInfo.get(path).childPath('*');
2551
+ }
2552
+ return userHasAccess(req.user, path, false);
2553
+ });
2554
+ if (targets.length === 0) {
2555
+ return sendUnauthorizedError(res, 'not_authorized', 'User is not authorized to access this data');
2556
+ }
2557
+
2558
+ const { cursor, timestamp } = data;
2559
+ const result = await db.api.getChanges({ for: targets, cursor, timestamp });
2560
+
2561
+ res.setHeader('AceBase-Context', JSON.stringify({ acebase_cursor: result.new_cursor }));
2562
+ res.contentType('application/json');
2563
+ res.send(result.changes);
2564
+ }
2565
+ catch(err) {
2566
+ sendError(res, err);
2567
+ }
2568
+ });
2428
2569
 
2429
2570
  const _transactions = new Map();
2430
2571
  app.post(`/transaction/${dbname}/start`, (req, res) => {
@@ -2554,6 +2695,21 @@ class AceBaseServer extends EventEmitter {
2554
2695
  }
2555
2696
  };
2556
2697
 
2698
+ this.shutdown = async () => {
2699
+ this.debug.warn('shutting down server');
2700
+ fs.unwatchFile(rulesFilePath);
2701
+ await new Promise((resolve) => {
2702
+ server.close(resolve);
2703
+ clients.list.slice().forEach(client => {
2704
+ client.socket.disconnect(true);
2705
+ });
2706
+ });
2707
+ this.debug.warn('closing database');
2708
+ await db.close(); // TODO: Check what happens if shutdown was triggered by SIGINT. Maybe already closed?
2709
+ this.debug.warn('shutdown complete');
2710
+ };
2711
+ process.on('SIGINT', () => this.shutdown());
2712
+
2557
2713
  /** @type {Map<string, Client>} */
2558
2714
  // const clientsNew = new Map();
2559
2715
 
@@ -2622,11 +2778,13 @@ class AceBaseServer extends EventEmitter {
2622
2778
  client.user = null;
2623
2779
  });
2624
2780
 
2625
- socket.on("oauth2-signin", async providerName => {
2781
+ socket.on("oauth2-signin", async request => {
2782
+ // acebase-client does not use socket oauth flow yet
2626
2783
  try {
2784
+ const providerName = typeof request === 'string' ? request : request.provider;
2627
2785
  const provider = this.authProviders[providerName];
2628
2786
  const state = Buffer.from(JSON.stringify({ flow: 'socket', provider: providerName, client_id: client.id })).toString('base64');
2629
- const clientAuthUrl = await provider.init({ redirectUrl: `${req.protocol}://${req.headers.host}/ouath2/${dbname}/signin`, state });
2787
+ const clientAuthUrl = await provider.init({ redirectUrl: `${req.protocol}://${req.headers.host}/ouath2/${dbname}/signin`, state, options: request.options });
2630
2788
  socket.emit('oauth2-signin', { action: 'auth', url: clientAuthUrl });
2631
2789
  }
2632
2790
  catch(err) {
@@ -2690,7 +2848,7 @@ class AceBaseServer extends EventEmitter {
2690
2848
  previous: previousValue
2691
2849
  });
2692
2850
  this.debug.verbose(`Sending data event "${data.event}" for path "/${path}" to client ${socket.id}`.colorize([ColorStyle.bgWhite, ColorStyle.black]));
2693
- socket.emit("data-event", {
2851
+ socket.emit('data-event', {
2694
2852
  subscr_path: subscriptionPath,
2695
2853
  path,
2696
2854
  event: data.event,
@@ -2864,6 +3022,16 @@ class AceBaseServer extends EventEmitter {
2864
3022
  throw new Error(`Failed to configure provider ${providerName}: ${err.message}`)
2865
3023
  }
2866
3024
  }
3025
+
3026
+ /**
3027
+ * Shuts down the server. Stops listening for incoming connections, breaks current connections and closes the database.
3028
+ */
3029
+ async shutdown() {
3030
+ // Overridden by constructor once the server is started.
3031
+ // If this code executes, the server's not running yet.
3032
+ await this.ready();
3033
+ await this.shutdown(); // Now has new implementation
3034
+ }
2867
3035
  }
2868
3036
 
2869
3037
  if (__filename.replace(/\\/g,"/").endsWith("/acebase-server.js") && process.argv[2] === "start") {
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
- ready(callback: () => void): Promise<void>
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
  /**
@@ -64,6 +71,11 @@ declare namespace acebaseserver {
64
71
  * @param {(req: Express.Request, res: Express.Response)} handler
65
72
  */
66
73
  extend(method:'get'|'put'|'post'|'delete', ext_path: string, handler: (req: Express.Request, res: Express.Response) => any): void
74
+
75
+ /**
76
+ * Shuts down the server. Stops listening for incoming connections, breaks current connections and closes the database.
77
+ */
78
+ shutdown(): Promise<void>
67
79
  }
68
80
 
69
81
  interface IOAuthProviderSettings {
@@ -95,6 +107,8 @@ declare namespace acebaseserver {
95
107
  allowOrigin?: string
96
108
  /** Email settings that enable AceBaseServer to send e-mails, eg for welcoming new users, to reset passwords, notify of new sign ins etc */
97
109
  email?: AceBaseServerEmailSettings
110
+ /** Transaction logging settings. Warning: BETA stage, do NOT use in production yet */
111
+ transactions?: AceBaseServerTransactionLogSettings
98
112
  }
99
113
 
100
114
  interface AceBaseServerHttpsSettings {
@@ -111,6 +125,8 @@ declare namespace acebaseserver {
111
125
  /** 'allow', 'deny' or 'auth' */
112
126
  defaultAccessRule?: string
113
127
  defaultAdminPassword?: string
128
+ separateDb?: boolean
129
+ /** @deprecated Misspelled, use separateDb instead */
114
130
  seperateDb?: boolean
115
131
  }
116
132
 
@@ -133,6 +149,12 @@ declare namespace acebaseserver {
133
149
  secure?: boolean
134
150
  }
135
151
 
152
+ interface AceBaseServerTransactionLogSettings {
153
+ log?: boolean;
154
+ maxAge?: number;
155
+ noWait?: boolean;
156
+ }
157
+
136
158
  interface AceBaseEmailRequest {
137
159
  readonly type: string
138
160
  readonly ip: string
@@ -16,10 +16,17 @@ class DropboxAuthProvider {
16
16
  * Starts auth flow by getting the url the user should be redirected to
17
17
  * @param info.redirectUrl Url dropbox will redirect to after authorizing
18
18
  * @param info.state Optional state that will be passed to redirectUri by dropbox
19
+ * @param info.options Optional Dropbox specific authentication settings
20
+ * @param info.options.require_role If this parameter is specified, the user will be asked to authorize with a particular type of Dropbox account, either work for a team account or personal for a personal account. Your app should still verify the type of Dropbox account after authorization since the user could modify or remove the require_role parameter.
21
+ * @param info.options.force_reapprove Whether or not to force the user to approve the app again if they've already done so. If false (default), a user who has already approved the application may be automatically redirected to the URI specified by redirect_uri. If true, the user will not be automatically redirected and will have to approve the app again.
22
+ * @param info.options.disable_signup When true (default is false) users will not be able to sign up for a Dropbox account via the authorization page. Instead, the authorization page will show a link to the Dropbox iOS app in the App Store. This is only intended for use when necessary for compliance with App Store policies.
23
+ * @param info.options.locale If the locale specified is a supported language, Dropbox will direct users to a translated version of the authorization website. Locale tags should be IETF language tags.
24
+ * @param info.options.force_reauthentication When true (default is false) users will be signed out if they are currently signed in. This will make sure the user is brought to a page where they can create a new account or sign in to another account. This should only be used when there is a definite reason to believe that the user needs to sign in to a new or different account.
19
25
  */
20
26
  async init(info) {
21
27
  // Return url to get authorization code with
22
- const authUrl = `https://www.dropbox.com/oauth2/authorize?response_type=code&token_access_type=offline&client_id=${this.settings.client_id}&scope=${encodeURIComponent(this.settings.scopes.join(' '))}&redirect_uri=${encodeURIComponent(info.redirect_url)}&state=${encodeURIComponent(info.state)}`;
28
+ const options = info.options || {};
29
+ const authUrl = `https://www.dropbox.com/oauth2/authorize?response_type=code&token_access_type=offline&client_id=${this.settings.client_id}&scope=${encodeURIComponent(this.settings.scopes.join(' '))}&redirect_uri=${encodeURIComponent(info.redirect_url)}&locale=${options.locale || ''}&require_role=${options.require_role || ''}&force_reauthentication=${options.force_reauthentication === true}&force_reapprove=${options.force_reapprove === true}&disable_signup=${options.disable_signup === true}&state=${encodeURIComponent(info.state)}`;
23
30
  return authUrl;
24
31
  }
25
32
  getAccessToken(params) {
package/src/start.js CHANGED
@@ -21,6 +21,12 @@ const options = { host, port, path };
21
21
  if (clusterPort > 0) {
22
22
  options.cluster = { enabled: true, port: clusterPort };
23
23
  }
24
+ if (+getVariable('TXLOG', 0) === 1) {
25
+ options.transactions = {
26
+ log: true,
27
+ maxAge: +getVariable('TXDAYS', 30)
28
+ };
29
+ }
24
30
  const server = new AceBaseServer(dbname, options);
25
31
  server.once("ready", () => {
26
32
  server.debug.log(`AceBase server running`);
@@ -59,10 +59,17 @@ export class DropboxAuthProvider implements IOAuth2Provider {
59
59
  * Starts auth flow by getting the url the user should be redirected to
60
60
  * @param info.redirectUrl Url dropbox will redirect to after authorizing
61
61
  * @param info.state Optional state that will be passed to redirectUri by dropbox
62
+ * @param info.options Optional Dropbox specific authentication settings
63
+ * @param info.options.require_role If this parameter is specified, the user will be asked to authorize with a particular type of Dropbox account, either work for a team account or personal for a personal account. Your app should still verify the type of Dropbox account after authorization since the user could modify or remove the require_role parameter.
64
+ * @param info.options.force_reapprove Whether or not to force the user to approve the app again if they've already done so. If false (default), a user who has already approved the application may be automatically redirected to the URI specified by redirect_uri. If true, the user will not be automatically redirected and will have to approve the app again.
65
+ * @param info.options.disable_signup When true (default is false) users will not be able to sign up for a Dropbox account via the authorization page. Instead, the authorization page will show a link to the Dropbox iOS app in the App Store. This is only intended for use when necessary for compliance with App Store policies.
66
+ * @param info.options.locale If the locale specified is a supported language, Dropbox will direct users to a translated version of the authorization website. Locale tags should be IETF language tags.
67
+ * @param info.options.force_reauthentication When true (default is false) users will be signed out if they are currently signed in. This will make sure the user is brought to a page where they can create a new account or sign in to another account. This should only be used when there is a definite reason to believe that the user needs to sign in to a new or different account.
62
68
  */
63
- async init(info: { redirect_url: string, state?: string }) {
69
+ async init(info: { redirect_url: string, state?: string, options?: { locale?: string, require_role?: string, disable_signup?: boolean, force_reapprove?: boolean, force_reauthentication?: boolean } }) {
64
70
  // Return url to get authorization code with
65
- const authUrl = `https://www.dropbox.com/oauth2/authorize?response_type=code&token_access_type=offline&client_id=${this.settings.client_id}&scope=${encodeURIComponent(this.settings.scopes.join(' '))}&redirect_uri=${encodeURIComponent(info.redirect_url)}&state=${encodeURIComponent(info.state)}`;
71
+ const options = info.options || {};
72
+ const authUrl = `https://www.dropbox.com/oauth2/authorize?response_type=code&token_access_type=offline&client_id=${this.settings.client_id}&scope=${encodeURIComponent(this.settings.scopes.join(' '))}&redirect_uri=${encodeURIComponent(info.redirect_url)}&locale=${options.locale || ''}&require_role=${options.require_role || ''}&force_reauthentication=${options.force_reauthentication === true}&force_reapprove=${options.force_reapprove === true}&disable_signup=${options.disable_signup === true}&state=${encodeURIComponent(info.state)}`;
66
73
  return authUrl;
67
74
  }
68
75
 
@@ -19,7 +19,7 @@ export interface IOAuth2TokenResult {
19
19
  refresh_token?: string
20
20
  }
21
21
  export interface IOAuth2Provider {
22
- init(options: { redirect_url: string, state?: string }): Promise<string>
22
+ init(info: { redirect_url: string, state?: string, options?: any }): Promise<string>
23
23
  getAccessToken(params: IOAuth2AuthCodeParams | IOAuth2RefreshTokenParams): Promise<IOAuth2TokenResult>
24
24
  getUserInfo(access_token: string): Promise<{ id: string, name: string, display_name: string, picture?: Array<{ width?: number, height?: number, url: string }>, email: string, email_verified: boolean, other?: { [key:string]: string|number|boolean } }>
25
25
  }
@@ -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.5.1/dist/browser.min.js"></script>
267
- <!-- <script type="text/javascript" src="./acebase-client-browser.min.js"></script> -->
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>
@@ -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
- connection.db = new AceBaseClient({ dbname, host: location.hostname, port: location.port, https: location.protocol === 'https:', autoConnect: true });
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(() => {
package/tsconfig.json CHANGED
@@ -3,7 +3,9 @@
3
3
  "target": "ES2017",
4
4
  "lib": ["ES2017"],
5
5
  "module": "CommonJS",
6
- "listEmittedFiles": true
6
+ "listEmittedFiles": true,
7
+ "rootDir": "./src/ts",
8
+ "outDir": "./src"
7
9
  },
8
10
  "exclude": [
9
11
  "node_modules"