@splithub/core 0.6.0 → 0.7.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/README.md +1 -1
- package/dist/chunk-73Y4OWKF.js +929 -0
- package/dist/chunk-73Y4OWKF.js.map +1 -0
- package/dist/index.cjs +255 -138
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +53 -5
- package/dist/index.d.ts +53 -5
- package/dist/index.js +1339 -40
- package/dist/index.js.map +1 -1
- package/dist/{types-CuEDa1Ew.d.cts → types-DEEyqr9C.d.cts} +6 -2
- package/dist/{types-CuEDa1Ew.d.ts → types-DEEyqr9C.d.ts} +6 -2
- package/dist/vite.cjs +77 -14
- package/dist/vite.cjs.map +1 -1
- package/dist/vite.d.cts +1 -1
- package/dist/vite.d.ts +1 -1
- package/dist/vite.js +4 -7
- package/dist/vite.js.map +1 -1
- package/package.json +2 -2
- package/dist/chunk-WFNCZZV3.js +0 -2173
- package/dist/chunk-WFNCZZV3.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -21,8 +21,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
CLOUD_ENDPOINT: () => CLOUD_ENDPOINT,
|
|
24
|
+
CLOUD_FALLBACK_ENDPOINTS: () => CLOUD_FALLBACK_ENDPOINTS,
|
|
24
25
|
ConfigError: () => ConfigError,
|
|
25
26
|
EMPTY_OVERRIDES: () => EMPTY_OVERRIDES,
|
|
27
|
+
ENDPOINT_KEY: () => ENDPOINT_KEY,
|
|
26
28
|
EVENT_NAME_PATTERN: () => EVENT_NAME_PATTERN,
|
|
27
29
|
EventQueue: () => EventQueue,
|
|
28
30
|
HOLDOUT_KEY: () => HOLDOUT_KEY,
|
|
@@ -42,6 +44,8 @@ __export(index_exports, {
|
|
|
42
44
|
applyRemoteOverrides: () => applyRemoteOverrides,
|
|
43
45
|
assign: () => assign,
|
|
44
46
|
browserContext: () => browserContext,
|
|
47
|
+
browserLocalStorage: () => browserLocalStorage,
|
|
48
|
+
cloudEndpoints: () => cloudEndpoints,
|
|
45
49
|
compareVersions: () => compareVersions,
|
|
46
50
|
contextValue: () => contextValue,
|
|
47
51
|
controlVariant: () => controlVariant,
|
|
@@ -56,7 +60,9 @@ __export(index_exports, {
|
|
|
56
60
|
evaluateCondition: () => evaluateCondition,
|
|
57
61
|
eventKey: () => eventKey,
|
|
58
62
|
explainAssignment: () => explainAssignment,
|
|
63
|
+
fetchCloudOverrides: () => fetchCloudOverrides,
|
|
59
64
|
fetchRemoteOverrides: () => fetchRemoteOverrides,
|
|
65
|
+
fetchRemoteOverridesFrom: () => fetchRemoteOverridesFrom,
|
|
60
66
|
getExperiment: () => getExperiment,
|
|
61
67
|
getOrCreateSessionId: () => getOrCreateSessionId,
|
|
62
68
|
getOrCreateVisitorId: () => getOrCreateVisitorId,
|
|
@@ -70,6 +76,7 @@ __export(index_exports, {
|
|
|
70
76
|
isActive: () => isActive,
|
|
71
77
|
isBot: () => isBot,
|
|
72
78
|
isBrowser: () => isBrowser,
|
|
79
|
+
isCloudEndpoint: () => isCloudEndpoint,
|
|
73
80
|
isCrawler: () => isCrawler,
|
|
74
81
|
isDebug: () => isDebug,
|
|
75
82
|
isDevDraft: () => isDevDraft,
|
|
@@ -88,6 +95,7 @@ __export(index_exports, {
|
|
|
88
95
|
newPropertyId: () => newPropertyId,
|
|
89
96
|
newSessionId: () => newSessionId,
|
|
90
97
|
newVisitorId: () => newVisitorId,
|
|
98
|
+
normalizeCloudEndpoint: () => normalizeCloudEndpoint,
|
|
91
99
|
normalizePath: () => normalizePath,
|
|
92
100
|
normalizeProperty: () => normalizeProperty,
|
|
93
101
|
onDebug: () => onDebug,
|
|
@@ -99,6 +107,8 @@ __export(index_exports, {
|
|
|
99
107
|
readRemoteCache: () => readRemoteCache,
|
|
100
108
|
redirectTarget: () => redirectTarget,
|
|
101
109
|
referrerOrigin: () => referrerOrigin,
|
|
110
|
+
rememberEndpoint: () => rememberEndpoint,
|
|
111
|
+
rememberedEndpoint: () => rememberedEndpoint,
|
|
102
112
|
resetAnalyticsClaims: () => resetAnalyticsClaims,
|
|
103
113
|
resetDevNotes: () => resetDevNotes,
|
|
104
114
|
resolveOverridesUrl: () => resolveOverridesUrl,
|
|
@@ -108,6 +118,203 @@ __export(index_exports, {
|
|
|
108
118
|
});
|
|
109
119
|
module.exports = __toCommonJS(index_exports);
|
|
110
120
|
|
|
121
|
+
// src/platform.ts
|
|
122
|
+
function memoryStorage(initial = {}) {
|
|
123
|
+
const map = new Map(Object.entries(initial));
|
|
124
|
+
return {
|
|
125
|
+
getItem: (k) => map.get(k) ?? null,
|
|
126
|
+
setItem: (k, v) => void map.set(k, String(v)),
|
|
127
|
+
removeItem: (k) => void map.delete(k)
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
function safeGet(storage3, key) {
|
|
131
|
+
try {
|
|
132
|
+
return storage3?.getItem(key) ?? null;
|
|
133
|
+
} catch {
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
function safeSet(storage3, key, value) {
|
|
138
|
+
try {
|
|
139
|
+
if (value === null) storage3?.removeItem(key);
|
|
140
|
+
else storage3?.setItem(key, value);
|
|
141
|
+
} catch {
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// src/remote.ts
|
|
146
|
+
var EMPTY_OVERRIDES = { version: 0, experiments: {} };
|
|
147
|
+
function applyToExperiment(experiment, o) {
|
|
148
|
+
let variants = experiment.variants;
|
|
149
|
+
if (o.weights) {
|
|
150
|
+
const raw = variants.map((v) => ({ ...v, weight: Math.max(0, o.weights[v.key] ?? v.weight) }));
|
|
151
|
+
const total = raw.reduce((s, v) => s + v.weight, 0);
|
|
152
|
+
if (total > 0) variants = raw.map((v) => ({ ...v, weight: v.weight / total }));
|
|
153
|
+
}
|
|
154
|
+
if (o.values && experiment.kind === "value") {
|
|
155
|
+
variants = variants.map((v) => v.key in o.values ? { ...v, value: o.values[v.key] } : v);
|
|
156
|
+
}
|
|
157
|
+
return {
|
|
158
|
+
...experiment,
|
|
159
|
+
status: o.status ?? experiment.status,
|
|
160
|
+
traffic: o.traffic !== void 0 && o.traffic >= 0 && o.traffic <= 1 ? o.traffic : experiment.traffic,
|
|
161
|
+
seed: o.seed ?? experiment.seed,
|
|
162
|
+
variants
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
function applyRemoteOverrides(config, overrides) {
|
|
166
|
+
if (!overrides) return config;
|
|
167
|
+
const patches = overrides.experiments ?? {};
|
|
168
|
+
const known = new Set(config.experiments.map((e) => e.key));
|
|
169
|
+
const added = (overrides.added ?? []).filter((e) => e && typeof e.key === "string" && !known.has(e.key) && Array.isArray(e.variants) && e.variants.length >= 2);
|
|
170
|
+
const knownGoals = new Set(config.goals.map((g) => g.key));
|
|
171
|
+
const addedGoals = (overrides.goals ?? []).filter((g) => g && typeof g.key === "string" && !knownGoals.has(g.key));
|
|
172
|
+
const holdout = !config.holdout && validHoldout(overrides.holdout) ? overrides.holdout : null;
|
|
173
|
+
const layers = overrides.layers && typeof overrides.layers === "object" ? { ...overrides.layers, ...config.layers ?? {} } : null;
|
|
174
|
+
const propertyPatches = overrides.properties ?? {};
|
|
175
|
+
const knownProps = new Set((config.properties ?? []).map((p) => p.key));
|
|
176
|
+
const addedProps = (overrides.addedProperties ?? []).filter((p) => p && typeof p.key === "string" && !knownProps.has(p.key));
|
|
177
|
+
const propsChanged = Object.keys(propertyPatches).length > 0 || addedProps.length > 0;
|
|
178
|
+
if (Object.keys(patches).length === 0 && added.length === 0 && addedGoals.length === 0 && !holdout && !layers && !propsChanged) return config;
|
|
179
|
+
const properties = propsChanged ? [...config.properties ?? [], ...addedProps.map((p) => ({ ...p, goals: Array.isArray(p.goals) ? p.goals : [], origin: "dashboard" }))].map((p) => {
|
|
180
|
+
const status = propertyPatches[p.key]?.status;
|
|
181
|
+
return status === "running" || status === "paused" ? { ...p, status } : p;
|
|
182
|
+
}) : config.properties;
|
|
183
|
+
const merged = [...config.experiments, ...added.map((e) => ({ ...e, origin: "dashboard" }))];
|
|
184
|
+
return {
|
|
185
|
+
...config,
|
|
186
|
+
...holdout ? { holdout } : {},
|
|
187
|
+
...layers ? { layers } : {},
|
|
188
|
+
goals: addedGoals.length ? [...config.goals, ...addedGoals] : config.goals,
|
|
189
|
+
...properties ? { properties } : {},
|
|
190
|
+
experiments: merged.map((e) => {
|
|
191
|
+
const o = patches[e.key];
|
|
192
|
+
return o ? applyToExperiment(e, o) : e;
|
|
193
|
+
})
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
function validHoldout(h) {
|
|
197
|
+
return Boolean(h) && typeof h.percent === "number" && h.percent > 0 && h.percent <= 50 && typeof h.seed === "string";
|
|
198
|
+
}
|
|
199
|
+
function isRemoteOverrides(value) {
|
|
200
|
+
return Boolean(value) && typeof value === "object" && typeof value.version === "number" && typeof value.experiments === "object";
|
|
201
|
+
}
|
|
202
|
+
function resolveOverridesUrl(eventsEndpoint, publicKey, all = false) {
|
|
203
|
+
const base = eventsEndpoint.replace(/\/events\/?$/, "");
|
|
204
|
+
return `${base}/overrides/${encodeURIComponent(publicKey)}.json${all ? "?all=1" : ""}`;
|
|
205
|
+
}
|
|
206
|
+
var REMOTE_CACHE_KEY = "sh_remote";
|
|
207
|
+
var REMOTE_TTL_MS = 6e4;
|
|
208
|
+
function storage(custom) {
|
|
209
|
+
if (custom) return custom;
|
|
210
|
+
try {
|
|
211
|
+
if (typeof window === "undefined" || !window.localStorage) return null;
|
|
212
|
+
return window.localStorage;
|
|
213
|
+
} catch {
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
var remoteCacheKey = (publicKey, all = false) => `${REMOTE_CACHE_KEY}:${publicKey}${all ? ":all" : ""}`;
|
|
218
|
+
function readRemoteCache(publicKey, store, all = false) {
|
|
219
|
+
try {
|
|
220
|
+
const raw = storage(store)?.getItem(remoteCacheKey(publicKey, all));
|
|
221
|
+
if (!raw) return null;
|
|
222
|
+
const parsed = JSON.parse(raw);
|
|
223
|
+
return isRemoteOverrides(parsed.overrides) && typeof parsed.fetchedAt === "number" ? parsed : null;
|
|
224
|
+
} catch {
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
function writeRemoteCache(publicKey, overrides, store, all = false) {
|
|
229
|
+
try {
|
|
230
|
+
storage(store)?.setItem(remoteCacheKey(publicKey, all), JSON.stringify({ fetchedAt: Date.now(), overrides }));
|
|
231
|
+
} catch {
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
async function fetchRemoteOverrides(url, fetchImpl = fetch) {
|
|
235
|
+
return (await fetchRemoteOverridesFrom([url], fetchImpl)).overrides;
|
|
236
|
+
}
|
|
237
|
+
async function fetchRemoteOverridesFrom(urls, fetchImpl = fetch) {
|
|
238
|
+
for (let i = 0; i < urls.length; i++) {
|
|
239
|
+
let res;
|
|
240
|
+
try {
|
|
241
|
+
res = await fetchImpl(urls[i], { cache: "no-cache" });
|
|
242
|
+
} catch {
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
try {
|
|
246
|
+
const body = res.ok ? await res.json() : null;
|
|
247
|
+
return { overrides: isRemoteOverrides(body) ? body : null, answered: i };
|
|
248
|
+
} catch {
|
|
249
|
+
return { overrides: null, answered: i };
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
return { overrides: null, answered: -1 };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// src/endpoint.ts
|
|
256
|
+
var CLOUD_ENDPOINT = "https://go.sh2.uk/v1/events";
|
|
257
|
+
var CLOUD_FALLBACK_ENDPOINTS = ["https://splithub-go.adbro.workers.dev/v1/events"];
|
|
258
|
+
var PAST_CLOUD_ENDPOINTS = ["https://go.splithub.io/v1/events"];
|
|
259
|
+
var ENDPOINT_KEY = "sh_endpoint";
|
|
260
|
+
var trim = (url) => url.replace(/\/+$/, "");
|
|
261
|
+
function isEndpointUrl(value) {
|
|
262
|
+
if (typeof value !== "string" || value.length > 200) return false;
|
|
263
|
+
try {
|
|
264
|
+
const u = new URL(value);
|
|
265
|
+
return u.protocol === "https:" && /\/v1\/events\/?$/.test(u.pathname) && !u.search && !u.hash;
|
|
266
|
+
} catch {
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
function normalizeCloudEndpoint(value) {
|
|
271
|
+
const v = value.trim();
|
|
272
|
+
const url = /^https?:\/\//i.test(v) ? v : `https://${v}`;
|
|
273
|
+
try {
|
|
274
|
+
const u = new URL(url);
|
|
275
|
+
if (u.pathname === "/" || u.pathname === "") return `${u.origin}/v1/events`;
|
|
276
|
+
} catch {
|
|
277
|
+
return v;
|
|
278
|
+
}
|
|
279
|
+
return url;
|
|
280
|
+
}
|
|
281
|
+
function isCloudEndpoint(endpoint) {
|
|
282
|
+
if (endpoint === void 0) return true;
|
|
283
|
+
const e = trim(endpoint);
|
|
284
|
+
return [CLOUD_ENDPOINT, ...CLOUD_FALLBACK_ENDPOINTS, ...PAST_CLOUD_ENDPOINTS].some((c) => c === e);
|
|
285
|
+
}
|
|
286
|
+
function rememberedEndpoint(store) {
|
|
287
|
+
const v = safeGet(store ?? null, ENDPOINT_KEY);
|
|
288
|
+
return isEndpointUrl(v) ? v : null;
|
|
289
|
+
}
|
|
290
|
+
function rememberEndpoint(store, endpoint) {
|
|
291
|
+
if (!isEndpointUrl(endpoint) || rememberedEndpoint(store) === endpoint) return;
|
|
292
|
+
safeSet(store ?? null, ENDPOINT_KEY, endpoint);
|
|
293
|
+
}
|
|
294
|
+
function cloudEndpoints(store, configured) {
|
|
295
|
+
const own = configured && !PAST_CLOUD_ENDPOINTS.includes(trim(configured)) ? configured : null;
|
|
296
|
+
const list2 = [rememberedEndpoint(store), own, CLOUD_ENDPOINT, ...CLOUD_FALLBACK_ENDPOINTS];
|
|
297
|
+
const out = [];
|
|
298
|
+
for (const e of list2) if (e && !out.includes(trim(e))) out.push(trim(e));
|
|
299
|
+
return out;
|
|
300
|
+
}
|
|
301
|
+
function browserLocalStorage() {
|
|
302
|
+
try {
|
|
303
|
+
return typeof window !== "undefined" && window.localStorage ? window.localStorage : null;
|
|
304
|
+
} catch {
|
|
305
|
+
return null;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
async function fetchCloudOverrides(endpoint, key, options = {}) {
|
|
309
|
+
const list2 = isCloudEndpoint(endpoint) ? cloudEndpoints(options.store, endpoint) : [endpoint];
|
|
310
|
+
const { overrides, answered } = await fetchRemoteOverridesFrom(list2.map((e) => resolveOverridesUrl(e, key, options.all)), options.fetchImpl);
|
|
311
|
+
if (isCloudEndpoint(endpoint)) {
|
|
312
|
+
if (answered > 0) rememberEndpoint(options.store, list2[answered]);
|
|
313
|
+
if (overrides && isEndpointUrl(overrides.endpoint)) rememberEndpoint(options.store, overrides.endpoint);
|
|
314
|
+
}
|
|
315
|
+
return overrides;
|
|
316
|
+
}
|
|
317
|
+
|
|
111
318
|
// src/hash.ts
|
|
112
319
|
function murmur3(input, seed = 0) {
|
|
113
320
|
let h = seed >>> 0;
|
|
@@ -727,7 +934,7 @@ function defineConfig(input) {
|
|
|
727
934
|
const properties = Object.entries(input.properties ?? {}).map(([key, p]) => normalizeProperty(key, p, goals));
|
|
728
935
|
return {
|
|
729
936
|
project: input.project,
|
|
730
|
-
storage: input.storage,
|
|
937
|
+
storage: input.storage.mode === "cloud" && input.storage.endpoint ? { ...input.storage, endpoint: normalizeCloudEndpoint(input.storage.endpoint) } : input.storage,
|
|
731
938
|
visitorKey: input.visitorKey ?? "sh_vid",
|
|
732
939
|
runDraftsInDev: input.runDraftsInDev === true,
|
|
733
940
|
goals,
|
|
@@ -999,30 +1206,6 @@ function referrerOrigin(referrer, siteHost) {
|
|
|
999
1206
|
return `${url.protocol}//${url.host}`.slice(0, 300);
|
|
1000
1207
|
}
|
|
1001
1208
|
|
|
1002
|
-
// src/platform.ts
|
|
1003
|
-
function memoryStorage(initial = {}) {
|
|
1004
|
-
const map = new Map(Object.entries(initial));
|
|
1005
|
-
return {
|
|
1006
|
-
getItem: (k) => map.get(k) ?? null,
|
|
1007
|
-
setItem: (k, v) => void map.set(k, String(v)),
|
|
1008
|
-
removeItem: (k) => void map.delete(k)
|
|
1009
|
-
};
|
|
1010
|
-
}
|
|
1011
|
-
function safeGet(storage3, key) {
|
|
1012
|
-
try {
|
|
1013
|
-
return storage3?.getItem(key) ?? null;
|
|
1014
|
-
} catch {
|
|
1015
|
-
return null;
|
|
1016
|
-
}
|
|
1017
|
-
}
|
|
1018
|
-
function safeSet(storage3, key, value) {
|
|
1019
|
-
try {
|
|
1020
|
-
if (value === null) storage3?.removeItem(key);
|
|
1021
|
-
else storage3?.setItem(key, value);
|
|
1022
|
-
} catch {
|
|
1023
|
-
}
|
|
1024
|
-
}
|
|
1025
|
-
|
|
1026
1209
|
// src/visitor.ts
|
|
1027
1210
|
var ONE_YEAR = 60 * 60 * 24 * 365;
|
|
1028
1211
|
var isBrowser = () => typeof window !== "undefined" && typeof document !== "undefined" && !isReactNative();
|
|
@@ -1131,16 +1314,18 @@ function decideRedirect(config, pathname, visitorId, context = {}, overrides = {
|
|
|
1131
1314
|
}
|
|
1132
1315
|
|
|
1133
1316
|
// src/events.ts
|
|
1134
|
-
function beaconUrl(options) {
|
|
1135
|
-
if (!options.key) return
|
|
1317
|
+
function beaconUrl(options, endpoint) {
|
|
1318
|
+
if (!options.key) return endpoint;
|
|
1136
1319
|
if (!options.keyInQuery) return null;
|
|
1137
|
-
return `${
|
|
1320
|
+
return `${endpoint}${endpoint.includes("?") ? "&" : "?"}key=${encodeURIComponent(options.key)}`;
|
|
1138
1321
|
}
|
|
1139
1322
|
function createFetchTransport(options) {
|
|
1323
|
+
const endpoints = [options.endpoint, ...(options.fallbacks ?? []).filter((e) => e !== options.endpoint)];
|
|
1324
|
+
let active = 0;
|
|
1140
1325
|
return {
|
|
1141
1326
|
async send(batch, { beacon }) {
|
|
1142
1327
|
const body = JSON.stringify(batch);
|
|
1143
|
-
const url = beacon && isBrowser() && typeof navigator.sendBeacon === "function" ? beaconUrl(options) : null;
|
|
1328
|
+
const url = beacon && isBrowser() && typeof navigator.sendBeacon === "function" ? beaconUrl(options, endpoints[active]) : null;
|
|
1144
1329
|
if (url) {
|
|
1145
1330
|
let sameOrigin = false;
|
|
1146
1331
|
try {
|
|
@@ -1155,7 +1340,22 @@ function createFetchTransport(options) {
|
|
|
1155
1340
|
}
|
|
1156
1341
|
const headers = { "content-type": "application/json" };
|
|
1157
1342
|
if (options.key) headers.authorization = `Bearer ${options.key}`;
|
|
1158
|
-
|
|
1343
|
+
let res = null;
|
|
1344
|
+
let failure;
|
|
1345
|
+
for (let i = 0; i < endpoints.length && !res; i++) {
|
|
1346
|
+
const at = (active + i) % endpoints.length;
|
|
1347
|
+
try {
|
|
1348
|
+
res = await fetch(endpoints[at], { method: "POST", headers, body, ...options.keepalive === false ? {} : { keepalive: true } });
|
|
1349
|
+
} catch (e) {
|
|
1350
|
+
failure = e;
|
|
1351
|
+
continue;
|
|
1352
|
+
}
|
|
1353
|
+
if (at !== active) {
|
|
1354
|
+
active = at;
|
|
1355
|
+
options.onEndpoint?.(endpoints[at]);
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
if (!res) throw failure;
|
|
1159
1359
|
if (!res.ok) {
|
|
1160
1360
|
const error = new Error(`[splithub] ingest responded ${res.status}`);
|
|
1161
1361
|
error.status = res.status;
|
|
@@ -1350,7 +1550,7 @@ var EventQueue = class {
|
|
|
1350
1550
|
|
|
1351
1551
|
// src/debug.ts
|
|
1352
1552
|
var KEY = "sh_debug";
|
|
1353
|
-
function
|
|
1553
|
+
function storage2() {
|
|
1354
1554
|
try {
|
|
1355
1555
|
return typeof sessionStorage !== "undefined" ? sessionStorage : null;
|
|
1356
1556
|
} catch {
|
|
@@ -1360,7 +1560,7 @@ function storage() {
|
|
|
1360
1560
|
function debugMode() {
|
|
1361
1561
|
if (typeof location === "undefined") return null;
|
|
1362
1562
|
const m = /[?&]sh_debug=([^&#]*)/.exec(location.search);
|
|
1363
|
-
const s =
|
|
1563
|
+
const s = storage2();
|
|
1364
1564
|
if (m) {
|
|
1365
1565
|
const v = decodeURIComponent(m[1] ?? "");
|
|
1366
1566
|
const mode = v === "0" || v === "off" || v === "false" ? null : v === "hold" ? "hold" : "on";
|
|
@@ -1400,111 +1600,10 @@ function onDebug(listener) {
|
|
|
1400
1600
|
return () => window.removeEventListener("splithub:debug", handler);
|
|
1401
1601
|
}
|
|
1402
1602
|
|
|
1403
|
-
// src/remote.ts
|
|
1404
|
-
var EMPTY_OVERRIDES = { version: 0, experiments: {} };
|
|
1405
|
-
function applyToExperiment(experiment, o) {
|
|
1406
|
-
let variants = experiment.variants;
|
|
1407
|
-
if (o.weights) {
|
|
1408
|
-
const raw = variants.map((v) => ({ ...v, weight: Math.max(0, o.weights[v.key] ?? v.weight) }));
|
|
1409
|
-
const total = raw.reduce((s, v) => s + v.weight, 0);
|
|
1410
|
-
if (total > 0) variants = raw.map((v) => ({ ...v, weight: v.weight / total }));
|
|
1411
|
-
}
|
|
1412
|
-
if (o.values && experiment.kind === "value") {
|
|
1413
|
-
variants = variants.map((v) => v.key in o.values ? { ...v, value: o.values[v.key] } : v);
|
|
1414
|
-
}
|
|
1415
|
-
return {
|
|
1416
|
-
...experiment,
|
|
1417
|
-
status: o.status ?? experiment.status,
|
|
1418
|
-
traffic: o.traffic !== void 0 && o.traffic >= 0 && o.traffic <= 1 ? o.traffic : experiment.traffic,
|
|
1419
|
-
seed: o.seed ?? experiment.seed,
|
|
1420
|
-
variants
|
|
1421
|
-
};
|
|
1422
|
-
}
|
|
1423
|
-
function applyRemoteOverrides(config, overrides) {
|
|
1424
|
-
if (!overrides) return config;
|
|
1425
|
-
const patches = overrides.experiments ?? {};
|
|
1426
|
-
const known = new Set(config.experiments.map((e) => e.key));
|
|
1427
|
-
const added = (overrides.added ?? []).filter((e) => e && typeof e.key === "string" && !known.has(e.key) && Array.isArray(e.variants) && e.variants.length >= 2);
|
|
1428
|
-
const knownGoals = new Set(config.goals.map((g) => g.key));
|
|
1429
|
-
const addedGoals = (overrides.goals ?? []).filter((g) => g && typeof g.key === "string" && !knownGoals.has(g.key));
|
|
1430
|
-
const holdout = !config.holdout && validHoldout(overrides.holdout) ? overrides.holdout : null;
|
|
1431
|
-
const layers = overrides.layers && typeof overrides.layers === "object" ? { ...overrides.layers, ...config.layers ?? {} } : null;
|
|
1432
|
-
const propertyPatches = overrides.properties ?? {};
|
|
1433
|
-
const knownProps = new Set((config.properties ?? []).map((p) => p.key));
|
|
1434
|
-
const addedProps = (overrides.addedProperties ?? []).filter((p) => p && typeof p.key === "string" && !knownProps.has(p.key));
|
|
1435
|
-
const propsChanged = Object.keys(propertyPatches).length > 0 || addedProps.length > 0;
|
|
1436
|
-
if (Object.keys(patches).length === 0 && added.length === 0 && addedGoals.length === 0 && !holdout && !layers && !propsChanged) return config;
|
|
1437
|
-
const properties = propsChanged ? [...config.properties ?? [], ...addedProps.map((p) => ({ ...p, goals: Array.isArray(p.goals) ? p.goals : [], origin: "dashboard" }))].map((p) => {
|
|
1438
|
-
const status = propertyPatches[p.key]?.status;
|
|
1439
|
-
return status === "running" || status === "paused" ? { ...p, status } : p;
|
|
1440
|
-
}) : config.properties;
|
|
1441
|
-
const merged = [...config.experiments, ...added.map((e) => ({ ...e, origin: "dashboard" }))];
|
|
1442
|
-
return {
|
|
1443
|
-
...config,
|
|
1444
|
-
...holdout ? { holdout } : {},
|
|
1445
|
-
...layers ? { layers } : {},
|
|
1446
|
-
goals: addedGoals.length ? [...config.goals, ...addedGoals] : config.goals,
|
|
1447
|
-
...properties ? { properties } : {},
|
|
1448
|
-
experiments: merged.map((e) => {
|
|
1449
|
-
const o = patches[e.key];
|
|
1450
|
-
return o ? applyToExperiment(e, o) : e;
|
|
1451
|
-
})
|
|
1452
|
-
};
|
|
1453
|
-
}
|
|
1454
|
-
function validHoldout(h) {
|
|
1455
|
-
return Boolean(h) && typeof h.percent === "number" && h.percent > 0 && h.percent <= 50 && typeof h.seed === "string";
|
|
1456
|
-
}
|
|
1457
|
-
function isRemoteOverrides(value) {
|
|
1458
|
-
return Boolean(value) && typeof value === "object" && typeof value.version === "number" && typeof value.experiments === "object";
|
|
1459
|
-
}
|
|
1460
|
-
function resolveOverridesUrl(eventsEndpoint, publicKey, all = false) {
|
|
1461
|
-
const base = eventsEndpoint.replace(/\/events\/?$/, "");
|
|
1462
|
-
return `${base}/overrides/${encodeURIComponent(publicKey)}.json${all ? "?all=1" : ""}`;
|
|
1463
|
-
}
|
|
1464
|
-
var REMOTE_CACHE_KEY = "sh_remote";
|
|
1465
|
-
var REMOTE_TTL_MS = 6e4;
|
|
1466
|
-
function storage2(custom) {
|
|
1467
|
-
if (custom) return custom;
|
|
1468
|
-
try {
|
|
1469
|
-
if (typeof window === "undefined" || !window.localStorage) return null;
|
|
1470
|
-
return window.localStorage;
|
|
1471
|
-
} catch {
|
|
1472
|
-
return null;
|
|
1473
|
-
}
|
|
1474
|
-
}
|
|
1475
|
-
var remoteCacheKey = (publicKey, all = false) => `${REMOTE_CACHE_KEY}:${publicKey}${all ? ":all" : ""}`;
|
|
1476
|
-
function readRemoteCache(publicKey, store, all = false) {
|
|
1477
|
-
try {
|
|
1478
|
-
const raw = storage2(store)?.getItem(remoteCacheKey(publicKey, all));
|
|
1479
|
-
if (!raw) return null;
|
|
1480
|
-
const parsed = JSON.parse(raw);
|
|
1481
|
-
return isRemoteOverrides(parsed.overrides) && typeof parsed.fetchedAt === "number" ? parsed : null;
|
|
1482
|
-
} catch {
|
|
1483
|
-
return null;
|
|
1484
|
-
}
|
|
1485
|
-
}
|
|
1486
|
-
function writeRemoteCache(publicKey, overrides, store, all = false) {
|
|
1487
|
-
try {
|
|
1488
|
-
storage2(store)?.setItem(remoteCacheKey(publicKey, all), JSON.stringify({ fetchedAt: Date.now(), overrides }));
|
|
1489
|
-
} catch {
|
|
1490
|
-
}
|
|
1491
|
-
}
|
|
1492
|
-
async function fetchRemoteOverrides(url, fetchImpl = fetch) {
|
|
1493
|
-
try {
|
|
1494
|
-
const res = await fetchImpl(url, { cache: "no-cache" });
|
|
1495
|
-
if (!res.ok) return null;
|
|
1496
|
-
const body = await res.json();
|
|
1497
|
-
return isRemoteOverrides(body) ? body : null;
|
|
1498
|
-
} catch {
|
|
1499
|
-
return null;
|
|
1500
|
-
}
|
|
1501
|
-
}
|
|
1502
|
-
|
|
1503
1603
|
// src/client.ts
|
|
1504
1604
|
var SDK_VERSION = "0.1.0";
|
|
1505
1605
|
var REMOTE_WAIT_MS = 400;
|
|
1506
1606
|
var DEV_NOTE = "Development sends nothing to the cloud, so your own visits never reach the results. Deployed builds send as usual.";
|
|
1507
|
-
var CLOUD_ENDPOINT = "https://go.sh2.uk/v1/events";
|
|
1508
1607
|
var PROPERTY_WAIT_MS = 5e3;
|
|
1509
1608
|
var MAX_PROPS_BYTES = 4096;
|
|
1510
1609
|
var MAX_ENGAGE_MS = 36e5;
|
|
@@ -1529,6 +1628,11 @@ function createClient(config, options = {}) {
|
|
|
1529
1628
|
const kv = (kind) => typeof __SPLITHUB_LITE__ === "undefined" && adapter ? kind === "local" ? adapter.storage : sessionStore : browser ? browserStorage(kind) : null;
|
|
1530
1629
|
const base = config;
|
|
1531
1630
|
const publicKey = config.storage.mode === "cloud" ? config.storage.key : void 0;
|
|
1631
|
+
const local = kv("local");
|
|
1632
|
+
const cloudList = config.storage.mode === "cloud" && isCloudEndpoint(config.storage.endpoint) && !options.remote?.url ? cloudEndpoints(local, config.storage.endpoint) : null;
|
|
1633
|
+
const adopt = (fresh) => {
|
|
1634
|
+
if (cloudList && fresh && isEndpointUrl(fresh.endpoint)) rememberEndpoint(local, fresh.endpoint);
|
|
1635
|
+
};
|
|
1532
1636
|
let effective = config;
|
|
1533
1637
|
let geo;
|
|
1534
1638
|
let waitFor = null;
|
|
@@ -1546,8 +1650,10 @@ function createClient(config, options = {}) {
|
|
|
1546
1650
|
}
|
|
1547
1651
|
const stale = !cached || Date.now() - cached.fetchedAt > (options.remote?.ttlMs ?? REMOTE_TTL_MS);
|
|
1548
1652
|
if (stale) {
|
|
1549
|
-
const
|
|
1550
|
-
const fetched =
|
|
1653
|
+
const urls = cloudList ? cloudList.map((e) => resolveOverridesUrl(e, publicKey, all)) : [options.remote?.url ?? resolveOverridesUrl(config.storage.endpoint ?? CLOUD_ENDPOINT, publicKey, all)];
|
|
1654
|
+
const fetched = fetchRemoteOverridesFrom(urls).then(({ overrides: fresh, answered }) => {
|
|
1655
|
+
if (cloudList && answered > 0) rememberEndpoint(local, cloudList[answered]);
|
|
1656
|
+
adopt(fresh);
|
|
1551
1657
|
if (fresh) writeRemoteCache(publicKey, fresh, adapter?.storage, all);
|
|
1552
1658
|
return fresh;
|
|
1553
1659
|
});
|
|
@@ -1604,7 +1710,8 @@ function createClient(config, options = {}) {
|
|
|
1604
1710
|
};
|
|
1605
1711
|
const transport = options.transport ?? createFetchTransport({
|
|
1606
1712
|
...resolveEndpoint(config),
|
|
1607
|
-
|
|
1713
|
+
...cloudList ? { endpoint: cloudList[0], fallbacks: cloudList.slice(1) } : {},
|
|
1714
|
+
keyInQuery: options.keyInQuery ?? (config.storage.mode === "cloud" && isCloudEndpoint(config.storage.endpoint)),
|
|
1608
1715
|
keepalive: !app
|
|
1609
1716
|
});
|
|
1610
1717
|
const queue = new EventQueue({
|
|
@@ -1766,8 +1873,8 @@ ${traits ? JSON.stringify(traits) : ""}`;
|
|
|
1766
1873
|
claimed.add(prop);
|
|
1767
1874
|
return true;
|
|
1768
1875
|
}
|
|
1769
|
-
if (typeof __SPLITHUB_LITE__ === "undefined") devNote(`shared:${prop}`, `Another
|
|
1770
|
-
hold(`s:${prop}`, "pageview", `${prop}: pageviews left to the other
|
|
1876
|
+
if (typeof __SPLITHUB_LITE__ === "undefined") devNote(`shared:${prop}`, `Another SplitHub script on this page already collects pageviews for "${prop}", so this one leaves them to it. Keep one install per property: the SDK or sh.js.`);
|
|
1877
|
+
hold(`s:${prop}`, "pageview", `${prop}: pageviews left to the other SplitHub script on this page`);
|
|
1771
1878
|
return false;
|
|
1772
1879
|
};
|
|
1773
1880
|
let lastPage = null;
|
|
@@ -2214,8 +2321,10 @@ function warnUnknown(key) {
|
|
|
2214
2321
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2215
2322
|
0 && (module.exports = {
|
|
2216
2323
|
CLOUD_ENDPOINT,
|
|
2324
|
+
CLOUD_FALLBACK_ENDPOINTS,
|
|
2217
2325
|
ConfigError,
|
|
2218
2326
|
EMPTY_OVERRIDES,
|
|
2327
|
+
ENDPOINT_KEY,
|
|
2219
2328
|
EVENT_NAME_PATTERN,
|
|
2220
2329
|
EventQueue,
|
|
2221
2330
|
HOLDOUT_KEY,
|
|
@@ -2235,6 +2344,8 @@ function warnUnknown(key) {
|
|
|
2235
2344
|
applyRemoteOverrides,
|
|
2236
2345
|
assign,
|
|
2237
2346
|
browserContext,
|
|
2347
|
+
browserLocalStorage,
|
|
2348
|
+
cloudEndpoints,
|
|
2238
2349
|
compareVersions,
|
|
2239
2350
|
contextValue,
|
|
2240
2351
|
controlVariant,
|
|
@@ -2249,7 +2360,9 @@ function warnUnknown(key) {
|
|
|
2249
2360
|
evaluateCondition,
|
|
2250
2361
|
eventKey,
|
|
2251
2362
|
explainAssignment,
|
|
2363
|
+
fetchCloudOverrides,
|
|
2252
2364
|
fetchRemoteOverrides,
|
|
2365
|
+
fetchRemoteOverridesFrom,
|
|
2253
2366
|
getExperiment,
|
|
2254
2367
|
getOrCreateSessionId,
|
|
2255
2368
|
getOrCreateVisitorId,
|
|
@@ -2263,6 +2376,7 @@ function warnUnknown(key) {
|
|
|
2263
2376
|
isActive,
|
|
2264
2377
|
isBot,
|
|
2265
2378
|
isBrowser,
|
|
2379
|
+
isCloudEndpoint,
|
|
2266
2380
|
isCrawler,
|
|
2267
2381
|
isDebug,
|
|
2268
2382
|
isDevDraft,
|
|
@@ -2281,6 +2395,7 @@ function warnUnknown(key) {
|
|
|
2281
2395
|
newPropertyId,
|
|
2282
2396
|
newSessionId,
|
|
2283
2397
|
newVisitorId,
|
|
2398
|
+
normalizeCloudEndpoint,
|
|
2284
2399
|
normalizePath,
|
|
2285
2400
|
normalizeProperty,
|
|
2286
2401
|
onDebug,
|
|
@@ -2292,6 +2407,8 @@ function warnUnknown(key) {
|
|
|
2292
2407
|
readRemoteCache,
|
|
2293
2408
|
redirectTarget,
|
|
2294
2409
|
referrerOrigin,
|
|
2410
|
+
rememberEndpoint,
|
|
2411
|
+
rememberedEndpoint,
|
|
2295
2412
|
resetAnalyticsClaims,
|
|
2296
2413
|
resetDevNotes,
|
|
2297
2414
|
resolveOverridesUrl,
|