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

@ -68,17 +68,17 @@ export default class FileReceiver extends Nanobus {
}
sendMessageToSw(msg) {
return new Promise( (resolve, reject) => {
return new Promise((resolve, reject) => {
const channel = new MessageChannel();
channel.port1.onmessage = function(event) {
if(event.data.error !== undefined) {
if (event.data.error !== undefined) {
reject(event.data.error);
} else {
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');
//throw new Error(0);
}
}
};
try {
this.state = 'downloading';
@ -107,7 +107,7 @@ export default class FileReceiver extends Nanobus {
};
await this.sendMessageToSw(info);
console.log("SENDING REQUEST FROM PAGE ONCE")
console.log('SENDING REQUEST FROM PAGE ONCE');
if (!noSave) {
const downloadUrl = `${location.protocol}//${
@ -119,12 +119,10 @@ export default class FileReceiver extends Nanobus {
a.click();
URL.revokeObjectURL(downloadUrl);
/*
const auth = await this.sendMessageToSw('authHeader');
if (auth) {
this.keychain.nonce = parseNonce(auth);
}
*/
let prog = 0;
while (prog < this.fileInfo.size) {
@ -137,7 +135,6 @@ export default class FileReceiver extends Nanobus {
this.downloadRequest = null;
this.msg = 'downloadFinish';
this.state = 'complete';
} catch (e) {
this.downloadRequest = null;
if (e === 'cancelled') {

View File

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

View File

@ -12,7 +12,7 @@
"prepush": "npm test",
"check": "nsp check",
"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:css": "stylelint app/*.css app/**/*.css",
"lint:js": "eslint .",

View File

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