/
StorageBrowserPolicy.ts
64 lines (56 loc) · 1.95 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
// Copyright (c) Microsoft Corporation.
// 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
*/
export class StorageBrowserPolicy extends BaseRequestPolicy {
/**
* Creates an instance of StorageBrowserPolicy.
* @param nextPolicy -
* @param options -
*/
// The base class has a protected constructor. Adding a public one to enable constructing of this class.
/* eslint-disable-next-line @typescript-eslint/no-useless-constructor*/
constructor(nextPolicy: RequestPolicy, options: RequestPolicyOptions) {
super(nextPolicy, options);
}
/**
* Sends out request.
*
* @param request -
*/
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);
}
}