Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fetch/Request #161

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 47 additions & 25 deletions src/wombat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2686,9 +2686,9 @@ Wombat.prototype.rewriteWorker = function(workerUrl) {
this.wb_abs_prefix +
'\', \'prefixMod\': \'' +
this.wb_abs_prefix +
'wkrf_/\', \'originalURL\': \'' +
originalURL +
'\'}); })();';
'wkrf_/\', \'originalURL\': ' +
JSON.stringify(originalURL) +
'}); })();';

workerCode = rw + workerCode;
}
Expand Down Expand Up @@ -4608,30 +4608,33 @@ Wombat.prototype.initHTTPOverrides = function() {
if (this.$wbwindow.fetch) {
var orig_fetch = this.$wbwindow.fetch;
this.$wbwindow.fetch = function fetch(input, init_opts) {
var rwInput = input;
var inputType = typeof input;
if (inputType === 'string') {
rwInput = wombat.rewriteUrl(input);
} else if (inputType === 'object' && input.url) {
var request;

if (input instanceof Request) {
var new_url = wombat.rewriteUrl(input.url);
if (new_url !== input.url) {
rwInput = new Request(new_url, init_opts);
var real_url = input.__WB_real_url;
// if already a Request with rewritten url, just use that
if (new_url === input.url || new_url === real_url || new_url === wombat.$wbwindow.location.origin + real_url) {
request = input;
} else {
request = new Request(new_url, input);
}
} else if (inputType === 'object' && input.href) {
// it is likely that input is either window.location or window.URL
rwInput = wombat.rewriteUrl(input.href);
}
} else {
input = wombat.rewriteUrl(input.toString());

if (!init_opts) {
init_opts = {};
}
if (init_opts.credentials === undefined) {
try {
init_opts.credentials = 'include';
} catch(e) {}
if (!init_opts) {
init_opts = {};
}
if (init_opts.credentials === undefined) {
try {
init_opts.credentials = 'include';
} catch(e) {}
}

request = new Request(input, init_opts);
}

return orig_fetch.call(wombat.proxyToObj(this), rwInput, init_opts);
return orig_fetch.call(wombat.proxyToObj(this), request);
};
}

Expand Down Expand Up @@ -4662,12 +4665,31 @@ Wombat.prototype.initHTTPOverrides = function() {
}
break;
}
newInitOpts['credentials'] = 'include';
const props = ['cache', 'headers', 'integrity',
'keepalive', 'method', 'mode', 'redirect',
'referrerPolicy', 'signal'
];

const newOpts = {};

for (const prop of props) {
newOpts[prop] = newInitOpts[prop];
}

newOpts.credentials = 'include';

if (newInitOpts.referrer) {
newInitOpts.referrer = wombat.rewriteUrl(newInitOpts.referrer);
newOpts.referrer = wombat.rewriteUrl(newInitOpts.referrer);
}

if (newInitOpts.duplex && newInitOpts.body) {
newOpts.body = newInitOpts.body;
newOpts.duplex = newInitOpts.duplex;
}

return new Request_(newInput, newInitOpts);
var request = new Request_(newInput, newOpts);
request.__WB_real_url = newInput;
return request;
};
})(this.$wbwindow.Request);

Expand Down
20 changes: 12 additions & 8 deletions test/overrides-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,11 @@ test('fetch: should rewrite the input argument when it is an Request object, als
const { sandbox, server } = t.context;
const result = await sandbox.evaluate(async () => {
let to;
let request = new Request('/test', {
method: 'GET'
});
let response = await Promise.race([
fetch(
new Request('/test', {
method: 'GET'
})
),
fetch(request),
new Promise(resolve => {
to = setTimeout(() => resolve('timed out'), 5000);
})
Expand All @@ -117,18 +116,23 @@ test('fetch: should rewrite the input argument when it is an Request object, als
throw new Error('no reply from server in 5 seconds');
clearTimeout(to);
const data = await response.json();
return {url: data.url, respURL: response.url};
return {url: data.url,
rwUrl: request.url,
realUrl: request.__WB_real_url,
respURL: response.url};
});
t.is(result.rwUrl, 'https://tests.wombat.io/test');
t.is(result.realUrl, '/live/20180803160549mp_/https://tests.wombat.io/test');
t.is(result.url, '/live/20180803160549mp_/https://tests.wombat.io/test');
t.is(result.respURL, 'https://tests.wombat.io/test');
});

test('fetch: should rewrite the input argument when it is a object with an href property, but return original', async t => {
test('fetch: should rewrite the input argument when it is a object, but return original', async t => {
const { sandbox, server } = t.context;
const result = await sandbox.evaluate(async () => {
let to;
let response = await Promise.race([
fetch({ href: '/test' }),
fetch({ href: '/test', toString: () => { return '/test'; }}),
new Promise(resolve => {
to = setTimeout(() => resolve('timed out'), 10000);
})
Expand Down
Loading