Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@wertarbyte/local-ftp-sftp

Package Overview
Dependencies
Maintainers
4
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wertarbyte/local-ftp-sftp

Treat Local, FTP and SFTP file systems the same way.

  • 0.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
15
decreased by-31.82%
Maintainers
4
Weekly downloads
 
Created
Source

Treat local, FTP and SFTP files the same way. 100% Node.js, no other software needed. Under the hood, this library uses the popular ssh2-sftp-client and ftp packages.

Contents

Installation

npm i --save @wertarbyte/local-ftp-sftp

Overview

Class hierarchy

interface FileSystem
  ├── class LocalFileSystem
  ├── class FtpFileSystem
  └── class SftpFileSystem

interface FileInfo
  ├── class LocalFileInfo
  ├── class FtpFileInfo
  └── class SftpFileInfo

Instantiate a FileSystem

const localFileSystem = new LocalFileSystem();
const ftpFileSystem = await FtpFileSystem.create({
  host: '127.0.0.1',
  port: 21,
  user: 'user1',
  password: '12345',
  connectionOptions: {}, // optional, see below
});
const sftpFileSystem = await SftpFileSystem.create({
  host: '127.0.0.1',
  port: 22,
  user: 'user1',
  password: '12345', // optional if you configure private key authentication using the connection options
  connectionOptions: {}, // optional, see below
});

The connectionOptions option is optional and is passed through to the connect methods of the underlying ftp or ssh2-sftp-client instance. You can use this for advanced configuration, e.g. FTPS settings or private key authentication for SFTP.

API

FileSystem

interface FileSystem {
  list(path: string): Promise<FileInfo[]>;
  put(src: NodeJS.ReadableStream, toPath: string): Promise<void>;
  get(path: string): Promise<Buffer>;
  getStream(path: string): Promise<NodeJS.ReadableStream>;
  mkdir(path: string, recursive: boolean): Promise<void>;
  rmdir(path: string, recursive: boolean): Promise<void>;
  delete(path: string): Promise<void>;
  rename(oldPath: string, newPath: string): Promise<void>;
  exists(path: string): Promise<FileType | false>;
  close(): Promise<void>;
}

FileInfo

interface FileInfo {
  readonly name: string;
  readonly size: number;
  readonly type: FileType;
  readonly isDirectory: boolean;
}

Examples

// constants for all examples
const host = '127.0.0.1';
const port = 21; // or 22 for SFTP
const user = 'root';
const password = 'password';

Listing items inside a directory

using Promise/then/catch

// You could just replace FtpFileSystem with SftpFileSystem for SFTP instead of FTP
FtpFileSystem.create({ host, port, user, password })
  // Or: SftpFileSystem.create({ host, port, user, password })
  .then((ftpFileSystem) => {
    // List remote files
    ftpFileSystem
      .list('/home')
      .then((files) => {
        console.log('Files:', files);
      })
      .catch((err) => {
        console.log('Could not retrieve directory /home', err);
      });
  })
  .catch((err) => {
    console.log('Error while connecting to FTP server:', err);
  });

Upload file from local file system

using Promise/then/catch

var localFileSystem = new LocalFileSystem();

SftpFileSystem.create({ host, port, user, password })
  // Or: FtpFileSystem.create({ host, port, user, password })
  .then((sftpFileSystem) => {
    // Get the local file as a stream
    localFileSystem.get('/Users/dennis/catpic.jpeg').then((readStream) => {
      // Now, upload the file to the SFTP server
      sftpFileSystem.put('/home/uploaded_catpic.jpeg', readStream).then(() => {
        console.log('Important file was uploaded successfully, meow!');
      });
    });
  })
  .catch((err) => {
    console.log('Error while uploading file:', err);
  });

Transfer file from FTP server to another SFTP server

using await

const ftpFileSystem = await FtpFileSystem.create({ host, port, user, password });
const sftpFileSystem = await SftpFileSystem.create({ hort, port, user, password });

const readStream = await ftpFileSystem.get('/home/catpic.jpeg');
await sftpFileSystem.put(readStream, '/var/catpic.jpeg');

Create and delete directory

using await

const ftpFileSystem = await FtpFileSystem.create({ host, port, user, password });

// Create the directory
await ftpFileSystem.mkdir('/home/catpics');

// Create directory and sub-directories (recursively: true)
await ftpFileSystem.mkdir('/home/even/more/catpics', true);

// Delete the newly created directory (Only works on empty directories)
await ftpFileSystem.rmdir('/home/catpics');

// Delete directory with content (recursively: true)
await ftpFileSystem.rmdir('/home/even/more/catpics', true);

Keywords

FAQs

Package last updated on 22 Jul 2021

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