dsh-better-edit 0.3.1 → 0.3.2

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 CHANGED
@@ -6,6 +6,12 @@ Entries link to the originating spec issue in [pi-hashline-edit-lsz](https://git
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.3.2] - 2026-08-23
10
+
11
+ ### Fixed
12
+
13
+ - Preserve a stripped UTF-8 BOM across `ctx.fs`-backed `edit` and `undo_last_edit` writes while retaining the file's original line endings (#23).
14
+
9
15
  ## [0.3.1] - 2026-08-21
10
16
 
11
17
  ### Changed
package/lib/fs-bridge.js CHANGED
@@ -50,6 +50,43 @@ export function mapFsError(error, displayPath) {
50
50
  }
51
51
  throw error;
52
52
  }
53
+ const UTF8_BOM = "\uFEFF";
54
+ const UTF8_BOM_BYTES = [0xef, 0xbb, 0xbf];
55
+ const UTF8_BOM_LEN = UTF8_BOM_BYTES.length;
56
+ /**
57
+ * Restore a UTF-8 BOM consumed by a backend's {@link TextDecoder}. The raw
58
+ * byte seam reads whole files rather than prefixes, so first use stat size to
59
+ * narrow the expensive probe to the only possible BOM case: exactly three
60
+ * storage bytes are missing from the decoded UTF-8 representation.
61
+ *
62
+ * Compensation for upstream BOM stripping: `dsh-fs-local` decodes with
63
+ * `TextDecoder("utf-8",{fatal:true})` (`ignoreBOM:false` by default) and
64
+ * swallows leading `EF BB BF`.
65
+ * https://github.com/deepseek-ai/deepseek-harness/discussions/1026
66
+ * https://github.com/Rianico/dsh-better-edit/issues/23
67
+ */
68
+ async function restoreStrippedUtf8Bom(fs, target, text, signal) {
69
+ if (text.startsWith(UTF8_BOM))
70
+ return text;
71
+ const info = await fs.stat(target, signal);
72
+ if (info?.size === undefined ||
73
+ info.size !== Buffer.byteLength(text, "utf-8") + UTF8_BOM_LEN) {
74
+ return text;
75
+ }
76
+ const bytes = await fs.readBytes(target, signal, info.size);
77
+ const encodedText = Buffer.from(text, "utf-8");
78
+ if (bytes.length !== encodedText.length + UTF8_BOM_LEN ||
79
+ bytes[0] !== UTF8_BOM_BYTES[0] ||
80
+ bytes[1] !== UTF8_BOM_BYTES[1] ||
81
+ bytes[2] !== UTF8_BOM_BYTES[2]) {
82
+ return text;
83
+ }
84
+ for (let i = 0; i < encodedText.length; i += 1) {
85
+ if (bytes[i + UTF8_BOM_LEN] !== encodedText[i])
86
+ return text;
87
+ }
88
+ return `${UTF8_BOM}${text}`;
89
+ }
53
90
  /** FileIO over the deployment's `ctx.fs` service. */
54
91
  export function ctxFsIO(fs, ctx) {
55
92
  return {
@@ -65,7 +102,8 @@ export function ctxFsIO(fs, ctx) {
65
102
  const target = await fs.resolve(absolutePath, {
66
103
  ...(signal !== undefined ? { signal } : {}),
67
104
  });
68
- return await fs.readText(target, signal);
105
+ const text = await fs.readText(target, signal);
106
+ return await restoreStrippedUtf8Bom(fs, target, text, signal);
69
107
  }
70
108
  catch (error) {
71
109
  return mapFsError(error, absolutePath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-better-edit",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "type": "module",
5
5
  "description": "Hash-anchored read/edit/undo_last_edit tools for DeepSeek Harness (dsh). Every line gets a unique 3-char hash (A-Za-z0-9) that stays stable across edits; stale or ambiguous anchors are rejected, never fuzzy-matched. Undo persists across restarts.",
6
6
  "main": "lib/index.js",