Skip to content

Commit 0f224fb

Browse files
committed
chore: use axios instead of fetch
1 parent 89ca1a0 commit 0f224fb

File tree

3 files changed

+65
-19
lines changed

3 files changed

+65
-19
lines changed

package-lock.json

Lines changed: 33 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@
7373
"webpack-shell-plugin": "^0.5.0"
7474
},
7575
"dependencies": {
76-
"node-fetch": "^2.6.0"
76+
"axios": "^0.19.0"
7777
}
7878
}

src/reader.js

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
1-
function getFetch() {
2-
if (typeof window != "undefined" && window.fetch) return window.fetch;
3-
else return eval("require")("node-fetch");
1+
function isHostTrusted(url, trustedHosts) {
2+
// TODO:
3+
return false;
4+
}
5+
function isHttpURL(uri) {
6+
return !!uri.match(/^https?\:\/\//);
47
}
58

6-
async function defaultImportReader(moduleName, importPaths = []) {
7-
const fetch = getFetch();
9+
async function defaultImportReader(
10+
moduleName,
11+
importPaths = [],
12+
requestOptions = {}
13+
) {
14+
const {
15+
allowHttp = false,
16+
trustedHosts = [],
17+
requestTimeout = 2000
18+
} = requestOptions;
819

920
if (typeof importPaths === "string") importPaths = [importPaths];
1021

1122
for (dir of importPaths) {
12-
const filepath = dir + "/" + moduleName + ".wy";
13-
if (filepath.match(/^\w+\:\/\//)) {
23+
const uri = dir + "/" + moduleName + ".wy";
24+
if (isHttpURL(uri)) {
25+
if (!allowHttp && !isHostTrusted(uri, trustedHosts)) {
26+
throw new Error(
27+
`URL request "${uri}" is blocked by default for security purpose.` +
28+
`You can turn it on by specify the "allowHttp" option.`
29+
);
30+
}
31+
1432
try {
15-
const data = await fetch(filepath);
16-
const text = await data.text();
17-
return text;
33+
const res = await axios(uri, {
34+
responseType: "text",
35+
timeout: requestTimeout
36+
});
37+
return res.data;
1838
} catch (e) {}
1939
} else {
2040
try {
21-
return eval("require")("fs").readFileSync(filepath, "utf-8");
41+
return await eval("require")("fs").promises.readFile(uri, "utf-8");
2242
} catch (e) {}
2343
}
2444
}

0 commit comments

Comments
 (0)