send/app/serviceWorker.js

42 lines
926 B
JavaScript
Raw Normal View History

2018-07-05 19:40:49 +00:00
import Keychain from './keychain';
2018-07-06 22:49:50 +00:00
self.addEventListener('install', event => {
2018-07-05 19:40:49 +00:00
self.skipWaiting();
});
async function decryptStream(request) {
2018-07-06 22:49:50 +00:00
const response = await fetch(request.url, {
method: 'GET',
headers: { Authorization: self.auth }
});
2018-07-05 19:40:49 +00:00
if (response.status !== 200) {
2018-07-06 22:49:50 +00:00
return response;
2018-07-05 19:40:49 +00:00
}
2018-07-06 22:49:50 +00:00
const body = response.body; //stream
const decrypted = self.keychain.decryptStream(body);
const headers = {
headers: {
'Content-Disposition': 'attachment; filename=' + self.filename
}
};
2018-07-05 19:40:49 +00:00
2018-07-06 22:49:50 +00:00
const newRes = new Response(decrypted, headers);
return newRes;
2018-07-05 19:40:49 +00:00
}
2018-07-06 22:49:50 +00:00
self.onfetch = event => {
2018-07-05 19:40:49 +00:00
const req = event.request.clone();
if (req.url.includes('/api/download')) {
event.respondWith(decryptStream(req));
}
};
2018-07-06 22:49:50 +00:00
self.onmessage = event => {
2018-07-05 19:40:49 +00:00
self.keychain = new Keychain(event.data.key, event.data.nonce);
2018-07-06 22:49:50 +00:00
self.filename = event.data.filename;
self.auth = event.data.auth;
};