Skip to content

Commit

Permalink
change order of resolutions, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FGasper committed Mar 24, 2019
1 parent 5f610b5 commit edd14cc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
22 changes: 17 additions & 5 deletions src/zsession.js
Expand Up @@ -737,10 +737,12 @@ Zmodem.Session.Receive = class ZmodemReceiveSession extends Zmodem.Session {
this._next_header_handler = {
ZEOF: function on_ZEOF(hdr) {
this._next_subpacket_handler = null;
this._consume_ZEOF(hdr);

var next_promise = this._make_promise_for_between_files();
resolve_accept(next_promise);
this._make_promise_for_between_files();

resolve_accept();

this._consume_ZEOF(hdr)
},
};
},
Expand All @@ -756,6 +758,16 @@ Zmodem.Session.Receive = class ZmodemReceiveSession extends Zmodem.Session {
var ret = this._make_promise_for_between_files();

if (this._accepted_offer) {
// There’s a race condition where we might attempt to
// skip() an in-progress transfer near its end but actually
// the skip() will fire after the transfer is complete.
// While there might be ways to prevent this, they likely
// would require extra work on the part of implementations.
//
// It seems far simpler just to make this function a no-op
// in these cases.
if (!this._current_transfer) return;

//For cancel of an in-progress transfer from lsz,
//it’s necessary to avoid this buffer overflow bug:
//
Expand Down Expand Up @@ -818,10 +830,10 @@ Zmodem.Session.Receive = class ZmodemReceiveSession extends Zmodem.Session {
throw( "ZEOF offset mismatch; unimplemented (local: " + this._file_offset + "; ZEOF: " + header.get_offset() + ")" );
}

this._send_ZRINIT();

this._on_file_end();

this._send_ZRINIT();

//Preserve these two so that file_end callbacks
//will have the right information.
this._file_info = null;
Expand Down
2 changes: 0 additions & 2 deletions tests/zsession_rz.js
Expand Up @@ -25,8 +25,6 @@ var helper = require('./lib/testhelp');
var dir_before = process.cwd();
tape.onFinish( () => process.chdir( dir_before ) );

process.chdir( helper.make_temp_dir() );

let TEST_STRINGS = [
"",
"0",
Expand Down
11 changes: 8 additions & 3 deletions tests/zsession_sz.js
Expand Up @@ -158,17 +158,22 @@ tape('skip() - immediately - at end of download', { timeout: 30000 }, (t) => {
] );
});

// Verify a skip() that happens after a transfer is complete.
// There are no assertions here.
tape('skip() - after a parse - at end of download', { timeout: 30000 }, (t) => {
var filenames = [helper.make_temp_file(123)];

var the_offer, started, skipped;
var the_offer, started, skipped, completed;

return _test_steps( t, filenames, [
(zsession) => {
if (!started) {
function offer_taker(offer) {
the_offer = offer;
the_offer.accept();
var promise = the_offer.accept();
promise.then( () => {
completed = 1;
} );
}
zsession.on("offer", offer_taker);
zsession.start();
Expand All @@ -178,7 +183,7 @@ tape('skip() - after a parse - at end of download', { timeout: 30000 }, (t) => {
return the_offer;
},
() => {
if (!skipped) {
if (!skipped && !completed) {
the_offer.skip();
skipped = true;
}
Expand Down

0 comments on commit edd14cc

Please sign in to comment.