@geek-fun/serverlessinsight 0.9.6 → 0.9.8

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 (90) hide show
  1. package/AGENTS.md +1 -0
  2. package/CHANGELOG.md +68 -0
  3. package/README.md +52 -11
  4. package/README.zh-CN.md +44 -10
  5. package/dist/package.json +2 -1
  6. package/dist/src/commands/deploy.js +3 -1
  7. package/dist/src/commands/destroy.js +3 -1
  8. package/dist/src/commands/forceUnlock.js +2 -2
  9. package/dist/src/commands/index.js +28 -8
  10. package/dist/src/commands/local.js +3 -2
  11. package/dist/src/commands/login.js +120 -37
  12. package/dist/src/commands/logout.js +3 -5
  13. package/dist/src/commands/migrate.js +17 -10
  14. package/dist/src/commands/plan.js +5 -1
  15. package/dist/src/commands/show.js +3 -1
  16. package/dist/src/commands/whoami.js +24 -6
  17. package/dist/src/common/aliyunClient/cdnOperations.js +64 -29
  18. package/dist/src/common/aliyunClient/dnsOperations.js +10 -0
  19. package/dist/src/common/aliyunClient/fc3Operations.js +36 -7
  20. package/dist/src/common/apiClient.js +1 -0
  21. package/dist/src/common/bindingReconcile.js +182 -0
  22. package/dist/src/common/cdnCapabilities.js +80 -0
  23. package/dist/src/common/certUtils.js +44 -5
  24. package/dist/src/common/consoleLines.js +126 -0
  25. package/dist/src/common/constants.js +4 -2
  26. package/dist/src/common/credentialStore.js +136 -39
  27. package/dist/src/common/domainUtils.js +23 -4
  28. package/dist/src/common/functionDefinition.js +4 -1
  29. package/dist/src/common/functionDomainCapabilities.js +28 -0
  30. package/dist/src/common/functionDomainReconcile.js +197 -0
  31. package/dist/src/common/lineContext.js +164 -0
  32. package/dist/src/common/planCapabilities.js +14 -27
  33. package/dist/src/common/planCompare.js +8 -7
  34. package/dist/src/common/planFormatter.js +71 -37
  35. package/dist/src/common/stateBackend/cosStateBackend.js +0 -46
  36. package/dist/src/common/stateBackend/index.js +24 -16
  37. package/dist/src/common/stateBackend/ossStateBackend.js +0 -25
  38. package/dist/src/common/stateBackend/remoteStateBackend.js +5 -22
  39. package/dist/src/common/stateBackend/saasStateBackend.js +46 -53
  40. package/dist/src/common/stateBackend/tosStateBackend.js +100 -0
  41. package/dist/src/common/stateBackend/types.js +0 -9
  42. package/dist/src/common/tencentClient/cdnOperations.js +165 -0
  43. package/dist/src/common/tencentClient/cosOperations.js +18 -2
  44. package/dist/src/common/tencentClient/dnspodOperations.js +12 -0
  45. package/dist/src/common/tencentClient/index.js +16 -0
  46. package/dist/src/common/tencentClient/scfOperations.js +23 -1
  47. package/dist/src/common/tencentClient/sslOperations.js +147 -77
  48. package/dist/src/common/volcengineClient/cdnOperations.js +221 -0
  49. package/dist/src/common/volcengineClient/dnsOperations.js +132 -0
  50. package/dist/src/common/volcengineClient/index.js +28 -0
  51. package/dist/src/common/volcengineClient/tosOperations.js +131 -1
  52. package/dist/src/lang/en.js +89 -12
  53. package/dist/src/lang/zh-CN.js +90 -11
  54. package/dist/src/parser/bucketParser.js +22 -79
  55. package/dist/src/parser/functionParser.js +13 -9
  56. package/dist/src/stack/aliyunStack/apigwResource.js +2 -1
  57. package/dist/src/stack/aliyunStack/fc3Planner.js +32 -12
  58. package/dist/src/stack/aliyunStack/fc3Resource.js +169 -121
  59. package/dist/src/stack/aliyunStack/ossPlanner.js +12 -4
  60. package/dist/src/stack/aliyunStack/ossResource.js +484 -501
  61. package/dist/src/stack/aliyunStack/ossTypes.js +21 -67
  62. package/dist/src/stack/localStack/aliyunFc.js +59 -9
  63. package/dist/src/stack/localStack/bucket.js +75 -28
  64. package/dist/src/stack/localStack/event.js +33 -24
  65. package/dist/src/stack/localStack/httpAdapter.js +1 -13
  66. package/dist/src/stack/localStack/index.js +2 -2
  67. package/dist/src/stack/localStack/localServer.js +85 -7
  68. package/dist/src/stack/scfStack/cosPlanner.js +11 -3
  69. package/dist/src/stack/scfStack/cosResource.js +339 -161
  70. package/dist/src/stack/scfStack/cosTypes.js +25 -34
  71. package/dist/src/stack/scfStack/scfPlanner.js +26 -8
  72. package/dist/src/stack/scfStack/scfResource.js +191 -124
  73. package/dist/src/stack/volcengineStack/tosPlanner.js +13 -1
  74. package/dist/src/stack/volcengineStack/tosResource.js +398 -7
  75. package/dist/src/stack/volcengineStack/tosTypes.js +27 -0
  76. package/dist/src/stack/volcengineStack/vefaasTypes.js +1 -1
  77. package/dist/src/types/domains/state.js +6 -0
  78. package/dist/src/validator/bucketSchema.js +35 -113
  79. package/dist/src/validator/cdnSchema.js +30 -0
  80. package/dist/src/validator/eventSchema.js +2 -20
  81. package/dist/src/validator/functionSchema.js +55 -8
  82. package/dist/src/validator/rootSchema.js +3 -3
  83. package/dist/src/validator/semanticValidation.js +504 -1
  84. package/dist/tsconfig.tsbuildinfo +1 -1
  85. package/package.json +2 -1
  86. package/release-notes.md +82 -6
  87. package/samples/aliyun-poc-api-cdn.yml +1 -1
  88. package/samples/aliyun-poc-bucket-cdn.yml +28 -26
  89. package/samples/aliyun-poc-domain.yml +2 -1
  90. package/samples/volcengine-poc-tos-state.yml +36 -0
@@ -7,6 +7,8 @@ exports.deleteResource = exports.updateResource = exports.readResource = exports
7
7
  const planCompare_1 = require("../../common/planCompare");
8
8
  const bucketPolicy_1 = require("../../common/bucketPolicy");
9
9
  const functionDefinition_1 = require("../../common/functionDefinition");
10
+ const bindingReconcile_1 = require("../../common/bindingReconcile");
11
+ const cdnCapabilities_1 = require("../../common/cdnCapabilities");
10
12
  const volcengineClient_1 = require("../../common/volcengineClient");
11
13
  const common_1 = require("../../common");
12
14
  const types_1 = require("../../types");
@@ -14,14 +16,11 @@ const tosTypes_1 = require("./tosTypes");
14
16
  const logger_1 = require("../../common/logger");
15
17
  const lang_1 = require("../../lang");
16
18
  const node_path_1 = __importDefault(require("node:path"));
19
+ const certUtils_1 = require("../../common/certUtils");
20
+ const domainUtils_1 = require("../../common/domainUtils");
17
21
  const ownershipTag_1 = require("../ownershipTag");
18
22
  const alreadyExists_1 = require("../alreadyExists");
19
23
  const buildTosBucketPolicyJson = (iam) => (0, bucketPolicy_1.buildBucketPolicy)(iam, 'volcengine');
20
- const buildTosPolicyInstance = (context, bucketName) => ({
21
- sid: (0, common_1.buildSid)('volcengine', 'tos-policy', context.stage, bucketName),
22
- type: tosTypes_1.TOS_BUCKET_POLICY_INSTANCE_TYPE,
23
- id: bucketName,
24
- });
25
24
  const applyBucketPolicy = async (client, bucket, owned = false) => {
26
25
  const policy = buildTosBucketPolicyJson(bucket.iam);
27
26
  if (!policy) {
@@ -38,6 +37,315 @@ const deleteBucketPolicy = async (client, bucketName) => {
38
37
  await client.tos.deleteBucketPolicy(bucketName);
39
38
  logger_1.logger.info(lang_1.lang.__('BUCKET_POLICY_DELETED', { bucketName }));
40
39
  };
40
+ const buildTosPolicyInstance = (context, bucketName) => ({
41
+ sid: (0, common_1.buildSid)('volcengine', 'tos-policy', context.stage, bucketName),
42
+ type: tosTypes_1.TOS_BUCKET_POLICY_INSTANCE_TYPE,
43
+ id: bucketName,
44
+ });
45
+ const asVolcengineGroup = (group) => group;
46
+ const VOLCENGINE_BINDING_ROLE_BY_TYPE = {
47
+ [types_1.ResourceTypeEnum.VOLCENGINE_TOS_DNS_CNAME]: 'dns',
48
+ [types_1.ResourceTypeEnum.VOLCENGINE_CDN_DOMAIN]: 'cdn',
49
+ [types_1.ResourceTypeEnum.VOLCENGINE_CDN_DNS_CNAME]: 'cdnDns',
50
+ };
51
+ const storedAccelerationEnabled = (definition) => (definition.accelerate ?? definition.accelerateEnabled) === true;
52
+ // Inline PEM reaches the shared resolver only on CDN bindings — semantic
53
+ // validation rejects it on direct volcengine bindings (TOS has no
54
+ // bucket-domain certificate-upload API).
55
+ const resolveCdnCertificateId = async (client, resolved) => {
56
+ if (resolved.mode === 'reference') {
57
+ logger_1.logger.info(lang_1.lang.__('CERT_RESOLVED_REFERENCE', {
58
+ name: resolved.certificateId,
59
+ certId: resolved.certificateId,
60
+ }));
61
+ return resolved.certificateId;
62
+ }
63
+ logger_1.logger.info(lang_1.lang.__('CERT_RESOLVED_UPLOAD', {
64
+ name: resolved.certificateName,
65
+ }));
66
+ // Repeatable false — the CDN returns the existing certificate id when the
67
+ // same content was uploaded before, so redeploys stay idempotent.
68
+ const certInfo = await client.cdn.addCertificate({
69
+ certificate: resolved.certificateBody,
70
+ privateKey: resolved.certificatePrivateKey,
71
+ name: resolved.certificateName,
72
+ });
73
+ return certInfo.certificateId;
74
+ };
75
+ // ---------------------------------------------------------------------------
76
+ // Direct / accelerate-only bindings
77
+ // ---------------------------------------------------------------------------
78
+ const bindTosDomain = async (context, client, bucket, binding, accelerate, instances, storedDns) => {
79
+ const hostname = binding.domain_name;
80
+ logger_1.logger.info(lang_1.lang.__('BINDING_CUSTOM_DOMAIN_TO_BUCKET', {
81
+ domain: hostname,
82
+ bucketName: bucket.name,
83
+ }));
84
+ // certificate_id passes through as the TOS-side CertId (a reference into
85
+ // the Volcengine certificate center); inline PEM cannot reach TOS.
86
+ await client.tos.putBucketCustomDomain(bucket.name, hostname, binding.certificate_id ? { certId: binding.certificate_id } : undefined);
87
+ const cname = accelerate
88
+ ? (0, volcengineClient_1.getTosAccelerateEndpoint)(bucket.name)
89
+ : (0, volcengineClient_1.getTosEndpoint)(bucket.name, context.region);
90
+ const mainDomain = (0, domainUtils_1.extractMainDomain)(hostname);
91
+ const recordConfig = {
92
+ domainName: mainDomain,
93
+ host: (0, domainUtils_1.extractHostRecord)(hostname, mainDomain),
94
+ type: 'CNAME',
95
+ value: cname,
96
+ ttl: 600,
97
+ };
98
+ // An si-recorded record is repointed in place when the CNAME target changes
99
+ // (e.g. acceleration newly enabled) — records si does not hold are managed
100
+ // by the value-matched add flow, which leaves foreign records untouched.
101
+ let dnsRecordId;
102
+ if (storedDns?.dnsRecordId && storedDns.cname && storedDns.cname !== cname) {
103
+ await client.dns.updateDomainRecord(storedDns.dnsRecordId, recordConfig);
104
+ logger_1.logger.info(lang_1.lang.__('DNS_RECORD_UPDATED', { domain: hostname }));
105
+ dnsRecordId = storedDns.dnsRecordId;
106
+ }
107
+ else {
108
+ dnsRecordId = await client.dns.addDomainRecord(recordConfig);
109
+ }
110
+ const id = dnsRecordId ?? hostname;
111
+ instances.push({
112
+ sid: (0, common_1.buildSid)('volcengine', 'dns', context.stage, id),
113
+ id,
114
+ type: types_1.ResourceTypeEnum.VOLCENGINE_TOS_DNS_CNAME,
115
+ domain: hostname,
116
+ cname,
117
+ ...(dnsRecordId ? { dnsRecordId } : {}),
118
+ });
119
+ };
120
+ // ---------------------------------------------------------------------------
121
+ // Per-binding CDN delivery (issue #294)
122
+ // ---------------------------------------------------------------------------
123
+ /**
124
+ * CDN pulls straight from the TOS bucket via its virtual-host endpoint
125
+ * (`InstanceType: 'TOS'`, primary slot). The custom domain is NOT bound to the
126
+ * bucket in CDN mode — unlike the Tencent COS path, TOS custom-domain
127
+ * registration plays no role in CDN origin pulls and would require
128
+ * domain-ownership verification.
129
+ *
130
+ * The provider requires a TOS origin's `OriginHost` to equal the origin
131
+ * address, so both carry the bucket endpoint.
132
+ */
133
+ const buildTosOrigin = (bucketName, region) => {
134
+ const endpoint = (0, volcengineClient_1.getTosEndpoint)(bucketName, region);
135
+ return {
136
+ originLines: [
137
+ {
138
+ originType: 'primary',
139
+ instanceType: 'TOS',
140
+ bucketName,
141
+ region,
142
+ address: endpoint,
143
+ originHost: endpoint,
144
+ },
145
+ ],
146
+ };
147
+ };
148
+ const ensureCdnDns = async (context, client, hostname, cname, instances) => {
149
+ // The CDN CNAME for a hostname is stable, so an existing value-matched
150
+ // record is adopted as-is — only a missing record is created here (and a
151
+ // foreign record is left untouched with a warning, never overwritten).
152
+ const mainDomain = (0, domainUtils_1.extractMainDomain)(hostname);
153
+ const dnsRecordId = await client.dns.addDomainRecord({
154
+ domainName: mainDomain,
155
+ host: (0, domainUtils_1.extractHostRecord)(hostname, mainDomain),
156
+ type: 'CNAME',
157
+ value: cname,
158
+ ttl: 600,
159
+ });
160
+ const id = dnsRecordId || hostname;
161
+ instances.push({
162
+ sid: (0, common_1.buildSid)('volcengine', 'cdn-dns', context.stage, id),
163
+ id,
164
+ type: types_1.ResourceTypeEnum.VOLCENGINE_CDN_DNS_CNAME,
165
+ domain: hostname,
166
+ cname,
167
+ ...(dnsRecordId ? { dnsRecordId } : {}),
168
+ });
169
+ };
170
+ const applyCdnOptions = async (client, hostname, cdnObj) => {
171
+ if (cdnObj?.cache_ttl != null || cdnObj?.ignore_query_string != null) {
172
+ await client.cdn.applyCacheConfig(hostname, cdnObj?.cache_ttl, cdnObj?.ignore_query_string);
173
+ }
174
+ if (cdnObj?.compression != null) {
175
+ await client.cdn.applyCompression(hostname, cdnObj.compression);
176
+ }
177
+ if (cdnObj?.force_redirect_https != null) {
178
+ await client.cdn.applyHttpsRedirect(hostname, cdnObj.force_redirect_https);
179
+ }
180
+ };
181
+ const createCdnBinding = async (context, client, bucket, binding, certificate, instances) => {
182
+ const hostname = binding.domain_name;
183
+ const cdnObj = (0, bindingReconcile_1.getBindingCdnOptions)(binding);
184
+ logger_1.logger.info(lang_1.lang.__('CREATING_CDN_DISTRIBUTION', { domain: hostname, origin: bucket.name }));
185
+ const originProtocol = (0, cdnCapabilities_1.mapOriginProtocol)('volcengine', cdnObj?.origin_protocol);
186
+ try {
187
+ await client.cdn.addCdnDomain({
188
+ domain: hostname,
189
+ serviceType: cdnObj?.cdn_type ?? 'web',
190
+ serviceRegion: (0, cdnCapabilities_1.mapCdnScope)('volcengine', cdnObj?.scope),
191
+ origin: buildTosOrigin(bucket.name, context.region),
192
+ ...(originProtocol ? { originProtocol } : {}),
193
+ });
194
+ }
195
+ catch (error) {
196
+ // Partial-failure retry: a CDN domain created by an interrupted earlier run
197
+ // must not block convergence — probe, verify ownership and continue as an
198
+ // update. A domain whose origin does not point at this bucket is not ours
199
+ // to adopt (repo convention: refuse un-owned resources); one still being
200
+ // deleted by a prior teardown is not adoptable either — surface the
201
+ // original conflict so the user retries after the deletion completes.
202
+ const existing = await client.cdn.describeCdnDomain(hostname);
203
+ if (!existing || existing.status === 'deleting')
204
+ throw error;
205
+ const origins = existing.origin ?? [];
206
+ const bucketEndpoint = (0, volcengineClient_1.getTosEndpoint)(bucket.name, context.region);
207
+ if (!origins.includes(bucket.name) && !origins.includes(bucketEndpoint)) {
208
+ throw new Error(lang_1.lang.__('CDN_DOMAIN_EXISTS_NOT_OWNED', { domain: hostname, bucketName: bucket.name }), { cause: error });
209
+ }
210
+ logger_1.logger.info(lang_1.lang.__('RESOURCE_OWNERSHIP_TAG_ADOPT', {
211
+ resourceType: 'CDN domain',
212
+ name: hostname,
213
+ tag: 'state',
214
+ }));
215
+ }
216
+ await updateCdnBinding(context, client, bucket, binding, certificate, { hostname, mode: 'cdn', cdn: { ...cdnObj, enabled: true } }, {
217
+ cdn: {
218
+ sid: (0, common_1.buildSid)('volcengine', 'cdn', context.stage, hostname),
219
+ id: hostname,
220
+ type: types_1.ResourceTypeEnum.VOLCENGINE_CDN_DOMAIN,
221
+ domainName: hostname,
222
+ pending: true,
223
+ },
224
+ }, instances);
225
+ };
226
+ const updateCdnBinding = async (context, client, bucket, binding, certificate, stored, group, instances) => {
227
+ const hostname = binding.domain_name;
228
+ const cdnObj = (0, bindingReconcile_1.getBindingCdnOptions)(binding);
229
+ const volcengineGroup = asVolcengineGroup(group);
230
+ const pending = volcengineGroup?.cdn?.pending === true;
231
+ const distributionIndex = instances.length;
232
+ instances.push({
233
+ ...volcengineGroup?.cdn,
234
+ sid: volcengineGroup?.cdn?.sid ?? (0, common_1.buildSid)('volcengine', 'cdn', context.stage, hostname),
235
+ id: hostname,
236
+ type: types_1.ResourceTypeEnum.VOLCENGINE_CDN_DOMAIN,
237
+ domainName: hostname,
238
+ pending: true,
239
+ });
240
+ // Volcengine updates service type / acceleration region / origin in place
241
+ // via UpdateCdnConfig — no distribution recreation needed.
242
+ if (!pending && (0, bindingReconcile_1.cdnDeliveryChanged)(stored.cdn, cdnObj)) {
243
+ await client.cdn.updateCdnDelivery(hostname, {
244
+ serviceType: cdnObj?.cdn_type ?? 'web',
245
+ serviceRegion: (0, cdnCapabilities_1.mapCdnScope)('volcengine', cdnObj?.scope),
246
+ origin: buildTosOrigin(bucket.name, context.region),
247
+ });
248
+ }
249
+ // Certificate first: the provider requires HTTPS to be enabled before a
250
+ // forced redirect can be turned on (HTTPS.ForcedRedirect error contract).
251
+ if (certificate) {
252
+ const certificateHash = (0, certUtils_1.resolveBindingCertificateHash)({
253
+ certificate_id: binding.certificate_id,
254
+ certificate_body: binding.certificate_body,
255
+ certificate_private_key: binding.certificate_private_key,
256
+ });
257
+ if ((0, bindingReconcile_1.shouldDeployBindingCertificate)({
258
+ pending,
259
+ certificateId: certificate.mode === 'reference' ? certificate.certificateId : undefined,
260
+ certificateHash,
261
+ stored,
262
+ })) {
263
+ logger_1.logger.info(lang_1.lang.__('CDN_DEPLOYING_CERTIFICATE', { domain: hostname }));
264
+ const certId = await resolveCdnCertificateId(client, certificate);
265
+ await client.cdn.applyCertificate(hostname, certId);
266
+ }
267
+ else {
268
+ logger_1.logger.info(lang_1.lang.__('CDN_CERT_UNCHANGED_SKIPPED', { domain: hostname }));
269
+ }
270
+ }
271
+ const applyOptions = pending || (0, bindingReconcile_1.cdnDeliveryChanged)(stored.cdn, cdnObj);
272
+ if (applyOptions) {
273
+ await applyCdnOptions(client, hostname, cdnObj);
274
+ }
275
+ else {
276
+ // Omitted options resolve to their documented defaults (issue #294):
277
+ // cache_ttl/ignore_query_string → provider-default cache behavior, the
278
+ // boolean knobs → off. The apply layer renders null as the default module,
279
+ // so a dropped option converges the cloud instead of leaving the previous
280
+ // setting behind (same contract on aliyun/tencent).
281
+ if ((stored.cdn?.cache_ttl ?? null) !== (cdnObj?.cache_ttl ?? null) ||
282
+ (stored.cdn?.ignore_query_string ?? null) !== (cdnObj?.ignore_query_string ?? null)) {
283
+ await client.cdn.applyCacheConfig(hostname, cdnObj?.cache_ttl ?? null, cdnObj?.ignore_query_string ?? null);
284
+ }
285
+ if ((stored.cdn?.compression ?? null) !== (cdnObj?.compression ?? null)) {
286
+ await client.cdn.applyCompression(hostname, cdnObj?.compression ?? false);
287
+ }
288
+ if ((stored.cdn?.force_redirect_https ?? null) !== (cdnObj?.force_redirect_https ?? null)) {
289
+ await client.cdn.applyHttpsRedirect(hostname, cdnObj?.force_redirect_https ?? false);
290
+ }
291
+ if ((stored.cdn?.origin_protocol ?? null) !== (cdnObj?.origin_protocol ?? null)) {
292
+ // An explicit protocol is required to reset the pull behavior; the
293
+ // provider default when unset is plain http.
294
+ await client.cdn.updateCdnDelivery(hostname, {
295
+ originProtocol: (0, cdnCapabilities_1.mapOriginProtocol)('volcengine', cdnObj?.origin_protocol) ??
296
+ (0, cdnCapabilities_1.mapOriginProtocol)('volcengine', 'http'),
297
+ });
298
+ }
299
+ }
300
+ if (volcengineGroup?.cdnDns) {
301
+ instances.push(volcengineGroup.cdnDns);
302
+ }
303
+ else {
304
+ const detail = await client.cdn.describeCdnDomain(hostname);
305
+ if (!detail?.cname) {
306
+ throw new Error(lang_1.lang.__('FAILED_TO_REFRESH_STATE', { resourceType: 'CDN domain', name: hostname }));
307
+ }
308
+ await ensureCdnDns(context, client, hostname, detail.cname, instances);
309
+ instances[distributionIndex] = { ...instances[distributionIndex], cname: detail.cname };
310
+ }
311
+ const { pending: _pending, ...ready } = instances[distributionIndex];
312
+ instances[distributionIndex] = ready;
313
+ };
314
+ const teardownTosBinding = async (client, bucketName, stored, group) => {
315
+ if (!group)
316
+ return;
317
+ const volcengineGroup = asVolcengineGroup(group);
318
+ if (stored.mode === 'cdn') {
319
+ const distributionName = volcengineGroup?.cdn?.domainName ?? stored.hostname;
320
+ await client.cdn.deleteCdnDomain(distributionName);
321
+ logger_1.logger.info(lang_1.lang.__('CDN_DOMAIN_DELETED', { domain: distributionName }));
322
+ if (volcengineGroup?.cdnDns?.dnsRecordId) {
323
+ try {
324
+ await client.dns.deleteDomainRecord(volcengineGroup.cdnDns.dnsRecordId);
325
+ }
326
+ catch (error) {
327
+ // Best-effort delivery-record cleanup: the binding teardown must not
328
+ // fail because the record is already gone.
329
+ logger_1.logger.debug(lang_1.lang.__('DNS_OPERATION_ERROR', { error: String(error) }));
330
+ }
331
+ }
332
+ return;
333
+ }
334
+ // direct / accelerate-only binding: TOS unbind + delivery CNAME removal.
335
+ const domain = volcengineGroup?.dns?.domain ?? stored.hostname;
336
+ await client.tos.deleteBucketCustomDomain(bucketName, domain);
337
+ if (volcengineGroup?.dns?.dnsRecordId) {
338
+ try {
339
+ await client.dns.deleteDomainRecord(volcengineGroup.dns.dnsRecordId);
340
+ }
341
+ catch (error) {
342
+ logger_1.logger.debug(lang_1.lang.__('DNS_OPERATION_ERROR', { error: String(error) }));
343
+ }
344
+ }
345
+ };
346
+ // ---------------------------------------------------------------------------
347
+ // Resource entry points
348
+ // ---------------------------------------------------------------------------
41
349
  const createResource = async (context, bucket, state) => {
42
350
  const config = (0, tosTypes_1.bucketToTosConfig)(bucket);
43
351
  const client = (0, volcengineClient_1.createVolcengineClient)(context);
@@ -115,6 +423,24 @@ const createResource = async (context, bucket, state) => {
115
423
  bucketInfo = probe;
116
424
  }
117
425
  const instances = [(0, tosTypes_1.buildTosInstanceFromProvider)(bucketInfo, sid)];
426
+ // Bucket-level transfer acceleration (issue #294)
427
+ if (bucket.accelerate === true) {
428
+ logger_1.logger.info(lang_1.lang.__('ENABLING_OSS_TRANSFER_ACCELERATION', { bucketName: bucket.name }));
429
+ await client.tos.putBucketTransferAcceleration(bucket.name, true);
430
+ }
431
+ const bindingLoop = async (accelerate) => {
432
+ for (const rawBinding of bucket.domains ?? []) {
433
+ const hostname = (0, domainUtils_1.normalizeHostname)(rawBinding.domain_name);
434
+ const binding = { ...rawBinding, domain_name: hostname };
435
+ if ((0, bindingReconcile_1.isBindingCdnEnabled)(binding)) {
436
+ await createCdnBinding(context, client, bucket, binding, (0, bindingReconcile_1.resolveBindingCertificate)(binding), instances);
437
+ }
438
+ else {
439
+ await bindTosDomain(context, client, bucket, binding, accelerate, instances);
440
+ }
441
+ }
442
+ };
443
+ await bindingLoop(bucket.accelerate === true);
118
444
  if (bucket.website?.code) {
119
445
  const codePath = node_path_1.default.resolve(process.cwd(), bucket.website.code);
120
446
  await client.tos.uploadFiles(bucket.name, codePath);
@@ -167,11 +493,68 @@ const updateResource = async (context, bucket, state) => {
167
493
  }
168
494
  const sid = (0, common_1.buildSid)('volcengine', 'tos', context.stage, bucket.name);
169
495
  const logicalId = `buckets.${bucket.key}`;
496
+ const existingState = state.resources[logicalId];
170
497
  const websiteCodeHash = bucket.website?.code
171
498
  ? (0, common_1.computeDirectoryHash)(node_path_1.default.resolve(process.cwd(), bucket.website.code))
172
499
  : undefined;
173
500
  const instances = [(0, tosTypes_1.buildTosInstanceFromProvider)(bucketInfo, sid)];
174
- const policyOwned = (0, bucketPolicy_1.isOwnedBucketPolicy)(tosTypes_1.TOS_BUCKET_POLICY_INSTANCE_TYPE, state.resources[logicalId]?.instances, state.resources[logicalId]?.definition);
501
+ // Per-binding reconcile (issue #294): bindings are identified by normalized
502
+ // hostname — re-binding an existing host is idempotent, delivery mode
503
+ // switches tear down and rebuild at binding level, undeclared stored
504
+ // bindings are torn down.
505
+ const desiredAccelerate = bucket.accelerate === true;
506
+ const groups = (0, bindingReconcile_1.groupBindingInstances)(existingState?.instances, VOLCENGINE_BINDING_ROLE_BY_TYPE);
507
+ const storedAccelerate = storedAccelerationEnabled(existingState?.definition ?? {});
508
+ const storedBindings = (0, bindingReconcile_1.buildStoredBindings)(existingState, groups, {
509
+ accelerateEnabled: storedAccelerate,
510
+ });
511
+ // Acceleration cannot be disabled through update — stored accelerate-only
512
+ // bindings would be orphaned with no way to repoint them here (aliyun
513
+ // parity). Fail loudly instead of drifting.
514
+ if (storedAccelerate && !desiredAccelerate) {
515
+ throw new Error(lang_1.lang.__('CDN_ACCELERATE_CONFIG_CHANGED', { bucketName: bucket.name }));
516
+ }
517
+ if (desiredAccelerate && !storedAccelerate) {
518
+ logger_1.logger.info(lang_1.lang.__('ENABLING_OSS_TRANSFER_ACCELERATION', { bucketName: bucket.name }));
519
+ await client.tos.putBucketTransferAcceleration(bucket.name, true);
520
+ }
521
+ const desiredDomains = bucket.domains ?? [];
522
+ const desiredHosts = new Set(desiredDomains.map((b) => (0, domainUtils_1.normalizeHostname)(b.domain_name)));
523
+ for (const rawBinding of desiredDomains) {
524
+ const hostname = (0, domainUtils_1.normalizeHostname)(rawBinding.domain_name);
525
+ const binding = { ...rawBinding, domain_name: hostname };
526
+ let stored = storedBindings.get(hostname);
527
+ const cdnEnabled = (0, bindingReconcile_1.isBindingCdnEnabled)(binding);
528
+ // Binding-level delivery-mode switch: tear the previous delivery group
529
+ // down and rebuild in the new mode below. A direct binding under newly
530
+ // enabled acceleration is rebound in place (its TOS rule is identical).
531
+ if (stored &&
532
+ stored.mode !== (0, bindingReconcile_1.desiredBindingMode)(cdnEnabled, desiredAccelerate) &&
533
+ !(stored.mode === 'direct' && desiredAccelerate && !cdnEnabled)) {
534
+ await teardownTosBinding(client, bucket.name, stored, groups.get(hostname));
535
+ stored = undefined;
536
+ }
537
+ if (cdnEnabled) {
538
+ const certificate = (0, bindingReconcile_1.resolveBindingCertificate)(binding);
539
+ if (stored?.mode === 'cdn' && groups.get(hostname)?.cdn) {
540
+ await updateCdnBinding(context, client, bucket, binding, certificate, stored, groups.get(hostname), instances);
541
+ }
542
+ else {
543
+ await createCdnBinding(context, client, bucket, binding, certificate, instances);
544
+ }
545
+ }
546
+ else {
547
+ await bindTosDomain(context, client, bucket, binding, desiredAccelerate, instances, asVolcengineGroup(groups.get(hostname))?.dns);
548
+ }
549
+ }
550
+ // Converge removed bindings: any stored binding no longer declared is torn down.
551
+ for (const [hostname, stored] of storedBindings) {
552
+ if (desiredHosts.has(hostname))
553
+ continue;
554
+ logger_1.logger.info(lang_1.lang.__('BINDING_REMOVING_DOMAIN', { domain: hostname, bucketName: bucket.name }));
555
+ await teardownTosBinding(client, bucket.name, stored, groups.get(hostname));
556
+ }
557
+ const policyOwned = (0, bucketPolicy_1.isOwnedBucketPolicy)(tosTypes_1.TOS_BUCKET_POLICY_INSTANCE_TYPE, existingState?.instances, existingState?.definition);
175
558
  await applyBucketPolicy(client, bucket, policyOwned);
176
559
  if (buildTosBucketPolicyJson(bucket.iam)) {
177
560
  instances.push(buildTosPolicyInstance(context, bucket.name));
@@ -180,7 +563,7 @@ const updateResource = async (context, bucket, state) => {
180
563
  const resourceState = {
181
564
  mode: 'managed',
182
565
  region: context.region,
183
- definition: (0, functionDefinition_1.withLifecycleIntent)((0, planCompare_1.mergeManagedIntent)(state.resources[logicalId]?.definition ?? {}, tosDefinition), tosDefinition, ['policy']),
566
+ definition: (0, functionDefinition_1.withLifecycleIntent)((0, planCompare_1.mergeManagedIntent)(existingState?.definition ?? {}, tosDefinition), tosDefinition, ['policy']),
184
567
  instances,
185
568
  lastUpdated: new Date().toISOString(),
186
569
  };
@@ -189,8 +572,16 @@ const updateResource = async (context, bucket, state) => {
189
572
  exports.updateResource = updateResource;
190
573
  const deleteResource = async (context, bucketName, logicalId, state) => {
191
574
  const client = (0, volcengineClient_1.createVolcengineClient)(context);
575
+ const existingState = state.resources[logicalId];
576
+ const groups = (0, bindingReconcile_1.groupBindingInstances)(existingState?.instances, VOLCENGINE_BINDING_ROLE_BY_TYPE);
577
+ const storedBindings = (0, bindingReconcile_1.buildStoredBindings)(existingState, groups, {
578
+ accelerateEnabled: storedAccelerationEnabled(existingState?.definition ?? {}),
579
+ });
192
580
  // Clean up bucket policy before deleting bucket
193
581
  await deleteBucketPolicy(client, bucketName);
582
+ for (const [hostname, stored] of storedBindings) {
583
+ await teardownTosBinding(client, bucketName, stored, groups.get(hostname));
584
+ }
194
585
  try {
195
586
  await client.tos.deleteBucket(bucketName);
196
587
  }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.buildTosInstanceFromProvider = exports.cloudTosToDefinition = exports.extractTosBucketDefinition = exports.bucketToTosConfig = exports.TOS_BUCKET_POLICY_INSTANCE_TYPE = void 0;
4
4
  const bucketPolicy_1 = require("../../common/bucketPolicy");
5
+ const bindingReconcile_1 = require("../../common/bindingReconcile");
5
6
  const aclMap = {
6
7
  PRIVATE: 'private',
7
8
  PUBLIC_READ: 'public-read',
@@ -27,9 +28,28 @@ const bucketToTosConfig = (bucket) => {
27
28
  errorDocument: bucket.website.index,
28
29
  };
29
30
  }
31
+ if (bucket.domains?.length) {
32
+ config.domains = bucket.domains.map((binding) => ({
33
+ domain: binding.domain_name,
34
+ certificateId: binding.certificate_id,
35
+ certificateBody: binding.certificate_body,
36
+ certificatePrivateKey: binding.certificate_private_key,
37
+ ...(binding.cdn != null ? { cdn: binding.cdn } : {}),
38
+ }));
39
+ }
40
+ if (bucket.accelerate != null) {
41
+ config.accelerateEnabled = bucket.accelerate === true;
42
+ }
30
43
  return config;
31
44
  };
32
45
  exports.bucketToTosConfig = bucketToTosConfig;
46
+ const tosBindingToDefinitionEntry = (binding) => (0, bindingReconcile_1.bindingToDefinitionEntry)({
47
+ domainName: binding.domain,
48
+ certificateId: binding.certificateId,
49
+ certificateBody: binding.certificateBody,
50
+ certificatePrivateKey: binding.certificatePrivateKey,
51
+ ...(binding.cdn != null ? { cdn: binding.cdn } : {}),
52
+ });
33
53
  const extractTosBucketDefinition = (config, websiteCodeHash) => {
34
54
  return {
35
55
  bucketName: config.bucketName,
@@ -42,6 +62,13 @@ const extractTosBucketDefinition = (config, websiteCodeHash) => {
42
62
  }
43
63
  : null,
44
64
  websiteCodeHash: websiteCodeHash ?? null,
65
+ // Bindings are canonicalized: sorted by hostname so list reordering in
66
+ // YAML never produces a plan change (issue #279/#294).
67
+ domains: (config.domains ?? [])
68
+ .slice()
69
+ .sort((a, b) => a.domain.localeCompare(b.domain))
70
+ .map(tosBindingToDefinitionEntry),
71
+ accelerate: config.accelerateEnabled ?? false,
45
72
  policy: (0, bucketPolicy_1.serializeBucketPolicy)(config.iam, 'volcengine'),
46
73
  };
47
74
  };
@@ -71,7 +71,7 @@ const extractVefaasDefinition = (config, codeHash, fn) => {
71
71
  logConfig: config.logConfig,
72
72
  }, fn,
73
73
  // veFaaS executor never applies HTTP triggers or custom domains; persisting them would record intent the cloud never receives.
74
- { http: false, domain: false });
74
+ { http: false, domains: false });
75
75
  };
76
76
  exports.extractVefaasDefinition = extractVefaasDefinition;
77
77
  const buildDefaultTrustPolicy = (trustedServices) => {
@@ -7,6 +7,8 @@ var ResourceTypeEnum;
7
7
  ResourceTypeEnum["SCF_FUNCTION"] = "SCF_FUNCTION";
8
8
  ResourceTypeEnum["COS_BUCKET"] = "COS_BUCKET";
9
9
  ResourceTypeEnum["COS_DNS_CNAME"] = "TENCENT_COS_DNS_CNAME";
10
+ ResourceTypeEnum["TENCENT_CDN_DOMAIN"] = "TENCENT_CDN_DOMAIN";
11
+ ResourceTypeEnum["TENCENT_CDN_DNS_CNAME"] = "TENCENT_CDN_DNS_CNAME";
10
12
  ResourceTypeEnum["TDSQL_C_SERVERLESS"] = "TDSQL_C_SERVERLESS";
11
13
  // Aliyun - Primary Resources
12
14
  ResourceTypeEnum["ALIYUN_FC3_FUNCTION"] = "ALIYUN_FC3_FUNCTION";
@@ -39,6 +41,10 @@ var ResourceTypeEnum;
39
41
  // Volcengine - Primary Resources
40
42
  ResourceTypeEnum["VOLCENGINE_VEFAAS_FUNCTION"] = "VOLCENGINE_VEFAAS_FUNCTION";
41
43
  ResourceTypeEnum["VOLCENGINE_TOS_BUCKET"] = "VOLCENGINE_TOS_BUCKET";
44
+ // Volcengine - TOS bucket domain bindings (issue #294)
45
+ ResourceTypeEnum["VOLCENGINE_TOS_DNS_CNAME"] = "VOLCENGINE_TOS_DNS_CNAME";
46
+ ResourceTypeEnum["VOLCENGINE_CDN_DOMAIN"] = "VOLCENGINE_CDN_DOMAIN";
47
+ ResourceTypeEnum["VOLCENGINE_CDN_DNS_CNAME"] = "VOLCENGINE_CDN_DNS_CNAME";
42
48
  })(ResourceTypeEnum || (exports.ResourceTypeEnum = ResourceTypeEnum = {}));
43
49
  exports.CURRENT_STATE_VERSION = '4.0';
44
50
  exports.LEGACY_STATE_VERSION = '3.0';