/
Pipeline.ts
205 lines (191 loc) · 6.26 KB
/
Pipeline.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import {
BaseRequestPolicy,
bearerTokenAuthenticationPolicy,
deserializationPolicy,
disableResponseDecompressionPolicy,
generateClientRequestIdPolicy,
HttpClient as IHttpClient,
HttpHeaders,
HttpOperationResponse,
HttpRequestBody,
isNode,
isTokenCredential,
KeepAliveOptions,
keepAlivePolicy,
logPolicy,
ProxyOptions,
proxyPolicy,
RequestPolicy,
RequestPolicyFactory,
RequestPolicyOptions,
ServiceClientOptions,
TokenCredential,
tracingPolicy,
UserAgentOptions,
WebResource
} from "@azure/core-http";
import { AnonymousCredential } from "./credentials/AnonymousCredential";
import { StorageSharedKeyCredential } from "./credentials/StorageSharedKeyCredential";
import { logger } from "./log";
import { StorageBrowserPolicyFactory } from "./StorageBrowserPolicyFactory";
import { StorageRetryOptions, StorageRetryPolicyFactory } from "./StorageRetryPolicyFactory";
import { TelemetryPolicyFactory } from "./TelemetryPolicyFactory";
import {
StorageDataLakeLoggingAllowedHeaderNames,
StorageDataLakeLoggingAllowedQueryParameters,
StorageOAuthScopes
} from "./utils/constants";
import { getCachedDefaultHttpClient } from "./utils/cache";
// Export following interfaces and types for customers who want to implement their
// own RequestPolicy or HTTPClient
export {
BaseRequestPolicy,
StorageOAuthScopes,
deserializationPolicy,
IHttpClient,
HttpHeaders,
HttpRequestBody,
HttpOperationResponse,
WebResource,
RequestPolicyFactory,
RequestPolicy,
RequestPolicyOptions
};
/**
* Option interface for Pipeline constructor.
*/
export interface PipelineOptions {
/**
* Optional. Configures the HTTP client to send requests and receive responses.
*/
httpClient?: IHttpClient;
}
/**
* A Pipeline class containing HTTP request policies.
* You can create a default Pipeline by calling {@link newPipeline}.
* Or you can create a Pipeline with your own policies by the constructor of Pipeline.
*
* Refer to {@link newPipeline} and provided policies before implementing your
* customized Pipeline.
*/
export class Pipeline {
/**
* A list of chained request policy factories.
*/
public readonly factories: RequestPolicyFactory[];
/**
* Configures pipeline logger and HTTP client.
*/
public readonly options: PipelineOptions;
/**
* Creates an instance of Pipeline. Customize HTTPClient by implementing IHttpClient interface.
*
* @param factories -
* @param options -
*/
constructor(factories: RequestPolicyFactory[], options: PipelineOptions = {}) {
this.factories = factories;
// when options.httpClient is not specified, passing in a DefaultHttpClient instance to
// avoid each client creating its own http client.
this.options = {
...options,
httpClient: options.httpClient || getCachedDefaultHttpClient()
};
}
/**
* Transfer Pipeline object to ServiceClientOptions object which is required by
* ServiceClient constructor.
*
* @returns The ServiceClientOptions object from this Pipeline.
*/
public toServiceClientOptions(): ServiceClientOptions {
return {
httpClient: this.options.httpClient,
requestPolicyFactories: this.factories
};
}
}
/**
* Options interface for the {@link newPipeline} function.
*/
export interface StoragePipelineOptions {
/**
* Options to configure a proxy for outgoing requests.
*/
proxyOptions?: ProxyOptions;
/**
* Options for adding user agent details to outgoing requests.
*/
userAgentOptions?: UserAgentOptions;
/**
* Configures the built-in retry policy behavior.
*/
retryOptions?: StorageRetryOptions;
/**
* Keep alive configurations. Default keep-alive is enabled.
*/
keepAliveOptions?: KeepAliveOptions;
/**
* Configures the HTTP client to send requests and receive responses.
*/
httpClient?: IHttpClient;
}
/**
* Creates a new Pipeline object with Credential provided.
*
* @param credential - Such as AnonymousCredential, StorageSharedKeyCredential or any credential from the `@azure/identity` package to authenticate requests to the service. You can also provide an object that implements the TokenCredential interface. If not specified, AnonymousCredential is used.
* @param pipelineOptions - Optional. Options.
* @returns A new Pipeline object.
*/
export function newPipeline(
credential?: StorageSharedKeyCredential | AnonymousCredential | TokenCredential,
pipelineOptions: StoragePipelineOptions = {}
): Pipeline {
if (credential === undefined) {
credential = new AnonymousCredential();
}
// Order is important. Closer to the API at the top & closer to the network at the bottom.
// The credential's policy factory must appear close to the wire so it can sign any
// changes made by other factories (like UniqueRequestIDPolicyFactory)
const telemetryPolicy = new TelemetryPolicyFactory(pipelineOptions.userAgentOptions);
const factories: RequestPolicyFactory[] = [
tracingPolicy({ userAgent: telemetryPolicy.telemetryString }),
keepAlivePolicy(pipelineOptions.keepAliveOptions),
telemetryPolicy,
generateClientRequestIdPolicy(),
new StorageBrowserPolicyFactory(),
new StorageRetryPolicyFactory(pipelineOptions.retryOptions), // Retry policy should be above any policy that throws retryable errors
deserializationPolicy(), // Default deserializationPolicy is provided by protocol layer
logPolicy({
logger: logger.info,
allowedHeaderNames: StorageDataLakeLoggingAllowedHeaderNames,
allowedQueryParameters: StorageDataLakeLoggingAllowedQueryParameters
})
];
if (isNode) {
// policies only available in Node.js runtime, not in browsers
factories.push(proxyPolicy(pipelineOptions.proxyOptions));
factories.push(disableResponseDecompressionPolicy());
}
factories.push(
isTokenCredential(credential)
? attachCredential(
bearerTokenAuthenticationPolicy(credential, StorageOAuthScopes),
credential
)
: credential
);
return new Pipeline(factories, pipelineOptions);
}
/**
* Attach a TokenCredential to an object.
*
* @param thing -
* @param credential -
*/
function attachCredential<T>(thing: T, credential: TokenCredential): T {
(thing as any).credential = credential;
return thing;
}