refactor to use http agent interface for tunnels

This commit is contained in:
Roman Shtylman
2018-05-15 14:49:19 -04:00
parent 30fd566c3a
commit c27100b98e
19 changed files with 1041 additions and 1345 deletions

View File

@@ -1,23 +0,0 @@
import http from 'http';
import util from 'util';
import assert from 'assert';
// binding agent will return a given options.socket as the socket for the agent
// this is useful if you already have a socket established and want the request
// to use that socket instead of making a new one
function BindingAgent(options) {
options = options || {};
http.Agent.call(this, options);
this.socket = options.socket;
assert(this.socket, 'socket is required for BindingAgent');
this.createConnection = create_connection;
}
util.inherits(BindingAgent, http.Agent);
function create_connection(port, host, options) {
return this.socket;
}
export default BindingAgent;

69
lib/Client.js Normal file
View File

@@ -0,0 +1,69 @@
import http from 'http';
import TunnelAgent from './TunnelAgent';
// A client encapsulates req/res handling using an agent
//
// If an agent is destroyed, the request handling will error
// The caller is responsible for handling a failed request
class Client {
constructor(options) {
this.agent = options.agent;
}
handleRequest(req, res) {
const opt = {
path: req.url,
agent: this.agent,
method: req.method,
headers: req.headers
};
const clientReq = http.request(opt, (clientRes) => {
// write response code and headers
res.writeHead(clientRes.statusCode, clientRes.headers);
clientRes.pipe(res);
});
// this can happen when underlying agent produces an error
// in our case we 504 gateway error this?
// if we have already sent headers?
clientReq.once('error', (err) => {
});
req.pipe(clientReq);
}
handleUpgrade(req, socket) {
this.agent.createConnection({}, (err, conn) => {
// any errors getting a connection mean we cannot service this request
if (err) {
socket.end();
return;
}
// socket met have disconnected while we waiting for a socket
if (!socket.readable || !socket.writable) {
socket.end();
return;
}
// websocket requests are special in that we simply re-create the header info
// then directly pipe the socket data
// avoids having to rebuild the request and handle upgrades via the http client
const arr = [`${req.method} ${req.url} HTTP/${req.httpVersion}`];
for (let i=0 ; i < (req.rawHeaders.length-1) ; i+=2) {
arr.push(`${req.rawHeaders[i]}: ${req.rawHeaders[i+1]}`);
}
arr.push('');
arr.push('');
conn.pipe(socket).pipe(conn);
conn.write(arr.join('\r\n'));
});
}
}
export default Client;

158
lib/Client.test.js Normal file
View File

@@ -0,0 +1,158 @@
import assert from 'assert';
import http from 'http';
import { Duplex } from 'stream';
import EventEmitter from 'events';
import WebSocket from 'ws';
import net from 'net';
import Client from './Client';
class DummySocket extends Duplex {
constructor(options) {
super(options);
}
_write(chunk, encoding, callback) {
callback();
}
_read(size) {
this.push('HTTP/1.1 304 Not Modified\r\nX-Powered-By: dummy\r\n\r\n\r\n');
this.push(null);
}
}
class DummyWebsocket extends Duplex {
constructor(options) {
super(options);
this.sentHeader = false;
}
_write(chunk, encoding, callback) {
const str = chunk.toString();
// if chunk contains `GET / HTTP/1.1` -> queue headers
// otherwise echo back received data
if (str.indexOf('GET / HTTP/1.1') === 0) {
const arr = [
'HTTP/1.1 101 Switching Protocols',
'Upgrade: websocket',
'Connection: Upgrade',
];
this.push(arr.join('\r\n'));
this.push('\r\n\r\n');
}
else {
this.push(str);
}
callback();
}
_read(size) {
// nothing to implement
}
}
class DummyAgent extends http.Agent {
constructor() {
super();
}
createConnection(options, cb) {
cb(null, new DummySocket());
}
}
describe('Client', () => {
it('should handle request', async () => {
const agent = new DummyAgent();
const client = new Client({ agent });
const server = http.createServer((req, res) => {
client.handleRequest(req, res);
});
await new Promise(resolve => server.listen(resolve));
const address = server.address();
const opt = {
host: 'localhost',
port: address.port,
path: '/',
};
const res = await new Promise((resolve) => {
const req = http.get(opt, (res) => {
resolve(res);
});
req.end();
});
assert.equal(res.headers['x-powered-by'], 'dummy');
server.close();
});
it('should handle upgrade', async () => {
// need a websocket server and a socket for it
class DummyWebsocketAgent extends http.Agent {
constructor() {
super();
}
createConnection(options, cb) {
cb(null, new DummyWebsocket());
}
}
const agent = new DummyWebsocketAgent();
const client = new Client({ agent });
const server = http.createServer();
server.on('upgrade', (req, socket, head) => {
client.handleUpgrade(req, socket);
});
await new Promise(resolve => server.listen(resolve));
const address = server.address();
const netClient = await new Promise((resolve) => {
const newClient = net.createConnection({ port: address.port }, () => {
resolve(newClient);
});
});
const out = [
'GET / HTTP/1.1',
'Connection: Upgrade',
'Upgrade: websocket'
];
netClient.write(out.join('\r\n') + '\r\n\r\n');
{
const data = await new Promise((resolve) => {
netClient.once('data', (chunk) => {
resolve(chunk.toString());
});
});
const exp = [
'HTTP/1.1 101 Switching Protocols',
'Upgrade: websocket',
'Connection: Upgrade',
];
assert.equal(exp.join('\r\n') + '\r\n\r\n', data);
}
{
netClient.write('foobar');
const data = await new Promise((resolve) => {
netClient.once('data', (chunk) => {
resolve(chunk.toString());
});
});
assert.equal('foobar', data);
}
netClient.destroy();
server.close();
});
});

View File

@@ -1,36 +1,31 @@
import Proxy from './Proxy';
import { hri } from 'human-readable-ids';
import Debug from 'debug';
// maybe remove?
import on_finished from 'on-finished';
import http from 'http';
import pump from 'pump';
import { hri } from "human-readable-ids";
import BindingAgent from './BindingAgent';
const NoOp = () => {};
import Client from './Client';
import TunnelAgent from './TunnelAgent';
// Manage sets of clients
//
// A client is a "user session" established to service a remote localtunnel client
class ClientManager {
constructor(opt) {
this.opt = opt;
this.reqId = 0;
this.opt = opt || {};
// id -> client instance
this.clients = Object.create(null);
this.clients = new Map();
// statistics
this.stats = {
tunnels: 0
};
this.debug = Debug('lt:ClientManager');
}
// create a new tunnel with `id`
// if the id is already used, a random id is assigned
async newClient (id) {
// if the tunnel could not be created, throws an error
async newClient(id) {
const clients = this.clients;
const stats = this.stats;
@@ -39,161 +34,68 @@ class ClientManager {
id = hri.random();
}
const popt = {
id: id,
max_tcp_sockets: this.opt.max_tcp_sockets
};
const maxSockets = this.opt.max_tcp_sockets;
const agent = new TunnelAgent({
maxSockets: 10,
});
const client = Proxy(popt);
agent.on('online', () => {
this.debug('client online %s', id);
});
agent.on('offline', () => {
// TODO(roman): grace period for re-connecting
// this period is short as the client is expected to maintain connections actively
// if they client does not reconnect on a dropped connection they need to re-establish
this.debug('client offline %s', id);
this.removeClient(id);
});
// TODO(roman): an agent error removes the client, the user needs to re-connect?
// how does a user realize they need to re-connect vs some random client being assigned same port?
agent.once('error', (err) => {
this.removeClient(id);
});
const client = new Client({ agent });
// add to clients map immediately
// avoiding races with other clients requesting same id
clients[id] = client;
client.on('end', () => {
--stats.tunnels;
delete clients[id];
});
// try/catch used here to remove client id
try {
const info = await agent.listen();
++stats.tunnels;
return {
id: id,
port: info.port,
max_conn_count: maxSockets,
};
}
catch (err) {
this.removeClient(id);
// rethrow error for upstream to handle
throw err;
}
}
return new Promise((resolve, reject) => {
// each local client has a tcp server to link with the remove localtunnel client
// this starts the server and waits until it is listening
client.start((err, info) => {
if (err) {
// clear the reserved client id
delete clients[id];
reject(err);
return;
}
++stats.tunnels;
info.id = id;
resolve(info);
});
});
removeClient(id) {
const client = this.clients[id];
if (!client) {
return;
}
--this.stats.tunnels;
delete this.clients[id];
client.agent.destroy();
}
hasClient(id) {
return this.clients[id];
}
// handle http request
handleRequest(clientId, req, res) {
const client = this.clients[clientId];
if (!client) {
return;
}
const reqId = this.reqId;
this.reqId = this.reqId + 1;
let endRes = () => {
endRes = NoOp;
res.end();
};
on_finished(res, () => {
endRes = NoOp;
});
client.nextSocket((clientSocket) => {
// response ended before we even got a socket to respond on
if (endRes === NoOp) {
return;
}
// happens when client upstream is disconnected (or disconnects)
// and the proxy iterates the waiting list and clears the callbacks
// we gracefully inform the user and kill their conn
// without this, the browser will leave some connections open
// and try to use them again for new requests
// TODO(roman) we could instead have a timeout above
// if no socket becomes available within some time,
// we just tell the user no resource available to service request
if (!clientSocket) {
endRes();
return;
}
const agent = new BindingAgent({
socket: clientSocket,
});
const opt = {
path: req.url,
agent: agent,
method: req.method,
headers: req.headers
};
return new Promise((resolve) => {
// what if error making this request?
const clientReq = http.request(opt, (clientRes) => {
// write response code and headers
res.writeHead(clientRes.statusCode, clientRes.headers);
// when this pump is done, we end our response
pump(clientRes, res, (err) => {
endRes();
resolve();
});
});
// we don't care about when this ends, only if there is error
pump(req, clientReq, (err) => {
if (err) {
endRes();
resolve();
}
});
});
});
}
// handle http upgrade
handleUpgrade(clientId, req, sock) {
const client = this.clients[clientId];
if (!client) {
return;
}
client.nextSocket(async (clientSocket) => {
if (!sock.readable || !sock.writable) {
sock.end();
return;
}
// happens when client upstream is disconnected (or disconnects)
// and the proxy iterates the waiting list and clears the callbacks
// we gracefully inform the user and kill their conn
// without this, the browser will leave some connections open
// and try to use them again for new requests
// TODO(roman) we could instead have a timeout above
// if no socket becomes available within some time,
// we just tell the user no resource available to service request
if (!clientSocket) {
sock.end();
return;
}
// websocket requests are special in that we simply re-create the header info
// then directly pipe the socket data
// avoids having to rebuild the request and handle upgrades via the http client
const arr = [`${req.method} ${req.url} HTTP/${req.httpVersion}`];
for (let i=0 ; i < (req.rawHeaders.length-1) ; i+=2) {
arr.push(`${req.rawHeaders[i]}: ${req.rawHeaders[i+1]}`);
}
arr.push('');
arr.push('');
clientSocket.pipe(sock).pipe(clientSocket);
clientSocket.write(arr.join('\r\n'));
await new Promise((resolve) => {
sock.once('end', resolve);
});
});
getClient(id) {
return this.clients[id];
}
}

51
lib/ClientManager.test.js Normal file
View File

@@ -0,0 +1,51 @@
import assert from 'assert';
import net from 'net';
import ClientManager from './ClientManager';
describe('ClientManager', () => {
it('should construct with no tunnels', () => {
const manager = new ClientManager();
assert.equal(manager.stats.tunnels, 0);
});
it('should create a new client with random id', async () => {
const manager = new ClientManager();
const client = await manager.newClient();
assert(manager.hasClient(client.id));
manager.removeClient(client.id);
});
it('should create a new client with id', async () => {
const manager = new ClientManager();
const client = await manager.newClient('foobar');
assert(manager.hasClient('foobar'));
manager.removeClient('foobar');
});
it('should create a new client with random id if previous exists', async () => {
const manager = new ClientManager();
const clientA = await manager.newClient('foobar');
const clientB = await manager.newClient('foobar');
assert(clientA.id, 'foobar');
assert(manager.hasClient(clientB.id));
assert(clientB.id != clientA.id);
manager.removeClient(clientB.id);
manager.removeClient('foobar');
});
it('should remove client once it goes offline', async () => {
const manager = new ClientManager();
const client = await manager.newClient('foobar');
const socket = await new Promise((resolve) => {
const netClient = net.createConnection({ port: client.port }, () => {
resolve(netClient);
});
});
const closePromise = new Promise(resolve => socket.once('close', resolve));
socket.end();
await closePromise;
assert(!manager.hasClient('foobar'));
});
});

View File

@@ -1,193 +0,0 @@
import net from 'net';
import EventEmitter from 'events';
import log from 'book';
import Debug from 'debug';
const debug = Debug('localtunnel:server');
const Proxy = function(opt) {
if (!(this instanceof Proxy)) {
return new Proxy(opt);
}
const self = this;
self.sockets = [];
self.waiting = [];
self.id = opt.id;
self.activeSockets = 0;
// default max is 10
self.max_tcp_sockets = opt.max_tcp_sockets || 10;
// new tcp server to service requests for this client
self.server = net.createServer();
// track initial user connection setup
self.conn_timeout = undefined;
self.debug = Debug(`localtunnel:server:${self.id}`);
};
Proxy.prototype.__proto__ = EventEmitter.prototype;
Proxy.prototype.start = function(cb) {
const self = this;
const server = self.server;
if (self.started) {
cb(new Error('already started'));
return;
}
self.started = true;
server.on('close', self._cleanup.bind(self));
server.on('connection', self._handle_socket.bind(self));
server.on('error', function(err) {
// where do these errors come from?
// other side creates a connection and then is killed?
if (err.code == 'ECONNRESET' || err.code == 'ETIMEDOUT') {
return;
}
log.error(err);
});
server.listen(function() {
const port = server.address().port;
self.debug('tcp server listening on port: %d', port);
cb(null, {
// port for lt client tcp connections
port: port,
// maximum number of tcp connections allowed by lt client
max_conn_count: self.max_tcp_sockets
});
});
self._maybe_destroy();
};
Proxy.prototype._maybe_destroy = function() {
const self = this;
clearTimeout(self.conn_timeout);
// After last socket is gone, we give opportunity to connect again quickly
self.conn_timeout = setTimeout(function() {
// sometimes the server is already closed but the event has not fired?
try {
clearTimeout(self.conn_timeout);
self.server.close();
}
catch (err) {
self._cleanup();
}
}, 1000);
}
// new socket connection from client for tunneling requests to client
Proxy.prototype._handle_socket = function(socket) {
const self = this;
// no more socket connections allowed
if (self.activeSockets >= self.max_tcp_sockets) {
return socket.end();
}
self.activeSockets = self.activeSockets + 1;
self.debug('new connection from: %s:%s', socket.address().address, socket.address().port);
// a single connection is enough to keep client id slot open
clearTimeout(self.conn_timeout);
socket.once('close', function(had_error) {
self.activeSockets = self.activeSockets - 1;
self.debug('closed socket (error: %s)', had_error);
// what if socket was servicing a request at this time?
// then it will be put back in available after right?
// we need a list of sockets servicing requests?
// remove this socket
const idx = self.sockets.indexOf(socket);
if (idx >= 0) {
self.sockets.splice(idx, 1);
}
// need to track total sockets, not just active available
self.debug('remaining client sockets: %s', self.sockets.length);
// no more sockets for this ident
if (self.sockets.length === 0) {
self.debug('all sockets disconnected');
self._maybe_destroy();
}
});
// close will be emitted after this
socket.on('error', function(err) {
// we don't log here to avoid logging crap for misbehaving clients
socket.destroy();
});
self.sockets.push(socket);
self._process_waiting();
};
Proxy.prototype._process_waiting = function() {
const self = this;
const fn = self.waiting.shift();
if (fn) {
self.debug('handling queued request');
self.nextSocket(fn);
}
};
Proxy.prototype._cleanup = function() {
const self = this;
self.debug('closed tcp socket for client(%s)', self.id);
clearTimeout(self.conn_timeout);
// clear waiting by ending responses, (requests?)
self.waiting.forEach(handler => handler(null));
self.emit('end');
};
Proxy.prototype.nextSocket = async function(fn) {
const self = this;
// socket is a tcp connection back to the user hosting the site
const sock = self.sockets.shift();
if (!sock) {
self.debug('no more clients, queue callback');
self.waiting.push(fn);
return;
}
self.debug('processing request');
await fn(sock);
if (!sock.destroyed) {
self.debug('retuning socket');
self.sockets.push(sock);
}
// no sockets left to process waiting requests
if (self.sockets.length === 0) {
return;
}
self._process_waiting();
};
Proxy.prototype._done = function() {
const self = this;
};
export default Proxy;

171
lib/TunnelAgent.js Normal file
View File

@@ -0,0 +1,171 @@
import { Agent } from 'http';
import net from 'net';
import assert from 'assert';
import log from 'book';
import Debug from 'debug';
const DEFAULT_MAX_SOCKETS = 10;
// Implements an http.Agent interface to a pool of tunnel sockets
// A tunnel socket is a connection _from_ a client that will
// service http requests. This agent is usable wherever one can use an http.Agent
class TunnelAgent extends Agent {
constructor(options = {}) {
super({
keepAlive: true,
// only allow keepalive to hold on to one socket
// this prevents it from holding on to all the sockets so they can be used for upgrades
maxFreeSockets: 1,
});
// sockets we can hand out via createConnection
this.availableSockets = [];
// when a createConnection cannot return a socket, it goes into a queue
// once a socket is available it is handed out to the next callback
this.waitingCreateConn = [];
this.debug = Debug('lt:TunnelAgent');
// track maximum allowed sockets
this.activeSockets = 0;
this.maxTcpSockets = options.maxTcpSockets || DEFAULT_MAX_SOCKETS;
// new tcp server to service requests for this client
this.server = net.createServer();
// flag to avoid double starts
this.started = false;
}
listen() {
const server = this.server;
if (this.started) {
throw new Error('already started');
}
this.started = true;
server.on('close', this._onClose.bind(this));
server.on('connection', this._onConnection.bind(this));
server.on('error', (err) => {
// where do these errors come from?
// other side creates a connection and then is killed?
if (err.code == 'ECONNRESET' || err.code == 'ETIMEDOUT') {
return;
}
log.error(err);
});
return new Promise((resolve) => {
server.listen(() => {
const port = server.address().port;
this.debug('tcp server listening on port: %d', port);
resolve({
// port for lt client tcp connections
port: port,
});
});
});
}
_onClose() {
this.debug('closed tcp socket');
clearTimeout(this.connTimeout);
// we will not invoke these callbacks?
// TODO(roman): we could invoke these with errors...?
// this makes downstream have to handle this
this.waitingCreateConn = [];
this.emit('end');
}
// new socket connection from client for tunneling requests to client
_onConnection(socket) {
// no more socket connections allowed
if (this.activeSockets >= this.maxTcpSockets) {
this.debug('no more sockets allowed');
socket.destroy();
return false;
}
// a new socket becomes available
if (this.activeSockets == 0) {
this.emit('online');
}
this.activeSockets += 1;
this.debug('new connection from: %s:%s', socket.address().address, socket.address().port);
// a single connection is enough to keep client id slot open
clearTimeout(this.connTimeout);
socket.once('close', (had_error) => {
this.debug('closed socket (error: %s)', had_error);
this.debug('removing socket');
this.activeSockets -= 1;
// remove the socket from available list
const idx = this.availableSockets.indexOf(socket);
if (idx >= 0) {
this.availableSockets.splice(idx, 1);
}
// need to track total sockets, not just active available
this.debug('remaining client sockets: %s', this.availableSockets.length);
// no more sockets for this session
// the session will become inactive if client does not reconnect
if (this.availableSockets.length <= 0) {
this.debug('all sockets disconnected');
this.emit('offline');
}
});
// close will be emitted after this
socket.once('error', (err) => {
// we do not log these errors, sessions can drop from clients for many reasons
// these are not actionable errors for our server
socket.destroy();
});
// make socket available for those waiting on sockets
this.availableSockets.push(socket);
// flush anyone waiting on sockets
this._callWaitingCreateConn();
}
// invoke when a new socket is available and there may be waiting createConnection calls
_callWaitingCreateConn() {
const fn = this.waitingCreateConn.shift();
if (!fn) {
return;
}
this.debug('handling queued request');
this.createConnection({}, fn);
}
// fetch a socket from the available socket pool for the agent
// if no socket is available, queue
// cb(err, socket)
createConnection(options, cb) {
// socket is a tcp connection back to the user hosting the site
const sock = this.availableSockets.shift();
// no available sockets
// wait until we have one
if (!sock) {
this.waitingCreateConn.push(cb);
this.debug('waiting');
return;
}
this.debug('socket given');
cb(null, sock);
}
destroy() {
this.server.close();
super.destroy();
}
}
export default TunnelAgent;

176
lib/TunnelAgent.test.js Normal file
View File

@@ -0,0 +1,176 @@
import http from 'http';
import net from 'net';
import assert from 'assert';
import TunnelAgent from './TunnelAgent';
describe('TunnelAgent', () => {
it('should create an empty agent', async () => {
const agent = new TunnelAgent();
assert.equal(agent.started, false);
const info = await agent.listen();
assert.ok(info.port > 0);
agent.destroy();
});
it('should create a new server and accept connections', async () => {
const agent = new TunnelAgent();
assert.equal(agent.started, false);
const info = await agent.listen();
const sock = net.createConnection({ port: info.port });
// in this test we wait for the socket to be connected
await new Promise(resolve => sock.once('connect', resolve));
const agentSock = await new Promise((resolve, reject) => {
agent.createConnection({}, (err, sock) => {
if (err) {
reject(err);
}
resolve(sock);
});
});
agentSock.write('foo');
await new Promise(resolve => sock.once('readable', resolve));
assert.equal('foo', sock.read().toString());
agent.destroy();
sock.destroy();
});
it('should reject connections over the max', async () => {
const agent = new TunnelAgent({
maxTcpSockets: 2,
});
assert.equal(agent.started, false);
const info = await agent.listen();
const sock1 = net.createConnection({ port: info.port });
const sock2 = net.createConnection({ port: info.port });
// two valid socket connections
const p1 = new Promise(resolve => sock1.once('connect', resolve));
const p2 = new Promise(resolve => sock2.once('connect', resolve));
await Promise.all([p1, p2]);
const sock3 = net.createConnection({ port: info.port });
const p3 = await new Promise(resolve => sock3.once('close', resolve));
agent.destroy();
sock1.destroy();
sock2.destroy();
sock3.destroy();
});
it('should queue createConnection requests', async () => {
const agent = new TunnelAgent();
assert.equal(agent.started, false);
const info = await agent.listen();
// create a promise for the next connection
let fulfilled = false;
const waitSockPromise = new Promise((resolve, reject) => {
agent.createConnection({}, (err, sock) => {
fulfilled = true;
if (err) {
reject(err);
}
resolve(sock);
});
});
// check that the next socket is not yet available
await new Promise(resolve => setTimeout(resolve, 500));
assert(!fulfilled);
// connect, this will make a socket available
const sock = net.createConnection({ port: info.port });
await new Promise(resolve => sock.once('connect', resolve));
const anotherAgentSock = await waitSockPromise;
agent.destroy();
sock.destroy();
});
it('should should emit a online event when a socket connects', async () => {
const agent = new TunnelAgent();
const info = await agent.listen();
const onlinePromise = new Promise(resolve => agent.once('online', resolve));
const sock = net.createConnection({ port: info.port });
await new Promise(resolve => sock.once('connect', resolve));
await onlinePromise;
agent.destroy();
sock.destroy();
});
it('should emit offline event when socket disconnects', async () => {
const agent = new TunnelAgent();
const info = await agent.listen();
const offlinePromise = new Promise(resolve => agent.once('offline', resolve));
const sock = net.createConnection({ port: info.port });
await new Promise(resolve => sock.once('connect', resolve));
sock.end();
await offlinePromise;
agent.destroy();
sock.destroy();
});
it('should emit offline event only when last socket disconnects', async () => {
const agent = new TunnelAgent();
const info = await agent.listen();
const offlinePromise = new Promise(resolve => agent.once('offline', resolve));
const sockA = net.createConnection({ port: info.port });
await new Promise(resolve => sockA.once('connect', resolve));
const sockB = net.createConnection({ port: info.port });
await new Promise(resolve => sockB.once('connect', resolve));
sockA.end();
const timeout = new Promise(resolve => setTimeout(resolve, 500));
await Promise.race([offlinePromise, timeout]);
sockB.end();
await offlinePromise;
agent.destroy();
});
it('should error an http request', async () => {
class ErrorAgent extends http.Agent {
constructor() {
super();
}
createConnection(options, cb) {
cb(new Error('foo'));
}
}
const agent = new ErrorAgent();
const opt = {
host: 'localhost',
port: 1234,
path: '/',
agent: agent,
};
const err = await new Promise((resolve) => {
const req = http.get(opt, (res) => {});
req.once('error', resolve);
});
assert.equal(err.message, 'foo');
});
});