1.1.1 • Published 5 months ago

scachetp v1.1.1

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

SCacheTP

A simple in-memory SFTP server that handles file uploads only. Uses ssh2 under the hood.

Installation

npm install scachetp

Features

  • Simple SFTP server focused on file uploads
  • In-memory file handling (no files written to disk by default)
  • Custom authentication support
  • Event callbacks for uploads, connections, and authentication

Quick Start

const UploadSftpServer = require('scachetp');
const fs = require('fs');
const path = require('path');

// Create a new server instance
const server = new UploadSftpServer({
  port: 2222,
  hostKeys: [fs.readFileSync(path.join(__dirname, 'host_key'))],
  users: [
    { username: 'testuser', password: 'password123' }
  ],
  onUpload: (fileData) => {
    console.log(`File uploaded: ${fileData.filename}`);
    console.log(`Size: ${fileData.size} bytes`);
    
    // Process the uploaded file (fileData.buffer contains the file content)
    // Example: save to disk
    fs.writeFileSync(
      path.join(__dirname, 'uploads', path.basename(fileData.filename)),
      fileData.buffer
    );
  }
});

// Start the server
server.listen().then(({ port, host }) => {
  console.log(`Server running at ${host}:${port}`);
});

API Reference

Constructor Options

The UploadSftpServer constructor accepts an options object with the following properties:

OptionTypeDefaultDescription
portNumber2222Port to listen on
hostString'0.0.0.0'Host to bind to
hostKeysArray[]Array of private keys (as Buffers) for the server
debugBooleanfalseEnable debug logging
usersArray[]Array of user objects with username and password properties
authHandlerFunctionnullCustom authentication handler function

Event Callbacks

You can provide callback functions for various events:

CallbackParametersDescription
onUpload(fileData)Called when a file is uploaded
onConnect(client)Called when a client connects
onDisconnect(client)Called when a client disconnects
onAuthenticated(username)Called when a user is authenticated
onError(error)Called when an error occurs

The fileData object passed to onUpload contains:

  • filename: The name of the uploaded file
  • username: The username of the uploader
  • buffer: The file content as a Buffer
  • size: The size of the file in bytes

Methods

MethodParametersReturnsDescription
listen([port, host])Optional port and hostPromiseStarts the server
close()NonePromiseStops the server

Custom Authentication

You can provide a custom authentication handler function:

const server = new UploadSftpServer({
  // ...other options
  authHandler: async (authInfo, filesystem) => {
    // authInfo contains: connection, username, password, method
    
    // Example: check against a database
    const user = await db.findUser(authInfo.username);
    if (user && user.password === authInfo.password) {
      return true; // Authentication successful
    }
    return false; // Authentication failed
  }
});

Generating Host Keys

To generate a host key for your server:

ssh-keygen -t rsa -f host_key -N ""

License

MIT

1.1.1

5 months ago

1.1.0

5 months ago