UNPKG

5.04 kBJavaScriptView Raw
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License. See License.txt in the project root for license information.
3import { __extends } from "tslib";
4import { BaseRequestPolicy, } from "./requestPolicy";
5import { Constants } from "../util/constants";
6import { URLBuilder } from "../url";
7/**
8 * @internal
9 */
10export var noProxyList = loadNoProxy();
11var byPassedList = new Map();
12/**
13 * @internal
14 */
15export function getEnvironmentValue(name) {
16 if (process.env[name]) {
17 return process.env[name];
18 }
19 else if (process.env[name.toLowerCase()]) {
20 return process.env[name.toLowerCase()];
21 }
22 return undefined;
23}
24function loadEnvironmentProxyValue() {
25 if (!process) {
26 return undefined;
27 }
28 var httpsProxy = getEnvironmentValue(Constants.HTTPS_PROXY);
29 var allProxy = getEnvironmentValue(Constants.ALL_PROXY);
30 var httpProxy = getEnvironmentValue(Constants.HTTP_PROXY);
31 return httpsProxy || allProxy || httpProxy;
32}
33// Check whether the host of a given `uri` is in the noProxyList.
34// If there's a match, any request sent to the same host won't have the proxy settings set.
35// This implementation is a port of https://github.com/Azure/azure-sdk-for-net/blob/8cca811371159e527159c7eb65602477898683e2/sdk/core/Azure.Core/src/Pipeline/Internal/HttpEnvironmentProxy.cs#L210
36function isBypassed(uri) {
37 if (noProxyList.length === 0) {
38 return false;
39 }
40 var host = URLBuilder.parse(uri).getHost();
41 if (byPassedList.has(host)) {
42 return byPassedList.get(host);
43 }
44 var isBypassedFlag = false;
45 for (var _i = 0, noProxyList_1 = noProxyList; _i < noProxyList_1.length; _i++) {
46 var pattern = noProxyList_1[_i];
47 if (pattern[0] === ".") {
48 // This should match either domain it self or any subdomain or host
49 // .foo.com will match foo.com it self or *.foo.com
50 if (host.endsWith(pattern)) {
51 isBypassedFlag = true;
52 }
53 else {
54 if (host.length === pattern.length - 1 && host === pattern.slice(1)) {
55 isBypassedFlag = true;
56 }
57 }
58 }
59 else {
60 if (host === pattern) {
61 isBypassedFlag = true;
62 }
63 }
64 }
65 byPassedList.set(host, isBypassedFlag);
66 return isBypassedFlag;
67}
68/**
69 * @internal
70 */
71export function loadNoProxy() {
72 var noProxy = getEnvironmentValue(Constants.NO_PROXY);
73 if (noProxy) {
74 return noProxy
75 .split(",")
76 .map(function (item) { return item.trim(); })
77 .filter(function (item) { return item.length; });
78 }
79 return [];
80}
81/**
82 * @internal
83 */
84function extractAuthFromUrl(url) {
85 var atIndex = url.indexOf("@");
86 if (atIndex === -1) {
87 return { urlWithoutAuth: url };
88 }
89 var schemeIndex = url.indexOf("://");
90 var authStart = schemeIndex !== -1 ? schemeIndex + 3 : 0;
91 var auth = url.substring(authStart, atIndex);
92 var colonIndex = auth.indexOf(":");
93 var hasPassword = colonIndex !== -1;
94 var username = hasPassword ? auth.substring(0, colonIndex) : auth;
95 var password = hasPassword ? auth.substring(colonIndex + 1) : undefined;
96 var urlWithoutAuth = url.substring(0, authStart) + url.substring(atIndex + 1);
97 return {
98 username: username,
99 password: password,
100 urlWithoutAuth: urlWithoutAuth,
101 };
102}
103export function getDefaultProxySettings(proxyUrl) {
104 if (!proxyUrl) {
105 proxyUrl = loadEnvironmentProxyValue();
106 if (!proxyUrl) {
107 return undefined;
108 }
109 }
110 var _a = extractAuthFromUrl(proxyUrl), username = _a.username, password = _a.password, urlWithoutAuth = _a.urlWithoutAuth;
111 var parsedUrl = URLBuilder.parse(urlWithoutAuth);
112 var schema = parsedUrl.getScheme() ? parsedUrl.getScheme() + "://" : "";
113 return {
114 host: schema + parsedUrl.getHost(),
115 port: Number.parseInt(parsedUrl.getPort() || "80"),
116 username: username,
117 password: password,
118 };
119}
120export function proxyPolicy(proxySettings) {
121 if (!proxySettings) {
122 proxySettings = getDefaultProxySettings();
123 }
124 return {
125 create: function (nextPolicy, options) {
126 return new ProxyPolicy(nextPolicy, options, proxySettings);
127 },
128 };
129}
130var ProxyPolicy = /** @class */ (function (_super) {
131 __extends(ProxyPolicy, _super);
132 function ProxyPolicy(nextPolicy, options, proxySettings) {
133 var _this = _super.call(this, nextPolicy, options) || this;
134 _this.proxySettings = proxySettings;
135 return _this;
136 }
137 ProxyPolicy.prototype.sendRequest = function (request) {
138 if (!request.proxySettings && !isBypassed(request.url)) {
139 request.proxySettings = this.proxySettings;
140 }
141 return this._nextPolicy.sendRequest(request);
142 };
143 return ProxyPolicy;
144}(BaseRequestPolicy));
145export { ProxyPolicy };
146//# sourceMappingURL=proxyPolicy.js.map
\No newline at end of file