From 7befb700abee81effc2833c8e37ed386ca0ad9da Mon Sep 17 00:00:00 2001 From: Jordan Jensen <23466445+Tiuipuv@users.noreply.github.com> Date: Fri, 29 Mar 2024 12:48:33 -0400 Subject: [PATCH] feat(webex-core): adding support for a custom proxy --- packages/@webex/webex-core/src/index.js | 1 + .../webex-core/src/interceptors/proxy.js | 70 +++++++++++++++++++ packages/@webex/webex-core/src/webex-core.js | 2 + .../test/unit/spec/interceptors/proxy.js | 52 ++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 packages/@webex/webex-core/src/interceptors/proxy.js create mode 100644 packages/@webex/webex-core/test/unit/spec/interceptors/proxy.js diff --git a/packages/@webex/webex-core/src/index.js b/packages/@webex/webex-core/src/index.js index a3da486372a..0025dcb14d7 100644 --- a/packages/@webex/webex-core/src/index.js +++ b/packages/@webex/webex-core/src/index.js @@ -51,6 +51,7 @@ export {default as RequestEventInterceptor} from './interceptors/request-event'; export {default as RequestLoggerInterceptor} from './interceptors/request-logger'; export {default as RequestTimingInterceptor} from './interceptors/request-timing'; export {default as UserAgentInterceptor} from './interceptors/user-agent'; +export {default as ProxyInterceptor} from './interceptors/proxy'; export {default as WebexTrackingIdInterceptor} from './interceptors/webex-tracking-id'; export {default as WebexUserAgentInterceptor} from './interceptors/webex-user-agent'; export {default as RateLimitInterceptor} from './interceptors/rate-limit'; diff --git a/packages/@webex/webex-core/src/interceptors/proxy.js b/packages/@webex/webex-core/src/interceptors/proxy.js new file mode 100644 index 00000000000..352e9e2b5c3 --- /dev/null +++ b/packages/@webex/webex-core/src/interceptors/proxy.js @@ -0,0 +1,70 @@ +/*! + * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file. + */ + +import {inBrowser} from '@webex/common'; +import {Interceptor} from '@webex/http-core'; +import {get} from 'lodash'; + +const strings = new WeakMap(); + +/** + * Sets a proxy on all requests if one is not present. + * Defaults to none, though a custom proxy can be set + * using the proxy configuration. e.g. + * + * webex = WebexSdk.init({ + * credentials: { + * supertoken: superToken + * }, + * config: { + * credentials: { + * client_id, + * client_secret + * }, + * proxy: 'http://myproxy.company.com' + * } + * }); + */ +export default class ProxyInterceptor extends Interceptor { + /** + * @param {Object} [options={}] + * @param {WebexCore} [options.webex] + * @private + * @returns {ProxyInterceptor} + */ + constructor(options = {}) { + const proxy = get(options, 'webex.config.proxy'); + + super(options); + if (proxy) { + strings.set(this, proxy); + } + } + + /** + * @returns {ProxyInterceptor} + */ + static create() { + return new ProxyInterceptor({webex: this}); + } + + /** + * @see Interceptor#onRequest + * @param {Object} options + * @returns {Object} + */ + onRequest(options) { + // Do not set a proxy for browsers + if (inBrowser) { + return options; + } + + const proxy = strings.get(this); + if (proxy) { + options.proxy = proxy; + } + + return options; + } +} diff --git a/packages/@webex/webex-core/src/webex-core.js b/packages/@webex/webex-core/src/webex-core.js index c9f33cdeda6..71f482c4094 100644 --- a/packages/@webex/webex-core/src/webex-core.js +++ b/packages/@webex/webex-core/src/webex-core.js @@ -26,6 +26,7 @@ import RequestTimingInterceptor from './interceptors/request-timing'; import ResponseLoggerInterceptor from './interceptors/response-logger'; import WebexHttpError from './lib/webex-http-error'; import UserAgentInterceptor from './interceptors/user-agent'; +import ProxyInterceptor from './interceptors/proxy'; import WebexTrackingIdInterceptor from './interceptors/webex-tracking-id'; import WebexUserAgentInterceptor from './interceptors/webex-user-agent'; import RateLimitInterceptor from './interceptors/rate-limit'; @@ -57,6 +58,7 @@ const interceptors = { RequestTimingInterceptor: RequestTimingInterceptor.create, ServiceInterceptor: undefined, UserAgentInterceptor: UserAgentInterceptor.create, + ProxyInterceptor: ProxyInterceptor.create, WebexUserAgentInterceptor: WebexUserAgentInterceptor.create, AuthInterceptor: AuthInterceptor.create, KmsDryErrorInterceptor: undefined, diff --git a/packages/@webex/webex-core/test/unit/spec/interceptors/proxy.js b/packages/@webex/webex-core/test/unit/spec/interceptors/proxy.js new file mode 100644 index 00000000000..a1dcd076f69 --- /dev/null +++ b/packages/@webex/webex-core/test/unit/spec/interceptors/proxy.js @@ -0,0 +1,52 @@ +/*! + * Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file. + */ + +import {assert} from '@webex/test-helper-chai'; +import {skipInBrowser} from '@webex/test-helper-mocha'; +import {ProxyInterceptor} from '@webex/webex-core'; + +import pkg from '../../../../package'; + +describe('webex-core', () => { + describe('Interceptors', () => { + describe('ProxyInterceptor', () => { + // Do not set proxy for browsers + skipInBrowser(describe)('#onRequest', () => { + it('default proxy', () => { + const interceptor = Reflect.apply( + ProxyInterceptor.create, + { + version: pkg.version, + }, + [] + ); + const options = {}; + + interceptor.onRequest(options); + + assert.isUndefined(options.proxy); + }); + + it('custom proxy', () => { + const interceptor = Reflect.apply( + ProxyInterceptor.create, + { + version: pkg.version, + config: { + proxy: 'http://proxy.company.com' + }, + }, + [] + ); + const options = {}; + + interceptor.onRequest(options); + + assert.property(options, 'proxy'); + assert.equal(options.proxy, 'http://proxy.company.com'); + }); + }); + }); + }); +});