parseControlResponse.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.positiveIntermediate = exports.positiveCompletion = exports.isMultiline = exports.isSingleLine = exports.parseControlResponse = void 0;
  4. const LF = "\n";
  5. /**
  6. * Parse an FTP control response as a collection of messages. A message is a complete
  7. * single- or multiline response. A response can also contain multiple multiline responses
  8. * that will each be represented by a message. A response can also be incomplete
  9. * and be completed on the next incoming data chunk for which case this function also
  10. * describes a `rest`. This function converts all CRLF to LF.
  11. */
  12. function parseControlResponse(text) {
  13. const lines = text.split(/\r?\n/).filter(isNotBlank);
  14. const messages = [];
  15. let startAt = 0;
  16. let tokenRegex;
  17. for (let i = 0; i < lines.length; i++) {
  18. const line = lines[i];
  19. // No group has been opened.
  20. if (!tokenRegex) {
  21. if (isMultiline(line)) {
  22. // Open a group by setting an expected token.
  23. const token = line.substr(0, 3);
  24. tokenRegex = new RegExp(`^${token}(?:$| )`);
  25. startAt = i;
  26. }
  27. else if (isSingleLine(line)) {
  28. // Single lines can be grouped immediately.
  29. messages.push(line);
  30. }
  31. }
  32. // Group has been opened, expect closing token.
  33. else if (tokenRegex.test(line)) {
  34. tokenRegex = undefined;
  35. messages.push(lines.slice(startAt, i + 1).join(LF));
  36. }
  37. }
  38. // The last group might not have been closed, report it as a rest.
  39. const rest = tokenRegex ? lines.slice(startAt).join(LF) + LF : "";
  40. return { messages, rest };
  41. }
  42. exports.parseControlResponse = parseControlResponse;
  43. function isSingleLine(line) {
  44. return /^\d\d\d(?:$| )/.test(line);
  45. }
  46. exports.isSingleLine = isSingleLine;
  47. function isMultiline(line) {
  48. return /^\d\d\d-/.test(line);
  49. }
  50. exports.isMultiline = isMultiline;
  51. /**
  52. * Return true if an FTP return code describes a positive completion.
  53. */
  54. function positiveCompletion(code) {
  55. return code >= 200 && code < 300;
  56. }
  57. exports.positiveCompletion = positiveCompletion;
  58. /**
  59. * Return true if an FTP return code describes a positive intermediate response.
  60. */
  61. function positiveIntermediate(code) {
  62. return code >= 300 && code < 400;
  63. }
  64. exports.positiveIntermediate = positiveIntermediate;
  65. function isNotBlank(str) {
  66. return str.trim() !== "";
  67. }