Skip to content

Commit

Permalink
Replace 'url' dependency with native URL global (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
NiranjanaBinoy authored Dec 15, 2020
1 parent 250eda0 commit 84fd68f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"name": "eth-json-rpc-middleware",
"version": "6.0.0",
"main": "block-ref.js",
"engines": {
"node": ">=10.0.0"
},
"directories": {
"test": "test"
},
Expand Down
18 changes: 8 additions & 10 deletions src/fetch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const url = require('url')
const { ethErrors } = require('eth-rpc-errors')
const btoa = require('btoa')
const createAsyncMiddleware = require('json-rpc-engine/src/createAsyncMiddleware')
Expand Down Expand Up @@ -69,6 +68,7 @@ function checkForHttpErrors (fetchRes) {
case 503:
case 504:
throw createTimeoutError()

default:
break
}
Expand All @@ -93,8 +93,8 @@ function parseResponse (fetchRes, body) {
}

function createFetchConfigFromReq ({ req, rpcUrl, originHttpHeaderKey }) {
// eslint-disable-next-line node/no-deprecated-api
const parsedUrl = url.parse(rpcUrl)

const parsedUrl = new URL(rpcUrl)
const fetchUrl = normalizeUrlFromParsed(parsedUrl)

// prepare payload
Expand Down Expand Up @@ -123,8 +123,9 @@ function createFetchConfigFromReq ({ req, rpcUrl, originHttpHeaderKey }) {
}

// encoded auth details as header (not allowed in fetch url)
if (parsedUrl.auth) {
const encodedAuth = btoa(parsedUrl.auth)
if (parsedUrl.username && parsedUrl.password) {
const authString = `${parsedUrl.username}:${parsedUrl.password}`
const encodedAuth = btoa(authString)
fetchParams.headers.Authorization = `Basic ${encodedAuth}`
}

Expand All @@ -139,14 +140,11 @@ function createFetchConfigFromReq ({ req, rpcUrl, originHttpHeaderKey }) {
function normalizeUrlFromParsed (parsedUrl) {
let result = ''
result += parsedUrl.protocol
if (parsedUrl.slashes) {
result += '//'
}
result += parsedUrl.hostname
result += `//${parsedUrl.hostname}`
if (parsedUrl.port) {
result += `:${parsedUrl.port}`
}
result += `${parsedUrl.path}`
result += `${parsedUrl.pathname}`
return result
}

Expand Down

0 comments on commit 84fd68f

Please sign in to comment.