@geek-fun/serverlessinsight 0.8.1 → 0.8.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.
Files changed (119) hide show
  1. package/.serverlessinsight/state-cdn-flow-app-cdn-flow-service.json.backup +14 -0
  2. package/.serverlessinsight/state-iam-test-app-iam-test-service.json.backup +14 -0
  3. package/.serverlessinsight/state-insight-poc-deploy-app-insight-poc-deploy.json.backup +348 -0
  4. package/.serverlessinsight/state-insight-poc-destroy-app-insight-poc-destroy.json.backup +14 -0
  5. package/.serverlessinsight/state-insight-volc-app-insight-volc.json.backup +114 -0
  6. package/.serverlessinsight/state-insight-volc-destroy-app-insight-volc-destroy.json.backup +1 -0
  7. package/README.md +26 -18
  8. package/README.zh-CN.md +19 -6
  9. package/dist/package.json +3 -1
  10. package/dist/src/commands/deploy.js +87 -4
  11. package/dist/src/commands/destroy.js +61 -10
  12. package/dist/src/commands/forceUnlock.js +8 -0
  13. package/dist/src/commands/index.js +34 -2
  14. package/dist/src/commands/login.js +195 -0
  15. package/dist/src/commands/logout.js +17 -0
  16. package/dist/src/commands/show.js +15 -7
  17. package/dist/src/commands/whoami.js +20 -0
  18. package/dist/src/common/aliyunClient/apigwOperations.js +332 -32
  19. package/dist/src/common/aliyunClient/esOperations.js +43 -29
  20. package/dist/src/common/aliyunClient/fc3Operations.js +481 -207
  21. package/dist/src/common/aliyunClient/ossOperations.js +165 -13
  22. package/dist/src/common/aliyunClient/rdsOperations.js +143 -39
  23. package/dist/src/common/aliyunClient/slsOperations.js +56 -26
  24. package/dist/src/common/aliyunClient/tablestoreOperations.js +25 -22
  25. package/dist/src/common/apiClient.js +102 -0
  26. package/dist/src/common/constants.js +9 -4
  27. package/dist/src/common/credentialStore.js +94 -0
  28. package/dist/src/common/credentials.js +2 -0
  29. package/dist/src/common/eventQueue.js +208 -0
  30. package/dist/src/common/hashUtils.js +87 -11
  31. package/dist/src/common/imsClient.js +1 -0
  32. package/dist/src/common/index.js +3 -0
  33. package/dist/src/common/lockManager.js +61 -54
  34. package/dist/src/common/logger.js +2 -0
  35. package/dist/src/common/polling.js +54 -0
  36. package/dist/src/common/reportResourceEvent.js +8 -0
  37. package/dist/src/common/runtimeMapper.js +11 -12
  38. package/dist/src/common/stateBackend/index.js +10 -1
  39. package/dist/src/common/stateBackend/localStateBackend.js +4 -1
  40. package/dist/src/common/stateBackend/remoteStateBackend.js +6 -3
  41. package/dist/src/common/stateBackend/saasStateBackend.js +266 -0
  42. package/dist/src/common/stateManager.js +135 -13
  43. package/dist/src/common/tencentClient/cosOperations.js +180 -3
  44. package/dist/src/common/tencentClient/esOperations.js +110 -46
  45. package/dist/src/common/tencentClient/index.js +22 -1
  46. package/dist/src/common/tencentClient/scfOperations.js +255 -133
  47. package/dist/src/common/tencentClient/tdsqlcClusterMapper.js +81 -0
  48. package/dist/src/common/tencentClient/tdsqlcOperations.js +93 -99
  49. package/dist/src/common/triggerMapper.js +36 -0
  50. package/dist/src/common/volcengineClient/apigwOperations.js +539 -159
  51. package/dist/src/common/volcengineClient/iamOperations.js +58 -48
  52. package/dist/src/common/volcengineClient/index.js +46 -21
  53. package/dist/src/common/volcengineClient/stsOperations.js +32 -0
  54. package/dist/src/common/volcengineClient/tlsOperations.js +229 -178
  55. package/dist/src/common/volcengineClient/tosOperations.js +78 -19
  56. package/dist/src/common/volcengineClient/vefaasOperations.js +272 -54
  57. package/dist/src/lang/en.js +84 -0
  58. package/dist/src/lang/zh-CN.js +84 -0
  59. package/dist/src/parser/eventParser.js +2 -0
  60. package/dist/src/parser/functionParser.js +96 -65
  61. package/dist/src/parser/index.js +7 -4
  62. package/dist/src/stack/aliyunStack/apigwExecutor.js +39 -1
  63. package/dist/src/stack/aliyunStack/apigwPlanner.js +14 -10
  64. package/dist/src/stack/aliyunStack/apigwResource.js +230 -125
  65. package/dist/src/stack/aliyunStack/apigwTypes.js +16 -3
  66. package/dist/src/stack/aliyunStack/databaseExecutor.js +39 -1
  67. package/dist/src/stack/aliyunStack/databasePlanner.js +13 -1
  68. package/dist/src/stack/aliyunStack/databaseResource.js +135 -40
  69. package/dist/src/stack/aliyunStack/deployer.js +2 -2
  70. package/dist/src/stack/aliyunStack/destroyer.js +2 -2
  71. package/dist/src/stack/aliyunStack/fc3Executor.js +25 -2
  72. package/dist/src/stack/aliyunStack/fc3Planner.js +11 -1
  73. package/dist/src/stack/aliyunStack/fc3Resource.js +327 -64
  74. package/dist/src/stack/aliyunStack/ossExecutor.js +39 -1
  75. package/dist/src/stack/aliyunStack/ossPlanner.js +15 -1
  76. package/dist/src/stack/aliyunStack/ossResource.js +267 -219
  77. package/dist/src/stack/aliyunStack/tablestoreExecutor.js +39 -1
  78. package/dist/src/stack/aliyunStack/tablestorePlanner.js +13 -1
  79. package/dist/src/stack/aliyunStack/tablestoreResource.js +54 -20
  80. package/dist/src/stack/alreadyExists.js +26 -0
  81. package/dist/src/stack/ownershipTag.js +40 -0
  82. package/dist/src/stack/rfsStack/index.js +3 -1
  83. package/dist/src/stack/scfStack/cosExecutor.js +39 -1
  84. package/dist/src/stack/scfStack/cosPlanner.js +11 -1
  85. package/dist/src/stack/scfStack/cosResource.js +167 -64
  86. package/dist/src/stack/scfStack/deployer.js +2 -2
  87. package/dist/src/stack/scfStack/destroyer.js +2 -2
  88. package/dist/src/stack/scfStack/esServerlessExecutor.js +39 -1
  89. package/dist/src/stack/scfStack/esServerlessPlanner.js +11 -1
  90. package/dist/src/stack/scfStack/esServerlessResource.js +88 -23
  91. package/dist/src/stack/scfStack/scfExecutor.js +39 -1
  92. package/dist/src/stack/scfStack/scfPlanner.js +12 -2
  93. package/dist/src/stack/scfStack/scfResource.js +484 -14
  94. package/dist/src/stack/scfStack/tdsqlcExecutor.js +39 -1
  95. package/dist/src/stack/scfStack/tdsqlcPlanner.js +12 -1
  96. package/dist/src/stack/scfStack/tdsqlcResource.js +95 -16
  97. package/dist/src/stack/scfStack/tdsqlcTypes.js +5 -1
  98. package/dist/src/stack/volcengineStack/apigwExecutor.js +39 -1
  99. package/dist/src/stack/volcengineStack/apigwPlanner.js +25 -53
  100. package/dist/src/stack/volcengineStack/apigwResource.js +418 -222
  101. package/dist/src/stack/volcengineStack/apigwTypes.js +76 -66
  102. package/dist/src/stack/volcengineStack/deployer.js +49 -14
  103. package/dist/src/stack/volcengineStack/destroyer.js +1 -1
  104. package/dist/src/stack/volcengineStack/index.js +1 -2
  105. package/dist/src/stack/volcengineStack/tosExecutor.js +25 -2
  106. package/dist/src/stack/volcengineStack/tosPlanner.js +11 -1
  107. package/dist/src/stack/volcengineStack/tosResource.js +82 -27
  108. package/dist/src/stack/volcengineStack/tosTypes.js +12 -0
  109. package/dist/src/stack/volcengineStack/vefaasExecutor.js +25 -2
  110. package/dist/src/stack/volcengineStack/vefaasPlanner.js +71 -2
  111. package/dist/src/stack/volcengineStack/vefaasResource.js +148 -98
  112. package/dist/src/stack/volcengineStack/vefaasTypes.js +1 -3
  113. package/dist/src/types/domains/backend.js +1 -0
  114. package/dist/src/types/domains/state.js +29 -1
  115. package/dist/src/validator/eventSchema.js +10 -0
  116. package/dist/src/validator/functionSchema.js +37 -12
  117. package/dist/src/validator/iacSchema.js +1 -0
  118. package/dist/tsconfig.tsbuildinfo +1 -1
  119. package/package.json +3 -1
@@ -13,6 +13,7 @@ const constants_1 = require("./constants");
13
13
  const lang_1 = require("../lang");
14
14
  const logger_1 = require("./logger");
15
15
  class LockError extends Error {
16
+ lockInfo;
16
17
  constructor(message, lockInfo) {
17
18
  super(message);
18
19
  this.lockInfo = lockInfo;
@@ -20,12 +21,10 @@ class LockError extends Error {
20
21
  }
21
22
  }
22
23
  exports.LockError = LockError;
23
- /* istanbul ignore next */
24
24
  const getLockPath = (statePath) => {
25
25
  return `${statePath}${constants_1.LOCK_FILE_SUFFIX}`;
26
26
  };
27
27
  exports.getLockPath = getLockPath;
28
- /* istanbul ignore next */
29
28
  const generateLockId = () => {
30
29
  return node_crypto_1.default.randomBytes(16).toString('hex');
31
30
  };
@@ -71,13 +70,6 @@ const readLockFile = (lockPath) => {
71
70
  }
72
71
  return null;
73
72
  };
74
- const writeLockFile = (lockPath, metadata) => {
75
- const lockDir = node_path_1.default.dirname(lockPath);
76
- if (!node_fs_1.default.existsSync(lockDir)) {
77
- node_fs_1.default.mkdirSync(lockDir, { recursive: true });
78
- }
79
- node_fs_1.default.writeFileSync(lockPath, JSON.stringify(metadata, null, 2), 'utf-8');
80
- };
81
73
  const removeLockFile = (lockPath) => {
82
74
  if (node_fs_1.default.existsSync(lockPath)) {
83
75
  node_fs_1.default.unlinkSync(lockPath);
@@ -119,7 +111,6 @@ const getTimeAgo = (acquiredAt) => {
119
111
  }
120
112
  }
121
113
  };
122
- /* istanbul ignore next */
123
114
  const formatLockInfo = (lock) => {
124
115
  const acquiredAt = new Date(lock.acquiredAt);
125
116
  const timeAgo = getTimeAgo(acquiredAt);
@@ -137,7 +128,6 @@ exports.formatLockInfo = formatLockInfo;
137
128
  const sleep = (ms) => {
138
129
  return new Promise((resolve) => setTimeout(resolve, ms));
139
130
  };
140
- /* istanbul ignore next */
141
131
  const acquireLockInternal = async (statePath, operation, options = {}) => {
142
132
  const timeout = options.timeout ?? constants_1.DEFAULT_LOCK_TIMEOUT;
143
133
  const retryDelay = options.retryDelay ?? constants_1.DEFAULT_LOCK_RETRY_DELAY;
@@ -147,51 +137,71 @@ const acquireLockInternal = async (statePath, operation, options = {}) => {
147
137
  const startTime = Date.now();
148
138
  let attempt = 0;
149
139
  while (attempt <= maxRetries) {
150
- // Check if lock file exists
151
- const existingLock = readLockFile(lockPath);
152
- if (!existingLock) {
153
- // No lock exists, try to acquire
140
+ try {
141
+ const lockDir = node_path_1.default.dirname(lockPath);
142
+ if (!node_fs_1.default.existsSync(lockDir)) {
143
+ node_fs_1.default.mkdirSync(lockDir, { recursive: true });
144
+ }
145
+ // Atomic O_EXCL create: fails with EEXIST if another process holds the
146
+ // lock, so two processes can never both acquire.
147
+ let fd = null;
154
148
  try {
155
- writeLockFile(lockPath, metadata);
156
- // Verify we got the lock by reading it back
157
- const verifyLock = readLockFile(lockPath);
158
- if (verifyLock && verifyLock.id === metadata.id) {
159
- return metadata;
160
- }
149
+ fd = node_fs_1.default.openSync(lockPath, 'wx');
150
+ node_fs_1.default.writeFileSync(fd, JSON.stringify(metadata, null, 2), 'utf-8');
151
+ node_fs_1.default.fsyncSync(fd);
161
152
  }
162
- catch {
163
- // Failed to write lock, will retry
153
+ finally {
154
+ if (fd !== null) {
155
+ node_fs_1.default.closeSync(fd);
156
+ }
164
157
  }
158
+ return metadata;
165
159
  }
166
- else {
167
- if (existingLock.hostname === node_os_1.default.hostname() && !isProcessAlive(existingLock.processId)) {
168
- logger_1.logger.info(lang_1.lang.__('LOCK_AUTO_RELEASED_DEAD_PROCESS', {
169
- processId: String(existingLock.processId),
170
- hostname: existingLock.hostname,
160
+ catch (error) {
161
+ if (error.code !== 'EEXIST') {
162
+ // If we created the lock file but failed to finish writing it, remove
163
+ // the partial file so a later attempt can retry cleanly.
164
+ if (node_fs_1.default.existsSync(lockPath)) {
165
+ try {
166
+ node_fs_1.default.unlinkSync(lockPath);
167
+ }
168
+ catch {
169
+ // best-effort cleanup
170
+ }
171
+ }
172
+ throw error;
173
+ }
174
+ const existingLock = readLockFile(lockPath);
175
+ if (existingLock) {
176
+ if (existingLock.hostname === node_os_1.default.hostname() && !isProcessAlive(existingLock.processId)) {
177
+ logger_1.logger.info(lang_1.lang.__('LOCK_AUTO_RELEASED_DEAD_PROCESS', {
178
+ processId: String(existingLock.processId),
179
+ hostname: existingLock.hostname,
180
+ user: existingLock.user,
181
+ acquiredAt: existingLock.acquiredAt,
182
+ }));
183
+ removeLockFile(lockPath);
184
+ attempt++;
185
+ continue;
186
+ }
187
+ if (isLockStale(existingLock)) {
188
+ // Lock is stale, but we don't auto-remove it
189
+ // User must use force-unlock
190
+ throw new LockError(`State is currently locked (stale lock detected).\n${(0, exports.formatLockInfo)(existingLock)}\nThis lock appears to be stale. If you are certain no other operation is running, use:\n si force-unlock ${existingLock.id}`, existingLock);
191
+ }
192
+ // Check if timeout exceeded
193
+ if (Date.now() - startTime >= timeout) {
194
+ throw new LockError(`Failed to acquire lock after ${timeout / 1000} seconds.\n${(0, exports.formatLockInfo)(existingLock)}\nIf you are certain no other operation is running, use:\n si force-unlock ${existingLock.id}`, existingLock);
195
+ }
196
+ const acquiredAt = new Date(existingLock.acquiredAt);
197
+ const minutesAgo = Math.floor((Date.now() - acquiredAt.getTime()) / 60000);
198
+ const timeAgo = minutesAgo < 1 ? lang_1.lang.__('LOCK_TIME_AGO_LESS_THAN_MINUTE') : `${minutesAgo}m`;
199
+ logger_1.logger.info(lang_1.lang.__('LOCK_WAITING', {
171
200
  user: existingLock.user,
172
- acquiredAt: existingLock.acquiredAt,
201
+ timeAgo,
202
+ attempt: String(attempt + 1),
173
203
  }));
174
- removeLockFile(lockPath);
175
- attempt++;
176
- continue;
177
- }
178
- if (isLockStale(existingLock)) {
179
- // Lock is stale, but we don't auto-remove it
180
- // User must use force-unlock
181
- throw new LockError(`State is currently locked (stale lock detected).\n${(0, exports.formatLockInfo)(existingLock)}\nThis lock appears to be stale. If you are certain no other operation is running, use:\n si force-unlock ${existingLock.id}`, existingLock);
182
- }
183
- // Check if timeout exceeded
184
- if (Date.now() - startTime >= timeout) {
185
- throw new LockError(`Failed to acquire lock after ${timeout / 1000} seconds.\n${(0, exports.formatLockInfo)(existingLock)}\nIf you are certain no other operation is running, use:\n si force-unlock ${existingLock.id}`, existingLock);
186
204
  }
187
- const acquiredAt = new Date(existingLock.acquiredAt);
188
- const minutesAgo = Math.floor((Date.now() - acquiredAt.getTime()) / 60000);
189
- const timeAgo = minutesAgo < 1 ? lang_1.lang.__('LOCK_TIME_AGO_LESS_THAN_MINUTE') : `${minutesAgo}m`;
190
- logger_1.logger.info(lang_1.lang.__('LOCK_WAITING', {
191
- user: existingLock.user,
192
- timeAgo,
193
- attempt: String(attempt + 1),
194
- }));
195
205
  }
196
206
  // Wait before retrying with exponential backoff
197
207
  const delay = Math.min(retryDelay * Math.pow(2, attempt), 30000); // Cap at 30 seconds
@@ -203,7 +213,6 @@ const acquireLockInternal = async (statePath, operation, options = {}) => {
203
213
  throw new LockError(`Failed to acquire lock.\n${existingLock ? (0, exports.formatLockInfo)(existingLock) : ''}\nIf you are certain no other operation is running, use:\n si force-unlock ${existingLock?.id || 'unknown'}`, existingLock || undefined);
204
214
  };
205
215
  exports.acquireLockInternal = acquireLockInternal;
206
- /* istanbul ignore next */
207
216
  const releaseLockInternal = (statePath, lockId) => {
208
217
  const lockPath = (0, exports.getLockPath)(statePath);
209
218
  const existingLock = readLockFile(lockPath);
@@ -213,7 +222,6 @@ const releaseLockInternal = (statePath, lockId) => {
213
222
  }
214
223
  };
215
224
  exports.releaseLockInternal = releaseLockInternal;
216
- /* istanbul ignore next */
217
225
  const forceUnlock = (statePath, lockId) => {
218
226
  const lockPath = (0, exports.getLockPath)(statePath);
219
227
  const existingLock = readLockFile(lockPath);
@@ -227,11 +235,11 @@ const forceUnlock = (statePath, lockId) => {
227
235
  return true;
228
236
  };
229
237
  exports.forceUnlock = forceUnlock;
230
- /* istanbul ignore next */
231
- const withLock = async (statePath, operation, fn, options) => {
238
+ const withLock = async (statePath, operation, fn, options, onLockAcquired) => {
232
239
  let lock = null;
233
240
  try {
234
241
  lock = await (0, exports.acquireLockInternal)(statePath, operation, options);
242
+ onLockAcquired?.(lock.id);
235
243
  return await fn();
236
244
  }
237
245
  finally {
@@ -242,7 +250,6 @@ const withLock = async (statePath, operation, fn, options) => {
242
250
  };
243
251
  exports.withLock = withLock;
244
252
  // Export only for forceUnlock command which needs to read lock info
245
- /* istanbul ignore next */
246
253
  const readLockFileForCommand = (statePath) => {
247
254
  const lockPath = (0, exports.getLockPath)(statePath);
248
255
  return readLockFile(lockPath);
@@ -65,6 +65,8 @@ const getSystemEncoding = () => {
65
65
  return 'utf8';
66
66
  };
67
67
  class EncodingTransformStream extends stream_1.Writable {
68
+ encoding;
69
+ formatLog;
68
70
  constructor(encoding) {
69
71
  super();
70
72
  this.encoding = encoding;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pollUntil = exports.PollingTimeoutError = void 0;
4
+ const retryUtils_1 = require("./retryUtils");
5
+ class PollingTimeoutError extends Error {
6
+ ctx;
7
+ constructor(ctx) {
8
+ super(`Polling timed out: ${ctx.description} after ${ctx.attempts} attempts ` +
9
+ `× ${ctx.intervalMs}ms (last value: ${ctx.lastValue === undefined ? 'undefined' : JSON.stringify(ctx.lastValue)})`);
10
+ this.ctx = ctx;
11
+ this.name = 'PollingTimeoutError';
12
+ }
13
+ }
14
+ exports.PollingTimeoutError = PollingTimeoutError;
15
+ /**
16
+ * Generic polling primitive — the single wait implementation for async cloud
17
+ * resource creation (mirrors Terraform's StateChangeConf / Tencent Cloud's
18
+ * waitScfFunctionReady pattern).
19
+ *
20
+ * - fetch errors propagate immediately (callers detect error states by throwing
21
+ * from their fetch wrapper)
22
+ * - on timeout, throws PollingTimeoutError carrying the last observed value
23
+ * - requiredConsecutiveHits guards against eventually-consistent APIs returning
24
+ * stale state (the done state must be observed N times in a row)
25
+ */
26
+ const pollUntil = async (options) => {
27
+ const { description, fetch, isDone, intervalMs, maxAttempts, onProgress, requiredConsecutiveHits = 1, } = options;
28
+ let consecutiveHits = 0;
29
+ let lastValue = null;
30
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
31
+ lastValue = await fetch();
32
+ onProgress?.(lastValue, attempt, maxAttempts);
33
+ if (isDone(lastValue)) {
34
+ consecutiveHits++;
35
+ if (consecutiveHits >= requiredConsecutiveHits) {
36
+ return lastValue;
37
+ }
38
+ }
39
+ else {
40
+ consecutiveHits = 0;
41
+ }
42
+ if (attempt < maxAttempts) {
43
+ await (0, retryUtils_1.sleep)(intervalMs);
44
+ }
45
+ }
46
+ throw new PollingTimeoutError({
47
+ description,
48
+ lastValue,
49
+ attempts: maxAttempts,
50
+ maxAttempts,
51
+ intervalMs,
52
+ });
53
+ };
54
+ exports.pollUntil = pollUntil;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.reportResourceEvent = void 0;
4
+ /** ADR-005: emit a resource lifecycle event via context.reportEvent (no-op if unwired). */
5
+ const reportResourceEvent = (context, event) => {
6
+ context.reportEvent?.(event);
7
+ };
8
+ exports.reportResourceEvent = reportResourceEvent;
@@ -144,20 +144,19 @@ const mapRuntime = (standardRuntime, provider) => {
144
144
  return providerRuntime;
145
145
  };
146
146
  exports.mapRuntime = mapRuntime;
147
+ // Valid veFaaS runtimes per the official CreateFunction API reference:
148
+ // golang/v1, native/v1, nativejava8/v1, node14/v1, node20/v1, nodeprime14/v1,
149
+ // python3.12/v1, python3.8/v1, python3.9/v1
147
150
  const VOLCENGINE_NATIVE_RUNTIMES = [
148
- 'nodejs/v20',
149
- 'nodejs/v18',
150
- 'nodejs/v16',
151
- 'nodejs/v14',
152
- 'python/v3.12',
153
- 'python/v3.11',
154
- 'python/v3.10',
155
- 'python/v3.9',
156
151
  'golang/v1',
157
- 'java/v21',
158
- 'java/v17',
159
- 'java/v11',
160
- 'java/v8',
152
+ 'native/v1',
153
+ 'nativejava8/v1',
154
+ 'node14/v1',
155
+ 'node20/v1',
156
+ 'nodeprime14/v1',
157
+ 'python3.12/v1',
158
+ 'python3.8/v1',
159
+ 'python3.9/v1',
161
160
  ];
162
161
  const isRuntimeSupported = (runtime, provider) => {
163
162
  if (provider === providerEnum_1.ProviderEnum.VOLCENGINE) {
@@ -20,14 +20,23 @@ const providerEnum_1 = require("../providerEnum");
20
20
  const localStateBackend_1 = require("./localStateBackend");
21
21
  const ossStateBackend_1 = require("./ossStateBackend");
22
22
  const cosStateBackend_1 = require("./cosStateBackend");
23
+ const saasStateBackend_1 = require("./saasStateBackend");
23
24
  __exportStar(require("./types"), exports);
24
25
  __exportStar(require("./localStateBackend"), exports);
25
26
  __exportStar(require("./ossStateBackend"), exports);
26
27
  __exportStar(require("./cosStateBackend"), exports);
27
28
  __exportStar(require("./remoteStateBackend"), exports);
29
+ __exportStar(require("./saasStateBackend"), exports);
28
30
  __exportStar(require("./lockUtils"), exports);
29
31
  const createStateBackend = (backendConfig, context) => {
30
- if (!backendConfig || backendConfig.type === types_1.StateBackendType.LOCAL) {
32
+ // SaaS (default) — requires API key
33
+ if (!backendConfig || backendConfig.type === types_1.StateBackendType.SAAS) {
34
+ return (0, saasStateBackend_1.createSaasStateBackend)({
35
+ app: context.app,
36
+ service: context.service,
37
+ });
38
+ }
39
+ if (backendConfig.type === types_1.StateBackendType.LOCAL) {
31
40
  return (0, localStateBackend_1.createLocalStateBackend)(context.app, context.service, context.baseDir);
32
41
  }
33
42
  const bucketConfig = backendConfig;
@@ -25,7 +25,10 @@ const createLocalStateBackend = (app, service, baseDir = process.cwd()) => {
25
25
  readLock: async () => {
26
26
  return (0, lockManager_1.readLockFileForCommand)(statePath);
27
27
  },
28
- withLock: async (operation, fn, options) => {
28
+ withLock: async (operation, fn, options, onLockAcquired) => {
29
+ if (onLockAcquired) {
30
+ return (0, lockManager_1.withLock)(statePath, operation, fn, options, onLockAcquired);
31
+ }
29
32
  return (0, lockManager_1.withLock)(statePath, operation, fn, options);
30
33
  },
31
34
  };
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.createRemoteStateBackend = void 0;
7
7
  const node_os_1 = __importDefault(require("node:os"));
8
8
  const types_1 = require("../../types");
9
+ const stateManager_1 = require("../stateManager");
9
10
  const constants_1 = require("../constants");
10
11
  const lockManager_1 = require("../lockManager");
11
12
  const logger_1 = require("../logger");
@@ -70,8 +71,9 @@ const createRemoteStateBackend = (adapter, config) => {
70
71
  loadState: async (provider, app, service, stage) => {
71
72
  const state = await adapter.read(config.key);
72
73
  if (state) {
73
- const stageResources = state.stages?.[stage]?.resources ?? {};
74
- return { ...state, resources: stageResources };
74
+ const migrated = (0, stateManager_1.migrateState)(state);
75
+ const stageResources = migrated.stages?.[stage]?.resources ?? {};
76
+ return { ...migrated, resources: stageResources };
75
77
  }
76
78
  return { version: types_1.CURRENT_STATE_VERSION, provider, app, service, stages: {}, resources: {} };
77
79
  },
@@ -129,11 +131,12 @@ const createRemoteStateBackend = (adapter, config) => {
129
131
  readLock: async () => {
130
132
  return readLockObject();
131
133
  },
132
- withLock: async (operation, fn, options) => {
134
+ withLock: async (operation, fn, options, onLockAcquired) => {
133
135
  let lockId = null;
134
136
  try {
135
137
  const meta = await acquireLockObject(operation, options);
136
138
  lockId = meta.id;
139
+ onLockAcquired?.(lockId);
137
140
  return await fn();
138
141
  }
139
142
  finally {
@@ -0,0 +1,266 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createSaasStateBackend = void 0;
7
+ const apiClient_1 = require("../apiClient");
8
+ const credentialStore_1 = require("../credentialStore");
9
+ const types_1 = require("../../types");
10
+ const stateManager_1 = require("../stateManager");
11
+ const eventQueue_1 = require("../eventQueue");
12
+ const lang_1 = require("../../lang");
13
+ const node_crypto_1 = __importDefault(require("node:crypto"));
14
+ const getDefaultState = (provider, app, service) => ({
15
+ version: types_1.CURRENT_STATE_VERSION,
16
+ provider,
17
+ app,
18
+ service,
19
+ stages: {},
20
+ resources: {},
21
+ });
22
+ const createSaasStateBackend = (context) => {
23
+ const creds = (0, credentialStore_1.loadCredentials)();
24
+ const apiKey = creds?.apiKey;
25
+ const consoleUrl = (0, credentialStore_1.getConsoleUrl)();
26
+ const orgId = creds?.orgId ?? '';
27
+ if (!apiKey) {
28
+ throw new Error(lang_1.lang.__('SAAS_BACKEND_NO_CREDENTIALS'));
29
+ }
30
+ const client = (0, apiClient_1.createApiClient)({
31
+ apiKey,
32
+ baseUrl: consoleUrl,
33
+ orgId,
34
+ });
35
+ let currentDeploymentId = null;
36
+ let currentStage = null;
37
+ let resolvedAppId = null;
38
+ let resolvedServiceId = null;
39
+ let provisioned = false;
40
+ let eventQueue = null;
41
+ let sequenceCounter = 0;
42
+ /** ADR-005: enqueue a typed event for the active deployment (durable JSONL + batch flush). */
43
+ const reportEvent = (event) => {
44
+ ensureEventQueue();
45
+ if (!eventQueue || !currentDeploymentId)
46
+ return;
47
+ sequenceCounter += 1;
48
+ eventQueue.report({
49
+ ...event,
50
+ deploymentId: currentDeploymentId,
51
+ sequence: event.sequence ?? sequenceCounter,
52
+ });
53
+ };
54
+ const ensureEventQueue = () => {
55
+ if (eventQueue || !currentDeploymentId)
56
+ return;
57
+ eventQueue = new eventQueue_1.EventQueue({
58
+ deploymentId: currentDeploymentId,
59
+ sendBatch: async (events) => {
60
+ await client.patch(`/api/v1/deployments/${currentDeploymentId}`, {
61
+ phase: 'event',
62
+ events,
63
+ ...(resolvedAppId ? { appId: resolvedAppId } : {}),
64
+ });
65
+ },
66
+ flushOnExit: true,
67
+ });
68
+ };
69
+ /**
70
+ * Provision the deployment by calling the unified POST endpoint.
71
+ * This resolves YAML names to Console UUIDs and caches them.
72
+ */
73
+ const provision = async (provider, stage) => {
74
+ const result = await client.post('/api/v1/deployments/', {
75
+ appName: context.app,
76
+ serviceName: context.service,
77
+ provider,
78
+ stage,
79
+ spec: { operation: 'init' },
80
+ source: 'cli',
81
+ });
82
+ resolvedAppId = result.appId;
83
+ resolvedServiceId = result.serviceId;
84
+ currentDeploymentId = result.id;
85
+ provisioned = true;
86
+ };
87
+ /**
88
+ * Ensure the backend is provisioned — calls provision() if not already done.
89
+ */
90
+ const ensureProvisioned = async (provider, stage) => {
91
+ if (!provisioned) {
92
+ await provision(provider, stage);
93
+ }
94
+ };
95
+ return {
96
+ loadState: async (provider, app, service, stage) => {
97
+ currentStage = stage;
98
+ await ensureProvisioned(provider, stage);
99
+ try {
100
+ const state = await client.get(`/api/v1/apps/${resolvedAppId}/services/${resolvedServiceId}/state/current?stage=${encodeURIComponent(stage)}`);
101
+ // Attach console metadata so subsequent deploys have the UUIDs
102
+ return {
103
+ ...(0, stateManager_1.migrateState)(state.stateJson),
104
+ orgId,
105
+ appId: resolvedAppId,
106
+ serviceId: resolvedServiceId,
107
+ };
108
+ }
109
+ catch {
110
+ const defaultState = getDefaultState(provider, app, service);
111
+ return {
112
+ ...defaultState,
113
+ orgId,
114
+ appId: resolvedAppId,
115
+ serviceId: resolvedServiceId,
116
+ };
117
+ }
118
+ },
119
+ saveState: async (state, app, service, stage) => {
120
+ // If not provisioned yet, use the state's provider for provisioning
121
+ await ensureProvisioned(state.provider, stage);
122
+ // The backend persists the full state file under state_json — send the whole
123
+ // StateFile (not just resources) so loadState round-trips it unchanged.
124
+ const body = {
125
+ appName: app,
126
+ serviceName: service,
127
+ provider: state.provider,
128
+ stage,
129
+ stateJson: state,
130
+ contentHash: node_crypto_1.default.createHash('sha256').update(JSON.stringify(state)).digest('hex'),
131
+ resourceCount: Object.keys(state.resources).length,
132
+ };
133
+ await client.post(`/api/v1/apps/${resolvedAppId}/services/${resolvedServiceId}/state/sync`, body);
134
+ },
135
+ acquireLock: async (_operation, _options) => {
136
+ return 'saas-lock-placeholder';
137
+ },
138
+ releaseLock: async (_lockId) => {
139
+ // Lock is managed by deployment lifecycle in withLock
140
+ },
141
+ forceUnlock: async (_lockId) => {
142
+ throw new Error(lang_1.lang.__('SAAS_FORCE_UNLOCK_NOT_SUPPORTED', {
143
+ consoleUrl,
144
+ }));
145
+ },
146
+ readLock: async () => {
147
+ if (!resolvedServiceId) {
148
+ return null;
149
+ }
150
+ try {
151
+ const active = await client.get(`/api/v1/deployments/active?service_id=${resolvedServiceId}&stage=${currentStage ?? ''}`);
152
+ // Backend returns data:null (not []) when no deployment is active
153
+ if (Array.isArray(active) && active.length > 0) {
154
+ const d = active[0];
155
+ return {
156
+ id: d.id,
157
+ user: 'Console',
158
+ processId: 0,
159
+ hostname: 'console',
160
+ operation: 'deploy',
161
+ acquiredAt: new Date().toISOString(),
162
+ path: `/api/v1/deployments/${d.id}`,
163
+ };
164
+ }
165
+ return null;
166
+ }
167
+ catch {
168
+ return null;
169
+ }
170
+ },
171
+ withLock: async (operation, fn, _options, onLockAcquired) => {
172
+ if (!currentStage) {
173
+ throw new Error('SaaS state backend: call loadState() first to set stage');
174
+ }
175
+ if (!currentDeploymentId) {
176
+ throw new Error('SaaS state backend: no deployment available, call loadState() first');
177
+ }
178
+ // Acquire lock via phase:start (server checks for 409 conflicts)
179
+ await client.patch(`/api/v1/deployments/${currentDeploymentId}`, { phase: 'start' });
180
+ onLockAcquired?.(currentDeploymentId);
181
+ ensureEventQueue();
182
+ reportEvent({ type: 'deployment_started', message: `Deployment ${operation} started` });
183
+ try {
184
+ const result = await fn();
185
+ // If the caller returned a deployment summary ({ stateJson, plan,
186
+ // contentHash, resourceCount }), forward it so the console persists the
187
+ // plan + linked infra state (Changes / State JSON on deployment detail).
188
+ // Otherwise fall back to the legacy contract (result = fn's value).
189
+ const summary = (result ?? {});
190
+ const isSummary = typeof result === 'object' &&
191
+ result !== null &&
192
+ ('stateJson' in summary || 'plan' in summary);
193
+ await client.patch(`/api/v1/deployments/${currentDeploymentId}`, {
194
+ phase: 'complete',
195
+ result: isSummary ? (summary['result'] ?? null) : result,
196
+ ...(isSummary
197
+ ? {
198
+ stateJson: summary['stateJson'],
199
+ contentHash: summary['contentHash'],
200
+ resourceCount: summary['resourceCount'],
201
+ plan: summary['plan'],
202
+ }
203
+ : {}),
204
+ });
205
+ reportEvent({ type: 'deployment_completed', message: `Deployment ${operation} completed` });
206
+ await eventQueue?.flushOnExit();
207
+ return result;
208
+ }
209
+ catch (err) {
210
+ // Fail with error. If the caller attached plan/stateJson to the error
211
+ // (see commands/deploy.ts), forward them so the console can render
212
+ // Changes + State JSON even for a failed deployment.
213
+ const failure = err;
214
+ try {
215
+ const payload = {
216
+ phase: 'fail',
217
+ error: { message: err instanceof Error ? err.message : String(err) },
218
+ ...(failure.plan ? { plan: failure.plan } : {}),
219
+ };
220
+ if (failure.stateJson) {
221
+ payload['stateJson'] = failure.stateJson;
222
+ payload['contentHash'] = node_crypto_1.default
223
+ .createHash('sha256')
224
+ .update(JSON.stringify(failure.stateJson))
225
+ .digest('hex');
226
+ payload['resourceCount'] = Object.keys(failure.stateJson.resources ?? {}).length;
227
+ }
228
+ await client.patch(`/api/v1/deployments/${currentDeploymentId}`, payload);
229
+ }
230
+ catch {
231
+ // Best-effort fail notification
232
+ }
233
+ reportEvent({
234
+ type: 'deployment_failed',
235
+ message: `Deployment ${operation} failed`,
236
+ error: { message: err instanceof Error ? err.message : String(err) },
237
+ severity: 'error',
238
+ });
239
+ await eventQueue?.flushOnExit();
240
+ throw err;
241
+ }
242
+ },
243
+ reportEvent,
244
+ flushEvents: async () => {
245
+ await eventQueue?.flushOnExit();
246
+ },
247
+ replayOrphanedEvents: async () => {
248
+ // Replays queue files from ANY previous run — each file carries its own
249
+ // deploymentId, and sendBatch targets the deployment embedded in the event.
250
+ const replayQueue = new eventQueue_1.EventQueue({
251
+ deploymentId: 'replay',
252
+ sendBatch: async (events) => {
253
+ const deploymentId = events[0]?.deploymentId;
254
+ if (!deploymentId)
255
+ return;
256
+ await client.patch(`/api/v1/deployments/${deploymentId}`, {
257
+ phase: 'event',
258
+ events,
259
+ });
260
+ },
261
+ });
262
+ await replayQueue.replayOrphanedQueues();
263
+ },
264
+ };
265
+ };
266
+ exports.createSaasStateBackend = createSaasStateBackend;