-
Notifications
You must be signed in to change notification settings - Fork 910
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[web3.js] Eliminate dependency on
URL
class (#27349)
* fix: `makeWebsocketUrl` no longer depends on the `URL` class * fix: `Connection` no longer relies on the `URL` class * fix: remove dependency on `react-native-url-polyfill`
- Loading branch information
1 parent
8d5e263
commit 4f2d052
Showing
8 changed files
with
23,019 additions
and
33,412 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,26 @@ | ||
import {URL} from './url-impl'; | ||
const URL_RE = /^[^:]+:\/\/([^:[]+|\[[^\]]+\])(:\d+)?(.*)/i; | ||
|
||
export function makeWebsocketUrl(endpoint: string) { | ||
let url = new URL(endpoint); | ||
const useHttps = url.protocol === 'https:'; | ||
|
||
url.protocol = useHttps ? 'wss:' : 'ws:'; | ||
url.host = ''; | ||
|
||
// Only shift the port by +1 as a convention for ws(s) only if given endpoint | ||
// is explictly specifying the endpoint port (HTTP-based RPC), assuming | ||
// we're directly trying to connect to solana-validator's ws listening port. | ||
// When the endpoint omits the port, we're connecting to the protocol | ||
// default ports: http(80) or https(443) and it's assumed we're behind a reverse | ||
// proxy which manages WebSocket upgrade and backend port redirection. | ||
if (url.port !== '') { | ||
url.port = String(Number(url.port) + 1); | ||
const matches = endpoint.match(URL_RE); | ||
if (matches == null) { | ||
throw TypeError(`Failed to validate endpoint URL \`${endpoint}\``); | ||
} | ||
return url.toString(); | ||
const [ | ||
_, // eslint-disable-line @typescript-eslint/no-unused-vars | ||
hostish, | ||
portWithColon, | ||
rest, | ||
] = matches; | ||
const protocol = endpoint.startsWith('https:') ? 'wss:' : 'ws:'; | ||
const startPort = | ||
portWithColon == null ? null : parseInt(portWithColon.slice(1), 10); | ||
const websocketPort = | ||
// Only shift the port by +1 as a convention for ws(s) only if given endpoint | ||
// is explictly specifying the endpoint port (HTTP-based RPC), assuming | ||
// we're directly trying to connect to solana-validator's ws listening port. | ||
// When the endpoint omits the port, we're connecting to the protocol | ||
// default ports: http(80) or https(443) and it's assumed we're behind a reverse | ||
// proxy which manages WebSocket upgrade and backend port redirection. | ||
startPort == null ? '' : `:${startPort + 1}`; | ||
return `${protocol}//${hostish}${websocketPort}${rest}`; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {expect} from 'chai'; | ||
|
||
import {makeWebsocketUrl} from '../src/utils/makeWebsocketUrl'; | ||
|
||
const INVALID_URLS = [ | ||
'', | ||
'0.0.0.0', | ||
'localhost', | ||
'www.no-protocol.com', | ||
'//api.protocol.relative.com', | ||
]; | ||
const TEST_CASES = [ | ||
// Non-https => `ws` | ||
['http://api.devnet.solana.com/', 'ws://api.devnet.solana.com/'], | ||
['gopher://gopher.example.com/', 'ws://gopher.example.com/'], | ||
['http://localhost/', 'ws://localhost/'], | ||
// `https` => `wss` | ||
['https://api.devnet.solana.com/', 'wss://api.devnet.solana.com/'], | ||
// IPv4 address | ||
['https://192.168.0.1/', 'wss://192.168.0.1/'], | ||
// IPv6 address | ||
['https://[0:0:0:0:0:0:0:0]/', 'wss://[0:0:0:0:0:0:0:0]/'], | ||
['https://[::]/', 'wss://[::]/'], | ||
['https://[::1]/', 'wss://[::1]/'], | ||
// Increment port if supplied | ||
['https://api.devnet.solana.com:80/', 'wss://api.devnet.solana.com:81/'], | ||
['https://192.168.0.1:443/', 'wss://192.168.0.1:444/'], | ||
['https://[::]:8080/', 'wss://[::]:8081/'], | ||
// No trailing slash | ||
['http://api.devnet.solana.com', 'ws://api.devnet.solana.com'], | ||
['https://api.devnet.solana.com', 'wss://api.devnet.solana.com'], | ||
['https://api.devnet.solana.com:80', 'wss://api.devnet.solana.com:81'], | ||
// Username | ||
['https://[email protected]', 'wss://[email protected]'], | ||
// Username/password | ||
['https://bob:[email protected]', 'wss://bob:[email protected]'], | ||
]; | ||
|
||
describe('makeWebsocketUrl', () => { | ||
TEST_CASES.forEach(([inputUrl, outputUrl]) => { | ||
it(`converts \`${inputUrl}\` to \`${outputUrl}\``, () => { | ||
expect(makeWebsocketUrl(inputUrl)).to.equal(outputUrl); | ||
}); | ||
}); | ||
INVALID_URLS.forEach(invalidUrl => { | ||
it(`fatals when called with invalid url \`${invalidUrl}\``, () => { | ||
expect(() => { | ||
makeWebsocketUrl(invalidUrl); | ||
}).to.throw(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.