This repository was archived by the owner on Nov 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFetchRequest.ts
More file actions
235 lines (215 loc) · 10.6 KB
/
FetchRequest.ts
File metadata and controls
235 lines (215 loc) · 10.6 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
namespace AdvancedServiceWorker {
export class FetchRequest {
messages: string[] = [];
request: Request;
response: Response = null;
rule: Rule;
cache: BrowserCache;
constructor(request: Request, rule: Rule, cache: BrowserCache) {
this.request = request;
this.rule = rule;
this.cache = cache;
}
message(message: string) {
this.messages.push(message);
}
wasSuccessful() {
return !!(this.response);
}
end(message: string, response?: Response): Response {
this.message(message);
if (!this.wasSuccessful()) {
this.response = response || null;
this.message("End of operation.");
}
return this.response;
}
fatal(message: string): Promise<Response> {
const error = new Error(message);
this.message(`Operation failed. ${error}`);
this.message(`Trying native fetch as the last resort.`);
this.response = null;
return fetch(this.request);
}
getResponse(): Promise<Response> {
switch (this.rule.strategy) {
case Strategies.NetworkFirst:
return this.strategyNetworkFirst();
case Strategies.CacheFirst:
return this.strategyCacheFirst();
case Strategies.NetworkOnly:
return this.strategyNetworkOnly();
case Strategies.CacheOnly:
return this.strategyCacheOnly();
case Strategies.Race:
return this.strategyRace();
case Strategies.Disable:
this.end("Disabled");
return null;
}
return null;
}
private strategyNetworkFirst(): Promise<Response> {
// load from network and if failed, try loading the cached version, if not available, serve the offline page
return this.getLive(this.request).then(
netResponse => {
this.message("Fetched from network.");
return this.cache.putResponse(this.request, netResponse.clone()).then(
() => this.end("Saved to cache.", netResponse),
(reason) => this.end(`Failed to save to cache. ${reason.toString()}`, netResponse)
);
},
(reason) => {
this.message(`Network failed, switching to cache. ${reason.toString()}`);
return this.cache.getResponse(this.request).then(
(cacheResponse) => this.end("Fetched from cache.", cacheResponse),
(reason) => {
this.message(`Cache failed, switching to offline page. ${reason.toString()}`);
if (!this.rule.offline) {
return this.fatal("Missing offline page.");
}
return this.cache.getResponse(this.rule.offline).then(
offlineResponse => this.end("Offline page served.", offlineResponse),
reason => this.fatal(`Cache failed for offline page. ${reason.toString()}`)
);
});
});
}
private strategyCacheFirst(): Promise<Response> {
// load from cache and if failed, try loading the live version from network,
// if not available, serve the offline page
return this.cache.getResponse(this.request).then(
cacheResponse => this.end("Fetched from cache.", cacheResponse),
(reason) => {
this.message(`Cache failed, switching to network. ${reason.toString()}`);
return this.getLive(this.request).then(netResponse => {
this.message("Fetched from network.");
return this.cache.putResponse(this.request, netResponse.clone()).then(
() => this.end("Saved to cache.", netResponse),
reason => this.end(`Failed to save to cache. ${reason.toString()}`,
netResponse)
);
},
(reason) => {
this.message(`Network failed, switching to offline page. ${reason.toString()}`);
if (!this.rule.offline) {
return this.fatal("Missing offline page.");
}
return this.cache.getResponse(this.rule.offline).then(
offlineResponse => this.end("Offline page served.", offlineResponse),
reason => this.fatal(`Cache failed for offline page. ${reason.toString()}`)
);
});
});
}
private strategyNetworkOnly(): Promise<Response> {
// load from network and if failed, serve the offline cached page
return this.getLive(this.request).then(
netResponse => this.end("Fetched from network.", netResponse),
reason => {
this.message(`Network failed, switching to offline page. ${reason.toString()}`);
if (!this.rule.offline) {
return this.fatal("Missing offline page.");
}
return this.cache.getResponse(this.rule.offline).then(
offlineResponse => this.end("Offline page served.", offlineResponse),
reason => this.fatal(`Cache failed for offline page. ${reason.toString()}`)
);
});
}
private strategyCacheOnly(): Promise<Response> {
// load from the cache and if failed, serve the offline cached page
return this.cache.getResponse(this.request).then(
cacheResponse => this.end("Fetched from cache.", cacheResponse),
reason => {
this.message(`Cache failed, switching to offline page. ${reason.toString()}`);
if (!this.rule.offline) {
return this.fatal("Missing offline page.");
}
return this.cache.getResponse(this.rule.offline).then(
offlineResponse => this.end("Offline page served.", offlineResponse),
reason => this.fatal(`Cache failed for offline page. ${reason.toString()}`)
);
}).catch(() => fetch(this.request));
}
private strategyRace(): Promise<Response> {
// try loading from network and on timeout or failure, load the cached version,
// if not available, serve the offline page
var live = this.getLive(this.request).then(
netResponse => {
this.message("Fetched from network.");
return this.cache.putResponse(this.request, netResponse.clone()).then(
() => this.end("Saved to cache.", netResponse),
(reason) => this.end(`Failed to save to cache. ${reason.toString()}`, netResponse)
);
});
var delay = this.rule.networkTimeout <= 0
? Promise.resolve({})
: new Promise(fulfill => {
setTimeout(
() => {
this.message(`Network delay reached, trying cache...`);
fulfill();
},
this.rule.networkTimeout);
});
return (new Promise((fulfill, reject) => {
var failed: number = 0;
live.then(
fulfill,
reason => {
// if net failed, try cache, even if timeout not yet reached
failed++;
// if cache already failed, reject
if (failed >= 2) {
reject(reason);
} else {
// force the cache if not yet started
this.cache.getResponse(this.request).then(
cacheResponse => fulfill(this.end("Fetched from cache.", cacheResponse)),
reject
);
}
});
delay.then(() =>
this.cache.getResponse(this.request).then(
cacheResponse => fulfill(this.end("Fetched from cache.", cacheResponse)),
reason => {
failed++;
// if net already failed, reject
if (failed >= 2) {
reject(reason);
}
}));
}).catch((reason) => {
this.message(`Race failed, switching to offline page. ${reason.toString()}`);
if (!this.rule.offline) {
return this.fatal("Missing offline page.");
}
return this.cache.getResponse(this.rule.offline).then(
offlineResponse => this.end("Offline page served.", offlineResponse),
reason => this.fatal(`Cache failed for offline page. ${reason.toString()}`)
);
})) as Promise<Response>;
}
private getLive(request: Request): Promise<Response> {
return fetch(request).then(
response => {
// in case of no network connectivity, throw
if (!response) {
throw new Error("Network is not accessible.");
}
// 500, 502, 503 and 504 statuses should be ignored and have the same effect as losing network connectivity
if (response.status === 408 ||
response.status === 500 ||
response.status === 502 ||
response.status === 503 ||
response.status === 504) {
throw new Error("Bad network response.");
}
// we accept all other status codes as valid
return response;
});
}
}
}