Socket
Socket
Sign inDemoInstall

undici

Package Overview
Dependencies
Maintainers
2
Versions
212
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

undici - npm Package Compare versions

Comparing version 4.0.0-alpha.5 to 4.0.0-rc.1

lib/core/connect.js

139

lib/client.js

@@ -5,5 +5,4 @@ 'use strict'

const assert = require('assert')
const net = require('net')
const tls = require('tls')
const assert = require('assert')
const util = require('./core/util')

@@ -26,2 +25,3 @@ const Request = require('./core/request')

} = require('./core/errors')
const makeConnect = require('./core/connect')

@@ -31,3 +31,3 @@ const {

kReset,
kHost,
kServerName,
kClient,

@@ -47,9 +47,5 @@ kBusy,

kNoRef,
kTLSServerName,
kTLSSession,
kSetTLSSession,
kConnectTimeoutValue,
kKeepAliveDefaultTimeout,
kHostHeader,
kTLSOpts,
kClosed,

@@ -63,3 +59,2 @@ kDestroyed,

kSocket,
kSocketPath,
kKeepAliveTimeoutValue,

@@ -71,14 +66,6 @@ kMaxHeadersSize,

kBodyTimeout,
kStrictContentLength
kStrictContentLength,
kConnector
} = require('./core/symbols')
function getServerName (client, host) {
return (
util.getServerName(host) ||
(client[kTLSOpts] && client[kTLSOpts].servername) ||
util.getServerName(client[kUrl].host) ||
null
)
}
class Client extends Dispatcher {

@@ -101,3 +88,4 @@ constructor (url, {

tls,
strictContentLength
strictContentLength,
[kConnect]: connect
} = {}) {

@@ -158,7 +146,11 @@ super()

if (connect != null && typeof connect !== 'function') {
throw new InvalidArgumentError('connect must be a function')
}
this[kUrl] = util.parseOrigin(url)
this[kConnector] = connect || makeConnect({ tls, socketPath })
this[kSocket] = null
this[kPipelining] = pipelining != null ? pipelining : 1
this[kMaxHeadersSize] = maxHeaderSize || 16384
this[kUrl] = util.parseOrigin(url)
this[kSocketPath] = socketPath
this[kConnectTimeoutValue] = connectTimeout == null ? 10e3 : connectTimeout

@@ -171,5 +163,3 @@ this[kKeepAliveDefaultTimeout] = keepAliveTimeout == null ? 4e3 : keepAliveTimeout

this[kDestroyed] = false
this[kTLSOpts] = tls
this[kTLSServerName] = getServerName(this)
this[kHost] = null
this[kServerName] = null
this[kOnDestroyed] = []

@@ -183,4 +173,2 @@ this[kResuming] = 0 // 0, idle, 1, scheduled, 2 resuming

this[kTLSSession] = tls && tls.session ? tls.session : null
// kQueue is built up of 3 sections separated by

@@ -251,7 +239,2 @@ // the kRunningIdx and kPendingIdx indices.

[kSetTLSSession] (session) {
this[kTLSSession] = session
this.emit('session', session)
}
dispatch (opts, handler) {

@@ -629,3 +612,3 @@ if (!handler || typeof handler !== 'object') {

assert(!socket.isPaused())
assert(socket._handle && socket._handle.reading)
assert(!this.paused)
assert(request.upgrade || request.method === 'CONNECT')

@@ -652,3 +635,13 @@

detachSocket(socket)
socket[kParser].destroy()
socket[kParser] = null
socket[kClient] = null
socket[kError] = null
socket
.removeListener('error', onSocketError)
.removeListener('data', onSocketData)
.removeListener('end', onSocketEnd)
.removeListener('close', onSocketClose)
client[kSocket] = null

@@ -894,3 +887,3 @@ client[kQueue][client[kRunningIdx]++] = null

if (!socket[kWriting]) {
assert(!socket.isPaused(), 'socket cannot be paused while waiting for headers')
assert(!parser.paused, 'cannot be paused while waiting for headers')
util.destroy(socket, new HeadersTimeoutError())

@@ -931,3 +924,3 @@ }

assert(client[kRunning] === 0)
while (client[kPending] > 0 && client[kQueue][client[kPendingIdx]].host === client[kHost]) {
while (client[kPending] > 0 && client[kQueue][client[kPendingIdx]].servername === client[kServerName]) {
const request = client[kQueue][client[kPendingIdx]++]

@@ -960,16 +953,2 @@ request.onError(err)

function detachSocket (socket) {
socket[kParser].destroy()
socket[kParser] = null
socket[kClient] = null
socket[kError] = null
socket
.removeListener('session', onSocketSession)
.removeListener('error', onSocketError)
.removeListener('data', onSocketData)
.removeListener('end', onSocketEnd)
.removeListener('close', onSocketClose)
}
function onSocketClose () {

@@ -980,10 +959,7 @@ const { [kClient]: client } = this

detachSocket(this)
this[kParser].destroy()
this[kParser] = null
client[kSocket] = null
if (err.code !== 'UND_ERR_INFO') {
// Evict session on errors.
client[kSetTLSSession](null)
}
if (client[kDestroyed]) {

@@ -1022,12 +998,6 @@ assert(client[kPending] === 0)

function onSocketSession (session) {
const { [kClient]: client } = this
// Cache new session for reuse.
client[kSetTLSSession](session)
}
function connect (client) {
assert(!client[kSocket])
let { protocol, port, hostname } = client[kUrl]
let { host, hostname, protocol, port } = client[kUrl]

@@ -1045,22 +1015,10 @@ // Resolve ipv6

let socket
if (protocol === 'https:') {
const tlsOpts = {
...client[kTLSOpts],
servername: client[kTLSServerName],
session: client[kTLSSession]
}
const socket = client[kConnector]({
host,
hostname,
protocol,
port,
servername: util.getServerName(client[kServerName])
}, onSocketConnect)
/* istanbul ignore next: https://github.com/mcollina/undici/issues/267 */
socket = client[kSocketPath]
? tls.connect(client[kSocketPath], tlsOpts)
: tls.connect(port || /* istanbul ignore next */ 443, hostname, tlsOpts)
socket.on('session', onSocketSession)
} else {
socket = client[kSocketPath]
? net.connect(client[kSocketPath])
: net.connect(port || /* istanbul ignore next */ 80, hostname)
}
client[kSocket] = socket

@@ -1076,4 +1034,2 @@

socket
.setNoDelay(true)
.on(protocol === 'https:' ? 'secureConnect' : 'connect', onSocketConnect)
.on('error', onSocketError)

@@ -1172,3 +1128,3 @@ .on('data', onSocketData)

if (client[kUrl].protocol === 'https:' && client[kHost] !== request.host) {
if (client[kUrl].protocol === 'https:' && client[kServerName] !== request.servername) {
if (client[kRunning] > 0) {

@@ -1178,14 +1134,7 @@ return

client[kHost] = request.host
client[kServerName] = request.servername
const servername = getServerName(client, request.host)
if (client[kTLSServerName] !== servername) {
client[kTLSServerName] = servername
client[kSetTLSSession](null)
if (socket) {
util.destroy(socket, new InformationalError('servername changed'))
return
}
if (socket && socket.servername !== request.servername) {
util.destroy(socket, new InformationalError('servername changed'))
return
}

@@ -1192,0 +1141,0 @@ }

@@ -124,2 +124,4 @@ 'use strict'

this.servername = util.getServerName(this.host)
this[kHandler] = handler

@@ -126,0 +128,0 @@ }

@@ -16,3 +16,3 @@ module.exports = {

kBodyTimeout: Symbol('body timeout'),
kTLSServerName: Symbol('server name'),
kServerName: Symbol('server name'),
kHost: Symbol('host'),

@@ -25,3 +25,2 @@ kNoRef: Symbol('no ref'),

kConnected: Symbol('connected'),
kTLSOpts: Symbol('TLS Options'),
kClosed: Symbol('closed'),

@@ -40,8 +39,6 @@ kNeedDrain: Symbol('need drain'),

kPipelining: Symbol('pipelinig'),
kSocketPath: Symbol('socket path'),
kSocket: Symbol('socket'),
kTLSSession: Symbol('tls session cache'),
kSetTLSSession: Symbol('set tls session'),
kHostHeader: Symbol('host header'),
kConnector: Symbol('connector'),
kStrictContentLength: Symbol('strict content length')
}

@@ -90,2 +90,4 @@ 'use strict'

assert.strictEqual(typeof host, 'string')
let servername = host

@@ -92,0 +94,0 @@

@@ -12,4 +12,5 @@ 'use strict'

const util = require('./core/util')
const { kTLSSession, kSize, kConnected, kRunning, kPending, kUrl, kBusy } = require('./core/symbols')
const { kSize, kConnected, kConnect, kRunning, kPending, kUrl, kBusy } = require('./core/symbols')
const assert = require('assert')
const makeConnect = require('./core/connect')

@@ -26,3 +27,2 @@ const kClients = Symbol('clients')

const kOnDisconnect = Symbol('onDisconnect')
const kOnTLSSession = Symbol('onTLSSession')
const kConnections = Symbol('connections')

@@ -37,3 +37,3 @@ const kFactory = Symbol('factory')

class Pool extends Dispatcher {
constructor (origin, { connections, factory = defaultFactory, ...options } = {}) {
constructor (origin, { connections, factory = defaultFactory, [kConnect]: connect, tls, socketPath, ...options } = {}) {
super()

@@ -49,5 +49,9 @@

if (connect != null && typeof connect !== 'function') {
throw new InvalidArgumentError('connect must be a function')
}
this[kConnections] = connections || null
this[kUrl] = util.parseOrigin(origin)
this[kOptions] = JSON.parse(JSON.stringify(options))
this[kOptions] = { ...JSON.parse(JSON.stringify(options)), [kConnect]: connect || makeConnect({ tls, socketPath }) }
this[kQueue] = new FixedQueue()

@@ -101,8 +105,2 @@ this[kClosedPromise] = null

}
this[kOnTLSSession] = function cacheClientTLSSession (session) {
if (session) {
pool[kTLSSession] = session
}
}
}

@@ -177,22 +175,6 @@

if (!this[kConnections] || this[kClients].length < this[kConnections]) {
let options = this[kOptions]
if (
options.tls &&
options.tls.reuseSessions !== false &&
!options.tls.session &&
this[kTLSSession]
) {
options = { ...options, tls: { ...options.tls, session: this[kTLSSession] } }
}
dispatcher = this[kFactory](this[kUrl], options)
dispatcher = this[kFactory](this[kUrl], this[kOptions])
.on('drain', this[kOnDrain])
.on('connect', this[kOnConnect])
.on('disconnect', this[kOnDisconnect])
if (!options.tls || (options.tls.reuseSessions !== false && !options.tls.session)) {
dispatcher.on('session', this[kOnTLSSession])
}
this[kClients].push(dispatcher)

@@ -199,0 +181,0 @@ }

{
"name": "undici",
"version": "4.0.0-alpha.5",
"version": "4.0.0-rc.1",
"description": "An HTTP/1.1 client, written from scratch for Node.js",

@@ -5,0 +5,0 @@ "homepage": "https://undici.nodejs.org",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc