New event emitters (connect, server-error, closing, closed) (#314)
This commit is contained in:
46
README.md
46
README.md
@@ -225,6 +225,42 @@ Disable write actions such as upload, delete, etc.
|
||||
|
||||
The `FtpSrv` class extends the [node net.Server](https://nodejs.org/api/net.html#net_class_net_server). Some custom events can be resolved or rejected, such as `login`.
|
||||
|
||||
### `client-error`
|
||||
```js
|
||||
ftpServer.on('client-error', ({connection, context, error}) => { ... });
|
||||
```
|
||||
|
||||
Occurs when an error arises in the client connection.
|
||||
|
||||
`connection` [client class object](src/connection.js)
|
||||
`context` string of where the error occurred
|
||||
`error` error object
|
||||
|
||||
### `disconnect`
|
||||
```js
|
||||
ftpServer.on('disconnect', ({connection, id, newConnectionCount}) => { ... });
|
||||
```
|
||||
|
||||
Occurs when a client has disconnected.
|
||||
|
||||
`connection` [client class object](src/connection.js)
|
||||
`id` string of the disconnected connection id
|
||||
`id` number of the new connection count (exclusive the disconnected client connection)
|
||||
|
||||
### `closed`
|
||||
```js
|
||||
ftpServer.on('closed', ({}) => { ... });
|
||||
```
|
||||
|
||||
Occurs when the FTP server has been closed.
|
||||
|
||||
### `closing`
|
||||
```js
|
||||
ftpServer.on('closing', ({}) => { ... });
|
||||
```
|
||||
|
||||
Occurs when the FTP server has started closing.
|
||||
|
||||
### `login`
|
||||
```js
|
||||
ftpServer.on('login', ({connection, username, password}, resolve, reject) => { ... });
|
||||
@@ -252,15 +288,13 @@ Occurs when a client is attempting to login. Here you can resolve the login requ
|
||||
|
||||
`reject` takes an error object
|
||||
|
||||
### `client-error`
|
||||
### `server-error`
|
||||
```js
|
||||
ftpServer.on('client-error', ({connection, context, error}) => { ... });
|
||||
ftpServer.on('server-error', ({error}) => { ... });
|
||||
```
|
||||
|
||||
Occurs when an error arises in the client connection.
|
||||
|
||||
`connection` [client class object](src/connection.js)
|
||||
`context` string of where the error occurred
|
||||
Occurs when an error arises in the FTP server.
|
||||
|
||||
`error` error object
|
||||
|
||||
### `RETR`
|
||||
|
||||
15
src/index.js
15
src/index.js
@@ -49,7 +49,11 @@ class FtpServer extends EventEmitter {
|
||||
this.connections[connection.id] = connection;
|
||||
|
||||
socket.on('close', () => this.disconnectClient(connection.id));
|
||||
socket.once('close', () => this.emit('disconnect', {connection, id: connection.id}));
|
||||
socket.once('close', () => {
|
||||
this.emit('disconnect', {connection, id: connection.id, newConnectionCount: Object.keys(this.connections).length});
|
||||
})
|
||||
|
||||
this.emit('connect', {connection, id: connection.id, newConnectionCount: Object.keys(this.connections).length});
|
||||
|
||||
const greeting = this._greeting || [];
|
||||
const features = this._features || 'Ready';
|
||||
@@ -59,8 +63,11 @@ class FtpServer extends EventEmitter {
|
||||
const serverOptions = Object.assign({}, this.isTLS ? this.options.tls : {}, {pauseOnConnect: true});
|
||||
|
||||
this.server = (this.isTLS ? tls : net).createServer(serverOptions, serverConnectionHandler);
|
||||
this.server.on('error', (err) => this.log.error(err, '[Event] error'));
|
||||
|
||||
this.server.on('error', (err) => {
|
||||
this.log.error(err, '[Event] error');
|
||||
this.emit('server-error', {error: err});
|
||||
});
|
||||
|
||||
const quit = _.debounce(this.quit.bind(this), 100);
|
||||
|
||||
process.on('SIGTERM', quit);
|
||||
@@ -143,6 +150,7 @@ class FtpServer extends EventEmitter {
|
||||
|
||||
close() {
|
||||
this.server.maxConnections = 0;
|
||||
this.emit('closing');
|
||||
this.log.info('Closing connections:', Object.keys(this.connections).length);
|
||||
|
||||
return Promise.all(Object.keys(this.connections).map((id) => this.disconnectClient(id)))
|
||||
@@ -155,6 +163,7 @@ class FtpServer extends EventEmitter {
|
||||
}))
|
||||
.then(() => {
|
||||
this.log.debug('Removing event listeners...')
|
||||
this.emit('closed', {});
|
||||
this.removeAllListeners();
|
||||
return;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user