Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove bunyan and move to winston #381

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
883 changes: 689 additions & 194 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
},
"dependencies": {
"bluebird": "^3.5.1",
"bunyan": "^1.8.12",
"winston": "^3.15.0",
"ip": "^1.1.5",
"lodash": "^4.17.15",
"moment": "^2.22.1",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class FtpCommands {
if (logCommand.directive === 'PASS') logCommand.arg = '********';

const log = this.connection.log.child({directive: command.directive});
log.trace({command: logCommand}, 'Handle command');
log.log('silly', 'Handle command', {command: logCommand});

if (!REGISTRY.hasOwnProperty(command.directive)) {
return this.connection.reply(502, `Command not allowed: ${command.directive}`);
Expand Down
10 changes: 5 additions & 5 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ class FtpConnection extends EventEmitter {
this.connector = new BaseConnector(this);

this.commandSocket.on('error', (err) => {
this.log.error(err, 'Client error');
this.log.error('Client error', {err});
this.server.emit('client-error', {connection: this, context: 'commandSocket', error: err});
});
this.commandSocket.on('data', this._handleData.bind(this));
this.commandSocket.on('timeout', () => {
this.log.trace('Client timeout');
this.log.log('silly','Client timeout');
this.close();
});
this.commandSocket.on('close', () => {
Expand All @@ -43,7 +43,7 @@ class FtpConnection extends EventEmitter {

_handleData(data) {
const messages = _.compact(data.toString(this.encoding).split('\r\n'));
this.log.trace(messages);
this.log.log('silly',messages);
return Promise.mapSeries(messages, (message) => this.commands.handle(message));
}

Expand Down Expand Up @@ -128,7 +128,7 @@ class FtpConnection extends EventEmitter {
const processLetter = (letter) => {
return new Promise((resolve, reject) => {
if (letter.socket && letter.socket.writable) {
this.log.trace({port: letter.socket.address().port, encoding: letter.encoding, message: letter.message}, 'Reply');
this.log.log('silly', 'Reply', {port: letter.socket.address().port, encoding: letter.encoding, message: letter.message});
letter.socket.write(letter.message + '\r\n', letter.encoding, (error) => {
if (error) {
this.log.error('[Process Letter] Socket Write Error', { error: error.message });
Expand All @@ -137,7 +137,7 @@ class FtpConnection extends EventEmitter {
resolve();
});
} else {
this.log.trace({message: letter.message}, 'Could not write message');
this.log.log('silly', 'Could not write message', {message: letter.message});
reject(new errors.SocketError('Socket not writable'));
}
});
Expand Down
12 changes: 6 additions & 6 deletions src/connector/passive.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ class Passive extends Connector {

const connectionHandler = (socket) => {
if (!ip.isEqual(this.connection.commandSocket.remoteAddress, socket.remoteAddress)) {
this.log.error({
this.log.error('Connecting addresses do not match', {
pasv_connection: socket.remoteAddress,
cmd_connection: this.connection.commandSocket.remoteAddress
}, 'Connecting addresses do not match');
});

socket.destroy();
return this.connection.reply(550, 'Remote addresses do not match')
.then(() => this.connection.close());
}
clearTimeout(idleServerTimeout);

this.log.trace({port, remoteAddress: socket.remoteAddress}, 'Passive connection fulfilled.');
this.log.log('silly', 'Passive connection fulfilled.', {port, remoteAddress: socket.remoteAddress});

this.dataSocket = socket;
this.dataSocket.on('error', (err) => this.server && this.server.emit('client-error', {connection: this.connection, context: 'dataSocket', error: err}));
Expand All @@ -65,7 +65,7 @@ class Passive extends Connector {

this.dataServer.on('error', (err) => this.server && this.server.emit('client-error', {connection: this.connection, context: 'dataServer', error: err}));
this.dataServer.once('close', () => {
this.log.trace('Passive server closed');
this.log.log('silly','Passive server closed');
this.end();
});

Expand All @@ -81,14 +81,14 @@ class Passive extends Connector {
else {
idleServerTimeout = setTimeout(() => this.closeServer(), CONNECT_TIMEOUT);

this.log.debug({port}, 'Passive connection listening');
this.log.log('debug', 'Passive connection listening', {port});
resolve(this.dataServer);
}
});
});
})
.catch((error) => {
this.log.trace(error.message);
this.log.log('silly', error.message);
throw error;
});
}
Expand Down
22 changes: 11 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const _ = require('lodash');
const Promise = require('bluebird');
const nodeUrl = require('url');
const buyan = require('bunyan');
const winston = require('winston');
const net = require('net');
const tls = require('tls');
const EventEmitter = require('events');
Expand All @@ -13,7 +13,7 @@ class FtpServer extends EventEmitter {
constructor(options = {}) {
super();
this.options = Object.assign({
log: buyan.createLogger({name: 'ftp-srv'}),
log: winston.createLogger({name: 'ftp-srv', silent: true}),
url: 'ftp://127.0.0.1:21',
pasv_min: 1024,
pasv_max: 65535,
Expand Down Expand Up @@ -52,7 +52,7 @@ class FtpServer extends EventEmitter {
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 || [];
Expand All @@ -64,10 +64,10 @@ class FtpServer extends EventEmitter {

this.server = (this.isTLS ? tls : net).createServer(serverOptions, serverConnectionHandler);
this.server.on('error', (err) => {
this.log.error(err, '[Event] error');
this.log.error('[Event] error', {err});
this.emit('server-error', {error: err});
});

const quit = _.debounce(this.quit.bind(this), 100);

process.on('SIGTERM', quit);
Expand All @@ -89,11 +89,11 @@ class FtpServer extends EventEmitter {
this.server.listen(this.url.port, this.url.hostname, (err) => {
this.server.removeListener('error', reject);
if (err) return reject(err);
this.log.info({
this.log.info('Listening', {
protocol: this.url.protocol.replace(/\W/g, ''),
ip: this.url.hostname,
port: this.url.port
}, 'Listening');
});
resolve('Listening');
});
});
Expand Down Expand Up @@ -136,9 +136,9 @@ class FtpServer extends EventEmitter {
try {
client.close(0);
} catch (err) {
this.log.error(err, 'Error closing connection', {id});
this.log.error('Error closing connection', {id, err});
}

resolve('Disconnected');
});
}
Expand All @@ -157,12 +157,12 @@ class FtpServer extends EventEmitter {
.then(() => new Promise((resolve) => {
this.server.close((err) => {
this.log.info('Server closing...');
if (err) this.log.error(err, 'Error closing server');
if (err) this.log.error('Error closing server', {err});
resolve('Closed');
});
}))
.then(() => {
this.log.debug('Removing event listeners...')
this.log.log('debug','Removing event listeners...')
this.emit('closed', {});
this.removeAllListeners();
return;
Expand Down
4 changes: 2 additions & 2 deletions test/commands/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {expect} = require('chai');
const Promise = require('bluebird');
const bunyan = require('bunyan');
const winston = require('winston');
const sinon = require('sinon');

const FtpCommands = require('../../src/commands');
Expand All @@ -10,7 +10,7 @@ describe('FtpCommands', function () {
let commands;
let mockConnection = {
authenticated: false,
log: bunyan.createLogger({name: 'FtpCommands'}),
log: winston.createLogger({name: 'FtpCommands'}),
reply: () => Promise.resolve({}),
server: {
options: {
Expand Down
4 changes: 2 additions & 2 deletions test/commands/registration/cdup.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const Promise = require('bluebird');
const bunyan = require('bunyan');
const winston = require('winston');
const {expect} = require('chai');
const sinon = require('sinon');

const CMD = 'CDUP';
describe(CMD, function () {
let sandbox;
let log = bunyan.createLogger({name: CMD});
let log = winston.createLogger({name: CMD});
const mockClient = {
reply: () => Promise.resolve(),
fs: {
Expand Down
4 changes: 2 additions & 2 deletions test/commands/registration/cwd.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const bunyan = require('bunyan');
const winston = require('winston');
const {expect} = require('chai');
const sinon = require('sinon');

const CMD = 'CWD';
describe(CMD, function () {
let sandbox;
let log = bunyan.createLogger({name: CMD});
let log = winston.createLogger({name: CMD});
const mockClient = {
reply: () => {},
fs: {chdir: () => {}}
Expand Down
4 changes: 2 additions & 2 deletions test/commands/registration/dele.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const bunyan = require('bunyan');
const winston = require('winston');
const {expect} = require('chai');
const sinon = require('sinon');

const CMD = 'DELE';
describe(CMD, function () {
let sandbox;
let log = bunyan.createLogger({name: CMD});
let log = winston.createLogger({name: CMD});
const mockClient = {
reply: () => {},
fs: {delete: () => {}}
Expand Down
4 changes: 2 additions & 2 deletions test/commands/registration/list.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const Promise = require('bluebird');
const bunyan = require('bunyan');
const winston = require('winston');
const {expect} = require('chai');
const sinon = require('sinon');

const CMD = 'LIST';
describe(CMD, function () {
let sandbox;
let log = bunyan.createLogger({name: CMD});
let log = winston.createLogger({name: CMD});
const mockClient = {
reply: () => {},
fs: {
Expand Down
4 changes: 2 additions & 2 deletions test/commands/registration/mdtm.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const bunyan = require('bunyan');
const winston = require('winston');
const {expect} = require('chai');
const sinon = require('sinon');

const CMD = 'MDTM';
describe(CMD, function () {
let sandbox;
let log = bunyan.createLogger({name: CMD});
let log = winston.createLogger({name: CMD});
const mockClient = {
reply: () => {},
fs: {get: () => {}}
Expand Down
4 changes: 2 additions & 2 deletions test/commands/registration/mkd.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const bunyan = require('bunyan');
const winston = require('winston');
const {expect} = require('chai');
const sinon = require('sinon');

const CMD = 'MKD';
describe(CMD, function () {
let sandbox;
let log = bunyan.createLogger({name: CMD});
let log = winston.createLogger({name: CMD});
const mockClient = {
reply: () => {},
fs: {mkdir: () => {}}
Expand Down
4 changes: 2 additions & 2 deletions test/commands/registration/nlst.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const Promise = require('bluebird');
const bunyan = require('bunyan');
const winston = require('winston');
const {expect} = require('chai');
const sinon = require('sinon');

const CMD = 'NLST';
describe(CMD, function () {
let sandbox;
let log = bunyan.createLogger({name: CMD});
let log = winston.createLogger({name: CMD});
const mockClient = {
reply: () => {},
fs: {
Expand Down
4 changes: 2 additions & 2 deletions test/commands/registration/pass.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const bunyan = require('bunyan');
const winston = require('winston');
const {expect} = require('chai');
const sinon = require('sinon');

const CMD = 'PASS';
describe(CMD, function () {
let sandbox;
let log = bunyan.createLogger({name: CMD});
let log = winston.createLogger({name: CMD});
const mockClient = {
reply: () => {},
login: () => {},
Expand Down
4 changes: 2 additions & 2 deletions test/commands/registration/pwd.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const bunyan = require('bunyan');
const winston = require('winston');
const {expect} = require('chai');
const sinon = require('sinon');

const CMD = 'PWD';
describe(CMD, function () {
let sandbox;
let log = bunyan.createLogger({name: CMD});
let log = winston.createLogger({name: CMD});
const mockClient = {
reply: () => {},
fs: {currentDirectory: () => {}}
Expand Down
4 changes: 2 additions & 2 deletions test/commands/registration/retr.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const Promise = require('bluebird');
const bunyan = require('bunyan');
const winston = require('winston');
const {expect} = require('chai');
const sinon = require('sinon');
const EventEmitter = require('events');

const CMD = 'RETR';
describe(CMD, function () {
let sandbox;
let log = bunyan.createLogger({name: CMD});
let log = winston.createLogger({name: CMD});
let emitter;
const mockClient = {
commandSocket: {
Expand Down
4 changes: 2 additions & 2 deletions test/commands/registration/site/site.spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
const Promise = require('bluebird');
const {expect} = require('chai');
const sinon = require('sinon');
const bunyan = require('bunyan');
const winston = require('winston');

const siteRegistry = require('../../../../src/commands/registration/site/registry');
const FtpCommands = require('../../../../src/commands');

const CMD = 'SITE';
describe(CMD, function () {
let sandbox;
const log = bunyan.createLogger({name: 'site-test'});
const log = winston.createLogger({name: 'site-test'});
const mockClient = {
reply: () => Promise.resolve(),
commands: new FtpCommands()
Expand Down
4 changes: 2 additions & 2 deletions test/commands/registration/stor.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
const Promise = require('bluebird');
const bunyan = require('bunyan');
const winston = require('winston');
const {expect} = require('chai');
const sinon = require('sinon');
const EventEmitter = require('events');

const CMD = 'STOR';
describe(CMD, function () {
let sandbox;
let log = bunyan.createLogger({name: CMD});
let log = winston.createLogger({name: CMD});
let emitter;
const mockClient = {
commandSocket: {
Expand Down
Loading