/
StorageBrowserPolicy.ts
68 lines (60 loc) · 2.02 KB
/
StorageBrowserPolicy.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. All rights reserved.
// Licensed under the MIT License.
import {
BaseRequestPolicy,
HttpOperationResponse,
isNode,
RequestPolicy,
RequestPolicyOptions,
WebResource
} from "@azure/core-http";
import { HeaderConstants, URLConstants } from "../utils/constants";
import { setURLParameter } from "../utils/utils.common";
/**
* StorageBrowserPolicy will handle differences between Node.js and browser runtime, including:
*
* 1. Browsers cache GET/HEAD requests by adding conditional headers such as 'IF_MODIFIED_SINCE'.
* StorageBrowserPolicy is a policy used to add a timestamp query to GET/HEAD request URL
* thus avoid the browser cache.
*
* 2. Remove cookie header for security
*
* 3. Remove content-length header to avoid browsers warning
*
* @class StorageBrowserPolicy
* @extends {BaseRequestPolicy}
*/
export class StorageBrowserPolicy extends BaseRequestPolicy {
/**
* Creates an instance of StorageBrowserPolicy.
* @param {RequestPolicy} nextPolicy
* @param {RequestPolicyOptions} options
* @memberof StorageBrowserPolicy
*/
constructor(nextPolicy: RequestPolicy, options: RequestPolicyOptions) {
super(nextPolicy, options);
}
/**
* Sends out request.
*
* @param {WebResource} request
* @returns {Promise<HttpOperationResponse>}
* @memberof StorageBrowserPolicy
*/
public async sendRequest(request: WebResource): Promise<HttpOperationResponse> {
if (isNode) {
return this._nextPolicy.sendRequest(request);
}
if (request.method.toUpperCase() === "GET" || request.method.toUpperCase() === "HEAD") {
request.url = setURLParameter(
request.url,
URLConstants.Parameters.FORCE_BROWSER_NO_CACHE,
new Date().getTime().toString()
);
}
request.headers.remove(HeaderConstants.COOKIE);
// According to XHR standards, content-length should be fully controlled by browsers
request.headers.remove(HeaderConstants.CONTENT_LENGTH);
return this._nextPolicy.sendRequest(request);
}
}