@getstrata/core 0.4.0 → 0.5.0
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/bootstrap/httpKernel.d.ts +2 -0
- package/dist/config/queue.d.ts +8 -0
- package/dist/config/rateLimit.d.ts +2 -1
- package/dist/core/database/baseRepository.d.ts +16 -4
- package/dist/core/database/index.d.ts +10 -4
- package/dist/core/database/migrations/advisoryLock.d.ts +6 -0
- package/dist/core/database/migrations/runner.d.ts +8 -2
- package/dist/core/database/migrations/types.d.ts +1 -0
- package/dist/core/database/model.d.ts +46 -17
- package/dist/core/database/query.d.ts +25 -14
- package/dist/core/database/relationships.d.ts +32 -2
- package/dist/core/database/repositoryQuery.d.ts +16 -1
- package/dist/core/database/schema/blueprint.d.ts +47 -0
- package/dist/core/database/schema/columnDefinition.d.ts +36 -0
- package/dist/core/database/schema/driver.d.ts +8 -0
- package/dist/core/database/schema/errors.d.ts +4 -0
- package/dist/core/database/schema/grammars/compileStatements.d.ts +8 -0
- package/dist/core/database/schema/grammars/createGrammar.d.ts +4 -0
- package/dist/core/database/schema/grammars/grammar.d.ts +10 -0
- package/dist/core/database/schema/grammars/index.d.ts +7 -0
- package/dist/core/database/schema/grammars/mysqlGrammar.d.ts +2 -0
- package/dist/core/database/schema/grammars/postgresGrammar.d.ts +2 -0
- package/dist/core/database/schema/grammars/sqliteGrammar.d.ts +2 -0
- package/dist/core/database/schema/index.d.ts +12 -0
- package/dist/core/database/schema/schema.d.ts +21 -0
- package/dist/core/database/seeders/runner.d.ts +6 -0
- package/dist/core/database/seeders/types.d.ts +8 -0
- package/dist/core/database/types.d.ts +39 -2
- package/dist/core/database/whereBuilder.d.ts +17 -0
- package/dist/core/http/index.d.ts +1 -1
- package/dist/core/http/securedRouteModelBinding.d.ts +2 -1
- package/dist/core/lifecycle/gracefulShutdown.d.ts +6 -0
- package/dist/core/pagination/index.d.ts +11 -1
- package/dist/core/queue/failedJobRepository.d.ts +6 -0
- package/dist/core/queue/failedJobService.d.ts +15 -0
- package/dist/core/queue/failedJobTable.d.ts +3 -0
- package/dist/core/queue/jobRegistry.d.ts +14 -0
- package/dist/core/queue/jobRunner.d.ts +9 -0
- package/dist/core/queue/publicQueue.d.ts +15 -0
- package/dist/core/queue/redisQueue.d.ts +29 -0
- package/dist/core/queue/resilientQueue.d.ts +9 -0
- package/dist/core/queue/types.d.ts +8 -0
- package/dist/core/scheduler/schedule.d.ts +15 -0
- package/dist/entries/auth/accessControl.js +113 -0
- package/dist/entries/auth/authContext.js +15 -0
- package/dist/entries/auth/guard.js +2870 -0
- package/dist/entries/auth/membershipContext.js +276 -0
- package/dist/entries/auth/membershipScope.js +390 -0
- package/dist/entries/auth/membershipService.js +477 -0
- package/dist/entries/auth/oauth/oidcProvider.js +49 -0
- package/dist/entries/auth/oauth/providers.js +71 -0
- package/dist/entries/auth/oauth/samlProvider.js +26 -0
- package/dist/entries/auth/oauth/types.js +1 -0
- package/dist/entries/auth/password.js +15 -0
- package/dist/entries/auth/policy.js +134 -0
- package/dist/entries/auth/sessionCookie.js +75 -0
- package/dist/entries/auth/tokenHash.js +17 -0
- package/dist/entries/cache/tags.js +13 -0
- package/dist/entries/crypto/fieldEncryption.js +93 -0
- package/dist/entries/crypto/mfaSecret.js +105 -0
- package/dist/entries/database/factory.js +17 -0
- package/dist/entries/database/seeders.js +30 -0
- package/dist/entries/database/types.js +1 -0
- package/dist/entries/database.js +2218 -0
- package/dist/entries/errors/http.js +78 -0
- package/dist/entries/http/contentNegotiation.js +26 -0
- package/dist/entries/http/csrfToken.js +147 -0
- package/dist/entries/http/etag.js +170 -0
- package/dist/entries/http/flashSession.js +106 -0
- package/dist/entries/http/middleware.js +68 -0
- package/dist/entries/http/parseFormBody.js +88 -0
- package/dist/entries/http/requestMetaContext.js +18 -0
- package/dist/entries/http/resources.js +19 -0
- package/dist/entries/http/webErrorResponse.js +3143 -0
- package/dist/entries/http/webFormRequest.js +293 -0
- package/dist/entries/http.js +3924 -0
- package/dist/entries/jobs/dispatchWebhookJob.js +342 -0
- package/dist/entries/lifecycle/gracefulShutdown.js +50 -0
- package/dist/entries/metrics/prometheus.js +70 -0
- package/dist/entries/pagination.js +14 -0
- package/dist/entries/queue/createAppQueue.js +2848 -0
- package/dist/entries/queue/failedJobService.js +38 -0
- package/dist/entries/queue/jobRegistry.js +32 -0
- package/dist/entries/queue/jobRunner.js +75 -0
- package/dist/entries/queue/publicQueue.js +2476 -0
- package/dist/entries/queue/queueMetrics.js +2898 -0
- package/dist/entries/queue/types.js +1 -0
- package/dist/entries/security/oauthState.js +75 -0
- package/dist/entries/security/publicReads.js +33 -0
- package/dist/entries/security/safeUrl.js +143 -0
- package/dist/entries/security/securityEvents.js +41 -0
- package/dist/entries/security/stripeWebhook.js +115 -0
- package/dist/entries/security/tokenExpiry.js +16 -0
- package/dist/entries/security/totp.js +51 -0
- package/dist/entries/storage/storage.js +123 -0
- package/dist/entries/tenant/tenantContext.js +30 -0
- package/dist/entries/tenant/tenantMiddleware.js +312 -0
- package/dist/entries/tracing/traceContext.js +15 -0
- package/dist/entries/validation/rules.js +232 -0
- package/dist/entries/view.js +3072 -0
- package/dist/framework/public-api.d.ts +65 -38
- package/dist/index.js +3198 -308
- package/dist/modules/user/apiTokenRepository.d.ts +1 -1
- package/dist/modules/user/apiTokenTable.d.ts +1 -1
- package/dist/modules/user/authService.d.ts +1 -1
- package/dist/modules/user/notificationRepository.d.ts +1 -1
- package/dist/modules/user/notificationService.d.ts +1 -1
- package/dist/modules/user/notificationTable.d.ts +1 -1
- package/dist/modules/user/oauthIdentityRepository.d.ts +2 -2
- package/dist/modules/user/provider.d.ts +1 -1
- package/dist/modules/user/repository.d.ts +1 -1
- package/dist/modules/user/table.d.ts +1 -1
- package/dist/modules/user/tokenService.d.ts +1 -1
- package/package.json +289 -3
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/auth/membershipContext.ts
|
|
3
|
+
import { AsyncLocalStorage as AsyncLocalStorage3 } from "async_hooks";
|
|
4
|
+
|
|
5
|
+
// ../../src/config/database.ts
|
|
6
|
+
function readInteger(name, fallback) {
|
|
7
|
+
const parsed = Number.parseInt(process.env[name] ?? String(fallback), 10);
|
|
8
|
+
return Number.isInteger(parsed) && parsed >= 0 ? parsed : fallback;
|
|
9
|
+
}
|
|
10
|
+
var databaseConfig = {
|
|
11
|
+
url: process.env.DATABASE_URL ?? "",
|
|
12
|
+
poolMax: readInteger("DB_POOL_MAX", 10),
|
|
13
|
+
idleTimeoutSeconds: readInteger("DB_POOL_IDLE_TIMEOUT", 30),
|
|
14
|
+
maxLifetimeSeconds: readInteger("DB_POOL_MAX_LIFETIME", 3600),
|
|
15
|
+
connectionTimeoutSeconds: readInteger("DB_CONNECTION_TIMEOUT", 10)
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// ../../src/core/database/connectionContext.ts
|
|
19
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
20
|
+
var activeConnection = new AsyncLocalStorage;
|
|
21
|
+
function runWithDatabaseConnection(connection, callback) {
|
|
22
|
+
return activeConnection.run(connection, callback);
|
|
23
|
+
}
|
|
24
|
+
function getActiveDatabaseConnection(fallback) {
|
|
25
|
+
return activeConnection.getStore() ?? fallback;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// ../../src/db/connection/createConnection.ts
|
|
29
|
+
var {SQL } = globalThis.Bun;
|
|
30
|
+
function createDatabaseConnection(config) {
|
|
31
|
+
if (!config.url) {
|
|
32
|
+
throw new Error("DATABASE_URL is not configured. Set DATABASE_URL before starting the app or running integration tests.");
|
|
33
|
+
}
|
|
34
|
+
return new SQL({
|
|
35
|
+
url: config.url,
|
|
36
|
+
max: config.poolMax,
|
|
37
|
+
idleTimeout: config.idleTimeoutSeconds,
|
|
38
|
+
maxLifetime: config.maxLifetimeSeconds,
|
|
39
|
+
connectionTimeout: config.connectionTimeoutSeconds
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ../../src/db/connection/index.ts
|
|
44
|
+
var connectionHolder = {
|
|
45
|
+
connection: null
|
|
46
|
+
};
|
|
47
|
+
function getDatabase() {
|
|
48
|
+
if (!connectionHolder.connection) {
|
|
49
|
+
connectionHolder.connection = createDatabaseConnection(databaseConfig);
|
|
50
|
+
}
|
|
51
|
+
return connectionHolder.connection;
|
|
52
|
+
}
|
|
53
|
+
var POOL_CONNECTION_METHODS = new Set(["begin", "close", "connect"]);
|
|
54
|
+
function resolveDatabase() {
|
|
55
|
+
return getActiveDatabaseConnection(getDatabase());
|
|
56
|
+
}
|
|
57
|
+
function resolveDatabaseForProperty(property) {
|
|
58
|
+
if (typeof property === "string" && POOL_CONNECTION_METHODS.has(property)) {
|
|
59
|
+
return getDatabase();
|
|
60
|
+
}
|
|
61
|
+
return resolveDatabase();
|
|
62
|
+
}
|
|
63
|
+
var db = new Proxy(function database() {}, {
|
|
64
|
+
apply(_target, _thisArg, args) {
|
|
65
|
+
return resolveDatabase()(...args);
|
|
66
|
+
},
|
|
67
|
+
get(_target, property) {
|
|
68
|
+
const connection = resolveDatabaseForProperty(property);
|
|
69
|
+
const value = connection[property];
|
|
70
|
+
return typeof value === "function" ? value.bind(connection) : value;
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
var connection_default = db;
|
|
74
|
+
|
|
75
|
+
// ../../src/modules/organization/memberRepository.ts
|
|
76
|
+
class OrganizationMemberRepository {
|
|
77
|
+
constructor() {}
|
|
78
|
+
async findMembership(userId, organizationId) {
|
|
79
|
+
const rows = await connection_default`
|
|
80
|
+
SELECT id, organization_id, user_id, role, created_at
|
|
81
|
+
FROM organization_member
|
|
82
|
+
WHERE user_id = ${userId} AND organization_id = ${organizationId}
|
|
83
|
+
LIMIT 1
|
|
84
|
+
`;
|
|
85
|
+
return rows[0] ?? null;
|
|
86
|
+
}
|
|
87
|
+
async listForUser(userId) {
|
|
88
|
+
return await connection_default`
|
|
89
|
+
SELECT id, organization_id, user_id, role, created_at
|
|
90
|
+
FROM organization_member
|
|
91
|
+
WHERE user_id = ${userId}
|
|
92
|
+
ORDER BY organization_id
|
|
93
|
+
`;
|
|
94
|
+
}
|
|
95
|
+
async listForOrganization(organizationId) {
|
|
96
|
+
return await connection_default`
|
|
97
|
+
SELECT id, organization_id, user_id, role, created_at
|
|
98
|
+
FROM organization_member
|
|
99
|
+
WHERE organization_id = ${organizationId}
|
|
100
|
+
ORDER BY id
|
|
101
|
+
`;
|
|
102
|
+
}
|
|
103
|
+
async addMember(input) {
|
|
104
|
+
const rows = await connection_default`
|
|
105
|
+
INSERT INTO organization_member (organization_id, user_id, role)
|
|
106
|
+
VALUES (${input.organizationId}, ${input.userId}, ${input.role ?? "member"})
|
|
107
|
+
RETURNING id, organization_id, user_id, role, created_at
|
|
108
|
+
`;
|
|
109
|
+
const row = rows[0];
|
|
110
|
+
if (!row) {
|
|
111
|
+
throw new Error("Organization member insert did not return a row.");
|
|
112
|
+
}
|
|
113
|
+
return row;
|
|
114
|
+
}
|
|
115
|
+
async removeMember(organizationId, userId) {
|
|
116
|
+
const rows = await connection_default`
|
|
117
|
+
DELETE FROM organization_member
|
|
118
|
+
WHERE organization_id = ${organizationId} AND user_id = ${userId}
|
|
119
|
+
RETURNING id
|
|
120
|
+
`;
|
|
121
|
+
return rows.length > 0;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
var memberRepository_default = OrganizationMemberRepository;
|
|
125
|
+
|
|
126
|
+
// ../../src/core/errors/http.ts
|
|
127
|
+
class HttpError extends Error {
|
|
128
|
+
status;
|
|
129
|
+
details;
|
|
130
|
+
constructor(status, message, details) {
|
|
131
|
+
super(message);
|
|
132
|
+
this.name = new.target.name;
|
|
133
|
+
this.status = status;
|
|
134
|
+
this.details = details;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
class BadRequestError extends HttpError {
|
|
139
|
+
constructor(message = "Bad Request", details) {
|
|
140
|
+
super(400, message, details);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
class NotFoundError extends HttpError {
|
|
145
|
+
constructor(message = "Not Found", details) {
|
|
146
|
+
super(404, message, details);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
class ConflictError extends HttpError {
|
|
151
|
+
constructor(message = "Conflict", details) {
|
|
152
|
+
super(409, message, details);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
class UnprocessableEntityError extends HttpError {
|
|
157
|
+
constructor(message = "Unprocessable Entity", details) {
|
|
158
|
+
super(422, message, details);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
class ValidationError extends HttpError {
|
|
163
|
+
constructor(message = "Validation failed", details) {
|
|
164
|
+
super(422, message, details);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
class ForbiddenError extends HttpError {
|
|
169
|
+
constructor(message = "Forbidden", details) {
|
|
170
|
+
super(403, message, details);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
class UnauthorizedError extends HttpError {
|
|
175
|
+
constructor(message = "Unauthorized", details) {
|
|
176
|
+
super(401, message, details);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
class PayloadTooLargeError extends HttpError {
|
|
181
|
+
constructor(message = "Payload Too Large", details) {
|
|
182
|
+
super(413, message, details);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
class PreconditionFailedError extends HttpError {
|
|
187
|
+
constructor(message = "Precondition Failed", details) {
|
|
188
|
+
super(412, message, details);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// ../../src/core/auth/authContext.ts
|
|
193
|
+
import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
|
|
194
|
+
var authContext = new AsyncLocalStorage2;
|
|
195
|
+
function runWithAuthUser(user, callback) {
|
|
196
|
+
return authContext.run(user, callback);
|
|
197
|
+
}
|
|
198
|
+
function currentAuthUser() {
|
|
199
|
+
return authContext.getStore() ?? null;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// ../../src/core/auth/accessControl.ts
|
|
203
|
+
var ROLE_RANK = {
|
|
204
|
+
member: 1,
|
|
205
|
+
admin: 2,
|
|
206
|
+
owner: 3
|
|
207
|
+
};
|
|
208
|
+
function isGlobalAdmin(user) {
|
|
209
|
+
return user?.role === "admin";
|
|
210
|
+
}
|
|
211
|
+
function hasMinimumOrgRole(role, minimum) {
|
|
212
|
+
if (!role) {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
return ROLE_RANK[role] >= ROLE_RANK[minimum];
|
|
216
|
+
}
|
|
217
|
+
function requireAuthenticatedUser() {
|
|
218
|
+
const user = currentAuthUser();
|
|
219
|
+
if (!user) {
|
|
220
|
+
throw new ForbiddenError("Authentication required.");
|
|
221
|
+
}
|
|
222
|
+
return user;
|
|
223
|
+
}
|
|
224
|
+
function resolveUserId(user) {
|
|
225
|
+
const userId = typeof user.id === "number" ? user.id : Number(user.id);
|
|
226
|
+
if (!Number.isInteger(userId) || userId <= 0) {
|
|
227
|
+
throw new ForbiddenError("Invalid authenticated user.");
|
|
228
|
+
}
|
|
229
|
+
return userId;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// ../../src/core/auth/membershipContext.ts
|
|
233
|
+
var membershipContext = new AsyncLocalStorage3;
|
|
234
|
+
var membershipRepository = new memberRepository_default;
|
|
235
|
+
async function runWithMembershipContext(callback) {
|
|
236
|
+
const user = currentAuthUser();
|
|
237
|
+
if (!user || isGlobalAdmin(user)) {
|
|
238
|
+
return await callback();
|
|
239
|
+
}
|
|
240
|
+
const memberships = await membershipRepository.listForUser(resolveUserId(user));
|
|
241
|
+
const context = {
|
|
242
|
+
organizationIds: memberships.map((membership) => membership.organization_id),
|
|
243
|
+
rolesByOrganizationId: new Map(memberships.map((membership) => [membership.organization_id, membership.role]))
|
|
244
|
+
};
|
|
245
|
+
return await membershipContext.run(context, callback);
|
|
246
|
+
}
|
|
247
|
+
function currentOrgRole(organizationId) {
|
|
248
|
+
return membershipContext.getStore()?.rolesByOrganizationId.get(organizationId) ?? null;
|
|
249
|
+
}
|
|
250
|
+
function hasOrgMembership(organizationId) {
|
|
251
|
+
return currentOrgRole(organizationId) !== null;
|
|
252
|
+
}
|
|
253
|
+
function currentOrganizationIds() {
|
|
254
|
+
return membershipContext.getStore()?.organizationIds ?? [];
|
|
255
|
+
}
|
|
256
|
+
function hasMinimumOrgRole2(organizationId, minimum) {
|
|
257
|
+
const role = currentOrgRole(organizationId);
|
|
258
|
+
if (!role) {
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
const ranks = {
|
|
262
|
+
member: 1,
|
|
263
|
+
admin: 2,
|
|
264
|
+
owner: 3
|
|
265
|
+
};
|
|
266
|
+
return ranks[role] >= ranks[minimum];
|
|
267
|
+
}
|
|
268
|
+
export {
|
|
269
|
+
runWithMembershipContext,
|
|
270
|
+
membershipRepository,
|
|
271
|
+
membershipContext,
|
|
272
|
+
hasOrgMembership,
|
|
273
|
+
hasMinimumOrgRole2 as hasMinimumOrgRole,
|
|
274
|
+
currentOrganizationIds,
|
|
275
|
+
currentOrgRole
|
|
276
|
+
};
|
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/errors/http.ts
|
|
3
|
+
class HttpError extends Error {
|
|
4
|
+
status;
|
|
5
|
+
details;
|
|
6
|
+
constructor(status, message, details) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = new.target.name;
|
|
9
|
+
this.status = status;
|
|
10
|
+
this.details = details;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
class BadRequestError extends HttpError {
|
|
15
|
+
constructor(message = "Bad Request", details) {
|
|
16
|
+
super(400, message, details);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
class NotFoundError extends HttpError {
|
|
21
|
+
constructor(message = "Not Found", details) {
|
|
22
|
+
super(404, message, details);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
class ConflictError extends HttpError {
|
|
27
|
+
constructor(message = "Conflict", details) {
|
|
28
|
+
super(409, message, details);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
class UnprocessableEntityError extends HttpError {
|
|
33
|
+
constructor(message = "Unprocessable Entity", details) {
|
|
34
|
+
super(422, message, details);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
class ValidationError extends HttpError {
|
|
39
|
+
constructor(message = "Validation failed", details) {
|
|
40
|
+
super(422, message, details);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
class ForbiddenError extends HttpError {
|
|
45
|
+
constructor(message = "Forbidden", details) {
|
|
46
|
+
super(403, message, details);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
class UnauthorizedError extends HttpError {
|
|
51
|
+
constructor(message = "Unauthorized", details) {
|
|
52
|
+
super(401, message, details);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
class PayloadTooLargeError extends HttpError {
|
|
57
|
+
constructor(message = "Payload Too Large", details) {
|
|
58
|
+
super(413, message, details);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
class PreconditionFailedError extends HttpError {
|
|
63
|
+
constructor(message = "Precondition Failed", details) {
|
|
64
|
+
super(412, message, details);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ../../src/core/tenant/tenantContext.ts
|
|
69
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
70
|
+
var tenantContext = new AsyncLocalStorage;
|
|
71
|
+
function runWithTenant(tenant, callback) {
|
|
72
|
+
return tenantContext.run(tenant, callback);
|
|
73
|
+
}
|
|
74
|
+
function currentTenant() {
|
|
75
|
+
return tenantContext.getStore() ?? null;
|
|
76
|
+
}
|
|
77
|
+
function currentTenantId() {
|
|
78
|
+
return currentTenant()?.id ?? 1;
|
|
79
|
+
}
|
|
80
|
+
function rateLimitMultiplierForPlan(plan) {
|
|
81
|
+
switch (plan) {
|
|
82
|
+
case "enterprise":
|
|
83
|
+
return 4;
|
|
84
|
+
case "pro":
|
|
85
|
+
return 2;
|
|
86
|
+
default:
|
|
87
|
+
return 1;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// ../../src/core/auth/authContext.ts
|
|
92
|
+
import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
|
|
93
|
+
var authContext = new AsyncLocalStorage2;
|
|
94
|
+
function runWithAuthUser(user, callback) {
|
|
95
|
+
return authContext.run(user, callback);
|
|
96
|
+
}
|
|
97
|
+
function currentAuthUser() {
|
|
98
|
+
return authContext.getStore() ?? null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// ../../src/core/auth/accessControl.ts
|
|
102
|
+
var ROLE_RANK = {
|
|
103
|
+
member: 1,
|
|
104
|
+
admin: 2,
|
|
105
|
+
owner: 3
|
|
106
|
+
};
|
|
107
|
+
function isGlobalAdmin(user) {
|
|
108
|
+
return user?.role === "admin";
|
|
109
|
+
}
|
|
110
|
+
function hasMinimumOrgRole(role, minimum) {
|
|
111
|
+
if (!role) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
return ROLE_RANK[role] >= ROLE_RANK[minimum];
|
|
115
|
+
}
|
|
116
|
+
function requireAuthenticatedUser() {
|
|
117
|
+
const user = currentAuthUser();
|
|
118
|
+
if (!user) {
|
|
119
|
+
throw new ForbiddenError("Authentication required.");
|
|
120
|
+
}
|
|
121
|
+
return user;
|
|
122
|
+
}
|
|
123
|
+
function resolveUserId(user) {
|
|
124
|
+
const userId = typeof user.id === "number" ? user.id : Number(user.id);
|
|
125
|
+
if (!Number.isInteger(userId) || userId <= 0) {
|
|
126
|
+
throw new ForbiddenError("Invalid authenticated user.");
|
|
127
|
+
}
|
|
128
|
+
return userId;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ../../src/core/auth/membershipContext.ts
|
|
132
|
+
import { AsyncLocalStorage as AsyncLocalStorage4 } from "async_hooks";
|
|
133
|
+
|
|
134
|
+
// ../../src/config/database.ts
|
|
135
|
+
function readInteger(name, fallback) {
|
|
136
|
+
const parsed = Number.parseInt(process.env[name] ?? String(fallback), 10);
|
|
137
|
+
return Number.isInteger(parsed) && parsed >= 0 ? parsed : fallback;
|
|
138
|
+
}
|
|
139
|
+
var databaseConfig = {
|
|
140
|
+
url: process.env.DATABASE_URL ?? "",
|
|
141
|
+
poolMax: readInteger("DB_POOL_MAX", 10),
|
|
142
|
+
idleTimeoutSeconds: readInteger("DB_POOL_IDLE_TIMEOUT", 30),
|
|
143
|
+
maxLifetimeSeconds: readInteger("DB_POOL_MAX_LIFETIME", 3600),
|
|
144
|
+
connectionTimeoutSeconds: readInteger("DB_CONNECTION_TIMEOUT", 10)
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
// ../../src/core/database/connectionContext.ts
|
|
148
|
+
import { AsyncLocalStorage as AsyncLocalStorage3 } from "async_hooks";
|
|
149
|
+
var activeConnection = new AsyncLocalStorage3;
|
|
150
|
+
function runWithDatabaseConnection(connection, callback) {
|
|
151
|
+
return activeConnection.run(connection, callback);
|
|
152
|
+
}
|
|
153
|
+
function getActiveDatabaseConnection(fallback) {
|
|
154
|
+
return activeConnection.getStore() ?? fallback;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// ../../src/db/connection/createConnection.ts
|
|
158
|
+
var {SQL } = globalThis.Bun;
|
|
159
|
+
function createDatabaseConnection(config) {
|
|
160
|
+
if (!config.url) {
|
|
161
|
+
throw new Error("DATABASE_URL is not configured. Set DATABASE_URL before starting the app or running integration tests.");
|
|
162
|
+
}
|
|
163
|
+
return new SQL({
|
|
164
|
+
url: config.url,
|
|
165
|
+
max: config.poolMax,
|
|
166
|
+
idleTimeout: config.idleTimeoutSeconds,
|
|
167
|
+
maxLifetime: config.maxLifetimeSeconds,
|
|
168
|
+
connectionTimeout: config.connectionTimeoutSeconds
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// ../../src/db/connection/index.ts
|
|
173
|
+
var connectionHolder = {
|
|
174
|
+
connection: null
|
|
175
|
+
};
|
|
176
|
+
function getDatabase() {
|
|
177
|
+
if (!connectionHolder.connection) {
|
|
178
|
+
connectionHolder.connection = createDatabaseConnection(databaseConfig);
|
|
179
|
+
}
|
|
180
|
+
return connectionHolder.connection;
|
|
181
|
+
}
|
|
182
|
+
var POOL_CONNECTION_METHODS = new Set(["begin", "close", "connect"]);
|
|
183
|
+
function resolveDatabase() {
|
|
184
|
+
return getActiveDatabaseConnection(getDatabase());
|
|
185
|
+
}
|
|
186
|
+
function resolveDatabaseForProperty(property) {
|
|
187
|
+
if (typeof property === "string" && POOL_CONNECTION_METHODS.has(property)) {
|
|
188
|
+
return getDatabase();
|
|
189
|
+
}
|
|
190
|
+
return resolveDatabase();
|
|
191
|
+
}
|
|
192
|
+
var db = new Proxy(function database() {}, {
|
|
193
|
+
apply(_target, _thisArg, args) {
|
|
194
|
+
return resolveDatabase()(...args);
|
|
195
|
+
},
|
|
196
|
+
get(_target, property) {
|
|
197
|
+
const connection = resolveDatabaseForProperty(property);
|
|
198
|
+
const value = connection[property];
|
|
199
|
+
return typeof value === "function" ? value.bind(connection) : value;
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
var connection_default = db;
|
|
203
|
+
|
|
204
|
+
// ../../src/modules/organization/memberRepository.ts
|
|
205
|
+
class OrganizationMemberRepository {
|
|
206
|
+
constructor() {}
|
|
207
|
+
async findMembership(userId, organizationId) {
|
|
208
|
+
const rows = await connection_default`
|
|
209
|
+
SELECT id, organization_id, user_id, role, created_at
|
|
210
|
+
FROM organization_member
|
|
211
|
+
WHERE user_id = ${userId} AND organization_id = ${organizationId}
|
|
212
|
+
LIMIT 1
|
|
213
|
+
`;
|
|
214
|
+
return rows[0] ?? null;
|
|
215
|
+
}
|
|
216
|
+
async listForUser(userId) {
|
|
217
|
+
return await connection_default`
|
|
218
|
+
SELECT id, organization_id, user_id, role, created_at
|
|
219
|
+
FROM organization_member
|
|
220
|
+
WHERE user_id = ${userId}
|
|
221
|
+
ORDER BY organization_id
|
|
222
|
+
`;
|
|
223
|
+
}
|
|
224
|
+
async listForOrganization(organizationId) {
|
|
225
|
+
return await connection_default`
|
|
226
|
+
SELECT id, organization_id, user_id, role, created_at
|
|
227
|
+
FROM organization_member
|
|
228
|
+
WHERE organization_id = ${organizationId}
|
|
229
|
+
ORDER BY id
|
|
230
|
+
`;
|
|
231
|
+
}
|
|
232
|
+
async addMember(input) {
|
|
233
|
+
const rows = await connection_default`
|
|
234
|
+
INSERT INTO organization_member (organization_id, user_id, role)
|
|
235
|
+
VALUES (${input.organizationId}, ${input.userId}, ${input.role ?? "member"})
|
|
236
|
+
RETURNING id, organization_id, user_id, role, created_at
|
|
237
|
+
`;
|
|
238
|
+
const row = rows[0];
|
|
239
|
+
if (!row) {
|
|
240
|
+
throw new Error("Organization member insert did not return a row.");
|
|
241
|
+
}
|
|
242
|
+
return row;
|
|
243
|
+
}
|
|
244
|
+
async removeMember(organizationId, userId) {
|
|
245
|
+
const rows = await connection_default`
|
|
246
|
+
DELETE FROM organization_member
|
|
247
|
+
WHERE organization_id = ${organizationId} AND user_id = ${userId}
|
|
248
|
+
RETURNING id
|
|
249
|
+
`;
|
|
250
|
+
return rows.length > 0;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
var memberRepository_default = OrganizationMemberRepository;
|
|
254
|
+
|
|
255
|
+
// ../../src/core/auth/membershipContext.ts
|
|
256
|
+
var membershipContext = new AsyncLocalStorage4;
|
|
257
|
+
var membershipRepository = new memberRepository_default;
|
|
258
|
+
async function runWithMembershipContext(callback) {
|
|
259
|
+
const user = currentAuthUser();
|
|
260
|
+
if (!user || isGlobalAdmin(user)) {
|
|
261
|
+
return await callback();
|
|
262
|
+
}
|
|
263
|
+
const memberships = await membershipRepository.listForUser(resolveUserId(user));
|
|
264
|
+
const context = {
|
|
265
|
+
organizationIds: memberships.map((membership) => membership.organization_id),
|
|
266
|
+
rolesByOrganizationId: new Map(memberships.map((membership) => [membership.organization_id, membership.role]))
|
|
267
|
+
};
|
|
268
|
+
return await membershipContext.run(context, callback);
|
|
269
|
+
}
|
|
270
|
+
function currentOrgRole(organizationId) {
|
|
271
|
+
return membershipContext.getStore()?.rolesByOrganizationId.get(organizationId) ?? null;
|
|
272
|
+
}
|
|
273
|
+
function hasOrgMembership(organizationId) {
|
|
274
|
+
return currentOrgRole(organizationId) !== null;
|
|
275
|
+
}
|
|
276
|
+
function currentOrganizationIds() {
|
|
277
|
+
return membershipContext.getStore()?.organizationIds ?? [];
|
|
278
|
+
}
|
|
279
|
+
function hasMinimumOrgRole2(organizationId, minimum) {
|
|
280
|
+
const role = currentOrgRole(organizationId);
|
|
281
|
+
if (!role) {
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
const ranks = {
|
|
285
|
+
member: 1,
|
|
286
|
+
admin: 2,
|
|
287
|
+
owner: 3
|
|
288
|
+
};
|
|
289
|
+
return ranks[role] >= ranks[minimum];
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// ../../src/core/auth/membershipScope.ts
|
|
293
|
+
function resolveOrganizationScope() {
|
|
294
|
+
const user = currentAuthUser();
|
|
295
|
+
if (!user) {
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
if (isGlobalAdmin(user)) {
|
|
299
|
+
return null;
|
|
300
|
+
}
|
|
301
|
+
return currentOrganizationIds();
|
|
302
|
+
}
|
|
303
|
+
function scopedOrganizationIds(requestedOrganizationId) {
|
|
304
|
+
const scope = resolveOrganizationScope();
|
|
305
|
+
if (scope === null) {
|
|
306
|
+
return requestedOrganizationId === undefined ? null : [requestedOrganizationId];
|
|
307
|
+
}
|
|
308
|
+
if (requestedOrganizationId !== undefined) {
|
|
309
|
+
return scope.includes(requestedOrganizationId) ? [requestedOrganizationId] : [];
|
|
310
|
+
}
|
|
311
|
+
return scope;
|
|
312
|
+
}
|
|
313
|
+
function appendOrganizationScope(where, requestedOrganizationId) {
|
|
314
|
+
const organizationIds = scopedOrganizationIds(requestedOrganizationId);
|
|
315
|
+
if (organizationIds === null) {
|
|
316
|
+
return where;
|
|
317
|
+
}
|
|
318
|
+
if (organizationIds.length === 0) {
|
|
319
|
+
return {
|
|
320
|
+
...where,
|
|
321
|
+
organization_id: [-1]
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
return {
|
|
325
|
+
...where,
|
|
326
|
+
organization_id: organizationIds.length === 1 ? organizationIds[0] : organizationIds
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
function appendProjectScope(where, accessibleProjectIds, requestedProjectId) {
|
|
330
|
+
if (accessibleProjectIds === null) {
|
|
331
|
+
if (requestedProjectId === undefined) {
|
|
332
|
+
return where;
|
|
333
|
+
}
|
|
334
|
+
return {
|
|
335
|
+
...where,
|
|
336
|
+
project_id: requestedProjectId
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
if (accessibleProjectIds.length === 0) {
|
|
340
|
+
return {
|
|
341
|
+
...where,
|
|
342
|
+
project_id: [-1]
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
if (requestedProjectId !== undefined) {
|
|
346
|
+
return {
|
|
347
|
+
...where,
|
|
348
|
+
project_id: accessibleProjectIds.includes(requestedProjectId) ? requestedProjectId : -1
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
return {
|
|
352
|
+
...where,
|
|
353
|
+
project_id: accessibleProjectIds
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
function emptyPaginateResult(page, perPage) {
|
|
357
|
+
return {
|
|
358
|
+
data: [],
|
|
359
|
+
meta: {
|
|
360
|
+
page,
|
|
361
|
+
per_page: perPage,
|
|
362
|
+
total: 0,
|
|
363
|
+
last_page: 1
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
function assertResourceInCurrentTenant(resourceTenantId, resourceLabel, resourceId) {
|
|
368
|
+
if (resourceTenantId !== currentTenantId()) {
|
|
369
|
+
throw new NotFoundError(`${resourceLabel} ${resourceId} not found.`);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
function assertOrganizationReadable(organizationId) {
|
|
373
|
+
const user = currentAuthUser();
|
|
374
|
+
if (!user || isGlobalAdmin(user)) {
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const organizationIds = scopedOrganizationIds();
|
|
378
|
+
if (organizationIds !== null && !organizationIds.includes(organizationId)) {
|
|
379
|
+
throw new NotFoundError(`Organization ${organizationId} not found.`);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
export {
|
|
383
|
+
scopedOrganizationIds,
|
|
384
|
+
resolveOrganizationScope,
|
|
385
|
+
emptyPaginateResult,
|
|
386
|
+
assertResourceInCurrentTenant,
|
|
387
|
+
assertOrganizationReadable,
|
|
388
|
+
appendProjectScope,
|
|
389
|
+
appendOrganizationScope
|
|
390
|
+
};
|