From 70164f981f8c50e30b77ecac8cca53632e8edac8 Mon Sep 17 00:00:00 2001 From: Shige Date: Tue, 29 Jan 2019 23:12:12 +0900 Subject: [PATCH] Fix 'ReferenceError: process is not defined' error in Angular --- lib/policies/proxyPolicy.browser.ts | 45 +++++++++++++++++++++++++++++ package.json | 1 + rollup.config.ts | 1 + webpack.testconfig.ts | 3 +- 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 lib/policies/proxyPolicy.browser.ts diff --git a/lib/policies/proxyPolicy.browser.ts b/lib/policies/proxyPolicy.browser.ts new file mode 100644 index 00000000..008c60af --- /dev/null +++ b/lib/policies/proxyPolicy.browser.ts @@ -0,0 +1,45 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. + +import { BaseRequestPolicy, RequestPolicy, RequestPolicyFactory, RequestPolicyOptions } from "./requestPolicy"; +import { HttpOperationResponse } from "../httpOperationResponse"; +import { ProxySettings } from "../serviceClient"; +import { WebResource } from "../webResource"; +import { URLBuilder } from "../url"; + +export function getDefaultProxySettings(proxyUrl?: string): ProxySettings | undefined { + if (!proxyUrl) { + return undefined; + } + + const parsedUrl = URLBuilder.parse(proxyUrl); + return { + host: parsedUrl.getScheme() + "://" + parsedUrl.getHost(), + port: Number.parseInt(parsedUrl.getPort() || "80") + }; +} + + +export function proxyPolicy(proxySettings?: ProxySettings): RequestPolicyFactory { + return { + create: (nextPolicy: RequestPolicy, options: RequestPolicyOptions) => { + return new ProxyPolicy(nextPolicy, options, proxySettings!); + } + }; +} + +export class ProxyPolicy extends BaseRequestPolicy { + proxySettings: ProxySettings; + + constructor(nextPolicy: RequestPolicy, options: RequestPolicyOptions, proxySettings: ProxySettings) { + super(nextPolicy, options); + this.proxySettings = proxySettings; + } + + public sendRequest(request: WebResource): Promise { + if (!request.proxySettings) { + request.proxySettings = this.proxySettings; + } + return this._nextPolicy.sendRequest(request); + } +} diff --git a/package.json b/package.json index 8bd92cd1..76a2dfb6 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ ], "browser": { "./es/lib/policies/msRestUserAgentPolicy.js": "./es/lib/policies/msRestUserAgentPolicy.browser.js", + "./es/lib/policies/proxyPolicy.js": "./es/lib/policies/proxyPolicy.browser.js", "./es/lib/util/base64.js": "./es/lib/util/base64.browser.js", "./es/lib/util/xml.js": "./es/lib/util/xml.browser.js", "./es/lib/defaultHttpClient.js": "./es/lib/defaultHttpClient.browser.js" diff --git a/rollup.config.ts b/rollup.config.ts index 8c82327c..b40ca56c 100644 --- a/rollup.config.ts +++ b/rollup.config.ts @@ -70,6 +70,7 @@ alias({ "./defaultHttpClient": "./defaultHttpClient.browser", "./msRestUserAgentPolicy": "./msRestUserAgentPolicy.browser", + "./proxyPolicy": "./proxyPolicy.browser", "./util/xml": "./util/xml.browser", "./util/base64": "./util/base64.browser", }), diff --git a/webpack.testconfig.ts b/webpack.testconfig.ts index fc6d368a..70c6c1a5 100644 --- a/webpack.testconfig.ts +++ b/webpack.testconfig.ts @@ -30,7 +30,8 @@ const config: webpack.Configuration = { new webpack.NormalModuleReplacementPlugin(/(\.).+util\/base64/, path.resolve(__dirname, "./lib/util/base64.browser.ts")), new webpack.NormalModuleReplacementPlugin(/(\.).+util\/xml/, path.resolve(__dirname, "./lib/util/xml.browser.ts")), new webpack.NormalModuleReplacementPlugin(/(\.).+defaultHttpClient/, path.resolve(__dirname, "./lib/defaultHttpClient.browser.ts")), - new webpack.NormalModuleReplacementPlugin(/(\.).+msRestUserAgentPolicy/, path.resolve(__dirname, "./lib/policies/msRestUserAgentPolicy.browser.ts")) + new webpack.NormalModuleReplacementPlugin(/(\.).+msRestUserAgentPolicy/, path.resolve(__dirname, "./lib/policies/msRestUserAgentPolicy.browser.ts")), + new webpack.NormalModuleReplacementPlugin(/(\.).+proxyPolicy/, path.resolve(__dirname, "./lib/policies/proxyPolicy.browser.ts")) ], module: { rules: [