diff --git a/app/api.js b/app/api.js index d8cd9b9b..4101f9a8 100644 --- a/app/api.js +++ b/app/api.js @@ -128,7 +128,6 @@ async function upload( streamInfo, metadata, verifierB64, - keychain, onprogress, canceller ) { @@ -176,14 +175,7 @@ async function upload( } } -export function uploadWs( - encrypted, - info, - metadata, - verifierB64, - keychain, - onprogress -) { +export function uploadWs(encrypted, info, metadata, verifierB64, onprogress) { const canceller = { cancelled: false }; return { @@ -195,7 +187,6 @@ export function uploadWs( info, metadata, verifierB64, - keychain, onprogress, canceller ) diff --git a/app/fileReceiver.js b/app/fileReceiver.js index 385b749b..2a22fbca 100644 --- a/app/fileReceiver.js +++ b/app/fileReceiver.js @@ -53,7 +53,7 @@ export default class FileReceiver extends Nanobus { async streamToArrayBuffer(stream, streamSize) { const reader = stream.getReader(); - const result = new Int8Array(streamSize); + const result = new Uint8Array(streamSize); let offset = 0; let state = await reader.read(); diff --git a/app/fileSender.js b/app/fileSender.js index 4f0a64a4..7c3c4de1 100644 --- a/app/fileSender.js +++ b/app/fileSender.js @@ -74,7 +74,6 @@ export default class FileSender extends Nanobus { enc.streamInfo, metadata, authKeyB64, - this.keychain, p => { this.progress = p; this.emit('progress'); diff --git a/server/routes/ws.js b/server/routes/ws.js index 50ef9055..ea81573c 100644 --- a/server/routes/ws.js +++ b/server/routes/ws.js @@ -16,51 +16,46 @@ module.exports = async function(ws, req) { } }); - let first = true; - ws.on('message', async function(message) { + ws.once('message', async function(message) { try { - if (first) { - const newId = crypto.randomBytes(5).toString('hex'); - const owner = crypto.randomBytes(10).toString('hex'); + const newId = crypto.randomBytes(5).toString('hex'); + const owner = crypto.randomBytes(10).toString('hex'); - const fileInfo = JSON.parse(message); - const metadata = fileInfo.fileMetadata; - const auth = fileInfo.authorization; - - if (!metadata || !auth) { - ws.send( - JSON.stringify({ - error: 400 - }) - ); - ws.close(); - } - - const meta = { - owner, - metadata, - auth: auth.split(' ')[1], - nonce: crypto.randomBytes(16).toString('base64') - }; - - const protocol = config.env === 'production' ? 'https' : req.protocol; - const url = `${protocol}://${req.get('host')}/download/${newId}/`; - - const limiter = new Limiter(config.max_file_size); - fileStream = wsStream(ws, { binary: true }).pipe(limiter); - storage.set(newId, fileStream, meta); + const fileInfo = JSON.parse(message); + const metadata = fileInfo.fileMetadata; + const auth = fileInfo.authorization; + if (!metadata || !auth) { ws.send( JSON.stringify({ - url, - owner: meta.owner, - id: newId, - authentication: `send-v1 ${meta.nonce}` + error: 400 }) ); - - first = false; + return ws.close(); } + + const meta = { + owner, + metadata, + auth: auth.split(' ')[1], + nonce: crypto.randomBytes(16).toString('base64') + }; + + const protocol = config.env === 'production' ? 'https' : req.protocol; + const url = `${protocol}://${req.get('host')}/download/${newId}/`; + + const limiter = new Limiter(config.max_file_size); + fileStream = wsStream(ws, { binary: true }).pipe(limiter); + storage.set(newId, fileStream, meta); + + ws.send( + JSON.stringify({ + url, + owner: meta.owner, + id: newId, + authentication: `send-v1 ${meta.nonce}` + }) + ); } catch (e) { log.error('upload', e); ws.send( diff --git a/test/frontend/tests/api-tests.js b/test/frontend/tests/api-tests.js index 1aa5a19f..22e221e4 100644 --- a/test/frontend/tests/api-tests.js +++ b/test/frontend/tests/api-tests.js @@ -17,14 +17,7 @@ describe('API', function() { const meta = await keychain.encryptMetadata(metadata); const verifierB64 = await keychain.authKeyB64(); const p = function() {}; - const up = api.uploadWs( - enc.stream, - enc.streamInfo, - meta, - verifierB64, - keychain, - p - ); + const up = api.uploadWs(enc.stream, enc.streamInfo, meta, verifierB64, p); const result = await up.result; assert.ok(result.url); @@ -38,14 +31,7 @@ describe('API', function() { const meta = await keychain.encryptMetadata(metadata); const verifierB64 = await keychain.authKeyB64(); const p = function() {}; - const up = api.uploadWs( - enc.stream, - enc.streamInfo, - meta, - verifierB64, - keychain, - p - ); + const up = api.uploadWs(enc.stream, enc.streamInfo, meta, verifierB64, p); up.cancel(); try { await up.result;