From cd2baaaad2e19a9948ddfe58c68f44b318add5f8 Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Wed, 20 Mar 2024 17:29:55 +0100 Subject: [PATCH] Fallback to `url-parse` if `URL` is not available In some environments (e.g. Cloudflare Workers), the `URL` component cannot be resolved altogether. It is neither available as nodejs `url` module nor as browser's `window.URL` object. To handle this extreme case, we are adding a last resort fallback to `url-parse`. --- typescript/package.json | 2 ++ typescript/src/lib/electrum/client.ts | 3 ++- typescript/yarn.lock | 23 +++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/typescript/package.json b/typescript/package.json index 17c2cb3af..62f7835ad 100644 --- a/typescript/package.json +++ b/typescript/package.json @@ -36,6 +36,7 @@ "electrum-client-js": "git+https://github.com/keep-network/electrum-client-js.git#v0.1.1", "ethers": "^5.5.2", "p-timeout": "^4.1.0", + "url-parse": "^1.5.10", "wif": "2.0.6" }, "devDependencies": { @@ -46,6 +47,7 @@ "@types/chai-as-promised": "^7.1.4", "@types/mocha": "^8.2.3", "@types/node": "^16.4.0", + "@types/url-parse": "^1.4.11", "@typescript-eslint/eslint-plugin": "^4.28.3", "@typescript-eslint/parser": "^4.28.3", "chai": "^4.3.4", diff --git a/typescript/src/lib/electrum/client.ts b/typescript/src/lib/electrum/client.ts index 0d3d177c5..a8f4e0b2b 100644 --- a/typescript/src/lib/electrum/client.ts +++ b/typescript/src/lib/electrum/client.ts @@ -16,13 +16,14 @@ import { import Electrum from "electrum-client-js" import { BigNumber } from "ethers" import { URL as nodeURL } from "url" +import URLParse from "url-parse" import { backoffRetrier, Hex, RetrierFn } from "../utils" import MainnetElectrumUrls from "./urls/mainnet.json" import TestnetElectrumUrls from "./urls/testnet.json" const browserURL = typeof window !== "undefined" && window.URL -const URL = nodeURL ?? browserURL +const URL = nodeURL ?? (browserURL || URLParse) /** * Represents a set of credentials required to establish an Electrum connection. diff --git a/typescript/yarn.lock b/typescript/yarn.lock index 07666f31a..50dcebc13 100644 --- a/typescript/yarn.lock +++ b/typescript/yarn.lock @@ -2221,6 +2221,11 @@ dependencies: "@types/node" "*" +"@types/url-parse@^1.4.11": + version "1.4.11" + resolved "https://registry.yarnpkg.com/@types/url-parse/-/url-parse-1.4.11.tgz#01f8faa7b8bfd438e5f5efb8337a74513a15602b" + integrity sha512-FKvKIqRaykZtd4n47LbK/W/5fhQQ1X7cxxzG9A48h0BGN+S04NH7ervcCjM8tyR0lyGru83FAHSmw2ObgKoESg== + "@types/utf8@^2.1.6": version "2.1.6" resolved "https://registry.yarnpkg.com/@types/utf8/-/utf8-2.1.6.tgz#430cabb71a42d0a3613cce5621324fe4f5a25753" @@ -6727,6 +6732,11 @@ querystring@0.2.0: resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620" integrity sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA= +querystringify@^2.1.1: + version "2.2.0" + resolved "https://registry.yarnpkg.com/querystringify/-/querystringify-2.2.0.tgz#3345941b4153cb9d082d8eee4cda2016a9aef7f6" + integrity sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ== + queue-microtask@^1.2.2, queue-microtask@^1.2.3: version "1.2.3" resolved "https://registry.yarnpkg.com/queue-microtask/-/queue-microtask-1.2.3.tgz#4929228bbc724dfac43e0efb058caf7b6cfb6243" @@ -6878,6 +6888,11 @@ require-main-filename@^2.0.0: resolved "https://registry.yarnpkg.com/require-main-filename/-/require-main-filename-2.0.0.tgz#d0b329ecc7cc0f61649f62215be69af54aa8989b" integrity sha512-NKN5kMDylKuldxYLSUfrbo5Tuzh4hd+2E8NPPX02mZtn1VuREQToYe/ZdlJy+J3uCpfaiGF05e7B8W0iXbQHmg== +requires-port@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff" + integrity sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ== + resolve-from@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/resolve-from/-/resolve-from-4.0.0.tgz#4abcd852ad32dd7baabfe9b40e00a36db5f392e6" @@ -7897,6 +7912,14 @@ url-parse-lax@^3.0.0: dependencies: prepend-http "^2.0.0" +url-parse@^1.5.10: + version "1.5.10" + resolved "https://registry.yarnpkg.com/url-parse/-/url-parse-1.5.10.tgz#9d3c2f736c1d75dd3bd2be507dcc111f1e2ea9c1" + integrity sha512-WypcfiRhfeUP9vvF0j6rw0J3hrWrw6iZv3+22h6iRMJ/8z1Tj6XfLP4DsUix5MhMPnXpiHDoKyoZ/bdCkwBCiQ== + dependencies: + querystringify "^2.1.1" + requires-port "^1.0.0" + url-set-query@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/url-set-query/-/url-set-query-1.0.0.tgz#016e8cfd7c20ee05cafe7795e892bd0702faa339"