@niledatabase/server 5.0.0-alpha.28 → 5.0.0-alpha.29
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.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +156 -136
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +156 -136
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -318,7 +318,7 @@ declare class Tenants {
|
|
|
318
318
|
acceptInvite<T = Response>(req?: {
|
|
319
319
|
identifier: string;
|
|
320
320
|
token: string;
|
|
321
|
-
|
|
321
|
+
callbackUrl?: string;
|
|
322
322
|
}, rawResponse?: boolean): Promise<T>;
|
|
323
323
|
/**
|
|
324
324
|
* Delete a pending invite using `DELETE /api/tenants/{tenantId}/invite/{inviteId}`.
|
package/dist/index.d.ts
CHANGED
|
@@ -318,7 +318,7 @@ declare class Tenants {
|
|
|
318
318
|
acceptInvite<T = Response>(req?: {
|
|
319
319
|
identifier: string;
|
|
320
320
|
token: string;
|
|
321
|
-
|
|
321
|
+
callbackUrl?: string;
|
|
322
322
|
}, rawResponse?: boolean): Promise<T>;
|
|
323
323
|
/**
|
|
324
324
|
* Delete a pending invite using `DELETE /api/tenants/{tenantId}/invite/{inviteId}`.
|
package/dist/index.js
CHANGED
|
@@ -131,139 +131,6 @@ function isUUID(value) {
|
|
|
131
131
|
return regex.test(value);
|
|
132
132
|
}
|
|
133
133
|
|
|
134
|
-
// src/api/utils/request.ts
|
|
135
|
-
async function request(url, _init, config) {
|
|
136
|
-
const { debug, info, error } = config.logger("[REQUEST]");
|
|
137
|
-
const { request: request2, ...init } = _init;
|
|
138
|
-
const requestUrl = new URL(request2.url);
|
|
139
|
-
const updatedHeaders = new Headers({});
|
|
140
|
-
if (request2.headers.get("cookie")) {
|
|
141
|
-
updatedHeaders.set("cookie", String(request2.headers.get("cookie")));
|
|
142
|
-
}
|
|
143
|
-
if (request2.headers.get(TENANT_COOKIE)) {
|
|
144
|
-
updatedHeaders.set(
|
|
145
|
-
TENANT_COOKIE,
|
|
146
|
-
String(request2.headers.get(TENANT_COOKIE))
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
if (config.secureCookies != null) {
|
|
150
|
-
updatedHeaders.set(HEADER_SECURE_COOKIES, String(config.secureCookies));
|
|
151
|
-
} else {
|
|
152
|
-
updatedHeaders.set(
|
|
153
|
-
HEADER_SECURE_COOKIES,
|
|
154
|
-
process.env.NODE_ENV === "production" ? "true" : "false"
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
updatedHeaders.set("host", requestUrl.host);
|
|
158
|
-
if (config.callbackUrl) {
|
|
159
|
-
const cbUrl = new URL(config.callbackUrl);
|
|
160
|
-
debug(`Obtained origin from config.callbackUrl ${config.callbackUrl}`);
|
|
161
|
-
updatedHeaders.set(HEADER_ORIGIN, cbUrl.origin);
|
|
162
|
-
} else if (config.origin) {
|
|
163
|
-
debug(`Obtained origin from config.origin ${config.origin}`);
|
|
164
|
-
updatedHeaders.set(HEADER_ORIGIN, config.origin);
|
|
165
|
-
} else {
|
|
166
|
-
const passedOrigin = request2.headers.get(HEADER_ORIGIN);
|
|
167
|
-
if (passedOrigin) {
|
|
168
|
-
updatedHeaders.set(HEADER_ORIGIN, passedOrigin);
|
|
169
|
-
} else {
|
|
170
|
-
const reqOrigin = config.routePrefix !== DEFAULT_PREFIX ? `${requestUrl.origin}${config.routePrefix}` : requestUrl.origin;
|
|
171
|
-
updatedHeaders.set(HEADER_ORIGIN, reqOrigin);
|
|
172
|
-
debug(`Obtained origin from request ${reqOrigin}`);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
const params = { ...init };
|
|
176
|
-
if (params.method?.toLowerCase() === "post" || params.method?.toLowerCase() === "put") {
|
|
177
|
-
try {
|
|
178
|
-
updatedHeaders.set("content-type", "application/json");
|
|
179
|
-
const bodyStream = _init.body ?? _init.request?.body ?? request2.body;
|
|
180
|
-
const bodyText = await new Response(bodyStream).text();
|
|
181
|
-
try {
|
|
182
|
-
params.body = JSON.stringify(JSON.parse(bodyText));
|
|
183
|
-
} catch {
|
|
184
|
-
updatedHeaders.set("content-type", "application/x-www-form-urlencoded");
|
|
185
|
-
params.body = bodyText;
|
|
186
|
-
}
|
|
187
|
-
} catch (e) {
|
|
188
|
-
error("Failed to parse request body");
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
params.headers = updatedHeaders;
|
|
192
|
-
const fullUrl = `${url}${requestUrl.search}`;
|
|
193
|
-
if (config.debug) {
|
|
194
|
-
params.headers.set("request-id", crypto.randomUUID());
|
|
195
|
-
params.cache = "no-store";
|
|
196
|
-
}
|
|
197
|
-
await config.extensionCtx?.runExtensions(
|
|
198
|
-
"onRequest" /* onRequest */,
|
|
199
|
-
config,
|
|
200
|
-
params,
|
|
201
|
-
_init
|
|
202
|
-
);
|
|
203
|
-
try {
|
|
204
|
-
const res = await fetch(fullUrl, {
|
|
205
|
-
...params
|
|
206
|
-
}).catch((e) => {
|
|
207
|
-
error("An error has occurred in the fetch", {
|
|
208
|
-
message: e.message,
|
|
209
|
-
stack: e.stack
|
|
210
|
-
});
|
|
211
|
-
return new Response(
|
|
212
|
-
"An unexpected (most likely configuration) problem has occurred",
|
|
213
|
-
{ status: 500 }
|
|
214
|
-
);
|
|
215
|
-
});
|
|
216
|
-
const loggingRes = typeof res?.clone === "function" ? res?.clone() : null;
|
|
217
|
-
info(`[${params.method ?? "GET"}] ${fullUrl}`, {
|
|
218
|
-
status: res?.status,
|
|
219
|
-
statusText: res?.statusText,
|
|
220
|
-
text: await loggingRes?.text()
|
|
221
|
-
});
|
|
222
|
-
const updatedRes = await config.extensionCtx?.runExtensions(
|
|
223
|
-
"onResponse" /* onResponse */,
|
|
224
|
-
config,
|
|
225
|
-
{ ...params, response: res }
|
|
226
|
-
);
|
|
227
|
-
if (updatedRes) {
|
|
228
|
-
return updatedRes;
|
|
229
|
-
}
|
|
230
|
-
return res;
|
|
231
|
-
} catch (e) {
|
|
232
|
-
if (e instanceof Error) {
|
|
233
|
-
error("An error has occurred in the fetch", {
|
|
234
|
-
message: e.message,
|
|
235
|
-
stack: e.stack
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
return new Response(
|
|
239
|
-
"An unexpected (most likely configuration) problem has occurred",
|
|
240
|
-
{ status: 500 }
|
|
241
|
-
);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
// src/api/utils/auth.ts
|
|
246
|
-
async function auth(req, config) {
|
|
247
|
-
const { info, error } = config.logger("[nileauth]");
|
|
248
|
-
info("checking auth");
|
|
249
|
-
const sessionUrl = `${config.apiUrl}/auth/session`;
|
|
250
|
-
info(`using session ${sessionUrl}`);
|
|
251
|
-
req.headers.delete("content-length");
|
|
252
|
-
const res = await request(sessionUrl, { request: req }, config);
|
|
253
|
-
try {
|
|
254
|
-
const session = await new Response(res.body).json();
|
|
255
|
-
if (Object.keys(session).length === 0) {
|
|
256
|
-
info("no session found");
|
|
257
|
-
return void 0;
|
|
258
|
-
}
|
|
259
|
-
info("session active");
|
|
260
|
-
return session;
|
|
261
|
-
} catch (e) {
|
|
262
|
-
error(e);
|
|
263
|
-
return void 0;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
134
|
// src/utils/Logger.ts
|
|
268
135
|
var red = "\x1B[31m";
|
|
269
136
|
var yellow = "\x1B[38;2;255;255;0m";
|
|
@@ -563,6 +430,160 @@ function serializeCookies(cookies) {
|
|
|
563
430
|
return Object.entries(cookies).map(([k, v]) => `${k}=${v}`).join("; ");
|
|
564
431
|
}
|
|
565
432
|
|
|
433
|
+
// src/api/utils/request.ts
|
|
434
|
+
async function request(url, _init, config) {
|
|
435
|
+
const { debug, info, error, warn: warn2 } = config.logger("[REQUEST]");
|
|
436
|
+
const { request: request2, ...init } = _init;
|
|
437
|
+
const requestUrl = new URL(request2.url);
|
|
438
|
+
const updatedHeaders = new Headers({});
|
|
439
|
+
if (request2.headers.get("cookie")) {
|
|
440
|
+
updatedHeaders.set("cookie", String(request2.headers.get("cookie")));
|
|
441
|
+
}
|
|
442
|
+
if (request2.headers.get(TENANT_COOKIE)) {
|
|
443
|
+
updatedHeaders.set(
|
|
444
|
+
TENANT_COOKIE,
|
|
445
|
+
String(request2.headers.get(TENANT_COOKIE))
|
|
446
|
+
);
|
|
447
|
+
}
|
|
448
|
+
if (config.secureCookies != null) {
|
|
449
|
+
updatedHeaders.set(HEADER_SECURE_COOKIES, String(config.secureCookies));
|
|
450
|
+
} else {
|
|
451
|
+
updatedHeaders.set(
|
|
452
|
+
HEADER_SECURE_COOKIES,
|
|
453
|
+
process.env.NODE_ENV === "production" ? "true" : "false"
|
|
454
|
+
);
|
|
455
|
+
}
|
|
456
|
+
updatedHeaders.set("host", requestUrl.host);
|
|
457
|
+
if (config.callbackUrl) {
|
|
458
|
+
const cbUrl = new URL(config.callbackUrl);
|
|
459
|
+
debug(`Obtained origin from config.callbackUrl ${config.callbackUrl}`);
|
|
460
|
+
updatedHeaders.set(HEADER_ORIGIN, cbUrl.origin);
|
|
461
|
+
} else if (config.origin) {
|
|
462
|
+
debug(`Obtained origin from config.origin ${config.origin}`);
|
|
463
|
+
updatedHeaders.set(HEADER_ORIGIN, config.origin);
|
|
464
|
+
} else {
|
|
465
|
+
const passedOrigin = request2.headers.get(HEADER_ORIGIN);
|
|
466
|
+
if (passedOrigin) {
|
|
467
|
+
updatedHeaders.set(HEADER_ORIGIN, passedOrigin);
|
|
468
|
+
} else {
|
|
469
|
+
const { headers } = ctx.get();
|
|
470
|
+
const host = headers.get("host");
|
|
471
|
+
if (host) {
|
|
472
|
+
const serverSideOrigin = `${getProtocolFromHeaders(headers)}://${host}`;
|
|
473
|
+
updatedHeaders.set(HEADER_ORIGIN, serverSideOrigin);
|
|
474
|
+
debug(`Obtained origin from server side headers ${serverSideOrigin}`);
|
|
475
|
+
} else {
|
|
476
|
+
const reqOrigin = config.routePrefix !== DEFAULT_PREFIX ? `${requestUrl.origin}${config.routePrefix}` : requestUrl.origin;
|
|
477
|
+
updatedHeaders.set(HEADER_ORIGIN, reqOrigin);
|
|
478
|
+
debug(`Obtained origin from request ${reqOrigin}`);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
const params = { ...init };
|
|
483
|
+
if (params.method?.toLowerCase() === "post" || params.method?.toLowerCase() === "put") {
|
|
484
|
+
try {
|
|
485
|
+
updatedHeaders.set("content-type", "application/json");
|
|
486
|
+
const bodyStream = _init.body ?? _init.request?.body ?? request2.body;
|
|
487
|
+
const bodyText = await new Response(bodyStream).text();
|
|
488
|
+
try {
|
|
489
|
+
params.body = JSON.stringify(JSON.parse(bodyText));
|
|
490
|
+
} catch {
|
|
491
|
+
updatedHeaders.set("content-type", "application/x-www-form-urlencoded");
|
|
492
|
+
params.body = bodyText;
|
|
493
|
+
}
|
|
494
|
+
} catch (e) {
|
|
495
|
+
error("Failed to parse request body");
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
params.headers = updatedHeaders;
|
|
499
|
+
const fullUrl = `${url}${requestUrl.search}`;
|
|
500
|
+
if (config.debug) {
|
|
501
|
+
params.headers.set("request-id", crypto.randomUUID());
|
|
502
|
+
params.cache = "no-store";
|
|
503
|
+
}
|
|
504
|
+
await config.extensionCtx?.runExtensions(
|
|
505
|
+
"onRequest" /* onRequest */,
|
|
506
|
+
config,
|
|
507
|
+
params,
|
|
508
|
+
_init
|
|
509
|
+
);
|
|
510
|
+
try {
|
|
511
|
+
const res = await fetch(fullUrl, {
|
|
512
|
+
...params
|
|
513
|
+
}).catch((e) => {
|
|
514
|
+
error("An error has occurred in the fetch", {
|
|
515
|
+
message: e.message,
|
|
516
|
+
stack: e.stack
|
|
517
|
+
});
|
|
518
|
+
return new Response(
|
|
519
|
+
"An unexpected (most likely configuration) problem has occurred",
|
|
520
|
+
{ status: 500 }
|
|
521
|
+
);
|
|
522
|
+
});
|
|
523
|
+
const loggingRes = typeof res?.clone === "function" ? res?.clone() : null;
|
|
524
|
+
info(`[${params.method ?? "GET"}] ${fullUrl}`, {
|
|
525
|
+
status: res?.status,
|
|
526
|
+
statusText: res?.statusText,
|
|
527
|
+
text: await loggingRes?.text()
|
|
528
|
+
});
|
|
529
|
+
const updatedRes = await config.extensionCtx?.runExtensions(
|
|
530
|
+
"onResponse" /* onResponse */,
|
|
531
|
+
config,
|
|
532
|
+
{ ...params, response: res }
|
|
533
|
+
);
|
|
534
|
+
if (updatedRes) {
|
|
535
|
+
return updatedRes;
|
|
536
|
+
}
|
|
537
|
+
return res;
|
|
538
|
+
} catch (e) {
|
|
539
|
+
if (e instanceof Error) {
|
|
540
|
+
error("An error has occurred in the fetch", {
|
|
541
|
+
message: e.message,
|
|
542
|
+
stack: e.stack
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
return new Response(
|
|
546
|
+
"An unexpected (most likely configuration) problem has occurred",
|
|
547
|
+
{ status: 500 }
|
|
548
|
+
);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
function getProtocolFromHeaders(headers) {
|
|
552
|
+
const get = (key17) => headers instanceof Headers ? headers.get(key17) : headers[key17.toLowerCase()];
|
|
553
|
+
const xfp = get("x-forwarded-proto");
|
|
554
|
+
if (xfp) return xfp.toLowerCase();
|
|
555
|
+
const forwarded = get("forwarded");
|
|
556
|
+
if (forwarded) {
|
|
557
|
+
const match = forwarded.match(/proto=(https?)/i);
|
|
558
|
+
if (match) return match[1].toLowerCase();
|
|
559
|
+
}
|
|
560
|
+
const ref = get("referer") || get("origin");
|
|
561
|
+
if (ref && ref.startsWith("https")) return "https";
|
|
562
|
+
return "http";
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// src/api/utils/auth.ts
|
|
566
|
+
async function auth(req, config) {
|
|
567
|
+
const { info, error } = config.logger("[nileauth]");
|
|
568
|
+
info("checking auth");
|
|
569
|
+
const sessionUrl = `${config.apiUrl}/auth/session`;
|
|
570
|
+
info(`using session ${sessionUrl}`);
|
|
571
|
+
req.headers.delete("content-length");
|
|
572
|
+
const res = await request(sessionUrl, { request: req }, config);
|
|
573
|
+
try {
|
|
574
|
+
const session = await new Response(res.body).json();
|
|
575
|
+
if (Object.keys(session).length === 0) {
|
|
576
|
+
info("no session found");
|
|
577
|
+
return void 0;
|
|
578
|
+
}
|
|
579
|
+
info("session active");
|
|
580
|
+
return session;
|
|
581
|
+
} catch (e) {
|
|
582
|
+
error(e);
|
|
583
|
+
return void 0;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
566
587
|
// src/api/routes/me/index.ts
|
|
567
588
|
var key = "ME";
|
|
568
589
|
async function route(request2, config) {
|
|
@@ -3214,7 +3235,6 @@ var Tenants = class {
|
|
|
3214
3235
|
return withNileContext(
|
|
3215
3236
|
this.#config,
|
|
3216
3237
|
async () => {
|
|
3217
|
-
await runExtensionContext(this.#config);
|
|
3218
3238
|
const { csrfToken } = await obtainCsrf(
|
|
3219
3239
|
this.#config
|
|
3220
3240
|
);
|
|
@@ -3264,8 +3284,8 @@ var Tenants = class {
|
|
|
3264
3284
|
throw new Error("The identifier and token are required.");
|
|
3265
3285
|
}
|
|
3266
3286
|
const { identifier, token } = req;
|
|
3267
|
-
const
|
|
3268
|
-
const callbackUrl =
|
|
3287
|
+
const { callbackUrl: cbUrl } = defaultCallbackUrl3(this.#config);
|
|
3288
|
+
const callbackUrl = fQUrl2(cbUrl, req?.callbackUrl ?? "/");
|
|
3269
3289
|
const res = await fetchInvite(
|
|
3270
3290
|
this.#config,
|
|
3271
3291
|
"PUT",
|