@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
@@ -3,10 +3,29 @@ 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.createOssStateBackend = void 0;
6
+ exports.createOssStateBackend = exports.createOssStorageAdapter = void 0;
7
7
  const ali_oss_1 = __importDefault(require("ali-oss"));
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");
12
+ const isObjectAbsent = (err) => {
13
+ if (err && typeof err === 'object' && 'code' in err) {
14
+ return err.code === 'NoSuchKey';
15
+ }
16
+ return false;
17
+ };
18
+ const assertNotFatalAbsence = (err) => {
19
+ if (err && typeof err === 'object' && 'code' in err) {
20
+ const code = err.code;
21
+ if (code === 'NoSuchBucket') {
22
+ throw new Error(lang_1.lang.__('STATE_BUCKET_NOT_FOUND', { code }));
23
+ }
24
+ if (code === 'AccessDenied') {
25
+ throw new Error(lang_1.lang.__('STATE_BUCKET_ACCESS_DENIED', { code }));
26
+ }
27
+ }
28
+ };
10
29
  const createOssStorageAdapter = (config) => {
11
30
  const ossClient = new ali_oss_1.default({
12
31
  accessKeyId: config.accessKeyId,
@@ -25,11 +44,9 @@ const createOssStorageAdapter = (config) => {
25
44
  return JSON.parse(content);
26
45
  }
27
46
  catch (err) {
28
- if (err && typeof err === 'object' && 'code' in err) {
29
- const code = err.code;
30
- if (code === 'NoSuchKey' || code === 'NoSuchBucket') {
31
- return null;
32
- }
47
+ assertNotFatalAbsence(err);
48
+ if (isObjectAbsent(err)) {
49
+ return null;
33
50
  }
34
51
  throw err;
35
52
  }
@@ -38,6 +55,30 @@ const createOssStorageAdapter = (config) => {
38
55
  const content = JSON.stringify(data, null, 2);
39
56
  await ossClient.put(key, Buffer.from(content, 'utf-8'));
40
57
  },
58
+ putIfAbsent: async (key, data) => {
59
+ const content = JSON.stringify(data, null, 2);
60
+ const versioning = await ossClient.getBucketVersioning(config.bucket);
61
+ if (versioning.versionStatus && versioning.versionStatus !== 'Disabled') {
62
+ throw new types_1.ConditionalPutUnsupportedError('bucket versioning disables exclusive creation');
63
+ }
64
+ try {
65
+ await ossClient.put(key, Buffer.from(content, 'utf-8'), {
66
+ headers: { 'x-oss-forbid-overwrite': 'true' },
67
+ });
68
+ return true;
69
+ }
70
+ catch (err) {
71
+ if (err && typeof err === 'object' && 'code' in err) {
72
+ const code = err.code;
73
+ if (code === 'FileAlreadyExists' ||
74
+ code === 'EtagMismatch' ||
75
+ code === 'PreconditionFailed') {
76
+ return false;
77
+ }
78
+ }
79
+ throw err;
80
+ }
81
+ },
41
82
  delete: async (key) => {
42
83
  try {
43
84
  await ossClient.delete(key);
@@ -46,11 +87,23 @@ const createOssStorageAdapter = (config) => {
46
87
  // noop
47
88
  }
48
89
  },
49
- lockPath: `oss://${config.bucket}/${config.key}`,
90
+ lockPath: config.lockPath,
50
91
  };
51
92
  };
93
+ exports.createOssStorageAdapter = createOssStorageAdapter;
52
94
  const createOssStateBackend = (config) => {
53
- const adapter = createOssStorageAdapter(config);
54
- return (0, remoteStateBackend_1.createRemoteStateBackend)(adapter, { key: config.key, markerAction: config.markerAction });
95
+ const adapter = (0, exports.createOssStorageAdapter)({
96
+ bucket: config.bucket,
97
+ region: config.region,
98
+ accessKeyId: config.accessKeyId,
99
+ accessKeySecret: config.accessKeySecret,
100
+ securityToken: config.securityToken,
101
+ lockPath: `oss://${config.bucket}/${config.key}`,
102
+ });
103
+ return (0, remoteStateBackend_1.createRemoteStateBackend)(adapter, {
104
+ scope: config.scope,
105
+ key: config.key,
106
+ markerAction: config.markerAction,
107
+ });
55
108
  };
56
109
  exports.createOssStateBackend = createOssStateBackend;
@@ -4,21 +4,46 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.createRemoteStateBackend = void 0;
7
+ const node_async_hooks_1 = require("node:async_hooks");
7
8
  const node_os_1 = __importDefault(require("node:os"));
8
- const types_1 = require("../../types");
9
- const stateManager_1 = require("../stateManager");
10
- const migrationMarker_1 = require("../migrationMarker");
9
+ const aggregateStore_1 = require("../aggregateStore");
10
+ const stateScope_1 = require("../stateScope");
11
11
  const constants_1 = require("../constants");
12
12
  const lockManager_1 = require("../lockManager");
13
13
  const logger_1 = require("../logger");
14
14
  const lang_1 = require("../../lang");
15
+ const types_1 = require("./types");
15
16
  const lockUtils_1 = require("./lockUtils");
16
17
  const createRemoteStateBackend = (adapter, config) => {
18
+ const normalized = (0, stateScope_1.normalizeScope)(config.scope);
17
19
  const lockKey = `${config.key}${lockUtils_1.LOCK_KEY_SUFFIX}`;
18
- const markerAction = config.markerAction ?? 'off';
20
+ const capabilities = {
21
+ conditionalCreate: typeof adapter.putIfAbsent === 'function',
22
+ };
23
+ const store = (0, aggregateStore_1.createAggregateStore)(adapter, normalized, {
24
+ key: config.key,
25
+ markerAction: config.markerAction ?? 'off',
26
+ backupWrites: true,
27
+ });
28
+ const writer = new node_async_hooks_1.AsyncLocalStorage();
29
+ let manualLockId = null;
19
30
  const readLockObject = async () => {
20
31
  return adapter.read(lockKey);
21
32
  };
33
+ const writeLockObject = async (metadata) => {
34
+ if (!adapter.putIfAbsent)
35
+ throw new Error(lang_1.lang.__('STATE_ATOMIC_CREATE_REQUIRED'));
36
+ try {
37
+ return await adapter.putIfAbsent(lockKey, metadata);
38
+ }
39
+ catch (error) {
40
+ if (error instanceof types_1.ConditionalPutUnsupportedError) {
41
+ capabilities.conditionalCreate = false;
42
+ throw new Error(lang_1.lang.__('STATE_ATOMIC_CREATE_REQUIRED'), { cause: error });
43
+ }
44
+ throw error;
45
+ }
46
+ };
22
47
  const acquireLockObject = async (operation, options = {}) => {
23
48
  const timeout = options.timeout ?? constants_1.DEFAULT_LOCK_TIMEOUT;
24
49
  const retryDelay = options.retryDelay ?? constants_1.DEFAULT_LOCK_RETRY_DELAY;
@@ -29,10 +54,12 @@ const createRemoteStateBackend = (adapter, config) => {
29
54
  while (attempt <= maxRetries) {
30
55
  const existingLock = await readLockObject();
31
56
  if (!existingLock) {
32
- await adapter.write(lockKey, metadata);
33
- const verify = await readLockObject();
34
- if (verify && verify.id === metadata.id) {
35
- return metadata;
57
+ const created = await writeLockObject(metadata);
58
+ if (created) {
59
+ const verify = await readLockObject();
60
+ if (verify && verify.id === metadata.id) {
61
+ return metadata;
62
+ }
36
63
  }
37
64
  }
38
65
  else {
@@ -48,10 +75,16 @@ const createRemoteStateBackend = (adapter, config) => {
48
75
  continue;
49
76
  }
50
77
  if ((0, lockUtils_1.isLockStale)(existingLock)) {
51
- throw new lockManager_1.LockError(`State is currently locked (stale lock detected).\n${(0, lockManager_1.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);
78
+ throw new lockManager_1.LockError(lang_1.lang.__('REGIONAL_LOCK_STALE', {
79
+ info: (0, lockManager_1.formatLockInfo)(existingLock),
80
+ lockId: existingLock.id,
81
+ }), existingLock);
52
82
  }
53
83
  if (Date.now() - startTime >= timeout) {
54
- throw new lockManager_1.LockError(`Failed to acquire lock after ${timeout / 1000} seconds.\n${(0, lockManager_1.formatLockInfo)(existingLock)}\nIf you are certain no other operation is running, use:\n si force-unlock ${existingLock.id}`, existingLock);
84
+ throw new lockManager_1.LockError(lang_1.lang.__('REGIONAL_LOCK_TIMEOUT', {
85
+ seconds: String(timeout / 1000),
86
+ info: (0, lockManager_1.formatLockInfo)(existingLock),
87
+ }), existingLock);
55
88
  }
56
89
  const acquiredAt = new Date(existingLock.acquiredAt);
57
90
  const minutesAgo = Math.floor((Date.now() - acquiredAt.getTime()) / 60000);
@@ -67,75 +100,60 @@ const createRemoteStateBackend = (adapter, config) => {
67
100
  attempt++;
68
101
  }
69
102
  const existingLock = await readLockObject();
70
- throw new lockManager_1.LockError(`Failed to acquire lock.\n${existingLock ? (0, lockManager_1.formatLockInfo)(existingLock) : ''}\nIf you are certain no other operation is running, use:\n si force-unlock ${existingLock?.id || 'unknown'}`, existingLock || undefined);
103
+ throw new lockManager_1.LockError(lang_1.lang.__('REGIONAL_LOCK_FAILED', { path: adapter.lockPath }), existingLock || undefined);
104
+ };
105
+ const releaseLock = async (lockId) => {
106
+ const existing = await readLockObject();
107
+ if (existing?.id === lockId)
108
+ await adapter.delete(lockKey);
109
+ if (manualLockId === lockId) {
110
+ manualLockId = null;
111
+ store.clearPlanBase();
112
+ }
113
+ };
114
+ const withLock = async (operation, fn, options, onLockAcquired) => {
115
+ const lock = await acquireLockObject(operation, options);
116
+ try {
117
+ return await writer.run(true, async () => {
118
+ if (!operation.startsWith('migrate'))
119
+ await store.verifyPlanBase();
120
+ onLockAcquired?.(lock.id);
121
+ return fn();
122
+ });
123
+ }
124
+ finally {
125
+ store.clearPlanBase();
126
+ await releaseLock(lock.id);
127
+ }
128
+ };
129
+ const mutate = async (operation, fn) => {
130
+ if (writer.getStore() || manualLockId)
131
+ return fn();
132
+ return withLock(operation, fn);
71
133
  };
72
134
  return {
73
- loadState: async (provider, app, service, stage) => {
74
- const state = await adapter.read(config.key);
75
- if (state) {
76
- const migrated = (0, stateManager_1.migrateState)(state);
77
- const stageResources = migrated.stages?.[stage]?.resources ?? {};
78
- const loaded = { ...migrated, resources: stageResources };
79
- (0, migrationMarker_1.enforceMigrationMarker)(loaded, markerAction);
80
- return loaded;
81
- }
82
- return { version: types_1.CURRENT_STATE_VERSION, provider, app, service, stages: {}, resources: {} };
83
- },
84
- patchPersisted: async (patch) => {
85
- const raw = await adapter.read(config.key);
86
- const merged = raw ? { ...(0, stateManager_1.toPersistedState)(raw) } : {};
87
- for (const [key, value] of Object.entries(patch)) {
88
- if (value === undefined) {
89
- delete merged[key];
90
- }
91
- else {
92
- merged[key] = value;
93
- }
94
- }
95
- await adapter.write(config.key, merged);
96
- },
97
- saveState: async (state, app, service, stage) => {
98
- let existing = {
99
- version: types_1.CURRENT_STATE_VERSION,
100
- provider: state.provider,
101
- app,
102
- service,
103
- stages: {},
104
- };
135
+ scope: normalized,
136
+ capabilities,
137
+ loadState: () => store.loadState(!writer.getStore() && !manualLockId),
138
+ saveState: (state) => mutate('state-save', () => store.saveState(state)),
139
+ patchPersisted: (patch) => mutate('migrate-metadata', () => store.patchPersisted(patch)),
140
+ upgradeLegacyState: (options) => mutate('migrate-upgrade', () => store.upgradeLegacyState(options)),
141
+ readStateSource: store.readStateSource,
142
+ probeWriteReadiness: store.probeWriteReadiness,
143
+ acquireLock: async (operation, options) => {
144
+ const lock = await acquireLockObject(operation, options);
105
145
  try {
106
- const raw = await adapter.read(config.key);
107
- if (raw)
108
- existing = (0, stateManager_1.toPersistedState)(raw);
109
- }
110
- catch {
111
- // noop
146
+ if (!operation.startsWith('migrate'))
147
+ await store.verifyPlanBase();
148
+ manualLockId = lock.id;
149
+ return lock.id;
112
150
  }
113
- const stateToSave = {
114
- ...existing,
115
- version: types_1.CURRENT_STATE_VERSION,
116
- app,
117
- service,
118
- provider: state.provider,
119
- stages: {
120
- ...existing.stages,
121
- [stage]: {
122
- resources: state.resources,
123
- shared: state.stages?.[stage]?.shared ?? existing.stages?.[stage]?.shared ?? {},
124
- },
125
- },
126
- };
127
- await adapter.write(config.key, stateToSave);
128
- },
129
- acquireLock: async (operation, options) => {
130
- const meta = await acquireLockObject(operation, options);
131
- return meta.id;
132
- },
133
- releaseLock: async (lockId) => {
134
- const existing = await readLockObject();
135
- if (existing && existing.id === lockId) {
136
- await adapter.delete(lockKey);
151
+ catch (error) {
152
+ await releaseLock(lock.id);
153
+ throw error;
137
154
  }
138
155
  },
156
+ releaseLock,
139
157
  forceUnlock: async (lockId) => {
140
158
  const existing = await readLockObject();
141
159
  if (!existing)
@@ -143,29 +161,11 @@ const createRemoteStateBackend = (adapter, config) => {
143
161
  if (existing.id !== lockId) {
144
162
  throw new Error(lang_1.lang.__('LOCK_ID_MISMATCH', { actualId: existing.id, providedId: lockId }));
145
163
  }
146
- await adapter.delete(lockKey);
164
+ await releaseLock(lockId);
147
165
  return true;
148
166
  },
149
- readLock: async () => {
150
- return readLockObject();
151
- },
152
- withLock: async (operation, fn, options, onLockAcquired) => {
153
- let lockId = null;
154
- try {
155
- const meta = await acquireLockObject(operation, options);
156
- lockId = meta.id;
157
- onLockAcquired?.(lockId);
158
- return await fn();
159
- }
160
- finally {
161
- if (lockId) {
162
- const existing = await readLockObject();
163
- if (existing && existing.id === lockId) {
164
- await adapter.delete(lockKey);
165
- }
166
- }
167
- }
168
- },
167
+ readLock: readLockObject,
168
+ withLock,
169
169
  };
170
170
  };
171
171
  exports.createRemoteStateBackend = createRemoteStateBackend;