forked from awslabs/aws-js-s3-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw_modify_header.js
60 lines (57 loc) · 2.3 KB
/
sw_modify_header.js
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
// Install Service Worker
self.addEventListener('install', (e) => {
console.log('Service Worker: Installed');
})
// Activate Service Worker
self.addEventListener('activate', (e) => {
console.log('Service Worker: Activated');
})
/**
* Add fetch event for service worker
*
* The AWS client automatically generates an Authorization request
* header. On the AAW environment, we have an EnvoyFilter that appears
* to automatically reject requests that have authorization headers
* other than 'cookie' or 'x-auth-token'. To avoid significantly refactoring
* the code for aws-js-explorer, this service worker acts as a
* 'middleware' on the client side to modify outgoing requests from
* the aws-js-explorer application and remove the erroneous Authorization
* header.
*/
self.addEventListener('fetch', (e) => {
// Any requests to s3proxy need to have the erroneous authorization header removed.
const headers = new Headers(e.request.headers);
headers.delete('authorization');
// If the request is not an AJAX call to one of the s3proxy buckets, do not
// modify the request.
if (!(e.request.url.includes('/aaw-unclassified')) && !(e.request.url.includes('/aaw-unclassified-ro')) && !(e.request.url.includes('/aaw-protected-b'))) {
e.respondWith(fetch(e.request));
}
// If the request is not a POST or a PUT request, forward the request with the
// modified headers
else if (!(e.request.method == 'PUT') && !(e.request.method == 'POST')) {
const req = new Request(e.request.url, {
headers,
method: e.request.method,
mode: e.request.mode,
credentials: e.request.credentials,
redirect: e.request.redirect
});
e.respondWith(fetch(req));
}
else {
// At this point, the user is trying to upload a file as either a single-part upload or
// a multi-part upload.
const promiseChain = e.request.arrayBuffer().then((originalBody) => {
return fetch(e.request.url, {
headers,
body: originalBody,
method: e.request.method,
mode: e.request.mode,
credentials: e.request.credentials,
redirect: e.request.redirect
});
});
e.respondWith(promiseChain);
}
});