Skip to content

Commit 1d30732

Browse files
authored
Merge pull request #8 from TENSIILE/release/v1.0.0
[sync]: release/v1.0.0
2 parents dc0fdb2 + ae9205c commit 1d30732

File tree

21 files changed

+4365
-250
lines changed

21 files changed

+4365
-250
lines changed

.editorconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
insert_final_newline = true
8+
trim_trailing_whitespace = true

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto eol=lf

.github/workflows/publish.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Publish to npm
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*'
7+
8+
jobs:
9+
publish:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
id-token: write
13+
contents: read
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
18+
- name: Setup Node.js
19+
uses: actions/setup-node@v4
20+
with:
21+
node-version: '20'
22+
registry-url: 'https://registry.npmjs.org/'
23+
24+
- name: Install dependencies
25+
run: npm ci
26+
27+
- name: Run build
28+
run: npm run build
29+
30+
- name: Publish to npm
31+
run: npm publish --provenance --access public
32+
env:
33+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.github/workflows/unpublish.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Unpublish Package
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
version:
7+
description: 'Version to unpublish (or "all")'
8+
required: true
9+
type: string
10+
11+
jobs:
12+
unpublish:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/setup-node@v4
16+
with:
17+
node-version: '20'
18+
registry-url: 'https://registry.npmjs.org/'
19+
20+
- run: |
21+
if [ "${{ github.event.inputs.version }}" == "all" ]; then
22+
echo "Attempting to unpublish entire package"
23+
npm unpublish saborter --force || echo "Failed to unpublish"
24+
else
25+
echo "Unpublishing version ${{ github.event.inputs.version }}"
26+
npm unpublish @saborter/server@${{ github.event.inputs.version }} || echo "Failed to unpublish"
27+
fi
28+
env:
29+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules/
22
npm-debug.log*
3+
dist
34
.DS_Store
45
.env

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<img src="https://img.shields.io/npm/v/@saborter/server?color=red&label=npm%20package" /></a>
66
<a href="https://www.npmjs.com/package/@saborter/server" alt="Npm downloads">
77
<img src="https://img.shields.io/npm/dm/@saborter/server.svg" /></a>
8+
<a href="https://github.com/TENSIILE/saborter-server/actions/workflows/publish.yml" alt="Release">
9+
<img src="https://github.com/TENSIILE/saborter-server/actions/workflows/publish.yml/badge.svg" /></a>
810
<a href="https://github.com/TENSIILE/saborter-server/actions/workflows/ci.yml" alt="CI">
911
<img src="https://github.com/TENSIILE/saborter-server/actions/workflows/ci.yml/badge.svg" /></a>
1012
<a href="https://github.com/TENSIILE/saborter-server/blob/develop/LICENSE" alt="License">
@@ -20,6 +22,8 @@
2022
The documentation is divided into several sections:
2123

2224
- [Installation](#📦-installation)
25+
- [Quick Start](#🚀-quick-start)
26+
- [Typescript](#🔖-typescript)
2327
- [License](#📋-license)
2428

2529
## 📦 Installation
@@ -30,6 +34,60 @@ npm install saborter @saborter/server
3034
yarn add saborter @saborter/server
3135
```
3236

37+
## 🚀 Quick Start
38+
39+
### Basic Usage for Express
40+
41+
```typescript
42+
import express from 'express';
43+
import { isAbortError } from 'saborter/lib';
44+
import { initRequestInterrupts, getAbortSignal } from '@saborter/server/express';
45+
46+
const app = express();
47+
const port = process.env.PORT || 3000;
48+
49+
initRequestInterrupts(app, { endpointName: '/api/abort' });
50+
app.use(express.json());
51+
52+
app.get('/', async (req, res) => {
53+
try {
54+
const result = await longRunningOperation(getAbortSignal(req));
55+
res.json(result);
56+
} catch (error) {
57+
if (isAbortError(error)) {
58+
return res.status(499).send();
59+
}
60+
61+
res.status(500).send();
62+
}
63+
});
64+
65+
app.listen(port, () => {
66+
console.log(`Server running at http://localhost:${port}`);
67+
});
68+
```
69+
70+
## 🔖 Typescript
71+
72+
### Signal Typing in Express Request
73+
74+
If you don't want to use the `getAbortSignal` function, you can declare the `Request` interface:
75+
76+
```typescript
77+
declare namespace Express {
78+
interface Request {
79+
signal?: AbortSignal;
80+
}
81+
}
82+
```
83+
84+
After the declaration, you can directly retrieve the signal from the request:
85+
86+
```typescript
87+
const result = await longRunningOperation(req.signal);
88+
res.json(result);
89+
```
90+
3391
## 📋 License
3492

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

0 commit comments

Comments
 (0)