@niledatabase/server 5.3.1 → 5.3.2-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -532,6 +532,30 @@ function headersToMap(headers) {
532
532
  }
533
533
 
534
534
  // src/api/utils/request.ts
535
+ var NULL_BODY_STATUSES = /* @__PURE__ */ new Set([204, 205, 304]);
536
+ function stripDecodedResponseHeaders(res) {
537
+ if (!(res instanceof Response) || res.status < 200 || res.status > 599) {
538
+ return res;
539
+ }
540
+ const hasContentEncoding = res.headers.has("content-encoding");
541
+ const hasTransferEncoding = res.headers.has("transfer-encoding");
542
+ if (!hasContentEncoding && !hasTransferEncoding) {
543
+ return res;
544
+ }
545
+ const headers = new Headers(res.headers);
546
+ if (hasContentEncoding) {
547
+ headers.delete("content-encoding");
548
+ headers.delete("content-length");
549
+ }
550
+ if (hasTransferEncoding) {
551
+ headers.delete("transfer-encoding");
552
+ }
553
+ return new Response(NULL_BODY_STATUSES.has(res.status) ? null : res.body, {
554
+ status: res.status,
555
+ statusText: res.statusText,
556
+ headers
557
+ });
558
+ }
535
559
  async function request(url, _init, config) {
536
560
  const { debug, info, error } = config.logger("[REQUEST]");
537
561
  const { request: request2, ...init } = _init;
@@ -620,7 +644,7 @@ async function request(url, _init, config) {
620
644
  _init
621
645
  );
622
646
  try {
623
- const res = await fetch(fullUrl, {
647
+ const fetchedRes = await fetch(fullUrl, {
624
648
  ...params
625
649
  }).catch((e) => {
626
650
  error("An error has occurred in the fetch", {
@@ -632,6 +656,7 @@ async function request(url, _init, config) {
632
656
  { status: 500 }
633
657
  );
634
658
  });
659
+ const res = stripDecodedResponseHeaders(fetchedRes);
635
660
  const loggingRes = typeof res?.clone === "function" ? res?.clone() : null;
636
661
  info(`[${params.method ?? "GET"}] ${fullUrl}`, {
637
662
  status: res?.status,
@@ -753,27 +778,20 @@ async function GET(url, init, config) {
753
778
 
754
779
  // src/utils/fetch.ts
755
780
  function getTokenFromCookie(headers, cookieKey) {
756
- const cookie = headers.get("cookie")?.split("; ");
757
- const _cookies = {};
758
- if (cookie) {
759
- for (const parts of cookie) {
760
- const cookieParts = parts.split("=");
761
- const _cookie = cookieParts.slice(1).join("=");
762
- const name = cookieParts[0];
763
- _cookies[name] = _cookie;
764
- }
781
+ const cookieHeader = headers.get("cookie");
782
+ if (!cookieHeader) {
783
+ return null;
765
784
  }
766
- if (cookie) {
767
- for (const parts of cookie) {
768
- const cookieParts = parts.split("=");
769
- const _cookie = cookieParts.slice(1).join("=");
770
- const name = cookieParts[0];
771
- _cookies[name] = _cookie;
785
+ const cookies = cookieHeader.split("; ");
786
+ for (const parts of cookies) {
787
+ const cookieParts = parts.split("=");
788
+ const name = cookieParts[0];
789
+ const value = cookieParts.slice(1).join("=");
790
+ if (name === cookieKey) {
791
+ return value;
772
792
  }
773
793
  }
774
- {
775
- return _cookies[cookieKey];
776
- }
794
+ return null;
777
795
  }
778
796
  function getTenantFromHttp(headers, context) {
779
797
  const cookieTenant = getTokenFromCookie(headers, TENANT_COOKIE);
@@ -2268,43 +2286,45 @@ var updateHeaders = (val) => {
2268
2286
  eventer.publish("headers" /* Headers */, val);
2269
2287
  };
2270
2288
  var watchHeaders = (cb) => eventer.subscribe("headers" /* Headers */, cb);
2271
-
2272
- // src/db/PoolProxy.ts
2273
- function createProxyForPool(pool, config, logger, context) {
2274
- const { info, error } = logger("[pool]");
2275
- return new Proxy(pool, {
2276
- get(target, property) {
2277
- if (property === "query") {
2278
- if (!config.connectionString) {
2279
- if (!config.user || !config.password) {
2280
- error(
2281
- "Cannot connect to the database. User and/or password are missing. Generate them at https://console.thenile.dev"
2282
- );
2283
- } else if (!config.database) {
2284
- error(
2285
- "Unable to obtain database name. Is process.env.NILEDB_POSTGRES_URL set?"
2286
- );
2287
- }
2288
- }
2289
- const caller = target[property];
2290
- return function query(...args) {
2291
- let log = "[QUERY]";
2292
- const [tenantId, userId] = context;
2293
- if (tenantId) {
2294
- log = `${log}[TENANT:${tenantId}]`;
2295
- }
2296
- if (userId) {
2297
- log = `${log}[USER:${userId}]`;
2298
- }
2299
- info(log, ...args);
2300
- const called = caller.apply(this, args);
2301
- return called;
2302
- };
2289
+ var PoolWithLogger = class extends pg__default.default.Pool {
2290
+ nileConfig;
2291
+ logger;
2292
+ context;
2293
+ info;
2294
+ error;
2295
+ constructor(config, logger, context) {
2296
+ super(config);
2297
+ this.nileConfig = config;
2298
+ this.logger = logger;
2299
+ this.context = context;
2300
+ const { info, error } = this.logger("[pool]");
2301
+ this.info = info;
2302
+ this.error = error;
2303
+ }
2304
+ query(...args) {
2305
+ if (!this.nileConfig.connectionString) {
2306
+ if (!this.nileConfig.user || !this.nileConfig.password) {
2307
+ this.error(
2308
+ "Cannot connect to the database. User and/or password are missing. Generate them at https://console.thenile.dev"
2309
+ );
2310
+ } else if (!this.nileConfig.database) {
2311
+ this.error(
2312
+ "Unable to obtain database name. Is process.env.NILEDB_POSTGRES_URL set?"
2313
+ );
2303
2314
  }
2304
- return target[property];
2305
2315
  }
2306
- });
2307
- }
2316
+ let log = "[QUERY]";
2317
+ const [tenantId, userId] = this.context;
2318
+ if (tenantId) {
2319
+ log = `${log}[TENANT:${tenantId}]`;
2320
+ }
2321
+ if (userId) {
2322
+ log = `${log}[USER:${userId}]`;
2323
+ }
2324
+ this.info(log, ...args);
2325
+ return super.query(...args);
2326
+ }
2327
+ };
2308
2328
 
2309
2329
  // src/db/NileInstance.ts
2310
2330
  var NileDatabase = class {
@@ -2329,8 +2349,7 @@ var NileDatabase = class {
2329
2349
  const cloned = { ...config };
2330
2350
  cloned.password = "***";
2331
2351
  this.logger.debug(`Connection pool config ${JSON.stringify(cloned)}`);
2332
- this.pool = createProxyForPool(
2333
- new pg__default.default.Pool(remaining),
2352
+ this.pool = new PoolWithLogger(
2334
2353
  this.config,
2335
2354
  logger,
2336
2355
  id === "base" ? [] : id.split(":")
@@ -2588,8 +2607,10 @@ var Auth = class {
2588
2607
  return withNileContext(
2589
2608
  this.#config,
2590
2609
  async () => {
2591
- ctx.set({ headers: null });
2592
2610
  const { email, password, ...params } = payload;
2611
+ if (!params.tenantId) {
2612
+ ctx.set({ headers: null });
2613
+ }
2593
2614
  if (!email || !password) {
2594
2615
  throw new Error(
2595
2616
  "Server side sign up requires a user email and password."
@@ -2627,7 +2648,12 @@ var Auth = class {
2627
2648
  );
2628
2649
  }
2629
2650
  const { headers } = ctx.get();
2630
- headers?.append("cookie", token);
2651
+ const cookie = [
2652
+ parseCSRF(res.headers) ?? parseCSRF(headers),
2653
+ parseCallback(res.headers) ?? parseCallback(headers),
2654
+ token
2655
+ ].filter(Boolean).join("; ");
2656
+ headers.set("cookie", cookie);
2631
2657
  ctx.set({ headers });
2632
2658
  updateHeaders(headers);
2633
2659
  if (rawResponse) {
@@ -3070,8 +3096,8 @@ async function obtainCsrf(config, rawResponse = false) {
3070
3096
  if (setCookie) {
3071
3097
  const cookie = [
3072
3098
  csrfCook,
3073
- parseCallback(res.headers),
3074
- parseToken(res.headers)
3099
+ parseCallback(res.headers) ?? parseCallback(headers),
3100
+ parseToken(res.headers) ?? parseToken(headers)
3075
3101
  ].filter(Boolean).join("; ");
3076
3102
  headers.set("cookie", cookie);
3077
3103
  ctx.set({ headers });