From cd0a78d44109b3378beb6037b9dd888bce05e878 Mon Sep 17 00:00:00 2001 From: Piotr Napierala Date: Wed, 1 Oct 2025 10:57:42 +0200 Subject: [PATCH] add Node.js fallback for file util Check whether XMLHttpRequest is defined and if not use the Node.js built-in fetch method. --- .../apis/txbuilder/governance/registration.tsx | 4 ++-- .../pages/apis/txbuilder/governance/update.tsx | 2 +- packages/mesh-common/src/utils/file.ts | 15 ++++++++++----- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/apps/playground/src/pages/apis/txbuilder/governance/registration.tsx b/apps/playground/src/pages/apis/txbuilder/governance/registration.tsx index d8587ef10..757d6787b 100644 --- a/apps/playground/src/pages/apis/txbuilder/governance/registration.tsx +++ b/apps/playground/src/pages/apis/txbuilder/governance/registration.tsx @@ -34,7 +34,7 @@ function Left() { let codeAnchor = ``; codeAnchor += `async function getMeshJsonHash(url: string) {\n`; - codeAnchor += ` var drepAnchor = getFile(url);\n`; + codeAnchor += ` var drepAnchor = await getFile(url);\n`; codeAnchor += ` const anchorObj = JSON.parse(drepAnchor);\n`; codeAnchor += ` return hashDrepAnchor(anchorObj);\n`; codeAnchor += `}\n`; @@ -129,7 +129,7 @@ function Right() { const [anchorUrl, setAnchorUrl] = useState(""); async function getMeshJsonHash(url: string) { - var drepAnchor = getFile(url); + var drepAnchor = await getFile(url); const anchorObj = JSON.parse(drepAnchor); const anchorHash = hashDrepAnchor(anchorObj); return anchorHash; diff --git a/apps/playground/src/pages/apis/txbuilder/governance/update.tsx b/apps/playground/src/pages/apis/txbuilder/governance/update.tsx index 70e86c8a8..fc1dc564e 100644 --- a/apps/playground/src/pages/apis/txbuilder/governance/update.tsx +++ b/apps/playground/src/pages/apis/txbuilder/governance/update.tsx @@ -65,7 +65,7 @@ function Right() { const [anchorUrl, setAnchorUrl] = useState(""); async function getMeshJsonHash(url: string) { - var drepAnchor = getFile(url); + var drepAnchor = await getFile(url); const anchorObj = JSON.parse(drepAnchor); const anchorHash = hashDrepAnchor(anchorObj); return anchorHash; diff --git a/packages/mesh-common/src/utils/file.ts b/packages/mesh-common/src/utils/file.ts index cc54345d7..052e22692 100644 --- a/packages/mesh-common/src/utils/file.ts +++ b/packages/mesh-common/src/utils/file.ts @@ -1,6 +1,11 @@ -export function getFile(url: string) { - var Httpreq = new XMLHttpRequest(); - Httpreq.open("GET", url, false); - Httpreq.send(null); - return Httpreq.responseText; +export async function getFile(url: string) { + if (typeof XMLHttpRequest !== "undefined") { + var Httpreq = new XMLHttpRequest(); + Httpreq.open("GET", url, false); + Httpreq.send(null); + return Httpreq.responseText; + } else { + const res = await fetch(url); + return res.text(); + } }