{"id":50720222,"url":"https://github.com/4ssh1/url-shortener-api","last_synced_at":"2026-06-09T23:01:59.802Z","repository":{"id":360038174,"uuid":"1236596776","full_name":"4ssh1/url-shortener-api","owner":"4ssh1","description":"A high-performance, secure URL shortener and analytics API built with TypeScript, Express, and MongoDB. It features sliding-window token management, non-blocking background click logging optimized for minimal computing overhead and fast link redirects.","archived":false,"fork":false,"pushed_at":"2026-05-25T11:54:10.000Z","size":389,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-05-25T13:33:21.304Z","etag":null,"topics":["expressjs","nodejs","security-hardening","sliding-window","typescript","url-shortener"],"latest_commit_sha":null,"homepage":"https://documenter.getpostman.com/view/40852797/2sBXqQHJno","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/4ssh1.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-05-12T11:51:50.000Z","updated_at":"2026-05-25T11:54:14.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/4ssh1/url-shortener-api","commit_stats":null,"previous_names":["4ssh1/url-shortener","4ssh1/url-shortener-api"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/4ssh1/url-shortener-api","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4ssh1%2Furl-shortener-api","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4ssh1%2Furl-shortener-api/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4ssh1%2Furl-shortener-api/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4ssh1%2Furl-shortener-api/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/4ssh1","download_url":"https://codeload.github.com/4ssh1/url-shortener-api/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4ssh1%2Furl-shortener-api/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34129072,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-09T02:00:06.510Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["expressjs","nodejs","security-hardening","sliding-window","typescript","url-shortener"],"created_at":"2026-06-09T23:01:58.208Z","updated_at":"2026-06-09T23:01:59.786Z","avatar_url":"https://github.com/4ssh1.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# URL Shortener \u0026 Analytics API\n\nA production-grade, high-performance, and secure URL shortening microservice built with Node.js, Express, TypeScript, and MongoDB. This platform is designed to handle high-traffic link redirection with sub-millisecond tracking analytics while keeping the core database healthy and protected against common vulnerabilities. \n\n**Frontend repo: https://github.com/4ssh1/url-shortener**\n\n## 2. High-Level Architecture\n\nThe system is built using a clean, layered architectural pattern that separates how data travels, how business logic is executed, and how data is stored.\n```\n[ Client Request ]\n│\n▼\n┌────────────────────────────────────────────────────────┐\n│ 1. Security \u0026 Routing Layer                           │\n│ - Global Protection (Helmet, CORS, Rate Limiters)     │\n│ - Session Guards (Access \u0026 Refresh Middleware)        │\n└────────────────────────────────────────────────────────┘\n│\n▼\n┌────────────────────────────────────────────────────────┐\n│ 2. Controller Layer (Traffic Control)                 │\n│ - Extracts data \u0026 handles HTTP responses              │\n│ - Enforces input safety shapes via Zod schemas        │\n└────────────────────────────────────────────────────────┘\n│\n▼\n┌────────────────────────────────────────────────────────┐\n│ 3. Service Layer (Core Business Logic)                │\n│ - Orchestrates data operations                        │\n│ - Triggers non-blocking background workers            │\n└────────────────────────────────────────────────────────┘\n│\n▼\n┌────────────────────────────────────────────────────────┐\n│ 4. Database Layer (MongoDB Atlas)                     │\n│ - Structured Document Storage                         │\n│ - Specialized performance indexes                     │\n└────────────────────────────────────────────────────────┘\n\n```\nBy ensuring that each layer has exactly one job, the codebase avoids redundancy (keeping it **DRY** — Don't Repeat Yourself). For instance, validation happens entirely before the core logic runs, meaning the service layer can trust the data implicitly without repeating safety checks.\n\n## 🛠️ Technologies Used\n\n- **Language**: TypeScript\n- **Runtime Environment**: Node.js\n- **Web Framework**: Express.js\n- **Database**: MongoDB (via Mongoose ODM)\n- **Data Validation**: Zod\n- **Security \u0026 Middleware**: Helmet, CORS, Cookie-Parser, Compression\n- **Logging \u0026 Telemetry**: Pino (with Pino-Abstract / Logtail transports)\n- **Cryptography**: Node.js Native Crypto Module, JSON Web Tokens (JWT)\n\n## End-to-End Breakdown\n\n### 1. The Secure Session Lifespan\n\nTo keep users securely logged in without ruining application performance, the platform splits authentication into a **dual-token system**:\n\n- **Short-Lived Access Key**: Used for rapid access to private features. Expires in **15 minutes**.\n- **Long-Lived Refresh Key**: Locked safely inside the browser's memory using a secure `httpOnly` cookie that JavaScript cannot read.\n\nWhen a user interacts with their dashboard, the server runs a **Sliding Window** check. Instead of writing to the database every single time, the system reads from a lightweight memory cache. It only performs a hard database rewrite to extend the session if the refresh key is nearing its actual expiration date.\n\n### 2. High-Speed Redirection \u0026 Fire-and-Forget Analytics\n\nThe primary job of a URL shortener is to redirect instantly. To eliminate any delay:\n\n- A visitor clicks the short link.\n- The server instantly increments the counter and issues the redirect.\n- In the background (completely hidden from the user), a separate micro-task logs deep metadata: device type (User-Agent), origin source (Referer), and connection timestamp.\n\n### 3. Bulletproof Password Recovery\n\nInstead of using complex JWTs, the system generates an **unguessable, cryptographically secure random identifier**. This single-use token is stored with a strict **10-minute expiration** on the account document. Once used, it is completely removed from the system, preventing token reuse or overlapping reset attempts.\n\n## Critical Engineering Decisions\n\n### Why We Restrict Token Swaps (Sliding Window over Regular Rotation)\n\nWhile rotating refresh tokens on every request is highly secure, it creates heavy write amplification on the database. The **Sliding Window** approach preserves **90%** of database computing power while maintaining strict security.\n\n### State-Backed Tokens vs. Stateless JWTs for Resets\n\nUsing a simple cryptographic string instead of a JWT keeps the system lightweight. Since the server must query the database during password updates anyway, embedding expiration and status checks in the same operation removes unnecessary complexity.\n\n### Intentional State Management for Safe Shutdowns\n\nTo handle cloud scaling and deployments safely, the application uses **Process Termination Traps** (`SIGINT`/`SIGTERM`). On shutdown, it:\n- Pauses operations\n- Flushes pending analytics streams to disk\n- Drains connection pools\n- Logs out cleanly\n\nThis prevents data loss or database corruption during server restarts.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F4ssh1%2Furl-shortener-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F4ssh1%2Furl-shortener-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F4ssh1%2Furl-shortener-api/lists"}