How erlantzcalvo/torrent-client works
Project Type and Purpose
• This is a Node.js-based, command-line torrent client implemented from scratch.
• Its goal is educational: illustrate how a BitTorrent peer downloads files by handling .torrent parsing, tracker communication, peer handshakes, messaging, piece/block management, and file I/O.
File‐Structure Analysis
• Top-level entry: index.js – orchestrates the end-to-end torrent download.
• bencoding/ – implements the Bencode encoder/decoder for .torrent parsing.
• connection/ – handles tracker announces (announceRequester.js), peer connections (peer.js), and low-level BitTorrent messages (message.js).
• managers/ – download-manager.js drives the download workflow (piece/block scheduling, reassembly, verification).
• structures/ – priorityQueue.js and queue.js support scheduling and block-request ordering.
• torrent/ – pieces.js (piece-level bookkeeping) and torrentInfo.js (metadata, info-hash generation).
• logger/ – simple logging abstraction.
• utils.js and constants.js – share helper functions and protocol constants.
README Insights
• Describes six logical stages: parsing, info-hashing, file pre-setup, tracker announce, peer handshake/messaging, piece-by-piece downloading.
• Highlights core technologies: Node.js, TCP sockets, SHA-1, HTTP GET for tracker.
a. Main Components
• CLI/Orchestrator (index.js)
• Bencoder Module (decoder.js, encoder.js)
• Torrent Metadata (torrentInfo.js)
• Tracker Client (announceRequester.js)
• Peer Manager (peer.js + message.js)
• Download Manager (download-manager.js)
• Data Structures (priorityQueue, queue)
• File I/O Layer (fs APIs)
• Logger Utility
• External Services: Tracker (HTTP), Peers (TCP)
b. Relationships & Data Flow
1. CLI → Bencoder: parse .torrent → JSON
2. TorrentInfo → compute info-hash → pass to Tracker Client
3. Tracker Client ↔ Tracker HTTP endpoint: announce request/response → list of peers
4. Peer Manager ↔ each Peer (TCP): handshake → message exchange
5. Download Manager uses Data Structures to schedule block requests → Peer Manager sends Request → receives Piece → verifies SHA-1 → reassembles pieces
6. Download Manager → File I/O: write blocks to disk (resume support on restart)
7. Logger: cross-cutting, logs events in all modules
c. Architectural Patterns & Principles
• Modular layering: parsing, networking, scheduling, persistence
• Producer-consumer queues for block requests
• Single responsibility per module
• Event-driven asynchronous I/O
d. Key Technologies & Libraries
• Node.js built-in net (TCP) and http modules
• Crypto (SHA-1)
• Bencode algorithm implemented in bencoding/
• Native fs for file writes and resume logic