revisions

This commit is contained in:
Emily Hou 2018-06-22 13:17:23 -07:00
parent 12ccce3016
commit dafe4884fc
9 changed files with 86 additions and 89 deletions

View file

@ -100,8 +100,30 @@ function asyncInitWebSocket(server) {
});
}
function listenForResponse(ws, canceller) {
return new Promise((resolve, reject) => {
ws.addEventListener('message', function(msg) {
try {
const response = JSON.parse(msg.data);
if (response.error) {
throw new Error(response.error);
} else {
resolve({
url: response.url,
id: response.id,
ownerToken: response.owner
});
}
} catch (e) {
canceller.cancelled = true;
canceller.error = e;
reject(e);
}
});
});
}
async function upload(
ws,
stream,
streamInfo,
metadata,
@ -110,55 +132,51 @@ async function upload(
onprogress,
canceller
) {
const metadataHeader = arrayToB64(new Uint8Array(metadata));
const fileMeta = {
fileMetadata: metadataHeader,
authorization: `send-v1 ${verifierB64}`
};
const host = window.location.hostname;
const port = window.location.port;
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const error = { cancelled: false };
const ws = await asyncInitWebSocket(`${protocol}//${host}:${port}/api/ws`);
function listenForResponse() {
return new Promise((resolve, reject) => {
ws.addEventListener('message', function(msg) {
const response = JSON.parse(msg.data);
if (response.error) {
reject(response.error);
} else {
resolve({
url: response.url,
id: response.id,
ownerToken: response.owner
});
}
});
});
}
try {
const metadataHeader = arrayToB64(new Uint8Array(metadata));
const fileMeta = {
fileMetadata: metadataHeader,
authorization: `send-v1 ${verifierB64}`
};
const resPromise = listenForResponse();
ws.send(JSON.stringify(fileMeta));
const responsePromise = listenForResponse(ws, error);
const reader = stream.getReader();
let state = await reader.read();
let size = 0;
while (!state.done) {
const buf = state.value;
if (canceller.cancelled) {
ws.close(4000, 'upload cancelled');
throw new Error(0);
ws.send(JSON.stringify(fileMeta));
const reader = stream.getReader();
let state = await reader.read();
let size = 0;
while (!state.done) {
const buf = state.value;
if (canceller.cancelled) {
throw new Error(0);
}
if (error.cancelled) {
throw new Error(error.error);
}
ws.send(buf);
onprogress([Math.min(streamInfo.fileSize, size), streamInfo.fileSize]);
size += streamInfo.recordSize;
state = await reader.read();
}
ws.send(buf);
onprogress([Math.min(streamInfo.fileSize, size), streamInfo.fileSize]);
size += streamInfo.recordSize;
state = await reader.read();
const response = await responsePromise; //promise only fufills if response is good
ws.close();
return response;
} catch (e) {
ws.close(4000);
throw e;
}
const response = await resPromise;
ws.close();
return response;
}
export async function uploadWs(
export function uploadWs(
encrypted,
info,
metadata,
@ -166,10 +184,6 @@ export async function uploadWs(
keychain,
onprogress
) {
const host = window.location.hostname;
const port = window.location.port;
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
const ws = await asyncInitWebSocket(`${protocol}//${host}:${port}/api/ws`);
const canceller = { cancelled: false };
return {
@ -177,7 +191,6 @@ export async function uploadWs(
canceller.cancelled = true;
},
result: upload(
ws,
encrypted,
info,
metadata,

View file

@ -282,11 +282,11 @@ export default class ECE {
this.streamInfo = {
recordSize: rs,
fileSize: input.size + 16 * Math.floor(input.size / (rs - 17))
fileSize: 21 + input.size + 16 * Math.floor(input.size / (rs - 17))
};
input = new BlobSliceStream(input, rs, mode);
const inputStream = new BlobSliceStream(input, rs, mode);
const ts = new TransformStream(new ECETransformer(mode, key, rs, salt));
this.stream = input.pipeThrough(ts);
this.stream = inputStream.pipeThrough(ts);
}
}

View file

@ -51,25 +51,18 @@ export default class FileReceiver extends Nanobus {
this.state = 'ready';
}
async streamToArrayBuffer(stream) {
async streamToArrayBuffer(stream, streamSize) {
const reader = stream.getReader();
const chunks = [];
let length = 0;
const result = new Int8Array(streamSize);
let offset = 0;
let state = await reader.read();
while (!state.done) {
chunks.push(state.value);
length += state.value.length;
result.set(state.value, offset);
offset += state.value.length;
state = await reader.read();
}
const result = new Int8Array(length);
let offset = 0;
for (let i = 0; i < chunks.length; i++) {
result.set(chunks[i], offset);
offset += chunks[i].length;
}
return result.buffer;
}
@ -93,8 +86,10 @@ export default class FileReceiver extends Nanobus {
const dec = await this.keychain.decryptStream(ciphertext);
const plainstream = dec.stream;
const plaintext = await this.streamToArrayBuffer(plainstream);
const plaintext = await this.streamToArrayBuffer(
plainstream,
dec.streamInfo.fileSize
);
if (!noSave) {
await saveFile({

View file

@ -65,11 +65,11 @@ export default class FileSender extends Nanobus {
this.msg = 'encryptingFile';
this.emit('encrypting');
const enc = await this.keychain.encryptStream(this.file);
const enc = this.keychain.encryptStream(this.file);
const metadata = await this.keychain.encryptMetadata(this.file);
const authKeyB64 = await this.keychain.authKeyB64();
this.uploadRequest = await uploadWs(
this.uploadRequest = uploadWs(
enc.stream,
enc.streamInfo,
metadata,

View file

@ -179,12 +179,12 @@ export default class Keychain {
return ciphertext;
}
async encryptStream(plaintext) {
encryptStream(plaintext) {
const enc = new ECE(plaintext, this.rawSecret, 'encrypt');
return enc;
}
async decryptStream(encstream) {
decryptStream(encstream) {
const dec = new ECE(encstream, this.rawSecret, 'decrypt');
return dec;
}