@@ -115060,7 +115060,7 @@ var QuarkHandler = class {
115060115060 "sec-ch-ua-mobile": "?0",
115061115061 "sec-ch-ua-platform": "\"Windows\"",
115062115062 "referer": "https://pan.quark.cn/",
115063- "Cookie": "_UP_A4A_11_=wb9ce1f8a7f74209b248cb5877a6a876; _UP_D_=pc; tfstk=gy2rZY6dQTBPmYbgblDFQOhX71M-pv7_ZJgIxkqnV40oVYZ3gkZN2Da7x2PEoyIJFYbJK2qTkbguRYTFLrko97gIP2kUvPSf5O6_2uHKEN_1C6GsZ4kK-9MhGxcbeD-R5O6_quHKKN__NjmeBVnnK0co-Ejq2qmoxy0oiEmZXv03-yj4mqnnK24nKirmkDDn-ycsi2063cllqNA0iO9nAbugqqJU2-ooaQEoupv33dGrSyg2Kp2q2kJ_DBpNOVVtPfguot9rLomUufNP7LzUxlFngJ8lVP40TymYMwRE7Wq76-GXxG0geo2tnj6Gbo2YPue4AHXncPg028DFIGHrE4DqDJ_vE2P0t8G-pUbEJ-r0EWSz_enc4z2LaeAEZmnq5iSVbveZi_88v3Rp9bFx0VsR2BdKZmnq5iS29BhoDmu10i5..; __uid=AARfyLe8Bke3UGL2FjhvNQn6; __sdid=AATHIp7rA8mvfKhhMvPnt0milUEzXi1ReJN0CjChc4LeUA6qYUdYA32jmaL/Lk2AA4s=; __pus=bd604e2725229c945e994c227de77f1cAASVpc7pXsTooap3JBI0XiJI9JU/JUngO9SJCOIRNOLXngLdOfozAiiwF9CnY/xOiEgE8NvKDDg1ZAwX7nVqe9H2ib5aMTAi+a2DfaPE6/6Won1vUOAfKCNELRmJOeYhVn3/eQUYx6B9EIw9HCYjQoKVh76daPI2lnp3QBTK/5zWGya9rVul08y86xpY3APre7I=; __kp=eb241d80-1ee6-11f1-85bf-6167dfcf1acd; __kuus=NenaSYNIw/O9SzABV2HtnjTDoH5aJthk7nOgDdt9pHeaJmMkCD/TY+jVFIrCn+WeFaQVR9h+E/YoStTBZAtB9va0ghzlZCgNuddki8Z8WOnYug==; __puus=2baf96cce907422be5e242bb1d280977AATi54Q/fKnsA5nM/iG/TZHL12hYZj/ELuUFbEwO/2jIXaSGwmvXppDKCITu73rdsZ1hRR0cY2QRpWdM0o+nFv65fCf5ZwPIaGHvUK9BOg3653jTngpTAj41u8kq5vTIDnPDPGWlYPYiUehDbyYBwxKYl12BFSjonkfKybr3Fpc2TYQcm566OQTrPzsupcOkOoa8CQllYvsoVyEBF2ZpiX/5"
115063+ "Cookie": this.cookie
115064115064 },
115065115065 data
115066115066 };
@@ -115087,7 +115087,7 @@ var QuarkHandler = class {
115087115087 "Accept": "*/*,application/json;charset=utf-8",
115088115088 "Accept-Encoding": "gzip, deflate, br, zstd",
115089115089 "Content-Type": "application/json",
115090- "Cookie": "b-user-id=7a421e0c-2ff4-1251-4069-cdfbfedcaf8d; _UP_A4A_11_=wb9cf15ce78d47ed921a9fab3faa85e8; xlly_s=1; __sdid=AAQCvlprpJtHMDSTmliW7CTXaxFJONVhZZyKc8NgJ6Y+XH4iO1v3Q8gYFeEi2hqeV4U=; _UP_D_=pc; __pus=97012d074b8d70d6eeaeb7dbe3fcb05dAAQB4QUahCaghYFA9y/NYud63QbrjWpcO/mp4OgGcA2UZ5O+VMUlfCpacaDzbRBV2O/DK9y9iAFBUL7iPmzdj2d9; __kp=ea759a90-1ed1-11f1-8f67-e7e56f2ee996; __kps=AARfyLe8Bke3UGL2FjhvNQn6; __ktd=axb8KS+96BcSmmEd+gddqg==; __uid=AARfyLe8Bke3UGL2FjhvNQn6; __puus=c6ca8123ab4024f139092c58678de061AATi54Q/fKnsA5nM/iG/TZHLzf8buD1i2D3OKKcgKFxmYvTq9ypLK3mbAQlXFlJz6zEGV7aROE4eXsRbMGviOH4EpwIMv8HZ3UUDPfRoIVsx+mV9xMqXujCxpQR96l9Alpe2B15pFBZl94/lBMSORn+M/cocX9mAe2wX82JvYhQYO46JRC1evvevyyyzaai9ItKcf72BRC6QzioBIic/XHI8; isg=BDg4TjMppS-k-Mk0_qdGtZTlCebKoZwrBz4YK3KgQXMmjdJ3GrWXu63lQY093VQD; tfstk=gTJiuT2wCC5_fZaEErWsOziN8jlKBO6X3EeAktQqTw7Ckctv0ty2knBOgVtv-Z7F2SJA0f_cTUs7_CCZSemDWnwA7NpAns8RR5Rx5qtFmULRuiSqcMkeuUYcGc_AuZYv0CnKeYK6ft62o4H-emTk0mLcbsyaLmScDPod_tj5i4Wqy4HL9k5sWt-TUIxGLH7CmiyNuEWULi7Ubl8V76zFqg6VuE8VTWSRcGPN7N-UTwsV3Z8V3DXFRiXVuEWqxHllpf763pJErkLuNeO_cCsGsa-N7nKvLPWT1nQ33-JHt1brQw243pjM09gPi8cAzQ_Owa8EpRXDYiYhwC0uIEx2VI5HnPo9zeRDuMOIoSbkghdWp6E4_HvcS_JNtoiJ4hs2usAIr8IGBBfkICib8h8RSQW6Du09x_AhNM5Um5W9w3pfadkgPwC5mefJs42MzglbT7y5t-sEDpPbG1SCxapyAdOShopf3Dm3am1NAG_-xDVbG1SCxannx7kf_Ms1y"
115090+ "Cookie": this.cookie
115091115091 },
115092115092 data
115093115093 };
@@ -383630,6 +383630,7 @@ var FileHeaderManager = class {
383630383630 };
383631383631 /**
383632383632 * Find the @header(...) block in the comment text
383633+ * Optimized state machine for parsing nested structures
383633383634 * @param {string} text Comment text
383634383635 * @param {string} ext File extension (.js or .py)
383635383636 * @returns {Object|null} { start, end, content }
@@ -383639,40 +383640,21 @@ var FileHeaderManager = class {
383639383640 if (startIndex === -1) return null;
383640383641 let index = startIndex + 8;
383641383642 let balance = 1;
383642- let inString = false;
383643- let stringChar = "";
383644- let escape = false;
383645- let inLineComment = false;
383646- let inBlockComment = false;
383647- for (; index < text.length; index++) {
383643+ const len = text.length;
383644+ while (index < len) {
383648383645 const char = text[index];
383649- if (inLineComment) {
383650- if (char === "\n") inLineComment = false;
383651- continue;
383652- }
383653- if (inBlockComment) {
383654- if (char === "*" && text[index + 1] === "/") {
383655- inBlockComment = false;
383656- index++;
383646+ if (char === "\"" || char === "'") {
383647+ const quote = char;
383648+ index++;
383649+ while (index < len) {
383650+ const c = text[index];
383651+ if (c === "\\") index += 2;
383652+ else if (c === quote) {
383653+ index++;
383654+ break;
383655+ } else index++;
383657383656 }
383658383657 continue;
383659- }
383660- if (inString) {
383661- if (escape) escape = false;
383662- else if (char === "\\") escape = true;
383663- else if (char === stringChar) inString = false;
383664- continue;
383665- }
383666- if (char === "/" && text[index + 1] === "/") {
383667- inLineComment = true;
383668- index++;
383669- } else if (char === "/" && text[index + 1] === "*") {
383670- inBlockComment = true;
383671- index++;
383672- } else if (ext === ".py" && char === "#") inLineComment = true;
383673- else if (char === "\"" || char === "'") {
383674- inString = true;
383675- stringChar = char;
383676383658 } else if (char === "(") balance++;
383677383659 else if (char === ")") {
383678383660 balance--;
@@ -383681,7 +383663,26 @@ var FileHeaderManager = class {
383681383663 end: index + 1,
383682383664 content: text.substring(startIndex + 8, index)
383683383665 };
383666+ } else if (char === "/") {
383667+ const next = text[index + 1];
383668+ if (next === "/") {
383669+ index += 2;
383670+ const newline = text.indexOf("\n", index);
383671+ index = newline === -1 ? len : newline;
383672+ continue;
383673+ } else if (next === "*") {
383674+ index += 2;
383675+ const endComment = text.indexOf("*/", index);
383676+ if (endComment === -1) index = len;
383677+ else index = endComment + 2;
383678+ continue;
383679+ }
383680+ } else if (ext === ".py" && char === "#") {
383681+ const newline = text.indexOf("\n", index + 1);
383682+ index = newline === -1 ? len : newline;
383683+ continue;
383684383684 }
383685+ index++;
383685383686 }
383686383687 return null;
383687383688 }
@@ -383761,17 +383762,17 @@ var FileHeaderManager = class {
383761383762 const { createBackup = false } = options;
383762383763 if (!filePath || typeof filePath !== "string") throw new Error("Invalid file path");
383763383764 if (!headerObj || typeof headerObj !== "object") throw new Error("Invalid header object");
383765+ const ext = path.extname(filePath);
383766+ const config = this.COMMENT_CONFIG[ext];
383767+ if (!config) throw new Error(`Unsupported file type: ${ext}`);
383768+ const headerStr = `@header(${JSON5.stringify(headerObj, null, 2)})`;
383769+ if (await this._replaceHeaderBuffer(filePath, headerStr, config, ext)) return;
383764383770 let content;
383765383771 try {
383766383772 content = await fs$1.readFile(filePath, "utf8");
383767383773 } catch (error) {
383768383774 throw new Error(`Failed to read file: ${error.message}`);
383769383775 }
383770- const originalContent = content;
383771- const ext = path.extname(filePath);
383772- const config = this.COMMENT_CONFIG[ext];
383773- if (!config) throw new Error(`Unsupported file type: ${ext}`);
383774- const headerStr = `@header(${JSON5.stringify(headerObj, null, 2)})`;
383775383776 const match = content.match(config.regex);
383776383777 let newContent;
383777383778 if (match) {
@@ -383809,15 +383810,8 @@ var FileHeaderManager = class {
383809383810 }
383810383811 if (!newContent.replace(config.regex, "").trim()) throw new Error("写入失败:内容不能只包含文件头而无原始内容");
383811383812 if (!newContent || newContent.trim().length === 0) throw new Error("Generated content is empty, operation aborted");
383812- const originalCodeLines = originalContent.split("\n").filter((line) => {
383813- const trimmed = line.trim();
383814- return trimmed && !trimmed.startsWith("//") && !trimmed.startsWith("/*") && !trimmed.startsWith("*") && !trimmed.startsWith("*/") && !trimmed.startsWith("#") && !trimmed.startsWith("\"\"\"") && !trimmed.startsWith("'''") && !trimmed.startsWith("<?php");
383815- });
383816- const newCodeLines = newContent.split("\n").filter((line) => {
383817- const trimmed = line.trim();
383818- return trimmed && !trimmed.startsWith("//") && !trimmed.startsWith("/*") && !trimmed.startsWith("*") && !trimmed.startsWith("*/") && !trimmed.startsWith("#") && !trimmed.startsWith("\"\"\"") && !trimmed.startsWith("'''") && !trimmed.startsWith("<?php") && !trimmed.includes("@header(");
383819- });
383820- if (originalCodeLines.length > 5 && newCodeLines.length < originalCodeLines.length * .8) throw new Error("Content integrity check failed: significant code loss detected, operation aborted");
383813+ const diffRatio = Math.abs(newContent.length - content.length) / content.length;
383814+ if (content.length > 100 && diffRatio > .5 && newContent.length < content.length) throw new Error("Content integrity check failed: significant size reduction detected, operation aborted");
383821383815 let backupPath = null;
383822383816 if (createBackup) try {
383823383817 backupPath = await this.createBackup(filePath);
@@ -383842,6 +383836,58 @@ var FileHeaderManager = class {
383842383836 }
383843383837 }
383844383838 /**
383839+ * 使用 Buffer 高效地替换文件头,避免大文件 String 转换开销
383840+ * @private
383841+ */
383842+ static async _replaceHeaderBuffer(filePath, headerStr, config, ext) {
383843+ let handle;
383844+ try {
383845+ handle = await fs$1.open(filePath, "r");
383846+ const stats = await handle.stat();
383847+ const scanSize = Math.min(stats.size, 64 * 1024);
383848+ const buffer = Buffer.alloc(scanSize);
383849+ await handle.read(buffer, 0, scanSize, 0);
383850+ await handle.close();
383851+ handle = null;
383852+ const match = buffer.toString("utf8").match(config.regex);
383853+ if (match) {
383854+ const [fullComment] = match;
383855+ const headerBlock = this.findHeaderBlock(fullComment, ext);
383856+ if (headerBlock) {
383857+ const commentStartOffset = buffer.indexOf(fullComment);
383858+ if (commentStartOffset === -1) return false;
383859+ const preHeaderStr = fullComment.substring(0, headerBlock.start);
383860+ const preHeaderLen = Buffer.byteLength(preHeaderStr);
383861+ const postHeaderStr = fullComment.substring(headerBlock.end);
383862+ const newHeaderBuf = Buffer.from(headerStr);
383863+ const replaceStart = commentStartOffset + preHeaderLen;
383864+ const oldHeaderContentStr = headerBlock.content;
383865+ const oldHeaderFullStr = fullComment.substring(headerBlock.start, headerBlock.end);
383866+ const oldHeaderByteLen = Buffer.byteLength(oldHeaderFullStr);
383867+ if (newHeaderBuf.length === oldHeaderByteLen) {
383868+ const writeHandle = await fs$1.open(filePath, "r+");
383869+ await writeHandle.write(newHeaderBuf, 0, newHeaderBuf.length, replaceStart);
383870+ await writeHandle.close();
383871+ return true;
383872+ } else {
383873+ const fullFileBuf = await fs$1.readFile(filePath);
383874+ const finalBuf = Buffer.concat([
383875+ fullFileBuf.subarray(0, replaceStart),
383876+ newHeaderBuf,
383877+ fullFileBuf.subarray(replaceStart + oldHeaderByteLen)
383878+ ]);
383879+ await fs$1.writeFile(filePath, finalBuf);
383880+ return true;
383881+ }
383882+ }
383883+ }
383884+ return false;
383885+ } catch (e) {
383886+ if (handle) await handle.close();
383887+ return false;
383888+ }
383889+ }
383890+ /**
383845383891 * 移除头信息区域
383846383892 * @param {string} input 文件路径或文件内容
383847383893 * @param {Object} [options] 配置选项
0 commit comments