forked from pull-stream/pull-ws
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
React-native has a hobbled URL class that seems to make it's own WebSocket class cause a fatal crash so stringify it before passing it in. Otherwise the URL class is supported everywhere now so no need to use iso-url any more. Also noteworthy is that react-native doesn't have any node core modules so we have to use a similar browser override system to ensure the pure-js/browser APIs are used instead.
- Loading branch information
1 parent
96f4c7b
commit 440af16
Showing
5 changed files
with
82 additions
and
32 deletions.
There are no files selected for viewing
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 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,6 +1,25 @@ | ||
import { relative } from 'iso-url' | ||
const map = { 'http:': 'ws:', 'https:': 'wss:' } | ||
const defaultProtocol = 'ws:' | ||
|
||
const map = { http: 'ws', https: 'wss' } | ||
const def = 'ws' | ||
export default (url: string, location?: Partial<Location>): URL => { | ||
if (url.startsWith('//')) { | ||
url = `${location?.protocol ?? defaultProtocol}${url}` | ||
} | ||
|
||
export default (url: string, location: string | Partial<Location>): string => relative(url, location, map, def) | ||
if (url.startsWith('/') && location != null) { | ||
const proto = location.protocol ?? defaultProtocol | ||
const host = location.host | ||
const port = location.port != null && host?.endsWith(`:${location.port}`) !== true ? `:${location.port}` : '' | ||
url = `${proto}//${host}${port}${url}` | ||
} | ||
|
||
const wsUrl = new URL(url) | ||
|
||
for (const [httpProto, wsProto] of Object.entries(map)) { | ||
if (wsUrl.protocol === httpProto) { | ||
wsUrl.protocol = wsProto | ||
} | ||
} | ||
|
||
return wsUrl | ||
} |
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