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/dist/index.mjs CHANGED
@@ -721,15 +721,16 @@ function fork(original) {
721
721
  return current;
722
722
  };
723
723
  const createProxy = (path = []) => {
724
- return new Proxy({}, {
725
- get(_, prop) {
726
- const target = objectPath(current, path);
727
- if (prop === UnWrapPropertyKey) return target;
728
- const value = target[prop];
724
+ const getTarget = () => objectPath(current, path);
725
+ return new Proxy(getTarget(), {
726
+ get(target, prop) {
727
+ const realTarget = getTarget();
728
+ if (prop === UnWrapPropertyKey) return realTarget;
729
+ const value = realTarget[prop];
729
730
  if (current !== original) {
730
731
  return value;
731
732
  }
732
- if (Array.isArray(target) && ARRAY_MUTATORS.has(prop)) {
733
+ if (Array.isArray(realTarget) && ARRAY_MUTATORS.has(prop)) {
733
734
  return new Proxy(value, {
734
735
  apply(fn, thisArg, args) {
735
736
  ensureCopy();
@@ -743,15 +744,23 @@ function fork(original) {
743
744
  }
744
745
  return value;
745
746
  },
746
- set(_, prop, value) {
747
+ set(target, prop, value) {
747
748
  ensureCopy();
748
749
  objectPath(current, path)[prop] = value;
749
750
  return true;
750
751
  },
751
- deleteProperty(_, prop) {
752
+ deleteProperty(target, prop) {
752
753
  ensureCopy();
753
754
  delete objectPath(current, path)[prop];
754
755
  return true;
756
+ },
757
+ ownKeys(target) {
758
+ const realTarget = getTarget();
759
+ return Reflect.ownKeys(realTarget);
760
+ },
761
+ getOwnPropertyDescriptor(target, prop) {
762
+ const realTarget = getTarget();
763
+ return Reflect.getOwnPropertyDescriptor(realTarget, prop);
755
764
  }
756
765
  });
757
766
  };
@@ -981,7 +990,7 @@ function assignSharedContext(target, source) {
981
990
 
982
991
  // src/orchestrator/orchestrator.ts
983
992
  var KeqMiddlewareOrchestrator = class _KeqMiddlewareOrchestrator {
984
- constructor(context, middlewares = []) {
993
+ constructor(context, middlewares = [], inherit) {
985
994
  __publicField(this, "main");
986
995
  __publicField(this, "status", "idle");
987
996
  __publicField(this, "context");
@@ -989,6 +998,7 @@ var KeqMiddlewareOrchestrator = class _KeqMiddlewareOrchestrator {
989
998
  __publicField(this, "current", -1);
990
999
  this.context = context;
991
1000
  this.executors = middlewares.map((mw) => new KeqMiddlewareExecutor(mw));
1001
+ if (inherit == null ? void 0 : inherit.main) this.main = inherit.main;
992
1002
  }
993
1003
  cancelNotFinished() {
994
1004
  const current = this.current;
@@ -1047,11 +1057,12 @@ var KeqMiddlewareOrchestrator = class _KeqMiddlewareOrchestrator {
1047
1057
  const context = cloneSharedContext(this.context);
1048
1058
  const next = this.current + 1;
1049
1059
  const middlewares = this.executors.slice(next).map((executor) => executor.middleware);
1050
- const forkedOrchestrator = new _KeqMiddlewareOrchestrator(context, middlewares);
1051
- forkedOrchestrator.main = {
1052
- orchestrator: this.main ? this.main.orchestrator : this,
1053
- index: this.main ? this.main.index + next : next
1054
- };
1060
+ const forkedOrchestrator = new _KeqMiddlewareOrchestrator(context, middlewares, {
1061
+ main: {
1062
+ orchestrator: this.main ? this.main.orchestrator : this,
1063
+ index: this.main ? this.main.index + next : next
1064
+ }
1065
+ });
1055
1066
  return forkedOrchestrator;
1056
1067
  }
1057
1068
  merge(source) {
@@ -1244,26 +1255,30 @@ var Core = class {
1244
1255
  ...options,
1245
1256
  url: new URL(url.href)
1246
1257
  });
1258
+ if (options.middlewares) {
1259
+ this.__append_middlewares__.push(...options.middlewares);
1260
+ }
1247
1261
  }
1248
1262
  get __middlewares__() {
1249
1263
  return [...this.__prepend_middlewares__, ...this.__append_middlewares__];
1250
1264
  }
1251
- prependMiddlewares(...middlewares) {
1265
+ // prependMiddlewares(...middlewares: KeqMiddleware[]): this {
1266
+ // this.__prepend_middlewares__.push(...middlewares)
1267
+ // return this
1268
+ // }
1269
+ // /**
1270
+ // * Appends middlewares to the end of the middleware chain.
1271
+ // * Using this method indiscriminately is discouraged;
1272
+ // * prefer using `.use` to maintain predictable execution order.
1273
+ // */
1274
+ // appendMiddlewares(...middlewares: KeqMiddleware[]): this {
1275
+ // this.__append_middlewares__.unshift(...middlewares)
1276
+ // return this
1277
+ // }
1278
+ use(...middlewares) {
1252
1279
  this.__prepend_middlewares__.push(...middlewares);
1253
1280
  return this;
1254
1281
  }
1255
- /**
1256
- * Appends middlewares to the end of the middleware chain.
1257
- * Using this method indiscriminately is discouraged;
1258
- * prefer using `.use` to maintain predictable execution order.
1259
- */
1260
- appendMiddlewares(...middlewares) {
1261
- this.__append_middlewares__.unshift(...middlewares);
1262
- return this;
1263
- }
1264
- use(...middlewares) {
1265
- return this.prependMiddlewares(...middlewares);
1266
- }
1267
1282
  on(event, listener) {
1268
1283
  this.__listeners__[event] = this.__listeners__[event] || [];
1269
1284
  this.__listeners__[event].push(listener);
@@ -1530,13 +1545,15 @@ var Keq = class extends Core {
1530
1545
  this.requestInit.mode = mod;
1531
1546
  return this;
1532
1547
  }
1533
- flowControl(mode, signal) {
1534
- const sig = signal ? signal : this.__locationId__;
1548
+ flowControl(mode, arg2, arg3) {
1549
+ const concurrencyLimit = typeof arg2 === "number" ? arg2 : void 0;
1550
+ const sig = typeof arg2 === "function" || typeof arg2 === "string" ? arg2 : arg3 && (typeof arg3 === "function" || typeof arg3 === "string") ? arg3 : this.__locationId__;
1535
1551
  if (!sig) {
1536
1552
  throw new Exception("please set signal to .flowControl()");
1537
1553
  }
1538
1554
  const flowControl = {
1539
1555
  mode,
1556
+ concurrencyLimit,
1540
1557
  signal: sig
1541
1558
  };
1542
1559
  this.option("flowControl", flowControl);
@@ -1707,20 +1724,22 @@ function keqTimeoutMiddleware() {
1707
1724
  import * as fastq from "fastq";
1708
1725
  function keqSerialFlowControlMiddleware() {
1709
1726
  return async function serialFlowControlMiddleware(ctx, next) {
1710
- if (!ctx.options.flowControl || ctx.options.flowControl.mode !== "serial") {
1727
+ if (!ctx.options.flowControl || !["serial", "concurrent"].includes(ctx.options.flowControl.mode)) {
1711
1728
  await next();
1712
1729
  return;
1713
1730
  }
1714
1731
  const { signal } = ctx.options.flowControl;
1732
+ 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);
1715
1733
  const key = typeof signal === "string" ? signal : signal(ctx);
1716
- if (!ctx.global.serialFlowControl) ctx.global.serialFlowControl = {};
1717
- if (!ctx.global.serialFlowControl[key]) {
1718
- ctx.global.serialFlowControl[key] = fastq.promise(async (next2) => {
1734
+ if (!ctx.global.core) ctx.global.core = {};
1735
+ if (!ctx.global.core.serialFlowControl) ctx.global.core.serialFlowControl = {};
1736
+ if (!ctx.global.core.serialFlowControl[key]) {
1737
+ ctx.global.core.serialFlowControl[key] = fastq.promise(async ({ next: next2 }) => {
1719
1738
  await next2();
1720
- }, 1);
1739
+ }, concurrent);
1721
1740
  }
1722
- const queue = ctx.global.serialFlowControl[key];
1723
- await queue.push(next);
1741
+ const queue = ctx.global.core.serialFlowControl[key];
1742
+ await queue.push({ next });
1724
1743
  };
1725
1744
  }
1726
1745
 
@@ -1733,19 +1752,20 @@ function keqAbortFlowControlMiddleware() {
1733
1752
  }
1734
1753
  const { signal } = ctx.options.flowControl;
1735
1754
  const key = typeof signal === "string" ? signal : signal(ctx);
1736
- if (!ctx.global.abortFlowControl) ctx.global.abortFlowControl = {};
1737
- const abort = ctx.global.abortFlowControl[key];
1755
+ if (!ctx.global.core) ctx.global.core = {};
1756
+ if (!ctx.global.core.abortFlowControl) ctx.global.core.abortFlowControl = {};
1757
+ const abort = ctx.global.core.abortFlowControl[key];
1738
1758
  if (abort) {
1739
1759
  const reason = new AbortException('Previous request was aborted by AbortFlowControl with key "'.concat(key, '"'));
1740
1760
  abort(reason);
1741
1761
  }
1742
1762
  const fn = ctx.request.abort.bind(ctx.request);
1743
- ctx.global.abortFlowControl[key] = fn;
1763
+ ctx.global.core.abortFlowControl[key] = fn;
1744
1764
  try {
1745
1765
  await next();
1746
1766
  } finally {
1747
- if (ctx.global.abortFlowControl[key] === fn) {
1748
- ctx.global.abortFlowControl[key] = void 0;
1767
+ if (ctx.global.core.abortFlowControl[key] === fn) {
1768
+ ctx.global.core.abortFlowControl[key] = void 0;
1749
1769
  }
1750
1770
  }
1751
1771
  };
@@ -1802,9 +1822,19 @@ var KeqRequest = class {
1802
1822
  return new URL(url.href);
1803
1823
  }
1804
1824
  __fetch__(url, init, locationId) {
1805
- const keq = new Keq(this.__formatUrl__(url), { ...init, locationId, global: this.global, qs: this.qs });
1806
- keq.appendMiddlewares(...this.postMiddlewares);
1807
- keq.prependMiddlewares(...this.preMiddlewares);
1825
+ const keq = new Keq(
1826
+ this.__formatUrl__(url),
1827
+ {
1828
+ ...init,
1829
+ locationId,
1830
+ global: this.global,
1831
+ qs: this.qs,
1832
+ middlewares: [
1833
+ ...this.preMiddlewares,
1834
+ ...this.postMiddlewares
1835
+ ]
1836
+ }
1837
+ );
1808
1838
  return keq;
1809
1839
  }
1810
1840
  fetch(url, init) {
@@ -1822,18 +1852,11 @@ var KeqRequest = class {
1822
1852
  );
1823
1853
  }
1824
1854
  put(url) {
1825
- const locationId = getLocationId(1);
1826
- const keq = new Keq(
1827
- this.__formatUrl__(url),
1828
- {
1829
- method: "put",
1830
- locationId,
1831
- global: this.global
1832
- }
1855
+ return this.__fetch__(
1856
+ url,
1857
+ { method: "put" },
1858
+ getLocationId(1)
1833
1859
  );
1834
- keq.appendMiddlewares(...this.postMiddlewares);
1835
- keq.prependMiddlewares(...this.preMiddlewares);
1836
- return keq;
1837
1860
  }
1838
1861
  delete(url) {
1839
1862
  return this.__fetch__(
@@ -1885,6 +1908,7 @@ var KeqRequest = class {
1885
1908
  context.emitter.on(event, listener);
1886
1909
  await next();
1887
1910
  };
1911
+ middleware.__keqMiddlewareName__ = "listen(".concat(event, ", ").concat(listener.name || "anonymous", ")");
1888
1912
  this.use(middleware);
1889
1913
  return this;
1890
1914
  }