1function doXhr(method, url, body, contentType, authToken = null) { 2 return new Promise((resolve, reject) => { 3 const x = new XMLHttpRequest(); 4 x.onreadystatechange = () => { 5 if (x.readyState !== 4) return; 6 if (x.status < 200 || x.status > 299) { 7 const errorMessage = getResponseFromXHR(x, "Request failed"); 8 console.warn("XHR failed with status " + x.status + ", response " + errorMessage); 9 // NOTE: clients might parse '[HTTP ...]' part from the reject result, 10 // so please preserve that! 11 reject(`[HTTP ${x.status}]: ${errorMessage}`); 12 return; 13 } 14 resolve(x.response); 15 }; 16 x.responseType = "arraybuffer"; 17 x.open(method, url); 18 if (authToken) x.setRequestHeader("Authorization", "Bearer " + authToken); 19 if (contentType) { 20 x.setRequestHeader("Content-Type", contentType); 21 } 22 23 if (body) x.send(body); 24 else x.send(); 25 }); 26} 27 28export function xhrGet(url, authToken = null) { 29 return doXhr("GET", url, null, null, authToken); 30} 31 32export function xhrPost(url, body, contentType, authToken = null) { 33 return doXhr("POST", url, body, contentType, authToken); 34} 35 36function getResponseFromXHR(xhrObj, defaultIfError = "(error reading response)") { 37 try {
38 if (!xhrObj || !xhrObj.response || !(xhrObj.response instanceof ArrayBuffer)) return defaultIfError; 39 return (new TextDecoder('utf-8')).decode(xhrObj.response); 40 } catch (e) { 41 return defaultIfError 42 } 43}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.