@geek-fun/serverlessinsight 0.9.4 → 0.9.6

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 (79) hide show
  1. package/AGENTS.md +5 -2
  2. package/CHANGELOG.md +44 -0
  3. package/README.md +26 -6
  4. package/README.zh-CN.md +26 -6
  5. package/codecov +0 -0
  6. package/codecov.SHA256SUM +1 -1
  7. package/codecov.SHA256SUM.sig +13 -13
  8. package/dist/package.json +1 -1
  9. package/dist/src/commands/deploy.js +7 -2
  10. package/dist/src/commands/destroy.js +6 -3
  11. package/dist/src/commands/forceUnlock.js +16 -21
  12. package/dist/src/commands/index.js +14 -4
  13. package/dist/src/commands/migrate.js +105 -132
  14. package/dist/src/commands/plan.js +2 -2
  15. package/dist/src/commands/show.js +17 -6
  16. package/dist/src/common/aggregateState.js +185 -0
  17. package/dist/src/common/aggregateStore.js +247 -0
  18. package/dist/src/common/apiClient.js +35 -12
  19. package/dist/src/common/constants.js +2 -1
  20. package/dist/src/common/context.js +22 -15
  21. package/dist/src/common/effectiveConfig.js +48 -0
  22. package/dist/src/common/iacHelper.js +2 -2
  23. package/dist/src/common/index.js +5 -0
  24. package/dist/src/common/jsonOutput.js +2 -13
  25. package/dist/src/common/migrationMarker.js +0 -7
  26. package/dist/src/common/nameBuilder.js +2 -3
  27. package/dist/src/common/regionalMigration.js +261 -0
  28. package/dist/src/common/saasProtocol.js +41 -0
  29. package/dist/src/common/stateBackend/cosStateBackend.js +88 -8
  30. package/dist/src/common/stateBackend/index.js +42 -18
  31. package/dist/src/common/stateBackend/localStateBackend.js +61 -26
  32. package/dist/src/common/stateBackend/ossStateBackend.js +62 -9
  33. package/dist/src/common/stateBackend/remoteStateBackend.js +94 -94
  34. package/dist/src/common/stateBackend/saasStateBackend.js +327 -92
  35. package/dist/src/common/stateBackend/types.js +9 -0
  36. package/dist/src/common/stateManager.js +38 -11
  37. package/dist/src/common/stateScope.js +34 -0
  38. package/dist/src/common/stateStorageIO.js +31 -0
  39. package/dist/src/lang/en.js +43 -2
  40. package/dist/src/lang/zh-CN.js +43 -2
  41. package/dist/src/parser/index.js +33 -8
  42. package/dist/src/parser/templateValues.js +31 -0
  43. package/dist/src/stack/aliyunStack/deployer.js +5 -5
  44. package/dist/src/stack/aliyunStack/destroyer.js +5 -5
  45. package/dist/src/stack/aliyunStack/fc3Planner.js +1 -1
  46. package/dist/src/stack/aliyunStack/fc3Resource.js +2 -2
  47. package/dist/src/stack/aliyunStack/planner.js +1 -1
  48. package/dist/src/stack/aliyunStack/sharedLogProject.js +2 -2
  49. package/dist/src/stack/ownershipTag.js +34 -26
  50. package/dist/src/stack/scfStack/deployer.js +5 -5
  51. package/dist/src/stack/scfStack/destroyer.js +5 -6
  52. package/dist/src/stack/scfStack/planner.js +1 -1
  53. package/dist/src/stack/scfStack/scfResource.js +1 -1
  54. package/dist/src/stack/scfStack/sharedLogset.js +5 -2
  55. package/dist/src/stack/volcengineStack/deployer.js +3 -3
  56. package/dist/src/stack/volcengineStack/destroyer.js +2 -3
  57. package/dist/src/stack/volcengineStack/planner.js +1 -1
  58. package/dist/src/stack/volcengineStack/sharedLogProject.js +4 -4
  59. package/dist/src/stack/volcengineStack/vefaasResource.js +1 -1
  60. package/dist/src/types/domains/state.js +28 -2
  61. package/dist/src/validator/index.js +3 -1
  62. package/dist/src/validator/rootSchema.js +8 -0
  63. package/dist/tsconfig.tsbuildinfo +1 -1
  64. package/package.json +1 -1
  65. package/release-notes.md +8 -36
  66. package/samples/README_TENCENT_COS.md +65 -23
  67. package/samples/README_TENCENT_SCF.md +44 -26
  68. package/.serverlessinsight/state-cdn-flow-app-cdn-flow-service.json.backup +0 -14
  69. package/.serverlessinsight/state-iam-test-app-iam-test-service.json.backup +0 -32
  70. package/.serverlessinsight/state-insight-poc-app-insight-poc-tencent.json.backup +0 -90
  71. package/.serverlessinsight/state-insight-poc-app-log-insight-poc-tencent-log.json.backup +0 -118
  72. package/.serverlessinsight/state-insight-poc-deploy-app-insight-poc-deploy.json.backup +0 -215
  73. package/.serverlessinsight/state-insight-poc-destroy-app-insight-poc-destroy.json.backup +0 -14
  74. package/.serverlessinsight/state-insight-poc-timer-app-insight-poc-timer.json.backup +0 -132
  75. package/.serverlessinsight/state-insight-volc-app-insight-volc.json.backup +0 -55
  76. package/.serverlessinsight/state-insight-volc-destroy-app-insight-volc-destroy.json.backup +0 -1
  77. package/.serverlessinsight/state-insight-volc-nested-app-insight-volc-nested.json.backup +0 -93
  78. package/.serverlessinsight/state-nested-drift-app-nested-drift-service.json.backup +0 -163
  79. package/.serverlessinsight/state-role-reconcile-test-app-role-reconcile-test-service.json.backup +0 -148
@@ -0,0 +1,261 @@
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.applyRegionalMigration = exports.readRegionalMigrationJournal = exports.buildScopedDocument = exports.planRegionalMigration = exports.hashDocument = exports.collectRegionEvidence = void 0;
7
+ const node_crypto_1 = __importDefault(require("node:crypto"));
8
+ const aggregateState_1 = require("./aggregateState");
9
+ const types_1 = require("../types");
10
+ const stateScope_1 = require("./stateScope");
11
+ const migrationMarker_1 = require("./migrationMarker");
12
+ const logger_1 = require("./logger");
13
+ const constants_1 = require("./constants");
14
+ const lang_1 = require("../lang");
15
+ const emptyRegion = (region) => !region || String(region).trim().length === 0;
16
+ const collectRegionEvidence = (legacy) => {
17
+ return Object.entries(legacy.stages ?? {}).flatMap(([stage, stageState]) => ['resource', 'shared'].flatMap((kind) => Object.entries(kind === 'resource' ? (stageState.resources ?? {}) : (stageState.shared ?? {})).map(([logicalId, resource]) => ({
18
+ stage,
19
+ evidence: {
20
+ logicalId,
21
+ kind,
22
+ region: emptyRegion(resource.region) ? null : String(resource.region).toLowerCase(),
23
+ },
24
+ }))));
25
+ };
26
+ exports.collectRegionEvidence = collectRegionEvidence;
27
+ const hashDocument = (doc) => node_crypto_1.default.createHash('sha256').update(JSON.stringify(doc)).digest('hex');
28
+ exports.hashDocument = hashDocument;
29
+ const planRegionalMigration = (legacy, options) => {
30
+ if (legacy.managedBy === migrationMarker_1.MANAGED_BY_SAAS) {
31
+ throw new Error(lang_1.lang.__('REGIONAL_MIGRATION_SOURCE_MANAGED_BY_SAAS'));
32
+ }
33
+ if (legacy.version && legacy.version !== types_1.LEGACY_STATE_VERSION) {
34
+ throw new Error(lang_1.lang.__('REGIONAL_MIGRATION_UNSUPPORTED_SOURCE', {
35
+ version: legacy.version,
36
+ expected: types_1.LEGACY_STATE_VERSION,
37
+ }));
38
+ }
39
+ const evidence = (0, exports.collectRegionEvidence)(legacy).filter(({ stage }) => !options.stage || stage === options.stage);
40
+ const unknown = evidence
41
+ .filter(({ evidence: item }) => item.region === null)
42
+ .map(({ stage, evidence: item }) => ({ stage, logicalId: item.logicalId, kind: item.kind }));
43
+ // A local Map keeps grouping linear without mutating the source state.
44
+ const regionGroups = evidence.reduce((groups, { stage, evidence: item }) => {
45
+ if (item.region === null)
46
+ return groups;
47
+ const key = `${stage}::${item.region}`;
48
+ const previous = groups.get(key) ?? { resources: 0, shared: 0 };
49
+ return groups.set(key, {
50
+ resources: previous.resources + Number(item.kind === 'resource'),
51
+ shared: previous.shared + Number(item.kind === 'shared'),
52
+ });
53
+ }, new Map());
54
+ const warnings = legacy.provider && legacy.provider !== options.provider
55
+ ? [
56
+ lang_1.lang.__('REGIONAL_MIGRATION_PROVIDER_MISMATCH', {
57
+ source: legacy.provider,
58
+ effective: options.provider,
59
+ }),
60
+ ]
61
+ : [];
62
+ return {
63
+ opId: node_crypto_1.default.randomUUID(),
64
+ sourceVersion: legacy.version ?? types_1.LEGACY_STATE_VERSION,
65
+ sourceHash: (0, exports.hashDocument)(legacy),
66
+ documents: Array.from(regionGroups.entries()).map(([key, group]) => {
67
+ const [stage, region] = key.split('::');
68
+ return { stage, region, ...group };
69
+ }),
70
+ unknown,
71
+ warnings,
72
+ };
73
+ };
74
+ exports.planRegionalMigration = planRegionalMigration;
75
+ const buildStageStateForRegion = (legacy, stage, region) => {
76
+ const stageState = legacy.stages?.[stage];
77
+ const selectRegion = (resources = {}) => Object.fromEntries(Object.entries(resources).filter(([, resource]) => !emptyRegion(resource.region) && String(resource.region).toLowerCase() === region));
78
+ return {
79
+ resources: selectRegion(stageState?.resources),
80
+ shared: selectRegion(stageState?.shared),
81
+ };
82
+ };
83
+ const buildScopedDocument = (legacy, plan, scope, target) => {
84
+ (0, stateScope_1.validateScopeKeyPart)(scope.stage, 'stage');
85
+ (0, stateScope_1.canonicalizeRegion)(scope.region);
86
+ const { resources, shared } = buildStageStateForRegion(legacy, scope.stage, scope.region);
87
+ const migration = {
88
+ opId: plan.opId,
89
+ sourceVersion: plan.sourceVersion,
90
+ sourceHash: plan.sourceHash,
91
+ migratedAt: new Date().toISOString(),
92
+ };
93
+ return {
94
+ version: types_1.CURRENT_STATE_VERSION,
95
+ provider: target.provider,
96
+ app: target.app,
97
+ service: target.service,
98
+ ...(legacy.orgId ? { orgId: legacy.orgId } : {}),
99
+ ...(legacy.appId ? { appId: legacy.appId } : {}),
100
+ ...(legacy.serviceId ? { serviceId: legacy.serviceId } : {}),
101
+ scope: { stage: scope.stage, region: scope.region },
102
+ lineage: node_crypto_1.default.randomUUID(),
103
+ serial: 1,
104
+ migration,
105
+ stages: {
106
+ [scope.stage]: {
107
+ resources,
108
+ ...(Object.keys(shared).length > 0 ? { shared } : {}),
109
+ },
110
+ },
111
+ resources,
112
+ };
113
+ };
114
+ exports.buildScopedDocument = buildScopedDocument;
115
+ const journalKey = (paths) => `${paths.legacyKey}${constants_1.REGIONAL_MIGRATION_SUFFIX}`;
116
+ const readRegionalMigrationJournal = (io, paths) => io.read(journalKey(paths));
117
+ exports.readRegionalMigrationJournal = readRegionalMigrationJournal;
118
+ const stripRuntimeProjection = (state) => {
119
+ const { resources: _projection, ...persisted } = state;
120
+ return persisted;
121
+ };
122
+ const buildMigrationTargets = (legacy, plan, options) => {
123
+ const bound = options.bindUnknownRegion
124
+ ? (0, stateScope_1.canonicalizeRegion)(options.bindUnknownRegion)
125
+ : undefined;
126
+ const boundScopes = bound
127
+ ? [...new Set(plan.unknown.map((item) => item.stage))]
128
+ .filter((stage) => !plan.documents.some((scope) => scope.stage === stage && scope.region === bound))
129
+ .map((stage) => ({ stage, region: bound, resources: 0, shared: 0 }))
130
+ : [];
131
+ return [...plan.documents, ...boundScopes].map(({ stage, region }) => {
132
+ const state = (0, exports.buildScopedDocument)(legacy, plan, { stage, region }, {
133
+ provider: legacy.provider || options.provider,
134
+ app: options.app,
135
+ service: options.service,
136
+ });
137
+ if (bound !== region)
138
+ return { stage, region, state: stripRuntimeProjection(state) };
139
+ const unknownFor = (kind) => Object.fromEntries(plan.unknown
140
+ .filter((item) => item.stage === stage && item.kind === kind)
141
+ .flatMap((item) => {
142
+ const store = kind === 'shared' ? 'shared' : 'resources';
143
+ const resource = legacy.stages[stage]?.[store]?.[item.logicalId];
144
+ return resource ? [[item.logicalId, { ...resource, region }]] : [];
145
+ }));
146
+ const shared = unknownFor('shared');
147
+ return {
148
+ stage,
149
+ region,
150
+ state: stripRuntimeProjection({
151
+ ...state,
152
+ stages: {
153
+ ...state.stages,
154
+ [stage]: {
155
+ ...state.stages[stage],
156
+ resources: { ...state.stages[stage].resources, ...unknownFor('resource') },
157
+ ...(Object.keys(shared).length
158
+ ? { shared: { ...state.stages[stage].shared, ...shared } }
159
+ : {}),
160
+ },
161
+ },
162
+ }),
163
+ };
164
+ });
165
+ };
166
+ const aggregateMigrationTargets = (targets, options) => targets.reduce((document, { stage, region, state }) => (0, aggregateState_1.mergeAggregateScope)(document, { app: options.app, service: options.service, provider: state.provider, stage, region }, { ...state, resources: state.stages[stage].resources }), { version: aggregateState_1.AGGREGATE_STATE_VERSION, app: options.app, service: options.service, stages: {} });
167
+ const migrationReport = (journal) => ({
168
+ ...journal.plan,
169
+ documents: journal.targets.map(({ stage, region, state }) => ({
170
+ stage,
171
+ region,
172
+ resources: Object.keys(state.stages[stage].resources).length,
173
+ shared: Object.keys(state.stages[stage].shared ?? {}).length,
174
+ })),
175
+ migratedAt: new Date().toISOString(),
176
+ barrierWritten: true,
177
+ });
178
+ /** The caller holds the original file/key lock throughout the in-place upgrade. */
179
+ const applyRegionalMigration = async (io, legacy, paths, plan, options) => {
180
+ if (plan.unknown.length > 0 && !options.bindUnknownRegion) {
181
+ throw new Error(lang_1.lang.__('REGIONAL_MIGRATION_UNKNOWN_REGION_REQUIRED'));
182
+ }
183
+ if ((0, exports.hashDocument)(legacy) !== plan.sourceHash) {
184
+ throw new Error(lang_1.lang.__('REGIONAL_MIGRATION_SOURCE_CHANGED'));
185
+ }
186
+ const previous = await (0, exports.readRegionalMigrationJournal)(io, paths);
187
+ if (previous &&
188
+ (previous.plan.opId !== plan.opId ||
189
+ !previous.aggregate ||
190
+ !['provider', 'app', 'service', 'stage', 'bindUnknownRegion'].every((key) => previous.options[key] === options[key]))) {
191
+ throw new Error(lang_1.lang.__('REGIONAL_MIGRATION_PENDING', { opId: previous.plan.opId }));
192
+ }
193
+ const targets = previous?.targets ?? buildMigrationTargets(legacy, plan, options);
194
+ const completePlan = {
195
+ ...(0, exports.planRegionalMigration)(legacy, { ...options, stage: undefined }),
196
+ opId: plan.opId,
197
+ };
198
+ const expectedTargets = buildMigrationTargets(legacy, completePlan, options);
199
+ const uncovered = Object.entries(legacy.stages).filter(([stage, value]) => (Object.keys(value.resources ?? {}).length || Object.keys(value.shared ?? {}).length) &&
200
+ !targets.some((target) => target.stage === stage));
201
+ const coverage = (items) => items
202
+ .map(({ stage, region, state }) => ({
203
+ stage,
204
+ region,
205
+ provider: state.provider,
206
+ resources: state.stages[stage].resources,
207
+ shared: state.stages[stage].shared ?? {},
208
+ }))
209
+ .sort((left, right) => `${left.stage}/${left.region}`.localeCompare(`${right.stage}/${right.region}`));
210
+ if (uncovered.length ||
211
+ (0, exports.hashDocument)(coverage(targets)) !== (0, exports.hashDocument)(coverage(expectedTargets))) {
212
+ throw new Error(lang_1.lang.__('REGIONAL_MIGRATION_INCOMPLETE_STAGE_COVERAGE', {
213
+ stages: Object.keys(legacy.stages).join(', '),
214
+ }));
215
+ }
216
+ const journal = previous ?? {
217
+ plan,
218
+ options,
219
+ targets,
220
+ phase: 'applying',
221
+ aggregate: aggregateMigrationTargets(targets, options),
222
+ };
223
+ if (journal.phase === 'complete') {
224
+ const report = await io.read(paths.reportKey(plan.opId));
225
+ if (report) {
226
+ (0, aggregateState_1.readAggregateDocument)(await io.read(paths.legacyKey), options, paths.legacyKey);
227
+ return report;
228
+ }
229
+ }
230
+ const current = await io.read(paths.legacyKey);
231
+ const sourceMatches = current !== null && (0, exports.hashDocument)(current) === plan.sourceHash;
232
+ const aggregateMatches = current !== null && (0, exports.hashDocument)(current) === (0, exports.hashDocument)(journal.aggregate);
233
+ if (!sourceMatches && !aggregateMatches) {
234
+ throw new Error(lang_1.lang.__('REGIONAL_MIGRATION_SOURCE_CHANGED'));
235
+ }
236
+ const backup = await io.read(paths.backupKey(plan.opId));
237
+ if (backup && (0, exports.hashDocument)(backup) !== plan.sourceHash) {
238
+ throw new Error(lang_1.lang.__('REGIONAL_MIGRATION_SOURCE_CHANGED'));
239
+ }
240
+ if (!backup)
241
+ await io.write(paths.backupKey(plan.opId), legacy);
242
+ if (!previous)
243
+ await io.write(journalKey(paths), journal);
244
+ try {
245
+ if (sourceMatches)
246
+ await io.write(paths.legacyKey, journal.aggregate);
247
+ const report = migrationReport(journal);
248
+ await io.write(paths.reportKey(plan.opId), report);
249
+ await io.write(journalKey(paths), { ...journal, phase: 'complete' });
250
+ logger_1.logger.info(lang_1.lang.__('REGIONAL_MIGRATION_APPLIED', { opId: plan.opId }));
251
+ return report;
252
+ }
253
+ catch (error) {
254
+ logger_1.logger.error(lang_1.lang.__('REGIONAL_MIGRATION_PARTIAL_FAILURE', {
255
+ opId: plan.opId,
256
+ error: error instanceof Error ? error.message : String(error),
257
+ }));
258
+ throw error;
259
+ }
260
+ };
261
+ exports.applyRegionalMigration = applyRegionalMigration;
@@ -0,0 +1,41 @@
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.isRegionalSaasSnapshot = exports.supportsRegionalSaas = exports.hashSaasState = exports.SCOPE_PROTOCOL_VERSION = void 0;
7
+ const node_crypto_1 = __importDefault(require("node:crypto"));
8
+ /** Regional identity and content-hash CAS shared by all SaaS commands. */
9
+ exports.SCOPE_PROTOCOL_VERSION = 3;
10
+ /** JSONB can reorder keys; content identity must survive a server round trip. */
11
+ const hashSaasState = (state) => node_crypto_1.default
12
+ .createHash('sha256')
13
+ .update(JSON.stringify(state, (_key, value) => value && typeof value === 'object' && !Array.isArray(value)
14
+ ? Object.fromEntries(Object.keys(value)
15
+ .sort()
16
+ .map((key) => [key, value[key]]))
17
+ : value))
18
+ .digest('hex');
19
+ exports.hashSaasState = hashSaasState;
20
+ const supportsRegionalSaas = (value) => {
21
+ const caps = value;
22
+ return (caps?.regionalScope === true &&
23
+ caps.providerScope === true &&
24
+ caps.scopeProtocol === exports.SCOPE_PROTOCOL_VERSION &&
25
+ caps.stateFormat === '4.0');
26
+ };
27
+ exports.supportsRegionalSaas = supportsRegionalSaas;
28
+ /** Scope authority is server metadata, independent of historical JSON format. */
29
+ const isRegionalSaasSnapshot = (snapshot, scope) => snapshot?.scopeProtocol === exports.SCOPE_PROTOCOL_VERSION &&
30
+ typeof snapshot.stageId === 'string' &&
31
+ snapshot.stageId.length > 0 &&
32
+ snapshot.stage === scope.stage &&
33
+ snapshot.provider === scope.provider &&
34
+ snapshot.region === scope.region &&
35
+ typeof snapshot.id === 'string' &&
36
+ snapshot.id.length > 0 &&
37
+ typeof snapshot.contentHash === 'string' &&
38
+ snapshot.contentHash.length > 0 &&
39
+ Number.isInteger(snapshot.versionNumber) &&
40
+ snapshot.versionNumber > 0;
41
+ exports.isRegionalSaasSnapshot = isRegionalSaasSnapshot;
@@ -3,14 +3,23 @@ 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.createCosStateBackend = void 0;
6
+ exports.createCosStateBackend = exports.createCosStorageAdapter = void 0;
7
7
  const cos_nodejs_sdk_v5_1 = __importDefault(require("cos-nodejs-sdk-v5"));
8
+ const types_1 = require("./types");
8
9
  const remoteStateBackend_1 = require("./remoteStateBackend");
9
10
  const constants_1 = require("../constants");
11
+ const lang_1 = require("../../lang");
10
12
  const promisifyGet = (cosClient, bucket, region, key) => {
11
13
  return new Promise((resolve, reject) => {
12
14
  cosClient.getObject({ Bucket: bucket, Region: region, Key: key }, (err, data) => {
13
15
  if (err) {
16
+ try {
17
+ classifyCosReadError(err);
18
+ }
19
+ catch (classified) {
20
+ reject(classified);
21
+ return;
22
+ }
14
23
  if (err.statusCode === 404) {
15
24
  resolve(null);
16
25
  }
@@ -34,6 +43,36 @@ const promisifyPut = (cosClient, bucket, region, key, body) => {
34
43
  });
35
44
  });
36
45
  };
46
+ const promisifyPutIfAbsent = (cosClient, bucket, region, key, body) => {
47
+ return new Promise((resolve, reject) => {
48
+ cosClient.putObject({
49
+ Bucket: bucket,
50
+ Region: region,
51
+ Key: key,
52
+ Body: body,
53
+ Headers: { 'x-cos-forbid-overwrite': 'true' },
54
+ }, (err) => {
55
+ if (err) {
56
+ const code = err.code ?? '';
57
+ if (err.statusCode === 412 ||
58
+ code === 'PreconditionFailed' ||
59
+ code === 'KeyConflict' ||
60
+ err.statusCode === 409) {
61
+ resolve(false);
62
+ return;
63
+ }
64
+ if (err.statusCode === 400 || err.statusCode === 501) {
65
+ reject(new types_1.ConditionalPutUnsupportedError(err));
66
+ return;
67
+ }
68
+ reject(err);
69
+ }
70
+ else {
71
+ resolve(true);
72
+ }
73
+ });
74
+ });
75
+ };
37
76
  const promisifyDelete = (cosClient, bucket, region, key) => {
38
77
  return new Promise((resolve, reject) => {
39
78
  cosClient.deleteObject({ Bucket: bucket, Region: region, Key: key }, (err) => {
@@ -44,6 +83,14 @@ const promisifyDelete = (cosClient, bucket, region, key) => {
44
83
  });
45
84
  });
46
85
  };
86
+ const classifyCosReadError = (err) => {
87
+ if (err.code === 'NoSuchBucket') {
88
+ throw new Error(lang_1.lang.__('STATE_BUCKET_NOT_FOUND', { code: err.code }));
89
+ }
90
+ if (err.statusCode === 403 || err.code === 'AccessDenied') {
91
+ throw new Error(lang_1.lang.__('STATE_BUCKET_ACCESS_DENIED', { code: err.code ?? 'AccessDenied' }));
92
+ }
93
+ };
47
94
  const createCosStorageAdapter = (config) => {
48
95
  const cosClient = new cos_nodejs_sdk_v5_1.default({
49
96
  SecretId: config.accessKeyId,
@@ -53,22 +100,55 @@ const createCosStorageAdapter = (config) => {
53
100
  });
54
101
  return {
55
102
  read: async (key) => {
56
- const body = await promisifyGet(cosClient, config.bucket, config.region, key);
57
- if (!body)
58
- return null;
59
- return JSON.parse(body.toString('utf-8'));
103
+ try {
104
+ const body = await promisifyGet(cosClient, config.bucket, config.region, key);
105
+ if (!body)
106
+ return null;
107
+ return JSON.parse(body.toString('utf-8'));
108
+ }
109
+ catch (err) {
110
+ classifyCosReadError(err);
111
+ throw err;
112
+ }
60
113
  },
61
114
  write: async (key, data) => {
62
115
  await promisifyPut(cosClient, config.bucket, config.region, key, JSON.stringify(data, null, 2));
63
116
  },
117
+ /** COS only honors forbid-overwrite on buckets that never enabled versioning. */
118
+ putIfAbsent: async (key, data) => {
119
+ const versioning = await new Promise((resolve, reject) => {
120
+ cosClient.getBucketVersioning({ Bucket: config.bucket, Region: config.region }, (error, data) => {
121
+ if (error)
122
+ reject(error);
123
+ else
124
+ resolve(data);
125
+ });
126
+ });
127
+ if (versioning.VersioningConfiguration.Status) {
128
+ throw new types_1.ConditionalPutUnsupportedError('bucket versioning disables exclusive creation');
129
+ }
130
+ return promisifyPutIfAbsent(cosClient, config.bucket, config.region, key, JSON.stringify(data, null, 2));
131
+ },
64
132
  delete: async (key) => {
65
133
  await promisifyDelete(cosClient, config.bucket, config.region, key);
66
134
  },
67
- lockPath: `cos://${config.bucket}/${config.key}`,
135
+ lockPath: config.lockPath,
68
136
  };
69
137
  };
138
+ exports.createCosStorageAdapter = createCosStorageAdapter;
70
139
  const createCosStateBackend = (config) => {
71
- const adapter = createCosStorageAdapter(config);
72
- return (0, remoteStateBackend_1.createRemoteStateBackend)(adapter, { key: config.key, markerAction: config.markerAction });
140
+ const adapter = (0, exports.createCosStorageAdapter)({
141
+ bucket: config.bucket,
142
+ region: config.region,
143
+ accessKeyId: config.accessKeyId,
144
+ accessKeySecret: config.accessKeySecret,
145
+ securityToken: config.securityToken,
146
+ lockPath: `cos://${config.bucket}/${config.key}`,
147
+ });
148
+ return (0, remoteStateBackend_1.createRemoteStateBackend)(adapter, {
149
+ scope: config.scope,
150
+ key: config.key,
151
+ markerAction: config.markerAction,
152
+ });
73
153
  };
74
154
  exports.createCosStateBackend = createCosStateBackend;
@@ -14,13 +14,16 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.createStateBackend = void 0;
17
+ exports.resolveStateStorage = exports.createStateBackend = void 0;
18
18
  const types_1 = require("../../types");
19
19
  const providerEnum_1 = require("../providerEnum");
20
+ const lang_1 = require("../../lang");
20
21
  const localStateBackend_1 = require("./localStateBackend");
21
22
  const ossStateBackend_1 = require("./ossStateBackend");
22
23
  const cosStateBackend_1 = require("./cosStateBackend");
23
24
  const saasStateBackend_1 = require("./saasStateBackend");
25
+ const stateScope_1 = require("../stateScope");
26
+ const credentials_1 = require("../credentials");
24
27
  __exportStar(require("./types"), exports);
25
28
  __exportStar(require("./localStateBackend"), exports);
26
29
  __exportStar(require("./ossStateBackend"), exports);
@@ -29,41 +32,62 @@ __exportStar(require("./remoteStateBackend"), exports);
29
32
  __exportStar(require("./saasStateBackend"), exports);
30
33
  __exportStar(require("./lockUtils"), exports);
31
34
  const createStateBackend = (backendConfig, context) => {
35
+ const scope = {
36
+ provider: context.provider,
37
+ app: context.app,
38
+ service: context.service,
39
+ stage: context.stage,
40
+ region: context.region,
41
+ };
32
42
  // SaaS (default) — requires API key
33
43
  if (!backendConfig || backendConfig.type === types_1.StateBackendType.SAAS) {
34
44
  return (0, saasStateBackend_1.createSaasStateBackend)({
35
- app: context.app,
36
- service: context.service,
45
+ scope,
37
46
  declaredOrg: context.declaredOrg,
38
- }, { apiKey: context.siApiKey });
47
+ }, { apiKey: context.siApiKey, mode: context.mode });
39
48
  }
40
49
  if (backendConfig.type === types_1.StateBackendType.LOCAL) {
41
- return (0, localStateBackend_1.createLocalStateBackend)(context.app, context.service, context.baseDir, context.migrationMarker);
50
+ return (0, localStateBackend_1.createLocalStateBackend)(scope, {
51
+ baseDir: context.baseDir,
52
+ markerAction: context.migrationMarker,
53
+ });
42
54
  }
43
55
  const bucketConfig = backendConfig;
44
- const region = context.region;
45
- const accessKeyId = context.accessKeyId;
46
- const accessKeySecret = context.accessKeySecret;
47
- const securityToken = context.securityToken;
48
- if (context.provider === providerEnum_1.ProviderEnum.TENCENT) {
56
+ const storage = (0, exports.resolveStateStorage)(bucketConfig, context);
57
+ if (storage.provider === providerEnum_1.ProviderEnum.TENCENT) {
49
58
  return (0, cosStateBackend_1.createCosStateBackend)({
59
+ scope,
50
60
  bucket: bucketConfig.bucket,
51
61
  key: bucketConfig.key,
52
- region,
53
- accessKeyId,
54
- accessKeySecret,
55
- securityToken,
62
+ ...storage,
56
63
  markerAction: context.migrationMarker,
57
64
  });
58
65
  }
59
66
  return (0, ossStateBackend_1.createOssStateBackend)({
67
+ scope,
60
68
  bucket: bucketConfig.bucket,
61
69
  key: bucketConfig.key,
62
- region,
63
- accessKeyId,
64
- accessKeySecret,
65
- securityToken,
70
+ ...storage,
66
71
  markerAction: context.migrationMarker,
67
72
  });
68
73
  };
69
74
  exports.createStateBackend = createStateBackend;
75
+ const resolveStateStorage = (bucketConfig, context) => {
76
+ const provider = bucketConfig.storageProvider ?? context.storage?.provider ?? context.provider;
77
+ const region = (0, stateScope_1.canonicalizeRegion)(bucketConfig.storageRegion ?? context.storage?.region ?? context.region);
78
+ if (provider !== providerEnum_1.ProviderEnum.ALIYUN && provider !== providerEnum_1.ProviderEnum.TENCENT) {
79
+ throw new Error(lang_1.lang.__('STATE_STORAGE_PROVIDER_UNSUPPORTED', { provider }));
80
+ }
81
+ const credentials = (0, credentials_1.getCredentials)(provider === context.provider ? context : undefined, provider);
82
+ if (!credentials.accessKeyId || !credentials.accessKeySecret) {
83
+ throw new Error(lang_1.lang.__('STATE_STORAGE_CREDENTIALS_MISSING', { provider }));
84
+ }
85
+ return {
86
+ provider,
87
+ region,
88
+ accessKeyId: credentials.accessKeyId,
89
+ accessKeySecret: credentials.accessKeySecret,
90
+ ...(credentials.securityToken ? { securityToken: credentials.securityToken } : {}),
91
+ };
92
+ };
93
+ exports.resolveStateStorage = resolveStateStorage;
@@ -1,42 +1,77 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createLocalStateBackend = void 0;
4
+ const node_async_hooks_1 = require("node:async_hooks");
5
+ const aggregateStore_1 = require("../aggregateStore");
4
6
  const stateManager_1 = require("../stateManager");
5
- const migrationMarker_1 = require("../migrationMarker");
7
+ const stateScope_1 = require("../stateScope");
8
+ const stateStorageIO_1 = require("../stateStorageIO");
6
9
  const lockManager_1 = require("../lockManager");
7
- const createLocalStateBackend = (app, service, baseDir = process.cwd(), markerAction = 'off') => {
8
- const statePath = (0, stateManager_1.getStatePath)(app, service, baseDir);
10
+ const createLocalStateBackend = (scope, options = {}) => {
11
+ const normalized = (0, stateScope_1.normalizeScope)(scope);
12
+ const baseDir = options.baseDir ?? process.cwd();
13
+ const statePath = (0, stateManager_1.getStatePath)(normalized.app, normalized.service, baseDir);
14
+ const store = (0, aggregateStore_1.createAggregateStore)((0, stateStorageIO_1.createLocalStateIO)({ backupWrites: false }), normalized, {
15
+ key: statePath,
16
+ backupWrites: true,
17
+ markerAction: options.markerAction ?? 'off',
18
+ });
19
+ const writer = new node_async_hooks_1.AsyncLocalStorage();
20
+ let manualLockId = null;
21
+ const withLock = async (operation, fn, lockOptions, onLockAcquired) => (0, lockManager_1.withLock)(statePath, operation, () => writer.run(true, async () => {
22
+ try {
23
+ if (!operation.startsWith('migrate'))
24
+ await store.verifyPlanBase();
25
+ return await fn();
26
+ }
27
+ finally {
28
+ store.clearPlanBase();
29
+ }
30
+ }), lockOptions, onLockAcquired);
31
+ const mutate = async (operation, fn) => {
32
+ if (writer.getStore() || manualLockId)
33
+ return fn();
34
+ return withLock(operation, fn);
35
+ };
9
36
  return {
10
- loadState: async (provider, loadApp, loadService, stage) => {
11
- const state = (0, stateManager_1.loadState)(provider, loadApp, loadService, stage, baseDir);
12
- (0, migrationMarker_1.enforceMigrationMarker)(state, markerAction);
13
- return state;
14
- },
15
- saveState: async (state, saveApp, saveService, stage) => {
16
- (0, stateManager_1.saveState)(state, saveApp, saveService, stage, baseDir);
17
- },
18
- patchPersisted: async (patch) => {
19
- (0, stateManager_1.patchPersistedState)(app, service, patch, baseDir);
20
- },
21
- acquireLock: async (operation, options) => {
22
- const lockMeta = await (0, lockManager_1.acquireLockInternal)(statePath, operation, options);
23
- return lockMeta.id;
37
+ scope: normalized,
38
+ capabilities: { conditionalCreate: true },
39
+ loadState: () => store.loadState(!writer.getStore() && !manualLockId),
40
+ saveState: (state) => mutate('state-save', () => store.saveState(state)),
41
+ patchPersisted: (patch) => mutate('migrate-metadata', () => store.patchPersisted(patch)),
42
+ upgradeLegacyState: (options) => mutate('migrate-upgrade', () => store.upgradeLegacyState(options)),
43
+ readStateSource: store.readStateSource,
44
+ probeWriteReadiness: store.probeWriteReadiness,
45
+ acquireLock: async (operation, lockOptions) => {
46
+ const lock = await (0, lockManager_1.acquireLockInternal)(statePath, operation, lockOptions);
47
+ try {
48
+ if (!operation.startsWith('migrate'))
49
+ await store.verifyPlanBase();
50
+ manualLockId = lock.id;
51
+ return lock.id;
52
+ }
53
+ catch (error) {
54
+ (0, lockManager_1.releaseLockInternal)(statePath, lock.id);
55
+ throw error;
56
+ }
24
57
  },
25
58
  releaseLock: async (lockId) => {
26
59
  (0, lockManager_1.releaseLockInternal)(statePath, lockId);
60
+ if (manualLockId === lockId) {
61
+ manualLockId = null;
62
+ store.clearPlanBase();
63
+ }
27
64
  },
28
65
  forceUnlock: async (lockId) => {
29
- return (0, lockManager_1.forceUnlock)(statePath, lockId);
30
- },
31
- readLock: async () => {
32
- return (0, lockManager_1.readLockFileForCommand)(statePath);
33
- },
34
- withLock: async (operation, fn, options, onLockAcquired) => {
35
- if (onLockAcquired) {
36
- return (0, lockManager_1.withLock)(statePath, operation, fn, options, onLockAcquired);
66
+ const removed = (0, lockManager_1.forceUnlock)(statePath, lockId);
67
+ if (removed && manualLockId === lockId) {
68
+ manualLockId = null;
69
+ store.clearPlanBase();
37
70
  }
38
- return (0, lockManager_1.withLock)(statePath, operation, fn, options);
71
+ return removed;
39
72
  },
73
+ readLock: async () => (0, lockManager_1.readLockFileForCommand)(statePath),
74
+ withLock,
40
75
  };
41
76
  };
42
77
  exports.createLocalStateBackend = createLocalStateBackend;