diff --git a/built/general.js b/built/general.js index e8104bee..09925a8a 100644 --- a/built/general.js +++ b/built/general.js @@ -15,7 +15,21 @@ async function getOEmbedPlayer($, pageUrl) { if (!href) { return null; } - const oEmbed = await get((new URL(href, pageUrl)).href); + const oEmbedUrl = (() => { + try { + return new URL(href, pageUrl); + } + catch { + return null; + } + })(); + if (!oEmbedUrl) { + return null; + } + const oEmbed = await get(oEmbedUrl.href).catch(() => null); + if (!oEmbed) { + return null; + } const body = (() => { try { return JSON.parse(oEmbed); diff --git a/package-lock.json b/package-lock.json index 2bef9e55..e9aad1b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,17 +1,17 @@ { "name": "summaly", - "version": "3.0.4", + "version": "4.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "summaly", - "version": "3.0.4", + "version": "4.0.0", "license": "MIT", "dependencies": { - "cheerio": "^1.0.0-rc.12", + "cheerio": "1.0.0-rc.12", "escape-regexp": "0.0.1", - "got": "^12.5.3", + "got": "^12.6.0", "html-entities": "2.3.2", "iconv-lite": "0.6.3", "jschardet": "3.0.0", @@ -25,7 +25,6 @@ "@types/cheerio": "0.22.18", "@types/debug": "4.1.7", "@types/escape-regexp": "^0.0.1", - "@types/html-entities": "1.3.4", "@types/node": "16.11.12", "debug": "^4.3.4", "fastify": "^4.13.0", @@ -1393,16 +1392,6 @@ "@types/node": "*" } }, - "node_modules/@types/html-entities": { - "version": "1.3.4", - "resolved": "https://registry.npmjs.org/@types/html-entities/-/html-entities-1.3.4.tgz", - "integrity": "sha512-Ut62LV90H9tgXwyhmfR8U6yCw/6xeo26IlsbAJJfqPomaqDN2zoLb2Z+cbmy5AycJFhwNJDdH0zqjQp7Ox/eXg==", - "deprecated": "This is a stub types definition. html-entities provides its own type definitions, so you do not need this installed.", - "dev": true, - "dependencies": { - "html-entities": "*" - } - }, "node_modules/@types/http-cache-semantics": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz", @@ -1838,9 +1827,9 @@ } }, "node_modules/cacheable-request": { - "version": "10.2.7", - "resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.7.tgz", - "integrity": "sha512-I4SA6mKgDxcxVbSt/UmIkb9Ny8qSkg6ReBHtAAXnZHk7KOSx5g3DTiAOaYzcHCs6oOdHn+bip9T48E6tMvK9hw==", + "version": "10.2.8", + "resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.8.tgz", + "integrity": "sha512-IDVO5MJ4LItE6HKFQTqT2ocAQsisOoCTUDu1ddCmnhyiwFQjXNPp4081Xj23N4tO+AFEFNzGuNEf/c8Gwwt15A==", "dependencies": { "@types/http-cache-semantics": "^4.0.1", "get-stream": "^6.0.1", @@ -2780,14 +2769,14 @@ } }, "node_modules/got": { - "version": "12.5.3", - "resolved": "https://registry.npmjs.org/got/-/got-12.5.3.tgz", - "integrity": "sha512-8wKnb9MGU8IPGRIo+/ukTy9XLJBwDiCpIf5TVzQ9Cpol50eMTpBq2GAuDsuDIz7hTYmZgMgC1e9ydr6kSDWs3w==", + "version": "12.6.0", + "resolved": "https://registry.npmjs.org/got/-/got-12.6.0.tgz", + "integrity": "sha512-WTcaQ963xV97MN3x0/CbAriXFZcXCfgxVp91I+Ze6pawQOa7SgzwSx2zIJJsX+kTajMnVs0xcFD1TxZKFqhdnQ==", "dependencies": { "@sindresorhus/is": "^5.2.0", "@szmarczak/http-timer": "^5.0.1", "cacheable-lookup": "^7.0.0", - "cacheable-request": "^10.2.1", + "cacheable-request": "^10.2.8", "decompress-response": "^6.0.0", "form-data-encoder": "^2.1.2", "get-stream": "^6.0.1", @@ -6322,15 +6311,6 @@ "@types/node": "*" } }, - "@types/html-entities": { - "version": "1.3.4", - "resolved": "https://registry.npmjs.org/@types/html-entities/-/html-entities-1.3.4.tgz", - "integrity": "sha512-Ut62LV90H9tgXwyhmfR8U6yCw/6xeo26IlsbAJJfqPomaqDN2zoLb2Z+cbmy5AycJFhwNJDdH0zqjQp7Ox/eXg==", - "dev": true, - "requires": { - "html-entities": "*" - } - }, "@types/http-cache-semantics": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz", @@ -6656,9 +6636,9 @@ "integrity": "sha512-+qJyx4xiKra8mZrcwhjMRMUhD5NR1R8esPkzIYxX96JiecFoxAXFuz/GpR3+ev4PE1WamHip78wV0vcmPQtp8w==" }, "cacheable-request": { - "version": "10.2.7", - "resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.7.tgz", - "integrity": "sha512-I4SA6mKgDxcxVbSt/UmIkb9Ny8qSkg6ReBHtAAXnZHk7KOSx5g3DTiAOaYzcHCs6oOdHn+bip9T48E6tMvK9hw==", + "version": "10.2.8", + "resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.8.tgz", + "integrity": "sha512-IDVO5MJ4LItE6HKFQTqT2ocAQsisOoCTUDu1ddCmnhyiwFQjXNPp4081Xj23N4tO+AFEFNzGuNEf/c8Gwwt15A==", "requires": { "@types/http-cache-semantics": "^4.0.1", "get-stream": "^6.0.1", @@ -7360,14 +7340,14 @@ "dev": true }, "got": { - "version": "12.5.3", - "resolved": "https://registry.npmjs.org/got/-/got-12.5.3.tgz", - "integrity": "sha512-8wKnb9MGU8IPGRIo+/ukTy9XLJBwDiCpIf5TVzQ9Cpol50eMTpBq2GAuDsuDIz7hTYmZgMgC1e9ydr6kSDWs3w==", + "version": "12.6.0", + "resolved": "https://registry.npmjs.org/got/-/got-12.6.0.tgz", + "integrity": "sha512-WTcaQ963xV97MN3x0/CbAriXFZcXCfgxVp91I+Ze6pawQOa7SgzwSx2zIJJsX+kTajMnVs0xcFD1TxZKFqhdnQ==", "requires": { "@sindresorhus/is": "^5.2.0", "@szmarczak/http-timer": "^5.0.1", "cacheable-lookup": "^7.0.0", - "cacheable-request": "^10.2.1", + "cacheable-request": "^10.2.8", "decompress-response": "^6.0.0", "form-data-encoder": "^2.1.2", "get-stream": "^6.0.1", diff --git a/src/general.ts b/src/general.ts index bb005f11..81a4323f 100644 --- a/src/general.ts +++ b/src/general.ts @@ -20,7 +20,20 @@ async function getOEmbedPlayer($: cheerio.CheerioAPI, pageUrl: string): Promise< return null; } - const oEmbed = await get((new URL(href, pageUrl)).href); + const oEmbedUrl = (() => { + try { + return new URL(href, pageUrl); + } catch { return null } + })(); + if (!oEmbedUrl) { + return null; + } + + const oEmbed = await get(oEmbedUrl.href).catch(() => null); + if (!oEmbed) { + return null; + } + const body = (() => { try { return JSON.parse(oEmbed); diff --git a/test/htmls/oembed-nonexistent-path.html b/test/htmls/oembed-nonexistent-path.html index 50813b0b..98a24ae5 100644 --- a/test/htmls/oembed-nonexistent-path.html +++ b/test/htmls/oembed-nonexistent-path.html @@ -1,2 +1,3 @@ + diff --git a/test/htmls/oembed-wrong-path.html b/test/htmls/oembed-wrong-path.html new file mode 100644 index 00000000..fe4a00f3 --- /dev/null +++ b/test/htmls/oembed-wrong-path.html @@ -0,0 +1,3 @@ + + + diff --git a/test/htmls/oembed-wrong.html b/test/htmls/oembed-wrong.html deleted file mode 100644 index 4327b9de..00000000 --- a/test/htmls/oembed-wrong.html +++ /dev/null @@ -1,2 +0,0 @@ - - diff --git a/test/index.ts b/test/index.ts index e1a3d2f1..1b91e838 100644 --- a/test/index.ts +++ b/test/index.ts @@ -322,12 +322,16 @@ describe("oEmbed", () => { test('oEmbed with nonexistent path', async () => { await setUpFastify('oembed.json', 'htmls/oembed-nonexistent-path.html'); - await expect(summaly(host)).rejects.toThrow('404 Not Found'); + const summary = await summaly(host); + expect(summary.player.url).toBe(null); + expect(summary.description).toBe('nonexistent'); }); test('oEmbed with wrong path', async () => { await setUpFastify('oembed.json', 'htmls/oembed-wrong-path.html'); - await expect(summaly(host)).rejects.toThrow(); + const summary = await summaly(host); + expect(summary.player.url).toBe(null); + expect(summary.description).toBe('wrong url'); }); test('oEmbed with OpenGraph', async () => {