hmpo-model 4.0.4 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yaml +1 -1
- package/lib/remote-model.js +25 -17
- package/package.json +9 -8
- package/test/helper.js +2 -0
- package/test/lib/spec.remote-model.js +30 -6
package/lib/remote-model.js
CHANGED
|
@@ -122,28 +122,36 @@ class RemoteModel extends LocalModel {
|
|
|
122
122
|
proxy(proxy = this.options.proxy, url) {
|
|
123
123
|
if (!proxy || !url) return;
|
|
124
124
|
|
|
125
|
-
if (typeof proxy === 'string') proxy = { proxy };
|
|
125
|
+
if (typeof proxy === 'string') proxy = { uri: proxy };
|
|
126
|
+
|
|
127
|
+
const proxyUrl = new URL(proxy.uri);
|
|
128
|
+
|
|
129
|
+
const getAuth = u =>
|
|
130
|
+
u.username && u.password ? decodeURIComponent(`${u.username}:${u.password}`) :
|
|
131
|
+
u.username ? decodeURIComponent(u.username) : null;
|
|
132
|
+
|
|
133
|
+
const getPath = u => u.pathname + u.search + u.hash;
|
|
134
|
+
|
|
135
|
+
const options = Object.assign({
|
|
136
|
+
host: proxyUrl.hostname,
|
|
137
|
+
port: proxyUrl.port,
|
|
138
|
+
protocol: proxyUrl.protocol,
|
|
139
|
+
path: getPath(proxyUrl),
|
|
140
|
+
auth: getAuth(proxyUrl),
|
|
141
|
+
maxSockets: 1,
|
|
142
|
+
keepAlive: false
|
|
143
|
+
}, proxy);
|
|
126
144
|
|
|
127
145
|
const isHttps = (new URL(url).protocol === 'https:');
|
|
128
146
|
|
|
129
147
|
if (isHttps) {
|
|
130
|
-
const
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
keepAlive: false,
|
|
134
|
-
maxSockets: 1,
|
|
135
|
-
maxFreeSockets: 1,
|
|
136
|
-
}, proxy))
|
|
137
|
-
};
|
|
148
|
+
const HttpsProxyAgent = require('https-proxy-agent');
|
|
149
|
+
const agent = new HttpsProxyAgent(options);
|
|
150
|
+
return { https: agent };
|
|
138
151
|
} else {
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
keepAlive: false,
|
|
143
|
-
maxSockets: 1,
|
|
144
|
-
maxFreeSockets: 1,
|
|
145
|
-
}, proxy))
|
|
146
|
-
};
|
|
152
|
+
const HttpProxyAgent = require('http-proxy-agent');
|
|
153
|
+
const agent = new HttpProxyAgent(options);
|
|
154
|
+
return { http: agent };
|
|
147
155
|
}
|
|
148
156
|
}
|
|
149
157
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hmpo-model",
|
|
3
|
-
"version": "4.0
|
|
3
|
+
"version": "4.2.0",
|
|
4
4
|
"description": "Simple model for interacting with http/rest apis.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -25,19 +25,20 @@
|
|
|
25
25
|
},
|
|
26
26
|
"homepage": "https://github.com/UKHomeOffice/passports-model",
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"debug": "^4.3.
|
|
29
|
-
"got": "
|
|
30
|
-
"
|
|
28
|
+
"debug": "^4.3.4",
|
|
29
|
+
"got": "<12",
|
|
30
|
+
"http-proxy-agent": "^5.0.0",
|
|
31
|
+
"https-proxy-agent": "^5.0.1",
|
|
31
32
|
"lodash.kebabcase": "^4.1.1"
|
|
32
33
|
},
|
|
33
34
|
"devDependencies": {
|
|
34
35
|
"chai": "^4.3.6",
|
|
35
|
-
"eslint": "^8.
|
|
36
|
-
"hmpo-logger": "^
|
|
37
|
-
"mocha": "^
|
|
36
|
+
"eslint": "^8.18.0",
|
|
37
|
+
"hmpo-logger": "^6.1.0",
|
|
38
|
+
"mocha": "^10.0.0",
|
|
38
39
|
"nyc": "^15.1.0",
|
|
39
40
|
"proxyquire": "^2.0.0",
|
|
40
|
-
"sinon": "^
|
|
41
|
+
"sinon": "^14.0.0",
|
|
41
42
|
"sinon-chai": "^3.7.0"
|
|
42
43
|
},
|
|
43
44
|
"nyc": {
|
package/test/helper.js
CHANGED
|
@@ -5,7 +5,8 @@ const ModelError = require('../../lib/model-error');
|
|
|
5
5
|
const BaseModel = require('../../lib/local-model');
|
|
6
6
|
const logger = require('hmpo-logger');
|
|
7
7
|
|
|
8
|
-
const
|
|
8
|
+
const HttpsProxyAgent = require('https-proxy-agent');
|
|
9
|
+
const HttpProxyAgent = require('http-proxy-agent');
|
|
9
10
|
|
|
10
11
|
describe('Remote Model', () => {
|
|
11
12
|
let model, cb, mocks;
|
|
@@ -71,7 +72,7 @@ describe('Remote Model', () => {
|
|
|
71
72
|
});
|
|
72
73
|
|
|
73
74
|
it('should use console log and a trimHtml pass-through if hmpo-logger is not available', () => {
|
|
74
|
-
logger.get.throws(
|
|
75
|
+
logger.get.throws({ message: 'test error' });
|
|
75
76
|
|
|
76
77
|
model = new Model();
|
|
77
78
|
|
|
@@ -431,19 +432,43 @@ describe('Remote Model', () => {
|
|
|
431
432
|
'method': 'VERB',
|
|
432
433
|
'url': 'http://example.net',
|
|
433
434
|
'proxy': {
|
|
434
|
-
|
|
435
|
-
keepAlive: true
|
|
435
|
+
uri: 'http://proxy.example.com:8000',
|
|
436
|
+
keepAlive: true,
|
|
437
|
+
maxSockets: 200
|
|
436
438
|
}
|
|
437
439
|
});
|
|
438
440
|
|
|
439
441
|
sinon.assert.match(returnedConfig, {
|
|
440
442
|
agent: {
|
|
441
443
|
http: {
|
|
442
|
-
|
|
444
|
+
proxy: {
|
|
445
|
+
keepAlive: true,
|
|
446
|
+
maxSockets: 200
|
|
447
|
+
}
|
|
443
448
|
}
|
|
444
449
|
}
|
|
445
450
|
});
|
|
446
451
|
});
|
|
452
|
+
|
|
453
|
+
it('should process auth for the proxy', () => {
|
|
454
|
+
let agent;
|
|
455
|
+
|
|
456
|
+
agent = model.proxy('http://username:password@host:123/path', 'https://example.com');
|
|
457
|
+
sinon.assert.match(agent.https.proxy, {
|
|
458
|
+
auth: 'username:password',
|
|
459
|
+
host: 'host',
|
|
460
|
+
port: 123,
|
|
461
|
+
protocol: 'http:'
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
agent = model.proxy('http://username@host:123/path', 'https://example.com');
|
|
465
|
+
sinon.assert.match(agent.https.proxy, {
|
|
466
|
+
auth: 'username',
|
|
467
|
+
host: 'host',
|
|
468
|
+
port: 123,
|
|
469
|
+
protocol: 'http:'
|
|
470
|
+
});
|
|
471
|
+
});
|
|
447
472
|
});
|
|
448
473
|
|
|
449
474
|
describe('with headers supplied into the constructor', () => {
|
|
@@ -742,7 +767,6 @@ describe('Remote Model', () => {
|
|
|
742
767
|
};
|
|
743
768
|
|
|
744
769
|
model.request(settings, cb);
|
|
745
|
-
console.log(model.logError.args[0][0]);
|
|
746
770
|
model.logError.should.have.been.calledWithExactly({
|
|
747
771
|
settings: settings,
|
|
748
772
|
statusCode: 404,
|