keq 2.8.0 → 2.8.2
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/CHANGELOG.md +15 -0
- package/dist/esm/src/constant.d.ts +0 -1
- package/dist/esm/src/constant.js +0 -1
- package/dist/esm/src/core.js +2 -2
- package/dist/esm/src/index.d.ts +1 -0
- package/dist/esm/src/index.js +1 -0
- package/dist/esm/src/is/is-buffer.d.ts +1 -0
- package/dist/esm/src/keq.d.ts +1 -0
- package/dist/esm/src/middlewares/proxy-response-middleware.js +3 -28
- package/dist/esm/src/middlewares/retry-middleware.js +2 -3
- package/dist/esm/src/types/keq-context.d.ts +9 -6
- package/dist/esm/src/types/keq-context.js +1 -1
- package/dist/esm/src/types/keq-operation.d.ts +1 -0
- package/dist/esm/src/util/compose-middleware.js +10 -12
- package/dist/esm/src/util/create-response-proxy.d.ts +1 -0
- package/dist/esm/src/util/create-response-proxy.js +28 -0
- package/dist/umd/src/constant.d.ts +0 -1
- package/dist/umd/src/constant.js +1 -2
- package/dist/umd/src/core.js +1 -1
- package/dist/umd/src/index.d.ts +1 -0
- package/dist/umd/src/index.js +4 -2
- package/dist/umd/src/is/is-buffer.d.ts +1 -0
- package/dist/umd/src/keq.d.ts +1 -0
- package/dist/umd/src/middlewares/proxy-response-middleware.js +4 -29
- package/dist/umd/src/middlewares/retry-middleware.js +3 -4
- package/dist/umd/src/types/keq-context.d.ts +9 -6
- package/dist/umd/src/types/keq-operation.d.ts +1 -0
- package/dist/umd/src/util/compose-middleware.js +11 -13
- package/dist/umd/src/util/create-response-proxy.d.ts +1 -0
- package/dist/umd/src/util/create-response-proxy.js +42 -0
- package/images/logo.svg +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
## [2.8.2](https://github.com/keq-request/keq/compare/v2.8.1...v2.8.2) (2024-10-07)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Performance Improvements
|
|
9
|
+
|
|
10
|
+
* export createResponseProxy function ([261606e](https://github.com/keq-request/keq/commit/261606e74f12d32c80428eb9b20d175b60171bff))
|
|
11
|
+
|
|
12
|
+
## [2.8.1](https://github.com/keq-request/keq/compare/v2.8.0...v2.8.1) (2024-09-13)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
### Performance Improvements
|
|
16
|
+
|
|
17
|
+
* allow middleware access context.metadata ([4561592](https://github.com/keq-request/keq/commit/4561592390c78a47cedb2d230e4765f51de7d854))
|
|
18
|
+
* set identifier to be readonly ([6f64f1b](https://github.com/keq-request/keq/commit/6f64f1b5e5a698bae15c2ddb1f3c4fe3a6e2a6ae))
|
|
19
|
+
|
|
5
20
|
## [2.8.0](https://github.com/keq-request/keq/compare/v2.7.5...v2.8.0) (2024-09-11)
|
|
6
21
|
|
|
7
22
|
|
package/dist/esm/src/constant.js
CHANGED
package/dist/esm/src/core.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import mitt from 'mitt';
|
|
3
3
|
import { Exception } from './exception/exception.js';
|
|
4
4
|
import { cloneBody } from './util/clone-body.js';
|
|
5
|
-
import { ABORT_PROPERTY,
|
|
5
|
+
import { ABORT_PROPERTY, OUTPUT_PROPERTY } from './constant.js';
|
|
6
6
|
import { composeMiddleware } from './util/compose-middleware.js';
|
|
7
7
|
import { shallowClone } from './util/shallow-clone.js';
|
|
8
8
|
import { compileUrl } from './util/compile-url.js';
|
|
@@ -81,7 +81,7 @@ export class Core {
|
|
|
81
81
|
}
|
|
82
82
|
}
|
|
83
83
|
const ctx = {
|
|
84
|
-
|
|
84
|
+
metadata: {
|
|
85
85
|
finished: false,
|
|
86
86
|
entryNextTimes: 0,
|
|
87
87
|
outNextTimes: 0,
|
package/dist/esm/src/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export { Keq } from './keq.js';
|
|
|
3
3
|
export { request } from './request.js';
|
|
4
4
|
export { composeMiddleware } from './util/compose-middleware.js';
|
|
5
5
|
export { composeRoute } from './util/compose-route.js';
|
|
6
|
+
export { createResponseProxy } from './util/create-response-proxy.js';
|
|
6
7
|
export type { KeqContext, KeqContextOptions } from './types/keq-context.js';
|
|
7
8
|
export type { KeqMiddleware } from './types/keq-middleware.js';
|
|
8
9
|
export type { KeqNext } from './types/keq-next.js';
|
package/dist/esm/src/index.js
CHANGED
|
@@ -3,6 +3,7 @@ export { Keq } from './keq.js';
|
|
|
3
3
|
export { request } from './request.js';
|
|
4
4
|
export { composeMiddleware } from './util/compose-middleware.js';
|
|
5
5
|
export { composeRoute } from './util/compose-route.js';
|
|
6
|
+
export { createResponseProxy } from './util/create-response-proxy.js';
|
|
6
7
|
export { keqHostRoute } from './router/keq-host-route.js';
|
|
7
8
|
export { keqLocationRoute } from './router/keq-location-route.js';
|
|
8
9
|
export { keqMethodRoute } from './router/keq-method-route.js';
|
package/dist/esm/src/keq.d.ts
CHANGED
|
@@ -1,34 +1,9 @@
|
|
|
1
|
+
import { createResponseProxy } from "../util/create-response-proxy.js";
|
|
1
2
|
export function proxyResponseMiddleware() {
|
|
2
3
|
return async function proxyResponseMiddleware(ctx, next) {
|
|
3
4
|
await next();
|
|
4
5
|
const res = ctx.res;
|
|
5
|
-
if (res)
|
|
6
|
-
ctx.response =
|
|
7
|
-
get(res, prop) {
|
|
8
|
-
if (typeof prop === 'string') {
|
|
9
|
-
if (['json', 'text', 'arrayBuffer', 'blob', 'buffer', 'formData'].includes(prop)) {
|
|
10
|
-
/**
|
|
11
|
-
* clone when invoking body, json, text, arrayBuffer, blob, buffer, formData
|
|
12
|
-
* to avoid time-consuming cloning
|
|
13
|
-
*/
|
|
14
|
-
return new Proxy(res[prop], {
|
|
15
|
-
apply(target, thisArg, argArray) {
|
|
16
|
-
const mirror = res.clone();
|
|
17
|
-
return mirror[prop](...argArray);
|
|
18
|
-
},
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
if (prop === 'body') {
|
|
22
|
-
const mirror = res.clone();
|
|
23
|
-
return mirror.body;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
if (typeof res[prop] === 'function') {
|
|
27
|
-
return res[prop].bind(res);
|
|
28
|
-
}
|
|
29
|
-
return res[prop];
|
|
30
|
-
},
|
|
31
|
-
});
|
|
32
|
-
}
|
|
6
|
+
if (res)
|
|
7
|
+
ctx.response = createResponseProxy(res);
|
|
33
8
|
};
|
|
34
9
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { NEXT_INVOKED_PROPERTY } from '../constant.js';
|
|
2
1
|
function sleep(ms) {
|
|
3
2
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
4
3
|
}
|
|
@@ -33,8 +32,8 @@ export function retryMiddleware() {
|
|
|
33
32
|
ctx.fetchArguments = undefined;
|
|
34
33
|
ctx.response = undefined;
|
|
35
34
|
ctx.req = undefined;
|
|
36
|
-
ctx
|
|
37
|
-
ctx
|
|
35
|
+
ctx.metadata.entryNextTimes = 0;
|
|
36
|
+
ctx.metadata.outNextTimes = 0;
|
|
38
37
|
await next();
|
|
39
38
|
}
|
|
40
39
|
catch (e) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ABORT_PROPERTY,
|
|
1
|
+
import { ABORT_PROPERTY, OUTPUT_PROPERTY } from '../constant.js';
|
|
2
2
|
import type { Emitter } from 'mitt';
|
|
3
3
|
import type { KeqOptionsParameter } from './keq-options.js';
|
|
4
4
|
import type { KeqEvents } from './keq-events.js';
|
|
@@ -9,12 +9,14 @@ export interface KeqContextOptions extends KeqOptionsParameter {
|
|
|
9
9
|
}
|
|
10
10
|
export interface KeqContext {
|
|
11
11
|
/**
|
|
12
|
-
* Middleware
|
|
12
|
+
* Middleware metadata
|
|
13
13
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
14
|
+
* This property does not share between middlewares
|
|
15
|
+
* And will be destroyed after the middleware finish
|
|
16
|
+
*
|
|
17
|
+
* next: to prevent someone invoke `next()` multiple times or forgetting await
|
|
16
18
|
*/
|
|
17
|
-
|
|
19
|
+
metadata: {
|
|
18
20
|
finished: boolean;
|
|
19
21
|
entryNextTimes: number;
|
|
20
22
|
outNextTimes: number;
|
|
@@ -23,8 +25,9 @@ export interface KeqContext {
|
|
|
23
25
|
abort: (reason: any) => void;
|
|
24
26
|
/**
|
|
25
27
|
* The unique identifier of the request's location in the code
|
|
28
|
+
* @readonly
|
|
26
29
|
*/
|
|
27
|
-
identifier: string;
|
|
30
|
+
readonly identifier: string;
|
|
28
31
|
emitter: Emitter<Omit<KeqEvents, never>>;
|
|
29
32
|
options: KeqContextOptions;
|
|
30
33
|
/**
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import { ABORT_PROPERTY,
|
|
2
|
+
import { ABORT_PROPERTY, OUTPUT_PROPERTY } from '../constant.js';
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Exception } from '../exception/exception.js';
|
|
2
|
-
import { NEXT_INVOKED_PROPERTY } from '../constant.js';
|
|
3
2
|
export function composeMiddleware(middlewares) {
|
|
4
3
|
if (!middlewares.length) {
|
|
5
4
|
throw new Exception('At least one middleware');
|
|
@@ -8,38 +7,37 @@ export function composeMiddleware(middlewares) {
|
|
|
8
7
|
.reverse()
|
|
9
8
|
.reduce(function (prev, curr) {
|
|
10
9
|
return async (ctx, next) => {
|
|
11
|
-
const
|
|
10
|
+
const metadata = {
|
|
12
11
|
finished: false,
|
|
13
12
|
entryNextTimes: 0,
|
|
14
13
|
outNextTimes: 0,
|
|
15
14
|
};
|
|
16
15
|
const context = new Proxy(ctx, {
|
|
17
16
|
get(target, property) {
|
|
18
|
-
if (property ===
|
|
19
|
-
return
|
|
20
|
-
}
|
|
17
|
+
if (property === 'metadata')
|
|
18
|
+
return metadata;
|
|
21
19
|
// @ts-ignore
|
|
22
20
|
return target[property];
|
|
23
21
|
},
|
|
24
22
|
});
|
|
25
23
|
await curr(context, async () => {
|
|
26
|
-
if (
|
|
24
|
+
if (metadata.finished) {
|
|
27
25
|
throw new Exception([
|
|
28
26
|
`next() should not invoke after ${curr.toString()} middleware finished.`,
|
|
29
27
|
].join(''));
|
|
30
28
|
}
|
|
31
|
-
if (
|
|
29
|
+
if (metadata.entryNextTimes > 1) {
|
|
32
30
|
console.warn(`next() had be invoke multiple times at ${curr.toString()} middleware`);
|
|
33
31
|
}
|
|
34
|
-
|
|
32
|
+
metadata.entryNextTimes += 1;
|
|
35
33
|
await prev(ctx, next);
|
|
36
|
-
|
|
34
|
+
metadata.outNextTimes += 1;
|
|
37
35
|
});
|
|
38
|
-
|
|
39
|
-
if (
|
|
36
|
+
metadata.finished = true;
|
|
37
|
+
if (metadata.entryNextTimes === 0) {
|
|
40
38
|
console.warn(`next() is not invoked at ${curr.toString()}.`);
|
|
41
39
|
}
|
|
42
|
-
if (
|
|
40
|
+
if (metadata.entryNextTimes !== metadata.outNextTimes) {
|
|
43
41
|
throw new Exception([
|
|
44
42
|
`next() should be invoke before ${curr.toString()} middleware finish.`,
|
|
45
43
|
'Maybe you forgot to await when calling next().',
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createResponseProxy(res: Response): Response;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export function createResponseProxy(res) {
|
|
2
|
+
return new Proxy(res, {
|
|
3
|
+
get(res, prop) {
|
|
4
|
+
if (typeof prop === 'string') {
|
|
5
|
+
if (['json', 'text', 'arrayBuffer', 'blob', 'buffer', 'formData'].includes(prop)) {
|
|
6
|
+
/**
|
|
7
|
+
* clone when invoking body, json, text, arrayBuffer, blob, buffer, formData
|
|
8
|
+
* to avoid time-consuming cloning
|
|
9
|
+
*/
|
|
10
|
+
return new Proxy(res[prop], {
|
|
11
|
+
apply(target, thisArg, argArray) {
|
|
12
|
+
const mirror = res.clone();
|
|
13
|
+
return mirror[prop](...argArray);
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
if (prop === 'body') {
|
|
18
|
+
const mirror = res.clone();
|
|
19
|
+
return mirror.body;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (typeof res[prop] === 'function') {
|
|
23
|
+
return res[prop].bind(res);
|
|
24
|
+
}
|
|
25
|
+
return res[prop];
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
}
|
package/dist/umd/src/constant.js
CHANGED
|
@@ -9,8 +9,7 @@
|
|
|
9
9
|
})(function (require, exports) {
|
|
10
10
|
"use strict";
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.ABORT_PROPERTY = exports.
|
|
12
|
+
exports.ABORT_PROPERTY = exports.OUTPUT_PROPERTY = void 0;
|
|
13
13
|
exports.OUTPUT_PROPERTY = Symbol('outputProperty');
|
|
14
|
-
exports.NEXT_INVOKED_PROPERTY = Symbol('nextInvokedProperty');
|
|
15
14
|
exports.ABORT_PROPERTY = Symbol('abortProperty');
|
|
16
15
|
});
|
package/dist/umd/src/core.js
CHANGED
package/dist/umd/src/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ export { Keq } from './keq.js';
|
|
|
3
3
|
export { request } from './request.js';
|
|
4
4
|
export { composeMiddleware } from './util/compose-middleware.js';
|
|
5
5
|
export { composeRoute } from './util/compose-route.js';
|
|
6
|
+
export { createResponseProxy } from './util/create-response-proxy.js';
|
|
6
7
|
export type { KeqContext, KeqContextOptions } from './types/keq-context.js';
|
|
7
8
|
export type { KeqMiddleware } from './types/keq-middleware.js';
|
|
8
9
|
export type { KeqNext } from './types/keq-next.js';
|
package/dist/umd/src/index.js
CHANGED
|
@@ -4,12 +4,12 @@
|
|
|
4
4
|
if (v !== undefined) module.exports = v;
|
|
5
5
|
}
|
|
6
6
|
else if (typeof define === "function" && define.amd) {
|
|
7
|
-
define(["require", "exports", "./create-request.js", "./keq.js", "./request.js", "./util/compose-middleware.js", "./util/compose-route.js", "./router/keq-host-route.js", "./router/keq-location-route.js", "./router/keq-method-route.js", "./router/keq-module-route.js", "./router/keq-pathname-route.js", "./router/keq-router.js"], factory);
|
|
7
|
+
define(["require", "exports", "./create-request.js", "./keq.js", "./request.js", "./util/compose-middleware.js", "./util/compose-route.js", "./util/create-response-proxy.js", "./router/keq-host-route.js", "./router/keq-location-route.js", "./router/keq-method-route.js", "./router/keq-module-route.js", "./router/keq-pathname-route.js", "./router/keq-router.js"], factory);
|
|
8
8
|
}
|
|
9
9
|
})(function (require, exports) {
|
|
10
10
|
"use strict";
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.KeqRouter = exports.keqPathnameRoute = exports.keqModuleRoute = exports.keqMethodRoute = exports.keqLocationRoute = exports.keqHostRoute = exports.composeRoute = exports.composeMiddleware = exports.request = exports.Keq = exports.createRequest = void 0;
|
|
12
|
+
exports.KeqRouter = exports.keqPathnameRoute = exports.keqModuleRoute = exports.keqMethodRoute = exports.keqLocationRoute = exports.keqHostRoute = exports.createResponseProxy = exports.composeRoute = exports.composeMiddleware = exports.request = exports.Keq = exports.createRequest = void 0;
|
|
13
13
|
var create_request_js_1 = require("./create-request.js");
|
|
14
14
|
Object.defineProperty(exports, "createRequest", { enumerable: true, get: function () { return create_request_js_1.createRequest; } });
|
|
15
15
|
var keq_js_1 = require("./keq.js");
|
|
@@ -20,6 +20,8 @@
|
|
|
20
20
|
Object.defineProperty(exports, "composeMiddleware", { enumerable: true, get: function () { return compose_middleware_js_1.composeMiddleware; } });
|
|
21
21
|
var compose_route_js_1 = require("./util/compose-route.js");
|
|
22
22
|
Object.defineProperty(exports, "composeRoute", { enumerable: true, get: function () { return compose_route_js_1.composeRoute; } });
|
|
23
|
+
var create_response_proxy_js_1 = require("./util/create-response-proxy.js");
|
|
24
|
+
Object.defineProperty(exports, "createResponseProxy", { enumerable: true, get: function () { return create_response_proxy_js_1.createResponseProxy; } });
|
|
23
25
|
var keq_host_route_js_1 = require("./router/keq-host-route.js");
|
|
24
26
|
Object.defineProperty(exports, "keqHostRoute", { enumerable: true, get: function () { return keq_host_route_js_1.keqHostRoute; } });
|
|
25
27
|
var keq_location_route_js_1 = require("./router/keq-location-route.js");
|
package/dist/umd/src/keq.d.ts
CHANGED
|
@@ -4,44 +4,19 @@
|
|
|
4
4
|
if (v !== undefined) module.exports = v;
|
|
5
5
|
}
|
|
6
6
|
else if (typeof define === "function" && define.amd) {
|
|
7
|
-
define(["require", "exports"], factory);
|
|
7
|
+
define(["require", "exports", "../util/create-response-proxy.js"], factory);
|
|
8
8
|
}
|
|
9
9
|
})(function (require, exports) {
|
|
10
10
|
"use strict";
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.proxyResponseMiddleware = void 0;
|
|
13
|
+
const create_response_proxy_js_1 = require("../util/create-response-proxy.js");
|
|
13
14
|
function proxyResponseMiddleware() {
|
|
14
15
|
return async function proxyResponseMiddleware(ctx, next) {
|
|
15
16
|
await next();
|
|
16
17
|
const res = ctx.res;
|
|
17
|
-
if (res)
|
|
18
|
-
ctx.response =
|
|
19
|
-
get(res, prop) {
|
|
20
|
-
if (typeof prop === 'string') {
|
|
21
|
-
if (['json', 'text', 'arrayBuffer', 'blob', 'buffer', 'formData'].includes(prop)) {
|
|
22
|
-
/**
|
|
23
|
-
* clone when invoking body, json, text, arrayBuffer, blob, buffer, formData
|
|
24
|
-
* to avoid time-consuming cloning
|
|
25
|
-
*/
|
|
26
|
-
return new Proxy(res[prop], {
|
|
27
|
-
apply(target, thisArg, argArray) {
|
|
28
|
-
const mirror = res.clone();
|
|
29
|
-
return mirror[prop](...argArray);
|
|
30
|
-
},
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
if (prop === 'body') {
|
|
34
|
-
const mirror = res.clone();
|
|
35
|
-
return mirror.body;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
if (typeof res[prop] === 'function') {
|
|
39
|
-
return res[prop].bind(res);
|
|
40
|
-
}
|
|
41
|
-
return res[prop];
|
|
42
|
-
},
|
|
43
|
-
});
|
|
44
|
-
}
|
|
18
|
+
if (res)
|
|
19
|
+
ctx.response = (0, create_response_proxy_js_1.createResponseProxy)(res);
|
|
45
20
|
};
|
|
46
21
|
}
|
|
47
22
|
exports.proxyResponseMiddleware = proxyResponseMiddleware;
|
|
@@ -4,13 +4,12 @@
|
|
|
4
4
|
if (v !== undefined) module.exports = v;
|
|
5
5
|
}
|
|
6
6
|
else if (typeof define === "function" && define.amd) {
|
|
7
|
-
define(["require", "exports"
|
|
7
|
+
define(["require", "exports"], factory);
|
|
8
8
|
}
|
|
9
9
|
})(function (require, exports) {
|
|
10
10
|
"use strict";
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.retryMiddleware = void 0;
|
|
13
|
-
const constant_js_1 = require("../constant.js");
|
|
14
13
|
function sleep(ms) {
|
|
15
14
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
16
15
|
}
|
|
@@ -45,8 +44,8 @@
|
|
|
45
44
|
ctx.fetchArguments = undefined;
|
|
46
45
|
ctx.response = undefined;
|
|
47
46
|
ctx.req = undefined;
|
|
48
|
-
ctx
|
|
49
|
-
ctx
|
|
47
|
+
ctx.metadata.entryNextTimes = 0;
|
|
48
|
+
ctx.metadata.outNextTimes = 0;
|
|
50
49
|
await next();
|
|
51
50
|
}
|
|
52
51
|
catch (e) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ABORT_PROPERTY,
|
|
1
|
+
import { ABORT_PROPERTY, OUTPUT_PROPERTY } from '../constant.js';
|
|
2
2
|
import type { Emitter } from 'mitt';
|
|
3
3
|
import type { KeqOptionsParameter } from './keq-options.js';
|
|
4
4
|
import type { KeqEvents } from './keq-events.js';
|
|
@@ -9,12 +9,14 @@ export interface KeqContextOptions extends KeqOptionsParameter {
|
|
|
9
9
|
}
|
|
10
10
|
export interface KeqContext {
|
|
11
11
|
/**
|
|
12
|
-
* Middleware
|
|
12
|
+
* Middleware metadata
|
|
13
13
|
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
14
|
+
* This property does not share between middlewares
|
|
15
|
+
* And will be destroyed after the middleware finish
|
|
16
|
+
*
|
|
17
|
+
* next: to prevent someone invoke `next()` multiple times or forgetting await
|
|
16
18
|
*/
|
|
17
|
-
|
|
19
|
+
metadata: {
|
|
18
20
|
finished: boolean;
|
|
19
21
|
entryNextTimes: number;
|
|
20
22
|
outNextTimes: number;
|
|
@@ -23,8 +25,9 @@ export interface KeqContext {
|
|
|
23
25
|
abort: (reason: any) => void;
|
|
24
26
|
/**
|
|
25
27
|
* The unique identifier of the request's location in the code
|
|
28
|
+
* @readonly
|
|
26
29
|
*/
|
|
27
|
-
identifier: string;
|
|
30
|
+
readonly identifier: string;
|
|
28
31
|
emitter: Emitter<Omit<KeqEvents, never>>;
|
|
29
32
|
options: KeqContextOptions;
|
|
30
33
|
/**
|
|
@@ -4,14 +4,13 @@
|
|
|
4
4
|
if (v !== undefined) module.exports = v;
|
|
5
5
|
}
|
|
6
6
|
else if (typeof define === "function" && define.amd) {
|
|
7
|
-
define(["require", "exports", "../exception/exception.js"
|
|
7
|
+
define(["require", "exports", "../exception/exception.js"], factory);
|
|
8
8
|
}
|
|
9
9
|
})(function (require, exports) {
|
|
10
10
|
"use strict";
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.composeMiddleware = void 0;
|
|
13
13
|
const exception_js_1 = require("../exception/exception.js");
|
|
14
|
-
const constant_js_1 = require("../constant.js");
|
|
15
14
|
function composeMiddleware(middlewares) {
|
|
16
15
|
if (!middlewares.length) {
|
|
17
16
|
throw new exception_js_1.Exception('At least one middleware');
|
|
@@ -20,38 +19,37 @@
|
|
|
20
19
|
.reverse()
|
|
21
20
|
.reduce(function (prev, curr) {
|
|
22
21
|
return async (ctx, next) => {
|
|
23
|
-
const
|
|
22
|
+
const metadata = {
|
|
24
23
|
finished: false,
|
|
25
24
|
entryNextTimes: 0,
|
|
26
25
|
outNextTimes: 0,
|
|
27
26
|
};
|
|
28
27
|
const context = new Proxy(ctx, {
|
|
29
28
|
get(target, property) {
|
|
30
|
-
if (property ===
|
|
31
|
-
return
|
|
32
|
-
}
|
|
29
|
+
if (property === 'metadata')
|
|
30
|
+
return metadata;
|
|
33
31
|
// @ts-ignore
|
|
34
32
|
return target[property];
|
|
35
33
|
},
|
|
36
34
|
});
|
|
37
35
|
await curr(context, async () => {
|
|
38
|
-
if (
|
|
36
|
+
if (metadata.finished) {
|
|
39
37
|
throw new exception_js_1.Exception([
|
|
40
38
|
`next() should not invoke after ${curr.toString()} middleware finished.`,
|
|
41
39
|
].join(''));
|
|
42
40
|
}
|
|
43
|
-
if (
|
|
41
|
+
if (metadata.entryNextTimes > 1) {
|
|
44
42
|
console.warn(`next() had be invoke multiple times at ${curr.toString()} middleware`);
|
|
45
43
|
}
|
|
46
|
-
|
|
44
|
+
metadata.entryNextTimes += 1;
|
|
47
45
|
await prev(ctx, next);
|
|
48
|
-
|
|
46
|
+
metadata.outNextTimes += 1;
|
|
49
47
|
});
|
|
50
|
-
|
|
51
|
-
if (
|
|
48
|
+
metadata.finished = true;
|
|
49
|
+
if (metadata.entryNextTimes === 0) {
|
|
52
50
|
console.warn(`next() is not invoked at ${curr.toString()}.`);
|
|
53
51
|
}
|
|
54
|
-
if (
|
|
52
|
+
if (metadata.entryNextTimes !== metadata.outNextTimes) {
|
|
55
53
|
throw new exception_js_1.Exception([
|
|
56
54
|
`next() should be invoke before ${curr.toString()} middleware finish.`,
|
|
57
55
|
'Maybe you forgot to await when calling next().',
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function createResponseProxy(res: Response): Response;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
(function (factory) {
|
|
2
|
+
if (typeof module === "object" && typeof module.exports === "object") {
|
|
3
|
+
var v = factory(require, exports);
|
|
4
|
+
if (v !== undefined) module.exports = v;
|
|
5
|
+
}
|
|
6
|
+
else if (typeof define === "function" && define.amd) {
|
|
7
|
+
define(["require", "exports"], factory);
|
|
8
|
+
}
|
|
9
|
+
})(function (require, exports) {
|
|
10
|
+
"use strict";
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.createResponseProxy = void 0;
|
|
13
|
+
function createResponseProxy(res) {
|
|
14
|
+
return new Proxy(res, {
|
|
15
|
+
get(res, prop) {
|
|
16
|
+
if (typeof prop === 'string') {
|
|
17
|
+
if (['json', 'text', 'arrayBuffer', 'blob', 'buffer', 'formData'].includes(prop)) {
|
|
18
|
+
/**
|
|
19
|
+
* clone when invoking body, json, text, arrayBuffer, blob, buffer, formData
|
|
20
|
+
* to avoid time-consuming cloning
|
|
21
|
+
*/
|
|
22
|
+
return new Proxy(res[prop], {
|
|
23
|
+
apply(target, thisArg, argArray) {
|
|
24
|
+
const mirror = res.clone();
|
|
25
|
+
return mirror[prop](...argArray);
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
if (prop === 'body') {
|
|
30
|
+
const mirror = res.clone();
|
|
31
|
+
return mirror.body;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
if (typeof res[prop] === 'function') {
|
|
35
|
+
return res[prop].bind(res);
|
|
36
|
+
}
|
|
37
|
+
return res[prop];
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
exports.createResponseProxy = createResponseProxy;
|
|
42
|
+
});
|
package/images/logo.svg
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
<svg
|
|
1
|
+
<svg height="512" viewBox="0 0 32 32" width="512" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><linearGradient id="linear-gradient" gradientUnits="userSpaceOnUse" x1="4" x2="28" y1="15.5" y2="15.5"><stop offset="0" stop-color="#fde941"/><stop offset="1" stop-color="#f37d1c"/></linearGradient><g id="Beetroot"><path d="m16 29a12.01375 12.01375 0 0 1 -12-12c0-7.46338 10.99512-14.5459 11.46289-14.84375a1.0026 1.0026 0 0 1 1.07422 0c.46777.29785 11.46289 7.38037 11.46289 14.84375a12.01375 12.01375 0 0 1 -12 12zm0-24.7959c-2.14844 1.48096-10 7.27149-10 12.7959a10 10 0 0 0 20 0c0-5.52441-7.85156-11.31494-10-12.7959zm0 20.7959a7.86558 7.86558 0 0 1 -7.85693-7.85693c0-4.15479 6.5-9.35205 7.24121-9.93116a1.00138 1.00138 0 0 1 1.23144 0c.74121.57911 7.24121 5.77637 7.24121 9.93116a7.86558 7.86558 0 0 1 -7.85693 7.85693zm0-15.71143c-2.06885 1.73731-5.85693 5.42188-5.85693 7.8545a5.85693 5.85693 0 1 0 11.71386 0c0-2.43262-3.78808-6.11719-5.85693-7.8545zm2.65674 10.69532a3.98581 3.98581 0 0 0 0-5.96778.99984.99984 0 1 0 -1.31348 1.50782 1.98716 1.98716 0 0 1 0 2.95214.99984.99984 0 1 0 1.31348 1.50782zm-3.90283-.09717a.99962.99962 0 0 0 -.09717-1.41065 1.98716 1.98716 0 0 1 0-2.95214.99984.99984 0 1 0 -1.31348-1.50782 3.98581 3.98581 0 0 0 0 5.96778.99946.99946 0 0 0 1.41065-.09717z" fill="url(#linear-gradient)"/></g></svg>
|