@niledatabase/server 3.0.0-alpha.22 → 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,17 +1513,18 @@ 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, info, error, url, cookieKey, headers, basicHeaders, authHeader, token, tenantId, userId, useableUrl, response, 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:
1501
- _Logger = Logger(config, '[server]'), info = _Logger.info, error = _Logger.error;
1521
+ _Logger = Logger(config, '[server]'), debug = _Logger.debug, error = _Logger.error;
1502
1522
  url = "" + ((_config$api = config.api) == null ? void 0 : _config$api.basePath) + path;
1503
1523
  cookieKey = (_config$api2 = config.api) == null ? void 0 : _config$api2.cookieKey;
1504
1524
  headers = new Headers(opts == null ? void 0 : opts.headers);
1505
1525
  basicHeaders = new Headers(opts == null ? void 0 : opts.headers);
1506
1526
  basicHeaders.set('content-type', 'application/json; charset=utf-8');
1527
+ // this is old, but still maybe something worth keeping.
1507
1528
  authHeader = headers.get('Authorization');
1508
1529
  if (!authHeader) {
1509
1530
  token = getTokenFromCookie(headers, cookieKey);
@@ -1530,8 +1551,9 @@ function _fetch2() {
1530
1551
  }));
1531
1552
  case 14:
1532
1553
  useableUrl = url.replace('{tenantId}', encodeURIComponent(String(tenantId))).replace('{userId}', encodeURIComponent(String(userId)));
1533
- info("[fetch] " + useableUrl);
1534
- _context.next = 18;
1554
+ debug("[fetch] " + useableUrl);
1555
+ _context.prev = 16;
1556
+ _context.next = 19;
1535
1557
  return fetch(useableUrl, _extends({}, opts, {
1536
1558
  headers: basicHeaders
1537
1559
  }))["catch"](function (e) {
@@ -1540,7 +1562,7 @@ function _fetch2() {
1540
1562
  stack: e.stack
1541
1563
  });
1542
1564
  });
1543
- case 18:
1565
+ case 19:
1544
1566
  response = _context.sent;
1545
1567
  if (!(response && response.status >= 200 && response.status < 300)) {
1546
1568
  _context.next = 42;
@@ -1550,31 +1572,32 @@ function _fetch2() {
1550
1572
  _context.next = 41;
1551
1573
  break;
1552
1574
  }
1553
- _context.prev = 21;
1554
- _context.t0 = info;
1555
- _context.t1 = JSON;
1556
- _context.next = 26;
1575
+ _context.prev = 22;
1576
+ _context.t0 = debug;
1577
+ _context.t1 = "[fetch][response][" + ((_opts$method = opts == null ? void 0 : opts.method) != null ? _opts$method : 'GET') + "] " + response.status + " " + useableUrl;
1578
+ _context.next = 27;
1557
1579
  return response.clone().json();
1558
- case 26:
1580
+ case 27:
1559
1581
  _context.t2 = _context.sent;
1560
- _context.t3 = _context.t1.stringify.call(_context.t1, _context.t2);
1561
- _context.t4 = "[fetch][response] " + _context.t3;
1562
- (0, _context.t0)(_context.t4);
1582
+ _context.t3 = {
1583
+ body: _context.t2
1584
+ };
1585
+ (0, _context.t0)(_context.t1, _context.t3);
1563
1586
  _context.next = 41;
1564
1587
  break;
1565
1588
  case 32:
1566
1589
  _context.prev = 32;
1567
- _context.t5 = _context["catch"](21);
1568
- _context.t6 = info;
1569
- _context.next = 37;
1590
+ _context.t4 = _context["catch"](22);
1591
+ _context.t5 = debug;
1592
+ _context.t6 = "[fetch][response][" + ((_opts$method2 = opts == null ? void 0 : opts.method) != null ? _opts$method2 : 'GET') + "] " + response.status + " " + useableUrl;
1593
+ _context.next = 38;
1570
1594
  return response.clone().text();
1571
- case 37:
1595
+ case 38:
1572
1596
  _context.t7 = _context.sent;
1573
- _context.t8 = "[fetch][response] " + _context.t7;
1574
- _context.t9 = {
1575
- e: _context.t5
1597
+ _context.t8 = {
1598
+ body: _context.t7
1576
1599
  };
1577
- (0, _context.t6)(_context.t8, _context.t9);
1600
+ (0, _context.t5)(_context.t6, _context.t8);
1578
1601
  case 41:
1579
1602
  return _context.abrupt("return", response);
1580
1603
  case 42:
@@ -1613,7 +1636,7 @@ function _fetch2() {
1613
1636
  break;
1614
1637
  case 56:
1615
1638
  _context.prev = 56;
1616
- _context.t10 = _context["catch"](50);
1639
+ _context.t9 = _context["catch"](50);
1617
1640
  if (!errorHandler) {
1618
1641
  _context.next = 63;
1619
1642
  break;
@@ -1630,7 +1653,7 @@ function _fetch2() {
1630
1653
  case 63:
1631
1654
  if (!msg) {
1632
1655
  error('[fetch][response]', {
1633
- e: _context.t10
1656
+ e: _context.t9
1634
1657
  });
1635
1658
  }
1636
1659
  case 64:
@@ -1668,11 +1691,17 @@ function _fetch2() {
1668
1691
  return _context.abrupt("return", new ResponseError(null, {
1669
1692
  status: (_response$status = response == null ? void 0 : response.status) != null ? _response$status : 500
1670
1693
  }));
1671
- case 76:
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:
1672
1701
  case "end":
1673
1702
  return _context.stop();
1674
1703
  }
1675
- }, _callee, null, [[21, 32], [50, 56]]);
1704
+ }, _callee, null, [[16, 78], [22, 32], [50, 56]]);
1676
1705
  }));
1677
1706
  return _fetch2.apply(this, arguments);
1678
1707
  }
@@ -3266,12 +3295,13 @@ var ORIGIN = 'https://us-west-2.api.dev.thenile.dev';
3266
3295
  function serverAuth(config, handlers) {
3267
3296
  var _Logger = Logger(config, '[server side login]'),
3268
3297
  info = _Logger.info,
3269
- error = _Logger.error;
3298
+ error = _Logger.error,
3299
+ debug = _Logger.debug;
3270
3300
  var routes = appRoutes(config.routePrefix);
3271
3301
  return /*#__PURE__*/function () {
3272
3302
  var _login = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(_ref) {
3273
3303
  var _providers, _exec;
3274
- 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;
3275
3305
  return _regeneratorRuntime().wrap(function _callee$(_context) {
3276
3306
  while (1) switch (_context.prev = _context.next) {
3277
3307
  case 0:
@@ -3365,51 +3395,59 @@ function serverAuth(config, handlers) {
3365
3395
  case 49:
3366
3396
  signInUrl = new URL(credentials.callbackUrl);
3367
3397
  if (csrfCookie) {
3368
- _context.next = 52;
3398
+ _context.next = 53;
3369
3399
  break;
3370
3400
  }
3371
- throw new Error('Unable to authenticate REST');
3372
- case 52:
3373
- 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
+ });
3374
3413
  postReq = new Request(signInUrl, {
3375
3414
  method: 'POST',
3376
- headers: new Headers(_extends({
3415
+ headers: new Headers(_extends({}, baseHeaders, {
3377
3416
  'content-type': 'application/json',
3378
- cookie: csrfCookie
3379
- }, baseHeaders)),
3380
- body: JSON.stringify({
3381
- email: email,
3382
- password: password,
3383
- csrfToken: csrfToken,
3384
- callbackUrl: credentials.callbackUrl
3385
- })
3417
+ cookie: csrfCookie.split(',').join('; ')
3418
+ })),
3419
+ body: body
3386
3420
  });
3387
- _context.next = 56;
3421
+ _context.next = 58;
3388
3422
  return handlers.POST(postReq);
3389
- case 56:
3423
+ case 58:
3390
3424
  loginRes = _context.sent;
3391
3425
  authCookie = loginRes == null ? void 0 : loginRes.headers.get('set-cookie');
3392
3426
  if (authCookie) {
3393
- _context.next = 60;
3427
+ _context.next = 62;
3394
3428
  break;
3395
3429
  }
3396
3430
  throw new Error('authentication failed');
3397
- case 60:
3431
+ case 62:
3398
3432
  _ref3 = (_exec = /((__Secure-)?nile\.session-token=.+?);/.exec(authCookie)) != null ? _exec : [], token = _ref3[1];
3399
3433
  if (token) {
3400
- _context.next = 63;
3434
+ _context.next = 66;
3401
3435
  break;
3402
3436
  }
3437
+ error('Unable to obtain auth token', {
3438
+ authCookie: authCookie
3439
+ });
3403
3440
  throw new Error('Server login failed');
3404
- case 63:
3441
+ case 66:
3405
3442
  info('Server login successful', {
3406
3443
  authCookie: authCookie,
3407
3444
  csrfCookie: csrfCookie
3408
3445
  });
3409
- return _context.abrupt("return", new Headers(_extends({
3446
+ headers = new Headers(_extends({}, baseHeaders, {
3410
3447
  cookie: [token, csrfCookie].join('; ')
3411
- }, baseHeaders)));
3412
- case 65:
3448
+ }));
3449
+ return _context.abrupt("return", headers);
3450
+ case 69:
3413
3451
  case "end":
3414
3452
  return _context.stop();
3415
3453
  }
@@ -4076,9 +4114,8 @@ var Server = /*#__PURE__*/function () {
4076
4114
  case 2:
4077
4115
  updatedConfig = _context.sent;
4078
4116
  this.setConfig(updatedConfig);
4079
- this.api = new Api(this.config);
4080
4117
  return _context.abrupt("return", this);
4081
- case 6:
4118
+ case 5:
4082
4119
  case "end":
4083
4120
  return _context.stop();
4084
4121
  }