diff --git a/src/DOM.js b/src/DOM.js index 044d370676..b36bd078a6 100644 --- a/src/DOM.js +++ b/src/DOM.js @@ -12,7 +12,7 @@ const he = require('he'); const parse5 = require('parse5'); const parseIntStrict = require('parse-int'); const selector = require('window-selector'); -const fetch = require('window-fetch'); +const fetch = require('./fetch'); const {Blob} = fetch; const htmlUnescape = require('unescape'); diff --git a/src/Window.js b/src/Window.js index 5198adc7be..cdaf5b1360 100644 --- a/src/Window.js +++ b/src/Window.js @@ -106,6 +106,7 @@ const { GlobalContext.id = id; GlobalContext.args = args; GlobalContext.version = version; +GlobalContext.userAgent = `Mozilla/5.0 (OS) AppleWebKit/999.0 (KHTML, like Gecko) Chrome/999.0.0.0 Safari/999.0 Exokit/${GlobalContext.version}`; const {_parseDocument, _parseDocumentAst, getBoundDocumentElements, DocumentType, DOMImplementation, initDocument} = require('./Document'); const { @@ -493,7 +494,7 @@ const _makeRequestAnimationFrame = window => (fn, priority = 0) => { } } window.navigator = { - userAgent: `Mozilla/5.0 (OS) AppleWebKit/999.0 (KHTML, like Gecko) Chrome/999.0.0.0 Safari/999.0 Exokit/${GlobalContext.version}`, + userAgent: GlobalContext.userAgent, vendor: 'Exokit', platform: os.platform(), hardwareConcurrency: os.cpus().length, diff --git a/src/WindowBase.js b/src/WindowBase.js index 3b4037b1b5..b5901e09e5 100644 --- a/src/WindowBase.js +++ b/src/WindowBase.js @@ -20,7 +20,7 @@ const { const {CustomEvent, DragEvent, ErrorEvent, Event, EventTarget, KeyboardEvent, MessageEvent, MouseEvent, WheelEvent, PromiseRejectionEvent} = require('./Event'); const {FileReader} = require('./File.js'); const {XMLHttpRequest, FormData} = require('window-xhr'); -const fetch = require('window-fetch'); +const fetch = require('./fetch'); const {Request, Response, Headers, Blob} = fetch; const WebSocket = require('ws/lib/websocket'); @@ -149,11 +149,6 @@ class Worker extends EventTarget { self.URL = URL; self.fetch = (u, options) => { - const o = Object.assign({}, options || {}); - const h = (o.headers instanceof Headers) ? o.headers : new Headers(o.headers); - h.set('User-Agent', self.navigator.userAgent); - o.headers = h; - options = o; if (typeof u === 'string') { const blob = URL.lookupObjectURL(u); if (blob) { diff --git a/src/Worker.js b/src/Worker.js index 56b908af46..9fce9d3919 100644 --- a/src/Worker.js +++ b/src/Worker.js @@ -10,7 +10,7 @@ const { } = require('worker_threads'); const {createImageBitmap} = require('./DOM.js'); -const fetch = require('window-fetch'); +const fetch = require('./fetch'); const {XMLHttpRequest} = require('window-xhr'); const WebSocket = require('ws/lib/websocket'); const {FileReader} = require('./File.js'); diff --git a/src/core.js b/src/core.js index d561b4182b..e3d74dd218 100644 --- a/src/core.js +++ b/src/core.js @@ -1,7 +1,7 @@ const url = require('url'); const {URL} = url; -const fetch = require('window-fetch'); +const fetch = require('./fetch'); const GlobalContext = require('./GlobalContext'); const symbols = require('./symbols'); const {_getBaseUrl} = require('./utils'); diff --git a/src/fetch.js b/src/fetch.js new file mode 100644 index 0000000000..9e5558b469 --- /dev/null +++ b/src/fetch.js @@ -0,0 +1,13 @@ +const GlobalContext = require('./GlobalContext'); +const fetch = require('window-fetch'); + +Object.assign(module.exports, fetch); + +module.exports = (u, options) => { + const o = Object.assign({}, options || {}); + const h = (o.headers instanceof fetch.Headers) ? o.headers : new fetch.Headers(o.headers); + h.set('User-Agent', GlobalContext.userAgent); + o.headers = h; + return fetch(u, o); +}; + diff --git a/src/request.js b/src/request.js index be37c72e7a..062251d185 100644 --- a/src/request.js +++ b/src/request.js @@ -17,7 +17,7 @@ consoleStream._writev = (chunks, callback) => { }; global.console = new Console(consoleStream); -const fetch = require('window-fetch'); +const fetch = require('./fetch'); const {workerData, parentPort} = require('worker_threads'); const {url, int32Array} = workerData;