-
-
Notifications
You must be signed in to change notification settings - Fork 610
Expand file tree
/
Copy pathstrings.ts
More file actions
21 lines (20 loc) · 608 Bytes
/
strings.ts
File metadata and controls
21 lines (20 loc) · 608 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { logError } from 'backend/logger'
/**
* Decodes a buffer to a UTF-8 string.
* Any leading continuation bytes are skipped to get a clean string start.
*
* @param buffer The buffer to decode
*/
export function decodeUTF8(buffer: Buffer): string {
try {
let skip = 0
// If bit 7 is set and bit 6 is cleared, this is a continuation byte.
while (skip < 4 && skip < buffer.length && (buffer[skip] & 0xc0) === 0x80) {
skip++
}
return buffer.subarray(skip).toString('utf-8')
} catch (error) {
logError(`Error decoding buffer as UTF-8: ${error}`)
throw error
}
}