Skip to content

Commit a0220f1

Browse files
authored
fix(#3817): send servername for SNI on TLS (#3821) [backport] (#3864)
* fix(#3817): send servername for SNI on TLS (#3821) * fix(#3817): send servername for SNI on TLS * fix: set host header to servername * refactor: attach regardless (cherry picked from commit b93a834) * feat: missing interceptor * fix: lint
1 parent 353ab63 commit a0220f1

File tree

5 files changed

+2163
-1
lines changed

5 files changed

+2163
-1
lines changed

docs/docs/api/Dispatcher.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,57 @@ client.dispatch(
986986
);
987987
```
988988

989+
##### `dns`
990+
991+
The `dns` interceptor enables you to cache DNS lookups for a given duration, per origin.
992+
993+
>It is well suited for scenarios where you want to cache DNS lookups to avoid the overhead of resolving the same domain multiple times
994+
995+
**Options**
996+
- `maxTTL` - The maximum time-to-live (in milliseconds) of the DNS cache. It should be a positive integer. Default: `10000`.
997+
- Set `0` to disable TTL.
998+
- `maxItems` - The maximum number of items to cache. It should be a positive integer. Default: `Infinity`.
999+
- `dualStack` - Whether to resolve both IPv4 and IPv6 addresses. Default: `true`.
1000+
- It will also attempt a happy-eyeballs-like approach to connect to the available addresses in case of a connection failure.
1001+
- `affinity` - Whether to use IPv4 or IPv6 addresses. Default: `4`.
1002+
- It can be either `'4` or `6`.
1003+
- It will only take effect if `dualStack` is `false`.
1004+
- `lookup: (hostname: string, options: LookupOptions, callback: (err: NodeJS.ErrnoException | null, addresses: DNSInterceptorRecord[]) => void) => void` - Custom lookup function. Default: `dns.lookup`.
1005+
- For more info see [dns.lookup](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback).
1006+
- `pick: (origin: URL, records: DNSInterceptorRecords, affinity: 4 | 6) => DNSInterceptorRecord` - Custom pick function. Default: `RoundRobin`.
1007+
- The function should return a single record from the records array.
1008+
- By default a simplified version of Round Robin is used.
1009+
- The `records` property can be mutated to store the state of the balancing algorithm.
1010+
1011+
> The `Dispatcher#options` also gets extended with the options `dns.affinity`, `dns.dualStack`, `dns.lookup` and `dns.pick` which can be used to configure the interceptor at a request-per-request basis.
1012+
1013+
1014+
**DNSInterceptorRecord**
1015+
It represents a DNS record.
1016+
- `family` - (`number`) The IP family of the address. It can be either `4` or `6`.
1017+
- `address` - (`string`) The IP address.
1018+
1019+
**DNSInterceptorOriginRecords**
1020+
It represents a map of DNS IP addresses records for a single origin.
1021+
- `4.ips` - (`DNSInterceptorRecord[] | null`) The IPv4 addresses.
1022+
- `6.ips` - (`DNSInterceptorRecord[] | null`) The IPv6 addresses.
1023+
1024+
**Example - Basic DNS Interceptor**
1025+
1026+
```js
1027+
const { Client, interceptors } = require("undici");
1028+
const { dns } = interceptors;
1029+
1030+
const client = new Agent().compose([
1031+
dns({ ...opts })
1032+
])
1033+
1034+
const response = await client.request({
1035+
origin: `http://localhost:3030`,
1036+
...requestOpts
1037+
})
1038+
```
1039+
9891040
##### `Response Error Interceptor`
9901041

9911042
**Introduction**

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ module.exports.createRedirectInterceptor = createRedirectInterceptor
4141
module.exports.interceptors = {
4242
redirect: require('./lib/interceptor/redirect'),
4343
retry: require('./lib/interceptor/retry'),
44-
dump: require('./lib/interceptor/dump')
44+
dump: require('./lib/interceptor/dump'),
45+
dns: require('./lib/interceptor/dns')
4546
}
4647

4748
module.exports.buildConnector = buildConnector

0 commit comments

Comments
 (0)