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
|
// By default, this worker responds to fetch events with
// respondWith(fetch(request)). Additionally, if the request has a &url
// parameter, it fetches the provided URL instead. Because it forwards fetch
// events to this other URL, it is called the "fetch rewrite" worker.
//
// The worker also looks for other params on the request to do more custom
// behavior, like falling back to network or throwing an error.
function get_query_params(url) {
var search = (new URL(url)).search;
if (!search) {
return {};
}
var ret = {};
var params = search.substring(1).split('&');
params.forEach(function(param) {
var element = param.split('=');
ret[decodeURIComponent(element[0])] = decodeURIComponent(element[1]);
});
return ret;
}
function get_request_init(base, params) {
var init = {};
init['method'] = params['method'] || base['method'];
init['mode'] = params['mode'] || base['mode'];
if (init['mode'] == 'navigate') {
init['mode'] = 'same-origin';
}
init['credentials'] = params['credentials'] || base['credentials'];
init['redirect'] = params['redirect-mode'] || base['redirect'];
return init;
}
self.addEventListener('fetch', function(event) {
var params = get_query_params(event.request.url);
var init = get_request_init(event.request, params);
var url = params['url'];
if (params['ignore']) {
return;
}
if (params['throw']) {
throw new Error('boom');
}
if (params['reject']) {
event.respondWith(new Promise(function(resolve, reject) {
reject();
}));
return;
}
if (params['resolve-null']) {
event.respondWith(new Promise(function(resolve) {
resolve(null);
}));
return;
}
if (params['generate-png']) {
var binary = atob(
'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAA' +
'RnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAAhSURBVDhPY3wro/Kf' +
'gQLABKXJBqMGjBoAAqMGDLwBDAwAEsoCTFWunmQAAAAASUVORK5CYII=');
var array = new Uint8Array(binary.length);
for(var i = 0; i < binary.length; i++) {
array[i] = binary.charCodeAt(i);
};
event.respondWith(new Response(new Blob([array], {type: 'image/png'})));
return;
}
if (params['check-ua-header']) {
var ua = event.request.headers.get('User-Agent');
if (ua) {
// We have a user agent!
event.respondWith(new Response(new Blob([ua])));
} else {
// We don't have a user-agent!
event.respondWith(new Response(new Blob(["NO_UA"])));
}
return;
}
if (params['check-accept-header']) {
var accept = event.request.headers.get('Accept');
if (accept) {
event.respondWith(new Response(accept));
} else {
event.respondWith(new Response('NO_ACCEPT'));
}
return;
}
event.respondWith(new Promise(function(resolve, reject) {
var request = event.request;
if (url) {
request = new Request(url, init);
} else if (params['change-request']) {
request = new Request(request, init);
}
const response_promise = params['navpreload'] ? event.preloadResponse
: fetch(request);
response_promise.then(function(response) {
var expectedType = params['expected_type'];
if (expectedType && response.type !== expectedType) {
// Resolve a JSON object with a failure instead of rejecting
// in order to distinguish this from a NetworkError, which
// may be expected even if the type is correct.
resolve(new Response(JSON.stringify({
result: 'failure',
detail: 'got ' + response.type + ' Response.type instead of ' +
expectedType
})));
}
var expectedRedirected = params['expected_redirected'];
if (typeof expectedRedirected !== 'undefined') {
var expected_redirected = (expectedRedirected === 'true');
if(response.redirected !== expected_redirected) {
// This is simply determining how to pass an error to the outer
// test case(fetch-request-redirect.https.html).
var execptedResolves = params['expected_resolves'];
if (execptedResolves === 'true') {
// Reject a JSON object with a failure since promise is expected
// to be resolved.
reject(new Response(JSON.stringify({
result: 'failure',
detail: 'got '+ response.redirected +
' Response.redirected instead of ' +
expectedRedirected
})));
} else {
// Resolve a JSON object with a failure since promise is
// expected to be rejected.
resolve(new Response(JSON.stringify({
result: 'failure',
detail: 'got '+ response.redirected +
' Response.redirected instead of ' +
expectedRedirected
})));
}
}
}
if (params['clone']) {
response = response.clone();
}
// |cache| means to bounce responses through Cache Storage and back.
if (params['cache']) {
var cacheName = "cached-fetches-" + performance.now() + "-" +
event.request.url;
var cache;
var cachedResponse;
return self.caches.open(cacheName).then(function(opened) {
cache = opened;
return cache.put(request, response);
}).then(function() {
return cache.match(request);
}).then(function(cached) {
cachedResponse = cached;
return self.caches.delete(cacheName);
}).then(function() {
resolve(cachedResponse);
});
} else {
resolve(response);
}
}, reject)
}));
});
|