-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows for node connection uris without a specified port to be parsed and assigned a default port of 8885. This allows the port to be left off in the rpc `Connect` call when connecting to the default port.
- Loading branch information
Showing
5 changed files
with
42 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import assert = require('assert'); | ||
|
||
export type UriParts = { | ||
nodePubKey: string; | ||
host: string; | ||
port: number; | ||
}; | ||
|
||
/** | ||
* Creates a URI string from the public key, host and port. | ||
*/ | ||
export const toUri = (uriParts: UriParts): string => { | ||
const { nodePubKey, host, port } = uriParts; | ||
return `${nodePubKey}@${host}:${port}`; | ||
}; | ||
|
||
/** | ||
* Splits a URI in the [pubkey]@[host]:[port] format into the public key, host and port components. | ||
* If port is not present in the URI, it defaults to 8885. | ||
*/ | ||
export const parseUri = (uri: string): UriParts => { | ||
// A regex that splits the string by the symbols "@" and ":" | ||
const split = uri.split(/[@:]+/); | ||
|
||
assert(split.length === 3 || split.length === 2); | ||
|
||
const port = split[2] !== undefined ? Number(split[2]) : 8885; | ||
assert(!isNaN(port)); | ||
|
||
return { | ||
port, | ||
nodePubKey: split[0], | ||
host: split[1], | ||
}; | ||
}; |
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