Socket
Socket
Sign inDemoInstall

undici

Package Overview
Dependencies
Maintainers
2
Versions
211
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 3.3.0 to 3.3.1

62

lib/agent.js
'use strict'
/* global WeakRef, FinalizationRegistry */
const { InvalidArgumentError, InvalidReturnValueError } = require('./core/errors')
const Pool = require('./pool')
const util = require('./core/util')
const { kAgentOpts, kAgentCache, kAgentCleanup } = require('./core/symbols')
const { kAgentOpts, kAgentCache } = require('./core/symbols')

@@ -13,11 +11,2 @@ class Agent {

this[kAgentCache] = new Map()
this[kAgentCleanup] = new FinalizationRegistry(key => {
// get the WeakRef from the cache
const ref = this[kAgentCache].get(key)
// if the WeakRef exists and the object has been reclaimed
if (ref !== undefined && ref.deref() === undefined) {
// remove the WeakRef from the cache
this[kAgentCache].delete(key)
}
})
}

@@ -30,21 +19,19 @@

// check the cache for an existing WeakRef
const ref = this[kAgentCache].get(origin)
const self = this
let pool = self[kAgentCache].get(origin)
// if one exists in the cache try to return the WeakRef
if (ref !== undefined) {
const cached = ref.deref()
if (cached !== undefined) {
return cached
function onDisconnect () {
if (this.connected === 0 && this.pending === 0) {
this.off('disconnect', onDisconnect)
self[kAgentCache].delete(origin)
}
}
// otherwise, if it isn't in the cache or the reference has been cleaned up, create a new one!
const value = new Pool(origin, this[kAgentOpts])
// add a WeakRef of the value to the cache
this[kAgentCache].set(origin, new WeakRef(value))
// add the value to the finalization registry
this[kAgentCleanup].register(value, origin)
if (!pool) {
pool = new Pool(origin, self[kAgentOpts])
pool.on('disconnect', onDisconnect)
self[kAgentCache].set(origin, pool)
}
return value
return pool
}

@@ -54,8 +41,4 @@

const closePromises = []
for (const ref of this[kAgentCache].values()) {
const pool = ref.deref()
if (pool) {
closePromises.push(pool.close())
}
for (const pool of this[kAgentCache].values()) {
closePromises.push(pool.close())
}

@@ -67,8 +50,4 @@ return Promise.all(closePromises)

const destroyPromises = []
for (const ref of this[kAgentCache].values()) {
const pool = ref.deref()
if (pool) {
destroyPromises.push(pool.destroy())
}
for (const pool of this[kAgentCache].values()) {
destroyPromises.push(pool.destroy())
}

@@ -79,8 +58,3 @@ return Promise.all(destroyPromises)

let globalAgent = null
try {
globalAgent = new Agent({ connections: null })
} catch (err) {
// Silently fail to set globalAgent due to unsupported environment.
}
let globalAgent = new Agent({ connections: null })

@@ -87,0 +61,0 @@ function setGlobalAgent (agent) {

@@ -83,3 +83,2 @@ 'use strict'

headers,
servername,
signal

@@ -91,3 +90,2 @@ } = opts

headers,
servername,
signal

@@ -94,0 +92,0 @@ }, connectHandler)

@@ -233,3 +233,2 @@ 'use strict'

idempotent,
servername,
signal

@@ -243,3 +242,2 @@ } = opts

idempotent,
servername,
signal

@@ -246,0 +244,0 @@ }, pipelineHandler)

@@ -86,3 +86,2 @@ 'use strict'

headers,
servername,
signal,

@@ -95,3 +94,2 @@ protocol

headers,
servername,
signal,

@@ -98,0 +96,0 @@ upgrade: protocol || 'Websocket'

@@ -213,3 +213,7 @@ 'use strict'

const socket = this[kSocket]
return (socket && (socket[kReset] || socket[kWriting])) || this.pending > 0
return (
(socket && (socket[kReset] || socket[kWriting])) ||
(this.size >= (this[kPipelining] || 1)) ||
this.pending > 0
)
}

@@ -962,16 +966,16 @@

if (!client.pending) {
if (client[kNeedDrain] === 2 && !client.busy) {
if (sync) {
client[kNeedDrain] = 1
process.nextTick(emitDrain, client)
} else {
emitDrain(client)
}
continue
if (client.busy) {
client[kNeedDrain] = 2
} else if (client[kNeedDrain] === 2) {
if (sync) {
client[kNeedDrain] = 1
process.nextTick(emitDrain, client)
} else {
emitDrain(client)
}
continue
}
if (!client.pending) {
return
} else {
client[kNeedDrain] = 2
}

@@ -978,0 +982,0 @@

@@ -36,4 +36,3 @@ module.exports = {

kAgentOpts: Symbol('agent opts'),
kAgentCache: Symbol('agent cache'),
kAgentCleanup: Symbol('agent cleanup')
kAgentCache: Symbol('agent cache')
}

@@ -29,7 +29,5 @@ 'use strict'

class Pool extends EventEmitter {
constructor (origin, opts = {}) {
constructor (origin, { connections, ...options } = {}) {
super()
const { connections, ...options } = opts
if (connections != null && (!Number.isFinite(connections) || connections < 0)) {

@@ -65,11 +63,13 @@ throw new InvalidArgumentError('invalid connections')

if (pool[kNeedDrain] && !pool[kPending]) {
pool[kNeedDrain] = false
pool.emit('drain')
}
if (queue.isEmpty()) {
if (pool[kNeedDrain]) {
pool[kNeedDrain] = false
pool.emit('drain')
}
if (pool[kClosedResolve] && !pool[kPending]) {
Promise
.all(pool[kClients].map(c => c.close()))
.then(pool[kClosedResolve])
if (pool[kClosedResolve]) {
Promise
.all(pool[kClients].map(c => c.close()))
.then(pool[kClosedResolve])
}
}

@@ -76,0 +76,0 @@ }

{
"name": "undici",
"version": "3.3.0",
"version": "3.3.1",
"description": "An HTTP/1.1 client, written from scratch for Node.js",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -653,4 +653,2 @@ # undici

Requires: Node.js v14+
Returns a new Agent instance for use with pool based requests or the following top-level methods `request`, `pipeline`, and `stream`.

@@ -662,4 +660,2 @@

Requires: Node.js v14+
This method retrieves Pool instances from the Agent. If the pool does not exist it is automatically added. You do not need to manually close these pools as they are automatically removed using a WeakCache based on WeakRef and FinalizationRegistry.

@@ -669,2 +665,10 @@

#### `agent.close(): Promise`
Returns a `Promise.all` operation closing all of the pool instances in the Agent instance. This calls `pool.close` under the hood.
#### `agent.destroy(): Promise`
Returns a `Promise.all` operation destroying all of the pool instances in the Agent instance. This calls `pool.destroy` under the hood.
### `undici.setGlobalAgent(agent)`

@@ -679,4 +683,2 @@

Requires: Node.js v14+
The agent must only **implement** the `Agent` API; not necessary extend from it.

@@ -683,0 +685,0 @@

@@ -10,5 +10,3 @@ 'use strict'

const SKIP = typeof WeakRef === 'undefined' || typeof FinalizationRegistry === 'undefined'
tap.test('Agent', { skip: SKIP }, t => {
tap.test('Agent', t => {
t.plan(6)

@@ -15,0 +13,0 @@

@@ -10,2 +10,3 @@ 'use strict'

const EE = require('events')
const { kConnect } = require('../lib/core/symbols')

@@ -936,1 +937,27 @@ test('basic get', (t) => {

})
test('busy', (t) => {
t.plan(2)
const server = createServer((req, res) => {
req.pipe(res)
})
t.tearDown(server.close.bind(server))
server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`, {
pipelining: 1
})
t.tearDown(client.close.bind(client))
client[kConnect](() => {
client.request({
path: '/',
method: 'GET'
}, (err) => {
t.error(err)
})
t.strictEqual(client.busy, true)
})
})
})
SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc