{"library":"node-sftp-server","title":"Node.js SFTP Server Implementation","description":"This library provides a simplified, event-driven interface for implementing a basic SFTP server in Node.js. It leverages the robust `ssh2` and `ssh2-streams` libraries for the underlying SSH and SFTP protocol handling. Currently at version 0.3.0, the package aims for a more straightforward API compared to directly using `ssh2` for server functionality, focusing on common SFTP operations. However, the package has not been updated since September 2017, indicating it is no longer actively maintained. Its release cadence has ceased, and it should be noted that more advanced SFTP server functionalities might require direct interaction with `ssh2` or a more modern alternative. Its key differentiator was offering a higher-level abstraction specifically for SFTP server creation.","language":"javascript","status":"abandoned","last_verified":"Sun Apr 19","install":{"commands":["npm install node-sftp-server"],"cli":null},"imports":["const SFTPServer = require('node-sftp-server');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const SFTPServer = require('node-sftp-server');\nconst fs = require('fs');\nconst path = require('path');\n\n// IMPORTANT: Generate a private key first:\n// ssh-keygen -t rsa -b 2048 -N \"\" -f ssh_host_rsa_key\n// Place 'ssh_host_rsa_key' in the same directory as this script.\nconst PRIVATE_KEY_PATH = path.join(__dirname, 'ssh_host_rsa_key');\n\nif (!fs.existsSync(PRIVATE_KEY_PATH)) {\n  console.error(`Error: Private key file not found at ${PRIVATE_KEY_PATH}`);\n  console.error('Please generate one using: ssh-keygen -t rsa -b 2048 -N \"\" -f ssh_host_rsa_key');\n  process.exit(1);\n}\n\nconst myserver = new SFTPServer({\n    privateKeyFile: PRIVATE_KEY_PATH,\n    debug: true // Enable for detailed console logging\n});\n\nmyserver.listen(2222, () => {\n    console.log('SFTP Server listening on port 2222');\n    console.log('Connect with an SFTP client (e.g., sftp -P 2222 user@localhost)');\n});\n\nmyserver.on('connect', (context, clientInfo) => {\n    console.log(`Client connected: ${clientInfo.ip} (User: ${context.username}, Method: ${context.method})`);\n\n    // Example: Basic password authentication\n    if (context.method === 'password' && context.username === 'testuser' && context.password === 'testpass') {\n        context.accept((session) => {\n            console.log('Authentication successful for testuser.');\n\n            session.on('realpath', (path, callback) => {\n                // Resolve paths relative to a user's 'home' directory\n                const userHome = path.join(__dirname, 'sftp_users', context.username);\n                fs.mkdirSync(userHome, { recursive: true }); // Ensure user's directory exists\n                callback(path.join(userHome, path.replace(/^\\//, ''))); // Simple path resolution\n            });\n\n            session.on('readfile', (path, writableStream) => {\n                console.log(`Client requested to read file: ${path}`);\n                const filePath = path.join(__dirname, 'sftp_users', context.username, path.replace(/^\\//, ''));\n                if (fs.existsSync(filePath) && fs.lstatSync(filePath).isFile()) {\n                    fs.createReadStream(filePath).pipe(writableStream);\n                } else {\n                    writableStream.destroy(new Error('File not found or not a file'));\n                }\n            });\n\n            session.on('writefile', (path, readableStream) => {\n                console.log(`Client requested to write file: ${path}`);\n                const filePath = path.join(__dirname, 'sftp_users', context.username, path.replace(/^\\//, ''));\n                const dir = path.dirname(filePath);\n                fs.mkdirSync(dir, { recursive: true });\n                const writeStream = fs.createWriteStream(filePath);\n                readableStream.pipe(writeStream);\n                readableStream.on('end', () => console.log(`File written: ${filePath}`));\n                readableStream.on('error', (err) => console.error(`Write error: ${err.message}`));\n            });\n\n            // Other SFTP events like 'opendir', 'readdir', 'mkdir', 'rmdir', 'remove', 'rename', 'stat' would also be handled here\n            session.on('error', (err) => console.error(`Session error: ${err.message}`));\n        });\n    } else {\n        console.log('Authentication failed.');\n        context.reject(); // Reject all other methods/credentials\n    }\n});\n\nmyserver.on('end', () => {\n    console.log('Client disconnected.');\n});\n\nmyserver.on('error', (err) => {\n    console.error('SFTP Server Error:', err.message);\n});\n","lang":"javascript","description":"This quickstart code sets up a basic SFTP server listening on port 2222, demonstrating how to handle client connections, authenticate users (hardcoded 'testuser'/'testpass'), and implement basic `realpath`, `readfile`, and `writefile` operations. It requires a pre-generated private key for the server to start.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}