@getstrata/core 2.2.59 → 3.0.3
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 +27 -0
- package/dist/core/audit/exportAuditLogs.d.ts +12 -3
- package/dist/core/audit/exportStore.d.ts +6 -2
- package/dist/core/audit/siemFormatter.d.ts +2 -2
- package/dist/core/auth/abilityCatalog.d.ts +1 -9
- package/dist/core/auth/authContext.d.ts +2 -0
- package/dist/core/auth/authOutcome.d.ts +32 -0
- package/dist/core/auth/authReader.d.ts +22 -0
- package/dist/core/auth/basicAuthGuard.d.ts +19 -3
- package/dist/core/auth/emailVerification.d.ts +1 -1
- package/dist/core/auth/guard.d.ts +27 -6
- package/dist/core/auth/intendedUrlCookie.d.ts +13 -7
- package/dist/core/auth/jwt.d.ts +5 -4
- package/dist/core/auth/jwtGuard.d.ts +7 -6
- package/dist/core/auth/membershipContext.d.ts +10 -7
- package/dist/core/auth/membershipContextMiddleware.d.ts +3 -3
- package/dist/core/auth/membershipMiddleware.d.ts +1 -2
- package/dist/core/auth/membershipServiceImplementation.d.ts +1 -1
- package/dist/core/auth/oauth/oidcIdToken.d.ts +25 -16
- package/dist/core/auth/oauth/oidcProvider.d.ts +6 -4
- package/dist/core/auth/oauth/providers.d.ts +3 -1
- package/dist/core/auth/oneTimeToken.d.ts +2 -3
- package/dist/core/auth/passwordConfirmCookie.d.ts +15 -6
- package/dist/core/auth/passwordLogin.d.ts +5 -5
- package/dist/core/auth/pendingMfaCookie.d.ts +2 -2
- package/dist/core/auth/policy.d.ts +23 -10
- package/dist/core/auth/requireAuthCapability.d.ts +3 -0
- package/dist/core/auth/resolveMembershipService.d.ts +3 -2
- package/dist/core/auth/saml/samlAssertionReplay.d.ts +4 -4
- package/dist/core/auth/saml/samlServiceProvider.d.ts +11 -7
- package/dist/core/auth/scimAuthMiddleware.d.ts +10 -3
- package/dist/core/auth/sessionCookie.d.ts +20 -9
- package/dist/core/auth/sessionGuard.d.ts +14 -3
- package/dist/core/cache/createCacheStore.d.ts +6 -3
- package/dist/core/cache/redisCacheStore.d.ts +5 -1
- package/dist/core/cache/repository.d.ts +5 -1
- package/dist/core/cache/simpleCache.d.ts +2 -1
- package/dist/core/cache/tokens.d.ts +8 -0
- package/dist/core/config/applicationConfig.d.ts +21 -0
- package/dist/core/config/configSnapshot.d.ts +3 -0
- package/dist/core/config/envSchema.d.ts +3 -2
- package/dist/core/contracts/application.d.ts +153 -0
- package/dist/core/contracts/authUserDirectory.d.ts +24 -7
- package/dist/core/contracts/container.d.ts +20 -23
- package/dist/core/contracts/di.d.ts +4 -2
- package/dist/core/contracts/executionContext.d.ts +5 -0
- package/dist/core/contracts/executionOperations.d.ts +10 -0
- package/dist/core/contracts/executionOwnership.d.ts +38 -0
- package/dist/core/contracts/persistence.d.ts +20 -0
- package/dist/core/contracts/serviceTokens.d.ts +32 -18
- package/dist/core/contracts/services.d.ts +63 -0
- package/dist/core/contracts/token.d.ts +19 -0
- package/dist/core/crypto/fieldEncryption.d.ts +19 -7
- package/dist/core/crypto/fieldEncryptionRotation.d.ts +21 -5
- package/dist/core/crypto/mfaSecret.d.ts +10 -3
- package/dist/core/database/baseRepository.d.ts +14 -10
- package/dist/core/database/bindings.d.ts +6 -0
- package/dist/core/database/bunSql.d.ts +3 -12
- package/dist/core/database/definitions.d.ts +209 -0
- package/dist/core/database/dialect.d.ts +5 -24
- package/dist/core/database/driverPorts.d.ts +116 -0
- package/dist/core/database/errors.d.ts +47 -7
- package/dist/core/database/factory.d.ts +51 -31
- package/dist/core/database/index.d.ts +9 -10
- package/dist/core/database/keyset.d.ts +2 -1
- package/dist/core/database/legacyNativeTransaction.d.ts +5 -0
- package/dist/core/database/manager.d.ts +12 -0
- package/dist/core/database/model.d.ts +91 -97
- package/dist/core/database/modelQuery.d.ts +9 -5
- package/dist/core/database/modelRegistry.d.ts +52 -33
- package/dist/core/database/mysqlConnection.d.ts +1 -19
- package/dist/core/database/mysqlNative.d.ts +38 -0
- package/dist/core/database/nativeTransactions.d.ts +16 -0
- package/dist/core/database/ormInternal.d.ts +85 -0
- package/dist/core/database/ormRuntime.d.ts +4 -0
- package/dist/core/database/ownedReadOnlyObservation.d.ts +16 -0
- package/dist/core/database/postgresNative.d.ts +11 -0
- package/dist/core/database/query.d.ts +24 -23
- package/dist/core/database/queryPlanSnapshot.d.ts +7 -0
- package/dist/core/database/rawMysql.d.ts +7 -0
- package/dist/core/database/rawPostgres.d.ts +23 -0
- package/dist/core/database/rawSqlite.d.ts +6 -0
- package/dist/core/database/relationQueries.d.ts +3 -2
- package/dist/core/database/relationQuery.d.ts +24 -6
- package/dist/core/database/repositoryQuery.d.ts +6 -1
- package/dist/core/database/schema/driver.d.ts +1 -1
- package/dist/core/database/seeders/types.d.ts +1 -1
- package/dist/core/database/session.d.ts +13 -0
- package/dist/core/database/sessionFactory.d.ts +9 -0
- package/dist/core/database/sqlDialects.d.ts +22 -0
- package/dist/core/database/tokens.d.ts +7 -0
- package/dist/core/database/transactionSession.d.ts +43 -0
- package/dist/core/errors/http.d.ts +4 -1
- package/dist/core/events/{eventBus.d.ts → eventBusClass.d.ts} +1 -3
- package/dist/core/events/index.d.ts +2 -3
- package/dist/core/events/outbox/index.d.ts +44 -13
- package/dist/core/events/outbox/metrics.d.ts +4 -4
- package/dist/core/events/outbox/workerTransport.d.ts +17 -0
- package/dist/core/http/authMiddleware.d.ts +2 -2
- package/dist/core/http/authorizeMiddleware.d.ts +2 -2
- package/dist/core/http/bodySizeLimitMiddleware.d.ts +1 -1
- package/dist/core/http/conditionalResponse.d.ts +7 -2
- package/dist/core/http/contentSecurityPolicy.d.ts +4 -3
- package/dist/core/http/corsMiddleware.d.ts +2 -1
- package/dist/core/http/csrfMiddleware.d.ts +6 -1
- package/dist/core/http/csrfToken.d.ts +17 -7
- package/dist/core/http/databaseError.d.ts +4 -0
- package/dist/core/http/etag.d.ts +5 -2
- package/dist/core/http/flashMiddleware.d.ts +1 -1
- package/dist/core/http/flashSession.d.ts +13 -7
- package/dist/core/http/index.d.ts +1 -1
- package/dist/core/http/loginThrottleMiddleware.d.ts +3 -3
- package/dist/core/http/memoryThrottleMiddleware.d.ts +2 -2
- package/dist/core/http/parseMultipartUpload.d.ts +3 -2
- package/dist/core/http/requestMetaContext.d.ts +5 -0
- package/dist/core/http/requireAbilityMiddleware.d.ts +1 -1
- package/dist/core/http/requireAuthMiddleware.d.ts +2 -2
- package/dist/core/http/requirePasswordConfirmMiddleware.d.ts +2 -1
- package/dist/core/http/requireVerifiedMiddleware.d.ts +2 -2
- package/dist/core/http/requireWebAuthMiddleware.d.ts +2 -2
- package/dist/core/http/response.d.ts +4 -3
- package/dist/core/http/scimThrottleMiddleware.d.ts +4 -3
- package/dist/core/http/securedRouteModelBinding.d.ts +10 -3
- package/dist/core/http/securityHeadersMiddleware.d.ts +1 -2
- package/dist/core/http/signedUrl.d.ts +8 -6
- package/dist/core/http/throttleMiddleware.d.ts +9 -5
- package/dist/core/http/uploads.d.ts +9 -3
- package/dist/core/http/webErrorResponse.d.ts +8 -2
- package/dist/core/jobs/exportAuditLogsJob.d.ts +5 -2
- package/dist/core/jobs/invalidateCacheTagsJob.d.ts +5 -4
- package/dist/core/lifecycle/gracefulShutdown.d.ts +2 -5
- package/dist/core/logging/requestLoggingMiddleware.d.ts +1 -1
- package/dist/core/mail/mailer.d.ts +7 -5
- package/dist/core/openapi/generator.d.ts +12 -4
- package/dist/core/openapi/registeredRoute.d.ts +4 -4
- package/dist/core/queue/createAppQueue.d.ts +9 -4
- package/dist/core/queue/failedJobMetrics.d.ts +11 -2
- package/dist/core/queue/failedJobRepository.d.ts +6 -3
- package/dist/core/queue/failedJobService.d.ts +1 -0
- package/dist/core/queue/index.d.ts +16 -6
- package/dist/core/queue/jobRegistry.d.ts +3 -2
- package/dist/core/queue/jobRunner.d.ts +10 -4
- package/dist/core/queue/publicQueue.d.ts +18 -9
- package/dist/core/queue/queueConfig.d.ts +20 -10
- package/dist/core/queue/queueHandoff.d.ts +31 -0
- package/dist/core/queue/queueMetrics.d.ts +10 -3
- package/dist/core/queue/queueServices.d.ts +28 -0
- package/dist/core/queue/queueWorkerTransport.d.ts +28 -0
- package/dist/core/queue/redisQueue.d.ts +19 -10
- package/dist/core/queue/redisQueueKeys.d.ts +5 -5
- package/dist/core/queue/redisStreams.d.ts +17 -10
- package/dist/core/queue/resilientQueue.d.ts +10 -4
- package/dist/core/queue/types.d.ts +1 -0
- package/dist/core/runtime/appKeyPrefix.d.ts +5 -5
- package/dist/core/runtime/application.d.ts +7 -0
- package/dist/core/runtime/executionContext.d.ts +9 -0
- package/dist/core/runtime/executionTasks.d.ts +49 -0
- package/dist/core/runtime/resourceJournal.d.ts +67 -0
- package/dist/core/runtime/serviceGraph.d.ts +14 -0
- package/dist/core/security/oauthState.d.ts +13 -9
- package/dist/core/security/publicReads.d.ts +2 -2
- package/dist/core/security/safeFetch.d.ts +1 -0
- package/dist/core/security/safeUrl.d.ts +3 -0
- package/dist/core/security/scimTenantTokens.d.ts +1 -1
- package/dist/core/security/tokenExpiry.d.ts +1 -1
- package/dist/core/security/webhookSignature.d.ts +1 -1
- package/dist/core/storage/storage.d.ts +24 -16
- package/dist/core/tenant/databaseTenantContext.d.ts +5 -7
- package/dist/core/tenant/resolveTenant.d.ts +2 -2
- package/dist/core/tenant/scopedDatabasePolicy.d.ts +8 -0
- package/dist/core/tenant/tenantDatabaseScope.d.ts +3 -7
- package/dist/core/tenant/tenantMiddleware.d.ts +15 -4
- package/dist/core/tracing/otel.d.ts +4 -6
- package/dist/core/tracing/tracingMiddleware.d.ts +3 -3
- package/dist/core/view/etaViewEngine.d.ts +1 -1
- package/dist/core/view/index.d.ts +5 -4
- package/dist/core/view/tokens.d.ts +8 -0
- package/dist/core/view/viewRenderer.d.ts +2 -2
- package/dist/core/view/webErrorView.d.ts +8 -7
- package/dist/core/view/webLayoutData.d.ts +6 -4
- package/dist/entries/audit/exportAuditLogs.js +87 -1024
- package/dist/entries/audit/siemFormatter.js +4 -90
- package/dist/entries/auth/abilityCatalog.js +28 -0
- package/dist/entries/auth/accessControl.js +5 -16
- package/dist/entries/auth/accountStateMigration.js +216 -236
- package/dist/entries/auth/authOutcome.js +80 -0
- package/dist/entries/auth/authReader.js +68 -0
- package/dist/entries/auth/basicAuthGuard.js +144 -1249
- package/dist/entries/auth/emailVerification.js +2 -2
- package/dist/entries/auth/intendedUrlCookie.js +35 -59
- package/dist/entries/auth/jwt.js +22 -47
- package/dist/entries/auth/jwtGuard.js +99 -231
- package/dist/entries/auth/membershipMiddleware.js +71 -10
- package/dist/entries/auth/membershipScope.js +16 -22
- package/dist/entries/auth/membershipService.js +11 -28
- package/dist/entries/auth/oauth/oidcProvider.js +59 -64
- package/dist/entries/auth/oauth/providers.js +12 -95
- package/dist/entries/auth/oauth/samlProvider.js +53 -1080
- package/dist/entries/auth/oneTimeToken.js +27 -289
- package/dist/entries/auth/passwordConfirmCookie.js +40 -63
- package/dist/entries/auth/passwordLogin.js +94 -399
- package/dist/entries/auth/pendingMfaCookie.js +6 -6
- package/dist/entries/auth/policy.js +22 -56
- package/dist/entries/auth/saml/samlServiceProvider.js +55 -1084
- package/dist/entries/auth/scimAuthMiddleware.js +16 -3358
- package/dist/entries/auth/sessionCookie.js +58 -73
- package/dist/entries/auth/sessionGuard.js +95 -248
- package/dist/entries/auth/tokenHash.js +11 -43
- package/dist/entries/cache/createCacheStore.js +126 -117
- package/dist/entries/cache/repository.js +25 -0
- package/dist/entries/cache/simpleCache.js +12 -7
- package/dist/entries/cache/tokens.js +9 -0
- package/dist/entries/config/applicationConfig.js +163 -0
- package/dist/entries/config/envSchema.js +21 -4
- package/dist/entries/contracts/application.js +1 -0
- package/dist/entries/contracts/authUserDirectory.js +10 -0
- package/dist/entries/contracts/container.js +99 -26
- package/dist/entries/contracts/di.js +3 -0
- package/dist/entries/contracts/executionContext.js +7 -0
- package/dist/entries/contracts/executionOperations.js +7 -0
- package/dist/entries/contracts/executionOwnership.js +7 -0
- package/dist/entries/contracts/membership.js +1 -0
- package/dist/entries/contracts/persistence.js +1 -0
- package/dist/entries/contracts/serviceTokens.js +26 -116
- package/dist/entries/contracts/services.js +98 -0
- package/dist/entries/contracts/token.js +41 -0
- package/dist/entries/crypto/fieldEncryption.js +49 -50
- package/dist/entries/crypto/mfaSecret.js +86 -82
- package/dist/entries/database/acquisitionMetrics.js +107 -0
- package/dist/entries/database/baseRepository.d.ts +2 -0
- package/dist/entries/database/bindings.js +2082 -0
- package/dist/entries/database/definitions.js +175 -0
- package/dist/entries/database/errors.js +114 -89
- package/dist/entries/database/factory.js +67 -28
- package/dist/entries/database/migrations.js +172 -224
- package/dist/entries/database/model.d.ts +1 -0
- package/dist/entries/database/ownedReadOnlyObservation.js +1282 -0
- package/dist/entries/database/query.js +159 -161
- package/dist/entries/database/repositoryQuery.d.ts +1 -0
- package/dist/entries/database/repositoryQuery.js +1 -1373
- package/dist/entries/database/schema.js +216 -236
- package/dist/entries/database/session.js +1 -0
- package/dist/entries/database/sqliteConnection.js +525 -83
- package/dist/entries/database/tokens.js +9 -0
- package/dist/entries/errors/http.js +118 -1
- package/dist/entries/http/authMiddleware.js +8 -5
- package/dist/entries/http/authorizeMiddleware.js +1 -1
- package/dist/entries/http/bodySizeLimitMiddleware.js +2 -2
- package/dist/entries/http/clientIp.js +2 -2
- package/dist/entries/http/conditionalResponse.js +19 -7
- package/dist/entries/http/corsMiddleware.js +6 -101
- package/dist/entries/http/csrfMiddleware.js +92 -106
- package/dist/entries/http/csrfToken.js +58 -94
- package/dist/entries/http/etag.js +7 -4
- package/dist/entries/http/flashMiddleware.js +57 -99
- package/dist/entries/http/flashSession.js +41 -68
- package/dist/entries/http/memoryThrottleMiddleware.js +43 -186
- package/dist/entries/http/metricsMiddleware.js +2 -16
- package/dist/entries/http/parseMultipartUpload.js +15 -29
- package/dist/entries/http/requireAbilityMiddleware.js +2 -2
- package/dist/entries/http/requirePasswordConfirmMiddleware.js +19 -31
- package/dist/entries/http/requireVerifiedMiddleware.js +4 -7
- package/dist/entries/http/requireWebAuthMiddleware.js +2 -2
- package/dist/entries/http/response.js +67 -233
- package/dist/entries/http/routeMiddleware.js +0 -14
- package/dist/entries/http/scimThrottleMiddleware.js +43 -237
- package/dist/entries/http/securedRouteModelBinding.js +37 -25
- package/dist/entries/http/securityHeadersMiddleware.js +8 -25
- package/dist/entries/http/signedUrl.js +20 -110
- package/dist/entries/http/throttleMiddleware.js +41 -185
- package/dist/entries/http/uploads.js +9 -24
- package/dist/entries/jobs/exportAuditLogsJob.js +17 -1498
- package/dist/entries/jobs/invalidateCacheTagsJob.js +12 -47
- package/dist/entries/logging/requestLoggingMiddleware.js +4 -4
- package/dist/entries/mail/mailer.js +68 -78
- package/dist/entries/openapi/generator.js +33 -100
- package/dist/entries/queue/createAppQueue.js +416 -254
- package/dist/entries/queue/failedJobRepository.js +19 -8
- package/dist/entries/queue/failedJobService.js +1 -0
- package/dist/entries/queue/jobRunner.js +141 -66
- package/dist/entries/queue/publicQueue.js +411 -255
- package/dist/entries/queue/queueConfig.js +61 -0
- package/dist/entries/queue/queueHandoff.js +318 -0
- package/dist/entries/queue/queueMetrics.js +989 -1384
- package/dist/entries/queue/queueServices.js +133 -0
- package/dist/entries/queue/queueWorkerTransport.js +1703 -0
- package/dist/entries/queue/redisQueue.js +282 -180
- package/dist/entries/queue.js +6 -45
- package/dist/entries/runtime/application.js +1585 -0
- package/dist/entries/runtime/asyncContextStore.js +0 -16
- package/dist/entries/security/oauthState.js +35 -67
- package/dist/entries/security/publicReads.js +4 -4
- package/dist/entries/security/safeFetch.js +1 -0
- package/dist/entries/security/scimTenantTokens.js +3 -3
- package/dist/entries/security/tokenExpiry.js +2 -2
- package/dist/entries/security/totp.js +1 -87
- package/dist/entries/security/webhookSignature.js +2 -90
- package/dist/entries/storage/storage.js +120 -45
- package/dist/entries/tenant/databaseTenantContext.js +6 -907
- package/dist/entries/tenant/publicTenantHost.js +78 -0
- package/dist/entries/tenant/resolveTenant.js +14 -0
- package/dist/entries/tenant/scopedDatabasePolicy.js +75 -0
- package/dist/entries/tenant/tenancyConfig.js +1 -19
- package/dist/entries/tenant/tenantDatabaseScope.js +3 -892
- package/dist/entries/tracing/tracingMiddleware.js +104 -173
- package/dist/entries/view/tokens.js +11 -0
- package/dist/framework/public-api.d.ts +62 -44
- package/dist/index.js +10433 -11347
- package/dist/internal/ormInternal.js +117 -0
- package/dist/internal/ormRuntime.js +5000 -0
- package/package.json +158 -46
- package/dist/core/database/bindConnection.d.ts +0 -1
- package/dist/core/database/boundConnection.d.ts +0 -5
- package/dist/core/database/defaultConnection.d.ts +0 -7
- package/dist/core/database/namedConnections.d.ts +0 -15
- package/dist/core/database/readOnlyCollector.d.ts +0 -7
- package/dist/core/database/repositoryConnection.d.ts +0 -4
- package/dist/core/database/transaction.d.ts +0 -23
- package/dist/core/database/transactionControl.d.ts +0 -6
- package/dist/core/events/deferredModelEvents.d.ts +0 -7
- package/dist/core/facades/index.d.ts +0 -10
- package/dist/core/runtime/applicationRegistry.d.ts +0 -18
- package/dist/entries/database/bindConnection.js +0 -1
- package/dist/entries/database/boundConnection.js +0 -1
- package/dist/entries/database/defaultConnection.js +0 -1
- package/dist/entries/database/namedConnections.js +0 -1
- package/dist/entries/database/repositoryConnection.js +0 -1
- package/dist/entries/database/transaction.js +0 -1
- package/dist/entries/facades.js +0 -711
- package/dist/entries/runtime/applicationRegistry.js +0 -1
|
@@ -1,51 +1,124 @@
|
|
|
1
1
|
// @bun
|
|
2
2
|
// ../../src/core/contracts/container.ts
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
import { valueService } from "@getstrata/core/contracts/services";
|
|
4
|
+
import {
|
|
5
|
+
assertServiceToken,
|
|
6
|
+
createServiceToken,
|
|
7
|
+
serviceTokenKey,
|
|
8
|
+
serviceTokenName
|
|
9
|
+
} from "@getstrata/core/contracts/token";
|
|
10
|
+
function identity(key) {
|
|
11
|
+
assertServiceToken(key);
|
|
12
|
+
return serviceTokenKey(key);
|
|
13
|
+
}
|
|
14
|
+
function display(key) {
|
|
15
|
+
return serviceTokenName(key);
|
|
7
16
|
}
|
|
8
17
|
|
|
9
18
|
class ServiceContainer {
|
|
19
|
+
parent;
|
|
10
20
|
services = new Map;
|
|
11
21
|
singletonFactories = new Map;
|
|
12
22
|
bindings = new Map;
|
|
23
|
+
aliases = new Map;
|
|
24
|
+
initializations = new Map;
|
|
13
25
|
resolutionPath = [];
|
|
14
|
-
|
|
26
|
+
sealed = false;
|
|
27
|
+
constructor(parent) {
|
|
28
|
+
this.parent = parent;
|
|
29
|
+
if (parent && !parent.sealed)
|
|
30
|
+
throw new Error("Service container parent must be sealed.");
|
|
31
|
+
}
|
|
32
|
+
mutable() {
|
|
33
|
+
if (this.sealed)
|
|
34
|
+
throw new Error("Service composition is sealed.");
|
|
35
|
+
}
|
|
36
|
+
clearDefinition(key) {
|
|
37
|
+
this.services.delete(key);
|
|
15
38
|
this.singletonFactories.delete(key);
|
|
16
39
|
this.bindings.delete(key);
|
|
17
|
-
this.
|
|
40
|
+
this.aliases.delete(key);
|
|
41
|
+
this.initializations.delete(key);
|
|
42
|
+
}
|
|
43
|
+
set(key, value) {
|
|
44
|
+
this.mutable();
|
|
45
|
+
const id = identity(key);
|
|
46
|
+
this.clearDefinition(id);
|
|
47
|
+
this.services.set(id, value);
|
|
18
48
|
return value;
|
|
19
49
|
}
|
|
20
50
|
singleton(key, factory) {
|
|
21
|
-
this.
|
|
22
|
-
|
|
23
|
-
this.
|
|
51
|
+
this.mutable();
|
|
52
|
+
const id = identity(key);
|
|
53
|
+
this.clearDefinition(id);
|
|
54
|
+
this.singletonFactories.set(id, factory);
|
|
24
55
|
}
|
|
25
56
|
bind(key, factory) {
|
|
26
|
-
this.
|
|
27
|
-
|
|
28
|
-
this.
|
|
57
|
+
this.mutable();
|
|
58
|
+
const id = identity(key);
|
|
59
|
+
this.clearDefinition(id);
|
|
60
|
+
this.bindings.set(id, factory);
|
|
61
|
+
}
|
|
62
|
+
initialize(key, factory, signal, checkpoint) {
|
|
63
|
+
const id = identity(key);
|
|
64
|
+
const existing = this.initializations.get(id);
|
|
65
|
+
if (existing)
|
|
66
|
+
return existing;
|
|
67
|
+
this.mutable();
|
|
68
|
+
const initializing = Promise.resolve().then(() => {
|
|
69
|
+
checkpoint?.();
|
|
70
|
+
if (signal.aborted)
|
|
71
|
+
throw signal.reason;
|
|
72
|
+
return factory();
|
|
73
|
+
}).then((value) => {
|
|
74
|
+
checkpoint?.();
|
|
75
|
+
if (signal.aborted)
|
|
76
|
+
throw signal.reason;
|
|
77
|
+
valueService(key, value);
|
|
78
|
+
checkpoint?.();
|
|
79
|
+
if (signal.aborted)
|
|
80
|
+
throw signal.reason;
|
|
81
|
+
this.services.set(id, value);
|
|
82
|
+
return value;
|
|
83
|
+
});
|
|
84
|
+
this.initializations.set(id, initializing);
|
|
85
|
+
initializing.catch(() => {});
|
|
86
|
+
return initializing;
|
|
87
|
+
}
|
|
88
|
+
alias(key, target) {
|
|
89
|
+
this.mutable();
|
|
90
|
+
const id = identity(key);
|
|
91
|
+
identity(target);
|
|
92
|
+
this.clearDefinition(id);
|
|
93
|
+
this.aliases.set(id, target);
|
|
94
|
+
}
|
|
95
|
+
seal() {
|
|
96
|
+
this.sealed = true;
|
|
29
97
|
}
|
|
30
98
|
get(key) {
|
|
31
|
-
|
|
32
|
-
|
|
99
|
+
const id = identity(key);
|
|
100
|
+
if (this.services.has(id))
|
|
101
|
+
return this.services.get(id);
|
|
102
|
+
const target = this.aliases.get(id);
|
|
103
|
+
if (target !== undefined) {
|
|
104
|
+
return this.resolveFactory(key, () => this.get(target));
|
|
33
105
|
}
|
|
34
|
-
const singletonFactory = this.singletonFactories.get(
|
|
106
|
+
const singletonFactory = this.singletonFactories.get(id);
|
|
35
107
|
if (singletonFactory) {
|
|
36
108
|
const value = this.resolveFactory(key, singletonFactory);
|
|
37
|
-
this.services.set(
|
|
109
|
+
this.services.set(id, value);
|
|
38
110
|
return value;
|
|
39
111
|
}
|
|
40
|
-
const binding = this.bindings.get(
|
|
41
|
-
if (binding)
|
|
112
|
+
const binding = this.bindings.get(id);
|
|
113
|
+
if (binding)
|
|
42
114
|
return this.resolveFactory(key, binding);
|
|
43
|
-
|
|
44
|
-
|
|
115
|
+
if (this.parent)
|
|
116
|
+
return this.parent.get(key);
|
|
117
|
+
throw new Error(`Service "${display(key)}" is not registered.`);
|
|
45
118
|
}
|
|
46
119
|
resolveFactory(key, factory) {
|
|
47
|
-
if (this.resolutionPath.
|
|
48
|
-
throw new Error(`Circular service dependency: ${[...this.resolutionPath, key].join(" -> ")}.`);
|
|
120
|
+
if (this.resolutionPath.some((entry) => identity(entry) === identity(key))) {
|
|
121
|
+
throw new Error(`Circular service dependency: ${[...this.resolutionPath, key].map(display).join(" -> ")}.`);
|
|
49
122
|
}
|
|
50
123
|
this.resolutionPath.push(key);
|
|
51
124
|
try {
|
|
@@ -64,7 +137,8 @@ class ServiceContainer {
|
|
|
64
137
|
return this.set(key, value);
|
|
65
138
|
}
|
|
66
139
|
has(key) {
|
|
67
|
-
|
|
140
|
+
const id = identity(key);
|
|
141
|
+
return this.services.has(id) || this.singletonFactories.has(id) || this.bindings.has(id) || this.aliases.has(id) || (this.parent?.has(key) ?? false);
|
|
68
142
|
}
|
|
69
143
|
}
|
|
70
144
|
|
|
@@ -77,10 +151,9 @@ class ConfigStore {
|
|
|
77
151
|
get(key) {
|
|
78
152
|
return this.values.get(key);
|
|
79
153
|
}
|
|
80
|
-
|
|
81
|
-
if (!this.values.has(key))
|
|
154
|
+
getRequired(key) {
|
|
155
|
+
if (!this.values.has(key))
|
|
82
156
|
throw new Error(`Config key "${key}" is not defined.`);
|
|
83
|
-
}
|
|
84
157
|
return this.values.get(key);
|
|
85
158
|
}
|
|
86
159
|
has(key) {
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
// ../../src/core/contracts/di.ts
|
|
3
3
|
var requiredDependencyKeys = [
|
|
4
4
|
"container",
|
|
5
|
+
"invoke",
|
|
5
6
|
"cache",
|
|
6
7
|
"storage"
|
|
7
8
|
];
|
|
@@ -13,6 +14,8 @@ function getRequiredDependency(dependencies, key) {
|
|
|
13
14
|
return dependency;
|
|
14
15
|
}
|
|
15
16
|
function assertAppDependenciesComplete(dependencies) {
|
|
17
|
+
if (dependencies.invoke !== undefined && typeof dependencies.invoke !== "function")
|
|
18
|
+
throw new TypeError("Application persistence invocation must be a function.");
|
|
16
19
|
for (const key of requiredDependencyKeys) {
|
|
17
20
|
getRequiredDependency(dependencies, key);
|
|
18
21
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/contracts/executionOperations.ts
|
|
3
|
+
import { createServiceToken } from "@getstrata/core/contracts/token";
|
|
4
|
+
var CORE_EXECUTION_OPERATIONS_TOKEN = createServiceToken("core.execution.operations");
|
|
5
|
+
export {
|
|
6
|
+
CORE_EXECUTION_OPERATIONS_TOKEN
|
|
7
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// @bun
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// @bun
|
|
@@ -1,107 +1,23 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
// ../../src/core/contracts/container.ts
|
|
3
|
-
function createServiceToken(key) {
|
|
4
|
-
if (!key.trim())
|
|
5
|
-
throw new Error("Service token key must not be empty.");
|
|
6
|
-
return key;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
class ServiceContainer {
|
|
10
|
-
services = new Map;
|
|
11
|
-
singletonFactories = new Map;
|
|
12
|
-
bindings = new Map;
|
|
13
|
-
resolutionPath = [];
|
|
14
|
-
set(key, value) {
|
|
15
|
-
this.singletonFactories.delete(key);
|
|
16
|
-
this.bindings.delete(key);
|
|
17
|
-
this.services.set(key, value);
|
|
18
|
-
return value;
|
|
19
|
-
}
|
|
20
|
-
singleton(key, factory) {
|
|
21
|
-
this.bindings.delete(key);
|
|
22
|
-
this.services.delete(key);
|
|
23
|
-
this.singletonFactories.set(key, factory);
|
|
24
|
-
}
|
|
25
|
-
bind(key, factory) {
|
|
26
|
-
this.singletonFactories.delete(key);
|
|
27
|
-
this.services.delete(key);
|
|
28
|
-
this.bindings.set(key, factory);
|
|
29
|
-
}
|
|
30
|
-
get(key) {
|
|
31
|
-
if (this.services.has(key)) {
|
|
32
|
-
return this.services.get(key);
|
|
33
|
-
}
|
|
34
|
-
const singletonFactory = this.singletonFactories.get(key);
|
|
35
|
-
if (singletonFactory) {
|
|
36
|
-
const value = this.resolveFactory(key, singletonFactory);
|
|
37
|
-
this.services.set(key, value);
|
|
38
|
-
return value;
|
|
39
|
-
}
|
|
40
|
-
const binding = this.bindings.get(key);
|
|
41
|
-
if (binding) {
|
|
42
|
-
return this.resolveFactory(key, binding);
|
|
43
|
-
}
|
|
44
|
-
throw new Error(`Service "${key}" is not registered.`);
|
|
45
|
-
}
|
|
46
|
-
resolveFactory(key, factory) {
|
|
47
|
-
if (this.resolutionPath.includes(key)) {
|
|
48
|
-
throw new Error(`Circular service dependency: ${[...this.resolutionPath, key].join(" -> ")}.`);
|
|
49
|
-
}
|
|
50
|
-
this.resolutionPath.push(key);
|
|
51
|
-
try {
|
|
52
|
-
return factory(this);
|
|
53
|
-
} finally {
|
|
54
|
-
this.resolutionPath.pop();
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
resolve(key) {
|
|
58
|
-
return this.get(key);
|
|
59
|
-
}
|
|
60
|
-
make(key) {
|
|
61
|
-
return this.resolve(key);
|
|
62
|
-
}
|
|
63
|
-
instance(key, value) {
|
|
64
|
-
return this.set(key, value);
|
|
65
|
-
}
|
|
66
|
-
has(key) {
|
|
67
|
-
return this.services.has(key) || this.singletonFactories.has(key) || this.bindings.has(key);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
class ConfigStore {
|
|
72
|
-
values = new Map;
|
|
73
|
-
set(key, value) {
|
|
74
|
-
this.values.set(key, value);
|
|
75
|
-
return value;
|
|
76
|
-
}
|
|
77
|
-
get(key) {
|
|
78
|
-
return this.values.get(key);
|
|
79
|
-
}
|
|
80
|
-
require(key) {
|
|
81
|
-
if (!this.values.has(key)) {
|
|
82
|
-
throw new Error(`Config key "${key}" is not defined.`);
|
|
83
|
-
}
|
|
84
|
-
return this.values.get(key);
|
|
85
|
-
}
|
|
86
|
-
has(key) {
|
|
87
|
-
return this.values.has(key);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
2
|
// ../../src/core/contracts/serviceTokens.ts
|
|
3
|
+
import { createServiceToken } from "@getstrata/core/contracts/token";
|
|
4
|
+
import { CORE_CACHE_TOKEN } from "@getstrata/core/cache/tokens";
|
|
92
5
|
var CORE_THROTTLE_QUOTA_POLICY_TOKEN = createServiceToken("core.throttleQuotaPolicy");
|
|
93
6
|
var CORE_PUBLIC_TENANCY_TOKEN = createServiceToken("core.publicTenancy");
|
|
94
7
|
var CORE_TENANT_RESOLVER_TOKEN = createServiceToken("core.tenantResolver");
|
|
95
8
|
var CORE_HTTP_CLEANUP_TOKEN = createServiceToken("core.httpCleanup");
|
|
96
|
-
var
|
|
97
|
-
var
|
|
98
|
-
var
|
|
99
|
-
var
|
|
100
|
-
var
|
|
101
|
-
var
|
|
102
|
-
var
|
|
103
|
-
var
|
|
104
|
-
var
|
|
9
|
+
var CORE_TRACING_RUNTIME_TOKEN = createServiceToken("core.tracingRuntime");
|
|
10
|
+
var CORE_MAIL_TOKEN = createServiceToken("core.mail");
|
|
11
|
+
var CORE_STORAGE_TOKEN = createServiceToken("core.storage");
|
|
12
|
+
var CORE_JSON_RESPONSE_POLICY_TOKEN = createServiceToken("core.jsonResponsePolicy");
|
|
13
|
+
var CORE_CONFIG_TOKEN = createServiceToken("core.config");
|
|
14
|
+
var FAILED_JOB_SERVICE_TOKEN = createServiceToken("core.failedJobs");
|
|
15
|
+
var CORE_QUEUE_TOKEN = createServiceToken("core.queue");
|
|
16
|
+
var CORE_EVENT_BUS_TOKEN = createServiceToken("core.eventBus");
|
|
17
|
+
var CORE_POLICY_GATE_TOKEN = createServiceToken("core.policyGate");
|
|
18
|
+
var CORE_AUTH_TOKEN = createServiceToken("core.auth");
|
|
19
|
+
var CORE_ABILITY_CHECKER_TOKEN = createServiceToken("core.abilityChecker");
|
|
20
|
+
var CORE_AUTH_USER_DIRECTORY_TOKEN = createServiceToken("core.authUserDirectory");
|
|
105
21
|
function isAbilityChecker(value) {
|
|
106
22
|
if (!value || typeof value !== "object") {
|
|
107
23
|
return false;
|
|
@@ -116,25 +32,15 @@ function isAuthUserDirectory(value) {
|
|
|
116
32
|
return typeof value.findByIdOrThrow === "function";
|
|
117
33
|
}
|
|
118
34
|
function resolveAbilityChecker(container) {
|
|
119
|
-
|
|
120
|
-
return container.resolve(CORE_ABILITY_CHECKER_TOKEN);
|
|
121
|
-
}
|
|
122
|
-
return container.resolve(CORE_TOKEN_SERVICE_TOKEN);
|
|
35
|
+
return container.resolve(CORE_ABILITY_CHECKER_TOKEN);
|
|
123
36
|
}
|
|
124
37
|
function resolveAuthUserDirectory(container) {
|
|
125
|
-
if (container.has(CORE_AUTH_USER_DIRECTORY_TOKEN))
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
if (container.has(CORE_TOKEN_SERVICE_TOKEN)) {
|
|
132
|
-
const tokenService = container.resolve(CORE_TOKEN_SERVICE_TOKEN);
|
|
133
|
-
if (isAuthUserDirectory(tokenService)) {
|
|
134
|
-
return tokenService;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
return null;
|
|
38
|
+
if (!container.has(CORE_AUTH_USER_DIRECTORY_TOKEN))
|
|
39
|
+
return null;
|
|
40
|
+
const directory = container.resolve(CORE_AUTH_USER_DIRECTORY_TOKEN);
|
|
41
|
+
if (!isAuthUserDirectory(directory))
|
|
42
|
+
throw new TypeError("Invalid auth account lookup capability.");
|
|
43
|
+
return directory;
|
|
138
44
|
}
|
|
139
45
|
export {
|
|
140
46
|
CORE_ABILITY_CHECKER_TOKEN,
|
|
@@ -144,12 +50,16 @@ export {
|
|
|
144
50
|
CORE_CONFIG_TOKEN,
|
|
145
51
|
CORE_EVENT_BUS_TOKEN,
|
|
146
52
|
CORE_HTTP_CLEANUP_TOKEN,
|
|
53
|
+
CORE_JSON_RESPONSE_POLICY_TOKEN,
|
|
54
|
+
CORE_MAIL_TOKEN,
|
|
147
55
|
CORE_POLICY_GATE_TOKEN,
|
|
148
56
|
CORE_PUBLIC_TENANCY_TOKEN,
|
|
149
57
|
CORE_QUEUE_TOKEN,
|
|
58
|
+
CORE_STORAGE_TOKEN,
|
|
150
59
|
CORE_TENANT_RESOLVER_TOKEN,
|
|
151
60
|
CORE_THROTTLE_QUOTA_POLICY_TOKEN,
|
|
152
|
-
|
|
61
|
+
CORE_TRACING_RUNTIME_TOKEN,
|
|
62
|
+
FAILED_JOB_SERVICE_TOKEN,
|
|
153
63
|
isAbilityChecker,
|
|
154
64
|
isAuthUserDirectory,
|
|
155
65
|
resolveAbilityChecker,
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/contracts/services.ts
|
|
3
|
+
import {
|
|
4
|
+
assertServiceToken
|
|
5
|
+
} from "@getstrata/core/contracts/token";
|
|
6
|
+
var serviceDeclarationType = Symbol("ServiceDeclaration");
|
|
7
|
+
function isServiceDeclaration(value) {
|
|
8
|
+
if (value === null || typeof value !== "object")
|
|
9
|
+
return false;
|
|
10
|
+
const brand = Object.getOwnPropertyDescriptor(value, serviceDeclarationType);
|
|
11
|
+
return Object.isFrozen(value) && brand !== undefined && "value" in brand && brand.value === value;
|
|
12
|
+
}
|
|
13
|
+
function declaration(value) {
|
|
14
|
+
Object.defineProperty(value, serviceDeclarationType, { value });
|
|
15
|
+
return Object.freeze(value);
|
|
16
|
+
}
|
|
17
|
+
function assertServicePayload(value) {
|
|
18
|
+
if (value === null || typeof value !== "object" && typeof value !== "function")
|
|
19
|
+
return;
|
|
20
|
+
let current = value;
|
|
21
|
+
const visited = new Set;
|
|
22
|
+
while (current !== null) {
|
|
23
|
+
if (visited.has(current))
|
|
24
|
+
throw new TypeError("Unsupported service payload prototype shape.");
|
|
25
|
+
visited.add(current);
|
|
26
|
+
const then = Object.getOwnPropertyDescriptor(current, "then");
|
|
27
|
+
if (then) {
|
|
28
|
+
if (!("value" in then) || typeof then.value === "function") {
|
|
29
|
+
throw new TypeError("PromiseLike service payloads are unsupported.");
|
|
30
|
+
}
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
current = Object.getPrototypeOf(current);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function service(token, options) {
|
|
37
|
+
assertServiceToken(token);
|
|
38
|
+
if (options.lifetime !== "application" && options.lifetime !== "execution") {
|
|
39
|
+
throw new TypeError("Unsupported service lifetime declaration.");
|
|
40
|
+
}
|
|
41
|
+
if (!Array.isArray(options.dependencies) || typeof options.create !== "function") {
|
|
42
|
+
throw new TypeError("Services must declare a dependency tuple and factory.");
|
|
43
|
+
}
|
|
44
|
+
for (const dependency of options.dependencies)
|
|
45
|
+
assertServiceToken(dependency);
|
|
46
|
+
return declaration({
|
|
47
|
+
kind: "factory",
|
|
48
|
+
token,
|
|
49
|
+
lifetime: options.lifetime,
|
|
50
|
+
dependencies: Object.freeze([...options.dependencies]),
|
|
51
|
+
create: options.create
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
function valueService(token, value) {
|
|
55
|
+
assertServiceToken(token);
|
|
56
|
+
assertServicePayload(value);
|
|
57
|
+
return declaration({
|
|
58
|
+
kind: "value",
|
|
59
|
+
token,
|
|
60
|
+
lifetime: "application",
|
|
61
|
+
value
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
function resourceService(token, options) {
|
|
65
|
+
assertServiceToken(token);
|
|
66
|
+
if (options.lifetime !== "application" && options.lifetime !== "execution") {
|
|
67
|
+
throw new TypeError("Unsupported resource service lifetime declaration.");
|
|
68
|
+
}
|
|
69
|
+
if (!Array.isArray(options.dependencies) || typeof options.create !== "function") {
|
|
70
|
+
throw new TypeError("Resource services must declare a dependency tuple and factory.");
|
|
71
|
+
}
|
|
72
|
+
for (const dependency of options.dependencies)
|
|
73
|
+
assertServiceToken(dependency);
|
|
74
|
+
return declaration({
|
|
75
|
+
kind: "resource",
|
|
76
|
+
token,
|
|
77
|
+
lifetime: options.lifetime,
|
|
78
|
+
dependencies: Object.freeze([...options.dependencies]),
|
|
79
|
+
create: options.create
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
function aliasService(aliasToken, targetToken) {
|
|
83
|
+
assertServiceToken(aliasToken);
|
|
84
|
+
assertServiceToken(targetToken);
|
|
85
|
+
return declaration({
|
|
86
|
+
kind: "alias",
|
|
87
|
+
token: aliasToken,
|
|
88
|
+
target: targetToken
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
export {
|
|
92
|
+
aliasService,
|
|
93
|
+
assertServicePayload,
|
|
94
|
+
isServiceDeclaration,
|
|
95
|
+
resourceService,
|
|
96
|
+
service,
|
|
97
|
+
valueService
|
|
98
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// ../../src/core/contracts/token.ts
|
|
3
|
+
var serviceTokenIdentity = Symbol("ServiceToken.identity");
|
|
4
|
+
function createServiceToken(name) {
|
|
5
|
+
if (typeof name !== "string" || !name.trim()) {
|
|
6
|
+
throw new TypeError("Service token name must be a nonempty string.");
|
|
7
|
+
}
|
|
8
|
+
return Object.freeze({ name, [serviceTokenIdentity]: Symbol(name) });
|
|
9
|
+
}
|
|
10
|
+
function isServiceToken(value) {
|
|
11
|
+
if (value === null || typeof value !== "object")
|
|
12
|
+
return false;
|
|
13
|
+
try {
|
|
14
|
+
if (Object.getPrototypeOf(value) !== Object.prototype || !Object.isFrozen(value))
|
|
15
|
+
return false;
|
|
16
|
+
const name = Object.getOwnPropertyDescriptor(value, "name");
|
|
17
|
+
const identity = Object.getOwnPropertyDescriptor(value, serviceTokenIdentity);
|
|
18
|
+
return name !== undefined && "value" in name && typeof name.value === "string" && name.value.trim().length > 0 && identity !== undefined && "value" in identity && typeof identity.value === "symbol";
|
|
19
|
+
} catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function assertServiceToken(value) {
|
|
24
|
+
if (!isServiceToken(value))
|
|
25
|
+
throw new TypeError("Expected an opaque service token.");
|
|
26
|
+
}
|
|
27
|
+
function serviceTokenKey(token) {
|
|
28
|
+
assertServiceToken(token);
|
|
29
|
+
return token[serviceTokenIdentity];
|
|
30
|
+
}
|
|
31
|
+
function serviceTokenName(token) {
|
|
32
|
+
assertServiceToken(token);
|
|
33
|
+
return token.name;
|
|
34
|
+
}
|
|
35
|
+
export {
|
|
36
|
+
assertServiceToken,
|
|
37
|
+
createServiceToken,
|
|
38
|
+
isServiceToken,
|
|
39
|
+
serviceTokenKey,
|
|
40
|
+
serviceTokenName
|
|
41
|
+
};
|
|
@@ -4,8 +4,8 @@ import { createCipheriv, createDecipheriv, createHmac, randomBytes } from "crypt
|
|
|
4
4
|
var ENCRYPTION_PREFIX = "enc:v1:";
|
|
5
5
|
var IV_LENGTH = 12;
|
|
6
6
|
var TAG_LENGTH = 16;
|
|
7
|
-
function resolveEncryptionKey() {
|
|
8
|
-
const raw =
|
|
7
|
+
function resolveEncryptionKey(env) {
|
|
8
|
+
const raw = env.KMS_ENCRYPTION_KEY?.trim();
|
|
9
9
|
if (!raw) {
|
|
10
10
|
return null;
|
|
11
11
|
}
|
|
@@ -18,15 +18,45 @@ function resolveEncryptionKey() {
|
|
|
18
18
|
}
|
|
19
19
|
throw new Error("KMS_ENCRYPTION_KEY must be 32 bytes (hex or base64).");
|
|
20
20
|
}
|
|
21
|
-
function
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
21
|
+
function createFieldEncryptionCodec(env) {
|
|
22
|
+
const configured = resolveFieldEncryptionKeyring(env);
|
|
23
|
+
const key = resolveEncryptionKey(env);
|
|
24
|
+
const enabled = env.FEATURE_FIELD_ENCRYPTION === "false" ? false : env.FEATURE_FIELD_ENCRYPTION === "true" ? true : Boolean(configured || key);
|
|
25
|
+
return Object.freeze({
|
|
26
|
+
isFieldEncryptionEnabled: () => enabled,
|
|
27
|
+
protectEmail(email) {
|
|
28
|
+
const normalized = normalizeEmail(email);
|
|
29
|
+
if (configured && enabled)
|
|
30
|
+
return {
|
|
31
|
+
storedEmail: configured.encrypt(normalized, "email"),
|
|
32
|
+
emailLookup: configured.keyring.lookup(normalized)
|
|
33
|
+
};
|
|
34
|
+
if (!key || !enabled)
|
|
35
|
+
return { storedEmail: normalized, emailLookup: normalized };
|
|
36
|
+
return {
|
|
37
|
+
storedEmail: encryptField(normalized, key),
|
|
38
|
+
emailLookup: hashLookupValue(normalized, key)
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
revealEmail(storedEmail) {
|
|
42
|
+
if (configured && storedEmail.startsWith("enc:"))
|
|
43
|
+
return configured.keyring.decrypt(storedEmail, "email");
|
|
44
|
+
if (storedEmail.startsWith("enc:") && !storedEmail.startsWith(ENCRYPTION_PREFIX))
|
|
45
|
+
throw new Error("Identified encrypted fields require KMS_ENCRYPTION_KEYRING.");
|
|
46
|
+
if (!key || !storedEmail.startsWith(ENCRYPTION_PREFIX))
|
|
47
|
+
return storedEmail;
|
|
48
|
+
return decryptField(storedEmail, key);
|
|
49
|
+
},
|
|
50
|
+
emailLookupForQuery(email) {
|
|
51
|
+
const normalized = normalizeEmail(email);
|
|
52
|
+
if (configured && enabled)
|
|
53
|
+
return configured.keyring.lookup(normalized);
|
|
54
|
+
return !key || !enabled ? normalized : hashLookupValue(normalized, key);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
function isFieldEncryptionEnabled(codec) {
|
|
59
|
+
return codec.isFieldEncryptionEnabled();
|
|
30
60
|
}
|
|
31
61
|
function encryptField(plaintext, key) {
|
|
32
62
|
const iv = randomBytes(IV_LENGTH);
|
|
@@ -54,46 +84,14 @@ function hashLookupValue(normalizedValue, key) {
|
|
|
54
84
|
function normalizeEmail(email) {
|
|
55
85
|
return email.trim().toLowerCase();
|
|
56
86
|
}
|
|
57
|
-
function protectEmail(email) {
|
|
58
|
-
|
|
59
|
-
const configured = resolveFieldEncryptionKeyring();
|
|
60
|
-
if (configured && isFieldEncryptionEnabled()) {
|
|
61
|
-
return {
|
|
62
|
-
storedEmail: configured.encrypt(normalized, "email"),
|
|
63
|
-
emailLookup: configured.keyring.lookup(normalized)
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
const key = resolveEncryptionKey();
|
|
67
|
-
if (!key || !isFieldEncryptionEnabled()) {
|
|
68
|
-
return { storedEmail: normalized, emailLookup: normalized };
|
|
69
|
-
}
|
|
70
|
-
return {
|
|
71
|
-
storedEmail: encryptField(normalized, key),
|
|
72
|
-
emailLookup: hashLookupValue(normalized, key)
|
|
73
|
-
};
|
|
87
|
+
function protectEmail(email, codec) {
|
|
88
|
+
return codec.protectEmail(email);
|
|
74
89
|
}
|
|
75
|
-
function revealEmail(storedEmail) {
|
|
76
|
-
|
|
77
|
-
if (configured && storedEmail.startsWith("enc:"))
|
|
78
|
-
return configured.keyring.decrypt(storedEmail, "email");
|
|
79
|
-
if (storedEmail.startsWith("enc:") && !storedEmail.startsWith(ENCRYPTION_PREFIX))
|
|
80
|
-
throw new Error("Identified encrypted fields require KMS_ENCRYPTION_KEYRING.");
|
|
81
|
-
const key = resolveEncryptionKey();
|
|
82
|
-
if (!key || !storedEmail.startsWith(ENCRYPTION_PREFIX)) {
|
|
83
|
-
return storedEmail;
|
|
84
|
-
}
|
|
85
|
-
return decryptField(storedEmail, key);
|
|
90
|
+
function revealEmail(storedEmail, codec) {
|
|
91
|
+
return codec.revealEmail(storedEmail);
|
|
86
92
|
}
|
|
87
|
-
function emailLookupForQuery(email) {
|
|
88
|
-
|
|
89
|
-
const configured = resolveFieldEncryptionKeyring();
|
|
90
|
-
if (configured && isFieldEncryptionEnabled())
|
|
91
|
-
return configured.keyring.lookup(normalized);
|
|
92
|
-
const key = resolveEncryptionKey();
|
|
93
|
-
if (!key || !isFieldEncryptionEnabled()) {
|
|
94
|
-
return normalized;
|
|
95
|
-
}
|
|
96
|
-
return hashLookupValue(normalized, key);
|
|
93
|
+
function emailLookupForQuery(email, codec) {
|
|
94
|
+
return codec.emailLookupForQuery(email);
|
|
97
95
|
}
|
|
98
96
|
function createFieldEncryptionKeyring(options) {
|
|
99
97
|
const identifier = /^[A-Za-z0-9_-]{1,64}$/;
|
|
@@ -174,7 +172,7 @@ function createFieldEncryptionKeyring(options) {
|
|
|
174
172
|
}
|
|
175
173
|
});
|
|
176
174
|
}
|
|
177
|
-
function resolveFieldEncryptionKeyring(env
|
|
175
|
+
function resolveFieldEncryptionKeyring(env) {
|
|
178
176
|
const raw = env.KMS_ENCRYPTION_KEYRING;
|
|
179
177
|
if (raw === undefined)
|
|
180
178
|
return null;
|
|
@@ -223,6 +221,7 @@ function resolveFieldEncryptionKeyring(env = process.env) {
|
|
|
223
221
|
}
|
|
224
222
|
}
|
|
225
223
|
export {
|
|
224
|
+
createFieldEncryptionCodec,
|
|
226
225
|
createFieldEncryptionKeyring,
|
|
227
226
|
decryptField,
|
|
228
227
|
emailLookupForQuery,
|