@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 +86 -60
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +86 -60
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
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
|
|
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
|
|
757
|
-
|
|
758
|
-
|
|
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
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
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
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
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 =
|
|
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
|
-
|
|
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 });
|