Skip to content

Commit 3f1c36c

Browse files
stepankuzmingithub-actions[bot]
authored andcommitted
Remove outdated browser workarounds for networking (internal-9541)
GitOrigin-RevId: 6797ce1a402ad1f73e864fc3add6a97cf5aa7443
1 parent 08ac69d commit 3f1c36c

2 files changed

Lines changed: 13 additions & 69 deletions

File tree

src/util/ajax.ts

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ const ResourceType = {
2929

3030
export {ResourceType};
3131

32-
if (typeof Object.freeze == 'function') {
33-
Object.freeze(ResourceType);
34-
}
32+
Object.freeze(ResourceType);
3533

3634
/**
3735
* A `RequestParameters` object to be returned from Map.options.transformRequest callbacks.
@@ -247,22 +245,11 @@ function makeXMLHttpRequest(requestParameters: RequestParameters, callback: Resp
247245
}
248246

249247
export const makeRequest = function (requestParameters: RequestParameters, callback: ResponseCallback<unknown>): Cancelable {
250-
// We're trying to use the Fetch API if possible. However, in some situations we can't use it:
251-
// - Safari exposes AbortController, but it doesn't work actually abort any requests in
252-
// older versions (see https://bugs.webkit.org/show_bug.cgi?id=174980#c2). In this case,
253-
// we dispatch the request to the main thread so that we can get an accurate referrer header.
254-
// - Requests for resources with the file:// URI scheme don't work with the Fetch API either. In
255-
// this case we unconditionally use XHR on the current thread since referrers don't matter.
256-
if (!isFileURL(requestParameters.url)) {
257-
if (self.fetch && self.Request && self.AbortController && Request.prototype.hasOwnProperty('signal')) {
258-
return makeFetchRequest(requestParameters, callback);
259-
}
260-
if (isWorker(self) && self.worker.actor) {
261-
const queueOnMainThread = true;
262-
return self.worker.actor.send('getResource', requestParameters, callback, undefined, queueOnMainThread);
263-
}
248+
// file:// URLs don't work with the Fetch API, use XHR instead
249+
if (isFileURL(requestParameters.url)) {
250+
return makeXMLHttpRequest(requestParameters, callback);
264251
}
265-
return makeXMLHttpRequest(requestParameters, callback);
252+
return makeFetchRequest(requestParameters, callback);
266253
};
267254

268255
export const getJSON = function (requestParameters: RequestParameters, callback: ResponseCallback<unknown>): Cancelable {
@@ -290,24 +277,6 @@ function sameOrigin(url: string) {
290277
return a.protocol === location.protocol && a.host === location.host;
291278
}
292279

293-
const transparentPngUrl = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVQYV2NgAAIAAAUAAarVyFEAAAAASUVORK5CYII=';
294-
295-
function arrayBufferToImage(data: ArrayBuffer, callback: Callback<HTMLImageElement>) {
296-
const img: HTMLImageElement = new Image();
297-
img.onload = () => {
298-
callback(null, img);
299-
URL.revokeObjectURL(img.src);
300-
// prevent image dataURI memory leak in Safari;
301-
// but don't free the image immediately because it might be uploaded in the next frame
302-
// https://github.com/mapbox/mapbox-gl-js/issues/10226
303-
img.onload = null;
304-
requestAnimationFrame(() => { img.src = transparentPngUrl; });
305-
};
306-
img.onerror = () => callback(new Error('Could not load image. Please make sure to use a supported image type such as PNG or JPEG. Note that SVGs are not supported.'));
307-
const blob: Blob = new Blob([new Uint8Array(data)], {type: 'image/png'});
308-
img.src = data.byteLength ? URL.createObjectURL(blob) : transparentPngUrl;
309-
}
310-
311280
function arrayBufferToImageBitmap(data: ArrayBuffer, callback: Callback<ImageBitmap>) {
312281
const blob: Blob = new Blob([new Uint8Array(data)], {type: 'image/png'});
313282
createImageBitmap(blob).then((imgBitmap) => {
@@ -327,7 +296,7 @@ resetImageRequestQueue();
327296

328297
export const getImage = function (
329298
requestParameters: RequestParameters,
330-
callback: ResponseCallback<HTMLImageElement | ImageBitmap>,
299+
callback: ResponseCallback<ImageBitmap>,
331300
): Cancelable {
332301
if (webpSupported.supported) {
333302
if (!requestParameters.headers) {
@@ -378,11 +347,7 @@ export const getImage = function (
378347
if (err) {
379348
callback(err);
380349
} else if (data) {
381-
if (self.createImageBitmap) {
382-
arrayBufferToImageBitmap(data, (err, imgBitmap) => callback(err, imgBitmap, headers));
383-
} else {
384-
arrayBufferToImage(data, (err, img) => callback(err, img, headers));
385-
}
350+
arrayBufferToImageBitmap(data, (err, imgBitmap) => callback(err, imgBitmap, headers));
386351
}
387352
});
388353

src/util/tile_request_cache.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,6 @@ export function cacheClose() {
4747
sharedCache = undefined;
4848
}
4949

50-
let responseConstructorSupportsReadableStream;
51-
function prepareBody(response: Response, callback: (body?: Blob | ReadableStream | null) => void) {
52-
if (responseConstructorSupportsReadableStream === undefined) {
53-
try {
54-
new Response(new ReadableStream());
55-
responseConstructorSupportsReadableStream = true;
56-
} catch (e) {
57-
// Edge
58-
responseConstructorSupportsReadableStream = false;
59-
}
60-
}
61-
62-
if (responseConstructorSupportsReadableStream) {
63-
callback(response.body);
64-
} else {
65-
response.blob().then(callback).catch((e: Error) => warnOnce(e.message));
66-
}
67-
}
68-
6950
// https://fetch.spec.whatwg.org/#null-body-status
7051
function isNullBodyStatus(status: Response["status"]): boolean {
7152
if (status === 200 || status === 404) {
@@ -111,15 +92,13 @@ export function cachePut(request: Request, response: Response, requestTime: numb
11192
strippedURL = setQueryParameters(strippedURL, {range});
11293
}
11394

114-
prepareBody(response, body => {
115-
const clonedResponse = new Response(isNullBodyStatus(response.status) ? null : body, options);
95+
const clonedResponse = new Response(isNullBodyStatus(response.status) ? null : response.body, options);
11696

117-
cacheOpen();
118-
if (sharedCache == null) return;
119-
sharedCache
120-
.then(cache => cache.put(strippedURL, clonedResponse))
121-
.catch((e: Error) => warnOnce(e.message));
122-
});
97+
cacheOpen();
98+
if (sharedCache == null) return;
99+
sharedCache
100+
.then(cache => cache.put(strippedURL, clonedResponse))
101+
.catch((e: Error) => warnOnce(e.message));
123102
}
124103

125104
export function cacheGet(

0 commit comments

Comments
 (0)