Skip to content

Commit 3388ab4

Browse files
Raphaël Drozdrzraf
Raphaël Droz
authored andcommitted
- Remove the distinction between sync/async readFileFn in order to use a
unified codepath. - Remove the need for calling the preprocessFinished() function after defining a (now async) preprocess() callback.
1 parent b0dffad commit 3388ab4

File tree

4 files changed

+23
-65
lines changed

4 files changed

+23
-65
lines changed

samples/Encryption.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const flow = new Flow({
3030
uploadMethod: 'POST',
3131
fileParameterName: 'file',
3232
// Asynchronous function called before each chunk upload request
33-
asyncReadFileFn: async function(flowObj, startByte, endByte, fileType, chunk) {
33+
readFileFn: async function(flowObj, startByte, endByte, fileType, chunk) {
3434
// Load file chunk in memory
3535
const plaintextbytes = await readFileChunk(flowObj.file, startByte, endByte);
3636
// Encrypt chunk
@@ -109,7 +109,7 @@ class StreamEncryptor {
109109
var encryptor = new StreamEncryptor(gpgKeys);
110110
new Flow({
111111
// ...
112-
asyncReadFileFn: encryptor.read.bind(encryptor),
112+
readFileFn: encryptor.read.bind(encryptor),
113113
initFileFn: encryptor.init.bind(encryptor),
114114
forceChunkSize: true,
115115
});

src/FlowChunk.js

+15-55
Original file line numberDiff line numberDiff line change
@@ -260,30 +260,7 @@ export default class FlowChunk {
260260
}
261261

262262
/**
263-
* Finish preprocess state
264-
* @function
265-
*/
266-
async preprocessFinished() {
267-
// Re-compute the endByte after the preprocess function to allow an
268-
// implementer of preprocess to set the fileObj size
269-
this.endByte = this.computeEndByte();
270-
271-
this.preprocessState = 2;
272-
await this.send();
273-
}
274-
275-
/**
276-
* Finish read state
277-
* @function
278-
*/
279-
readFinished(payload) {
280-
this.readState = 2;
281-
this.payload = payload;
282-
this.send();
283-
}
284-
285-
/**
286-
* asyncReadFileFn() helper provides the ability of asynchronous read()
263+
* readFileFn() helper provides the ability of asynchronous read()
287264
* Eg: When reading from a ReadableStream.getReader().
288265
*
289266
* But:
@@ -322,15 +299,17 @@ export default class FlowChunk {
322299

323300
this.readState = 1;
324301
await this.readStreamGuard();
325-
var data, asyncRead = this.flowObj.opts.asyncReadFileFn;
326-
327-
data = await asyncRead(this.fileObj, this.startByte, this.endByte, this.fileObj.file.type, this);
302+
let data = await this.flowObj.opts.readFileFn(this.fileObj, this.startByte, this.endByte, this.fileObj.file.type, this);
328303
this.readStreamState.resolve();
329304

330-
// Equivalent to readFinished()
305+
// Equivalent to former readFinished()
331306
this.readState = 2;
332307

333308
if (data) {
309+
if (typeof data === 'string') { // In case a regular string is returned
310+
data = new Blob([data], {type: 'application/octet-stream'});
311+
}
312+
334313
this.readBytes = data.size || data.size === 0 ? data.size : -1;
335314
}
336315

@@ -374,36 +353,17 @@ export default class FlowChunk {
374353
*/
375354
async send() {
376355
var preprocess = this.flowObj.opts.preprocess;
377-
var read = this.flowObj.opts.readFileFn;
378-
var asyncRead = this.flowObj.opts.asyncReadFileFn;
379-
380356
if (typeof preprocess === 'function') {
381-
switch (this.preprocessState) {
382-
case 0:
383-
this.preprocessState = 1;
384-
preprocess(this);
385-
return;
386-
case 1:
387-
return;
388-
}
389-
}
390-
391-
if (asyncRead) {
392-
await this.readStreamChunk();
393-
return;
394-
}
395-
396-
switch (this.readState) {
397-
case 0:
398-
this.readState = 1;
399-
const data = read(this.fileObj, this.startByte, this.endByte, this.fileObj.file.type, this);
400-
this.readFinished(data);
401-
return;
402-
case 1:
403-
return;
357+
this.preprocessState = 1;
358+
await preprocess(this);
359+
// Finish preprocess state
360+
// Re-compute the endByte after the preprocess function to allow an
361+
// implementer of preprocess to set the fileObj size
362+
this.endByte = this.computeEndByte();
363+
this.preprocessState = 2;
404364
}
405365

406-
this.xhrSend(this.payload);
366+
await this.readStreamChunk();
407367
}
408368

409369
/**

test/asyncSpec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ describe('upload stream', function() {
104104
xhr_server.restore();
105105
});
106106

107-
it('synchronous initFileFn and asyncReadFileFn', function (done) {
107+
it('synchronous initFileFn and async readFileFn', function (done) {
108108
// No File.stream() support : No test
109109
// No support for skipping() test from Jasmine (https://github.com/jasmine/jasmine/issues/1709)
110110
if (typeof Blob === 'undefined' || Blob.prototype.stream !== 'function') {
@@ -145,7 +145,7 @@ describe('upload stream', function() {
145145
flow.opts.simultaneousUploads = simultaneousUploads;
146146
flow.opts.initFileFn = streamer.init.bind(streamer);
147147
flow.opts.readFileFn = streamer.read.bind(streamer);
148-
flow.opts.asyncReadFileFn = streamer.read.bind(streamer);
148+
149149
(async function() {
150150
await flow.addFile(sample_file);
151151
await flow.upload();
@@ -208,7 +208,7 @@ describe('upload stream', function() {
208208

209209
var streamer = new Streamer(1);
210210
flow.opts.initFileFn = streamer.init.bind(streamer);
211-
flow.opts.asyncReadFileFn = streamer.read.bind(streamer);
211+
flow.opts.readFileFn = streamer.read.bind(streamer);
212212

213213
flow.opts.chunkSize = 1;
214214
flow.opts.maxChunkRetries = 3;
@@ -242,7 +242,7 @@ describe('upload stream', function() {
242242
xhr_server.respondWith([200, { "Content-Type": "text/plain" }, 'ok']);
243243
var streamer = new Streamer(1);
244244
flow.opts.initFileFn = streamer.init.bind(streamer);
245-
flow.opts.asyncReadFileFn = streamer.read.bind(streamer);
245+
flow.opts.readFileFn = streamer.read.bind(streamer);
246246

247247
flow.opts.chunkSize = 1;
248248
flow.opts.maxChunkRetries = 3;
@@ -283,7 +283,7 @@ describe('upload stream', function() {
283283
xhr_server.configure({autoRespond: false, respondImmediately: false});
284284
var streamer = new Streamer(1);
285285
flow.opts.initFileFn = streamer.init.bind(streamer);
286-
flow.opts.asyncReadFileFn = streamer.read.bind(streamer);
286+
flow.opts.readFileFn = streamer.read.bind(streamer);
287287

288288
flow.opts.chunkSize = 1;
289289
flow.opts.maxChunkRetries = 3;

test/uploadSpec.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,6 @@ describe('upload file', function() {
400400
expect(xhr.requests.length).toBe(0);
401401
expect(preprocess).toHaveBeenCalledWith(file.chunks[0]);
402402
expect(file.chunks[0].preprocessState).toBe(1);
403-
file.chunks[0].preprocessFinished();
404403
expect(xhr.requests.length).toBe(1);
405404
xhr.requests[0].respond(200, [], "response");
406405
expect(success).toHaveBeenCalledWith(asCustomEvent(file, "response", file.chunks[0]));
@@ -416,7 +415,7 @@ describe('upload file', function() {
416415
var file = flow.files[0];
417416
var secondFile = flow.files[1];
418417
await flow.upload();
419-
expect(xhr.requests.length).toBe(0);
418+
expect(xhr.requests.length).toBe(1);
420419
expect(preprocess).toHaveBeenCalledWith(file.chunks[0]);
421420
expect(preprocess).not.toHaveBeenCalledWith(secondFile.chunks[0]);
422421

@@ -439,7 +438,6 @@ describe('upload file', function() {
439438
await flow.upload();
440439
for(var i=0; i<file.chunks.length; i++) {
441440
expect(preprocess).toHaveBeenCalledWith(file.chunks[i]);
442-
file.chunks[i].preprocessFinished();
443441
await file.pause();
444442
await file.resume();
445443
xhr.requests[xhr.requests.length-1].respond(200, [], "response");

0 commit comments

Comments
 (0)