Skip to content

Commit bfcbaf0

Browse files
fix: extracted optional require for proxy agent as a function
1 parent 3ff1361 commit bfcbaf0

File tree

6 files changed

+40
-16
lines changed

6 files changed

+40
-16
lines changed

lib-es5/api_client/execute_request.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ 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();
1314

1415
var extend = utils.extend,
1516
includes = utils.includes,
@@ -63,12 +64,10 @@ function execute_request(method, params, auth, api_url, callback) {
6364
var proxy = options.api_proxy || config().api_proxy;
6465
if (!isEmpty(proxy)) {
6566
if (!request_options.agent) {
66-
try {
67-
var ProxyAgent = require('proxy-agent');
68-
request_options.agent = new ProxyAgent(proxy);
69-
} catch (requireError) {
67+
if (ProxyAgent === null) {
7068
throw new Error("Proxy value is set, but `proxy-agent` is not installed, please install `proxy-agent` module.");
7169
}
70+
request_options.agent = new ProxyAgent(proxy);
7271
} else {
7372
console.warn("Proxy is set, but request uses a custom agent, proxy is ignored.");
7473
}

lib-es5/uploader.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ 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();
3839

3940
var build_upload_params = utils.build_upload_params,
4041
extend = utils.extend,
@@ -680,12 +681,10 @@ function post(url, post_data, boundary, file, callback, options) {
680681
var proxy = options.api_proxy || config().api_proxy;
681682
if (!isEmpty(proxy)) {
682683
if (!post_options.agent) {
683-
try {
684-
var ProxyAgent = require('proxy-agent');
685-
post_options.agent = new ProxyAgent(proxy);
686-
} catch (requireError) {
684+
if (ProxyAgent === null) {
687685
throw new Error("Proxy value is set, but `proxy-agent` is not installed, please install `proxy-agent` module.");
688686
}
687+
post_options.agent = new ProxyAgent(proxy);
689688
} else {
690689
console.warn("Proxy is set, but request uses a custom agent, proxy is ignored.");
691690
}

lib-es5/utils/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,6 +1669,19 @@ 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+
16721685
/**
16731686
* Empty function - do nothing
16741687
*
@@ -1726,6 +1739,7 @@ exports.jsonArrayParam = jsonArrayParam;
17261739
exports.download_folder = download_folder;
17271740
exports.base_api_url = base_api_url;
17281741
exports.download_backedup_asset = download_backedup_asset;
1742+
exports.optionalRequireProxyAgent = optionalRequireProxyAgent;
17291743

17301744
// was exported before, so kept for backwards compatibility
17311745
exports.DEFAULT_POSTER_OPTIONS = DEFAULT_POSTER_OPTIONS;

lib/api_client/execute_request.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ 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();
910

1011
const { extend, includes, isEmpty } = utils;
1112

@@ -53,12 +54,10 @@ function execute_request(method, params, auth, api_url, callback, options = {})
5354
let proxy = options.api_proxy || config().api_proxy;
5455
if (!isEmpty(proxy)) {
5556
if (!request_options.agent) {
56-
try {
57-
const ProxyAgent = require('proxy-agent');
58-
request_options.agent = new ProxyAgent(proxy);
59-
} catch (requireError) {
57+
if (ProxyAgent === null) {
6058
throw new Error("Proxy value is set, but `proxy-agent` is not installed, please install `proxy-agent` module.")
6159
}
60+
request_options.agent = new ProxyAgent(proxy);
6261
} else {
6362
console.warn("Proxy is set, but request uses a custom agent, proxy is ignored.");
6463
}

lib/uploader.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ 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();
1819

1920
const {
2021
build_upload_params,
@@ -578,12 +579,10 @@ function post(url, post_data, boundary, file, callback, options) {
578579
let proxy = options.api_proxy || config().api_proxy;
579580
if (!isEmpty(proxy)) {
580581
if (!post_options.agent) {
581-
try {
582-
const ProxyAgent = require('proxy-agent');
583-
post_options.agent = new ProxyAgent(proxy);
584-
} catch (requireError) {
582+
if (ProxyAgent === null) {
585583
throw new Error("Proxy value is set, but `proxy-agent` is not installed, please install `proxy-agent` module.")
586584
}
585+
post_options.agent = new ProxyAgent(proxy);
587586
} else {
588587
console.warn("Proxy is set, but request uses a custom agent, proxy is ignored.");
589588
}

lib/utils/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,19 @@ 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+
15361549
/**
15371550
* Empty function - do nothing
15381551
*
@@ -1588,6 +1601,7 @@ exports.jsonArrayParam = jsonArrayParam;
15881601
exports.download_folder = download_folder;
15891602
exports.base_api_url = base_api_url;
15901603
exports.download_backedup_asset = download_backedup_asset;
1604+
exports.optionalRequireProxyAgent = optionalRequireProxyAgent;
15911605

15921606
// was exported before, so kept for backwards compatibility
15931607
exports.DEFAULT_POSTER_OPTIONS = DEFAULT_POSTER_OPTIONS;

0 commit comments

Comments
 (0)