@niledatabase/server 3.0.0-alpha.18 → 3.0.0-alpha.19

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.
@@ -6,6 +6,85 @@ require('dotenv/config');
6
6
  var pg = require('pg');
7
7
  var jose = require('jose');
8
8
 
9
+ function _arrayLikeToArray(r, a) {
10
+ (null == a || a > r.length) && (a = r.length);
11
+ for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
12
+ return n;
13
+ }
14
+ function asyncGeneratorStep(n, t, e, r, o, a, c) {
15
+ try {
16
+ var i = n[a](c),
17
+ u = i.value;
18
+ } catch (n) {
19
+ return void e(n);
20
+ }
21
+ i.done ? t(u) : Promise.resolve(u).then(r, o);
22
+ }
23
+ function _asyncToGenerator(n) {
24
+ return function () {
25
+ var t = this,
26
+ e = arguments;
27
+ return new Promise(function (r, o) {
28
+ var a = n.apply(t, e);
29
+ function _next(n) {
30
+ asyncGeneratorStep(a, r, o, _next, _throw, "next", n);
31
+ }
32
+ function _throw(n) {
33
+ asyncGeneratorStep(a, r, o, _next, _throw, "throw", n);
34
+ }
35
+ _next(void 0);
36
+ });
37
+ };
38
+ }
39
+ function _defineProperties(e, r) {
40
+ for (var t = 0; t < r.length; t++) {
41
+ var o = r[t];
42
+ o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o);
43
+ }
44
+ }
45
+ function _createClass(e, r, t) {
46
+ return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", {
47
+ writable: !1
48
+ }), e;
49
+ }
50
+ function _createForOfIteratorHelperLoose(r, e) {
51
+ var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
52
+ if (t) return (t = t.call(r)).next.bind(t);
53
+ if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) {
54
+ t && (r = t);
55
+ var o = 0;
56
+ return function () {
57
+ return o >= r.length ? {
58
+ done: !0
59
+ } : {
60
+ done: !1,
61
+ value: r[o++]
62
+ };
63
+ };
64
+ }
65
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
66
+ }
67
+ function _extends() {
68
+ return _extends = Object.assign ? Object.assign.bind() : function (n) {
69
+ for (var e = 1; e < arguments.length; e++) {
70
+ var t = arguments[e];
71
+ for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
72
+ }
73
+ return n;
74
+ }, _extends.apply(null, arguments);
75
+ }
76
+ function _inheritsLoose(t, o) {
77
+ t.prototype = Object.create(o.prototype), t.prototype.constructor = t, _setPrototypeOf(t, o);
78
+ }
79
+ function _objectWithoutPropertiesLoose(r, e) {
80
+ if (null == r) return {};
81
+ var t = {};
82
+ for (var n in r) if ({}.hasOwnProperty.call(r, n)) {
83
+ if (e.includes(n)) continue;
84
+ t[n] = r[n];
85
+ }
86
+ return t;
87
+ }
9
88
  function _regeneratorRuntime() {
10
89
  _regeneratorRuntime = function () {
11
90
  return e;
@@ -307,6 +386,11 @@ function _regeneratorRuntime() {
307
386
  }
308
387
  }, e;
309
388
  }
389
+ function _setPrototypeOf(t, e) {
390
+ return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) {
391
+ return t.__proto__ = e, t;
392
+ }, _setPrototypeOf(t, e);
393
+ }
310
394
  function _toPrimitive(t, r) {
311
395
  if ("object" != typeof t || !t) return t;
312
396
  var e = t[Symbol.toPrimitive];
@@ -321,122 +405,13 @@ function _toPropertyKey(t) {
321
405
  var i = _toPrimitive(t, "string");
322
406
  return "symbol" == typeof i ? i : i + "";
323
407
  }
324
- function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
325
- try {
326
- var info = gen[key](arg);
327
- var value = info.value;
328
- } catch (error) {
329
- reject(error);
330
- return;
331
- }
332
- if (info.done) {
333
- resolve(value);
334
- } else {
335
- Promise.resolve(value).then(_next, _throw);
408
+ function _unsupportedIterableToArray(r, a) {
409
+ if (r) {
410
+ if ("string" == typeof r) return _arrayLikeToArray(r, a);
411
+ var t = {}.toString.call(r).slice(8, -1);
412
+ return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
336
413
  }
337
414
  }
338
- function _asyncToGenerator(fn) {
339
- return function () {
340
- var self = this,
341
- args = arguments;
342
- return new Promise(function (resolve, reject) {
343
- var gen = fn.apply(self, args);
344
- function _next(value) {
345
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
346
- }
347
- function _throw(err) {
348
- asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
349
- }
350
- _next(undefined);
351
- });
352
- };
353
- }
354
- function _defineProperties(target, props) {
355
- for (var i = 0; i < props.length; i++) {
356
- var descriptor = props[i];
357
- descriptor.enumerable = descriptor.enumerable || false;
358
- descriptor.configurable = true;
359
- if ("value" in descriptor) descriptor.writable = true;
360
- Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
361
- }
362
- }
363
- function _createClass(Constructor, protoProps, staticProps) {
364
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
365
- if (staticProps) _defineProperties(Constructor, staticProps);
366
- Object.defineProperty(Constructor, "prototype", {
367
- writable: false
368
- });
369
- return Constructor;
370
- }
371
- function _extends() {
372
- _extends = Object.assign ? Object.assign.bind() : function (target) {
373
- for (var i = 1; i < arguments.length; i++) {
374
- var source = arguments[i];
375
- for (var key in source) {
376
- if (Object.prototype.hasOwnProperty.call(source, key)) {
377
- target[key] = source[key];
378
- }
379
- }
380
- }
381
- return target;
382
- };
383
- return _extends.apply(this, arguments);
384
- }
385
- function _inheritsLoose(subClass, superClass) {
386
- subClass.prototype = Object.create(superClass.prototype);
387
- subClass.prototype.constructor = subClass;
388
- _setPrototypeOf(subClass, superClass);
389
- }
390
- function _setPrototypeOf(o, p) {
391
- _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
392
- o.__proto__ = p;
393
- return o;
394
- };
395
- return _setPrototypeOf(o, p);
396
- }
397
- function _objectWithoutPropertiesLoose(source, excluded) {
398
- if (source == null) return {};
399
- var target = {};
400
- var sourceKeys = Object.keys(source);
401
- var key, i;
402
- for (i = 0; i < sourceKeys.length; i++) {
403
- key = sourceKeys[i];
404
- if (excluded.indexOf(key) >= 0) continue;
405
- target[key] = source[key];
406
- }
407
- return target;
408
- }
409
- function _unsupportedIterableToArray(o, minLen) {
410
- if (!o) return;
411
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
412
- var n = Object.prototype.toString.call(o).slice(8, -1);
413
- if (n === "Object" && o.constructor) n = o.constructor.name;
414
- if (n === "Map" || n === "Set") return Array.from(o);
415
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
416
- }
417
- function _arrayLikeToArray(arr, len) {
418
- if (len == null || len > arr.length) len = arr.length;
419
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
420
- return arr2;
421
- }
422
- function _createForOfIteratorHelperLoose(o, allowArrayLike) {
423
- var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
424
- if (it) return (it = it.call(o)).next.bind(it);
425
- if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
426
- if (it) o = it;
427
- var i = 0;
428
- return function () {
429
- if (i >= o.length) return {
430
- done: true
431
- };
432
- return {
433
- done: false,
434
- value: o[i++]
435
- };
436
- };
437
- }
438
- throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
439
- }
440
415
 
441
416
  function Logger(config) {
442
417
  for (var _len = arguments.length, params = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
@@ -576,14 +551,9 @@ var getBasePath = function getBasePath(cfg) {
576
551
  var _Logger7 = Logger(config, '[basePath]'),
577
552
  info = _Logger7.info;
578
553
  var basePath = config == null || (_config$api4 = config.api) == null ? void 0 : _config$api4.basePath;
579
- var version = '';
580
- if (!/\/v\d\//.test(String(basePath))) {
581
- var _config$api5, _config$api$version, _config$api6;
582
- version = (config == null || (_config$api5 = config.api) == null ? void 0 : _config$api5.version) === 1 ? '' : "/v" + ((_config$api$version = config == null || (_config$api6 = config.api) == null ? void 0 : _config$api6.version) != null ? _config$api$version : 2);
583
- }
584
554
  if (basePath) {
585
555
  logger && info(logger, 'config', basePath);
586
- return "" + basePath + version;
556
+ return basePath;
587
557
  }
588
558
  if (process.env.NILEDB_API_URL) {
589
559
  logger && info(logger, 'NILEDB_API_URL', process.env.NILEDB_API_URL);
@@ -685,8 +655,8 @@ var Config = /*#__PURE__*/function () {
685
655
  function Config(_config, logger) {
686
656
  var _this = this,
687
657
  _config$db2,
688
- _config$api$cookieKey2,
689
- _config$api2;
658
+ _config$api$cookieKey3,
659
+ _config$api3;
690
660
  this.user = void 0;
691
661
  this.password = void 0;
692
662
  this.databaseId = void 0;
@@ -699,9 +669,9 @@ var Config = /*#__PURE__*/function () {
699
669
  this._tenantId = void 0;
700
670
  this._userId = void 0;
701
671
  this.configure = /*#__PURE__*/function () {
702
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config) {
703
- var _config$db, _config$api$cookieKey, _config$api;
704
- var _Logger, info, error, envVarConfig, _ref3, host, port, dbConfig, configuredHost, configuredPort, basePath, cp, databaseName, url, res, database, possibleError, json, message, _database, apiHost, dbHost, name, id, dburl;
672
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(config) {
673
+ var _config$db, _config$api$cookieKey2, _config$api2;
674
+ var _Logger, info, error, envVarConfig, _ref3, host, port, dbConfig, configuredHost, configuredPort, basePath, _config$api$cookieKey, _config$api, cp, databaseName, url, res, database, possibleError, json, message, _database, apiHost, dbHost, name, id, dburl;
705
675
  return _regeneratorRuntime().wrap(function _callee$(_context) {
706
676
  while (1) switch (_context.prev = _context.next) {
707
677
  case 0:
@@ -714,12 +684,27 @@ var Config = /*#__PURE__*/function () {
714
684
  configuredPort = port != null ? port : getDbPort(envVarConfig);
715
685
  basePath = getBasePath(envVarConfig);
716
686
  if (!(configuredHost && _this.databaseName && _this.databaseId && basePath)) {
717
- _context.next = 9;
687
+ _context.next = 12;
718
688
  break;
719
689
  }
720
690
  info('Already configured, aborting fetch');
691
+ _this.api = new ApiConfig({
692
+ basePath: basePath,
693
+ cookieKey: (_config$api$cookieKey = config == null || (_config$api = config.api) == null ? void 0 : _config$api.cookieKey) != null ? _config$api$cookieKey : 'token',
694
+ token: getToken({
695
+ config: config
696
+ })
697
+ });
698
+ _this.db = _extends({
699
+ user: _this.user,
700
+ password: _this.password,
701
+ host: configuredHost,
702
+ port: configuredPort,
703
+ database: _this.databaseName
704
+ }, dbConfig);
705
+ info('[config set]', _this);
721
706
  return _context.abrupt("return", _this);
722
- case 9:
707
+ case 12:
723
708
  cp = getControlPlane(envVarConfig);
724
709
  databaseName = getDatabaseName({
725
710
  config: config,
@@ -730,7 +715,7 @@ var Config = /*#__PURE__*/function () {
730
715
  url.searchParams.set('databaseName', databaseName);
731
716
  }
732
717
  info('configuring from', url.href);
733
- _context.next = 16;
718
+ _context.next = 19;
734
719
  return fetch(url, {
735
720
  headers: {
736
721
  Authorization: "Bearer " + getInfoBearer({
@@ -738,13 +723,13 @@ var Config = /*#__PURE__*/function () {
738
723
  })
739
724
  }
740
725
  });
741
- case 16:
726
+ case 19:
742
727
  res = _context.sent;
743
728
  possibleError = res.clone();
744
- _context.prev = 18;
745
- _context.next = 21;
729
+ _context.prev = 21;
730
+ _context.next = 24;
746
731
  return res.json();
747
- case 21:
732
+ case 24:
748
733
  json = _context.sent;
749
734
  if (res.status === 404) {
750
735
  info('is the configured databaseName correct?');
@@ -756,34 +741,34 @@ var Config = /*#__PURE__*/function () {
756
741
  } else {
757
742
  database = json;
758
743
  }
759
- _context.next = 33;
744
+ _context.next = 36;
760
745
  break;
761
- case 26:
762
- _context.prev = 26;
763
- _context.t0 = _context["catch"](18);
764
- _context.next = 30;
746
+ case 29:
747
+ _context.prev = 29;
748
+ _context.t0 = _context["catch"](21);
749
+ _context.next = 33;
765
750
  return possibleError.text();
766
- case 30:
751
+ case 33:
767
752
  message = _context.sent;
768
753
  error(message);
769
754
  database = {
770
755
  message: message
771
756
  };
772
- case 33:
757
+ case 36:
773
758
  info('[fetched database]', database);
774
759
  if (!('message' in database)) {
775
- _context.next = 42;
760
+ _context.next = 45;
776
761
  break;
777
762
  }
778
763
  if (!('statusCode' in database)) {
779
- _context.next = 41;
764
+ _context.next = 44;
780
765
  break;
781
766
  }
782
767
  error(database);
783
768
  throw new Error('HTTP error has occurred');
784
- case 41:
769
+ case 44:
785
770
  throw new Error('Unable to auto-configure. Please remove NILEDB_NAME, NILEDB_API_URL, NILEDB_POSTGRES_URL, and/or NILEDB_HOST from your environment variables.');
786
- case 42:
771
+ case 45:
787
772
  if (typeof database === 'object') {
788
773
  _database = database, apiHost = _database.apiHost, dbHost = _database.dbHost, name = _database.name, id = _database.id;
789
774
  basePath = basePath || apiHost;
@@ -792,10 +777,10 @@ var Config = /*#__PURE__*/function () {
792
777
  dburl = new URL(dbHost);
793
778
  configuredHost = dburl.host;
794
779
  }
795
- case 43:
780
+ case 46:
796
781
  _this.api = new ApiConfig({
797
782
  basePath: basePath,
798
- cookieKey: (_config$api$cookieKey = config == null || (_config$api = config.api) == null ? void 0 : _config$api.cookieKey) != null ? _config$api$cookieKey : 'token',
783
+ cookieKey: (_config$api$cookieKey2 = config == null || (_config$api2 = config.api) == null ? void 0 : _config$api2.cookieKey) != null ? _config$api$cookieKey2 : 'token',
799
784
  token: getToken({
800
785
  config: config
801
786
  })
@@ -809,11 +794,11 @@ var Config = /*#__PURE__*/function () {
809
794
  }, dbConfig);
810
795
  info('[config set]', _this);
811
796
  return _context.abrupt("return", _this);
812
- case 47:
797
+ case 50:
813
798
  case "end":
814
799
  return _context.stop();
815
800
  }
816
- }, _callee, null, [[18, 26]]);
801
+ }, _callee, null, [[21, 29]]);
817
802
  }));
818
803
  return function (_x) {
819
804
  return _ref2.apply(this, arguments);
@@ -847,7 +832,7 @@ var Config = /*#__PURE__*/function () {
847
832
  var _configuredPort = _port != null ? _port : getDbPort(_envVarConfig);
848
833
  this.api = new ApiConfig({
849
834
  basePath: _basePath,
850
- cookieKey: (_config$api$cookieKey2 = _config == null || (_config$api2 = _config.api) == null ? void 0 : _config$api2.cookieKey) != null ? _config$api$cookieKey2 : 'token',
835
+ cookieKey: (_config$api$cookieKey3 = _config == null || (_config$api3 = _config.api) == null ? void 0 : _config$api3.cookieKey) != null ? _config$api$cookieKey3 : 'token',
851
836
  token: getToken({
852
837
  config: _config
853
838
  })
@@ -890,14 +875,12 @@ var Events;
890
875
  })(Events || (Events = {}));
891
876
  var Eventer = /*#__PURE__*/function () {
892
877
  function Eventer() {
893
- this.events = void 0;
894
878
  this.events = {};
895
879
  }
896
880
  var _proto = Eventer.prototype;
881
+ // Publish event and notify all subscribers
897
882
  _proto.publish = function publish(eventName, value) {
898
- // Get all the callback functions of the current event
899
883
  var callbackList = this.events[eventName];
900
- // execute each callback function
901
884
  if (callbackList) {
902
885
  for (var _iterator = _createForOfIteratorHelperLoose(callbackList), _step; !(_step = _iterator()).done;) {
903
886
  var callback = _step.value;
@@ -908,19 +891,25 @@ var Eventer = /*#__PURE__*/function () {
908
891
  // Subscribe to events
909
892
  ;
910
893
  _proto.subscribe = function subscribe(eventName, callback) {
911
- // initialize this event
912
894
  if (!this.events[eventName]) {
913
895
  this.events[eventName] = [];
914
896
  }
915
- // store the callback function of the subscriber
916
897
  this.events[eventName].push(callback);
917
- };
898
+ }
899
+ // Unsubscribe from an event
900
+ ;
918
901
  _proto.unsubscribe = function unsubscribe(eventName, callback) {
919
- var toRemove = this.events[eventName].findIndex(function (cb) {
920
- return cb === callback;
921
- });
922
- if (toRemove !== -1) {
923
- this.events[eventName].splice(toRemove, 1);
902
+ var callbackList = this.events[eventName];
903
+ if (!callbackList) {
904
+ return; // Early exit if no event exists
905
+ }
906
+ var index = callbackList.indexOf(callback);
907
+ if (index !== -1) {
908
+ callbackList.splice(index, 1); // Remove the callback
909
+ }
910
+ // If there are no more listeners, clean up the event
911
+ if (callbackList.length === 0) {
912
+ delete this.events[eventName];
924
913
  }
925
914
  };
926
915
  return Eventer;
@@ -944,9 +933,6 @@ var watchToken = function watchToken(cb) {
944
933
  var watchEvictPool = function watchEvictPool(cb) {
945
934
  return eventer.subscribe(Events.EvictPool, cb);
946
935
  };
947
- var closeEvictPool = function closeEvictPool(cb) {
948
- return eventer.unsubscribe(Events.EvictPool, cb);
949
- };
950
936
  var evictPool = function evictPool(val) {
951
937
  eventer.publish(Events.EvictPool, val);
952
938
  };
@@ -982,14 +968,6 @@ function createProxyForPool(pool, config) {
982
968
  });
983
969
  }
984
970
 
985
- function isUUID(value) {
986
- if (!value) {
987
- return false;
988
- }
989
- var regex = /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
990
- return regex.test(value);
991
- }
992
-
993
971
  var _excluded$1 = ["afterCreate"];
994
972
  var NileDatabase = /*#__PURE__*/function () {
995
973
  function NileDatabase(config, id) {
@@ -1021,7 +999,7 @@ var NileDatabase = /*#__PURE__*/function () {
1021
999
  // start the timer for cleanup
1022
1000
  this.startTimeout();
1023
1001
  this.pool.on('connect', /*#__PURE__*/function () {
1024
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(client) {
1002
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(client) {
1025
1003
  var afterCreate;
1026
1004
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1027
1005
  while (1) switch (_context.prev = _context.next) {
@@ -1048,7 +1026,7 @@ var NileDatabase = /*#__PURE__*/function () {
1048
1026
  };
1049
1027
  }());
1050
1028
  this.pool.on('error', /*#__PURE__*/function () {
1051
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(e) {
1029
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(e) {
1052
1030
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1053
1031
  while (1) switch (_context2.prev = _context2.next) {
1054
1032
  case 0:
@@ -1070,30 +1048,22 @@ var NileDatabase = /*#__PURE__*/function () {
1070
1048
  }
1071
1049
  var _proto = NileDatabase.prototype;
1072
1050
  _proto.startTimeout = function startTimeout() {
1073
- var _this2 = this;
1051
+ var _this2 = this,
1052
+ _Number2;
1074
1053
  var _Logger3 = Logger(this.config, '[NileInstance]'),
1075
1054
  info = _Logger3.info;
1076
1055
  if (this.timer) {
1077
1056
  clearTimeout(this.timer);
1078
1057
  }
1079
- this.timer = setTimeout( /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3() {
1080
- return _regeneratorRuntime().wrap(function _callee3$(_context3) {
1081
- while (1) switch (_context3.prev = _context3.next) {
1082
- case 0:
1083
- info('Pool reached idleTimeoutMillis.', _this2.id, 'evicted after', _this2.config.db.idleTimeoutMillis, 'ms');
1084
- _context3.next = 3;
1085
- return _this2.pool.end(function () {
1086
- info('Pool end called');
1087
- // something odd going on here. Without the callback, pool.end() is flakey
1088
- });
1089
- case 3:
1090
- evictPool(_this2.id);
1091
- case 4:
1092
- case "end":
1093
- return _context3.stop();
1094
- }
1095
- }, _callee3);
1096
- })), this.config.db.idleTimeoutMillis);
1058
+ this.timer = setTimeout(function () {
1059
+ var _Number;
1060
+ info('Pool reached idleTimeoutMillis.', _this2.id, 'evicted after', (_Number = Number(_this2.config.db.idleTimeoutMillis)) != null ? _Number : 30000, 'ms');
1061
+ _this2.pool.end(function () {
1062
+ evictPool(_this2.id);
1063
+ info('Pool end called for', _this2.id);
1064
+ // something odd going on here. Without the callback, pool.end() is flakey
1065
+ });
1066
+ }, (_Number2 = Number(this.config.db.idleTimeoutMillis)) != null ? _Number2 : 30000);
1097
1067
  };
1098
1068
  return NileDatabase;
1099
1069
  }();
@@ -1106,9 +1076,9 @@ function makeAfterCreate(config) {
1106
1076
  warn('Connection was terminated by server', error);
1107
1077
  done(error, conn);
1108
1078
  });
1109
- if (isUUID(config.tenantId)) {
1079
+ if (config.tenantId) {
1110
1080
  var query = ["SET nile.tenant_id = '" + config.tenantId + "'"];
1111
- if (isUUID(config.userId)) {
1081
+ if (config.userId) {
1112
1082
  if (!config.tenantId) {
1113
1083
  warn('A user id cannot be set in context without a tenant id');
1114
1084
  }
@@ -1134,6 +1104,7 @@ var DBManager = /*#__PURE__*/function () {
1134
1104
  var _this = this;
1135
1105
  this.connections = void 0;
1136
1106
  this.cleared = void 0;
1107
+ this.poolWatcherFn = void 0;
1137
1108
  this.poolWatcher = function (config) {
1138
1109
  return function (id) {
1139
1110
  var _Logger = Logger(config, '[DBManager]'),
@@ -1141,6 +1112,8 @@ var DBManager = /*#__PURE__*/function () {
1141
1112
  if (id && _this.connections.has(id)) {
1142
1113
  info('Removing', id, 'from db connection pool.');
1143
1114
  _this.connections["delete"](id);
1115
+ } else {
1116
+ info('missed eviction of', id);
1144
1117
  }
1145
1118
  };
1146
1119
  };
@@ -1159,10 +1132,8 @@ var DBManager = /*#__PURE__*/function () {
1159
1132
  _this.connections.set(id, newOne);
1160
1133
  info('created new', id);
1161
1134
  info('# of instances:', _this.connections.size);
1162
- // resume listening to the evict pool if a connection is requested.
1163
1135
  if (_this.cleared) {
1164
1136
  _this.cleared = false;
1165
- watchEvictPool(_this.poolWatcher(config));
1166
1137
  }
1167
1138
  return newOne.pool;
1168
1139
  };
@@ -1170,26 +1141,20 @@ var DBManager = /*#__PURE__*/function () {
1170
1141
  var _Logger3 = Logger(config, '[DBManager]'),
1171
1142
  info = _Logger3.info;
1172
1143
  info('Clearing all connections', _this.connections.size);
1173
- closeEvictPool(_this.poolWatcher(config));
1174
1144
  _this.cleared = true;
1175
1145
  _this.connections.clear();
1176
1146
  };
1177
- var _Logger4 = Logger(_config, '[DBManager]'),
1178
- _info = _Logger4.info;
1179
1147
  this.cleared = false;
1180
1148
  this.connections = new Map();
1181
- // add the base one, so you can at least query
1182
- var _id = this.makeId();
1183
- _info('constructor', _id);
1184
- this.connections.set(_id, new NileDatabase(new Config(_config), _id));
1185
- watchEvictPool(this.poolWatcher(_config));
1149
+ this.poolWatcherFn = this.poolWatcher(_config);
1150
+ watchEvictPool(this.poolWatcherFn);
1186
1151
  }
1187
1152
  var _proto = DBManager.prototype;
1188
1153
  _proto.makeId = function makeId(tenantId, userId) {
1189
- if (isUUID(tenantId) && isUUID(userId)) {
1154
+ if (tenantId && userId) {
1190
1155
  return tenantId + ":" + userId;
1191
1156
  }
1192
- if (isUUID(tenantId)) {
1157
+ if (tenantId) {
1193
1158
  return "" + tenantId;
1194
1159
  }
1195
1160
  return 'base';
@@ -1197,14 +1162,6 @@ var DBManager = /*#__PURE__*/function () {
1197
1162
  return DBManager;
1198
1163
  }();
1199
1164
 
1200
- var getServerId = function getServerId(config) {
1201
- var cfg = new Config(config);
1202
- return makeServerId(cfg);
1203
- };
1204
- var makeServerId = function makeServerId(config) {
1205
- return Buffer.from(JSON.stringify(config), 'utf8').toString('base64');
1206
- };
1207
-
1208
1165
  var NILEDB_API_URL = process.env.NILEDB_API_URL;
1209
1166
  function makeRestUrl(config, path, qp) {
1210
1167
  var url = config.api.basePath || NILEDB_API_URL;
@@ -1253,7 +1210,7 @@ function request(_x, _x2, _x3) {
1253
1210
  return _request.apply(this, arguments);
1254
1211
  }
1255
1212
  function _request() {
1256
- _request = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(url, _init, config) {
1213
+ _request = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(url, _init, config) {
1257
1214
  var _params$method;
1258
1215
  var _Logger, info, error, request, init, requestUrl, updatedHeaders, params, _init$body, res, loggingRes;
1259
1216
  return _regeneratorRuntime().wrap(function _callee$(_context) {
@@ -1265,7 +1222,6 @@ function _request() {
1265
1222
  updatedHeaders = new Headers(request.headers);
1266
1223
  updatedHeaders.set('host', requestUrl.host);
1267
1224
  updatedHeaders.set('niledb-origin', requestUrl.origin);
1268
- updatedHeaders.set('niledb-creds', Buffer.from(process.env.NILEDB_USER + ":" + process.env.NILEDB_PASSWORD).toString('base64'));
1269
1225
  params = _extends({}, init, {
1270
1226
  headers: updatedHeaders
1271
1227
  });
@@ -1275,26 +1231,26 @@ function _request() {
1275
1231
  params.duplex = 'half';
1276
1232
  }
1277
1233
  info("[" + ((_params$method = params.method) != null ? _params$method : 'GET') + "]", url);
1278
- _context.next = 12;
1234
+ _context.next = 11;
1279
1235
  return fetch(url, _extends({}, params))["catch"](function (e) {
1280
1236
  error('An error has occurred in the fetch', e);
1281
1237
  return new Response('An unexpected (most likely configuration) problem has occurred', {
1282
1238
  status: 500
1283
1239
  });
1284
1240
  });
1285
- case 12:
1241
+ case 11:
1286
1242
  res = _context.sent;
1287
1243
  loggingRes = typeof (res == null ? void 0 : res.clone) === 'function' ? res == null ? void 0 : res.clone() : null;
1288
1244
  _context.t0 = info;
1289
1245
  _context.t1 = res == null ? void 0 : res.status;
1290
1246
  _context.t2 = res == null ? void 0 : res.statusText;
1291
- _context.next = 19;
1247
+ _context.next = 18;
1292
1248
  return loggingRes == null ? void 0 : loggingRes.text();
1293
- case 19:
1249
+ case 18:
1294
1250
  _context.t3 = _context.sent;
1295
1251
  (0, _context.t0)('[Response]', _context.t1, _context.t2, _context.t3);
1296
1252
  return _context.abrupt("return", res);
1297
- case 22:
1253
+ case 21:
1298
1254
  case "end":
1299
1255
  return _context.stop();
1300
1256
  }
@@ -1332,11 +1288,11 @@ var key$7 = 'ME';
1332
1288
  * description: Unauthorized
1333
1289
  * content: {}
1334
1290
  */
1335
- function GET$3(_x, _x2, _x3, _x4) {
1336
- return _GET$3.apply(this, arguments);
1291
+ function GET$4(_x, _x2, _x3, _x4) {
1292
+ return _GET$4.apply(this, arguments);
1337
1293
  }
1338
- function _GET$3() {
1339
- _GET$3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(url, init, log, config) {
1294
+ function _GET$4() {
1295
+ _GET$4 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(url, init, log, config) {
1340
1296
  var res;
1341
1297
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1342
1298
  while (1) switch (_context.prev = _context.next) {
@@ -1353,13 +1309,13 @@ function _GET$3() {
1353
1309
  }
1354
1310
  }, _callee);
1355
1311
  }));
1356
- return _GET$3.apply(this, arguments);
1312
+ return _GET$4.apply(this, arguments);
1357
1313
  }
1358
1314
  function route$a(_x5, _x6) {
1359
1315
  return _route$a.apply(this, arguments);
1360
1316
  }
1361
1317
  function _route$a() {
1362
- _route$a = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(request, config) {
1318
+ _route$a = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(request, config) {
1363
1319
  var url, _Logger, info;
1364
1320
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
1365
1321
  while (1) switch (_context2.prev = _context2.next) {
@@ -1373,7 +1329,7 @@ function _route$a() {
1373
1329
  break;
1374
1330
  case 5:
1375
1331
  _context2.next = 7;
1376
- return GET$3(url, {
1332
+ return GET$4(url, {
1377
1333
  request: request
1378
1334
  }, info, config);
1379
1335
  case 7:
@@ -1398,7 +1354,7 @@ function auth(_x, _x2) {
1398
1354
  return _auth.apply(this, arguments);
1399
1355
  }
1400
1356
  function _auth() {
1401
- _auth = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, config) {
1357
+ _auth = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, config) {
1402
1358
  var _Logger, info, error, sessionUrl, res;
1403
1359
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1404
1360
  while (1) switch (_context.prev = _context.next) {
@@ -1494,9 +1450,9 @@ function _fetch(_x, _x2, _x3) {
1494
1450
  return _fetch2.apply(this, arguments);
1495
1451
  }
1496
1452
  function _fetch2() {
1497
- _fetch2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, path, opts) {
1453
+ _fetch2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, path, opts) {
1498
1454
  var _config$api, _config$api2, _response$status;
1499
- var _Logger, info, error, url, cookieKey, headers, basicHeaders, creds, authHeader, token, tenantId, userId, useableUrl, response, res, errorHandler, msg, _res, message, _res2, _res2$errors, _message;
1455
+ var _Logger, info, error, url, cookieKey, headers, basicHeaders, authHeader, token, tenantId, userId, useableUrl, response, res, errorHandler, msg, _res, message, _res2, _res2$errors, _message;
1500
1456
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1501
1457
  while (1) switch (_context.prev = _context.next) {
1502
1458
  case 0:
@@ -1506,8 +1462,6 @@ function _fetch2() {
1506
1462
  headers = new Headers(opts == null ? void 0 : opts.headers);
1507
1463
  basicHeaders = new Headers(opts == null ? void 0 : opts.headers);
1508
1464
  basicHeaders.set('content-type', 'application/json; charset=utf-8');
1509
- creds = Buffer.from(process.env.NILEDB_USER + ":" + process.env.NILEDB_PASSWORD, 'utf8').toString('base64');
1510
- basicHeaders.set('niledb-creds', creds);
1511
1465
  authHeader = headers.get('Authorization');
1512
1466
  if (!authHeader) {
1513
1467
  token = getTokenFromCookie(headers, cookieKey);
@@ -1526,114 +1480,114 @@ function _fetch2() {
1526
1480
  userId = getUserFromHttp(headers, config);
1527
1481
  updateUserId(userId);
1528
1482
  if (!(url.includes('{tenantId}') && !tenantId)) {
1529
- _context.next = 16;
1483
+ _context.next = 14;
1530
1484
  break;
1531
1485
  }
1532
1486
  return _context.abrupt("return", new ResponseError('tenantId is not set for request', {
1533
1487
  status: 400
1534
1488
  }));
1535
- case 16:
1489
+ case 14:
1536
1490
  useableUrl = url.replace('{tenantId}', encodeURIComponent(String(tenantId))).replace('{userId}', encodeURIComponent(String(userId)));
1537
1491
  info('[fetch]', useableUrl);
1538
- _context.next = 20;
1492
+ _context.next = 18;
1539
1493
  return fetch(useableUrl, _extends({}, opts, {
1540
1494
  headers: basicHeaders
1541
1495
  }))["catch"](function (e) {
1542
1496
  error('[fetch]', '[response]', e);
1543
1497
  });
1544
- case 20:
1498
+ case 18:
1545
1499
  response = _context.sent;
1546
1500
  if (!(response && response.status >= 200 && response.status < 300)) {
1547
- _context.next = 39;
1501
+ _context.next = 37;
1548
1502
  break;
1549
1503
  }
1550
1504
  if (!(typeof response.clone === 'function')) {
1551
- _context.next = 38;
1505
+ _context.next = 36;
1552
1506
  break;
1553
1507
  }
1554
- _context.prev = 23;
1508
+ _context.prev = 21;
1555
1509
  _context.t0 = info;
1556
- _context.next = 27;
1510
+ _context.next = 25;
1557
1511
  return response.clone().json();
1558
- case 27:
1512
+ case 25:
1559
1513
  _context.t1 = _context.sent;
1560
1514
  (0, _context.t0)('[fetch]', '[response]', _context.t1);
1561
- _context.next = 38;
1515
+ _context.next = 36;
1562
1516
  break;
1563
- case 31:
1564
- _context.prev = 31;
1565
- _context.t2 = _context["catch"](23);
1517
+ case 29:
1518
+ _context.prev = 29;
1519
+ _context.t2 = _context["catch"](21);
1566
1520
  _context.t3 = info;
1567
- _context.next = 36;
1521
+ _context.next = 34;
1568
1522
  return response.clone().text();
1569
- case 36:
1523
+ case 34:
1570
1524
  _context.t4 = _context.sent;
1571
1525
  (0, _context.t3)('[fetch]', '[response]', _context.t4);
1572
- case 38:
1526
+ case 36:
1573
1527
  return _context.abrupt("return", response);
1574
- case 39:
1528
+ case 37:
1575
1529
  if (!((response == null ? void 0 : response.status) === 404)) {
1576
- _context.next = 41;
1530
+ _context.next = 39;
1577
1531
  break;
1578
1532
  }
1579
1533
  return _context.abrupt("return", new ResponseError('Not found', {
1580
1534
  status: 404
1581
1535
  }));
1582
- case 41:
1536
+ case 39:
1583
1537
  if (!((response == null ? void 0 : response.status) === 401)) {
1584
- _context.next = 43;
1538
+ _context.next = 41;
1585
1539
  break;
1586
1540
  }
1587
1541
  return _context.abrupt("return", new ResponseError('Unauthorized', {
1588
1542
  status: 401
1589
1543
  }));
1590
- case 43:
1544
+ case 41:
1591
1545
  if (!((response == null ? void 0 : response.status) === 405)) {
1592
- _context.next = 45;
1546
+ _context.next = 43;
1593
1547
  break;
1594
1548
  }
1595
1549
  return _context.abrupt("return", new ResponseError('Method not allowed', {
1596
1550
  status: 405
1597
1551
  }));
1598
- case 45:
1552
+ case 43:
1599
1553
  errorHandler = typeof (response == null ? void 0 : response.clone) === 'function' ? response.clone() : null;
1600
1554
  msg = '';
1601
- _context.prev = 47;
1602
- _context.next = 50;
1555
+ _context.prev = 45;
1556
+ _context.next = 48;
1603
1557
  return response == null ? void 0 : response.json();
1604
- case 50:
1558
+ case 48:
1605
1559
  res = _context.sent;
1606
- _context.next = 61;
1560
+ _context.next = 59;
1607
1561
  break;
1608
- case 53:
1609
- _context.prev = 53;
1610
- _context.t5 = _context["catch"](47);
1562
+ case 51:
1563
+ _context.prev = 51;
1564
+ _context.t5 = _context["catch"](45);
1611
1565
  if (!errorHandler) {
1612
- _context.next = 60;
1566
+ _context.next = 58;
1613
1567
  break;
1614
1568
  }
1615
- _context.next = 58;
1569
+ _context.next = 56;
1616
1570
  return errorHandler.text();
1617
- case 58:
1571
+ case 56:
1618
1572
  msg = _context.sent;
1619
1573
  if (msg) {
1620
1574
  error('[fetch]', '[response]', "[status: " + errorHandler.status + "]", msg);
1621
1575
  }
1622
- case 60:
1576
+ case 58:
1623
1577
  if (!msg) {
1624
1578
  error('[fetch]', '[response]', _context.t5);
1625
1579
  }
1626
- case 61:
1580
+ case 59:
1627
1581
  if (!msg) {
1628
- _context.next = 63;
1582
+ _context.next = 61;
1629
1583
  break;
1630
1584
  }
1631
1585
  return _context.abrupt("return", new ResponseError(msg, {
1632
1586
  status: errorHandler == null ? void 0 : errorHandler.status
1633
1587
  }));
1634
- case 63:
1588
+ case 61:
1635
1589
  if (!(res && 'message' in res)) {
1636
- _context.next = 67;
1590
+ _context.next = 65;
1637
1591
  break;
1638
1592
  }
1639
1593
  _res = res, message = _res.message;
@@ -1641,9 +1595,9 @@ function _fetch2() {
1641
1595
  return _context.abrupt("return", new ResponseError(message, {
1642
1596
  status: 400
1643
1597
  }));
1644
- case 67:
1598
+ case 65:
1645
1599
  if (!(res && 'errors' in res)) {
1646
- _context.next = 71;
1600
+ _context.next = 69;
1647
1601
  break;
1648
1602
  }
1649
1603
  _res2 = res, _res2$errors = _res2.errors, _message = _res2$errors[0];
@@ -1651,16 +1605,16 @@ function _fetch2() {
1651
1605
  return _context.abrupt("return", new ResponseError(_message, {
1652
1606
  status: 400
1653
1607
  }));
1654
- case 71:
1608
+ case 69:
1655
1609
  error('[fetch]', '[response]', "[status: " + (errorHandler == null ? void 0 : errorHandler.status) + "]", 'UNHANDLED ERROR');
1656
1610
  return _context.abrupt("return", new ResponseError(null, {
1657
1611
  status: (_response$status = response == null ? void 0 : response.status) != null ? _response$status : 500
1658
1612
  }));
1659
- case 73:
1613
+ case 71:
1660
1614
  case "end":
1661
1615
  return _context.stop();
1662
1616
  }
1663
- }, _callee, null, [[23, 31], [47, 53]]);
1617
+ }, _callee, null, [[21, 29], [45, 51]]);
1664
1618
  }));
1665
1619
  return _fetch2.apply(this, arguments);
1666
1620
  }
@@ -1727,23 +1681,15 @@ function _fetch2() {
1727
1681
  * description: Unauthorized
1728
1682
  * content: {}
1729
1683
  */
1730
- function POST$2(_x, _x2, _x3, _x4) {
1684
+ function POST$2(_x, _x2, _x3) {
1731
1685
  return _POST$2.apply(this, arguments);
1732
1686
  }
1733
1687
  function _POST$2() {
1734
- _POST$2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, session, init, log) {
1688
+ _POST$2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, init, log) {
1735
1689
  var yurl, tenantId, tenant, url;
1736
1690
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1737
1691
  while (1) switch (_context.prev = _context.next) {
1738
1692
  case 0:
1739
- if (session) {
1740
- _context.next = 2;
1741
- break;
1742
- }
1743
- return _context.abrupt("return", new Response(null, {
1744
- status: 401
1745
- }));
1746
- case 2:
1747
1693
  init.body = init.request.body;
1748
1694
  init.method = 'POST';
1749
1695
  yurl = new URL(init.request.url);
@@ -1751,11 +1697,11 @@ function _POST$2() {
1751
1697
  tenant = tenantId != null ? tenantId : getTenantFromHttp(init.request.headers);
1752
1698
  url = apiRoutes(config).USERS(tenant ? tenant : undefined);
1753
1699
  log && log('[POST]', url);
1754
- _context.next = 11;
1700
+ _context.next = 9;
1755
1701
  return request(url, init, config);
1756
- case 11:
1702
+ case 9:
1757
1703
  return _context.abrupt("return", _context.sent);
1758
- case 12:
1704
+ case 10:
1759
1705
  case "end":
1760
1706
  return _context.stop();
1761
1707
  }
@@ -1795,11 +1741,11 @@ function _POST$2() {
1795
1741
  * description: Unauthorized
1796
1742
  * content: {}
1797
1743
  */
1798
- function GET$2(_x, _x2, _x3) {
1799
- return _GET$2.apply(this, arguments);
1744
+ function GET$3(_x, _x2, _x3) {
1745
+ return _GET$3.apply(this, arguments);
1800
1746
  }
1801
- function _GET$2() {
1802
- _GET$2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, init, log) {
1747
+ function _GET$3() {
1748
+ _GET$3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, init, log) {
1803
1749
  var yurl, tenantId, tenant, url;
1804
1750
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1805
1751
  while (1) switch (_context.prev = _context.next) {
@@ -1829,7 +1775,7 @@ function _GET$2() {
1829
1775
  }
1830
1776
  }, _callee);
1831
1777
  }));
1832
- return _GET$2.apply(this, arguments);
1778
+ return _GET$3.apply(this, arguments);
1833
1779
  }
1834
1780
 
1835
1781
  /**
@@ -1868,11 +1814,11 @@ function _GET$2() {
1868
1814
  * description: Unauthorized
1869
1815
  * content: {}
1870
1816
  */
1871
- function PUT$1(_x, _x2, _x3, _x4) {
1872
- return _PUT$1.apply(this, arguments);
1817
+ function PUT$2(_x, _x2, _x3, _x4) {
1818
+ return _PUT$2.apply(this, arguments);
1873
1819
  }
1874
- function _PUT$1() {
1875
- _PUT$1 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, session, init, log) {
1820
+ function _PUT$2() {
1821
+ _PUT$2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, session, init, log) {
1876
1822
  var _URL$pathname$split$r, userId, url;
1877
1823
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1878
1824
  while (1) switch (_context.prev = _context.next) {
@@ -1901,7 +1847,7 @@ function _PUT$1() {
1901
1847
  }
1902
1848
  }, _callee);
1903
1849
  }));
1904
- return _PUT$1.apply(this, arguments);
1850
+ return _PUT$2.apply(this, arguments);
1905
1851
  }
1906
1852
 
1907
1853
  var key$6 = 'USERS';
@@ -1909,7 +1855,7 @@ function route$9(_x, _x2) {
1909
1855
  return _route$9.apply(this, arguments);
1910
1856
  }
1911
1857
  function _route$9() {
1912
- _route$9 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(request, config) {
1858
+ _route$9 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(request, config) {
1913
1859
  var _Logger, info, session;
1914
1860
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1915
1861
  while (1) switch (_context.prev = _context.next) {
@@ -1926,21 +1872,21 @@ function _route$9() {
1926
1872
  break;
1927
1873
  case 7:
1928
1874
  _context.next = 9;
1929
- return GET$2(config, {
1875
+ return GET$3(config, {
1930
1876
  request: request
1931
1877
  }, info);
1932
1878
  case 9:
1933
1879
  return _context.abrupt("return", _context.sent);
1934
1880
  case 10:
1935
1881
  _context.next = 12;
1936
- return POST$2(config, session, {
1882
+ return POST$2(config, {
1937
1883
  request: request
1938
1884
  }, info);
1939
1885
  case 12:
1940
1886
  return _context.abrupt("return", _context.sent);
1941
1887
  case 13:
1942
1888
  _context.next = 15;
1943
- return PUT$1(config, session, {
1889
+ return PUT$2(config, session, {
1944
1890
  request: request
1945
1891
  }, info);
1946
1892
  case 15:
@@ -1992,11 +1938,11 @@ function matches$9(configRoutes, request) {
1992
1938
  * schema:
1993
1939
  * $ref: '#/components/schemas/APIError'
1994
1940
  */
1995
- function GET$1(_x, _x2, _x3) {
1996
- return _GET$1.apply(this, arguments);
1941
+ function GET$2(_x, _x2, _x3) {
1942
+ return _GET$2.apply(this, arguments);
1997
1943
  }
1998
- function _GET$1() {
1999
- _GET$1 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, init, log) {
1944
+ function _GET$2() {
1945
+ _GET$2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, init, log) {
2000
1946
  var yurl, _yurl$pathname$split$, tenantId, url;
2001
1947
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2002
1948
  while (1) switch (_context.prev = _context.next) {
@@ -2023,7 +1969,7 @@ function _GET$1() {
2023
1969
  }
2024
1970
  }, _callee);
2025
1971
  }));
2026
- return _GET$1.apply(this, arguments);
1972
+ return _GET$2.apply(this, arguments);
2027
1973
  }
2028
1974
 
2029
1975
  /**
@@ -2070,7 +2016,7 @@ function POST$1(_x, _x2, _x3, _x4) {
2070
2016
  return _POST$1.apply(this, arguments);
2071
2017
  }
2072
2018
  function _POST$1() {
2073
- _POST$1 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, session, init, log) {
2019
+ _POST$1 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, session, init, log) {
2074
2020
  var yurl, _yurl$pathname$split$, tenantId, url;
2075
2021
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2076
2022
  while (1) switch (_context.prev = _context.next) {
@@ -2127,7 +2073,7 @@ function DELETE$1(_x, _x2, _x3) {
2127
2073
  return _DELETE$1.apply(this, arguments);
2128
2074
  }
2129
2075
  function _DELETE$1() {
2130
- _DELETE$1 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, init, log) {
2076
+ _DELETE$1 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, init, log) {
2131
2077
  var yurl, _yurl$pathname$split$, userId, tenantId, url;
2132
2078
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2133
2079
  while (1) switch (_context.prev = _context.next) {
@@ -2186,11 +2132,11 @@ function _DELETE$1() {
2186
2132
  * "201":
2187
2133
  * description: add user to tenant
2188
2134
  */
2189
- function PUT(_x, _x2, _x3) {
2190
- return _PUT.apply(this, arguments);
2135
+ function PUT$1(_x, _x2, _x3) {
2136
+ return _PUT$1.apply(this, arguments);
2191
2137
  }
2192
- function _PUT() {
2193
- _PUT = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, init, log) {
2138
+ function _PUT$1() {
2139
+ _PUT$1 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, init, log) {
2194
2140
  var yurl, _yurl$pathname$split$, tenantId, url;
2195
2141
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2196
2142
  while (1) switch (_context.prev = _context.next) {
@@ -2218,7 +2164,7 @@ function _PUT() {
2218
2164
  }
2219
2165
  }, _callee);
2220
2166
  }));
2221
- return _PUT.apply(this, arguments);
2167
+ return _PUT$1.apply(this, arguments);
2222
2168
  }
2223
2169
 
2224
2170
  var key$5 = 'TENANT_USERS';
@@ -2226,7 +2172,7 @@ function route$8(_x, _x2) {
2226
2172
  return _route$8.apply(this, arguments);
2227
2173
  }
2228
2174
  function _route$8() {
2229
- _route$8 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(request, config) {
2175
+ _route$8 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(request, config) {
2230
2176
  var _Logger, info, session;
2231
2177
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2232
2178
  while (1) switch (_context.prev = _context.next) {
@@ -2252,7 +2198,7 @@ function _route$8() {
2252
2198
  break;
2253
2199
  case 10:
2254
2200
  _context.next = 12;
2255
- return GET$1(config, {
2201
+ return GET$2(config, {
2256
2202
  request: request
2257
2203
  }, info);
2258
2204
  case 12:
@@ -2266,7 +2212,7 @@ function _route$8() {
2266
2212
  return _context.abrupt("return", _context.sent);
2267
2213
  case 16:
2268
2214
  _context.next = 18;
2269
- return PUT(config, {
2215
+ return PUT$1(config, {
2270
2216
  request: request
2271
2217
  }, info);
2272
2218
  case 18:
@@ -2331,11 +2277,11 @@ function matches$8(configRoutes, request) {
2331
2277
  * description: Unauthorized
2332
2278
  * content: {}
2333
2279
  */
2334
- function GET(_x, _x2, _x3, _x4) {
2335
- return _GET.apply(this, arguments);
2280
+ function GET$1(_x, _x2, _x3, _x4) {
2281
+ return _GET$1.apply(this, arguments);
2336
2282
  }
2337
- function _GET() {
2338
- _GET = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, session, init, log) {
2283
+ function _GET$1() {
2284
+ _GET$1 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, session, init, log) {
2339
2285
  var url, res;
2340
2286
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2341
2287
  while (1) switch (_context.prev = _context.next) {
@@ -2356,6 +2302,74 @@ function _GET() {
2356
2302
  }
2357
2303
  }, _callee);
2358
2304
  }));
2305
+ return _GET$1.apply(this, arguments);
2306
+ }
2307
+
2308
+ /**
2309
+ * @swagger
2310
+ * /api/tenants/{tenantId}:
2311
+ * get:
2312
+ * tags:
2313
+ * - tenants
2314
+ * summary: Obtains a specific tenant.
2315
+ * operationId: getTenant
2316
+ * parameters:
2317
+ * - name: tenantId
2318
+ * in: path
2319
+ * required: true
2320
+ * schema:
2321
+ * type: string
2322
+ * responses:
2323
+ * "200":
2324
+ * description: the desired tenant
2325
+ * application/json:
2326
+ * schema:
2327
+ * $ref: '#/components/schemas/Tenant'
2328
+ * "401":
2329
+ * description: Unauthorized
2330
+ * content:
2331
+ * application/json:
2332
+ * schema:
2333
+ * $ref: '#/components/schemas/APIError'
2334
+ * "404":
2335
+ * description: Tenant not found
2336
+ * content:
2337
+ * application/json:
2338
+ * schema:
2339
+ * $ref: '#/components/schemas/APIError'
2340
+ */
2341
+ function GET(_x, _x2, _x3) {
2342
+ return _GET.apply(this, arguments);
2343
+ }
2344
+ function _GET() {
2345
+ _GET = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, init, log) {
2346
+ var yurl, _yurl$pathname$split$, tenantId, url;
2347
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
2348
+ while (1) switch (_context.prev = _context.next) {
2349
+ case 0:
2350
+ yurl = new URL(init.request.url);
2351
+ _yurl$pathname$split$ = yurl.pathname.split('/').reverse(), tenantId = _yurl$pathname$split$[0];
2352
+ if (tenantId) {
2353
+ _context.next = 4;
2354
+ break;
2355
+ }
2356
+ return _context.abrupt("return", new Response(null, {
2357
+ status: 404
2358
+ }));
2359
+ case 4:
2360
+ init.method = 'GET';
2361
+ url = "" + apiRoutes(config).TENANT(tenantId);
2362
+ log('[GET]', url);
2363
+ _context.next = 9;
2364
+ return request(url, init, config);
2365
+ case 9:
2366
+ return _context.abrupt("return", _context.sent);
2367
+ case 10:
2368
+ case "end":
2369
+ return _context.stop();
2370
+ }
2371
+ }, _callee);
2372
+ }));
2359
2373
  return _GET.apply(this, arguments);
2360
2374
  }
2361
2375
 
@@ -2393,7 +2407,7 @@ function DELETE(_x, _x2, _x3) {
2393
2407
  return _DELETE.apply(this, arguments);
2394
2408
  }
2395
2409
  function _DELETE() {
2396
- _DELETE = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, init, log) {
2410
+ _DELETE = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, init, log) {
2397
2411
  var yurl, _yurl$pathname$split$, tenantId, url;
2398
2412
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2399
2413
  while (1) switch (_context.prev = _context.next) {
@@ -2424,6 +2438,76 @@ function _DELETE() {
2424
2438
  return _DELETE.apply(this, arguments);
2425
2439
  }
2426
2440
 
2441
+ /**
2442
+ * @swagger
2443
+ * /api/tenants/{tenantId}:
2444
+ * put:
2445
+ * tags:
2446
+ * - tenants
2447
+ * summary: Obtains a specific tenant.
2448
+ * operationId: updateTenant
2449
+ * parameters:
2450
+ * - name: tenantId
2451
+ * in: path
2452
+ * required: true
2453
+ * schema:
2454
+ * type: string
2455
+ * responses:
2456
+ * "201":
2457
+ * description: update an existing tenant
2458
+ * content:
2459
+ * application/json:
2460
+ * schema:
2461
+ * $ref: '#/components/schemas/Tenant'
2462
+ * "401":
2463
+ * description: Unauthorized
2464
+ * content:
2465
+ * application/json:
2466
+ * schema:
2467
+ * $ref: '#/components/schemas/APIError'
2468
+ * "404":
2469
+ * description: Tenant not found
2470
+ * content:
2471
+ * application/json:
2472
+ * schema:
2473
+ * $ref: '#/components/schemas/APIError'
2474
+ */
2475
+ function PUT(_x, _x2, _x3) {
2476
+ return _PUT.apply(this, arguments);
2477
+ }
2478
+ function _PUT() {
2479
+ _PUT = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, init, log) {
2480
+ var yurl, _yurl$pathname$split$, tenantId, url;
2481
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
2482
+ while (1) switch (_context.prev = _context.next) {
2483
+ case 0:
2484
+ yurl = new URL(init.request.url);
2485
+ _yurl$pathname$split$ = yurl.pathname.split('/').reverse(), tenantId = _yurl$pathname$split$[0];
2486
+ if (tenantId) {
2487
+ _context.next = 4;
2488
+ break;
2489
+ }
2490
+ return _context.abrupt("return", new Response(null, {
2491
+ status: 404
2492
+ }));
2493
+ case 4:
2494
+ init.body = init.request.body;
2495
+ init.method = 'PUT';
2496
+ url = "" + apiRoutes(config).TENANT(tenantId);
2497
+ log('[PUT]', url);
2498
+ _context.next = 10;
2499
+ return request(url, init, config);
2500
+ case 10:
2501
+ return _context.abrupt("return", _context.sent);
2502
+ case 11:
2503
+ case "end":
2504
+ return _context.stop();
2505
+ }
2506
+ }, _callee);
2507
+ }));
2508
+ return _PUT.apply(this, arguments);
2509
+ }
2510
+
2427
2511
  /**
2428
2512
  * @swagger
2429
2513
  * /api/tenants:
@@ -2469,7 +2553,7 @@ function POST(_x, _x2, _x3) {
2469
2553
  return _POST.apply(this, arguments);
2470
2554
  }
2471
2555
  function _POST() {
2472
- _POST = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, init, log) {
2556
+ _POST = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, init, log) {
2473
2557
  var url;
2474
2558
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2475
2559
  while (1) switch (_context.prev = _context.next) {
@@ -2491,13 +2575,21 @@ function _POST() {
2491
2575
  return _POST.apply(this, arguments);
2492
2576
  }
2493
2577
 
2578
+ function isUUID(value) {
2579
+ if (!value) {
2580
+ return false;
2581
+ }
2582
+ // is any UUID
2583
+ var regex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5|7][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
2584
+ return regex.test(value);
2585
+ }
2494
2586
  var key$4 = 'TENANTS';
2495
2587
  function route$7(_x, _x2) {
2496
2588
  return _route$7.apply(this, arguments);
2497
2589
  }
2498
2590
  function _route$7() {
2499
- _route$7 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(request, config) {
2500
- var _Logger, info, session;
2591
+ _route$7 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(request, config) {
2592
+ var _Logger, info, session, _request$url$split$re, possibleTenantId;
2501
2593
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2502
2594
  while (1) switch (_context.prev = _context.next) {
2503
2595
  case 0:
@@ -2517,35 +2609,54 @@ function _route$7() {
2517
2609
  status: 401
2518
2610
  }));
2519
2611
  case 7:
2612
+ _request$url$split$re = request.url.split('/').reverse(), possibleTenantId = _request$url$split$re[0];
2520
2613
  _context.t0 = request.method;
2521
- _context.next = _context.t0 === 'GET' ? 10 : _context.t0 === 'POST' ? 13 : _context.t0 === 'DELETE' ? 16 : 19;
2614
+ _context.next = _context.t0 === 'GET' ? 11 : _context.t0 === 'POST' ? 18 : _context.t0 === 'DELETE' ? 21 : _context.t0 === 'PUT' ? 24 : 27;
2522
2615
  break;
2523
- case 10:
2524
- _context.next = 12;
2525
- return GET(config, session, {
2616
+ case 11:
2617
+ if (!isUUID(possibleTenantId)) {
2618
+ _context.next = 15;
2619
+ break;
2620
+ }
2621
+ _context.next = 14;
2622
+ return GET(config, {
2526
2623
  request: request
2527
2624
  }, info);
2528
- case 12:
2625
+ case 14:
2529
2626
  return _context.abrupt("return", _context.sent);
2530
- case 13:
2531
- _context.next = 15;
2627
+ case 15:
2628
+ _context.next = 17;
2629
+ return GET$1(config, session, {
2630
+ request: request
2631
+ }, info);
2632
+ case 17:
2633
+ return _context.abrupt("return", _context.sent);
2634
+ case 18:
2635
+ _context.next = 20;
2532
2636
  return POST(config, {
2533
2637
  request: request
2534
2638
  }, info);
2535
- case 15:
2639
+ case 20:
2536
2640
  return _context.abrupt("return", _context.sent);
2537
- case 16:
2538
- _context.next = 18;
2641
+ case 21:
2642
+ _context.next = 23;
2539
2643
  return DELETE(config, {
2540
2644
  request: request
2541
2645
  }, info);
2542
- case 18:
2646
+ case 23:
2543
2647
  return _context.abrupt("return", _context.sent);
2544
- case 19:
2648
+ case 24:
2649
+ _context.next = 26;
2650
+ return PUT(config, {
2651
+ request: request
2652
+ }, info);
2653
+ case 26:
2654
+ return _context.abrupt("return", _context.sent);
2655
+ case 27:
2545
2656
  return _context.abrupt("return", new Response('method not allowed', {
2546
2657
  status: 405
2547
2658
  }));
2548
- case 20:
2659
+ case 28:
2549
2660
  case "end":
2550
2661
  return _context.stop();
2551
2662
  }
@@ -2574,7 +2685,7 @@ function route$6(_x, _x2) {
2574
2685
  return _route$6.apply(this, arguments);
2575
2686
  }
2576
2687
  function _route$6() {
2577
- _route$6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, config) {
2688
+ _route$6 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, config) {
2578
2689
  var url, init, _URL$pathname$split$r, provider, passThroughUrl, params, res;
2579
2690
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2580
2691
  while (1) switch (_context.prev = _context.next) {
@@ -2617,7 +2728,7 @@ function route$5(_x, _x2) {
2617
2728
  return _route$5.apply(this, arguments);
2618
2729
  }
2619
2730
  function _route$5() {
2620
- _route$5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, config) {
2731
+ _route$5 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, config) {
2621
2732
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2622
2733
  while (1) switch (_context.prev = _context.next) {
2623
2734
  case 0:
@@ -2641,7 +2752,7 @@ function route$4(_x, _x2) {
2641
2752
  return _route$4.apply(this, arguments);
2642
2753
  }
2643
2754
  function _route$4() {
2644
- _route$4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, config) {
2755
+ _route$4 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, config) {
2645
2756
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2646
2757
  while (1) switch (_context.prev = _context.next) {
2647
2758
  case 0:
@@ -2665,7 +2776,7 @@ function route$3(_x, _x2) {
2665
2776
  return _route$3.apply(this, arguments);
2666
2777
  }
2667
2778
  function _route$3() {
2668
- _route$3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, config) {
2779
+ _route$3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, config) {
2669
2780
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2670
2781
  while (1) switch (_context.prev = _context.next) {
2671
2782
  case 0:
@@ -2690,7 +2801,7 @@ function route$2(_x, _x2) {
2690
2801
  return _route$2.apply(this, arguments);
2691
2802
  }
2692
2803
  function _route$2() {
2693
- _route$2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(request$1, config) {
2804
+ _route$2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(request$1, config) {
2694
2805
  var _Logger, error, _URL$pathname$split$r, provider, passThroughUrl, params, url, res, location;
2695
2806
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2696
2807
  while (1) switch (_context.prev = _context.next) {
@@ -2742,7 +2853,7 @@ function route$1(_x, _x2) {
2742
2853
  return _route$1.apply(this, arguments);
2743
2854
  }
2744
2855
  function _route$1() {
2745
- _route$1 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(request$1, config) {
2856
+ _route$1 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(request$1, config) {
2746
2857
  var url, init, _URL$pathname$split$r, provider, res;
2747
2858
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2748
2859
  while (1) switch (_context.prev = _context.next) {
@@ -2780,7 +2891,7 @@ function route(_x, _x2) {
2780
2891
  return _route.apply(this, arguments);
2781
2892
  }
2782
2893
  function _route() {
2783
- _route = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, config) {
2894
+ _route = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, config) {
2784
2895
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2785
2896
  while (1) switch (_context.prev = _context.next) {
2786
2897
  case 0:
@@ -2805,7 +2916,7 @@ function GETTER(configRoutes, config) {
2805
2916
  info = _Logger.info,
2806
2917
  warn = _Logger.warn;
2807
2918
  return /*#__PURE__*/function () {
2808
- var _GET = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
2919
+ var _GET = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
2809
2920
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2810
2921
  while (1) switch (_context.prev = _context.next) {
2811
2922
  case 0:
@@ -2908,7 +3019,7 @@ function POSTER(configRoutes, config) {
2908
3019
  info = _Logger.info,
2909
3020
  warn = _Logger.warn;
2910
3021
  return /*#__PURE__*/function () {
2911
- var _POST = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
3022
+ var _POST = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
2912
3023
  return _regeneratorRuntime().wrap(function _callee$(_context) {
2913
3024
  while (1) switch (_context.prev = _context.next) {
2914
3025
  case 0:
@@ -2997,7 +3108,7 @@ function DELETER(configRoutes, config) {
2997
3108
  info = _Logger.info,
2998
3109
  warn = _Logger.warn;
2999
3110
  return /*#__PURE__*/function () {
3000
- var _DELETE = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
3111
+ var _DELETE = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
3001
3112
  return _regeneratorRuntime().wrap(function _callee$(_context) {
3002
3113
  while (1) switch (_context.prev = _context.next) {
3003
3114
  case 0:
@@ -3037,7 +3148,7 @@ function PUTER(configRoutes, config) {
3037
3148
  info = _Logger.info,
3038
3149
  warn = _Logger.warn;
3039
3150
  return /*#__PURE__*/function () {
3040
- var _PUT = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
3151
+ var _PUT = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(req) {
3041
3152
  return _regeneratorRuntime().wrap(function _callee$(_context) {
3042
3153
  while (1) switch (_context.prev = _context.next) {
3043
3154
  case 0:
@@ -3124,7 +3235,7 @@ function serverAuth(config, handlers) {
3124
3235
  error = _Logger.error;
3125
3236
  var routes = appRoutes(config.routePrefix);
3126
3237
  return /*#__PURE__*/function () {
3127
- var _login = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
3238
+ var _login = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
3128
3239
  var _providers, _exec;
3129
3240
  var email, password, sessionUrl, sessionReq, sessionRes, providers, csrf, csrfReq, csrfRes, csrfToken, _yield$csrfRes$json, json, _ref2, credentials, csrfCookie, signInUrl, postReq, loginRes, authCookie, _ref3, token;
3130
3241
  return _regeneratorRuntime().wrap(function _callee$(_context) {
@@ -3276,7 +3387,7 @@ var Requester = /*#__PURE__*/function (_Config) {
3276
3387
  _inheritsLoose(Requester, _Config);
3277
3388
  var _proto = Requester.prototype;
3278
3389
  _proto.rawRequest = /*#__PURE__*/function () {
3279
- var _rawRequest = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(method, url, init, body) {
3390
+ var _rawRequest = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(method, url, init, body) {
3280
3391
  var _init, res;
3281
3392
  return _regeneratorRuntime().wrap(function _callee$(_context) {
3282
3393
  while (1) switch (_context.prev = _context.next) {
@@ -3322,7 +3433,7 @@ var Requester = /*#__PURE__*/function (_Config) {
3322
3433
  _proto.request =
3323
3434
  /*#__PURE__*/
3324
3435
  function () {
3325
- var _request = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(method, url, req, init) {
3436
+ var _request = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(method, url, req, init) {
3326
3437
  var headers, tenantId, cookie, _headers, _tenantId, _cookie, body, _init;
3327
3438
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
3328
3439
  while (1) switch (_context2.prev = _context2.next) {
@@ -3396,7 +3507,7 @@ var Requester = /*#__PURE__*/function (_Config) {
3396
3507
  return request;
3397
3508
  }();
3398
3509
  _proto.post = /*#__PURE__*/function () {
3399
- var _post = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(req, url, init) {
3510
+ var _post = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(req, url, init) {
3400
3511
  var response;
3401
3512
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
3402
3513
  while (1) switch (_context3.prev = _context3.next) {
@@ -3424,7 +3535,7 @@ var Requester = /*#__PURE__*/function (_Config) {
3424
3535
  return post;
3425
3536
  }();
3426
3537
  _proto.get = /*#__PURE__*/function () {
3427
- var _get = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(req, url, init) {
3538
+ var _get = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(req, url, init) {
3428
3539
  var response;
3429
3540
  return _regeneratorRuntime().wrap(function _callee4$(_context4) {
3430
3541
  while (1) switch (_context4.prev = _context4.next) {
@@ -3452,7 +3563,7 @@ var Requester = /*#__PURE__*/function (_Config) {
3452
3563
  return get;
3453
3564
  }();
3454
3565
  _proto.put = /*#__PURE__*/function () {
3455
- var _put = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(req, url, init) {
3566
+ var _put = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(req, url, init) {
3456
3567
  var response;
3457
3568
  return _regeneratorRuntime().wrap(function _callee5$(_context5) {
3458
3569
  while (1) switch (_context5.prev = _context5.next) {
@@ -3480,7 +3591,7 @@ var Requester = /*#__PURE__*/function (_Config) {
3480
3591
  return put;
3481
3592
  }();
3482
3593
  _proto["delete"] = /*#__PURE__*/function () {
3483
- var _delete2 = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(req, url, init) {
3594
+ var _delete2 = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(req, url, init) {
3484
3595
  var response;
3485
3596
  return _regeneratorRuntime().wrap(function _callee6$(_context6) {
3486
3597
  while (1) switch (_context6.prev = _context6.next) {
@@ -3516,7 +3627,7 @@ var Tenants = /*#__PURE__*/function (_Config) {
3516
3627
  _this = _Config.call(this, config) || this;
3517
3628
  _this.headers = void 0;
3518
3629
  _this.createTenant = /*#__PURE__*/function () {
3519
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, init) {
3630
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, init) {
3520
3631
  var _req, _requester, _init;
3521
3632
  return _regeneratorRuntime().wrap(function _callee$(_context) {
3522
3633
  while (1) switch (_context.prev = _context.next) {
@@ -3545,7 +3656,7 @@ var Tenants = /*#__PURE__*/function (_Config) {
3545
3656
  };
3546
3657
  }();
3547
3658
  _this.getTenant = /*#__PURE__*/function () {
3548
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(req, init) {
3659
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(req, init) {
3549
3660
  var _requester, _init;
3550
3661
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
3551
3662
  while (1) switch (_context2.prev = _context2.next) {
@@ -3567,7 +3678,7 @@ var Tenants = /*#__PURE__*/function (_Config) {
3567
3678
  };
3568
3679
  }();
3569
3680
  _this.listTenants = /*#__PURE__*/function () {
3570
- var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(req, init) {
3681
+ var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(req, init) {
3571
3682
  var _requester, _init;
3572
3683
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
3573
3684
  while (1) switch (_context3.prev = _context3.next) {
@@ -3631,7 +3742,7 @@ var Users = /*#__PURE__*/function (_Config) {
3631
3742
  _this = _Config.call(this, config) || this;
3632
3743
  _this.headers = void 0;
3633
3744
  _this.createUser = /*#__PURE__*/function () {
3634
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, init) {
3745
+ var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(req, init) {
3635
3746
  var _requester, _init;
3636
3747
  return _regeneratorRuntime().wrap(function _callee$(_context) {
3637
3748
  while (1) switch (_context.prev = _context.next) {
@@ -3653,7 +3764,7 @@ var Users = /*#__PURE__*/function (_Config) {
3653
3764
  };
3654
3765
  }();
3655
3766
  _this.updateUser = /*#__PURE__*/function () {
3656
- var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(userId, req, init) {
3767
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(userId, req, init) {
3657
3768
  var _requester, _init;
3658
3769
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
3659
3770
  while (1) switch (_context2.prev = _context2.next) {
@@ -3675,7 +3786,7 @@ var Users = /*#__PURE__*/function (_Config) {
3675
3786
  };
3676
3787
  }();
3677
3788
  _this.listUsers = /*#__PURE__*/function () {
3678
- var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(req, init) {
3789
+ var _ref3 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(req, init) {
3679
3790
  var _requester, _init;
3680
3791
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
3681
3792
  while (1) switch (_context3.prev = _context3.next) {
@@ -3697,7 +3808,7 @@ var Users = /*#__PURE__*/function (_Config) {
3697
3808
  };
3698
3809
  }();
3699
3810
  _this.linkUser = /*#__PURE__*/function () {
3700
- var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4(req, init) {
3811
+ var _ref4 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(req, init) {
3701
3812
  var _requester, _init;
3702
3813
  return _regeneratorRuntime().wrap(function _callee4$(_context4) {
3703
3814
  while (1) switch (_context4.prev = _context4.next) {
@@ -3723,7 +3834,7 @@ var Users = /*#__PURE__*/function (_Config) {
3723
3834
  return "/tenants/" + ((_this$tenantId = _this.tenantId) != null ? _this$tenantId : '{tenantId}') + "/users/" + (userId != null ? userId : '{userId}');
3724
3835
  };
3725
3836
  _this.getUserId = /*#__PURE__*/function () {
3726
- var _ref5 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee5(req) {
3837
+ var _ref5 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(req) {
3727
3838
  var body;
3728
3839
  return _regeneratorRuntime().wrap(function _callee5$(_context5) {
3729
3840
  while (1) switch (_context5.prev = _context5.next) {
@@ -3760,7 +3871,7 @@ var Users = /*#__PURE__*/function (_Config) {
3760
3871
  };
3761
3872
  }();
3762
3873
  _this.unlinkUser = /*#__PURE__*/function () {
3763
- var _ref6 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee6(req, init) {
3874
+ var _ref6 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(req, init) {
3764
3875
  var _requester, userId, _init;
3765
3876
  return _regeneratorRuntime().wrap(function _callee6$(_context6) {
3766
3877
  while (1) switch (_context6.prev = _context6.next) {
@@ -3786,7 +3897,7 @@ var Users = /*#__PURE__*/function (_Config) {
3786
3897
  };
3787
3898
  }();
3788
3899
  _this.me = /*#__PURE__*/function () {
3789
- var _ref7 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee7(req, init) {
3900
+ var _ref7 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(req, init) {
3790
3901
  var _requester, _init;
3791
3902
  return _regeneratorRuntime().wrap(function _callee7$(_context7) {
3792
3903
  while (1) switch (_context7.prev = _context7.next) {
@@ -3861,7 +3972,7 @@ var Api = /*#__PURE__*/function () {
3861
3972
  }
3862
3973
  var _proto = Api.prototype;
3863
3974
  _proto.login = /*#__PURE__*/function () {
3864
- var _login = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(payload) {
3975
+ var _login = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(payload) {
3865
3976
  return _regeneratorRuntime().wrap(function _callee$(_context) {
3866
3977
  while (1) switch (_context.prev = _context.next) {
3867
3978
  case 0:
@@ -3895,9 +4006,7 @@ var Server = /*#__PURE__*/function () {
3895
4006
  this.config = void 0;
3896
4007
  this.api = void 0;
3897
4008
  this.manager = void 0;
3898
- this.servers = void 0;
3899
4009
  this.config = new Config(config, '[initial config]');
3900
- this.servers = new Map();
3901
4010
  this.api = new Api(this.config);
3902
4011
  this.manager = new DBManager(this.config);
3903
4012
  watchTenantId(function (tenantId) {
@@ -3915,7 +4024,7 @@ var Server = /*#__PURE__*/function () {
3915
4024
  this.config = new Config(cfg);
3916
4025
  };
3917
4026
  _proto.init = /*#__PURE__*/function () {
3918
- var _init = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(cfg) {
4027
+ var _init = /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(cfg) {
3919
4028
  var updatedConfig;
3920
4029
  return _regeneratorRuntime().wrap(function _callee$(_context) {
3921
4030
  while (1) switch (_context.prev = _context.next) {
@@ -3925,11 +4034,9 @@ var Server = /*#__PURE__*/function () {
3925
4034
  case 2:
3926
4035
  updatedConfig = _context.sent;
3927
4036
  this.setConfig(updatedConfig);
3928
- this.manager.clear(this.config);
3929
- this.manager = new DBManager(this.config);
3930
4037
  this.api = new Api(this.config);
3931
4038
  return _context.abrupt("return", this);
3932
- case 8:
4039
+ case 6:
3933
4040
  case "end":
3934
4041
  return _context.stop();
3935
4042
  }
@@ -3941,32 +4048,19 @@ var Server = /*#__PURE__*/function () {
3941
4048
  return init;
3942
4049
  }();
3943
4050
  /**
3944
- * A utility function if you want to manage different NileDB instances yourself
3945
- * returns the global Server object, an existing server that's already been configured,
3946
- * or a new one if the config isn't in the cache
4051
+ * A convenience function that applies a config and ensures whatever was passed is set properly
3947
4052
  */
3948
4053
  _proto.getInstance = function getInstance(config) {
3949
4054
  var _config = _extends({}, this.config, config);
3950
- var serverId = getServerId(_config);
3951
- var currentServerId = makeServerId(this.config);
3952
- if (serverId === currentServerId) {
3953
- return this;
3954
- }
3955
- var existing = this.servers.get(serverId);
3956
- if (existing) {
3957
- // be sure the config is up to date
3958
- var updatedConfig = new Config(_config);
3959
- existing.setConfig(updatedConfig);
3960
- // propagate special config items
3961
- existing.tenantId = updatedConfig.tenantId;
3962
- existing.userId = updatedConfig.userId;
3963
- existing.token = updatedConfig.api.token;
3964
- existing.databaseId = updatedConfig.databaseId;
3965
- return existing;
3966
- }
3967
- var newServer = new Server(_config);
3968
- this.servers.set(serverId, newServer);
3969
- return newServer;
4055
+ // be sure the config is up to date
4056
+ var updatedConfig = new Config(_config);
4057
+ this.setConfig(updatedConfig);
4058
+ // propagate special config items
4059
+ this.tenantId = updatedConfig.tenantId;
4060
+ this.userId = updatedConfig.userId;
4061
+ this.token = updatedConfig.api.token;
4062
+ this.databaseId = updatedConfig.databaseId;
4063
+ return this;
3970
4064
  };
3971
4065
  return _createClass(Server, [{
3972
4066
  key: "databaseId",
@@ -4025,16 +4119,18 @@ var Server = /*#__PURE__*/function () {
4025
4119
  }
4026
4120
  }]);
4027
4121
  }();
4122
+ var server;
4028
4123
  function create(_x2) {
4029
4124
  return _create.apply(this, arguments);
4030
4125
  }
4031
4126
  function _create() {
4032
- _create = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(config) {
4033
- var server;
4127
+ _create = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(config) {
4034
4128
  return _regeneratorRuntime().wrap(function _callee2$(_context2) {
4035
4129
  while (1) switch (_context2.prev = _context2.next) {
4036
4130
  case 0:
4037
- server = new Server(config);
4131
+ if (!server) {
4132
+ server = new Server(config);
4133
+ }
4038
4134
  _context2.next = 3;
4039
4135
  return server.init();
4040
4136
  case 3: