MCPcopy Create free account
hub / github.com/reactnativecn/react-native-update / executeEndpointFallback

Function executeEndpointFallback

src/endpoint.ts:111–171  ·  view source on GitHub ↗
({
  configuredEndpoints,
  getRemoteEndpoints,
  tryEndpoint,
  random = Math.random,
  now = Date.now,
  onFirstFailure,
}: ExecuteEndpointFallbackOptions<T>)

Source from the content-addressed store, hash-verified

109}
110
111export async function executeEndpointFallback<T>({
112 configuredEndpoints,
113 getRemoteEndpoints,
114 tryEndpoint,
115 random = Math.random,
116 now = Date.now,
117 onFirstFailure,
118}: ExecuteEndpointFallbackOptions<T>): Promise<EndpointAttemptSuccess<T>> {
119 const excludedEndpoints = new Set<string>();
120 let candidates = dedupeEndpoints(configuredEndpoints);
121
122 if (!candidates.length) {
123 throw new Error('No endpoints configured');
124 }
125
126 const firstEndpoint = pickRandomEndpoint(candidates, random);
127
128 try {
129 return {
130 endpoint: firstEndpoint,
131 value: await tryEndpoint(firstEndpoint),
132 duration: 0,
133 };
134 } catch (error) {
135 const firstFailure = {
136 endpoint: firstEndpoint,
137 error: normalizeError(error),
138 };
139 excludedEndpoints.add(firstEndpoint);
140 await onFirstFailure?.(firstFailure);
141 let lastError = firstFailure.error;
142
143 while (true) {
144 const remoteEndpoints = getRemoteEndpoints
145 ? await getRemoteEndpoints().catch(() => [])
146 : [];
147 candidates = dedupeEndpoints([...candidates, ...remoteEndpoints]).filter(
148 endpoint => !excludedEndpoints.has(endpoint),
149 );
150
151 if (!candidates.length) {
152 throw lastError;
153 }
154
155 const { successes, failures } = await selectFastestSuccessfulEndpoint(
156 candidates,
157 tryEndpoint,
158 now,
159 );
160
161 if (successes.length) {
162 return successes[0];
163 }
164
165 for (const failure of failures) {
166 excludedEndpoints.add(failure.endpoint);
167 lastError = failure.error;
168 }

Callers 1

endpoint.test.tsFile · 0.90

Calls 4

dedupeEndpointsFunction · 0.85
pickRandomEndpointFunction · 0.85
normalizeErrorFunction · 0.85

Tested by

no test coverage detected