utopia-ia/elffuss-code public
Changesets · Branches · Tags · Compare
| Changeset | 152be91d1268abb8dbd845f8108b0cc89dd7ba4e096c7ff4ebd50780d69dc6c6 |
|---|---|
| Author | KikoCis <kikocisneros@gmail.com> · 2026-10-11 17:34 +02:00 |
| Committer | KikoCis <kikocisneros@gmail.com> · 2026-10-11 17:34 +02:00 |
| Parents | a6879f75021c |
2 files changed
modified tests/web-honest.mjs +47 −6
| @@ -9,7 +9,10 @@ | ||
| 9 | 9 | // 3. DuckDuckGo pide su verificación anti-bot: es un bloqueo, no una lista vacía; |
| 10 | 10 | // 4. con resultados, los da (y «Sin resultados» solo cuando el buscador |
| 11 | 11 | // contestó y no encontró nada); |
| 12 | -// 5. un sitio con CORS se lee directo, sin pasar por el proxy. | |
| 12 | +// 5. un sitio con CORS se lee directo, sin pasar por el proxy; | |
| 13 | +// 6-8. y sin decir más de lo que se sabe: el 404 del propio sitio es que la | |
| 14 | +// página no existe; un fetch fallido sin respuesta no distingue «sin CORS» | |
| 15 | +// de «no se pudo conectar», y no se afirma que la página exista. | |
| 13 | 16 | // Todo con page.route: ninguna petición sale a internet. |
| 14 | 17 | // |
| 15 | 18 | // BASE=http://localhost:8799 node tests/web-honest.mjs |
| @@ -25,14 +28,17 @@ | ||
| 25 | 28 | const ANTIBOT = '<html><body><div class="anomaly-modal__title">Unfortunately, bots use DuckDuckGo too.</div></body></html>'; |
| 26 | 29 | |
| 27 | 30 | const b = await chromium.launch(); |
| 28 | -async function escenario(proxy, { directoCORS = false } = {}) { | |
| 31 | +async function escenario(proxy, { directoCORS = false, sitio = null } = {}) { | |
| 29 | 32 | const ctx = await b.newContext(); |
| 30 | 33 | const p = await ctx.newPage(); |
| 31 | 34 | const alProxy = []; |
| 32 | 35 | // Los sitios de fuera: sin CORS (el fetch directo falla), salvo que se diga. |
| 33 | - await p.route(/^https:\/\/(example\.com|html\.duckduckgo\.com|lite\.duckduckgo\.com)\//, r => directoCORS | |
| 34 | - ? r.fulfill({ status: 200, contentType: 'text/html', headers: { 'access-control-allow-origin': '*' }, body: '<html><body><p>Example Domain directo</p></body></html>' }) | |
| 35 | - : r.abort('failed')); | |
| 36 | + // `sitio` contesta en su lugar (un 404 propio, un dominio que no resuelve…). | |
| 37 | + await p.route(/^https:\/\/(example\.com|html\.duckduckgo\.com|lite\.duckduckgo\.com|cors-site\.example|no-such-host\.invalid)\//, r => sitio | |
| 38 | + ? sitio(r) | |
| 39 | + : directoCORS | |
| 40 | + ? r.fulfill({ status: 200, contentType: 'text/html', headers: { 'access-control-allow-origin': '*' }, body: '<html><body><p>Example Domain directo</p></body></html>' }) | |
| 41 | + : r.abort('failed')); | |
| 36 | 42 | await p.route('**/proxy?url=*', r => { alProxy.push(decodeURIComponent(r.request().url().split('url=')[1] || '')); return proxy(r); }); |
| 37 | 43 | await p.goto(BASE + '/manifest.json'); |
| 38 | 44 | const llama = (que, args) => p.evaluate(async ({ que, args }) => { |
| @@ -48,7 +54,10 @@ | ||
| 48 | 54 | const { ctx, llama } = await escenario(r => r.fulfill({ status: 404, contentType: 'text/html', body: '<h1>404 Not Found</h1>' })); |
| 49 | 55 | const f = await llama('fetch', { url: 'https://example.com' }); |
| 50 | 56 | ok('1 · web.fetch sin proxy: falla diciendo que el servidor no tiene el proxy', !f.ok && /no tiene el proxy de respaldo/.test(f.out) && /404/.test(f.out), f.out); |
| 51 | - ok(' …y que no es que la página no exista', /No es que la página no exista/.test(f.out), f.out); | |
| 57 | + // Desde el navegador un fetch fallido no distingue «sin CORS» de «no existe»: | |
| 58 | + // se dicen las dos posibilidades y no se afirma que la página exista. | |
| 59 | + ok(' …sin afirmar que la página exista: CORS o sin conexión, no se distingue', | |
| 60 | + /CORS/.test(f.out) && /no se pudiera conectar/.test(f.out) && /no dice nada de si la página existe/.test(f.out) && !/No es que la página no exista/.test(f.out), f.out); | |
| 52 | 61 | const s = await llama('search', { query: 'mdn array map' }); |
| 53 | 62 | ok('1 · web.search sin proxy: falla con la causa, NO «Sin resultados»', !s.ok && /no tiene el proxy de respaldo/.test(s.out) && !/Sin resultados/.test(s.out), s.out); |
| 54 | 63 | await ctx.close(); |
| @@ -85,6 +94,38 @@ | ||
| 85 | 94 | ok('5 · con CORS se lee directo y el proxy ni se toca', f.ok && /Example Domain directo/.test(f.out) && alProxy.length === 0, `${f.out} · proxy ${alProxy.length}`); |
| 86 | 95 | await ctx.close(); |
| 87 | 96 | } |
| 97 | +// 6 · el sitio contesta él mismo un 404 (con CORS se ve): la página NO existe, y | |
| 98 | +// eso es lo que se dice. Antes, con /proxy en 404, decía «CORS» y «no es que la | |
| 99 | +// página no exista». | |
| 100 | +{ | |
| 101 | + const sinProxy = r => r.fulfill({ status: 404, contentType: 'text/html', body: '<h1>404 Not Found</h1>' }); | |
| 102 | + const { ctx, alProxy, llama } = await escenario(sinProxy, { | |
| 103 | + sitio: r => r.fulfill({ status: 404, contentType: 'text/html', headers: { 'access-control-allow-origin': '*' }, body: 'Not Found' }), | |
| 104 | + }); | |
| 105 | + const f = await llama('fetch', { url: 'https://cors-site.example/no-such-page' }); | |
| 106 | + ok('6 · el 404 del propio sitio: dice que esa página no existe, sin hablar de CORS', !f.ok && /respondió HTTP 404/.test(f.out) && /no existe/.test(f.out) && !/CORS/.test(f.out) && !/No es que/.test(f.out), f.out); | |
| 107 | + ok(' …y ni pregunta al proxy (la respuesta del sitio ya es la que es)', alProxy.length === 0, `proxy ${alProxy.length}`); | |
| 108 | + await ctx.close(); | |
| 109 | +} | |
| 110 | +// 7 · un dominio que no resuelve, sin proxy: no se le llama «CORS» a secas ni se | |
| 111 | +// dice que la página exista. | |
| 112 | +{ | |
| 113 | + const { ctx, llama } = await escenario(r => r.fulfill({ status: 404, contentType: 'text/html', body: '<h1>404 Not Found</h1>' }), { | |
| 114 | + sitio: r => r.abort('namenotresolved'), | |
| 115 | + }); | |
| 116 | + const f = await llama('fetch', { url: 'https://no-such-host.invalid/' }); | |
| 117 | + ok('7 · un dominio que no resuelve: CORS o sin conexión, sin afirmar que la página exista', !f.ok && /no se pudiera conectar/.test(f.out) && !/No es que la página no exista/.test(f.out), f.out); | |
| 118 | + await ctx.close(); | |
| 119 | +} | |
| 120 | +// 8 · el sitio contesta otro error (403) y no hay proxy: se dice su estado. | |
| 121 | +{ | |
| 122 | + const { ctx, alProxy, llama } = await escenario(r => r.fulfill({ status: 404, contentType: 'text/html', body: '<h1>404 Not Found</h1>' }), { | |
| 123 | + sitio: r => r.fulfill({ status: 403, contentType: 'text/html', headers: { 'access-control-allow-origin': '*' }, body: 'Forbidden' }), | |
| 124 | + }); | |
| 125 | + const f = await llama('fetch', { url: 'https://cors-site.example/privada' }); | |
| 126 | + ok('8 · otro error del sitio (403): se dice su estado y que no hay proxy para intentarlo de otra forma', !f.ok && /respondió HTTP 403/.test(f.out) && /no tiene el proxy de respaldo/.test(f.out) && !/CORS/.test(f.out), `${f.out} · proxy ${alProxy.length}`); | |
| 127 | + await ctx.close(); | |
| 128 | +} | |
| 88 | 129 | |
| 89 | 130 | console.log(fails ? `\n❌ ${fails} FALLO(S)` : '\n✅ WEB: si no puede leer o buscar, dice por qué'); |
| 90 | 131 | await b.close(); |
modified web/js/tools/web.js +15 −3
| @@ -15,17 +15,29 @@ | ||
| 15 | 15 | // proxy no existe en este servidor (404): antes salía «HTTP 404» a secas, que |
| 16 | 16 | // se lee como «esa página no existe», y la búsqueda lo convertía en «Sin |
| 17 | 17 | // resultados», como si internet no tuviera nada que decir. |
| 18 | +// | |
| 19 | +// Y sin decir más de lo que se sabe. Si el sitio contestó al fetch directo (con | |
| 20 | +// CORS el navegador deja ver su estado), lo que importa es lo que dijo: su 404 | |
| 21 | +// es que la página no existe, haya proxy o no. Si el fetch directo falló sin | |
| 22 | +// respuesta, desde el navegador no se distingue un sitio sin CORS de uno que no | |
| 23 | +// existe o no resuelve: se dicen las dos cosas y no se afirma que la página | |
| 24 | +// exista. Antes, con /proxy en 404, siempre era «CORS» y «no es que la página no | |
| 25 | +// exista», también ante el 404 del propio sitio o un dominio que no existe. | |
| 18 | 26 | const hostDe = url => { try { return new URL(url).host; } catch { return url; } }; |
| 19 | 27 | async function proxyGet(url) { |
| 20 | - let directo; | |
| 21 | - try { const r = await fetch(url); if (r.ok) return await r.text(); directo = new Error('HTTP ' + r.status); } | |
| 28 | + let directo, estado = 0; | |
| 29 | + try { const r = await fetch(url); if (r.ok) return await r.text(); estado = r.status; directo = new Error('HTTP ' + r.status); } | |
| 22 | 30 | catch (e) { directo = e; } |
| 31 | + if (estado === 404 || estado === 410) | |
| 32 | + throw new Error(`No pude leer ${url}: el sitio respondió HTTP ${estado}, esa página no existe (o ya no está).`); | |
| 23 | 33 | let r; |
| 24 | 34 | try { r = await fetch('/proxy?url=' + encodeURIComponent(url)); } |
| 25 | 35 | catch (e) { throw new Error(`No pude leer ${hostDe(url)}: ni directamente (${directo?.message || 'sin respuesta'}) ni por el proxy del servidor (${e?.message || 'sin respuesta'}).`); } |
| 26 | 36 | if (r.ok) return await r.text(); |
| 27 | 37 | if (r.status === 404 || r.status === 405 || r.status === 501) { |
| 28 | - const e = new Error(`No pude leer ${hostDe(url)}: el sitio no deja leerlo directamente desde el navegador (CORS) y este servidor no tiene el proxy de respaldo (/proxy respondió ${r.status}). No es que la página no exista: desde aquí no se puede leer.`); | |
| 38 | + const e = new Error(estado | |
| 39 | + ? `No pude leer ${hostDe(url)}: el sitio respondió HTTP ${estado}, y este servidor no tiene el proxy de respaldo (/proxy respondió ${r.status}) para intentarlo de otra forma.` | |
| 40 | + : `No pude leer ${hostDe(url)}: el navegador no pudo leerlo directamente (puede que el sitio no lo permita, por CORS, o que no se pudiera conectar con él; desde aquí no se distingue) y este servidor no tiene el proxy de respaldo (/proxy respondió ${r.status}). Esto no dice nada de si la página existe.`); | |
| 29 | 41 | e.sinProxy = true; |
| 30 | 42 | throw e; |
| 31 | 43 | } |