1// net.js 2 3// CONFIG 4const DEFAULT_CREDENTIALS = "same-origin"; 5const VBM_REQUEST_HEADER = { "VBM-Request": "true" }; 6const JSON_HEADER = { "Content-Type": "application/json" }; 7 8// HELPERS 9const mergeHeaders = (...headerObjects) => 10 headerObjects.reduce((acc, headers) => ({ ...acc, ...(headers || {}) }), {}); 11 12const buildFetchOptions = ({ method, credentials, headers, ...rest } = {}) => ({ 13 method, 14 credentials: credentials ?? DEFAULT_CREDENTIALS, 15 ...rest, 16 headers: mergeHeaders(VBM_REQUEST_HEADER, headers), 17}); 18 19const assertOkOrReturn = async (res, { url, returnErrors } = {}) => { 20 if (returnErrors && !res.ok) return res; 21 if (!res.ok) throw new Error(`HTTP ${res.status} for ${url}`); 22 return res; 23}; 24 25// EXPORTS 26export const getHTML = async (url, options = {}) => { 27 const { meta = false, ...rest } = options; 28 29 const fetchOptions = buildFetchOptions(rest); 30 const res = await fetch(url, fetchOptions); 31 32 await assertOkOrReturn(res, { url }); 33 34 const html = await res.text(); 35 return meta ? { html, res } : html; 36}; 37 38export const postHTML = async (url, body, options = {}) => { 39 const { 40 meta = false, 41 returnErrors = false, 42 returnErrorText = false, 43 headers = {}, 44 credentials = "same-origin", 45 ...rest 46 } = options; 47 48 const res = await fetch(url, { 49 method: "POST", 50 credentials, 51 headers: { 52 "VBM-Request": "true", 53 "Content-Type": "application/json", 54 ...headers, 55 }, 56 body: JSON.stringify(body), 57 ...rest, 58 }); 59 60 if (returnErrors && !meta && !res.ok) return res; 61 62 const text = await res.text(); 63 64 if (!res.ok && returnErrorText) { 65 return meta ? { text, res } : text; 66 } 67 68 if (!res.ok) throw new Error(`HTTP ${res.status} for ${url}`); 69 70 if (meta) return { text, res }; 71 72 return text; 73};
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.