New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ftp-library

Package Overview
Dependencies
Maintainers
0
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ftp-library

A lightweight Node.js FTP server library built from scratch. Fully compliant with RFC 959, it supports custom configurations, directory listings, authentication, and more. Perfect for developers needing a robust FTP server for testing, development, or lig

  • 1.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
0
Weekly downloads
 
Created
Source

FTP Library

A lightweight FTP server library for Node.js. Fully compliant with RFC 959, this library is designed to be fast, reliable, and customizable. It is ideal for developers needing an FTP server for development, testing, or lightweight production use.

Features

  • Simple API for creating FTP servers.
  • Customizable port, host, and root directory settings.
  • Built-in authentication and directory listing support.
  • Lightweight, fast, and easy to extend.
  • Newly Added Commands: CWD, MKD, RMD, STAT, and HELP for enhanced functionality.

Installation

npm install js-ftp-server

Usage

const { FTPServer } = require('js-ftp-server');

const server = new FTPServer({ port: 2121 });
server.start();

Examples

1. Basic Server Setup

const { FTPServer } = require('ftp-library');

const server = new FTPServer({ port: 2121 });
server.start();

2. Custom Port and Host

const { FTPServer } = require('ftp-library');

const server = new FTPServer({ port: 2121, host: '127.0.0.1' });
server.start();

3. Anonymous Access

const { FTPServer } = require('ftp-library');

const server = new FTPServer({ allowAnonymous: true });
server.start();

4. Custom Root Directory

const { FTPServer } = require('ftp-library');

const server = new FTPServer({ rootDir: '/var/ftp' });
server.start();

5. User Authentication

const { FTPServer } = require('ftp-library');
const { validateCredentials } = require('./src/utils');

const server = new FTPServer();

server.handleConnection = (socket) => {
  socket.write('220 Welcome to the custom FTP server\r\n');
  socket.on('data', (data) => {
    const command = data.toString().trim();
    if (command.startsWith('USER admin') && validateCredentials('admin', 'password')) {
      socket.write('230 User logged in, proceed\r\n');
    } else {
      socket.write('530 Login incorrect\r\n');
    }
  });
};

server.start();

6. File Upload

const { FTPServer } = require('ftp-library');
const fs = require('fs');
const path = require('path');

const server = new FTPServer();

server.handleConnection = (socket) => {
  socket.on('data', (data) => {
    const command = data.toString().trim();
    if (command.startsWith('STOR')) {
      const fileName = command.split(' ')[1];
      const writeStream = fs.createWriteStream(path.join(server.rootDir, fileName));
      socket.pipe(writeStream);
      socket.write('226 Transfer complete\r\n');
    }
  });
};

server.start();

7. File Download

const { FTPServer } = require('ftp-library');
const fs = require('fs');
const path = require('path');

const server = new FTPServer();

server.handleConnection = (socket) => {
  socket.on('data', (data) => {
    const command = data.toString().trim();
    if (command.startsWith('RETR')) {
      const fileName = command.split(' ')[1];
      const readStream = fs.createReadStream(path.join(server.rootDir, fileName));
      readStream.pipe(socket);
      socket.write('226 Transfer complete\r\n');
    }
  });
};

server.start();

8. Passive Mode Support

const { FTPServer } = require('ftp-library');

const server = new FTPServer();

server.handleConnection = (socket) => {
  socket.on('data', (data) => {
    const command = data.toString().trim();
    if (command.startsWith('PASV')) {
      socket.write('227 Entering Passive Mode (127,0,0,1,192,168)\r\n');
    }
  });
};

server.start();

9. Logging Commands

const { FTPServer } = require('ftp-library');

const server = new FTPServer();

server.handleConnection = (socket) => {
  socket.on('data', (data) => {
    console.log(`Command received: ${data.toString().trim()}`);
    socket.write('502 Command not implemented\r\n');
  });
};

server.start();

10. Configurable Welcome Message

const { FTPServer } = require('ftp-library');

const server = new FTPServer();

server.handleConnection = (socket) => {
  socket.write('220 Welcome to My Custom FTP Server\r\n');
};

server.start();

11. Multiple Connections

const { FTPServer } = require('ftp-library');

const server = new FTPServer();

server.handleConnection = (socket) => {
  console.log('New connection established');
  socket.write('220 Ready\r\n');
};

server.start();

12. Rename File

const { FTPServer } = require('ftp-library');
const fs = require('fs');

const server = new FTPServer();

server.handleConnection = (socket) => {
  socket.on('data', (data) => {
    const command = data.toString().trim();
    if (command.startsWith('RNFR')) {
      const oldName = command.split(' ')[1];
      socket.once('data', (newData) => {
        const newName = newData.toString().trim().split(' ')[1];
        fs.renameSync(oldName, newName);
        socket.write('250 File renamed\r\n');
      });
    }
  });
};

server.start();

13. Delete File

const { FTPServer } = require('ftp-library');
const fs = require('fs');

const server = new FTPServer();

server.handleConnection = (socket) => {
  socket.on('data', (data) => {
    const command = data.toString().trim();
    if (command.startsWith('DELE')) {
      const fileName = command.split(' ')[1];
      fs.unlinkSync(fileName);
      socket.write('250 File deleted\r\n');
    }
  });
};

server.start();

14. Custom Command Implementation

const { FTPServer } = require('ftp-library');

const server = new FTPServer();

server.handleConnection = (socket) => {
  socket.on('data', (data) => {
    const command = data.toString().trim();
    if (command === 'MYCMD') {
      socket.write('200 Custom command executed\r\n');
    }
  });
};

server.start();

15. Advanced Logging

const { FTPServer } = require('ftp-library');
const fs = require('fs');

const logStream = fs.createWriteStream('./ftp-commands.log', { flags: 'a' });

const server = new FTPServer();

server.handleConnection = (socket) => {
  socket.on('data', (data) => {
    const command = data.toString().trim();
    logStream.write(`${new Date().toISOString()} - ${command}\n`);
    socket.write('502 Command not implemented\r\n');
  });
};

server.start();

License

MIT

Keywords

FAQs

Package last updated on 01 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc