forked from firefox-devtools/profiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery-api.js
165 lines (150 loc) · 4.95 KB
/
query-api.js
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @flow
import type { ApiQueryError } from 'firefox-profiler/types';
import type { BrowserConnection } from 'firefox-profiler/app-logic/browser-connection';
/**
* An abstraction which simplifies writing tests for things like source and assembly
* fetching. These things get their data from external sources like the browser or the
* network.
*
* In most cases you'll want to use RegularExternalCommunicationDelegate.
*/
export interface ExternalCommunicationDelegate {
// Fetch a cross-origin URL and return its Response. If postData is specified,
// the method should be POST.
fetchUrlResponse(url: string, postData?: MixedObject): Promise<Response>;
// Query the symbolication API of the browser, if a connection to the browser
// is available.
queryBrowserSymbolicationApi(
path: string,
requestJson: string
): Promise<string>;
}
export type ApiQueryResult<T> =
| { type: 'SUCCESS', convertedResponse: T }
| { type: 'ERROR', errors: ApiQueryError[] };
/**
* Sends a JSON query to the browser symbolication API and, if supplied, to a
* symbol server.
*/
export async function queryApiWithFallback<T>(
path: string,
requestJson: string,
symbolServerUrlForFallback: string | null,
delegate: ExternalCommunicationDelegate,
convertJsonResponse: (MixedObject) => T
): Promise<ApiQueryResult<T>> {
const errors: ApiQueryError[] = [];
// See if we can get an API result from the browser.
try {
const response = await delegate.queryBrowserSymbolicationApi(
path,
requestJson
);
try {
const responseJson = JSON.parse(response);
if (!responseJson.error) {
const convertedResponse = convertJsonResponse(responseJson);
return { type: 'SUCCESS', convertedResponse };
}
errors.push({
type: 'BROWSER_API_ERROR',
apiErrorMessage: responseJson.error,
});
} catch (e) {
errors.push({
type: 'BROWSER_API_MALFORMED_RESPONSE',
errorMessage: e.toString(),
});
}
} catch (e) {
errors.push({
type: 'BROWSER_CONNECTION_ERROR',
browserConnectionErrorMessage: e.toString(),
});
}
// See if we can get sources from a local symbol server.
if (symbolServerUrlForFallback !== null) {
const url = symbolServerUrlForFallback + path;
try {
const response = await delegate.fetchUrlResponse(url, requestJson);
const responseText = await response.text();
try {
const responseJson = JSON.parse(responseText);
if (!responseJson.error) {
const convertedResponse = convertJsonResponse(responseJson);
return { type: 'SUCCESS', convertedResponse };
}
errors.push({
type: 'SYMBOL_SERVER_API_ERROR',
apiErrorMessage: responseJson.error,
});
} catch (e) {
errors.push({
type: 'SYMBOL_SERVER_API_MALFORMED_RESPONSE',
errorMessage: e.toString(),
});
}
} catch (e) {
errors.push({
type: 'NETWORK_ERROR',
url,
networkErrorMessage: e.toString(),
});
}
}
return { type: 'ERROR', errors };
}
export interface ExternalCommunicationCallbacks {
onBeginUrlRequest(url: string): void;
onBeginBrowserConnectionQuery(): void;
}
/**
* The default implementation of the ExternalCommunicationDelegate interface.
* Uses fetch for URL requests and the supplied browser connection for browser
* connection requests.
*
* It also takes an object with callbacks, for loading indicators.
*/
export class RegularExternalCommunicationDelegate
implements ExternalCommunicationDelegate
{
_browserConnection: BrowserConnection | null;
_callbacks: ExternalCommunicationCallbacks;
constructor(
browserConnection: BrowserConnection | null,
callbacks: ExternalCommunicationCallbacks
) {
this._browserConnection = browserConnection;
this._callbacks = callbacks;
}
async fetchUrlResponse(url: string, postData?: MixedObject) {
this._callbacks.onBeginUrlRequest(url);
const requestInit =
postData !== undefined
? {
body: postData,
method: 'POST',
mode: 'cors',
credentials: 'omit',
}
: { credentials: 'omit' };
const response = await fetch(url, requestInit);
if (response.status !== 200) {
throw new Error(
`The request to ${url} returned HTTP status ${response.status}`
);
}
return response;
}
async queryBrowserSymbolicationApi(path: string, requestJson: string) {
const browserConnection = this._browserConnection;
if (browserConnection === null) {
throw new Error('No connection to the browser.');
}
this._callbacks.onBeginBrowserConnectionQuery();
return browserConnection.querySymbolicationApi(path, requestJson);
}
}