/
TelemetryPolicyFactory.ts
68 lines (59 loc) · 1.86 KB
/
TelemetryPolicyFactory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import {
isNode,
RequestPolicy,
RequestPolicyFactory,
RequestPolicyOptions,
UserAgentOptions,
} from "@azure/core-http";
import * as os from "os";
import { TelemetryPolicy } from "./policies/TelemetryPolicy";
import { SDK_VERSION } from "./utils/constants";
/**
* TelemetryPolicyFactory is a factory class helping generating {@link TelemetryPolicy} objects.
*/
export class TelemetryPolicyFactory implements RequestPolicyFactory {
/**
* @internal
*/
public readonly telemetryString: string;
/**
* Creates an instance of TelemetryPolicyFactory.
* @param telemetry -
*/
constructor(telemetry?: UserAgentOptions) {
const userAgentInfo: string[] = [];
if (isNode) {
if (telemetry) {
const telemetryString = telemetry.userAgentPrefix || "";
if (telemetryString.length > 0 && userAgentInfo.indexOf(telemetryString) === -1) {
userAgentInfo.push(telemetryString);
}
}
// e.g. azsdk-js-storageblob/10.0.0
const libInfo = `azsdk-js-storageblob/${SDK_VERSION}`;
if (userAgentInfo.indexOf(libInfo) === -1) {
userAgentInfo.push(libInfo);
}
// e.g. (NODE-VERSION 4.9.1; Windows_NT 10.0.16299)
let runtimeInfo = `(NODE-VERSION ${process.version})`;
if (os) {
runtimeInfo = `(NODE-VERSION ${process.version}; ${os.type()} ${os.release()})`;
}
if (userAgentInfo.indexOf(runtimeInfo) === -1) {
userAgentInfo.push(runtimeInfo);
}
}
this.telemetryString = userAgentInfo.join(" ");
}
/**
* Creates a TelemetryPolicy object.
*
* @param nextPolicy -
* @param options -
*/
public create(nextPolicy: RequestPolicy, options: RequestPolicyOptions): TelemetryPolicy {
return new TelemetryPolicy(nextPolicy, options, this.telemetryString);
}
}