@geek-fun/serverlessinsight 0.8.2 → 0.8.4

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 (124) hide show
  1. package/.serverlessinsight/state-cdn-flow-app-cdn-flow-service.json.backup +15 -0
  2. package/.serverlessinsight/state-iam-test-app-iam-test-service.json.backup +33 -0
  3. package/.serverlessinsight/state-insight-poc-deploy-app-insight-poc-deploy.json.backup +359 -0
  4. package/.serverlessinsight/state-insight-poc-destroy-app-insight-poc-destroy.json.backup +15 -0
  5. package/.serverlessinsight/state-insight-volc-app-insight-volc.json.backup +154 -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 +5 -2
  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 +363 -30
  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 +90 -28
  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 +13 -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 +10 -4
  41. package/dist/src/common/stateBackend/saasStateBackend.js +266 -0
  42. package/dist/src/common/stateManager.js +186 -14
  43. package/dist/src/common/tencentClient/clsOperations.js +137 -0
  44. package/dist/src/common/tencentClient/cosOperations.js +180 -3
  45. package/dist/src/common/tencentClient/esOperations.js +110 -46
  46. package/dist/src/common/tencentClient/index.js +43 -2
  47. package/dist/src/common/tencentClient/scfOperations.js +266 -133
  48. package/dist/src/common/tencentClient/tdsqlcClusterMapper.js +81 -0
  49. package/dist/src/common/tencentClient/tdsqlcOperations.js +93 -99
  50. package/dist/src/common/triggerMapper.js +36 -0
  51. package/dist/src/common/volcengineClient/apigwOperations.js +539 -159
  52. package/dist/src/common/volcengineClient/iamOperations.js +91 -48
  53. package/dist/src/common/volcengineClient/index.js +46 -21
  54. package/dist/src/common/volcengineClient/stsOperations.js +32 -0
  55. package/dist/src/common/volcengineClient/tlsOperations.js +305 -171
  56. package/dist/src/common/volcengineClient/tosOperations.js +78 -19
  57. package/dist/src/common/volcengineClient/vefaasOperations.js +272 -54
  58. package/dist/src/lang/en.js +107 -1
  59. package/dist/src/lang/zh-CN.js +107 -1
  60. package/dist/src/parser/eventParser.js +2 -0
  61. package/dist/src/parser/functionParser.js +96 -65
  62. package/dist/src/parser/index.js +7 -4
  63. package/dist/src/stack/aliyunStack/apigwExecutor.js +39 -1
  64. package/dist/src/stack/aliyunStack/apigwPlanner.js +16 -10
  65. package/dist/src/stack/aliyunStack/apigwResource.js +334 -125
  66. package/dist/src/stack/aliyunStack/apigwTypes.js +35 -4
  67. package/dist/src/stack/aliyunStack/databaseExecutor.js +39 -1
  68. package/dist/src/stack/aliyunStack/databasePlanner.js +13 -1
  69. package/dist/src/stack/aliyunStack/databaseResource.js +135 -40
  70. package/dist/src/stack/aliyunStack/deployer.js +2 -2
  71. package/dist/src/stack/aliyunStack/destroyer.js +27 -2
  72. package/dist/src/stack/aliyunStack/fc3Executor.js +25 -2
  73. package/dist/src/stack/aliyunStack/fc3Planner.js +11 -1
  74. package/dist/src/stack/aliyunStack/fc3Resource.js +416 -94
  75. package/dist/src/stack/aliyunStack/ossExecutor.js +39 -1
  76. package/dist/src/stack/aliyunStack/ossPlanner.js +15 -1
  77. package/dist/src/stack/aliyunStack/ossResource.js +267 -219
  78. package/dist/src/stack/aliyunStack/sharedLogProject.js +137 -0
  79. package/dist/src/stack/aliyunStack/tablestoreExecutor.js +39 -1
  80. package/dist/src/stack/aliyunStack/tablestorePlanner.js +13 -1
  81. package/dist/src/stack/aliyunStack/tablestoreResource.js +54 -20
  82. package/dist/src/stack/alreadyExists.js +26 -0
  83. package/dist/src/stack/ownershipTag.js +58 -0
  84. package/dist/src/stack/rfsStack/index.js +3 -1
  85. package/dist/src/stack/scfStack/cosExecutor.js +39 -1
  86. package/dist/src/stack/scfStack/cosPlanner.js +11 -1
  87. package/dist/src/stack/scfStack/cosResource.js +167 -64
  88. package/dist/src/stack/scfStack/deployer.js +2 -2
  89. package/dist/src/stack/scfStack/destroyer.js +15 -2
  90. package/dist/src/stack/scfStack/esServerlessExecutor.js +39 -1
  91. package/dist/src/stack/scfStack/esServerlessPlanner.js +11 -1
  92. package/dist/src/stack/scfStack/esServerlessResource.js +88 -23
  93. package/dist/src/stack/scfStack/scfExecutor.js +39 -1
  94. package/dist/src/stack/scfStack/scfPlanner.js +21 -3
  95. package/dist/src/stack/scfStack/scfResource.js +686 -24
  96. package/dist/src/stack/scfStack/scfTypes.js +6 -0
  97. package/dist/src/stack/scfStack/sharedLogset.js +126 -0
  98. package/dist/src/stack/scfStack/tdsqlcExecutor.js +39 -1
  99. package/dist/src/stack/scfStack/tdsqlcPlanner.js +12 -1
  100. package/dist/src/stack/scfStack/tdsqlcResource.js +95 -16
  101. package/dist/src/stack/scfStack/tdsqlcTypes.js +5 -1
  102. package/dist/src/stack/volcengineStack/apigwExecutor.js +39 -1
  103. package/dist/src/stack/volcengineStack/apigwPlanner.js +25 -53
  104. package/dist/src/stack/volcengineStack/apigwResource.js +434 -222
  105. package/dist/src/stack/volcengineStack/apigwTypes.js +85 -66
  106. package/dist/src/stack/volcengineStack/deployer.js +49 -14
  107. package/dist/src/stack/volcengineStack/destroyer.js +13 -1
  108. package/dist/src/stack/volcengineStack/index.js +1 -2
  109. package/dist/src/stack/volcengineStack/sharedLogProject.js +190 -0
  110. package/dist/src/stack/volcengineStack/tosExecutor.js +25 -2
  111. package/dist/src/stack/volcengineStack/tosPlanner.js +11 -1
  112. package/dist/src/stack/volcengineStack/tosResource.js +82 -27
  113. package/dist/src/stack/volcengineStack/tosTypes.js +12 -0
  114. package/dist/src/stack/volcengineStack/vefaasExecutor.js +25 -2
  115. package/dist/src/stack/volcengineStack/vefaasPlanner.js +78 -2
  116. package/dist/src/stack/volcengineStack/vefaasResource.js +194 -144
  117. package/dist/src/stack/volcengineStack/vefaasTypes.js +1 -3
  118. package/dist/src/types/domains/backend.js +1 -0
  119. package/dist/src/types/domains/state.js +32 -1
  120. package/dist/src/validator/eventSchema.js +10 -0
  121. package/dist/src/validator/functionSchema.js +37 -12
  122. package/dist/src/validator/iacSchema.js +1 -0
  123. package/dist/tsconfig.tsbuildinfo +1 -1
  124. package/package.json +5 -2
@@ -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;
@@ -3,11 +3,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.getRoleArnFromState = exports.getAllResources = exports.removeResource = exports.setResource = exports.getResource = exports.saveStateWithLock = exports.saveState = exports.loadState = exports.ensureStateDir = exports.getStatePath = void 0;
6
+ exports.getRoleArnFromState = exports.removeSharedResource = exports.setSharedResource = exports.getSharedResource = exports.getAllSharedResources = exports.getAllResources = exports.removeResource = exports.setResource = exports.getResource = exports.saveStateWithLock = exports.saveState = exports.loadState = exports.migrateState = exports.clearStateMigrations = exports.registerStateMigration = exports.ensureStateDir = exports.getStatePath = void 0;
7
7
  const node_fs_1 = __importDefault(require("node:fs"));
8
8
  const node_path_1 = __importDefault(require("node:path"));
9
+ const node_crypto_1 = __importDefault(require("node:crypto"));
9
10
  const types_1 = require("../types");
10
11
  const lockManager_1 = require("./lockManager");
12
+ const logger_1 = require("./logger");
11
13
  const STATE_DIR = '.serverlessinsight';
12
14
  /* istanbul ignore next */
13
15
  const getStatePath = (app, service, baseDir = process.cwd()) => {
@@ -22,32 +24,125 @@ const ensureStateDir = (baseDir = process.cwd()) => {
22
24
  }
23
25
  };
24
26
  exports.ensureStateDir = ensureStateDir;
27
+ const STATE_MIGRATIONS = [];
28
+ /**
29
+ * Register a migration from `fromVersion` to a newer format. Applied in
30
+ * registration order during `loadState`; empty today because CURRENT_STATE_VERSION
31
+ * ('3.0') is the only format this CLI writes so far.
32
+ */
33
+ const registerStateMigration = (fromVersion, migrate) => {
34
+ STATE_MIGRATIONS.push({ fromVersion, migrate });
35
+ };
36
+ exports.registerStateMigration = registerStateMigration;
37
+ /** Drop all registered migrations (test/cleanup helper). */
38
+ const clearStateMigrations = () => {
39
+ STATE_MIGRATIONS.length = 0;
40
+ };
41
+ exports.clearStateMigrations = clearStateMigrations;
42
+ /**
43
+ * Parse a state version string ("3.0", "1.0.0") into a comparable number.
44
+ * Versions are treated as legacy-format (major.minor[.patch]); a missing
45
+ * minor/patch defaults to 0. Non-numeric segments are ignored.
46
+ */
47
+ const parseStateVersion = (version) => {
48
+ const [major, minor] = version.split('.').map((part) => parseInt(part, 10));
49
+ return (isNaN(major) ? 0 : major) * 1000 + (isNaN(minor) ? 0 : minor);
50
+ };
51
+ /**
52
+ * Apply registered migrations to bring `raw` up to CURRENT_STATE_VERSION, then
53
+ * guard against versions that cannot reach it. A state written by a NEWER CLI
54
+ * must not be silently read (downgraded) — that risks data loss — so it is
55
+ * rejected instead. Older/legacy versions without a registered migration load
56
+ * as-is (they predate the migration registry).
57
+ */
58
+ const migrateState = (raw) => {
59
+ if (!raw.version) {
60
+ logger_1.logger.debug({ stateVersion: raw.version }, 'state file has no version field; treating as legacy');
61
+ return raw;
62
+ }
63
+ if (raw.version === types_1.CURRENT_STATE_VERSION) {
64
+ return raw;
65
+ }
66
+ let migrated = raw;
67
+ let hops = 0;
68
+ while (migrated.version !== types_1.CURRENT_STATE_VERSION && hops < STATE_MIGRATIONS.length) {
69
+ const entry = STATE_MIGRATIONS.find((m) => m.fromVersion === migrated.version);
70
+ if (!entry) {
71
+ break;
72
+ }
73
+ migrated = entry.migrate(migrated);
74
+ hops++;
75
+ }
76
+ const migratedVersion = String(migrated.version ?? '');
77
+ if (migratedVersion && migratedVersion !== types_1.CURRENT_STATE_VERSION) {
78
+ // A registered migration ran but did not land on CURRENT — refuse rather
79
+ // than persist a half-migrated state.
80
+ if (hops > 0) {
81
+ throw new types_1.StateVersionError(migratedVersion, types_1.CURRENT_STATE_VERSION);
82
+ }
83
+ // No migration was applicable. Only reject versions NEWER than CURRENT
84
+ // (written by a newer CLI, or an unknown future format). Older legacy
85
+ // versions load as-is.
86
+ if (parseStateVersion(migratedVersion) > parseStateVersion(types_1.CURRENT_STATE_VERSION)) {
87
+ throw new types_1.StateVersionError(migratedVersion, types_1.CURRENT_STATE_VERSION);
88
+ }
89
+ }
90
+ return migrated;
91
+ };
92
+ exports.migrateState = migrateState;
25
93
  /**
26
94
  * Load state file, scoped to the given stage.
27
95
  * The returned StateFile has `resources` populated from `stages[stage].resources`.
96
+ * A corrupt primary is recovered from `statePath + '.backup'`; if both are corrupt,
97
+ * StateCorruptError is thrown instead of silently returning empty state.
98
+ * Registered version migrations are applied on load; a state version that cannot
99
+ * be migrated to CURRENT_STATE_VERSION throws StateVersionError.
28
100
  */
29
- /* istanbul ignore next */
30
101
  const loadState = (provider, app, service, stage, baseDir = process.cwd()) => {
31
102
  const statePath = (0, exports.getStatePath)(app, service, baseDir);
32
- try {
33
- if (node_fs_1.default.existsSync(statePath)) {
34
- const content = node_fs_1.default.readFileSync(statePath, 'utf-8');
35
- const raw = JSON.parse(content);
36
- // Populate in-memory resources from the stage slice
37
- const stageResources = raw.stages?.[stage]?.resources ?? {};
38
- return { ...raw, resources: stageResources };
103
+ if (!node_fs_1.default.existsSync(statePath)) {
104
+ return { version: types_1.CURRENT_STATE_VERSION, provider, app, service, stages: {}, resources: {} };
105
+ }
106
+ const readBackup = () => {
107
+ const backupPath = `${statePath}.backup`;
108
+ if (!node_fs_1.default.existsSync(backupPath)) {
109
+ return null;
110
+ }
111
+ try {
112
+ const content = node_fs_1.default.readFileSync(backupPath, 'utf-8');
113
+ return JSON.parse(content);
39
114
  }
115
+ catch {
116
+ return null;
117
+ }
118
+ };
119
+ const hydrate = (raw) => {
120
+ const migrated = (0, exports.migrateState)(raw);
121
+ const stageResources = migrated.stages?.[stage]?.resources ?? {};
122
+ return { ...migrated, resources: stageResources };
123
+ };
124
+ try {
125
+ const content = node_fs_1.default.readFileSync(statePath, 'utf-8');
126
+ return hydrate(JSON.parse(content));
40
127
  }
41
- catch {
42
- // Ignore error, fall through to empty state
128
+ catch (error) {
129
+ if (error instanceof types_1.StateVersionError) {
130
+ throw error;
131
+ }
132
+ const backup = readBackup();
133
+ if (backup) {
134
+ return hydrate(backup);
135
+ }
136
+ throw new types_1.StateCorruptError(statePath, error);
43
137
  }
44
- return { version: types_1.CURRENT_STATE_VERSION, provider, app, service, stages: {}, resources: {} };
45
138
  };
46
139
  exports.loadState = loadState;
47
140
  /* istanbul ignore next */
48
141
  const saveState = (state, app, service, stage, baseDir = process.cwd()) => {
49
142
  (0, exports.ensureStateDir)(baseDir);
50
143
  const statePath = (0, exports.getStatePath)(app, service, baseDir);
144
+ const tmpPath = `${statePath}.tmp`;
145
+ const backupPath = `${statePath}.backup`;
51
146
  // Read the existing file to preserve other stages
52
147
  let existing = {
53
148
  version: types_1.CURRENT_STATE_VERSION,
@@ -70,16 +165,46 @@ const saveState = (state, app, service, stage, baseDir = process.cwd()) => {
70
165
  const stateToSave = {
71
166
  ...existing,
72
167
  version: types_1.CURRENT_STATE_VERSION,
168
+ lineage: existing.lineage || node_crypto_1.default.randomUUID(),
169
+ serial: (existing.serial ?? 0) + 1,
73
170
  app,
74
171
  service,
75
172
  provider: state.provider,
76
173
  stages: {
77
174
  ...existing.stages,
78
- [stage]: { resources: state.resources },
175
+ [stage]: {
176
+ resources: state.resources,
177
+ shared: state.stages?.[stage]?.shared ?? existing.stages?.[stage]?.shared ?? {},
178
+ },
79
179
  },
80
180
  resources: state.resources,
81
181
  };
82
- node_fs_1.default.writeFileSync(statePath, JSON.stringify(stateToSave, null, 2), 'utf-8');
182
+ // Atomic write: back up the previous state, then write tmp + fsync + rename
183
+ try {
184
+ if (node_fs_1.default.existsSync(statePath)) {
185
+ node_fs_1.default.copyFileSync(statePath, backupPath);
186
+ }
187
+ const fd = node_fs_1.default.openSync(tmpPath, 'w');
188
+ try {
189
+ node_fs_1.default.writeFileSync(fd, JSON.stringify(stateToSave, null, 2), 'utf-8');
190
+ node_fs_1.default.fsyncSync(fd);
191
+ }
192
+ finally {
193
+ node_fs_1.default.closeSync(fd);
194
+ }
195
+ node_fs_1.default.renameSync(tmpPath, statePath);
196
+ }
197
+ catch (error) {
198
+ try {
199
+ if (node_fs_1.default.existsSync(tmpPath)) {
200
+ node_fs_1.default.rmSync(tmpPath);
201
+ }
202
+ }
203
+ catch {
204
+ // ignore cleanup failure
205
+ }
206
+ throw error;
207
+ }
83
208
  };
84
209
  exports.saveState = saveState;
85
210
  /**
@@ -124,6 +249,53 @@ const getAllResources = (state) => {
124
249
  return state.resources;
125
250
  };
126
251
  exports.getAllResources = getAllResources;
252
+ /* istanbul ignore next */
253
+ const getAllSharedResources = (state, stage) => {
254
+ return state.stages?.[stage]?.shared ?? {};
255
+ };
256
+ exports.getAllSharedResources = getAllSharedResources;
257
+ /* istanbul ignore next */
258
+ const getSharedResource = (state, stage, key) => {
259
+ return state.stages?.[stage]?.shared?.[key];
260
+ };
261
+ exports.getSharedResource = getSharedResource;
262
+ /* istanbul ignore next */
263
+ const setSharedResource = (state, stage, key, resourceState) => {
264
+ return {
265
+ ...state,
266
+ stages: {
267
+ ...state.stages,
268
+ [stage]: {
269
+ ...state.stages?.[stage],
270
+ resources: state.stages?.[stage]?.resources ?? {},
271
+ shared: {
272
+ ...state.stages?.[stage]?.shared,
273
+ [key]: resourceState,
274
+ },
275
+ },
276
+ },
277
+ };
278
+ };
279
+ exports.setSharedResource = setSharedResource;
280
+ /* istanbul ignore next */
281
+ const removeSharedResource = (state, stage, key) => {
282
+ const existingShared = state.stages?.[stage]?.shared;
283
+ if (!existingShared) {
284
+ return state;
285
+ }
286
+ const { [key]: _removed, ...remainingShared } = existingShared;
287
+ return {
288
+ ...state,
289
+ stages: {
290
+ ...state.stages,
291
+ [stage]: {
292
+ ...state.stages[stage],
293
+ shared: remainingShared,
294
+ },
295
+ },
296
+ };
297
+ };
298
+ exports.removeSharedResource = removeSharedResource;
127
299
  /**
128
300
  * Extract role ARN from function state for event resources.
129
301
  * Looks through all function resources to find a RAM role instance and returns its ARN.
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createClsOperations = void 0;
4
+ const polling_1 = require("../polling");
5
+ const constants_1 = require("../constants");
6
+ // CLS reports missing resources with `ResourceNotFound.<Kind>NotExist` codes —
7
+ // match by suffix so get/delete operations stay idempotent.
8
+ const isResourceNotFound = (error) => {
9
+ if (!error || typeof error !== 'object' || !('code' in error)) {
10
+ return false;
11
+ }
12
+ const code = error.code;
13
+ return typeof code === 'string' && code.includes('NotExist');
14
+ };
15
+ const isAlreadyExists = (error) => {
16
+ if (!error || typeof error !== 'object') {
17
+ return false;
18
+ }
19
+ const rec = error;
20
+ const code = typeof rec.code === 'string' ? rec.code : '';
21
+ const message = typeof rec.message === 'string' ? rec.message.toLowerCase() : '';
22
+ return (code.includes('AlreadyExists') ||
23
+ code.includes('ResourceExists') ||
24
+ message.includes('already exist'));
25
+ };
26
+ const createClsOperations = (clsClient) => {
27
+ const operations = {
28
+ createLogset: async (logsetName, tags) => {
29
+ const response = await clsClient.CreateLogset({
30
+ LogsetName: logsetName,
31
+ Tags: tags.map((t) => ({ Key: t.key, Value: t.value })),
32
+ });
33
+ return { logsetId: response?.LogsetId ?? '' };
34
+ },
35
+ getLogsetByName: async (logsetName) => {
36
+ const response = await clsClient.DescribeLogsets({
37
+ Filters: [{ Key: 'logsetName', Values: [logsetName] }],
38
+ });
39
+ // Preserve Tags so adopters can verify app-scope ownership of the logset.
40
+ const found = response?.Logsets?.find((l) => l.LogsetName === logsetName) ?? null;
41
+ if (!found) {
42
+ return null;
43
+ }
44
+ return {
45
+ LogsetId: found.LogsetId,
46
+ LogsetName: found.LogsetName,
47
+ Tags: found.Tags,
48
+ };
49
+ },
50
+ listTopicsByLogset: async (logsetId) => {
51
+ const response = await clsClient.DescribeTopics({
52
+ Filters: [{ Key: 'logsetId', Values: [logsetId] }],
53
+ Limit: 500,
54
+ });
55
+ return response?.Topics ?? [];
56
+ },
57
+ getTopicByName: async (logsetId, topicName) => {
58
+ const response = await clsClient.DescribeTopics({
59
+ Filters: [
60
+ { Key: 'topicName', Values: [topicName] },
61
+ { Key: 'logsetId', Values: [logsetId] },
62
+ ],
63
+ PreciseSearch: 3,
64
+ });
65
+ return response?.Topics?.find((t) => t.TopicName === topicName) ?? null;
66
+ },
67
+ waitForTopic: async (logsetId, topicName) => (0, polling_1.pollUntil)({
68
+ description: `CLS topic ${topicName} to become visible`,
69
+ fetch: () => operations.getTopicByName(logsetId, topicName),
70
+ isDone: (topic) => topic !== null,
71
+ intervalMs: constants_1.CLS_TOPIC_POLL_INTERVAL_MS,
72
+ maxAttempts: constants_1.CLS_TOPIC_POLL_MAX_ATTEMPTS,
73
+ }),
74
+ // Topic creation is async on Tencent CLS — CreateTopic returns before the
75
+ // topic is queryable, so poll until DescribeTopics sees it.
76
+ createTopic: async (logsetId, topicName, opts) => {
77
+ await clsClient.CreateTopic({
78
+ LogsetId: logsetId,
79
+ TopicName: topicName,
80
+ StorageType: opts.storageType ?? 'hot',
81
+ Period: opts.period ?? 30,
82
+ Tags: opts.tags.map((t) => ({ Key: t.key, Value: t.value })),
83
+ });
84
+ const topic = await operations.waitForTopic(logsetId, topicName);
85
+ return { topicId: topic?.TopicId ?? '' };
86
+ },
87
+ deleteTopic: async (topicId) => {
88
+ try {
89
+ await clsClient.DeleteTopic({ TopicId: topicId });
90
+ }
91
+ catch (error) {
92
+ if (isResourceNotFound(error)) {
93
+ return;
94
+ }
95
+ throw error;
96
+ }
97
+ },
98
+ deleteLogset: async (logsetId) => {
99
+ try {
100
+ await clsClient.DeleteLogset({ LogsetId: logsetId });
101
+ }
102
+ catch (error) {
103
+ if (isResourceNotFound(error)) {
104
+ return;
105
+ }
106
+ throw error;
107
+ }
108
+ },
109
+ createFulltextIndex: async (topicId) => {
110
+ try {
111
+ await clsClient.CreateIndex({
112
+ TopicId: topicId,
113
+ Rule: { FullText: { CaseSensitive: false, Tokenizer: ' ,.?;!\n\t' } },
114
+ });
115
+ }
116
+ catch (error) {
117
+ if (isResourceNotFound(error) || isAlreadyExists(error)) {
118
+ return;
119
+ }
120
+ throw error;
121
+ }
122
+ },
123
+ deleteIndex: async (topicId) => {
124
+ try {
125
+ await clsClient.DeleteIndex({ TopicId: topicId });
126
+ }
127
+ catch (error) {
128
+ if (isResourceNotFound(error)) {
129
+ return;
130
+ }
131
+ throw error;
132
+ }
133
+ },
134
+ };
135
+ return operations;
136
+ };
137
+ exports.createClsOperations = createClsOperations;