Permalink
Cannot retrieve contributors at this time
88 lines (75 sloc)
2.62 KB
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
codeql-action/node_modules/@azure/ms-rest-js/lib/policies/userAgentPolicy.ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) Microsoft Corporation. All rights reserved. | |
// Licensed under the MIT License. See License.txt in the project root for license information. | |
import { HttpHeaders } from "../httpHeaders"; | |
import { HttpOperationResponse } from "../httpOperationResponse"; | |
import { Constants } from "../util/constants"; | |
import { WebResourceLike } from "../webResource"; | |
import { getDefaultUserAgentKey, getPlatformSpecificData } from "./msRestUserAgentPolicy"; | |
import { | |
BaseRequestPolicy, | |
RequestPolicy, | |
RequestPolicyFactory, | |
RequestPolicyOptionsLike, | |
} from "./requestPolicy"; | |
export type TelemetryInfo = { key?: string; value?: string }; | |
function getRuntimeInfo(): TelemetryInfo[] { | |
const msRestRuntime = { | |
key: "ms-rest-js", | |
value: Constants.msRestVersion, | |
}; | |
return [msRestRuntime]; | |
} | |
function getUserAgentString( | |
telemetryInfo: TelemetryInfo[], | |
keySeparator = " ", | |
valueSeparator = "/" | |
): string { | |
return telemetryInfo | |
.map((info) => { | |
const value = info.value ? `${valueSeparator}${info.value}` : ""; | |
return `${info.key}${value}`; | |
}) | |
.join(keySeparator); | |
} | |
export const getDefaultUserAgentHeaderName = getDefaultUserAgentKey; | |
export function getDefaultUserAgentValue(): string { | |
const runtimeInfo = getRuntimeInfo(); | |
const platformSpecificData = getPlatformSpecificData(); | |
const userAgent = getUserAgentString(runtimeInfo.concat(platformSpecificData)); | |
return userAgent; | |
} | |
export function userAgentPolicy(userAgentData?: TelemetryInfo): RequestPolicyFactory { | |
const key: string = | |
!userAgentData || userAgentData.key == undefined ? getDefaultUserAgentKey() : userAgentData.key; | |
const value: string = | |
!userAgentData || userAgentData.value == undefined | |
? getDefaultUserAgentValue() | |
: userAgentData.value; | |
return { | |
create: (nextPolicy: RequestPolicy, options: RequestPolicyOptionsLike) => { | |
return new UserAgentPolicy(nextPolicy, options, key, value); | |
}, | |
}; | |
} | |
export class UserAgentPolicy extends BaseRequestPolicy { | |
constructor( | |
readonly _nextPolicy: RequestPolicy, | |
readonly _options: RequestPolicyOptionsLike, | |
protected headerKey: string, | |
protected headerValue: string | |
) { | |
super(_nextPolicy, _options); | |
} | |
sendRequest(request: WebResourceLike): Promise<HttpOperationResponse> { | |
this.addUserAgentHeader(request); | |
return this._nextPolicy.sendRequest(request); | |
} | |
addUserAgentHeader(request: WebResourceLike): void { | |
if (!request.headers) { | |
request.headers = new HttpHeaders(); | |
} | |
if (!request.headers.get(this.headerKey) && this.headerValue) { | |
request.headers.set(this.headerKey, this.headerValue); | |
} | |
} | |
} |