Skip to content

Commit

Permalink
Merge pull request #36 from trs/fix-process-exit
Browse files Browse the repository at this point in the history
Fix process exit
  • Loading branch information
trs authored Aug 14, 2017
2 parents f318331 + 31f0f3b commit ed086e5
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 30 deletions.
1 change: 1 addition & 0 deletions config/verify/.eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ parserOptions:
<<: *confit-parserOptions

rules:
no-process-exit: 0
max-len:
- warn
- 200 # Line Length
Expand Down
8 changes: 2 additions & 6 deletions src/commands/registration/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,9 @@ module.exports = {

const simple = command.directive === 'NLST';

let dataSocket;
const path = command.arg || '.';
return this.connector.waitForConnection()
.then(socket => {
this.commandSocket.pause();
dataSocket = socket;
})
.tap(() => this.commandSocket.pause())
.then(() => when.try(this.fs.get.bind(this.fs), path))
.then(stat => stat.isDirectory() ? when.try(this.fs.list.bind(this.fs), path) : [stat])
.then(files => {
Expand All @@ -33,7 +29,7 @@ module.exports = {
return {
raw: true,
message,
socket: dataSocket
socket: this.connector.socket
};
});
return this.reply(150)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/registration/quit.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
directive: 'QUIT',
handler: function () {
return this.close(221);
return this.close(221, 'Client called QUIT');
},
syntax: '{{cmd}}',
description: 'Disconnect',
Expand Down
12 changes: 4 additions & 8 deletions src/commands/registration/retr.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,18 @@ module.exports = {
if (!this.fs) return this.reply(550, 'File system not instantiated');
if (!this.fs.read) return this.reply(402, 'Not supported by file system');

let dataSocket;
return this.connector.waitForConnection()
.then(socket => {
this.commandSocket.pause();
dataSocket = socket;
})
.tap(() => this.commandSocket.pause())
.then(() => when.try(this.fs.read.bind(this.fs), command.arg, {start: this.restByteCount}))
.then(stream => {
this.restByteCount = 0;
return when.promise((resolve, reject) => {
dataSocket.on('error', err => stream.emit('error', err));
this.connector.socket.on('error', err => stream.emit('error', err));

stream.on('data', data => dataSocket.write(data, this.transferType));
stream.on('data', data => this.connector.socket.write(data, this.transferType));
stream.on('end', () => resolve(this.reply(226)));
stream.on('error', err => reject(err));
this.reply(150).then(() => dataSocket.resume());
this.reply(150).then(() => this.connector.socket.resume());
});
})
.catch(when.TimeoutError, err => {
Expand Down
18 changes: 7 additions & 11 deletions src/commands/registration/stor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,24 @@ module.exports = {
const append = command.directive === 'APPE';
const fileName = command.arg;

let dataSocket;
return this.connector.waitForConnection()
.then(socket => {
this.commandSocket.pause();
dataSocket = socket;
})
.tap(() => this.commandSocket.pause())
.then(() => when.try(this.fs.write.bind(this.fs), fileName, {append, start: this.restByteCount}))
.then(stream => {
this.restByteCount = 0;
return when.promise((resolve, reject) => {
stream.once('error', err => dataSocket.emit('error', err));
stream.once('error', err => this.connector.socket.emit('error', err));
stream.once('finish', () => resolve(this.reply(226, fileName)));

// Emit `close` if stream has a close listener, otherwise emit `finish` with the end() method
// It is assumed that the `close` handler will call the end() method
dataSocket.once('end', () => stream.listenerCount('close') ? stream.emit('close') : stream.end());
dataSocket.once('error', err => reject(err));
dataSocket.on('data', data => stream.write(data, this.transferType));
this.connector.socket.once('end', () => stream.listenerCount('close') ? stream.emit('close') : stream.end());
this.connector.socket.once('error', err => reject(err));
this.connector.socket.on('data', data => stream.write(data, this.transferType));

this.reply(150).then(() => dataSocket.resume());
this.reply(150).then(() => this.connector.socket.resume());
})
.finally(() => when.try(stream.destroy.bind(stream)));
.finally(() => stream.destroy ? when.try(stream.destroy.bind(stream)) : null);
})
.catch(when.TimeoutError, err => {
log.error(err);
Expand Down
4 changes: 4 additions & 0 deletions src/connector/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class Connector {
return this.connection.log;
}

get socket() {
return this.dataSocket;
}

get server() {
return this.connection.server;
}
Expand Down
13 changes: 9 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class FtpServer {
this.once = this.server.once.bind(this.server);
this.listeners = this.server.listeners.bind(this.server);

process.on('SIGTERM', () => this.close());
process.on('SIGINT', () => this.close());
process.on('SIGQUIT', () => this.close());
process.on('SIGTERM', () => this.quit());
process.on('SIGINT', () => this.quit());
process.on('SIGQUIT', () => this.quit());
}

get isTLS() {
Expand Down Expand Up @@ -130,10 +130,15 @@ class FtpServer {
});
}

quit() {
return this.close()
.finally(() => process.exit(0));
}

close() {
this.log.info('Server closing...');
this.server.maxConnections = 0;
return when.map(Object.keys(this.connections), id => this.disconnectClient(id))
return when.map(Object.keys(this.connections), id => when.try(this.disconnectClient.bind(this), id))
.then(() => when.promise(resolve => {
this.server.close(err => {
if (err) this.log.error(err, 'Error closing server');
Expand Down

0 comments on commit ed086e5

Please sign in to comment.