How rbren/rss-parser works
Project Type and Purpose
• This is an open-source JavaScript library (npm package) for parsing RSS/Atom XML feeds into JavaScript objects. It targets Node.js (via parseURL/parseString APIs) and browsers (pre-built bundles in dist/).
File-Structure Analysis
• Top-level files:
• package.json / index.js / index.d.ts – entry point and TypeScript declarations
• lib/ – core parsing logic (fields extraction, parser orchestration, utility functions)
• dist/ – pre-built browser bundles (rss-parser.js + source map)
• scripts/ – build scripts (e.g., build.sh)
• webpack.config.js – bundler configuration for browser output
• test/ – test harness: sample feed inputs, expected JSON outputs, test runners
• .github/workflows, .travis.yml – CI configuration
• Patterns:
• Modular core in lib/
• Build pipeline produces dist/
• Test pipeline reads from test/input/, runs parser, compares with test/output/
Key README Insights
• Core APIs: parseURL, parseString (callbacks & Promises)
• Configurable via constructor options: customFields, xml2js options, HTTP headers, timeouts, redirects
• Supports Node.js HTTP(S) and browser via bundled script + polyfill
• Tests are “golden-file” comparisons between parsed JSON and committed outputs
Main Components
a. CoreParser Module (lib/parser.js)
• Accepts XML string or URL + options
• Coordinates fetching (if URL) and parsing
b. HTTP Client Layer
• Node.js http/https modules with customizable headers, timeouts, redirect handling
• Browser variant via XHR/fetch (in bundled dist/)
c. XML Parser Layer
• [xml2js](https://github.com/Leonidas-from-XIV/node-xml2js) instance with user-provided options
d. Post-Processing Layer
• lib/fields.js – maps and cleans up XML fields (dc: prefix removal, date normalization)
• lib/utils.js – snippet extraction, HTML unescape, category flattening, custom-fields injection
e. Output Model
• Final JavaScript object: feed (with items array) returned via Promise or callback
f. Build Pipeline
• scripts/build.sh + webpack.config.js → produce UMD/ES Bundle in dist/
g. Test Harness
• test/parser.js runs parsing for all files under test/input
• Compares with test/output → reports failures
Interactions & Data Flow
1. Consumer code calls new Parser(options).parseURL(url)
2. → HTTP Client fetches XML
3. → Pass XML string to xml2js Parser
4. → xml2js emits raw JS object of XML
5. → CoreParser applies post-processing (fields.js, utils.js)
6. → Returns normalized Feed object
7. → In browser, fetch and xml2js run inside bundled rss-parser.js
Architectural Patterns & Principles
• Modular single-responsibility components (fetcher, XML parsing, mapping, output)
• Strategy/Options pattern for xml2js, HTTP, custom fields
• Promise + Callback adapters for API flexibility
• Build pipeline separates source (lib/, index.js) from distributable (dist/)