keq 5.0.0-alpha.25 → 5.0.0-alpha.27

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 CHANGED
@@ -1,5 +1,21 @@
1
1
  ## 5.0.0-alpha.6 (2025-09-17)
2
2
 
3
+ ## 5.0.0-alpha.27
4
+
5
+ ### Major Changes
6
+
7
+ - 0c7db81: **BREAKING CHANGE:** remove appendMiddlewares and prependMiddlewares from Keq.
8
+
9
+ ## 5.0.0-alpha.26
10
+
11
+ ### Minor Changes
12
+
13
+ - 63161c4: **Feat:** the .flowControl() support concurrency.
14
+
15
+ ### Patch Changes
16
+
17
+ - 22ce01a: **Fix:** JSON.stringify ctx.response always get empty object.
18
+
3
19
  ## 5.0.0-alpha.25
4
20
 
5
21
  ### Major Changes
@@ -1,7 +1,12 @@
1
1
  import type { queueAsPromised } from 'fastq';
2
2
  import type { KeqNext } from '../../middleware/index.js';
3
- export interface KeqGlobal {
3
+ export interface KeqGlobalCore {
4
4
  abortFlowControl?: Record<string, ((reason: any) => void) | undefined>;
5
- serialFlowControl?: Record<string, queueAsPromised<KeqNext, void>>;
5
+ serialFlowControl?: Record<string, queueAsPromised<{
6
+ next: KeqNext;
7
+ }, void>>;
8
+ }
9
+ export interface KeqGlobal {
10
+ core?: KeqGlobalCore;
6
11
  }
7
12
  //# sourceMappingURL=keq-global.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"keq-global.d.ts","sourceRoot":"","sources":["../../../src/context/types/keq-global.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,OAAO,CAAA;AAC5C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAA;AAGpD,MAAM,WAAW,SAAS;IAExB,gBAAgB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,GAAG,KAAK,IAAI,CAAC,GAAG,SAAS,CAAC,CAAA;IACtE,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAA;CACnE"}
1
+ {"version":3,"file":"keq-global.d.ts","sourceRoot":"","sources":["../../../src/context/types/keq-global.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,OAAO,CAAA;AAC5C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAA;AAGpD,MAAM,WAAW,aAAa;IAC5B,gBAAgB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,GAAG,KAAK,IAAI,CAAC,GAAG,SAAS,CAAC,CAAA;IACtE,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC;QAAE,IAAI,EAAE,OAAO,CAAA;KAAE,EAAE,IAAI,CAAC,CAAC,CAAA;CAC7E;AAED,MAAM,WAAW,SAAS;IACxB,IAAI,CAAC,EAAE,aAAa,CAAA;CACrB"}
@@ -1 +1 @@
1
- {"version":3,"file":"fork.d.ts","sourceRoot":"","sources":["../../../src/context/utils/fork.ts"],"names":[],"mappings":"AAuBA;;GAEG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,CAAC,CAgEtC;AAED,wBAAgB,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAErC"}
1
+ {"version":3,"file":"fork.d.ts","sourceRoot":"","sources":["../../../src/context/utils/fork.ts"],"names":[],"mappings":"AAuBA;;GAEG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,GAAG,CAAC,CA4EtC;AAED,wBAAgB,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAErC"}
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ export { createRequest, Keq, request, KeqRequest, type KeqOptions, type KeqApiSc
2
2
  export { type KeqBodyInit, } from './request-init';
3
3
  export { composeMiddleware, type KeqMiddleware, type KeqNext, } from './middleware';
4
4
  export { type KeqRoute, composeRoute, } from './router';
5
- export { createProxyResponse, KeqExecutionContext, KeqSharedContext, type KeqSharedContextOptions, type KeqContext, type KeqContextData, type KeqContextEmitter, type KeqGlobal, type KeqContextOptions, type KeqMiddlewareOptions, type KeqEvents, type KeqRetryDelay, type KeqRetryOn, } from './context';
5
+ export { createProxyResponse, KeqExecutionContext, KeqSharedContext, type KeqSharedContextOptions, type KeqContext, type KeqContextData, type KeqContextEmitter, type KeqGlobal, type KeqGlobalCore, type KeqContextOptions, type KeqMiddlewareOptions, type KeqEvents, type KeqRetryDelay, type KeqRetryOn, } from './context';
6
6
  export { Exception, TypeException, TimeoutException, AbortException, RequestException, BadRequestException, UnauthorizedException, ForbiddenException, NotFoundedException, MethodNotAllowedException, NotAcceptableException, ProxyAuthenticationRequiredException, RequestTimeoutException, ConflictException, PreconditionFailedException, ContentTooLargeException, UriTooLongException, ImATeapotException, TooManyRequestsException, UnsupportedMediaTypeException, InternalServerErrorException, NotImplementedException, BadGatewayException, ServiceUnavailableException, GatewayTimeoutException, } from './exception';
7
7
  export { KeqMiddlewareOrchestrator, KeqMiddlewareExecutor, } from './orchestrator';
8
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,GAAG,EACH,OAAO,EACP,UAAU,EACV,KAAK,UAAU,EACf,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,mBAAmB,EACxB,KAAK,YAAY,EACjB,KAAK,oBAAoB,EACzB,KAAK,eAAe,EACpB,KAAK,eAAe,GACrB,MAAM,WAAW,CAAA;AAElB,OAAO,EACL,KAAK,WAAW,GACjB,MAAM,gBAAgB,CAAA;AAEvB,OAAO,EACL,iBAAiB,EACjB,KAAK,aAAa,EAClB,KAAK,OAAO,GACb,MAAM,cAAc,CAAA;AAErB,OAAO,EACL,KAAK,QAAQ,EACb,YAAY,GACb,MAAM,UAAU,CAAA;AAEjB,OAAO,EACL,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,EAChB,KAAK,uBAAuB,EAC5B,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,iBAAiB,EACtB,KAAK,SAAS,EACd,KAAK,iBAAiB,EACtB,KAAK,oBAAoB,EACzB,KAAK,SAAS,EACd,KAAK,aAAa,EAClB,KAAK,UAAU,GAChB,MAAM,WAAW,CAAA;AAElB,OAAO,EACL,SAAS,EACT,aAAa,EACb,gBAAgB,EAChB,cAAc,EAEd,gBAAgB,EAEhB,mBAAmB,EACnB,qBAAqB,EACrB,kBAAkB,EAClB,mBAAmB,EACnB,yBAAyB,EACzB,sBAAsB,EACtB,oCAAoC,EACpC,uBAAuB,EACvB,iBAAiB,EACjB,2BAA2B,EAC3B,wBAAwB,EACxB,mBAAmB,EACnB,kBAAkB,EAClB,wBAAwB,EACxB,6BAA6B,EAE7B,4BAA4B,EAC5B,uBAAuB,EACvB,mBAAmB,EACnB,2BAA2B,EAC3B,uBAAuB,GACxB,MAAM,aAAa,CAAA;AAEpB,OAAO,EACL,yBAAyB,EACzB,qBAAqB,GACtB,MAAM,gBAAgB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,GAAG,EACH,OAAO,EACP,UAAU,EACV,KAAK,UAAU,EACf,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,mBAAmB,EACxB,KAAK,YAAY,EACjB,KAAK,oBAAoB,EACzB,KAAK,eAAe,EACpB,KAAK,eAAe,GACrB,MAAM,WAAW,CAAA;AAElB,OAAO,EACL,KAAK,WAAW,GACjB,MAAM,gBAAgB,CAAA;AAEvB,OAAO,EACL,iBAAiB,EACjB,KAAK,aAAa,EAClB,KAAK,OAAO,GACb,MAAM,cAAc,CAAA;AAErB,OAAO,EACL,KAAK,QAAQ,EACb,YAAY,GACb,MAAM,UAAU,CAAA;AAEjB,OAAO,EACL,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,EAChB,KAAK,uBAAuB,EAC5B,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,iBAAiB,EACtB,KAAK,SAAS,EACd,KAAK,aAAa,EAClB,KAAK,iBAAiB,EACtB,KAAK,oBAAoB,EACzB,KAAK,SAAS,EACd,KAAK,aAAa,EAClB,KAAK,UAAU,GAChB,MAAM,WAAW,CAAA;AAElB,OAAO,EACL,SAAS,EACT,aAAa,EACb,gBAAgB,EAChB,cAAc,EAEd,gBAAgB,EAEhB,mBAAmB,EACnB,qBAAqB,EACrB,kBAAkB,EAClB,mBAAmB,EACnB,yBAAyB,EACzB,sBAAsB,EACtB,oCAAoC,EACpC,uBAAuB,EACvB,iBAAiB,EACjB,2BAA2B,EAC3B,wBAAwB,EACxB,mBAAmB,EACnB,kBAAkB,EAClB,wBAAwB,EACxB,6BAA6B,EAE7B,4BAA4B,EAC5B,uBAAuB,EACvB,mBAAmB,EACnB,2BAA2B,EAC3B,uBAAuB,GACxB,MAAM,aAAa,CAAA;AAEpB,OAAO,EACL,yBAAyB,EACzB,qBAAqB,GACtB,MAAM,gBAAgB,CAAA"}
package/dist/index.js CHANGED
@@ -790,15 +790,16 @@ function fork(original) {
790
790
  return current;
791
791
  };
792
792
  const createProxy = (path = []) => {
793
- return new Proxy({}, {
794
- get(_, prop) {
795
- const target = objectPath(current, path);
796
- if (prop === UnWrapPropertyKey) return target;
797
- const value = target[prop];
793
+ const getTarget = () => objectPath(current, path);
794
+ return new Proxy(getTarget(), {
795
+ get(target, prop) {
796
+ const realTarget = getTarget();
797
+ if (prop === UnWrapPropertyKey) return realTarget;
798
+ const value = realTarget[prop];
798
799
  if (current !== original) {
799
800
  return value;
800
801
  }
801
- if (Array.isArray(target) && ARRAY_MUTATORS.has(prop)) {
802
+ if (Array.isArray(realTarget) && ARRAY_MUTATORS.has(prop)) {
802
803
  return new Proxy(value, {
803
804
  apply(fn, thisArg, args) {
804
805
  ensureCopy();
@@ -812,15 +813,23 @@ function fork(original) {
812
813
  }
813
814
  return value;
814
815
  },
815
- set(_, prop, value) {
816
+ set(target, prop, value) {
816
817
  ensureCopy();
817
818
  objectPath(current, path)[prop] = value;
818
819
  return true;
819
820
  },
820
- deleteProperty(_, prop) {
821
+ deleteProperty(target, prop) {
821
822
  ensureCopy();
822
823
  delete objectPath(current, path)[prop];
823
824
  return true;
825
+ },
826
+ ownKeys(target) {
827
+ const realTarget = getTarget();
828
+ return Reflect.ownKeys(realTarget);
829
+ },
830
+ getOwnPropertyDescriptor(target, prop) {
831
+ const realTarget = getTarget();
832
+ return Reflect.getOwnPropertyDescriptor(realTarget, prop);
824
833
  }
825
834
  });
826
835
  };
@@ -1050,7 +1059,7 @@ function assignSharedContext(target, source) {
1050
1059
 
1051
1060
  // src/orchestrator/orchestrator.ts
1052
1061
  var KeqMiddlewareOrchestrator = class _KeqMiddlewareOrchestrator {
1053
- constructor(context, middlewares = []) {
1062
+ constructor(context, middlewares = [], inherit) {
1054
1063
  __publicField(this, "main");
1055
1064
  __publicField(this, "status", "idle");
1056
1065
  __publicField(this, "context");
@@ -1058,6 +1067,7 @@ var KeqMiddlewareOrchestrator = class _KeqMiddlewareOrchestrator {
1058
1067
  __publicField(this, "current", -1);
1059
1068
  this.context = context;
1060
1069
  this.executors = middlewares.map((mw) => new KeqMiddlewareExecutor(mw));
1070
+ if (inherit == null ? void 0 : inherit.main) this.main = inherit.main;
1061
1071
  }
1062
1072
  cancelNotFinished() {
1063
1073
  const current = this.current;
@@ -1116,11 +1126,12 @@ var KeqMiddlewareOrchestrator = class _KeqMiddlewareOrchestrator {
1116
1126
  const context = cloneSharedContext(this.context);
1117
1127
  const next = this.current + 1;
1118
1128
  const middlewares = this.executors.slice(next).map((executor) => executor.middleware);
1119
- const forkedOrchestrator = new _KeqMiddlewareOrchestrator(context, middlewares);
1120
- forkedOrchestrator.main = {
1121
- orchestrator: this.main ? this.main.orchestrator : this,
1122
- index: this.main ? this.main.index + next : next
1123
- };
1129
+ const forkedOrchestrator = new _KeqMiddlewareOrchestrator(context, middlewares, {
1130
+ main: {
1131
+ orchestrator: this.main ? this.main.orchestrator : this,
1132
+ index: this.main ? this.main.index + next : next
1133
+ }
1134
+ });
1124
1135
  return forkedOrchestrator;
1125
1136
  }
1126
1137
  merge(source) {
@@ -1313,26 +1324,30 @@ var Core = class {
1313
1324
  ...options,
1314
1325
  url: new URL(url.href)
1315
1326
  });
1327
+ if (options.middlewares) {
1328
+ this.__append_middlewares__.push(...options.middlewares);
1329
+ }
1316
1330
  }
1317
1331
  get __middlewares__() {
1318
1332
  return [...this.__prepend_middlewares__, ...this.__append_middlewares__];
1319
1333
  }
1320
- prependMiddlewares(...middlewares) {
1334
+ // prependMiddlewares(...middlewares: KeqMiddleware[]): this {
1335
+ // this.__prepend_middlewares__.push(...middlewares)
1336
+ // return this
1337
+ // }
1338
+ // /**
1339
+ // * Appends middlewares to the end of the middleware chain.
1340
+ // * Using this method indiscriminately is discouraged;
1341
+ // * prefer using `.use` to maintain predictable execution order.
1342
+ // */
1343
+ // appendMiddlewares(...middlewares: KeqMiddleware[]): this {
1344
+ // this.__append_middlewares__.unshift(...middlewares)
1345
+ // return this
1346
+ // }
1347
+ use(...middlewares) {
1321
1348
  this.__prepend_middlewares__.push(...middlewares);
1322
1349
  return this;
1323
1350
  }
1324
- /**
1325
- * Appends middlewares to the end of the middleware chain.
1326
- * Using this method indiscriminately is discouraged;
1327
- * prefer using `.use` to maintain predictable execution order.
1328
- */
1329
- appendMiddlewares(...middlewares) {
1330
- this.__append_middlewares__.unshift(...middlewares);
1331
- return this;
1332
- }
1333
- use(...middlewares) {
1334
- return this.prependMiddlewares(...middlewares);
1335
- }
1336
1351
  on(event, listener) {
1337
1352
  this.__listeners__[event] = this.__listeners__[event] || [];
1338
1353
  this.__listeners__[event].push(listener);
@@ -1599,13 +1614,15 @@ var Keq = class extends Core {
1599
1614
  this.requestInit.mode = mod;
1600
1615
  return this;
1601
1616
  }
1602
- flowControl(mode, signal) {
1603
- const sig = signal ? signal : this.__locationId__;
1617
+ flowControl(mode, arg2, arg3) {
1618
+ const concurrencyLimit = typeof arg2 === "number" ? arg2 : void 0;
1619
+ const sig = typeof arg2 === "function" || typeof arg2 === "string" ? arg2 : arg3 && (typeof arg3 === "function" || typeof arg3 === "string") ? arg3 : this.__locationId__;
1604
1620
  if (!sig) {
1605
1621
  throw new Exception("please set signal to .flowControl()");
1606
1622
  }
1607
1623
  const flowControl = {
1608
1624
  mode,
1625
+ concurrencyLimit,
1609
1626
  signal: sig
1610
1627
  };
1611
1628
  this.option("flowControl", flowControl);
@@ -1776,20 +1793,22 @@ function keqTimeoutMiddleware() {
1776
1793
  var fastq = __toESM(require("fastq"));
1777
1794
  function keqSerialFlowControlMiddleware() {
1778
1795
  return async function serialFlowControlMiddleware(ctx, next) {
1779
- if (!ctx.options.flowControl || ctx.options.flowControl.mode !== "serial") {
1796
+ if (!ctx.options.flowControl || !["serial", "concurrent"].includes(ctx.options.flowControl.mode)) {
1780
1797
  await next();
1781
1798
  return;
1782
1799
  }
1783
1800
  const { signal } = ctx.options.flowControl;
1801
+ const concurrent = ctx.options.flowControl.mode === "serial" ? 1 : !ctx.options.flowControl.concurrencyLimit ? 1 : ctx.options.flowControl.concurrencyLimit < 1 ? 1 : parseInt(ctx.options.flowControl.concurrencyLimit, 10);
1784
1802
  const key = typeof signal === "string" ? signal : signal(ctx);
1785
- if (!ctx.global.serialFlowControl) ctx.global.serialFlowControl = {};
1786
- if (!ctx.global.serialFlowControl[key]) {
1787
- ctx.global.serialFlowControl[key] = fastq.promise(async (next2) => {
1803
+ if (!ctx.global.core) ctx.global.core = {};
1804
+ if (!ctx.global.core.serialFlowControl) ctx.global.core.serialFlowControl = {};
1805
+ if (!ctx.global.core.serialFlowControl[key]) {
1806
+ ctx.global.core.serialFlowControl[key] = fastq.promise(async ({ next: next2 }) => {
1788
1807
  await next2();
1789
- }, 1);
1808
+ }, concurrent);
1790
1809
  }
1791
- const queue = ctx.global.serialFlowControl[key];
1792
- await queue.push(next);
1810
+ const queue = ctx.global.core.serialFlowControl[key];
1811
+ await queue.push({ next });
1793
1812
  };
1794
1813
  }
1795
1814
 
@@ -1802,19 +1821,20 @@ function keqAbortFlowControlMiddleware() {
1802
1821
  }
1803
1822
  const { signal } = ctx.options.flowControl;
1804
1823
  const key = typeof signal === "string" ? signal : signal(ctx);
1805
- if (!ctx.global.abortFlowControl) ctx.global.abortFlowControl = {};
1806
- const abort = ctx.global.abortFlowControl[key];
1824
+ if (!ctx.global.core) ctx.global.core = {};
1825
+ if (!ctx.global.core.abortFlowControl) ctx.global.core.abortFlowControl = {};
1826
+ const abort = ctx.global.core.abortFlowControl[key];
1807
1827
  if (abort) {
1808
1828
  const reason = new AbortException('Previous request was aborted by AbortFlowControl with key "'.concat(key, '"'));
1809
1829
  abort(reason);
1810
1830
  }
1811
1831
  const fn = ctx.request.abort.bind(ctx.request);
1812
- ctx.global.abortFlowControl[key] = fn;
1832
+ ctx.global.core.abortFlowControl[key] = fn;
1813
1833
  try {
1814
1834
  await next();
1815
1835
  } finally {
1816
- if (ctx.global.abortFlowControl[key] === fn) {
1817
- ctx.global.abortFlowControl[key] = void 0;
1836
+ if (ctx.global.core.abortFlowControl[key] === fn) {
1837
+ ctx.global.core.abortFlowControl[key] = void 0;
1818
1838
  }
1819
1839
  }
1820
1840
  };
@@ -1871,9 +1891,19 @@ var KeqRequest = class {
1871
1891
  return new URL(url.href);
1872
1892
  }
1873
1893
  __fetch__(url, init, locationId) {
1874
- const keq = new Keq(this.__formatUrl__(url), { ...init, locationId, global: this.global, qs: this.qs });
1875
- keq.appendMiddlewares(...this.postMiddlewares);
1876
- keq.prependMiddlewares(...this.preMiddlewares);
1894
+ const keq = new Keq(
1895
+ this.__formatUrl__(url),
1896
+ {
1897
+ ...init,
1898
+ locationId,
1899
+ global: this.global,
1900
+ qs: this.qs,
1901
+ middlewares: [
1902
+ ...this.preMiddlewares,
1903
+ ...this.postMiddlewares
1904
+ ]
1905
+ }
1906
+ );
1877
1907
  return keq;
1878
1908
  }
1879
1909
  fetch(url, init) {
@@ -1891,18 +1921,11 @@ var KeqRequest = class {
1891
1921
  );
1892
1922
  }
1893
1923
  put(url) {
1894
- const locationId = getLocationId(1);
1895
- const keq = new Keq(
1896
- this.__formatUrl__(url),
1897
- {
1898
- method: "put",
1899
- locationId,
1900
- global: this.global
1901
- }
1924
+ return this.__fetch__(
1925
+ url,
1926
+ { method: "put" },
1927
+ getLocationId(1)
1902
1928
  );
1903
- keq.appendMiddlewares(...this.postMiddlewares);
1904
- keq.prependMiddlewares(...this.preMiddlewares);
1905
- return keq;
1906
1929
  }
1907
1930
  delete(url) {
1908
1931
  return this.__fetch__(
@@ -1954,6 +1977,7 @@ var KeqRequest = class {
1954
1977
  context.emitter.on(event, listener);
1955
1978
  await next();
1956
1979
  };
1980
+ middleware.__keqMiddlewareName__ = "listen(".concat(event, ", ").concat(listener.name || "anonymous", ")");
1957
1981
  this.use(middleware);
1958
1982
  return this;
1959
1983
  }