-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: James M Snell <jasnell@gmail.com> PR-URL: #37917 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
- Loading branch information
1 parent
7f54ccc
commit 82363d8
Showing
10 changed files
with
520 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,153 @@ | ||
'use strict'; | ||
|
||
const { | ||
ObjectSetPrototypeOf, | ||
Symbol, | ||
} = primordials; | ||
|
||
const { | ||
SocketAddress: _SocketAddress, | ||
AF_INET, | ||
AF_INET6, | ||
} = internalBinding('block_list'); | ||
|
||
const { | ||
validateObject, | ||
validateString, | ||
validatePort, | ||
validateUint32, | ||
} = require('internal/validators'); | ||
|
||
const { | ||
codes: { | ||
ERR_INVALID_ARG_VALUE, | ||
}, | ||
} = require('internal/errors'); | ||
|
||
const { | ||
customInspectSymbol: kInspect, | ||
} = require('internal/util'); | ||
|
||
const { inspect } = require('internal/util/inspect'); | ||
|
||
const { | ||
JSTransferable, | ||
kClone, | ||
kDeserialize, | ||
} = require('internal/worker/js_transferable'); | ||
|
||
const kHandle = Symbol('kHandle'); | ||
const kDetail = Symbol('kDetail'); | ||
|
||
class SocketAddress extends JSTransferable { | ||
static isSocketAddress(value) { | ||
return value?.[kHandle] !== undefined; | ||
} | ||
|
||
constructor(options = {}) { | ||
super(); | ||
validateObject(options, 'options'); | ||
const { | ||
family = 'ipv4', | ||
address = (family === 'ipv4' ? '127.0.0.1' : '::'), | ||
port = 0, | ||
flowlabel = 0, | ||
} = options; | ||
|
||
let type; | ||
switch (family) { | ||
case 'ipv4': | ||
type = AF_INET; | ||
break; | ||
case 'ipv6': | ||
type = AF_INET6; | ||
break; | ||
default: | ||
throw new ERR_INVALID_ARG_VALUE('options.family', family); | ||
} | ||
|
||
validateString(address, 'options.address'); | ||
validatePort(port, 'options.port'); | ||
validateUint32(flowlabel, 'options.flowlabel', false); | ||
|
||
this[kHandle] = new _SocketAddress(address, port, type, flowlabel); | ||
this[kDetail] = this[kHandle].detail({ | ||
address: undefined, | ||
port: undefined, | ||
family: undefined, | ||
flowlabel: undefined, | ||
}); | ||
} | ||
|
||
get address() { | ||
return this[kDetail].address; | ||
} | ||
|
||
get port() { | ||
return this[kDetail].port; | ||
} | ||
|
||
get family() { | ||
return this[kDetail].family === AF_INET ? 'ipv4' : 'ipv6'; | ||
} | ||
|
||
get flowlabel() { | ||
// The flow label can be changed internally. | ||
return this[kHandle].flowlabel(); | ||
} | ||
|
||
[kInspect](depth, options) { | ||
if (depth < 0) | ||
return this; | ||
|
||
const opts = { | ||
...options, | ||
depth: options.depth == null ? null : options.depth - 1 | ||
}; | ||
|
||
return `SocketAddress ${inspect(this.toJSON(), opts)}`; | ||
} | ||
|
||
[kClone]() { | ||
const handle = this[kHandle]; | ||
return { | ||
data: { handle }, | ||
deserializeInfo: 'internal/socketaddress:InternalSocketAddress', | ||
}; | ||
} | ||
|
||
[kDeserialize]({ handle }) { | ||
this[kHandle] = handle; | ||
this[kDetail] = handle.detail({ | ||
address: undefined, | ||
port: undefined, | ||
family: undefined, | ||
flowlabel: undefined, | ||
}); | ||
} | ||
|
||
toJSON() { | ||
return { | ||
address: this.address, | ||
port: this.port, | ||
family: this.family, | ||
flowlabel: this.flowlabel, | ||
}; | ||
} | ||
} | ||
|
||
class InternalSocketAddress extends JSTransferable { | ||
constructor(handle) { | ||
super(); | ||
this[kHandle] = handle; | ||
} | ||
} | ||
|
||
InternalSocketAddress.prototype.constructor = | ||
SocketAddress.prototype.construtor; | ||
ObjectSetPrototypeOf(InternalSocketAddress.prototype, SocketAddress.prototype); | ||
|
||
module.exports = { | ||
SocketAddress, | ||
InternalSocketAddress, | ||
}; |
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 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
Oops, something went wrong.