Skip to content

Commit 74294ce

Browse files
authored
Merge pull request #5 from TENSIILE/feature/demo
feat: improves the API for working with saborter.server + adds a demo server for testing
2 parents 21b3d99 + fe39d95 commit 74294ce

15 files changed

Lines changed: 147 additions & 118 deletions

.eslintrc

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,52 @@
11
{
2-
"root": true,
3-
"env": { "browser": true, "es2020": true, "jest": true },
4-
"extends": ["eslint:recommended", "airbnb", "plugin:import/typescript", "plugin:prettier/recommended"],
5-
"parser": "@typescript-eslint/parser",
6-
"ignorePatterns": [
7-
"**/dist/",
8-
"**/node_modules/",
9-
"**/*.json",
10-
"**/*.html",
11-
"/**/coverage/",
12-
"**/integrations/**/",
13-
"./vite.config.js"
14-
],
15-
"plugins": ["@typescript-eslint", "prettier"],
16-
"rules": {
17-
"no-continue": "off",
18-
"quotes": "off",
19-
"import/prefer-default-export": ["off"],
20-
"class-methods-use-this": ["off"],
21-
"func-names": ["off"],
22-
"no-plusplus": ["off"],
23-
"prefer-spread": ["off"],
24-
"consistent-return": ["off"],
25-
"newline-before-return": "warn",
26-
"default-case": ["off"],
27-
"comma-dangle": ["off"],
28-
"import/extensions": ["off"],
29-
"max-len": ["off"],
30-
"no-console": ["warn", { "allow": ["warn", "error"] }],
31-
"@typescript-eslint/lines-between-class-members": ["off", {}],
32-
"import/no-extraneous-dependencies": ["off"],
33-
"@typescript-eslint/no-unused-vars": ["off", {}],
34-
"no-unused-vars": ["off"],
35-
"prettier/prettier": [
36-
"error",
37-
{
38-
"arrowParens": "always",
39-
"endOfLine": "auto"
40-
}
41-
]
42-
},
43-
"overrides": [
2+
"root": true,
3+
"env": { "browser": true, "es2020": true, "jest": true },
4+
"extends": ["eslint:recommended", "airbnb", "plugin:import/typescript", "plugin:prettier/recommended"],
5+
"parser": "@typescript-eslint/parser",
6+
"ignorePatterns": [
7+
"**/dist/",
8+
"**/node_modules/",
9+
"**/*.json",
10+
"**/*.html",
11+
"/**/coverage/",
12+
"**/integrations/**/",
13+
"./vite.config.js"
14+
],
15+
"plugins": ["@typescript-eslint", "prettier"],
16+
"rules": {
17+
"no-continue": "off",
18+
"quotes": "off",
19+
"import/prefer-default-export": ["off"],
20+
"class-methods-use-this": ["off"],
21+
"func-names": ["off"],
22+
"no-plusplus": ["off"],
23+
"prefer-spread": ["off"],
24+
"consistent-return": ["off"],
25+
"newline-before-return": "warn",
26+
"default-case": ["off"],
27+
"comma-dangle": ["off"],
28+
"import/extensions": ["off"],
29+
"max-len": ["off"],
30+
"no-console": ["warn", { "allow": ["warn", "error"] }],
31+
"@typescript-eslint/lines-between-class-members": ["off", {}],
32+
"import/no-extraneous-dependencies": ["off"],
33+
"@typescript-eslint/no-unused-vars": ["off", {}],
34+
"no-unused-vars": ["off"],
35+
"import/no-unresolved": "off",
36+
"prettier/prettier": [
37+
"error",
4438
{
45-
"files": ["*.ts", "*.tsx"],
46-
"rules": {
47-
"no-undef": "off"
48-
}
39+
"arrowParens": "always",
40+
"endOfLine": "auto"
4941
}
5042
]
51-
}
43+
},
44+
"overrides": [
45+
{
46+
"files": ["*.ts", "*.tsx"],
47+
"rules": {
48+
"no-undef": "off"
49+
}
50+
}
51+
]
52+
}

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ name: CI
33
on:
44
pull_request:
55
branches:
6-
- main
6+
- master
77
- develop
88
- 'release/v*.*.*'
99
push:
1010
branches:
11-
- main
11+
- master
1212
- develop
1313
- 'release/v*.*.*'
1414

File renamed without changes.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<img src="https://img.shields.io/badge/repository-github-color" /></a>
1414
</p>
1515

16-
**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.
16+
**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.
1717

1818
## 📚 Documentation
1919

assets/logo.png

52 Bytes
Loading

demo/index.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import express from 'express';
2+
import cors from 'cors';
3+
import { isAbortError } from 'saborter/lib';
4+
import { initRequestInterruptionService } from '../src';
5+
6+
const app = express();
7+
// eslint-disable-next-line dot-notation
8+
const port = process.env['PORT'] || 3000;
9+
10+
initRequestInterruptionService(app);
11+
12+
app.use(cors());
13+
app.use(express.json());
14+
15+
const longRunningOperation = async (signal?: AbortSignal | null) => {
16+
return new Promise((resolve, reject) => {
17+
// eslint-disable-next-line no-console
18+
console.log('Work...');
19+
const timeout = setTimeout(() => {
20+
// reject(new Error('!!!'));
21+
22+
// eslint-disable-next-line no-console
23+
console.log('Done!');
24+
resolve({ done: true });
25+
}, 10_000);
26+
27+
signal?.addEventListener(
28+
'abort',
29+
() => {
30+
clearTimeout(timeout);
31+
const error = new Error('Operation cancelled');
32+
error.name = 'AbortError';
33+
34+
reject(error);
35+
},
36+
{ once: true }
37+
);
38+
});
39+
};
40+
41+
app.get('/', async (req, res) => {
42+
try {
43+
const result = await longRunningOperation(req.signal);
44+
45+
res.json(result);
46+
} catch (error) {
47+
if (isAbortError(error)) {
48+
return res.status(499);
49+
}
50+
51+
res.status(500).send();
52+
}
53+
});
54+
55+
app.listen(port, () => {
56+
// eslint-disable-next-line no-console
57+
console.log(`Server running at http://localhost:${port}`);
58+
});

package-lock.json

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"start": "node dist/index.js",
77
"build": "tsc",
8-
"dev": "nodemon src/index.ts",
8+
"dev": "nodemon demo/index.ts",
99
"typecheck": "tsc --pretty --noEmit --skipLibCheck",
1010
"verify:prettier": "npx prettier . --check",
1111
"verify:eslint": "npx eslint \"./**/*.{js,jsx,ts,tsx}\" --max-warnings=0",
@@ -21,7 +21,8 @@
2121
"license": "ISC",
2222
"description": "",
2323
"dependencies": {
24-
"express": "^5.2.1"
24+
"express": "^5.2.1",
25+
"saborter": "^2.1.0"
2526
},
2627
"devDependencies": {
2728
"@types/cors": "^2.8.19",

src/global.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare namespace NodeJS {
2+
interface ProcessEnv {
3+
PORT?: string;
4+
}
5+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './service';

0 commit comments

Comments
 (0)