keq 5.0.0-alpha.26 → 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
@@ -990,7 +990,7 @@ function assignSharedContext(target, source) {
990
990
 
991
991
  // src/orchestrator/orchestrator.ts
992
992
  var KeqMiddlewareOrchestrator = class _KeqMiddlewareOrchestrator {
993
- constructor(context, middlewares = []) {
993
+ constructor(context, middlewares = [], inherit) {
994
994
  __publicField(this, "main");
995
995
  __publicField(this, "status", "idle");
996
996
  __publicField(this, "context");
@@ -998,6 +998,7 @@ var KeqMiddlewareOrchestrator = class _KeqMiddlewareOrchestrator {
998
998
  __publicField(this, "current", -1);
999
999
  this.context = context;
1000
1000
  this.executors = middlewares.map((mw) => new KeqMiddlewareExecutor(mw));
1001
+ if (inherit == null ? void 0 : inherit.main) this.main = inherit.main;
1001
1002
  }
1002
1003
  cancelNotFinished() {
1003
1004
  const current = this.current;
@@ -1056,11 +1057,12 @@ var KeqMiddlewareOrchestrator = class _KeqMiddlewareOrchestrator {
1056
1057
  const context = cloneSharedContext(this.context);
1057
1058
  const next = this.current + 1;
1058
1059
  const middlewares = this.executors.slice(next).map((executor) => executor.middleware);
1059
- const forkedOrchestrator = new _KeqMiddlewareOrchestrator(context, middlewares);
1060
- forkedOrchestrator.main = {
1061
- orchestrator: this.main ? this.main.orchestrator : this,
1062
- index: this.main ? this.main.index + next : next
1063
- };
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
+ });
1064
1066
  return forkedOrchestrator;
1065
1067
  }
1066
1068
  merge(source) {
@@ -1253,26 +1255,30 @@ var Core = class {
1253
1255
  ...options,
1254
1256
  url: new URL(url.href)
1255
1257
  });
1258
+ if (options.middlewares) {
1259
+ this.__append_middlewares__.push(...options.middlewares);
1260
+ }
1256
1261
  }
1257
1262
  get __middlewares__() {
1258
1263
  return [...this.__prepend_middlewares__, ...this.__append_middlewares__];
1259
1264
  }
1260
- 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) {
1261
1279
  this.__prepend_middlewares__.push(...middlewares);
1262
1280
  return this;
1263
1281
  }
1264
- /**
1265
- * Appends middlewares to the end of the middleware chain.
1266
- * Using this method indiscriminately is discouraged;
1267
- * prefer using `.use` to maintain predictable execution order.
1268
- */
1269
- appendMiddlewares(...middlewares) {
1270
- this.__append_middlewares__.unshift(...middlewares);
1271
- return this;
1272
- }
1273
- use(...middlewares) {
1274
- return this.prependMiddlewares(...middlewares);
1275
- }
1276
1282
  on(event, listener) {
1277
1283
  this.__listeners__[event] = this.__listeners__[event] || [];
1278
1284
  this.__listeners__[event].push(listener);
@@ -1725,14 +1731,15 @@ function keqSerialFlowControlMiddleware() {
1725
1731
  const { signal } = ctx.options.flowControl;
1726
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);
1727
1733
  const key = typeof signal === "string" ? signal : signal(ctx);
1728
- if (!ctx.global.serialFlowControl) ctx.global.serialFlowControl = {};
1729
- if (!ctx.global.serialFlowControl[key]) {
1730
- 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 }) => {
1731
1738
  await next2();
1732
1739
  }, concurrent);
1733
1740
  }
1734
- const queue = ctx.global.serialFlowControl[key];
1735
- await queue.push(next);
1741
+ const queue = ctx.global.core.serialFlowControl[key];
1742
+ await queue.push({ next });
1736
1743
  };
1737
1744
  }
1738
1745
 
@@ -1745,19 +1752,20 @@ function keqAbortFlowControlMiddleware() {
1745
1752
  }
1746
1753
  const { signal } = ctx.options.flowControl;
1747
1754
  const key = typeof signal === "string" ? signal : signal(ctx);
1748
- if (!ctx.global.abortFlowControl) ctx.global.abortFlowControl = {};
1749
- 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];
1750
1758
  if (abort) {
1751
1759
  const reason = new AbortException('Previous request was aborted by AbortFlowControl with key "'.concat(key, '"'));
1752
1760
  abort(reason);
1753
1761
  }
1754
1762
  const fn = ctx.request.abort.bind(ctx.request);
1755
- ctx.global.abortFlowControl[key] = fn;
1763
+ ctx.global.core.abortFlowControl[key] = fn;
1756
1764
  try {
1757
1765
  await next();
1758
1766
  } finally {
1759
- if (ctx.global.abortFlowControl[key] === fn) {
1760
- ctx.global.abortFlowControl[key] = void 0;
1767
+ if (ctx.global.core.abortFlowControl[key] === fn) {
1768
+ ctx.global.core.abortFlowControl[key] = void 0;
1761
1769
  }
1762
1770
  }
1763
1771
  };
@@ -1814,9 +1822,19 @@ var KeqRequest = class {
1814
1822
  return new URL(url.href);
1815
1823
  }
1816
1824
  __fetch__(url, init, locationId) {
1817
- const keq = new Keq(this.__formatUrl__(url), { ...init, locationId, global: this.global, qs: this.qs });
1818
- keq.appendMiddlewares(...this.postMiddlewares);
1819
- 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
+ );
1820
1838
  return keq;
1821
1839
  }
1822
1840
  fetch(url, init) {
@@ -1834,18 +1852,11 @@ var KeqRequest = class {
1834
1852
  );
1835
1853
  }
1836
1854
  put(url) {
1837
- const locationId = getLocationId(1);
1838
- const keq = new Keq(
1839
- this.__formatUrl__(url),
1840
- {
1841
- method: "put",
1842
- locationId,
1843
- global: this.global
1844
- }
1855
+ return this.__fetch__(
1856
+ url,
1857
+ { method: "put" },
1858
+ getLocationId(1)
1845
1859
  );
1846
- keq.appendMiddlewares(...this.postMiddlewares);
1847
- keq.prependMiddlewares(...this.preMiddlewares);
1848
- return keq;
1849
1860
  }
1850
1861
  delete(url) {
1851
1862
  return this.__fetch__(