11cb0ef41Sopenharmony_ci# Connector
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciUndici creates the underlying socket via the connector builder.
41cb0ef41Sopenharmony_ciNormally, this happens automatically and you don't need to care about this,
51cb0ef41Sopenharmony_cibut if you need to perform some additional check over the currently used socket,
61cb0ef41Sopenharmony_cithis is the right place.
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciIf you want to create a custom connector, you must import the `buildConnector` utility.
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci#### Parameter: `buildConnector.BuildOptions`
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ciEvery Tls option, see [here](https://nodejs.org/api/tls.html#tls_tls_connect_options_callback).
131cb0ef41Sopenharmony_ciFurthermore, the following options can be passed:
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci* **socketPath** `string | null` (optional) - Default: `null` - An IPC endpoint, either Unix domain socket or Windows named pipe.
161cb0ef41Sopenharmony_ci* **maxCachedSessions** `number | null` (optional) - Default: `100` - Maximum number of TLS cached sessions. Use 0 to disable TLS session caching. Default: `100`.
171cb0ef41Sopenharmony_ci* **timeout** `number | null` (optional) -  In milliseconds. Default `10e3`.
181cb0ef41Sopenharmony_ci* **servername** `string | null` (optional)
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciOnce you call `buildConnector`, it will return a connector function, which takes the following parameters.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci#### Parameter: `connector.Options`
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci* **hostname** `string` (required)
251cb0ef41Sopenharmony_ci* **host** `string` (optional)
261cb0ef41Sopenharmony_ci* **protocol** `string` (required)
271cb0ef41Sopenharmony_ci* **port** `string` (required)
281cb0ef41Sopenharmony_ci* **servername** `string` (optional)
291cb0ef41Sopenharmony_ci* **localAddress** `string | null` (optional) Local address the socket should connect from.
301cb0ef41Sopenharmony_ci* **httpSocket** `Socket` (optional) Establish secure connection on a given socket rather than creating a new socket. It can only be sent on TLS update.
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci### Basic example
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci```js
351cb0ef41Sopenharmony_ci'use strict'
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciimport { Client, buildConnector } from 'undici'
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciconst connector = buildConnector({ rejectUnauthorized: false })
401cb0ef41Sopenharmony_ciconst client = new Client('https://localhost:3000', {
411cb0ef41Sopenharmony_ci  connect (opts, cb) {
421cb0ef41Sopenharmony_ci    connector(opts, (err, socket) => {
431cb0ef41Sopenharmony_ci      if (err) {
441cb0ef41Sopenharmony_ci        cb(err)
451cb0ef41Sopenharmony_ci      } else if (/* assertion */) {
461cb0ef41Sopenharmony_ci        socket.destroy()
471cb0ef41Sopenharmony_ci        cb(new Error('kaboom'))
481cb0ef41Sopenharmony_ci      } else {
491cb0ef41Sopenharmony_ci        cb(null, socket)
501cb0ef41Sopenharmony_ci      }
511cb0ef41Sopenharmony_ci    })
521cb0ef41Sopenharmony_ci  }
531cb0ef41Sopenharmony_ci})
541cb0ef41Sopenharmony_ci```
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci### Example: validate the CA fingerprint
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci```js
591cb0ef41Sopenharmony_ci'use strict'
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ciimport { Client, buildConnector } from 'undici'
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ciconst caFingerprint = 'FO:OB:AR'
641cb0ef41Sopenharmony_ciconst connector = buildConnector({ rejectUnauthorized: false })
651cb0ef41Sopenharmony_ciconst client = new Client('https://localhost:3000', {
661cb0ef41Sopenharmony_ci  connect (opts, cb) {
671cb0ef41Sopenharmony_ci    connector(opts, (err, socket) => {
681cb0ef41Sopenharmony_ci      if (err) {
691cb0ef41Sopenharmony_ci        cb(err)
701cb0ef41Sopenharmony_ci      } else if (getIssuerCertificate(socket).fingerprint256 !== caFingerprint) {
711cb0ef41Sopenharmony_ci        socket.destroy()
721cb0ef41Sopenharmony_ci        cb(new Error('Fingerprint does not match or malformed certificate'))
731cb0ef41Sopenharmony_ci      } else {
741cb0ef41Sopenharmony_ci        cb(null, socket)
751cb0ef41Sopenharmony_ci      }
761cb0ef41Sopenharmony_ci    })
771cb0ef41Sopenharmony_ci  }
781cb0ef41Sopenharmony_ci})
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ciclient.request({
811cb0ef41Sopenharmony_ci  path: '/',
821cb0ef41Sopenharmony_ci  method: 'GET'
831cb0ef41Sopenharmony_ci}, (err, data) => {
841cb0ef41Sopenharmony_ci  if (err) throw err
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ci  const bufs = []
871cb0ef41Sopenharmony_ci  data.body.on('data', (buf) => {
881cb0ef41Sopenharmony_ci    bufs.push(buf)
891cb0ef41Sopenharmony_ci  })
901cb0ef41Sopenharmony_ci  data.body.on('end', () => {
911cb0ef41Sopenharmony_ci    console.log(Buffer.concat(bufs).toString('utf8'))
921cb0ef41Sopenharmony_ci    client.close()
931cb0ef41Sopenharmony_ci  })
941cb0ef41Sopenharmony_ci})
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_cifunction getIssuerCertificate (socket) {
971cb0ef41Sopenharmony_ci  let certificate = socket.getPeerCertificate(true)
981cb0ef41Sopenharmony_ci  while (certificate && Object.keys(certificate).length > 0) {
991cb0ef41Sopenharmony_ci    // invalid certificate
1001cb0ef41Sopenharmony_ci    if (certificate.issuerCertificate == null) {
1011cb0ef41Sopenharmony_ci      return null
1021cb0ef41Sopenharmony_ci    }
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci    // We have reached the root certificate.
1051cb0ef41Sopenharmony_ci    // In case of self-signed certificates, `issuerCertificate` may be a circular reference.
1061cb0ef41Sopenharmony_ci    if (certificate.fingerprint256 === certificate.issuerCertificate.fingerprint256) {
1071cb0ef41Sopenharmony_ci      break
1081cb0ef41Sopenharmony_ci    }
1091cb0ef41Sopenharmony_ci
1101cb0ef41Sopenharmony_ci    // continue the loop
1111cb0ef41Sopenharmony_ci    certificate = certificate.issuerCertificate
1121cb0ef41Sopenharmony_ci  }
1131cb0ef41Sopenharmony_ci  return certificate
1141cb0ef41Sopenharmony_ci}
1151cb0ef41Sopenharmony_ci```
116