@niledatabase/server 3.0.0-alpha.23 → 3.0.0-alpha.24

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.
@@ -737,14 +737,22 @@ var Config = /*#__PURE__*/function () {
737
737
  config: config
738
738
  })
739
739
  }
740
+ })["catch"](function () {
741
+ error("Unable to auto-configure. is " + url + " available?");
740
742
  });
741
743
  case 19:
742
744
  res = _context.sent;
745
+ if (res) {
746
+ _context.next = 22;
747
+ break;
748
+ }
749
+ return _context.abrupt("return", _this);
750
+ case 22:
743
751
  possibleError = res.clone();
744
- _context.prev = 21;
745
- _context.next = 24;
752
+ _context.prev = 23;
753
+ _context.next = 26;
746
754
  return res.json();
747
- case 24:
755
+ case 26:
748
756
  json = _context.sent;
749
757
  if (res.status === 404) {
750
758
  info('is the configured databaseName correct?');
@@ -756,35 +764,35 @@ var Config = /*#__PURE__*/function () {
756
764
  } else {
757
765
  database = json;
758
766
  }
759
- _context.next = 37;
767
+ _context.next = 39;
760
768
  break;
761
- case 29:
762
- _context.prev = 29;
763
- _context.t0 = _context["catch"](21);
764
- _context.next = 33;
769
+ case 31:
770
+ _context.prev = 31;
771
+ _context.t0 = _context["catch"](23);
772
+ _context.next = 35;
765
773
  return possibleError.text();
766
- case 33:
774
+ case 35:
767
775
  message = _context.sent;
768
776
  debug('Unable to auto-configure');
769
777
  error(message);
770
778
  database = {
771
779
  message: message
772
780
  };
773
- case 37:
781
+ case 39:
774
782
  info('[fetched database]', database);
775
783
  if (!('message' in database)) {
776
- _context.next = 46;
784
+ _context.next = 48;
777
785
  break;
778
786
  }
779
787
  if (!('statusCode' in database)) {
780
- _context.next = 45;
788
+ _context.next = 47;
781
789
  break;
782
790
  }
783
791
  error(database);
784
792
  throw new Error('HTTP error has occurred');
785
- case 45:
793
+ case 47:
786
794
  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.');
787
- case 46:
795
+ case 48:
788
796
  if (typeof database === 'object') {
789
797
  _database = database, apiHost = _database.apiHost, dbHost = _database.dbHost, name = _database.name, id = _database.id;
790
798
  basePath = basePath || apiHost;
@@ -793,7 +801,7 @@ var Config = /*#__PURE__*/function () {
793
801
  dburl = new URL(dbHost);
794
802
  configuredHost = dburl.host;
795
803
  }
796
- case 47:
804
+ case 49:
797
805
  _this.api = new ApiConfig({
798
806
  basePath: basePath,
799
807
  cookieKey: (_config$api$cookieKey2 = config == null || (_config$api2 = config.api) == null ? void 0 : _config$api2.cookieKey) != null ? _config$api$cookieKey2 : 'token',
@@ -813,11 +821,11 @@ var Config = /*#__PURE__*/function () {
813
821
  api: _this.api
814
822
  });
815
823
  return _context.abrupt("return", _this);
816
- case 51:
824
+ case 53:
817
825
  case "end":
818
826
  return _context.stop();
819
827
  }
820
- }, _callee, null, [[21, 29]]);
828
+ }, _callee, null, [[23, 31]]);
821
829
  }));
822
830
  return function (_x) {
823
831
  return _ref2.apply(this, arguments);
@@ -1249,8 +1257,7 @@ function request(_x, _x2, _x3) {
1249
1257
  }
1250
1258
  function _request() {
1251
1259
  _request = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(url, _init, config) {
1252
- var _params$method;
1253
- var _Logger, info, error, request, init, requestUrl, updatedHeaders, params, _init$body, res, loggingRes;
1260
+ var _Logger, info, error, request, init, requestUrl, updatedHeaders, params, _init$body, _params$method, res, loggingRes;
1254
1261
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1255
1262
  while (1) switch (_context.prev = _context.next) {
1256
1263
  case 0:
@@ -1268,7 +1275,8 @@ function _request() {
1268
1275
  // @ts-expect-error - its there
1269
1276
  params.duplex = 'half';
1270
1277
  }
1271
- _context.next = 10;
1278
+ _context.prev = 8;
1279
+ _context.next = 11;
1272
1280
  return fetch(url, _extends({}, params))["catch"](function (e) {
1273
1281
  error('An error has occurred in the fetch', {
1274
1282
  message: e.message,
@@ -1278,16 +1286,16 @@ function _request() {
1278
1286
  status: 500
1279
1287
  });
1280
1288
  });
1281
- case 10:
1289
+ case 11:
1282
1290
  res = _context.sent;
1283
1291
  loggingRes = typeof (res == null ? void 0 : res.clone) === 'function' ? res == null ? void 0 : res.clone() : null;
1284
1292
  _context.t0 = info;
1285
1293
  _context.t1 = "[" + ((_params$method = params.method) != null ? _params$method : 'GET') + "] " + url;
1286
1294
  _context.t2 = res == null ? void 0 : res.status;
1287
1295
  _context.t3 = res == null ? void 0 : res.statusText;
1288
- _context.next = 18;
1296
+ _context.next = 19;
1289
1297
  return loggingRes == null ? void 0 : loggingRes.text();
1290
- case 18:
1298
+ case 19:
1291
1299
  _context.t4 = _context.sent;
1292
1300
  _context.t5 = {
1293
1301
  status: _context.t2,
@@ -1296,11 +1304,23 @@ function _request() {
1296
1304
  };
1297
1305
  (0, _context.t0)(_context.t1, _context.t5);
1298
1306
  return _context.abrupt("return", res);
1299
- case 22:
1307
+ case 25:
1308
+ _context.prev = 25;
1309
+ _context.t6 = _context["catch"](8);
1310
+ if (_context.t6 instanceof Error) {
1311
+ error('An error has occurred in the fetch', {
1312
+ message: _context.t6.message,
1313
+ stack: _context.t6.stack
1314
+ });
1315
+ }
1316
+ return _context.abrupt("return", new Response('An unexpected (most likely configuration) problem has occurred', {
1317
+ status: 500
1318
+ }));
1319
+ case 29:
1300
1320
  case "end":
1301
1321
  return _context.stop();
1302
1322
  }
1303
- }, _callee);
1323
+ }, _callee, null, [[8, 25]]);
1304
1324
  }));
1305
1325
  return _request.apply(this, arguments);
1306
1326
  }
@@ -1493,8 +1513,8 @@ function _fetch(_x, _x2, _x3) {
1493
1513
  }
1494
1514
  function _fetch2() {
1495
1515
  _fetch2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(config, path, opts) {
1496
- var _config$api, _config$api2, _response$status;
1497
- var _Logger, debug, error, url, cookieKey, headers, basicHeaders, authHeader, token, tenantId, userId, useableUrl, response, _opts$method, _opts$method2, res, errorHandler, msg, _res, message, _res2, _res2$errors, _message;
1516
+ var _config$api, _config$api2;
1517
+ var _Logger, debug, error, url, cookieKey, headers, basicHeaders, authHeader, token, tenantId, userId, useableUrl, _response$status, response, _opts$method, _opts$method2, res, errorHandler, msg, _res, message, _res2, _res2$errors, _message;
1498
1518
  return _regeneratorRuntime().wrap(function _callee$(_context) {
1499
1519
  while (1) switch (_context.prev = _context.next) {
1500
1520
  case 0:
@@ -1532,7 +1552,8 @@ function _fetch2() {
1532
1552
  case 14:
1533
1553
  useableUrl = url.replace('{tenantId}', encodeURIComponent(String(tenantId))).replace('{userId}', encodeURIComponent(String(userId)));
1534
1554
  debug("[fetch] " + useableUrl);
1535
- _context.next = 18;
1555
+ _context.prev = 16;
1556
+ _context.next = 19;
1536
1557
  return fetch(useableUrl, _extends({}, opts, {
1537
1558
  headers: basicHeaders
1538
1559
  }))["catch"](function (e) {
@@ -1541,111 +1562,111 @@ function _fetch2() {
1541
1562
  stack: e.stack
1542
1563
  });
1543
1564
  });
1544
- case 18:
1565
+ case 19:
1545
1566
  response = _context.sent;
1546
1567
  if (!(response && response.status >= 200 && response.status < 300)) {
1547
- _context.next = 41;
1568
+ _context.next = 42;
1548
1569
  break;
1549
1570
  }
1550
1571
  if (!(typeof response.clone === 'function')) {
1551
- _context.next = 40;
1572
+ _context.next = 41;
1552
1573
  break;
1553
1574
  }
1554
- _context.prev = 21;
1575
+ _context.prev = 22;
1555
1576
  _context.t0 = debug;
1556
1577
  _context.t1 = "[fetch][response][" + ((_opts$method = opts == null ? void 0 : opts.method) != null ? _opts$method : 'GET') + "] " + response.status + " " + useableUrl;
1557
- _context.next = 26;
1578
+ _context.next = 27;
1558
1579
  return response.clone().json();
1559
- case 26:
1580
+ case 27:
1560
1581
  _context.t2 = _context.sent;
1561
1582
  _context.t3 = {
1562
1583
  body: _context.t2
1563
1584
  };
1564
1585
  (0, _context.t0)(_context.t1, _context.t3);
1565
- _context.next = 40;
1586
+ _context.next = 41;
1566
1587
  break;
1567
- case 31:
1568
- _context.prev = 31;
1569
- _context.t4 = _context["catch"](21);
1588
+ case 32:
1589
+ _context.prev = 32;
1590
+ _context.t4 = _context["catch"](22);
1570
1591
  _context.t5 = debug;
1571
1592
  _context.t6 = "[fetch][response][" + ((_opts$method2 = opts == null ? void 0 : opts.method) != null ? _opts$method2 : 'GET') + "] " + response.status + " " + useableUrl;
1572
- _context.next = 37;
1593
+ _context.next = 38;
1573
1594
  return response.clone().text();
1574
- case 37:
1595
+ case 38:
1575
1596
  _context.t7 = _context.sent;
1576
1597
  _context.t8 = {
1577
1598
  body: _context.t7
1578
1599
  };
1579
1600
  (0, _context.t5)(_context.t6, _context.t8);
1580
- case 40:
1581
- return _context.abrupt("return", response);
1582
1601
  case 41:
1602
+ return _context.abrupt("return", response);
1603
+ case 42:
1583
1604
  if (!((response == null ? void 0 : response.status) === 404)) {
1584
- _context.next = 43;
1605
+ _context.next = 44;
1585
1606
  break;
1586
1607
  }
1587
1608
  return _context.abrupt("return", new ResponseError('Not found', {
1588
1609
  status: 404
1589
1610
  }));
1590
- case 43:
1611
+ case 44:
1591
1612
  if (!((response == null ? void 0 : response.status) === 401)) {
1592
- _context.next = 45;
1613
+ _context.next = 46;
1593
1614
  break;
1594
1615
  }
1595
1616
  return _context.abrupt("return", new ResponseError('Unauthorized', {
1596
1617
  status: 401
1597
1618
  }));
1598
- case 45:
1619
+ case 46:
1599
1620
  if (!((response == null ? void 0 : response.status) === 405)) {
1600
- _context.next = 47;
1621
+ _context.next = 48;
1601
1622
  break;
1602
1623
  }
1603
1624
  return _context.abrupt("return", new ResponseError('Method not allowed', {
1604
1625
  status: 405
1605
1626
  }));
1606
- case 47:
1627
+ case 48:
1607
1628
  errorHandler = typeof (response == null ? void 0 : response.clone) === 'function' ? response.clone() : null;
1608
1629
  msg = '';
1609
- _context.prev = 49;
1610
- _context.next = 52;
1630
+ _context.prev = 50;
1631
+ _context.next = 53;
1611
1632
  return response == null ? void 0 : response.json();
1612
- case 52:
1633
+ case 53:
1613
1634
  res = _context.sent;
1614
- _context.next = 63;
1635
+ _context.next = 64;
1615
1636
  break;
1616
- case 55:
1617
- _context.prev = 55;
1618
- _context.t9 = _context["catch"](49);
1637
+ case 56:
1638
+ _context.prev = 56;
1639
+ _context.t9 = _context["catch"](50);
1619
1640
  if (!errorHandler) {
1620
- _context.next = 62;
1641
+ _context.next = 63;
1621
1642
  break;
1622
1643
  }
1623
- _context.next = 60;
1644
+ _context.next = 61;
1624
1645
  return errorHandler.text();
1625
- case 60:
1646
+ case 61:
1626
1647
  msg = _context.sent;
1627
1648
  if (msg) {
1628
1649
  error("[fetch][response] status: " + errorHandler.status + "]", {
1629
1650
  message: msg
1630
1651
  });
1631
1652
  }
1632
- case 62:
1653
+ case 63:
1633
1654
  if (!msg) {
1634
1655
  error('[fetch][response]', {
1635
1656
  e: _context.t9
1636
1657
  });
1637
1658
  }
1638
- case 63:
1659
+ case 64:
1639
1660
  if (!msg) {
1640
- _context.next = 65;
1661
+ _context.next = 66;
1641
1662
  break;
1642
1663
  }
1643
1664
  return _context.abrupt("return", new ResponseError(msg, {
1644
1665
  status: errorHandler == null ? void 0 : errorHandler.status
1645
1666
  }));
1646
- case 65:
1667
+ case 66:
1647
1668
  if (!(res && 'message' in res)) {
1648
- _context.next = 69;
1669
+ _context.next = 70;
1649
1670
  break;
1650
1671
  }
1651
1672
  _res = res, message = _res.message;
@@ -1653,9 +1674,9 @@ function _fetch2() {
1653
1674
  return _context.abrupt("return", new ResponseError(message, {
1654
1675
  status: 400
1655
1676
  }));
1656
- case 69:
1677
+ case 70:
1657
1678
  if (!(res && 'errors' in res)) {
1658
- _context.next = 73;
1679
+ _context.next = 74;
1659
1680
  break;
1660
1681
  }
1661
1682
  _res2 = res, _res2$errors = _res2.errors, _message = _res2$errors[0];
@@ -1663,18 +1684,24 @@ function _fetch2() {
1663
1684
  return _context.abrupt("return", new ResponseError(_message, {
1664
1685
  status: 400
1665
1686
  }));
1666
- case 73:
1687
+ case 74:
1667
1688
  error("[fetch][response] status: " + (errorHandler == null ? void 0 : errorHandler.status) + "] UNHANDLED ERROR", {
1668
1689
  res: res
1669
1690
  });
1670
1691
  return _context.abrupt("return", new ResponseError(null, {
1671
1692
  status: (_response$status = response == null ? void 0 : response.status) != null ? _response$status : 500
1672
1693
  }));
1673
- case 75:
1694
+ case 78:
1695
+ _context.prev = 78;
1696
+ _context.t10 = _context["catch"](16);
1697
+ return _context.abrupt("return", new ResponseError('an unexpected error has occurred', {
1698
+ status: 500
1699
+ }));
1700
+ case 81:
1674
1701
  case "end":
1675
1702
  return _context.stop();
1676
1703
  }
1677
- }, _callee, null, [[21, 31], [49, 55]]);
1704
+ }, _callee, null, [[16, 78], [22, 32], [50, 56]]);
1678
1705
  }));
1679
1706
  return _fetch2.apply(this, arguments);
1680
1707
  }
@@ -3268,12 +3295,13 @@ var ORIGIN = 'https://us-west-2.api.dev.thenile.dev';
3268
3295
  function serverAuth(config, handlers) {
3269
3296
  var _Logger = Logger(config, '[server side login]'),
3270
3297
  info = _Logger.info,
3271
- error = _Logger.error;
3298
+ error = _Logger.error,
3299
+ debug = _Logger.debug;
3272
3300
  var routes = appRoutes(config.routePrefix);
3273
3301
  return /*#__PURE__*/function () {
3274
3302
  var _login = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
3275
3303
  var _providers, _exec;
3276
- var email, password, sessionUrl, baseHeaders, sessionReq, sessionRes, providers, csrf, csrfReq, csrfRes, csrfToken, _yield$csrfRes$json, json, _ref2, credentials, csrfCookie, signInUrl, postReq, loginRes, authCookie, _ref3, token;
3304
+ var email, password, sessionUrl, baseHeaders, sessionReq, sessionRes, providers, csrf, csrfReq, csrfRes, csrfToken, _yield$csrfRes$json, json, _ref2, credentials, csrfCookie, signInUrl, body, postReq, loginRes, authCookie, _ref3, token, headers;
3277
3305
  return _regeneratorRuntime().wrap(function _callee$(_context) {
3278
3306
  while (1) switch (_context.prev = _context.next) {
3279
3307
  case 0:
@@ -3367,51 +3395,59 @@ function serverAuth(config, handlers) {
3367
3395
  case 49:
3368
3396
  signInUrl = new URL(credentials.callbackUrl);
3369
3397
  if (csrfCookie) {
3370
- _context.next = 52;
3398
+ _context.next = 53;
3371
3399
  break;
3372
3400
  }
3373
- throw new Error('Unable to authenticate REST');
3374
- case 52:
3375
- info("Attempting sign in with email " + email);
3401
+ debug('CSRF failed', {
3402
+ headers: csrfRes == null ? void 0 : csrfRes.headers
3403
+ });
3404
+ throw new Error('Unable to authenticate REST, CSRF missing.');
3405
+ case 53:
3406
+ info("Attempting sign in with email " + email + " " + signInUrl.href);
3407
+ body = JSON.stringify({
3408
+ email: email,
3409
+ password: password,
3410
+ csrfToken: csrfToken,
3411
+ callbackUrl: credentials.callbackUrl
3412
+ });
3376
3413
  postReq = new Request(signInUrl, {
3377
3414
  method: 'POST',
3378
- headers: new Headers(_extends({
3415
+ headers: new Headers(_extends({}, baseHeaders, {
3379
3416
  'content-type': 'application/json',
3380
- cookie: csrfCookie
3381
- }, baseHeaders)),
3382
- body: JSON.stringify({
3383
- email: email,
3384
- password: password,
3385
- csrfToken: csrfToken,
3386
- callbackUrl: credentials.callbackUrl
3387
- })
3417
+ cookie: csrfCookie.split(',').join('; ')
3418
+ })),
3419
+ body: body
3388
3420
  });
3389
- _context.next = 56;
3421
+ _context.next = 58;
3390
3422
  return handlers.POST(postReq);
3391
- case 56:
3423
+ case 58:
3392
3424
  loginRes = _context.sent;
3393
3425
  authCookie = loginRes == null ? void 0 : loginRes.headers.get('set-cookie');
3394
3426
  if (authCookie) {
3395
- _context.next = 60;
3427
+ _context.next = 62;
3396
3428
  break;
3397
3429
  }
3398
3430
  throw new Error('authentication failed');
3399
- case 60:
3431
+ case 62:
3400
3432
  _ref3 = (_exec = /((__Secure-)?nile\.session-token=.+?);/.exec(authCookie)) != null ? _exec : [], token = _ref3[1];
3401
3433
  if (token) {
3402
- _context.next = 63;
3434
+ _context.next = 66;
3403
3435
  break;
3404
3436
  }
3437
+ error('Unable to obtain auth token', {
3438
+ authCookie: authCookie
3439
+ });
3405
3440
  throw new Error('Server login failed');
3406
- case 63:
3441
+ case 66:
3407
3442
  info('Server login successful', {
3408
3443
  authCookie: authCookie,
3409
3444
  csrfCookie: csrfCookie
3410
3445
  });
3411
- return _context.abrupt("return", new Headers(_extends({
3446
+ headers = new Headers(_extends({}, baseHeaders, {
3412
3447
  cookie: [token, csrfCookie].join('; ')
3413
- }, baseHeaders)));
3414
- case 65:
3448
+ }));
3449
+ return _context.abrupt("return", headers);
3450
+ case 69:
3415
3451
  case "end":
3416
3452
  return _context.stop();
3417
3453
  }