fix: don't try to re-decode prepared response (#498)

* fix: don't try to re-decode prepared response

* Remove stray console.log
pull/499/head
Ethan Jucovy 2 years ago committed by GitHub
parent 9515dc28c1
commit af9cfcd120
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -26,7 +26,11 @@ const Resource = {
},
};
result = { body: preparedResponse, response: validResponse };
result = {
body: preparedResponse,
response: validResponse,
alreadyDecoded: true,
};
} else {
result = await fetchResource(url, parsedUrl, headers);
}
@ -39,7 +43,7 @@ const Resource = {
return this.generateDoc(result);
},
generateDoc({ body: content, response }) {
generateDoc({ body: content, response, alreadyDecoded = false }) {
const { 'content-type': contentType = '' } = response.headers;
// TODO: Implement is_text function from
@ -48,7 +52,7 @@ const Resource = {
throw new Error('Content does not appear to be text.');
}
let $ = this.encodeDoc({ content, contentType });
let $ = this.encodeDoc({ content, contentType, alreadyDecoded });
if ($.root().children().length === 0) {
throw new Error('No children, likely a bad parse.');
@ -61,11 +65,14 @@ const Resource = {
return $;
},
encodeDoc({ content, contentType }) {
encodeDoc({ content, contentType, alreadyDecoded = false }) {
if (alreadyDecoded) {
return cheerio.load(content);
}
const encoding = getEncoding(contentType);
let decodedContent = iconv.decode(content, encoding);
let $ = cheerio.load(decodedContent);
// after first cheerio.load, check to see if encoding matches
const contentTypeSelector = cheerio.browser
? 'meta[http-equiv=content-type]'

@ -78,6 +78,16 @@ describe('Resource', () => {
assert.equal(badEncodingRe.test($.html()), false);
assert.equal(typeof $, 'function');
});
it('doesnt mangle non-ascii characters in prefetched response', async () => {
const url = 'https://www.gruene.de/themen/digitalisierung';
const prefetched =
'<!DOCTYPE html><html lang="de"><head><meta charSet="UTF-8"/><meta http-equiv="x-ua-compatible" content="ie=edge"/><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/></head><body><div id="___gatsby"><h1 class="styles-module--headline--niWjO">Wir gestalten die Digitalisierung</h1><p>Wir Grüne kämpfen für ein offenes, gemeinwohlorientiertes Netz. Wir wollen den digitalen Wandel gerecht gestalten und setzen uns für Verantwortung, Freiheit und Recht im Netz ein. Netzpolitik und Digitalisierung sind zentrale politische Querschnittsaufgaben für eine moderne Gesellschaft. Im Mittelpunkt stehen für uns eine zukunftsfähige digitale Infrastruktur, der freie und gleichberechtigte Zugang zum Netz für alle, der Schutz unserer Privatsphäre und persönlichen Daten, sowie eine modernisierte Verwaltung.</p></div></body></html>';
const $ = await Resource.create(url, prefetched);
assert.equal(/Gr&#xFC;ne/.test($.html()), true);
assert.equal(/&#xFFFD;/.test($.html()), false);
});
});
describe('generateDoc({ body, response })', () => {

Loading…
Cancel
Save