download cancel and progress

This commit is contained in:
Emily 2018-07-09 17:00:19 -07:00
parent 921df9e1aa
commit 6db3009e5f
5 changed files with 21 additions and 28 deletions

View File

@ -77,7 +77,7 @@ export default class FileReceiver extends Nanobus {
} else { } else {
resolve(event.data); resolve(event.data);
} }
} };
navigator.serviceWorker.controller.postMessage(msg, [channel.port2]); navigator.serviceWorker.controller.postMessage(msg, [channel.port2]);
}); });
} }
@ -93,7 +93,7 @@ export default class FileReceiver extends Nanobus {
this.sendMessageToSw('cancel'); this.sendMessageToSw('cancel');
//throw new Error(0); //throw new Error(0);
} }
} };
try { try {
this.state = 'downloading'; this.state = 'downloading';
@ -107,7 +107,7 @@ export default class FileReceiver extends Nanobus {
}; };
await this.sendMessageToSw(info); await this.sendMessageToSw(info);
console.log("SENDING REQUEST FROM PAGE ONCE") console.log('SENDING REQUEST FROM PAGE ONCE');
if (!noSave) { if (!noSave) {
const downloadUrl = `${location.protocol}//${ const downloadUrl = `${location.protocol}//${
@ -119,12 +119,10 @@ export default class FileReceiver extends Nanobus {
a.click(); a.click();
URL.revokeObjectURL(downloadUrl); URL.revokeObjectURL(downloadUrl);
/*
const auth = await this.sendMessageToSw('authHeader'); const auth = await this.sendMessageToSw('authHeader');
if (auth) { if (auth) {
this.keychain.nonce = parseNonce(auth); this.keychain.nonce = parseNonce(auth);
} }
*/
let prog = 0; let prog = 0;
while (prog < this.fileInfo.size) { while (prog < this.fileInfo.size) {
@ -137,7 +135,6 @@ export default class FileReceiver extends Nanobus {
this.downloadRequest = null; this.downloadRequest = null;
this.msg = 'downloadFinish'; this.msg = 'downloadFinish';
this.state = 'complete'; this.state = 'complete';
} catch (e) { } catch (e) {
this.downloadRequest = null; this.downloadRequest = null;
if (e === 'cancelled') { if (e === 'cancelled') {

View File

@ -6,8 +6,8 @@ self.addEventListener('install', event => {
async function decryptStream(request) { async function decryptStream(request) {
self.controller = new AbortController(); self.controller = new AbortController();
console.log("SW INTERCEPTED DOWNLOAD") //console.log('SW INTERCEPTED DOWNLOAD');
console.log(request)
const response = await fetch(request.url, { const response = await fetch(request.url, {
method: 'GET', method: 'GET',
headers: { Authorization: self.auth }, headers: { Authorization: self.auth },
@ -15,13 +15,11 @@ async function decryptStream(request) {
}); });
if (response.status !== 200) { if (response.status !== 200) {
console.log(response.status);
return response; return response;
} }
self.authHeader = response.headers.get('WWW-Authenticate'); self.authHeader = response.headers.get('WWW-Authenticate');
const body = response.body; //stream const body = response.body; //stream
const progStream = new TransformStream({ const progStream = new TransformStream({
@ -52,30 +50,25 @@ self.onfetch = event => {
self.onmessage = event => { self.onmessage = event => {
if (event.data.key) { if (event.data.key) {
if (!self.keychain) {
self.keychain = new Keychain(event.data.key, event.data.nonce); self.keychain = new Keychain(event.data.key, event.data.nonce);
}
self.filename = event.data.filename; self.filename = event.data.filename;
self.auth = event.data.auth; self.auth = event.data.auth;
self.progress = 0; self.progress = 0;
self.cancelled = false; self.cancelled = false;
event.ports[0].postMessage("file info received"); event.ports[0].postMessage('file info received');
} else if (event.data === 'progress') {
} else if (event.data === "progress") {
if (self.cancelled) { if (self.cancelled) {
event.ports[0].postMessage({error: "cancelled"}); event.ports[0].postMessage({ error: 'cancelled' });
} else { } else {
event.ports[0].postMessage(self.progress); event.ports[0].postMessage(self.progress);
} }
} else if (event.data === 'authHeader') {
} else if (event.data === "authHeader") {
event.ports[0].postMessage(self.authHeader); event.ports[0].postMessage(self.authHeader);
} else if (event.data === 'cancel') {
} else if (event.data === "cancel") {
self.cancelled = true; self.cancelled = true;
if (self.controller) { if (self.controller) {
self.controller.abort(); self.controller.abort();
} }
event.ports[0].postMessage("download cancelled"); event.ports[0].postMessage('download cancelled');
} }
}; };

View File

@ -8,7 +8,6 @@ module.exports = function(state, emit) {
function download(event) { function download(event) {
event.preventDefault(); event.preventDefault();
console.log("DOWNLOAD FIRE")
emit('download', state.fileInfo); emit('download', state.fileInfo);
} }
}; };

View File

@ -12,7 +12,7 @@
"prepush": "npm test", "prepush": "npm test",
"check": "nsp check", "check": "nsp check",
"clean": "rimraf dist", "clean": "rimraf dist",
"build": "npm run clean && webpack -p && webpack --config webpackSw.config.js -p", "build": "npm run clean && webpack && webpack --config webpackSw.config.js",
"lint": "npm-run-all lint:*", "lint": "npm-run-all lint:*",
"lint:css": "stylelint app/*.css app/**/*.css", "lint:css": "stylelint app/*.css app/**/*.css",
"lint:js": "eslint .", "lint:js": "eslint .",

View File

@ -1,10 +1,12 @@
const path = require('path'); const path = require('path');
/*
const regularJSOptions = { const regularJSOptions = {
babelrc: false, babelrc: false,
presets: [['env'], 'stage-2'], presets: [['env'], 'stage-2'],
plugins: ['transform-runtime'] plugins: ['transform-runtime']
}; };
*/
const entry = { const entry = {
serviceWorker: ['./app/serviceWorker.js'] serviceWorker: ['./app/serviceWorker.js']
@ -18,6 +20,7 @@ module.exports = {
publicPath: '/' publicPath: '/'
}, },
/*
module: { module: {
rules: [ rules: [
{ {
@ -27,4 +30,5 @@ module.exports = {
} }
] ]
} }
*/
}; };