Skip to content

TENSIILE/saborter-server

Repository files navigation

Logo

Saborter Server a lightweight Node.js library that automatically cancels server-side operations when the client aborts a request. Built on the standard AbortController API, it seamlessly with Express framework and allows you to stop unnecessary database queries, file writes, or external API calls, saving server resources and improving scalability. Fully tree‑shakeable – only the code you actually use ends up in your bundle.

📚 Documentation

The documentation is divided into several sections:

📦 Installation

npm install saborter @saborter/server
# or
yarn add saborter @saborter/server

🚀 Quick Start

Basic Usage for Express

import express from 'express';
import { isAbortError } from 'saborter/lib';
import { initRequestInterrupts, getAbortSignal } from '@saborter/server/express';

const app = express();
const port = process.env.PORT || 3000;

initRequestInterrupts(app);
app.use(express.json());

app.get('/', async (req, res) => {
  try {
    const result = await longRunningOperation(getAbortSignal(req));
    res.json(result);
  } catch (error) {
    if (isAbortError(error)) {
      return res.status(499).send();
    }

    res.status(500).send();
  }
});

app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}`);
});

🔖 Typescript

Signal Typing in Express Request

If you don't want to use the getAbortSignal function, you can declare the Request interface:

declare namespace Express {
  interface Request {
    signal?: AbortSignal;
  }
}

After the declaration, you can directly retrieve the signal from the request:

const result = await longRunningOperation(req.signal);
res.json(result);

📋 License

MIT License - see LICENSE for details.

About

🚀 💻 Lightweight, tree‑shakeable utility to cancel server tasks on client abort

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Contributors