Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
33 changes: 33 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Publish to npm

on:
push:
tags:
- 'v*'

jobs:
publish:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
registry-url: 'https://registry.npmjs.org/'

- name: Install dependencies
run: npm ci

- name: Run build
run: npm run build

- name: Publish to npm
run: npm publish --provenance --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
29 changes: 29 additions & 0 deletions .github/workflows/unpublish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Unpublish Package

on:
workflow_dispatch:
inputs:
version:
description: 'Version to unpublish (or "all")'
required: true
type: string

jobs:
unpublish:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-node@v4
with:
node-version: '20'
registry-url: 'https://registry.npmjs.org/'

- run: |
if [ "${{ github.event.inputs.version }}" == "all" ]; then
echo "Attempting to unpublish entire package"
npm unpublish saborter --force || echo "Failed to unpublish"
else
echo "Unpublishing version ${{ github.event.inputs.version }}"
npm unpublish @saborter/server@${{ github.event.inputs.version }} || echo "Failed to unpublish"
fi
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
npm-debug.log*
dist
.DS_Store
.env
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<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" alt="License">
Expand All @@ -20,6 +22,8 @@
The documentation is divided into several sections:

- [Installation](#📦-installation)
- [Quick Start](#🚀-quick-start)
- [Typescript](#🔖-typescript)
- [License](#📋-license)

## 📦 Installation
Expand All @@ -30,6 +34,60 @@ npm install saborter @saborter/server
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, { endpointName: '/api/abort' });
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.
Loading
Loading