Skip to content

Commit

Permalink
feat: add support for localaddress (#1659)
Browse files Browse the repository at this point in the history
* build(deps-dev): bump @types/node from 17.0.45 to 18.0.3 (#10)

Bumps [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) from 17.0.45 to 18.0.3.
- [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases)
- [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node)

---
updated-dependencies:
- dependency-name: "@types/node"
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* feat: add support for localaddress

* tests: add testing

* revert: missed test

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
  • Loading branch information
metcoder95 and dependabot[bot] committed Oct 17, 2022
1 parent 6714f9d commit 220103d
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 16 deletions.
23 changes: 17 additions & 6 deletions lib/client.js
Expand Up @@ -60,7 +60,8 @@ const {
kClose,
kDestroy,
kDispatch,
kInterceptors
kInterceptors,
kLocalAddress
} = require('./core/symbols')

const kClosedResolve = Symbol('kClosedResolve')
Expand Down Expand Up @@ -102,7 +103,8 @@ class Client extends DispatcherBase {
maxCachedSessions,
maxRedirections,
connect,
maxRequestsPerClient
maxRequestsPerClient,
localAddress
} = {}) {
super()

Expand Down Expand Up @@ -170,6 +172,10 @@ class Client extends DispatcherBase {
throw new InvalidArgumentError('maxRequestsPerClient must be a positive number')
}

if (localAddress != null && (typeof localAddress !== 'string' || net.isIP(localAddress) === 0)) {
throw new InvalidArgumentError('localAddress must be valid string IP address')
}

if (typeof connect !== 'function') {
connect = buildConnector({
...tls,
Expand All @@ -193,6 +199,7 @@ class Client extends DispatcherBase {
this[kKeepAliveTimeoutThreshold] = keepAliveTimeoutThreshold == null ? 1e3 : keepAliveTimeoutThreshold
this[kKeepAliveTimeoutValue] = this[kKeepAliveDefaultTimeout]
this[kServerName] = null
this[kLocalAddress] = localAddress != null ? localAddress : null
this[kResuming] = 0 // 0, idle, 1, scheduled, 2 resuming
this[kNeedDrain] = 0 // 0, idle, 1, scheduled, 2 resuming
this[kHostHeader] = `host: ${this[kUrl].hostname}${this[kUrl].port ? `:${this[kUrl].port}` : ''}\r\n`
Expand Down Expand Up @@ -1020,7 +1027,8 @@ async function connect (client) {
hostname,
protocol,
port,
servername: client[kServerName]
servername: client[kServerName],
localAddress: client[kLocalAddress]
},
connector: client[kConnector]
})
Expand All @@ -1033,7 +1041,8 @@ async function connect (client) {
hostname,
protocol,
port,
servername: client[kServerName]
servername: client[kServerName],
localAddress: client[kLocalAddress]
}, (err, socket) => {
if (err) {
reject(err)
Expand Down Expand Up @@ -1076,7 +1085,8 @@ async function connect (client) {
hostname,
protocol,
port,
servername: client[kServerName]
servername: client[kServerName],
localAddress: client[kLocalAddress]
},
connector: client[kConnector],
socket
Expand All @@ -1093,7 +1103,8 @@ async function connect (client) {
hostname,
protocol,
port,
servername: client[kServerName]
servername: client[kServerName],
localAddress: client[kLocalAddress]
},
connector: client[kConnector],
error: err
Expand Down
4 changes: 3 additions & 1 deletion lib/core/connect.js
Expand Up @@ -21,7 +21,7 @@ function buildConnector ({ maxCachedSessions, socketPath, timeout, ...opts }) {
timeout = timeout == null ? 10e3 : timeout
maxCachedSessions = maxCachedSessions == null ? 100 : maxCachedSessions

return function connect ({ hostname, host, protocol, port, servername, httpSocket }, callback) {
return function connect ({ hostname, host, protocol, port, servername, localAddress, httpSocket }, callback) {
let socket
if (protocol === 'https:') {
if (!tls) {
Expand All @@ -39,6 +39,7 @@ function buildConnector ({ maxCachedSessions, socketPath, timeout, ...opts }) {
...options,
servername,
session,
localAddress,
socket: httpSocket, // upgrade socket connection
port: port || 443,
host: hostname
Expand Down Expand Up @@ -70,6 +71,7 @@ function buildConnector ({ maxCachedSessions, socketPath, timeout, ...opts }) {
socket = net.connect({
highWaterMark: 64 * 1024, // Same as nodejs fs streams.
...options,
localAddress,
port: port || 80,
host: hostname
})
Expand Down
1 change: 1 addition & 0 deletions lib/core/symbols.js
Expand Up @@ -17,6 +17,7 @@ module.exports = {
kHeadersTimeout: Symbol('headers timeout'),
kBodyTimeout: Symbol('body timeout'),
kServerName: Symbol('server name'),
kLocalAddress: Symbol('local address'),
kHost: Symbol('host'),
kNoRef: Symbol('no ref'),
kBodyUsed: Symbol('used'),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -66,7 +66,7 @@
},
"devDependencies": {
"@sinonjs/fake-timers": "^9.1.2",
"@types/node": "^17.0.45",
"@types/node": "^18.0.3",
"abort-controller": "^3.0.0",
"atomic-sleep": "^1.0.0",
"chai": "^4.3.4",
Expand Down
20 changes: 20 additions & 0 deletions test/client-errors.js
Expand Up @@ -307,6 +307,26 @@ test('invalid options throws', (t) => {
t.equal(err.message, 'invalid keepAliveTimeout')
}

try {
new Client(new URL('http://localhost:200'), { // eslint-disable-line
localAddress: 123
}) // eslint-disable-line
t.fail()
} catch (err) {
t.type(err, errors.InvalidArgumentError)
t.equal(err.message, 'localAddress must be valid string IP address')
}

try {
new Client(new URL('http://localhost:200'), { // eslint-disable-line
localAddress: 'abcd123'
}) // eslint-disable-line
t.fail()
} catch (err) {
t.type(err, errors.InvalidArgumentError)
t.equal(err.message, 'localAddress must be valid string IP address')
}

try {
new Client(new URL('http://localhost:200'), { // eslint-disable-line
keepAliveMaxTimeout: 'asd'
Expand Down
4 changes: 2 additions & 2 deletions test/diagnostics-channel/connect-error.js
Expand Up @@ -22,7 +22,7 @@ diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectPa
_connector = connector

t.equal(typeof _connector, 'function')
t.equal(Object.keys(connectParams).length, 5)
t.equal(Object.keys(connectParams).length, 6)

const { host, hostname, protocol, port, servername } = connectParams

Expand All @@ -34,7 +34,7 @@ diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectPa
})

diagnosticsChannel.channel('undici:client:connectError').subscribe(({ error, connectParams, connector }) => {
t.equal(Object.keys(connectParams).length, 5)
t.equal(Object.keys(connectParams).length, 6)
t.equal(_connector, connector)

const { host, hostname, protocol, port, servername } = connectParams
Expand Down
4 changes: 2 additions & 2 deletions test/diagnostics-channel/get.js
Expand Up @@ -49,7 +49,7 @@ diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectPa
_connector = connector

t.equal(typeof _connector, 'function')
t.equal(Object.keys(connectParams).length, 5)
t.equal(Object.keys(connectParams).length, 6)

const { host, hostname, protocol, port, servername } = connectParams

Expand All @@ -65,7 +65,7 @@ diagnosticsChannel.channel('undici:client:connected').subscribe(({ connectParams
_socket = socket

t.equal(_connector, connector)
t.equal(Object.keys(connectParams).length, 5)
t.equal(Object.keys(connectParams).length, 6)

const { host, hostname, protocol, port, servername } = connectParams

Expand Down
4 changes: 2 additions & 2 deletions test/diagnostics-channel/post-stream.js
Expand Up @@ -52,7 +52,7 @@ diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectPa
_connector = connector

t.equal(typeof _connector, 'function')
t.equal(Object.keys(connectParams).length, 5)
t.equal(Object.keys(connectParams).length, 6)

const { host, hostname, protocol, port, servername } = connectParams

Expand All @@ -67,7 +67,7 @@ let _socket
diagnosticsChannel.channel('undici:client:connected').subscribe(({ connectParams, socket, connector }) => {
_socket = socket

t.equal(Object.keys(connectParams).length, 5)
t.equal(Object.keys(connectParams).length, 6)
t.equal(_connector, connector)

const { host, hostname, protocol, port, servername } = connectParams
Expand Down
4 changes: 2 additions & 2 deletions test/diagnostics-channel/post.js
Expand Up @@ -50,7 +50,7 @@ diagnosticsChannel.channel('undici:client:beforeConnect').subscribe(({ connectPa
_connector = connector

t.equal(typeof _connector, 'function')
t.equal(Object.keys(connectParams).length, 5)
t.equal(Object.keys(connectParams).length, 6)

const { host, hostname, protocol, port, servername } = connectParams

Expand All @@ -65,7 +65,7 @@ let _socket
diagnosticsChannel.channel('undici:client:connected').subscribe(({ connectParams, socket, connector }) => {
_socket = socket

t.equal(Object.keys(connectParams).length, 5)
t.equal(Object.keys(connectParams).length, 6)
t.equal(_connector, connector)

const { host, hostname, protocol, port, servername } = connectParams
Expand Down

0 comments on commit 220103d

Please sign in to comment.