/** * Retrieves a directory listing * * @param {String} path, a string containing the path to a directory * @return {Promise} data, list info */ SftpClient.prototype.list = function(path) { let reg = /-/gi; return new Promise((resolve, reject) => { let sftp = this.sftp; if (sftp) { this.client.on('error', reject ); // CHANGED TO PASS reject FUNCTION DIRECTLY sftp.readdir(path, (err, list) => { this.client.removeListener( 'error', reject ); // ADDED if (err) { reject(err); return false; } // reset file info list.forEach((item, i) => { list[i] = { type: item.longname.substr(0, 1), name: item.filename, size: item.attrs.size, modifyTime: item.attrs.mtime * 1000, accessTime: item.attrs.atime * 1000, rights: { user: item.longname.substr(1, 3).replace(reg, ''), group: item.longname.substr(4,3).replace(reg, ''), other: item.longname.substr(7, 3).replace(reg, '') }, owner: item.attrs.uid, group: item.attrs.gid } }); resolve(list); }); } else { reject(Error('sftp connect error')); } }); }; /** * get file * * @param {String} path, path * @param {Object} useCompression, config options * @param {String} encoding. Encoding for the ReadStream, can be any value supported by node streams. Use 'null' for binary (https://nodejs.org/api/stream.html#stream_readable_setencoding_encoding) * @return {Promise} stream, readable stream */ SftpClient.prototype.get = function(path, useCompression, encoding, otherOptions) { let options = this.getOptions(useCompression, encoding, otherOptions) return new Promise((resolve, reject) => { let sftp = this.sftp; if (sftp) { try { this.client.on('error', reject ); // CHANGED TO PASS reject FUNCTION DIRECTLY let stream = sftp.createReadStream(path, options); stream.on('error', (err) => { this.client.removeListener( 'error', reject ); // ADDED reject(err); }); stream.on('readable', () => { this.client.removeListener( 'error', reject ); // ADDED resolve(stream); }); } catch(err) { this.client.removeListener( 'error', reject ); // ADDED reject(err); } } else { reject(Error('sftp connect error')); } }); };