@friggframework/devtools 2.0.0-next.9 → 2.0.0-next.90

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.
Files changed (240) hide show
  1. package/frigg-cli/README.md +1289 -0
  2. package/frigg-cli/__tests__/unit/commands/build.test.js +279 -0
  3. package/frigg-cli/__tests__/unit/commands/db-setup.test.js +649 -0
  4. package/frigg-cli/__tests__/unit/commands/deploy.test.js +320 -0
  5. package/frigg-cli/__tests__/unit/commands/doctor.test.js +309 -0
  6. package/frigg-cli/__tests__/unit/commands/install.test.js +400 -0
  7. package/frigg-cli/__tests__/unit/commands/ui.test.js +346 -0
  8. package/frigg-cli/__tests__/unit/dependencies.test.js +74 -0
  9. package/frigg-cli/__tests__/unit/utils/database-validator.test.js +397 -0
  10. package/frigg-cli/__tests__/unit/utils/error-messages.test.js +345 -0
  11. package/frigg-cli/__tests__/unit/version-detection.test.js +171 -0
  12. package/frigg-cli/__tests__/utils/mock-factory.js +270 -0
  13. package/frigg-cli/__tests__/utils/prisma-mock.js +194 -0
  14. package/frigg-cli/__tests__/utils/test-fixtures.js +463 -0
  15. package/frigg-cli/__tests__/utils/test-setup.js +287 -0
  16. package/frigg-cli/auth-command/CLAUDE.md +293 -0
  17. package/frigg-cli/auth-command/README.md +450 -0
  18. package/frigg-cli/auth-command/api-key-flow.js +153 -0
  19. package/frigg-cli/auth-command/auth-tester.js +344 -0
  20. package/frigg-cli/auth-command/credential-storage.js +182 -0
  21. package/frigg-cli/auth-command/index.js +256 -0
  22. package/frigg-cli/auth-command/json-schema-form.js +67 -0
  23. package/frigg-cli/auth-command/module-loader.js +172 -0
  24. package/frigg-cli/auth-command/oauth-callback-server.js +431 -0
  25. package/frigg-cli/auth-command/oauth-flow.js +195 -0
  26. package/frigg-cli/auth-command/utils/browser.js +30 -0
  27. package/frigg-cli/build-command/index.js +45 -12
  28. package/frigg-cli/db-setup-command/index.js +246 -0
  29. package/frigg-cli/deploy-command/SPEC-DEPLOY-DRY-RUN.md +981 -0
  30. package/frigg-cli/deploy-command/index.js +295 -23
  31. package/frigg-cli/doctor-command/index.js +335 -0
  32. package/frigg-cli/generate-command/__tests__/generate-command.test.js +301 -0
  33. package/frigg-cli/generate-command/azure-generator.js +43 -0
  34. package/frigg-cli/generate-command/gcp-generator.js +47 -0
  35. package/frigg-cli/generate-command/index.js +332 -0
  36. package/frigg-cli/generate-command/terraform-generator.js +555 -0
  37. package/frigg-cli/generate-iam-command.js +118 -0
  38. package/frigg-cli/index.js +174 -1
  39. package/frigg-cli/index.test.js +1 -4
  40. package/frigg-cli/init-command/backend-first-handler.js +756 -0
  41. package/frigg-cli/init-command/index.js +93 -0
  42. package/frigg-cli/init-command/template-handler.js +143 -0
  43. package/frigg-cli/install-command/index.js +1 -4
  44. package/frigg-cli/jest.config.js +124 -0
  45. package/frigg-cli/package.json +63 -0
  46. package/frigg-cli/repair-command/index.js +564 -0
  47. package/frigg-cli/start-command/index.js +118 -5
  48. package/frigg-cli/start-command/start-command.test.js +297 -0
  49. package/frigg-cli/test/init-command.test.js +180 -0
  50. package/frigg-cli/test/npm-registry.test.js +319 -0
  51. package/frigg-cli/ui-command/index.js +154 -0
  52. package/frigg-cli/utils/app-resolver.js +319 -0
  53. package/frigg-cli/utils/backend-path.js +16 -17
  54. package/frigg-cli/utils/database-validator.js +167 -0
  55. package/frigg-cli/utils/error-messages.js +329 -0
  56. package/frigg-cli/utils/npm-registry.js +167 -0
  57. package/frigg-cli/utils/process-manager.js +199 -0
  58. package/frigg-cli/utils/repo-detection.js +405 -0
  59. package/infrastructure/ARCHITECTURE.md +487 -0
  60. package/infrastructure/CLAUDE.md +481 -0
  61. package/infrastructure/HEALTH.md +468 -0
  62. package/infrastructure/README.md +522 -0
  63. package/infrastructure/__tests__/fixtures/mock-aws-resources.js +391 -0
  64. package/infrastructure/__tests__/helpers/test-utils.js +277 -0
  65. package/infrastructure/__tests__/postgres-config.test.js +914 -0
  66. package/infrastructure/__tests__/template-generation.test.js +687 -0
  67. package/infrastructure/create-frigg-infrastructure.js +129 -20
  68. package/infrastructure/docs/POSTGRES-CONFIGURATION.md +630 -0
  69. package/infrastructure/docs/PRE-DEPLOYMENT-HEALTH-CHECK-SPEC.md +1317 -0
  70. package/infrastructure/docs/WEBSOCKET-CONFIGURATION.md +105 -0
  71. package/infrastructure/docs/deployment-instructions.md +268 -0
  72. package/infrastructure/docs/generate-iam-command.md +278 -0
  73. package/infrastructure/docs/iam-policy-templates.md +193 -0
  74. package/infrastructure/domains/database/aurora-builder.js +857 -0
  75. package/infrastructure/domains/database/aurora-builder.test.js +960 -0
  76. package/infrastructure/domains/database/aurora-discovery.js +87 -0
  77. package/infrastructure/domains/database/aurora-discovery.test.js +188 -0
  78. package/infrastructure/domains/database/aurora-resolver.js +210 -0
  79. package/infrastructure/domains/database/aurora-resolver.test.js +347 -0
  80. package/infrastructure/domains/database/migration-builder.js +701 -0
  81. package/infrastructure/domains/database/migration-builder.test.js +321 -0
  82. package/infrastructure/domains/database/migration-resolver.js +163 -0
  83. package/infrastructure/domains/database/migration-resolver.test.js +337 -0
  84. package/infrastructure/domains/health/application/ports/IPropertyReconciler.js +164 -0
  85. package/infrastructure/domains/health/application/ports/IResourceDetector.js +129 -0
  86. package/infrastructure/domains/health/application/ports/IResourceImporter.js +142 -0
  87. package/infrastructure/domains/health/application/ports/IStackRepository.js +131 -0
  88. package/infrastructure/domains/health/application/ports/index.js +26 -0
  89. package/infrastructure/domains/health/application/use-cases/__tests__/execute-resource-import-use-case.test.js +679 -0
  90. package/infrastructure/domains/health/application/use-cases/__tests__/mismatch-analyzer-method-name.test.js +167 -0
  91. package/infrastructure/domains/health/application/use-cases/__tests__/repair-via-import-use-case.test.js +1130 -0
  92. package/infrastructure/domains/health/application/use-cases/execute-resource-import-use-case.js +221 -0
  93. package/infrastructure/domains/health/application/use-cases/reconcile-properties-use-case.js +152 -0
  94. package/infrastructure/domains/health/application/use-cases/reconcile-properties-use-case.test.js +343 -0
  95. package/infrastructure/domains/health/application/use-cases/repair-via-import-use-case.js +535 -0
  96. package/infrastructure/domains/health/application/use-cases/repair-via-import-use-case.test.js +376 -0
  97. package/infrastructure/domains/health/application/use-cases/run-health-check-use-case.js +213 -0
  98. package/infrastructure/domains/health/application/use-cases/run-health-check-use-case.test.js +441 -0
  99. package/infrastructure/domains/health/docs/ACME-DEV-DRIFT-ANALYSIS.md +267 -0
  100. package/infrastructure/domains/health/docs/BUILD-VS-DEPLOYED-TEMPLATE-ANALYSIS.md +324 -0
  101. package/infrastructure/domains/health/docs/ORPHAN-DETECTION-ANALYSIS.md +386 -0
  102. package/infrastructure/domains/health/docs/SPEC-CLEANUP-COMMAND.md +1419 -0
  103. package/infrastructure/domains/health/docs/TDD-IMPLEMENTATION-SUMMARY.md +391 -0
  104. package/infrastructure/domains/health/docs/TEMPLATE-COMPARISON-IMPLEMENTATION.md +551 -0
  105. package/infrastructure/domains/health/domain/entities/issue.js +299 -0
  106. package/infrastructure/domains/health/domain/entities/issue.test.js +528 -0
  107. package/infrastructure/domains/health/domain/entities/property-mismatch.js +108 -0
  108. package/infrastructure/domains/health/domain/entities/property-mismatch.test.js +275 -0
  109. package/infrastructure/domains/health/domain/entities/resource.js +159 -0
  110. package/infrastructure/domains/health/domain/entities/resource.test.js +432 -0
  111. package/infrastructure/domains/health/domain/entities/stack-health-report.js +306 -0
  112. package/infrastructure/domains/health/domain/entities/stack-health-report.test.js +601 -0
  113. package/infrastructure/domains/health/domain/services/__tests__/health-score-percentage-based.test.js +380 -0
  114. package/infrastructure/domains/health/domain/services/__tests__/import-progress-monitor.test.js +971 -0
  115. package/infrastructure/domains/health/domain/services/__tests__/import-template-generator.test.js +1150 -0
  116. package/infrastructure/domains/health/domain/services/__tests__/logical-id-mapper.test.js +672 -0
  117. package/infrastructure/domains/health/domain/services/__tests__/template-parser.test.js +496 -0
  118. package/infrastructure/domains/health/domain/services/__tests__/update-progress-monitor.test.js +419 -0
  119. package/infrastructure/domains/health/domain/services/health-score-calculator.js +248 -0
  120. package/infrastructure/domains/health/domain/services/health-score-calculator.test.js +504 -0
  121. package/infrastructure/domains/health/domain/services/import-progress-monitor.js +195 -0
  122. package/infrastructure/domains/health/domain/services/import-template-generator.js +435 -0
  123. package/infrastructure/domains/health/domain/services/logical-id-mapper.js +345 -0
  124. package/infrastructure/domains/health/domain/services/mismatch-analyzer.js +234 -0
  125. package/infrastructure/domains/health/domain/services/mismatch-analyzer.test.js +431 -0
  126. package/infrastructure/domains/health/domain/services/property-mutability-config.js +382 -0
  127. package/infrastructure/domains/health/domain/services/template-parser.js +245 -0
  128. package/infrastructure/domains/health/domain/services/update-progress-monitor.js +192 -0
  129. package/infrastructure/domains/health/domain/value-objects/health-score.js +138 -0
  130. package/infrastructure/domains/health/domain/value-objects/health-score.test.js +267 -0
  131. package/infrastructure/domains/health/domain/value-objects/property-mutability.js +161 -0
  132. package/infrastructure/domains/health/domain/value-objects/property-mutability.test.js +198 -0
  133. package/infrastructure/domains/health/domain/value-objects/resource-state.js +167 -0
  134. package/infrastructure/domains/health/domain/value-objects/resource-state.test.js +196 -0
  135. package/infrastructure/domains/health/domain/value-objects/stack-identifier.js +192 -0
  136. package/infrastructure/domains/health/domain/value-objects/stack-identifier.test.js +262 -0
  137. package/infrastructure/domains/health/infrastructure/adapters/__tests__/orphan-detection-cfn-tagged.test.js +312 -0
  138. package/infrastructure/domains/health/infrastructure/adapters/__tests__/orphan-detection-multi-stack.test.js +367 -0
  139. package/infrastructure/domains/health/infrastructure/adapters/__tests__/orphan-detection-relationship-analysis.test.js +432 -0
  140. package/infrastructure/domains/health/infrastructure/adapters/aws-property-reconciler.js +784 -0
  141. package/infrastructure/domains/health/infrastructure/adapters/aws-property-reconciler.test.js +1133 -0
  142. package/infrastructure/domains/health/infrastructure/adapters/aws-resource-detector.js +565 -0
  143. package/infrastructure/domains/health/infrastructure/adapters/aws-resource-detector.test.js +554 -0
  144. package/infrastructure/domains/health/infrastructure/adapters/aws-resource-importer.js +318 -0
  145. package/infrastructure/domains/health/infrastructure/adapters/aws-resource-importer.test.js +398 -0
  146. package/infrastructure/domains/health/infrastructure/adapters/aws-stack-repository.js +777 -0
  147. package/infrastructure/domains/health/infrastructure/adapters/aws-stack-repository.test.js +580 -0
  148. package/infrastructure/domains/integration/integration-builder.js +596 -0
  149. package/infrastructure/domains/integration/integration-builder.test.js +939 -0
  150. package/infrastructure/domains/integration/integration-resolver.js +170 -0
  151. package/infrastructure/domains/integration/integration-resolver.test.js +369 -0
  152. package/infrastructure/domains/integration/websocket-builder.js +69 -0
  153. package/infrastructure/domains/integration/websocket-builder.test.js +195 -0
  154. package/infrastructure/domains/networking/vpc-builder.js +2051 -0
  155. package/infrastructure/domains/networking/vpc-builder.test.js +1960 -0
  156. package/infrastructure/domains/networking/vpc-discovery.js +177 -0
  157. package/infrastructure/domains/networking/vpc-discovery.test.js +350 -0
  158. package/infrastructure/domains/networking/vpc-resolver.js +505 -0
  159. package/infrastructure/domains/networking/vpc-resolver.test.js +801 -0
  160. package/infrastructure/domains/parameters/ssm-builder.js +79 -0
  161. package/infrastructure/domains/parameters/ssm-builder.test.js +189 -0
  162. package/infrastructure/domains/parameters/ssm-discovery.js +84 -0
  163. package/infrastructure/domains/parameters/ssm-discovery.test.js +210 -0
  164. package/infrastructure/domains/scheduler/scheduler-builder.js +211 -0
  165. package/infrastructure/domains/security/iam-generator.js +816 -0
  166. package/infrastructure/domains/security/iam-generator.test.js +204 -0
  167. package/infrastructure/domains/security/kms-builder.js +415 -0
  168. package/infrastructure/domains/security/kms-builder.test.js +392 -0
  169. package/infrastructure/domains/security/kms-discovery.js +80 -0
  170. package/infrastructure/domains/security/kms-discovery.test.js +177 -0
  171. package/infrastructure/domains/security/kms-resolver.js +96 -0
  172. package/infrastructure/domains/security/kms-resolver.test.js +216 -0
  173. package/infrastructure/domains/security/templates/frigg-deployment-iam-stack.yaml +401 -0
  174. package/infrastructure/domains/security/templates/iam-policy-basic.json +218 -0
  175. package/infrastructure/domains/security/templates/iam-policy-full.json +288 -0
  176. package/infrastructure/domains/shared/base-builder.js +112 -0
  177. package/infrastructure/domains/shared/base-resolver.js +186 -0
  178. package/infrastructure/domains/shared/base-resolver.test.js +305 -0
  179. package/infrastructure/domains/shared/builder-orchestrator.js +212 -0
  180. package/infrastructure/domains/shared/builder-orchestrator.test.js +213 -0
  181. package/infrastructure/domains/shared/cloudformation-discovery-v2.js +334 -0
  182. package/infrastructure/domains/shared/cloudformation-discovery.js +681 -0
  183. package/infrastructure/domains/shared/cloudformation-discovery.test.js +1320 -0
  184. package/infrastructure/domains/shared/environment-builder.js +119 -0
  185. package/infrastructure/domains/shared/environment-builder.test.js +247 -0
  186. package/infrastructure/domains/shared/providers/aws-provider-adapter.js +579 -0
  187. package/infrastructure/domains/shared/providers/aws-provider-adapter.test.js +416 -0
  188. package/infrastructure/domains/shared/providers/azure-provider-adapter.stub.js +93 -0
  189. package/infrastructure/domains/shared/providers/cloud-provider-adapter.js +136 -0
  190. package/infrastructure/domains/shared/providers/gcp-provider-adapter.stub.js +82 -0
  191. package/infrastructure/domains/shared/providers/provider-factory.js +108 -0
  192. package/infrastructure/domains/shared/providers/provider-factory.test.js +170 -0
  193. package/infrastructure/domains/shared/resource-discovery.enhanced.test.js +306 -0
  194. package/infrastructure/domains/shared/resource-discovery.js +256 -0
  195. package/infrastructure/domains/shared/resource-discovery.test.js +757 -0
  196. package/infrastructure/domains/shared/types/app-definition.js +205 -0
  197. package/infrastructure/domains/shared/types/discovery-result.js +106 -0
  198. package/infrastructure/domains/shared/types/discovery-result.test.js +258 -0
  199. package/infrastructure/domains/shared/types/index.js +46 -0
  200. package/infrastructure/domains/shared/types/resource-ownership.js +108 -0
  201. package/infrastructure/domains/shared/types/resource-ownership.test.js +101 -0
  202. package/infrastructure/domains/shared/utilities/base-definition-factory.js +408 -0
  203. package/infrastructure/domains/shared/utilities/base-definition-factory.js.bak +338 -0
  204. package/infrastructure/domains/shared/utilities/base-definition-factory.test.js +291 -0
  205. package/infrastructure/domains/shared/utilities/handler-path-resolver.js +134 -0
  206. package/infrastructure/domains/shared/utilities/handler-path-resolver.test.js +268 -0
  207. package/infrastructure/domains/shared/utilities/prisma-layer-manager.js +159 -0
  208. package/infrastructure/domains/shared/utilities/prisma-layer-manager.test.js +444 -0
  209. package/infrastructure/domains/shared/validation/env-validator.js +78 -0
  210. package/infrastructure/domains/shared/validation/env-validator.test.js +173 -0
  211. package/infrastructure/domains/shared/validation/plugin-validator.js +187 -0
  212. package/infrastructure/domains/shared/validation/plugin-validator.test.js +323 -0
  213. package/infrastructure/esbuild.config.js +53 -0
  214. package/infrastructure/infrastructure-composer.js +119 -0
  215. package/infrastructure/infrastructure-composer.test.js +1896 -0
  216. package/infrastructure/integration.test.js +383 -0
  217. package/infrastructure/scripts/build-prisma-layer.js +701 -0
  218. package/infrastructure/scripts/build-prisma-layer.test.js +170 -0
  219. package/infrastructure/scripts/build-time-discovery.js +238 -0
  220. package/infrastructure/scripts/build-time-discovery.test.js +379 -0
  221. package/infrastructure/scripts/run-discovery.js +110 -0
  222. package/infrastructure/scripts/verify-prisma-layer.js +72 -0
  223. package/management-ui/README.md +203 -0
  224. package/package.json +44 -14
  225. package/test/index.js +2 -4
  226. package/test/mock-api.js +1 -3
  227. package/test/mock-integration.js +4 -14
  228. package/.eslintrc.json +0 -3
  229. package/CHANGELOG.md +0 -132
  230. package/infrastructure/app-handler-helpers.js +0 -57
  231. package/infrastructure/backend-utils.js +0 -87
  232. package/infrastructure/routers/auth.js +0 -26
  233. package/infrastructure/routers/integration-defined-routers.js +0 -42
  234. package/infrastructure/routers/middleware/loadUser.js +0 -15
  235. package/infrastructure/routers/middleware/requireLoggedInUser.js +0 -12
  236. package/infrastructure/routers/user.js +0 -41
  237. package/infrastructure/routers/websocket.js +0 -55
  238. package/infrastructure/serverless-template.js +0 -292
  239. package/infrastructure/workers/integration-defined-workers.js +0 -24
  240. package/test/auther-definition-tester.js +0 -125
@@ -0,0 +1,596 @@
1
+ /**
2
+ * Integration Builder
3
+ *
4
+ * Domain Layer - Hexagonal Architecture
5
+ *
6
+ * Responsible for:
7
+ * - Creating SQS queues for each integration (CloudFormation resources)
8
+ * - Creating InternalErrorQueue (dead letter queue)
9
+ * - Creating Lambda function definitions (serverless template code)
10
+ * - Creating queue worker Lambda functions
11
+ * - Creating webhook handler functions
12
+ * - Configuring integration-specific routes and handlers
13
+ *
14
+ * Uses ownership-based architecture to support both stack-managed
15
+ * and externally-provided SQS queues.
16
+ */
17
+
18
+ const {
19
+ InfrastructureBuilder,
20
+ ValidationResult,
21
+ } = require('../shared/base-builder');
22
+ const IntegrationResourceResolver = require('./integration-resolver');
23
+ const {
24
+ createEmptyDiscoveryResult,
25
+ ResourceOwnership,
26
+ } = require('../shared/types');
27
+
28
+ class IntegrationBuilder extends InfrastructureBuilder {
29
+ constructor() {
30
+ super();
31
+ this.name = 'IntegrationBuilder';
32
+ }
33
+
34
+ shouldExecute(appDefinition) {
35
+ return (
36
+ Array.isArray(appDefinition.integrations) &&
37
+ appDefinition.integrations.length > 0
38
+ );
39
+ }
40
+
41
+ getDependencies() {
42
+ return []; // No dependencies - integrations can run independently
43
+ }
44
+
45
+ validate(appDefinition) {
46
+ const result = new ValidationResult();
47
+
48
+ if (!appDefinition.integrations) {
49
+ return result; // Not an error, just no integrations
50
+ }
51
+
52
+ if (!Array.isArray(appDefinition.integrations)) {
53
+ result.addError('integrations must be an array');
54
+ return result;
55
+ }
56
+
57
+ // Validate each integration
58
+ appDefinition.integrations.forEach((integration, index) => {
59
+ if (!integration?.Definition?.name) {
60
+ result.addError(
61
+ `Integration at index ${index} is missing Definition or name`
62
+ );
63
+ }
64
+ });
65
+
66
+ return result;
67
+ }
68
+
69
+ /**
70
+ * Build integration infrastructure using ownership-based architecture
71
+ */
72
+ async build(appDefinition, discoveredResources) {
73
+ console.log(`\n[${this.name}] Configuring integrations...`);
74
+ console.log(
75
+ ` Processing ${appDefinition.integrations.length} integrations...`
76
+ );
77
+
78
+ const usePrismaLayer = appDefinition.usePrismaLambdaLayer !== false;
79
+
80
+ const result = {
81
+ functions: {},
82
+ resources: {},
83
+ environment: {},
84
+ custom: {},
85
+ iamStatements: [],
86
+ };
87
+
88
+ // Get structured discovery result
89
+ const discovery =
90
+ discoveredResources._structured ||
91
+ this.convertFlatDiscoveryToStructured(discoveredResources);
92
+
93
+ // Use IntegrationResourceResolver to make ownership decisions
94
+ const resolver = new IntegrationResourceResolver();
95
+ const decisions = resolver.resolveAll(appDefinition, discovery);
96
+
97
+ console.log('\n 📋 Resource Ownership Decisions:');
98
+ console.log(
99
+ ` InternalErrorQueue: ${decisions.internalErrorQueue.ownership} - ${decisions.internalErrorQueue.reason}`
100
+ );
101
+
102
+ // Log per-integration decisions
103
+ Object.keys(decisions.integrations).forEach((integrationName) => {
104
+ const queueDecision = decisions.integrations[integrationName].queue;
105
+ console.log(
106
+ ` ${integrationName}Queue: ${queueDecision.ownership} - ${queueDecision.reason}`
107
+ );
108
+ });
109
+
110
+ // Build resources based on ownership decisions
111
+ await this.buildFromDecisions(
112
+ decisions,
113
+ appDefinition,
114
+ result,
115
+ usePrismaLayer
116
+ );
117
+
118
+ console.log(`[${this.name}] ✅ Integration configuration completed`);
119
+ return result;
120
+ }
121
+
122
+ /**
123
+ * Convert flat discovery to structured discovery
124
+ * Provides backwards compatibility
125
+ */
126
+ convertFlatDiscoveryToStructured(flatDiscovery) {
127
+ const discovery = createEmptyDiscoveryResult();
128
+
129
+ if (!flatDiscovery) {
130
+ return discovery;
131
+ }
132
+
133
+ // Check if resources are from CloudFormation stack
134
+ if (flatDiscovery.fromCloudFormationStack) {
135
+ discovery.fromCloudFormation = true;
136
+ discovery.stackName = flatDiscovery.stackName || 'assumed-stack';
137
+
138
+ // Add stack-managed resources from existingLogicalIds
139
+ const existingLogicalIds = flatDiscovery.existingLogicalIds || [];
140
+ existingLogicalIds.forEach((logicalId) => {
141
+ let resourceType = '';
142
+ let physicalId = '';
143
+
144
+ // Determine resource type and physical ID
145
+ if (logicalId === 'InternalErrorQueue') {
146
+ resourceType = 'AWS::SQS::Queue';
147
+ physicalId = flatDiscovery.internalErrorQueueUrl;
148
+ } else if (logicalId.endsWith('Queue')) {
149
+ // Integration-specific queue (e.g., SlackQueue, HubspotQueue)
150
+ resourceType = 'AWS::SQS::Queue';
151
+ const integrationName = logicalId
152
+ .replace('Queue', '')
153
+ .toLowerCase();
154
+ physicalId = flatDiscovery[`${integrationName}QueueUrl`];
155
+ }
156
+
157
+ if (physicalId && typeof physicalId === 'string') {
158
+ discovery.stackManaged.push({
159
+ logicalId,
160
+ physicalId,
161
+ resourceType,
162
+ });
163
+ }
164
+ });
165
+ }
166
+
167
+ return discovery;
168
+ }
169
+
170
+ /**
171
+ * Build integration resources based on ownership decisions
172
+ */
173
+ async buildFromDecisions(
174
+ decisions,
175
+ appDefinition,
176
+ result,
177
+ usePrismaLayer = true
178
+ ) {
179
+ // Create package config first — needed by all Lambda functions including DLQ processor
180
+ const functionPackageConfig =
181
+ this.createFunctionPackageConfig(usePrismaLayer);
182
+
183
+ // Create InternalErrorQueue if ownership = STACK
184
+ const shouldCreateInternalErrorQueue =
185
+ decisions.internalErrorQueue.ownership === ResourceOwnership.STACK;
186
+
187
+ if (shouldCreateInternalErrorQueue) {
188
+ console.log(' → Creating InternalErrorQueue in stack');
189
+ this.createInternalErrorQueue(result, functionPackageConfig);
190
+ } else {
191
+ console.log(' → Using external InternalErrorQueue');
192
+ this.useExternalInternalErrorQueue(
193
+ decisions.internalErrorQueue,
194
+ result,
195
+ functionPackageConfig
196
+ );
197
+ }
198
+
199
+ for (const integration of appDefinition.integrations) {
200
+ const integrationName = integration.Definition.name;
201
+ const queueDecision = decisions.integrations[integrationName].queue;
202
+
203
+ console.log(`\n Adding integration: ${integrationName}`);
204
+
205
+ // Create Lambda function definitions (serverless template code)
206
+ await this.createFunctionDefinitions(
207
+ integration,
208
+ functionPackageConfig,
209
+ result,
210
+ usePrismaLayer
211
+ );
212
+
213
+ // Create or reference SQS queue based on ownership decision
214
+ const shouldCreateQueue =
215
+ queueDecision.ownership === ResourceOwnership.STACK;
216
+
217
+ if (shouldCreateQueue) {
218
+ console.log(
219
+ ` ✓ Creating ${integrationName}Queue in stack`
220
+ );
221
+ this.createIntegrationQueue(integrationName, result);
222
+ } else {
223
+ console.log(` ✓ Using external ${integrationName}Queue`);
224
+ this.useExternalIntegrationQueue(
225
+ integrationName,
226
+ queueDecision,
227
+ result
228
+ );
229
+ }
230
+ }
231
+ }
232
+
233
+ /**
234
+ * Create function package exclusion configuration
235
+ */
236
+ createFunctionPackageConfig(usePrismaLayer = true) {
237
+ return {
238
+ exclude: [
239
+ // Exclude AWS SDK (provided by Lambda runtime)
240
+ 'node_modules/aws-sdk/**',
241
+ 'node_modules/@aws-sdk/**',
242
+
243
+ // Exclude Prisma (provided via Lambda Layer)
244
+ ...(usePrismaLayer
245
+ ? [
246
+ 'node_modules/@prisma/**',
247
+ 'node_modules/.prisma/**',
248
+ 'node_modules/prisma/**',
249
+ 'node_modules/@friggframework/core/generated/**',
250
+ ]
251
+ : []),
252
+
253
+ // Exclude ALL nested node_modules
254
+ 'node_modules/**/node_modules/**',
255
+
256
+ // Exclude build tools (not needed at runtime)
257
+ 'node_modules/esbuild/**',
258
+ 'node_modules/@esbuild/**',
259
+ 'node_modules/typescript/**',
260
+ 'node_modules/webpack/**',
261
+ 'node_modules/osls/**',
262
+ 'node_modules/serverless-esbuild/**',
263
+ 'node_modules/serverless-jetpack/**',
264
+ 'node_modules/serverless-offline/**',
265
+ 'node_modules/serverless-offline-sqs/**',
266
+ 'node_modules/serverless-dotenv-plugin/**',
267
+ 'node_modules/serverless-kms-grants/**',
268
+ // Note: DO NOT exclude serverless-http - it's a runtime dependency!
269
+
270
+ // Exclude dev/test dependencies
271
+ 'node_modules/@friggframework/test/**',
272
+ 'node_modules/@friggframework/eslint-config/**',
273
+ 'node_modules/@friggframework/prettier-config/**',
274
+ 'node_modules/jest/**',
275
+ 'node_modules/prettier/**',
276
+ 'node_modules/eslint/**',
277
+
278
+ // Exclude local dev files
279
+ 'deploy.log',
280
+ '.env.backup',
281
+ 'docker-compose.yml',
282
+ 'jest.config.js',
283
+ 'jest.unit.config.js',
284
+ '.eslintrc.json',
285
+ '.prettierrc',
286
+ '.prettierignore',
287
+ '.markdownlintignore',
288
+ 'package-lock.json',
289
+
290
+ // Exclude development/test files (keep src/ - needed for integrations and api-modules)
291
+ 'coverage/**',
292
+ 'test/**',
293
+ 'layers/**',
294
+ // Note: DO NOT exclude src/** - handlers need src/integrations and src/api-modules at runtime
295
+ '**/*.test.js',
296
+ '**/*.spec.js',
297
+ '**/.claude-flow/**',
298
+ '**/.swarm/**',
299
+ ],
300
+ };
301
+ }
302
+
303
+ /**
304
+ * Create Lambda function definitions for an integration
305
+ * These are serverless framework template function definitions
306
+ */
307
+ async createFunctionDefinitions(
308
+ integration,
309
+ functionPackageConfig,
310
+ result,
311
+ usePrismaLayer = true
312
+ ) {
313
+ const integrationName = integration.Definition.name;
314
+
315
+ // Add webhook handler if enabled (BEFORE catch-all proxy route)
316
+ // CRITICAL: Webhook routes must be defined before the catch-all {proxy+} route
317
+ // to ensure proper route matching in AWS API Gateway/HTTP API
318
+ const webhookConfig = integration.Definition.webhooks;
319
+ if (
320
+ webhookConfig &&
321
+ (webhookConfig === true || webhookConfig.enabled === true)
322
+ ) {
323
+ const webhookFunctionName = `${integrationName}Webhook`;
324
+
325
+ result.functions[webhookFunctionName] = {
326
+ handler: `node_modules/@friggframework/core/handlers/routers/integration-webhook-routers.handlers.${integrationName}Webhook.handler`,
327
+ skipEsbuild: true, // Nested exports in node_modules - skip esbuild bundling
328
+ package: functionPackageConfig,
329
+ ...(usePrismaLayer && {
330
+ layers: [{ Ref: 'PrismaLambdaLayer' }],
331
+ }), // Webhook handlers need Prisma for credential lookups
332
+ events: [
333
+ {
334
+ httpApi: {
335
+ path: `/api/${integrationName}-integration/webhooks`,
336
+ method: 'POST',
337
+ },
338
+ },
339
+ {
340
+ httpApi: {
341
+ path: `/api/${integrationName}-integration/webhooks/{integrationId}`,
342
+ method: 'POST',
343
+ },
344
+ },
345
+ ],
346
+ };
347
+ console.log(` ✓ Webhook handler function defined`);
348
+ }
349
+
350
+ // Create HTTP API handler for integration (catch-all route AFTER
351
+ // webhooks). Extension routes get their own functions below.
352
+ result.functions[integrationName] = {
353
+ handler: `node_modules/@friggframework/core/handlers/routers/integration-defined-routers.handlers.${integrationName}.handler`,
354
+ skipEsbuild: true, // Nested exports in node_modules - skip esbuild bundling
355
+ package: functionPackageConfig,
356
+ ...(usePrismaLayer && { layers: [{ Ref: 'PrismaLambdaLayer' }] }), // HTTP handlers need Prisma for integration queries
357
+ events: [
358
+ {
359
+ httpApi: {
360
+ path: `/api/${integrationName}-integration/{proxy+}`,
361
+ method: 'ANY',
362
+ },
363
+ },
364
+ ],
365
+ };
366
+ console.log(` ✓ HTTP handler function defined`);
367
+
368
+ // One serverless function per extension binding, namespaced under
369
+ // /{bindingKey}. Prisma layer attached only when useDatabase is true.
370
+ const sanitizeBindingKey = (name) =>
371
+ String(name).replace(/[^A-Za-z0-9]/g, '');
372
+ const extensionEntries = Object.entries(
373
+ integration.Definition.extensions || {}
374
+ );
375
+ for (const [bindingKey, binding] of extensionEntries) {
376
+ const extension = binding && binding.extension;
377
+ const routes = (extension && extension.routes) || [];
378
+ if (routes.length === 0) continue;
379
+ const useDatabase =
380
+ binding.useDatabase ??
381
+ (extension && extension.useDatabase) ??
382
+ false;
383
+ // Wire contract: core's integration-defined-routers derives the
384
+ // identical handler key. Keep both in sync.
385
+ const fnName = `${integrationName}__${sanitizeBindingKey(
386
+ bindingKey
387
+ )}`;
388
+ // Distinct binding keys can sanitize to the same fnName — fail loud rather than overwrite.
389
+ if (
390
+ Object.prototype.hasOwnProperty.call(result.functions, fnName)
391
+ ) {
392
+ throw new Error(
393
+ `Integration "${integrationName}" extension function conflict: ` +
394
+ `binding "${bindingKey}" sanitizes to "${fnName}", which is already taken. ` +
395
+ `Use binding keys that are distinct after stripping non-alphanumeric characters.`
396
+ );
397
+ }
398
+ result.functions[fnName] = {
399
+ handler: `node_modules/@friggframework/core/handlers/routers/integration-defined-routers.handlers.${fnName}.handler`,
400
+ skipEsbuild: true,
401
+ package: functionPackageConfig,
402
+ ...(usePrismaLayer &&
403
+ useDatabase && { layers: [{ Ref: 'PrismaLambdaLayer' }] }),
404
+ events: routes.map((route) => ({
405
+ httpApi: {
406
+ path: `/api/${integrationName}-integration/${bindingKey}${route.path}`,
407
+ method: route.method,
408
+ },
409
+ })),
410
+ };
411
+ console.log(
412
+ ` ✓ Extension handler function defined: ${fnName} (useDatabase: ${useDatabase})`
413
+ );
414
+ }
415
+
416
+ // Create Queue Worker function
417
+ const queueWorkerName = `${integrationName}QueueWorker`;
418
+ result.functions[queueWorkerName] = {
419
+ handler: `node_modules/@friggframework/core/handlers/workers/integration-defined-workers.handlers.${integrationName}.queueWorker`,
420
+ skipEsbuild: true, // Nested exports in node_modules - skip esbuild bundling
421
+ package: functionPackageConfig,
422
+ ...(usePrismaLayer && { layers: [{ Ref: 'PrismaLambdaLayer' }] }), // Queue workers need Prisma for database operations
423
+ reservedConcurrency: 20,
424
+ events: [
425
+ {
426
+ sqs: {
427
+ arn: {
428
+ 'Fn::GetAtt': [
429
+ `${this.capitalizeFirst(integrationName)}Queue`,
430
+ 'Arn',
431
+ ],
432
+ },
433
+ batchSize: 1,
434
+ functionResponseType: 'ReportBatchItemFailures',
435
+ },
436
+ },
437
+ ],
438
+ timeout: 900, // 15 minutes max for queue workers (Lambda maximum)
439
+ };
440
+ console.log(` ✓ Queue worker function defined`);
441
+ }
442
+
443
+ /**
444
+ * Create InternalErrorQueue CloudFormation resource
445
+ */
446
+ createInternalErrorQueue(result, functionPackageConfig) {
447
+ const queueName =
448
+ '${self:service}-${self:provider.stage}-InternalErrorQueue';
449
+
450
+ result.custom.InternalErrorQueue = queueName;
451
+
452
+ result.resources.InternalErrorQueue = {
453
+ Type: 'AWS::SQS::Queue',
454
+ Properties: {
455
+ QueueName: '${self:custom.InternalErrorQueue}',
456
+ MessageRetentionPeriod: 1209600, // 14 days
457
+ VisibilityTimeout: 300, // 5 minutes — must be >= 6x DLQ processor Lambda timeout (30s × 6 = 180s)
458
+ },
459
+ };
460
+
461
+ this.createDLQObservability(
462
+ result,
463
+ functionPackageConfig,
464
+ {
465
+ 'Fn::GetAtt': ['InternalErrorQueue', 'Arn'],
466
+ },
467
+ {
468
+ 'Fn::GetAtt': ['InternalErrorQueue', 'QueueName'],
469
+ }
470
+ );
471
+
472
+ console.log(' ✓ Created InternalErrorQueue resource');
473
+ }
474
+
475
+ /**
476
+ * Use external InternalErrorQueue
477
+ */
478
+ useExternalInternalErrorQueue(decision, result, functionPackageConfig) {
479
+ // Add ARN to environment for Lambda functions
480
+ result.environment.INTERNAL_ERROR_QUEUE_ARN = decision.physicalId;
481
+
482
+ // Extract queue name from ARN for CloudWatch dimensions
483
+ const arnParts = decision.physicalId.split(':');
484
+ const queueName = arnParts[arnParts.length - 1];
485
+
486
+ this.createDLQObservability(
487
+ result,
488
+ functionPackageConfig,
489
+ decision.physicalId,
490
+ queueName
491
+ );
492
+
493
+ console.log(
494
+ ` ✓ Using external InternalErrorQueue: ${decision.physicalId}`
495
+ );
496
+ }
497
+
498
+ /**
499
+ * Create DLQ observability resources (alarm + processor Lambda).
500
+ * Called for both stack-owned and external InternalErrorQueues.
501
+ */
502
+ createDLQObservability(result, functionPackageConfig, queueArn, queueName) {
503
+ // CloudWatch Alarm: fires when any message lands in the DLQ
504
+ result.resources.DLQMessageAlarm = {
505
+ Type: 'AWS::CloudWatch::Alarm',
506
+ Properties: {
507
+ AlarmDescription:
508
+ 'Messages in dead-letter queue — integration queue processing failures',
509
+ Namespace: 'AWS/SQS',
510
+ MetricName: 'ApproximateNumberOfMessagesVisible',
511
+ Statistic: 'Maximum',
512
+ Threshold: 500,
513
+ ComparisonOperator: 'GreaterThanThreshold',
514
+ EvaluationPeriods: 1,
515
+ Period: 300,
516
+ AlarmActions: [{ Ref: 'InternalErrorBridgeTopic' }],
517
+ Dimensions: [{ Name: 'QueueName', Value: queueName }],
518
+ },
519
+ };
520
+
521
+ // DLQ processor Lambda: logs failed messages with structured context
522
+ result.functions.dlqProcessor = {
523
+ handler:
524
+ 'node_modules/@friggframework/core/handlers/workers/dlq-processor.dlqProcessor',
525
+ skipEsbuild: true,
526
+ package: functionPackageConfig,
527
+ reservedConcurrency: 1,
528
+ timeout: 30,
529
+ events: [
530
+ {
531
+ sqs: {
532
+ arn: queueArn,
533
+ batchSize: 10,
534
+ functionResponseType: 'ReportBatchItemFailures',
535
+ },
536
+ },
537
+ ],
538
+ };
539
+
540
+ console.log(' ✓ Created DLQ CloudWatch alarm');
541
+ console.log(' ✓ Created DLQ processor Lambda');
542
+ }
543
+
544
+ /**
545
+ * Create integration-specific SQS queue CloudFormation resource
546
+ */
547
+ createIntegrationQueue(integrationName, result) {
548
+ const queueReference = `${this.capitalizeFirst(integrationName)}Queue`;
549
+ const queueName = `\${self:service}--\${self:provider.stage}-${queueReference}`;
550
+
551
+ result.resources[queueReference] = {
552
+ Type: 'AWS::SQS::Queue',
553
+ Properties: {
554
+ QueueName: `\${self:custom.${queueReference}}`,
555
+ MessageRetentionPeriod: 345600, // 4 days (SQS default)
556
+ VisibilityTimeout: 1800,
557
+ RedrivePolicy: {
558
+ maxReceiveCount: 3,
559
+ deadLetterTargetArn: {
560
+ 'Fn::GetAtt': ['InternalErrorQueue', 'Arn'],
561
+ },
562
+ },
563
+ },
564
+ };
565
+
566
+ // Add queue URL to environment
567
+ result.environment[`${integrationName.toUpperCase()}_QUEUE_URL`] = {
568
+ Ref: queueReference,
569
+ };
570
+
571
+ // Add queue name to custom section
572
+ result.custom[queueReference] = queueName;
573
+
574
+ console.log(` ✓ Created ${queueReference} resource`);
575
+ }
576
+
577
+ /**
578
+ * Use external integration queue
579
+ */
580
+ useExternalIntegrationQueue(integrationName, decision, result) {
581
+ // Add queue URL to environment for Lambda functions
582
+ result.environment[`${integrationName.toUpperCase()}_QUEUE_URL`] =
583
+ decision.physicalId;
584
+
585
+ console.log(` ✓ Using external queue: ${decision.physicalId}`);
586
+ }
587
+
588
+ /**
589
+ * Capitalize first letter of string (e.g., 'slack' -> 'Slack')
590
+ */
591
+ capitalizeFirst(str) {
592
+ return str.charAt(0).toUpperCase() + str.slice(1);
593
+ }
594
+ }
595
+
596
+ module.exports = { IntegrationBuilder };