SW bug fixes.

This commit is contained in:
Bowen Ding
2020-06-30 23:54:06 +08:00
parent 0e49dae029
commit 233a6012a9

View File

@@ -36,13 +36,15 @@ class Decryptor {
}
this._role = "main";
this._config = config;
const local_config = this._mGetLocalConfig();
if (local_config) {
this._config = local_config;
}
window.addEventListener(
"load",
(e) => this._mSetupServiceWorker(),
document.addEventListener(
"DOMContentLoaded",
(e) => {
const local_config = this._mGetLocalConfig();
if (local_config) {
this._config = local_config;
}
this._mSetupServiceWorker();
},
{ once: true, passive: true }
);
}
@@ -142,15 +144,11 @@ class Decryptor {
set workerReady(val) {
this._workerReady = (val ? true : false);
if (this._workerReady) {
const eventTarget = (Decryptor.isWorker() ? self : document);
Decryptor._sendEvent(eventTarget, "DecryptWorkerReady");
}
}
_mSetWorkerReady() {
this.workerReady = true;
const had_been_ready_before = localStorage.getItem(this._config.galleryId) !== null;
const had_been_ready_before = this._mGetLocalConfig() !== null;
localStorage.setItem(this._config.galleryId, JSON.stringify(this._config));
if (!had_been_ready_before) {
window.location.reload();
@@ -160,21 +158,25 @@ class Decryptor {
_mUnsetWorkerReady() {
this.workerReady = false;
localStorage.removeItem(this._config.galleryId);
alert("Incorrect password!");
}
_mGetLocalConfig() {
const local_config = JSON.parse(localStorage.getItem(this._config.galleryId));
if (local_config
&& local_config.galleryId
&& local_config.sw_script
&& local_config.password
&& local_config.gcm_tag
&& local_config.kdf_salt
&& local_config.kdf_iters) {
return local_config;
} else {
return null;
}
try {
const local_config = JSON.parse(localStorage.getItem(this._config.galleryId));
if (local_config
&& local_config.galleryId
&& local_config.sw_script
&& local_config.password
&& local_config.gcm_tag
&& local_config.kdf_salt
&& local_config.kdf_iters) {
return local_config;
}
} catch (e) {
console.error("Error retrieving config from local storage: " + e);
}
return null;
}
async _mSetupServiceWorker() {
@@ -221,7 +223,7 @@ class Decryptor {
/* main thread only */
async _mAskPassword() {
const config = JSON.parse(localStorage.getItem(this._config.galleryId));
const config = this._mGetLocalConfig();
if (config && config.password) {
return config.password;
}
@@ -452,6 +454,11 @@ class Decryptor {
return Decryptor.onMessage(e.source, e);
}
static async _addToCache(request, response) {
const cache = await caches.open("v1");
await cache.put(request, response);
}
static async _swHandleFetch(e) {
const request = e.request;
try {
@@ -470,7 +477,7 @@ class Decryptor {
response = await fetch(request);
} catch (error) {
console.debug(`Fetch failed when trying for ${request.url}: ${error}`);
throw error;
return Decryptor.generalErrorResponse.clone();
}
if (!response.ok) {
@@ -503,21 +510,16 @@ class Decryptor {
if (!Decryptor.isInitialized()) {
if ('decryptor' in self) {
try{
const clients = await self.clients.matchAll({type: "window"});
const races = Promise.race(
clients.map((client) => {
return self.decryptor._proxyWrap(client)._mGetLocalConfig();
})
);
const config = await Promise.timeout(races, 100);
const client = await self.clients.get(e.clientId);
const config = await self.decryptor._proxyWrap(client)._mGetLocalConfig();
await self.decryptor._swInitServiceWorker(config);
} catch (error) {
// do nothing
}
}
if (!Decryptor.isInitialized()) {
console.debug(`Service worker not initialized on fetch event`);
return Decryptor.errorResponse.clone();
console.debug(`Decryptor not initialized on fetch event`);
return Decryptor.imageErrorResponse.clone();
}
}
@@ -530,7 +532,7 @@ class Decryptor {
} catch (error) {
console.debug(`Decryption failed for ${request.url}: ${error.message}`);
console.error("Corrupted data??? This shouldn't occur.");
return Decryptor.errorResponse.clone();
return Decryptor.imageErrorResponse.clone();
}
const decrypted_response = new Response(
@@ -543,9 +545,7 @@ class Decryptor {
);
decrypted_response.headers.set("content-length", decrypted_blob.size);
const decrypted_response_clone = decrypted_response.clone();
const cache = await caches.open("v1");
cache.put(request, decrypted_response_clone);
Decryptor._addToCache(request, decrypted_response.clone());
console.debug(`Responding with decrypted response ${request.url}`);
return decrypted_response;
@@ -578,7 +578,7 @@ Decryptor.imagePlaceholderBlob = new Blob([
</g>
</svg>`], {type: "image/svg+xml"});
Decryptor.errorResponse = new Response(
Decryptor.imageErrorResponse = new Response(
Decryptor.imagePlaceholderBlob,
{
status: 200,
@@ -589,9 +589,17 @@ Decryptor.errorResponse = new Response(
}
);
Decryptor.generalErrorResponse = new Response(
null,
{
status: 500,
statusText: "Server Error"
}
);
Promise.timeout = function(cb_or_pm, timeout) {
return Promise.race([
cb_or_pm instanceof Function ? new Promise(cb) : cb_or_pm,
cb_or_pm instanceof Function ? new Promise(cb_or_pm) : cb_or_pm,
new Promise((resolve, reject) => {
setTimeout(() => {
reject('Timed out');