Skip to content

Commit 18acad3

Browse files
lpincaMylesBorins
authored andcommittedApr 13, 2018
http: make socketPath work with no agent
Currently `Agent.prototype.createConnection()` is called uncoditionally if the `socketPath` option is used. This throws an error if no agent is used, preventing, for example, the `socketPath` and `createConnection` options to be used together. This commit fixes the issue by falling back to the `createConnection` option or `net.createConnection()`. PR-URL: #19425 Reviewed-By: Rod Vagg <rod@vagg.org> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Matheus Marchini <matheus@sthima.com> Reviewed-By: Chen Gang <gangc.cxy@foxmail.com>
1 parent 1edadeb commit 18acad3

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed
 

‎lib/_http_client.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,11 @@ function ClientRequest(options, cb) {
138138
timeout: self.timeout,
139139
rejectUnauthorized: !!options.rejectUnauthorized
140140
};
141-
const newSocket = self.agent.createConnection(optionsPath, oncreate);
141+
const newSocket = self.agent
142+
? self.agent.createConnection(optionsPath, oncreate)
143+
: typeof options.createConnection === 'function'
144+
? options.createConnection(optionsPath, oncreate)
145+
: net.createConnection(optionsPath);
142146
if (newSocket && !called) {
143147
called = true;
144148
self.onSocket(newSocket);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
const common = require('../common');
3+
const Countdown = require('../common/countdown');
4+
5+
const http = require('http');
6+
const { createConnection } = require('net');
7+
8+
const server = http.createServer((req, res) => {
9+
res.end();
10+
});
11+
12+
const countdown = new Countdown(2, () => {
13+
server.close();
14+
});
15+
16+
common.refreshTmpDir();
17+
18+
server.listen(common.PIPE, common.mustCall(() => {
19+
http.get({ createConnection, socketPath: common.PIPE }, onResponse);
20+
http.get({ agent: 0, socketPath: common.PIPE }, onResponse);
21+
}));
22+
23+
function onResponse(res) {
24+
res.on('end', () => {
25+
countdown.dec();
26+
});
27+
res.resume();
28+
}

0 commit comments

Comments
 (0)
Please sign in to comment.