Skip to content

Commit 0ce2e47

Browse files
fix: native http agent used instead of an external dependency
1 parent 8aa469c commit 0ce2e47

File tree

5 files changed

+20
-27
lines changed

5 files changed

+20
-27
lines changed

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 & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const config = require("./config");
1717
const ensureOption = require('./utils/ensureOption').defaults(config());
1818
const ProxyAgent = utils.optionalRequireProxyAgent();
1919

20+
const agent = config.api_proxy ? new https.Agent(config.api_proxy) : null;
21+
2022
const {
2123
build_upload_params,
2224
extend,
@@ -578,11 +580,10 @@ function post(url, post_data, boundary, file, callback, options) {
578580
}
579581
let proxy = options.api_proxy || config().api_proxy;
580582
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);
583+
if (!post_options.agent && agent) {
584+
post_options.agent = agent;
585+
} else if (!post_options.agent) {
586+
post_options.agent = new https.Agent(proxy);
586587
} else {
587588
console.warn("Proxy is set, but request uses a custom agent, proxy is ignored.");
588589
}

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)