import { useState } from 'react'; import { windowsUpdateScriptHash } from './script-console'; import { record, RecordValue, stringField } from './api'; /** Preserve wire bytes. Text decoding is a labeled display choice, never HTML or terminal interpretation. */ export function outputBytes(value: unknown): Uint8Array { if (!Array.isArray(value) || value.length > 262144 || value.some(byte => !Number.isInteger(byte) || byte < 0 || byte > 255)) throw new Error('Ungültige oder zu große Ausgabebytes'); return new Uint8Array(value); } type OutputEncoding = 'utf-8' | 'cp850' | 'powershell'; // Unicode mapping of bytes 0x80–0xff in IBM code page 850. ASCII bytes are unchanged. const cp850 = "ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜø£Ø×ƒáíóúñѪº¿®¬½¼¡«»░▒▓│┤ÁÂÀ©╣║╗╝¢¥┐└┴┬├─┼ãÃ╚╔╩╦╠═╬¤ðÐÊËÈıÍÎÏ┘┌█▄¦Ì▀ÓßÔÒõÕµþÞÚÛÙýݯ´­±‗¾¶§÷¸°¨·¹³²■ "; function cp850Text(raw: Uint8Array): string { return Array.from(raw, byte => byte < 128 ? String.fromCharCode(byte) : cp850[byte - 128]).join(''); } export function outputText(raw: Uint8Array, selected: OutputEncoding = 'utf-8'): { text: string; encoding: string } { let decoded: string, encoding: string; if (selected === 'cp850') { decoded = cp850Text(raw); encoding = 'Textdekodierung Windows OEM 850 (CP850)'; } else { try { decoded = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true }).decode(raw); encoding = 'UTF-8 lesbar; Quellencoding nicht separat belegt'; } catch { if (selected === 'powershell') { decoded = cp850Text(raw); encoding = 'Textdekodierung Windows OEM 850 (CP850), automatisch für native PowerShell-Ausgabe gewählt; Quellcodepage nicht separat belegt'; } else { decoded = new TextDecoder('utf-8', { ignoreBOM: true }).decode(raw); encoding = 'Encoding unbekannt / kein gültiges UTF-8: Ersatzzeichen nur in der Textansicht'; } } } return { text: visibleControls(decoded.replaceAll('\r\n', '\n')), encoding }; } export function visibleControls(text: string): string { return text.replace(/[\u0000-\u0008\u000b-\u001f\u007f-\u009f\u061c\u200e\u200f\u202a-\u202e\u2066-\u2069\ufeff]/g, char => `\\u${char.charCodeAt(0).toString(16).padStart(4, '0')}`); } function download(raw: Uint8Array, name: 'stdout' | 'stderr') { const url = URL.createObjectURL(new Blob([raw], { type: 'application/octet-stream' })); const link = document.createElement('a'); link.href = url; link.download = `${name}.bin`; link.click(); setTimeout(() => URL.revokeObjectURL(url), 1000); } function OutputChannel({ value, name, nativePowerShell }: { value: unknown; name: 'stdout' | 'stderr'; nativePowerShell: boolean }) { const [selected, setSelected] = useState<'auto' | 'utf-8' | 'cp850'>('auto'); try { const raw = outputBytes(value), view = outputText(raw, selected === 'auto' ? nativePowerShell ? 'powershell' : 'utf-8' : selected); return

{name === 'stdout' ? 'Standardausgabe' : 'Fehlerausgabe'}

{raw.length ? view.text : '(leere Ausgabe)'}
Zeichensatz und Download · {name}

{view.encoding}. Steuerzeichen erscheinen als {'\\uXXXX'}.

{name} Originalbytes (dezimal)
{JSON.stringify(Array.from(raw))}
; } catch (error) { return

{name}: {error instanceof Error ? error.message : 'Ausgabe nicht lesbar'}. Keine verlässliche Textansicht.

; } } export function ConsoleOutput({ job }: { job: RecordValue }) { const state = stringField(job, 'state'); return
{state === 'unknown' &&

Wirkung unklar. Ein weiterer Versuch benötigt einen Abgleich und einen neuen genehmigten Plan.

} {job.cancel_requested === true &&

Abbruch angefordert. Die Rückmeldung des Geräts steht aus.

} {job.pending_reason != null &&

Wartegrund: {String(job.pending_reason)}

} {job.result == null ?

Noch kein gespeichertes Ergebnis.

: }
Versuch und Datenstand

Zustand {state} · Versuch {stringField(job,'attempt_id')} · Revision {typeof job.revision === 'number' ? job.revision : 'unbekannt'}.

Die Ansicht zeigt die gespeicherte Ausgabe und aktualisiert den Stand alle vier Sekunden.

; } function Result({ value }: { value: unknown }) { try { const result = record(value, 'Jobergebnis'); const evidence = result.evidence && typeof result.evidence === 'object' && !Array.isArray(result.evidence) ? record(result.evidence) : {}; const nativePowerShell = evidence.interpreter === 'powershell' && evidence.output_encoding === 'raw_native_bytes'; const windowsUpdate = evidence.script_sha256 === windowsUpdateScriptHash; const reboot = windowsUpdate && Object.hasOwn(evidence, 'windows_update_reboot_required') ? evidence.windows_update_reboot_required : result.reboot_required; const exit = typeof result.exit_code === 'number' && Number.isInteger(result.exit_code) ? String(result.exit_code) : 'Nicht gemeldet'; if (result.output_restricted === true) return <>

Exitcode {exit}

Für diese Protokollausgabe fehlt dir die Leseberechtigung.

; return <>

Exitcode {exit}{typeof result.error_code === 'string' && result.error_code && ' · Fehlercode '+result.error_code}

{reboot === true &&

Ein Neustart ist erforderlich.

} {windowsUpdate && reboot == null &&

Neustartbedarf unbekannt.

} {windowsUpdate && reboot === false &&

Das Gerät meldet keinen Neustartbedarf.

} {result.output_truncated === true &&

Die gespeicherte Ausgabe ist nur ein Ausschnitt.

} {typeof result.output_truncated !== 'boolean' &&

Vollständigkeit ist nicht bestätigt.

} ; } catch { return

Unbekanntes Ergebnisformat. Die Originaldaten stehen unter „Technische Ausführungsdaten“.

; } }