@blotoutio/providers-evo-search-sdk 1.64.2 → 1.65.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/core.cjs.js +234 -49
- package/core.js +234 -49
- package/core.mjs +234 -49
- package/hooks.d.ts +2 -1
- package/index.cjs.js +281 -136
- package/index.js +281 -136
- package/index.mjs +281 -136
- package/package.json +1 -1
package/index.mjs
CHANGED
|
@@ -817,6 +817,95 @@ const resetRegistry = () => {
|
|
|
817
817
|
notifySubscribers(registry.subscribers, 'resetRegistry');
|
|
818
818
|
};
|
|
819
819
|
|
|
820
|
+
// Resilience wrapper for /search requests. A transient network failure or a
|
|
821
|
+
// hung request must not wedge the UI: add a per-attempt timeout (a hang becomes
|
|
822
|
+
// a rejection), retry transient failures with backoff, and never retry a
|
|
823
|
+
// caller-superseded (aborted) request. Shared by the widget and the headless
|
|
824
|
+
// hook. See callers: search-widget.ts (performAutocomplete) and
|
|
825
|
+
// hooks/use-evo-search.ts.
|
|
826
|
+
const DEFAULTS = {
|
|
827
|
+
timeoutMs: 7000,
|
|
828
|
+
retries: 2,
|
|
829
|
+
backoffMs: [400, 1200],
|
|
830
|
+
};
|
|
831
|
+
// Deterministic HTTP errors (4xx other than 429) will never succeed on
|
|
832
|
+
// retry — only retry timeouts, network errors, 429, and 5xx. Errors without
|
|
833
|
+
// an attached `status` (network/timeout failures) are always retryable.
|
|
834
|
+
const isRetryable = (error) => {
|
|
835
|
+
const status = error === null || error === void 0 ? void 0 : error.status;
|
|
836
|
+
return status == null || status === 429 || status >= 500;
|
|
837
|
+
};
|
|
838
|
+
const delay = (ms, signal) => new Promise((resolve, reject) => {
|
|
839
|
+
if (signal.aborted)
|
|
840
|
+
return reject(new DOMException('Aborted', 'AbortError'));
|
|
841
|
+
const t = setTimeout(resolve, ms);
|
|
842
|
+
signal.addEventListener('abort', () => {
|
|
843
|
+
clearTimeout(t);
|
|
844
|
+
reject(new DOMException('Aborted', 'AbortError'));
|
|
845
|
+
}, { once: true });
|
|
846
|
+
});
|
|
847
|
+
/**
|
|
848
|
+
* Run `attempt` with a timeout, retrying transient failures. `callerSignal`
|
|
849
|
+
* supersedes everything: if it aborts, we stop immediately without retrying
|
|
850
|
+
* (the query changed / the widget cleaned up). A timeout or network error is
|
|
851
|
+
* retried up to `retries` times; anything left over is rethrown.
|
|
852
|
+
*/
|
|
853
|
+
async function withSearchResilience(attempt, callerSignal, options = {}) {
|
|
854
|
+
var _a, _b;
|
|
855
|
+
const { timeoutMs, retries, backoffMs } = { ...DEFAULTS, ...options };
|
|
856
|
+
let lastError;
|
|
857
|
+
for (let i = 0; i <= retries; i++) {
|
|
858
|
+
if (callerSignal.aborted)
|
|
859
|
+
throw new DOMException('Aborted', 'AbortError');
|
|
860
|
+
const local = new AbortController();
|
|
861
|
+
const onCallerAbort = () => local.abort();
|
|
862
|
+
callerSignal.addEventListener('abort', onCallerAbort, { once: true });
|
|
863
|
+
// Race the attempt against an explicit timeout that REJECTS. Aborting the
|
|
864
|
+
// signal alone isn't enough — an attempt that ignores its signal would
|
|
865
|
+
// otherwise hang forever and never retry. Reject with a TimeoutError (not
|
|
866
|
+
// an AbortError) so callers surface it instead of treating it as a
|
|
867
|
+
// caller-cancellation.
|
|
868
|
+
let timer;
|
|
869
|
+
const timeout = new Promise((_, reject) => {
|
|
870
|
+
timer = setTimeout(() => {
|
|
871
|
+
local.abort(); // still cancel the in-flight request to free the socket
|
|
872
|
+
reject(new DOMException('Search request timed out', 'TimeoutError'));
|
|
873
|
+
}, timeoutMs);
|
|
874
|
+
});
|
|
875
|
+
try {
|
|
876
|
+
return await Promise.race([attempt(local.signal), timeout]);
|
|
877
|
+
}
|
|
878
|
+
catch (error) {
|
|
879
|
+
lastError = error;
|
|
880
|
+
// The caller superseded this request — do not retry, propagate the abort.
|
|
881
|
+
if (callerSignal.aborted)
|
|
882
|
+
throw error;
|
|
883
|
+
// Otherwise it was a timeout or a transient network error: retry if budget remains.
|
|
884
|
+
if (i < retries && isRetryable(error)) {
|
|
885
|
+
const wait = (_b = (_a = backoffMs[i]) !== null && _a !== void 0 ? _a : backoffMs[backoffMs.length - 1]) !== null && _b !== void 0 ? _b : 0;
|
|
886
|
+
// Rejects with an AbortError if the caller aborts mid-backoff —
|
|
887
|
+
// propagate that cancellation instead of the transient `error` above.
|
|
888
|
+
await delay(wait, callerSignal);
|
|
889
|
+
continue;
|
|
890
|
+
}
|
|
891
|
+
throw error;
|
|
892
|
+
}
|
|
893
|
+
finally {
|
|
894
|
+
clearTimeout(timer);
|
|
895
|
+
callerSignal.removeEventListener('abort', onCallerAbort);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
throw lastError;
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
// Client-side deadlines for the Ask SSE path. `/search` already gets these via
|
|
902
|
+
// withSearchResilience; Ask is the streaming counterpart. The request timeout
|
|
903
|
+
// matches the resilience wrapper's per-attempt budget; the idle timeout catches
|
|
904
|
+
// a stream that stalls mid-flight without a terminal frame and resets on every
|
|
905
|
+
// chunk. Both surface a TimeoutError (not an AbortError) so submitAsk routes
|
|
906
|
+
// them to the search fallback instead of treating them as caller-cancellation.
|
|
907
|
+
const ASK_REQUEST_TIMEOUT_MS = 7000;
|
|
908
|
+
const ASK_SSE_IDLE_TIMEOUT_MS = 10000;
|
|
820
909
|
// Always defer to what the storefront itself is rendering — same source
|
|
821
910
|
// shop-gpt reads from. Two widgets on the same page would otherwise drift:
|
|
822
911
|
// shop-gpt picks Shopify, evo-search would pick its own override and show
|
|
@@ -844,15 +933,28 @@ const createEvoSearchAPI = ({ fetch: fetchImpl = window.fetch, baseURL, userId,
|
|
|
844
933
|
if (!sendTag) {
|
|
845
934
|
throw new Error('sendTag missing');
|
|
846
935
|
}
|
|
936
|
+
const getChannel = () => { var _a; return (((_a = window[registryKey]) === null || _a === void 0 ? void 0 : _a.ui) ? 'widget' : 'headless'); };
|
|
847
937
|
const getHeaders = () => {
|
|
848
|
-
var _a;
|
|
849
938
|
const headers = new Headers({ EdgeTagUserId: userId });
|
|
850
939
|
if (sessionId) {
|
|
851
940
|
headers.set('EdgeTagSessionId', sessionId);
|
|
852
941
|
}
|
|
853
|
-
headers.set('X-EvoSearch-Channel', (
|
|
942
|
+
headers.set('X-EvoSearch-Channel', getChannel());
|
|
854
943
|
return headers;
|
|
855
944
|
};
|
|
945
|
+
// Identity for the public read endpoints travels as query params instead of
|
|
946
|
+
// headers so the GET stays a CORS-simple request — no OPTIONS preflight,
|
|
947
|
+
// one connection attempt per keystroke instead of two (a hung transport
|
|
948
|
+
// path then wedges half as often, and withSearchResilience's retry isn't
|
|
949
|
+
// preceded by a second preflight). /user/* keeps the header form: the
|
|
950
|
+
// worker's cookie-HMAC auth binds to it, and those routes preflight anyway.
|
|
951
|
+
const setIdentityParams = (url) => {
|
|
952
|
+
url.searchParams.set('uid', userId);
|
|
953
|
+
if (sessionId) {
|
|
954
|
+
url.searchParams.set('sid', sessionId);
|
|
955
|
+
}
|
|
956
|
+
url.searchParams.set('channel', getChannel());
|
|
957
|
+
};
|
|
856
958
|
// Parsing `baseURL` once at API construction avoids re-parsing the host on
|
|
857
959
|
// every keystroke when `getURL` runs inside the autocomplete hot path.
|
|
858
960
|
const baseOrigin = new URL(baseURL).origin;
|
|
@@ -875,10 +977,14 @@ const createEvoSearchAPI = ({ fetch: fetchImpl = window.fetch, baseURL, userId,
|
|
|
875
977
|
for (const [key, value] of Object.entries(params !== null && params !== void 0 ? params : {})) {
|
|
876
978
|
url.searchParams.set(key, value);
|
|
877
979
|
}
|
|
980
|
+
const credentialed = needsCredentials(path);
|
|
981
|
+
if (!credentialed) {
|
|
982
|
+
setIdentityParams(url);
|
|
983
|
+
}
|
|
878
984
|
const response = await fetchImpl(url, {
|
|
879
985
|
method: 'GET',
|
|
880
|
-
headers: getHeaders(),
|
|
881
|
-
credentials:
|
|
986
|
+
...(credentialed ? { headers: getHeaders() } : {}),
|
|
987
|
+
credentials: credentialed ? 'include' : 'omit',
|
|
882
988
|
signal,
|
|
883
989
|
});
|
|
884
990
|
if (!response.ok) {
|
|
@@ -898,17 +1004,38 @@ const createEvoSearchAPI = ({ fetch: fetchImpl = window.fetch, baseURL, userId,
|
|
|
898
1004
|
logger.info('EvoSearch API: Fetching ask config');
|
|
899
1005
|
return getJSON('/config', 'Config', undefined, signal);
|
|
900
1006
|
};
|
|
901
|
-
//
|
|
902
|
-
// index off the request path
|
|
903
|
-
//
|
|
904
|
-
//
|
|
1007
|
+
// Isolate warmer doubling as connection establisher: primes the worker's
|
|
1008
|
+
// search index off the request path AND opens the h2/h3 connection before
|
|
1009
|
+
// the first real keystroke needs it. Runs under the resilience wrapper so a
|
|
1010
|
+
// black-holed connection attempt (hung QUIC/IPv6 path) is aborted and
|
|
1011
|
+
// retried — the retry is what lets the browser fall back to a working
|
|
1012
|
+
// transport while the shopper is still reaching for the search box. Any
|
|
1013
|
+
// HTTP response counts as success: even an error status proves the
|
|
1014
|
+
// connection is established. Never rejects; concurrent triggers (open,
|
|
1015
|
+
// focus, first keystroke, keep-warm tick) coalesce into one attempt.
|
|
1016
|
+
let warmInFlight = null;
|
|
1017
|
+
let warmAbort = null;
|
|
905
1018
|
const warm = () => {
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
1019
|
+
if (warmInFlight) {
|
|
1020
|
+
return warmInFlight;
|
|
1021
|
+
}
|
|
1022
|
+
const url = getURL('/warm');
|
|
1023
|
+
setIdentityParams(url);
|
|
1024
|
+
warmAbort = new AbortController();
|
|
1025
|
+
warmInFlight = withSearchResilience((signal) => fetchImpl(url, { method: 'GET', credentials: 'omit', signal }), warmAbort.signal)
|
|
1026
|
+
.then(() => undefined)
|
|
1027
|
+
.catch(() => undefined)
|
|
1028
|
+
.finally(() => {
|
|
1029
|
+
warmInFlight = null;
|
|
1030
|
+
warmAbort = null;
|
|
1031
|
+
});
|
|
1032
|
+
return warmInFlight;
|
|
911
1033
|
};
|
|
1034
|
+
// Closing the widget must kill the chain: a black-holed attempt would
|
|
1035
|
+
// otherwise keep re-establishing connections for the wrapper's full budget
|
|
1036
|
+
// (~23s) after the overlay is gone — exactly the churn warm() exists to
|
|
1037
|
+
// avoid. The next warm() mints a fresh controller.
|
|
1038
|
+
const cancelWarm = () => warmAbort === null || warmAbort === void 0 ? void 0 : warmAbort.abort();
|
|
912
1039
|
const recents = (signal) => {
|
|
913
1040
|
logger.info('EvoSearch API: Fetching recent searches');
|
|
914
1041
|
return getJSON('/user/recents', 'Recents', undefined, signal);
|
|
@@ -949,50 +1076,107 @@ const createEvoSearchAPI = ({ fetch: fetchImpl = window.fetch, baseURL, userId,
|
|
|
949
1076
|
// reader, the for-await loop drops out, and the iterator returns.
|
|
950
1077
|
async function* askStream(query, signal) {
|
|
951
1078
|
const url = getURL('/ask');
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
1079
|
+
// One controller drives teardown for the whole stream: the caller's signal,
|
|
1080
|
+
// the initial-request deadline, and the SSE idle timeout all abort through
|
|
1081
|
+
// it, so a stall closes the fetch instead of hanging. `timedOut` lets us
|
|
1082
|
+
// rethrow a TimeoutError rather than the AbortError the abort would raise.
|
|
1083
|
+
const controller = new AbortController();
|
|
1084
|
+
const onCallerAbort = () => controller.abort();
|
|
1085
|
+
signal === null || signal === void 0 ? void 0 : signal.addEventListener('abort', onCallerAbort, { once: true });
|
|
1086
|
+
if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
|
|
1087
|
+
controller.abort();
|
|
1088
|
+
}
|
|
1089
|
+
let timedOut = false;
|
|
1090
|
+
const initialTimer = setTimeout(() => {
|
|
1091
|
+
timedOut = true;
|
|
1092
|
+
controller.abort();
|
|
1093
|
+
}, ASK_REQUEST_TIMEOUT_MS);
|
|
1094
|
+
// Outer try guarantees the caller-signal listener is removed on EVERY exit
|
|
1095
|
+
// path. It's `{ once: true }`, so a caller abort auto-removes it — but the
|
|
1096
|
+
// initial-timeout, network-error, and non-ok paths never fire the abort
|
|
1097
|
+
// event, so without this they'd leak a listener on a long-lived shared
|
|
1098
|
+
// signal (one per Ask query).
|
|
970
1099
|
try {
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
1100
|
+
let response;
|
|
1101
|
+
try {
|
|
1102
|
+
response = await fetchImpl(url, {
|
|
1103
|
+
method: 'POST',
|
|
1104
|
+
headers: (() => {
|
|
1105
|
+
const h = getHeaders();
|
|
1106
|
+
h.set('Content-Type', 'application/json');
|
|
1107
|
+
return h;
|
|
1108
|
+
})(),
|
|
1109
|
+
body: JSON.stringify({ query }),
|
|
1110
|
+
credentials: 'omit',
|
|
1111
|
+
signal: controller.signal,
|
|
1112
|
+
});
|
|
1113
|
+
}
|
|
1114
|
+
catch (error) {
|
|
1115
|
+
if (timedOut) {
|
|
1116
|
+
throw new DOMException('Ask request timed out', 'TimeoutError');
|
|
975
1117
|
}
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
1118
|
+
throw error;
|
|
1119
|
+
}
|
|
1120
|
+
finally {
|
|
1121
|
+
// The idle timeout takes over once the response resolves.
|
|
1122
|
+
clearTimeout(initialTimer);
|
|
1123
|
+
}
|
|
1124
|
+
if (!response.ok || !response.body) {
|
|
1125
|
+
throw new Error(`Ask failed - ${response.status}`);
|
|
1126
|
+
}
|
|
1127
|
+
const reader = response.body
|
|
1128
|
+
.pipeThrough(new TextDecoderStream())
|
|
1129
|
+
.getReader();
|
|
1130
|
+
let buffer = '';
|
|
1131
|
+
try {
|
|
1132
|
+
while (true) {
|
|
1133
|
+
// Idle timeout, reset every chunk: a stream that stalls mid-flight
|
|
1134
|
+
// (no terminal frame) rejects here instead of freezing the skeleton.
|
|
1135
|
+
let idleTimer;
|
|
1136
|
+
const idle = new Promise((_, reject) => {
|
|
1137
|
+
idleTimer = setTimeout(() => {
|
|
1138
|
+
timedOut = true;
|
|
1139
|
+
controller.abort();
|
|
1140
|
+
reject(new DOMException('Ask stream stalled', 'TimeoutError'));
|
|
1141
|
+
}, ASK_SSE_IDLE_TIMEOUT_MS);
|
|
1142
|
+
});
|
|
1143
|
+
let result;
|
|
1144
|
+
try {
|
|
1145
|
+
result = await Promise.race([reader.read(), idle]);
|
|
984
1146
|
}
|
|
985
|
-
|
|
1147
|
+
finally {
|
|
1148
|
+
clearTimeout(idleTimer);
|
|
1149
|
+
}
|
|
1150
|
+
const { done, value } = result;
|
|
1151
|
+
if (done) {
|
|
1152
|
+
break;
|
|
1153
|
+
}
|
|
1154
|
+
buffer += value;
|
|
1155
|
+
let boundary = buffer.indexOf('\n\n');
|
|
1156
|
+
while (boundary !== -1) {
|
|
1157
|
+
const frame = buffer.slice(0, boundary);
|
|
1158
|
+
buffer = buffer.slice(boundary + 2);
|
|
1159
|
+
const event = parseSseFrame(frame);
|
|
1160
|
+
if (event) {
|
|
1161
|
+
yield event;
|
|
1162
|
+
}
|
|
1163
|
+
boundary = buffer.indexOf('\n\n');
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
finally {
|
|
1168
|
+
try {
|
|
1169
|
+
// cancel (not releaseLock) — safe with an outstanding read and tears
|
|
1170
|
+
// down the upstream fetch on a caller-abort or idle timeout.
|
|
1171
|
+
await reader.cancel();
|
|
1172
|
+
}
|
|
1173
|
+
catch {
|
|
1174
|
+
// ignore — already closed
|
|
986
1175
|
}
|
|
987
1176
|
}
|
|
988
1177
|
}
|
|
989
1178
|
finally {
|
|
990
|
-
|
|
991
|
-
reader.releaseLock();
|
|
992
|
-
}
|
|
993
|
-
catch {
|
|
994
|
-
// ignore
|
|
995
|
-
}
|
|
1179
|
+
signal === null || signal === void 0 ? void 0 : signal.removeEventListener('abort', onCallerAbort);
|
|
996
1180
|
}
|
|
997
1181
|
}
|
|
998
1182
|
const sendEvent = (action, currency, actionData, clickData, beacon) => {
|
|
@@ -1044,6 +1228,7 @@ const createEvoSearchAPI = ({ fetch: fetchImpl = window.fetch, baseURL, userId,
|
|
|
1044
1228
|
trending,
|
|
1045
1229
|
config,
|
|
1046
1230
|
warm,
|
|
1231
|
+
cancelWarm,
|
|
1047
1232
|
user: {
|
|
1048
1233
|
recents,
|
|
1049
1234
|
deleteRecent,
|
|
@@ -8307,88 +8492,13 @@ const searchWidgetStyles = i$6 `
|
|
|
8307
8492
|
}
|
|
8308
8493
|
`;
|
|
8309
8494
|
|
|
8310
|
-
// Resilience wrapper for /search requests. A transient network failure or a
|
|
8311
|
-
// hung request must not wedge the UI: add a per-attempt timeout (a hang becomes
|
|
8312
|
-
// a rejection), retry transient failures with backoff, and never retry a
|
|
8313
|
-
// caller-superseded (aborted) request. Shared by the widget and the headless
|
|
8314
|
-
// hook. See callers: search-widget.ts (performAutocomplete) and
|
|
8315
|
-
// hooks/use-evo-search.ts.
|
|
8316
|
-
const DEFAULTS = {
|
|
8317
|
-
timeoutMs: 7000,
|
|
8318
|
-
retries: 2,
|
|
8319
|
-
backoffMs: [400, 1200],
|
|
8320
|
-
};
|
|
8321
|
-
// Deterministic HTTP errors (4xx other than 429) will never succeed on
|
|
8322
|
-
// retry — only retry timeouts, network errors, 429, and 5xx. Errors without
|
|
8323
|
-
// an attached `status` (network/timeout failures) are always retryable.
|
|
8324
|
-
const isRetryable = (error) => {
|
|
8325
|
-
const status = error === null || error === void 0 ? void 0 : error.status;
|
|
8326
|
-
return status == null || status === 429 || status >= 500;
|
|
8327
|
-
};
|
|
8328
|
-
const delay = (ms, signal) => new Promise((resolve, reject) => {
|
|
8329
|
-
if (signal.aborted)
|
|
8330
|
-
return reject(new DOMException('Aborted', 'AbortError'));
|
|
8331
|
-
const t = setTimeout(resolve, ms);
|
|
8332
|
-
signal.addEventListener('abort', () => {
|
|
8333
|
-
clearTimeout(t);
|
|
8334
|
-
reject(new DOMException('Aborted', 'AbortError'));
|
|
8335
|
-
}, { once: true });
|
|
8336
|
-
});
|
|
8337
|
-
/**
|
|
8338
|
-
* Run `attempt` with a timeout, retrying transient failures. `callerSignal`
|
|
8339
|
-
* supersedes everything: if it aborts, we stop immediately without retrying
|
|
8340
|
-
* (the query changed / the widget cleaned up). A timeout or network error is
|
|
8341
|
-
* retried up to `retries` times; anything left over is rethrown.
|
|
8342
|
-
*/
|
|
8343
|
-
async function withSearchResilience(attempt, callerSignal, options = {}) {
|
|
8344
|
-
var _a, _b;
|
|
8345
|
-
const { timeoutMs, retries, backoffMs } = { ...DEFAULTS, ...options };
|
|
8346
|
-
let lastError;
|
|
8347
|
-
for (let i = 0; i <= retries; i++) {
|
|
8348
|
-
if (callerSignal.aborted)
|
|
8349
|
-
throw new DOMException('Aborted', 'AbortError');
|
|
8350
|
-
const local = new AbortController();
|
|
8351
|
-
const onCallerAbort = () => local.abort();
|
|
8352
|
-
callerSignal.addEventListener('abort', onCallerAbort, { once: true });
|
|
8353
|
-
// Race the attempt against an explicit timeout that REJECTS. Aborting the
|
|
8354
|
-
// signal alone isn't enough — an attempt that ignores its signal would
|
|
8355
|
-
// otherwise hang forever and never retry. Reject with a TimeoutError (not
|
|
8356
|
-
// an AbortError) so callers surface it instead of treating it as a
|
|
8357
|
-
// caller-cancellation.
|
|
8358
|
-
let timer;
|
|
8359
|
-
const timeout = new Promise((_, reject) => {
|
|
8360
|
-
timer = setTimeout(() => {
|
|
8361
|
-
local.abort(); // still cancel the in-flight request to free the socket
|
|
8362
|
-
reject(new DOMException('Search request timed out', 'TimeoutError'));
|
|
8363
|
-
}, timeoutMs);
|
|
8364
|
-
});
|
|
8365
|
-
try {
|
|
8366
|
-
return await Promise.race([attempt(local.signal), timeout]);
|
|
8367
|
-
}
|
|
8368
|
-
catch (error) {
|
|
8369
|
-
lastError = error;
|
|
8370
|
-
// The caller superseded this request — do not retry, propagate the abort.
|
|
8371
|
-
if (callerSignal.aborted)
|
|
8372
|
-
throw error;
|
|
8373
|
-
// Otherwise it was a timeout or a transient network error: retry if budget remains.
|
|
8374
|
-
if (i < retries && isRetryable(error)) {
|
|
8375
|
-
const wait = (_b = (_a = backoffMs[i]) !== null && _a !== void 0 ? _a : backoffMs[backoffMs.length - 1]) !== null && _b !== void 0 ? _b : 0;
|
|
8376
|
-
// Rejects with an AbortError if the caller aborts mid-backoff —
|
|
8377
|
-
// propagate that cancellation instead of the transient `error` above.
|
|
8378
|
-
await delay(wait, callerSignal);
|
|
8379
|
-
continue;
|
|
8380
|
-
}
|
|
8381
|
-
throw error;
|
|
8382
|
-
}
|
|
8383
|
-
finally {
|
|
8384
|
-
clearTimeout(timer);
|
|
8385
|
-
callerSignal.removeEventListener('abort', onCallerAbort);
|
|
8386
|
-
}
|
|
8387
|
-
}
|
|
8388
|
-
throw lastError;
|
|
8389
|
-
}
|
|
8390
|
-
|
|
8391
8495
|
const MIN_QUERY_LENGTH = 3;
|
|
8496
|
+
// Keep-warm cadence while the widget is open. Chrome reaps used-idle sockets
|
|
8497
|
+
// after ~5 minutes and the edge sooner than that under load; a ping well
|
|
8498
|
+
// inside those windows keeps the established h2/h3 connection alive so a
|
|
8499
|
+
// keystroke after an idle pause multiplexes onto it instead of paying (and
|
|
8500
|
+
// possibly wedging on) fresh connection establishment.
|
|
8501
|
+
const KEEP_WARM_INTERVAL_MS = 60000;
|
|
8392
8502
|
// @state fields whose change should be persisted to the session snapshot.
|
|
8393
8503
|
const SESSION_PERSIST_KEYS = [
|
|
8394
8504
|
'searchQuery',
|
|
@@ -8554,21 +8664,55 @@ class SearchWidget extends ElementWithStylesheet {
|
|
|
8554
8664
|
this.isMobile = this.mobileMediaQuery.matches;
|
|
8555
8665
|
this.mobileMediaQuery.addEventListener('change', this.mobileMediaListener);
|
|
8556
8666
|
window.addEventListener('online', this.onlineListener);
|
|
8667
|
+
// Restart keep-warm when the same instance re-attaches (a host re-append
|
|
8668
|
+
// moves the subtree and fires disconnected→connected without a fresh
|
|
8669
|
+
// firstUpdated). On the very first connect `api` may not be bound yet —
|
|
8670
|
+
// firstUpdated repeats the call below and the timer guard dedupes.
|
|
8671
|
+
this.startKeepWarm();
|
|
8557
8672
|
}
|
|
8558
8673
|
firstUpdated() {
|
|
8559
8674
|
this.focusSearchBox();
|
|
8560
8675
|
this.fetchRecents();
|
|
8676
|
+
// Establish the worker connection while the shopper is still reaching for
|
|
8677
|
+
// the keyboard, and re-ping while the widget stays open so it never idles
|
|
8678
|
+
// out. warm() coalesces with the focus/first-keystroke triggers.
|
|
8679
|
+
this.startKeepWarm();
|
|
8680
|
+
}
|
|
8681
|
+
startKeepWarm() {
|
|
8682
|
+
var _a, _b;
|
|
8683
|
+
// The immediate ping stays outside the timer guard: warm() coalesces, so
|
|
8684
|
+
// a duplicate call is free, and skipping it would lose the first-open
|
|
8685
|
+
// preconnect (api binds between connectedCallback and firstUpdated).
|
|
8686
|
+
void ((_b = (_a = this.api) === null || _a === void 0 ? void 0 : _a.warm) === null || _b === void 0 ? void 0 : _b.call(_a));
|
|
8687
|
+
if (this.keepWarmTimer !== undefined) {
|
|
8688
|
+
return;
|
|
8689
|
+
}
|
|
8690
|
+
this.keepWarmTimer = setInterval(() => {
|
|
8691
|
+
var _a, _b;
|
|
8692
|
+
// An overlay left open in a backgrounded tab has no keystroke coming,
|
|
8693
|
+
// and /warm shares the per-IP rate-limit bucket with /search — don't
|
|
8694
|
+
// spend a NAT'd storefront's budget on a shopper who isn't looking.
|
|
8695
|
+
// Focus and the first keystroke both re-warm when they return.
|
|
8696
|
+
if (document.hidden) {
|
|
8697
|
+
return;
|
|
8698
|
+
}
|
|
8699
|
+
void ((_b = (_a = this.api) === null || _a === void 0 ? void 0 : _a.warm) === null || _b === void 0 ? void 0 : _b.call(_a));
|
|
8700
|
+
}, KEEP_WARM_INTERVAL_MS);
|
|
8561
8701
|
}
|
|
8562
8702
|
disconnectedCallback() {
|
|
8563
|
-
var _a, _b, _c;
|
|
8703
|
+
var _a, _b, _c, _d, _e;
|
|
8564
8704
|
// Closing the widget settles whatever query was on screen.
|
|
8565
8705
|
this.finalizeSettledQuery();
|
|
8566
8706
|
clearTimeout(this.autocompleteTimer);
|
|
8567
|
-
(
|
|
8568
|
-
|
|
8707
|
+
clearInterval(this.keepWarmTimer);
|
|
8708
|
+
this.keepWarmTimer = undefined;
|
|
8709
|
+
// Stops an in-flight warm retry chain from outliving the overlay.
|
|
8710
|
+
(_b = (_a = this.api) === null || _a === void 0 ? void 0 : _a.cancelWarm) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
8711
|
+
(_c = this.askAbort) === null || _c === void 0 ? void 0 : _c.abort();
|
|
8712
|
+
(_d = this.resultsAbort) === null || _d === void 0 ? void 0 : _d.abort();
|
|
8569
8713
|
this.resetAnswerBuffer();
|
|
8570
8714
|
this.removeEventListener('keydown', this.keydownListener);
|
|
8571
|
-
(
|
|
8715
|
+
(_e = this.mobileMediaQuery) === null || _e === void 0 ? void 0 : _e.removeEventListener('change', this.mobileMediaListener);
|
|
8572
8716
|
window.removeEventListener('online', this.onlineListener);
|
|
8573
8717
|
super.disconnectedCallback();
|
|
8574
8718
|
}
|
|
@@ -9034,7 +9178,8 @@ class SearchWidget extends ElementWithStylesheet {
|
|
|
9034
9178
|
if (!this.api) {
|
|
9035
9179
|
return;
|
|
9036
9180
|
}
|
|
9037
|
-
const
|
|
9181
|
+
const api = this.api;
|
|
9182
|
+
const response = await withSearchResilience((signal) => api.search(query, undefined, signal), this.newResultsSignal());
|
|
9038
9183
|
if (generation !== this.requestGeneration) {
|
|
9039
9184
|
return;
|
|
9040
9185
|
}
|