@getstrata/bootstrap 0.2.9 → 0.2.10

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.
@@ -1,10 +1,10 @@
1
1
  /**
2
2
  * @getstrata/bootstrap — application shell for Strata sibling apps.
3
3
  */
4
+ export { resolveApplicationAuth, resolveApplicationCache, resolveApplicationConfig, resolveApplicationDependencies, resolveApplicationLogger, resolveApplicationPolicyGate, resolveApplicationQueue, setActiveApplicationContext, } from "@getstrata/core";
4
5
  export { scheduleRunCommand } from "../cli/commands/scheduleRun.ts";
5
6
  export type { ScheduledTask } from "../core/scheduler/schedule.ts";
6
7
  export { appSchedule, runDueScheduledTasks, Schedule } from "../core/scheduler/schedule.ts";
7
- export { resolveApplicationAuth, resolveApplicationCache, resolveApplicationConfig, resolveApplicationDependencies, resolveApplicationLogger, resolveApplicationPolicyGate, resolveApplicationQueue, setActiveApplicationContext, } from "./applicationRegistry.ts";
8
8
  export { APP_PORT_CONFIG_KEY, CORE_AUTH_TOKEN, CORE_CACHE_TOKEN, CORE_CONFIG_TOKEN, CORE_POLICY_GATE_TOKEN, CORE_QUEUE_TOKEN, CORE_TOKEN_SERVICE_TOKEN, DATABASE_URL_CONFIG_KEY, DEFAULT_APP_PORT, REDIS_URL_CONFIG_KEY, } from "./config.ts";
9
9
  export { collectProviders, createAppContext, runProviderPhase } from "./context.ts";
10
10
  export type { AppContext, AppDependencies, AppModule, AppRouteMap, CachedJson, ConfigStore, ModuleRouteContext, MutableAppDependencies, ProviderContext, ServiceFactory, ServiceProvider, } from "./contracts.ts";
@@ -1,11 +1,10 @@
1
- import { AsyncLocalStorage } from "node:async_hooks";
2
1
  type AuthUser = {
3
2
  id: number | string;
4
3
  role?: string;
5
4
  abilities?: string[];
6
5
  tokenId?: number;
7
6
  };
8
- declare const authContext: AsyncLocalStorage<AuthUser | null>;
7
+ declare const authContext: import("node:async_hooks").AsyncLocalStorage<AuthUser | null>;
9
8
  declare function runWithAuthUser<T>(user: AuthUser | null, callback: () => T | Promise<T>): T | Promise<T>;
10
9
  declare function currentAuthUser(): AuthUser | null;
11
10
  export type { AuthUser };
@@ -1,11 +1,10 @@
1
- import { AsyncLocalStorage } from "node:async_hooks";
2
1
  import OrganizationMemberRepository from "../../modules/organization/memberRepository";
3
2
  import type { OrganizationMemberRole } from "../../modules/organization/memberTypes";
4
3
  type MembershipContext = {
5
4
  organizationIds: number[];
6
5
  rolesByOrganizationId: Map<number, OrganizationMemberRole>;
7
6
  };
8
- declare const membershipContext: AsyncLocalStorage<MembershipContext>;
7
+ declare const membershipContext: import("node:async_hooks").AsyncLocalStorage<MembershipContext>;
9
8
  declare const membershipRepository: OrganizationMemberRepository;
10
9
  declare function runWithMembershipContext<T>(callback: () => T | Promise<T>): Promise<T | Promise<T>>;
11
10
  declare function currentOrgRole(organizationId: number): OrganizationMemberRole | null;
@@ -1,4 +1,3 @@
1
- import { AsyncLocalStorage } from "node:async_hooks";
2
1
  type RequestMeta = {
3
2
  ipAddress: string | null;
4
3
  userAgent: string | null;
@@ -9,7 +8,7 @@ type RequestMeta = {
9
8
  } | null;
10
9
  csrfToken?: string;
11
10
  };
12
- declare const requestMetaContext: AsyncLocalStorage<RequestMeta>;
11
+ declare const requestMetaContext: import("node:async_hooks").AsyncLocalStorage<RequestMeta>;
13
12
  declare function runWithRequestMeta<T>(meta: RequestMeta, callback: () => T | Promise<T>): T | Promise<T>;
14
13
  declare function currentRequestMeta(): RequestMeta;
15
14
  export type { RequestMeta };
@@ -0,0 +1,3 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ declare function createAsyncContextStore<T>(key: string): AsyncLocalStorage<T>;
3
+ export { createAsyncContextStore };
@@ -1,11 +1,10 @@
1
- import { AsyncLocalStorage } from "node:async_hooks";
2
1
  type TenantContext = {
3
2
  id: number;
4
3
  slug: string;
5
4
  plan: "free" | "pro" | "enterprise";
6
5
  region: "eu" | "us" | "apac";
7
6
  };
8
- declare const tenantContext: AsyncLocalStorage<TenantContext>;
7
+ declare const tenantContext: import("node:async_hooks").AsyncLocalStorage<TenantContext>;
9
8
  declare function runWithTenant<T>(tenant: TenantContext, callback: () => T | Promise<T>): T | Promise<T>;
10
9
  declare function currentTenant(): TenantContext | null;
11
10
  declare function currentTenantId(): number;
@@ -1,9 +1,8 @@
1
- import { AsyncLocalStorage } from "node:async_hooks";
2
1
  type TraceContext = {
3
2
  traceId: string;
4
3
  spanId: string;
5
4
  };
6
- declare const traceContextStorage: AsyncLocalStorage<TraceContext>;
5
+ declare const traceContextStorage: import("node:async_hooks").AsyncLocalStorage<TraceContext>;
7
6
  declare function runWithTraceContext<T>(context: TraceContext, callback: () => T | Promise<T>): T | Promise<T>;
8
7
  declare function currentTraceId(): string | null;
9
8
  export type { TraceContext };
@@ -142,15 +142,28 @@ function resolveService(dependencies, token) {
142
142
  }
143
143
 
144
144
  // ../../src/bootstrap/applicationRegistry.ts
145
+ var APPLICATION_CONTEXT_KEY = Symbol.for("@getstrata/applicationContext");
145
146
  var activeContext;
147
+ function readStoredApplicationContext() {
148
+ if (activeContext) {
149
+ return activeContext;
150
+ }
151
+ const globalContext = globalThis[APPLICATION_CONTEXT_KEY];
152
+ if (globalContext) {
153
+ activeContext = globalContext;
154
+ }
155
+ return activeContext;
156
+ }
146
157
  function setActiveApplicationContext(context) {
147
158
  activeContext = context;
159
+ globalThis[APPLICATION_CONTEXT_KEY] = context;
148
160
  }
149
161
  function requireActiveApplicationContext() {
150
- if (!activeContext) {
162
+ const context = readStoredApplicationContext();
163
+ if (!context) {
151
164
  throw new Error("The application context has not been bootstrapped.");
152
165
  }
153
- return activeContext;
166
+ return context;
154
167
  }
155
168
  function resolveApplicationCache() {
156
169
  return getRequiredDependency(requireActiveApplicationContext().dependencies, "cache");
@@ -1,6 +1,6 @@
1
1
  // @bun
2
2
  // ../../src/bootstrap/context.ts
3
- import { setActiveApplicationContext as setActiveApplicationContext2 } from "@getstrata/core";
3
+ import { setActiveApplicationContext } from "@getstrata/core";
4
4
 
5
5
  // ../../src/bootstrap/contracts.ts
6
6
  class ServiceContainer {
@@ -217,9 +217,22 @@ var databaseConfig = {
217
217
  connectionTimeoutSeconds: readInteger("DB_CONNECTION_TIMEOUT", 10)
218
218
  };
219
219
 
220
- // ../../src/core/database/connectionContext.ts
220
+ // ../../src/core/runtime/asyncContextStore.ts
221
221
  import { AsyncLocalStorage } from "async_hooks";
222
- var activeConnection = new AsyncLocalStorage;
222
+ function createAsyncContextStore(key) {
223
+ const symbol = Symbol.for(key);
224
+ const globalRecord = globalThis;
225
+ const existing = globalRecord[symbol];
226
+ if (existing) {
227
+ return existing;
228
+ }
229
+ const store = new AsyncLocalStorage;
230
+ globalRecord[symbol] = store;
231
+ return store;
232
+ }
233
+
234
+ // ../../src/core/database/connectionContext.ts
235
+ var activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
223
236
  function getActiveDatabaseConnection(fallback) {
224
237
  return activeConnection.getStore() ?? fallback;
225
238
  }
@@ -535,8 +548,7 @@ class PreconditionFailedError extends HttpError {
535
548
  }
536
549
 
537
550
  // ../../src/core/auth/authContext.ts
538
- import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
539
- var authContext = new AsyncLocalStorage2;
551
+ var authContext = createAsyncContextStore("@getstrata/authContext");
540
552
  function currentAuthUser() {
541
553
  return authContext.getStore() ?? null;
542
554
  }
@@ -1410,6 +1422,9 @@ function discoverListeners() {
1410
1422
  return appListeners;
1411
1423
  }
1412
1424
 
1425
+ // ../../src/bootstrap/listeners/invalidateCacheOnModelWrite.ts
1426
+ import { resolveApplicationCache as resolveApplicationCache2, resolveApplicationQueue } from "@getstrata/core";
1427
+
1413
1428
  // ../../src/core/cache/modelCacheTags.ts
1414
1429
  function cacheTagsForModelWrite(tableName, action) {
1415
1430
  const module = appModules.find((entry) => entry.tableName === tableName);
@@ -3334,6 +3349,9 @@ function createProductionQueue(driver, options = {}) {
3334
3349
  return new ResilientQueue(failedJobs, driver === "async");
3335
3350
  }
3336
3351
 
3352
+ // ../../src/bootstrap/queue/defaultJobs.ts
3353
+ import { resolveApplicationCache } from "@getstrata/core";
3354
+
3337
3355
  // ../../src/core/jobs/dispatchWebhookJob.ts
3338
3356
  import { createHmac as createHmac2 } from "crypto";
3339
3357
 
@@ -3521,75 +3539,6 @@ class DispatchWebhookJob extends Job {
3521
3539
  }
3522
3540
  var dispatchWebhookJob_default = DispatchWebhookJob;
3523
3541
 
3524
- // ../../src/core/logging/logger.ts
3525
- class Logger {
3526
- channel;
3527
- constructor(channel = "app") {
3528
- this.channel = channel;
3529
- }
3530
- write(level, message, context = {}) {
3531
- const entry = {
3532
- level,
3533
- channel: this.channel,
3534
- message,
3535
- timestamp: new Date().toISOString(),
3536
- ...context
3537
- };
3538
- const line = JSON.stringify(entry);
3539
- if (level === "error") {
3540
- console.error(line);
3541
- return;
3542
- }
3543
- console.log(line);
3544
- }
3545
- debug(message, context) {
3546
- this.write("debug", message, context);
3547
- }
3548
- info(message, context) {
3549
- this.write("info", message, context);
3550
- }
3551
- warn(message, context) {
3552
- this.write("warn", message, context);
3553
- }
3554
- error(message, context) {
3555
- this.write("error", message, context);
3556
- }
3557
- }
3558
- var appLogger = new Logger("app");
3559
-
3560
- // ../../src/bootstrap/applicationRegistry.ts
3561
- var activeContext;
3562
- function setActiveApplicationContext(context) {
3563
- activeContext = context;
3564
- }
3565
- function requireActiveApplicationContext() {
3566
- if (!activeContext) {
3567
- throw new Error("The application context has not been bootstrapped.");
3568
- }
3569
- return activeContext;
3570
- }
3571
- function resolveApplicationCache() {
3572
- return getRequiredDependency(requireActiveApplicationContext().dependencies, "cache");
3573
- }
3574
- function resolveApplicationQueue() {
3575
- return requireActiveApplicationContext().container.resolve(CORE_QUEUE_TOKEN);
3576
- }
3577
- function resolveApplicationAuth() {
3578
- return requireActiveApplicationContext().container.resolve(CORE_AUTH_TOKEN);
3579
- }
3580
- function resolveApplicationPolicyGate() {
3581
- return requireActiveApplicationContext().container.resolve(CORE_POLICY_GATE_TOKEN);
3582
- }
3583
- function resolveApplicationConfig() {
3584
- return requireActiveApplicationContext().config;
3585
- }
3586
- function resolveApplicationLogger() {
3587
- return appLogger;
3588
- }
3589
- function resolveApplicationDependencies() {
3590
- return requireActiveApplicationContext().dependencies;
3591
- }
3592
-
3593
3542
  // ../../src/bootstrap/queue/defaultJobs.ts
3594
3543
  function registerDefaultJobs() {
3595
3544
  jobRegistry.register("cache.invalidate-tags", () => {
@@ -3621,7 +3570,7 @@ function registerInvalidateCacheOnModelWriteListeners(bus = eventBus) {
3621
3570
  let cache;
3622
3571
  let queue;
3623
3572
  try {
3624
- cache = resolveApplicationCache();
3573
+ cache = resolveApplicationCache2();
3625
3574
  queue = resolveApplicationQueue();
3626
3575
  } catch {
3627
3576
  return;
@@ -3855,8 +3804,7 @@ function isViewsEnabled() {
3855
3804
  }
3856
3805
 
3857
3806
  // ../../src/core/http/requestMetaContext.ts
3858
- import { AsyncLocalStorage as AsyncLocalStorage3 } from "async_hooks";
3859
- var requestMetaContext = new AsyncLocalStorage3;
3807
+ var requestMetaContext = createAsyncContextStore("@getstrata/requestMetaContext");
3860
3808
  function currentRequestMeta() {
3861
3809
  return requestMetaContext.getStore() ?? {
3862
3810
  ipAddress: null,
@@ -4189,7 +4137,7 @@ function createAppContext() {
4189
4137
  config,
4190
4138
  dependencies
4191
4139
  };
4192
- setActiveApplicationContext2(appContext);
4140
+ setActiveApplicationContext(appContext);
4193
4141
  return appContext;
4194
4142
  }
4195
4143
  var cachedAppContext;
@@ -173,9 +173,22 @@ var databaseConfig = {
173
173
  connectionTimeoutSeconds: readInteger("DB_CONNECTION_TIMEOUT", 10)
174
174
  };
175
175
 
176
- // ../../src/core/database/connectionContext.ts
176
+ // ../../src/core/runtime/asyncContextStore.ts
177
177
  import { AsyncLocalStorage } from "async_hooks";
178
- var activeConnection = new AsyncLocalStorage;
178
+ function createAsyncContextStore(key) {
179
+ const symbol = Symbol.for(key);
180
+ const globalRecord = globalThis;
181
+ const existing = globalRecord[symbol];
182
+ if (existing) {
183
+ return existing;
184
+ }
185
+ const store = new AsyncLocalStorage;
186
+ globalRecord[symbol] = store;
187
+ return store;
188
+ }
189
+
190
+ // ../../src/core/database/connectionContext.ts
191
+ var activeConnection = createAsyncContextStore("@getstrata/databaseConnectionContext");
179
192
  function getActiveDatabaseConnection(fallback) {
180
193
  return activeConnection.getStore() ?? fallback;
181
194
  }
@@ -486,8 +499,7 @@ import { resolveDefaultTokenExpiryDays as resolveDefaultTokenExpiryDays2 } from
486
499
  var tokenServiceToken = CORE_TOKEN_SERVICE_TOKEN;
487
500
 
488
501
  // ../../src/core/auth/authContext.ts
489
- import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
490
- var authContext = new AsyncLocalStorage2;
502
+ var authContext = createAsyncContextStore("@getstrata/authContext");
491
503
  function currentAuthUser() {
492
504
  return authContext.getStore() ?? null;
493
505
  }
@@ -518,8 +530,7 @@ function readRequestCookie(request, name) {
518
530
  }
519
531
 
520
532
  // ../../src/core/http/requestMetaContext.ts
521
- import { AsyncLocalStorage as AsyncLocalStorage3 } from "async_hooks";
522
- var requestMetaContext = new AsyncLocalStorage3;
533
+ var requestMetaContext = createAsyncContextStore("@getstrata/requestMetaContext");
523
534
  function currentRequestMeta() {
524
535
  return requestMetaContext.getStore() ?? {
525
536
  ipAddress: null,
@@ -1,7 +1,23 @@
1
1
  // @bun
2
- // ../../src/core/auth/authContext.ts
2
+ // ../../src/bootstrap/http/securedRouteModelBinding.ts
3
+ import { resolveApplicationAuth, resolveApplicationPolicyGate } from "@getstrata/core";
4
+
5
+ // ../../src/core/runtime/asyncContextStore.ts
3
6
  import { AsyncLocalStorage } from "async_hooks";
4
- var authContext = new AsyncLocalStorage;
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");
5
21
  function currentAuthUser() {
6
22
  return authContext.getStore() ?? null;
7
23
  }
@@ -143,8 +159,7 @@ function applyConditionalGet(request, response, etag) {
143
159
  }
144
160
 
145
161
  // ../../src/core/tenant/tenantContext.ts
146
- import { AsyncLocalStorage as AsyncLocalStorage2 } from "async_hooks";
147
- var tenantContext = new AsyncLocalStorage2;
162
+ var tenantContext = createAsyncContextStore("@getstrata/tenantContext");
148
163
 
149
164
  // ../../src/core/http/validation.ts
150
165
  function parsePositiveIntParam(value, name = "id") {
@@ -155,181 +170,6 @@ function parsePositiveIntParam(value, name = "id") {
155
170
  return parsed;
156
171
  }
157
172
 
158
- // ../../src/core/logging/logger.ts
159
- class Logger {
160
- channel;
161
- constructor(channel = "app") {
162
- this.channel = channel;
163
- }
164
- write(level, message, context = {}) {
165
- const entry = {
166
- level,
167
- channel: this.channel,
168
- message,
169
- timestamp: new Date().toISOString(),
170
- ...context
171
- };
172
- const line = JSON.stringify(entry);
173
- if (level === "error") {
174
- console.error(line);
175
- return;
176
- }
177
- console.log(line);
178
- }
179
- debug(message, context) {
180
- this.write("debug", message, context);
181
- }
182
- info(message, context) {
183
- this.write("info", message, context);
184
- }
185
- warn(message, context) {
186
- this.write("warn", message, context);
187
- }
188
- error(message, context) {
189
- this.write("error", message, context);
190
- }
191
- }
192
- var appLogger = new Logger("app");
193
-
194
- // ../../src/bootstrap/config.ts
195
- var APP_PORT_CONFIG_KEY = "app.port";
196
- var CACHE_TTL_MS_CONFIG_KEY = "cache.ttlMs";
197
- var CACHE_MAX_ENTRIES_CONFIG_KEY = "cache.maxEntries";
198
- var CACHE_DRIVER_CONFIG_KEY = "cache.driver";
199
- var REDIS_URL_CONFIG_KEY = "cache.redisUrl";
200
- var DATABASE_URL_CONFIG_KEY = "database.url";
201
- var CORE_CONFIG_TOKEN = "core.config";
202
- var CORE_CACHE_TOKEN = "core.cache";
203
- var CORE_QUEUE_TOKEN = "core.queue";
204
- var CORE_POLICY_GATE_TOKEN = "core.policyGate";
205
- var CORE_AUTH_TOKEN = "core.auth";
206
- var CORE_TOKEN_SERVICE_TOKEN = "core.tokenService";
207
- var AUTH_DEV_HEADERS_CONFIG_KEY = "auth.allowDevHeaders";
208
- var DEFAULT_APP_PORT = 3000;
209
- var DEFAULT_CACHE_TTL_MS = 3600000;
210
- var DEFAULT_CACHE_MAX_ENTRIES = 100;
211
- var DEFAULT_CACHE_DRIVER = "array";
212
- var DEFAULT_API_TOKEN = "";
213
- var DEFAULT_QUEUE_DRIVER = "sync";
214
-
215
- // ../../src/bootstrap/contracts.ts
216
- class ServiceContainer {
217
- services = new Map;
218
- singletonFactories = new Map;
219
- bindings = new Map;
220
- set(key, value) {
221
- this.singletonFactories.delete(key);
222
- this.bindings.delete(key);
223
- this.services.set(key, value);
224
- return value;
225
- }
226
- singleton(key, factory) {
227
- this.bindings.delete(key);
228
- this.services.delete(key);
229
- this.singletonFactories.set(key, factory);
230
- }
231
- bind(key, factory) {
232
- this.singletonFactories.delete(key);
233
- this.services.delete(key);
234
- this.bindings.set(key, factory);
235
- }
236
- get(key) {
237
- if (this.services.has(key)) {
238
- return this.services.get(key);
239
- }
240
- const singletonFactory = this.singletonFactories.get(key);
241
- if (singletonFactory) {
242
- const value = singletonFactory(this);
243
- this.services.set(key, value);
244
- return value;
245
- }
246
- const binding = this.bindings.get(key);
247
- if (binding) {
248
- return binding(this);
249
- }
250
- throw new Error(`Service "${key}" is not registered.`);
251
- }
252
- resolve(key) {
253
- return this.get(key);
254
- }
255
- has(key) {
256
- return this.services.has(key) || this.singletonFactories.has(key) || this.bindings.has(key);
257
- }
258
- }
259
-
260
- class ConfigStore {
261
- values = new Map;
262
- set(key, value) {
263
- this.values.set(key, value);
264
- return value;
265
- }
266
- get(key) {
267
- return this.values.get(key);
268
- }
269
- require(key) {
270
- if (!this.values.has(key)) {
271
- throw new Error(`Config key "${key}" is not defined.`);
272
- }
273
- return this.values.get(key);
274
- }
275
- has(key) {
276
- return this.values.has(key);
277
- }
278
- }
279
- var requiredDependencyKeys = [
280
- "container",
281
- "cache",
282
- "storage"
283
- ];
284
- function getRequiredDependency(dependencies, key) {
285
- const dependency = dependencies[key];
286
- if (dependency === undefined) {
287
- throw new Error(`Required dependency "${key}" is not registered.`);
288
- }
289
- return dependency;
290
- }
291
- function assertAppDependenciesComplete(dependencies) {
292
- for (const key of requiredDependencyKeys) {
293
- getRequiredDependency(dependencies, key);
294
- }
295
- }
296
- function resolveService(dependencies, token) {
297
- return dependencies.container.resolve(token);
298
- }
299
-
300
- // ../../src/bootstrap/applicationRegistry.ts
301
- var activeContext;
302
- function setActiveApplicationContext(context) {
303
- activeContext = context;
304
- }
305
- function requireActiveApplicationContext() {
306
- if (!activeContext) {
307
- throw new Error("The application context has not been bootstrapped.");
308
- }
309
- return activeContext;
310
- }
311
- function resolveApplicationCache() {
312
- return getRequiredDependency(requireActiveApplicationContext().dependencies, "cache");
313
- }
314
- function resolveApplicationQueue() {
315
- return requireActiveApplicationContext().container.resolve(CORE_QUEUE_TOKEN);
316
- }
317
- function resolveApplicationAuth() {
318
- return requireActiveApplicationContext().container.resolve(CORE_AUTH_TOKEN);
319
- }
320
- function resolveApplicationPolicyGate() {
321
- return requireActiveApplicationContext().container.resolve(CORE_POLICY_GATE_TOKEN);
322
- }
323
- function resolveApplicationConfig() {
324
- return requireActiveApplicationContext().config;
325
- }
326
- function resolveApplicationLogger() {
327
- return appLogger;
328
- }
329
- function resolveApplicationDependencies() {
330
- return requireActiveApplicationContext().dependencies;
331
- }
332
-
333
173
  // ../../src/bootstrap/http/securedRouteModelBinding.ts
334
174
  function isMutatingPolicyAction(action) {
335
175
  return action === "update" || action === "delete";