@getstrata/core 0.5.42 → 0.5.44
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/entries/admin/formatValue.js +32 -0
- package/dist/entries/admin/registry.js +32 -0
- package/dist/entries/audit/siemFormatter.js +37 -0
- package/dist/entries/auth/abilityChecker.js +1 -0
- package/dist/entries/auth/membershipMiddleware.js +298 -0
- package/dist/entries/auth/scimAuthMiddleware.js +251 -0
- package/dist/entries/auth/sessionGuard.js +72 -0
- package/dist/entries/database/migrations/types.js +1 -0
- package/dist/entries/database/migrations.js +127 -0
- package/dist/entries/database/schema.js +1054 -0
- package/dist/entries/database/seeders/types.js +1 -0
- package/dist/entries/http/conditionalResponse.js +192 -0
- package/dist/entries/http/corsMiddleware.js +54 -0
- package/dist/entries/http/csrfMiddleware.js +236 -0
- package/dist/entries/http/csrfToken.js +3 -0
- package/dist/entries/http/flashMiddleware.js +143 -0
- package/dist/entries/http/formRequest.js +152 -0
- package/dist/entries/http/loginThrottleMiddleware.js +46 -0
- package/dist/entries/http/memoryThrottleMiddleware.js +30 -0
- package/dist/entries/http/requireAbilityMiddleware.js +110 -0
- package/dist/entries/http/requireAuthMiddleware.js +80 -0
- package/dist/entries/http/requireGlobalAdminMiddleware.js +147 -0
- package/dist/entries/http/requireWebAuthMiddleware.js +107 -0
- package/dist/entries/http/route.js +8 -0
- package/dist/entries/http/routeMiddleware.js +32 -0
- package/dist/entries/http/routeModelBinding.js +141 -0
- package/dist/entries/http/scimThrottleMiddleware.js +23 -0
- package/dist/entries/http/securedRouteModelBinding.js +10 -0
- package/dist/entries/http/securityHeadersMiddleware.js +77 -0
- package/dist/entries/http/throttleMiddleware.js +87 -0
- package/dist/entries/http/webErrorResponse.js +72 -0
- package/dist/entries/http/webFormRequest.js +10 -0
- package/dist/entries/logging/requestLoggingMiddleware.js +89 -0
- package/dist/entries/mail/mailer.js +208 -0
- package/dist/entries/mail/markdownMail.js +63 -0
- package/dist/entries/mail/markdownMailable.js +78 -0
- package/dist/entries/notifications.js +152 -0
- package/dist/entries/openapi/generator.js +178 -0
- package/dist/entries/openapi/validate.js +28 -0
- package/dist/entries/queue/createAppQueue.js +58 -0
- package/dist/entries/queue/failedJobRepository.js +58 -0
- package/dist/entries/queue/publicQueue.js +58 -0
- package/dist/entries/queue/queueMetrics.js +58 -0
- package/dist/entries/runtime/asyncContextStore.js +17 -0
- package/dist/entries/security/safeFetch.js +211 -0
- package/dist/entries/security/timingSafeCompare.js +14 -0
- package/dist/entries/tenant/databaseTenantContext.js +116 -0
- package/dist/entries/tenant/tenantDatabaseScope.js +10 -0
- package/dist/entries/tracing/tracingMiddleware.js +103 -0
- package/dist/entries/view.js +72 -0
- package/package.json +202 -7
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/http/middleware.ts
|
|
3
|
+
function composeMiddleware(...middleware) {
|
|
4
|
+
return (handler) => {
|
|
5
|
+
return async (request) => {
|
|
6
|
+
let index = 0;
|
|
7
|
+
const dispatch = async () => {
|
|
8
|
+
if (index >= middleware.length) {
|
|
9
|
+
return await handler(request);
|
|
10
|
+
}
|
|
11
|
+
const current = middleware[index];
|
|
12
|
+
index += 1;
|
|
13
|
+
if (!current) {
|
|
14
|
+
return await handler(request);
|
|
15
|
+
}
|
|
16
|
+
return await current(request, dispatch);
|
|
17
|
+
};
|
|
18
|
+
return await dispatch();
|
|
19
|
+
};
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// ../../src/core/http/routeMiddleware.ts
|
|
24
|
+
function withMiddleware(...middleware) {
|
|
25
|
+
const wrap = composeMiddleware(...middleware);
|
|
26
|
+
return (handler) => {
|
|
27
|
+
return wrap(handler);
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export {
|
|
31
|
+
withMiddleware
|
|
32
|
+
};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/runtime/asyncContextStore.ts
|
|
3
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
4
|
+
function createAsyncContextStore(key) {
|
|
5
|
+
const symbol = Symbol.for(key);
|
|
6
|
+
const globalRecord = globalThis;
|
|
7
|
+
const existing = globalRecord[symbol];
|
|
8
|
+
if (existing) {
|
|
9
|
+
return existing;
|
|
10
|
+
}
|
|
11
|
+
const store = new AsyncLocalStorage;
|
|
12
|
+
globalRecord[symbol] = store;
|
|
13
|
+
return store;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// ../../src/core/auth/authContext.ts
|
|
17
|
+
var authContext = createAsyncContextStore("@getstrata/authContext");
|
|
18
|
+
function runWithAuthUser(user, callback) {
|
|
19
|
+
return authContext.run(user, callback);
|
|
20
|
+
}
|
|
21
|
+
function currentAuthUser() {
|
|
22
|
+
return authContext.getStore() ?? null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// ../../src/core/errors/http.ts
|
|
26
|
+
class HttpError extends Error {
|
|
27
|
+
status;
|
|
28
|
+
details;
|
|
29
|
+
constructor(status, message, details) {
|
|
30
|
+
super(message);
|
|
31
|
+
this.name = new.target.name;
|
|
32
|
+
this.status = status;
|
|
33
|
+
this.details = details;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
class BadRequestError extends HttpError {
|
|
38
|
+
constructor(message = "Bad Request", details) {
|
|
39
|
+
super(400, message, details);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
class NotFoundError extends HttpError {
|
|
44
|
+
constructor(message = "Not Found", details) {
|
|
45
|
+
super(404, message, details);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
class ConflictError extends HttpError {
|
|
50
|
+
constructor(message = "Conflict", details) {
|
|
51
|
+
super(409, message, details);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
class UnprocessableEntityError extends HttpError {
|
|
56
|
+
constructor(message = "Unprocessable Entity", details) {
|
|
57
|
+
super(422, message, details);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
class ValidationError extends HttpError {
|
|
62
|
+
constructor(message = "Validation failed", details) {
|
|
63
|
+
super(422, message, details);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
class ForbiddenError extends HttpError {
|
|
68
|
+
constructor(message = "Forbidden", details) {
|
|
69
|
+
super(403, message, details);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
class UnauthorizedError extends HttpError {
|
|
74
|
+
constructor(message = "Unauthorized", details) {
|
|
75
|
+
super(401, message, details);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
class PayloadTooLargeError extends HttpError {
|
|
80
|
+
constructor(message = "Payload Too Large", details) {
|
|
81
|
+
super(413, message, details);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
class PreconditionFailedError extends HttpError {
|
|
86
|
+
constructor(message = "Precondition Failed", details) {
|
|
87
|
+
super(412, message, details);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// ../../src/core/tenant/tenantContext.ts
|
|
92
|
+
var tenantContext = createAsyncContextStore("@getstrata/tenantContext");
|
|
93
|
+
function runWithTenant(tenant, callback) {
|
|
94
|
+
return tenantContext.run(tenant, callback);
|
|
95
|
+
}
|
|
96
|
+
function currentTenant() {
|
|
97
|
+
return tenantContext.getStore() ?? null;
|
|
98
|
+
}
|
|
99
|
+
function currentTenantId() {
|
|
100
|
+
return currentTenant()?.id ?? 1;
|
|
101
|
+
}
|
|
102
|
+
function rateLimitMultiplierForPlan(plan) {
|
|
103
|
+
switch (plan) {
|
|
104
|
+
case "enterprise":
|
|
105
|
+
return 4;
|
|
106
|
+
case "pro":
|
|
107
|
+
return 2;
|
|
108
|
+
default:
|
|
109
|
+
return 1;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ../../src/core/http/validation.ts
|
|
114
|
+
async function parseJsonBody(request, validator) {
|
|
115
|
+
let payload;
|
|
116
|
+
try {
|
|
117
|
+
payload = await request.json();
|
|
118
|
+
} catch {
|
|
119
|
+
throw new BadRequestError("Request body must be valid JSON.");
|
|
120
|
+
}
|
|
121
|
+
return validator(payload);
|
|
122
|
+
}
|
|
123
|
+
function parsePositiveIntParam(value, name = "id") {
|
|
124
|
+
const parsed = Number.parseInt(value, 10);
|
|
125
|
+
if (!Number.isInteger(parsed) || parsed <= 0) {
|
|
126
|
+
throw new BadRequestError(`Invalid ${name}. Expected a positive integer.`);
|
|
127
|
+
}
|
|
128
|
+
return parsed;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ../../src/core/http/routeModelBinding.ts
|
|
132
|
+
function bindRouteModel(param, resolver, handler) {
|
|
133
|
+
return async (request) => {
|
|
134
|
+
const id = parsePositiveIntParam(String(request.params[param]), String(param));
|
|
135
|
+
const model = await resolver(id, request);
|
|
136
|
+
return await handler(request, model);
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
export {
|
|
140
|
+
bindRouteModel
|
|
141
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/http/scimThrottleMiddleware.ts
|
|
3
|
+
var {RedisClient } = globalThis.Bun;
|
|
4
|
+
function createScimThrottleMiddleware(options) {
|
|
5
|
+
const client = options.redisUrl ? new RedisClient(options.redisUrl) : null;
|
|
6
|
+
return async (request, next) => {
|
|
7
|
+
const identity = request.headers.get("authorization")?.slice("Bearer ".length, "Bearer ".length + 16) ?? request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ?? "unknown";
|
|
8
|
+
const key = `workhub:scim-throttle:${identity}`;
|
|
9
|
+
if (client) {
|
|
10
|
+
const attempts = Number(await client.incr(key));
|
|
11
|
+
if (attempts === 1) {
|
|
12
|
+
await client.expire(key, options.decaySeconds);
|
|
13
|
+
}
|
|
14
|
+
if (attempts > options.maxAttempts) {
|
|
15
|
+
return Response.json({ error: "Too many SCIM requests." }, { status: 429, headers: { "retry-after": String(options.decaySeconds) } });
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return await next();
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
createScimThrottleMiddleware
|
|
23
|
+
};
|
|
@@ -288,6 +288,16 @@ function currentTenant() {
|
|
|
288
288
|
function currentTenantId() {
|
|
289
289
|
return currentTenant()?.id ?? 1;
|
|
290
290
|
}
|
|
291
|
+
function rateLimitMultiplierForPlan(plan) {
|
|
292
|
+
switch (plan) {
|
|
293
|
+
case "enterprise":
|
|
294
|
+
return 4;
|
|
295
|
+
case "pro":
|
|
296
|
+
return 2;
|
|
297
|
+
default:
|
|
298
|
+
return 1;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
291
301
|
|
|
292
302
|
// ../../src/core/http/validation.ts
|
|
293
303
|
async function parseJsonBody(request, validator) {
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/config/app.ts
|
|
3
|
+
var appConfig = {
|
|
4
|
+
name: "WorkHub",
|
|
5
|
+
env: process.env.APP_ENV ?? "local",
|
|
6
|
+
debug: (process.env.APP_DEBUG ?? "true") !== "false",
|
|
7
|
+
url: process.env.APP_URL ?? "http://localhost:3000",
|
|
8
|
+
apiPrefix: process.env.API_PREFIX ?? "/api/v1"
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// ../../src/config/contentSecurityPolicy.ts
|
|
12
|
+
function strictApiContentSecurityPolicy() {
|
|
13
|
+
return "default-src 'none'; frame-ancestors 'none'; base-uri 'none'";
|
|
14
|
+
}
|
|
15
|
+
function serverHtmxContentSecurityPolicy() {
|
|
16
|
+
return [
|
|
17
|
+
"default-src 'self'",
|
|
18
|
+
"script-src 'self' https://unpkg.com",
|
|
19
|
+
"style-src 'self'",
|
|
20
|
+
"connect-src 'self'",
|
|
21
|
+
"img-src 'self'",
|
|
22
|
+
"font-src 'self'",
|
|
23
|
+
"form-action 'self'",
|
|
24
|
+
"frame-ancestors 'none'",
|
|
25
|
+
"base-uri 'self'"
|
|
26
|
+
].join("; ");
|
|
27
|
+
}
|
|
28
|
+
function spaContentSecurityPolicy() {
|
|
29
|
+
return [
|
|
30
|
+
"default-src 'self'",
|
|
31
|
+
"script-src 'self'",
|
|
32
|
+
"style-src 'self'",
|
|
33
|
+
"connect-src 'self'",
|
|
34
|
+
"img-src 'self'",
|
|
35
|
+
"font-src 'self'",
|
|
36
|
+
"frame-ancestors 'none'",
|
|
37
|
+
"base-uri 'self'"
|
|
38
|
+
].join("; ");
|
|
39
|
+
}
|
|
40
|
+
function resolveContentSecurityPolicy(response) {
|
|
41
|
+
const contentType = response.headers.get("content-type")?.toLowerCase() ?? "";
|
|
42
|
+
if (!contentType.includes("text/html")) {
|
|
43
|
+
return strictApiContentSecurityPolicy();
|
|
44
|
+
}
|
|
45
|
+
const frontendMode = (process.env.FRONTEND_MODE ?? "api").trim();
|
|
46
|
+
if (frontendMode === "server-htmx") {
|
|
47
|
+
return serverHtmxContentSecurityPolicy();
|
|
48
|
+
}
|
|
49
|
+
if (frontendMode === "spa-react") {
|
|
50
|
+
return spaContentSecurityPolicy();
|
|
51
|
+
}
|
|
52
|
+
return strictApiContentSecurityPolicy();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ../../src/core/http/securityHeadersMiddleware.ts
|
|
56
|
+
function createSecurityHeadersMiddleware() {
|
|
57
|
+
return async (_request, next) => {
|
|
58
|
+
const response = await next();
|
|
59
|
+
const headers = new Headers(response.headers);
|
|
60
|
+
headers.set("X-Content-Type-Options", "nosniff");
|
|
61
|
+
headers.set("X-Frame-Options", "DENY");
|
|
62
|
+
headers.set("Referrer-Policy", "strict-origin-when-cross-origin");
|
|
63
|
+
headers.set("X-XSS-Protection", "0");
|
|
64
|
+
headers.set("Content-Security-Policy", resolveContentSecurityPolicy(response));
|
|
65
|
+
if (appConfig.env === "production") {
|
|
66
|
+
headers.set("Strict-Transport-Security", "max-age=31536000; includeSubDomains");
|
|
67
|
+
}
|
|
68
|
+
return new Response(response.body, {
|
|
69
|
+
status: response.status,
|
|
70
|
+
statusText: response.statusText,
|
|
71
|
+
headers
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
export {
|
|
76
|
+
createSecurityHeadersMiddleware
|
|
77
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/http/throttleMiddleware.ts
|
|
3
|
+
var {RedisClient } = globalThis.Bun;
|
|
4
|
+
|
|
5
|
+
// ../../src/core/runtime/asyncContextStore.ts
|
|
6
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
7
|
+
function createAsyncContextStore(key) {
|
|
8
|
+
const symbol = Symbol.for(key);
|
|
9
|
+
const globalRecord = globalThis;
|
|
10
|
+
const existing = globalRecord[symbol];
|
|
11
|
+
if (existing) {
|
|
12
|
+
return existing;
|
|
13
|
+
}
|
|
14
|
+
const store = new AsyncLocalStorage;
|
|
15
|
+
globalRecord[symbol] = store;
|
|
16
|
+
return store;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// ../../src/core/auth/authContext.ts
|
|
20
|
+
var authContext = createAsyncContextStore("@getstrata/authContext");
|
|
21
|
+
function runWithAuthUser(user, callback) {
|
|
22
|
+
return authContext.run(user, callback);
|
|
23
|
+
}
|
|
24
|
+
function currentAuthUser() {
|
|
25
|
+
return authContext.getStore() ?? null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// ../../src/core/tenant/tenantContext.ts
|
|
29
|
+
var tenantContext = createAsyncContextStore("@getstrata/tenantContext");
|
|
30
|
+
function runWithTenant(tenant, callback) {
|
|
31
|
+
return tenantContext.run(tenant, callback);
|
|
32
|
+
}
|
|
33
|
+
function currentTenant() {
|
|
34
|
+
return tenantContext.getStore() ?? null;
|
|
35
|
+
}
|
|
36
|
+
function currentTenantId() {
|
|
37
|
+
return currentTenant()?.id ?? 1;
|
|
38
|
+
}
|
|
39
|
+
function rateLimitMultiplierForPlan(plan) {
|
|
40
|
+
switch (plan) {
|
|
41
|
+
case "enterprise":
|
|
42
|
+
return 4;
|
|
43
|
+
case "pro":
|
|
44
|
+
return 2;
|
|
45
|
+
default:
|
|
46
|
+
return 1;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ../../src/core/http/throttleMiddleware.ts
|
|
51
|
+
function resolveThrottleIdentity(request) {
|
|
52
|
+
const user = currentAuthUser();
|
|
53
|
+
if (user?.tokenId !== undefined) {
|
|
54
|
+
return `token:${user.tokenId}`;
|
|
55
|
+
}
|
|
56
|
+
if (user) {
|
|
57
|
+
return `user:${user.id}`;
|
|
58
|
+
}
|
|
59
|
+
return request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ?? "unknown";
|
|
60
|
+
}
|
|
61
|
+
function createThrottleMiddleware(options) {
|
|
62
|
+
const client = new RedisClient(options.redisUrl);
|
|
63
|
+
const prefix = options.keyPrefix ?? "workhub:throttle:";
|
|
64
|
+
return async (request, next) => {
|
|
65
|
+
const identity = resolveThrottleIdentity(request);
|
|
66
|
+
const path = new URL(request.url).pathname;
|
|
67
|
+
const throttleKey = `${prefix}${identity}:${path}`;
|
|
68
|
+
const attempts = Number(await client.incr(throttleKey));
|
|
69
|
+
if (attempts === 1) {
|
|
70
|
+
await client.expire(throttleKey, options.decaySeconds);
|
|
71
|
+
}
|
|
72
|
+
const maxAttempts = options.maxAttempts * rateLimitMultiplierForPlan(currentTenant()?.plan ?? "free");
|
|
73
|
+
if (attempts > maxAttempts) {
|
|
74
|
+
return Response.json({ error: "Too many requests." }, {
|
|
75
|
+
status: 429,
|
|
76
|
+
headers: {
|
|
77
|
+
"retry-after": String(options.decaySeconds)
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
return await next();
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
export {
|
|
85
|
+
resolveThrottleIdentity,
|
|
86
|
+
createThrottleMiddleware
|
|
87
|
+
};
|
|
@@ -2153,6 +2153,45 @@ class Blueprint {
|
|
|
2153
2153
|
});
|
|
2154
2154
|
}
|
|
2155
2155
|
}
|
|
2156
|
+
// ../../src/core/database/schema/driver.ts
|
|
2157
|
+
function normalizeConnectionName(connection) {
|
|
2158
|
+
const normalized = connection.trim().toLowerCase();
|
|
2159
|
+
if (normalized === "pgsql" || normalized === "postgres" || normalized === "postgresql") {
|
|
2160
|
+
return "pgsql";
|
|
2161
|
+
}
|
|
2162
|
+
if (normalized === "mysql" || normalized === "mariadb") {
|
|
2163
|
+
return "mysql";
|
|
2164
|
+
}
|
|
2165
|
+
if (normalized === "sqlite") {
|
|
2166
|
+
return "sqlite";
|
|
2167
|
+
}
|
|
2168
|
+
throw new Error(`Unsupported DB_CONNECTION: ${connection}`);
|
|
2169
|
+
}
|
|
2170
|
+
function resolveDriverFromUrl(url) {
|
|
2171
|
+
const normalized = url.trim().toLowerCase();
|
|
2172
|
+
if (normalized.startsWith("postgres://") || normalized.startsWith("postgresql://") || normalized.startsWith("postgres:")) {
|
|
2173
|
+
return "pgsql";
|
|
2174
|
+
}
|
|
2175
|
+
if (normalized.startsWith("mysql://") || normalized.startsWith("mysql:")) {
|
|
2176
|
+
return "mysql";
|
|
2177
|
+
}
|
|
2178
|
+
if (normalized.startsWith("sqlite:")) {
|
|
2179
|
+
return "sqlite";
|
|
2180
|
+
}
|
|
2181
|
+
return null;
|
|
2182
|
+
}
|
|
2183
|
+
function resolveDatabaseDriver(options = {}) {
|
|
2184
|
+
const connection = options.connection ?? process.env.DB_CONNECTION;
|
|
2185
|
+
if (connection) {
|
|
2186
|
+
return normalizeConnectionName(connection);
|
|
2187
|
+
}
|
|
2188
|
+
const url = options.url ?? process.env.DATABASE_URL ?? "";
|
|
2189
|
+
const fromUrl = resolveDriverFromUrl(url);
|
|
2190
|
+
if (fromUrl) {
|
|
2191
|
+
return fromUrl;
|
|
2192
|
+
}
|
|
2193
|
+
return "pgsql";
|
|
2194
|
+
}
|
|
2156
2195
|
// ../../src/core/database/schema/errors.ts
|
|
2157
2196
|
class UnsupportedSchemaFeatureError extends Error {
|
|
2158
2197
|
constructor(feature, driver) {
|
|
@@ -2497,6 +2536,25 @@ class SchemaBuilder {
|
|
|
2497
2536
|
}
|
|
2498
2537
|
}
|
|
2499
2538
|
}
|
|
2539
|
+
|
|
2540
|
+
class Schema {
|
|
2541
|
+
static builder(driver) {
|
|
2542
|
+
return new SchemaBuilder(driver ?? resolveDatabaseDriver());
|
|
2543
|
+
}
|
|
2544
|
+
static async run(db, driver, callback) {
|
|
2545
|
+
const schema = Schema.builder(driver);
|
|
2546
|
+
await callback(schema);
|
|
2547
|
+
await schema.execute(db);
|
|
2548
|
+
}
|
|
2549
|
+
}
|
|
2550
|
+
function createSchemaBuilder(db, driver) {
|
|
2551
|
+
const builder = Schema.builder(driver);
|
|
2552
|
+
return Object.assign(builder, {
|
|
2553
|
+
async commit() {
|
|
2554
|
+
await builder.execute(db);
|
|
2555
|
+
}
|
|
2556
|
+
});
|
|
2557
|
+
}
|
|
2500
2558
|
// ../../src/core/database/table.ts
|
|
2501
2559
|
function defineTable(definition) {
|
|
2502
2560
|
return definition;
|
|
@@ -2567,6 +2625,7 @@ var db = new Proxy(function database() {}, {
|
|
|
2567
2625
|
return typeof value === "function" ? value.bind(connection) : value;
|
|
2568
2626
|
}
|
|
2569
2627
|
});
|
|
2628
|
+
var connection_default = db;
|
|
2570
2629
|
|
|
2571
2630
|
// ../../src/modules/user/apiTokenTable.ts
|
|
2572
2631
|
var apiTokenTable = defineTable({
|
|
@@ -2708,6 +2767,9 @@ function currentAuthUser() {
|
|
|
2708
2767
|
|
|
2709
2768
|
// ../../src/core/http/requestMetaContext.ts
|
|
2710
2769
|
var requestMetaContext = createAsyncContextStore("@getstrata/requestMetaContext");
|
|
2770
|
+
function runWithRequestMeta(meta, callback) {
|
|
2771
|
+
return requestMetaContext.run(meta, callback);
|
|
2772
|
+
}
|
|
2711
2773
|
function currentRequestMeta() {
|
|
2712
2774
|
return requestMetaContext.getStore() ?? {
|
|
2713
2775
|
ipAddress: null,
|
|
@@ -2801,6 +2863,16 @@ function currentTenant() {
|
|
|
2801
2863
|
function currentTenantId() {
|
|
2802
2864
|
return currentTenant()?.id ?? 1;
|
|
2803
2865
|
}
|
|
2866
|
+
function rateLimitMultiplierForPlan(plan) {
|
|
2867
|
+
switch (plan) {
|
|
2868
|
+
case "enterprise":
|
|
2869
|
+
return 4;
|
|
2870
|
+
case "pro":
|
|
2871
|
+
return 2;
|
|
2872
|
+
default:
|
|
2873
|
+
return 1;
|
|
2874
|
+
}
|
|
2875
|
+
}
|
|
2804
2876
|
|
|
2805
2877
|
// ../../src/domain/abilities.ts
|
|
2806
2878
|
var MEMBER_ABILITIES = [
|
|
@@ -140,6 +140,16 @@ function currentTenant() {
|
|
|
140
140
|
function currentTenantId() {
|
|
141
141
|
return currentTenant()?.id ?? 1;
|
|
142
142
|
}
|
|
143
|
+
function rateLimitMultiplierForPlan(plan) {
|
|
144
|
+
switch (plan) {
|
|
145
|
+
case "enterprise":
|
|
146
|
+
return 4;
|
|
147
|
+
case "pro":
|
|
148
|
+
return 2;
|
|
149
|
+
default:
|
|
150
|
+
return 1;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
143
153
|
|
|
144
154
|
// ../../src/core/http/validation.ts
|
|
145
155
|
async function parseJsonBody(request, validator) {
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/runtime/asyncContextStore.ts
|
|
3
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
4
|
+
function createAsyncContextStore(key) {
|
|
5
|
+
const symbol = Symbol.for(key);
|
|
6
|
+
const globalRecord = globalThis;
|
|
7
|
+
const existing = globalRecord[symbol];
|
|
8
|
+
if (existing) {
|
|
9
|
+
return existing;
|
|
10
|
+
}
|
|
11
|
+
const store = new AsyncLocalStorage;
|
|
12
|
+
globalRecord[symbol] = store;
|
|
13
|
+
return store;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// ../../src/core/http/requestMetaContext.ts
|
|
17
|
+
var requestMetaContext = createAsyncContextStore("@getstrata/requestMetaContext");
|
|
18
|
+
function runWithRequestMeta(meta, callback) {
|
|
19
|
+
return requestMetaContext.run(meta, callback);
|
|
20
|
+
}
|
|
21
|
+
function currentRequestMeta() {
|
|
22
|
+
return requestMetaContext.getStore() ?? {
|
|
23
|
+
ipAddress: null,
|
|
24
|
+
userAgent: null
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// ../../src/core/logging/logger.ts
|
|
29
|
+
class Logger {
|
|
30
|
+
channel;
|
|
31
|
+
constructor(channel = "app") {
|
|
32
|
+
this.channel = channel;
|
|
33
|
+
}
|
|
34
|
+
write(level, message, context = {}) {
|
|
35
|
+
const entry = {
|
|
36
|
+
level,
|
|
37
|
+
channel: this.channel,
|
|
38
|
+
message,
|
|
39
|
+
timestamp: new Date().toISOString(),
|
|
40
|
+
...context
|
|
41
|
+
};
|
|
42
|
+
const line = JSON.stringify(entry);
|
|
43
|
+
if (level === "error") {
|
|
44
|
+
console.error(line);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
console.log(line);
|
|
48
|
+
}
|
|
49
|
+
debug(message, context) {
|
|
50
|
+
this.write("debug", message, context);
|
|
51
|
+
}
|
|
52
|
+
info(message, context) {
|
|
53
|
+
this.write("info", message, context);
|
|
54
|
+
}
|
|
55
|
+
warn(message, context) {
|
|
56
|
+
this.write("warn", message, context);
|
|
57
|
+
}
|
|
58
|
+
error(message, context) {
|
|
59
|
+
this.write("error", message, context);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
var appLogger = new Logger("app");
|
|
63
|
+
|
|
64
|
+
// ../../src/core/logging/requestLoggingMiddleware.ts
|
|
65
|
+
function createRequestLoggingMiddleware() {
|
|
66
|
+
return async (request, next) => {
|
|
67
|
+
return await runWithRequestMeta({
|
|
68
|
+
ipAddress: request.headers.get("x-forwarded-for")?.split(",")[0]?.trim() ?? request.headers.get("x-real-ip"),
|
|
69
|
+
userAgent: request.headers.get("user-agent"),
|
|
70
|
+
request
|
|
71
|
+
}, async () => {
|
|
72
|
+
const startedAt = performance.now();
|
|
73
|
+
const requestId = request.headers.get("x-request-id") ?? crypto.randomUUID();
|
|
74
|
+
const response = await next();
|
|
75
|
+
const durationMs = Math.round(performance.now() - startedAt);
|
|
76
|
+
appLogger.info("HTTP request completed", {
|
|
77
|
+
requestId,
|
|
78
|
+
method: request.method,
|
|
79
|
+
path: new URL(request.url).pathname,
|
|
80
|
+
status: response.status,
|
|
81
|
+
durationMs
|
|
82
|
+
});
|
|
83
|
+
return response;
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
export {
|
|
88
|
+
createRequestLoggingMiddleware
|
|
89
|
+
};
|