Skip to content

Commit

Permalink
feat: 'disconnect' event (#175)
Browse files Browse the repository at this point in the history
Fixes #174
  • Loading branch information
leolabs authored and trs committed Nov 8, 2019
1 parent 0dbb7f9 commit a18841d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
7 changes: 7 additions & 0 deletions ftp-srv.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ export class FtpServer extends EventEmitter {
whitelist?: Array<string>
}) => void,
reject: (err?: Error) => void
) => void): this;

on(event: "disconnect", listener: (
data: {
connection: FtpConnection,
id: string
}
) => void): this;

on(event: "client-error", listener: (
Expand Down
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class FtpServer extends EventEmitter {
_.get(this, 'options.pasv_min'),
_.get(this, 'options.pasv_max'));

const timeout = Number(this.options.timeout);
this.options.timeout = isNaN(timeout) ? 0 : Number(timeout);
const timeout = Number(this.options.timeout);
this.options.timeout = isNaN(timeout) ? 0 : Number(timeout);

const serverConnectionHandler = (socket) => {
socket.setTimeout(this.options.timeout);
Expand All @@ -53,7 +53,7 @@ class FtpServer extends EventEmitter {
const greeting = this._greeting || [];
const features = this._features || 'Ready';
return connection.reply(220, ...greeting, features)
.finally(() => socket.resume());
.finally(() => socket.resume());
};
const serverOptions = Object.assign({}, this.isTLS ? this.options.tls : {}, {pauseOnConnect: true});

Expand Down Expand Up @@ -119,6 +119,7 @@ class FtpServer extends EventEmitter {
return new Promise((resolve) => {
const client = this.connections[id];
if (!client) return resolve();
this.emit('disconnect', {connection: client, id});
delete this.connections[id];
try {
client.close(0);
Expand Down
33 changes: 33 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,39 @@ describe('Integration', function () {
});
}

describe('Server events', function () {
const disconnect = sinon.spy();
const login = sinon.spy();

before(() => {
server.on('login', login);
server.on('disconnect', disconnect);
return connectClient({
host: server.url.hostname,
port: server.url.port,
user: 'test',
password: 'test'
});
});

after(() => {
server.off('login', login);
server.off('disconnect', disconnect);
})

it('should fire a login event on connect', () => {
expect(login.calledOnce).to.be.true;
});

it('should fire a close event on disconnect', (done) => {
client.end();
setTimeout(() => {
expect(disconnect.calledOnce).to.be.true;
done();
}, 100)
});
});

describe('#ASCII', function () {
before(() => {
return connectClient({
Expand Down

0 comments on commit a18841d

Please sign in to comment.