-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdex-opt.js
More file actions
225 lines (203 loc) · 8.95 KB
/
Copy pathdex-opt.js
File metadata and controls
225 lines (203 loc) · 8.95 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
var db;
function isFirstPartyDomain(url) {
var parsedURL = new URL(url);
var firstPartyDomain = dexecure.firstPartyDomain;
for (var i = firstPartyDomain.length - 1; i >= 0; i--) {
if (firstPartyDomain[i].toLowerCase() == parsedURL.host.toLowerCase()) {
return true;
}
}
return false;
}
function changeToDexecureURL(inputURL) {
var changedURL = new URL(inputURL);
dexecure.debugMode && console.log('inputURL is ', inputURL);
dexecure.debugMode && console.log('inputURL.hostname is ', inputURL.hostname)
changedURL.hostname = dexecure.server[changedURL.hostname];
dexecure.debugMode && console.log('changedURL.hostname is ', changedURL.hostname)
dexecure.debugMode && console.log('changedURL is ', changedURL.href)
return changedURL.href;
}
function isCloudflareURL(inputURL) {
return new URL(inputURL).pathname.indexOf('/cdn-cgi/') == 0;
}
function isPageEnabled(referrerURL) {
if (!referrerURL) {
if(dexecure.debugMode)
console.log('returning isPageEnabled false due to no referrer')
return false;
}
var parsedURL = new URL(referrerURL);
var urlToMatchAgainst = parsedURL.origin + parsedURL.pathname;
if(dexecure.debugMode) {
console.log('parsed URL is', urlToMatchAgainst)
console.log('matching URL is ', referrerURL);
}
for (var i = dexecure.pagesEnabled.length - 1; i >= 0; i--) {
if (urlToMatchAgainst.indexOf(dexecure.pagesEnabled[i]) == 0) {
if (dexecure.debugMode)
console.log('dexecure.pagesEnabled[i] is ', dexecure.pagesEnabled[i]);
return true;
} else {
if (dexecure.debugMode) {
console.log('dexecure.pagesEnabled[i] does not match ', dexecure.pagesEnabled[i])
console.log('parsedURL.hostname + parsedURL.pathname.indexOf(dexecure.pagesEnabled[i]) is ', (urlToMatchAgainst).indexOf(pagesEnabled[i]));
}
}
}
return true;
}
function getIDB() {
return new Promise((resolve, reject) => {
if (db) {
dexecure.debugMode && console.log("returning existing IDB instance");
resolve(db);
} else {
var request = indexedDB.open("Dexecure", 1);
request.onerror = function(event) {
dexecure.debugMode && console.log("error: ");
reject(request.error);
};
request.onsuccess = function(event) {
db = request.result;
resolve(request.result);
};
}
});
}
function getBrotliStatus() {
return getIDB()
.then((db) => {
return new Promise((resolve, reject) => {
var transaction = db.transaction("config");
var objectStore = transaction.objectStore("config");
var request = objectStore.get(1);
request.onerror = function(event) {
// safely asssume brotli not supported
reject(false);
};
request.onsuccess = function(event) {
dexecure.debugMode && console.log('reading from idb', event.target.result.isBrotliSupported);
resolve(event.target.result.isBrotliSupported);
};
});
})
}
if (dexecure.optimisationsEnabled) {
self.addEventListener('install', function(event) {
if (dexecure.debugMode)
console.log('install triggered');
event.waitUntil(
fetch('https://blog.dexecure.com/content/images/perm/test.br')
.then((res) => {
if (res.ok) {
return res.text();
}
})
.then((resp) => {
// check if the browser was able to decode the Brotli response correctly
var isBrotliSupported = (resp === 'Dexecure');
return isBrotliSupported;
})
.catch(() => {
// assume brotli is not supported if request fails to be safe
var isBrotliSupported = false;
return isBrotliSupported;
})
.then((isBrotliSupported) => {
return new Promise((resolve, reject) => {
var request = indexedDB.open("Dexecure", 1);
request.onerror = function(event) {
dexecure.debugMode && console.log("error: ");
reject(request.error);
};
request.onsuccess = function(event) {
db = request.result;
var transaction = db.transaction("config", "readwrite");
var store = transaction.objectStore("config");
var config = {
isBrotliSupported: isBrotliSupported
}
var request2 = store.put(config, 1);
request2.onsuccess = function() {
resolve();
}
};
request.onupgradeneeded = function(e) {
var thisDB = e.target.result;
if (!thisDB.objectStoreNames.contains("config")) {
thisDB.createObjectStore("config");
}
}
});
})
.then(() => self.skipWaiting())
);
// event.waitUntil(self.skipWaiting());
});
self.addEventListener('activate', function(event) {
event.waitUntil(self.clients.claim());
});
self.addEventListener('fetch', function(event) {
if (!event.clientId) return fetch(e.request);
if (dexecure.debugMode)
console.log('fetch triggered');
return self.clients.get(event.clientId).then(client => {
const clientURL = new URL(client.url);
if (!isPageEnabled(clientURL.pathname)) {
return fetch(event.request);
}
var imageMatchRegex = new RegExp(dexecure.imageMatchRegex, "i");
var textMatchRegex = new RegExp(dexecure.textMatchRegex, "i");
var isImageRequest = imageMatchRegex.test(event.request.url.toLowerCase());
var isTextRequest = textMatchRegex.test(event.request.url.toLowerCase());
if (dexecure.debugMode)
console.log('input url is ', event.request.url);
if (event.request.method != 'GET') {
return;
}
var shouldOptimize = (isImageRequest || isTextRequest) && isFirstPartyDomain(event.request.url) && !isCloudflareURL(event.request.url);
if (!shouldOptimize) {
return;
}
event.respondWith(getBrotliStatus()
.then((isBrotliSupported) => {
var headersToSendJS = {};
if (isImageRequest) {
if (event.request.headers.has('Accept')) {
headersToSendJS['Accept'] = event.request.headers.get('Accept');
}
}
if (isTextRequest && typeof isBrotliSupported !== 'undefined' && isBrotliSupported) {
headersToSendJS['Dex-Accept-Encoding'] = 'br';
}
var headersToSend = new Headers(headersToSendJS);
var dexecureURL = changeToDexecureURL(event.request.url);
dexecureURL = decodeURIComponent(dexecureURL);
if (dexecure.debugMode)
console.log('output url is ', dexecureURL);
return new Promise((resolve, reject) => {
setTimeout(reject, dexecure.timeoutDuration);
return fetch(dexecureURL, { mode: 'cors', headers: headersToSend })
.then(response => {
if (response.ok) {
return response;
} else {
if (dexecure.debugMode)
console.log('Responding with original image as optimiser was not reachable ', event.request.url);
throw new Error('Unable to fetch optimised image');
}
})
})
.catch(err => {
if (dexecure.debugMode) {
console.log('Sending original image as an error occured when trying to optimise ', event.request.url);
console.log('The error was ', err);
}
return fetch(event.request);
})
})
);
});
})
}