@sanity/client 6.20.2-beta.1 → 6.20.2
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 +5 -56
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +0 -7
- package/dist/index.browser.d.ts +0 -7
- package/dist/index.browser.js +5 -56
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +5 -57
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +0 -7
- package/dist/index.d.ts +0 -7
- package/dist/index.js +5 -57
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/data/listen.ts +1 -13
- package/src/http/browserMiddleware.ts +1 -3
- package/src/http/nodeMiddleware.ts +0 -3
- package/src/http/requestOptions.ts +0 -1
- package/src/types.ts +0 -8
- package/umd/sanityClient.js +5 -56
- package/umd/sanityClient.min.js +3 -3
- package/src/http/domainSharding.ts +0 -96
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sanity/client",
|
|
3
|
-
"version": "6.20.2
|
|
3
|
+
"version": "6.20.2",
|
|
4
4
|
"description": "Client for retrieving, creating and patching data from Sanity.io",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -118,7 +118,7 @@
|
|
|
118
118
|
},
|
|
119
119
|
"dependencies": {
|
|
120
120
|
"@sanity/eventsource": "^5.0.2",
|
|
121
|
-
"get-it": "^8.6.
|
|
121
|
+
"get-it": "^8.6.3",
|
|
122
122
|
"rxjs": "^7.0.0"
|
|
123
123
|
},
|
|
124
124
|
"devDependencies": {
|
package/src/data/listen.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import {Observable} from 'rxjs'
|
|
2
2
|
|
|
3
|
-
import {domainSharder as sharder} from '../http/domainSharding'
|
|
4
3
|
import type {ObservableSanityClient, SanityClient} from '../SanityClient'
|
|
5
4
|
import type {Any, ListenEvent, ListenOptions, ListenParams, MutationEvent} from '../types'
|
|
6
5
|
import defaults from '../util/defaults'
|
|
@@ -65,11 +64,7 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
|
|
|
65
64
|
const listenOpts = pick(options, possibleOptions)
|
|
66
65
|
const qs = encodeQueryString({query, params, options: {tag, ...listenOpts}})
|
|
67
66
|
|
|
68
|
-
|
|
69
|
-
if (this.config().useDomainSharding) {
|
|
70
|
-
uri = sharder.getShardedUrl(uri)
|
|
71
|
-
}
|
|
72
|
-
|
|
67
|
+
const uri = `${url}${_getDataUrl(this, 'listen', qs)}`
|
|
73
68
|
if (uri.length > MAX_URL_LENGTH) {
|
|
74
69
|
return new Observable((observer) => observer.error(new Error('Query too large for listener')))
|
|
75
70
|
}
|
|
@@ -96,12 +91,6 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
|
|
|
96
91
|
// Once it is`true`, it will never be `false` again.
|
|
97
92
|
let unsubscribed = false
|
|
98
93
|
|
|
99
|
-
// We're about to connect, and will reuse the same shard/bucket for every reconnect henceforth.
|
|
100
|
-
// This may seem inoptimal, but once connected we should just consider this as a "permanent"
|
|
101
|
-
// connection, since we'll automatically retry on failures/disconnects. Once we explicitly
|
|
102
|
-
// unsubsccribe, we can decrement the bucket and free up the shard.
|
|
103
|
-
sharder.incrementBucketForUrl(uri)
|
|
104
|
-
|
|
105
94
|
open()
|
|
106
95
|
|
|
107
96
|
function onError() {
|
|
@@ -198,7 +187,6 @@ export function _listen<R extends Record<string, Any> = Record<string, Any>>(
|
|
|
198
187
|
stopped = true
|
|
199
188
|
unsubscribe()
|
|
200
189
|
unsubscribed = true
|
|
201
|
-
sharder.decrementBucketForUrl(uri)
|
|
202
190
|
}
|
|
203
191
|
|
|
204
192
|
return stop
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import {agent, debug, headers} from 'get-it/middleware'
|
|
2
2
|
|
|
3
3
|
import {name, version} from '../../package.json'
|
|
4
|
-
import {domainSharder} from './domainSharding'
|
|
5
4
|
|
|
6
5
|
const middleware = [
|
|
7
|
-
domainSharder.middleware,
|
|
8
|
-
|
|
9
6
|
debug({verbose: true, namespace: 'sanity:client'}),
|
|
10
7
|
headers({'User-Agent': `${name} ${version}`}),
|
|
11
8
|
|
|
@@ -29,7 +29,6 @@ export function requestOptions(config: Any, overrides: Any = {}): Omit<RequestOp
|
|
|
29
29
|
proxy: overrides.proxy || config.proxy,
|
|
30
30
|
json: true,
|
|
31
31
|
withCredentials,
|
|
32
|
-
useDomainSharding: config.useDomainSharding,
|
|
33
32
|
fetch:
|
|
34
33
|
typeof overrides.fetch === 'object' && typeof config.fetch === 'object'
|
|
35
34
|
? {...config.fetch, ...overrides.fetch}
|
package/src/types.ts
CHANGED
|
@@ -46,14 +46,6 @@ export interface ClientConfig {
|
|
|
46
46
|
apiVersion?: string
|
|
47
47
|
proxy?: string
|
|
48
48
|
|
|
49
|
-
/**
|
|
50
|
-
* Spread the requests over a number of hostnames to work around HTTP/1.1 limitations.
|
|
51
|
-
* Only applicable in browsers, and for certain allowed projects.
|
|
52
|
-
*
|
|
53
|
-
* @alpha
|
|
54
|
-
*/
|
|
55
|
-
useDomainSharding?: boolean
|
|
56
|
-
|
|
57
49
|
/**
|
|
58
50
|
* Optional request tag prefix for all request tags
|
|
59
51
|
*/
|
package/umd/sanityClient.js
CHANGED
|
@@ -2393,7 +2393,6 @@ ${selectionOpts}`);
|
|
|
2393
2393
|
proxy: overrides.proxy || config.proxy,
|
|
2394
2394
|
json: !0,
|
|
2395
2395
|
withCredentials,
|
|
2396
|
-
useDomainSharding: config.useDomainSharding,
|
|
2397
2396
|
fetch: typeof overrides.fetch == "object" && typeof config.fetch == "object" ? { ...config.fetch, ...overrides.fetch } : overrides.fetch || config.fetch
|
|
2398
2397
|
});
|
|
2399
2398
|
}
|
|
@@ -2663,55 +2662,6 @@ ${selectionOpts}`);
|
|
|
2663
2662
|
opts
|
|
2664
2663
|
);
|
|
2665
2664
|
}
|
|
2666
|
-
const UNSHARDED_URL_RE = /^https:\/\/([a-z0-9]+)\.api\.(sanity\..*)/, SHARDED_URL_RE = /^https:\/\/[a-z0-9]+\.api\.s(\d+)\.sanity\.(.*)/, domainSharder = getDomainSharder();
|
|
2667
|
-
function getDomainSharder(initialBuckets) {
|
|
2668
|
-
const buckets = new Array(10).fill(0, 0);
|
|
2669
|
-
function incrementBucketForUrl(url) {
|
|
2670
|
-
const shard = getShardFromUrl(url);
|
|
2671
|
-
shard !== null && buckets[shard]++;
|
|
2672
|
-
}
|
|
2673
|
-
function decrementBucketForUrl(url) {
|
|
2674
|
-
const shard = getShardFromUrl(url);
|
|
2675
|
-
shard !== null && buckets[shard]--;
|
|
2676
|
-
}
|
|
2677
|
-
function getShardedUrl(url) {
|
|
2678
|
-
const [isMatch, projectId2, rest] = url.match(UNSHARDED_URL_RE) || [];
|
|
2679
|
-
if (!isMatch)
|
|
2680
|
-
return url;
|
|
2681
|
-
const bucket = buckets.reduce(
|
|
2682
|
-
(smallest, count, index) => count < buckets[smallest] ? index : smallest,
|
|
2683
|
-
0
|
|
2684
|
-
);
|
|
2685
|
-
return `https://${projectId2}.api.s${bucket}.${rest}`;
|
|
2686
|
-
}
|
|
2687
|
-
function getShardFromUrl(url) {
|
|
2688
|
-
const [isMatch, shard] = url.match(SHARDED_URL_RE) || [];
|
|
2689
|
-
return isMatch ? parseInt(shard, 10) : null;
|
|
2690
|
-
}
|
|
2691
|
-
return {
|
|
2692
|
-
middleware: {
|
|
2693
|
-
processOptions: (options) => {
|
|
2694
|
-
if (!useDomainSharding(options))
|
|
2695
|
-
return options;
|
|
2696
|
-
const url = getShardedUrl(options.url);
|
|
2697
|
-
return options.url = url, options;
|
|
2698
|
-
},
|
|
2699
|
-
onRequest(req) {
|
|
2700
|
-
return useDomainSharding(req.options) && incrementBucketForUrl(req.options.url), req;
|
|
2701
|
-
},
|
|
2702
|
-
onResponse(res, context) {
|
|
2703
|
-
return useDomainSharding(context.options) && decrementBucketForUrl(context.options.url), res;
|
|
2704
|
-
}
|
|
2705
|
-
},
|
|
2706
|
-
incrementBucketForUrl,
|
|
2707
|
-
decrementBucketForUrl,
|
|
2708
|
-
getShardedUrl,
|
|
2709
|
-
getBuckets: () => buckets
|
|
2710
|
-
};
|
|
2711
|
-
}
|
|
2712
|
-
function useDomainSharding(options) {
|
|
2713
|
-
return "useDomainSharding" in options && options.useDomainSharding === !0;
|
|
2714
|
-
}
|
|
2715
2665
|
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), {});
|
|
2716
2666
|
const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop] > "u" || (selection[prop] = obj[prop]), selection), {}), MAX_URL_LENGTH = 14800, possibleOptions = [
|
|
2717
2667
|
"includePreviousRevision",
|
|
@@ -2723,16 +2673,15 @@ ${selectionOpts}`);
|
|
|
2723
2673
|
includeResult: !0
|
|
2724
2674
|
};
|
|
2725
2675
|
function _listen(query, params, opts = {}) {
|
|
2726
|
-
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 } })
|
|
2727
|
-
|
|
2728
|
-
if (this.config().useDomainSharding && (uri = domainSharder.getShardedUrl(uri)), uri.length > MAX_URL_LENGTH)
|
|
2676
|
+
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)}`;
|
|
2677
|
+
if (uri.length > MAX_URL_LENGTH)
|
|
2729
2678
|
return new Observable((observer) => observer.error(new Error("Query too large for listener")));
|
|
2730
2679
|
const listenFor = options.events ? options.events : ["mutation"], shouldEmitReconnect = listenFor.indexOf("reconnect") !== -1, esOptions = {};
|
|
2731
2680
|
return (token || withCredentials) && (esOptions.withCredentials = !0), token && (esOptions.headers = {
|
|
2732
2681
|
Authorization: `Bearer ${token}`
|
|
2733
2682
|
}), new Observable((observer) => {
|
|
2734
2683
|
let es, reconnectTimer, stopped = !1, unsubscribed = !1;
|
|
2735
|
-
|
|
2684
|
+
open();
|
|
2736
2685
|
function onError() {
|
|
2737
2686
|
stopped || (emitReconnect(), !stopped && es.readyState === es.CLOSED && (unsubscribe(), clearTimeout(reconnectTimer), reconnectTimer = setTimeout(open, 100)));
|
|
2738
2687
|
}
|
|
@@ -2767,7 +2716,7 @@ ${selectionOpts}`);
|
|
|
2767
2716
|
});
|
|
2768
2717
|
}
|
|
2769
2718
|
function stop() {
|
|
2770
|
-
stopped = !0, unsubscribe(), unsubscribed = !0
|
|
2719
|
+
stopped = !0, unsubscribe(), unsubscribed = !0;
|
|
2771
2720
|
}
|
|
2772
2721
|
return stop;
|
|
2773
2722
|
});
|
|
@@ -3388,7 +3337,7 @@ ${selectionOpts}`);
|
|
|
3388
3337
|
return printNoDefaultExport(), createClient2(config);
|
|
3389
3338
|
};
|
|
3390
3339
|
}
|
|
3391
|
-
var envMiddleware = [
|
|
3340
|
+
var envMiddleware = [];
|
|
3392
3341
|
const exp = defineCreateClientExports(envMiddleware, SanityClient), requester = exp.requester, createClient = exp.createClient, deprecatedCreateClient = defineDeprecatedCreateClient(createClient);
|
|
3393
3342
|
|
|
3394
3343
|
const reKeySegment = /_key\s*==\s*['"](.*)['"]/;
|