@getstrata/bootstrap 0.2.23 → 0.2.25

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.
@@ -294,7 +294,6 @@ var db = new Proxy(function database() {}, {
294
294
  return typeof value === "function" ? value.bind(connection) : value;
295
295
  }
296
296
  });
297
- var connection_default = db;
298
297
 
299
298
  // ../../src/modules/user/apiTokenTable.ts
300
299
  import { defineTable } from "@getstrata/core/database";
@@ -1180,9 +1179,6 @@ var queueConfig = {
1180
1179
  };
1181
1180
 
1182
1181
  // ../../src/core/config/envSchema.ts
1183
- function defineEnvSchema(schema) {
1184
- return schema;
1185
- }
1186
1182
  function validateEnv(schema, env = process.env) {
1187
1183
  const resolved = {};
1188
1184
  for (const [name, rule] of Object.entries(schema)) {
@@ -1211,6 +1207,7 @@ function validateEnv(schema, env = process.env) {
1211
1207
  }
1212
1208
 
1213
1209
  // ../../src/bootstrap/env.ts
1210
+ import { defineEnvSchema } from "@getstrata/core/config/envSchema";
1214
1211
  var appEnvSchema = defineEnvSchema({
1215
1212
  DATABASE_URL: { required: true, pattern: /^postgres(ql)?:\/\// },
1216
1213
  PORT: {
@@ -3699,9 +3696,6 @@ function resolveApplicationAuth2() {
3699
3696
  function resolveApplicationPolicyGate2() {
3700
3697
  return requireActiveApplicationContext().container.resolve(CORE_POLICY_GATE_TOKEN2);
3701
3698
  }
3702
- function resolveApplicationDependencies2() {
3703
- return requireActiveApplicationContext().dependencies;
3704
- }
3705
3699
 
3706
3700
  // ../../src/bootstrap/queue/defaultJobs.ts
3707
3701
  function registerDefaultJobs() {
@@ -3903,16 +3897,6 @@ class EtaViewEngine {
3903
3897
  });
3904
3898
  }
3905
3899
  }
3906
- // ../../src/core/view/htmlResponse.ts
3907
- function htmlResponse(html, init = {}) {
3908
- return new Response(html, {
3909
- status: init.status ?? 200,
3910
- statusText: init.statusText,
3911
- headers: {
3912
- "Content-Type": "text/html; charset=utf-8"
3913
- }
3914
- });
3915
- }
3916
3900
  // ../../src/core/http/cookies.ts
3917
3901
  function readRequestCookie(request, name) {
3918
3902
  const cookies = request.cookies;
@@ -142,9 +142,6 @@ function resolveApplicationAuth() {
142
142
  function resolveApplicationPolicyGate() {
143
143
  return requireActiveApplicationContext().container.resolve(CORE_POLICY_GATE_TOKEN);
144
144
  }
145
- function resolveApplicationDependencies() {
146
- return requireActiveApplicationContext().dependencies;
147
- }
148
145
 
149
146
  // ../../src/core/crypto/nonCryptographicHash.ts
150
147
  function nonCryptographicDigest(input) {
@@ -158,10 +155,6 @@ function isEtagEnabled() {
158
155
  function formatWeakEtag(digest) {
159
156
  return `W/"${digest}"`;
160
157
  }
161
- function computeEtagFromJson(data) {
162
- const digest = nonCryptographicDigest(JSON.stringify(data)).slice(0, 32);
163
- return formatWeakEtag(digest);
164
- }
165
158
  function etagFromResource(resource) {
166
159
  const version = resource.updated_at ?? resource.created_at ?? "";
167
160
  const versionText = version instanceof Date ? version.toISOString() : version ? String(version) : "0";
@@ -1,396 +1,6 @@
1
1
  // @bun
2
- // ../../src/core/contracts/di.ts
3
- function getRequiredDependency(dependencies, key) {
4
- const dependency = dependencies[key];
5
- if (dependency === undefined) {
6
- throw new Error(`Required dependency "${key}" is not registered.`);
7
- }
8
- return dependency;
9
- }
10
-
11
- // ../../src/core/contracts/serviceTokens.ts
12
- var CORE_POLICY_GATE_TOKEN = "core.policyGate";
13
- var CORE_AUTH_TOKEN = "core.auth";
14
-
15
- // ../../src/core/logging/logger.ts
16
- class Logger {
17
- channel;
18
- constructor(channel = "app") {
19
- this.channel = channel;
20
- }
21
- write(level, message, context = {}) {
22
- const entry = {
23
- level,
24
- channel: this.channel,
25
- message,
26
- timestamp: new Date().toISOString(),
27
- ...context
28
- };
29
- const line = JSON.stringify(entry);
30
- if (level === "error") {
31
- console.error(line);
32
- return;
33
- }
34
- console.log(line);
35
- }
36
- debug(message, context) {
37
- this.write("debug", message, context);
38
- }
39
- info(message, context) {
40
- this.write("info", message, context);
41
- }
42
- warn(message, context) {
43
- this.write("warn", message, context);
44
- }
45
- error(message, context) {
46
- this.write("error", message, context);
47
- }
48
- }
49
- var appLogger = new Logger("app");
50
-
51
- // ../../src/core/runtime/applicationRegistry.ts
52
- var APPLICATION_CONTEXT_KEY = Symbol.for("@getstrata/applicationContext");
53
- var activeContext;
54
- function readStoredApplicationContext() {
55
- if (activeContext) {
56
- return activeContext;
57
- }
58
- const globalContext = globalThis[APPLICATION_CONTEXT_KEY];
59
- if (globalContext) {
60
- activeContext = globalContext;
61
- }
62
- return activeContext;
63
- }
64
- function requireActiveApplicationContext() {
65
- const context = readStoredApplicationContext();
66
- if (!context) {
67
- throw new Error("The application context has not been bootstrapped.");
68
- }
69
- return context;
70
- }
71
- function resolveApplicationCache() {
72
- return getRequiredDependency(requireActiveApplicationContext().dependencies, "cache");
73
- }
74
- function resolveApplicationAuth() {
75
- return requireActiveApplicationContext().container.resolve(CORE_AUTH_TOKEN);
76
- }
77
- function resolveApplicationPolicyGate() {
78
- return requireActiveApplicationContext().container.resolve(CORE_POLICY_GATE_TOKEN);
79
- }
80
- function resolveApplicationDependencies() {
81
- return requireActiveApplicationContext().dependencies;
82
- }
83
-
84
- // ../../src/core/errors/http.ts
85
- class HttpError extends Error {
86
- status;
87
- details;
88
- constructor(status, message, details) {
89
- super(message);
90
- this.name = new.target.name;
91
- this.status = status;
92
- this.details = details;
93
- }
94
- }
95
-
96
- class BadRequestError extends HttpError {
97
- constructor(message = "Bad Request", details) {
98
- super(400, message, details);
99
- }
100
- }
101
- class ConflictError extends HttpError {
102
- constructor(message = "Conflict", details) {
103
- super(409, message, details);
104
- }
105
- }
106
-
107
- class UnprocessableEntityError extends HttpError {
108
- constructor(message = "Unprocessable Entity", details) {
109
- super(422, message, details);
110
- }
111
- }
112
- class ForbiddenError extends HttpError {
113
- constructor(message = "Forbidden", details) {
114
- super(403, message, details);
115
- }
116
- }
117
-
118
- class UnauthorizedError extends HttpError {
119
- constructor(message = "Unauthorized", details) {
120
- super(401, message, details);
121
- }
122
- }
123
- class PreconditionFailedError extends HttpError {
124
- constructor(message = "Precondition Failed", details) {
125
- super(412, message, details);
126
- }
127
- }
128
-
129
- // ../../src/core/runtime/asyncContextStore.ts
130
- import { AsyncLocalStorage } from "async_hooks";
131
- function createAsyncContextStore(key) {
132
- const symbol = Symbol.for(key);
133
- const globalRecord = globalThis;
134
- const existing = globalRecord[symbol];
135
- if (existing) {
136
- return existing;
137
- }
138
- const store = new AsyncLocalStorage;
139
- globalRecord[symbol] = store;
140
- return store;
141
- }
142
-
143
- // ../../src/core/auth/authContext.ts
144
- var authContext = createAsyncContextStore("@getstrata/authContext");
145
- function currentAuthUser() {
146
- return authContext.getStore() ?? null;
147
- }
148
-
149
- // ../../src/core/auth/accessControl.ts
150
- var ROLE_RANK = {
151
- member: 1,
152
- admin: 2,
153
- owner: 3
154
- };
155
- function isGlobalAdmin(user) {
156
- return user?.role === "admin";
157
- }
158
- function hasMinimumOrgRole(role, minimum) {
159
- if (!role) {
160
- return false;
161
- }
162
- return ROLE_RANK[role] >= ROLE_RANK[minimum];
163
- }
164
- function resolveUserId(user) {
165
- const userId = typeof user.id === "number" ? user.id : Number(user.id);
166
- if (!Number.isInteger(userId) || userId <= 0) {
167
- throw new ForbiddenError("Invalid authenticated user.");
168
- }
169
- return userId;
170
- }
171
-
172
- // ../../src/config/database.ts
173
- function readInteger(name, fallback) {
174
- const parsed = Number.parseInt(process.env[name] ?? String(fallback), 10);
175
- return Number.isInteger(parsed) && parsed >= 0 ? parsed : fallback;
176
- }
177
- var databaseConfig = {
178
- url: process.env.DATABASE_URL ?? "",
179
- poolMax: readInteger("DB_POOL_MAX", 10),
180
- idleTimeoutSeconds: readInteger("DB_POOL_IDLE_TIMEOUT", 30),
181
- maxLifetimeSeconds: readInteger("DB_POOL_MAX_LIFETIME", 3600),
182
- connectionTimeoutSeconds: readInteger("DB_CONNECTION_TIMEOUT", 10)
183
- };
184
-
185
- // ../../src/core/database/connectionContext.ts
186
- var activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
187
- function getActiveDatabaseConnection(fallback) {
188
- return activeConnection.getStore() ?? fallback;
189
- }
190
-
191
- // ../../src/core/database/queryProxy.ts
192
- var POOL_CONNECTION_METHODS = new Set(["begin", "close", "connect"]);
193
- function createDatabaseQueryProxy(pool) {
194
- function resolveDatabase() {
195
- return getActiveDatabaseConnection(pool);
196
- }
197
- function resolveDatabaseForProperty(property) {
198
- if (typeof property === "string" && POOL_CONNECTION_METHODS.has(property)) {
199
- return pool;
200
- }
201
- return resolveDatabase();
202
- }
203
- return new Proxy(function database() {}, {
204
- apply(_target, _thisArg, args) {
205
- return resolveDatabase()(...args);
206
- },
207
- get(_target, property) {
208
- const connection = resolveDatabaseForProperty(property);
209
- const value = connection[property];
210
- return typeof value === "function" ? value.bind(connection) : value;
211
- }
212
- });
213
- }
214
-
215
- // ../../src/core/database/defaultConnection.ts
216
- var defaultPool = {
217
- connection: null
218
- };
219
- var defaultQuery = {
220
- connection: null
221
- };
222
- function registerDefaultDatabasePool(connection) {
223
- defaultPool.connection = connection;
224
- defaultQuery.connection = createDatabaseQueryProxy(connection);
225
- }
226
- function getDefaultDatabaseQuery() {
227
- if (!defaultQuery.connection) {
228
- throw new Error("Default database query handle is not registered. Call registerDefaultDatabasePool() during app bootstrap.");
229
- }
230
- return defaultQuery.connection;
231
- }
232
-
233
- // ../../src/db/connection/createConnection.ts
234
- var {SQL } = globalThis.Bun;
235
- function createDatabaseConnection(config) {
236
- if (!config.url) {
237
- throw new Error("DATABASE_URL is not configured. Set DATABASE_URL before starting the app or running integration tests.");
238
- }
239
- return new SQL({
240
- url: config.url,
241
- max: config.poolMax,
242
- idleTimeout: config.idleTimeoutSeconds,
243
- maxLifetime: config.maxLifetimeSeconds,
244
- connectionTimeout: config.connectionTimeoutSeconds
245
- });
246
- }
247
-
248
- // ../../src/db/connection/index.ts
249
- var connectionHolder = {
250
- connection: null
251
- };
252
- function getDatabase() {
253
- if (!connectionHolder.connection) {
254
- connectionHolder.connection = createDatabaseConnection(databaseConfig);
255
- registerDefaultDatabasePool(connectionHolder.connection);
256
- }
257
- return connectionHolder.connection;
258
- }
259
- function getDb() {
260
- getDatabase();
261
- return getDefaultDatabaseQuery();
262
- }
263
- var db = new Proxy(function database() {}, {
264
- apply(_target, _thisArg, args) {
265
- return getDb()(...args);
266
- },
267
- get(_target, property) {
268
- const connection = getDb();
269
- const value = connection[property];
270
- return typeof value === "function" ? value.bind(connection) : value;
271
- }
272
- });
273
- var connection_default = db;
274
-
275
- // ../../src/modules/organization/memberRepository.ts
276
- class OrganizationMemberRepository {
277
- constructor() {}
278
- async findMembership(userId, organizationId) {
279
- const rows = await connection_default`
280
- SELECT id, organization_id, user_id, role, created_at
281
- FROM organization_member
282
- WHERE user_id = ${userId} AND organization_id = ${organizationId}
283
- LIMIT 1
284
- `;
285
- return rows[0] ?? null;
286
- }
287
- async listForUser(userId) {
288
- return await connection_default`
289
- SELECT id, organization_id, user_id, role, created_at
290
- FROM organization_member
291
- WHERE user_id = ${userId}
292
- ORDER BY organization_id
293
- `;
294
- }
295
- async listForOrganization(organizationId) {
296
- return await connection_default`
297
- SELECT id, organization_id, user_id, role, created_at
298
- FROM organization_member
299
- WHERE organization_id = ${organizationId}
300
- ORDER BY id
301
- `;
302
- }
303
- async addMember(input) {
304
- const rows = await connection_default`
305
- INSERT INTO organization_member (organization_id, user_id, role)
306
- VALUES (${input.organizationId}, ${input.userId}, ${input.role ?? "member"})
307
- RETURNING id, organization_id, user_id, role, created_at
308
- `;
309
- const row = rows[0];
310
- if (!row) {
311
- throw new Error("Organization member insert did not return a row.");
312
- }
313
- return row;
314
- }
315
- async removeMember(organizationId, userId) {
316
- const rows = await connection_default`
317
- DELETE FROM organization_member
318
- WHERE organization_id = ${organizationId} AND user_id = ${userId}
319
- RETURNING id
320
- `;
321
- return rows.length > 0;
322
- }
323
- }
324
- var memberRepository_default = OrganizationMemberRepository;
325
-
326
- // ../../src/core/auth/membershipContext.ts
327
- var membershipContext = createAsyncContextStore("@getstrata/membershipContext");
328
- var membershipRepository = new memberRepository_default;
329
-
330
- // ../../src/core/auth/membershipService.ts
331
- class MembershipService {
332
- members;
333
- constructor(members = membershipRepository) {
334
- this.members = members;
335
- }
336
- async listOrganizationIdsForUser(userId) {
337
- const memberships = await this.members.listForUser(userId);
338
- return memberships.map((membership) => membership.organization_id);
339
- }
340
- async getOrgRole(userId, organizationId) {
341
- const membership = await this.members.findMembership(userId, organizationId);
342
- return membership?.role ?? null;
343
- }
344
- async requireOrgAccess(organizationId, minimumRole = "member", user = currentAuthUser()) {
345
- if (!user) {
346
- throw new ForbiddenError("Authentication required.");
347
- }
348
- if (isGlobalAdmin(user)) {
349
- return "owner";
350
- }
351
- const role = await this.getOrgRole(resolveUserId(user), organizationId);
352
- if (!role || !hasMinimumOrgRole(role, minimumRole)) {
353
- throw new ForbiddenError("Organization membership required.");
354
- }
355
- return role;
356
- }
357
- async filterAccessibleOrganizationIds(organizationIds, user = currentAuthUser()) {
358
- if (!user) {
359
- return [];
360
- }
361
- if (isGlobalAdmin(user)) {
362
- return organizationIds;
363
- }
364
- const allowed = new Set(await this.listOrganizationIdsForUser(resolveUserId(user)));
365
- return organizationIds.filter((organizationId) => allowed.has(organizationId));
366
- }
367
- async addOwnerOnOrganizationCreate(organizationId, userId) {
368
- await this.members.addMember({
369
- organizationId,
370
- userId,
371
- role: "owner"
372
- });
373
- }
374
- listMembersForOrganization(organizationId) {
375
- return this.members.listForOrganization(organizationId);
376
- }
377
- addMember(input) {
378
- return this.members.addMember(input);
379
- }
380
- removeMember(organizationId, userId) {
381
- return this.members.removeMember(organizationId, userId);
382
- }
383
- }
384
- var membershipService_default = MembershipService;
385
-
386
- // ../../src/core/auth/resolveMembershipService.ts
387
- function resolveMembershipService() {
388
- const dependencies = resolveApplicationDependencies();
389
- if (dependencies.container.has("core.membership")) {
390
- return dependencies.container.resolve("core.membership");
391
- }
392
- return new membershipService_default;
393
- }
2
+ // ../../src/bootstrap/membershipService.ts
3
+ import { resolveMembershipService } from "@getstrata/core/auth/membershipService";
394
4
  export {
395
5
  resolveMembershipService
396
6
  };
@@ -69,16 +69,6 @@ class EtaViewEngine {
69
69
  });
70
70
  }
71
71
  }
72
- // ../../src/core/view/htmlResponse.ts
73
- function htmlResponse(html, init = {}) {
74
- return new Response(html, {
75
- status: init.status ?? 200,
76
- statusText: init.statusText,
77
- headers: {
78
- "Content-Type": "text/html; charset=utf-8"
79
- }
80
- });
81
- }
82
72
  // ../../src/bootstrap/config.ts
83
73
  import {
84
74
  CORE_AUTH_TOKEN,
@@ -233,7 +223,6 @@ var db = new Proxy(function database() {}, {
233
223
  return typeof value === "function" ? value.bind(connection) : value;
234
224
  }
235
225
  });
236
- var connection_default = db;
237
226
 
238
227
  // ../../src/modules/user/apiTokenTable.ts
239
228
  import { defineTable } from "@getstrata/core/database";
@@ -211,7 +211,6 @@ var db = new Proxy(function database() {}, {
211
211
  return typeof value === "function" ? value.bind(connection) : value;
212
212
  }
213
213
  });
214
- var connection_default = db;
215
214
 
216
215
  // ../../src/modules/user/apiTokenTable.ts
217
216
  import { defineTable } from "@getstrata/core/database";
@@ -1097,9 +1096,6 @@ var queueConfig = {
1097
1096
  };
1098
1097
 
1099
1098
  // ../../src/core/config/envSchema.ts
1100
- function defineEnvSchema(schema) {
1101
- return schema;
1102
- }
1103
1099
  function validateEnv(schema, env = process.env) {
1104
1100
  const resolved = {};
1105
1101
  for (const [name, rule] of Object.entries(schema)) {
@@ -1128,6 +1124,7 @@ function validateEnv(schema, env = process.env) {
1128
1124
  }
1129
1125
 
1130
1126
  // ../../src/bootstrap/env.ts
1127
+ import { defineEnvSchema } from "@getstrata/core/config/envSchema";
1131
1128
  var appEnvSchema = defineEnvSchema({
1132
1129
  DATABASE_URL: { required: true, pattern: /^postgres(ql)?:\/\// },
1133
1130
  PORT: {
@@ -3689,9 +3686,6 @@ function resolveApplicationAuth2() {
3689
3686
  function resolveApplicationPolicyGate2() {
3690
3687
  return requireActiveApplicationContext().container.resolve(CORE_POLICY_GATE_TOKEN2);
3691
3688
  }
3692
- function resolveApplicationDependencies2() {
3693
- return requireActiveApplicationContext().dependencies;
3694
- }
3695
3689
 
3696
3690
  // ../../src/bootstrap/queue/defaultJobs.ts
3697
3691
  function registerDefaultJobs() {
@@ -3893,16 +3887,6 @@ class EtaViewEngine {
3893
3887
  });
3894
3888
  }
3895
3889
  }
3896
- // ../../src/core/view/htmlResponse.ts
3897
- function htmlResponse(html, init = {}) {
3898
- return new Response(html, {
3899
- status: init.status ?? 200,
3900
- statusText: init.statusText,
3901
- headers: {
3902
- "Content-Type": "text/html; charset=utf-8"
3903
- }
3904
- });
3905
- }
3906
3890
  // ../../src/core/http/cookies.ts
3907
3891
  function readRequestCookie(request, name) {
3908
3892
  const cookies = request.cookies;
@@ -462,9 +462,6 @@ function resolveApplicationAuth() {
462
462
  function resolveApplicationPolicyGate() {
463
463
  return requireActiveApplicationContext().container.resolve(CORE_POLICY_GATE_TOKEN);
464
464
  }
465
- function resolveApplicationDependencies() {
466
- return requireActiveApplicationContext().dependencies;
467
- }
468
465
 
469
466
  // ../../src/bootstrap/queue/defaultJobs.ts
470
467
  function registerDefaultJobs() {
@@ -145,9 +145,6 @@ function resolveApplicationAuth() {
145
145
  function resolveApplicationPolicyGate() {
146
146
  return requireActiveApplicationContext().container.resolve(CORE_POLICY_GATE_TOKEN);
147
147
  }
148
- function resolveApplicationDependencies() {
149
- return requireActiveApplicationContext().dependencies;
150
- }
151
148
 
152
149
  // ../../src/core/crypto/nonCryptographicHash.ts
153
150
  function nonCryptographicDigest(input) {
@@ -161,10 +158,6 @@ function isEtagEnabled() {
161
158
  function formatWeakEtag(digest) {
162
159
  return `W/"${digest}"`;
163
160
  }
164
- function computeEtagFromJson(data) {
165
- const digest = nonCryptographicDigest(JSON.stringify(data)).slice(0, 32);
166
- return formatWeakEtag(digest);
167
- }
168
161
  function etagFromResource(resource) {
169
162
  const version = resource.updated_at ?? resource.created_at ?? "";
170
163
  const versionText = version instanceof Date ? version.toISOString() : version ? String(version) : "0";