feat(server): worker improve (#10176)

fix AF-2225
This commit is contained in:
darkskygit
2025-02-14 09:47:57 +00:00
parent 1bf1832211
commit 981b4efecf
6 changed files with 207 additions and 10 deletions

View File

@@ -76,3 +76,55 @@ Generated by [AVA](https://avajs.dev).
url: 'http://example.com/page',
videos: [],
}
> Snapshot 5
{
charset: 'gbk',
favicons: [
'http://localhost:3010/api/worker/image-proxy?url=https%3A%2F%2Fexample.com%2Ffavicon.ico',
],
images: [],
title: '你好,世界。',
url: 'http://example.com/gb2312',
videos: [],
}
> Snapshot 6
{
charset: 'shift_jis',
favicons: [
'http://localhost:3010/api/worker/image-proxy?url=https%3A%2F%2Fexample.com%2Ffavicon.ico',
],
images: [],
title: 'こんにちは、世界。',
url: 'http://example.com/shift-jis',
videos: [],
}
> Snapshot 7
{
charset: 'big5',
favicons: [
'http://localhost:3010/api/worker/image-proxy?url=https%3A%2F%2Fexample.com%2Ffavicon.ico',
],
images: [],
title: '你好,世界。',
url: 'http://example.com/big5',
videos: [],
}
> Snapshot 8
{
charset: 'euc-kr',
favicons: [
'http://localhost:3010/api/worker/image-proxy?url=https%3A%2F%2Fexample.com%2Ffavicon.ico',
],
images: [],
title: '안녕하세요, 세계.',
url: 'http://example.com/euc-kr',
videos: [],
}

View File

@@ -171,4 +171,56 @@ test('should preview link', async t => {
fetchSpy.restore();
}
{
const encoded = [
{
content: 'xOO6w6OsysC956Gj',
charset: 'gb2312',
},
{
content: 'grGC8YLJgr+CzYFBkKKKRYFC',
charset: 'shift-jis',
},
{
content: 'p0GmbqFBpUCsyaFD',
charset: 'big5',
},
{
content: 'vsiz58fPvLy/5CwgvLyw6C4=',
charset: 'euc-kr',
},
];
for (const { content, charset } of encoded) {
const before = Buffer.from(`<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=${charset}" />
<meta property="og:title" content="`);
const encoded = Buffer.from(content, 'base64');
const after = Buffer.from(`" />
</head>
</html>
`);
const fakeHTML = new Response(Buffer.concat([before, encoded, after]));
Object.defineProperty(fakeHTML, 'url', {
value: `http://example.com/${charset}`,
});
const fetchSpy = Sinon.stub(global, 'fetch').resolves(fakeHTML);
await assertAndSnapshot(
'/api/worker/link-preview',
'should decode HTML content with charset',
{
status: 200,
method: 'POST',
body: { url: `http://example.com/${charset}` },
}
);
fetchSpy.restore();
}
}
});