Skip to content

Commit

Permalink
Wait for data and control socket being completely done for every tran…
Browse files Browse the repository at this point in the history
…sfer
  • Loading branch information
patrickjuchli committed Jan 14, 2018
1 parent 7fd298c commit eabd0cd
Showing 1 changed file with 47 additions and 33 deletions.
80 changes: 47 additions & 33 deletions lib/ftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,13 +646,8 @@ function parseIPv4PasvResponse(message) {
* @return {FileInfo[]>}
*/
function list(ftp, parseList = parseListUnix) {
// Some FTP servers transmit the list data and then confirm on the
// control socket that the transfer is complete, others do it the
// other way around. We'll need to make sure that we wait until
// both the data and the confirmation have arrived.
let ctrlDone = false;
const resolver = new TransferResolver(ftp);
let rawList = "";
let parsedList = undefined;
return ftp.handle("LIST", (res, task) => {
if (res.code === 150) { // Ready to download
ftp.dataSocket.on("data", data => {
Expand All @@ -661,17 +656,11 @@ function list(ftp, parseList = parseListUnix) {
ftp.dataSocket.once("end", () => {
ftp.dataSocket = undefined;
ftp.log(rawList);
parsedList = parseList(rawList);
if (ctrlDone) {
task.resolve(parsedList);
}
resolver.resolve(task, parseList(rawList));
});
}
else if (res.code === 226) { // Transfer complete
ctrlDone = true;
if (parsedList) {
task.resolve(parsedList);
}
resolver.confirm(task);
}
else if (res.code >= 400 || res.error) {
ftp.dataSocket = undefined;
Expand All @@ -691,16 +680,20 @@ function list(ftp, parseList = parseListUnix) {
* @returns {Promise<PositiveResponse>}
*/
function upload(ftp, readableStream, remoteFilename) {
const resolver = new TransferResolver(ftp);
const command = "STOR " + remoteFilename;
return ftp.handle(command, (res, task) => {
if (res.code === 150) { // Ready to upload
// If all data has been flushed, close the socket to signal
// the FTP server that the transfer is complete.
ftp.dataSocket.once("finish", () => ftp.dataSocket = undefined);
ftp.dataSocket.once("finish", () => {
ftp.dataSocket = undefined;
resolver.confirm(task);
});
readableStream.pipe(ftp.dataSocket);
}
else if (res.code === 226) { // Transfer complete
task.resolve(res);
resolver.resolve(task, res.code);
}
else if (res.code >= 400 || res.error) {
ftp.dataSocket = undefined;
Expand All @@ -721,32 +714,18 @@ function upload(ftp, readableStream, remoteFilename) {
* @returns {Promise<PositiveResponse>}
*/
function download(ftp, writableStream, remoteFilename, startAt = 0) {
// We have to make sure that the task is only resolved if the
// the server reported transfer complete and the transfer is
// really complete (which is not guaranteed).
let ctrlCode = 0;
let dataDone = false;
function tryResolve(task) {
if (ctrlCode > 0 && dataDone) {
ftp.dataSocket = undefined;
task.resolve(ctrlCode);
}
}
const resolver = new TransferResolver(ftp);
const command = startAt > 0 ? `REST ${startAt}` : `RETR ${remoteFilename}`;
return ftp.handle(command, (res, task) => {
if (res.code === 150) { // Ready to download
ftp.dataSocket.once("end", () => {
dataDone = true;
tryResolve(task)
});
ftp.dataSocket.once("end", () => resolver.confirm(task));
ftp.dataSocket.pipe(writableStream);
}
else if (res.code === 350) { // Restarting at startAt.
ftp.send("RETR " + remoteFilename);
}
else if (res.code === 226) { // Transfer complete
ctrlCode = res.code;
tryResolve(task);
resolver.resolve(task, res.code);
}
else if (res.code >= 400 || res.error) {
ftp.dataSocket = undefined;
Expand All @@ -755,6 +734,41 @@ function download(ftp, writableStream, remoteFilename, startAt = 0) {
});
}

/**
* Resolves a given task if one party has provided a result and another
* one confirmed it. This is used for all FTP transfers. For example when
* downloading, the server might confirm with "226 Transfer complete" when
* in fact the download on the data connection has not finished yet. With
* all transfers we make sure that a) the result arrived and b) has been
* confirmed by e.g. the control connection. We just don't know in which
* order this will happen.
*/
class TransferResolver {

constructor(ftp) {
this.ftp = ftp;
this.result = undefined;
this.confirmed = false;
}

resolve(task, result) {
this.result = result;
this._tryResolve(task);
}

confirm(task) {
this.confirmed = true;
this._tryResolve(task);
}

_tryResolve(task) {
if (this.confirmed && this.result !== undefined) {
this.ftp.dataSocket = undefined;
task.resolve(this.result);
}
}
}

async function ensureLocalDirectory(path) {
try {
await fsStat(path);
Expand Down

0 comments on commit eabd0cd

Please sign in to comment.