npm package: @saborter/server
Description: Lightweight, tree‑shakeable utility to cancel server tasks on client abort
Author: Laptev Vladislav
Latest version: 1.0.1

![Logo](./assets/logo.png)

<p  align='center'>
<a href="https://www.npmjs.com/package/@saborter/server" alt="Npm package">
        <img src="https://img.shields.io/npm/v/@saborter/server?color=red&label=npm%20package" /></a>
<a href="https://www.npmjs.com/package/@saborter/server" alt="Npm downloads">
        <img src="https://img.shields.io/npm/dm/@saborter/server.svg" /></a>
<a href="https://github.com/TENSIILE/saborter-server/actions/workflows/publish.yml" alt="Release">
        <img src="https://github.com/TENSIILE/saborter-server/actions/workflows/publish.yml/badge.svg" /></a>
<a href="https://github.com/TENSIILE/saborter-server/actions/workflows/ci.yml" alt="CI">
        <img src="https://github.com/TENSIILE/saborter-server/actions/workflows/ci.yml/badge.svg" /></a>
<a href="https://github.com/TENSIILE/saborter-server/blob/develop/LICENSE.md" alt="License">
        <img src="https://img.shields.io/badge/license-MIT-blue" /></a>
<a href="https://github.com/TENSIILE/saborter-server" alt="Github">
        <img src="https://img.shields.io/badge/repository-github-color" /></a>
</p>

**Saborter Server** a lightweight [Node.js](https://nodejs.org/en) library that automatically cancels server-side operations when the client aborts a request. Built on the standard [AbortController API](https://developer.mozilla.org/en-US/docs/Web/API/AbortController), it seamlessly with [Express](https://expressjs.com/) 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](#📦-installation)
- [Quick Start](#🚀-quick-start)
- [Typescript](#🔖-typescript)
- [License](#📋-license)

## 📦 Installation

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

## 🚀 Quick Start

### Basic Usage for Express

```typescript
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:

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

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

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

## 📋 License

MIT License - see [LICENSE](./LICENSE) for details.