@fatcherjs/middleware-aborter 1.0.0 → 1.4.0
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/README.md +2 -2
- package/dist/aborter.d.ts +52 -0
- package/dist/aborter.esm.js +54 -0
- package/dist/aborter.js +59 -0
- package/dist/aborter.min.js +1 -0
- package/package.json +13 -9
- package/dist/index.d.ts +0 -27
- package/dist/index.esm.js +0 -31
- package/dist/index.js +0 -35
- package/dist/index.min.js +0 -1
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ A middleware for aborting fatcher request.
|
|
|
13
13
|
### CDN
|
|
14
14
|
|
|
15
15
|
```html
|
|
16
|
-
<script src="https://cdn.jsdelivr.net/npm/@fatcherjs/middleware-aborter/dist/
|
|
16
|
+
<script src="https://cdn.jsdelivr.net/npm/@fatcherjs/middleware-aborter/dist/aborter.min.js"></script>
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
@@ -69,4 +69,4 @@ A callback when aborting this request.
|
|
|
69
69
|
|
|
70
70
|
## License
|
|
71
71
|
|
|
72
|
-
[MIT](
|
|
72
|
+
[MIT](https://github.com/fatcherjs/fatcher/blob/master/LICENSE)
|
|
@@ -0,0 +1,52 @@
|
|
|
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 };
|
|
@@ -0,0 +1,54 @@
|
|
|
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
|
+
roadMap[group].push({
|
|
24
|
+
abort: (reason) => {
|
|
25
|
+
abort.call(abortController);
|
|
26
|
+
onAbort == null ? void 0 : onAbort(reason);
|
|
27
|
+
},
|
|
28
|
+
timer: _timeout ? setTimeout(() => abort("timeout"), _timeout) : null,
|
|
29
|
+
signal
|
|
30
|
+
});
|
|
31
|
+
signal.addEventListener("abort", () => {
|
|
32
|
+
roadMap[group] = roadMap[group].filter((item) => {
|
|
33
|
+
if (item.signal === signal) {
|
|
34
|
+
if (item.timer) {
|
|
35
|
+
clearTimeout(item.timer);
|
|
36
|
+
}
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
return true;
|
|
40
|
+
});
|
|
41
|
+
if (!roadMap[group].length) {
|
|
42
|
+
delete roadMap[group];
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
return requestTask;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function isAbortError(error) {
|
|
51
|
+
return error instanceof DOMException && error.name === "AbortError";
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export { aborter, isAbortError };
|
package/dist/aborter.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
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
|
+
roadMap[group].push({
|
|
28
|
+
abort: (reason) => {
|
|
29
|
+
abort.call(abortController);
|
|
30
|
+
onAbort == null ? void 0 : onAbort(reason);
|
|
31
|
+
},
|
|
32
|
+
timer: _timeout ? setTimeout(() => abort("timeout"), _timeout) : null,
|
|
33
|
+
signal
|
|
34
|
+
});
|
|
35
|
+
signal.addEventListener("abort", () => {
|
|
36
|
+
roadMap[group] = roadMap[group].filter((item) => {
|
|
37
|
+
if (item.signal === signal) {
|
|
38
|
+
if (item.timer) {
|
|
39
|
+
clearTimeout(item.timer);
|
|
40
|
+
}
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
return true;
|
|
44
|
+
});
|
|
45
|
+
if (!roadMap[group].length) {
|
|
46
|
+
delete roadMap[group];
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
return requestTask;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function isAbortError(error) {
|
|
55
|
+
return error instanceof DOMException && error.name === "AbortError";
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
exports.aborter = aborter;
|
|
59
|
+
exports.isAbortError = isAbortError;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(t,e){typeof exports=="object"&&typeof module<"u"?e(exports):typeof define=="function"&&define.amd?define(["exports"],e):(t=typeof globalThis<"u"?globalThis:t||self,e(t.FatcherMiddlewareAborter={}))})(this,function(t){"use strict";const e={};function p(o={}){const{timeout:u=0,onAbort:s=null,concurrency:g,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:h,signal:a}=m,y=v({signal:a}),r=(d=c?.(i))!=null?d:`${i.url}_${i.method}_${new URLSearchParams(i.params).toString()}`;return((f=e[r])==null?void 0:f.length)&&g&&e[r].forEach(n=>{n.abort("concurrency")}),(b=e[r])!=null||(e[r]=[]),e[r].push({abort:n=>{h.call(m),s?.(n)},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]}),y}}}function _(o){return o instanceof DOMException&&o.name==="AbortError"}t.aborter=p,t.isAbortError=_,Object.defineProperty(t,"__esModule",{value:!0})});
|
package/package.json
CHANGED
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fatcherjs/middleware-aborter",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"main": "dist/
|
|
5
|
-
"module": "dist/
|
|
6
|
-
"browser": "dist/
|
|
7
|
-
"typings": "dist/
|
|
8
|
-
"repository": "https://github.com/fatcherjs/middlewares/tree/master/packages/aborter",
|
|
3
|
+
"version": "1.4.0",
|
|
4
|
+
"main": "dist/aborter.js",
|
|
5
|
+
"module": "dist/aborter.esm.js",
|
|
6
|
+
"browser": "dist/aborter.min.js",
|
|
7
|
+
"typings": "dist/aborter.d.ts",
|
|
9
8
|
"license": "MIT",
|
|
10
9
|
"files": [
|
|
11
10
|
"dist"
|
|
12
11
|
],
|
|
12
|
+
"homepage": "https://github.com/fatcherjs/fatcher/tree/master/packages/aborter",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/fatcherjs/fatcher.git"
|
|
16
|
+
},
|
|
13
17
|
"peerDependencies": {
|
|
14
18
|
"fatcher": "^1.0.0"
|
|
15
19
|
},
|
|
16
20
|
"scripts": {
|
|
17
21
|
"dev": "rimraf dist && rollup -c rollup.config.ts -w",
|
|
18
|
-
"build": "rimraf dist && rollup -c rollup.config.ts"
|
|
19
|
-
|
|
20
|
-
|
|
22
|
+
"build": "rimraf dist && rollup -c rollup.config.ts",
|
|
23
|
+
"deploy": "pnpm run build && pnpm publish --no-git-check"
|
|
24
|
+
}
|
|
21
25
|
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { Middleware } from 'fatcher';
|
|
2
|
-
|
|
3
|
-
interface AborterOptions {
|
|
4
|
-
/**
|
|
5
|
-
* Request timeout
|
|
6
|
-
*
|
|
7
|
-
* `ms`
|
|
8
|
-
*
|
|
9
|
-
* @default 0
|
|
10
|
-
*/
|
|
11
|
-
timeout?: number;
|
|
12
|
-
/**
|
|
13
|
-
* Callback with aborted request.
|
|
14
|
-
*
|
|
15
|
-
* @default null
|
|
16
|
-
*/
|
|
17
|
-
onAbort?: (() => void) | null;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* A middleware for aborting fatcher request.
|
|
22
|
-
* @param options
|
|
23
|
-
* @returns
|
|
24
|
-
*/
|
|
25
|
-
declare function aborter(options?: AborterOptions): Middleware;
|
|
26
|
-
|
|
27
|
-
export { AborterOptions, aborter };
|
package/dist/index.esm.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { warn } from 'fatcher';
|
|
2
|
-
|
|
3
|
-
function aborter(options = {}) {
|
|
4
|
-
const { timeout = 0, onAbort = null } = options;
|
|
5
|
-
let _timeout = timeout;
|
|
6
|
-
if (isNaN(timeout) || ~~timeout <= 0) {
|
|
7
|
-
warn("<Aborter> Timeout is not a valid number.");
|
|
8
|
-
_timeout = 0;
|
|
9
|
-
}
|
|
10
|
-
return {
|
|
11
|
-
name: "fatcher-middleware-aborter",
|
|
12
|
-
async use(context, next) {
|
|
13
|
-
const abortController = new AbortController();
|
|
14
|
-
const requestTask = next({
|
|
15
|
-
signal: abortController.signal
|
|
16
|
-
});
|
|
17
|
-
if (!_timeout) {
|
|
18
|
-
return requestTask;
|
|
19
|
-
}
|
|
20
|
-
const timer = setTimeout(() => {
|
|
21
|
-
abortController.abort();
|
|
22
|
-
onAbort == null ? void 0 : onAbort();
|
|
23
|
-
}, _timeout);
|
|
24
|
-
const response = await requestTask;
|
|
25
|
-
clearTimeout(timer);
|
|
26
|
-
return response;
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export { aborter };
|
package/dist/index.js
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var fatcher = require('fatcher');
|
|
6
|
-
|
|
7
|
-
function aborter(options = {}) {
|
|
8
|
-
const { timeout = 0, onAbort = null } = options;
|
|
9
|
-
let _timeout = timeout;
|
|
10
|
-
if (isNaN(timeout) || ~~timeout <= 0) {
|
|
11
|
-
fatcher.warn("<Aborter> Timeout is not a valid number.");
|
|
12
|
-
_timeout = 0;
|
|
13
|
-
}
|
|
14
|
-
return {
|
|
15
|
-
name: "fatcher-middleware-aborter",
|
|
16
|
-
async use(context, next) {
|
|
17
|
-
const abortController = new AbortController();
|
|
18
|
-
const requestTask = next({
|
|
19
|
-
signal: abortController.signal
|
|
20
|
-
});
|
|
21
|
-
if (!_timeout) {
|
|
22
|
-
return requestTask;
|
|
23
|
-
}
|
|
24
|
-
const timer = setTimeout(() => {
|
|
25
|
-
abortController.abort();
|
|
26
|
-
onAbort == null ? void 0 : onAbort();
|
|
27
|
-
}, _timeout);
|
|
28
|
-
const response = await requestTask;
|
|
29
|
-
clearTimeout(timer);
|
|
30
|
-
return response;
|
|
31
|
-
}
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
exports.aborter = aborter;
|
package/dist/index.min.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
(function(e,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("fatcher")):typeof define=="function"&&define.amd?define(["exports","fatcher"],t):(e=typeof globalThis<"u"?globalThis:e||self,t(e.FatcherMiddlewareAborter={},e.Fatcher))})(this,function(e,t){"use strict";function u(a={}){const{timeout:r=0,onAbort:o=null}=a;let n=r;return(isNaN(r)||~~r<=0)&&(t.warn("<Aborter> Timeout is not a valid number."),n=0),{name:"fatcher-middleware-aborter",async use(l,c){const i=new AbortController,s=c({signal:i.signal});if(!n)return s;const f=setTimeout(()=>{i.abort(),o?.()},n),d=await s;return clearTimeout(f),d}}}e.aborter=u,Object.defineProperty(e,"__esModule",{value:!0})});
|