-
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.
replace SocketAddress w/ addressUtils
Removes the `SocketAddress` class to avoid confusion with the `Address` type and moves the functionality to `addressUtils` helper classes.
- Loading branch information
Showing
5 changed files
with
48 additions
and
57 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
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,34 @@ | ||
import { Socket } from 'net'; | ||
import { Address } from '../types/p2p'; | ||
import assert from 'assert'; | ||
|
||
/** Helper methods for interacting with the [[Address]] type. */ | ||
const addressUtils = { | ||
/** | ||
* Create an [[Address]] using the remote host and port of a socket. | ||
*/ | ||
fromSocket: (socket: Socket): Address => { | ||
const { remoteAddress, remotePort } = socket; | ||
assert(remoteAddress, 'socket must have a remoteAddress value'); | ||
assert(remotePort, 'socket must have a remotePort value'); | ||
return { host: remoteAddress!, port: remotePort! }; | ||
}, | ||
|
||
/** | ||
* Create an [[Address]] from a string. | ||
* @param addressString a string in the "{host}:{port}" format | ||
* @param port a port number to use if no port is specified in the string, defaults to 8885 | ||
*/ | ||
fromString: (addressString: string, port = 8885): Address => { | ||
const arr = addressString.split(':'); | ||
return { | ||
host: arr[0], | ||
port: arr[1] ? parseInt(arr[1], 10) : port, | ||
}; | ||
}, | ||
|
||
/** Convert an [[Address]] to a string in the "{host}:{port}" format. */ | ||
toString: (address: Address) => `${address.host}:${address.port}`, | ||
}; | ||
|
||
export default addressUtils; |