MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / detectEncodingForResolvedPath

Function detectEncodingForResolvedPath

source code/utils/fileRead.ts:22–51  ·  view source on GitHub ↗
(
  resolvedPath: string,
)

Source from the content-addressed store, hash-verified

20export type LineEndingType = 'CRLF' | 'LF'
21
22export function detectEncodingForResolvedPath(
23 resolvedPath: string,
24): BufferEncoding {
25 const { buffer, bytesRead } = getFsImplementation().readSync(resolvedPath, {
26 length: 4096,
27 })
28
29 // Empty files should default to utf8, not ascii
30 // This fixes a bug where writing emojis/CJK to empty files caused corruption
31 if (bytesRead === 0) {
32 return 'utf8'
33 }
34
35 if (bytesRead >= 2) {
36 if (buffer[0] === 0xff && buffer[1] === 0xfe) return 'utf16le'
37 }
38
39 if (
40 bytesRead >= 3 &&
41 buffer[0] === 0xef &&
42 buffer[1] === 0xbb &&
43 buffer[2] === 0xbf
44 ) {
45 return 'utf8'
46 }
47
48 // For non-empty files, default to utf8 since it's a superset of ascii
49 // and handles all Unicode characters properly
50 return 'utf8'
51}
52
53export function detectLineEndingsForString(content: string): LineEndingType {
54 let crlfCount = 0

Callers 3

SedEditPermissionRequestFunction · 0.85
detectFileEncodingFunction · 0.85
readFileSyncWithMetadataFunction · 0.85

Calls 1

getFsImplementationFunction · 0.85

Tested by

no test coverage detected