dsh-better-edit 0.2.2 → 0.3.1
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/CHANGELOG.md +33 -0
- package/README.md +317 -240
- package/README.zh.md +19 -11
- package/lib/constants.js +3 -1
- package/lib/contract.js +159 -63
- package/lib/drift.js +5 -98
- package/lib/dsh-context.js +2 -24
- package/lib/edit-engine.js +9 -8
- package/lib/edit-pipeline.js +3 -121
- package/lib/edit-response.js +16 -8
- package/lib/file-kind.js +6 -121
- package/lib/file-reader.js +4 -77
- package/lib/file-view.js +486 -0
- package/lib/guidance/index.js +8 -0
- package/lib/guidance/materialize.js +212 -0
- package/lib/guidance/parse.js +80 -0
- package/lib/guidance/resolve.js +116 -0
- package/lib/guidance.js +6 -388
- package/lib/hash-store.js +6 -4
- package/lib/hashline/alphabet.js +2 -6
- package/lib/hashline/anchor-pipeline.js +786 -0
- package/lib/hashline/apply.js +2 -223
- package/lib/hashline/hash-assign.js +255 -0
- package/lib/hashline/hash.js +67 -29
- package/lib/hashline/hasher.js +2 -23
- package/lib/hashline/index.js +9 -4
- package/lib/hashline/parse.js +2 -35
- package/lib/hashline/pure.js +2 -213
- package/lib/hashline/resolve.js +3 -361
- package/lib/hashline/served.js +2 -120
- package/lib/index.js +1 -3
- package/lib/mutation.js +266 -0
- package/lib/noop-guard.js +20 -0
- package/lib/preview-controller.js +65 -0
- package/lib/prompts.js +14 -20
- package/lib/read-and-serve.js +23 -23
- package/lib/read-render.js +3 -139
- package/lib/served-store.js +2 -216
- package/lib/session-view.js +332 -0
- package/lib/tool-batch-edit.js +9 -10
- package/lib/tool-edit.js +73 -129
- package/lib/tool-read.js +3 -5
- package/lib/tool-undo.js +16 -10
- package/lib/truncate.js +3 -95
- package/lib/types/constants.d.ts +2 -0
- package/lib/types/contract.d.ts +120 -15
- package/lib/types/drift.d.ts +5 -29
- package/lib/types/dsh-context.d.ts +2 -20
- package/lib/types/edit-engine.d.ts +2 -2
- package/lib/types/edit-pipeline.d.ts +3 -44
- package/lib/types/file-kind.d.ts +6 -18
- package/lib/types/file-reader.d.ts +4 -45
- package/lib/types/file-view.d.ts +139 -0
- package/lib/types/guidance/index.d.ts +8 -0
- package/lib/types/guidance/materialize.d.ts +23 -0
- package/lib/types/guidance/parse.d.ts +36 -0
- package/lib/types/guidance/resolve.d.ts +55 -0
- package/lib/types/guidance.d.ts +6 -127
- package/lib/types/hashline/alphabet.d.ts +1 -5
- package/lib/types/hashline/anchor-pipeline.d.ts +127 -0
- package/lib/types/hashline/apply.d.ts +1 -22
- package/lib/types/hashline/hash-assign.d.ts +25 -0
- package/lib/types/hashline/hash.d.ts +8 -9
- package/lib/types/hashline/hasher.d.ts +1 -8
- package/lib/types/hashline/index.d.ts +12 -4
- package/lib/types/hashline/parse.d.ts +1 -7
- package/lib/types/hashline/pure.d.ts +1 -26
- package/lib/types/hashline/resolve.d.ts +1 -58
- package/lib/types/hashline/served.d.ts +1 -43
- package/lib/types/mutation.d.ts +131 -0
- package/lib/types/noop-guard.d.ts +29 -0
- package/lib/types/preview-controller.d.ts +33 -0
- package/lib/types/prompts.d.ts +9 -9
- package/lib/types/read-and-serve.d.ts +6 -5
- package/lib/types/read-render.d.ts +3 -18
- package/lib/types/served-store.d.ts +2 -81
- package/lib/types/session-view.d.ts +92 -0
- package/lib/types/tool-edit.d.ts +3 -14
- package/lib/types/truncate.d.ts +3 -29
- package/lib/types/validation.d.ts +6 -7
- package/lib/types/workspace.d.ts +2 -21
- package/lib/validation.js +6 -33
- package/lib/workspace.js +2 -27
- package/lib/write-hook.js +2 -2
- package/package.json +2 -2
package/lib/edit-pipeline.js
CHANGED
|
@@ -1,124 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* served state → apply → re-hash with stable anchors → drift notice. The
|
|
5
|
-
* apply/reject/re-hash computation lives in the edit engine
|
|
6
|
-
* ({@link edit-engine}); this module keeps the single-edit orchestration
|
|
7
|
-
* shape, preview-mode options, and the pipeline result type. All IO goes
|
|
8
|
-
* through the {@link FileIO} bridge so the same pipeline runs on `ctx.fs` and
|
|
9
|
-
* on the host filesystem.
|
|
2
|
+
* @deprecated — private to Mutation seam. Use `from "./mutation.js"` instead.
|
|
3
|
+
* Thin shim re-exporting the pipeline now owned by Mutation.
|
|
10
4
|
* @module dsh-better-edit/edit-pipeline
|
|
11
5
|
*/
|
|
12
|
-
|
|
13
|
-
import { toCwd } from './paths.js';
|
|
14
|
-
import { resEdit, MAX_HASH_LINES, } from './hashline/index.js';
|
|
15
|
-
import { AnchorMismatchError, ServedRejectionError, recordEchoServes, } from './hashline/served.js';
|
|
16
|
-
import { loadServed, sessionKeyFor } from './served-store.js';
|
|
17
|
-
import { scanDrift } from './drift.js';
|
|
18
|
-
import { abortIf, splitLines } from './utils.js';
|
|
19
|
-
import { applyOne } from './edit-engine.js';
|
|
20
|
-
export async function execPipeline(io, params, cwd, options) {
|
|
21
|
-
const path = params.path;
|
|
22
|
-
const editWarnings = [];
|
|
23
|
-
// Resolve the edit up front (before IO) so malformed anchors fail before
|
|
24
|
-
// any filesystem work, exactly as the tool always did.
|
|
25
|
-
const edit = resEdit({
|
|
26
|
-
remove_from: params.remove_from,
|
|
27
|
-
remove_to: params.remove_to,
|
|
28
|
-
replacement_text: params.replacement_text,
|
|
29
|
-
}, editWarnings);
|
|
30
|
-
const hashStore = options?.store;
|
|
31
|
-
const signal = options?.signal;
|
|
32
|
-
abortIf(signal);
|
|
33
|
-
const absolutePath = await io.resolve(path, cwd, signal);
|
|
34
|
-
const rawText = await io.readText(absolutePath, signal);
|
|
35
|
-
const { normalized: originalNormalized, bom, originalEnding, fileHashes: originalHashes, hadUtf8DecodeErrors, } = await normFromText({
|
|
36
|
-
absolutePath,
|
|
37
|
-
rawText,
|
|
38
|
-
displayPath: path,
|
|
39
|
-
signal,
|
|
40
|
-
maxLines: MAX_HASH_LINES,
|
|
41
|
-
store: hashStore,
|
|
42
|
-
noPersist: options?.noPersist,
|
|
43
|
-
});
|
|
44
|
-
const sessionKey = options?.sessionKey ?? sessionKeyFor(undefined);
|
|
45
|
-
const served = await loadServed(sessionKey, absolutePath);
|
|
46
|
-
const policy = options?.noPersist === true ? 'preview' : 'live';
|
|
47
|
-
const applied = await applyOne({
|
|
48
|
-
content: originalNormalized,
|
|
49
|
-
hashes: originalHashes,
|
|
50
|
-
served,
|
|
51
|
-
removeFrom: params.remove_from,
|
|
52
|
-
removeTo: params.remove_to,
|
|
53
|
-
replacementText: params.replacement_text,
|
|
54
|
-
absolutePath,
|
|
55
|
-
displayPath: path,
|
|
56
|
-
signal,
|
|
57
|
-
warnings: editWarnings,
|
|
58
|
-
store: hashStore,
|
|
59
|
-
persist: options?.noPersist !== true,
|
|
60
|
-
edit,
|
|
61
|
-
}, async (error) => {
|
|
62
|
-
if (error instanceof AnchorMismatchError ||
|
|
63
|
-
error instanceof ServedRejectionError) {
|
|
64
|
-
await recordEchoServes(sessionKey, absolutePath, error.servedRows, policy, originalHashes.length);
|
|
65
|
-
}
|
|
66
|
-
throw error;
|
|
67
|
-
});
|
|
68
|
-
const result = applied.result;
|
|
69
|
-
const isNoop = applied.noop;
|
|
70
|
-
const warnings = [...editWarnings, ...(applied.anchorWarnings ?? [])];
|
|
71
|
-
let driftNotice;
|
|
72
|
-
if (options?.noPersist !== true) {
|
|
73
|
-
try {
|
|
74
|
-
driftNotice = await scanDrift({
|
|
75
|
-
sessionKey,
|
|
76
|
-
served,
|
|
77
|
-
resultHashes: applied.hashes,
|
|
78
|
-
resultLines: splitLines(result),
|
|
79
|
-
range: applied.range,
|
|
80
|
-
path: absolutePath,
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
catch (error) {
|
|
84
|
-
console.error('Failed to compute drift notice:', error);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return {
|
|
88
|
-
path,
|
|
89
|
-
absolutePath,
|
|
90
|
-
originalNormalized,
|
|
91
|
-
result,
|
|
92
|
-
bom,
|
|
93
|
-
originalEnding,
|
|
94
|
-
hadUtf8DecodeErrors,
|
|
95
|
-
warnings,
|
|
96
|
-
noopEdit: applied.noopEdit,
|
|
97
|
-
firstChangedLine: applied.firstChangedLine,
|
|
98
|
-
lastChangedLine: applied.lastChangedLine,
|
|
99
|
-
originalHashes,
|
|
100
|
-
resultHashes: applied.hashes,
|
|
101
|
-
totalAddedLines: applied.totalAddedLines,
|
|
102
|
-
totalRemovedLines: applied.totalRemovedLines,
|
|
103
|
-
driftNotice,
|
|
104
|
-
range: applied.range,
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
/** Resolve the display path a caller names against the session cwd. */
|
|
108
|
-
export function resolveDisplayPath(path, cwd) {
|
|
109
|
-
return toCwd(path, cwd);
|
|
110
|
-
}
|
|
111
|
-
/** Snapshot bookkeeping for noop/success results (best-effort). */
|
|
112
|
-
export async function snapshotIdFor(io, absolutePath, signal) {
|
|
113
|
-
try {
|
|
114
|
-
return await io.statVersion(absolutePath, signal);
|
|
115
|
-
}
|
|
116
|
-
catch {
|
|
117
|
-
try {
|
|
118
|
-
return (await fileSnap(absolutePath)).snapshotId;
|
|
119
|
-
}
|
|
120
|
-
catch {
|
|
121
|
-
return undefined;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
6
|
+
export * from "./mutation.js";
|
package/lib/edit-response.js
CHANGED
|
@@ -34,7 +34,7 @@ export function finalizeToolResult(details) {
|
|
|
34
34
|
return { content: [{ type: "text", text }], servedRows: details.servedRows };
|
|
35
35
|
}
|
|
36
36
|
function warnBlock(warnings) {
|
|
37
|
-
return warnings?.length ? `\n\
|
|
37
|
+
return warnings?.length ? `\n\n${warnings.join("\n")}` : "";
|
|
38
38
|
}
|
|
39
39
|
function driftBlock(driftNotice) {
|
|
40
40
|
return driftNotice ? `\n\n${driftNotice}` : "";
|
|
@@ -61,7 +61,7 @@ export function buildNoop(input) {
|
|
|
61
61
|
classification: "noop",
|
|
62
62
|
metrics,
|
|
63
63
|
...(warnings !== undefined && warnings.length > 0 ? { warnings } : {}),
|
|
64
|
-
...(driftNotice
|
|
64
|
+
...(driftNotice === undefined ? {} : { driftNotice }),
|
|
65
65
|
},
|
|
66
66
|
};
|
|
67
67
|
}
|
|
@@ -92,6 +92,10 @@ export function buildChanged(input) {
|
|
|
92
92
|
addedLines,
|
|
93
93
|
removedLines,
|
|
94
94
|
});
|
|
95
|
+
const denseServedRows = [];
|
|
96
|
+
for (let i = 0; i < resultHashes.length; i++) {
|
|
97
|
+
denseServedRows.push({ position: i, hash: resultHashes[i] });
|
|
98
|
+
}
|
|
95
99
|
return {
|
|
96
100
|
content: [{ type: "text", text }],
|
|
97
101
|
details: {
|
|
@@ -100,8 +104,8 @@ export function buildChanged(input) {
|
|
|
100
104
|
snapshotId,
|
|
101
105
|
metrics,
|
|
102
106
|
...(warnings !== undefined && warnings.length > 0 ? { warnings } : {}),
|
|
103
|
-
servedRows:
|
|
104
|
-
...(driftNotice
|
|
107
|
+
servedRows: denseServedRows,
|
|
108
|
+
...(driftNotice === undefined ? {} : { driftNotice }),
|
|
105
109
|
},
|
|
106
110
|
};
|
|
107
111
|
}
|
|
@@ -132,7 +136,7 @@ export function buildBatchResult(sections) {
|
|
|
132
136
|
warningsCount: warnings.length,
|
|
133
137
|
}),
|
|
134
138
|
...(warnings.length > 0 ? { warnings } : {}),
|
|
135
|
-
...(driftNotice
|
|
139
|
+
...(driftNotice === undefined ? {} : { driftNotice }),
|
|
136
140
|
},
|
|
137
141
|
};
|
|
138
142
|
}
|
|
@@ -141,8 +145,12 @@ export function buildBatchResult(sections) {
|
|
|
141
145
|
for (const s of appliedFiles) {
|
|
142
146
|
const diffResult = genDiff(s.originalNormalized, s.result, 1, s.resultHashes, s.originalHashes);
|
|
143
147
|
diffParts.push(`--- ${s.path} ---\n${diffResult.diff}`);
|
|
144
|
-
|
|
145
|
-
|
|
148
|
+
const denseRows = [];
|
|
149
|
+
for (let i = 0; i < s.resultHashes.length; i++) {
|
|
150
|
+
denseRows.push({ position: i, hash: s.resultHashes[i] });
|
|
151
|
+
}
|
|
152
|
+
if (denseRows.length > 0) {
|
|
153
|
+
servedByPath.push({ path: s.path, servedRows: denseRows });
|
|
146
154
|
}
|
|
147
155
|
}
|
|
148
156
|
const diff = diffParts.join("\n\n");
|
|
@@ -166,7 +174,7 @@ export function buildBatchResult(sections) {
|
|
|
166
174
|
...(warnings.length > 0 ? { warnings } : {}),
|
|
167
175
|
servedRows: servedByPath.flatMap((e) => e.servedRows),
|
|
168
176
|
servedByPath,
|
|
169
|
-
...(driftNotice
|
|
177
|
+
...(driftNotice === undefined ? {} : { driftNotice }),
|
|
170
178
|
},
|
|
171
179
|
};
|
|
172
180
|
}
|
package/lib/file-kind.js
CHANGED
|
@@ -1,121 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
"image/gif",
|
|
8
|
-
"image/webp",
|
|
9
|
-
]);
|
|
10
|
-
const TEXT_TYPES = new Set([
|
|
11
|
-
"application/rtf",
|
|
12
|
-
"application/xml",
|
|
13
|
-
"application/x-ms-regedit",
|
|
14
|
-
]);
|
|
15
|
-
function detectTextBom(sample) {
|
|
16
|
-
if (sample.length >= 4 &&
|
|
17
|
-
sample[0] === 0xff &&
|
|
18
|
-
sample[1] === 0xfe &&
|
|
19
|
-
sample[2] === 0x00 &&
|
|
20
|
-
sample[3] === 0x00)
|
|
21
|
-
return "UTF-32LE";
|
|
22
|
-
if (sample.length >= 4 &&
|
|
23
|
-
sample[0] === 0x00 &&
|
|
24
|
-
sample[1] === 0x00 &&
|
|
25
|
-
sample[2] === 0xfe &&
|
|
26
|
-
sample[3] === 0xff)
|
|
27
|
-
return "UTF-32BE";
|
|
28
|
-
if (sample.length >= 2 && sample[0] === 0xff && sample[1] === 0xfe)
|
|
29
|
-
return "UTF-16LE";
|
|
30
|
-
if (sample.length >= 2 && sample[0] === 0xfe && sample[1] === 0xff)
|
|
31
|
-
return "UTF-16BE";
|
|
32
|
-
return undefined;
|
|
33
|
-
}
|
|
34
|
-
function isTextType(mimeType) {
|
|
35
|
-
return mimeType.startsWith("text/") || TEXT_TYPES.has(mimeType);
|
|
36
|
-
}
|
|
37
|
-
export async function loadFileKindAndText(filePath, options) {
|
|
38
|
-
const pathStat = await fsStat(filePath);
|
|
39
|
-
if (pathStat.isDirectory()) {
|
|
40
|
-
return { kind: "directory" };
|
|
41
|
-
}
|
|
42
|
-
if (!pathStat.isFile()) {
|
|
43
|
-
return {
|
|
44
|
-
kind: "binary",
|
|
45
|
-
description: "unsupported file type",
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
if (pathStat.size > MAX_BYTES) {
|
|
49
|
-
return {
|
|
50
|
-
kind: "binary",
|
|
51
|
-
description: `file exceeds ${MAX_BYTES} byte limit`
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
const fileHandle = await fsOpen(filePath, "r");
|
|
55
|
-
try {
|
|
56
|
-
const buffer = Buffer.alloc(SNIFF_BYTES);
|
|
57
|
-
const { bytesRead } = await fileHandle.read(buffer, 0, SNIFF_BYTES, 0);
|
|
58
|
-
if (bytesRead === 0) {
|
|
59
|
-
return { kind: "text", text: "" };
|
|
60
|
-
}
|
|
61
|
-
const sample = buffer.subarray(0, bytesRead);
|
|
62
|
-
const textBom = detectTextBom(sample);
|
|
63
|
-
if (textBom) {
|
|
64
|
-
return {
|
|
65
|
-
kind: "binary",
|
|
66
|
-
description: `${textBom} encoded text`
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
const detectedMimeType = (await fileTypeFromBuffer(sample))?.mime;
|
|
70
|
-
if (detectedMimeType !== undefined &&
|
|
71
|
-
!isTextType(detectedMimeType)) {
|
|
72
|
-
if (IMG_TYPES.has(detectedMimeType)) {
|
|
73
|
-
return { kind: "image", mimeType: detectedMimeType };
|
|
74
|
-
}
|
|
75
|
-
return {
|
|
76
|
-
kind: "binary",
|
|
77
|
-
description: detectedMimeType,
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
const decoder = new TextDecoder("utf-8", { fatal: false, ignoreBOM: true });
|
|
81
|
-
let hadUtf8DecodeErrors = false;
|
|
82
|
-
let newlineCount = 0;
|
|
83
|
-
const parts = [];
|
|
84
|
-
function decodeChunk(chunk, stream) {
|
|
85
|
-
const decoded = decoder.decode(chunk, { stream });
|
|
86
|
-
if (!hadUtf8DecodeErrors && decoded.includes("\uFFFD")) {
|
|
87
|
-
hadUtf8DecodeErrors = true;
|
|
88
|
-
}
|
|
89
|
-
if (options?.maxLines !== undefined) {
|
|
90
|
-
for (let i = 0; i < decoded.length; i++) {
|
|
91
|
-
if (decoded.charCodeAt(i) === 10)
|
|
92
|
-
newlineCount++;
|
|
93
|
-
}
|
|
94
|
-
if (newlineCount > options.maxLines) {
|
|
95
|
-
throw new Error(`[E_FILE_TOO_LARGE] ${options.displayPath ?? filePath} has more than ${options.maxLines} lines, exceeding the ${options.maxLines}-line edit limit. Hashline editing targets source-sized files; for very large files use write or a non-line-based approach.`);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
return decoded;
|
|
99
|
-
}
|
|
100
|
-
parts.push(decodeChunk(sample, true));
|
|
101
|
-
let position = bytesRead;
|
|
102
|
-
while (true) {
|
|
103
|
-
const { bytesRead: chunkBytesRead } = await fileHandle.read(buffer, 0, SNIFF_BYTES, position);
|
|
104
|
-
if (chunkBytesRead === 0) {
|
|
105
|
-
break;
|
|
106
|
-
}
|
|
107
|
-
const chunk = buffer.subarray(0, chunkBytesRead);
|
|
108
|
-
parts.push(decodeChunk(chunk, true));
|
|
109
|
-
position += chunkBytesRead;
|
|
110
|
-
}
|
|
111
|
-
parts.push(decodeChunk(new Uint8Array(0), false));
|
|
112
|
-
return {
|
|
113
|
-
kind: "text",
|
|
114
|
-
text: parts.join(""),
|
|
115
|
-
...(hadUtf8DecodeErrors ? { hadUtf8DecodeErrors: true } : {}),
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
finally {
|
|
119
|
-
await fileHandle.close();
|
|
120
|
-
}
|
|
121
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @deprecated — private to FileView seam. Use `from "./file-view.js"` instead.
|
|
3
|
+
* Thin shim re-exporting the read path now owned by FileView.
|
|
4
|
+
* @module dsh-better-edit/file-kind
|
|
5
|
+
*/
|
|
6
|
+
export * from "./file-view.js";
|
package/lib/file-reader.js
CHANGED
|
@@ -1,79 +1,6 @@
|
|
|
1
|
-
import { constants } from "node:fs";
|
|
2
|
-
import { stat } from "fs/promises";
|
|
3
|
-
import { lineHashes } from "./hashline/index.js";
|
|
4
|
-
import { loadFileKindAndText } from "./file-kind.js";
|
|
5
|
-
import { resolveTarget, toCwd } from "./paths.js";
|
|
6
|
-
import { detectEnding, toLF, stripBOM } from "./edit-diff.js";
|
|
7
|
-
import { abortIf } from "./utils.js";
|
|
8
|
-
import { valKind, valAccess } from "./validation.js";
|
|
9
|
-
import { visLines } from "./utils.js";
|
|
10
|
-
function fmtSnapId(canonicalPath, info) {
|
|
11
|
-
return `v2|${canonicalPath}|${info.ino}|${info.mtimeMs}|${info.ctimeMs}|${info.size}`;
|
|
12
|
-
}
|
|
13
|
-
export async function fileSnap(absolutePath) {
|
|
14
|
-
const canonicalPath = await resolveTarget(absolutePath);
|
|
15
|
-
const stats = await stat(canonicalPath);
|
|
16
|
-
return {
|
|
17
|
-
snapshotId: fmtSnapId(canonicalPath, stats),
|
|
18
|
-
ino: stats.ino,
|
|
19
|
-
mtimeMs: stats.mtimeMs,
|
|
20
|
-
ctimeMs: stats.ctimeMs,
|
|
21
|
-
size: stats.size,
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
1
|
/**
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* This is the string-level core of the read/edit pipeline; the caller owns
|
|
30
|
-
* path resolution and raw IO (local filesystem, `ctx.fs`, or a sandbox
|
|
31
|
-
* backend) so the same anchors are produced no matter where the file lives.
|
|
2
|
+
* @deprecated — private to FileView seam. Use `from "./file-view.js"` instead.
|
|
3
|
+
* Thin shim re-exporting the read path now owned by FileView.
|
|
4
|
+
* @module dsh-better-edit/file-reader
|
|
32
5
|
*/
|
|
33
|
-
export
|
|
34
|
-
const { absolutePath, displayPath, signal } = input;
|
|
35
|
-
abortIf(signal);
|
|
36
|
-
const { bom, text: rawContent } = stripBOM(input.rawText);
|
|
37
|
-
const originalEnding = detectEnding(rawContent);
|
|
38
|
-
const normalized = toLF(rawContent);
|
|
39
|
-
if (input.maxLines !== undefined) {
|
|
40
|
-
const lineCount = visLines(normalized).length;
|
|
41
|
-
if (lineCount > input.maxLines) {
|
|
42
|
-
throw new Error(`[E_FILE_TOO_LARGE] ${displayPath} has ${lineCount} lines, exceeding the ${input.maxLines}-line edit limit. Hashline editing targets source-sized files; for very large files use write or a non-line-based approach.`);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
const fileHashes = await lineHashes(normalized, absolutePath, undefined, input.store, input.noPersist !== true);
|
|
46
|
-
return {
|
|
47
|
-
absolutePath,
|
|
48
|
-
normalized,
|
|
49
|
-
bom,
|
|
50
|
-
originalEnding,
|
|
51
|
-
fileHashes,
|
|
52
|
-
hadUtf8DecodeErrors: input.hadUtf8DecodeErrors === true,
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
export async function readNormFile(path, cwd, options) {
|
|
56
|
-
const absolutePath = toCwd(path, cwd);
|
|
57
|
-
const resolvedPath = await resolveTarget(absolutePath);
|
|
58
|
-
const signal = options?.signal;
|
|
59
|
-
const accessMode = options?.accessMode ?? constants.R_OK;
|
|
60
|
-
abortIf(signal);
|
|
61
|
-
await valAccess(resolvedPath, path, accessMode);
|
|
62
|
-
abortIf(signal);
|
|
63
|
-
const file = options?.preloadedFile ??
|
|
64
|
-
(await loadFileKindAndText(resolvedPath, {
|
|
65
|
-
maxLines: options?.maxLines,
|
|
66
|
-
displayPath: path,
|
|
67
|
-
}));
|
|
68
|
-
valKind(file, path);
|
|
69
|
-
return normFromText({
|
|
70
|
-
absolutePath: resolvedPath,
|
|
71
|
-
rawText: file.text,
|
|
72
|
-
displayPath: path,
|
|
73
|
-
signal,
|
|
74
|
-
maxLines: options?.maxLines,
|
|
75
|
-
store: options?.store,
|
|
76
|
-
noPersist: options?.noPersist,
|
|
77
|
-
hadUtf8DecodeErrors: file.hadUtf8DecodeErrors,
|
|
78
|
-
});
|
|
79
|
-
}
|
|
6
|
+
export * from "./file-view.js";
|