/** * Web Fetch API polyfill source for QuickJS. * Exported as a string constant to be evaluated inside the sandbox. * Provides URLSearchParams, URL, Headers, Response, Request, and fetch(). */ export declare const FETCH_POLYFILL_SOURCE = "\n(function() {\n // --- URLSearchParams ---\n function URLSearchParams(init) {\n this._entries = [];\n if (!init) return;\n if (typeof init === 'string') {\n var s = init;\n if (s.charAt(0) === '?') s = s.slice(1);\n var pairs = s.split('&');\n for (var i = 0; i < pairs.length; i++) {\n var pair = pairs[i];\n if (pair === '') continue;\n var eq = pair.indexOf('=');\n if (eq === -1) {\n this._entries.push([decodeURIComponent(pair), '']);\n } else {\n this._entries.push([\n decodeURIComponent(pair.slice(0, eq)),\n decodeURIComponent(pair.slice(eq + 1))\n ]);\n }\n }\n } else if (typeof init === 'object' && init !== null) {\n if (init instanceof URLSearchParams) {\n this._entries = init._entries.slice();\n } else {\n var keys = Object.keys(init);\n for (var i = 0; i < keys.length; i++) {\n this._entries.push([keys[i], String(init[keys[i]])]);\n }\n }\n }\n }\n\n URLSearchParams.prototype.append = function(name, value) {\n this._entries.push([String(name), String(value)]);\n };\n\n URLSearchParams.prototype.delete = function(name) {\n var n = String(name);\n this._entries = this._entries.filter(function(e) { return e[0] !== n; });\n };\n\n URLSearchParams.prototype.get = function(name) {\n var n = String(name);\n for (var i = 0; i < this._entries.length; i++) {\n if (this._entries[i][0] === n) return this._entries[i][1];\n }\n return null;\n };\n\n URLSearchParams.prototype.getAll = function(name) {\n var n = String(name);\n var result = [];\n for (var i = 0; i < this._entries.length; i++) {\n if (this._entries[i][0] === n) result.push(this._entries[i][1]);\n }\n return result;\n };\n\n URLSearchParams.prototype.has = function(name) {\n var n = String(name);\n for (var i = 0; i < this._entries.length; i++) {\n if (this._entries[i][0] === n) return true;\n }\n return false;\n };\n\n URLSearchParams.prototype.set = function(name, value) {\n var n = String(name);\n var v = String(value);\n var found = false;\n var newEntries = [];\n for (var i = 0; i < this._entries.length; i++) {\n if (this._entries[i][0] === n) {\n if (!found) {\n newEntries.push([n, v]);\n found = true;\n }\n } else {\n newEntries.push(this._entries[i]);\n }\n }\n if (!found) newEntries.push([n, v]);\n this._entries = newEntries;\n };\n\n URLSearchParams.prototype.sort = function() {\n this._entries.sort(function(a, b) {\n if (a[0] < b[0]) return -1;\n if (a[0] > b[0]) return 1;\n return 0;\n });\n };\n\n URLSearchParams.prototype.toString = function() {\n return this._entries.map(function(e) {\n return encodeURIComponent(e[0]) + '=' + encodeURIComponent(e[1]);\n }).join('&');\n };\n\n URLSearchParams.prototype.forEach = function(callback, thisArg) {\n for (var i = 0; i < this._entries.length; i++) {\n callback.call(thisArg, this._entries[i][1], this._entries[i][0], this);\n }\n };\n\n URLSearchParams.prototype.entries = function() {\n var idx = 0;\n var entries = this._entries;\n return {\n next: function() {\n if (idx >= entries.length) return { done: true, value: undefined };\n return { done: false, value: entries[idx++].slice() };\n },\n [Symbol.iterator]: function() { return this; }\n };\n };\n\n URLSearchParams.prototype.keys = function() {\n var idx = 0;\n var entries = this._entries;\n return {\n next: function() {\n if (idx >= entries.length) return { done: true, value: undefined };\n return { done: false, value: entries[idx++][0] };\n },\n [Symbol.iterator]: function() { return this; }\n };\n };\n\n URLSearchParams.prototype.values = function() {\n var idx = 0;\n var entries = this._entries;\n return {\n next: function() {\n if (idx >= entries.length) return { done: true, value: undefined };\n return { done: false, value: entries[idx++][1] };\n },\n [Symbol.iterator]: function() { return this; }\n };\n };\n\n URLSearchParams.prototype[Symbol.iterator] = URLSearchParams.prototype.entries;\n\n Object.defineProperty(URLSearchParams.prototype, 'size', {\n get: function() { return this._entries.length; }\n });\n\n // --- URL ---\n var urlRegex = /^([a-zA-Z][a-zA-Z0-9+.-]*):(?:\\/\\/(?:([^:@/?#]*)(?::([^@/?#]*))?@)?([^:/?#]*)(?::([0-9]+))?)?(\\/[^?#]*)?(?:\\?([^#]*))?(?:#(.*))?$/;\n\n function URL(url, base) {\n var input = String(url);\n\n if (base !== undefined) {\n var baseUrl = (base instanceof URL) ? base : new URL(String(base));\n // Resolve relative URL against base\n if (/^[a-zA-Z][a-zA-Z0-9+.-]*:/.test(input)) {\n // Absolute URL - parse as-is\n } else if (input.charAt(0) === '/' && input.charAt(1) === '/') {\n // Protocol-relative\n input = baseUrl.protocol + input;\n } else if (input.charAt(0) === '/') {\n // Absolute path\n input = baseUrl.origin + input;\n } else if (input.charAt(0) === '?' || input.charAt(0) === '#') {\n // Query or hash only\n var basePath = baseUrl.protocol + '//' + baseUrl.host + baseUrl.pathname;\n if (input.charAt(0) === '#') {\n input = basePath + baseUrl.search + input;\n } else {\n input = basePath + input;\n }\n } else {\n // Relative path\n var basePath = baseUrl.protocol + '//' + baseUrl.host;\n var dirPath = baseUrl.pathname;\n var lastSlash = dirPath.lastIndexOf('/');\n if (lastSlash >= 0) dirPath = dirPath.slice(0, lastSlash + 1);\n else dirPath = '/';\n input = basePath + dirPath + input;\n }\n }\n\n var m = urlRegex.exec(input);\n if (!m) throw new TypeError(\"Invalid URL: \" + String(url));\n\n this.protocol = m[1].toLowerCase() + ':';\n this.username = m[2] ? decodeURIComponent(m[2]) : '';\n this.password = m[3] ? decodeURIComponent(m[3]) : '';\n this.hostname = m[4] || '';\n this.port = m[5] || '';\n this.pathname = m[6] || '/';\n this.hash = m[8] ? '#' + m[8] : '';\n\n // Normalize pathname (resolve . and ..)\n var parts = this.pathname.split('/');\n var resolved = [];\n for (var i = 0; i < parts.length; i++) {\n if (parts[i] === '..') { if (resolved.length > 1) resolved.pop(); }\n else if (parts[i] !== '.') resolved.push(parts[i]);\n }\n this.pathname = resolved.join('/') || '/';\n\n // searchParams is live\n this._searchParamsStr = m[7] || '';\n this.searchParams = new URLSearchParams(this._searchParamsStr);\n }\n\n Object.defineProperty(URL.prototype, 'search', {\n get: function() {\n var s = this.searchParams.toString();\n return s ? '?' + s : '';\n },\n set: function(v) {\n this.searchParams = new URLSearchParams(String(v));\n }\n });\n\n Object.defineProperty(URL.prototype, 'host', {\n get: function() {\n return this.port ? this.hostname + ':' + this.port : this.hostname;\n }\n });\n\n Object.defineProperty(URL.prototype, 'origin', {\n get: function() {\n return this.protocol + '//' + this.host;\n }\n });\n\n Object.defineProperty(URL.prototype, 'href', {\n get: function() {\n var auth = '';\n if (this.username) {\n auth = this.username;\n if (this.password) auth += ':' + this.password;\n auth += '@';\n }\n return this.protocol + '//' + auth + this.host + this.pathname + this.search + this.hash;\n },\n set: function(v) {\n var parsed = new URL(String(v));\n this.protocol = parsed.protocol;\n this.username = parsed.username;\n this.password = parsed.password;\n this.hostname = parsed.hostname;\n this.port = parsed.port;\n this.pathname = parsed.pathname;\n this.searchParams = parsed.searchParams;\n this.hash = parsed.hash;\n }\n });\n\n URL.prototype.toString = function() { return this.href; };\n URL.prototype.toJSON = function() { return this.href; };\n\n // --- Headers ---\n function Headers(init) {\n this._map = {};\n if (!init) return;\n if (init instanceof Headers) {\n var keys = Object.keys(init._map);\n for (var i = 0; i < keys.length; i++) {\n this._map[keys[i]] = init._map[keys[i]].slice();\n }\n } else if (typeof init === 'object') {\n var keys = Object.keys(init);\n for (var i = 0; i < keys.length; i++) {\n this._map[keys[i].toLowerCase()] = [String(init[keys[i]])];\n }\n }\n }\n\n Headers.prototype.append = function(name, value) {\n var key = String(name).toLowerCase();\n if (!this._map[key]) this._map[key] = [];\n this._map[key].push(String(value));\n };\n\n Headers.prototype.delete = function(name) {\n delete this._map[String(name).toLowerCase()];\n };\n\n Headers.prototype.get = function(name) {\n var vals = this._map[String(name).toLowerCase()];\n return vals ? vals.join(', ') : null;\n };\n\n Headers.prototype.has = function(name) {\n return String(name).toLowerCase() in this._map;\n };\n\n Headers.prototype.set = function(name, value) {\n this._map[String(name).toLowerCase()] = [String(value)];\n };\n\n Headers.prototype.forEach = function(callback, thisArg) {\n var keys = Object.keys(this._map).sort();\n for (var i = 0; i < keys.length; i++) {\n callback.call(thisArg, this._map[keys[i]].join(', '), keys[i], this);\n }\n };\n\n Headers.prototype.entries = function() {\n var keys = Object.keys(this._map).sort();\n var map = this._map;\n var idx = 0;\n return {\n next: function() {\n if (idx >= keys.length) return { done: true, value: undefined };\n var k = keys[idx++];\n return { done: false, value: [k, map[k].join(', ')] };\n },\n [Symbol.iterator]: function() { return this; }\n };\n };\n\n Headers.prototype.keys = function() {\n var keys = Object.keys(this._map).sort();\n var idx = 0;\n return {\n next: function() {\n if (idx >= keys.length) return { done: true, value: undefined };\n return { done: false, value: keys[idx++] };\n },\n [Symbol.iterator]: function() { return this; }\n };\n };\n\n Headers.prototype.values = function() {\n var keys = Object.keys(this._map).sort();\n var map = this._map;\n var idx = 0;\n return {\n next: function() {\n if (idx >= keys.length) return { done: true, value: undefined };\n return { done: false, value: map[keys[idx++]].join(', ') };\n },\n [Symbol.iterator]: function() { return this; }\n };\n };\n\n Headers.prototype[Symbol.iterator] = Headers.prototype.entries;\n\n // --- Response ---\n function Response(body, init) {\n if (init === undefined) init = {};\n this.status = init.status !== undefined ? init.status : 200;\n this.statusText = init.statusText !== undefined ? init.statusText : '';\n this.headers = init.headers instanceof Headers ? init.headers : new Headers(init.headers);\n this.body = body !== undefined && body !== null ? String(body) : '';\n this.ok = this.status >= 200 && this.status <= 299;\n this.url = '';\n this.redirected = false;\n this.type = 'basic';\n this.bodyUsed = false;\n }\n\n Response.prototype.text = function() {\n this.bodyUsed = true;\n return Promise.resolve(this.body);\n };\n\n Response.prototype.json = function() {\n this.bodyUsed = true;\n try {\n return Promise.resolve(JSON.parse(this.body));\n } catch (e) {\n return Promise.reject(e);\n }\n };\n\n Response.prototype.clone = function() {\n var r = new Response(this.body, {\n status: this.status,\n statusText: this.statusText,\n headers: new Headers(this.headers)\n });\n r.url = this.url;\n r.redirected = this.redirected;\n r.type = this.type;\n return r;\n };\n\n Response.json = function(data, init) {\n if (init === undefined) init = {};\n var headers = init.headers instanceof Headers ? init.headers : new Headers(init.headers);\n if (!headers.has('content-type')) {\n headers.set('content-type', 'application/json');\n }\n return new Response(JSON.stringify(data), {\n status: init.status !== undefined ? init.status : 200,\n statusText: init.statusText || '',\n headers: headers\n });\n };\n\n Response.error = function() {\n var r = new Response(null, { status: 0, statusText: '' });\n r.type = 'error';\n r.ok = false;\n return r;\n };\n\n Response.redirect = function(url, status) {\n if (status === undefined) status = 302;\n var r = new Response(null, {\n status: status,\n statusText: '',\n headers: new Headers({ location: String(url) })\n });\n r.redirected = true;\n return r;\n };\n\n // --- Request ---\n function Request(input, init) {\n if (init === undefined) init = {};\n if (input instanceof Request) {\n this.url = input.url;\n this.method = input.method;\n this.headers = new Headers(input.headers);\n this.body = input.body;\n } else {\n this.url = String(input);\n this.method = 'GET';\n this.headers = new Headers();\n this.body = null;\n }\n if (init.method !== undefined) this.method = String(init.method).toUpperCase();\n if (init.headers !== undefined) this.headers = init.headers instanceof Headers ? init.headers : new Headers(init.headers);\n if (init.body !== undefined) this.body = init.body !== null ? String(init.body) : null;\n }\n\n Request.prototype.clone = function() {\n return new Request(this);\n };\n\n // --- Assign to globalThis ---\n globalThis.URLSearchParams = URLSearchParams;\n globalThis.URL = URL;\n globalThis.Headers = Headers;\n globalThis.Response = Response;\n globalThis.Request = Request;\n\n // --- Wrap native fetch ---\n var _nativeFetch = globalThis[Symbol.for('jb:fetch')];\n globalThis.fetch = function fetch(input, init) {\n try {\n var url, method, headers, body;\n\n if (input instanceof Request) {\n url = input.url;\n method = input.method;\n headers = {};\n input.headers.forEach(function(v, k) { headers[k] = v; });\n body = input.body;\n } else {\n url = String(input);\n method = undefined;\n headers = undefined;\n body = undefined;\n }\n\n if (init) {\n if (init.method !== undefined) method = String(init.method).toUpperCase();\n if (init.headers !== undefined) {\n var h = init.headers instanceof Headers ? init.headers : new Headers(init.headers);\n headers = {};\n h.forEach(function(v, k) { headers[k] = v; });\n }\n if (init.body !== undefined) body = init.body !== null ? String(init.body) : undefined;\n }\n\n var opts = Object.create(null);\n if (method) opts.method = method;\n if (headers) opts.headers = headers;\n if (body) opts.body = body;\n\n var raw = _nativeFetch(url, opts);\n\n var respHeaders = new Headers(raw.headers || {});\n var response = new Response(raw.body, {\n status: raw.status,\n statusText: raw.statusText || '',\n headers: respHeaders\n });\n response.url = raw.url || url;\n\n return Promise.resolve(response);\n } catch (e) {\n return Promise.reject(new TypeError(e.message || 'fetch failed'));\n }\n };\n})();\n";