Skip to content

Commit

Permalink
fix: Finish the push promise after the 'close' event is triggered (ap…
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Feb 3, 2020
1 parent 3742afa commit a2ba798
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib/commands/file-movement.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,18 @@ async function pushFileToRealDevice (device, remotePath, base64Data) {
try {
await mkdirpDevice(service, path.dirname(relativePath));
const stream = await service.createWriteStream(relativePath, {autoDestroy: true});
let pushError = null;
const pushPromise = new B((resolve, reject) => {
stream.on('error', reject);
stream.on('close', resolve);
stream.on('error', (e) => {
pushError = e;
});
stream.on('close', () => {
if (pushError) {
reject(pushError);
} else {
resolve();
}
});
});
stream.write(Buffer.from(base64Data, 'base64'));
stream.end();
Expand Down
12 changes: 10 additions & 2 deletions lib/ios-deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,19 @@ class IOSDeploy {
const readStream = fs.createReadStream(itemPath, {autoClose: true});
const writeStream = await afcService.createWriteStream(pathOnPhone, {autoDestroy: true});
writeStream.on('finish', writeStream.destroy);
let pushError = null;
const itemPushWait = new B((resolve, reject) => {
writeStream.on('close', resolve);
writeStream.on('close', () => {
if (pushError) {
reject(pushError);
} else {
resolve();
}
});
const onStreamError = (e) => {
readStream.unpipe(writeStream);
reject(e);
log.debug(e);
pushError = e;
};
writeStream.on('error', onStreamError);
readStream.on('error', onStreamError);
Expand Down

0 comments on commit a2ba798

Please sign in to comment.