-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathwombatWorkers.js
449 lines (415 loc) · 13.4 KB
/
wombatWorkers.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/**
* Mini wombat for performing URL rewriting within the
* Web/Shared/Service Worker context
* @param {Object} info
* @return {WBWombat}
*/
function WBWombat(info) {
if (!(this instanceof WBWombat)) return new WBWombat(info);
/** @type {Object} */
this.info = info;
this.initImportScriptsRewrite();
this.initHTTPOverrides();
this.initClientApisOverride();
this.initCacheApisOverride();
}
/**
* Returns T/F indicating if the supplied URL is not to be rewritten
* @param {string} url
* @return {boolean}
*/
WBWombat.prototype.noRewrite = function(url) {
return (
!url ||
url.indexOf('blob:') === 0 ||
url.indexOf('javascript:') === 0 ||
url.indexOf('data:') === 0 ||
url.indexOf(this.info.prefix) === 0
);
};
/**
* Returns T/F indicating if the supplied URL is an relative URL
* @param {string} url
* @return {boolean}
*/
WBWombat.prototype.isRelURL = function(url) {
return url.indexOf('/') === 0 || (!url.startsWith('http:') && !url.startsWith('https:'));
};
/**
* Attempts to resolve the supplied relative URL against
* the origin this worker was created on
* @param {string} maybeRelURL
* @param {string} against
* @return {string}
*/
WBWombat.prototype.maybeResolveURL = function(maybeRelURL, against) {
if (!against) return maybeRelURL;
try {
var resolved = new URL(maybeRelURL, against);
return resolved.href;
} catch (e) {}
return maybeRelURL;
};
/**
* Returns null to indicate that the supplied URL is not to be rewritten.
* Otherwise returns a URL that can be rewritten
* @param {*} url
* @param {string} resolveAgainst
* @return {?string}
*/
WBWombat.prototype.ensureURL = function(url, resolveAgainst) {
if (!url) return url;
var newURL;
switch (typeof url) {
case 'string':
newURL = url;
break;
case 'object':
newURL = url.toString();
break;
default:
return null;
}
if (this.noRewrite(newURL)) return null;
if (this.isRelURL(newURL)) {
return this.maybeResolveURL(newURL, resolveAgainst);
}
// if url starts with current origin, but not properly rewritten, rewrite against current baseUr
if (newURL.indexOf(self.location.origin) === 0) {
return this.maybeResolveURL(newURL.slice(self.location.origin.length), resolveAgainst);
}
return newURL;
};
/**
* Rewrites the supplied URL
* @param {string} url
* @return {string}
*/
WBWombat.prototype.rewriteURL = function(url) {
var rwURL = this.ensureURL(url, this.info.originalURL);
if (!rwURL) return url;
if (this.info.prefixMod) {
return this.info.prefixMod + rwURL;
}
return rwURL;
};
/**
* Rewrites the supplied URL of an controlled page using the mp\_ modifier
* @param {string} url
* @param {WindowClient} [client]
* @return {string}
*/
WBWombat.prototype.rewriteClientWindowURL = function(url, client) {
var rwURL = this.ensureURL(url, client ? client.url : this.info.originalURL);
if (!rwURL) return url;
if (this.info.prefix) {
return this.info.prefix + 'mp_/' + rwURL;
}
return rwURL;
};
/**
* Mini url rewriter specifically for rewriting web sockets
* @param {?string} originalURL
* @return {string}
*/
WBWombat.prototype.rewriteWSURL = function(originalURL) {
// If undefined, just return it
if (!originalURL) return originalURL;
var urltype_ = typeof originalURL;
var url = originalURL;
// If object, use toString
if (urltype_ === 'object') {
url = originalURL.toString();
} else if (urltype_ !== 'string') {
return originalURL;
}
// empty string check
if (!url) return url;
var wsScheme = 'ws://';
var wssScheme = 'wss://';
var https = 'https://';
var wbSecure = this.info.prefix.indexOf(https) === 0;
var wbPrefix =
this.info.prefix.replace(
wbSecure ? https : 'http://',
wbSecure ? wssScheme : wsScheme
) + 'ws_/';
return wbPrefix + url;
};
/**
* Rewrites all URLs in the supplied arguments object
* @param {Object} argsObj
* @return {Array<string>}
*/
WBWombat.prototype.rewriteArgs = function(argsObj) {
// recreate the original arguments object just with URLs rewritten
var newArgObj = new Array(argsObj.length);
for (var i = 0; i < newArgObj.length; i++) {
newArgObj[i] = this.rewriteURL(argsObj[i]);
}
return newArgObj;
};
/**
* Rewrites the input to one of the Fetch APIs
* @param {*|string|Request} input
* @return {*|string|Request}
*/
WBWombat.prototype.rewriteFetchApi = function(input) {
var rwInput = input;
switch (typeof input) {
case 'string':
rwInput = this.rewriteURL(input);
break;
case 'object':
if (input.url) {
var new_url = this.rewriteURL(input.url);
if (new_url !== input.url) {
// not much we can do here Request.url is read only
// https://developer.mozilla.org/en-US/docs/Web/API/Request/url
rwInput = new Request(new_url, input);
}
} else if (input.href) {
// it is likely that input is either self.location or self.URL
// we cant do anything here so just let it go
rwInput = input.href;
}
break;
}
return rwInput;
};
/**
* Rewrites the input to one of the Cache APIs
* @param {*|string|Request} request
* @return {*|string|Request}
*/
WBWombat.prototype.rewriteCacheApi = function(request) {
var rwRequest = request;
if (typeof request === 'string') {
rwRequest = this.rewriteURL(request);
}
return rwRequest;
};
/**
* Applies an override to the importScripts function
* @see https://html.spec.whatwg.org/multipage/workers.html#dom-workerglobalscope-importscripts
*/
WBWombat.prototype.initImportScriptsRewrite = function() {
if (!self.importScripts) return;
var wombat = this;
var origImportScripts = self.importScripts;
self.importScripts = function importScripts() {
// rewrite the arguments object and call original function via fn.apply
var rwArgs = wombat.rewriteArgs(arguments);
return origImportScripts.apply(this, rwArgs);
};
};
/**
* Applies overrides to the XMLHttpRequest.open and XMLHttpRequest.responseURL
* in order to ensure URLs are rewritten.
*
* Applies an override to window.fetch in order to rewrite URLs and URLs of
* the supplied Request objects used as arguments to fetch.
*
* Applies overrides to window.Request, window.Response, window.EventSource,
* and window.WebSocket in order to ensure URLs they operate on are rewritten.
*
* @see https://xhr.spec.whatwg.org/
* @see https://fetch.spec.whatwg.org/
* @see https://html.spec.whatwg.org/multipage/web-sockets.html#websocket
* @see https://html.spec.whatwg.org/multipage/server-sent-events.html#the-eventsource-interface
*/
WBWombat.prototype.initHTTPOverrides = function() {
var wombat = this;
if (
self.XMLHttpRequest &&
self.XMLHttpRequest.prototype &&
self.XMLHttpRequest.prototype.open
) {
var oXHROpen = self.XMLHttpRequest.prototype.open;
self.XMLHttpRequest.prototype.open = function open(
method,
url,
async,
user,
password
) {
var rwURL = wombat.rewriteURL(url);
var openAsync = true;
if (async != null && !async) openAsync = false;
oXHROpen.call(this, method, rwURL, openAsync, user, password);
if (rwURL.indexOf('data:') === -1) {
this.setRequestHeader('X-Pywb-Requested-With', 'XMLHttpRequest');
}
};
}
if (self.fetch != null) {
// this fetch is Worker.fetch
var orig_fetch = self.fetch;
self.fetch = function fetch(input, init_opts) {
var rwInput = wombat.rewriteFetchApi(input);
var newInitOpts = init_opts || {};
newInitOpts['credentials'] = 'include';
return orig_fetch.call(this, rwInput, newInitOpts);
};
}
if (self.Request && self.Request.prototype) {
var orig_request = self.Request;
self.Request = (function(Request_) {
return function Request(input, init_opts) {
var newInitOpts = init_opts || {};
var newInput = wombat.rewriteFetchApi(input);
newInitOpts['credentials'] = 'include';
return new Request_(newInput, newInitOpts);
};
})(self.Request);
self.Request.prototype = orig_request.prototype;
}
if (self.Response && self.Response.prototype) {
var originalRedirect = self.Response.prototype.redirect;
self.Response.prototype.redirect = function redirect(url, status) {
var rwURL = wombat.rewriteUrl(url);
return originalRedirect.call(this, rwURL, status);
};
}
if (self.EventSource && self.EventSource.prototype) {
var origEventSource = self.EventSource;
self.EventSource = (function(EventSource_) {
return function EventSource(url, configuration) {
var rwURL = url;
if (url != null) {
rwURL = wombat.rewriteUrl(url);
}
return new EventSource_(rwURL, configuration);
};
})(self.EventSource);
self.EventSource.prototype = origEventSource.prototype;
Object.defineProperty(self.EventSource.prototype, 'constructor', {
value: self.EventSource
});
}
if (self.WebSocket && self.WebSocket.prototype) {
var origWebSocket = self.WebSocket;
self.WebSocket = (function(WebSocket_) {
return function WebSocket(url, configuration) {
var rwURL = url;
if (url != null) {
rwURL = wombat.rewriteWSURL(url);
}
return new WebSocket_(rwURL, configuration);
};
})(self.WebSocket);
self.WebSocket.prototype = origWebSocket.prototype;
Object.defineProperty(self.WebSocket.prototype, 'constructor', {
value: self.WebSocket
});
}
};
/**
* Applies an override to Clients.openWindow and WindowClient.navigate that rewrites
* the supplied URL that represents a controlled window
* @see https://w3c.github.io/ServiceWorker/#window-client-interface
* @see https://w3c.github.io/ServiceWorker/#clients-interface
*/
WBWombat.prototype.initClientApisOverride = function() {
var wombat = this;
if (
self.Clients &&
self.Clients.prototype &&
self.Clients.prototype.openWindow
) {
var oClientsOpenWindow = self.Clients.prototype.openWindow;
self.Clients.prototype.openWindow = function openWindow(url) {
var rwURL = wombat.rewriteClientWindowURL(url);
return oClientsOpenWindow.call(this, rwURL);
};
}
if (
self.WindowClient &&
self.WindowClient.prototype &&
self.WindowClient.prototype.navigate
) {
var oWinClientNavigate = self.WindowClient.prototype.navigate;
self.WindowClient.prototype.navigate = function navigate(url) {
var rwURL = wombat.rewriteClientWindowURL(url, this);
return oWinClientNavigate.call(this, rwURL);
};
}
};
/**
* Applies overrides to the CacheStorage and Cache interfaces in order
* to rewrite the URLs they operate on
* @see https://w3c.github.io/ServiceWorker/#cachestorage
* @see https://w3c.github.io/ServiceWorker/#cache-interface
*/
WBWombat.prototype.initCacheApisOverride = function() {
var wombat = this;
if (
self.CacheStorage &&
self.CacheStorage.prototype &&
self.CacheStorage.prototype.match
) {
var oCacheStorageMatch = self.CacheStorage.prototype.match;
self.CacheStorage.prototype.match = function match(request, options) {
var rwRequest = wombat.rewriteCacheApi(request);
return oCacheStorageMatch.call(this, rwRequest, options);
};
}
if (self.Cache && self.Cache.prototype) {
if (self.Cache.prototype.match) {
var oCacheMatch = self.Cache.prototype.match;
self.Cache.prototype.match = function match(request, options) {
var rwRequest = wombat.rewriteCacheApi(request);
return oCacheMatch.call(this, rwRequest, options);
};
}
if (self.Cache.prototype.matchAll) {
var oCacheMatchAll = self.Cache.prototype.matchAll;
self.Cache.prototype.matchAll = function matchAll(request, options) {
var rwRequest = wombat.rewriteCacheApi(request);
return oCacheMatchAll.call(this, rwRequest, options);
};
}
if (self.Cache.prototype.add) {
var oCacheAdd = self.Cache.prototype.add;
self.Cache.prototype.add = function add(request, options) {
var rwRequest = wombat.rewriteCacheApi(request);
return oCacheAdd.call(this, rwRequest, options);
};
}
if (self.Cache.prototype.addAll) {
var oCacheAddAll = self.Cache.prototype.addAll;
self.Cache.prototype.addAll = function addAll(requests) {
var rwRequests = requests;
if (Array.isArray(requests)) {
rwRequests = new Array(requests.length);
for (var i = 0; i < requests.length; i++) {
rwRequests[i] = wombat.rewriteCacheApi(requests[i]);
}
}
return oCacheAddAll.call(this, rwRequests);
};
}
if (self.Cache.prototype.put) {
var oCachePut = self.Cache.prototype.put;
self.Cache.prototype.put = function put(request, response) {
var rwRequest = wombat.rewriteCacheApi(request);
return oCachePut.call(this, rwRequest, response);
};
}
if (self.Cache.prototype.delete) {
var oCacheDelete = self.Cache.prototype.delete;
self.Cache.prototype.delete = function newCacheDelete(request, options) {
var rwRequest = wombat.rewriteCacheApi(request);
return oCacheDelete.call(this, rwRequest, options);
};
}
if (self.Cache.prototype.keys) {
var oCacheKeys = self.Cache.prototype.keys;
self.Cache.prototype.keys = function keys(request, options) {
var rwRequest = wombat.rewriteCacheApi(request);
return oCacheKeys.call(this, rwRequest, options);
};
}
}
};
self.WBWombat = WBWombat;