The Ultimate high-performance HTTP library for WEB – DEV Community


Tired of wrestling with the limitations of the native fetch API? Meet Stretto, a lightweight, high-performance TypeScript fetch wrapper that brings enterprise-grade resilience and simplicity to your HTTP requests. Built for modern web applications, Stretto combines the elegance of fetch with powerful features like retries, timeouts, and streaming—without the boilerplate.




Why Native Fetch Falls Short

The fetch API is great for quick HTTP requests, but in production, it leaves gaps:

// Basic fetch - looks simple, but what if it fails?
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1');
const data = await response.json();
Enter fullscreen mode

Exit fullscreen mode

Real-world challenges like network failures, 503 errors, rate limits, or timeouts can derail your app. Writing custom retry logic, backoff strategies, or stream handling? That’s a lot of code to maintain. Stretto solves these problems while keeping things developer-friendly.




Why Choose Stretto? ✨

Stretto enhances fetch with production-ready features, making your HTTP requests robust, efficient, and easy to use. Here’s what makes it stand out:



🔄 Smart Retries with Exponential Backoff

Handle transient failures like a pro with Stretto’s intelligent retry mechanism:

import stretto from 'stretto';

const response = await stretto('https://jsonplaceholder.typicode.com/todos/1', {
  retries: 5, // Try up to 5 times
  timeout: 10000 // 10-second timeout
});
Enter fullscreen mode

Exit fullscreen mode

Stretto uses exponential backoff with jitter to avoid overwhelming servers, ensuring your app stays resilient without flooding recovering endpoints.



Effortless Timeout Management

Timeouts and cancellations shouldn’t be a headache. Stretto’s createTimeoutController simplifies both:

const controller = new AbortController();
setTimeout(() => controller.abort(), 5000); // Cancel after 5s

const response = await stretto('https://jsonplaceholder.typicode.com/todos/1', {
  signal: controller.signal,
  timeout: 30000 // 30-second timeout
});
Enter fullscreen mode

Exit fullscreen mode

Optimized for performance, Stretto minimizes memory leaks and ensures clean cancellation.



🌊 Zero-Copy Streaming for Big Data

Need to process large responses or real-time streams? Stretto’s StrettoStreamableResponse delivers zero-copy streaming for maximum efficiency:

const response = await stretto('https://sse.dev/test', { stream: true });

for await (const chunk of response) {
  processChunk(chunk); // Handle data as it arrives
}
Enter fullscreen mode

Exit fullscreen mode

No unnecessary copying, just pure performance.



📡 Seamless Server-Sent Events (SSE)

Real-time apps love Server-Sent Events, but parsing them can be tricky. Stretto’s JSONStreamTransformer makes it a breeze:

import { JSONStreamTransformer } from 'stretto/transformers';

const response = await stretto('https://sse.dev/test', {
  stream: true,
  transformers: [new JSONStreamTransformer({
    maxBuffer: 8192,
    parseData: true,
    onParseError: 'skip'
  })]
});

for await (const event of response) {
  handleRealtimeEvent(event);
}
Enter fullscreen mode

Exit fullscreen mode

It’s secure, performant, and handles edge cases like buffer overflows with ease.




🚀 Performance & Security: Built for the Real World

Stretto isn’t just about features—it’s designed for speed and safety:

  • Memory Efficiency: Zero-copy streaming and buffer reuse keep memory usage low.
  • Minimal Allocations: Optimized hot paths reduce overhead.
  • Buffer Overflow Protection: Configurable limits prevent memory exhaustion attacks.
  • Data Security: Internal buffers are zeroed out to avoid leaks in memory dumps.



Get Started in Minutes! 🛠️

Ready to level up your HTTP requests? Installing Stretto is as easy as:

npm install stretto
Enter fullscreen mode

Exit fullscreen mode

Try it with a simple request:

import stretto from 'stretto';

// Quick and simple
const response = await stretto('https://jsonplaceholder.typicode.com/todos');
const data = await response.json();

// Or go robust
const robustResponse = await stretto('https://api.example.com/data', {
  retries: 3,
  timeout: 10000,
  stream: true
});
Enter fullscreen mode

Exit fullscreen mode




Why Developers ❤️ Stretto

  • Familiar API: Builds on fetch, so there’s no steep learning curve.
  • Production-Ready: Retries, timeouts, and streaming out of the box.
  • TypeScript-Powered: Full type safety for confident coding.
  • Lightweight: Minimal footprint, maximum impact.



Join the Stretto Community! 🌟

Stretto is more than a library—it’s a tool to make your apps more reliable and your life easier. Want to dive in?

  • 📦 Install it: npm install stretto
  • 📚 Explore the docs: Stretto GitHub
  • 💬 Share your feedback: Join our community discussions!

Try Stretto in your next project and experience HTTP requests done right. Your users—and your future self—will thank you! 🚀



Source link