Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use http-proxy-agent and https-proxy-agent instead of hpagent #53

Merged
merged 1 commit into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions lib/remote-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,28 +122,36 @@ class RemoteModel extends LocalModel {
proxy(proxy = this.options.proxy, url) {
if (!proxy || !url) return;

if (typeof proxy === 'string') proxy = { proxy };
if (typeof proxy === 'string') proxy = { uri: proxy };

const proxyUrl = new URL(proxy.uri);

const getAuth = u =>
u.username && u.password ? decodeURIComponent(`${u.username}:${u.password}`) :
u.username ? decodeURIComponent(u.username) : null;

const getPath = u => u.pathname + u.search + u.hash;

const options = Object.assign({
host: proxyUrl.hostname,
port: proxyUrl.port,
protocol: proxyUrl.protocol,
path: getPath(proxyUrl),
auth: getAuth(proxyUrl),
maxSockets: 1,
keepAlive: false
}, proxy);

const isHttps = (new URL(url).protocol === 'https:');

if (isHttps) {
const { HttpsProxyAgent } = require('hpagent');
return {
https: new HttpsProxyAgent(Object.assign({
keepAlive: false,
maxSockets: 1,
maxFreeSockets: 1,
}, proxy))
};
const HttpsProxyAgent = require('https-proxy-agent');
const agent = new HttpsProxyAgent(options);
return { https: agent };
} else {
const { HttpProxyAgent } = require('hpagent');
return {
http: new HttpProxyAgent(Object.assign({
keepAlive: false,
maxSockets: 1,
maxFreeSockets: 1,
}, proxy))
};
const HttpProxyAgent = require('http-proxy-agent');
const agent = new HttpProxyAgent(options);
return { http: agent };
}
}

Expand Down
43 changes: 36 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"dependencies": {
"debug": "^4.3.3",
"got": "^11.8.3",
"hpagent": "^0.1.2",
"http-proxy-agent": "^5.0.0",
"https-proxy-agent": "^5.0.0",
"lodash.kebabcase": "^4.1.1"
},
"devDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ global.should = chai.should();
global.expect = chai.expect;
global.sinon = require('sinon');
chai.use(require('sinon-chai'));

require('hmpo-logger').config();
36 changes: 30 additions & 6 deletions test/lib/spec.remote-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const ModelError = require('../../lib/model-error');
const BaseModel = require('../../lib/local-model');
const logger = require('hmpo-logger');

const { HttpProxyAgent, HttpsProxyAgent } = require('hpagent');
const HttpsProxyAgent = require('https-proxy-agent');
const HttpProxyAgent = require('http-proxy-agent');

describe('Remote Model', () => {
let model, cb, mocks;
Expand Down Expand Up @@ -71,7 +72,7 @@ describe('Remote Model', () => {
});

it('should use console log and a trimHtml pass-through if hmpo-logger is not available', () => {
logger.get.throws(new Error());
logger.get.throws({ message: 'test error' });

model = new Model();

Expand Down Expand Up @@ -431,19 +432,43 @@ describe('Remote Model', () => {
'method': 'VERB',
'url': 'http://example.net',
'proxy': {
proxy: 'http://proxy.example.com:8000',
keepAlive: true
uri: 'http://proxy.example.com:8000',
keepAlive: true,
maxSockets: 200
}
});

sinon.assert.match(returnedConfig, {
agent: {
http: {
keepAlive: true
proxy: {
keepAlive: true,
maxSockets: 200
}
}
}
});
});

it('should process auth for the proxy', () => {
let agent;

agent = model.proxy('http://username:password@host:123/path', 'https://example.com');
sinon.assert.match(agent.https.proxy, {
auth: 'username:password',
host: 'host',
port: 123,
protocol: 'http:'
});

agent = model.proxy('http://username@host:123/path', 'https://example.com');
sinon.assert.match(agent.https.proxy, {
auth: 'username',
host: 'host',
port: 123,
protocol: 'http:'
});
});
});

describe('with headers supplied into the constructor', () => {
Expand Down Expand Up @@ -742,7 +767,6 @@ describe('Remote Model', () => {
};

model.request(settings, cb);
console.log(model.logError.args[0][0]);
model.logError.should.have.been.calledWithExactly({
settings: settings,
statusCode: 404,
Expand Down