Skip to content

Commit a1b07a5

Browse files
committed
fix?
1 parent 979e022 commit a1b07a5

1 file changed

Lines changed: 37 additions & 41 deletions

File tree

proxyServiceValidator.js

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -240,51 +240,42 @@ xx xx
240240
try {
241241
results[0].ultraviolet = generatedUrl;
242242

243-
try {
244-
await fetch(generatedUrl, {
245-
method: 'GET',
246-
mode: 'no-cors',
247-
credentials: 'omit',
248-
cache: 'no-store',
249-
});
250-
} catch (_) {
251-
}
252-
253-
// Test to see if the document title for example.com has loaded,
254-
// by appending an IFrame to the document and grabbing its content.
255-
const testGeneratedUrlHacky = async (url) => {
256-
const exampleIFrame = document.createElement('iframe');
257-
exampleIFrame.style.display = 'none';
258-
document.documentElement.appendChild(exampleIFrame);
259-
260-
const checkTitle = () => {
261-
try {
262-
return (
263-
exampleIFrame.contentWindow.document.title === pageTitle
264-
);
265-
} catch (_) {
266-
return false;
267-
}
243+
const fetchProxiedPage = async (url) => {
244+
const out = {
245+
status: 0,
246+
ok: false,
247+
length: 0,
248+
hasTitle: false,
249+
error: null,
268250
};
269-
270-
const waitForDocument = new Promise((resolve) => {
271-
exampleIFrame.addEventListener('load', () => {
272-
setTimeout(() => resolve(checkTitle()), 250);
251+
const controller = new AbortController();
252+
const abortId = setTimeout(() => controller.abort(), 30000);
253+
try {
254+
const response = await fetch(url, {
255+
method: 'GET',
256+
credentials: 'omit',
257+
cache: 'no-store',
258+
signal: controller.signal,
273259
});
274-
});
275-
276-
// Give 30 seconds for the IFrame to load before manually checking.
277-
const timeout = new Promise((resolve) => {
278-
setTimeout(() => resolve(checkTitle()), 30000);
279-
});
280-
281-
exampleIFrame.src = url;
282-
return await Promise.race([waitForDocument, timeout]);
260+
out.status = response.status;
261+
out.ok = response.ok;
262+
const body = await response.text();
263+
out.length = body.length;
264+
out.hasTitle = body.indexOf('<title>' + pageTitle) !== -1;
265+
} catch (e) {
266+
out.error = e && e.message ? e.message : String(e);
267+
} finally {
268+
clearTimeout(abortId);
269+
}
270+
return out;
283271
};
284272

285-
results[1].uvTestPassed =
286-
!!results[0].ultraviolet.indexOf(errorPrefix) &&
287-
(await testGeneratedUrlHacky(results[0].ultraviolet));
273+
const proxiedResult =
274+
results[0].ultraviolet.indexOf(errorPrefix) === 0
275+
? { status: 0, ok: false, length: 0, hasTitle: false, error: 'omnibox failure' }
276+
: await fetchProxiedPage(results[0].ultraviolet);
277+
results[1].uvProxiedResponse = proxiedResult;
278+
results[1].uvTestPassed = proxiedResult.ok && proxiedResult.hasTitle;
288279
} catch (e) {
289280
results[0].ultraviolet = errorPrefix + ': ' + e.message;
290281
}
@@ -295,6 +286,11 @@ xx xx
295286
);
296287

297288
console.log('Ultraviolet test results:', testResults[0]);
289+
if (testResults[1] && testResults[1].uvProxiedResponse)
290+
console.log(
291+
'Ultraviolet proxied response:',
292+
testResults[1].uvProxiedResponse
293+
);
298294
const uvTestPassed =
299295
testResults[0].ultraviolet &&
300296
testResults[0].ultraviolet !== 'failure' &&

0 commit comments

Comments
 (0)