@fatcherjs/middleware-aborter 1.8.0 → 3.0.0-alpha-4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +41 -36
- package/package.json +17 -15
- package/dist/aborter.d.ts +0 -52
- package/dist/aborter.esm.js +0 -55
- package/dist/aborter.js +0 -60
- package/dist/aborter.min.js +0 -1
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
# @
|
|
2
|
-
|
|
3
|
-
A middleware for aborting fatcher request.
|
|
1
|
+
# @fatcher/middleware-aborter
|
|
4
2
|
|
|
5
3
|
## Install
|
|
6
4
|
|
|
@@ -13,49 +11,56 @@ A middleware for aborting fatcher request.
|
|
|
13
11
|
### CDN
|
|
14
12
|
|
|
15
13
|
```html
|
|
16
|
-
<script src="https://cdn.jsdelivr.net/npm/@fatcherjs/middleware-aborter/dist/
|
|
14
|
+
<script src="https://cdn.jsdelivr.net/npm/@fatcherjs/middleware-aborter/dist/index.min.js"></script>
|
|
17
15
|
```
|
|
18
16
|
|
|
19
17
|
## Usage
|
|
20
18
|
|
|
21
19
|
```ts
|
|
20
|
+
import { fatcher } from 'fatcher';
|
|
22
21
|
import { aborter } from '@fatcherjs/middleware-aborter';
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
console.log('Request is Aborted.');
|
|
32
|
-
},
|
|
33
|
-
}),
|
|
34
|
-
],
|
|
35
|
-
})
|
|
36
|
-
.then(res => {
|
|
37
|
-
// Request success in 10s
|
|
38
|
-
console.log(res);
|
|
39
|
-
})
|
|
40
|
-
.catch(err => {
|
|
41
|
-
if (isAbortError(err)) {
|
|
42
|
-
//Run error when request aborted.
|
|
43
|
-
console.error(err);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// Other errors.
|
|
47
|
-
});
|
|
22
|
+
|
|
23
|
+
fatcher('https://foo.bar', {
|
|
24
|
+
middlewares: [
|
|
25
|
+
aborter({
|
|
26
|
+
/* Options*/
|
|
27
|
+
}),
|
|
28
|
+
],
|
|
29
|
+
});
|
|
48
30
|
```
|
|
49
31
|
|
|
50
32
|
## Options
|
|
51
33
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
34
|
+
### onAbort
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { fatcher } from 'fatcher';
|
|
38
|
+
import { aborter } from '@fatcherjs/middleware-aborter';
|
|
39
|
+
|
|
40
|
+
fatcher('https://foo.bar', {
|
|
41
|
+
middlewares: [
|
|
42
|
+
aborter({
|
|
43
|
+
onAbort: () => console.log('aborted'),
|
|
44
|
+
}),
|
|
45
|
+
],
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### timeout
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import { fatcher } from 'fatcher';
|
|
53
|
+
import { aborter } from '@fatcherjs/middleware-aborter';
|
|
54
|
+
|
|
55
|
+
fatcher('https://foo.bar', {
|
|
56
|
+
middlewares: [
|
|
57
|
+
aborter({
|
|
58
|
+
timeout: 10 * 1000, // abort in 10 seconds
|
|
59
|
+
}),
|
|
60
|
+
],
|
|
61
|
+
});
|
|
62
|
+
```
|
|
58
63
|
|
|
59
64
|
## License
|
|
60
65
|
|
|
61
|
-
[MIT](https://github.com/
|
|
66
|
+
[MIT](https://github.com/fanhaoyuan/fatcher/blob/master/LICENSE)
|
package/package.json
CHANGED
|
@@ -1,25 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fatcherjs/middleware-aborter",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"
|
|
3
|
+
"homepage": "https://github.com/fanhaoyuan/fatcher",
|
|
4
|
+
"repository": {
|
|
5
|
+
"type": "git",
|
|
6
|
+
"url": "git+https://github.com/fanhaoyuan/fatcher.git"
|
|
7
|
+
},
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"module": "dist/index.esm.js",
|
|
10
|
+
"browser": "dist/index.min.js",
|
|
11
|
+
"typings": "dist/index.d.ts",
|
|
9
12
|
"files": [
|
|
10
13
|
"dist"
|
|
11
14
|
],
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
"type": "git",
|
|
15
|
-
"url": "git+https://github.com/fatcherjs/fatcher.git"
|
|
15
|
+
"peerDependencies": {
|
|
16
|
+
"fatcher": "3.0.0-alpha-4"
|
|
16
17
|
},
|
|
17
|
-
"
|
|
18
|
-
"fatcher": "
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"fatcher": "3.0.0-alpha-4"
|
|
19
20
|
},
|
|
21
|
+
"version": "3.0.0-alpha-4",
|
|
20
22
|
"scripts": {
|
|
21
|
-
"
|
|
22
|
-
"build": "
|
|
23
|
-
"
|
|
23
|
+
"clean": "rimraf dist",
|
|
24
|
+
"build": "npm run clean && rollup -c rollup.config.ts",
|
|
25
|
+
"tsc": "tsc --noEmit"
|
|
24
26
|
}
|
|
25
27
|
}
|
package/dist/aborter.d.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { Context, Middleware } from 'fatcher';
|
|
2
|
-
|
|
3
|
-
declare type AbortReason = 'concurrency' | 'timeout' | 'manual';
|
|
4
|
-
declare type AbortEventHandler = (type: AbortReason) => void;
|
|
5
|
-
declare type RoadSign = {
|
|
6
|
-
abort: (type: AbortReason) => void;
|
|
7
|
-
timer: NodeJS.Timeout | null;
|
|
8
|
-
signal: AbortSignal;
|
|
9
|
-
};
|
|
10
|
-
declare type RoadMap = Record<string, RoadSign[]>;
|
|
11
|
-
interface AborterOptions {
|
|
12
|
-
/**
|
|
13
|
-
* Request timeout
|
|
14
|
-
*
|
|
15
|
-
* `ms`
|
|
16
|
-
*
|
|
17
|
-
* @default 0
|
|
18
|
-
*/
|
|
19
|
-
timeout?: number;
|
|
20
|
-
/**
|
|
21
|
-
* Callback with aborted request.
|
|
22
|
-
*
|
|
23
|
-
* @default null
|
|
24
|
-
*/
|
|
25
|
-
onAbort?: AbortEventHandler | null;
|
|
26
|
-
/**
|
|
27
|
-
* Request concurrency restrictions
|
|
28
|
-
*
|
|
29
|
-
* @default false
|
|
30
|
-
*/
|
|
31
|
-
concurrency?: boolean;
|
|
32
|
-
/**
|
|
33
|
-
* Concurrency key.
|
|
34
|
-
*/
|
|
35
|
-
groupBy?: (context: Readonly<Context>) => string;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* A middleware for aborting fatcher request.
|
|
40
|
-
* @param options
|
|
41
|
-
* @returns
|
|
42
|
-
*/
|
|
43
|
-
declare function aborter(options?: AborterOptions): Middleware;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Confirm an error whether is DOMException
|
|
47
|
-
* @param error
|
|
48
|
-
* @returns
|
|
49
|
-
*/
|
|
50
|
-
declare function isAbortError(error: unknown): error is DOMException;
|
|
51
|
-
|
|
52
|
-
export { AbortEventHandler, AbortReason, AborterOptions, RoadMap, RoadSign, aborter, isAbortError };
|
package/dist/aborter.esm.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
const roadMap = {};
|
|
2
|
-
function aborter(options = {}) {
|
|
3
|
-
const { timeout = 0, onAbort = null, concurrency, groupBy } = options;
|
|
4
|
-
let _timeout = timeout;
|
|
5
|
-
if (isNaN(timeout) || ~~timeout < 0) {
|
|
6
|
-
console.warn("[fatcher-middleware-aborter] Timeout is not a valid number.");
|
|
7
|
-
_timeout = 0;
|
|
8
|
-
}
|
|
9
|
-
return {
|
|
10
|
-
name: "fatcher-middleware-aborter",
|
|
11
|
-
async use(context, next) {
|
|
12
|
-
var _a, _b, _c;
|
|
13
|
-
const abortController = new AbortController();
|
|
14
|
-
const { abort, signal } = abortController;
|
|
15
|
-
const requestTask = next({ signal });
|
|
16
|
-
const group = (_a = groupBy == null ? void 0 : groupBy(context)) != null ? _a : `${context.url}_${context.method}_${new URLSearchParams(context.params).toString()}`;
|
|
17
|
-
if (((_b = roadMap[group]) == null ? void 0 : _b.length) && concurrency) {
|
|
18
|
-
roadMap[group].forEach((item) => {
|
|
19
|
-
item.abort("concurrency");
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
(_c = roadMap[group]) != null ? _c : roadMap[group] = [];
|
|
23
|
-
const trigger = (reason) => {
|
|
24
|
-
abort.call(abortController);
|
|
25
|
-
onAbort == null ? void 0 : onAbort(reason);
|
|
26
|
-
};
|
|
27
|
-
roadMap[group].push({
|
|
28
|
-
abort: trigger,
|
|
29
|
-
timer: _timeout ? setTimeout(() => trigger("timeout"), _timeout) : null,
|
|
30
|
-
signal
|
|
31
|
-
});
|
|
32
|
-
signal.addEventListener("abort", () => {
|
|
33
|
-
roadMap[group] = roadMap[group].filter((item) => {
|
|
34
|
-
if (item.signal === signal) {
|
|
35
|
-
if (item.timer) {
|
|
36
|
-
clearTimeout(item.timer);
|
|
37
|
-
}
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
return true;
|
|
41
|
-
});
|
|
42
|
-
if (!roadMap[group].length) {
|
|
43
|
-
delete roadMap[group];
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
return requestTask;
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function isAbortError(error) {
|
|
52
|
-
return error instanceof DOMException && error.name === "AbortError";
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export { aborter, isAbortError };
|
package/dist/aborter.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
const roadMap = {};
|
|
6
|
-
function aborter(options = {}) {
|
|
7
|
-
const { timeout = 0, onAbort = null, concurrency, groupBy } = options;
|
|
8
|
-
let _timeout = timeout;
|
|
9
|
-
if (isNaN(timeout) || ~~timeout < 0) {
|
|
10
|
-
console.warn("[fatcher-middleware-aborter] Timeout is not a valid number.");
|
|
11
|
-
_timeout = 0;
|
|
12
|
-
}
|
|
13
|
-
return {
|
|
14
|
-
name: "fatcher-middleware-aborter",
|
|
15
|
-
async use(context, next) {
|
|
16
|
-
var _a, _b, _c;
|
|
17
|
-
const abortController = new AbortController();
|
|
18
|
-
const { abort, signal } = abortController;
|
|
19
|
-
const requestTask = next({ signal });
|
|
20
|
-
const group = (_a = groupBy == null ? void 0 : groupBy(context)) != null ? _a : `${context.url}_${context.method}_${new URLSearchParams(context.params).toString()}`;
|
|
21
|
-
if (((_b = roadMap[group]) == null ? void 0 : _b.length) && concurrency) {
|
|
22
|
-
roadMap[group].forEach((item) => {
|
|
23
|
-
item.abort("concurrency");
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
(_c = roadMap[group]) != null ? _c : roadMap[group] = [];
|
|
27
|
-
const trigger = (reason) => {
|
|
28
|
-
abort.call(abortController);
|
|
29
|
-
onAbort == null ? void 0 : onAbort(reason);
|
|
30
|
-
};
|
|
31
|
-
roadMap[group].push({
|
|
32
|
-
abort: trigger,
|
|
33
|
-
timer: _timeout ? setTimeout(() => trigger("timeout"), _timeout) : null,
|
|
34
|
-
signal
|
|
35
|
-
});
|
|
36
|
-
signal.addEventListener("abort", () => {
|
|
37
|
-
roadMap[group] = roadMap[group].filter((item) => {
|
|
38
|
-
if (item.signal === signal) {
|
|
39
|
-
if (item.timer) {
|
|
40
|
-
clearTimeout(item.timer);
|
|
41
|
-
}
|
|
42
|
-
return false;
|
|
43
|
-
}
|
|
44
|
-
return true;
|
|
45
|
-
});
|
|
46
|
-
if (!roadMap[group].length) {
|
|
47
|
-
delete roadMap[group];
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
return requestTask;
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function isAbortError(error) {
|
|
56
|
-
return error instanceof DOMException && error.name === "AbortError";
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
exports.aborter = aborter;
|
|
60
|
-
exports.isAbortError = isAbortError;
|
package/dist/aborter.min.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
(function(t,e){typeof exports=="object"&&typeof module!="undefined"?e(exports):typeof define=="function"&&define.amd?define(["exports"],e):(t=typeof globalThis!="undefined"?globalThis:t||self,e(t.FatcherMiddlewareAborter={}))})(this,function(t){"use strict";const e={};function p(o={}){const{timeout:u=0,onAbort:s=null,concurrency:_,groupBy:c}=o;let l=u;return(isNaN(u)||~~u<0)&&(console.warn("[fatcher-middleware-aborter] Timeout is not a valid number."),l=0),{name:"fatcher-middleware-aborter",async use(i,v){var d,f,b;const m=new AbortController,{abort:y,signal:a}=m,w=v({signal:a}),r=(d=c==null?void 0:c(i))!=null?d:`${i.url}_${i.method}_${new URLSearchParams(i.params).toString()}`;((f=e[r])==null?void 0:f.length)&&_&&e[r].forEach(n=>{n.abort("concurrency")}),(b=e[r])!=null||(e[r]=[]);const h=n=>{y.call(m),s==null||s(n)};return e[r].push({abort:h,timer:l?setTimeout(()=>h("timeout"),l):null,signal:a}),a.addEventListener("abort",()=>{e[r]=e[r].filter(n=>n.signal===a?(n.timer&&clearTimeout(n.timer),!1):!0),e[r].length||delete e[r]}),w}}}function g(o){return o instanceof DOMException&&o.name==="AbortError"}t.aborter=p,t.isAbortError=g,Object.defineProperty(t,"__esModule",{value:!0})});
|