network-request.js (1504B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 "use strict"; 6 7 async function networkRequest(url, opts) { 8 const supportedProtocols = ["http:", "https:", "data:"]; 9 10 // Add file, chrome or moz-extension if the initial source was served by the 11 // same protocol. 12 const ADDITIONAL_PROTOCOLS = ["chrome:", "file:", "moz-extension:"]; 13 for (const protocol of ADDITIONAL_PROTOCOLS) { 14 if (opts.sourceMapBaseURL?.startsWith(protocol)) { 15 supportedProtocols.push(protocol); 16 } 17 } 18 19 if (supportedProtocols.every(protocol => !url.startsWith(protocol))) { 20 throw new Error(`unsupported protocol for sourcemap request ${url}`); 21 } 22 23 const response = await fetch(url, { 24 cache: opts.loadFromCache ? "default" : "no-cache", 25 // See Bug 1899389, by default fetch calls from the system principal no 26 // longer use credentials. 27 credentials: "same-origin", 28 redirect: opts.allowRedirects ? "follow" : "error", 29 }); 30 31 if (response.ok) { 32 if (response.headers.get("Content-Type") === "application/wasm") { 33 const buffer = await response.arrayBuffer(); 34 return { 35 content: buffer, 36 isDwarf: true, 37 }; 38 } 39 const text = await response.text(); 40 return { content: text }; 41 } 42 43 throw new Error(`request failed with status ${response.status}`); 44 } 45 46 module.exports = { networkRequest };