fixed minor streaming nits

This commit is contained in:
Danny Coates 2018-06-25 10:57:52 -07:00
parent c157e4d31c
commit a4cf46c0eb
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
5 changed files with 36 additions and 65 deletions

View File

@ -128,7 +128,6 @@ async function upload(
streamInfo, streamInfo,
metadata, metadata,
verifierB64, verifierB64,
keychain,
onprogress, onprogress,
canceller canceller
) { ) {
@ -176,14 +175,7 @@ async function upload(
} }
} }
export function uploadWs( export function uploadWs(encrypted, info, metadata, verifierB64, onprogress) {
encrypted,
info,
metadata,
verifierB64,
keychain,
onprogress
) {
const canceller = { cancelled: false }; const canceller = { cancelled: false };
return { return {
@ -195,7 +187,6 @@ export function uploadWs(
info, info,
metadata, metadata,
verifierB64, verifierB64,
keychain,
onprogress, onprogress,
canceller canceller
) )

View File

@ -53,7 +53,7 @@ export default class FileReceiver extends Nanobus {
async streamToArrayBuffer(stream, streamSize) { async streamToArrayBuffer(stream, streamSize) {
const reader = stream.getReader(); const reader = stream.getReader();
const result = new Int8Array(streamSize); const result = new Uint8Array(streamSize);
let offset = 0; let offset = 0;
let state = await reader.read(); let state = await reader.read();

View File

@ -74,7 +74,6 @@ export default class FileSender extends Nanobus {
enc.streamInfo, enc.streamInfo,
metadata, metadata,
authKeyB64, authKeyB64,
this.keychain,
p => { p => {
this.progress = p; this.progress = p;
this.emit('progress'); this.emit('progress');

View File

@ -16,51 +16,46 @@ module.exports = async function(ws, req) {
} }
}); });
let first = true; ws.once('message', async function(message) {
ws.on('message', async function(message) {
try { try {
if (first) { const newId = crypto.randomBytes(5).toString('hex');
const newId = crypto.randomBytes(5).toString('hex'); const owner = crypto.randomBytes(10).toString('hex');
const owner = crypto.randomBytes(10).toString('hex');
const fileInfo = JSON.parse(message); const fileInfo = JSON.parse(message);
const metadata = fileInfo.fileMetadata; const metadata = fileInfo.fileMetadata;
const auth = fileInfo.authorization; 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);
if (!metadata || !auth) {
ws.send( ws.send(
JSON.stringify({ JSON.stringify({
url, error: 400
owner: meta.owner,
id: newId,
authentication: `send-v1 ${meta.nonce}`
}) })
); );
return ws.close();
first = false;
} }
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) { } catch (e) {
log.error('upload', e); log.error('upload', e);
ws.send( ws.send(

View File

@ -17,14 +17,7 @@ describe('API', function() {
const meta = await keychain.encryptMetadata(metadata); const meta = await keychain.encryptMetadata(metadata);
const verifierB64 = await keychain.authKeyB64(); const verifierB64 = await keychain.authKeyB64();
const p = function() {}; const p = function() {};
const up = api.uploadWs( const up = api.uploadWs(enc.stream, enc.streamInfo, meta, verifierB64, p);
enc.stream,
enc.streamInfo,
meta,
verifierB64,
keychain,
p
);
const result = await up.result; const result = await up.result;
assert.ok(result.url); assert.ok(result.url);
@ -38,14 +31,7 @@ describe('API', function() {
const meta = await keychain.encryptMetadata(metadata); const meta = await keychain.encryptMetadata(metadata);
const verifierB64 = await keychain.authKeyB64(); const verifierB64 = await keychain.authKeyB64();
const p = function() {}; const p = function() {};
const up = api.uploadWs( const up = api.uploadWs(enc.stream, enc.streamInfo, meta, verifierB64, p);
enc.stream,
enc.streamInfo,
meta,
verifierB64,
keychain,
p
);
up.cancel(); up.cancel();
try { try {
await up.result; await up.result;