@niledatabase/server 5.0.0-alpha.27 → 5.0.0-alpha.28

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
@@ -477,8 +477,17 @@ var ctx = {
477
477
  if (partial.headers === null) {
478
478
  store.headers = new Headers();
479
479
  } else if (partial.headers && store.headers instanceof Headers) {
480
- for (const [k, v] of new Headers(partial.headers).entries()) {
481
- store.headers.set(k, v);
480
+ for (const [key17, value] of new Headers(partial.headers).entries()) {
481
+ if (key17.toLowerCase() === "cookie") {
482
+ const existingCookies = parseCookieHeader(
483
+ store.headers.get("cookie") || ""
484
+ );
485
+ const newCookies = parseCookieHeader(value);
486
+ const mergedCookies = { ...existingCookies, ...newCookies };
487
+ store.headers.set("cookie", serializeCookies(mergedCookies));
488
+ } else {
489
+ store.headers.set(key17, value);
490
+ }
482
491
  }
483
492
  }
484
493
  if ("tenantId" in partial)
@@ -543,6 +552,16 @@ function serializeContext(context) {
543
552
  preserveHeaders: context.preserveHeaders
544
553
  });
545
554
  }
555
+ function parseCookieHeader(header) {
556
+ return header.split(";").map((c) => c.trim()).filter(Boolean).reduce((acc, curr) => {
557
+ const [key17, ...val] = curr.split("=");
558
+ if (key17) acc[key17] = val.join("=");
559
+ return acc;
560
+ }, {});
561
+ }
562
+ function serializeCookies(cookies) {
563
+ return Object.entries(cookies).map(([k, v]) => `${k}=${v}`).join("; ");
564
+ }
546
565
 
547
566
  // src/api/routes/me/index.ts
548
567
  var key = "ME";
@@ -2694,7 +2713,7 @@ var Auth = class {
2694
2713
  ].filter(Boolean).join("; ");
2695
2714
  const uHeaders = new Headers({ cookie });
2696
2715
  updateHeaders(uHeaders);
2697
- ctx.set({ headers: uHeaders });
2716
+ ctx.set({ headers: uHeaders, preserveHeaders: true });
2698
2717
  } else {
2699
2718
  error("Unable to set context after sign in", {
2700
2719
  headers: signInRes.headers
@@ -2848,6 +2867,22 @@ async function obtainCsrf(config, rawResponse = false) {
2848
2867
  }
2849
2868
  }
2850
2869
 
2870
+ // src/utils/qualifyDomain.ts
2871
+ function fQUrl2(callbackUrl, path) {
2872
+ if (path.startsWith("/")) {
2873
+ if (callbackUrl) {
2874
+ const { origin } = new URL(callbackUrl);
2875
+ return `${origin}${path}`;
2876
+ }
2877
+ }
2878
+ try {
2879
+ new URL(path);
2880
+ } catch {
2881
+ throw new Error("An invalid URL has been passed.");
2882
+ }
2883
+ return path;
2884
+ }
2885
+
2851
2886
  // src/users/index.ts
2852
2887
  var Users = class {
2853
2888
  #config;
@@ -2917,7 +2952,10 @@ var Users = class {
2917
2952
  async verifySelf(options, rawResponse = false) {
2918
2953
  return withNileContext(this.#config, async () => {
2919
2954
  const bypassEmail = typeof options === "object" && options?.bypassEmail === true;
2920
- const callbackUrl = typeof options === "object" ? options.callbackUrl : defaultCallbackUrl2().callbackUrl;
2955
+ const callbackUrl = fQUrl2(
2956
+ defaultCallbackUrl2().callbackUrl,
2957
+ typeof options === "object" ? String(options.callbackUrl) : "/"
2958
+ );
2921
2959
  let res;
2922
2960
  try {
2923
2961
  const me = await this.getSelf();
@@ -3188,11 +3226,12 @@ var Tenants = class {
3188
3226
  if ("email" in req) {
3189
3227
  identifier = req.email;
3190
3228
  }
3229
+ const { callbackUrl: cbUrl } = defaultCallbackUrl3(this.#config);
3191
3230
  if ("callbackUrl" in req) {
3192
- callbackUrl = fQUrl2(req.callbackUrl ?? "", this.#config);
3231
+ callbackUrl = fQUrl2(cbUrl, req.callbackUrl ?? "/");
3193
3232
  }
3194
3233
  if ("redirectUrl" in req) {
3195
- redirectUrl = fQUrl2(req.redirectUrl ?? "", this.#config);
3234
+ redirectUrl = fQUrl2(cbUrl, req.redirectUrl ?? "/");
3196
3235
  }
3197
3236
  }
3198
3237
  const { headers } = ctx.get();
@@ -3294,21 +3333,6 @@ function defaultCallbackUrl3(config) {
3294
3333
  }
3295
3334
  return { callbackUrl: cb, redirectUrl: redirect };
3296
3335
  }
3297
- function fQUrl2(path, config) {
3298
- if (path.startsWith("/")) {
3299
- const { callbackUrl } = defaultCallbackUrl3(config);
3300
- if (callbackUrl) {
3301
- const { origin } = new URL(callbackUrl);
3302
- return `${origin}${path}`;
3303
- }
3304
- }
3305
- try {
3306
- new URL(path);
3307
- } catch {
3308
- throw new Error("An invalid URL has been passed.");
3309
- }
3310
- return path;
3311
- }
3312
3336
 
3313
3337
  // src/api/handlers/withContext/index.ts
3314
3338
  function handlersWithContext(config) {
@@ -3401,6 +3425,7 @@ var Server = class {
3401
3425
  watchHeaders((headers) => {
3402
3426
  if (headers) {
3403
3427
  this.#config.context.headers = new Headers(headers);
3428
+ this.#config.context.preserveHeaders = true;
3404
3429
  this.#reset();
3405
3430
  }
3406
3431
  });