Skip to content

Commit eb6862d

Browse files
Merge pull request #618 from cloudinary/proxy-agent-removed
fix: native http agent used instead of an external dependency
2 parents 8aa469c + d160902 commit eb6862d

File tree

9 files changed

+32
-68
lines changed

9 files changed

+32
-68
lines changed

lib-es5/api_client/execute_request.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ var Q = require('q');
1010
var url = require('url');
1111
var utils = require("../utils");
1212
var ensureOption = require('../utils/ensureOption').defaults(config());
13-
var ProxyAgent = utils.optionalRequireProxyAgent();
1413

1514
var extend = utils.extend,
1615
includes = utils.includes,
1716
isEmpty = utils.isEmpty;
1817

1918

19+
var agent = config.api_proxy ? new https.Agent(config.api_proxy) : null;
20+
2021
function execute_request(method, params, auth, api_url, callback) {
2122
var options = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : {};
2223

@@ -63,11 +64,10 @@ function execute_request(method, params, auth, api_url, callback) {
6364

6465
var proxy = options.api_proxy || config().api_proxy;
6566
if (!isEmpty(proxy)) {
66-
if (!request_options.agent) {
67-
if (ProxyAgent === null) {
68-
throw new Error("Proxy value is set, but `proxy-agent` is not installed, please install `proxy-agent` module.");
69-
}
70-
request_options.agent = new ProxyAgent(proxy);
67+
if (!request_options.agent && agent) {
68+
request_options.agent = agent;
69+
} else if (!request_options.agent) {
70+
request_options.agent = new https.Agent(proxy);
7171
} else {
7272
console.warn("Proxy is set, but request uses a custom agent, proxy is ignored.");
7373
}

lib-es5/uploader.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ var utils = require("./utils");
3535
var UploadStream = require('./upload_stream');
3636
var config = require("./config");
3737
var ensureOption = require('./utils/ensureOption').defaults(config());
38-
var ProxyAgent = utils.optionalRequireProxyAgent();
38+
39+
var agent = config.api_proxy ? new https.Agent(config.api_proxy) : null;
3940

4041
var build_upload_params = utils.build_upload_params,
4142
extend = utils.extend,
@@ -680,11 +681,10 @@ function post(url, post_data, boundary, file, callback, options) {
680681
}
681682
var proxy = options.api_proxy || config().api_proxy;
682683
if (!isEmpty(proxy)) {
683-
if (!post_options.agent) {
684-
if (ProxyAgent === null) {
685-
throw new Error("Proxy value is set, but `proxy-agent` is not installed, please install `proxy-agent` module.");
686-
}
687-
post_options.agent = new ProxyAgent(proxy);
684+
if (!post_options.agent && agent) {
685+
post_options.agent = agent;
686+
} else if (!post_options.agent) {
687+
post_options.agent = new https.Agent(proxy);
688688
} else {
689689
console.warn("Proxy is set, but request uses a custom agent, proxy is ignored.");
690690
}

lib-es5/utils/index.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,19 +1669,6 @@ function jsonArrayParam(data, modifier) {
16691669
return JSON.stringify(data);
16701670
}
16711671

1672-
function optionalRequireProxyAgent() {
1673-
var module = void 0;
1674-
try {
1675-
module = require('proxy-agent');
1676-
return module;
1677-
} catch (e) {
1678-
if (e.code === "MODULE_NOT_FOUND") {
1679-
return null;
1680-
}
1681-
throw e;
1682-
}
1683-
}
1684-
16851672
/**
16861673
* Empty function - do nothing
16871674
*
@@ -1739,7 +1726,6 @@ exports.jsonArrayParam = jsonArrayParam;
17391726
exports.download_folder = download_folder;
17401727
exports.base_api_url = base_api_url;
17411728
exports.download_backedup_asset = download_backedup_asset;
1742-
exports.optionalRequireProxyAgent = optionalRequireProxyAgent;
17431729

17441730
// was exported before, so kept for backwards compatibility
17451731
exports.DEFAULT_POSTER_OPTIONS = DEFAULT_POSTER_OPTIONS;

lib/api_client/execute_request.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ const Q = require('q');
66
const url = require('url');
77
const utils = require("../utils");
88
const ensureOption = require('../utils/ensureOption').defaults(config());
9-
const ProxyAgent = utils.optionalRequireProxyAgent();
109

1110
const { extend, includes, isEmpty } = utils;
1211

12+
const agent = config.api_proxy ? new https.Agent(config.api_proxy) : null;
13+
1314
function execute_request(method, params, auth, api_url, callback, options = {}) {
1415
method = method.toUpperCase();
1516
const deferred = Q.defer();
@@ -53,11 +54,10 @@ function execute_request(method, params, auth, api_url, callback, options = {})
5354

5455
let proxy = options.api_proxy || config().api_proxy;
5556
if (!isEmpty(proxy)) {
56-
if (!request_options.agent) {
57-
if (ProxyAgent === null) {
58-
throw new Error("Proxy value is set, but `proxy-agent` is not installed, please install `proxy-agent` module.")
59-
}
60-
request_options.agent = new ProxyAgent(proxy);
57+
if (!request_options.agent && agent) {
58+
request_options.agent = agent;
59+
} else if (!request_options.agent) {
60+
request_options.agent = new https.Agent(proxy);
6161
} else {
6262
console.warn("Proxy is set, but request uses a custom agent, proxy is ignored.");
6363
}

lib/uploader.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const utils = require("./utils");
1515
const UploadStream = require('./upload_stream');
1616
const config = require("./config");
1717
const ensureOption = require('./utils/ensureOption').defaults(config());
18-
const ProxyAgent = utils.optionalRequireProxyAgent();
18+
19+
const agent = config.api_proxy ? new https.Agent(config.api_proxy) : null;
1920

2021
const {
2122
build_upload_params,
@@ -578,11 +579,10 @@ function post(url, post_data, boundary, file, callback, options) {
578579
}
579580
let proxy = options.api_proxy || config().api_proxy;
580581
if (!isEmpty(proxy)) {
581-
if (!post_options.agent) {
582-
if (ProxyAgent === null) {
583-
throw new Error("Proxy value is set, but `proxy-agent` is not installed, please install `proxy-agent` module.")
584-
}
585-
post_options.agent = new ProxyAgent(proxy);
582+
if (!post_options.agent && agent) {
583+
post_options.agent = agent;
584+
} else if (!post_options.agent) {
585+
post_options.agent = new https.Agent(proxy);
586586
} else {
587587
console.warn("Proxy is set, but request uses a custom agent, proxy is ignored.");
588588
}

lib/utils/index.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,19 +1533,6 @@ function jsonArrayParam(data, modifier) {
15331533
return JSON.stringify(data);
15341534
}
15351535

1536-
function optionalRequireProxyAgent() {
1537-
let module;
1538-
try {
1539-
module = require('proxy-agent');
1540-
return module;
1541-
} catch (e) {
1542-
if (e.code === "MODULE_NOT_FOUND") {
1543-
return null;
1544-
}
1545-
throw e;
1546-
}
1547-
}
1548-
15491536
/**
15501537
* Empty function - do nothing
15511538
*
@@ -1601,7 +1588,6 @@ exports.jsonArrayParam = jsonArrayParam;
16011588
exports.download_folder = download_folder;
16021589
exports.base_api_url = base_api_url;
16031590
exports.download_backedup_asset = download_backedup_asset;
1604-
exports.optionalRequireProxyAgent = optionalRequireProxyAgent;
16051591

16061592
// was exported before, so kept for backwards compatibility
16071593
exports.DEFAULT_POSTER_OPTIONS = DEFAULT_POSTER_OPTIONS;

package.json

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
},
1212
"main": "cloudinary.js",
1313
"dependencies": {
14-
"cloudinary-core": "2.13.0",
15-
"core-js": "3.30.2",
16-
"lodash": "4.17.21",
17-
"q": "1.5.1"
14+
"cloudinary-core": "^2.13.0",
15+
"core-js": "^3.30.1",
16+
"lodash": "^4.17.21",
17+
"q": "^1.5.1"
1818
},
1919
"devDependencies": {
2020
"@types/mocha": "^7.0.2",
@@ -68,12 +68,6 @@
6868
"test-es5": "tools/scripts/test.es5.sh",
6969
"docs": "tools/scripts/docs.sh"
7070
},
71-
"optionalDependencies": {
72-
"proxy-agent": "^5.0.0"
73-
},
74-
"overrides": {
75-
"vm2": "3.9.19"
76-
},
7771
"engines": {
7872
"node": ">=0.6"
7973
}

test/integration/api/admin/api_spec.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const sinon = require('sinon');
22
const formatDate = require("date-fns").format;
33
const subDate = require("date-fns").sub;
44
const https = require('https');
5-
const ProxyAgent = require('proxy-agent');
65
const ClientRequest = require('_http_client').ClientRequest;
76
const Q = require('q');
87
const cloudinary = require("../../../../cloudinary");
@@ -1476,7 +1475,7 @@ describe("api", function () {
14761475
cloudinary.config({api_proxy: "https://myuser:[email protected]"});
14771476
cloudinary.v2.api.resources({});
14781477
sinon.assert.calledWith(mocked.request, sinon.match(
1479-
arg => arg.agent instanceof ProxyAgent
1478+
arg => arg.agent instanceof https.Agent
14801479
));
14811480
});
14821481
it("should prioritize custom agent", function () {
@@ -1491,7 +1490,7 @@ describe("api", function () {
14911490
cloudinary.config({});
14921491
cloudinary.v2.api.resources({api_proxy: "https://myuser:[email protected]"});
14931492
sinon.assert.calledWith(mocked.request, sinon.match(
1494-
arg => arg.agent instanceof ProxyAgent
1493+
arg => arg.agent instanceof https.Agent
14951494
));
14961495
});
14971496
})

test/integration/api/uploader/uploader_spec.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const cloudinary = require("../../../../cloudinary");
1111
const helper = require("../../../spechelper");
1212
const describe = require('../../../testUtils/suite');
1313
const cloneDeep = require('lodash/cloneDeep');
14-
const ProxyAgent = require('proxy-agent');
1514
const assert = require('assert');
1615

1716
const IMAGE_FILE = helper.IMAGE_FILE;
@@ -1512,7 +1511,7 @@ describe("uploader", function () {
15121511
cloudinary.config({api_proxy: proxy});
15131512
UPLOADER_V2.upload(IMAGE_FILE, {"tags": [TEST_TAG]});
15141513
sinon.assert.calledWith(mocked.request, sinon.match(
1515-
arg => arg.agent instanceof ProxyAgent
1514+
arg => arg.agent instanceof https.Agent
15161515
));
15171516
});
15181517
it("should prioritize custom agent", function () {
@@ -1527,7 +1526,7 @@ describe("uploader", function () {
15271526
cloudinary.config({});
15281527
UPLOADER_V2.upload(IMAGE_FILE, {"tags": [TEST_TAG], api_proxy: proxy});
15291528
sinon.assert.calledWith(mocked.request, sinon.match(
1530-
arg => arg.agent instanceof ProxyAgent
1529+
arg => arg.agent instanceof https.Agent
15311530
));
15321531
});
15331532
})

0 commit comments

Comments
 (0)