fix: give null when oEmbed access fails (#8)

This commit is contained in:
Kagami Sascha Rosylight 2023-03-16 04:22:23 +01:00 committed by GitHub
parent 028b2fed2f
commit 376bba9c61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 57 additions and 44 deletions

View File

@ -15,7 +15,21 @@ async function getOEmbedPlayer($, pageUrl) {
if (!href) { if (!href) {
return null; 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 = (() => { const body = (() => {
try { try {
return JSON.parse(oEmbed); return JSON.parse(oEmbed);

56
package-lock.json generated
View File

@ -1,17 +1,17 @@
{ {
"name": "summaly", "name": "summaly",
"version": "3.0.4", "version": "4.0.0",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "summaly", "name": "summaly",
"version": "3.0.4", "version": "4.0.0",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"cheerio": "^1.0.0-rc.12", "cheerio": "1.0.0-rc.12",
"escape-regexp": "0.0.1", "escape-regexp": "0.0.1",
"got": "^12.5.3", "got": "^12.6.0",
"html-entities": "2.3.2", "html-entities": "2.3.2",
"iconv-lite": "0.6.3", "iconv-lite": "0.6.3",
"jschardet": "3.0.0", "jschardet": "3.0.0",
@ -25,7 +25,6 @@
"@types/cheerio": "0.22.18", "@types/cheerio": "0.22.18",
"@types/debug": "4.1.7", "@types/debug": "4.1.7",
"@types/escape-regexp": "^0.0.1", "@types/escape-regexp": "^0.0.1",
"@types/html-entities": "1.3.4",
"@types/node": "16.11.12", "@types/node": "16.11.12",
"debug": "^4.3.4", "debug": "^4.3.4",
"fastify": "^4.13.0", "fastify": "^4.13.0",
@ -1393,16 +1392,6 @@
"@types/node": "*" "@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": { "node_modules/@types/http-cache-semantics": {
"version": "4.0.1", "version": "4.0.1",
"resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz", "resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz",
@ -1838,9 +1827,9 @@
} }
}, },
"node_modules/cacheable-request": { "node_modules/cacheable-request": {
"version": "10.2.7", "version": "10.2.8",
"resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.7.tgz", "resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.8.tgz",
"integrity": "sha512-I4SA6mKgDxcxVbSt/UmIkb9Ny8qSkg6ReBHtAAXnZHk7KOSx5g3DTiAOaYzcHCs6oOdHn+bip9T48E6tMvK9hw==", "integrity": "sha512-IDVO5MJ4LItE6HKFQTqT2ocAQsisOoCTUDu1ddCmnhyiwFQjXNPp4081Xj23N4tO+AFEFNzGuNEf/c8Gwwt15A==",
"dependencies": { "dependencies": {
"@types/http-cache-semantics": "^4.0.1", "@types/http-cache-semantics": "^4.0.1",
"get-stream": "^6.0.1", "get-stream": "^6.0.1",
@ -2780,14 +2769,14 @@
} }
}, },
"node_modules/got": { "node_modules/got": {
"version": "12.5.3", "version": "12.6.0",
"resolved": "https://registry.npmjs.org/got/-/got-12.5.3.tgz", "resolved": "https://registry.npmjs.org/got/-/got-12.6.0.tgz",
"integrity": "sha512-8wKnb9MGU8IPGRIo+/ukTy9XLJBwDiCpIf5TVzQ9Cpol50eMTpBq2GAuDsuDIz7hTYmZgMgC1e9ydr6kSDWs3w==", "integrity": "sha512-WTcaQ963xV97MN3x0/CbAriXFZcXCfgxVp91I+Ze6pawQOa7SgzwSx2zIJJsX+kTajMnVs0xcFD1TxZKFqhdnQ==",
"dependencies": { "dependencies": {
"@sindresorhus/is": "^5.2.0", "@sindresorhus/is": "^5.2.0",
"@szmarczak/http-timer": "^5.0.1", "@szmarczak/http-timer": "^5.0.1",
"cacheable-lookup": "^7.0.0", "cacheable-lookup": "^7.0.0",
"cacheable-request": "^10.2.1", "cacheable-request": "^10.2.8",
"decompress-response": "^6.0.0", "decompress-response": "^6.0.0",
"form-data-encoder": "^2.1.2", "form-data-encoder": "^2.1.2",
"get-stream": "^6.0.1", "get-stream": "^6.0.1",
@ -6322,15 +6311,6 @@
"@types/node": "*" "@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": { "@types/http-cache-semantics": {
"version": "4.0.1", "version": "4.0.1",
"resolved": "https://registry.npmjs.org/@types/http-cache-semantics/-/http-cache-semantics-4.0.1.tgz", "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==" "integrity": "sha512-+qJyx4xiKra8mZrcwhjMRMUhD5NR1R8esPkzIYxX96JiecFoxAXFuz/GpR3+ev4PE1WamHip78wV0vcmPQtp8w=="
}, },
"cacheable-request": { "cacheable-request": {
"version": "10.2.7", "version": "10.2.8",
"resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.7.tgz", "resolved": "https://registry.npmjs.org/cacheable-request/-/cacheable-request-10.2.8.tgz",
"integrity": "sha512-I4SA6mKgDxcxVbSt/UmIkb9Ny8qSkg6ReBHtAAXnZHk7KOSx5g3DTiAOaYzcHCs6oOdHn+bip9T48E6tMvK9hw==", "integrity": "sha512-IDVO5MJ4LItE6HKFQTqT2ocAQsisOoCTUDu1ddCmnhyiwFQjXNPp4081Xj23N4tO+AFEFNzGuNEf/c8Gwwt15A==",
"requires": { "requires": {
"@types/http-cache-semantics": "^4.0.1", "@types/http-cache-semantics": "^4.0.1",
"get-stream": "^6.0.1", "get-stream": "^6.0.1",
@ -7360,14 +7340,14 @@
"dev": true "dev": true
}, },
"got": { "got": {
"version": "12.5.3", "version": "12.6.0",
"resolved": "https://registry.npmjs.org/got/-/got-12.5.3.tgz", "resolved": "https://registry.npmjs.org/got/-/got-12.6.0.tgz",
"integrity": "sha512-8wKnb9MGU8IPGRIo+/ukTy9XLJBwDiCpIf5TVzQ9Cpol50eMTpBq2GAuDsuDIz7hTYmZgMgC1e9ydr6kSDWs3w==", "integrity": "sha512-WTcaQ963xV97MN3x0/CbAriXFZcXCfgxVp91I+Ze6pawQOa7SgzwSx2zIJJsX+kTajMnVs0xcFD1TxZKFqhdnQ==",
"requires": { "requires": {
"@sindresorhus/is": "^5.2.0", "@sindresorhus/is": "^5.2.0",
"@szmarczak/http-timer": "^5.0.1", "@szmarczak/http-timer": "^5.0.1",
"cacheable-lookup": "^7.0.0", "cacheable-lookup": "^7.0.0",
"cacheable-request": "^10.2.1", "cacheable-request": "^10.2.8",
"decompress-response": "^6.0.0", "decompress-response": "^6.0.0",
"form-data-encoder": "^2.1.2", "form-data-encoder": "^2.1.2",
"get-stream": "^6.0.1", "get-stream": "^6.0.1",

View File

@ -20,7 +20,20 @@ async function getOEmbedPlayer($: cheerio.CheerioAPI, pageUrl: string): Promise<
return null; 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 = (() => { const body = (() => {
try { try {
return JSON.parse(oEmbed); return JSON.parse(oEmbed);

View File

@ -1,2 +1,3 @@
<!DOCTYPE html> <!DOCTYPE html>
<link type="application/json+oembed" href="http://localhost:3060/oembe.json" /> <link type="application/json+oembed" href="http://localhost:3060/oembe.json" />
<meta property="og:description" content="nonexistent">

View File

@ -0,0 +1,3 @@
<!DOCTYPE html>
<link type="application/json+oembed" href="http://localhost:+3060/oembed.json" />
<meta property="og:description" content="wrong url">

View File

@ -1,2 +0,0 @@
<!DOCTYPE html>
<link type="application/json+oembed" href="http://localhost+:3060/oembed.json" />

View File

@ -322,12 +322,16 @@ describe("oEmbed", () => {
test('oEmbed with nonexistent path', async () => { test('oEmbed with nonexistent path', async () => {
await setUpFastify('oembed.json', 'htmls/oembed-nonexistent-path.html'); 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 () => { test('oEmbed with wrong path', async () => {
await setUpFastify('oembed.json', 'htmls/oembed-wrong-path.html'); 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 () => { test('oEmbed with OpenGraph', async () => {