97 lines
7.1 KiB
TypeScript
97 lines
7.1 KiB
TypeScript
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<ArrayBuffer> {
|
||
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<ArrayBuffer>): string {
|
||
return Array.from(raw, byte => byte < 128 ? String.fromCharCode(byte) : cp850[byte - 128]).join('');
|
||
}
|
||
export function outputText(raw: Uint8Array<ArrayBuffer>, 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<ArrayBuffer>, 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 <div><h4>{name === 'stdout' ? 'Standardausgabe' : 'Fehlerausgabe'}</h4>
|
||
<pre className="console-output" aria-label={name + ' Textansicht'}>{raw.length ? view.text : '(leere Ausgabe)'}</pre>
|
||
<details><summary>Zeichensatz und Download · {name}</summary>
|
||
<label>{name} Textdekodierung<select aria-label={name + ' Textdekodierung'} value={selected} onChange={event => setSelected(event.target.value as typeof selected)}>
|
||
<option value="auto">{nativePowerShell ? 'Automatisch · UTF-8, sonst CP850' : 'Automatisch · UTF-8'}</option>
|
||
<option value="utf-8">UTF-8</option><option value="cp850">Windows OEM 850 (CP850)</option>
|
||
</select></label>
|
||
<p className="muted">{view.encoding}. Steuerzeichen erscheinen als {'\\uXXXX'}.</p>
|
||
<button className="quiet" type="button" onClick={() => download(raw, name)}>{name} als unveränderte Rohbytes herunterladen</button>
|
||
<details><summary>{name} Originalbytes (dezimal)</summary><pre>{JSON.stringify(Array.from(raw))}</pre></details>
|
||
</details>
|
||
</div>;
|
||
} catch (error) { return <p role="alert" className="error">{name}: {error instanceof Error ? error.message : 'Ausgabe nicht lesbar'}. Keine verlässliche Textansicht.</p>; }
|
||
}
|
||
export function ConsoleOutput({ job }: { job: RecordValue }) {
|
||
const state = stringField(job, 'state');
|
||
return <section aria-label="Gespeichertes Konsolenergebnis">
|
||
{state === 'unknown' && <p className="warning">Wirkung unklar. Ein weiterer Versuch benötigt einen Abgleich und einen neuen genehmigten Plan.</p>}
|
||
{job.cancel_requested === true && <p className="warning">Abbruch angefordert. Die Rückmeldung des Geräts steht aus.</p>}
|
||
{job.pending_reason != null && <p>Wartegrund: {String(job.pending_reason)}</p>}
|
||
{job.result == null ? <p>Noch kein gespeichertes Ergebnis.</p> : <Result key={stringField(job,'job_id')+'/'+stringField(job,'attempt_id')} value={job.result}/>}
|
||
<details><summary>Versuch und Datenstand</summary>
|
||
<p>Zustand {state} · Versuch {stringField(job,'attempt_id')} · Revision {typeof job.revision === 'number' ? job.revision : 'unbekannt'}.</p>
|
||
<p>Die Ansicht zeigt die gespeicherte Ausgabe und aktualisiert den Stand alle vier Sekunden.</p>
|
||
</details>
|
||
</section>;
|
||
}
|
||
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 <>
|
||
<p><strong>Exitcode {exit}</strong></p>
|
||
<p>Für diese Protokollausgabe fehlt dir die Leseberechtigung.</p>
|
||
</>;
|
||
return <>
|
||
<p><strong>Exitcode {exit}</strong>{typeof result.error_code === 'string' && result.error_code && ' · Fehlercode '+result.error_code}</p>
|
||
{reboot === true && <p className="warning">Ein Neustart ist erforderlich.</p>}
|
||
{windowsUpdate && reboot == null && <p className="warning">Neustartbedarf unbekannt.</p>}
|
||
{windowsUpdate && reboot === false && <p>Das Gerät meldet keinen Neustartbedarf.</p>}
|
||
{result.output_truncated === true && <p className="warning">Die gespeicherte Ausgabe ist nur ein Ausschnitt.</p>}
|
||
{typeof result.output_truncated !== 'boolean' && <p className="muted">Vollständigkeit ist nicht bestätigt.</p>}
|
||
<OutputChannel name="stdout" value={result.stdout} nativePowerShell={nativePowerShell}/>
|
||
<OutputChannel name="stderr" value={result.stderr} nativePowerShell={nativePowerShell}/>
|
||
</>;
|
||
} catch { return <p className="error" role="alert">Unbekanntes Ergebnisformat. Die Originaldaten stehen unter „Technische Ausführungsdaten“.</p>; }
|
||
}
|