@niledatabase/server 5.0.0-alpha.5 → 5.0.0-alpha.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -25,6 +25,12 @@ var LoginUserResponseTokenTypeEnum = {
25
25
  IdToken: "ID_TOKEN"
26
26
  };
27
27
 
28
+ // src/utils/constants.ts
29
+ var TENANT_COOKIE = "nile.tenant-id";
30
+ var USER_COOKIE = "nile.user-id";
31
+ var HEADER_ORIGIN = "nile-origin";
32
+ var HEADER_SECURE_COOKIES = "nile-secure-cookies";
33
+
28
34
  // src/api/utils/routes/index.ts
29
35
  var NILEDB_API_URL = process.env.NILEDB_API_URL;
30
36
  var DEFAULT_PREFIX = "/api";
@@ -114,74 +120,9 @@ function isUUID(value) {
114
120
  return regex.test(value);
115
121
  }
116
122
 
117
- // src/utils/Logger.ts
118
- var red = "\x1B[31m";
119
- var yellow = "\x1B[38;2;255;255;0m";
120
- var purple = "\x1B[38;2;200;160;255m";
121
- var orange = "\x1B[38;2;255;165;0m";
122
- var reset = "\x1B[0m";
123
- var baseLogger = (config, ...params) => ({
124
- info(message, meta) {
125
- if (config?.debug) {
126
- console.info(
127
- `${orange}[niledb]${reset}${purple}[DEBUG]${reset}${params.join(
128
- ""
129
- )}${reset} ${message}`,
130
- meta ? `${JSON.stringify(meta)}` : ""
131
- );
132
- }
133
- },
134
- debug(message, meta) {
135
- if (config?.debug) {
136
- console.debug(
137
- `${orange}[niledb]${reset}${purple}[DEBUG]${reset}${params.join(
138
- ""
139
- )}${reset} ${message}`,
140
- meta ? `${JSON.stringify(meta)}` : ""
141
- );
142
- }
143
- },
144
- warn(message, meta) {
145
- if (config?.debug) {
146
- console.warn(
147
- `${orange}[niledb]${reset}${yellow}[WARN]${reset}${params.join(
148
- ""
149
- )}${reset} ${message}`,
150
- meta ? JSON.stringify(meta) : ""
151
- );
152
- }
153
- },
154
- error(message, meta) {
155
- console.error(
156
- `${orange}[niledb]${reset}${red}[ERROR]${reset}${params.join(
157
- ""
158
- )}${red} ${message}`,
159
- meta ? meta : "",
160
- `${reset}`
161
- );
162
- }
163
- });
164
- function Logger(config, ...params) {
165
- const base = baseLogger(config, params);
166
- const info = config?.logger?.info ?? base.info;
167
- const debug = config?.logger?.debug ?? base.debug;
168
- const warn = config?.logger?.warn ?? base.warn;
169
- const error = config?.logger?.error ?? base.error;
170
- return { info, warn, error, debug };
171
- }
172
- function matchesLog(configRoutes, request2) {
173
- return urlMatches(request2.url, configRoutes.LOG);
174
- }
175
-
176
- // src/utils/constants.ts
177
- var TENANT_COOKIE = "nile.tenant-id";
178
- var USER_COOKIE = "nile.user-id";
179
- var HEADER_ORIGIN = "nile-origin";
180
- var HEADER_SECURE_COOKIES = "nile-secure-cookies";
181
-
182
123
  // src/api/utils/request.ts
183
124
  async function request(url, _init, config) {
184
- const { debug, info, error } = Logger(config, "[REQUEST]");
125
+ const { debug, info, error } = config.logger("[REQUEST]");
185
126
  const { request: request2, ...init } = _init;
186
127
  const requestUrl = new URL(request2.url);
187
128
  const updatedHeaders = new Headers({});
@@ -279,7 +220,7 @@ async function request(url, _init, config) {
279
220
 
280
221
  // src/api/utils/auth.ts
281
222
  async function auth(req, config) {
282
- const { info, error } = Logger(config, "[nileauth]");
223
+ const { info, error } = config.logger("[nileauth]");
283
224
  info("checking auth");
284
225
  const sessionUrl = `${config.apiUrl}/auth/session`;
285
226
  info(`using session ${sessionUrl}`);
@@ -422,10 +363,7 @@ async function PUT2(config, init) {
422
363
  // src/api/routes/users/index.ts
423
364
  var key2 = "USERS";
424
365
  async function route2(request2, config) {
425
- const { info } = Logger(
426
- { ...config, debug: config.debug },
427
- `[ROUTES][${key2}]`
428
- );
366
+ const { info } = config.logger(`[ROUTES][${key2}]`);
429
367
  switch (request2.method) {
430
368
  case "GET":
431
369
  return await GET2(config, { request: request2 }, info);
@@ -466,10 +404,7 @@ async function POST2(config, init) {
466
404
  // src/api/routes/tenants/[tenantId]/users/index.ts
467
405
  var key3 = "TENANT_USERS";
468
406
  async function route3(request2, config) {
469
- const { info } = Logger(
470
- { ...config, debug: config.debug },
471
- `[ROUTES][${key3}]`
472
- );
407
+ const { info } = config.logger(`[ROUTES][${key3}]`);
473
408
  const yurl = new URL(request2.url);
474
409
  const [, tenantId] = yurl.pathname.split("/").reverse();
475
410
  if (!tenantId) {
@@ -501,8 +436,8 @@ async function fetchTenantUsers(config, method, payload) {
501
436
  'Unable to fetch tenant, the tenantId context is missing. Call nile.setContext({ tenantId }), set nile.tenantId = "tenantId", or add it to the function call'
502
437
  );
503
438
  }
504
- if (!isUUID(config.tenantId) && config.logger?.warn) {
505
- config.logger?.warn(
439
+ if (!isUUID(config.tenantId)) {
440
+ config.logger("fetchTenantUsers").warn(
506
441
  "nile.tenantId is not a valid UUID. This may lead to unexpected behavior in your application."
507
442
  );
508
443
  }
@@ -592,8 +527,8 @@ async function fetchInvite(config, method, body) {
592
527
  'Unable to fetch tenant, the tenantId context is missing. Call nile.setContext({ tenantId }), set nile.tenantId = "tenantId", or add it to the function call'
593
528
  );
594
529
  }
595
- if (!isUUID(config.tenantId) && config.logger?.warn) {
596
- config.logger?.warn(
530
+ if (!isUUID(config.tenantId)) {
531
+ config.logger("fetchInvite").warn(
597
532
  "nile.tenantId is not a valid UUID. This may lead to unexpected behavior in your application."
598
533
  );
599
534
  }
@@ -647,8 +582,8 @@ async function fetchInvites(config) {
647
582
  'Unable to fetch tenant, the tenantId context is missing. Call nile.setContext({ tenantId }), set nile.tenantId = "tenantId", or add it to the function call'
648
583
  );
649
584
  }
650
- if (!isUUID(config.tenantId) && config.logger?.warn) {
651
- config.logger?.warn(
585
+ if (!isUUID(config.tenantId)) {
586
+ config.logger("fetchInvites").warn(
652
587
  "nile.tenantId is not a valid UUID. This may lead to unexpected behavior in your application."
653
588
  );
654
589
  }
@@ -716,10 +651,7 @@ async function POST4(config, init) {
716
651
  // src/api/routes/tenants/index.ts
717
652
  var key6 = "TENANTS";
718
653
  async function route6(request2, config) {
719
- const { info } = Logger(
720
- { ...config, debug: config.debug },
721
- `[ROUTES][${key6}]`
722
- );
654
+ const { info } = config.logger(`[ROUTES][${key6}]`);
723
655
  const session = await auth(request2, config);
724
656
  if (!session) {
725
657
  info("401");
@@ -763,8 +695,8 @@ async function fetchTenant(config, method, body) {
763
695
  'Unable to fetch tenant, the tenantId context is missing. Call nile.setContext({ tenantId }), set nile.tenantId = "tenantId", or add it to the function call'
764
696
  );
765
697
  }
766
- if (!isUUID(config.tenantId) && config.logger?.warn) {
767
- config.logger?.warn(
698
+ if (!isUUID(config.tenantId)) {
699
+ config.logger("fetch tenant").warn(
768
700
  "nile.tenantId is not a valid UUID. This may lead to unexpected behavior in your application."
769
701
  );
770
702
  }
@@ -781,21 +713,17 @@ async function fetchTenant(config, method, body) {
781
713
  return await config.handlers[m](req);
782
714
  }
783
715
  async function fetchTenantsByUser(config) {
784
- if (config.logger?.warn) {
785
- if (!config.userId) {
786
- config.logger?.warn(
787
- "nile.userId is not set. The call will still work for the API, but the database context is not set properly and may lead to unexpected behavior in your application."
788
- );
789
- } else if (!isUUID(config.userId)) {
790
- config.logger?.warn(
791
- "nile.userId is not a valid UUID. This may lead to unexpected behavior in your application."
792
- );
793
- }
716
+ const { warn } = config.logger("fetchTenantsByUser");
717
+ if (!config.userId) {
718
+ warn(
719
+ "nile.userId is not set. The call will still work for the API, but the database context is not set properly and may lead to unexpected behavior in your application."
720
+ );
721
+ } else if (!isUUID(config.userId)) {
722
+ warn(
723
+ "nile.userId is not a valid UUID. This may lead to unexpected behavior in your application."
724
+ );
794
725
  }
795
- const clientUrl = `${config.serverOrigin}${config.routePrefix}${"/users/{userId}/tenants" /* USER_TENANTS */.replace(
796
- "{userId}",
797
- config.userId ?? "WARN_NOT_SET"
798
- )}`;
726
+ const clientUrl = `${config.serverOrigin}${config.routePrefix}${"/tenants" /* TENANTS */}`;
799
727
  const req = new Request(clientUrl, { headers: config.headers });
800
728
  return await config.handlers.GET(req);
801
729
  }
@@ -903,10 +831,7 @@ async function fetchCsrf(config) {
903
831
  // src/api/routes/auth/callback.ts
904
832
  var key8 = "CALLBACK";
905
833
  async function route11(req, config) {
906
- const { error } = Logger(
907
- { ...config, debug: config.debug },
908
- `[ROUTES][${key8}]`
909
- );
834
+ const { error } = config.logger(`[ROUTES][${key8}]`);
910
835
  const [provider] = new URL(req.url).pathname.split("/").reverse();
911
836
  try {
912
837
  const passThroughUrl = new URL(req.url);
@@ -1097,7 +1022,7 @@ async function fetchVerifyEmail(config, method, body) {
1097
1022
 
1098
1023
  // src/api/handlers/GET.ts
1099
1024
  function GETTER(configRoutes, config) {
1100
- const { info, warn } = Logger(config, "[GET MATCHER]");
1025
+ const { info, warn } = config.logger("[GET MATCHER]");
1101
1026
  return async function GET7(req) {
1102
1027
  if (matches(configRoutes, req)) {
1103
1028
  info("matches me");
@@ -1168,6 +1093,68 @@ function GETTER(configRoutes, config) {
1168
1093
  };
1169
1094
  }
1170
1095
 
1096
+ // src/utils/Logger.ts
1097
+ var red = "\x1B[31m";
1098
+ var yellow = "\x1B[38;2;255;255;0m";
1099
+ var purple = "\x1B[38;2;200;160;255m";
1100
+ var orange = "\x1B[38;2;255;165;0m";
1101
+ var reset = "\x1B[0m";
1102
+ var baseLogger = (config, ...params) => ({
1103
+ info(message, meta) {
1104
+ if (config?.debug) {
1105
+ console.info(
1106
+ `${orange}[niledb]${reset}${purple}[DEBUG]${reset}${params.join(
1107
+ ""
1108
+ )}${reset} ${message}`,
1109
+ meta ? `${JSON.stringify(meta)}` : ""
1110
+ );
1111
+ }
1112
+ },
1113
+ debug(message, meta) {
1114
+ if (config?.debug) {
1115
+ console.debug(
1116
+ `${orange}[niledb]${reset}${purple}[DEBUG]${reset}${params.join(
1117
+ ""
1118
+ )}${reset} ${message}`,
1119
+ meta ? `${JSON.stringify(meta)}` : ""
1120
+ );
1121
+ }
1122
+ },
1123
+ warn(message, meta) {
1124
+ if (config?.debug) {
1125
+ console.warn(
1126
+ `${orange}[niledb]${reset}${yellow}[WARN]${reset}${params.join(
1127
+ ""
1128
+ )}${reset} ${message}`,
1129
+ meta ? JSON.stringify(meta) : ""
1130
+ );
1131
+ }
1132
+ },
1133
+ error(message, meta) {
1134
+ console.error(
1135
+ `${orange}[niledb]${reset}${red}[ERROR]${reset}${params.join(
1136
+ ""
1137
+ )}${red} ${message}`,
1138
+ meta ? meta : "",
1139
+ `${reset}`
1140
+ );
1141
+ }
1142
+ });
1143
+ function Logger(config) {
1144
+ return (prefixes) => {
1145
+ const { info, debug, warn, error } = config && typeof config?.logger === "function" ? config.logger(prefixes) : baseLogger(config, prefixes);
1146
+ return {
1147
+ info,
1148
+ debug,
1149
+ warn,
1150
+ error
1151
+ };
1152
+ };
1153
+ }
1154
+ function matchesLog(configRoutes, request2) {
1155
+ return urlMatches(request2.url, configRoutes.LOG);
1156
+ }
1157
+
1171
1158
  // src/api/routes/signup/POST.ts
1172
1159
  async function POST5(config, init) {
1173
1160
  init.body = init.request.body;
@@ -1209,7 +1196,7 @@ async function fetchSignUp(config, payload) {
1209
1196
 
1210
1197
  // src/api/handlers/POST.ts
1211
1198
  function POSTER(configRoutes, config) {
1212
- const { info, warn, error } = Logger(config, "[POST MATCHER]");
1199
+ const { info, warn, error } = config.logger("[POST MATCHER]");
1213
1200
  return async function POST6(req) {
1214
1201
  if (matchesLog(configRoutes, req)) {
1215
1202
  try {
@@ -1302,10 +1289,7 @@ async function PUT5(config, init) {
1302
1289
  // src/api/routes/tenants/[tenantId]/users/[userId]/index.ts
1303
1290
  var key15 = "TENANT_USER";
1304
1291
  async function route18(request2, config) {
1305
- const { info } = Logger(
1306
- { ...config, debug: config.debug },
1307
- `[ROUTES][${key15}]`
1308
- );
1292
+ const { info } = config.logger(`[ROUTES][${key15}]`);
1309
1293
  const session = await auth(request2, config);
1310
1294
  if (!session) {
1311
1295
  info("401");
@@ -1388,7 +1372,7 @@ function matches19(configRoutes, request2) {
1388
1372
 
1389
1373
  // src/api/handlers/DELETE.ts
1390
1374
  function DELETER(configRoutes, config) {
1391
- const { info, warn } = Logger(config, "[DELETE MATCHER]");
1375
+ const { info, warn } = config.logger("[DELETE MATCHER]");
1392
1376
  return async function DELETE5(req) {
1393
1377
  if (matches19(configRoutes, req)) {
1394
1378
  info("matches tenant invite id");
@@ -1417,7 +1401,7 @@ function DELETER(configRoutes, config) {
1417
1401
 
1418
1402
  // src/api/handlers/PUT.ts
1419
1403
  function PUTER(configRoutes, config) {
1420
- const { info, warn } = Logger(config, "[PUT MATCHER]");
1404
+ const { info, warn } = config.logger("[PUT MATCHER]");
1421
1405
  return async function PUT6(req) {
1422
1406
  if (matches4(configRoutes, req)) {
1423
1407
  info("matches tenant invite");
@@ -1497,14 +1481,14 @@ var getSecureCookies = (cfg) => {
1497
1481
  var getUsername = (cfg) => {
1498
1482
  const { config } = cfg;
1499
1483
  const logger = config.logger;
1500
- const { info } = Logger(config, "[username]");
1484
+ const { info } = logger();
1501
1485
  if (config?.user) {
1502
- logger && info(`${logger}[config] ${config.user}`);
1486
+ info(`[config] ${config.user}`);
1503
1487
  return String(config?.user);
1504
1488
  }
1505
1489
  const user = stringCheck(process.env.NILEDB_USER);
1506
1490
  if (user) {
1507
- logger && info(`${logger}[NILEDB_USER] ${user}`);
1491
+ info(`[NILEDB_USER] ${user}`);
1508
1492
  return user;
1509
1493
  }
1510
1494
  const pg2 = stringCheck(process.env.NILEDB_POSTGRES_URL);
@@ -1525,14 +1509,13 @@ var getPassword = (cfg) => {
1525
1509
  const { config } = cfg;
1526
1510
  const logger = config.logger;
1527
1511
  const log = logProtector(logger);
1528
- const { info } = Logger(config, "[password]");
1529
1512
  if (stringCheck(config?.password)) {
1530
- log && info(`${logger}[config] ***`);
1513
+ log && log("[config]").info("***");
1531
1514
  return String(config?.password);
1532
1515
  }
1533
1516
  const pass = stringCheck(process.env.NILEDB_PASSWORD);
1534
1517
  if (pass) {
1535
- logger && info(`${logger}[NILEDB_PASSWORD] ***`);
1518
+ logger("[NILEDB_PASSWORD]").info("***");
1536
1519
  return pass;
1537
1520
  }
1538
1521
  const pg2 = stringCheck(process.env.NILEDB_POSTGRES_URL);
@@ -1551,15 +1534,14 @@ var getPassword = (cfg) => {
1551
1534
  };
1552
1535
  var getDatabaseName = (cfg) => {
1553
1536
  const { config } = cfg;
1554
- const logger = config.logger;
1555
- const { info } = Logger(config, "[databaseName]");
1537
+ const { info } = config.logger("[databaseName]");
1556
1538
  if (stringCheck(config?.databaseName)) {
1557
- logger && info(`${logger}[config] ${config?.databaseName}`);
1539
+ info(`[config] ${config?.databaseName}`);
1558
1540
  return String(config?.databaseName);
1559
1541
  }
1560
1542
  const name = stringCheck(process.env.NILEDB_NAME);
1561
1543
  if (name) {
1562
- logger && info(`${logger}[NILEDB_NAME] ${name}`);
1544
+ info(`[NILEDB_NAME] ${name}`);
1563
1545
  return name;
1564
1546
  }
1565
1547
  if (process.env.NILEDB_POSTGRES_URL) {
@@ -1575,14 +1557,13 @@ var getDatabaseName = (cfg) => {
1575
1557
  };
1576
1558
  var getTenantId = (cfg) => {
1577
1559
  const { config } = cfg;
1578
- const logger = config.logger;
1579
- const { info } = Logger(config, "[tenantId]");
1560
+ const { info } = config.logger("[tenantId]");
1580
1561
  if (stringCheck(config?.tenantId)) {
1581
- logger && info(`${logger}[config] ${config?.tenantId}`);
1562
+ info(`[config] ${config?.tenantId}`);
1582
1563
  return String(config?.tenantId);
1583
1564
  }
1584
1565
  if (stringCheck(process.env.NILEDB_TENANT)) {
1585
- logger && info(`${logger}[NILEDB_TENANT] ${process.env.NILEDB_TENANT}`);
1566
+ info(`[NILEDB_TENANT] ${process.env.NILEDB_TENANT}`);
1586
1567
  return String(process.env.NILEDB_TENANT);
1587
1568
  }
1588
1569
  return null;
@@ -1590,37 +1571,37 @@ var getTenantId = (cfg) => {
1590
1571
  function getDbHost(cfg) {
1591
1572
  const { config } = cfg;
1592
1573
  const logger = config.logger;
1593
- const { info } = Logger(config, "[db.host]");
1574
+ const { info } = logger("[db.host]");
1594
1575
  if (stringCheck(config?.db && config.db.host)) {
1595
- logger && info(`${logger}[config] ${config?.db?.host}`);
1576
+ info(`[config] ${config?.db?.host}`);
1596
1577
  return String(config?.db?.host);
1597
1578
  }
1598
1579
  if (stringCheck(process.env.NILEDB_HOST)) {
1599
- logger && info(`${logger}[NILEDB_HOST] ${process.env.NILEDB_HOST}`);
1580
+ info(`[NILEDB_HOST] ${process.env.NILEDB_HOST}`);
1600
1581
  return process.env.NILEDB_HOST;
1601
1582
  }
1602
1583
  const pg2 = stringCheck(process.env.NILEDB_POSTGRES_URL);
1603
1584
  if (pg2) {
1604
1585
  try {
1605
1586
  const pgUrl = new URL(pg2);
1606
- logger && info(`${logger}[NILEDB_POSTGRES_URL] ${pgUrl.hostname}`);
1587
+ info(`[NILEDB_POSTGRES_URL] ${pgUrl.hostname}`);
1607
1588
  return pgUrl.hostname;
1608
1589
  } catch (e) {
1609
1590
  }
1610
1591
  }
1611
- logger && info(`${logger}[default] db.thenile.dev`);
1592
+ info("[default] db.thenile.dev");
1612
1593
  return "db.thenile.dev";
1613
1594
  }
1614
1595
  function getDbPort(cfg) {
1615
1596
  const { config } = cfg;
1616
1597
  const logger = config.logger;
1617
- const { info } = Logger(config, "[db.port]");
1598
+ const { info } = logger("[db.port]");
1618
1599
  if (config?.db?.port && config.db.port != null) {
1619
- logger && info(`${logger}[config] ${config?.db.port}`);
1600
+ info(`[config] ${config?.db.port}`);
1620
1601
  return Number(config.db?.port);
1621
1602
  }
1622
1603
  if (stringCheck(process.env.NILEDB_PORT)) {
1623
- logger && info(`${logger}[NILEDB_PORT] ${process.env.NILEDB_PORT}`);
1604
+ info(`[NILEDB_PORT] ${process.env.NILEDB_PORT}`);
1624
1605
  return Number(process.env.NILEDB_PORT);
1625
1606
  }
1626
1607
  const pg2 = stringCheck(process.env.NILEDB_POSTGRES_URL);
@@ -1633,7 +1614,7 @@ function getDbPort(cfg) {
1633
1614
  } catch (e) {
1634
1615
  }
1635
1616
  }
1636
- logger && info(`${logger}[default] 5432`);
1617
+ info("[default] 5432");
1637
1618
  return 5432;
1638
1619
  }
1639
1620
  var logProtector = (logger) => {
@@ -1686,7 +1667,6 @@ var Config = class {
1686
1667
  */
1687
1668
  routePrefix;
1688
1669
  db;
1689
- // api: ApiConfig;
1690
1670
  constructor(config) {
1691
1671
  this.routePrefix = config?.routePrefix ?? "/api";
1692
1672
  this.debug = config?.debug;
@@ -1694,7 +1674,7 @@ var Config = class {
1694
1674
  this.extensions = config?.extensions;
1695
1675
  this.extensionCtx = config?.extensionCtx;
1696
1676
  this.serverOrigin = config?.origin ?? "http://localhost:3000";
1697
- this.logger = config?.logger ?? Logger(this);
1677
+ this.logger = Logger(config);
1698
1678
  const envVarConfig = {
1699
1679
  config: { ...config, logger: this.logger }
1700
1680
  };
@@ -1815,7 +1795,7 @@ var watchHeaders = (cb) => eventer.subscribe("headers" /* Headers */, cb);
1815
1795
 
1816
1796
  // src/db/PoolProxy.ts
1817
1797
  function createProxyForPool(pool, config) {
1818
- const { info, error } = Logger(config, "[pool]");
1798
+ const { info, error } = config.logger("[pool]");
1819
1799
  return new Proxy(pool, {
1820
1800
  get(target, property) {
1821
1801
  if (property === "query") {
@@ -1851,7 +1831,7 @@ var NileDatabase = class {
1851
1831
  config;
1852
1832
  timer;
1853
1833
  constructor(config, id) {
1854
- const { warn, info, debug } = Logger(config, "[NileInstance]");
1834
+ const { warn, info, debug } = config.logger("[NileInstance]");
1855
1835
  this.id = id;
1856
1836
  const poolConfig = {
1857
1837
  min: 0,
@@ -1880,7 +1860,7 @@ var NileDatabase = class {
1880
1860
  `${this.id}-${this.timer}`
1881
1861
  );
1882
1862
  afterCreate2(client, (err) => {
1883
- const { error } = Logger(config, "[after create callback]");
1863
+ const { error } = config.logger("[after create callback]");
1884
1864
  if (err) {
1885
1865
  clearTimeout(this.timer);
1886
1866
  error("after create failed", {
@@ -1908,7 +1888,7 @@ var NileDatabase = class {
1908
1888
  });
1909
1889
  }
1910
1890
  startTimeout() {
1911
- const { debug } = Logger(this.config, "[NileInstance]");
1891
+ const { debug } = this.config.logger("[NileInstance]");
1912
1892
  if (this.timer) {
1913
1893
  clearTimeout(this.timer);
1914
1894
  }
@@ -1923,7 +1903,7 @@ var NileDatabase = class {
1923
1903
  }, Number(this.config.db.idleTimeoutMillis) ?? 3e4);
1924
1904
  }
1925
1905
  shutdown() {
1926
- const { debug } = Logger(this.config, "[NileInstance]");
1906
+ const { debug } = this.config.logger("[NileInstance]");
1927
1907
  debug(`attempting to shut down ${this.id}`);
1928
1908
  clearTimeout(this.timer);
1929
1909
  this.pool.end(() => {
@@ -1933,7 +1913,7 @@ var NileDatabase = class {
1933
1913
  };
1934
1914
  var NileInstance_default = NileDatabase;
1935
1915
  function makeAfterCreate(config, id) {
1936
- const { error, warn, debug } = Logger(config, "[afterCreate]");
1916
+ const { error, warn, debug } = config.logger("[afterCreate]");
1937
1917
  return (conn, done) => {
1938
1918
  conn.on("error", function errorHandler(e) {
1939
1919
  error(`Connection ${id} was terminated by server`, {
@@ -1997,7 +1977,7 @@ var DBManager = class {
1997
1977
  watchEvictPool(this.poolWatcherFn);
1998
1978
  }
1999
1979
  poolWatcher = (config) => (id) => {
2000
- const { info, warn } = Logger(config, "[DBManager]");
1980
+ const { info, warn } = Logger(config)("[DBManager]");
2001
1981
  if (id && this.connections.has(id)) {
2002
1982
  info(`Removing ${id} from db connection pool.`);
2003
1983
  const connection = this.connections.get(id);
@@ -2008,7 +1988,7 @@ var DBManager = class {
2008
1988
  }
2009
1989
  };
2010
1990
  getConnection = (config) => {
2011
- const { info } = Logger(config, "[DBManager]");
1991
+ const { info } = Logger(config)("[DBManager]");
2012
1992
  const id = this.makeId(config.tenantId, config.userId);
2013
1993
  const existing = this.connections.get(id);
2014
1994
  info(`# of instances: ${this.connections.size}`);
@@ -2027,7 +2007,7 @@ var DBManager = class {
2027
2007
  return newOne.pool;
2028
2008
  };
2029
2009
  clear = (config) => {
2030
- const { info } = Logger(config, "[DBManager]");
2010
+ const { info } = Logger(config)("[DBManager]");
2031
2011
  info(`Clearing all connections ${this.connections.size}`);
2032
2012
  this.cleared = true;
2033
2013
  this.connections.forEach((connection) => {
@@ -2043,7 +2023,7 @@ var Auth = class {
2043
2023
  #config;
2044
2024
  constructor(config) {
2045
2025
  this.#config = config;
2046
- this.#logger = Logger(config, "[auth]");
2026
+ this.#logger = config.logger("[auth]");
2047
2027
  }
2048
2028
  async getSession(rawResponse = false) {
2049
2029
  const res = await fetchSession(this.#config);
@@ -2493,7 +2473,7 @@ var Users = class {
2493
2473
  #logger;
2494
2474
  constructor(config) {
2495
2475
  this.#config = config;
2496
- this.#logger = Logger(config, "[me]");
2476
+ this.#logger = config.logger("[me]");
2497
2477
  }
2498
2478
  async updateSelf(req, rawResponse) {
2499
2479
  const res = await fetchMe(this.#config, "PUT", JSON.stringify(req));
@@ -2585,10 +2565,8 @@ function defaultCallbackUrl2(config) {
2585
2565
 
2586
2566
  // src/tenants/index.ts
2587
2567
  var Tenants = class {
2588
- #logger;
2589
2568
  #config;
2590
2569
  constructor(config) {
2591
- this.#logger = Logger(config, "[tenants]");
2592
2570
  this.#config = config;
2593
2571
  }
2594
2572
  async create(req, rawResponse) {
@@ -2871,6 +2849,7 @@ function updateConfig(response, config) {
2871
2849
  // src/api/utils/extensions.ts
2872
2850
  function bindHandleOnRequest(instance) {
2873
2851
  return async function handleOnRequest(config, _init, params) {
2852
+ const { debug } = config.logger("[EXTENSIONS]");
2874
2853
  if (config.extensions) {
2875
2854
  for (const create2 of config.extensions) {
2876
2855
  if (typeof create2 !== "function") {
@@ -2878,19 +2857,22 @@ function bindHandleOnRequest(instance) {
2878
2857
  }
2879
2858
  const ext = await create2(instance);
2880
2859
  if (ext.onRequest) {
2881
- const modified = await ext.onRequest(_init.request);
2882
- if (modified?.headers) {
2883
- const modHeaders = new Headers(modified.headers);
2884
- const cookie = modHeaders.get("cookie");
2860
+ await ext.onRequest(_init.request);
2861
+ const updatedContext = instance.getContext();
2862
+ if (updatedContext?.headers) {
2863
+ const cookie = updatedContext.headers.get("cookie");
2885
2864
  if (cookie) {
2886
2865
  params.headers.set("cookie", cookie);
2887
- config.logger.debug(
2888
- `extension ${ext.id ?? create2.name} modified cookie`
2866
+ }
2867
+ if (updatedContext.tenantId) {
2868
+ params.headers.set(
2869
+ TENANT_COOKIE,
2870
+ String(updatedContext.headers.get(TENANT_COOKIE))
2889
2871
  );
2890
2872
  }
2891
2873
  }
2874
+ debug(`${ext.id ?? create2.name} ran onRequest`);
2892
2875
  }
2893
- config.logger.debug(`extension ${ext.id ?? create2.name} ran onRequest`);
2894
2876
  }
2895
2877
  }
2896
2878
  };
@@ -3013,7 +2995,7 @@ var Server = class {
3013
2995
  return;
3014
2996
  }
3015
2997
  }
3016
- const { warn } = Logger(this.#config, "[API]");
2998
+ const { warn } = Logger(this.#config)("[API]");
3017
2999
  if (warn) {
3018
3000
  warn(
3019
3001
  "Set context expects a Request, Header instance or an object of Record<string, string>"
@@ -3068,6 +3050,7 @@ var Server = class {
3068
3050
  for (const [key17, value] of Object.entries(merged)) {
3069
3051
  this.#headers.set(key17, value);
3070
3052
  }
3053
+ this.#config.logger("[handleHeaders]").debug(JSON.stringify(merged));
3071
3054
  this.#config.headers = this.#headers;
3072
3055
  }
3073
3056
  /**