Created
October 20, 2022 17:05
-
-
Save mcollina/65bdf8b319638df4e70060de50c69e66 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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