Skip to content

Commit e548e41

Browse files
authored
🐛 重构 encoding.ts 以整合并改进检测机制 (#1426)
* fix(encoding): 重构 encoding.ts 以整合并改进检测机制 * 人工优化 * readBlobContent -> readRawContent * Update encoding.test.ts * typescript fix * typescript fix
1 parent fdb1906 commit e548e41

6 files changed

Lines changed: 815 additions & 782 deletions

File tree

src/app/service/service_worker/resource.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { calculateHashFromArrayBuffer } from "@App/pkg/utils/crypto";
1313
import { isBase64, parseUrlSRI } from "./utils";
1414
import { stackAsyncTask } from "@App/pkg/utils/async_queue";
1515
import { blobToUint8Array } from "@App/pkg/utils/datatype";
16-
import { readBlobContent } from "@App/pkg/utils/encoding";
16+
import { readRawContent } from "@App/pkg/utils/encoding";
1717

1818
export class ResourceService {
1919
logger: Logger;
@@ -282,7 +282,7 @@ export class ResourceService {
282282
};
283283
if (isText(uint8Array)) {
284284
if (type === "require" || type === "require-css") {
285-
resource.content = await readBlobContent(data, contentType); // @require和@require-css 是会转换成代码运行的,可以进行解码
285+
resource.content = await readRawContent(data, contentType); // @require和@require-css 是会转换成代码运行的,可以进行解码
286286
} else {
287287
resource.content = await data.text(); // @resource 应该要保留原汁原味
288288
}

src/pages/install/App.tsx

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { CACHE_KEY_SCRIPT_INFO } from "@App/app/cache_key";
3333
import { cacheInstance } from "@App/app/cache";
3434
import { formatBytes } from "@App/pkg/utils/utils";
3535
import { ScriptIcons } from "../options/routes/utils";
36-
import { bytesDecode, detectEncoding } from "@App/pkg/utils/encoding";
36+
import { readRawContent } from "@App/pkg/utils/encoding";
3737
import { prettyUrl } from "@App/pkg/utils/url-utils";
3838

3939
const backgroundPromptShownKey = "background_prompt_shown";
@@ -107,19 +107,8 @@ const fetchScriptBody = async (url: string, { onProgress }: { [key: string]: any
107107
position += chunk.length;
108108
}
109109

110-
// 检测编码:优先使用 Content-Type,回退到 chardet(仅检测前16KB)
111110
const contentType = response.headers.get("content-type");
112-
const encode = detectEncoding(chunksAll, contentType);
113-
114-
// 使用检测到的 charset 解码
115-
let code;
116-
try {
117-
code = bytesDecode(encode, chunksAll);
118-
} catch (e: any) {
119-
console.warn(`Failed to decode response with charset ${encode}: ${e.message}`);
120-
// 回退到 UTF-8
121-
code = new TextDecoder("utf-8").decode(chunksAll);
122-
}
111+
const code = await readRawContent(chunksAll, contentType);
123112

124113
const metadata = parseMetadata(code);
125114
if (!metadata) {

src/pages/store/favicons.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { type Script, ScriptDAO } from "@App/app/repo/scripts";
22
import { FaviconDAO, type FaviconFile, type FaviconRecord } from "@App/app/repo/favicon";
33
import { v5 as uuidv5 } from "uuid";
44
import { getFaviconRootFolder } from "@App/app/service/service_worker/utils";
5-
import { readBlobContent } from "@App/pkg/utils/encoding";
5+
import { readRawContent } from "@App/pkg/utils/encoding";
66

77
let scriptDAO: ScriptDAO | null = null;
88
let faviconDAO: FaviconDAO | null = null;
@@ -149,7 +149,7 @@ export async function fetchIconByDomain(domain: string): Promise<string[]> {
149149

150150
// 获取页面HTML
151151
const response = await fetch(url, { signal: timeoutAbortSignal(timeout) });
152-
const html = await readBlobContent(response, response.headers.get("content-type"));
152+
const html = await readRawContent(response, response.headers.get("content-type"));
153153
const resolvedPageUrl = response.url;
154154
const resolvedUrl = new URL(resolvedPageUrl);
155155
const resolvedOrigin = resolvedUrl.origin;

0 commit comments

Comments
 (0)