A robust TypeScript library providing seamless file system operations over SSH/SFTP with a clean, promise-based API. Perfect for secure file transfers, remote server management, and automation tasks.
npm install fs-tunnelimport { SSHFileSystem } from 'fs-tunnel'
const config = {
host: 'example.com',
port: 22,
username: 'user',
password: 'password'
}
async function main() {
const fs = new SSHFileSystem(config)
try {
await fs.connect()
// List files in directory
const files = await fs.readdir('/remote/path')
console.log('Directory contents:', files)
// Get file stats
const stats = await fs.stat('/remote/file.txt')
console.log('File stats:', stats)
// Download file
const readStream = fs.createReadStream('/remote/file.txt')
readStream.pipe(fs.createWriteStream('./local-file.txt'))
} finally {
fs.disconnect()
}
}
main()// Upload directory recursively
async function uploadDirectory(localPath, remotePath) {
const items = await fs.promises.readdir(localPath, { withFileTypes: true })
await fs.mkdir(remotePath)
for (const item of items) {
const localItemPath = path.join(localPath, item.name)
const remoteItemPath = path.posix.join(remotePath, item.name)
if (item.isDirectory()) {
await uploadDirectory(localItemPath, remoteItemPath)
} else {
const readStream = fs.createReadStream(localItemPath)
const writeStream = fs.createWriteStream(remoteItemPath)
readStream.pipe(writeStream)
}
}
}new SSHFileSystem(config: SSHConfiguration)config: Connection configuration objecthost: Server hostname (required)port: SSH port (required)username: Authentication usernamepassword: Authentication password
| Method | Description |
|---|---|
connect() |
Connects using configured credentials |
connectWithCredentials(username, password) |
Connects with explicit credentials |
readdir(path) |
Lists directory contents |
stat(path) |
Gets file/directory stats |
mkdir(path) |
Creates a directory |
rmdir(path) |
Removes a directory |
unlink(path) |
Deletes a file |
rename(oldPath, newPath) |
Renames/moves a file |
createReadStream(path) |
Creates readable file stream |
createWriteStream(path, options) |
Creates writable file stream |
disconnect() |
Closes the connection |
{
name: string
isDirectory: boolean
size: number
mtime: Date
mode: number
}{
isDirectory: boolean
size: number
mtime: Date
mode: number
}{
host: string
port: number
username?: string
password?: string
}Contributions are welcome! Please open an issue or submit a pull request on the GitHub repository.
fs-tunnel is licensed under the MIT license.
| @SheikhAminul |