UNPKG

universal-file-client

Version:

Universal file transfer client with unified interface for FTP, SFTP, and HTTP protocols

153 lines 5.33 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SftpAdapter = void 0; const ssh2_sftp_client_1 = __importDefault(require("ssh2-sftp-client")); const path = __importStar(require("path")); const protocol_detector_1 = require("../utils/protocol-detector"); class SftpAdapter { constructor() { this.isConnected = false; this.client = new ssh2_sftp_client_1.default(); } async connect(config) { try { const normalizedHost = protocol_detector_1.ProtocolDetector.normalizeHost(config.host); await this.client.connect({ host: normalizedHost, username: config.username, password: config.password, port: config.port ?? 22, }); this.isConnected = true; } catch (error) { this.isConnected = false; throw new Error(`SFTP connection failed: ${error.message}`); } } async disconnect() { if (this.isConnected) { await this.client.end(); this.isConnected = false; } } async list(path = '.') { if (!this.isConnected) { throw new Error('Not connected to SFTP server'); } try { const files = await this.client.list(path); return files .filter((file) => file.type === '-') // Only files, not directories .map((file) => ({ name: file.name, size: file.size, date: new Date(file.modifyTime), type: file.type === 'd' ? 'directory' : 'file', isDirectory: file.type === 'd', modifyTime: file.modifyTime, })); } catch (error) { throw new Error(`Failed to list SFTP directory: ${error.message}`); } } async download(remotePath) { if (!this.isConnected) { throw new Error('Not connected to SFTP server'); } try { const data = await this.client.get(remotePath); return data; } catch (error) { throw new Error(`Failed to download file from SFTP: ${error.message}`); } } async upload(localPath, remotePath) { if (!this.isConnected) { throw new Error('Not connected to SFTP server'); } try { await this.client.put(localPath, remotePath); } catch (error) { throw new Error(`Failed to upload file to SFTP: ${error.message}`); } } async stat(filePath) { if (!this.isConnected) { throw new Error('Not connected to SFTP server'); } try { const stats = await this.client.stat(filePath); if (!stats) return null; return { name: path.basename(filePath), size: stats.size, date: new Date(stats.modifyTime || Date.now()), type: stats.isDirectory ? 'directory' : 'file', isDirectory: !!stats.isDirectory, modifyTime: stats.modifyTime || Date.now(), }; } catch (error) { return null; } } async exists(filePath) { if (!this.isConnected) { throw new Error('Not connected to SFTP server'); } try { const exists = await this.client.exists(filePath); return !!exists; } catch (error) { return false; } } async lastModified(filePath) { const stat = await this.stat(filePath); return stat ? stat.date : null; } } exports.SftpAdapter = SftpAdapter; //# sourceMappingURL=sftp-adapter.js.map