Skip to content

Commit

Permalink
SDK: Fallback to url-parse if URL is not available (#800)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
tomaszslabon authored Mar 20, 2024
2 parents c57d64b + cd2baaa commit fd88dd9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 2 additions & 0 deletions typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion typescript/src/lib/electrum/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions typescript/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -6727,6 +6732,11 @@ [email protected]:
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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit fd88dd9

Please sign in to comment.