@getstrata/bootstrap 3.0.4 → 3.0.7
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/CHANGELOG.md +26 -0
- package/README.md +11 -8
- package/dist/bootstrap/application/config.d.ts +7 -0
- package/dist/bootstrap/application/services.d.ts +28 -0
- package/dist/bootstrap/application.d.ts +115 -0
- package/dist/bootstrap/auth/accountWorkflows.d.ts +68 -0
- package/dist/bootstrap/auth/runtimeConfig.d.ts +66 -0
- package/dist/bootstrap/config.d.ts +1 -1
- package/dist/bootstrap/contracts.d.ts +1 -0
- package/dist/bootstrap/createSpaRoutes.d.ts +2 -0
- package/dist/bootstrap/databaseCommands.d.ts +26 -0
- package/dist/bootstrap/discoverJobs.d.ts +1 -2
- package/dist/bootstrap/discoverListeners.d.ts +4 -0
- package/dist/bootstrap/httpKernel.d.ts +0 -1
- package/dist/bootstrap/httpResponseComposition.d.ts +32 -0
- package/dist/bootstrap/jobDiscoveryState.d.ts +12 -0
- package/dist/bootstrap/providers/events.d.ts +2 -2
- package/dist/bootstrap/public-api.d.ts +3 -2
- package/dist/bootstrap/testing.d.ts +4 -0
- package/dist/entries/application.js +1 -0
- package/dist/entries/auth/accountWorkflows.js +98 -0
- package/dist/entries/auth/runtimeConfig.js +79 -0
- package/dist/entries/buildModuleRoutes.js +114 -94
- package/dist/entries/buildWebModuleRoutes.js +114 -94
- package/dist/entries/config.js +2 -0
- package/dist/entries/context.js +12 -18
- package/dist/entries/createSpaRoutes.js +2 -2
- package/dist/entries/createWebRoutes.js +114 -94
- package/dist/entries/databaseCommands.js +103 -0
- package/dist/entries/dependencies.js +12 -18
- package/dist/entries/discoverJobs.js +2 -15
- package/dist/entries/discoverListeners.js +34 -0
- package/dist/entries/health.js +1 -0
- package/dist/entries/httpKernel.js +114 -94
- package/dist/entries/providers/auth.js +1 -0
- package/dist/entries/providers/events.js +1 -0
- package/dist/entries/providers.js +6 -15
- package/dist/entries/testing.js +10 -0
- package/dist/entries/web/routing.js +114 -94
- package/dist/entries/web/server.js +2 -1
- package/dist/index.js +3222 -2272
- package/dist/internal/jobDiscoveryState.js +14 -0
- package/package.json +35 -6
package/dist/entries/context.js
CHANGED
|
@@ -204,6 +204,7 @@ import {
|
|
|
204
204
|
CORE_EVENT_BUS_TOKEN,
|
|
205
205
|
CORE_HTTP_CLEANUP_TOKEN,
|
|
206
206
|
CORE_POLICY_GATE_TOKEN,
|
|
207
|
+
CORE_POLICY_REGISTRY_TOKEN,
|
|
207
208
|
CORE_PUBLIC_TENANCY_TOKEN,
|
|
208
209
|
CORE_QUEUE_TOKEN,
|
|
209
210
|
CORE_TENANT_RESOLVER_TOKEN,
|
|
@@ -398,7 +399,7 @@ var appEnvSchema = defineEnvSchema({
|
|
|
398
399
|
},
|
|
399
400
|
JWT_TTL_SECONDS: {
|
|
400
401
|
integer: true,
|
|
401
|
-
minimum:
|
|
402
|
+
minimum: 1,
|
|
402
403
|
default: "3600"
|
|
403
404
|
},
|
|
404
405
|
APP_ENV: {
|
|
@@ -579,7 +580,9 @@ import { PolicyGate } from "@getstrata/core/auth/policy";
|
|
|
579
580
|
var policyProvider = {
|
|
580
581
|
name: "core.policy",
|
|
581
582
|
register({ container }) {
|
|
582
|
-
|
|
583
|
+
const gate = new PolicyGate;
|
|
584
|
+
container.set(CORE_POLICY_GATE_TOKEN, gate);
|
|
585
|
+
container.set(CORE_POLICY_REGISTRY_TOKEN, gate);
|
|
583
586
|
}
|
|
584
587
|
};
|
|
585
588
|
var policy_default = policyProvider;
|
|
@@ -648,15 +651,7 @@ async function importInfrastructure(file) {
|
|
|
648
651
|
}
|
|
649
652
|
|
|
650
653
|
// ../../src/bootstrap/discoverJobs.ts
|
|
651
|
-
|
|
652
|
-
function state(registry) {
|
|
653
|
-
let value = states.get(registry);
|
|
654
|
-
if (!value) {
|
|
655
|
-
value = { loads: new Map, registered: new Map };
|
|
656
|
-
states.set(registry, value);
|
|
657
|
-
}
|
|
658
|
-
return value;
|
|
659
|
-
}
|
|
654
|
+
import { jobDiscoveryState as state } from "#strata-job-discovery";
|
|
660
655
|
function validateClass(value, location) {
|
|
661
656
|
if (typeof value !== "function" || typeof value.jobName !== "string" || !value.jobName.trim() || !(value.prototype instanceof Job) && typeof value.prototype?.handle !== "function")
|
|
662
657
|
throw new TypeError(`Invalid job at ${location}: expected a class with nonempty static jobName and handle().`);
|
|
@@ -738,10 +733,6 @@ function discoverJobs(registry, options = {}) {
|
|
|
738
733
|
cache.set(key, pending);
|
|
739
734
|
return pending;
|
|
740
735
|
}
|
|
741
|
-
function resetDiscoverJobsForTests(registry) {
|
|
742
|
-
state(registry).loads = new Map;
|
|
743
|
-
state(registry).registered = new Map;
|
|
744
|
-
}
|
|
745
736
|
|
|
746
737
|
// ../../src/bootstrap/queue/defaultJobs.ts
|
|
747
738
|
import { ExportAuditLogsJob } from "@getstrata/core/jobs/exportAuditLogsJob";
|
|
@@ -966,6 +957,7 @@ function createApplicationContext(providers, options) {
|
|
|
966
957
|
}
|
|
967
958
|
};
|
|
968
959
|
container.set(CORE_HTTP_CLEANUP_TOKEN2, (callback) => context.onCleanup(callback));
|
|
960
|
+
let selectedProviders = [];
|
|
969
961
|
application = createApplication({
|
|
970
962
|
...options.definition,
|
|
971
963
|
providers: [
|
|
@@ -980,9 +972,11 @@ function createApplicationContext(providers, options) {
|
|
|
980
972
|
flush: () => phase("flush"),
|
|
981
973
|
close: () => phase("close")
|
|
982
974
|
});
|
|
983
|
-
|
|
984
|
-
await runProviderPhase(
|
|
985
|
-
|
|
975
|
+
selectedProviders = [...configured, ...options.providersFromScope?.(scope) ?? []];
|
|
976
|
+
await runProviderPhase(selectedProviders, "register", context);
|
|
977
|
+
},
|
|
978
|
+
boot: async () => {
|
|
979
|
+
await runProviderPhase(selectedProviders, "boot", context);
|
|
986
980
|
assertAppDependenciesComplete(dependencies);
|
|
987
981
|
}
|
|
988
982
|
}
|
|
@@ -239,14 +239,14 @@ function createSpaRoutes(_dependencies, options = {}) {
|
|
|
239
239
|
[`${prefix}/`]: handler,
|
|
240
240
|
[`${prefix}/*`]: handler
|
|
241
241
|
};
|
|
242
|
-
if (!isViewsEnabled()) {
|
|
242
|
+
if (!(options.viewsEnabled ?? isViewsEnabled())) {
|
|
243
243
|
routes["/"] = async () => Response.redirect(`${prefix}/`, 302);
|
|
244
244
|
}
|
|
245
245
|
const plan = compileRoutePlan([{ name: "SPA", routes }]);
|
|
246
246
|
return publishRoutePlan(plan, plan.routes);
|
|
247
247
|
}
|
|
248
248
|
function mergeSpaRoutes(dependencies, routes, options) {
|
|
249
|
-
if (!isSpaEnabled()) {
|
|
249
|
+
if (!(options?.enabled ?? isSpaEnabled())) {
|
|
250
250
|
return routes;
|
|
251
251
|
}
|
|
252
252
|
const spaRoutes = createSpaRoutes(dependencies, options);
|
|
@@ -27,33 +27,20 @@ function createApplicationCachedJson(dependencies) {
|
|
|
27
27
|
// ../../src/bootstrap/httpKernel.ts
|
|
28
28
|
import { establishedAuthReader as establishedAuthReader2 } from "@getstrata/core/auth/authReader";
|
|
29
29
|
import { hasVerifiedEmail } from "@getstrata/core/auth/emailVerification";
|
|
30
|
-
import {
|
|
31
|
-
CORE_TRACING_RUNTIME_TOKEN,
|
|
32
|
-
resolveAbilityChecker
|
|
33
|
-
} from "@getstrata/core/contracts/serviceTokens";
|
|
30
|
+
import { resolveAbilityChecker } from "@getstrata/core/contracts/serviceTokens";
|
|
34
31
|
import { createAuthorizeMiddleware } from "@getstrata/core/http/authorizeMiddleware";
|
|
35
|
-
import { createBodySizeLimitMiddleware } from "@getstrata/core/http/bodySizeLimitMiddleware";
|
|
36
|
-
import { createCorsMiddleware, resolveCorsConfig } from "@getstrata/core/http/corsMiddleware";
|
|
37
|
-
import { createCsrfMiddleware } from "@getstrata/core/http/csrfMiddleware";
|
|
38
32
|
import { createFlashMiddleware } from "@getstrata/core/http/flashMiddleware";
|
|
39
33
|
import { createLoginThrottleMiddleware } from "@getstrata/core/http/loginThrottleMiddleware";
|
|
40
34
|
import { createMemoryThrottleMiddleware } from "@getstrata/core/http/memoryThrottleMiddleware";
|
|
41
|
-
import { createMetricsMiddleware } from "@getstrata/core/http/metricsMiddleware";
|
|
42
|
-
import { requestIdMiddleware } from "@getstrata/core/http/middleware";
|
|
43
35
|
import { createRequireAbilityMiddleware } from "@getstrata/core/http/requireAbilityMiddleware";
|
|
44
36
|
import { createRequireAuthMiddleware } from "@getstrata/core/http/requireAuthMiddleware";
|
|
45
37
|
import { createRequireGlobalAdminMiddleware } from "@getstrata/core/http/requireGlobalAdminMiddleware";
|
|
46
38
|
import { createRequirePasswordConfirmMiddleware } from "@getstrata/core/http/requirePasswordConfirmMiddleware";
|
|
47
39
|
import { createRequireVerifiedMiddleware } from "@getstrata/core/http/requireVerifiedMiddleware";
|
|
48
40
|
import { createRequireWebAuthMiddleware } from "@getstrata/core/http/requireWebAuthMiddleware";
|
|
49
|
-
import { createJsonErrorMiddleware, createWebErrorMiddleware } from "@getstrata/core/http/response";
|
|
50
41
|
import { withMiddleware } from "@getstrata/core/http/routeMiddleware";
|
|
51
|
-
import { createSecurityHeadersMiddleware } from "@getstrata/core/http/securityHeadersMiddleware";
|
|
52
42
|
import { createValidateSignatureMiddleware } from "@getstrata/core/http/signedUrl";
|
|
53
43
|
import { createThrottleMiddleware } from "@getstrata/core/http/throttleMiddleware";
|
|
54
|
-
import { createRequestLoggingMiddleware } from "@getstrata/core/logging/requestLoggingMiddleware";
|
|
55
|
-
import { createTracingMiddleware } from "@getstrata/core/tracing/tracingMiddleware";
|
|
56
|
-
import { CORE_WEB_ERROR_VIEW_TOKEN } from "@getstrata/core/view/tokens";
|
|
57
44
|
import {
|
|
58
45
|
assertHttpHandler,
|
|
59
46
|
identityModes,
|
|
@@ -72,6 +59,7 @@ import {
|
|
|
72
59
|
CORE_EVENT_BUS_TOKEN,
|
|
73
60
|
CORE_HTTP_CLEANUP_TOKEN,
|
|
74
61
|
CORE_POLICY_GATE_TOKEN,
|
|
62
|
+
CORE_POLICY_REGISTRY_TOKEN,
|
|
75
63
|
CORE_PUBLIC_TENANCY_TOKEN,
|
|
76
64
|
CORE_QUEUE_TOKEN,
|
|
77
65
|
CORE_TENANT_RESOLVER_TOKEN,
|
|
@@ -241,39 +229,18 @@ function createHttpPersistence(invoke, options) {
|
|
|
241
229
|
};
|
|
242
230
|
}
|
|
243
231
|
|
|
244
|
-
// ../../src/bootstrap/
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
if (typeof value === "function") {
|
|
257
|
-
wrapped[path] = enter(value);
|
|
258
|
-
continue;
|
|
259
|
-
}
|
|
260
|
-
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
261
|
-
const entries = Object.entries(value);
|
|
262
|
-
if (entries.some(([, handler]) => typeof handler === "function" || handler instanceof Response)) {
|
|
263
|
-
wrapped[path] = Object.fromEntries(entries.map(([method, handler]) => {
|
|
264
|
-
const callback = handler instanceof Response ? () => handler.clone() : handler;
|
|
265
|
-
return [
|
|
266
|
-
method,
|
|
267
|
-
typeof callback === "function" ? enter(callback) : callback
|
|
268
|
-
];
|
|
269
|
-
}));
|
|
270
|
-
continue;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
wrapped[path] = value;
|
|
274
|
-
}
|
|
275
|
-
return wrapped;
|
|
276
|
-
}
|
|
232
|
+
// ../../src/bootstrap/httpResponseComposition.ts
|
|
233
|
+
import { CORE_TRACING_RUNTIME_TOKEN } from "@getstrata/core/contracts/serviceTokens";
|
|
234
|
+
import { createBodySizeLimitMiddleware } from "@getstrata/core/http/bodySizeLimitMiddleware";
|
|
235
|
+
import { createCorsMiddleware, resolveCorsConfig } from "@getstrata/core/http/corsMiddleware";
|
|
236
|
+
import { createCsrfMiddleware } from "@getstrata/core/http/csrfMiddleware";
|
|
237
|
+
import { createMetricsMiddleware } from "@getstrata/core/http/metricsMiddleware";
|
|
238
|
+
import { requestIdMiddleware } from "@getstrata/core/http/middleware";
|
|
239
|
+
import { createJsonErrorMiddleware, createWebErrorMiddleware } from "@getstrata/core/http/response";
|
|
240
|
+
import { createSecurityHeadersMiddleware } from "@getstrata/core/http/securityHeadersMiddleware";
|
|
241
|
+
import { createRequestLoggingMiddleware } from "@getstrata/core/logging/requestLoggingMiddleware";
|
|
242
|
+
import { createTracingMiddleware } from "@getstrata/core/tracing/tracingMiddleware";
|
|
243
|
+
import { CORE_WEB_ERROR_VIEW_TOKEN } from "@getstrata/core/view/tokens";
|
|
277
244
|
|
|
278
245
|
// ../../src/bootstrap/rateLimit.ts
|
|
279
246
|
var LOCAL_LOGIN_RATE_LIMIT = {
|
|
@@ -318,6 +285,97 @@ function resolveRegisterRateLimit(env = {}) {
|
|
|
318
285
|
};
|
|
319
286
|
}
|
|
320
287
|
|
|
288
|
+
// ../../src/bootstrap/httpResponseComposition.ts
|
|
289
|
+
function resolveHttpResponseSettings(dependencies, http) {
|
|
290
|
+
const config = dependencies.container.has(CORE_CONFIG_TOKEN) ? dependencies.container.resolve(CORE_CONFIG_TOKEN) : undefined;
|
|
291
|
+
const features = http?.features ?? config?.get("http.features");
|
|
292
|
+
const emailVerificationRequired = features?.emailVerificationRequired ?? false;
|
|
293
|
+
const viewsEnabled = features?.viewsEnabled ?? false;
|
|
294
|
+
const publicReadsEnabled = features?.publicReadsEnabled ?? false;
|
|
295
|
+
const etagEnabled = features?.etagEnabled ?? true;
|
|
296
|
+
const policy = http?.responsePolicy ?? config?.get("http.responsePolicy");
|
|
297
|
+
if (policy?.cors)
|
|
298
|
+
resolveCorsConfig(policy.cors);
|
|
299
|
+
const responsePolicy = Object.freeze({
|
|
300
|
+
cors: policy?.cors ? {
|
|
301
|
+
allowedOrigins: [...policy.cors.allowedOrigins ?? []],
|
|
302
|
+
additionalAllowedHeaders: [...policy.cors.additionalAllowedHeaders ?? []]
|
|
303
|
+
} : undefined,
|
|
304
|
+
securityHeaders: policy?.securityHeaders ?? {
|
|
305
|
+
production: http?.production ?? true,
|
|
306
|
+
frontendMode: viewsEnabled ? "server-htmx" : "api"
|
|
307
|
+
},
|
|
308
|
+
maxBodyBytes: policy?.maxBodyBytes ?? 1048576,
|
|
309
|
+
rateLimitPerMinute: policy?.rateLimitPerMinute ?? 120,
|
|
310
|
+
namespace: policy?.namespace ?? "strata",
|
|
311
|
+
trustForwardedFor: policy?.trustForwardedFor ?? false,
|
|
312
|
+
loginRateLimit: Object.freeze({ ...policy?.loginRateLimit ?? resolveLoginRateLimit() }),
|
|
313
|
+
registerRateLimit: Object.freeze({
|
|
314
|
+
...policy?.registerRateLimit ?? resolveRegisterRateLimit()
|
|
315
|
+
})
|
|
316
|
+
});
|
|
317
|
+
const targetOrigin = dependencies.container.has(CORE_CONFIG_TOKEN) ? dependencies.container.resolve(CORE_CONFIG_TOKEN).get(CSRF_TARGET_ORIGIN_CONFIG_KEY) : undefined;
|
|
318
|
+
const configuredCsrfMiddleware = createCsrfMiddleware({ targetOrigin, ...http?.csrf });
|
|
319
|
+
return {
|
|
320
|
+
emailVerificationRequired,
|
|
321
|
+
viewsEnabled,
|
|
322
|
+
publicReadsEnabled,
|
|
323
|
+
etagEnabled,
|
|
324
|
+
responsePolicy,
|
|
325
|
+
configuredCsrfMiddleware
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
function createHttpResponseMiddleware(dependencies, settings, errorFormat) {
|
|
329
|
+
return [
|
|
330
|
+
createCorsMiddleware(settings.responsePolicy.cors),
|
|
331
|
+
createSecurityHeadersMiddleware(settings.responsePolicy.securityHeaders),
|
|
332
|
+
createBodySizeLimitMiddleware(settings.responsePolicy.maxBodyBytes),
|
|
333
|
+
...dependencies.container.has(CORE_TRACING_RUNTIME_TOKEN) ? [createTracingMiddleware(dependencies.container.resolve(CORE_TRACING_RUNTIME_TOKEN))] : [],
|
|
334
|
+
createMetricsMiddleware(),
|
|
335
|
+
createRequestLoggingMiddleware(settings.responsePolicy.trustForwardedFor),
|
|
336
|
+
requestIdMiddleware,
|
|
337
|
+
errorFormat === "json" ? createJsonErrorMiddleware() : createWebErrorMiddleware({
|
|
338
|
+
enabled: settings.viewsEnabled,
|
|
339
|
+
production: dependencies.container.has(HTTP_PERSISTENCE_OPTIONS) ? dependencies.container.resolve(HTTP_PERSISTENCE_OPTIONS).production ?? true : true,
|
|
340
|
+
render: dependencies.container.has(CORE_WEB_ERROR_VIEW_TOKEN) ? dependencies.container.resolve(CORE_WEB_ERROR_VIEW_TOKEN) : undefined
|
|
341
|
+
})
|
|
342
|
+
];
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// ../../src/bootstrap/moduleRouteMiddleware.ts
|
|
346
|
+
function enterModuleRoutes(routes, enter) {
|
|
347
|
+
const wrapped = {};
|
|
348
|
+
for (const [path, value] of Object.entries(routes)) {
|
|
349
|
+
if (value instanceof Response) {
|
|
350
|
+
const methods = {};
|
|
351
|
+
for (const method of ["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"]) {
|
|
352
|
+
methods[method] = enter(() => value.clone());
|
|
353
|
+
}
|
|
354
|
+
wrapped[path] = methods;
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
if (typeof value === "function") {
|
|
358
|
+
wrapped[path] = enter(value);
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
362
|
+
const entries = Object.entries(value);
|
|
363
|
+
if (entries.some(([, handler]) => typeof handler === "function" || handler instanceof Response)) {
|
|
364
|
+
wrapped[path] = Object.fromEntries(entries.map(([method, handler]) => {
|
|
365
|
+
const callback = handler instanceof Response ? () => handler.clone() : handler;
|
|
366
|
+
return [
|
|
367
|
+
method,
|
|
368
|
+
typeof callback === "function" ? enter(callback) : callback
|
|
369
|
+
];
|
|
370
|
+
}));
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
wrapped[path] = value;
|
|
375
|
+
}
|
|
376
|
+
return wrapped;
|
|
377
|
+
}
|
|
378
|
+
|
|
321
379
|
// ../../src/bootstrap/httpKernel.ts
|
|
322
380
|
class HttpKernel {
|
|
323
381
|
dependencies;
|
|
@@ -336,38 +394,16 @@ class HttpKernel {
|
|
|
336
394
|
this.dependencies = dependencies;
|
|
337
395
|
const http = dependencies.container.has(HTTP_PERSISTENCE_OPTIONS) ? dependencies.container.resolve(HTTP_PERSISTENCE_OPTIONS) : undefined;
|
|
338
396
|
this.httpOptions = http;
|
|
339
|
-
const
|
|
340
|
-
|
|
341
|
-
this.
|
|
342
|
-
this.
|
|
343
|
-
this.
|
|
344
|
-
this.
|
|
345
|
-
|
|
346
|
-
if (policy?.cors)
|
|
347
|
-
resolveCorsConfig(policy.cors);
|
|
348
|
-
this.responsePolicy = Object.freeze({
|
|
349
|
-
cors: policy?.cors ? {
|
|
350
|
-
allowedOrigins: [...policy.cors.allowedOrigins ?? []],
|
|
351
|
-
additionalAllowedHeaders: [...policy.cors.additionalAllowedHeaders ?? []]
|
|
352
|
-
} : undefined,
|
|
353
|
-
securityHeaders: policy?.securityHeaders ?? {
|
|
354
|
-
production: http?.production ?? true,
|
|
355
|
-
frontendMode: this.viewsEnabled ? "server-htmx" : "api"
|
|
356
|
-
},
|
|
357
|
-
maxBodyBytes: policy?.maxBodyBytes ?? 1048576,
|
|
358
|
-
rateLimitPerMinute: policy?.rateLimitPerMinute ?? 120,
|
|
359
|
-
namespace: policy?.namespace ?? "strata",
|
|
360
|
-
trustForwardedFor: policy?.trustForwardedFor ?? false,
|
|
361
|
-
loginRateLimit: Object.freeze({ ...policy?.loginRateLimit ?? resolveLoginRateLimit() }),
|
|
362
|
-
registerRateLimit: Object.freeze({
|
|
363
|
-
...policy?.registerRateLimit ?? resolveRegisterRateLimit()
|
|
364
|
-
})
|
|
365
|
-
});
|
|
366
|
-
const targetOrigin = dependencies.container.has(CORE_CONFIG_TOKEN) ? dependencies.container.resolve(CORE_CONFIG_TOKEN).get(CSRF_TARGET_ORIGIN_CONFIG_KEY) : undefined;
|
|
367
|
-
this.configuredCsrfMiddleware = createCsrfMiddleware({ targetOrigin, ...http?.csrf });
|
|
397
|
+
const settings = resolveHttpResponseSettings(dependencies, http);
|
|
398
|
+
this.emailVerificationRequired = settings.emailVerificationRequired;
|
|
399
|
+
this.viewsEnabled = settings.viewsEnabled;
|
|
400
|
+
this.publicReadsEnabled = settings.publicReadsEnabled;
|
|
401
|
+
this.etagEnabled = settings.etagEnabled;
|
|
402
|
+
this.responsePolicy = settings.responsePolicy;
|
|
403
|
+
this.configuredCsrfMiddleware = settings.configuredCsrfMiddleware;
|
|
368
404
|
this.responses = Object.freeze({
|
|
369
|
-
json: Object.freeze(
|
|
370
|
-
web: Object.freeze(
|
|
405
|
+
json: Object.freeze(createHttpResponseMiddleware(dependencies, settings, "json")),
|
|
406
|
+
web: Object.freeze(createHttpResponseMiddleware(dependencies, settings, "web"))
|
|
371
407
|
});
|
|
372
408
|
this.persistence = dependencies.container.has(HTTP_PERSISTENCE_OPTIONS) ? createHttpPersistence(dependencies.invoke, dependencies.container.resolve(HTTP_PERSISTENCE_OPTIONS)) : undefined;
|
|
373
409
|
if (dependencies.container.has(CORE_HTTP_CLEANUP_TOKEN)) {
|
|
@@ -462,22 +498,6 @@ class HttpKernel {
|
|
|
462
498
|
throw new TypeError("SCIM and identity operation declarations cannot be combined.");
|
|
463
499
|
return this.composed(handler, "json", undefined, Object.freeze([...before]));
|
|
464
500
|
}
|
|
465
|
-
responseMiddleware(errorFormat) {
|
|
466
|
-
return [
|
|
467
|
-
createCorsMiddleware(this.responsePolicy.cors),
|
|
468
|
-
createSecurityHeadersMiddleware(this.responsePolicy.securityHeaders),
|
|
469
|
-
createBodySizeLimitMiddleware(this.responsePolicy.maxBodyBytes),
|
|
470
|
-
...this.dependencies.container.has(CORE_TRACING_RUNTIME_TOKEN) ? [createTracingMiddleware(this.dependencies.container.resolve(CORE_TRACING_RUNTIME_TOKEN))] : [],
|
|
471
|
-
createMetricsMiddleware(),
|
|
472
|
-
createRequestLoggingMiddleware(this.responsePolicy.trustForwardedFor),
|
|
473
|
-
requestIdMiddleware,
|
|
474
|
-
errorFormat === "json" ? createJsonErrorMiddleware() : createWebErrorMiddleware({
|
|
475
|
-
enabled: this.viewsEnabled,
|
|
476
|
-
production: this.dependencies.container.has(HTTP_PERSISTENCE_OPTIONS) ? this.dependencies.container.resolve(HTTP_PERSISTENCE_OPTIONS).production ?? true : true,
|
|
477
|
-
render: this.dependencies.container.has(CORE_WEB_ERROR_VIEW_TOKEN) ? this.dependencies.container.resolve(CORE_WEB_ERROR_VIEW_TOKEN) : undefined
|
|
478
|
-
})
|
|
479
|
-
];
|
|
480
|
-
}
|
|
481
501
|
wrapPublicDocument(handler) {
|
|
482
502
|
assertHttpHandler(handler);
|
|
483
503
|
return ownedPublicHttpRoute(handler, (request, work) => Promise.resolve(withMiddleware(...this.responses.web)(work)(request)));
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/bootstrap/databaseCommands.ts
|
|
3
|
+
import { mkdirSync } from "fs";
|
|
4
|
+
import { dirname, resolve } from "path";
|
|
5
|
+
import { runWithSqlDialect } from "@getstrata/core/database/dialect";
|
|
6
|
+
import { isProductionEnv } from "@getstrata/core/runtime/appEnv";
|
|
7
|
+
import {
|
|
8
|
+
ensurePostgresDatabaseAndAppRole,
|
|
9
|
+
POSTGRES_APP_ROLE_PASSWORD
|
|
10
|
+
} from "@getstrata/core/tenant/enableTenantRls";
|
|
11
|
+
function resolveDatabaseOptions(environment, defaults) {
|
|
12
|
+
const production = isProductionEnv(environment);
|
|
13
|
+
const url = environment.APP_DATABASE_URL?.trim() || environment.DATABASE_URL?.trim() || (!production || defaults.driver === "sqlite" ? defaults.defaultUrl : undefined);
|
|
14
|
+
if (!url)
|
|
15
|
+
throw new Error("DATABASE_URL or APP_DATABASE_URL is required in production/staging.");
|
|
16
|
+
if (defaults.driver === "sqlite") {
|
|
17
|
+
const value = url.trim();
|
|
18
|
+
const filename = value === ":memory:" || value === "sqlite::memory:" ? ":memory:" : value.startsWith("sqlite:") ? value.slice(7).replace(/^\/\//, "") || "./storage/app.sqlite" : value;
|
|
19
|
+
return Object.freeze({
|
|
20
|
+
driver: "sqlite",
|
|
21
|
+
filename: filename === ":memory:" ? filename : resolve(defaults.root ?? process.cwd(), filename)
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
return defaults.driver === "pgsql" ? Object.freeze({ driver: "pgsql", url, max: 5 }) : Object.freeze({ driver: "mysql", url });
|
|
25
|
+
}
|
|
26
|
+
function safeDatabaseName(url) {
|
|
27
|
+
let name;
|
|
28
|
+
try {
|
|
29
|
+
name = decodeURIComponent(new URL(url).pathname.replace(/^\//, ""));
|
|
30
|
+
} catch {
|
|
31
|
+
throw new Error("DATABASE_URL is not a valid database URL.");
|
|
32
|
+
}
|
|
33
|
+
if (!name)
|
|
34
|
+
throw new Error("DATABASE_URL is missing a database name.");
|
|
35
|
+
if (!/^[A-Za-z0-9_]+$/.test(name))
|
|
36
|
+
throw new Error(`Refusing to create a database with an unsafe name: ${name}`);
|
|
37
|
+
return name;
|
|
38
|
+
}
|
|
39
|
+
async function ensureDatabase(options) {
|
|
40
|
+
if (!(options.provision ?? !options.production))
|
|
41
|
+
return options.runtimeUrl;
|
|
42
|
+
if (options.driver === "pgsql") {
|
|
43
|
+
if (options.production) {
|
|
44
|
+
if (!options.migrationUrl?.trim())
|
|
45
|
+
throw new Error("MIGRATION_DATABASE_URL is required for production/staging provisioning.");
|
|
46
|
+
if (!options.password?.trim() || options.password === POSTGRES_APP_ROLE_PASSWORD)
|
|
47
|
+
throw new Error("Set an explicit non-development STRATA_APP_PASSWORD for production/staging provisioning.");
|
|
48
|
+
}
|
|
49
|
+
await ensurePostgresDatabaseAndAppRole({
|
|
50
|
+
runtimeUrl: options.runtimeUrl,
|
|
51
|
+
migrationUrl: options.migrationUrl,
|
|
52
|
+
password: options.password ?? POSTGRES_APP_ROLE_PASSWORD
|
|
53
|
+
});
|
|
54
|
+
} else {
|
|
55
|
+
const name = safeDatabaseName(options.runtimeUrl);
|
|
56
|
+
const admin = new URL(options.migrationUrl?.trim() || options.runtimeUrl);
|
|
57
|
+
admin.pathname = "/";
|
|
58
|
+
const { createConnection } = await import("mysql2/promise");
|
|
59
|
+
const connection = await createConnection(admin.toString());
|
|
60
|
+
try {
|
|
61
|
+
await connection.query(`CREATE DATABASE IF NOT EXISTS ${name}`);
|
|
62
|
+
} finally {
|
|
63
|
+
await connection.end();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return options.runtimeUrl;
|
|
67
|
+
}
|
|
68
|
+
async function withMigrationDatabase(options, operation, controls = {}) {
|
|
69
|
+
const environment = Object.freeze({ ...controls.environment ?? process.env });
|
|
70
|
+
let connection;
|
|
71
|
+
if (options.driver === "pgsql") {
|
|
72
|
+
const { openPostgresAdminConnection } = await import("@getstrata/core/tenant/enableTenantRls");
|
|
73
|
+
connection = await openPostgresAdminConnection({
|
|
74
|
+
runtimeUrl: options.url,
|
|
75
|
+
migrationUrl: environment.MIGRATION_DATABASE_URL
|
|
76
|
+
});
|
|
77
|
+
} else if (options.driver === "mysql") {
|
|
78
|
+
const { createMysqlConnection } = await import("@getstrata/core/database/mysqlConnection");
|
|
79
|
+
connection = createMysqlConnection(environment.MIGRATION_DATABASE_URL?.trim() || options.url);
|
|
80
|
+
} else {
|
|
81
|
+
if (options.filename !== ":memory:")
|
|
82
|
+
mkdirSync(dirname(options.filename), { recursive: true });
|
|
83
|
+
const { createSqliteConnection } = await import("@getstrata/core/database/sqliteConnection");
|
|
84
|
+
connection = createSqliteConnection(options.filename);
|
|
85
|
+
}
|
|
86
|
+
try {
|
|
87
|
+
const result = await runWithSqlDialect(options.driver, () => operation(connection));
|
|
88
|
+
if (options.driver === "pgsql" && controls.grantRuntimePrivileges !== false) {
|
|
89
|
+
const { grantPostgresAppRolePrivileges, postgresDatabaseNameFromUrl } = await import("@getstrata/core/tenant/enableTenantRls");
|
|
90
|
+
await grantPostgresAppRolePrivileges(connection, {
|
|
91
|
+
database: postgresDatabaseNameFromUrl(options.url)
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
95
|
+
} finally {
|
|
96
|
+
await connection.close?.();
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
export {
|
|
100
|
+
ensureDatabase,
|
|
101
|
+
resolveDatabaseOptions,
|
|
102
|
+
withMigrationDatabase
|
|
103
|
+
};
|
|
@@ -204,6 +204,7 @@ import {
|
|
|
204
204
|
CORE_EVENT_BUS_TOKEN,
|
|
205
205
|
CORE_HTTP_CLEANUP_TOKEN,
|
|
206
206
|
CORE_POLICY_GATE_TOKEN,
|
|
207
|
+
CORE_POLICY_REGISTRY_TOKEN,
|
|
207
208
|
CORE_PUBLIC_TENANCY_TOKEN,
|
|
208
209
|
CORE_QUEUE_TOKEN,
|
|
209
210
|
CORE_TENANT_RESOLVER_TOKEN,
|
|
@@ -398,7 +399,7 @@ var appEnvSchema = defineEnvSchema({
|
|
|
398
399
|
},
|
|
399
400
|
JWT_TTL_SECONDS: {
|
|
400
401
|
integer: true,
|
|
401
|
-
minimum:
|
|
402
|
+
minimum: 1,
|
|
402
403
|
default: "3600"
|
|
403
404
|
},
|
|
404
405
|
APP_ENV: {
|
|
@@ -579,7 +580,9 @@ import { PolicyGate } from "@getstrata/core/auth/policy";
|
|
|
579
580
|
var policyProvider = {
|
|
580
581
|
name: "core.policy",
|
|
581
582
|
register({ container }) {
|
|
582
|
-
|
|
583
|
+
const gate = new PolicyGate;
|
|
584
|
+
container.set(CORE_POLICY_GATE_TOKEN, gate);
|
|
585
|
+
container.set(CORE_POLICY_REGISTRY_TOKEN, gate);
|
|
583
586
|
}
|
|
584
587
|
};
|
|
585
588
|
var policy_default = policyProvider;
|
|
@@ -648,15 +651,7 @@ async function importInfrastructure(file) {
|
|
|
648
651
|
}
|
|
649
652
|
|
|
650
653
|
// ../../src/bootstrap/discoverJobs.ts
|
|
651
|
-
|
|
652
|
-
function state(registry) {
|
|
653
|
-
let value = states.get(registry);
|
|
654
|
-
if (!value) {
|
|
655
|
-
value = { loads: new Map, registered: new Map };
|
|
656
|
-
states.set(registry, value);
|
|
657
|
-
}
|
|
658
|
-
return value;
|
|
659
|
-
}
|
|
654
|
+
import { jobDiscoveryState as state } from "#strata-job-discovery";
|
|
660
655
|
function validateClass(value, location) {
|
|
661
656
|
if (typeof value !== "function" || typeof value.jobName !== "string" || !value.jobName.trim() || !(value.prototype instanceof Job) && typeof value.prototype?.handle !== "function")
|
|
662
657
|
throw new TypeError(`Invalid job at ${location}: expected a class with nonempty static jobName and handle().`);
|
|
@@ -738,10 +733,6 @@ function discoverJobs(registry, options = {}) {
|
|
|
738
733
|
cache.set(key, pending);
|
|
739
734
|
return pending;
|
|
740
735
|
}
|
|
741
|
-
function resetDiscoverJobsForTests(registry) {
|
|
742
|
-
state(registry).loads = new Map;
|
|
743
|
-
state(registry).registered = new Map;
|
|
744
|
-
}
|
|
745
736
|
|
|
746
737
|
// ../../src/bootstrap/queue/defaultJobs.ts
|
|
747
738
|
import { ExportAuditLogsJob } from "@getstrata/core/jobs/exportAuditLogsJob";
|
|
@@ -966,6 +957,7 @@ function createApplicationContext(providers, options) {
|
|
|
966
957
|
}
|
|
967
958
|
};
|
|
968
959
|
container.set(CORE_HTTP_CLEANUP_TOKEN2, (callback) => context.onCleanup(callback));
|
|
960
|
+
let selectedProviders = [];
|
|
969
961
|
application = createApplication({
|
|
970
962
|
...options.definition,
|
|
971
963
|
providers: [
|
|
@@ -980,9 +972,11 @@ function createApplicationContext(providers, options) {
|
|
|
980
972
|
flush: () => phase("flush"),
|
|
981
973
|
close: () => phase("close")
|
|
982
974
|
});
|
|
983
|
-
|
|
984
|
-
await runProviderPhase(
|
|
985
|
-
|
|
975
|
+
selectedProviders = [...configured, ...options.providersFromScope?.(scope) ?? []];
|
|
976
|
+
await runProviderPhase(selectedProviders, "register", context);
|
|
977
|
+
},
|
|
978
|
+
boot: async () => {
|
|
979
|
+
await runProviderPhase(selectedProviders, "boot", context);
|
|
986
980
|
assertAppDependenciesComplete(dependencies);
|
|
987
981
|
}
|
|
988
982
|
}
|
|
@@ -53,15 +53,7 @@ async function importInfrastructure(file) {
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
// ../../src/bootstrap/discoverJobs.ts
|
|
56
|
-
|
|
57
|
-
function state(registry) {
|
|
58
|
-
let value = states.get(registry);
|
|
59
|
-
if (!value) {
|
|
60
|
-
value = { loads: new Map, registered: new Map };
|
|
61
|
-
states.set(registry, value);
|
|
62
|
-
}
|
|
63
|
-
return value;
|
|
64
|
-
}
|
|
56
|
+
import { jobDiscoveryState as state } from "#strata-job-discovery";
|
|
65
57
|
function validateClass(value, location) {
|
|
66
58
|
if (typeof value !== "function" || typeof value.jobName !== "string" || !value.jobName.trim() || !(value.prototype instanceof Job) && typeof value.prototype?.handle !== "function")
|
|
67
59
|
throw new TypeError(`Invalid job at ${location}: expected a class with nonempty static jobName and handle().`);
|
|
@@ -143,11 +135,6 @@ function discoverJobs(registry, options = {}) {
|
|
|
143
135
|
cache.set(key, pending);
|
|
144
136
|
return pending;
|
|
145
137
|
}
|
|
146
|
-
function resetDiscoverJobsForTests(registry) {
|
|
147
|
-
state(registry).loads = new Map;
|
|
148
|
-
state(registry).registered = new Map;
|
|
149
|
-
}
|
|
150
138
|
export {
|
|
151
|
-
discoverJobs
|
|
152
|
-
resetDiscoverJobsForTests
|
|
139
|
+
discoverJobs
|
|
153
140
|
};
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
// ../../src/bootstrap/discoverListeners.ts
|
|
3
3
|
import { join as join2 } from "path";
|
|
4
|
+
import { APPLICATION_EVENT_BUS_TOKEN } from "@getstrata/bootstrap/providers/events";
|
|
5
|
+
import { resourceService, service } from "@getstrata/core/contracts/services";
|
|
6
|
+
import { createServiceToken } from "@getstrata/core/contracts/token";
|
|
7
|
+
import { repositoryEffects } from "@getstrata/core/database/definitions";
|
|
4
8
|
|
|
5
9
|
// ../../src/bootstrap/infrastructureDiscovery.ts
|
|
6
10
|
import { readdir } from "fs/promises";
|
|
@@ -86,6 +90,36 @@ async function discoverListeners(options = {}) {
|
|
|
86
90
|
}
|
|
87
91
|
return Object.freeze(result);
|
|
88
92
|
}
|
|
93
|
+
function defineModelListener(repository, action, handler) {
|
|
94
|
+
const token = createServiceToken(`${repository.name}.${action}.listener`);
|
|
95
|
+
return Object.freeze({
|
|
96
|
+
services: Object.freeze([
|
|
97
|
+
service(token, { lifetime: "application", dependencies: [], create: () => handler })
|
|
98
|
+
]),
|
|
99
|
+
repositoryEffects: Object.freeze([
|
|
100
|
+
repositoryEffects(repository, [{ action, afterCommit: token }])
|
|
101
|
+
])
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
function defineEventListener(event, handler) {
|
|
105
|
+
const token = createServiceToken(`${event}.listener`);
|
|
106
|
+
return Object.freeze({
|
|
107
|
+
repositoryEffects: Object.freeze([]),
|
|
108
|
+
services: Object.freeze([
|
|
109
|
+
resourceService(token, {
|
|
110
|
+
lifetime: "application",
|
|
111
|
+
dependencies: [APPLICATION_EVENT_BUS_TOKEN],
|
|
112
|
+
create: (bus) => ({
|
|
113
|
+
name: event,
|
|
114
|
+
open: () => ({ ok: true, value: bus.listen(event, handler) }),
|
|
115
|
+
close: (unsubscribe) => unsubscribe()
|
|
116
|
+
})
|
|
117
|
+
})
|
|
118
|
+
])
|
|
119
|
+
});
|
|
120
|
+
}
|
|
89
121
|
export {
|
|
122
|
+
defineEventListener,
|
|
123
|
+
defineModelListener,
|
|
90
124
|
discoverListeners
|
|
91
125
|
};
|