-
-
Notifications
You must be signed in to change notification settings - Fork 750
/
Copy pathajax.ts
297 lines (275 loc) · 11.4 KB
/
ajax.ts
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
import {extend, isWorker} from './util';
import {createAbortError} from './abort_error';
import {getProtocol} from '../source/protocol_crud';
import {MessageType} from './actor_messages';
/**
* This is used to identify the global dispatcher id when sending a message from the worker without a target map id.
*/
export const GLOBAL_DISPATCHER_ID = 'global-dispatcher';
/**
* A type used to store the tile's expiration date and cache control definition
*/
export type ExpiryData = {cacheControl?: string | null; expires?: Date | string | null};
/**
* A `RequestParameters` object to be returned from Map.options.transformRequest callbacks.
* @example
* ```ts
* // use transformRequest to modify requests that begin with `http://myHost`
* transformRequest: function(url, resourceType) {
* if (resourceType === 'Source' && url.indexOf('http://myHost') > -1) {
* return {
* url: url.replace('http', 'https'),
* headers: { 'my-custom-header': true },
* credentials: 'include' // Include cookies for cross-origin requests
* }
* }
* }
* ```
*/
export type RequestParameters = {
/**
* The URL to be requested.
*/
url: string;
/**
* The headers to be sent with the request.
*/
headers?: any;
/**
* Request method `'GET' | 'POST' | 'PUT'`.
*/
method?: 'GET' | 'POST' | 'PUT';
/**
* Request body.
*/
body?: string;
/**
* Response body type to be returned.
*/
type?: 'string' | 'json' | 'arrayBuffer' | 'image';
/**
* `'same-origin'|'include'` Use 'include' to send cookies with cross-origin requests.
*/
credentials?: 'same-origin' | 'include';
/**
* If `true`, Resource Timing API information will be collected for these transformed requests and returned in a resourceTiming property of relevant data events.
*/
collectResourceTiming?: boolean;
/**
* Parameters supported only by browser fetch API. Property of the Request interface contains the cache mode of the request. It controls how the request will interact with the browser's HTTP cache. (https://developer.mozilla.org/en-US/docs/Web/API/Request/cache)
*/
cache?: RequestCache;
};
/**
* The response object returned from a successful AJAx request
*/
export type GetResourceResponse<T> = ExpiryData & {
data: T;
}
/**
* The response callback used in various places
*/
export type ResponseCallback<T> = (
error?: Error | null,
data?: T | null,
cacheControl?: string | null,
expires?: string | Date | null
) => void;
/**
* An error thrown when a HTTP request results in an error response.
*/
export class AJAXError extends Error {
/**
* The response's HTTP status code.
*/
status: number;
/**
* The response's HTTP status text.
*/
statusText: string;
/**
* The request's URL.
*/
url: string;
/**
* The response's body.
*/
body: Blob;
/**
* @param status - The response's HTTP status code.
* @param statusText - The response's HTTP status text.
* @param url - The request's URL.
* @param body - The response's body.
*/
constructor(status: number, statusText: string, url: string, body: Blob) {
super(`AJAXError: ${statusText} (${status}): ${url}`);
this.status = status;
this.statusText = statusText;
this.url = url;
this.body = body;
}
}
/**
* Ensure that we're sending the correct referrer from blob URL worker bundles.
* For files loaded from the local file system, `location.origin` will be set
* to the string(!) "null" (Firefox), or "file://" (Chrome, Safari, Edge),
* and we will set an empty referrer. Otherwise, we're using the document's URL.
*/
export const getReferrer = () => isWorker(self) ?
self.worker && self.worker.referrer :
(window.location.protocol === 'blob:' ? window.parent : window).location.href;
/**
* Determines whether a URL is a file:// URL. This is obviously the case if it begins
* with file://. Relative URLs are also file:// URLs iff the original document was loaded
* via a file:// URL.
* @param url - The URL to check
* @returns `true` if the URL is a file:// URL, `false` otherwise
*/
const isFileURL = url => /^file:/.test(url) || (/^file:/.test(getReferrer()) && !/^\w+:/.test(url));
async function makeFetchRequest(requestParameters: RequestParameters, abortController: AbortController): Promise<GetResourceResponse<any>> {
const request = new Request(requestParameters.url, {
method: requestParameters.method || 'GET',
body: requestParameters.body,
credentials: requestParameters.credentials,
headers: requestParameters.headers,
cache: requestParameters.cache,
referrer: getReferrer(),
signal: abortController.signal
});
// If the user has already set an Accept header, do not overwrite it here
if (requestParameters.type === 'json' && !request.headers.has('Accept')) {
request.headers.set('Accept', 'application/json');
}
const response = await fetch(request);
if (!response.ok) {
const body = await response.blob();
throw new AJAXError(response.status, response.statusText, requestParameters.url, body);
}
let parsePromise: Promise<any>;
if ((requestParameters.type === 'arrayBuffer' || requestParameters.type === 'image')) {
parsePromise = response.arrayBuffer();
} else if (requestParameters.type === 'json') {
parsePromise = response.json();
} else {
parsePromise = response.text();
}
const result = await parsePromise;
if (abortController.signal.aborted) {
throw createAbortError();
}
return {data: result, cacheControl: response.headers.get('Cache-Control'), expires: response.headers.get('Expires')};
}
function makeXMLHttpRequest(requestParameters: RequestParameters, abortController: AbortController): Promise<GetResourceResponse<any>> {
return new Promise((resolve, reject) => {
const xhr: XMLHttpRequest = new XMLHttpRequest();
xhr.open(requestParameters.method || 'GET', requestParameters.url, true);
if (requestParameters.type === 'arrayBuffer' || requestParameters.type === 'image') {
xhr.responseType = 'arraybuffer';
}
for (const k in requestParameters.headers) {
xhr.setRequestHeader(k, requestParameters.headers[k]);
}
if (requestParameters.type === 'json') {
xhr.responseType = 'text';
// Do not overwrite the user-provided Accept header
if (!requestParameters.headers?.Accept) {
xhr.setRequestHeader('Accept', 'application/json');
}
}
xhr.withCredentials = requestParameters.credentials === 'include';
xhr.onerror = () => {
reject(new Error(xhr.statusText));
};
xhr.onload = () => {
if (abortController.signal.aborted) {
return;
}
if (((xhr.status >= 200 && xhr.status < 300) || xhr.status === 0) && xhr.response !== null) {
let data: unknown = xhr.response;
if (requestParameters.type === 'json') {
// We're manually parsing JSON here to get better error messages.
try {
data = JSON.parse(xhr.response);
} catch (err) {
reject(err);
return;
}
}
resolve({data, cacheControl: xhr.getResponseHeader('Cache-Control'), expires: xhr.getResponseHeader('Expires')});
} else {
const body = new Blob([xhr.response], {type: xhr.getResponseHeader('Content-Type')});
reject(new AJAXError(xhr.status, xhr.statusText, requestParameters.url, body));
}
};
abortController.signal.addEventListener('abort', () => {
xhr.abort();
reject(createAbortError());
});
xhr.send(requestParameters.body);
});
}
/**
* We're trying to use the Fetch API if possible. However, requests for resources with the file:// URI scheme don't work with the Fetch API.
* In this case we unconditionally use XHR on the current thread since referrers don't matter.
* This method can also use the registered method if `addProtocol` was called.
* @param requestParameters - The request parameters
* @param abortController - The abort controller allowing to cancel the request
* @returns a promise resolving to the response, including cache control and expiry data
*/
export const makeRequest = function(requestParameters: RequestParameters, abortController: AbortController): Promise<GetResourceResponse<any>> {
if (/:\/\//.test(requestParameters.url) && !(/^https?:|^file:/.test(requestParameters.url))) {
const protocolLoadFn = getProtocol(requestParameters.url);
if (protocolLoadFn) {
return protocolLoadFn(requestParameters, abortController);
}
if (isWorker(self) && self.worker && self.worker.actor) {
return self.worker.actor.sendAsync({type: MessageType.getResource, data: requestParameters, targetMapId: GLOBAL_DISPATCHER_ID}, abortController);
}
}
if (!isFileURL(requestParameters.url)) {
if (fetch && Request && AbortController && Object.prototype.hasOwnProperty.call(Request.prototype, 'signal')) {
return makeFetchRequest(requestParameters, abortController);
}
if (isWorker(self) && self.worker && self.worker.actor) {
return self.worker.actor.sendAsync({type: MessageType.getResource, data: requestParameters, mustQueue: true, targetMapId: GLOBAL_DISPATCHER_ID}, abortController);
}
}
return makeXMLHttpRequest(requestParameters, abortController);
};
export const getJSON = <T>(requestParameters: RequestParameters, abortController: AbortController): Promise<{data: T} & ExpiryData> => {
return makeRequest(extend(requestParameters, {type: 'json'}), abortController);
};
export const getArrayBuffer = (requestParameters: RequestParameters, abortController: AbortController): Promise<{data: ArrayBuffer} & ExpiryData> => {
return makeRequest(extend(requestParameters, {type: 'arrayBuffer'}), abortController);
};
export function sameOrigin(inComingUrl: string) {
// A relative URL "/foo" or "./foo" will throw exception in URL's ctor,
// try-catch is expansive so just use a heuristic check to avoid it
// also check data URL
if (!inComingUrl ||
inComingUrl.indexOf('://') <= 0 || // relative URL
inComingUrl.indexOf('data:image/') === 0 || // data image URL
inComingUrl.indexOf('blob:') === 0) { // blob
return true;
}
const urlObj = new URL(inComingUrl);
const locationObj = window.location;
return urlObj.protocol === locationObj.protocol && urlObj.host === locationObj.host;
}
export const getVideo = (urls: Array<string>): Promise<HTMLVideoElement> => {
const video: HTMLVideoElement = window.document.createElement('video');
video.muted = true;
return new Promise((resolve) => {
video.onloadstart = () => {
resolve(video);
};
for (const url of urls) {
const s: HTMLSourceElement = window.document.createElement('source');
if (!sameOrigin(url)) {
video.crossOrigin = 'Anonymous';
}
s.src = url;
video.appendChild(s);
}
});
};