Skip to content

Commit c7d2099

Browse files
authored
Merge pull request #5 from mailsvb/listenEvents
emit specific listen events
2 parents 3155069 + 552ca5c commit c7d2099

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

lib/jsftpd.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,20 @@ class ftpd {
8484
this._tcp = net.createServer()
8585
this._tcp.on('connection', (socket) => this.Handler(this, socket))
8686
this._tcp.on('error', (err) => this.ErrorHandler(err))
87-
this._tcp.on('listening', () => this.DebugHandler(`FTP server listening on ${util.inspect(this._tcp.address(), { showHidden: false, depth: null, breakLength: 'Infinity' })}`))
87+
this._tcp.on('listening', () => {
88+
this.DebugHandler(`FTP server listening on ${util.inspect(this._tcp.address(), { showHidden: false, depth: null, breakLength: 'Infinity' })}`)
89+
this.emit('listen-tcp', this._tcp.address())
90+
})
8891
this._tcp.maxConnections = this._opt.cnf.maxConnections
8992

9093
// setup FTP on TLS
9194
this._tls = tls.createServer(this._opt.tls)
9295
this._tls.on('secureConnection', (socket) => this.Handler(this, socket))
9396
this._tls.on('error', (err) => this.ErrorHandler(err))
94-
this._tls.on('listening', () => this.DebugHandler(`FTP server listening on ${util.inspect(this._tls.address(), { showHidden: false, depth: null, breakLength: 'Infinity' })}`))
97+
this._tls.on('listening', () => {
98+
this.DebugHandler(`FTP server listening on ${util.inspect(this._tls.address(), { showHidden: false, depth: null, breakLength: 'Infinity' })}`)
99+
this.emit('listen-tls', this._tls.address())
100+
})
95101
this._tls.maxConnections = this._opt.cnf.maxConnections
96102
}
97103

test/jsftpd.test.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,25 @@ test('create ftpd instance without options created with default values', () => {
3838
expect(server._opt.cnf.securePort).toBe(990)
3939
});
4040

41-
test('ftp server can be started on non default ports', () => {
41+
test('ftp server can be started on non default ports', async () => {
4242
server = new ftpd({tls: {rejectUnauthorized: false}, cnf: {port: 50021, securePort: 50990}})
4343
expect(server).toBeInstanceOf(ftpd);
4444
expect(server._opt.cnf.port).toBe(50021)
4545
expect(server._opt.cnf.securePort).toBe(50990)
4646
server.start()
4747
expect(server._tcp.address().port).toBe(50021)
4848
expect(server._tls.address().port).toBe(50990)
49+
server.on('listen-tls', data => expect(data.port).toBe(50990))
50+
server.on('listen-tcp', data => expect(data.port).toBe(50021))
51+
52+
const promiseSocket = new PromiseSocket(new net.Socket())
53+
const socket = promiseSocket.stream
54+
let content
55+
await socket.connect(50021, 'localhost')
56+
content = await promiseSocket.read();
57+
expect(content.toString().trim()).toBe('220 Welcome')
58+
59+
await promiseSocket.end()
4960
});
5061

5162
test('ftp server fails when basefolder does not exist', () => {

0 commit comments

Comments
 (0)