@bbc/http-transport
Advanced tools
Comparing version 4.1.1 to 4.2.0
@@ -149,3 +149,3 @@ import * as fetch from 'node-fetch'; | ||
type TransportOptions = { | ||
export type TransportOptions = { | ||
agentOpts?: https.AgentOptions, | ||
@@ -155,2 +155,3 @@ defaults?: { | ||
compress?: boolean | ||
proxy?: string | ||
} | ||
@@ -157,0 +158,0 @@ } |
@@ -6,3 +6,3 @@ 'use strict'; | ||
const http = require('node:http'); | ||
// eslint-disable-next-line func-style | ||
const HttpsProxyAgent = require('https-proxy-agent').HttpsProxyAgent; | ||
const fetch = require('node-fetch'); | ||
@@ -26,2 +26,6 @@ | ||
if (options?.defaults?.proxy) { | ||
this._httpsProxyAgent = new HttpsProxyAgent(options.defaults.proxy, options?.agentOpts); | ||
} | ||
this.defaults = options?.defaults; | ||
@@ -80,2 +84,9 @@ | ||
selectAgent(ctx) { | ||
if (this._httpsProxyAgent) return this._httpsProxyAgent; | ||
const protocol = new URL(ctx.req.getUrl()).protocol; | ||
return protocol === 'http:' ? this._httpAgent : this._httpsAgent; | ||
} | ||
async makeRequest(ctx, opts) { | ||
@@ -89,3 +100,3 @@ const controller = new AbortController(); | ||
method, | ||
agent: new URL(ctx.req.getUrl()).protocol === 'http:' ? this._httpAgent : this._httpsAgent, | ||
agent: this.selectAgent(ctx), | ||
signal: controller.signal | ||
@@ -92,0 +103,0 @@ }; |
{ | ||
"name": "@bbc/http-transport", | ||
"version": "4.1.1", | ||
"version": "4.2.0", | ||
"description": "A flexible, modular REST client built for ease-of-use and resilience.", | ||
@@ -51,2 +51,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"https-proxy-agent": "^7.0.2", | ||
"koa-compose": "^4.0.0", | ||
@@ -53,0 +54,0 @@ "lodash": "^4.17.4", |
[![NPM downloads](https://img.shields.io/npm/dm/@bbc/http-transport.svg?style=flat)](https://npmjs.org/package/@bbc/http-transport) | ||
[![Build Status](https://api.travis-ci.org/bbc/http-transport.svg)](https://travis-ci.org/bbc/http-transport) | ||
![npm](https://img.shields.io/npm/v/@bbc/http-transport.svg) | ||
@@ -5,0 +4,0 @@ ![license](https://img.shields.io/badge/license-MIT-blue.svg) |
@@ -13,2 +13,3 @@ 'use strict'; | ||
const httpsUrl = 'https://www.example.com/'; | ||
const proxyUrl = 'http://forward-proxy.ibl.test.api.bbci.co.uk'; | ||
const host = 'http://www.example.com'; | ||
@@ -299,52 +300,104 @@ const httpsHost = 'https://www.example.com'; | ||
it('selects httpAgent when protocol is http and agent options have been provided', () => { | ||
const ctx = createContext(url); | ||
const options = { | ||
agentOpts: { | ||
keepAlive: true, | ||
maxSockets: 1000 | ||
} | ||
}; | ||
describe('HTTP Agent', () => { | ||
it('selects httpAgent when protocol is http and agent options have been provided', () => { | ||
const ctx = createContext(url); | ||
const options = { | ||
agentOpts: { | ||
keepAlive: true, | ||
maxSockets: 1000 | ||
} | ||
}; | ||
const fetchTransport = new FetchTransport(options); | ||
const fetchTransport = new FetchTransport(options); | ||
const spy = sinon.spy(fetchTransport, '_fetch'); | ||
const spy = sinon.spy(fetchTransport, '_fetch'); | ||
return fetchTransport | ||
.execute(ctx) | ||
.catch(assert.ifError) | ||
.then(() => { | ||
sinon.assert.calledWithMatch(spy, url, { agent: { | ||
protocol: 'http:', | ||
return fetchTransport | ||
.execute(ctx) | ||
.catch(assert.ifError) | ||
.then(() => { | ||
sinon.assert.calledWithMatch(spy, url, { agent: { | ||
protocol: 'http:', | ||
keepAlive: true, | ||
maxSockets: 1000 | ||
} }); | ||
}); | ||
}); | ||
it('selects httpsAgent when protocol is https and agent options have been provided', () => { | ||
const ctx = createContext(httpsUrl); | ||
const options = { | ||
agentOpts: { | ||
keepAlive: true, | ||
maxSockets: 1000 | ||
} }); | ||
}); | ||
}); | ||
} | ||
}; | ||
it('selects httpsAgent when protocol is https and agent options have been provided', () => { | ||
const ctx = createContext(httpsUrl); | ||
const options = { | ||
agentOpts: { | ||
keepAlive: true, | ||
maxSockets: 1000 | ||
} | ||
}; | ||
const fetchTransport = new FetchTransport(options); | ||
const fetchTransport = new FetchTransport(options); | ||
const spy = sinon.spy(fetchTransport, '_fetch'); | ||
const spy = sinon.spy(fetchTransport, '_fetch'); | ||
return fetchTransport | ||
.execute(ctx) | ||
.catch(assert.ifError) | ||
.then(() => { | ||
sinon.assert.calledWithMatch(spy, httpsUrl, { agent: { | ||
protocol: 'https:', | ||
keepAlive: true, | ||
maxSockets: 1000 | ||
} }); | ||
}); | ||
}); | ||
return fetchTransport | ||
.execute(ctx) | ||
.catch(assert.ifError) | ||
.then(() => { | ||
sinon.assert.calledWithMatch(spy, httpsUrl, { agent: { | ||
protocol: 'https:', | ||
it('selects proxy httpsAgent when protocol proxy has been provided', () => { | ||
const ctx = createContext(url); | ||
const options = { | ||
defaults: { | ||
proxy: proxyUrl | ||
} | ||
}; | ||
const fetchTransport = new FetchTransport(options); | ||
const spy = sinon.spy(fetchTransport, '_fetch'); | ||
return fetchTransport | ||
.execute(ctx) | ||
.catch(assert.ifError) | ||
.then(() => { | ||
sinon.assert.calledWithMatch(spy, url, { agent: { | ||
proxy: new URL(proxyUrl) | ||
} }); | ||
}); | ||
}); | ||
it('selects proxy httpsAgent when protocol proxy has been provided and applies agent options', () => { | ||
const ctx = createContext(url); | ||
const options = { | ||
agentOpts: { | ||
keepAlive: true, | ||
maxSockets: 1000 | ||
} }); | ||
}); | ||
}, | ||
defaults: { | ||
proxy: proxyUrl | ||
} | ||
}; | ||
const fetchTransport = new FetchTransport(options); | ||
const spy = sinon.spy(fetchTransport, '_fetch'); | ||
return fetchTransport | ||
.execute(ctx) | ||
.catch(assert.ifError) | ||
.then(() => { | ||
sinon.assert.calledWithMatch(spy, url, { agent: { | ||
proxy: new URL(proxyUrl), | ||
keepAlive: true, | ||
maxSockets: 1000 | ||
} }); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
384456
6720
5
83
2
+ Addedhttps-proxy-agent@^7.0.2
+ Addedagent-base@7.1.3(transitive)
+ Addeddebug@4.4.0(transitive)
+ Addedhttps-proxy-agent@7.0.6(transitive)
+ Addedms@2.1.3(transitive)