esa-cli 0.0.2-beta.10 → 0.0.2-beta.12
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/commands/commit/index.js +34 -24
- package/dist/commands/deploy/helper.js +10 -26
- package/dist/commands/deploy/index.js +49 -60
- package/dist/commands/deployments/delete.js +1 -16
- package/dist/commands/deployments/index.js +1 -1
- package/dist/commands/deployments/list.js +8 -15
- package/dist/commands/dev/ew2/cacheService.js +33 -0
- package/dist/commands/dev/ew2/devEntry.js +2 -1
- package/dist/commands/dev/ew2/devPack.js +5 -3
- package/dist/commands/dev/ew2/kvService.js +27 -0
- package/dist/commands/dev/ew2/mock/cache.js +99 -15
- package/dist/commands/dev/ew2/mock/kv.js +142 -21
- package/dist/commands/dev/ew2/server.js +150 -18
- package/dist/commands/dev/index.js +2 -3
- package/dist/commands/domain/add.js +1 -1
- package/dist/commands/domain/delete.js +4 -4
- package/dist/commands/domain/index.js +1 -1
- package/dist/commands/domain/list.js +3 -3
- package/dist/commands/init/helper.js +28 -4
- package/dist/commands/init/index.js +78 -14
- package/dist/commands/login/index.js +49 -3
- package/dist/commands/logout.js +1 -1
- package/dist/commands/route/add.js +50 -52
- package/dist/commands/route/delete.js +29 -23
- package/dist/commands/route/helper.js +124 -0
- package/dist/commands/route/index.js +1 -1
- package/dist/commands/route/list.js +53 -14
- package/dist/commands/routine/index.js +1 -1
- package/dist/commands/routine/list.js +4 -5
- package/dist/commands/site/index.js +1 -1
- package/dist/commands/utils.js +5 -5
- package/dist/docs/Commands_en.md +27 -13
- package/dist/docs/Commands_zh_CN.md +14 -0
- package/dist/docs/Dev_en.md +0 -0
- package/dist/docs/Dev_zh_CN.md +0 -0
- package/dist/i18n/locales.json +102 -10
- package/dist/index.js +6 -1
- package/dist/libs/api.js +32 -9
- package/dist/libs/apiService.js +88 -78
- package/dist/libs/interface.js +0 -1
- package/dist/utils/checkIsRoutineCreated.js +0 -16
- package/package.json +3 -4
|
@@ -1,31 +1,115 @@
|
|
|
1
1
|
class MockCache {
|
|
2
|
-
constructor() {
|
|
3
|
-
this.
|
|
2
|
+
constructor(port) {
|
|
3
|
+
this.port = port;
|
|
4
4
|
}
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
async put(reqOrUrl, response) {
|
|
7
|
+
if (arguments.length < 2) {
|
|
8
|
+
throw new TypeError(
|
|
9
|
+
`Failed to execute 'put' on 'cache': 2 arguments required, but only ${arguments.length} present.`
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
if (!reqOrUrl) {
|
|
13
|
+
throw new TypeError(
|
|
14
|
+
"Failed to execute 'put' on 'cache': 2 arguments required, but only 0 present."
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
if (!(response instanceof Response)) {
|
|
18
|
+
throw new TypeError(
|
|
19
|
+
"Failed to execute 'put' on 'cache': Argument 2 is not of type Response."
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
const body = await response.clone().text();
|
|
25
|
+
const headers = {};
|
|
26
|
+
response.headers.forEach((v, k) => (headers[k] = v));
|
|
27
|
+
|
|
28
|
+
const cacheControl = response.headers.get('Cache-Control') || '';
|
|
29
|
+
const ttl = this.parseTTL(cacheControl);
|
|
10
30
|
|
|
11
|
-
|
|
12
|
-
|
|
31
|
+
const key = this.normalizeKey(reqOrUrl);
|
|
32
|
+
const fetchRes = await fetch(
|
|
33
|
+
`http://localhost:${this.port}/mock_cache/put`,
|
|
34
|
+
{
|
|
35
|
+
method: 'POST',
|
|
36
|
+
headers: { 'Content-Type': 'application/json' },
|
|
37
|
+
body: JSON.stringify({
|
|
38
|
+
key,
|
|
39
|
+
response: {
|
|
40
|
+
status: response.status,
|
|
41
|
+
headers,
|
|
42
|
+
body
|
|
43
|
+
},
|
|
44
|
+
ttl
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
);
|
|
48
|
+
if (!fetchRes.ok) {
|
|
49
|
+
const error = await fetchRes.json();
|
|
50
|
+
throw new Error(error.error);
|
|
51
|
+
}
|
|
52
|
+
return undefined;
|
|
53
|
+
} catch (err) {
|
|
54
|
+
throw new Error(`Cache put failed: ${err.message}`);
|
|
55
|
+
}
|
|
13
56
|
}
|
|
14
57
|
|
|
15
|
-
async
|
|
16
|
-
|
|
58
|
+
async get(reqOrUrl) {
|
|
59
|
+
const key = this.normalizeKey(reqOrUrl);
|
|
60
|
+
const fetchRes = await fetch(
|
|
61
|
+
`http://localhost:${this.port}/mock_cache/get`,
|
|
62
|
+
{
|
|
63
|
+
method: 'POST',
|
|
64
|
+
headers: { 'Content-Type': 'application/json' },
|
|
65
|
+
body: JSON.stringify({
|
|
66
|
+
key
|
|
67
|
+
})
|
|
68
|
+
}
|
|
69
|
+
);
|
|
70
|
+
if (!fetchRes.ok) {
|
|
71
|
+
const error = await fetchRes.json();
|
|
72
|
+
throw new Error(error.error);
|
|
73
|
+
}
|
|
74
|
+
const res = await fetchRes.json();
|
|
75
|
+
if (res && res.success) {
|
|
76
|
+
return new Response(res.data.response.body, {
|
|
77
|
+
status: res.data.response.status,
|
|
78
|
+
headers: new Headers(res.data.response.headers)
|
|
79
|
+
});
|
|
80
|
+
} else {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
17
83
|
}
|
|
18
84
|
|
|
19
85
|
async delete(reqOrUrl) {
|
|
20
|
-
|
|
86
|
+
const key = this.normalizeKey(reqOrUrl);
|
|
87
|
+
const fetchRes = await fetch(
|
|
88
|
+
`http://localhost:${this.port}/mock_cache/delete`,
|
|
89
|
+
{
|
|
90
|
+
method: 'POST',
|
|
91
|
+
headers: { 'Content-Type': 'application/json' },
|
|
92
|
+
body: JSON.stringify({
|
|
93
|
+
key
|
|
94
|
+
})
|
|
95
|
+
}
|
|
96
|
+
);
|
|
97
|
+
if (!fetchRes.ok) {
|
|
98
|
+
const error = await fetchRes.json();
|
|
99
|
+
throw new Error(error.error);
|
|
100
|
+
}
|
|
101
|
+
const res = await fetchRes.json();
|
|
102
|
+
return res.success;
|
|
21
103
|
}
|
|
22
104
|
|
|
23
|
-
|
|
24
|
-
|
|
105
|
+
normalizeKey(input) {
|
|
106
|
+
const url = input instanceof Request ? input.url : input;
|
|
107
|
+
return url.replace(/^https:/i, 'http:');
|
|
25
108
|
}
|
|
26
109
|
|
|
27
|
-
|
|
28
|
-
|
|
110
|
+
parseTTL(cacheControl) {
|
|
111
|
+
const maxAgeMatch = cacheControl.match(/max-age=(\d+)/);
|
|
112
|
+
return maxAgeMatch ? parseInt(maxAgeMatch[1]) : 3600;
|
|
29
113
|
}
|
|
30
114
|
}
|
|
31
115
|
|
|
@@ -1,44 +1,165 @@
|
|
|
1
1
|
class EdgeKV {
|
|
2
|
+
static port = 0;
|
|
3
|
+
JS_RESPONSE_BUFFER_THRESHOLD = 64 * 1024;
|
|
2
4
|
constructor(options) {
|
|
5
|
+
if (!options || (!options.namespace && !options.namespaceId)) {
|
|
6
|
+
throw new TypeError(
|
|
7
|
+
'The argument to `EdgeKV` must be an object with a `namespace` or `namespaceId` field'
|
|
8
|
+
);
|
|
9
|
+
}
|
|
3
10
|
this.namespace = options.namespace;
|
|
4
|
-
this.allData = {};
|
|
5
11
|
}
|
|
6
12
|
|
|
7
|
-
async
|
|
8
|
-
|
|
9
|
-
|
|
13
|
+
async put(key, value) {
|
|
14
|
+
if (arguments.length < 2) {
|
|
15
|
+
throw new TypeError(
|
|
16
|
+
`Failed to execute 'put' on 'EdgeKV': 2 arguments required, but only ${arguments.length} present.`
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
if (!key) {
|
|
20
|
+
throw new TypeError(
|
|
21
|
+
"Failed to execute 'put' on 'EdgeKV': 2 arguments required, but only 0 present."
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
if (typeof key !== 'string') {
|
|
25
|
+
throw new TypeError(
|
|
26
|
+
`Failed to execute 'put' on 'EdgeKV': 1th argument must be a string.`
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
let body;
|
|
32
|
+
if (typeof value === 'string') {
|
|
33
|
+
if (value.length > this.JS_RESPONSE_BUFFER_THRESHOLD) {
|
|
34
|
+
const encoder = new TextEncoder();
|
|
35
|
+
const encodedValue = encoder.encode(value);
|
|
36
|
+
|
|
37
|
+
body = new ReadableStream({
|
|
38
|
+
start(controller) {
|
|
39
|
+
controller.enqueue(encodedValue);
|
|
40
|
+
controller.close();
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
} else {
|
|
44
|
+
body = value;
|
|
45
|
+
}
|
|
46
|
+
} else if (value instanceof Response) {
|
|
47
|
+
const resBody = await value.clone().text();
|
|
48
|
+
const headers = {};
|
|
49
|
+
value.headers.forEach((v, k) => (headers[k] = v));
|
|
50
|
+
body = JSON.stringify({
|
|
51
|
+
body: resBody,
|
|
52
|
+
headers,
|
|
53
|
+
status: value.status
|
|
54
|
+
});
|
|
55
|
+
} else if (
|
|
56
|
+
value instanceof ReadableStream ||
|
|
57
|
+
value instanceof ArrayBuffer ||
|
|
58
|
+
ArrayBuffer.isView(value)
|
|
59
|
+
) {
|
|
60
|
+
body = value;
|
|
61
|
+
} else {
|
|
62
|
+
throw new TypeError(
|
|
63
|
+
`Failed to execute 'put' on 'EdgeKV': 2nd argument should be one of string/Response/ArrayBuffer/ArrayBufferView/ReadableStream`
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const fetchRes = await fetch(
|
|
68
|
+
`http://localhost:${EdgeKV.port}/mock_kv/put?key=${key}&namespace=${this.namespace}`,
|
|
69
|
+
{
|
|
70
|
+
method: 'POST',
|
|
71
|
+
headers: { 'Content-Type': 'application/json' },
|
|
72
|
+
body
|
|
73
|
+
}
|
|
74
|
+
);
|
|
75
|
+
if (!fetchRes.ok) {
|
|
76
|
+
const error = await fetchRes.json();
|
|
77
|
+
throw new Error(error.error);
|
|
78
|
+
}
|
|
10
79
|
return undefined;
|
|
80
|
+
} catch (err) {
|
|
81
|
+
throw new Error(`Cache put failed: ${err.message}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async get(key, options) {
|
|
86
|
+
const isTypeValid = (ty) =>
|
|
87
|
+
typeof ty === 'string' &&
|
|
88
|
+
(ty === 'text' ||
|
|
89
|
+
ty === 'json' ||
|
|
90
|
+
ty === 'stream' ||
|
|
91
|
+
ty === 'arrayBuffer');
|
|
92
|
+
|
|
93
|
+
if (options && !isTypeValid(options?.type)) {
|
|
94
|
+
throw new TypeError(
|
|
95
|
+
"EdgeKV.get: 2nd optional argument must be an object with a 'type' field. The 'type' field specifies the format of the return value and must be a string of 'text', 'json', 'stream' or 'arrayBuffer'"
|
|
96
|
+
);
|
|
11
97
|
}
|
|
12
|
-
const value = namespaceData[key];
|
|
13
98
|
const type = options?.type || 'text';
|
|
99
|
+
const fetchRes = await fetch(
|
|
100
|
+
`http://localhost:${EdgeKV.port}/mock_kv/get?key=${key}&namespace=${this.namespace}`,
|
|
101
|
+
{
|
|
102
|
+
method: 'POST',
|
|
103
|
+
headers: { 'Content-Type': 'application/json' }
|
|
104
|
+
}
|
|
105
|
+
);
|
|
106
|
+
// 判断是否存在key
|
|
107
|
+
let isGetFailed = false;
|
|
108
|
+
fetchRes.headers.forEach((v, k) => {
|
|
109
|
+
if (k === 'kv-get-empty') {
|
|
110
|
+
isGetFailed = true;
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
if (isGetFailed) {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
14
116
|
switch (type) {
|
|
15
117
|
case 'text':
|
|
16
|
-
return
|
|
118
|
+
return fetchRes.text();
|
|
17
119
|
case 'json':
|
|
18
120
|
try {
|
|
19
|
-
|
|
121
|
+
const value = await fetchRes.text();
|
|
122
|
+
const userObject = JSON.parse(value);
|
|
123
|
+
return userObject;
|
|
20
124
|
} catch (error) {
|
|
21
|
-
throw new
|
|
125
|
+
throw new TypeError(`Invalid JSON: ${err.message}`);
|
|
22
126
|
}
|
|
23
127
|
case 'arrayBuffer':
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
128
|
+
try {
|
|
129
|
+
const buffer = await fetchRes.arrayBuffer();
|
|
130
|
+
return buffer;
|
|
131
|
+
} catch (error) {
|
|
132
|
+
throw new TypeError(
|
|
133
|
+
`Failed to read the response body into an ArrayBuffer: ${error.message}`
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
case 'stream':
|
|
137
|
+
const value = await fetchRes.text();
|
|
138
|
+
return new ReadableStream({
|
|
139
|
+
start(controller) {
|
|
140
|
+
controller.enqueue(new TextEncoder().encode(value));
|
|
141
|
+
controller.close();
|
|
142
|
+
}
|
|
143
|
+
});
|
|
27
144
|
default:
|
|
28
|
-
throw new Error(
|
|
145
|
+
throw new Error(`Unsupported type: ${type}`);
|
|
29
146
|
}
|
|
30
147
|
}
|
|
31
148
|
|
|
32
|
-
async put(key, value) {
|
|
33
|
-
const namespaceData = this.allData[this.namespace] || {};
|
|
34
|
-
namespaceData[key] = value;
|
|
35
|
-
this.allData[this.namespace] = namespaceData;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
149
|
async delete(key) {
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
150
|
+
const fetchRes = await fetch(
|
|
151
|
+
`http://localhost:${EdgeKV.port}/mock_kv/delete?key=${key}&namespace=${this.namespace}`,
|
|
152
|
+
{
|
|
153
|
+
method: 'POST',
|
|
154
|
+
headers: { 'Content-Type': 'application/json' }
|
|
155
|
+
}
|
|
156
|
+
);
|
|
157
|
+
if (!fetchRes.ok) {
|
|
158
|
+
const error = await fetchRes.json();
|
|
159
|
+
throw new Error(error.error);
|
|
160
|
+
}
|
|
161
|
+
const res = await fetchRes.json();
|
|
162
|
+
return res.success;
|
|
42
163
|
}
|
|
43
164
|
}
|
|
44
165
|
|
|
@@ -15,6 +15,8 @@ import { getRoot } from '../../../utils/fileUtils/base.js';
|
|
|
15
15
|
import { EW2BinPath } from '../../../utils/installEw2.js';
|
|
16
16
|
import { HttpProxyAgent } from 'http-proxy-agent';
|
|
17
17
|
import chalk from 'chalk';
|
|
18
|
+
import CacheService from './cacheService.js';
|
|
19
|
+
import EdgeKV from './kvService.js';
|
|
18
20
|
import t from '../../../i18n/index.js';
|
|
19
21
|
import sleep from '../../../utils/sleep.js';
|
|
20
22
|
const getColorForStatusCode = (statusCode, message) => {
|
|
@@ -40,6 +42,8 @@ const getColorForStatusCode = (statusCode, message) => {
|
|
|
40
42
|
class Ew2Server {
|
|
41
43
|
constructor(props) {
|
|
42
44
|
this.worker = null;
|
|
45
|
+
this.cache = null;
|
|
46
|
+
this.kv = null;
|
|
43
47
|
this.startingWorker = false;
|
|
44
48
|
this.workerStartTimeout = undefined;
|
|
45
49
|
this.server = null;
|
|
@@ -57,6 +61,8 @@ class Ew2Server {
|
|
|
57
61
|
return __awaiter(this, void 0, void 0, function* () {
|
|
58
62
|
this.startingWorker = true;
|
|
59
63
|
const result = yield this.openEdgeWorker();
|
|
64
|
+
this.cache = new CacheService();
|
|
65
|
+
this.kv = new EdgeKV();
|
|
60
66
|
if (!result) {
|
|
61
67
|
throw new Error('Worker start failed');
|
|
62
68
|
}
|
|
@@ -128,37 +134,60 @@ class Ew2Server {
|
|
|
128
134
|
}
|
|
129
135
|
createServer() {
|
|
130
136
|
this.server = http.createServer((req, res) => __awaiter(this, void 0, void 0, function* () {
|
|
137
|
+
var _a, _b, _c;
|
|
138
|
+
if (req.url === '/favicon.ico') {
|
|
139
|
+
res.writeHead(204, {
|
|
140
|
+
'Content-Type': 'image/x-icon',
|
|
141
|
+
'Content-Length': 0
|
|
142
|
+
});
|
|
143
|
+
return res.end();
|
|
144
|
+
}
|
|
145
|
+
if ((_a = req.url) === null || _a === void 0 ? void 0 : _a.includes('/mock_cache')) {
|
|
146
|
+
const cacheResult = yield this.handleCache(req);
|
|
147
|
+
return res.end(JSON.stringify(cacheResult));
|
|
148
|
+
}
|
|
149
|
+
if ((_b = req.url) === null || _b === void 0 ? void 0 : _b.includes('/mock_kv')) {
|
|
150
|
+
const kvResult = yield this.handleKV(req);
|
|
151
|
+
if ((_c = req.url) === null || _c === void 0 ? void 0 : _c.includes('/get')) {
|
|
152
|
+
if (kvResult.success) {
|
|
153
|
+
return res.end(kvResult.value);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
res.setHeader('Kv-Get-Empty', 'true');
|
|
157
|
+
return res.end();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
return res.end(JSON.stringify(kvResult));
|
|
162
|
+
}
|
|
163
|
+
}
|
|
131
164
|
try {
|
|
132
165
|
const host = req.headers.host;
|
|
133
166
|
const url = req.url;
|
|
167
|
+
const method = req.method;
|
|
168
|
+
const headers = Object.entries(req.headers).reduce((acc, [key, value]) => {
|
|
169
|
+
if (Array.isArray(value)) {
|
|
170
|
+
acc[key] = value.join(', ');
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
acc[key] = value;
|
|
174
|
+
}
|
|
175
|
+
return acc;
|
|
176
|
+
}, {});
|
|
134
177
|
// @ts-ignore
|
|
135
178
|
const ew2Port = global.ew2Port;
|
|
136
179
|
// @ts-ignore
|
|
137
180
|
const localUpstream = global.localUpstream;
|
|
138
181
|
const workerRes = yield fetch(`http://${localUpstream ? localUpstream : host}${url}`, {
|
|
139
|
-
method
|
|
140
|
-
headers: {
|
|
141
|
-
|
|
142
|
-
'x-er-id': 'a.bA'
|
|
143
|
-
},
|
|
182
|
+
method,
|
|
183
|
+
headers: Object.assign(Object.assign({}, headers), { 'x-er-context': 'eyJzaXRlX2lkIjogIjYyMjcxODQ0NjgwNjA4IiwgInNpdGVfbmFtZSI6ICJjb21wdXRlbHguYWxpY2RuLXRlc3QuY29tIiwgInNpdGVfcmVjb3JkIjogIm1vY2hlbi1uY2RuLmNvbXB1dGVseC5hbGljZG4tdGVzdC5jb20iLCAiYWxpdWlkIjogIjEzMjI0OTI2ODY2NjU2MDgiLCAic2NoZW1lIjoiaHR0cCIsICAiaW1hZ2VfZW5hYmxlIjogdHJ1ZX0=', 'x-er-id': 'a.bA' }),
|
|
184
|
+
body: req.method === 'GET' ? undefined : req,
|
|
144
185
|
agent: new HttpProxyAgent(`http://127.0.0.1:${ew2Port}`)
|
|
145
186
|
});
|
|
146
187
|
const workerHeaders = Object.fromEntries(workerRes.headers.entries());
|
|
147
188
|
// 解决 gzip 兼容性问题,防止net::ERR_CONTENT_DECODING_FAILED
|
|
148
189
|
workerHeaders['content-encoding'] = 'identity';
|
|
149
190
|
if (workerRes.body) {
|
|
150
|
-
// if (workerRes.headers.get('content-type')?.includes('text/')) {
|
|
151
|
-
// const text = await workerRes.text();
|
|
152
|
-
// // 出现换行符之类会导致 content-length 不一致
|
|
153
|
-
// workerHeaders['content-length'] =
|
|
154
|
-
// Buffer.byteLength(text).toString();
|
|
155
|
-
// console.log(workerHeaders['content-length']);
|
|
156
|
-
// res.writeHead(workerRes.status, workerHeaders);
|
|
157
|
-
// res.end(text);
|
|
158
|
-
// } else {
|
|
159
|
-
// res.writeHead(workerRes.status, workerHeaders);
|
|
160
|
-
// workerRes.body.pipe(res);
|
|
161
|
-
// }
|
|
162
191
|
res.writeHead(workerRes.status, workerHeaders);
|
|
163
192
|
workerRes.body.pipe(res);
|
|
164
193
|
logger.log(`[ESA Dev] ${req.method} ${url} ${getColorForStatusCode(workerRes.status, workerRes.statusText)}`);
|
|
@@ -176,6 +205,65 @@ class Ew2Server {
|
|
|
176
205
|
logger.log(`listening on port ${this.port}`);
|
|
177
206
|
});
|
|
178
207
|
}
|
|
208
|
+
handleCache(req) {
|
|
209
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
210
|
+
var _a, _b, _c, _d, _e, _f;
|
|
211
|
+
const body = yield this.parseCacheBody(req);
|
|
212
|
+
if ((_a = req.url) === null || _a === void 0 ? void 0 : _a.includes('/put')) {
|
|
213
|
+
(_b = this.cache) === null || _b === void 0 ? void 0 : _b.put(body.key, body);
|
|
214
|
+
return { success: true };
|
|
215
|
+
}
|
|
216
|
+
if ((_c = req.url) === null || _c === void 0 ? void 0 : _c.includes('/get')) {
|
|
217
|
+
const res = (_d = this.cache) === null || _d === void 0 ? void 0 : _d.get(body.key);
|
|
218
|
+
if (!res) {
|
|
219
|
+
return { success: false, key: body.key };
|
|
220
|
+
}
|
|
221
|
+
return { success: true, key: body.key, data: res === null || res === void 0 ? void 0 : res.serializedResponse };
|
|
222
|
+
}
|
|
223
|
+
if ((_e = req.url) === null || _e === void 0 ? void 0 : _e.includes('/delete')) {
|
|
224
|
+
const res = (_f = this.cache) === null || _f === void 0 ? void 0 : _f.delete(body.key);
|
|
225
|
+
return { success: !!res };
|
|
226
|
+
}
|
|
227
|
+
return { success: false };
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
handleKV(req) {
|
|
231
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
232
|
+
var _a, _b, _c, _d, _e, _f;
|
|
233
|
+
const url = new URL(req.url, 'http://localhost');
|
|
234
|
+
const key = url.searchParams.get('key');
|
|
235
|
+
const namespace = url.searchParams.get('namespace');
|
|
236
|
+
const body = yield this.parseKVBody(req);
|
|
237
|
+
if (!key || !namespace) {
|
|
238
|
+
return {
|
|
239
|
+
success: false
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
if ((_a = req.url) === null || _a === void 0 ? void 0 : _a.includes('/put')) {
|
|
243
|
+
(_b = this.kv) === null || _b === void 0 ? void 0 : _b.put(key, body, namespace);
|
|
244
|
+
return {
|
|
245
|
+
success: true
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
if ((_c = req.url) === null || _c === void 0 ? void 0 : _c.includes('/get')) {
|
|
249
|
+
const res = (_d = this.kv) === null || _d === void 0 ? void 0 : _d.get(key, namespace);
|
|
250
|
+
const params = { success: true, value: res };
|
|
251
|
+
if (!res) {
|
|
252
|
+
params.success = false;
|
|
253
|
+
}
|
|
254
|
+
return params;
|
|
255
|
+
}
|
|
256
|
+
if ((_e = req.url) === null || _e === void 0 ? void 0 : _e.includes('/delete')) {
|
|
257
|
+
const res = (_f = this.kv) === null || _f === void 0 ? void 0 : _f.delete(key, namespace);
|
|
258
|
+
return {
|
|
259
|
+
success: res
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
return {
|
|
263
|
+
success: false
|
|
264
|
+
};
|
|
265
|
+
});
|
|
266
|
+
}
|
|
179
267
|
stdoutHandler(chunk) {
|
|
180
268
|
logger.log(`${chalk.bgGreen('[Worker]')} ${chunk.toString().trim()}`);
|
|
181
269
|
}
|
|
@@ -202,6 +290,48 @@ class Ew2Server {
|
|
|
202
290
|
this.onClose && this.onClose();
|
|
203
291
|
});
|
|
204
292
|
}
|
|
293
|
+
parseCacheBody(req) {
|
|
294
|
+
return new Promise((resolve, reject) => {
|
|
295
|
+
const chunks = [];
|
|
296
|
+
let totalLength = 0;
|
|
297
|
+
req.on('data', (chunk) => {
|
|
298
|
+
chunks.push(chunk);
|
|
299
|
+
totalLength += chunk.length;
|
|
300
|
+
});
|
|
301
|
+
req.on('end', () => {
|
|
302
|
+
try {
|
|
303
|
+
const buffer = Buffer.concat(chunks, totalLength);
|
|
304
|
+
const rawBody = buffer.toString('utf8');
|
|
305
|
+
resolve(rawBody ? JSON.parse(rawBody) : {});
|
|
306
|
+
}
|
|
307
|
+
catch (err) {
|
|
308
|
+
reject(new Error(`Invalid JSON: ${err.message}`));
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
req.on('error', reject);
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
parseKVBody(req) {
|
|
315
|
+
return new Promise((resolve, reject) => {
|
|
316
|
+
const chunks = [];
|
|
317
|
+
let totalLength = 0;
|
|
318
|
+
req.on('data', (chunk) => {
|
|
319
|
+
chunks.push(chunk);
|
|
320
|
+
totalLength += chunk.length;
|
|
321
|
+
});
|
|
322
|
+
req.on('end', () => {
|
|
323
|
+
try {
|
|
324
|
+
const buffer = Buffer.concat(chunks, totalLength);
|
|
325
|
+
const rawBody = buffer.toString();
|
|
326
|
+
resolve(rawBody);
|
|
327
|
+
}
|
|
328
|
+
catch (err) {
|
|
329
|
+
reject(new Error(`Invalid JSON: ${err.message}`));
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
req.on('error', reject);
|
|
333
|
+
});
|
|
334
|
+
}
|
|
205
335
|
runCommand(command) {
|
|
206
336
|
var _a, _b;
|
|
207
337
|
(_b = (_a = this.worker) === null || _a === void 0 ? void 0 : _a.stdin) === null || _b === void 0 ? void 0 : _b.write(command);
|
|
@@ -222,10 +352,12 @@ class Ew2Server {
|
|
|
222
352
|
(_a = this.server) === null || _a === void 0 ? void 0 : _a.close();
|
|
223
353
|
});
|
|
224
354
|
}
|
|
225
|
-
restart() {
|
|
355
|
+
restart(devPack) {
|
|
226
356
|
return __awaiter(this, void 0, void 0, function* () {
|
|
227
357
|
this.restarting = true;
|
|
358
|
+
console.clear();
|
|
228
359
|
yield this.stop();
|
|
360
|
+
yield devPack();
|
|
229
361
|
this.start();
|
|
230
362
|
logger.log(t('dev_server_restart').d('Worker server restarted'));
|
|
231
363
|
logger.info('Worker server restarted');
|
|
@@ -203,7 +203,7 @@ const dev = {
|
|
|
203
203
|
userFileRepacking = false;
|
|
204
204
|
return;
|
|
205
205
|
}
|
|
206
|
-
worker.restart();
|
|
206
|
+
worker.restart(devPack);
|
|
207
207
|
return;
|
|
208
208
|
}
|
|
209
209
|
logger.info('Dev repack');
|
|
@@ -215,8 +215,7 @@ const dev = {
|
|
|
215
215
|
catch (err) { }
|
|
216
216
|
}
|
|
217
217
|
userFileRepacking = true;
|
|
218
|
-
yield devPack
|
|
219
|
-
yield worker.restart();
|
|
218
|
+
yield worker.restart(devPack);
|
|
220
219
|
}), 500));
|
|
221
220
|
var { devElement, exit } = doProcess(worker);
|
|
222
221
|
const { waitUntilExit } = devElement;
|
|
@@ -14,7 +14,7 @@ import logger from '../../libs/logger.js';
|
|
|
14
14
|
import { validRoutine } from '../../utils/checkIsRoutineCreated.js';
|
|
15
15
|
const addDomain = {
|
|
16
16
|
command: 'add <domain>',
|
|
17
|
-
describe:
|
|
17
|
+
describe: `🔗 ${t('domain_add_describe').d('Bind a domain to a routine')}`,
|
|
18
18
|
builder: (yargs) => {
|
|
19
19
|
return yargs
|
|
20
20
|
.positional('domain', {
|
|
@@ -31,7 +31,7 @@ const deleteDomain = {
|
|
|
31
31
|
export default deleteDomain;
|
|
32
32
|
export function handleDeleteDomain(argv) {
|
|
33
33
|
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
-
var _a
|
|
34
|
+
var _a;
|
|
35
35
|
if (!checkDirectory()) {
|
|
36
36
|
return;
|
|
37
37
|
}
|
|
@@ -44,10 +44,10 @@ export function handleDeleteDomain(argv) {
|
|
|
44
44
|
yield validRoutine(projectConfig.name);
|
|
45
45
|
const server = yield ApiService.getInstance();
|
|
46
46
|
const req = { Name: projectConfig.name || '' };
|
|
47
|
-
const
|
|
48
|
-
if (!
|
|
47
|
+
const listRoutineRelatedRecordRes = yield server.listRoutineRelatedRecords(req);
|
|
48
|
+
if (!listRoutineRelatedRecordRes)
|
|
49
49
|
return;
|
|
50
|
-
const relatedRecords = (
|
|
50
|
+
const relatedRecords = ((_a = listRoutineRelatedRecordRes.data) === null || _a === void 0 ? void 0 : _a.RelatedRecords) || [];
|
|
51
51
|
const relatedDomain = argv.domain;
|
|
52
52
|
const matchedSite = relatedRecords.find((item) => {
|
|
53
53
|
return String(item.RecordName) === relatedDomain;
|
|
@@ -5,7 +5,7 @@ import t from '../../i18n/index.js';
|
|
|
5
5
|
let yargsIns;
|
|
6
6
|
const domainCommand = {
|
|
7
7
|
command: 'domain [script]',
|
|
8
|
-
describe:
|
|
8
|
+
describe: `🔗 ${t('domain_describe').d('Manage the domain names bound to your routine')}`,
|
|
9
9
|
builder: (yargs) => {
|
|
10
10
|
yargsIns = yargs;
|
|
11
11
|
return yargs
|
|
@@ -36,10 +36,10 @@ export function handleListDomains(argv) {
|
|
|
36
36
|
yield validRoutine(projectConfig.name);
|
|
37
37
|
const server = yield ApiService.getInstance();
|
|
38
38
|
const req = { Name: projectConfig.name };
|
|
39
|
-
const
|
|
40
|
-
if (!
|
|
39
|
+
const res = yield server.listRoutineRelatedRecords(req);
|
|
40
|
+
if (!res)
|
|
41
41
|
return;
|
|
42
|
-
const relatedRecords = (_b = (_a =
|
|
42
|
+
const relatedRecords = (_b = (_a = res.data) === null || _a === void 0 ? void 0 : _a.RelatedRecords) !== null && _b !== void 0 ? _b : [];
|
|
43
43
|
if (relatedRecords.length === 0) {
|
|
44
44
|
logger.log(`🙅 ${t('domain_list_empty').d('No related domains found')}`);
|
|
45
45
|
return;
|