UNPKG

ftp-srv-u

Version:
49 lines (42 loc) 1.54 kB
const {Socket} = require('net'); const tls = require('tls'); const Promise = require('bluebird'); const Connector = require('./base'); class Active extends Connector { constructor(connection) { super(connection); this.type = 'active'; } waitForConnection({timeout = 5000, delay = 250} = {}) { const checkSocket = () => { if (this.dataSocket && this.dataSocket.connected) { return Promise.resolve(this.dataSocket); } return Promise.resolve().delay(delay) .then(() => checkSocket()); }; return checkSocket().timeout(timeout); } setupConnection(host, port, family = 4) { const closeExistingServer = () => Promise.resolve( this.dataSocket ? this.dataSocket.destroy() : undefined); return closeExistingServer() .then(() => { this.dataSocket = new Socket(); this.dataSocket.on('error', (err) => this.server && this.server.emit('client-error', {connection: this.connection, context: 'dataSocket', error: err})); this.dataSocket.connect({host, port, family}, () => { this.dataSocket.pause(); if (this.connection.secure) { const secureContext = tls.createSecureContext(this.server.options.tls); const secureSocket = new tls.TLSSocket(this.dataSocket, { isServer: true, secureContext }); this.dataSocket = secureSocket; } this.dataSocket.connected = true; }); }); } } module.exports = Active;