Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

undici

Package Overview
Dependencies
Maintainers
2
Versions
220
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.1.0 to 4.1.1

123

docs/api/Client.md

@@ -38,3 +38,3 @@ # Class: Client

'use strict'
const { Client } = require('undici')
import { Client } from 'undici'

@@ -124,31 +124,32 @@ const client = new Client('http://localhost:3000')

```js
'use strict'
const { createServer } = require('http')
const { Client } = require('undici')
import { createServer } from 'http'
import { Client } from 'undici'
import { once } from 'events'
const server = createServer((request, response) => {
response.end('Hello, World!')
})
}).listen()
server.listen(() => {
const client = new Client(`http://localhost:${server.address().port}`)
await once(server, 'listening')
client.on('connect', (origin) => {
console.log(`Connected to ${origin}`) // should print before the request body statement
})
const client = new Client(`http://localhost:${server.address().port}`)
client.request({
client.on('connect', (origin) => {
console.log(`Connected to ${origin}`) // should print before the request body statement
})
try {
const { body } = await client.request({
path: '/',
method: 'GET',
}).then(({ body }) => {
body.setEncoding('utf8')
body.on('data', console.log)
client.close()
server.close()
}).catch(error => {
console.error(error)
client.close()
server.close()
method: 'GET'
})
})
body.setEncoding('utf-8')
body.on('data', console.log)
client.close()
server.close()
} catch (error) {
console.error(error)
client.close()
server.close()
}
```

@@ -171,26 +172,28 @@

```js
'use strict'
const { createServer } = require('http')
const { Client } = require('undici')
import { createServer } from 'http'
import { Client } from 'undici'
import { once } from 'events'
const server = createServer((request, response) => {
response.destroy()
})
}).listen()
server.listen(() => {
const client = new Client(`http://localhost:${server.address().port}`)
await once(server, 'listening')
client.on('disconnect', (origin) => {
console.log(`Disconnected from ${origin}`) // should print before the SocketError
})
const client = new Client(`http://localhost:${server.address().port}`)
client.request({
client.on('disconnect', (origin) => {
console.log(`Disconnected from ${origin}`)
})
try {
await client.request({
path: '/',
method: 'GET',
}).catch(error => {
console.error(error.message)
client.close()
server.close()
method: 'GET'
})
})
} catch (error) {
console.error(error.message)
client.close()
server.close()
}
```

@@ -207,32 +210,32 @@

```js
'use strict'
const { createServer } = require('http')
const { Client } = require('undici')
import { createServer } from 'http'
import { Client } from 'undici'
import { once } from 'events'
const server = createServer((request, response) => {
response.end('Hello, World!')
}).listen()
await once(server, 'listening')
const client = new Client(`http://localhost:${server.address().port}`)
client.on('drain', () => {
console.log('drain event')
console.log(`Is Client busy: ${client.busy}`)
client.close()
server.close()
})
server.listen(() => {
const client = new Client(`http://localhost:${server.address().port}`)
const requests = [
client.request({ path: '/', method: 'GET' }),
client.request({ path: '/', method: 'GET' }),
client.request({ path: '/', method: 'GET' })
]
client.on('drain', () => {
console.log('drain event')
console.log(`Is Client busy: ${client.busy}`)
client.close()
server.close()
})
console.log(`Is Client busy: ${client.busy}`)
const requests = [
client.request({ path: '/', method: 'GET' }),
client.request({ path: '/', method: 'GET' }),
client.request({ path: '/', method: 'GET' })
]
await Promise.all(requests)
console.log(`Is Client busy: ${client.busy}`)
Promise.all(requests).then(() => {
console.log('requests completed')
})
})
console.log('requests completed')
```

@@ -239,0 +242,0 @@

@@ -29,30 +29,27 @@ # Dispatcher

```js
'use strict'
const { createServer } = require('http')
const { Client } = require('undici')
import { createServer } from 'http'
import { Client } from 'undici'
import { once } from 'events'
const server = createServer((request, response) => {
response.end('undici')
})
}).listen()
server.listen(() => {
const client = new Client(`http://localhost:${server.address().port}`)
await once(server, 'listening')
const request = client.request({
path: '/',
method: 'GET'
const client = new Client(`http://localhost:${server.address().port}`)
try {
const { body } = await client.request({
path: '/',
method: 'GET'
})
body.setEncoding('utf8')
body.on('data', console.log)
} catch (error) {}
client.close()
.then(() => {
// This waits for the previous request to complete
console.log('Client closed')
server.close()
})
await client.close()
request.then(({ body }) => {
body.setEncoding('utf8')
body.on('data', console.log) // This logs before 'Client closed'
})
})
console.log('Client closed')
server.close()
```

@@ -88,9 +85,9 @@

```js
'use strict'
const { createServer } = require('http')
const { Client } = require('undici')
import { createServer } from 'http'
import { Client } from 'undici'
import { once } from 'events'
const server = createServer((request, response) => {
throw Error('should never get here')
})
}).listen()

@@ -110,20 +107,21 @@ server.on('connect', (req, socket, head) => {

server.listen(() => {
const client = new Client(`http://localhost:${server.address().port}`)
await once(server, 'listening')
client
.connect({ path: '/' })
.then(({ socket }) => {
const wanted = 'Body'
let data = ''
socket.on('data', d => { data += d })
socket.on('end', () => {
console.log(`Data received: ${data.toString()} | Data wanted: ${wanted}`)
client.close()
server.close()
})
socket.write(wanted)
socket.end()
})
})
const client = new Client(`http://localhost:${server.address().port}`)
try {
const { socket } = await client.connect({
path: '/'
})
const wanted = 'Body'
let data = ''
socket.on('data', d => { data += d })
socket.on('end', () => {
console.log(`Data received: ${data.toString()} | Data wanted: ${wanted}`)
client.close()
server.close()
})
socket.write(wanted)
socket.end()
} catch (error) { }
```

@@ -154,13 +152,15 @@

```js
'use strict'
const { createServer } = require('http')
const { Client } = require('undici')
import { createServer } from 'http'
import { Client } from 'undici'
import { once } from 'events'
const server = createServer((request, response) => {
response.end('undici')
})
response.end()
}).listen()
server.listen(() => {
const client = new Client(`http://localhost:${server.address().port}`)
await once(server, 'listening')
const client = new Client(`http://localhost:${server.address().port}`)
try {
const request = client.request({

@@ -170,15 +170,11 @@ path: '/',

})
client.destroy()
.then(() => {
// Still waits for requests to complete
console.log('Client destroyed')
server.close()
})
// The request promise will reject with an Undici Client Destroyed error
request.catch(error => {
console.error(error)
})
})
await request
} catch (error) {
console.error(error)
}
```

@@ -219,42 +215,43 @@

```js
'use strict'
const { createServer } = require('http')
const { Client } = require('undici')
import { createServer } from 'http'
import { Client } from 'undici'
import { once } from 'events'
const server = createServer((request, response) => {
response.end('Hello, World!')
})
server.listen(() => {
const client = new Client(`http://localhost:${server.address().port}`)
}).listen()
const data = []
await once(server, 'listening')
client.dispatch({
path: '/',
method: 'GET',
headers: {
'x-foo': 'bar'
}
}, {
onConnect: () => {
console.log('Connected!')
},
onError: (error) => {
console.error(error)
},
onHeaders: (statusCode, headers) => {
console.log(`onHeaders | statusCode: ${statusCode} | headers: ${headers}`)
},
onData: (chunk) => {
console.log('onData : chunk received')
data.push(chunk)
},
onComplete: (trailers) => {
console.log(`onComplete | trailers: ${trailers}`)
const res = Buffer.concat(data).toString('utf8')
console.log(`Data: ${res}`)
client.close()
server.close()
}
})
const client = new Client(`http://localhost:${server.address().port}`)
const data = []
client.dispatch({
path: '/',
method: 'GET',
headers: {
'x-foo': 'bar'
}
}, {
onConnect: () => {
console.log('Connected!')
},
onError: (error) => {
console.error(error)
},
onHeaders: (statusCode, headers) => {
console.log(`onHeaders | statusCode: ${statusCode} | headers: ${headers}`)
},
onData: (chunk) => {
console.log('onData: chunk received')
data.push(chunk)
},
onComplete: (trailers) => {
console.log(`onComplete | trailers: ${trailers}`)
const res = Buffer.concat(data).toString('utf8')
console.log(`Data: ${res}`)
client.close()
server.close()
}
})

@@ -266,10 +263,12 @@ ```

```js
'use strict'
const { createServer } = require('http')
const { Client } = require('undici')
import { createServer } from 'http'
import { Client } from 'undici'
import { once } from 'events'
const server = createServer((request, response) => {
response.end()
})
}).listen()
await once(server, 'listening')
server.on('upgrade', (request, socket, head) => {

@@ -284,29 +283,27 @@ console.log('Node.js Server - upgrade event')

server.listen(() => {
const client = new Client(`http://localhost:${server.address().port}`)
const client = new Client(`http://localhost:${server.address().port}`)
client.dispatch({
path: '/',
method: 'GET',
upgrade: 'websocket'
}, {
onConnect: () => {
console.log('Undici Client - onConnect')
},
onError: (error) => {
console.log('onError') // shouldn't print
},
onUpgrade: (statusCode, headers, socket) => {
console.log('Undici Client - onUpgrade')
console.log(`onUpgrade Headers: ${headers}`)
socket.on('data', buffer => {
console.log(buffer.toString('utf8'))
})
socket.on('end', () => {
client.close()
server.close()
})
socket.end()
}
})
client.dispatch({
path: '/',
method: 'GET',
upgrade: 'websocket'
}, {
onConnect: () => {
console.log('Undici Client - onConnect')
},
onError: (error) => {
console.log('onError') // shouldn't print
},
onUpgrade: (statusCode, headers, socket) => {
console.log('Undici Client - onUpgrade')
console.log(`onUpgrade Headers: ${headers}`)
socket.on('data', buffer => {
console.log(buffer.toString('utf8'))
})
socket.on('end', () => {
client.close()
server.close()
})
socket.end()
}
})

@@ -343,52 +340,51 @@ ```

```js
'use strict'
const { Readable, Writable, PassThrough, pipeline } = require('stream')
const { createServer } = require('http')
const { Client } = require('undici')
import { Readable, Writable, PassThrough, pipeline } from 'stream'
import { createServer } from 'http'
import { Client } from 'undici'
import { once } from 'events'
const server = createServer((request, response) => {
request.pipe(response)
})
}).listen()
server.listen(() => {
const client = new Client(`http://localhost:${server.address().port}`)
await once(server, 'listening')
let res = ''
const client = new Client(`http://localhost:${server.address().port}`)
pipeline(
new Readable({
read () {
this.push(Buffer.from('undici'))
this.push(null)
}
}),
client.pipeline({
path: '/',
method: 'GET'
}, ({ statusCode, headers, body }) => {
console.log(`response received ${statusCode}`)
console.log('headers', headers)
return pipeline(body, new PassThrough(), () => {})
}),
new Writable({
write (chunk, _, callback) {
res += chunk.toString()
callback()
},
final (callback) {
console.log(`Response pipelined to writable: ${res}`)
callback()
}
}),
error => {
if (error) {
console.error(error)
}
let res = ''
client.close()
server.close()
pipeline(
new Readable({
read () {
this.push(Buffer.from('undici'))
this.push(null)
}
)
})
}),
client.pipeline({
path: '/',
method: 'GET'
}, ({ statusCode, headers, body }) => {
console.log(`response received ${statusCode}`)
console.log('headers', headers)
return pipeline(body, new PassThrough(), () => {})
}),
new Writable({
write (chunk, _, callback) {
res += chunk.toString()
callback()
},
final (callback) {
console.log(`Response pipelined to writable: ${res}`)
callback()
}
}),
error => {
if (error) {
console.error(error)
}
client.close()
server.close()
}
)
```

@@ -439,31 +435,32 @@

```js
'use strict'
const { createServer } = require('http')
const { Client } = require('undici')
import { createServer } from 'http'
import { Client } from 'undici'
import { once } from 'events'
const server = createServer((request, response) => {
response.end('Hello, World!')
})
}).listen()
server.listen(() => {
const client = new Client(`http://localhost:${server.address().port}`)
await once(server, 'listening')
client.request({
const client = new Client(`http://localhost:${server.address().port}`)
try {
const { body, headers, statusCode, trailers } = await client.request({
path: '/',
method: 'GET'
}).then(({ body, headers, statusCode, trailers }) => {
console.log(`response received ${statusCode}`)
console.log('headers', headers)
body.setEncoding('utf8')
body.on('data', console.log)
body.on('end', () => {
console.log('trailers', trailers)
})
})
console.log(`response received ${statusCode}`)
console.log('headers', headers)
body.setEncoding('utf8')
body.on('data', console.log)
body.on('end', () => {
console.log('trailers', trailers)
})
client.close()
server.close()
}).catch(error => {
console.error(error)
})
})
client.close()
server.close()
} catch (error) {
console.error(error)
}
```

@@ -476,14 +473,16 @@

```js
'use strict'
const { createServer } = require('http')
const { Client } = require('undici')
import { createServer } from 'http'
import { Client } from 'undici'
import { once } from 'events'
const server = createServer((request, response) => {
response.end('Hello, World!')
})
}).listen()
server.listen(() => {
const client = new Client(`http://localhost:${server.address().port}`)
const abortController = new AbortController()
await once(server, 'listening')
const client = new Client(`http://localhost:${server.address().port}`)
const abortController = new AbortController()
try {
client.request({

@@ -493,11 +492,10 @@ path: '/',

signal: abortController.signal
}).catch(error => {
console.error(error) // should print an RequestAbortedError
client.close()
server.close()
})
} catch (error) {
console.error(error) // should print an RequestAbortedError
client.close()
server.close()
}
abortController.abort()
})
abortController.abort()
```

@@ -508,15 +506,16 @@

```js
'use strict'
const EventEmitter = require('events')
const { createServer } = require('http')
const { Client } = require('undici')
import { createServer } from 'http'
import { Client } from 'undici'
import EventEmitter, { once } from 'events'
const server = createServer((request, response) => {
response.end('Hello, World!')
})
}).listen()
server.listen(() => {
const client = new Client(`http://localhost:${server.address().port}`)
const ee = new EventEmitter()
await once(server, 'listening')
const client = new Client(`http://localhost:${server.address().port}`)
const ee = new EventEmitter()
try {
client.request({

@@ -526,10 +525,10 @@ path: '/',

signal: ee
}).catch(error => {
console.error(error) // should print an RequestAbortedError
client.close()
server.close()
})
} catch (error) {
console.error(error) // should print an RequestAbortedError
client.close()
server.close()
}
ee.emit('abort')
})
ee.emit('abort')
```

@@ -540,24 +539,25 @@

```js
'use strict'
const { createServer } = require('http')
const { Client } = require('undici')
import { createServer } from 'http'
import { Client } from 'undici'
import { once } from 'events'
const server = createServer((request, response) => {
response.end('Hello, World!')
})
}).listen()
server.listen(() => {
const client = new Client(`http://localhost:${server.address().port}`)
await once(server, 'listening')
client.request({
const client = new Client(`http://localhost:${server.address().port}`)
try {
const { body } = await client.request({
path: '/',
method: 'GET',
}).then(({ body }) => {
body.destroy()
}).catch(error => {
console.error(error) // should print an RequestAbortedError
client.close()
server.close()
method: 'GET'
})
})
body.destroy()
} catch (error) {
console.error(error) // should print an RequestAbortedError
client.close()
server.close()
}
```

@@ -594,17 +594,19 @@

```js
'use strict'
const { createServer } = require('http')
const { Client } = require('undici')
const { Writable } = require('stream')
import { createServer } from 'http'
import { Client } from 'undici'
import { once } from 'events'
import { Writable } from 'stream'
const server = createServer((request, response) => {
response.end('Hello, World!')
})
}).listen()
server.listen(() => {
const client = new Client(`http://localhost:${server.address().port}`)
await once(server, 'listening')
const bufs = []
const client = new Client(`http://localhost:${server.address().port}`)
client.stream({
const bufs = []
try {
await client.stream({
path: '/',

@@ -622,11 +624,11 @@ method: 'GET',

})
}).then(({ opaque: { bufs } }) => {
console.log(Buffer.concat(bufs).toString('utf-8'))
})
client.close()
server.close()
}).catch(error => {
console.error(error)
})
})
console.log(Buffer.concat(bufs).toString('utf-8'))
client.close()
server.close()
} catch (error) {
console.error(error)
}
```

@@ -639,52 +641,52 @@

```js
'use strict'
import { createServer } from 'http'
import { Client } from 'undici'
import { once } from 'events'
import fastify from 'fastify'
const { createServer } = require('http')
const undici = require('undici')
const fastify = require('fastify')
const nodeServer = createServer((request, response) => {
response.end('Hello, World! From Node.js HTTP Server')
}).listen()
await once(nodeServer, 'listening')
console.log('Node Server listening')
const nodeServerUndiciClient = new Client(`http://localhost:${nodeServer.address().port}`)
const fastifyServer = fastify()
fastifyServer.route({
url: '/',
method: 'GET',
handler: (request, response) => {
nodeServerUndiciClient.stream({
path: '/',
method: 'GET',
opaque: response
}, ({ opaque }) => opaque.raw)
}
})
nodeServer.listen(() => {
console.log('Node Server listening')
await fastifyServer.listen()
const nodeServerUndiciClient = new undici.Client(`http://localhost:${nodeServer.address().port}`)
console.log('Fastify Server listening')
const fastifyServer = fastify()
const fastifyServerUndiciClient = new Client(`http://localhost:${fastifyServer.server.address().port}`)
fastifyServer.route({
url: '/',
method: 'GET',
handler: (request, response) => {
nodeServerUndiciClient.stream({
path: '/',
method: 'GET',
opaque: response
}, ({ opaque }) => opaque.raw)
}
try {
const { statusCode, body } = await fastifyServerUndiciClient.request({
path: '/',
method: 'GET'
})
console.log(`response received ${statusCode}`)
body.setEncoding('utf8')
body.on('data', console.log)
fastifyServer
.listen()
.then(() => {
console.log('Fastify Server listening')
const fastifyServerUndiciClient = new undici.Client(`http://localhost:${fastifyServer.server.address().port}`)
fastifyServerUndiciClient.request({
path: '/',
method: 'GET'
}).then(({ statusCode, body }) => {
console.log(`response received ${statusCode}`)
body.setEncoding('utf8')
body.on('data', console.log)
nodeServerUndiciClient.close()
fastifyServerUndiciClient.close()
fastifyServer.close()
nodeServer.close()
})
})
})
nodeServerUndiciClient.close()
fastifyServerUndiciClient.close()
fastifyServer.close()
nodeServer.close()
} catch (error) { }
```

@@ -721,5 +723,5 @@

```js
'use strict'
const { Client } = require('undici')
const { createServer } = require('http')
import { createServer } from 'http'
import { Client } from 'undici'
import { once } from 'events'

@@ -731,23 +733,23 @@ const server = createServer((request, response) => {

response.end()
})
}).listen()
server.listen(() => {
const client = new Client(`http://localhost:${server.address().port}`)
await once(server, 'listening')
client
.upgrade({ path: '/' })
.then(({ headers, socket }) => {
socket.on('end', () => {
console.log(`upgrade: ${headers.upgrade}`)
client.close()
server.close()
})
socket.end()
})
.catch(error => {
console.error(error)
client.close()
server.close()
})
})
const client = new Client(`http://localhost:${server.address().port}`)
try {
const { headers, socket } = await client.upgrade({
path: '/',
})
socket.on('end', () => {
console.log(`upgrade: ${headers.upgrade}`) // upgrade: Websocket
client.close()
server.close()
})
socket.end()
} catch (error) {
console.error(error)
client.close()
server.close()
}
```

@@ -754,0 +756,0 @@

@@ -7,3 +7,3 @@ # Errors

```js
const { errors } = require('undici')
import { errors } from 'undici'
```

@@ -10,0 +10,0 @@

@@ -26,4 +26,3 @@ # Class: MockAgent

```js
'use strict'
const { MockAgent } = require('undici')
import { MockAgent } from 'undici'

@@ -36,4 +35,3 @@ const mockAgent = new MockAgent()

```js
'use strict'
const { Agent, MockAgent } = require('undici')
import { Agent, MockAgent } from 'undici'

@@ -73,4 +71,3 @@ const agent = new Agent()

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -84,14 +81,11 @@ const mockAgent = new MockAgent()

path: '/foo',
method: 'GET',
method: 'GET'
}).reply(200, 'foo')
const {
statusCode,
body
} = await request('http://localhost:3000/foo')
const { statusCode, body } = await request('http://localhost:3000/foo')
console.log('response received', statusCode) // 200
console.log('response received', statusCode) // response received 200
for await (const data of body) {
console.log('data', data) // 'foo'
console.log('data', data.toString('utf8')) // data foo
}

@@ -103,4 +97,3 @@ ```

```js
'use strict'
const { MockAgent, request } = require('undici')
import { MockAgent, request } from 'undici'

@@ -110,5 +103,6 @@ const mockAgent = new MockAgent()

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

@@ -121,6 +115,6 @@

console.log('response received', statusCode) // 200
console.log('response received', statusCode) // response received 200
for await (const data of body) {
console.log('data', data) // 'foo'
console.log('data', data.toString('utf8')) // data foo
}

@@ -132,4 +126,3 @@ ```

```js
'use strict'
const { MockAgent, request } = require('undici')
import { MockAgent, request } from 'undici'

@@ -139,5 +132,6 @@ const mockAgent = new MockAgent()

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

@@ -150,6 +144,6 @@

console.log('response received', statusCode) // 200
console.log('response received', statusCode) // response received 200
for await (const data of body) {
console.log('data', data) // 'foo'
console.log('data', data.toString('utf8')) // data foo
}

@@ -161,4 +155,3 @@ ```

```js
'use strict'
const { MockAgent, request } = require('undici')
import { MockAgent, request } from 'undici'

@@ -168,5 +161,6 @@ const mockAgent = new MockAgent({ connections: 1 })

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

@@ -179,6 +173,6 @@

console.log('response received', statusCode) // 200
console.log('response received', statusCode) // response received 200
for await (const data of body) {
console.log('data', data) // 'foo'
console.log('data', data.toString('utf8')) // data foo
}

@@ -190,4 +184,3 @@ ```

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -201,3 +194,3 @@ const mockAgent = new MockAgent()

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

@@ -207,9 +200,20 @@

path: '/hello',
method: 'GET',
method: 'GET'
}).reply(200, 'hello')
const result1 = await request('http://localhost:3000/foo')
console.log('response received', result1.statusCode) // response received 200
for await (const data of result1.body) {
console.log('data', data.toString('utf8')) // data foo
}
const result2 = await request('http://localhost:3000/hello')
console.log('response received', result2.statusCode) // response received 200
for await (const data of result2.body) {
console.log('data', data.toString('utf8')) // data hello
}
```

@@ -220,4 +224,3 @@

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -228,2 +231,3 @@ const mockAgent = new MockAgent()

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

@@ -244,14 +248,14 @@ path: '/foo?hello=there&see=ya',

} = await request('http://localhost:3000/foo?hello=there&see=ya', {
method: 'POST',
body: 'form1=data1&form2=data2'
})
method: 'POST',
body: 'form1=data1&form2=data2'
})
console.log('response received', statusCode) // 200
console.log('headers', headers) // {"content-type":"application/json"}
console.log('response received', statusCode) // response received 200
console.log('headers', headers) // { 'content-type': 'application/json' }
for await (const data of body) {
console.log('data', data) // '{"foo":"bar"}'
console.log('data', data.toString('utf8')) // '{"foo":"bar"}'
}
console.log('trailers', trailers) // {"Content-MD5":"test"}
console.log('trailers', trailers) // { 'content-md5': 'test' }
```

@@ -262,4 +266,3 @@

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -270,2 +273,3 @@ const mockAgent = new MockAgent()

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

@@ -281,6 +285,6 @@ path: '/foo',

console.log('response received', statusCode) // 200
console.log('response received', statusCode) // response received 200
for await (const data of body) {
console.log('data', data) // 'foo'
console.log('data', data.toString('utf8')) // data foo
}

@@ -292,4 +296,3 @@ ```

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -300,2 +303,3 @@ const mockAgent = new MockAgent()

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

@@ -311,6 +315,6 @@ path: '/foo',

console.log('response received', statusCode) // 200
console.log('response received', statusCode) // response received 200
for await (const data of body) {
console.log('data', data) // 'foo'
console.log('data', data.toString('utf8')) // data foo
}

@@ -328,4 +332,3 @@ ```

```js
'use strict'
const { MockAgent, setGlobalDispatcher } = require('undici')
import { MockAgent, setGlobalDispatcher } from 'undici'

@@ -349,4 +352,3 @@ const mockAgent = new MockAgent()

```js
'use strict'
const { MockAgent } = require('undici')
import { MockAgent } from 'undici'

@@ -356,2 +358,3 @@ const mockAgent = new MockAgent()

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

@@ -370,2 +373,8 @@ path: '/foo',

})
console.log('response received', statusCode) // response received 200
for await (const data of body) {
console.log('data', data.toString('utf8')) // data foo
}
```

@@ -382,4 +391,3 @@

```js
'use strict'
const { MockAgent, setGlobalDispatcher } = require('undici')
import { MockAgent, setGlobalDispatcher } from 'undici'

@@ -401,4 +409,3 @@ const mockAgent = new MockAgent()

```js
'use strict'
const { MockAgent, setGlobalDispatcher } = require('undici')
import { MockAgent, setGlobalDispatcher } from 'undici'

@@ -430,4 +437,3 @@ const mockAgent = new MockAgent()

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -446,4 +452,3 @@ const mockAgent = new MockAgent()

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -469,4 +474,3 @@ const mockAgent = new MockAgent()

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -485,4 +489,3 @@ const mockAgent = new MockAgent()

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -507,4 +510,3 @@ const mockAgent = new MockAgent()

```js
'use strict'
const { MockAgent, request } = require('undici')
import { MockAgent, request } from 'undici'

@@ -511,0 +513,0 @@ const mockAgent = new MockAgent()

@@ -27,4 +27,3 @@ # Class: MockClient

```js
'use strict'
const { MockAgent } = require('undici')
import { MockAgent } from 'undici'

@@ -58,4 +57,3 @@ // Connections must be set to 1 to return a MockClient instance

```js
'use strict'
const { MockAgent } = require('undici')
import { MockAgent } from 'undici'

@@ -78,2 +76,8 @@ const mockAgent = new MockAgent({ connections: 1 })

})
console.log('response received', statusCode) // response received 200
for await (const data of body) {
console.log('data', data.toString('utf8')) // data foo
}
```

@@ -7,3 +7,3 @@ # MockErrors

```js
const { mockErrors } = require('undici')
import { mockErrors } from 'undici'
```

@@ -10,0 +10,0 @@

@@ -27,4 +27,3 @@ # Class: MockPool

```js
'use strict'
const { MockAgent } = require('undici')
import { MockAgent } from 'undici'

@@ -91,4 +90,3 @@ const mockAgent = new MockAgent()

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -111,6 +109,6 @@ const mockAgent = new MockAgent()

console.log('response received', statusCode) // 200
console.log('response received', statusCode) // response received 200
for await (const data of body) {
console.log('data', data) // 'foo'
console.log('data', data.toString('utf8')) // data foo
}

@@ -122,4 +120,3 @@ ```

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -143,3 +140,15 @@ const mockAgent = new MockAgent()

console.log('response received', result1.statusCode) // response received 200
for await (const data of result1.body) {
console.log('data', data.toString('utf8')) // data foo
}
const result2 = await request('http://localhost:3000/hello')
console.log('response received', result2.statusCode) // response received 200
for await (const data of result2.body) {
console.log('data', data.toString('utf8')) // data hello
}
```

@@ -150,4 +159,3 @@

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -158,2 +166,3 @@ const mockAgent = new MockAgent()

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

@@ -187,10 +196,10 @@ path: '/foo?hello=there&see=ya',

console.log('response received', statusCode) // 200
console.log('headers', headers) // {"content-type":"application/json"}
console.log('response received', statusCode) // response received 200
console.log('headers', headers) // { 'content-type': 'application/json' }
for await (const data of body) {
console.log('data', data) // '{"foo":"bar"}'
console.log('data', data.toString('utf8')) // '{"foo":"bar"}'
}
console.log('trailers', trailers) // {"Content-MD5":"test"}
console.log('trailers', trailers) // { 'content-md5': 'test' }
```

@@ -201,4 +210,3 @@

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -220,3 +228,6 @@ const mockAgent = new MockAgent()

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

@@ -230,3 +241,8 @@ body: 'form=data',

})
// Will match and return mocked data
console.log('response received', statusCode) // response received 200
for await (const data of body) {
console.log('data', data.toString('utf8')) // data foo
}
```

@@ -237,4 +253,3 @@

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -251,6 +266,9 @@ const mockAgent = new MockAgent()

await request('http://localhost:3000/foo', {
method: 'GET'
})
// Will throw new Error('kaboom')
try {
await request('http://localhost:3000/foo', {
method: 'GET'
})
} catch (error) {
console.error(error) // Error: kaboom
}
```

@@ -261,4 +279,3 @@

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -277,3 +294,4 @@ const mockAgent = new MockAgent()

const { headers } = await request('http://localhost:3000/foo')
// headers: {"foo":"bar"}
console.log('headers', headers) // headers { foo: 'bar' }
```

@@ -284,4 +302,3 @@

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -299,4 +316,5 @@ const mockAgent = new MockAgent()

const { trailers } = await request('http://localhost:3000/foo')
// trailers: {"foo":"bar"}
const { trailers } = await request('http://localhost:3000/foo')
console.log('trailers', trailers) // trailers { foo: 'bar' }
```

@@ -307,4 +325,3 @@

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -322,3 +339,4 @@ const mockAgent = new MockAgent()

const { headers } = await request('http://localhost:3000/foo')
// headers: {"content-length":"3"}
console.log('headers', headers) // headers { 'content-length': '3' }
```

@@ -329,4 +347,3 @@

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -344,3 +361,4 @@ const mockAgent = new MockAgent()

const { headers } = await request('http://localhost:3000/foo')
// headers: {"content-length":"13"}
console.log('headers', headers) // headers { 'content-length': '13' }
```

@@ -351,4 +369,3 @@

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -377,4 +394,3 @@ const mockAgent = new MockAgent()

```js
'use strict'
const { MockAgent, setGlobalDispatcher, request } = require('undici')
import { MockAgent, setGlobalDispatcher, request } from 'undici'

@@ -410,4 +426,3 @@ const mockAgent = new MockAgent()

```js
'use strict'
const { MockAgent } = require('undici')
import { MockAgent } from 'undici'

@@ -431,4 +446,3 @@ const mockAgent = new MockAgent()

```js
'use strict'
const { MockAgent } = require('undici')
import { MockAgent } from 'undici'

@@ -451,2 +465,8 @@ const mockAgent = new MockAgent()

})
console.log('response received', statusCode) // response received 200
for await (const data of body) {
console.log('data', data.toString('utf8')) // data foo
}
```

@@ -1419,3 +1419,3 @@ 'use strict'

if (contentLength === 0) {
socket.write(`${header}content-length: ${contentLength}\r\n\r\n\r\n`, 'ascii')
socket.write(`${header}content-length: 0\r\n\r\n`, 'ascii')
} else {

@@ -1431,3 +1431,2 @@ assert(contentLength === null, 'no body must not have content length')

socket.write(body)
socket.write('\r\n', 'ascii')
socket.uncork()

@@ -1564,3 +1563,3 @@

socket.write(`${header}content-length: 0\r\n\r\n\r\n`, 'ascii')
socket.write(`${header}content-length: 0\r\n\r\n`, 'ascii')
} else {

@@ -1686,3 +1685,3 @@ socket.write(`${header}\r\n`, 'ascii')

socket.write(`${header}content-length: 0\r\n\r\n\r\n`, 'ascii')
socket.write(`${header}content-length: 0\r\n\r\n`, 'ascii')
} else {

@@ -1689,0 +1688,0 @@ socket.write(`${header}\r\n`, 'ascii')

@@ -61,4 +61,3 @@ 'use strict'

.on('error', function (err) {
assert(this.servername)
if (err.code !== 'UND_ERR_INFO') {
if (this.servername && err.code !== 'UND_ERR_INFO') {
// TODO (fix): Only delete for session related errors.

@@ -65,0 +64,0 @@ cache.delete(this.servername)

@@ -51,3 +51,3 @@ 'use strict'

// https://tools.ietf.org/html/rfc7231#section-6.4
this.opts.headers = cleanRequestHeaders(this.opts.headers, statusCode === 303)
this.opts.headers = cleanRequestHeaders(this.opts.headers, statusCode === 303, this.opts.origin !== origin)
this.opts.path = path

@@ -123,6 +123,7 @@ this.opts.origin = origin

// https://tools.ietf.org/html/rfc7231#section-6.4.4
function shouldRemoveHeader (header, removeContent) {
function shouldRemoveHeader (header, removeContent, unknownOrigin) {
return (
(header.length === 4 && header.toString().toLowerCase() === 'host') ||
(removeContent && header.toString().toLowerCase().indexOf('content-') === 0)
(removeContent && header.toString().toLowerCase().indexOf('content-') === 0) ||
(unknownOrigin && header.length === 13 && header.toString().toLowerCase() === 'authorization')
)

@@ -132,7 +133,7 @@ }

// https://tools.ietf.org/html/rfc7231#section-6.4
function cleanRequestHeaders (headers, removeContent) {
function cleanRequestHeaders (headers, removeContent, unknownOrigin) {
const ret = []
if (Array.isArray(headers)) {
for (let i = 0; i < headers.length; i += 2) {
if (!shouldRemoveHeader(headers[i], removeContent)) {
if (!shouldRemoveHeader(headers[i], removeContent, unknownOrigin)) {
ret.push(headers[i], headers[i + 1])

@@ -145,3 +146,3 @@ }

const key = keys[i]
if (!shouldRemoveHeader(key, removeContent)) {
if (!shouldRemoveHeader(key, removeContent, unknownOrigin)) {
ret.push(key, headers[key])

@@ -148,0 +149,0 @@ }

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

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

@@ -61,2 +61,4 @@ import { URL } from 'url'

opaque?: unknown;
/** Default: 0 */
maxRedirections?: number;
}

@@ -68,2 +70,4 @@ export interface RequestOptions extends DispatchOptions {

signal?: AbortSignal | EventEmitter | null;
/** Default: 0 */
maxRedirections?: number;
}

@@ -84,2 +88,4 @@ export interface PipelineOptions extends RequestOptions {

signal?: AbortSignal | EventEmitter | null;
/** Default: 0 */
maxRedirections?: number;
}

@@ -86,0 +92,0 @@ export interface ConnectData {

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