@bbc/http-transport
Advanced tools
Comparing version 4.3.0 to 4.4.0
@@ -6,8 +6,6 @@ 'use strict'; | ||
const http = require('node:http'); | ||
const HttpsProxyAgent = require('https-proxy-agent').HttpsProxyAgent; | ||
const HttpProxyAgent = require('http-proxy-agent').HttpProxyAgent; | ||
const fetch = require('node-fetch'); | ||
const REQUIRED_PROPERTIES = [ | ||
'body', | ||
'elapsedTime', | ||
'url', | ||
@@ -27,3 +25,3 @@ 'statusCode', | ||
if (options?.defaults?.proxy) { | ||
this._httpsProxyAgent = new HttpsProxyAgent(options.defaults.proxy, options?.agentOpts); | ||
this._httpProxyAgent = new HttpProxyAgent(options.defaults.proxy, options?.agentOpts); | ||
} | ||
@@ -77,8 +75,12 @@ | ||
// currently supports json and text formats only | ||
to.body = await from.text(); | ||
const contentType = to.headers['content-type']; | ||
if (this.parseAsJson(ctx, contentType)) { | ||
try { | ||
to.body = JSON.parse(to.body); | ||
} catch {} // If body is not parseable, leave as text | ||
const text = await from.text(); | ||
if (text) { | ||
to.body = text; | ||
const contentType = to.headers['content-type']; | ||
if (this.parseAsJson(ctx, contentType)) { | ||
try { | ||
to.body = JSON.parse(to.body); | ||
} catch {} // If body is not parseable, leave as text | ||
} | ||
} | ||
@@ -91,6 +93,6 @@ | ||
selectAgent(ctx) { | ||
if (this._httpsProxyAgent) return this._httpsProxyAgent; | ||
if (this?.defaults?.proxy) return this._httpProxyAgent; | ||
const protocol = new URL(ctx.req.getUrl()).protocol; | ||
return protocol === 'http:' ? this._httpAgent : this._httpsAgent; | ||
const http = new URL(ctx.req.getUrl()).protocol === 'http:'; | ||
return http ? this._httpAgent : this._httpsAgent; | ||
} | ||
@@ -97,0 +99,0 @@ |
{ | ||
"name": "@bbc/http-transport", | ||
"version": "4.3.0", | ||
"version": "4.4.0", | ||
"description": "A flexible, modular REST client built for ease-of-use and resilience.", | ||
@@ -51,3 +51,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"https-proxy-agent": "^7.0.2", | ||
"http-proxy-agent": "^7.0.0", | ||
"koa-compose": "^4.0.0", | ||
@@ -54,0 +54,0 @@ "lodash": "^4.17.4", |
@@ -86,3 +86,3 @@ 'use strict'; | ||
assert.deepEqual(res.body, ''); | ||
assert.deepEqual(res.body, undefined); | ||
}); | ||
@@ -558,6 +558,5 @@ | ||
nock.cleanAll(); | ||
api.get(path).reply(200, responseBody); | ||
api.get(path).reply(200, '1234'); | ||
const client = HttpTransport.createBuilder() | ||
.use(toJson()) | ||
.createClient(); | ||
@@ -567,3 +566,3 @@ | ||
.use(setContextProperty({ | ||
time: false | ||
json: true | ||
}, | ||
@@ -575,3 +574,3 @@ 'opts' | ||
assert.isUndefined(res.elapsedTime); | ||
assert.strictEqual(res.body, 1234); | ||
}); | ||
@@ -578,0 +577,0 @@ |
@@ -372,3 +372,3 @@ 'use strict'; | ||
it('selects proxy httpsAgent when protocol proxy has been provided', () => { | ||
it('selects httpProxyAgent when proxy has been provided', () => { | ||
const ctx = createContext(url); | ||
@@ -390,3 +390,4 @@ const options = { | ||
sinon.assert.calledWithMatch(spy, url, { agent: { | ||
proxy: new URL(proxyUrl) | ||
proxy: new URL(proxyUrl), | ||
protocol: 'http:' | ||
} }); | ||
@@ -396,3 +397,3 @@ }); | ||
it('selects proxy httpsAgent when protocol proxy has been provided and applies agent options', () => { | ||
it('selects httpProxyAgent when proxy has been provided and applies agent options', () => { | ||
const ctx = createContext(url); | ||
@@ -399,0 +400,0 @@ const options = { |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
385433
6744
+ Addedhttp-proxy-agent@^7.0.0
+ Addedhttp-proxy-agent@7.0.2(transitive)
- Removedhttps-proxy-agent@^7.0.2
- Removedhttps-proxy-agent@7.0.6(transitive)