Skip to content

Commit

Permalink
deps: update undici to 5.0.0
Browse files Browse the repository at this point in the history
PR-URL: #42583
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Robert Nagy <[email protected]>
Reviewed-By: Mohammed Keyvanzadeh <[email protected]>
Reviewed-By: Mestery <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Darshan Sen <[email protected]>
  • Loading branch information
nodejs-github-bot authored and Trott committed Apr 7, 2022
1 parent 85a65c3 commit c7ac11f
Show file tree
Hide file tree
Showing 20 changed files with 1,576 additions and 1,538 deletions.
54 changes: 9 additions & 45 deletions deps/undici/src/docs/api/MockAgent.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ const mockAgent = new MockAgent()
setGlobalDispatcher(mockAgent)

const mockPool = mockAgent.get('http://localhost:3000')

mockPool.intercept({
path: '/foo',
method: 'GET'
}).reply(200, 'foo')
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')

const { statusCode, body } = await request('http://localhost:3000/foo')

Expand All @@ -95,11 +91,7 @@ import { MockAgent, request } from 'undici'
const mockAgent = new MockAgent()

const mockPool = mockAgent.get('http://localhost:3000')

mockPool.intercept({
path: '/foo',
method: 'GET'
}).reply(200, 'foo')
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')

const {
statusCode,
Expand All @@ -121,11 +113,7 @@ import { MockAgent, request } from 'undici'
const mockAgent = new MockAgent()

const mockPool = mockAgent.get('http://localhost:3000')

mockPool.intercept({
path: '/foo',
method: 'GET'
}).reply(200, 'foo')
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')

const {
statusCode,
Expand All @@ -147,11 +135,7 @@ import { MockAgent, request } from 'undici'
const mockAgent = new MockAgent({ connections: 1 })

const mockClient = mockAgent.get('http://localhost:3000')

mockClient.intercept({
path: '/foo',
method: 'GET'
}).reply(200, 'foo')
mockClient.intercept({ path: '/foo' }).reply(200, 'foo')

const {
statusCode,
Expand All @@ -174,16 +158,8 @@ const mockAgent = new MockAgent()
setGlobalDispatcher(mockAgent)

const mockPool = mockAgent.get('http://localhost:3000')

mockPool.intercept({
path: '/foo',
method: 'GET'
}).reply(200, 'foo')

mockPool.intercept({
path: '/hello',
method: 'GET'
}).reply(200, 'hello')
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')
mockPool.intercept({ path: '/hello'}).reply(200, 'hello')

const result1 = await request('http://localhost:3000/foo')

Expand Down Expand Up @@ -250,11 +226,7 @@ const mockAgent = new MockAgent()
setGlobalDispatcher(mockAgent)

const mockPool = mockAgent.get(new RegExp('http://localhost:3000'))

mockPool.intercept({
path: '/foo',
method: 'GET',
}).reply(200, 'foo')
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')

const {
statusCode,
Expand All @@ -277,11 +249,7 @@ const mockAgent = new MockAgent()
setGlobalDispatcher(mockAgent)

const mockPool = mockAgent.get((origin) => origin === 'http://localhost:3000')

mockPool.intercept({
path: '/foo',
method: 'GET'
}).reply(200, 'foo')
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')

const {
statusCode,
Expand Down Expand Up @@ -328,11 +296,7 @@ import { MockAgent } from 'undici'
const mockAgent = new MockAgent()

const mockPool = mockAgent.get('http://localhost:3000')

mockPool.intercept({
path: '/foo',
method: 'GET'
}).reply(200, 'foo')
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')

const {
statusCode,
Expand Down
5 changes: 1 addition & 4 deletions deps/undici/src/docs/api/MockClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ import { MockAgent } from 'undici'
const mockAgent = new MockAgent({ connections: 1 })

const mockClient = mockAgent.get('http://localhost:3000')
mockClient.intercept({
path: '/foo',
method: 'GET',
}).reply(200, 'foo')
mockClient.intercept({ path: '/foo' }).reply(200, 'foo')

const {
statusCode,
Expand Down
6 changes: 1 addition & 5 deletions deps/undici/src/docs/api/MockPool.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ setGlobalDispatcher(mockAgent)

// MockPool
const mockPool = mockAgent.get('http://localhost:3000')

mockPool.intercept({
path: '/foo',
method: 'GET',
}).reply(200, 'foo')
mockPool.intercept({ path: '/foo' }).reply(200, 'foo')

const {
statusCode,
Expand Down
128 changes: 30 additions & 98 deletions deps/undici/src/lib/agent.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
'use strict'

const {
ClientClosedError,
InvalidArgumentError,
ClientDestroyedError
} = require('./core/errors')
const { kClients, kRunning } = require('./core/symbols')
const Dispatcher = require('./dispatcher')
const { InvalidArgumentError } = require('./core/errors')
const { kClients, kRunning, kClose, kDestroy, kDispatch } = require('./core/symbols')
const DispatcherBase = require('./dispatcher-base')
const Pool = require('./pool')
const Client = require('./client')
const util = require('./core/util')
const RedirectHandler = require('./handler/redirect')
const { WeakRef, FinalizationRegistry } = require('./compat/dispatcher-weakref')()

const kDestroyed = Symbol('destroyed')
const kClosed = Symbol('closed')
const kOnConnect = Symbol('onConnect')
const kOnDisconnect = Symbol('onDisconnect')
const kOnConnectionError = Symbol('onConnectionError')
Expand All @@ -30,7 +24,7 @@ function defaultFactory (origin, opts) {
: new Pool(origin, opts)
}

class Agent extends Dispatcher {
class Agent extends DispatcherBase {
constructor ({ factory = defaultFactory, maxRedirections = 0, connect, ...options } = {}) {
super()

Expand Down Expand Up @@ -60,8 +54,6 @@ class Agent extends Dispatcher {
this[kClients].delete(key)
}
})
this[kClosed] = false
this[kDestroyed] = false

const agent = this

Expand Down Expand Up @@ -94,76 +86,38 @@ class Agent extends Dispatcher {
return ret
}

dispatch (opts, handler) {
if (!handler || typeof handler !== 'object') {
throw new InvalidArgumentError('handler must be an object.')
[kDispatch] (opts, handler) {
let key
if (opts.origin && (typeof opts.origin === 'string' || opts.origin instanceof URL)) {
key = String(opts.origin)
} else {
throw new InvalidArgumentError('opts.origin must be a non-empty string or URL.')
}

try {
if (!opts || typeof opts !== 'object') {
throw new InvalidArgumentError('opts must be an object.')
}

let key
if (opts.origin && (typeof opts.origin === 'string' || opts.origin instanceof URL)) {
key = String(opts.origin)
} else {
throw new InvalidArgumentError('opts.origin must be a non-empty string or URL.')
}

if (this[kDestroyed]) {
throw new ClientDestroyedError()
}

if (this[kClosed]) {
throw new ClientClosedError()
}
const ref = this[kClients].get(key)

const ref = this[kClients].get(key)

let dispatcher = ref ? ref.deref() : null
if (!dispatcher) {
dispatcher = this[kFactory](opts.origin, this[kOptions])
.on('drain', this[kOnDrain])
.on('connect', this[kOnConnect])
.on('disconnect', this[kOnDisconnect])
.on('connectionError', this[kOnConnectionError])

this[kClients].set(key, new WeakRef(dispatcher))
this[kFinalizer].register(dispatcher, key)
}

const { maxRedirections = this[kMaxRedirections] } = opts
if (maxRedirections != null && maxRedirections !== 0) {
opts = { ...opts, maxRedirections: 0 } // Stop sub dispatcher from also redirecting.
handler = new RedirectHandler(this, maxRedirections, opts, handler)
}

return dispatcher.dispatch(opts, handler)
} catch (err) {
if (typeof handler.onError !== 'function') {
throw new InvalidArgumentError('invalid onError method')
}
let dispatcher = ref ? ref.deref() : null
if (!dispatcher) {
dispatcher = this[kFactory](opts.origin, this[kOptions])
.on('drain', this[kOnDrain])
.on('connect', this[kOnConnect])
.on('disconnect', this[kOnDisconnect])
.on('connectionError', this[kOnConnectionError])

handler.onError(err)
this[kClients].set(key, new WeakRef(dispatcher))
this[kFinalizer].register(dispatcher, key)
}
}

get closed () {
return this[kClosed]
}

get destroyed () {
return this[kDestroyed]
}

close (callback) {
if (callback != null && typeof callback !== 'function') {
throw new InvalidArgumentError('callback must be a function')
const { maxRedirections = this[kMaxRedirections] } = opts
if (maxRedirections != null && maxRedirections !== 0) {
opts = { ...opts, maxRedirections: 0 } // Stop sub dispatcher from also redirecting.
handler = new RedirectHandler(this, maxRedirections, opts, handler)
}

this[kClosed] = true
return dispatcher.dispatch(opts, handler)
}

async [kClose] () {
const closePromises = []
for (const ref of this[kClients].values()) {
const client = ref.deref()
Expand All @@ -173,27 +127,10 @@ class Agent extends Dispatcher {
}
}

if (!callback) {
return Promise.all(closePromises)
}

// Should never error.
Promise.all(closePromises).then(() => process.nextTick(callback))
await Promise.all(closePromises)
}

destroy (err, callback) {
if (typeof err === 'function') {
callback = err
err = null
}

if (callback != null && typeof callback !== 'function') {
throw new InvalidArgumentError('callback must be a function')
}

this[kClosed] = true
this[kDestroyed] = true

async [kDestroy] (err) {
const destroyPromises = []
for (const ref of this[kClients].values()) {
const client = ref.deref()
Expand All @@ -203,12 +140,7 @@ class Agent extends Dispatcher {
}
}

if (!callback) {
return Promise.all(destroyPromises)
}

// Should never error.
Promise.all(destroyPromises).then(() => process.nextTick(callback))
await Promise.all(destroyPromises)
}
}

Expand Down
Loading

0 comments on commit c7ac11f

Please sign in to comment.