FTP Response Stream Parser
The `ftp-response-parser` package provides a lightweight, streaming parser specifically designed for handling FTP server response format strings. It efficiently processes incoming FTP protocol messages, including multi-line responses, and emits structured JavaScript objects. Each emitted object includes the numeric status `code`, the full `text` of the response, and boolean flags `isMark` and `isError`. This library is currently at version 1.0.1, with its last update occurring over a decade ago. It functions as a classic Node.js `Stream` and is primarily differentiated by its low-overhead, event-driven approach to parsing continuous streams of FTP data, making it suitable for integration into custom FTP client implementations that require incremental response processing.
Common errors
-
TypeError: ftp-response-parser is not a constructor
cause Attempting to use an ESM `import` statement for a CommonJS-only module, which does not correctly resolve the constructor.fixChange your import statement to `const ResponseParser = require('ftp-response-parser');`. -
Error: read after end
cause Invoking `parser.write()` on the stream after `parser.end()` has already been called, which signifies no more data will be written.fixEnsure all data is written to the parser using `parser.write()` before `parser.end()` is invoked. `parser.end()` should be the last write operation. -
Incomplete parsing of final FTP response lines (no specific error message)
cause The readable stream's `read()` method is not being called sufficiently often in response to the `'readable'` event, or `parser.end()` was not called after all input data was provided.fixEnsure your `on('readable')` listener contains a `while` loop that continuously calls `parser.read()` until it returns `null`. Additionally, confirm `parser.end()` is called once all expected input has been written.
Warnings
- breaking This package is CommonJS-only and does not provide an ESM export. Direct `import` statements (e.g., `import { ResponseParser } from 'ftp-response-parser';`) will result in a runtime error.
- gotcha The `ftp-response-parser` package is abandoned, with its last update occurring over a decade ago (May 2013). It may contain unaddressed bugs, security vulnerabilities, or compatibility issues with modern Node.js versions (e.g., Node.js v16+ and later).
- gotcha As a Node.js `Stream`, if the input stream is not explicitly ended (via `parser.end()`) after all data has been written, the parser might hold onto buffered data indefinitely, potentially leading to memory leaks or incomplete parsing of the final response segments.
Install
-
npm install ftp-response-parser -
yarn add ftp-response-parser -
pnpm add ftp-response-parser
Imports
- ResponseParser
import { ResponseParser } from 'ftp-response-parser';const ResponseParser = require('ftp-response-parser');
Quickstart
const ResponseParser = require('ftp-response-parser');
const myParser = new ResponseParser();
myParser.on('readable', function() {
let line;
while ((line = myParser.read()) !== null) {
console.log(`Code: ${line.code}, Text: ${line.text}`);
}
});
myParser.on('error', (err) => {
console.error('Parser encountered an error:', err);
});
myParser.on('end', () => {
console.log('End of FTP response stream.');
});
// Example FTP multi-line response string
const ftpResponse = `211-Features supported:\n EPRT\n EPSV\n MDTM\n MLST type*;perm*;size*;modify*;unique*;unix.mode;unix.uid;unix.gid;\n REST STREAM\n SIZE\n TVFS\n UTF8\n211 End FEAT.\n215 UNIX Type: L8\n331 Username ok, send password.\n230 Login successful.\n200 Type set to: Binary.\n250 "/test" is the current directory.\n`;
myParser.write(ftpResponse);
myParser.end(); // Signal that no more data will be written
console.log('Parser initialized and example data sent.');