forked from devsapp/cdn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
191 lines (161 loc) · 5.88 KB
/
index.ts
File metadata and controls
191 lines (161 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import logger from './common/logger';
import {InputProps} from './common/entity';
import {CDNConfig} from './lib/interface/cdn/CDNConfig';
import {CDNClient} from './utils/client';
import {
askForAddFun,
askForStartCdnDomain,
handlerPreMethod,
hasAddCname,
helpAddCname,
isChanging,
wait
} from './utils/util';
import {CatchableError, help, lodash as _} from "@serverless-devs/core";
import {HELP_INFO} from "./common/contants";
export default class CdnComponent {
/**
* 修改或者添加加速域名的配置
* @param inputs
* @returns
*/
async deploy(inputs: InputProps<CDNConfig>) {
await handlerPreMethod(inputs);
const client = await CDNClient.getInstant(inputs);
const config = inputs.props;
const domainName = config.domainName;
let cdnDomain = await client.getCdnDomain(domainName);
if (cdnDomain) {
// 是否处于变更中
if (isChanging(cdnDomain.domainStatus)) {
throw new CatchableError('Please try it later!', 'The cdnDomain is changing!');
}
// 是否已停用
if (cdnDomain.domainStatus == 'offline') {
await askForStartCdnDomain(client.startCdnDomain, inputs);
}
await client.updateCdnDomain(config);
} else {
// 校验域名归属
if (!await client.domainHasVerify(domainName)) {
const domainVerifyContent = await client.getDomainVerifyContent(domainName);
throw new CatchableError('Owner verification of the root domain failed.', `Pleas go to the DNS service provider to configure the TXT record\nHost: [verification] Record Type: [TXT] RecordValue: [${domainVerifyContent}]`);
}
await client.addCdnDomain(config, {waitUntilFinished: config.waitUntilFinished});
// 重试5次,尽量保证可以获取到cname
let counter = 5;
do {
if (counter <= 0) {
break;
}
await wait(counter-- * 1000);
cdnDomain = await client.getCdnDomain(domainName);
} while (_.isEmpty(cdnDomain.cname));
}
// 校验cname是否已添加
const cname = cdnDomain.cname;
await helpAddCname(cname, domainName);
if (await hasAddCname(cname, domainName)) {
if (config.refreshAfterDeploy) {
await client.refreshObjectCaches(config.refreshConfig);
}
}
}
/**
* 启用配置的域名并且状态为停用的加速域名
* @param inputs
*/
async start(inputs: InputProps<CDNConfig>) {
await handlerPreMethod(inputs);
const instant = await CDNClient.getInstant(inputs);
const domainName = inputs.props.domainName;
const cdnDomain = await instant.getCdnDomain(domainName);
if (_.isEmpty(cdnDomain)) {
await askForAddFun(this.deploy, inputs);
return;
}
// 是否处于变更中
if (isChanging(cdnDomain.domainStatus)) {
throw new CatchableError('Please try it later!', 'The cdnDomain is changing!');
}
const success = await instant.startCdnDomain(domainName);
if (!success) {
await askForAddFun(this.deploy, inputs);
}
// 校验cname是否已添加
const cname = cdnDomain.cname;
await helpAddCname(cname, domainName);
}
/**
* 停用配置的加速域名
* @param inputs
*/
async stop(inputs: InputProps<CDNConfig>) {
await handlerPreMethod(inputs);
const instant = await CDNClient.getInstant(inputs);
const domainName = inputs.props.domainName;
const cdnDomain = await instant.getCdnDomain(domainName);
if (_.isEmpty(cdnDomain)) {
throw new CatchableError(`The domain: [${domainName}] not found!`);
}
// 是否处于变更中
if (isChanging(cdnDomain.domainStatus)) {
throw new CatchableError('Please try it later!', 'The cdnDomain is changing!');
}
await instant.stopCdnDomain(inputs.props.domainName);
}
/**
* 刷新节点上的文件内容
* @param inputs
*/
async refresh(inputs: InputProps<CDNConfig>) {
await handlerPreMethod(inputs, {
requiredRefreshConfig: true
});
const instant = await CDNClient.getInstant(inputs);
await instant.refreshObjectCaches(inputs.props.refreshConfig);
}
/**
* 预热源站内容
* @param inputs
*/
async warmUp(inputs: InputProps<CDNConfig>) {
await handlerPreMethod(inputs, {
requiredPushObjectCacheConfig: true
});
const instant = await CDNClient.getInstant(inputs);
await instant.pushObjectCache(inputs.props.pushObjectCacheConfig);
}
/**
* 删除加速域名
* @param inputs
*/
async remove(inputs: InputProps<CDNConfig>) {
await handlerPreMethod(inputs);
const instant = await CDNClient.getInstant(inputs);
const domainName = inputs.props.domainName;
const cdnDomain = await instant.getCdnDomain(domainName);
if (_.isEmpty(cdnDomain)) {
throw new CatchableError(`The domain: [${domainName}] not found!`);
}
// 是否处于变更中
if (isChanging(cdnDomain.domainStatus)) {
throw new CatchableError('Please try it later!', 'The cdnDomain is changing!');
}
await instant.deleteCdnDomain(inputs.props.domainName);
}
/**
* 帮助命令
* @param inputs
*/
async help(inputs: InputProps<CDNConfig>) {
help(HELP_INFO);
}
/**
* 将 SDK 方法抛出【待定】
* @param inputs
*/
async api(inputs: InputProps<CDNConfig>) {
logger.log(JSON.stringify(inputs, null, 2));
}
}