The headless browser for AI agents and web scraping
fix: preserve Request headers in fetch
hongde7a46ca73f07d
diff --git a/crates/obscura-js/js/bootstrap.js b/crates/obscura-js/js/bootstrap.jsindex d7b4aff8..a75b544d 100644--- a/crates/obscura-js/js/bootstrap.js+++ b/crates/obscura-js/js/bootstrap.js@@ -6715,21 +6715,21 @@ function _formDataToMultipart(fd) {// Coerce a fetch()/XHR body into the bytes op_fetch_url expects, attaching a// Content-Type header for body types that need one (FormData, URLSearchParams).-function _serializeBody(initBody, headers) {+function _serializeBody(initBody, headers, synthesizeContentType = true) {if (initBody == null || initBody === '') return new Uint8Array(0);if (initBody instanceof FormData) {const mp = _formDataToMultipart(initBody);- headers['Content-Type'] = 'multipart/form-data; boundary=' + mp.boundary;+ if (synthesizeContentType) headers['Content-Type'] = 'multipart/form-data; boundary=' + mp.boundary;return mp.body;}if (initBody instanceof URLSearchParams) {- if (!Object.keys(headers).some(k => k.toLowerCase() === 'content-type')) {+ if (synthesizeContentType && !Object.keys(headers).some(k => k.toLowerCase() === 'content-type')) {headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';}return new TextEncoder().encode(initBody.toString());}if (typeof Blob !== 'undefined' && initBody instanceof Blob) {- if (initBody.type && !Object.keys(headers).some(k => k.toLowerCase() === 'content-type')) {+ if (synthesizeContentType && initBody.type && !Object.keys(headers).some(k => k.toLowerCase() === 'content-type')) {headers['Content-Type'] = initBody.type;}return _bodyToUint8Array(initBody);@@ -6745,26 +6745,29 @@ function _serializeBody(initBody, headers) {globalThis.fetch = async (input, init = {}) => {init = init || {};+ const request = input instanceof Request ? input : null;let url = typeof input === "string"? input- : (input instanceof Request- ? input.url+ : (request+ ? request.url: ((typeof URL === 'function' && input instanceof URL) ? input.href : (input?.url || input?.href || String(input || ""))));// Always resolve: the URL parser, not a "://" substring search, decides// whether the input is absolute. _resolveUrl leaves absolute URLs// unchanged and keeps unparseable input as-is.url = _resolveUrl(url);- const method = init.method || (input instanceof Request ? input.method : "GET");- let _h = init.headers instanceof Headers ? Object.fromEntries(init.headers.entries()) : (init.headers || {});+ const method = init.method || (request ? request.method : "GET");+ const headers = init.headers !== undefined ? init.headers : (request ? request.headers : undefined);+ let _h = headers instanceof Headers ? Object.fromEntries(headers.entries()) : (headers || {});+ const inheritsRequestBody = init.body === undefined && request !== null;const initBody = init.body !== undefined? init.body- : (input instanceof Request ? input.body : undefined);- const body = _serializeBody(initBody, _h);+ : (request ? request.body : undefined);+ const body = _serializeBody(initBody, _h, !(inheritsRequestBody && init.headers !== undefined));const hdrs = JSON.stringify(_h);- const fetchMode = init.mode || (input instanceof Request ? input.mode : "cors");+ const fetchMode = init.mode || (request ? request.mode : "cors");const fetchCredentials = init.credentials !== undefined? String(init.credentials)- : (input instanceof Request ? input.credentials : "same-origin");+ : (request ? request.credentials : "same-origin");if (fetchCredentials !== "omit" && fetchCredentials !== "same-origin" && fetchCredentials !== "include") {throw new TypeError("Failed to execute 'fetch': '" + fetchCredentials + "' is not a valid RequestCredentials value");}diff --git a/crates/obscura-js/src/runtime.rs b/crates/obscura-js/src/runtime.rsindex 34df2818..5984e93e 100644--- a/crates/obscura-js/src/runtime.rs+++ b/crates/obscura-js/src/runtime.rs@@ -14091,6 +14091,8 @@ mod tests {(url, method, headers, body) => {calls.push({path: new URL(url).pathname,+ method,+ headers: JSON.parse(headers),isUint8Array: body instanceof Uint8Array,bytes: Array.from(body instanceof Uint8Array@@ -14121,10 +14123,27 @@ mod tests {body: backing.subarray(1, 5),});- await fetch(new Request("/request", {+ const request = new Request("/request", {method: "POST",+ headers: {+ authorization: "Bearer test-token",+ "x-request-source": "request",+ },body: new Uint8Array(sentinel),- }));+ });+ await fetch(request);+ await fetch(request, {+ headers: { "x-init-override": "yes" },+ });++ await fetch(new Request("/request-params-object", {+ method: "POST",+ body: new URLSearchParams({ a: "b" }),+ }), { headers: {} });+ await fetch(new Request("/request-params-headers", {+ method: "POST",+ body: new URLSearchParams({ a: "b" }),+ }), { headers: new Headers() });return calls;} finally {@@ -14142,10 +14161,13 @@ mod tests {assert_eq!(result.value.unwrap(),serde_json::json!([- { "path": "/blob", "isUint8Array": true, "bytes": [0, 128, 255, 16] },- { "path": "/array-buffer", "isUint8Array": true, "bytes": [0, 128, 255, 16] },- { "path": "/typed-array-view", "isUint8Array": true, "bytes": [0, 128, 255, 16] },- { "path": "/request", "isUint8Array": true, "bytes": [0, 128, 255, 16] },+ { "path": "/blob", "method": "POST", "headers": {}, "isUint8Array": true, "bytes": [0, 128, 255, 16] },+ { "path": "/array-buffer", "method": "POST", "headers": {}, "isUint8Array": true, "bytes": [0, 128, 255, 16] },+ { "path": "/typed-array-view", "method": "POST", "headers": {}, "isUint8Array": true, "bytes": [0, 128, 255, 16] },+ { "path": "/request", "method": "POST", "headers": { "authorization": "Bearer test-token", "x-request-source": "request" }, "isUint8Array": true, "bytes": [0, 128, 255, 16] },+ { "path": "/request", "method": "POST", "headers": { "x-init-override": "yes" }, "isUint8Array": true, "bytes": [0, 128, 255, 16] },+ { "path": "/request-params-object", "method": "POST", "headers": {}, "isUint8Array": true, "bytes": [97, 61, 98] },+ { "path": "/request-params-headers", "method": "POST", "headers": {}, "isUint8Array": true, "bytes": [97, 61, 98] },]));}