@sanity/client 6.20.1 → 6.20.2-beta.0

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.d.cts CHANGED
@@ -336,6 +336,13 @@ export declare interface ClientConfig {
336
336
  apiHost?: string
337
337
  apiVersion?: string
338
338
  proxy?: string
339
+ /**
340
+ * Spread the requests over a number of hostnames to work around HTTP/1.1 limitations.
341
+ * Only applicable in browsers, and for certain allowed projects.
342
+ *
343
+ * @alpha
344
+ */
345
+ useDomainSharding?: boolean
339
346
  /**
340
347
  * Optional request tag prefix for all request tags
341
348
  */
package/dist/index.d.ts CHANGED
@@ -336,6 +336,13 @@ export declare interface ClientConfig {
336
336
  apiHost?: string
337
337
  apiVersion?: string
338
338
  proxy?: string
339
+ /**
340
+ * Spread the requests over a number of hostnames to work around HTTP/1.1 limitations.
341
+ * Only applicable in browsers, and for certain allowed projects.
342
+ *
343
+ * @alpha
344
+ */
345
+ useDomainSharding?: boolean
339
346
  /**
340
347
  * Optional request tag prefix for all request tags
341
348
  */
package/dist/index.js CHANGED
@@ -855,6 +855,55 @@ function optionsFromFile(opts, file) {
855
855
  opts
856
856
  );
857
857
  }
858
+ const UNSHARDED_URL_RE = /^https:\/\/([a-z0-9]+)\.api\.(sanity\..*)/, SHARDED_URL_RE = /^https:\/\/[a-z0-9]+\.api\.s(\d+)\.sanity\.(.*)/, domainSharder = getDomainSharder();
859
+ function getDomainSharder(initialBuckets) {
860
+ const buckets = new Array(10).fill(0, 0);
861
+ function incrementBucketForUrl(url) {
862
+ const shard = getShardFromUrl(url);
863
+ shard !== null && buckets[shard]++;
864
+ }
865
+ function decrementBucketForUrl(url) {
866
+ const shard = getShardFromUrl(url);
867
+ shard !== null && buckets[shard]--;
868
+ }
869
+ function getShardedUrl(url) {
870
+ const [isMatch, projectId2, rest] = url.match(UNSHARDED_URL_RE) || [];
871
+ if (!isMatch)
872
+ return url;
873
+ const bucket = buckets.reduce(
874
+ (smallest, count, index) => count < buckets[smallest] ? index : smallest,
875
+ 0
876
+ );
877
+ return `https://${projectId2}.api.s${bucket}.${rest}`;
878
+ }
879
+ function getShardFromUrl(url) {
880
+ const [isMatch, shard] = url.match(SHARDED_URL_RE) || [];
881
+ return isMatch ? parseInt(shard, 10) : null;
882
+ }
883
+ return {
884
+ middleware: {
885
+ processOptions: (options) => {
886
+ if (!useDomainSharding(options))
887
+ return options;
888
+ const url = getShardedUrl(options.url);
889
+ return options.url = url, options;
890
+ },
891
+ onRequest(req) {
892
+ return useDomainSharding(req.options) && incrementBucketForUrl(req.options.url), req;
893
+ },
894
+ onResponse(res, context) {
895
+ return useDomainSharding(context.options) && decrementBucketForUrl(context.options.url), res;
896
+ }
897
+ },
898
+ incrementBucketForUrl,
899
+ decrementBucketForUrl,
900
+ getShardedUrl,
901
+ getBuckets: () => buckets
902
+ };
903
+ }
904
+ function useDomainSharding(options) {
905
+ return "useDomainSharding" in options && options.useDomainSharding === !0;
906
+ }
858
907
  var defaults = (obj, defaults2) => Object.keys(defaults2).concat(Object.keys(obj)).reduce((target, prop) => (target[prop] = typeof obj[prop] > "u" ? defaults2[prop] : obj[prop], target), {});
859
908
  const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop] > "u" || (selection[prop] = obj[prop]), selection), {}), MAX_URL_LENGTH = 14800, possibleOptions = [
860
909
  "includePreviousRevision",
@@ -866,15 +915,16 @@ const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop]
866
915
  includeResult: !0
867
916
  };
868
917
  function _listen(query, params, opts = {}) {
869
- const { url, token, withCredentials, requestTagPrefix } = this.config(), tag = opts.tag && requestTagPrefix ? [requestTagPrefix, opts.tag].join(".") : opts.tag, options = { ...defaults(opts, defaultOptions), tag }, listenOpts = pick(options, possibleOptions), qs = encodeQueryString({ query, params, options: { tag, ...listenOpts } }), uri = `${url}${_getDataUrl(this, "listen", qs)}`;
870
- if (uri.length > MAX_URL_LENGTH)
918
+ const { url, token, withCredentials, requestTagPrefix } = this.config(), tag = opts.tag && requestTagPrefix ? [requestTagPrefix, opts.tag].join(".") : opts.tag, options = { ...defaults(opts, defaultOptions), tag }, listenOpts = pick(options, possibleOptions), qs = encodeQueryString({ query, params, options: { tag, ...listenOpts } });
919
+ let uri = `${url}${_getDataUrl(this, "listen", qs)}`;
920
+ if (this.config().useDomainSharding && (uri = domainSharder.getShardedUrl(uri)), uri.length > MAX_URL_LENGTH)
871
921
  return new Observable((observer) => observer.error(new Error("Query too large for listener")));
872
922
  const listenFor = options.events ? options.events : ["mutation"], shouldEmitReconnect = listenFor.indexOf("reconnect") !== -1, esOptions = {};
873
923
  return (token || withCredentials) && (esOptions.withCredentials = !0), token && (esOptions.headers = {
874
924
  Authorization: `Bearer ${token}`
875
925
  }), new Observable((observer) => {
876
926
  let es, reconnectTimer, stopped = !1, unsubscribed = !1;
877
- open();
927
+ domainSharder.incrementBucketForUrl(uri), open();
878
928
  function onError() {
879
929
  stopped || (emitReconnect(), !stopped && es.readyState === es.CLOSED && (unsubscribe(), clearTimeout(reconnectTimer), reconnectTimer = setTimeout(open, 100)));
880
930
  }
@@ -909,7 +959,7 @@ function _listen(query, params, opts = {}) {
909
959
  });
910
960
  }
911
961
  function stop() {
912
- stopped = !0, unsubscribe(), unsubscribed = !0;
962
+ stopped = !0, unsubscribe(), unsubscribed = !0, domainSharder.decrementBucketForUrl(uri);
913
963
  }
914
964
  return stop;
915
965
  });
@@ -1520,6 +1570,7 @@ function defineCreateClientExports(envMiddleware, ClassConstructor) {
1520
1570
  maxRedirects: 0,
1521
1571
  maxRetries: config.maxRetries,
1522
1572
  retryDelay: config.retryDelay,
1573
+ useDomainSharding: config.useDomainSharding,
1523
1574
  ...options
1524
1575
  }),
1525
1576
  config
@@ -1530,8 +1581,9 @@ function defineDeprecatedCreateClient(createClient2) {
1530
1581
  return printNoDefaultExport(), createClient2(config);
1531
1582
  };
1532
1583
  }
1533
- var name = "@sanity/client", version = "6.20.1";
1584
+ var name = "@sanity/client", version = "6.20.2-beta.0";
1534
1585
  const middleware = [
1586
+ domainSharder.middleware,
1535
1587
  debug({ verbose: !0, namespace: "sanity:client" }),
1536
1588
  headers({ "User-Agent": `${name} ${version}` }),
1537
1589
  // Enable keep-alive, and in addition limit the number of sockets that can be opened.