@sanity/client 6.20.1 → 6.20.2-beta.1

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.
@@ -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
  */
@@ -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
  */
@@ -586,6 +586,7 @@ function requestOptions(config, overrides = {}) {
586
586
  proxy: overrides.proxy || config.proxy,
587
587
  json: !0,
588
588
  withCredentials,
589
+ useDomainSharding: config.useDomainSharding,
589
590
  fetch: typeof overrides.fetch == "object" && typeof config.fetch == "object" ? { ...config.fetch, ...overrides.fetch } : overrides.fetch || config.fetch
590
591
  });
591
592
  }
@@ -855,6 +856,55 @@ function optionsFromFile(opts, file) {
855
856
  opts
856
857
  );
857
858
  }
859
+ const UNSHARDED_URL_RE = /^https:\/\/([a-z0-9]+)\.api\.(sanity\..*)/, SHARDED_URL_RE = /^https:\/\/[a-z0-9]+\.api\.s(\d+)\.sanity\.(.*)/, domainSharder = getDomainSharder();
860
+ function getDomainSharder(initialBuckets) {
861
+ const buckets = new Array(10).fill(0, 0);
862
+ function incrementBucketForUrl(url) {
863
+ const shard = getShardFromUrl(url);
864
+ shard !== null && buckets[shard]++;
865
+ }
866
+ function decrementBucketForUrl(url) {
867
+ const shard = getShardFromUrl(url);
868
+ shard !== null && buckets[shard]--;
869
+ }
870
+ function getShardedUrl(url) {
871
+ const [isMatch, projectId2, rest] = url.match(UNSHARDED_URL_RE) || [];
872
+ if (!isMatch)
873
+ return url;
874
+ const bucket = buckets.reduce(
875
+ (smallest, count, index) => count < buckets[smallest] ? index : smallest,
876
+ 0
877
+ );
878
+ return `https://${projectId2}.api.s${bucket}.${rest}`;
879
+ }
880
+ function getShardFromUrl(url) {
881
+ const [isMatch, shard] = url.match(SHARDED_URL_RE) || [];
882
+ return isMatch ? parseInt(shard, 10) : null;
883
+ }
884
+ return {
885
+ middleware: {
886
+ processOptions: (options) => {
887
+ if (!useDomainSharding(options))
888
+ return options;
889
+ const url = getShardedUrl(options.url);
890
+ return options.url = url, options;
891
+ },
892
+ onRequest(req) {
893
+ return useDomainSharding(req.options) && incrementBucketForUrl(req.options.url), req;
894
+ },
895
+ onResponse(res, context) {
896
+ return useDomainSharding(context.options) && decrementBucketForUrl(context.options.url), res;
897
+ }
898
+ },
899
+ incrementBucketForUrl,
900
+ decrementBucketForUrl,
901
+ getShardedUrl,
902
+ getBuckets: () => buckets
903
+ };
904
+ }
905
+ function useDomainSharding(options) {
906
+ return "useDomainSharding" in options && options.useDomainSharding === !0;
907
+ }
858
908
  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
909
  const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop] > "u" || (selection[prop] = obj[prop]), selection), {}), MAX_URL_LENGTH = 14800, possibleOptions = [
860
910
  "includePreviousRevision",
@@ -866,15 +916,16 @@ const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop]
866
916
  includeResult: !0
867
917
  };
868
918
  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)
919
+ 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 } });
920
+ let uri = `${url}${_getDataUrl(this, "listen", qs)}`;
921
+ if (this.config().useDomainSharding && (uri = domainSharder.getShardedUrl(uri)), uri.length > MAX_URL_LENGTH)
871
922
  return new Observable((observer) => observer.error(new Error("Query too large for listener")));
872
923
  const listenFor = options.events ? options.events : ["mutation"], shouldEmitReconnect = listenFor.indexOf("reconnect") !== -1, esOptions = {};
873
924
  return (token || withCredentials) && (esOptions.withCredentials = !0), token && (esOptions.headers = {
874
925
  Authorization: `Bearer ${token}`
875
926
  }), new Observable((observer) => {
876
927
  let es, reconnectTimer, stopped = !1, unsubscribed = !1;
877
- open();
928
+ domainSharder.incrementBucketForUrl(uri), open();
878
929
  function onError() {
879
930
  stopped || (emitReconnect(), !stopped && es.readyState === es.CLOSED && (unsubscribe(), clearTimeout(reconnectTimer), reconnectTimer = setTimeout(open, 100)));
880
931
  }
@@ -909,7 +960,7 @@ function _listen(query, params, opts = {}) {
909
960
  });
910
961
  }
911
962
  function stop() {
912
- stopped = !0, unsubscribe(), unsubscribed = !0;
963
+ stopped = !0, unsubscribe(), unsubscribed = !0, domainSharder.decrementBucketForUrl(uri);
913
964
  }
914
965
  return stop;
915
966
  });
@@ -1530,7 +1581,7 @@ function defineDeprecatedCreateClient(createClient2) {
1530
1581
  return printNoDefaultExport(), createClient2(config);
1531
1582
  };
1532
1583
  }
1533
- var envMiddleware = [];
1584
+ var envMiddleware = [domainSharder.middleware];
1534
1585
  const exp = defineCreateClientExports(envMiddleware, SanityClient), requester = exp.requester, createClient = exp.createClient, deprecatedCreateClient = defineDeprecatedCreateClient(createClient);
1535
1586
  export {
1536
1587
  BasePatch,