universal-file-client
Version:
Universal file transfer client with unified interface for FTP, SFTP, and HTTP protocols
176 lines • 6.66 kB
JavaScript
;
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.HttpAdapter = void 0;
const axios_1 = __importDefault(require("axios"));
const path = __importStar(require("path"));
class HttpAdapter {
constructor() {
this.config = null;
this.isConnected = false;
}
async connect(config) {
this.config = config;
this.isConnected = true;
}
async disconnect() {
this.config = null;
this.isConnected = false;
}
async list(_path = '.') {
throw new Error('HTTP adapter does not support directory listing');
}
async download(remotePath) {
if (!this.isConnected || !this.config) {
throw new Error('Not connected to HTTP server');
}
try {
const fileUrl = remotePath.startsWith('http') ? remotePath : `${this.config.host}${remotePath}`;
if (!this.isValidUrl(fileUrl)) {
throw new Error(`Invalid URL: ${fileUrl}`);
}
const axiosConfig = {
responseType: 'arraybuffer',
timeout: this.config.timeout || 30000,
};
if (this.config.username && this.config.password) {
axiosConfig.auth = {
username: this.config.username,
password: this.config.password,
};
}
const response = await axios_1.default.get(fileUrl, axiosConfig);
if (response.status !== 200) {
throw new Error(`HTTP request failed with status: ${response.status}`);
}
return Buffer.from(response.data);
}
catch (error) {
throw new Error(`Failed to download file from HTTP: ${error.message}`);
}
}
async upload(_localPath, _remotePath) {
throw new Error('HTTP adapter does not support file upload');
}
async stat(filePath) {
if (!this.isConnected || !this.config) {
throw new Error('Not connected to HTTP server');
}
try {
const fileUrl = filePath.startsWith('http') ? filePath : `${this.config.host}${filePath}`;
if (!this.isValidUrl(fileUrl)) {
return null;
}
const axiosConfig = {
method: 'HEAD',
timeout: this.config.timeout || 30000,
};
if (this.config.username && this.config.password) {
axiosConfig.auth = {
username: this.config.username,
password: this.config.password,
};
}
const response = await (0, axios_1.default)(fileUrl, axiosConfig);
if (response.status !== 200) {
return null;
}
const contentLength = response.headers['content-length'];
const lastModified = response.headers['last-modified'];
// const contentType = response.headers['content-type'] || '';
return {
name: path.basename(filePath),
size: contentLength ? parseInt(contentLength, 10) : 0,
date: lastModified ? new Date(lastModified) : new Date(),
type: 'file',
isDirectory: false,
modifyTime: lastModified ? new Date(lastModified).getTime() : Date.now(),
};
}
catch (error) {
return null;
}
}
async exists(filePath) {
const stat = await this.stat(filePath);
return stat !== null;
}
async lastModified(filePath) {
const stat = await this.stat(filePath);
return stat ? stat.date : null;
}
isValidUrl(url) {
try {
new URL(url);
return true;
}
catch {
return false;
}
}
async getFileInfo(filePath) {
if (!this.isConnected || !this.config) {
throw new Error('Not connected to HTTP server');
}
try {
const fileUrl = filePath.startsWith('http') ? filePath : `${this.config.host}${filePath}`;
const axiosConfig = {
method: 'HEAD',
timeout: this.config.timeout || 30000,
};
if (this.config.username && this.config.password) {
axiosConfig.auth = {
username: this.config.username,
password: this.config.password,
};
}
const response = await (0, axios_1.default)(fileUrl, axiosConfig);
const contentType = response.headers['content-type'] ?? '';
const isJson = contentType.includes('application/json') || contentType.includes('text/json');
const lastModified = response.headers['last-modified']
? new Date(response.headers['last-modified'])
: null;
return { isJson, lastModified };
}
catch (error) {
return { isJson: false, lastModified: null };
}
}
}
exports.HttpAdapter = HttpAdapter;
//# sourceMappingURL=http-adapter.js.map