add streaming
This commit is contained in:
parent
34cb970f11
commit
1bd7e4d486
16 changed files with 438 additions and 187 deletions
|
@ -3,21 +3,29 @@ import * as api from '../../../app/api';
|
|||
import Keychain from '../../../app/keychain';
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
const plaintext = encoder.encode('hello world!');
|
||||
const plaintext = new Blob([encoder.encode('hello world!')]);
|
||||
const metadata = {
|
||||
name: 'test.txt',
|
||||
type: 'text/plain'
|
||||
};
|
||||
|
||||
describe('API', function() {
|
||||
describe('uploadFile', function() {
|
||||
describe('websocket upload', function() {
|
||||
it('returns file info on success', async function() {
|
||||
const keychain = new Keychain();
|
||||
const encrypted = await keychain.encryptFile(plaintext);
|
||||
const enc = await keychain.encryptStream(plaintext);
|
||||
const meta = await keychain.encryptMetadata(metadata);
|
||||
const verifierB64 = await keychain.authKeyB64();
|
||||
const p = function() {};
|
||||
const up = api.uploadFile(encrypted, meta, verifierB64, keychain, p);
|
||||
const up = await api.uploadWs(
|
||||
enc.stream,
|
||||
enc.streamInfo,
|
||||
meta,
|
||||
verifierB64,
|
||||
keychain,
|
||||
p
|
||||
);
|
||||
|
||||
const result = await up.result;
|
||||
assert.ok(result.url);
|
||||
assert.ok(result.id);
|
||||
|
@ -26,11 +34,18 @@ describe('API', function() {
|
|||
|
||||
it('can be cancelled', async function() {
|
||||
const keychain = new Keychain();
|
||||
const encrypted = await keychain.encryptFile(plaintext);
|
||||
const enc = await keychain.encryptStream(plaintext);
|
||||
const meta = await keychain.encryptMetadata(metadata);
|
||||
const verifierB64 = await keychain.authKeyB64();
|
||||
const p = function() {};
|
||||
const up = api.uploadFile(encrypted, meta, verifierB64, keychain, p);
|
||||
const up = await api.uploadWs(
|
||||
enc.stream,
|
||||
enc.streamInfo,
|
||||
meta,
|
||||
verifierB64,
|
||||
keychain,
|
||||
p
|
||||
);
|
||||
up.cancel();
|
||||
try {
|
||||
await up.result;
|
||||
|
|
|
@ -1,13 +1,10 @@
|
|||
const streams = require('web-streams-polyfill');
|
||||
const ece = require('http_ece');
|
||||
require('buffer');
|
||||
|
||||
import assert from 'assert';
|
||||
import { b64ToArray } from '../../../app/utils';
|
||||
import ECETransformer from '../../../app/ece.js';
|
||||
import BlobSliceStream from '../../../app/blobslicer.js';
|
||||
import ECE from '../../../app/ece.js';
|
||||
|
||||
const decoder = new TextDecoder('utf-8');
|
||||
const rs = 36;
|
||||
|
||||
const str = 'You are the dancing queen, young and sweet, only seventeen.';
|
||||
|
@ -33,28 +30,10 @@ describe('Streaming', function() {
|
|||
const salt = b64ToArray(testSalt).buffer;
|
||||
const blob = new Blob([str], { type: 'text/plain' });
|
||||
|
||||
it('blob slice stream works', async function() {
|
||||
const rs = await new BlobSliceStream(blob, 100);
|
||||
const reader = rs.getReader();
|
||||
|
||||
let result = '';
|
||||
let state = await reader.read();
|
||||
while (!state.done) {
|
||||
result = decoder.decode(state.value);
|
||||
state = await reader.read();
|
||||
}
|
||||
|
||||
assert.equal(result, str);
|
||||
});
|
||||
|
||||
it('can encrypt', async function() {
|
||||
const enc = new streams.TransformStream(
|
||||
new ECETransformer('encrypt', key, rs, salt)
|
||||
);
|
||||
const encStream = new ECE(blob, key, 'encrypt', rs, salt).stream;
|
||||
const reader = encStream.getReader();
|
||||
|
||||
const rstream = await new BlobSliceStream(blob, rs - 17);
|
||||
|
||||
const reader = rstream.pipeThrough(enc).getReader();
|
||||
let result = Buffer.from([]);
|
||||
|
||||
let state = await reader.read();
|
||||
|
@ -68,12 +47,9 @@ describe('Streaming', function() {
|
|||
|
||||
it('can decrypt', async function() {
|
||||
const encBlob = new Blob([encrypted]);
|
||||
const dec = new streams.TransformStream(
|
||||
new ECETransformer('decrypt', key, rs)
|
||||
);
|
||||
const decStream = await new ECE(encBlob, key, 'decrypt', rs).stream;
|
||||
|
||||
const rstream = await new BlobSliceStream(encBlob, rs, true);
|
||||
const reader = rstream.pipeThrough(dec).getReader();
|
||||
const reader = decStream.getReader();
|
||||
let result = Buffer.from([]);
|
||||
|
||||
let state = await reader.read();
|
||||
|
|
|
@ -93,7 +93,7 @@ describe('Upload / Download flow', function() {
|
|||
fs.cancel(); // before encrypting
|
||||
try {
|
||||
await up;
|
||||
assert.fail('not cancelled');
|
||||
assert.fail('not cancelled 1');
|
||||
} catch (e) {
|
||||
assert.equal(e.message, '0');
|
||||
}
|
||||
|
@ -101,7 +101,7 @@ describe('Upload / Download flow', function() {
|
|||
fs.once('encrypting', () => fs.cancel());
|
||||
try {
|
||||
await fs.upload();
|
||||
assert.fail('not cancelled');
|
||||
assert.fail('not cancelled 2');
|
||||
} catch (e) {
|
||||
assert.equal(e.message, '0');
|
||||
}
|
||||
|
@ -109,7 +109,7 @@ describe('Upload / Download flow', function() {
|
|||
fs.once('progress', () => fs.cancel());
|
||||
try {
|
||||
await fs.upload();
|
||||
assert.fail('not cancelled');
|
||||
assert.fail('not cancelled 3');
|
||||
} catch (e) {
|
||||
assert.equal(e.message, '0');
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue