From 449cc0eb67c34188e9bc09738aaf7b8fa454b4ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20R=C3=A8gne?= Date: Mon, 30 Mar 2020 22:26:24 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20Ne=20pas=20t=C3=A9l=C3=A9charger=20les?= =?UTF-8?q?=20fichiers=20audio=20/=20vid=C3=A9os.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 15 +++++++++++++++ package.json | 1 + src/core/scrapers.js | 7 ++++++- test/polyfill.js | 18 ++++++++++-------- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index cf507f24..9fb80eca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -548,6 +548,15 @@ "integrity": "sha512-tsFzPpcttalNjFBCFMqsKYQcWxxen1pgJR56by//QwvJc4/OUS3kPOOttx2tSIfjsylB0pYu7f5D3K1RCxUnUg==", "dev": true }, + "abort-controller": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", + "integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==", + "dev": true, + "requires": { + "event-target-shim": "^5.0.0" + } + }, "acorn": { "version": "6.4.1", "resolved": "https://registry.npmjs.org/acorn/-/acorn-6.4.1.tgz", @@ -4591,6 +4600,12 @@ "es5-ext": "~0.10.14" } }, + "event-target-shim": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", + "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==", + "dev": true + }, "event-to-promise": { "version": "0.8.0", "resolved": "https://registry.npmjs.org/event-to-promise/-/event-to-promise-0.8.0.tgz", diff --git a/package.json b/package.json index 44983fa0..4dcbc81f 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "clean": "rm -rf .nyc_output/ build/ coverage/ jsdocs/ node_modules/" }, "devDependencies": { + "abort-controller": "3.0.0", "addons-linter": "^1.22.0", "david": "^12.0.0", "eslint": "^6.8.0", diff --git a/src/core/scrapers.js b/src/core/scrapers.js index 291a78ea..9f5c3bf1 100644 --- a/src/core/scrapers.js +++ b/src/core/scrapers.js @@ -116,7 +116,10 @@ export const extract = async function (url, options) { const content = { html: cacheable(async () => { try { - const response = await fetch(url.href); + const controller = new AbortController(); + const response = await fetch(url.href, { + signal: controller.signal, + }); const contentType = response.headers.get("Content-Type"); if (null !== contentType && (contentType.startsWith("text/html") || @@ -124,6 +127,8 @@ export const extract = async function (url, options) { const text = await response.text(); return new DOMParser().parseFromString(text, "text/html"); } + // Si ce n'est pas du HTML : annuler la requête. + controller.abort(); } catch { // Ignorer le cas où l'URL n'est pas accessible. } diff --git a/test/polyfill.js b/test/polyfill.js index 38a35109..ac4a5d76 100644 --- a/test/polyfill.js +++ b/test/polyfill.js @@ -1,9 +1,11 @@ -import { URL } from "url"; -import { JSDOM } from "jsdom"; -import { browser } from "./polyfill/browser.js"; -import { fetch } from "./polyfill/fetch.js"; +import { URL } from "url"; +import AbortController from "abort-controller"; +import { JSDOM } from "jsdom"; +import { browser } from "./polyfill/browser.js"; +import { fetch } from "./polyfill/fetch.js"; -globalThis.URL = URL; -globalThis.DOMParser = new JSDOM().window.DOMParser; -globalThis.browser = browser; -globalThis.fetch = fetch; +globalThis.URL = URL; +globalThis.AbortController = AbortController; +globalThis.DOMParser = new JSDOM().window.DOMParser; +globalThis.browser = browser; +globalThis.fetch = fetch;