From 477b93638c035ae9772376736726dbdc51845f51 Mon Sep 17 00:00:00 2001 From: David Skoland Date: Wed, 29 Nov 2023 23:10:57 +0100 Subject: Rename fileExists.js to fileExists.ts --- webAO/utils/fileExists.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 webAO/utils/fileExists.ts (limited to 'webAO/utils/fileExists.ts') diff --git a/webAO/utils/fileExists.ts b/webAO/utils/fileExists.ts new file mode 100644 index 0000000..5b60976 --- /dev/null +++ b/webAO/utils/fileExists.ts @@ -0,0 +1,18 @@ +const fileExists = async (url) => new Promise((resolve) => { + const xhr = new XMLHttpRequest(); + xhr.open('HEAD', url); + xhr.onload = function checkLoad() { + if (xhr.readyState === 4) { + if (xhr.status === 200) { + resolve(true); + } else { + resolve(false); + } + } + }; + xhr.onerror = function checkError() { + resolve(false); + }; + xhr.send(null); +}); +export default fileExists; -- cgit From 7661b6c7314327b6b8523976ba89755db4d281b9 Mon Sep 17 00:00:00 2001 From: David Skoland Date: Wed, 29 Nov 2023 23:14:35 +0100 Subject: Simplify and make fileExists proper typescript --- webAO/utils/fileExists.ts | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) (limited to 'webAO/utils/fileExists.ts') diff --git a/webAO/utils/fileExists.ts b/webAO/utils/fileExists.ts index 5b60976..06621e4 100644 --- a/webAO/utils/fileExists.ts +++ b/webAO/utils/fileExists.ts @@ -1,18 +1,7 @@ -const fileExists = async (url) => new Promise((resolve) => { - const xhr = new XMLHttpRequest(); - xhr.open('HEAD', url); - xhr.onload = function checkLoad() { - if (xhr.readyState === 4) { - if (xhr.status === 200) { - resolve(true); - } else { - resolve(false); - } - } - }; - xhr.onerror = function checkError() { - resolve(false); - }; - xhr.send(null); -}); -export default fileExists; +export default async function fileExists(url: string): Promise { + return fetch(url, { + method: 'HEAD', + }).then((response) => { + return response.ok; + }); +} -- cgit From 4766a801382e6373b982a2731cdc7ea24f0c70b3 Mon Sep 17 00:00:00 2001 From: David Skoland Date: Wed, 29 Nov 2023 23:54:23 +0100 Subject: Catch errors in fileExists --- webAO/utils/fileExists.ts | 2 ++ 1 file changed, 2 insertions(+) (limited to 'webAO/utils/fileExists.ts') diff --git a/webAO/utils/fileExists.ts b/webAO/utils/fileExists.ts index 06621e4..f1a9924 100644 --- a/webAO/utils/fileExists.ts +++ b/webAO/utils/fileExists.ts @@ -3,5 +3,7 @@ export default async function fileExists(url: string): Promise { method: 'HEAD', }).then((response) => { return response.ok; + }).catch(() => { + return false; }); } -- cgit From 90ed4b74cac7e8e410175cc3445d7140671e221f Mon Sep 17 00:00:00 2001 From: David Skoland Date: Thu, 30 Nov 2023 00:35:08 +0100 Subject: Revert fileExists --- webAO/utils/fileExists.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'webAO/utils/fileExists.ts') diff --git a/webAO/utils/fileExists.ts b/webAO/utils/fileExists.ts index f1a9924..abb2928 100644 --- a/webAO/utils/fileExists.ts +++ b/webAO/utils/fileExists.ts @@ -1,9 +1,19 @@ export default async function fileExists(url: string): Promise { - return fetch(url, { - method: 'HEAD', - }).then((response) => { - return response.ok; - }).catch(() => { - return false; + return new Promise((resolve) => { + const xhr = new XMLHttpRequest(); + xhr.open('HEAD', url); + xhr.onload = function checkLoad() { + if (xhr.readyState === 4) { + if (xhr.status === 200) { + resolve(true); + } else { + resolve(false); + } + } + }; + xhr.onerror = function checkError() { + resolve(false); + }; + xhr.send(null); }); } -- cgit