@geek-fun/serverlessinsight 0.7.9 → 0.8.1

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.
package/dist/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geek-fun/serverlessinsight",
3
- "version": "0.7.9",
3
+ "version": "0.8.1",
4
4
  "description": "Full life cycle cross providers serverless application management for your fast-growing business.",
5
5
  "homepage": "https://serverlessinsight.geekfun.club",
6
6
  "main": "dist/src/index.js",
@@ -626,6 +626,25 @@ const createApigwOperations = (apigwClient, dnsClient, _context) => {
626
626
  });
627
627
  await apigwClient.modifyApi(request);
628
628
  },
629
+ /**
630
+ * List all APIs in a group
631
+ */
632
+ listApisByGroup: async (groupId) => {
633
+ const request = new cloudapi.DescribeApisRequest({
634
+ groupId,
635
+ pageSize: 100,
636
+ pageNumber: 1,
637
+ });
638
+ const response = await apigwClient.describeApis(request);
639
+ const apiSummaries = response.body?.apiSummarys?.apiSummary ?? [];
640
+ const results = [];
641
+ for (const api of apiSummaries) {
642
+ if (api.apiId && api.apiName) {
643
+ results.push({ apiId: api.apiId, apiName: api.apiName });
644
+ }
645
+ }
646
+ return results;
647
+ },
629
648
  /**
630
649
  * Delete API
631
650
  */
@@ -10,6 +10,7 @@ const certUtils_1 = require("../../common/certUtils");
10
10
  const logger_1 = require("../../common/logger");
11
11
  const lang_1 = require("../../lang");
12
12
  const domainUtils_1 = require("../../common/domainUtils");
13
+ const hashUtils_1 = require("../../common/hashUtils");
13
14
  const getCdnConfig = (event) => {
14
15
  const domainCdn = event.domain?.cdn;
15
16
  if (domainCdn == null) {
@@ -444,15 +445,93 @@ const updateApigwResource = async (context, event, serviceName, roleArn, state)
444
445
  const logicalId = `events.${event.key}`;
445
446
  const existingState = (0, stateManager_1.getResource)(state, logicalId);
446
447
  const client = (0, aliyunClient_1.createAliyunClient)(context);
447
- if (!existingState) {
448
+ let existingInstances;
449
+ let groupId;
450
+ // Precompute expected API names from triggers to safely filter cloud APIs
451
+ const expectedApiNames = new Set(event.triggers.map((trigger) => {
452
+ const apiConfig = (0, apigwTypes_1.triggerToApigwApiConfig)(event, trigger, 'placeholder', serviceName, context.region, context.stage, roleArn);
453
+ return apiConfig.apiName;
454
+ }));
455
+ const buildCloudInstance = (cloudGroup, cloudApis) => {
456
+ const gId = cloudGroup.groupId;
457
+ return {
458
+ groupId: gId,
459
+ instances: [
460
+ buildApigwGroupInstanceFromProvider(cloudGroup, context.stage),
461
+ // Only include cloud APIs whose name matches our expected set.
462
+ // This prevents accidentally managing or deleting APIs created
463
+ // by other processes within the same group.
464
+ ...cloudApis
465
+ .filter((api) => expectedApiNames.has(api.apiName))
466
+ .map((api) => ({
467
+ type: 'ALIYUN_APIGW_API',
468
+ sid: (0, common_1.buildSid)('aliyun', 'apigateway', context.stage, `${gId}/${api.apiId}`),
469
+ id: api.apiId,
470
+ apiId: api.apiId,
471
+ apiName: api.apiName,
472
+ groupId: gId,
473
+ })),
474
+ ],
475
+ };
476
+ };
477
+ // Shared helper: try to find the group from cloud. On any error (or not found),
478
+ // fall back to full creation since there's nothing to update.
479
+ const findCloudGroupOrCreate = async () => {
448
480
  return (0, exports.createApigwResource)(context, event, serviceName, roleArn, state);
481
+ };
482
+ if (existingState) {
483
+ existingInstances = existingState.instances;
484
+ const groupInstance = existingInstances.find((i) => i.type === 'ALIYUN_APIGW_GROUP');
485
+ if (groupInstance) {
486
+ groupId = groupInstance.id;
487
+ }
488
+ else {
489
+ const groupConfig = (0, apigwTypes_1.eventToApigwGroupConfig)(event, serviceName, context.stage);
490
+ try {
491
+ const cloudGroup = await client.apigw.findApiGroupByName(groupConfig.groupName);
492
+ if (!cloudGroup?.groupId) {
493
+ return findCloudGroupOrCreate();
494
+ }
495
+ const cloudApis = await client.apigw.listApisByGroup(cloudGroup.groupId);
496
+ const built = buildCloudInstance(cloudGroup, cloudApis);
497
+ groupId = built.groupId;
498
+ existingInstances = built.instances;
499
+ state = (0, stateManager_1.setResource)(state, logicalId, {
500
+ mode: 'managed',
501
+ region: context.region,
502
+ definition: {},
503
+ instances: existingInstances,
504
+ lastUpdated: new Date().toISOString(),
505
+ });
506
+ }
507
+ catch {
508
+ return findCloudGroupOrCreate();
509
+ }
510
+ }
449
511
  }
450
- const existingInstances = existingState.instances;
451
- const groupInstance = existingInstances.find((i) => i.type === 'ALIYUN_APIGW_GROUP');
452
- if (!groupInstance) {
453
- return (0, exports.createApigwResource)(context, event, serviceName, roleArn, state);
512
+ else {
513
+ const groupConfig = (0, apigwTypes_1.eventToApigwGroupConfig)(event, serviceName, context.stage);
514
+ try {
515
+ const cloudGroup = await client.apigw.findApiGroupByName(groupConfig.groupName);
516
+ if (!cloudGroup?.groupId) {
517
+ return findCloudGroupOrCreate();
518
+ }
519
+ const cloudApis = await client.apigw.listApisByGroup(cloudGroup.groupId);
520
+ const built = buildCloudInstance(cloudGroup, cloudApis);
521
+ groupId = built.groupId;
522
+ existingInstances = built.instances;
523
+ state = (0, stateManager_1.setResource)(state, logicalId, {
524
+ mode: 'managed',
525
+ region: context.region,
526
+ definition: {},
527
+ instances: existingInstances,
528
+ lastUpdated: new Date().toISOString(),
529
+ });
530
+ }
531
+ catch {
532
+ return findCloudGroupOrCreate();
533
+ }
454
534
  }
455
- const groupId = groupInstance.id;
456
535
  const groupConfig = (0, apigwTypes_1.eventToApigwGroupConfig)(event, serviceName, context.stage);
457
536
  await client.apigw.updateApiGroup(groupId, groupConfig);
458
537
  const groupInfo = await client.apigw.getApiGroup(groupId);
@@ -511,14 +590,39 @@ const updateApigwResource = async (context, event, serviceName, roleArn, state)
511
590
  }
512
591
  }
513
592
  if (event.domain) {
593
+ const desiredDomainDef = (0, apigwTypes_1.extractEventDomainDefinition)(event.domain);
594
+ const previousDomainDef = existingState?.definition?.domain;
595
+ // Skip domain binding when the domain configuration hasn't changed AND
596
+ // the domain was previously successfully bound (DNS records exist in state).
597
+ // This avoids unnecessary DNS verification (minutes-long) on every deploy.
598
+ // We check DNS sub-resources to avoid skipping after a previously failed binding.
599
+ const hasBoundDomains = (0, stateManager_1.getResource)(state, `${logicalId}.dns_verification`) !== undefined ||
600
+ (0, stateManager_1.getResource)(state, `${logicalId}.dns_txt_verification`) !== undefined;
601
+ if (previousDomainDef &&
602
+ desiredDomainDef &&
603
+ hasBoundDomains &&
604
+ (0, hashUtils_1.attributesEqual)(previousDomainDef, desiredDomainDef)) {
605
+ logger_1.logger.info('Domain configuration unchanged and previously bound, skipping DNS verification');
606
+ // Still include existing domain instances for state consistency
607
+ const existingDomainInstances = existingState?.instances?.filter((i) => i.type === 'ALIYUN_APIGW_GROUP' ||
608
+ i.type === 'ALIYUN_APIGW_API' ||
609
+ i.type === 'ALIYUN_APIGW_DEPLOYMENT') ?? [];
610
+ instances.push(...existingDomainInstances);
611
+ return (0, stateManager_1.setResource)(state, logicalId, {
612
+ mode: 'managed',
613
+ region: context.region,
614
+ definition: { ...(existingState?.definition ?? {}), domain: desiredDomainDef },
615
+ instances: [...instances, ...existingDomainInstances],
616
+ lastUpdated: new Date().toISOString(),
617
+ });
618
+ }
514
619
  const primaryDomain = event.domain.domain_name;
515
620
  const wwwBindApex = event.domain.www_bind_apex === true;
516
621
  const wwwDomain = wwwBindApex ? (0, domainUtils_1.deriveWwwDomain)(primaryDomain) : null;
517
622
  const isCdnEnabled = getIsCdnEnabled(event);
518
- const existingDomain = existingState.definition?.domain;
519
- const previousWwwBindApex = existingDomain?.wwwBindApex === true;
520
- const previousDomainName = existingDomain?.domainName;
521
- const previousCdnEnabled = existingDomain?.cdnEnabled === true;
623
+ const previousWwwBindApex = previousDomainDef?.wwwBindApex === true;
624
+ const previousDomainName = previousDomainDef?.domainName;
625
+ const previousCdnEnabled = previousDomainDef?.cdnEnabled === true;
522
626
  const previousTrackedDomains = getApigwTrackedDomains(previousDomainName, previousWwwBindApex);
523
627
  const originDomain = groupInfo.subDomain;
524
628
  if (isCdnEnabled && !originDomain) {
@@ -585,7 +689,7 @@ const updateApigwResource = async (context, event, serviceName, roleArn, state)
585
689
  }
586
690
  }
587
691
  else {
588
- const existingDomain = existingState.definition?.domain;
692
+ const existingDomain = existingState?.definition?.domain;
589
693
  if (existingDomain?.domainName) {
590
694
  const previousDomain = existingDomain.domainName;
591
695
  const previousCdnEnabled = existingDomain.cdnEnabled === true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geek-fun/serverlessinsight",
3
- "version": "0.7.9",
3
+ "version": "0.8.1",
4
4
  "description": "Full life cycle cross providers serverless application management for your fast-growing business.",
5
5
  "homepage": "https://serverlessinsight.geekfun.club",
6
6
  "main": "dist/src/index.js",