@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.
- package/dist/index.browser.cjs +56 -5
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +7 -0
- package/dist/index.browser.d.ts +7 -0
- package/dist/index.browser.js +56 -5
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +57 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +57 -5
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/data/listen.ts +13 -1
- package/src/http/browserMiddleware.ts +3 -1
- package/src/http/domainSharding.ts +96 -0
- package/src/http/nodeMiddleware.ts +3 -0
- package/src/http/requestOptions.ts +1 -0
- package/src/types.ts +8 -0
- package/umd/sanityClient.js +56 -5
- package/umd/sanityClient.min.js +3 -3
package/dist/index.browser.cjs
CHANGED
|
@@ -601,6 +601,7 @@ function requestOptions(config, overrides = {}) {
|
|
|
601
601
|
proxy: overrides.proxy || config.proxy,
|
|
602
602
|
json: !0,
|
|
603
603
|
withCredentials,
|
|
604
|
+
useDomainSharding: config.useDomainSharding,
|
|
604
605
|
fetch: typeof overrides.fetch == "object" && typeof config.fetch == "object" ? { ...config.fetch, ...overrides.fetch } : overrides.fetch || config.fetch
|
|
605
606
|
});
|
|
606
607
|
}
|
|
@@ -872,6 +873,55 @@ function optionsFromFile(opts, file) {
|
|
|
872
873
|
opts
|
|
873
874
|
);
|
|
874
875
|
}
|
|
876
|
+
const UNSHARDED_URL_RE = /^https:\/\/([a-z0-9]+)\.api\.(sanity\..*)/, SHARDED_URL_RE = /^https:\/\/[a-z0-9]+\.api\.s(\d+)\.sanity\.(.*)/, domainSharder = getDomainSharder();
|
|
877
|
+
function getDomainSharder(initialBuckets) {
|
|
878
|
+
const buckets = new Array(10).fill(0, 0);
|
|
879
|
+
function incrementBucketForUrl(url) {
|
|
880
|
+
const shard = getShardFromUrl(url);
|
|
881
|
+
shard !== null && buckets[shard]++;
|
|
882
|
+
}
|
|
883
|
+
function decrementBucketForUrl(url) {
|
|
884
|
+
const shard = getShardFromUrl(url);
|
|
885
|
+
shard !== null && buckets[shard]--;
|
|
886
|
+
}
|
|
887
|
+
function getShardedUrl(url) {
|
|
888
|
+
const [isMatch, projectId2, rest] = url.match(UNSHARDED_URL_RE) || [];
|
|
889
|
+
if (!isMatch)
|
|
890
|
+
return url;
|
|
891
|
+
const bucket = buckets.reduce(
|
|
892
|
+
(smallest, count, index) => count < buckets[smallest] ? index : smallest,
|
|
893
|
+
0
|
|
894
|
+
);
|
|
895
|
+
return `https://${projectId2}.api.s${bucket}.${rest}`;
|
|
896
|
+
}
|
|
897
|
+
function getShardFromUrl(url) {
|
|
898
|
+
const [isMatch, shard] = url.match(SHARDED_URL_RE) || [];
|
|
899
|
+
return isMatch ? parseInt(shard, 10) : null;
|
|
900
|
+
}
|
|
901
|
+
return {
|
|
902
|
+
middleware: {
|
|
903
|
+
processOptions: (options) => {
|
|
904
|
+
if (!useDomainSharding(options))
|
|
905
|
+
return options;
|
|
906
|
+
const url = getShardedUrl(options.url);
|
|
907
|
+
return options.url = url, options;
|
|
908
|
+
},
|
|
909
|
+
onRequest(req) {
|
|
910
|
+
return useDomainSharding(req.options) && incrementBucketForUrl(req.options.url), req;
|
|
911
|
+
},
|
|
912
|
+
onResponse(res, context) {
|
|
913
|
+
return useDomainSharding(context.options) && decrementBucketForUrl(context.options.url), res;
|
|
914
|
+
}
|
|
915
|
+
},
|
|
916
|
+
incrementBucketForUrl,
|
|
917
|
+
decrementBucketForUrl,
|
|
918
|
+
getShardedUrl,
|
|
919
|
+
getBuckets: () => buckets
|
|
920
|
+
};
|
|
921
|
+
}
|
|
922
|
+
function useDomainSharding(options) {
|
|
923
|
+
return "useDomainSharding" in options && options.useDomainSharding === !0;
|
|
924
|
+
}
|
|
875
925
|
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
926
|
const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop] > "u" || (selection[prop] = obj[prop]), selection), {}), MAX_URL_LENGTH = 14800, possibleOptions = [
|
|
877
927
|
"includePreviousRevision",
|
|
@@ -883,15 +933,16 @@ const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop]
|
|
|
883
933
|
includeResult: !0
|
|
884
934
|
};
|
|
885
935
|
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 } })
|
|
887
|
-
|
|
936
|
+
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 } });
|
|
937
|
+
let uri = `${url}${_getDataUrl(this, "listen", qs)}`;
|
|
938
|
+
if (this.config().useDomainSharding && (uri = domainSharder.getShardedUrl(uri)), uri.length > MAX_URL_LENGTH)
|
|
888
939
|
return new rxjs.Observable((observer) => observer.error(new Error("Query too large for listener")));
|
|
889
940
|
const listenFor = options.events ? options.events : ["mutation"], shouldEmitReconnect = listenFor.indexOf("reconnect") !== -1, esOptions = {};
|
|
890
941
|
return (token || withCredentials) && (esOptions.withCredentials = !0), token && (esOptions.headers = {
|
|
891
942
|
Authorization: `Bearer ${token}`
|
|
892
943
|
}), new rxjs.Observable((observer) => {
|
|
893
944
|
let es, reconnectTimer, stopped = !1, unsubscribed = !1;
|
|
894
|
-
open();
|
|
945
|
+
domainSharder.incrementBucketForUrl(uri), open();
|
|
895
946
|
function onError() {
|
|
896
947
|
stopped || (emitReconnect(), !stopped && es.readyState === es.CLOSED && (unsubscribe(), clearTimeout(reconnectTimer), reconnectTimer = setTimeout(open, 100)));
|
|
897
948
|
}
|
|
@@ -926,7 +977,7 @@ function _listen(query, params, opts = {}) {
|
|
|
926
977
|
});
|
|
927
978
|
}
|
|
928
979
|
function stop() {
|
|
929
|
-
stopped = !0, unsubscribe(), unsubscribed = !0;
|
|
980
|
+
stopped = !0, unsubscribe(), unsubscribed = !0, domainSharder.decrementBucketForUrl(uri);
|
|
930
981
|
}
|
|
931
982
|
return stop;
|
|
932
983
|
});
|
|
@@ -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,
|