Skip to content

Instantly share code, notes, and snippets.

@mcollina
Created October 20, 2022 17:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcollina/65bdf8b319638df4e70060de50c69e66 to your computer and use it in GitHub Desktop.
Save mcollina/65bdf8b319638df4e70060de50c69e66 to your computer and use it in GitHub Desktop.
import { createServer } from 'http'
import { connect } from 'net'
const server = createServer(function (req, res) {
console.log('request!')
server.close()
res.setHeader('Connection', 'close')
res.end('hello world')
})
await new Promise((resolve) => { server.listen(3000, resolve) })
const client = connect({ port: 3000 })
function sendRequest() {
client.cork()
client.write('GET / HTTP/1.1\r\n')
client.write('Host: localhost:3000\r\n')
client.write('User-Agent: curl/7.79.1\r\n')
client.write('Accept: */*\r\n')
client.write('\r\n')
client.uncork()
}
sendRequest()
sendRequest()
sendRequest()
client.pipe(process.stdout)
// There should be 3 requests not two
import { createServer } from 'http'
import { connect } from 'net'
import { Client } from 'undici'
const server = createServer(function (req, res) {
console.log('request!')
server.close()
res.setHeader('Connection', 'close')
res.end('hello world')
})
await new Promise((resolve) => { server.listen(3000, resolve) })
const client = new Client('http://localhost:3000', {
pipelining: 1
})
const responses = await Promise.all([
client.request({
path: '/',
method: 'GET'
}),
client.request({
path: '/',
method: 'GET'
}),
client.request({
path: '/',
method: 'GET'
})
])
const statusCodes = responses.map(r => r.statusCode)
console.log(statusCodes)
await client.close()
console.log('all good')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment