esa-cli 0.0.2-beta.2 → 0.0.2-beta.20
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 +48 -115
- package/dist/commands/commit/prodBuild.js +2 -3
- package/dist/commands/common/constant.js +0 -19
- package/dist/commands/common/utils.js +416 -0
- package/dist/commands/config.js +1 -1
- package/dist/commands/deploy/helper.js +51 -72
- package/dist/commands/deploy/index.js +50 -188
- package/dist/commands/deployments/delete.js +32 -22
- package/dist/commands/deployments/index.js +2 -2
- package/dist/commands/deployments/list.js +22 -38
- package/dist/commands/dev/build.js +3 -3
- package/dist/commands/dev/doProcess.js +5 -5
- 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 +39 -43
- 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 +165 -29
- package/dist/commands/dev/index.js +17 -17
- package/dist/commands/dev/mockWorker/devPack.js +35 -24
- package/dist/commands/dev/mockWorker/server.js +7 -6
- package/dist/commands/domain/add.js +2 -2
- package/dist/commands/domain/delete.js +7 -7
- package/dist/commands/domain/index.js +2 -2
- package/dist/commands/domain/list.js +10 -10
- package/dist/commands/init/helper.js +759 -0
- package/dist/commands/init/index.js +88 -220
- package/dist/commands/init/snippets/nextjs/next.config.mjs +6 -0
- package/dist/commands/init/snippets/nextjs/next.config.ts +7 -0
- package/dist/commands/init/snippets/react-router/react-router.config.ts +7 -0
- package/dist/commands/init/template.jsonc +84 -0
- package/dist/commands/init/types.js +1 -0
- package/dist/commands/lang.js +2 -2
- package/dist/commands/login/index.js +74 -34
- package/dist/commands/logout.js +5 -5
- package/dist/commands/route/add.js +105 -49
- package/dist/commands/route/delete.js +33 -27
- package/dist/commands/route/helper.js +123 -0
- package/dist/commands/route/index.js +2 -2
- package/dist/commands/route/list.js +56 -17
- package/dist/commands/routine/delete.js +2 -2
- package/dist/commands/routine/index.js +2 -2
- package/dist/commands/routine/list.js +43 -37
- package/dist/commands/site/index.js +1 -1
- package/dist/commands/site/list.js +6 -7
- package/dist/commands/utils.js +59 -23
- package/dist/components/descriptionInput.js +1 -1
- package/dist/components/filterSelector.js +1 -1
- package/dist/components/mutiLevelSelect.js +43 -55
- package/dist/components/mutiSelectTable.js +1 -1
- package/dist/components/routeBuilder.js +68 -0
- package/dist/components/selectInput.js +2 -3
- package/dist/components/selectItem.js +1 -1
- package/dist/docs/Commands_en.md +142 -131
- package/dist/docs/Commands_zh_CN.md +139 -127
- package/dist/i18n/index.js +2 -2
- package/dist/i18n/locales.json +435 -23
- package/dist/index.js +27 -20
- package/dist/libs/api.js +32 -9
- package/dist/libs/apiService.js +267 -88
- package/dist/libs/git/index.js +86 -9
- package/dist/libs/interface.js +0 -1
- package/dist/libs/logger.js +162 -10
- package/dist/libs/service.js +2 -2
- package/dist/libs/templates/index.js +3 -2
- package/dist/utils/checkAssetsExist.js +80 -0
- package/dist/utils/checkDevPort.js +3 -17
- package/dist/utils/checkEntryFileExist.js +10 -0
- package/dist/utils/checkIsRoutineCreated.js +28 -27
- package/dist/utils/checkVersion.js +119 -1
- package/dist/utils/command.js +149 -0
- package/dist/utils/compress.js +136 -0
- package/dist/utils/download.js +182 -0
- package/dist/utils/fileMd5.js +1 -1
- package/dist/utils/fileUtils/base.js +1 -1
- package/dist/utils/fileUtils/index.js +136 -44
- package/dist/utils/installDeno.js +8 -8
- package/dist/utils/installEw2.js +7 -7
- package/dist/utils/openInBrowser.js +1 -1
- package/dist/utils/prompt.js +97 -0
- package/package.json +19 -12
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
class CacheService {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.store = new Map();
|
|
4
|
+
this.maxQuota = 100 * 1024 * 1024;
|
|
5
|
+
}
|
|
6
|
+
put(key, serializedResponse) {
|
|
7
|
+
const expires = serializedResponse.ttl === 0
|
|
8
|
+
? Infinity
|
|
9
|
+
: Date.now() + serializedResponse.ttl * 1000;
|
|
10
|
+
this.store.set(key, {
|
|
11
|
+
serializedResponse,
|
|
12
|
+
expires: expires,
|
|
13
|
+
lastUsed: Date.now()
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
get(key) {
|
|
17
|
+
const entry = this.store.get(key);
|
|
18
|
+
if (!entry || Date.now() > entry.expires) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
entry.lastUsed = Date.now();
|
|
22
|
+
return entry;
|
|
23
|
+
}
|
|
24
|
+
delete(key) {
|
|
25
|
+
return this.deleteEntry(key);
|
|
26
|
+
}
|
|
27
|
+
deleteEntry(key) {
|
|
28
|
+
if (!this.store.has(key))
|
|
29
|
+
return false;
|
|
30
|
+
return this.store.delete(key);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export default CacheService;
|
|
@@ -2,8 +2,9 @@ import worker from '$userPath';
|
|
|
2
2
|
import Cache from './mock/cache.js';
|
|
3
3
|
import mockKV from './mock/kv.js';
|
|
4
4
|
|
|
5
|
-
var mock_cache =
|
|
5
|
+
var mock_cache = new Cache($userPort);
|
|
6
6
|
globalThis.mockCache = mock_cache;
|
|
7
|
+
mockKV.port = $userPort;
|
|
7
8
|
globalThis.mockKV = mockKV;
|
|
8
9
|
|
|
9
10
|
export default worker;
|
|
@@ -7,16 +7,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import path from 'path';
|
|
11
10
|
import fs from 'fs';
|
|
12
|
-
import
|
|
13
|
-
import devBuild from '../build.js';
|
|
11
|
+
import path from 'path';
|
|
14
12
|
import t from '../../../i18n/index.js';
|
|
15
|
-
import
|
|
16
|
-
import { getRoot, getDirName } from '../../../utils/fileUtils/base.js';
|
|
13
|
+
import logger from '../../../libs/logger.js';
|
|
17
14
|
import { checkPort } from '../../../utils/checkDevPort.js';
|
|
15
|
+
import { getRoot, getDirName } from '../../../utils/fileUtils/base.js';
|
|
16
|
+
import { getDevConf } from '../../../utils/fileUtils/index.js';
|
|
18
17
|
import { EW2Path } from '../../../utils/installEw2.js';
|
|
19
|
-
|
|
18
|
+
import devBuild from '../build.js';
|
|
19
|
+
// Generate available Ew2 port
|
|
20
20
|
const generateEw2Port = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
21
|
let ew2port = 3322;
|
|
22
22
|
let portAvailable = yield checkPort(ew2port);
|
|
@@ -60,8 +60,8 @@ conf_path = "${erConfPath}"
|
|
|
60
60
|
fs.promises.writeFile(erConfPath, erConf)
|
|
61
61
|
]);
|
|
62
62
|
};
|
|
63
|
-
//
|
|
64
|
-
const generateEntry = (id, projectEntry, userRoot) => __awaiter(void 0, void 0, void 0, function* () {
|
|
63
|
+
// Generate entry file
|
|
64
|
+
const generateEntry = (id, projectEntry, userRoot, port) => __awaiter(void 0, void 0, void 0, function* () {
|
|
65
65
|
const __dirname = getDirName(import.meta.url);
|
|
66
66
|
const devDir = path.resolve(userRoot, '.dev');
|
|
67
67
|
const devEntry = path.resolve(devDir, `devEntry-${id}.js`);
|
|
@@ -81,22 +81,24 @@ const generateEntry = (id, projectEntry, userRoot) => __awaiter(void 0, void 0,
|
|
|
81
81
|
const destPath = path.resolve(mockDevDir, file);
|
|
82
82
|
yield fs.promises.copyFile(srcPath, destPath);
|
|
83
83
|
}
|
|
84
|
-
return fs.promises.writeFile(devEntry, devEntryTempFile
|
|
84
|
+
return fs.promises.writeFile(devEntry, devEntryTempFile
|
|
85
|
+
.replace(/'\$userPath'/g, `'${projectEntry.replace(/\\/g, '/')}'`)
|
|
86
|
+
.replace(/\$userPort/g, `${port}`));
|
|
85
87
|
});
|
|
86
|
-
//
|
|
88
|
+
// Preliminary preparation
|
|
87
89
|
const prepare = (configPath, entry, port, localUpstream, userRoot) => __awaiter(void 0, void 0, void 0, function* () {
|
|
88
90
|
const options = {};
|
|
89
91
|
const currentOptions = { entry, port, localUpstream };
|
|
90
|
-
//
|
|
92
|
+
// Support running multiple workers simultaneously
|
|
91
93
|
const id = new Date().getTime().toString();
|
|
92
94
|
// @ts-ignore
|
|
93
95
|
global.id = id;
|
|
94
|
-
//
|
|
95
|
-
yield generateEntry(id, entry, userRoot);
|
|
96
|
-
//
|
|
96
|
+
// Generate entry file
|
|
97
|
+
yield generateEntry(id, entry, userRoot, port);
|
|
98
|
+
// Generate Ew2 configuration
|
|
97
99
|
const ew2port = yield generateEw2Port();
|
|
98
100
|
yield writeEw2config(id, ew2port, userRoot);
|
|
99
|
-
//
|
|
101
|
+
// Configuration items for each dev session, distinguished by id in one file
|
|
100
102
|
if (fs.existsSync(configPath)) {
|
|
101
103
|
const currentConfig = fs
|
|
102
104
|
.readFileSync(configPath, 'utf-8')
|
|
@@ -104,29 +106,15 @@ const prepare = (configPath, entry, port, localUpstream, userRoot) => __awaiter(
|
|
|
104
106
|
const currentConfigObj = JSON.parse(currentConfig);
|
|
105
107
|
const currentIds = Object.keys(currentConfigObj);
|
|
106
108
|
if (currentIds[0] && /^\d+$/.test(currentIds[0])) {
|
|
107
|
-
//
|
|
109
|
+
// Remove unused entries
|
|
108
110
|
for (let currentId of currentIds) {
|
|
109
|
-
const
|
|
110
|
-
if (
|
|
111
|
-
const
|
|
112
|
-
const
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
fs.rmSync(
|
|
116
|
-
force: true,
|
|
117
|
-
recursive: true,
|
|
118
|
-
maxRetries: 5
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
if (fs.existsSync(unusedTemp)) {
|
|
122
|
-
fs.rmSync(unusedTemp, {
|
|
123
|
-
force: true,
|
|
124
|
-
recursive: true,
|
|
125
|
-
maxRetries: 5
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
if (fs.existsSync(unusedConfig)) {
|
|
129
|
-
fs.rmSync(unusedConfig, {
|
|
111
|
+
const unused = yield checkPort(currentConfigObj[currentId].port);
|
|
112
|
+
if (unused) {
|
|
113
|
+
const devDir = path.resolve(userRoot, '.dev');
|
|
114
|
+
const files = fs.readdirSync(devDir);
|
|
115
|
+
const filesToDelete = files.filter((file) => file.includes(currentId));
|
|
116
|
+
for (const file of filesToDelete) {
|
|
117
|
+
fs.rmSync(path.resolve(devDir, file), {
|
|
130
118
|
force: true,
|
|
131
119
|
recursive: true,
|
|
132
120
|
maxRetries: 5
|
|
@@ -141,13 +129,22 @@ const prepare = (configPath, entry, port, localUpstream, userRoot) => __awaiter(
|
|
|
141
129
|
}
|
|
142
130
|
return fs.promises.writeFile(configPath, `export default ${JSON.stringify(Object.assign(options, { [id]: currentOptions }))}`);
|
|
143
131
|
});
|
|
144
|
-
const devPack = (
|
|
132
|
+
const devPack = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
145
133
|
logger.ora.start('Processing...\n');
|
|
146
134
|
const userRoot = getRoot();
|
|
147
|
-
|
|
135
|
+
// Try to find config file in order of preference: .jsonc, .toml
|
|
136
|
+
const configFormats = ['esa.jsonc', 'esa.toml'];
|
|
137
|
+
let configPath = null;
|
|
138
|
+
for (const format of configFormats) {
|
|
139
|
+
const testPath = path.resolve(userRoot, format);
|
|
140
|
+
if (fs.existsSync(testPath)) {
|
|
141
|
+
configPath = testPath;
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
148
145
|
let port, minify, localUpstream, entry;
|
|
149
146
|
let projectEntry = path.resolve(userRoot, 'src/index.js');
|
|
150
|
-
if (
|
|
147
|
+
if (configPath) {
|
|
151
148
|
port = getDevConf('port', 'dev', 18080);
|
|
152
149
|
minify = getDevConf('minify', 'dev', false);
|
|
153
150
|
localUpstream = getDevConf('localUpstream', 'dev', '');
|
|
@@ -158,14 +155,13 @@ const devPack = (...args_1) => __awaiter(void 0, [...args_1], void 0, function*
|
|
|
158
155
|
}
|
|
159
156
|
else {
|
|
160
157
|
logger.notInProject();
|
|
161
|
-
process.exit(
|
|
158
|
+
process.exit(1);
|
|
162
159
|
}
|
|
163
160
|
return prepare(path.resolve(userRoot, '.dev/devConfig.js'), projectEntry, port, localUpstream, userRoot)
|
|
164
161
|
.then(() => {
|
|
165
162
|
logger.ora.succeed(t('dev_pack_config_success').d('Config created successfully'));
|
|
166
163
|
return devBuild({
|
|
167
|
-
minify
|
|
168
|
-
isNode: true
|
|
164
|
+
minify
|
|
169
165
|
});
|
|
170
166
|
})
|
|
171
167
|
.then(() => {
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
class EdgeKV {
|
|
2
|
+
constructor() { }
|
|
3
|
+
get(key, namespace) {
|
|
4
|
+
const store = EdgeKV.store.get(namespace);
|
|
5
|
+
if (!store || !store.has(key)) {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
return store.get(key);
|
|
9
|
+
}
|
|
10
|
+
put(key, value, namespace) {
|
|
11
|
+
let store = EdgeKV.store.get(namespace);
|
|
12
|
+
if (!store) {
|
|
13
|
+
EdgeKV.store.set(namespace, new Map([[key, value]]));
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
store.set(key, value);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
delete(key, namespace) {
|
|
20
|
+
const store = EdgeKV.store.get(namespace);
|
|
21
|
+
if (!store)
|
|
22
|
+
return false;
|
|
23
|
+
return store.delete(key);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
EdgeKV.store = new Map();
|
|
27
|
+
export default EdgeKV;
|
|
@@ -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
|
+
// Check if key exists
|
|
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 stream = fetchRes.body;
|
|
138
|
+
return new ReadableStream({
|
|
139
|
+
start(controller) {
|
|
140
|
+
controller.enqueue(new TextEncoder().encode('hello world'));
|
|
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
|
|