Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/misskey-dev/summaly
Browse files Browse the repository at this point in the history
  • Loading branch information
tamaina committed Mar 16, 2023
2 parents 441e8c2 + 376bba9 commit 1bab7af
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 44 deletions.
16 changes: 15 additions & 1 deletion built/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
56 changes: 18 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion src/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions test/htmls/oembed-nonexistent-path.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<!DOCTYPE html>
<link type="application/json+oembed" href="http://localhost:3060/oembe.json" />
<meta property="og:description" content="nonexistent">
3 changes: 3 additions & 0 deletions test/htmls/oembed-wrong-path.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!DOCTYPE html>
<link type="application/json+oembed" href="http://localhost:+3060/oembed.json" />
<meta property="og:description" content="wrong url">
2 changes: 0 additions & 2 deletions test/htmls/oembed-wrong.html

This file was deleted.

8 changes: 6 additions & 2 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 1bab7af

Please sign in to comment.