Skip to content

Commit 014d0e5

Browse files
committed
refactor: use XMLHttpRequest for blocking request and remove async functions
1 parent 6175b30 commit 014d0e5

File tree

5 files changed

+41
-87
lines changed

5 files changed

+41
-87
lines changed

package-lock.json

Lines changed: 10 additions & 59 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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,12 @@
6767
"prettier": "^1.19.1",
6868
"raw-loader": "^4.0.0",
6969
"rimraf": "^3.0.0",
70-
"synchronized-promise": "^0.2.0",
7170
"version-bump-prompt": "^5.0.6",
7271
"webpack": "^4.41.4",
7372
"webpack-cli": "^3.3.10",
7473
"webpack-shell-plugin": "^0.5.0"
7574
},
7675
"dependencies": {
77-
"axios": "^0.19.0"
76+
"xmlhttprequest": "^1.8.0"
7877
}
7978
}

src/macro.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
async function extractMacros(txt, options = {}) {
1+
function extractMacros(txt, options = {}) {
22
const { lib, reader, lang, importPaths, requestOptions } = options;
33

44
function getImports() {
@@ -108,9 +108,9 @@ async function extractMacros(txt, options = {}) {
108108
} else if (imports[i] in lib) {
109109
isrc = lib[imports[i]];
110110
} else {
111-
isrc = await reader(imports[i], importPaths, requestOptions);
111+
isrc = reader(imports[i], importPaths, requestOptions);
112112
}
113-
macros = macros.concat(await extractMacros(isrc, options));
113+
macros = macros.concat(extractMacros(isrc, options));
114114
}
115115
return macros;
116116
}

src/parser.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var compilers = require("./compiler/compilers");
77
var { typecheck, printSignature } = require("./typecheck");
88
var { expandMacros, extractMacros } = require("./macro.js");
99
var { defaultImportReader } = require("./reader");
10-
const makeSynchronize = require("synchronized-promise");
1110

1211
const defaultTrustedHosts = [
1312
"https://raw.githubusercontent.com/LingDong-/wenyan-lang/master"
@@ -636,7 +635,7 @@ function pyWrapModule(name, src) {
636635
return `#/*___wenyan_module_${name}_start___*/\n${src}\n#/*___wenyan_module_${name}_end___*/\n`;
637636
}
638637

639-
async function compileAsync(arg1, arg2, arg3) {
638+
function compile(arg1, arg2, arg3) {
640639
let options = {};
641640
let txt = "";
642641

@@ -701,7 +700,7 @@ async function compileAsync(arg1, arg2, arg3) {
701700
return 0;
702701
}
703702

704-
var macros = await extractMacros(txt, {
703+
var macros = extractMacros(txt, {
705704
lib,
706705
reader,
707706
lang,
@@ -742,7 +741,7 @@ async function compileAsync(arg1, arg2, arg3) {
742741
}
743742
var klass = compilers[lang];
744743
var compiler = new klass(asc);
745-
var result = await compiler.compile({ imports });
744+
var result = compiler.compile({ imports });
746745
var { imports, result } = result;
747746
var targ = result;
748747
logCallback(targ);
@@ -755,12 +754,12 @@ async function compileAsync(arg1, arg2, arg3) {
755754
} else if (imports[i] in lib) {
756755
isrc = lib[imports[i]];
757756
} else {
758-
isrc = await reader(imports[i], importPaths);
757+
isrc = reader(imports[i], importPaths);
759758
}
760759
targ =
761760
mwrapper(
762761
imports[i],
763-
await compile(isrc, {
762+
compile(isrc, {
764763
...options,
765764
resetVarCnt: false,
766765
strict: false
@@ -771,8 +770,6 @@ async function compileAsync(arg1, arg2, arg3) {
771770
return targ;
772771
}
773772

774-
const compile = makeSynchronize(compileAsync, { tick: 10 });
775-
776773
function isLangSupportedForEval(lang) {
777774
if (lang !== "js")
778775
throw new Error(
@@ -828,21 +825,17 @@ function evalCompiled(compiledCode, options = {}) {
828825
})();
829826
}
830827

831-
async function executeAsync(source, options = {}) {
828+
function execute(source, options = {}) {
832829
const { lang = "js" } = options;
833830
isLangSupportedForEval(lang);
834-
const compiled = compileAsync(source, options);
831+
const compiled = compile(source, options);
835832
evalCompiled(compiled, options);
836833
}
837834

838-
const execute = makeSynchronize(executeAsync, { tick: 10 });
839-
840835
var parser = {
841836
compile,
842-
compileAsync,
843837
evalCompiled,
844838
execute,
845-
executeAsync,
846839
version,
847840
wy2tokens,
848841
tokens2asc,

src/reader.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const axios = require("axios");
2-
31
function isHostTrusted(url, trustedHosts) {
42
for (const host of trustedHosts) {
53
// FIXME: it can be bypassed by relative path resolving,
@@ -13,7 +11,24 @@ function isHttpURL(uri) {
1311
return !!uri.match(/^https?\:\/\//);
1412
}
1513

16-
async function defaultImportReader(
14+
function fetchTextSync(url, timeout) {
15+
let XHR;
16+
if (typeof window !== "undefined" && "XMLHttpRequest" in window)
17+
XHR = window.XMLHttpRequest;
18+
else XHR = eval("require")("xmlhttprequest").XMLHttpRequest;
19+
20+
var xmlHttp = new XHR();
21+
xmlHttp.timeout = timeout;
22+
xmlHttp.open("GET", url, false); // false for synchronous request
23+
xmlHttp.send(null);
24+
25+
if (xmlHttp.status >= 200 && xmlHttp.status < 300)
26+
return xmlHttp.responseText;
27+
28+
throw new URIError(xmlHttp.responseText);
29+
}
30+
31+
function defaultImportReader(
1732
moduleName,
1833
importPaths = [],
1934
requestOptions = {}
@@ -37,15 +52,11 @@ async function defaultImportReader(
3752
}
3853

3954
try {
40-
const res = await axios(uri, {
41-
responseType: "text",
42-
timeout: requestTimeout
43-
});
44-
return res.data;
55+
return fetchTextSync(uri, requestTimeout);
4556
} catch (e) {}
4657
} else {
4758
try {
48-
return await eval("require")("fs").readFileSync(uri, "utf-8");
59+
return eval("require")("fs").readFileSync(uri, "utf-8");
4960
} catch (e) {}
5061
}
5162
}

0 commit comments

Comments
 (0)