parseList.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.parseList = void 0;
  27. const dosParser = __importStar(require("./parseListDOS"));
  28. const unixParser = __importStar(require("./parseListUnix"));
  29. const mlsdParser = __importStar(require("./parseListMLSD"));
  30. /**
  31. * Available directory listing parsers. These are candidates that will be tested
  32. * in the order presented. The first candidate will be used to parse the whole list.
  33. */
  34. const availableParsers = [
  35. dosParser,
  36. unixParser,
  37. mlsdParser // Keep MLSD last, may accept filename only
  38. ];
  39. function firstCompatibleParser(line, parsers) {
  40. return parsers.find(parser => parser.testLine(line) === true);
  41. }
  42. function isNotBlank(str) {
  43. return str.trim() !== "";
  44. }
  45. function isNotMeta(str) {
  46. return !str.startsWith("total");
  47. }
  48. const REGEX_NEWLINE = /\r?\n/;
  49. /**
  50. * Parse raw directory listing.
  51. */
  52. function parseList(rawList) {
  53. const lines = rawList
  54. .split(REGEX_NEWLINE)
  55. .filter(isNotBlank)
  56. .filter(isNotMeta);
  57. if (lines.length === 0) {
  58. return [];
  59. }
  60. const testLine = lines[lines.length - 1];
  61. const parser = firstCompatibleParser(testLine, availableParsers);
  62. if (!parser) {
  63. 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.");
  64. }
  65. const files = lines
  66. .map(parser.parseLine)
  67. .filter((info) => info !== undefined);
  68. return parser.transformList(files);
  69. }
  70. exports.parseList = parseList;