basic-ftp-ct
Version:
FTP client for Node.js, supports explicit FTPS over TLS, IPv6, Async/Await, and Typescript.
50 lines (49 loc) • 1.9 kB
JavaScript
;
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const dosParser = __importStar(require("./parseListDOS"));
const unixParser = __importStar(require("./parseListUnix"));
const mlsdParser = __importStar(require("./parseListMLSD"));
/**
* Available directory listing parsers. These are candidates that will be tested
* in the order presented. The first candidate will be used to parse the whole list.
*/
const availableParsers = [
dosParser,
unixParser,
mlsdParser // Keep MLSD last, may accept filename only
];
function firstCompatibleParser(line, parsers) {
return parsers.find(parser => parser.testLine(line) === true);
}
function stringIsNotBlank(str) {
return str.trim() !== "";
}
const REGEX_NEWLINE = /\r?\n/;
/**
* Parse raw directory listing.
*/
function parseList(rawList) {
const lines = rawList
.split(REGEX_NEWLINE)
.filter(stringIsNotBlank);
if (lines.length === 0) {
return [];
}
const testLine = lines[lines.length - 1];
const parser = firstCompatibleParser(testLine, availableParsers);
if (!parser) {
throw new Error("This library only supports MLSD, Unix- or DOS-style directory listing. Your FTP server seems to be using another format. You can see the transmitted listing when setting `client.ftp.verbose = true`. You can then provide a custom parser to `client.parseList`, see the documentation for details.");
}
const files = lines
.map(parser.parseLine)
.filter((info) => info !== undefined);
return parser.transformList(files);
}
exports.parseList = parseList;