encoding-aware-fs 0.1.3 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +40 -3
- package/dist/server.js +35 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -25813,6 +25813,36 @@ var init_read_file = __esm({
|
|
|
25813
25813
|
}
|
|
25814
25814
|
});
|
|
25815
25815
|
|
|
25816
|
+
// src/encoding/line-endings.ts
|
|
25817
|
+
function detectLineEnding(buffer) {
|
|
25818
|
+
let crlf = 0;
|
|
25819
|
+
let lf = 0;
|
|
25820
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
25821
|
+
if (buffer[i] === 13 && i + 1 < buffer.length && buffer[i + 1] === 10) {
|
|
25822
|
+
crlf++;
|
|
25823
|
+
i++;
|
|
25824
|
+
} else if (buffer[i] === 10) {
|
|
25825
|
+
lf++;
|
|
25826
|
+
}
|
|
25827
|
+
}
|
|
25828
|
+
if (crlf === 0 && lf === 0) {
|
|
25829
|
+
return process.platform === "win32" ? "CRLF" : "LF";
|
|
25830
|
+
}
|
|
25831
|
+
return crlf >= lf ? "CRLF" : "LF";
|
|
25832
|
+
}
|
|
25833
|
+
function restoreLineEndings(text, style) {
|
|
25834
|
+
const normalized = text.replace(/\r\n/g, "\n");
|
|
25835
|
+
if (style === "CRLF") {
|
|
25836
|
+
return normalized.replace(/\n/g, "\r\n");
|
|
25837
|
+
}
|
|
25838
|
+
return normalized;
|
|
25839
|
+
}
|
|
25840
|
+
var init_line_endings = __esm({
|
|
25841
|
+
"src/encoding/line-endings.ts"() {
|
|
25842
|
+
"use strict";
|
|
25843
|
+
}
|
|
25844
|
+
});
|
|
25845
|
+
|
|
25816
25846
|
// src/tools/write-file.ts
|
|
25817
25847
|
function registerWriteFile(server2, config3, allowedDirectories) {
|
|
25818
25848
|
server2.registerTool(
|
|
@@ -25829,8 +25859,10 @@ function registerWriteFile(server2, config3, allowedDirectories) {
|
|
|
25829
25859
|
async (args) => {
|
|
25830
25860
|
const validPath = await validatePath(args.path, allowedDirectories);
|
|
25831
25861
|
let targetEncoding = config3.sourceEncoding || "GB18030";
|
|
25862
|
+
let lineEndingStyle = process.platform === "win32" ? "CRLF" : "LF";
|
|
25832
25863
|
try {
|
|
25833
|
-
await fs5.
|
|
25864
|
+
const existingBuffer = await fs5.readFile(validPath);
|
|
25865
|
+
lineEndingStyle = detectLineEnding(existingBuffer);
|
|
25834
25866
|
const detection = await detectEncoding(validPath);
|
|
25835
25867
|
if (detection.encoding && isUtf8Encoding(detection.encoding)) {
|
|
25836
25868
|
targetEncoding = "UTF-8";
|
|
@@ -25839,7 +25871,8 @@ function registerWriteFile(server2, config3, allowedDirectories) {
|
|
|
25839
25871
|
}
|
|
25840
25872
|
} catch {
|
|
25841
25873
|
}
|
|
25842
|
-
const
|
|
25874
|
+
const finalContent = restoreLineEndings(args.content, lineEndingStyle);
|
|
25875
|
+
const encoded = encodeFromUtf8(finalContent, targetEncoding);
|
|
25843
25876
|
try {
|
|
25844
25877
|
await fs5.writeFile(validPath, encoded, { flag: "wx" });
|
|
25845
25878
|
} catch (error2) {
|
|
@@ -25875,6 +25908,7 @@ var init_write_file = __esm({
|
|
|
25875
25908
|
init_path_validation();
|
|
25876
25909
|
init_detector();
|
|
25877
25910
|
init_converter();
|
|
25911
|
+
init_line_endings();
|
|
25878
25912
|
}
|
|
25879
25913
|
});
|
|
25880
25914
|
|
|
@@ -26400,6 +26434,7 @@ function registerEditFile(server2, config3, allowedDirectories) {
|
|
|
26400
26434
|
async (args) => {
|
|
26401
26435
|
const validPath = await validatePath(args.path, allowedDirectories);
|
|
26402
26436
|
const buffer = await fs6.readFile(validPath);
|
|
26437
|
+
const originalLineEnding = detectLineEnding(buffer);
|
|
26403
26438
|
const detection = await detectEncoding(validPath);
|
|
26404
26439
|
let originalEncoding = "UTF-8";
|
|
26405
26440
|
if (detection.encoding && detection.confidence >= config3.confidenceThreshold && isGBEncoding(detection.encoding)) {
|
|
@@ -26411,7 +26446,8 @@ function registerEditFile(server2, config3, allowedDirectories) {
|
|
|
26411
26446
|
const modifiedContent = applyEditsToContent(utf8Content, args.edits);
|
|
26412
26447
|
const diff = createUnifiedDiff(utf8Content, modifiedContent, args.path);
|
|
26413
26448
|
if (!args.dryRun) {
|
|
26414
|
-
const
|
|
26449
|
+
const finalContent = restoreLineEndings(modifiedContent, originalLineEnding);
|
|
26450
|
+
const encoded = isGBEncoding(originalEncoding) ? encodeFromUtf8(finalContent, originalEncoding) : Buffer.from(finalContent, "utf-8");
|
|
26415
26451
|
const tempPath = `${validPath}.${(0, import_crypto2.randomBytes)(16).toString("hex")}.tmp`;
|
|
26416
26452
|
try {
|
|
26417
26453
|
await fs6.writeFile(tempPath, encoded);
|
|
@@ -26441,6 +26477,7 @@ var init_edit_file = __esm({
|
|
|
26441
26477
|
init_path_validation();
|
|
26442
26478
|
init_detector();
|
|
26443
26479
|
init_converter();
|
|
26480
|
+
init_line_endings();
|
|
26444
26481
|
}
|
|
26445
26482
|
});
|
|
26446
26483
|
|
package/dist/server.js
CHANGED
|
@@ -25050,6 +25050,33 @@ function registerReadFile(server2, config3, allowedDirectories) {
|
|
|
25050
25050
|
// src/tools/write-file.ts
|
|
25051
25051
|
var fs5 = __toESM(require("fs/promises"));
|
|
25052
25052
|
var import_crypto = require("crypto");
|
|
25053
|
+
|
|
25054
|
+
// src/encoding/line-endings.ts
|
|
25055
|
+
function detectLineEnding(buffer) {
|
|
25056
|
+
let crlf = 0;
|
|
25057
|
+
let lf = 0;
|
|
25058
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
25059
|
+
if (buffer[i] === 13 && i + 1 < buffer.length && buffer[i + 1] === 10) {
|
|
25060
|
+
crlf++;
|
|
25061
|
+
i++;
|
|
25062
|
+
} else if (buffer[i] === 10) {
|
|
25063
|
+
lf++;
|
|
25064
|
+
}
|
|
25065
|
+
}
|
|
25066
|
+
if (crlf === 0 && lf === 0) {
|
|
25067
|
+
return process.platform === "win32" ? "CRLF" : "LF";
|
|
25068
|
+
}
|
|
25069
|
+
return crlf >= lf ? "CRLF" : "LF";
|
|
25070
|
+
}
|
|
25071
|
+
function restoreLineEndings(text, style) {
|
|
25072
|
+
const normalized = text.replace(/\r\n/g, "\n");
|
|
25073
|
+
if (style === "CRLF") {
|
|
25074
|
+
return normalized.replace(/\n/g, "\r\n");
|
|
25075
|
+
}
|
|
25076
|
+
return normalized;
|
|
25077
|
+
}
|
|
25078
|
+
|
|
25079
|
+
// src/tools/write-file.ts
|
|
25053
25080
|
function registerWriteFile(server2, config3, allowedDirectories) {
|
|
25054
25081
|
server2.registerTool(
|
|
25055
25082
|
"write_file",
|
|
@@ -25065,8 +25092,10 @@ function registerWriteFile(server2, config3, allowedDirectories) {
|
|
|
25065
25092
|
async (args) => {
|
|
25066
25093
|
const validPath = await validatePath(args.path, allowedDirectories);
|
|
25067
25094
|
let targetEncoding = config3.sourceEncoding || "GB18030";
|
|
25095
|
+
let lineEndingStyle = process.platform === "win32" ? "CRLF" : "LF";
|
|
25068
25096
|
try {
|
|
25069
|
-
await fs5.
|
|
25097
|
+
const existingBuffer = await fs5.readFile(validPath);
|
|
25098
|
+
lineEndingStyle = detectLineEnding(existingBuffer);
|
|
25070
25099
|
const detection = await detectEncoding(validPath);
|
|
25071
25100
|
if (detection.encoding && isUtf8Encoding(detection.encoding)) {
|
|
25072
25101
|
targetEncoding = "UTF-8";
|
|
@@ -25075,7 +25104,8 @@ function registerWriteFile(server2, config3, allowedDirectories) {
|
|
|
25075
25104
|
}
|
|
25076
25105
|
} catch {
|
|
25077
25106
|
}
|
|
25078
|
-
const
|
|
25107
|
+
const finalContent = restoreLineEndings(args.content, lineEndingStyle);
|
|
25108
|
+
const encoded = encodeFromUtf8(finalContent, targetEncoding);
|
|
25079
25109
|
try {
|
|
25080
25110
|
await fs5.writeFile(validPath, encoded, { flag: "wx" });
|
|
25081
25111
|
} catch (error2) {
|
|
@@ -25604,6 +25634,7 @@ function registerEditFile(server2, config3, allowedDirectories) {
|
|
|
25604
25634
|
async (args) => {
|
|
25605
25635
|
const validPath = await validatePath(args.path, allowedDirectories);
|
|
25606
25636
|
const buffer = await fs6.readFile(validPath);
|
|
25637
|
+
const originalLineEnding = detectLineEnding(buffer);
|
|
25607
25638
|
const detection = await detectEncoding(validPath);
|
|
25608
25639
|
let originalEncoding = "UTF-8";
|
|
25609
25640
|
if (detection.encoding && detection.confidence >= config3.confidenceThreshold && isGBEncoding(detection.encoding)) {
|
|
@@ -25615,7 +25646,8 @@ function registerEditFile(server2, config3, allowedDirectories) {
|
|
|
25615
25646
|
const modifiedContent = applyEditsToContent(utf8Content, args.edits);
|
|
25616
25647
|
const diff = createUnifiedDiff(utf8Content, modifiedContent, args.path);
|
|
25617
25648
|
if (!args.dryRun) {
|
|
25618
|
-
const
|
|
25649
|
+
const finalContent = restoreLineEndings(modifiedContent, originalLineEnding);
|
|
25650
|
+
const encoded = isGBEncoding(originalEncoding) ? encodeFromUtf8(finalContent, originalEncoding) : Buffer.from(finalContent, "utf-8");
|
|
25619
25651
|
const tempPath = `${validPath}.${(0, import_crypto2.randomBytes)(16).toString("hex")}.tmp`;
|
|
25620
25652
|
try {
|
|
25621
25653
|
await fs6.writeFile(tempPath, encoded);
|