@geek-fun/serverlessinsight 0.5.6 → 0.5.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.
- package/README.md +12 -10
- package/dist/package.json +2 -1
- package/dist/src/commands/deploy.js +74 -2
- package/dist/src/commands/index.js +17 -16
- package/dist/src/commands/show.js +302 -0
- package/dist/src/common/aliyunClient/apigwOperations.js +6 -22
- package/dist/src/common/aliyunClient/index.js +3 -2
- package/dist/src/common/aliyunClient/ossOperations.js +194 -1
- package/dist/src/common/constants.js +2 -3
- package/dist/src/common/context.js +4 -1
- package/dist/src/common/domainUtils.js +121 -0
- package/dist/src/common/iacHelper.js +1 -8
- package/dist/src/common/stateBackend/cosStateBackend.js +16 -188
- package/dist/src/common/stateBackend/index.js +2 -0
- package/dist/src/common/stateBackend/lockUtils.js +55 -0
- package/dist/src/common/stateBackend/ossStateBackend.js +27 -199
- package/dist/src/common/stateBackend/remoteStateBackend.js +150 -0
- package/dist/src/common/tencentClient/cosOperations.js +486 -213
- package/dist/src/common/tencentClient/dnspodOperations.js +105 -0
- package/dist/src/common/tencentClient/index.js +5 -1
- package/dist/src/lang/en.js +40 -0
- package/dist/src/lang/zh-CN.js +38 -0
- package/dist/src/stack/aliyunStack/apigwResource.js +35 -0
- package/dist/src/stack/aliyunStack/ossResource.js +73 -19
- package/dist/src/stack/deploy.js +6 -33
- package/dist/src/stack/scfStack/cosResource.js +68 -8
- package/dist/src/types/domains/state.js +13 -0
- package/dist/src/validator/templateRefSchema.js +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -1
- package/dist/src/commands/template.js +0 -28
|
@@ -6,10 +6,200 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.createOssOperations = void 0;
|
|
7
7
|
const node_fs_1 = __importDefault(require("node:fs"));
|
|
8
8
|
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
-
const
|
|
9
|
+
const logger_1 = require("../logger");
|
|
10
|
+
const lang_1 = require("../../lang");
|
|
11
|
+
const domainUtils_1 = require("../domainUtils");
|
|
12
|
+
const createOssOperations = (ossClient, region, dnsOps) => {
|
|
10
13
|
const useBucket = (bucketName) => {
|
|
11
14
|
ossClient.useBucket(bucketName);
|
|
12
15
|
};
|
|
16
|
+
const getOssEndpoint = (bucketName) => {
|
|
17
|
+
return `${bucketName}.oss-${region}.aliyuncs.com`;
|
|
18
|
+
};
|
|
19
|
+
const putBucketCname = async (bucketName, domain) => {
|
|
20
|
+
useBucket(bucketName);
|
|
21
|
+
const xml = `<?xml version="1.0" encoding="UTF-8"?>
|
|
22
|
+
<BucketCnameConfiguration>
|
|
23
|
+
<Cname>
|
|
24
|
+
<Domain>${domain}</Domain>
|
|
25
|
+
</Cname>
|
|
26
|
+
</BucketCnameConfiguration>`;
|
|
27
|
+
try {
|
|
28
|
+
const params = {
|
|
29
|
+
method: 'POST',
|
|
30
|
+
bucket: bucketName,
|
|
31
|
+
subres: { cname: '', comp: 'add' },
|
|
32
|
+
headers: {
|
|
33
|
+
'Content-Type': 'application/xml',
|
|
34
|
+
'Content-MD5': '',
|
|
35
|
+
},
|
|
36
|
+
content: xml,
|
|
37
|
+
successStatuses: [200],
|
|
38
|
+
};
|
|
39
|
+
await ossClient.request(params);
|
|
40
|
+
logger_1.logger.info(lang_1.lang.__('OSS_BUCKET_CNAME_BOUND', { domain }));
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
const err = error;
|
|
45
|
+
if (err.code === 'CnameAlreadyExists' || err.status === 409) {
|
|
46
|
+
logger_1.logger.info(lang_1.lang.__('OSS_BUCKET_CNAME_EXISTS', { domain }));
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
if (err.code === 'NeedVerifyDomainOwnership') {
|
|
50
|
+
logger_1.logger.warn(lang_1.lang.__('OSS_BUCKET_CNAME_NEED_VERIFY', { domain }));
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
logger_1.logger.warn(lang_1.lang.__('OSS_BUCKET_CNAME_BIND_FAILED', { error: String(error) }));
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
const deleteBucketCname = async (bucketName, domain) => {
|
|
58
|
+
useBucket(bucketName);
|
|
59
|
+
const xml = `<?xml version="1.0" encoding="UTF-8"?>
|
|
60
|
+
<BucketCnameConfiguration>
|
|
61
|
+
<Cname>
|
|
62
|
+
<Domain>${domain}</Domain>
|
|
63
|
+
</Cname>
|
|
64
|
+
</BucketCnameConfiguration>`;
|
|
65
|
+
try {
|
|
66
|
+
const params = {
|
|
67
|
+
method: 'POST',
|
|
68
|
+
bucket: bucketName,
|
|
69
|
+
subres: { cname: '', comp: 'delete' },
|
|
70
|
+
headers: {
|
|
71
|
+
'Content-Type': 'application/xml',
|
|
72
|
+
},
|
|
73
|
+
content: xml,
|
|
74
|
+
successStatuses: [200, 204],
|
|
75
|
+
};
|
|
76
|
+
await ossClient.request(params);
|
|
77
|
+
logger_1.logger.info(lang_1.lang.__('OSS_BUCKET_CNAME_UNBOUND', { domain }));
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
logger_1.logger.warn(lang_1.lang.__('OSS_BUCKET_CNAME_UNBIND_FAILED', { error: String(error) }));
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
const buildCorsRuleForDomain = (domain) => ({
|
|
86
|
+
allowedOrigin: [`https://${domain}`, `http://${domain}`],
|
|
87
|
+
allowedMethod: ['GET', 'HEAD'],
|
|
88
|
+
allowedHeader: ['*'],
|
|
89
|
+
exposeHeader: ['ETag', 'Content-Length'],
|
|
90
|
+
maxAgeSeconds: '3600',
|
|
91
|
+
});
|
|
92
|
+
const isDomainCorsRule = (rule, domain) => {
|
|
93
|
+
const origins = Array.isArray(rule.allowedOrigin) ? rule.allowedOrigin : [rule.allowedOrigin];
|
|
94
|
+
const expected = new Set([`https://${domain}`, `http://${domain}`]);
|
|
95
|
+
return origins.length === expected.size && origins.every((o) => expected.has(o));
|
|
96
|
+
};
|
|
97
|
+
const addCorsRuleForDomain = async (bucketName, domain) => {
|
|
98
|
+
try {
|
|
99
|
+
useBucket(bucketName);
|
|
100
|
+
let existingRules = [];
|
|
101
|
+
try {
|
|
102
|
+
const corsResult = await ossClient.getBucketCORS(bucketName);
|
|
103
|
+
if (corsResult.rules && corsResult.rules.length > 0) {
|
|
104
|
+
existingRules = corsResult.rules;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
// no existing CORS config
|
|
109
|
+
}
|
|
110
|
+
if (existingRules.some((rule) => isDomainCorsRule(rule, domain))) {
|
|
111
|
+
logger_1.logger.info(lang_1.lang.__('OSS_CORS_RULE_EXISTS', { domain }));
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const allRules = [...existingRules, buildCorsRuleForDomain(domain)];
|
|
115
|
+
await ossClient.putBucketCORS(bucketName, allRules);
|
|
116
|
+
logger_1.logger.info(lang_1.lang.__('OSS_CORS_RULE_ADDED', { domain }));
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
logger_1.logger.warn(lang_1.lang.__('OSS_CORS_RULE_ADD_FAILED', { domain, error: String(error) }));
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
const removeCorsRuleForDomain = async (bucketName, domain) => {
|
|
123
|
+
try {
|
|
124
|
+
useBucket(bucketName);
|
|
125
|
+
let existingRules = [];
|
|
126
|
+
try {
|
|
127
|
+
const corsResult = await ossClient.getBucketCORS(bucketName);
|
|
128
|
+
if (corsResult.rules && corsResult.rules.length > 0) {
|
|
129
|
+
existingRules = corsResult.rules;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
const remainingRules = existingRules.filter((rule) => !isDomainCorsRule(rule, domain));
|
|
136
|
+
if (remainingRules.length === existingRules.length) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (remainingRules.length > 0) {
|
|
140
|
+
await ossClient.putBucketCORS(bucketName, remainingRules);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
await ossClient.deleteBucketCORS(bucketName);
|
|
144
|
+
}
|
|
145
|
+
logger_1.logger.info(lang_1.lang.__('OSS_CORS_RULE_REMOVED', { domain }));
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
logger_1.logger.warn(lang_1.lang.__('OSS_CORS_RULE_REMOVE_FAILED', { domain, error: String(error) }));
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
const bindCustomDomain = async (bucketName, domain) => {
|
|
152
|
+
const mainDomain = (0, domainUtils_1.extractMainDomain)(domain);
|
|
153
|
+
const hostRecord = (0, domainUtils_1.extractHostRecord)(domain, mainDomain);
|
|
154
|
+
const ossEndpoint = getOssEndpoint(bucketName);
|
|
155
|
+
const bucketCnameBound = await putBucketCname(bucketName, domain);
|
|
156
|
+
await addCorsRuleForDomain(bucketName, domain);
|
|
157
|
+
if (!dnsOps) {
|
|
158
|
+
logger_1.logger.warn(lang_1.lang.__('OSS_DNS_MANUAL_CONFIG_REQUIRED', { domain, cname: ossEndpoint }));
|
|
159
|
+
return { domain, cname: ossEndpoint, bucketCnameBound };
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
const existingRecords = await dnsOps.describeDomainRecords(mainDomain, hostRecord);
|
|
163
|
+
const existingRecord = existingRecords.find((record) => record.rr === hostRecord && record.type === 'CNAME' && record.value === ossEndpoint);
|
|
164
|
+
if (existingRecord) {
|
|
165
|
+
logger_1.logger.info(lang_1.lang.__('OSS_DNS_CNAME_EXISTS', { domain, cname: ossEndpoint }));
|
|
166
|
+
return {
|
|
167
|
+
domain,
|
|
168
|
+
cname: ossEndpoint,
|
|
169
|
+
dnsRecordId: existingRecord.recordId,
|
|
170
|
+
bucketCnameBound,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
const recordId = await dnsOps.addDomainRecord({
|
|
174
|
+
domainName: mainDomain,
|
|
175
|
+
rr: hostRecord,
|
|
176
|
+
type: 'CNAME',
|
|
177
|
+
value: ossEndpoint,
|
|
178
|
+
ttl: 600,
|
|
179
|
+
});
|
|
180
|
+
logger_1.logger.info(lang_1.lang.__('OSS_DNS_CNAME_CREATED', { domain, cname: ossEndpoint }));
|
|
181
|
+
return { domain, cname: ossEndpoint, dnsRecordId: recordId, bucketCnameBound };
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
logger_1.logger.warn(lang_1.lang.__('OSS_DNS_DOMAIN_NOT_MANAGED', { domain, cname: ossEndpoint }));
|
|
185
|
+
logger_1.logger.debug(`DNS error: ${error}`);
|
|
186
|
+
return { domain, cname: ossEndpoint, bucketCnameBound };
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
const unbindCustomDomain = async (bucketName, domain, dnsRecordId) => {
|
|
190
|
+
await removeCorsRuleForDomain(bucketName, domain);
|
|
191
|
+
await deleteBucketCname(bucketName, domain);
|
|
192
|
+
if (!dnsOps || !dnsRecordId || dnsRecordId === 'existing') {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
try {
|
|
196
|
+
await dnsOps.deleteDomainRecord(dnsRecordId);
|
|
197
|
+
logger_1.logger.info(lang_1.lang.__('OSS_DNS_CNAME_DELETED', { domain }));
|
|
198
|
+
}
|
|
199
|
+
catch (error) {
|
|
200
|
+
logger_1.logger.warn(lang_1.lang.__('OSS_DNS_CNAME_DELETE_FAILED', { domain, error: String(error) }));
|
|
201
|
+
}
|
|
202
|
+
};
|
|
13
203
|
return {
|
|
14
204
|
createBucket: async (config) => {
|
|
15
205
|
if (config.storageClass) {
|
|
@@ -236,6 +426,9 @@ const createOssOperations = (ossClient, region) => {
|
|
|
236
426
|
useBucket(bucketName);
|
|
237
427
|
await ossClient.put(objectKey, filePath);
|
|
238
428
|
},
|
|
429
|
+
bindCustomDomain,
|
|
430
|
+
unbindCustomDomain,
|
|
431
|
+
getOssEndpoint,
|
|
239
432
|
};
|
|
240
433
|
};
|
|
241
434
|
exports.createOssOperations = createOssOperations;
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RAM_ROLE_PROPAGATION_DELAY_MS = exports.APIGW_DNS_PUBLIC_RESOLUTION_DELAY_MS = exports.APIGW_DNS_PUBLIC_RESOLUTION_MAX_ATTEMPTS = exports.APIGW_DOMAIN_BIND_RETRY_DELAY_MS = exports.APIGW_DOMAIN_BIND_MAX_RETRIES = exports.STALE_LOCK_THRESHOLD = exports.DEFAULT_LOCK_RETRY_DELAY = exports.DEFAULT_LOCK_TIMEOUT = exports.LOCK_FILE_SUFFIX = exports.OSS_STATE_REQUEST_TIMEOUT_MS = exports.OSS_STATE_CONNECT_TIMEOUT_MS = exports.FC3_CODE_INLINE_SIZE_LIMIT = exports.ALIYUN_FC3_READ_TIMEOUT_MS = exports.ALIYUN_FC3_CONNECT_TIMEOUT_MS = exports.SI_LOCALSTACK_SERVER_PORT = exports.SI_BOOTSTRAP_BUCKET_PREFIX = exports.SI_BOOTSTRAP_FC_PREFIX = exports.
|
|
4
|
-
exports.CODE_ZIP_SIZE_LIMIT = 300 * 1000; // 300 KB
|
|
3
|
+
exports.RAM_ROLE_PROPAGATION_DELAY_MS = exports.APIGW_DNS_PUBLIC_RESOLUTION_DELAY_MS = exports.APIGW_DNS_PUBLIC_RESOLUTION_MAX_ATTEMPTS = exports.APIGW_DOMAIN_BIND_RETRY_DELAY_MS = exports.APIGW_DOMAIN_BIND_MAX_RETRIES = exports.STALE_LOCK_THRESHOLD = exports.DEFAULT_LOCK_RETRY_DELAY = exports.DEFAULT_LOCK_TIMEOUT = exports.LOCK_FILE_SUFFIX = exports.OSS_STATE_REQUEST_TIMEOUT_MS = exports.OSS_STATE_CONNECT_TIMEOUT_MS = exports.FC3_CODE_INLINE_SIZE_LIMIT = exports.ALIYUN_FC3_READ_TIMEOUT_MS = exports.ALIYUN_FC3_CONNECT_TIMEOUT_MS = exports.SI_LOCALSTACK_SERVER_PORT = exports.SI_BOOTSTRAP_BUCKET_PREFIX = exports.SI_BOOTSTRAP_FC_PREFIX = exports.OSS_DEPLOYMENT_TIMEOUT = exports.CODE_ZIP_SIZE_LIMIT = void 0;
|
|
4
|
+
exports.CODE_ZIP_SIZE_LIMIT = 300 * 1000; // 300 KB - code package size limit for inline deployment
|
|
5
5
|
exports.OSS_DEPLOYMENT_TIMEOUT = 3000; // in seconds
|
|
6
|
-
exports.ROS_STACK_OPERATION_TIMEOUT = 3600; // in seconds
|
|
7
6
|
exports.SI_BOOTSTRAP_FC_PREFIX = 'si-bootstrap-api';
|
|
8
7
|
exports.SI_BOOTSTRAP_BUCKET_PREFIX = 'si-bootstrap-artifacts';
|
|
9
8
|
exports.SI_LOCALSTACK_SERVER_PORT = 4567;
|
|
@@ -55,9 +55,12 @@ exports.getIacLocation = getIacLocation;
|
|
|
55
55
|
const setContext = async (config, reaValToken = false) => {
|
|
56
56
|
const region = config.region ??
|
|
57
57
|
config.iacProvider?.region ??
|
|
58
|
-
process.env.
|
|
58
|
+
process.env.SI_REGION ??
|
|
59
59
|
process.env.ALIYUN_REGION ??
|
|
60
60
|
'cn-hangzhou';
|
|
61
|
+
if (process.env.ROS_REGION_ID && !process.env.SI_REGION) {
|
|
62
|
+
console.warn('Warning: ROS_REGION_ID environment variable is deprecated. Please use SI_REGION instead.');
|
|
63
|
+
}
|
|
61
64
|
const provider = (config.provider ??
|
|
62
65
|
config.iacProvider?.name ??
|
|
63
66
|
providerEnum_1.ProviderEnum.ALIYUN);
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractHostRecord = exports.extractMainDomain = void 0;
|
|
4
|
+
const MULTI_LEVEL_TLDS = new Set([
|
|
5
|
+
// United Kingdom
|
|
6
|
+
'co.uk',
|
|
7
|
+
'org.uk',
|
|
8
|
+
'me.uk',
|
|
9
|
+
'net.uk',
|
|
10
|
+
'ltd.uk',
|
|
11
|
+
'plc.uk',
|
|
12
|
+
'gov.uk',
|
|
13
|
+
'nhs.uk',
|
|
14
|
+
'police.uk',
|
|
15
|
+
'mod.uk',
|
|
16
|
+
'sch.uk',
|
|
17
|
+
'ac.uk',
|
|
18
|
+
// China
|
|
19
|
+
'com.cn',
|
|
20
|
+
'net.cn',
|
|
21
|
+
'org.cn',
|
|
22
|
+
'gov.cn',
|
|
23
|
+
'edu.cn',
|
|
24
|
+
'ac.cn',
|
|
25
|
+
'mil.cn',
|
|
26
|
+
// Australia
|
|
27
|
+
'com.au',
|
|
28
|
+
'net.au',
|
|
29
|
+
'org.au',
|
|
30
|
+
'gov.au',
|
|
31
|
+
'edu.au',
|
|
32
|
+
'asn.au',
|
|
33
|
+
'id.au',
|
|
34
|
+
// Brazil
|
|
35
|
+
'com.br',
|
|
36
|
+
'net.br',
|
|
37
|
+
'org.br',
|
|
38
|
+
'gov.br',
|
|
39
|
+
'edu.br',
|
|
40
|
+
// Argentina
|
|
41
|
+
'com.ar',
|
|
42
|
+
'net.ar',
|
|
43
|
+
'org.ar',
|
|
44
|
+
'gov.ar',
|
|
45
|
+
// Japan
|
|
46
|
+
'co.jp',
|
|
47
|
+
'ne.jp',
|
|
48
|
+
'or.jp',
|
|
49
|
+
'go.jp',
|
|
50
|
+
'ac.jp',
|
|
51
|
+
'ad.jp',
|
|
52
|
+
// South Korea
|
|
53
|
+
'co.kr',
|
|
54
|
+
'or.kr',
|
|
55
|
+
'go.kr',
|
|
56
|
+
'ac.kr',
|
|
57
|
+
'ne.kr',
|
|
58
|
+
// India
|
|
59
|
+
'co.in',
|
|
60
|
+
'net.in',
|
|
61
|
+
'org.in',
|
|
62
|
+
'gov.in',
|
|
63
|
+
'edu.in',
|
|
64
|
+
'nic.in',
|
|
65
|
+
// New Zealand
|
|
66
|
+
'co.nz',
|
|
67
|
+
'net.nz',
|
|
68
|
+
'org.nz',
|
|
69
|
+
'govt.nz',
|
|
70
|
+
'ac.nz',
|
|
71
|
+
// South Africa
|
|
72
|
+
'co.za',
|
|
73
|
+
'net.za',
|
|
74
|
+
'org.za',
|
|
75
|
+
'gov.za',
|
|
76
|
+
'ac.za',
|
|
77
|
+
// Hong Kong
|
|
78
|
+
'com.hk',
|
|
79
|
+
'net.hk',
|
|
80
|
+
'org.hk',
|
|
81
|
+
'gov.hk',
|
|
82
|
+
'edu.hk',
|
|
83
|
+
// Taiwan
|
|
84
|
+
'com.tw',
|
|
85
|
+
'net.tw',
|
|
86
|
+
'org.tw',
|
|
87
|
+
'gov.tw',
|
|
88
|
+
'edu.tw',
|
|
89
|
+
// Singapore
|
|
90
|
+
'com.sg',
|
|
91
|
+
'net.sg',
|
|
92
|
+
'org.sg',
|
|
93
|
+
'gov.sg',
|
|
94
|
+
'edu.sg',
|
|
95
|
+
]);
|
|
96
|
+
const extractMainDomain = (domainName) => {
|
|
97
|
+
const parts = domainName.split('.');
|
|
98
|
+
if (parts.length <= 2) {
|
|
99
|
+
return domainName;
|
|
100
|
+
}
|
|
101
|
+
const potentialTld = `${parts[parts.length - 2]}.${parts[parts.length - 1]}`;
|
|
102
|
+
if (MULTI_LEVEL_TLDS.has(potentialTld)) {
|
|
103
|
+
if (parts.length <= 3) {
|
|
104
|
+
return domainName;
|
|
105
|
+
}
|
|
106
|
+
return parts.slice(-3).join('.');
|
|
107
|
+
}
|
|
108
|
+
return parts.slice(-2).join('.');
|
|
109
|
+
};
|
|
110
|
+
exports.extractMainDomain = extractMainDomain;
|
|
111
|
+
const extractHostRecord = (fullDomain, mainDomain) => {
|
|
112
|
+
if (fullDomain === mainDomain) {
|
|
113
|
+
return '@';
|
|
114
|
+
}
|
|
115
|
+
const suffix = `.${mainDomain}`;
|
|
116
|
+
if (fullDomain.endsWith(suffix)) {
|
|
117
|
+
return fullDomain.slice(0, -suffix.length);
|
|
118
|
+
}
|
|
119
|
+
return fullDomain;
|
|
120
|
+
};
|
|
121
|
+
exports.extractHostRecord = extractHostRecord;
|
|
@@ -3,7 +3,7 @@ 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.
|
|
6
|
+
exports.getIacDefinition = exports.calcValue = exports.readCodeSize = exports.resolveCode = void 0;
|
|
7
7
|
const node_path_1 = __importDefault(require("node:path"));
|
|
8
8
|
const node_fs_1 = __importDefault(require("node:fs"));
|
|
9
9
|
const lodash_1 = require("lodash");
|
|
@@ -91,10 +91,3 @@ const getIacDefinition = (iac, rawValue) => {
|
|
|
91
91
|
return iac.functions?.find((fc) => fc.key === rawValue);
|
|
92
92
|
};
|
|
93
93
|
exports.getIacDefinition = getIacDefinition;
|
|
94
|
-
const splitDomain = (domain) => {
|
|
95
|
-
const parts = domain.split('.');
|
|
96
|
-
const rr = parts.length > 2 ? parts[0] : '@';
|
|
97
|
-
const domainName = parts.length > 2 ? parts.slice(1).join('.') : domain;
|
|
98
|
-
return { rr, domainName };
|
|
99
|
-
};
|
|
100
|
-
exports.splitDomain = splitDomain;
|
|
@@ -5,45 +5,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.createCosStateBackend = void 0;
|
|
7
7
|
const cos_nodejs_sdk_v5_1 = __importDefault(require("cos-nodejs-sdk-v5"));
|
|
8
|
-
const
|
|
9
|
-
const node_os_1 = __importDefault(require("node:os"));
|
|
10
|
-
const node_child_process_1 = require("node:child_process");
|
|
11
|
-
const types_1 = require("../../types");
|
|
8
|
+
const remoteStateBackend_1 = require("./remoteStateBackend");
|
|
12
9
|
const constants_1 = require("../constants");
|
|
13
|
-
const lockManager_1 = require("../lockManager");
|
|
14
|
-
const logger_1 = require("../logger");
|
|
15
|
-
const lang_1 = require("../../lang");
|
|
16
|
-
const LOCK_KEY_SUFFIX = '.si-lock';
|
|
17
|
-
const getUserEmail = () => {
|
|
18
|
-
try {
|
|
19
|
-
const email = (0, node_child_process_1.execSync)('git config user.email', {
|
|
20
|
-
encoding: 'utf-8',
|
|
21
|
-
stdio: ['pipe', 'pipe', 'ignore'],
|
|
22
|
-
}).trim();
|
|
23
|
-
if (email)
|
|
24
|
-
return email;
|
|
25
|
-
}
|
|
26
|
-
catch {
|
|
27
|
-
// noop
|
|
28
|
-
}
|
|
29
|
-
const username = node_os_1.default.userInfo().username || 'unknown';
|
|
30
|
-
const hostname = node_os_1.default.hostname() || 'unknown';
|
|
31
|
-
return `${username}@${hostname}`;
|
|
32
|
-
};
|
|
33
|
-
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
34
|
-
const isLockStale = (lock) => {
|
|
35
|
-
const acquiredAt = new Date(lock.acquiredAt).getTime();
|
|
36
|
-
return Date.now() - acquiredAt > constants_1.STALE_LOCK_THRESHOLD;
|
|
37
|
-
};
|
|
38
|
-
const isProcessAlive = (pid) => {
|
|
39
|
-
try {
|
|
40
|
-
process.kill(pid, 0);
|
|
41
|
-
return true;
|
|
42
|
-
}
|
|
43
|
-
catch {
|
|
44
|
-
return false;
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
10
|
const promisifyGet = (cosClient, bucket, region, key) => {
|
|
48
11
|
return new Promise((resolve, reject) => {
|
|
49
12
|
cosClient.getObject({ Bucket: bucket, Region: region, Key: key }, (err, data) => {
|
|
@@ -81,166 +44,31 @@ const promisifyDelete = (cosClient, bucket, region, key) => {
|
|
|
81
44
|
});
|
|
82
45
|
});
|
|
83
46
|
};
|
|
84
|
-
const
|
|
47
|
+
const createCosStorageAdapter = (config) => {
|
|
85
48
|
const cosClient = new cos_nodejs_sdk_v5_1.default({
|
|
86
49
|
SecretId: config.accessKeyId,
|
|
87
50
|
SecretKey: config.accessKeySecret,
|
|
88
51
|
SecurityToken: config.securityToken,
|
|
89
52
|
Timeout: constants_1.OSS_STATE_REQUEST_TIMEOUT_MS,
|
|
90
53
|
});
|
|
91
|
-
const lockKey = `${config.key}${LOCK_KEY_SUFFIX}`;
|
|
92
|
-
const readObject = async (key) => {
|
|
93
|
-
const body = await promisifyGet(cosClient, config.bucket, config.region, key);
|
|
94
|
-
if (!body)
|
|
95
|
-
return null;
|
|
96
|
-
return JSON.parse(body.toString('utf-8'));
|
|
97
|
-
};
|
|
98
|
-
const writeObject = async (key, data) => {
|
|
99
|
-
await promisifyPut(cosClient, config.bucket, config.region, key, JSON.stringify(data, null, 2));
|
|
100
|
-
};
|
|
101
|
-
const deleteObject = async (key) => {
|
|
102
|
-
await promisifyDelete(cosClient, config.bucket, config.region, key);
|
|
103
|
-
};
|
|
104
|
-
const readLockObject = async () => {
|
|
105
|
-
return readObject(lockKey);
|
|
106
|
-
};
|
|
107
|
-
const acquireLockObject = async (operation, options = {}) => {
|
|
108
|
-
const timeout = options.timeout ?? constants_1.DEFAULT_LOCK_TIMEOUT;
|
|
109
|
-
const retryDelay = options.retryDelay ?? constants_1.DEFAULT_LOCK_RETRY_DELAY;
|
|
110
|
-
const maxRetries = options.maxRetries ?? Math.ceil(timeout / retryDelay);
|
|
111
|
-
const metadata = {
|
|
112
|
-
id: node_crypto_1.default.randomBytes(16).toString('hex'),
|
|
113
|
-
user: getUserEmail(),
|
|
114
|
-
processId: process.pid,
|
|
115
|
-
hostname: node_os_1.default.hostname(),
|
|
116
|
-
operation,
|
|
117
|
-
acquiredAt: new Date().toISOString(),
|
|
118
|
-
path: `cos://${config.bucket}/${config.key}`,
|
|
119
|
-
};
|
|
120
|
-
const startTime = Date.now();
|
|
121
|
-
let attempt = 0;
|
|
122
|
-
while (attempt <= maxRetries) {
|
|
123
|
-
const existingLock = await readLockObject();
|
|
124
|
-
if (!existingLock) {
|
|
125
|
-
await writeObject(lockKey, metadata);
|
|
126
|
-
const verify = await readLockObject();
|
|
127
|
-
if (verify && verify.id === metadata.id) {
|
|
128
|
-
return metadata;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
if (existingLock.hostname === node_os_1.default.hostname() && !isProcessAlive(existingLock.processId)) {
|
|
133
|
-
logger_1.logger.info(lang_1.lang.__('LOCK_AUTO_RELEASED_DEAD_PROCESS', {
|
|
134
|
-
processId: String(existingLock.processId),
|
|
135
|
-
hostname: existingLock.hostname,
|
|
136
|
-
user: existingLock.user,
|
|
137
|
-
acquiredAt: existingLock.acquiredAt,
|
|
138
|
-
}));
|
|
139
|
-
await deleteObject(lockKey);
|
|
140
|
-
attempt++;
|
|
141
|
-
continue;
|
|
142
|
-
}
|
|
143
|
-
if (isLockStale(existingLock)) {
|
|
144
|
-
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);
|
|
145
|
-
}
|
|
146
|
-
if (Date.now() - startTime >= timeout) {
|
|
147
|
-
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);
|
|
148
|
-
}
|
|
149
|
-
const acquiredAt = new Date(existingLock.acquiredAt);
|
|
150
|
-
const minutesAgo = Math.floor((Date.now() - acquiredAt.getTime()) / 60000);
|
|
151
|
-
const timeAgo = minutesAgo < 1 ? lang_1.lang.__('LOCK_TIME_AGO_LESS_THAN_MINUTE') : `${minutesAgo}m`;
|
|
152
|
-
logger_1.logger.info(lang_1.lang.__('LOCK_WAITING', {
|
|
153
|
-
user: existingLock.user,
|
|
154
|
-
timeAgo,
|
|
155
|
-
attempt: String(attempt + 1),
|
|
156
|
-
}));
|
|
157
|
-
}
|
|
158
|
-
const delay = Math.min(retryDelay * Math.pow(2, attempt), 30000);
|
|
159
|
-
await sleep(delay);
|
|
160
|
-
attempt++;
|
|
161
|
-
}
|
|
162
|
-
const existingLock = await readLockObject();
|
|
163
|
-
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);
|
|
164
|
-
};
|
|
165
54
|
return {
|
|
166
|
-
|
|
167
|
-
const
|
|
168
|
-
if (
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
return { version: types_1.CURRENT_STATE_VERSION, provider, app, service, stages: {}, resources: {} };
|
|
173
|
-
},
|
|
174
|
-
saveState: async (state, app, service, stage) => {
|
|
175
|
-
let existing = {
|
|
176
|
-
version: types_1.CURRENT_STATE_VERSION,
|
|
177
|
-
provider: state.provider,
|
|
178
|
-
app,
|
|
179
|
-
service,
|
|
180
|
-
stages: {},
|
|
181
|
-
resources: {},
|
|
182
|
-
};
|
|
183
|
-
try {
|
|
184
|
-
const raw = await readObject(config.key);
|
|
185
|
-
if (raw)
|
|
186
|
-
existing = raw;
|
|
187
|
-
}
|
|
188
|
-
catch {
|
|
189
|
-
// noop
|
|
190
|
-
}
|
|
191
|
-
const stateToSave = {
|
|
192
|
-
...existing,
|
|
193
|
-
version: types_1.CURRENT_STATE_VERSION,
|
|
194
|
-
app,
|
|
195
|
-
service,
|
|
196
|
-
provider: state.provider,
|
|
197
|
-
stages: {
|
|
198
|
-
...existing.stages,
|
|
199
|
-
[stage]: { resources: state.resources },
|
|
200
|
-
},
|
|
201
|
-
resources: state.resources,
|
|
202
|
-
};
|
|
203
|
-
await writeObject(config.key, stateToSave);
|
|
204
|
-
},
|
|
205
|
-
acquireLock: async (operation, options) => {
|
|
206
|
-
const meta = await acquireLockObject(operation, options);
|
|
207
|
-
return meta.id;
|
|
55
|
+
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'));
|
|
208
60
|
},
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
if (existing && existing.id === lockId) {
|
|
212
|
-
await deleteObject(lockKey);
|
|
213
|
-
}
|
|
61
|
+
write: async (key, data) => {
|
|
62
|
+
await promisifyPut(cosClient, config.bucket, config.region, key, JSON.stringify(data, null, 2));
|
|
214
63
|
},
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
if (!existing)
|
|
218
|
-
return false;
|
|
219
|
-
if (existing.id !== lockId) {
|
|
220
|
-
throw new Error(`Lock ID mismatch. Current lock ID is ${existing.id}, but you provided ${lockId}`);
|
|
221
|
-
}
|
|
222
|
-
await deleteObject(lockKey);
|
|
223
|
-
return true;
|
|
224
|
-
},
|
|
225
|
-
readLock: async () => {
|
|
226
|
-
return readLockObject();
|
|
227
|
-
},
|
|
228
|
-
withLock: async (operation, fn, options) => {
|
|
229
|
-
let lockId = null;
|
|
230
|
-
try {
|
|
231
|
-
const meta = await acquireLockObject(operation, options);
|
|
232
|
-
lockId = meta.id;
|
|
233
|
-
return await fn();
|
|
234
|
-
}
|
|
235
|
-
finally {
|
|
236
|
-
if (lockId) {
|
|
237
|
-
const existing = await readLockObject();
|
|
238
|
-
if (existing && existing.id === lockId) {
|
|
239
|
-
await deleteObject(lockKey);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
}
|
|
64
|
+
delete: async (key) => {
|
|
65
|
+
await promisifyDelete(cosClient, config.bucket, config.region, key);
|
|
243
66
|
},
|
|
67
|
+
lockPath: `cos://${config.bucket}/${config.key}`,
|
|
244
68
|
};
|
|
245
69
|
};
|
|
70
|
+
const createCosStateBackend = (config) => {
|
|
71
|
+
const adapter = createCosStorageAdapter(config);
|
|
72
|
+
return (0, remoteStateBackend_1.createRemoteStateBackend)(adapter, { key: config.key });
|
|
73
|
+
};
|
|
246
74
|
exports.createCosStateBackend = createCosStateBackend;
|
|
@@ -24,6 +24,8 @@ __exportStar(require("./types"), exports);
|
|
|
24
24
|
__exportStar(require("./localStateBackend"), exports);
|
|
25
25
|
__exportStar(require("./ossStateBackend"), exports);
|
|
26
26
|
__exportStar(require("./cosStateBackend"), exports);
|
|
27
|
+
__exportStar(require("./remoteStateBackend"), exports);
|
|
28
|
+
__exportStar(require("./lockUtils"), exports);
|
|
27
29
|
const createStateBackend = (backendConfig, context) => {
|
|
28
30
|
if (!backendConfig || backendConfig.type === types_1.StateBackendType.LOCAL) {
|
|
29
31
|
return (0, localStateBackend_1.createLocalStateBackend)(context.app, context.service, context.baseDir);
|
|
@@ -0,0 +1,55 @@
|
|
|
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.createLockMetadata = exports.isProcessAlive = exports.isLockStale = exports.sleep = exports.getUserEmail = exports.LOCK_KEY_SUFFIX = void 0;
|
|
7
|
+
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
8
|
+
const node_os_1 = __importDefault(require("node:os"));
|
|
9
|
+
const node_child_process_1 = require("node:child_process");
|
|
10
|
+
const constants_1 = require("../constants");
|
|
11
|
+
exports.LOCK_KEY_SUFFIX = '.si-lock';
|
|
12
|
+
const getUserEmail = () => {
|
|
13
|
+
try {
|
|
14
|
+
const email = (0, node_child_process_1.execSync)('git config user.email', {
|
|
15
|
+
encoding: 'utf-8',
|
|
16
|
+
stdio: ['pipe', 'pipe', 'ignore'],
|
|
17
|
+
}).trim();
|
|
18
|
+
if (email)
|
|
19
|
+
return email;
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
// noop
|
|
23
|
+
}
|
|
24
|
+
const username = node_os_1.default.userInfo().username || 'unknown';
|
|
25
|
+
const hostname = node_os_1.default.hostname() || 'unknown';
|
|
26
|
+
return `${username}@${hostname}`;
|
|
27
|
+
};
|
|
28
|
+
exports.getUserEmail = getUserEmail;
|
|
29
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
30
|
+
exports.sleep = sleep;
|
|
31
|
+
const isLockStale = (lock) => {
|
|
32
|
+
const acquiredAt = new Date(lock.acquiredAt).getTime();
|
|
33
|
+
return Date.now() - acquiredAt > constants_1.STALE_LOCK_THRESHOLD;
|
|
34
|
+
};
|
|
35
|
+
exports.isLockStale = isLockStale;
|
|
36
|
+
const isProcessAlive = (pid) => {
|
|
37
|
+
try {
|
|
38
|
+
process.kill(pid, 0);
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
exports.isProcessAlive = isProcessAlive;
|
|
46
|
+
const createLockMetadata = (operation, lockPath) => ({
|
|
47
|
+
id: node_crypto_1.default.randomBytes(16).toString('hex'),
|
|
48
|
+
user: (0, exports.getUserEmail)(),
|
|
49
|
+
processId: process.pid,
|
|
50
|
+
hostname: node_os_1.default.hostname(),
|
|
51
|
+
operation,
|
|
52
|
+
acquiredAt: new Date().toISOString(),
|
|
53
|
+
path: lockPath,
|
|
54
|
+
});
|
|
55
|
+
exports.createLockMetadata = createLockMetadata;
|