Repository navigation
Expand file tree
/
Copy pathHttpUtils.ts
More file actions
303 lines (268 loc) · 14.3 KB
/
Copy pathHttpUtils.ts
File metadata and controls
303 lines (268 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import alert from '@components/Alert';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {RequestType} from '@src/types/onyx/Request';
import type Response from '@src/types/onyx/Response';
import type {fetch as nitroFetch} from 'react-native-nitro-fetch';
import type {OnyxKey} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import Onyx from 'react-native-onyx';
import {setTimeSkew} from './actions/Network';
import {alertUser} from './actions/UpdateRequired';
import {READ_COMMANDS, SIDE_EFFECT_REQUEST_COMMANDS, WRITE_COMMANDS} from './API/types';
import {getCommandURL} from './ApiUtils';
import HttpsError from './Errors/HttpsError';
import {setLoadTestParameters} from './Network/LoadTestState';
import preparePrefetchRequest from './Prefetch/preparePrefetchRequest';
import registerPrefetchOnAppStart from './Prefetch/registerPrefetchOnAppStart';
import prepareRequestPayload from './prepareRequestPayload';
import {cancelSpan, endSpan, endSpanWithAttributes} from './telemetry/activeSpans';
import markAppStartupNetworkRequestEnd from './telemetry/markAppStartupNetworkRequestEnd';
import getRequestPhaseSpanNames, {getNextRequestPhaseAttempt} from './telemetry/measuredRequestPhaseCommands';
import startRequestPhaseSpan, {getRequestPhaseSpanId} from './telemetry/startRequestPhaseSpan';
let shouldFailAllRequests = false;
let shouldForceOffline = false;
const ABORT_COMMANDS = {
All: 'All',
[READ_COMMANDS.SEARCH_FOR_REPORTS]: READ_COMMANDS.SEARCH_FOR_REPORTS,
[READ_COMMANDS.SEARCH_FOR_USERS]: READ_COMMANDS.SEARCH_FOR_USERS,
[SIDE_EFFECT_REQUEST_COMMANDS.OPEN_SEARCH_TAG_FILTERS_PAGE]: SIDE_EFFECT_REQUEST_COMMANDS.OPEN_SEARCH_TAG_FILTERS_PAGE,
} as const;
type AbortCommand = keyof typeof ABORT_COMMANDS;
// We have used `connectWithoutView` here because HttpUtils is not connected to any UI component
Onyx.connectWithoutView({
key: ONYXKEYS.NETWORK,
callback: (network) => {
if (!network) {
return;
}
shouldFailAllRequests = !!network.shouldFailAllRequests;
shouldForceOffline = !!network.shouldForceOffline;
},
});
// We use the AbortController API to terminate pending request in `cancelPendingRequests`
const abortControllerMap = new Map<AbortCommand, AbortController>();
abortControllerMap.set(ABORT_COMMANDS.All, new AbortController());
abortControllerMap.set(ABORT_COMMANDS.SearchForReports, new AbortController());
abortControllerMap.set(ABORT_COMMANDS.SearchForUsers, new AbortController());
abortControllerMap.set(ABORT_COMMANDS.OpenSearchTagFiltersPage, new AbortController());
/**
* The API commands that require the skew calculation
*/
const addSkewList = new Set<string>([WRITE_COMMANDS.OPEN_REPORT, SIDE_EFFECT_REQUEST_COMMANDS.RECONNECT_APP, WRITE_COMMANDS.OPEN_APP]);
/**
* Per-command server response messages we recognize as the PHP-wrapped "AlreadyCreated" error.
* Add new variants here as we discover them for other non-idempotent commands.
*
* DUPLICATE_RECORD is also listed here because the API layer can re-wrap Auth's 400 as a 666. Without it,
* that form matches neither guard and a record that already exists on the server gets rolled back.
*/
const ALREADY_CREATED_MESSAGES = new Set<string>([CONST.ERROR_TITLE.ALREADY_CREATED_TRANSACTION, CONST.ERROR_TITLE.ALREADY_PAID, CONST.ERROR_TITLE.DUPLICATE_RECORD]);
/**
* Regex to get API command from the command
*/
const APICommandRegex = /\/api\/([^&?]+)\??.*/;
/**
* Send an HTTP request, and attempt to resolve the json response.
* If there is a network error, we'll set the application offline.
*/
function processHTTPRequest<TKey extends OnyxKey>(
url: string,
method: RequestType = 'get',
body: FormData | null = null,
abortSignal: AbortSignal | undefined = undefined,
shouldSkipPrefetch = false,
): Promise<Response<TKey>> {
const startTime = new Date().valueOf();
const command = url.match(APICommandRegex)?.[1];
const {prefetchKey, prefetchHeaders} = shouldSkipPrefetch ? {prefetchKey: undefined, prefetchHeaders: undefined} : preparePrefetchRequest(command);
let isPrefetchedResponse = false;
const fetchParams: NonNullable<Parameters<typeof nitroFetch>[1]> = {
// We hook requests to the same Controller signal, so we can cancel them all at once
signal: abortSignal,
method,
body,
headers: prefetchHeaders,
// On Web fetch already defaults to 'omit' for credentials, but it seems that this is not the case for the ReactNative implementation
// so to avoid sending cookies with the request we set it to 'omit' explicitly
// this avoids us sending specially the expensifyWeb cookie, which makes a CSRF token required
// more on that here: https://stackoverflowteams.com/c/expensify/questions/93
credentials: 'omit',
};
registerPrefetchOnAppStart({prefetchKey, fetchParams, command, url});
// Mirrors the "Waiting" / "Content Download" split Chrome shows for this request.
const phaseSpanNames = getRequestPhaseSpanNames(command);
const attempt = phaseSpanNames ? getNextRequestPhaseAttempt(phaseSpanNames.WAIT) : 0;
const waitSpanId = phaseSpanNames ? getRequestPhaseSpanId(phaseSpanNames.WAIT, attempt) : '';
const downloadSpanId = phaseSpanNames ? getRequestPhaseSpanId(phaseSpanNames.DOWNLOAD, attempt) : '';
if (phaseSpanNames && command) {
startRequestPhaseSpan(phaseSpanNames.WAIT, attempt, command);
}
return fetch(url, fetchParams)
.then((response) => {
if (phaseSpanNames && command) {
endSpan(waitSpanId);
startRequestPhaseSpan(phaseSpanNames.DOWNLOAD, attempt, command, {
[CONST.TELEMETRY.ATTRIBUTE_CONTENT_LENGTH]: response.headers?.get('content-length') ?? undefined,
});
}
if (response.headers) {
setLoadTestParameters(response.headers.get('X-Load-Test'));
isPrefetchedResponse = response.headers.get('nitroPrefetched') === 'true';
}
// We are calculating the skew to minimize the delay when posting the messages
if (command && addSkewList.has(command) && response.headers) {
const dateHeaderValue = response.headers.get('Date');
const serverTime = dateHeaderValue ? new Date(dateHeaderValue).valueOf() : new Date().valueOf();
const endTime = new Date().valueOf();
const latency = (endTime - startTime) / 2;
const skew = serverTime - startTime + latency;
setTimeSkew(dateHeaderValue ? skew : 0);
}
return response;
})
.then((response) => {
// Test mode where all requests will succeed in the server, but fail to return a response
if (shouldFailAllRequests || shouldForceOffline) {
throw new HttpsError({
message: CONST.ERROR.FAILED_TO_FETCH,
});
}
if (!response.ok) {
// Expensify site is down or there was an internal server error, or something temporary like a Bad Gateway, or unknown error occurred
const serviceInterruptedStatuses: Array<ValueOf<typeof CONST.HTTP_STATUS>> = [
CONST.HTTP_STATUS.INTERNAL_SERVER_ERROR,
CONST.HTTP_STATUS.BAD_GATEWAY,
CONST.HTTP_STATUS.GATEWAY_TIMEOUT,
CONST.HTTP_STATUS.UNKNOWN_ERROR,
];
if (serviceInterruptedStatuses.indexOf(response.status as ValueOf<typeof CONST.HTTP_STATUS>) > -1) {
throw new HttpsError({
message: CONST.ERROR.EXPENSIFY_SERVICE_INTERRUPTED,
status: response.status.toString(),
title: 'Issue connecting to Expensify site',
});
}
if (response.status === CONST.HTTP_STATUS.TOO_MANY_REQUESTS) {
throw new HttpsError({
message: CONST.ERROR.THROTTLED,
status: response.status.toString(),
title: 'API request throttled',
});
}
throw new HttpsError({
message: response.statusText,
status: response.status.toString(),
});
}
const parsedResponse = response.json() as Promise<Response<TKey>>;
if (!phaseSpanNames) {
return parsedResponse;
}
// The server's requestID only exists once the body is parsed, which is exactly when this phase ends. It ties every phase of one attempt together in Sentry.
return parsedResponse.then(
(parsedBody) => {
endSpanWithAttributes(downloadSpanId, {[CONST.TELEMETRY.ATTRIBUTE_REQUEST_ID]: parsedBody?.requestID});
return parsedBody;
},
(error: unknown) => {
endSpan(downloadSpanId);
throw error;
},
);
})
.then((response) => {
// Some retried requests will result in a "Unique Constraints Violation" error from the server, which just means the record already exists
if (response.jsonCode === CONST.JSON_CODE.BAD_REQUEST && response.message === CONST.ERROR_TITLE.DUPLICATE_RECORD) {
throw new HttpsError({
message: CONST.ERROR.DUPLICATE_RECORD,
status: CONST.JSON_CODE.BAD_REQUEST.toString(),
title: CONST.ERROR_TITLE.DUPLICATE_RECORD,
requestID: response.requestID,
});
}
// Per-command messages indicating the resource already exists on the server (e.g. retry after a successful first attempt).
if (response.jsonCode === CONST.JSON_CODE.EXP_ERROR && response.message && ALREADY_CREATED_MESSAGES.has(response.message)) {
throw new HttpsError({
message: CONST.ERROR.ALREADY_CREATED,
status: CONST.JSON_CODE.EXP_ERROR.toString(),
title: response.message,
});
}
// Auth is down or timed out while making a request
if (response.jsonCode === CONST.JSON_CODE.EXP_ERROR && response.title === CONST.ERROR_TITLE.SOCKET && response.type === CONST.ERROR_TYPE.SOCKET) {
throw new HttpsError({
message: CONST.ERROR.EXPENSIFY_SERVICE_INTERRUPTED,
status: CONST.JSON_CODE.EXP_ERROR.toString(),
title: CONST.ERROR_TITLE.SOCKET,
requestID: response.requestID,
});
}
// The server sheds writes during instability with an app-level 503, which asks us to try again shortly
if (response.jsonCode === CONST.JSON_CODE.SERVICE_UNAVAILABLE) {
throw new HttpsError({
message: CONST.ERROR.SERVICE_UNAVAILABLE,
status: CONST.JSON_CODE.SERVICE_UNAVAILABLE.toString(),
title: CONST.ERROR_TITLE.SERVICE_UNAVAILABLE,
requestID: response.requestID,
});
}
if (response.data && (response.data?.authWriteCommands?.length ?? 0)) {
const {phpCommandName, authWriteCommands} = response.data;
const message = `The API command ${phpCommandName} is doing too many Auth writes. Count ${authWriteCommands.length}, commands: ${authWriteCommands.join(
', ',
)}. If you modified this command, you MUST refactor it to remove the extra Auth writes. Otherwise, update the allowed write count in Web-Expensify APIWriteCommands.`;
alert('Too many auth writes', message);
}
if (response.jsonCode === CONST.JSON_CODE.UPDATE_REQUIRED) {
// A prefetched response can come from a body saved by the previous build, so it carries that build's appversion.
// https://github.com/Expensify/App/pull/103842 leaves appversion out of saved bodies, but bodies saved by older builds still have it.
// Retry without the prefetchKey, because nitro-fetch can serve the same cached response again for a few seconds.
if (isPrefetchedResponse) {
return processHTTPRequest<TKey>(url, method, body, abortSignal, true);
}
// Trigger a modal and disable the app as the user needs to upgrade to the latest minimum version to continue
alertUser();
}
return response;
})
.catch((error: unknown) => {
// A rejected fetch skips the success path above, leaving these spans open to record everything until something else tears them down.
cancelSpan(waitSpanId);
cancelSpan(downloadSpanId);
throw error;
})
.finally(() => markAppStartupNetworkRequestEnd(command));
}
/**
* Makes XHR request
* @param command the name of the API command
* @param data parameters for the API command
* @param type HTTP request type (get/post)
* @param shouldUseSecure should we use the secure server
*/
function xhr<TKey extends OnyxKey>(
command: string,
data: Record<string, unknown>,
type: RequestType = CONST.NETWORK.METHOD.POST,
shouldUseSecure = false,
initiatedOffline = false,
): Promise<Response<TKey>> {
return prepareRequestPayload(command, data, initiatedOffline).then((formData) => {
const url = getCommandURL({shouldUseSecure, command});
const abortSignalController = data.canCancel ? (abortControllerMap.get(command as AbortCommand) ?? abortControllerMap.get(ABORT_COMMANDS.All)) : undefined;
return processHTTPRequest(url, type, formData, abortSignalController?.signal);
});
}
function cancelPendingRequests(command: AbortCommand = ABORT_COMMANDS.All) {
const controller = abortControllerMap.get(command);
controller?.abort();
// We create a new instance because once `abort()` is called any future requests using the same controller would
// automatically get rejected: https://dom.spec.whatwg.org/#abortcontroller-api-integration
abortControllerMap.set(command, new AbortController());
}
export default {
xhr,
cancelPendingRequests,
processHTTPRequest,
};