@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.
@@ -872,6 +872,55 @@ function optionsFromFile(opts, file) {
872
872
  opts
873
873
  );
874
874
  }
875
+ const UNSHARDED_URL_RE = /^https:\/\/([a-z0-9]+)\.api\.(sanity\..*)/, SHARDED_URL_RE = /^https:\/\/[a-z0-9]+\.api\.s(\d+)\.sanity\.(.*)/, domainSharder = getDomainSharder();
876
+ function getDomainSharder(initialBuckets) {
877
+ const buckets = new Array(10).fill(0, 0);
878
+ function incrementBucketForUrl(url) {
879
+ const shard = getShardFromUrl(url);
880
+ shard !== null && buckets[shard]++;
881
+ }
882
+ function decrementBucketForUrl(url) {
883
+ const shard = getShardFromUrl(url);
884
+ shard !== null && buckets[shard]--;
885
+ }
886
+ function getShardedUrl(url) {
887
+ const [isMatch, projectId2, rest] = url.match(UNSHARDED_URL_RE) || [];
888
+ if (!isMatch)
889
+ return url;
890
+ const bucket = buckets.reduce(
891
+ (smallest, count, index) => count < buckets[smallest] ? index : smallest,
892
+ 0
893
+ );
894
+ return `https://${projectId2}.api.s${bucket}.${rest}`;
895
+ }
896
+ function getShardFromUrl(url) {
897
+ const [isMatch, shard] = url.match(SHARDED_URL_RE) || [];
898
+ return isMatch ? parseInt(shard, 10) : null;
899
+ }
900
+ return {
901
+ middleware: {
902
+ processOptions: (options) => {
903
+ if (!useDomainSharding(options))
904
+ return options;
905
+ const url = getShardedUrl(options.url);
906
+ return options.url = url, options;
907
+ },
908
+ onRequest(req) {
909
+ return useDomainSharding(req.options) && incrementBucketForUrl(req.options.url), req;
910
+ },
911
+ onResponse(res, context) {
912
+ return useDomainSharding(context.options) && decrementBucketForUrl(context.options.url), res;
913
+ }
914
+ },
915
+ incrementBucketForUrl,
916
+ decrementBucketForUrl,
917
+ getShardedUrl,
918
+ getBuckets: () => buckets
919
+ };
920
+ }
921
+ function useDomainSharding(options) {
922
+ return "useDomainSharding" in options && options.useDomainSharding === !0;
923
+ }
875
924
  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), {});
876
925
  const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop] > "u" || (selection[prop] = obj[prop]), selection), {}), MAX_URL_LENGTH = 14800, possibleOptions = [
877
926
  "includePreviousRevision",
@@ -883,15 +932,16 @@ const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop]
883
932
  includeResult: !0
884
933
  };
885
934
  function _listen(query, params, opts = {}) {
886
- 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)}`;
887
- if (uri.length > MAX_URL_LENGTH)
935
+ 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 } });
936
+ let uri = `${url}${_getDataUrl(this, "listen", qs)}`;
937
+ if (this.config().useDomainSharding && (uri = domainSharder.getShardedUrl(uri)), uri.length > MAX_URL_LENGTH)
888
938
  return new rxjs.Observable((observer) => observer.error(new Error("Query too large for listener")));
889
939
  const listenFor = options.events ? options.events : ["mutation"], shouldEmitReconnect = listenFor.indexOf("reconnect") !== -1, esOptions = {};
890
940
  return (token || withCredentials) && (esOptions.withCredentials = !0), token && (esOptions.headers = {
891
941
  Authorization: `Bearer ${token}`
892
942
  }), new rxjs.Observable((observer) => {
893
943
  let es, reconnectTimer, stopped = !1, unsubscribed = !1;
894
- open();
944
+ domainSharder.incrementBucketForUrl(uri), open();
895
945
  function onError() {
896
946
  stopped || (emitReconnect(), !stopped && es.readyState === es.CLOSED && (unsubscribe(), clearTimeout(reconnectTimer), reconnectTimer = setTimeout(open, 100)));
897
947
  }
@@ -926,7 +976,7 @@ function _listen(query, params, opts = {}) {
926
976
  });
927
977
  }
928
978
  function stop() {
929
- stopped = !0, unsubscribe(), unsubscribed = !0;
979
+ stopped = !0, unsubscribe(), unsubscribed = !0, domainSharder.decrementBucketForUrl(uri);
930
980
  }
931
981
  return stop;
932
982
  });
@@ -1537,6 +1587,7 @@ function defineCreateClientExports(envMiddleware2, ClassConstructor) {
1537
1587
  maxRedirects: 0,
1538
1588
  maxRetries: config.maxRetries,
1539
1589
  retryDelay: config.retryDelay,
1590
+ useDomainSharding: config.useDomainSharding,
1540
1591
  ...options
1541
1592
  }),
1542
1593
  config
@@ -1547,7 +1598,7 @@ function defineDeprecatedCreateClient(createClient2) {
1547
1598
  return printNoDefaultExport(), createClient2(config);
1548
1599
  };
1549
1600
  }
1550
- var envMiddleware = [];
1601
+ var envMiddleware = [domainSharder.middleware];
1551
1602
  const exp = defineCreateClientExports(envMiddleware, SanityClient), requester = exp.requester, createClient = exp.createClient, deprecatedCreateClient = defineDeprecatedCreateClient(createClient);
1552
1603
  Object.defineProperty(exports, "unstable__adapter", {
1553
1604
  enumerable: !0,