feat(#1211): web package from the in-memory log, redacted

Web has no log files, so textPackageZip zips the log text the screen hands over, through the same redactor, with the manifest. Also from the Gemini review: log files are now redacted as a stream, line by line, and old packages are cleaned from their own temp folder instead of by listing the whole temp directory.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
docs/1232-region-scope-guide
diffwireauto 2 days ago committed by Strycher
parent 49b11e3ec5
commit cf9206478b

@ -54,13 +54,18 @@ class LogPackage {
for (final log in logFiles.reversed) {
if (!await log.exists()) continue;
final text = utf8.decode(await log.readAsBytes(), allowMalformed: true);
final redacted = const LineSplitter()
.convert(text)
.map(redactor.line)
.join('\n');
final out = File('${dir.path}$sep${_name(log)}');
await out.writeAsString('$redacted\n');
final sink = out.openWrite();
try {
await log
.openRead()
.transform(const Utf8Decoder(allowMalformed: true))
.transform(const LineSplitter())
.map(redactor.line)
.forEach(sink.writeln);
} finally {
await sink.close();
}
written.add(out);
}
if (traceFiles.isNotEmpty) {
@ -111,6 +116,28 @@ class LogPackage {
'files': files,
};
/// The web package (#1211): there are no log files on web, so the in-memory
/// log [text] goes in as one file, through the same redactor.
static Uint8List textPackageZip(
String text, {
MeshCoreConnector? radio,
DateTime Function() now = DateTime.now,
}) {
const logName = 'offband-log.txt';
final redactor = LogRedactor();
final redacted = const LineSplitter()
.convert(text)
.map(redactor.line)
.join('\n');
final manifest = const JsonEncoder.withIndent(' ').convert(
manifestFor(radio: radio, createdAt: now(), files: const [logName]),
);
final archive = Archive()
..addFile(ArchiveFile.string(manifestName, manifest))
..addFile(ArchiveFile.string(logName, '$redacted\n'));
return ZipEncoder().encodeBytes(archive);
}
/// Every file, at its path inside the package, in one zip.
Future<Uint8List> zip() async {
final archive = Archive();
@ -138,15 +165,16 @@ class LogPackage {
/// removed straight after sending.
static Future<Directory> _freshTempDir() async {
final temp = await getTemporaryDirectory();
await for (final old in temp.list()) {
if (old is Directory && _name(old).startsWith(_tempPrefix)) {
await old.delete(recursive: true).catchError((_) => old);
}
final parent = await Directory(
'${temp.path}${Platform.pathSeparator}$_tempParent',
).create();
await for (final old in parent.list()) {
await old.delete(recursive: true).catchError((_) => old);
}
return temp.createTemp(_tempPrefix);
return parent.createTemp('package-');
}
static const _tempPrefix = 'offband-logs-';
static const _tempParent = 'offband-log-packages';
static String _name(FileSystemEntity f) =>
f.uri.pathSegments.lastWhere((s) => s.isNotEmpty);

@ -85,6 +85,25 @@ void main() {
expect(r.line(redacted), redacted);
});
test('the web package zips the in-memory log, redacted', () {
final text = [
_frameLine('TX', 'CMD_SEND_LOGIN', buildSendLoginFrame(_peer, 'hunter2')),
'[12:00:00.000] [INFO] [App] connected',
].join('\n');
final zip = ZipDecoder().decodeBytes(LogPackage.textPackageZip(text));
expect(zip.files.map((f) => f.name), [
LogPackage.manifestName,
'offband-log.txt',
]);
final log = utf8.decode(zip.files[1].content as List<int>);
expect(log, isNot(contains(_hex(latin1.encode('hunter2')))));
expect(log, contains('[App] connected'));
final manifest =
jsonDecode(utf8.decode(zip.files[0].content as List<int>))
as Map<String, dynamic>;
expect(manifest['files'], ['offband-log.txt']);
});
test('the manifest names the files, and the zip holds them all', () async {
final log = await write('offband.log', ['one']);
final trace = await write('anr-1.txt', ['trace']);

Loading…
Cancel
Save

Powered by TurnKey Linux.