Skip to content

Commit

Permalink
New event emitters (connect, server-error, closing, closed) (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartbutenaers authored Jun 24, 2022
1 parent 50e8c45 commit 51049f6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
46 changes: 40 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) => { ... });
Expand Down Expand Up @@ -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`
Expand Down
15 changes: 12 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -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)))
Expand All @@ -155,6 +163,7 @@ class FtpServer extends EventEmitter {
}))
.then(() => {
this.log.debug('Removing event listeners...')
this.emit('closed', {});
this.removeAllListeners();
return;
});
Expand Down

0 comments on commit 51049f6

Please sign in to comment.