Skip to content

Commit cd0a78d

Browse files
committed
add Node.js fallback for file util
Check whether XMLHttpRequest is defined and if not use the Node.js built-in fetch method.
1 parent f114b2c commit cd0a78d

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

apps/playground/src/pages/apis/txbuilder/governance/registration.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function Left() {
3434

3535
let codeAnchor = ``;
3636
codeAnchor += `async function getMeshJsonHash(url: string) {\n`;
37-
codeAnchor += ` var drepAnchor = getFile(url);\n`;
37+
codeAnchor += ` var drepAnchor = await getFile(url);\n`;
3838
codeAnchor += ` const anchorObj = JSON.parse(drepAnchor);\n`;
3939
codeAnchor += ` return hashDrepAnchor(anchorObj);\n`;
4040
codeAnchor += `}\n`;
@@ -129,7 +129,7 @@ function Right() {
129129
const [anchorUrl, setAnchorUrl] = useState<string>("");
130130

131131
async function getMeshJsonHash(url: string) {
132-
var drepAnchor = getFile(url);
132+
var drepAnchor = await getFile(url);
133133
const anchorObj = JSON.parse(drepAnchor);
134134
const anchorHash = hashDrepAnchor(anchorObj);
135135
return anchorHash;

apps/playground/src/pages/apis/txbuilder/governance/update.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function Right() {
6565
const [anchorUrl, setAnchorUrl] = useState<string>("");
6666

6767
async function getMeshJsonHash(url: string) {
68-
var drepAnchor = getFile(url);
68+
var drepAnchor = await getFile(url);
6969
const anchorObj = JSON.parse(drepAnchor);
7070
const anchorHash = hashDrepAnchor(anchorObj);
7171
return anchorHash;
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
export function getFile(url: string) {
2-
var Httpreq = new XMLHttpRequest();
3-
Httpreq.open("GET", url, false);
4-
Httpreq.send(null);
5-
return Httpreq.responseText;
1+
export async function getFile(url: string) {
2+
if (typeof XMLHttpRequest !== "undefined") {
3+
var Httpreq = new XMLHttpRequest();
4+
Httpreq.open("GET", url, false);
5+
Httpreq.send(null);
6+
return Httpreq.responseText;
7+
} else {
8+
const res = await fetch(url);
9+
return res.text();
10+
}
611
}

0 commit comments

Comments
 (0)