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.
Files changed (32) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/esm/src/constant.d.ts +0 -1
  3. package/dist/esm/src/constant.js +0 -1
  4. package/dist/esm/src/core.js +2 -2
  5. package/dist/esm/src/index.d.ts +1 -0
  6. package/dist/esm/src/index.js +1 -0
  7. package/dist/esm/src/is/is-buffer.d.ts +1 -0
  8. package/dist/esm/src/keq.d.ts +1 -0
  9. package/dist/esm/src/middlewares/proxy-response-middleware.js +3 -28
  10. package/dist/esm/src/middlewares/retry-middleware.js +2 -3
  11. package/dist/esm/src/types/keq-context.d.ts +9 -6
  12. package/dist/esm/src/types/keq-context.js +1 -1
  13. package/dist/esm/src/types/keq-operation.d.ts +1 -0
  14. package/dist/esm/src/util/compose-middleware.js +10 -12
  15. package/dist/esm/src/util/create-response-proxy.d.ts +1 -0
  16. package/dist/esm/src/util/create-response-proxy.js +28 -0
  17. package/dist/umd/src/constant.d.ts +0 -1
  18. package/dist/umd/src/constant.js +1 -2
  19. package/dist/umd/src/core.js +1 -1
  20. package/dist/umd/src/index.d.ts +1 -0
  21. package/dist/umd/src/index.js +4 -2
  22. package/dist/umd/src/is/is-buffer.d.ts +1 -0
  23. package/dist/umd/src/keq.d.ts +1 -0
  24. package/dist/umd/src/middlewares/proxy-response-middleware.js +4 -29
  25. package/dist/umd/src/middlewares/retry-middleware.js +3 -4
  26. package/dist/umd/src/types/keq-context.d.ts +9 -6
  27. package/dist/umd/src/types/keq-operation.d.ts +1 -0
  28. package/dist/umd/src/util/compose-middleware.js +11 -13
  29. package/dist/umd/src/util/create-response-proxy.d.ts +1 -0
  30. package/dist/umd/src/util/create-response-proxy.js +42 -0
  31. package/images/logo.svg +1 -1
  32. 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
 
@@ -1,3 +1,2 @@
1
1
  export declare const OUTPUT_PROPERTY: unique symbol;
2
- export declare const NEXT_INVOKED_PROPERTY: unique symbol;
3
2
  export declare const ABORT_PROPERTY: unique symbol;
@@ -1,3 +1,2 @@
1
1
  export const OUTPUT_PROPERTY = Symbol('outputProperty');
2
- export const NEXT_INVOKED_PROPERTY = Symbol('nextInvokedProperty');
3
2
  export const ABORT_PROPERTY = Symbol('abortProperty');
@@ -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, NEXT_INVOKED_PROPERTY, OUTPUT_PROPERTY } from './constant.js';
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
- [NEXT_INVOKED_PROPERTY]: {
84
+ metadata: {
85
85
  finished: false,
86
86
  entryNextTimes: 0,
87
87
  outNextTimes: 0,
@@ -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';
@@ -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';
@@ -1,2 +1,3 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  export declare function isBuffer(obj: any): obj is Buffer;
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { Core } from './core.js';
3
4
  import { KeqFlowControlMode, KeqFlowControlSignal } from './types/keq-flow-control.js';
4
5
  import type { KeqRetryOn, KeqRetryDelay } from './types/keq-retry.js';
@@ -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 = new Proxy(res, {
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[NEXT_INVOKED_PROPERTY].entryNextTimes = 0;
37
- ctx[NEXT_INVOKED_PROPERTY].outNextTimes = 0;
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, NEXT_INVOKED_PROPERTY, OUTPUT_PROPERTY } from '../constant.js';
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 invoker counter
12
+ * Middleware metadata
13
13
  *
14
- * to prevent someone from calling next
15
- * multiple times or forgetting to write await
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
- [NEXT_INVOKED_PROPERTY]: {
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, NEXT_INVOKED_PROPERTY, OUTPUT_PROPERTY } from '../constant.js';
2
+ import { ABORT_PROPERTY, OUTPUT_PROPERTY } from '../constant.js';
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { ExtractProperty } from './extract-property.js';
3
4
  import { KeqContextRequestMethod } from './keq-context-request.js';
4
5
  export interface KeqOperation {
@@ -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 invoked = {
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 === NEXT_INVOKED_PROPERTY) {
19
- return invoked;
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 (invoked.finished) {
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 (invoked.entryNextTimes > 1) {
29
+ if (metadata.entryNextTimes > 1) {
32
30
  console.warn(`next() had be invoke multiple times at ${curr.toString()} middleware`);
33
31
  }
34
- invoked.entryNextTimes += 1;
32
+ metadata.entryNextTimes += 1;
35
33
  await prev(ctx, next);
36
- invoked.outNextTimes += 1;
34
+ metadata.outNextTimes += 1;
37
35
  });
38
- invoked.finished = true;
39
- if (invoked.entryNextTimes === 0) {
36
+ metadata.finished = true;
37
+ if (metadata.entryNextTimes === 0) {
40
38
  console.warn(`next() is not invoked at ${curr.toString()}.`);
41
39
  }
42
- if (invoked.entryNextTimes !== invoked.outNextTimes) {
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
+ }
@@ -1,3 +1,2 @@
1
1
  export declare const OUTPUT_PROPERTY: unique symbol;
2
- export declare const NEXT_INVOKED_PROPERTY: unique symbol;
3
2
  export declare const ABORT_PROPERTY: unique symbol;
@@ -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.NEXT_INVOKED_PROPERTY = exports.OUTPUT_PROPERTY = void 0;
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
  });
@@ -96,7 +96,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
96
96
  }
97
97
  }
98
98
  const ctx = {
99
- [constant_js_1.NEXT_INVOKED_PROPERTY]: {
99
+ metadata: {
100
100
  finished: false,
101
101
  entryNextTimes: 0,
102
102
  outNextTimes: 0,
@@ -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';
@@ -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");
@@ -1,2 +1,3 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  export declare function isBuffer(obj: any): obj is Buffer;
@@ -1,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { Core } from './core.js';
3
4
  import { KeqFlowControlMode, KeqFlowControlSignal } from './types/keq-flow-control.js';
4
5
  import type { KeqRetryOn, KeqRetryDelay } from './types/keq-retry.js';
@@ -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 = new Proxy(res, {
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", "../constant.js"], factory);
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[constant_js_1.NEXT_INVOKED_PROPERTY].entryNextTimes = 0;
49
- ctx[constant_js_1.NEXT_INVOKED_PROPERTY].outNextTimes = 0;
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, NEXT_INVOKED_PROPERTY, OUTPUT_PROPERTY } from '../constant.js';
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 invoker counter
12
+ * Middleware metadata
13
13
  *
14
- * to prevent someone from calling next
15
- * multiple times or forgetting to write await
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
- [NEXT_INVOKED_PROPERTY]: {
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,4 +1,5 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { ExtractProperty } from './extract-property.js';
3
4
  import { KeqContextRequestMethod } from './keq-context-request.js';
4
5
  export interface KeqOperation {
@@ -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", "../constant.js"], factory);
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 invoked = {
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 === constant_js_1.NEXT_INVOKED_PROPERTY) {
31
- return invoked;
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 (invoked.finished) {
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 (invoked.entryNextTimes > 1) {
41
+ if (metadata.entryNextTimes > 1) {
44
42
  console.warn(`next() had be invoke multiple times at ${curr.toString()} middleware`);
45
43
  }
46
- invoked.entryNextTimes += 1;
44
+ metadata.entryNextTimes += 1;
47
45
  await prev(ctx, next);
48
- invoked.outNextTimes += 1;
46
+ metadata.outNextTimes += 1;
49
47
  });
50
- invoked.finished = true;
51
- if (invoked.entryNextTimes === 0) {
48
+ metadata.finished = true;
49
+ if (metadata.entryNextTimes === 0) {
52
50
  console.warn(`next() is not invoked at ${curr.toString()}.`);
53
51
  }
54
- if (invoked.entryNextTimes !== invoked.outNextTimes) {
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 enable-background="new 0 0 430 430" height="512" viewBox="0 0 430 430" width="512" xmlns="http://www.w3.org/2000/svg"><g id="isometric"><g><path d="m270.5 279-37-16.8-185 83.7 37 16.7 74-33.4 37 16.7 37-16.7-37-16.8z" fill="#597ba1"/><path d="m196.5 379.3 37-16.7-37-16.7-37 16.7z" fill="#597ba1"/><path d="m196.5 412.8 37-16.7-37-16.8-37 16.8z" fill="#597ba1"/><path d="m233.5 329.2 37 16.7 37-16.7-37-16.8z" fill="#597ba1"/><path d="m307.5 329.2 37 16.7 37-16.7-37-16.8z" fill="#597ba1"/><g fill="#0f4c81"><path d="m196.5 312.4v33.5l37-16.7z"/><path d="m196.5 379.3 37-16.7-37-16.7z"/><path d="m196.5 412.8 37-16.7-37-16.8z"/><path d="m270.5 345.9 37-16.7-37-16.8z"/><path d="m344.5 312.4v33.5l37-16.7z"/><path d="m233.5 262.2v33.5l37-16.7z"/></g><g fill="#597ba1"><path d="m270.5 35-74 33.4 37 16.8 37-16.8z"/><path d="m196.5 101.9 37 16.7v-33.4z"/><path d="m196.5 135.4 37 16.7v-33.5z"/><path d="m196.5 168.8v41.8l37-16.7v-41.8z"/><path d="m270.5 101.9v41.8l37-16.8v-41.7z"/><path d="m344.5 101.9v41.8l37-16.8v-41.7z"/><path d="m159.5 85.2-74 33.4v41.8l74-33.5z"/></g><path d="m159.5 85.2v33.4l37-16.7z" fill="#0f4c81"/><path d="m159.5 118.6v33.5l37-16.7z" fill="#0f4c81"/><path d="m159.5 152.1v41.8l37 16.7v-41.8z" fill="#0f4c81"/><path d="m48.5 101.9v41.8l37 16.7v-41.8z" fill="#0f4c81"/><path d="m233.5 85.2v41.7l37 16.8v-41.8z" fill="#0f4c81"/><path d="m307.5 85.2v41.7l37 16.8v-41.8z" fill="#0f4c81"/><path d="m270.5 35-37-16.8-185 83.7 37 16.7 74-33.4 37 16.7 37-16.7-37-16.8z" fill="#c6d1e0"/><path d="m196.5 135.3 37-16.7-37-16.7-37 16.7z" fill="#c6d1e0"/><path d="m196.5 168.8 37-16.7-37-16.8-37 16.8z" fill="#c6d1e0"/><path d="m233.5 85.2 37 16.7 37-16.7-37-16.8z" fill="#c6d1e0"/><path d="m307.5 85.2 37 16.7 37-16.7-37-16.8z" fill="#c6d1e0"/></g></g><g id="black"><g><path d="m382.9 326.4-37-16.7c-.9-.4-2-.4-2.9 0l-35.6 16.1-35.6-16.1c-.9-.4-2-.4-2.9 0l-35.6 16.1-28.3-12.9 29.9-13.5 37-16.7c2.7-1.1 2.7-5.3 0-6.4l-37-16.7c-.9-.4-2-.4-2.9 0l-185 83.6c-2.7 1.1-2.7 5.3 0 6.4 0 0 37 16.7 37 16.7.5.2 1 .3 1.4.3s1-.1 1.4-.3l72.6-32.8 28.5 12.9-29.9 13.5c-1.3.6-2.1 1.8-2.1 3.2s.8 2.6 2.1 3.2l29.9 13.5-29.9 13.5c-2.7 1.1-2.7 5.3 0 6.4 0 0 37 16.7 37 16.7.9.4 2 .4 2.9 0l37-16.7c2.7-1.1 2.7-5.3 0-6.4l-29.9-13.5 29.9-13.5c1.3-.6 2.1-1.8 2.1-3.2s-.8-2.6-2.1-3.2l-29.9-13.6 28.5-12.9 35.6 16.1c.5.2.9.3 1.4.3s1-.1 1.4-.3l35.6-16.1 35.6 16.1c.5.2 1 .3 1.4.3s1-.1 1.4-.3l37-16.7c2.7-1.1 2.7-5.3 0-6.4zm-157.9 70.1-28.5 12.9-28.5-12.9 25.1-11.3v12.1c0 1.9 1.6 3.5 3.5 3.5s3.5-1.6 3.5-3.5v-12zm0-33.4-28.5 12.9-28.5-12.9 25.1-11.3v11.5c0 1.9 1.6 3.5 3.5 3.5s3.5-1.6 3.5-3.5v-11.4zm-28.5-20.6-35.6-16.1c-.9-.4-2-.4-2.9 0l-72.6 32.8-28.4-12.9 173-78.2v12.1c0 1.9 1.6 3.5 3.5 3.5s3.5-1.6 3.5-3.5v-12.1l25 11.3-66.9 30.3c-1.4.6-2.3 2.3-2 3.8v16.5c.1 4.6 6.9 4.6 7 0v-11.6l24.9 11.3zm74 0-28.5-12.9 25-11.3v11.7c0 1.9 1.6 3.5 3.5 3.5s3.5-1.6 3.5-3.5v-11.7l25 11.3zm74 0-28.5-12.9 25-11.3v11.7c0 1.9 1.6 3.5 3.5 3.5s3.5-1.6 3.5-3.5v-11.7l25 11.3z"/><path d="m48.5 189.7c1.7 0 3-1.3 3-3v-18.2c0-1.7-1.3-3-3-3s-3 1.3-3 3v18.2c0 1.7 1.3 3 3 3z"/><path d="m307.5 168.2c1.7 0 3-1.3 3-3v-18.2c0-1.7-1.3-3-3-3s-3 1.3-3 3v18.2c0 1.7 1.3 3 3 3z"/><path d="m384.5 147.1c0-1.7-1.3-3-3-3s-3 1.3-3 3v18.2c0 1.7 1.3 3 3 3s3-1.3 3-3z"/><path d="m344.5 220.6c1.7 0 3-1.3 3-3v-54.2c0-1.7-1.3-3-3-3s-3 1.3-3 3v54.2c0 1.6 1.3 3 3 3z"/><path d="m341.5 244.6c0 1.7 1.3 3 3 3s3-1.3 3-3v-9.1c0-1.7-1.3-3-3-3s-3 1.3-3 3z"/><path d="m85.3 239.1c1.7 0 3-1.3 3-3v-53.2c0-1.7-1.3-3-3-3s-3 1.3-3 3v53.2c0 1.7 1.4 3 3 3z"/><path d="m82.3 263.1c0 1.7 1.3 3 3 3s3-1.3 3-3v-9.1c0-1.7-1.3-3-3-3s-3 1.3-3 3z"/><circle cx="381.5" cy="182.4" r="3"/><circle cx="307.5" cy="182.4" r="3"/><path d="m48.5 206.9c1.7 0 3-1.3 3-3s-1.3-3-3-3-3 1.3-3 3 1.3 3 3 3z"/><path d="m383.6 80.6-37-16.7c-.7-.3-1.4-.4-2.1-.4s-1.4.1-2.1.4l-34.9 15.8-32-14.5v-30.2c0-2-1.2-3.7-2.9-4.6l-37-16.7c-.7-.3-1.4-.4-2.1-.4s-1.4.1-2.1.4l-185 83.6c-1.8.8-2.9 2.6-2.9 4.6v41.8c0 2 1.2 3.7 2.9 4.6l37 16.7c.7.3 1.4.4 2.1.4s1.4-.1 2.1-.4l66.9-30.3v59.2c0 2 1.2 3.7 2.9 4.6l37 16.7c.7.3 1.4.4 2.1.4s1.4-.1 2.1-.4l37-16.7c1.8-.8 2.9-2.6 2.9-4.6v-59.2l29.9 13.5c.7.3 1.4.4 2.1.4s1.4-.1 2.1-.4l34.9-15.8 34.9 15.8c.7.3 1.4.4 2.1.4s1.4-.1 2.1-.4l37-16.7c1.8-.8 2.9-2.6 2.9-4.6v-41.7c0-2-1.2-3.8-2.9-4.6zm-300.1 76.7-33-14.9v-37.4l33 14.9zm74-31.6-70 31.6v-37.4l65.6-29.6 4.4-2zm2.2-42.5c-.4-.1-.7 0-1.1.2l-73.2 33.1-32.1-14.5 180.1-81.4 32.2 14.4-70 31.6c-1.5.6-1.5 3 0 3.6l33 14.9-32.1 14.5-36.2-16.4c-.2.1-.3 0-.6 0zm4.7 35.4 32.1-14.5 32.1 14.5-32.1 14.5zm27.2 16.8-30.1 13.6v-22-5.2zm-30.1-19.9v-27.2l30.1 13.6zm33 92-33-14.9v-37.4l33 14.9zm2-40.9-32.1-14.5 32.1-14.5 32.1 14.5zm35 26-33 14.9v-37.4l33-14.9zm0-43.6-30.1-13.6 30.1-13.6zm0-33.5-30.1-13.6 30.1-13.6zm-30.1-47.1 67.1-30.4v29.1l-35 15.8zm67.1 72.2-33-14.9v-7-30.4l33 14.9zm-30.1-55.4 32.1-14.5 32.1 14.5-32.1 14.5zm67.1 40.5-33 14.9v-37.4l33-14.9zm37 14.9-33-14.9v-37.4l33 14.9zm-30.2-55.4 32.1-14.5 32.1 14.5c-1.7.8-29.5 13.4-32.1 14.5zm67.2 40.5-33 14.9v-37.4c1.4-.6 32.5-14.7 33-14.9z"/></g></g></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>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keq",
3
- "version": "2.8.0",
3
+ "version": "2.8.2",
4
4
  "description": "Request API write by Typescript for flexibility, readability, and a low learning curve.",
5
5
  "keywords": [
6
6
  "request",