mirror of
https://github.com/misskey-dev/summaly.git
synced 2025-04-29 02:37:27 +09:00
fix: give null when oEmbed access fails (#8)
This commit is contained in:
parent
028b2fed2f
commit
376bba9c61
@ -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);
|
||||
|
56
package-lock.json
generated
56
package-lock.json
generated
@ -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",
|
||||
|
@ -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);
|
||||
|
@ -1,2 +1,3 @@
|
||||
<!DOCTYPE html>
|
||||
<link type="application/json+oembed" href="http://localhost:3060/oembe.json" />
|
||||
<meta property="og:description" content="nonexistent">
|
||||
|
3
test/htmls/oembed-wrong-path.html
Normal file
3
test/htmls/oembed-wrong-path.html
Normal 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">
|
@ -1,2 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<link type="application/json+oembed" href="http://localhost+:3060/oembed.json" />
|
@ -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 () => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user