tftsr-devops_investigation/node_modules/text-decoder/lib/utf8-decoder.js
Shaun Arman 8839075805 feat: initial implementation of TFTSR IT Triage & RCA application
Implements Phases 1-8 of the TFTSR implementation plan.

Rust backend (Tauri 2.x, src-tauri/):
- Multi-provider AI: OpenAI-compatible, Anthropic, Gemini, Mistral, Ollama
- PII detection engine: 11 regex patterns with overlap resolution
- SQLCipher AES-256 encrypted database with 10 versioned migrations
- 28 Tauri IPC commands for triage, analysis, document, and system ops
- Ollama: hardware probe, model recommendations, pull/delete with events
- RCA and blameless post-mortem Markdown document generators
- PDF export via printpdf
- Audit log: SHA-256 hash of every external data send
- Integration stubs for Confluence, ServiceNow, Azure DevOps (v0.2)

Frontend (React 18 + TypeScript + Vite, src/):
- 9 pages: full triage workflow NewIssue→LogUpload→Triage→Resolution→RCA→Postmortem→History+Settings
- 7 components: ChatWindow, TriageProgress, PiiDiffViewer, DocEditor, HardwareReport, ModelSelector, UI primitives
- 3 Zustand stores: session, settings (persisted), history
- Type-safe tauriCommands.ts matching Rust backend types exactly
- 8 IT domain system prompts (Linux, Windows, Network, K8s, DB, Virt, HW, Obs)

DevOps:
- .woodpecker/test.yml: rustfmt, clippy, cargo test, tsc, vitest on every push
- .woodpecker/release.yml: linux/amd64 + linux/arm64 builds, Gogs release upload

Verified:
- cargo check: zero errors
- tsc --noEmit: zero errors
- vitest run: 13/13 unit tests passing

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
2026-03-14 22:36:25 -05:00

186 lines
4.3 KiB
JavaScript

const b4a = require('b4a')
/**
* https://encoding.spec.whatwg.org/#utf-8-decoder
*/
module.exports = class UTF8Decoder {
constructor() {
this._reset()
}
get remaining() {
return this.bytesSeen
}
decode(data) {
if (data.byteLength === 0) return ''
if (this.bytesNeeded === 0 && trailingIncomplete(data, 0) === 0) {
this.bytesSeen = trailingBytesSeen(data)
return b4a.toString(data, 'utf8')
}
let result = ''
let start = 0
if (this.bytesNeeded > 0) {
while (start < data.byteLength) {
const byte = data[start]
if (byte < this.lowerBoundary || byte > this.upperBoundary) {
result += '\ufffd'
this._reset()
break
}
this.lowerBoundary = 0x80
this.upperBoundary = 0xbf
this.codePoint = (this.codePoint << 6) | (byte & 0x3f)
this.bytesSeen++
start++
if (this.bytesSeen === this.bytesNeeded) {
result += String.fromCodePoint(this.codePoint)
this._reset()
break
}
}
if (this.bytesNeeded > 0) return result
}
const trailing = trailingIncomplete(data, start)
const end = data.byteLength - trailing
if (end > start) result += b4a.toString(data, 'utf8', start, end)
for (let i = end; i < data.byteLength; i++) {
const byte = data[i]
if (this.bytesNeeded === 0) {
if (byte <= 0x7f) {
this.bytesSeen = 0
result += String.fromCharCode(byte)
} else if (byte >= 0xc2 && byte <= 0xdf) {
this.bytesNeeded = 2
this.bytesSeen = 1
this.codePoint = byte & 0x1f
} else if (byte >= 0xe0 && byte <= 0xef) {
if (byte === 0xe0) this.lowerBoundary = 0xa0
else if (byte === 0xed) this.upperBoundary = 0x9f
this.bytesNeeded = 3
this.bytesSeen = 1
this.codePoint = byte & 0xf
} else if (byte >= 0xf0 && byte <= 0xf4) {
if (byte === 0xf0) this.lowerBoundary = 0x90
else if (byte === 0xf4) this.upperBoundary = 0x8f
this.bytesNeeded = 4
this.bytesSeen = 1
this.codePoint = byte & 0x7
} else {
this.bytesSeen = 1
result += '\ufffd'
}
continue
}
if (byte < this.lowerBoundary || byte > this.upperBoundary) {
result += '\ufffd'
i--
this._reset()
continue
}
this.lowerBoundary = 0x80
this.upperBoundary = 0xbf
this.codePoint = (this.codePoint << 6) | (byte & 0x3f)
this.bytesSeen++
if (this.bytesSeen === this.bytesNeeded) {
result += String.fromCodePoint(this.codePoint)
this._reset()
}
}
return result
}
flush() {
const result = this.bytesNeeded > 0 ? '\ufffd' : ''
this._reset()
return result
}
_reset() {
this.codePoint = 0
this.bytesNeeded = 0
this.bytesSeen = 0
this.lowerBoundary = 0x80
this.upperBoundary = 0xbf
}
}
function trailingIncomplete(data, start) {
const len = data.byteLength
if (len <= start) return 0
const limit = Math.max(start, len - 4)
let i = len - 1
while (i > limit && (data[i] & 0xc0) === 0x80) i--
if (i < start) return 0
const byte = data[i]
let needed
if (byte <= 0x7f) return 0
if (byte >= 0xc2 && byte <= 0xdf) needed = 2
else if (byte >= 0xe0 && byte <= 0xef) needed = 3
else if (byte >= 0xf0 && byte <= 0xf4) needed = 4
else return 0
const available = len - i
return available < needed ? available : 0
}
function trailingBytesSeen(data) {
const len = data.byteLength
if (len === 0) return 0
const last = data[len - 1]
if (last <= 0x7f) return 0
if ((last & 0xc0) !== 0x80) return 1
const limit = Math.max(0, len - 4)
let i = len - 2
while (i >= limit && (data[i] & 0xc0) === 0x80) i--
if (i < 0) return 1
const first = data[i]
let needed
if (first >= 0xc2 && first <= 0xdf) needed = 2
else if (first >= 0xe0 && first <= 0xef) needed = 3
else if (first >= 0xf0 && first <= 0xf4) needed = 4
else return 1
if (len - i !== needed) return 1
if (needed >= 3) {
const second = data[i + 1]
if (first === 0xe0 && second < 0xa0) return 1
if (first === 0xed && second > 0x9f) return 1
if (first === 0xf0 && second < 0x90) return 1
if (first === 0xf4 && second > 0x8f) return 1
}
return 0
}