@sanity/client 6.20.0 → 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.browser.cjs +61 -7
- 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 +61 -7
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +62 -7
- 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 +62 -7
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/config.ts +5 -0
- package/src/data/listen.ts +13 -1
- package/src/defineCreateClient.ts +1 -0
- package/src/http/browserMiddleware.ts +3 -1
- package/src/http/domainSharding.ts +96 -0
- package/src/http/nodeMiddleware.ts +3 -0
- package/src/types.ts +8 -0
- package/src/warnings.ts +5 -0
- package/umd/sanityClient.js +75 -26
- package/umd/sanityClient.min.js +3 -3
package/dist/index.browser.cjs
CHANGED
|
@@ -506,7 +506,10 @@ function once(fn) {
|
|
|
506
506
|
const createWarningPrinter = (message) => (
|
|
507
507
|
// eslint-disable-next-line no-console
|
|
508
508
|
once((...args) => console.warn(message.join(" "), ...args))
|
|
509
|
-
),
|
|
509
|
+
), printCdnAndWithCredentialsWarning = createWarningPrinter([
|
|
510
|
+
"Because you set `withCredentials` to true, we will override your `useCdn`",
|
|
511
|
+
"setting to be false since (cookie-based) credentials are never set on the CDN"
|
|
512
|
+
]), printCdnWarning = createWarningPrinter([
|
|
510
513
|
"Since you haven't set a value for `useCdn`, we will deliver content using our",
|
|
511
514
|
"global, edge-cached API-CDN. If you wish to have content delivered faster, set",
|
|
512
515
|
"`useCdn: false` to use the Live API. Note: You may incur higher costs using the live API."
|
|
@@ -584,7 +587,7 @@ const validateApiPerspective = function(perspective) {
|
|
|
584
587
|
`stega.studioUrl must be a string or a function, received ${newConfig.stega.studioUrl}`
|
|
585
588
|
);
|
|
586
589
|
const isBrowser = typeof window < "u" && window.location && window.location.hostname, isLocalhost = isBrowser && isLocal(window.location.hostname);
|
|
587
|
-
isBrowser && isLocalhost && newConfig.token && newConfig.ignoreBrowserTokenWarning !== !0 ? printBrowserTokenWarning() : typeof newConfig.useCdn > "u" && printCdnWarning(), projectBased && projectId(newConfig.projectId), newConfig.dataset && dataset(newConfig.dataset), "requestTagPrefix" in newConfig && (newConfig.requestTagPrefix = newConfig.requestTagPrefix ? requestTag(newConfig.requestTagPrefix).replace(/\.+$/, "") : void 0), newConfig.apiVersion = `${newConfig.apiVersion}`.replace(/^v/, ""), newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost, newConfig.useCdn = newConfig.useCdn !== !1 && !newConfig.withCredentials, validateApiVersion(newConfig.apiVersion);
|
|
590
|
+
isBrowser && isLocalhost && newConfig.token && newConfig.ignoreBrowserTokenWarning !== !0 ? printBrowserTokenWarning() : typeof newConfig.useCdn > "u" && printCdnWarning(), projectBased && projectId(newConfig.projectId), newConfig.dataset && dataset(newConfig.dataset), "requestTagPrefix" in newConfig && (newConfig.requestTagPrefix = newConfig.requestTagPrefix ? requestTag(newConfig.requestTagPrefix).replace(/\.+$/, "") : void 0), newConfig.apiVersion = `${newConfig.apiVersion}`.replace(/^v/, ""), newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost, newConfig.useCdn === !0 && newConfig.withCredentials && printCdnAndWithCredentialsWarning(), newConfig.useCdn = newConfig.useCdn !== !1 && !newConfig.withCredentials, validateApiVersion(newConfig.apiVersion);
|
|
588
591
|
const hostParts = newConfig.apiHost.split("://", 2), protocol = hostParts[0], host = hostParts[1], cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
|
|
589
592
|
return newConfig.useProjectHostname ? (newConfig.url = `${protocol}://${newConfig.projectId}.${host}/v${newConfig.apiVersion}`, newConfig.cdnUrl = `${protocol}://${newConfig.projectId}.${cdnHost}/v${newConfig.apiVersion}`) : (newConfig.url = `${newConfig.apiHost}/v${newConfig.apiVersion}`, newConfig.cdnUrl = newConfig.url), newConfig;
|
|
590
593
|
}, projectHeader = "X-Sanity-Project-ID";
|
|
@@ -869,6 +872,55 @@ function optionsFromFile(opts, file) {
|
|
|
869
872
|
opts
|
|
870
873
|
);
|
|
871
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
|
+
}
|
|
872
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), {});
|
|
873
925
|
const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop] > "u" || (selection[prop] = obj[prop]), selection), {}), MAX_URL_LENGTH = 14800, possibleOptions = [
|
|
874
926
|
"includePreviousRevision",
|
|
@@ -880,15 +932,16 @@ const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop]
|
|
|
880
932
|
includeResult: !0
|
|
881
933
|
};
|
|
882
934
|
function _listen(query, params, opts = {}) {
|
|
883
|
-
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 } })
|
|
884
|
-
|
|
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)
|
|
885
938
|
return new rxjs.Observable((observer) => observer.error(new Error("Query too large for listener")));
|
|
886
939
|
const listenFor = options.events ? options.events : ["mutation"], shouldEmitReconnect = listenFor.indexOf("reconnect") !== -1, esOptions = {};
|
|
887
940
|
return (token || withCredentials) && (esOptions.withCredentials = !0), token && (esOptions.headers = {
|
|
888
941
|
Authorization: `Bearer ${token}`
|
|
889
942
|
}), new rxjs.Observable((observer) => {
|
|
890
943
|
let es, reconnectTimer, stopped = !1, unsubscribed = !1;
|
|
891
|
-
open();
|
|
944
|
+
domainSharder.incrementBucketForUrl(uri), open();
|
|
892
945
|
function onError() {
|
|
893
946
|
stopped || (emitReconnect(), !stopped && es.readyState === es.CLOSED && (unsubscribe(), clearTimeout(reconnectTimer), reconnectTimer = setTimeout(open, 100)));
|
|
894
947
|
}
|
|
@@ -923,7 +976,7 @@ function _listen(query, params, opts = {}) {
|
|
|
923
976
|
});
|
|
924
977
|
}
|
|
925
978
|
function stop() {
|
|
926
|
-
stopped = !0, unsubscribe(), unsubscribed = !0;
|
|
979
|
+
stopped = !0, unsubscribe(), unsubscribed = !0, domainSharder.decrementBucketForUrl(uri);
|
|
927
980
|
}
|
|
928
981
|
return stop;
|
|
929
982
|
});
|
|
@@ -1534,6 +1587,7 @@ function defineCreateClientExports(envMiddleware2, ClassConstructor) {
|
|
|
1534
1587
|
maxRedirects: 0,
|
|
1535
1588
|
maxRetries: config.maxRetries,
|
|
1536
1589
|
retryDelay: config.retryDelay,
|
|
1590
|
+
useDomainSharding: config.useDomainSharding,
|
|
1537
1591
|
...options
|
|
1538
1592
|
}),
|
|
1539
1593
|
config
|
|
@@ -1544,7 +1598,7 @@ function defineDeprecatedCreateClient(createClient2) {
|
|
|
1544
1598
|
return printNoDefaultExport(), createClient2(config);
|
|
1545
1599
|
};
|
|
1546
1600
|
}
|
|
1547
|
-
var envMiddleware = [];
|
|
1601
|
+
var envMiddleware = [domainSharder.middleware];
|
|
1548
1602
|
const exp = defineCreateClientExports(envMiddleware, SanityClient), requester = exp.requester, createClient = exp.createClient, deprecatedCreateClient = defineDeprecatedCreateClient(createClient);
|
|
1549
1603
|
Object.defineProperty(exports, "unstable__adapter", {
|
|
1550
1604
|
enumerable: !0,
|