@geek-fun/serverlessinsight 0.9.2 → 0.9.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/.serverlessinsight/state-cdn-flow-app-cdn-flow-service.json.backup +1 -1
  2. package/.serverlessinsight/state-iam-test-app-iam-test-service.json.backup +2 -2
  3. package/.serverlessinsight/state-insight-poc-app-insight-poc-tencent.json.backup +4 -8
  4. package/.serverlessinsight/state-insight-poc-app-log-insight-poc-tencent-log.json.backup +3 -8
  5. package/.serverlessinsight/state-insight-poc-deploy-app-insight-poc-deploy.json.backup +4 -4
  6. package/.serverlessinsight/state-insight-poc-destroy-app-insight-poc-destroy.json.backup +1 -1
  7. package/.serverlessinsight/state-insight-poc-timer-app-insight-poc-timer.json.backup +28 -5
  8. package/.serverlessinsight/state-insight-volc-app-insight-volc.json.backup +2 -2
  9. package/.serverlessinsight/state-insight-volc-nested-app-insight-volc-nested.json.backup +3 -3
  10. package/.serverlessinsight/state-nested-drift-app-nested-drift-service.json.backup +20 -5
  11. package/.serverlessinsight/state-role-reconcile-test-app-role-reconcile-test-service.json.backup +3 -3
  12. package/AGENTS.md +10 -0
  13. package/CHANGELOG.md +34 -0
  14. package/README.md +29 -15
  15. package/README.zh-CN.md +7 -14
  16. package/dist/package.json +1 -1
  17. package/dist/src/commands/index.js +19 -3
  18. package/dist/src/commands/local.js +16 -3
  19. package/dist/src/commands/plan.js +10 -3
  20. package/dist/src/common/aliyunClient/apigwOperations.js +17 -1
  21. package/dist/src/common/aliyunClient/fc3Operations.js +7 -1
  22. package/dist/src/common/aliyunClient/ossOperations.js +214 -221
  23. package/dist/src/common/bucketPolicy.js +63 -0
  24. package/dist/src/common/domainUtils.js +4 -1
  25. package/dist/src/common/functionDefinition.js +25 -0
  26. package/dist/src/common/index.js +4 -0
  27. package/dist/src/common/jsonOutput.js +6 -1
  28. package/dist/src/common/nameBuilder.js +3 -1
  29. package/dist/src/common/planCapabilities.js +119 -0
  30. package/dist/src/common/planCompare.js +119 -66
  31. package/dist/src/common/planFormatter.js +22 -14
  32. package/dist/src/common/planRedaction.js +26 -0
  33. package/dist/src/common/refreshPlanner.js +66 -21
  34. package/dist/src/common/tencentClient/cosOperations.js +326 -322
  35. package/dist/src/common/tencentClient/scfOperations.js +9 -2
  36. package/dist/src/common/tencentClient/tdsqlcOperations.js +6 -11
  37. package/dist/src/common/triggerMapper.js +16 -1
  38. package/dist/src/common/volcengineClient/apigwOperations.js +82 -0
  39. package/dist/src/common/volcengineClient/iamOperations.js +27 -3
  40. package/dist/src/common/volcengineClient/tosOperations.js +62 -56
  41. package/dist/src/lang/en.js +28 -4
  42. package/dist/src/lang/zh-CN.js +28 -4
  43. package/dist/src/parser/eventParser.js +1 -0
  44. package/dist/src/parser/functionParser.js +1 -1
  45. package/dist/src/stack/aliyunStack/apigwExecutor.js +1 -1
  46. package/dist/src/stack/aliyunStack/apigwPlanner.js +103 -23
  47. package/dist/src/stack/aliyunStack/apigwResource.js +4 -3
  48. package/dist/src/stack/aliyunStack/apigwTypes.js +3 -7
  49. package/dist/src/stack/aliyunStack/deployer.js +2 -0
  50. package/dist/src/stack/aliyunStack/esServerlessTypes.js +8 -4
  51. package/dist/src/stack/aliyunStack/fc3Executor.js +7 -3
  52. package/dist/src/stack/aliyunStack/fc3Planner.js +124 -155
  53. package/dist/src/stack/aliyunStack/fc3Resource.js +48 -52
  54. package/dist/src/stack/aliyunStack/fc3Types.js +30 -16
  55. package/dist/src/stack/aliyunStack/ossPlanner.js +15 -1
  56. package/dist/src/stack/aliyunStack/ossResource.js +113 -102
  57. package/dist/src/stack/aliyunStack/ossTypes.js +20 -20
  58. package/dist/src/stack/localStack/aliyunFc.js +1 -6
  59. package/dist/src/stack/localStack/event.js +31 -60
  60. package/dist/src/stack/localStack/function.js +24 -65
  61. package/dist/src/stack/localStack/functionRunner.js +10 -2
  62. package/dist/src/stack/localStack/httpAdapter.js +54 -0
  63. package/dist/src/stack/localStack/httpError.js +5 -0
  64. package/dist/src/stack/localStack/legacyLambda.js +26 -0
  65. package/dist/src/stack/localStack/localContext.js +29 -0
  66. package/dist/src/stack/localStack/localRoute.js +12 -0
  67. package/dist/src/stack/localStack/localRuntimes.js +22 -0
  68. package/dist/src/stack/localStack/localServer.js +8 -3
  69. package/dist/src/stack/localStack/requestBody.js +24 -0
  70. package/dist/src/stack/localStack/requestId.js +18 -0
  71. package/dist/src/stack/localStack/tencent.js +147 -0
  72. package/dist/src/stack/localStack/timer.js +23 -2
  73. package/dist/src/stack/localStack/volcengine.js +68 -0
  74. package/dist/src/stack/scfStack/cosPlanner.js +9 -14
  75. package/dist/src/stack/scfStack/cosResource.js +42 -50
  76. package/dist/src/stack/scfStack/cosTypes.js +19 -14
  77. package/dist/src/stack/scfStack/deployer.js +2 -0
  78. package/dist/src/stack/scfStack/scfExecutor.js +7 -3
  79. package/dist/src/stack/scfStack/scfHttpTrigger.js +106 -0
  80. package/dist/src/stack/scfStack/scfPlanner.js +67 -16
  81. package/dist/src/stack/scfStack/scfResource.js +170 -321
  82. package/dist/src/stack/scfStack/scfTypes.js +4 -3
  83. package/dist/src/stack/scfStack/tdsqlcPlanner.js +10 -13
  84. package/dist/src/stack/volcengineStack/apigwDomains.js +64 -0
  85. package/dist/src/stack/volcengineStack/apigwPlanner.js +71 -15
  86. package/dist/src/stack/volcengineStack/apigwResource.js +60 -24
  87. package/dist/src/stack/volcengineStack/apigwTypes.js +5 -1
  88. package/dist/src/stack/volcengineStack/deployer.js +6 -0
  89. package/dist/src/stack/volcengineStack/tosPlanner.js +8 -13
  90. package/dist/src/stack/volcengineStack/tosResource.js +22 -35
  91. package/dist/src/stack/volcengineStack/tosTypes.js +5 -8
  92. package/dist/src/stack/volcengineStack/vefaasPlanner.js +142 -49
  93. package/dist/src/stack/volcengineStack/vefaasResource.js +17 -12
  94. package/dist/src/stack/volcengineStack/vefaasTypes.js +6 -3
  95. package/dist/tsconfig.tsbuildinfo +1 -1
  96. package/package.json +1 -1
  97. package/release-notes.md +79 -0
  98. package/samples/tencent-local/index.js +13 -0
  99. package/samples/tencent-local/serverlessinsight.yml +23 -0
  100. package/samples/volcengine-local/index.js +13 -0
  101. package/samples/volcengine-local/serverlessinsight.yml +28 -0
@@ -330,30 +330,13 @@ const createCosOperations = (cosClient, region, dnsOps) => {
330
330
  }
331
331
  },
332
332
  getBucket: async (bucketName, region) => {
333
- try {
334
- // Check if bucket exists. The HeadBucket response carries the bucket
335
- // creation date in the x-cos-creation-date header — preserved so the
336
- // state instance no longer reports creationDate: null.
337
- const headBucketData = await new Promise((resolve, reject) => {
338
- cosClient.headBucket({
339
- Bucket: bucketName,
340
- Region: region,
341
- }, (err, data) => {
342
- if (err) {
343
- reject(err);
344
- }
345
- else {
346
- resolve(data);
347
- }
348
- });
349
- });
350
- const creationDate = headBucketData.headers?.['x-cos-creation-date'];
351
- // Get ACL
352
- let acl;
353
- let accessControlPolicy;
333
+ const headBucketData = await (async () => {
354
334
  try {
355
- const aclResult = await new Promise((resolve, reject) => {
356
- cosClient.getBucketAcl({
335
+ // Check if bucket exists. The HeadBucket response carries the bucket
336
+ // creation date in the x-cos-creation-date header — preserved so the
337
+ // state instance no longer reports creationDate: null.
338
+ return await new Promise((resolve, reject) => {
339
+ cosClient.headBucket({
357
340
  Bucket: bucketName,
358
341
  Region: region,
359
342
  }, (err, data) => {
@@ -365,322 +348,342 @@ const createCosOperations = (cosClient, region, dnsOps) => {
365
348
  }
366
349
  });
367
350
  });
368
- acl = aclResult.ACL;
369
- accessControlPolicy = {
370
- owner: aclResult.Owner
371
- ? {
372
- id: aclResult.Owner.ID,
373
- displayName: undefined,
374
- }
375
- : undefined,
376
- grants: aclResult.Grants?.map((g) => ({
377
- grantee: g.Grantee
378
- ? {
379
- type: 'ID' in g.Grantee ? 'CanonicalUser' : 'Group',
380
- uri: 'URI' in g.Grantee ? g.Grantee.URI : undefined,
381
- id: 'ID' in g.Grantee ? g.Grantee.ID : undefined,
382
- displayName: undefined,
383
- }
384
- : undefined,
385
- permission: g.Permission,
386
- })),
387
- };
388
351
  }
389
- catch {
390
- // ACL might not be accessible due to insufficient permissions
391
- }
392
- // Get CORS configuration
393
- let corsConfiguration;
394
- try {
395
- const corsResult = await new Promise((resolve, reject) => {
396
- cosClient.getBucketCors({
397
- Bucket: bucketName,
398
- Region: region,
399
- }, (err, data) => {
400
- if (err) {
401
- reject(err);
402
- }
403
- else {
404
- resolve(data);
405
- }
406
- });
407
- });
408
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
409
- corsConfiguration = corsResult.CORSRules?.map((r) => ({
410
- id: r.ID,
411
- allowedOrigins: r.AllowedOrigin,
412
- allowedMethods: r.AllowedMethod,
413
- allowedHeaders: r.AllowedHeader,
414
- exposeHeaders: r.ExposeHeader,
415
- maxAgeSeconds: r.MaxAgeSeconds,
416
- }));
417
- }
418
- catch {
419
- // CORS might not be configured
420
- }
421
- // Get website configuration
422
- let websiteConfig;
423
- try {
424
- const websiteResult = await new Promise((resolve, reject) => {
425
- cosClient.getBucketWebsite({
426
- Bucket: bucketName,
427
- Region: region,
428
- }, (err, data) => {
429
- if (err) {
430
- reject(err);
431
- }
432
- else {
433
- resolve(data);
434
- }
435
- });
436
- });
437
- if (websiteResult.WebsiteConfiguration?.IndexDocument ||
438
- websiteResult.WebsiteConfiguration?.ErrorDocument) {
439
- websiteConfig = {
440
- IndexDocument: websiteResult.WebsiteConfiguration.IndexDocument,
441
- ErrorDocument: websiteResult.WebsiteConfiguration.ErrorDocument,
442
- };
352
+ catch (error) {
353
+ if (error && typeof error === 'object' && 'statusCode' in error) {
354
+ const statusCode = error.statusCode;
355
+ if (statusCode === 404) {
356
+ return null;
357
+ }
443
358
  }
359
+ throw error;
444
360
  }
445
- catch {
446
- // Website configuration might not be set
447
- }
448
- // Get versioning configuration
449
- let versioningConfig;
450
- try {
451
- const versioningResult = await new Promise((resolve, reject) => {
452
- cosClient.getBucketVersioning({
453
- Bucket: bucketName,
454
- Region: region,
455
- }, (err, data) => {
456
- if (err) {
457
- reject(err);
458
- }
459
- else {
460
- resolve(data);
461
- }
462
- });
361
+ })();
362
+ if (headBucketData === null)
363
+ return null;
364
+ const creationDate = headBucketData.headers?.['x-cos-creation-date'];
365
+ // Get ACL
366
+ let acl;
367
+ let accessControlPolicy;
368
+ try {
369
+ const aclResult = await new Promise((resolve, reject) => {
370
+ cosClient.getBucketAcl({
371
+ Bucket: bucketName,
372
+ Region: region,
373
+ }, (err, data) => {
374
+ if (err) {
375
+ reject(err);
376
+ }
377
+ else {
378
+ resolve(data);
379
+ }
463
380
  });
464
- versioningConfig = {
465
- status: versioningResult.VersioningConfiguration?.Status,
466
- };
467
- }
468
- catch {
469
- // Versioning might not be accessible
470
- }
471
- // Get tagging configuration
472
- let taggingConfig;
473
- try {
474
- const taggingResult = await new Promise((resolve, reject) => {
475
- cosClient.getBucketTagging({
476
- Bucket: bucketName,
477
- Region: region,
478
- }, (err, data) => {
479
- if (err) {
480
- reject(err);
481
- }
482
- else {
483
- resolve(data);
381
+ });
382
+ acl = aclResult.ACL;
383
+ accessControlPolicy = {
384
+ owner: aclResult.Owner
385
+ ? {
386
+ id: aclResult.Owner.ID,
387
+ displayName: undefined,
388
+ }
389
+ : undefined,
390
+ grants: aclResult.Grants?.map((g) => ({
391
+ grantee: g.Grantee
392
+ ? {
393
+ type: 'ID' in g.Grantee ? 'CanonicalUser' : 'Group',
394
+ uri: 'URI' in g.Grantee ? g.Grantee.URI : undefined,
395
+ id: 'ID' in g.Grantee ? g.Grantee.ID : undefined,
396
+ displayName: undefined,
484
397
  }
485
- });
398
+ : undefined,
399
+ permission: g.Permission,
400
+ })),
401
+ };
402
+ }
403
+ catch {
404
+ // ACL might not be accessible due to insufficient permissions
405
+ }
406
+ // Get CORS configuration
407
+ let corsConfiguration;
408
+ try {
409
+ const corsResult = await new Promise((resolve, reject) => {
410
+ cosClient.getBucketCors({
411
+ Bucket: bucketName,
412
+ Region: region,
413
+ }, (err, data) => {
414
+ if (err) {
415
+ reject(err);
416
+ }
417
+ else {
418
+ resolve(data);
419
+ }
486
420
  });
487
- taggingConfig = {
488
- tags: taggingResult.Tags?.map((t) => ({
489
- key: t.Key,
490
- value: t.Value,
491
- })),
421
+ });
422
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
423
+ corsConfiguration = corsResult.CORSRules?.map((r) => ({
424
+ id: r.ID,
425
+ allowedOrigins: r.AllowedOrigin,
426
+ allowedMethods: r.AllowedMethod,
427
+ allowedHeaders: r.AllowedHeader,
428
+ exposeHeaders: r.ExposeHeader,
429
+ maxAgeSeconds: r.MaxAgeSeconds,
430
+ }));
431
+ }
432
+ catch {
433
+ // CORS might not be configured
434
+ }
435
+ // Get website configuration
436
+ let websiteConfig = null;
437
+ try {
438
+ const websiteResult = await new Promise((resolve, reject) => {
439
+ cosClient.getBucketWebsite({
440
+ Bucket: bucketName,
441
+ Region: region,
442
+ }, (err, data) => {
443
+ if (err) {
444
+ reject(err);
445
+ }
446
+ else {
447
+ resolve(data);
448
+ }
449
+ });
450
+ });
451
+ if (websiteResult.WebsiteConfiguration?.IndexDocument ||
452
+ websiteResult.WebsiteConfiguration?.ErrorDocument) {
453
+ websiteConfig = {
454
+ IndexDocument: websiteResult.WebsiteConfiguration.IndexDocument,
455
+ ErrorDocument: websiteResult.WebsiteConfiguration.ErrorDocument,
492
456
  };
493
457
  }
494
- catch (error) {
495
- // A 404 or NoSuchTagSet means the bucket has no tagging config — not
496
- // an error. Any other failure (auth, network) must surface loudly so
497
- // it is not misread as "bucket not owned".
498
- const err = error;
499
- const isNoTags = err.statusCode === 404 ||
500
- err.code === 'NoSuchTagSet' ||
501
- String(err.message ?? '').includes('NoSuchTagSet');
502
- if (!isNoTags) {
503
- throw error;
458
+ }
459
+ catch (error) {
460
+ const code = error?.code;
461
+ if (code !== 'NoSuchWebsiteConfiguration' && code !== 'NoSuchConfiguration')
462
+ throw error;
463
+ }
464
+ // Get versioning configuration
465
+ const versioningResult = await new Promise((resolve, reject) => {
466
+ cosClient.getBucketVersioning({
467
+ Bucket: bucketName,
468
+ Region: region,
469
+ }, (err, data) => {
470
+ if (err) {
471
+ reject(err);
504
472
  }
505
- }
506
- // Get lifecycle configuration
507
- let lifecycleConfiguration;
508
- try {
509
- const lifecycleResult = await new Promise((resolve, reject) => {
510
- cosClient.getBucketLifecycle({
511
- Bucket: bucketName,
512
- Region: region,
513
- }, (err, data) => {
514
- if (err) {
515
- reject(err);
516
- }
517
- else {
518
- resolve(data);
519
- }
520
- });
473
+ else {
474
+ resolve(data);
475
+ }
476
+ });
477
+ });
478
+ const versioningConfig = {
479
+ status: versioningResult.VersioningConfiguration?.Status,
480
+ };
481
+ // Get tagging configuration
482
+ let taggingConfig;
483
+ try {
484
+ const taggingResult = await new Promise((resolve, reject) => {
485
+ cosClient.getBucketTagging({
486
+ Bucket: bucketName,
487
+ Region: region,
488
+ }, (err, data) => {
489
+ if (err) {
490
+ reject(err);
491
+ }
492
+ else {
493
+ resolve(data);
494
+ }
521
495
  });
522
- lifecycleConfiguration = {
523
- rules: lifecycleResult.Rules?.map((r) => ({
524
- id: r.ID,
525
- status: r.Status,
526
- prefix: typeof r.Filter?.Prefix === 'string' ? r.Filter.Prefix : undefined,
527
- expiration: {
528
- days: r.Expiration?.Days,
529
- date: r.Expiration?.Date,
530
- expiredObjectDeleteMarker: r.Expiration?.ExpiredObjectDeleteMarker,
531
- },
532
- transition: {
533
- days: r.Transition?.Days,
534
- date: r.Transition?.Date,
535
- storageClass: r.Transition?.StorageClass,
536
- },
537
- })),
538
- };
539
- }
540
- catch {
541
- // Lifecycle might not be configured
496
+ });
497
+ taggingConfig = {
498
+ tags: taggingResult.Tags?.map((t) => ({
499
+ key: t.Key,
500
+ value: t.Value,
501
+ })),
502
+ };
503
+ }
504
+ catch (error) {
505
+ // A 404 or NoSuchTagSet means the bucket has no tagging config — not
506
+ // an error. Any other failure (auth, network) must surface loudly so
507
+ // it is not misread as "bucket not owned".
508
+ const err = error;
509
+ const isNoTags = err.statusCode === 404 ||
510
+ err.code === 'NoSuchTagSet' ||
511
+ String(err.message ?? '').includes('NoSuchTagSet');
512
+ if (!isNoTags) {
513
+ throw error;
542
514
  }
543
- // Get logging configuration
544
- let loggingConfiguration;
545
- try {
546
- const loggingResult = await new Promise((resolve, reject) => {
547
- cosClient.getBucketLogging({
548
- Bucket: bucketName,
549
- Region: region,
550
- }, (err, data) => {
551
- if (err) {
552
- reject(err);
553
- }
554
- else {
555
- resolve(data);
556
- }
557
- });
515
+ }
516
+ // Get lifecycle configuration
517
+ let lifecycleConfiguration;
518
+ try {
519
+ const lifecycleResult = await new Promise((resolve, reject) => {
520
+ cosClient.getBucketLifecycle({
521
+ Bucket: bucketName,
522
+ Region: region,
523
+ }, (err, data) => {
524
+ if (err) {
525
+ reject(err);
526
+ }
527
+ else {
528
+ resolve(data);
529
+ }
558
530
  });
559
- loggingConfiguration = {
560
- targetBucket: loggingResult.BucketLoggingStatus?.LoggingEnabled?.TargetBucket,
561
- targetPrefix: loggingResult.BucketLoggingStatus?.LoggingEnabled?.TargetPrefix,
562
- };
563
- }
564
- catch {
565
- // Logging might not be configured
566
- }
567
- // Get replication configuration
568
- let replicationConfiguration;
569
- try {
570
- const replicationResult = await new Promise((resolve, reject) => {
571
- cosClient.getBucketReplication({
572
- Bucket: bucketName,
573
- Region: region,
574
- }, (err, data) => {
575
- if (err) {
576
- reject(err);
577
- }
578
- else {
579
- resolve(data);
580
- }
581
- });
531
+ });
532
+ lifecycleConfiguration = {
533
+ rules: lifecycleResult.Rules?.map((r) => ({
534
+ id: r.ID,
535
+ status: r.Status,
536
+ prefix: typeof r.Filter?.Prefix === 'string' ? r.Filter.Prefix : undefined,
537
+ expiration: {
538
+ days: r.Expiration?.Days,
539
+ date: r.Expiration?.Date,
540
+ expiredObjectDeleteMarker: r.Expiration?.ExpiredObjectDeleteMarker,
541
+ },
542
+ transition: {
543
+ days: r.Transition?.Days,
544
+ date: r.Transition?.Date,
545
+ storageClass: r.Transition?.StorageClass,
546
+ },
547
+ })),
548
+ };
549
+ }
550
+ catch {
551
+ // Lifecycle might not be configured
552
+ }
553
+ // Get logging configuration
554
+ let loggingConfiguration;
555
+ try {
556
+ const loggingResult = await new Promise((resolve, reject) => {
557
+ cosClient.getBucketLogging({
558
+ Bucket: bucketName,
559
+ Region: region,
560
+ }, (err, data) => {
561
+ if (err) {
562
+ reject(err);
563
+ }
564
+ else {
565
+ resolve(data);
566
+ }
582
567
  });
583
- replicationConfiguration = {
584
- role: replicationResult.ReplicationConfiguration?.Role,
585
- rules: replicationResult.ReplicationConfiguration?.Rules?.map((r) => ({
586
- id: r.ID,
587
- status: r.Status,
588
- prefix: r.Prefix,
589
- destination: {
590
- bucket: r.Destination?.Bucket,
591
- storageClass: r.Destination?.StorageClass,
592
- },
593
- })),
594
- };
595
- }
596
- catch {
597
- // Replication might not be configured
598
- }
599
- // Get server-side encryption configuration
600
- let sseConfiguration;
601
- try {
602
- const encryptionResult = await new Promise((resolve, reject) => {
603
- cosClient.getBucketEncryption({
604
- Bucket: bucketName,
605
- Region: region,
606
- }, (err, data) => {
607
- if (err) {
608
- reject(err);
609
- }
610
- else {
611
- resolve(data);
612
- }
613
- });
568
+ });
569
+ loggingConfiguration = {
570
+ targetBucket: loggingResult.BucketLoggingStatus?.LoggingEnabled?.TargetBucket,
571
+ targetPrefix: loggingResult.BucketLoggingStatus?.LoggingEnabled?.TargetPrefix,
572
+ };
573
+ }
574
+ catch {
575
+ // Logging might not be configured
576
+ }
577
+ // Get replication configuration
578
+ let replicationConfiguration;
579
+ try {
580
+ const replicationResult = await new Promise((resolve, reject) => {
581
+ cosClient.getBucketReplication({
582
+ Bucket: bucketName,
583
+ Region: region,
584
+ }, (err, data) => {
585
+ if (err) {
586
+ reject(err);
587
+ }
588
+ else {
589
+ resolve(data);
590
+ }
614
591
  });
615
- const rule = encryptionResult.ServerSideEncryptionConfiguration?.Rule?.[0];
616
- // SDK quirk: the type declares ApplySideEncryptionConfiguration, but the
617
- // raw COS API returns ApplyServerSideEncryptionByDefault — read both.
618
- const apply =
619
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
620
- rule?.ApplyServerSideEncryptionByDefault ??
621
- rule?.ApplySideEncryptionConfiguration;
622
- sseConfiguration = {
623
- sseAlgorithm: apply?.SSEAlgorithm,
624
- sseKmsMasterKeyId: apply?.KMSMasterKeyID,
625
- };
626
- }
627
- catch {
628
- // Encryption might not be configured
629
- }
630
- // Get bucket policy (cloud-effective, may differ from the definition)
631
- let policy = null;
632
- try {
633
- const policyResult = await new Promise((resolve, reject) => {
634
- cosClient.getBucketPolicy({
635
- Bucket: bucketName,
636
- Region: region,
637
- }, (err, data) => {
638
- if (err)
639
- reject(err);
640
- else
641
- resolve(data);
642
- });
592
+ });
593
+ replicationConfiguration = {
594
+ role: replicationResult.ReplicationConfiguration?.Role,
595
+ rules: replicationResult.ReplicationConfiguration?.Rules?.map((r) => ({
596
+ id: r.ID,
597
+ status: r.Status,
598
+ prefix: r.Prefix,
599
+ destination: {
600
+ bucket: r.Destination?.Bucket,
601
+ storageClass: r.Destination?.StorageClass,
602
+ },
603
+ })),
604
+ };
605
+ }
606
+ catch {
607
+ // Replication might not be configured
608
+ }
609
+ // Get server-side encryption configuration
610
+ let sseConfiguration = null;
611
+ try {
612
+ const encryptionResult = await new Promise((resolve, reject) => {
613
+ cosClient.getBucketEncryption({
614
+ Bucket: bucketName,
615
+ Region: region,
616
+ }, (err, data) => {
617
+ if (err) {
618
+ reject(err);
619
+ }
620
+ else {
621
+ resolve(data);
622
+ }
643
623
  });
644
- if (policyResult.Policy) {
645
- policy =
646
- typeof policyResult.Policy === 'string'
647
- ? JSON.parse(policyResult.Policy)
648
- : policyResult.Policy;
649
- }
650
- }
651
- catch (err) {
652
- const e = err;
653
- if (e.statusCode !== 404 && e.statusCode !== 403 && e.code !== 'NoSuchBucketPolicy') {
654
- throw err;
655
- }
656
- }
657
- return {
658
- Name: bucketName,
659
- Location: region,
660
- CreationDate: creationDate,
661
- ACL: acl,
662
- WebsiteConfiguration: websiteConfig,
663
- AccessControlPolicy: accessControlPolicy,
664
- CorsConfiguration: corsConfiguration,
665
- VersioningConfiguration: versioningConfig,
666
- TaggingConfiguration: taggingConfig,
667
- Tags: taggingConfig?.tags?.map((t) => ({ Key: t.key ?? '', Value: t.value ?? '' })),
668
- LifecycleConfiguration: lifecycleConfiguration,
669
- LoggingConfiguration: loggingConfiguration,
670
- ReplicationConfiguration: replicationConfiguration,
671
- SseConfiguration: sseConfiguration,
672
- Policy: policy,
624
+ });
625
+ const rule = encryptionResult.ServerSideEncryptionConfiguration?.Rule?.[0];
626
+ // SDK quirk: the type declares ApplySideEncryptionConfiguration, but the
627
+ // raw COS API returns ApplyServerSideEncryptionByDefault — read both.
628
+ const apply =
629
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
630
+ rule?.ApplyServerSideEncryptionByDefault ??
631
+ rule?.ApplySideEncryptionConfiguration;
632
+ sseConfiguration = {
633
+ sseAlgorithm: apply?.SSEAlgorithm,
634
+ sseKmsMasterKeyId: apply?.KMSMasterKeyID,
673
635
  };
674
636
  }
675
637
  catch (error) {
676
- if (error && typeof error === 'object' && 'statusCode' in error) {
677
- const statusCode = error.statusCode;
678
- if (statusCode === 404) {
679
- return null;
680
- }
638
+ const code = error?.code;
639
+ if (code !== 'ServerSideEncryptionConfigurationNotFoundError' &&
640
+ code !== 'NoSuchConfiguration')
641
+ throw error;
642
+ }
643
+ // Get bucket policy (cloud-effective, may differ from the definition)
644
+ let policy = null;
645
+ try {
646
+ const policyResult = await new Promise((resolve, reject) => {
647
+ cosClient.getBucketPolicy({
648
+ Bucket: bucketName,
649
+ Region: region,
650
+ }, (err, data) => {
651
+ if (err)
652
+ reject(err);
653
+ else
654
+ resolve(data);
655
+ });
656
+ });
657
+ if (policyResult.Policy) {
658
+ policy =
659
+ typeof policyResult.Policy === 'string'
660
+ ? JSON.parse(policyResult.Policy)
661
+ : policyResult.Policy;
681
662
  }
682
- throw error;
683
663
  }
664
+ catch (err) {
665
+ const e = err;
666
+ if (e?.code !== 'NoSuchBucketPolicy') {
667
+ throw err;
668
+ }
669
+ }
670
+ return {
671
+ Name: bucketName,
672
+ Location: region,
673
+ CreationDate: creationDate,
674
+ ACL: acl,
675
+ WebsiteConfiguration: websiteConfig,
676
+ AccessControlPolicy: accessControlPolicy,
677
+ CorsConfiguration: corsConfiguration,
678
+ VersioningConfiguration: versioningConfig,
679
+ TaggingConfiguration: taggingConfig,
680
+ Tags: taggingConfig?.tags?.map((t) => ({ Key: t.key ?? '', Value: t.value ?? '' })),
681
+ LifecycleConfiguration: lifecycleConfiguration,
682
+ LoggingConfiguration: loggingConfiguration,
683
+ ReplicationConfiguration: replicationConfiguration,
684
+ SseConfiguration: sseConfiguration,
685
+ Policy: policy,
686
+ };
684
687
  },
685
688
  updateBucketAcl: async (bucketName, region, acl) => {
686
689
  await new Promise((resolve, reject) => {
@@ -767,7 +770,7 @@ const createCosOperations = (cosClient, region, dnsOps) => {
767
770
  }
768
771
  catch (err) {
769
772
  const e = err;
770
- if (e.statusCode === 404 || e.statusCode === 403 || e.code === 'NoSuchBucketPolicy') {
773
+ if (e?.code === 'NoSuchBucketPolicy') {
771
774
  return null;
772
775
  }
773
776
  throw err;
@@ -795,8 +798,9 @@ const createCosOperations = (cosClient, region, dnsOps) => {
795
798
  });
796
799
  logger_1.logger.info(lang_1.lang.__('COS_BUCKET_POLICY_DELETED', { bucketName }));
797
800
  }
798
- catch {
799
- // Best effort cleanup
801
+ catch (error) {
802
+ if (error?.code !== 'NoSuchBucketPolicy')
803
+ throw error;
800
804
  }
801
805
  },
802
806
  };