From cf9206478b0dd085c2901e972cbb556c7edf68c9 Mon Sep 17 00:00:00 2001 From: diffwireauto <330573775+diffwireauto@users.noreply.github.com> Date: Wed, 25 Nov 2026 23:57:03 -0500 Subject: [PATCH] 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 --- lib/services/log_package.dart | 52 ++++++++++++++++++++++------- test/services/log_package_test.dart | 19 +++++++++++ 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/lib/services/log_package.dart b/lib/services/log_package.dart index 72a7afc8..460c0756 100644 --- a/lib/services/log_package.dart +++ b/lib/services/log_package.dart @@ -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 zip() async { final archive = Archive(); @@ -138,15 +165,16 @@ class LogPackage { /// removed straight after sending. static Future _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); diff --git a/test/services/log_package_test.dart b/test/services/log_package_test.dart index 4b291422..1c82cf8b 100644 --- a/test/services/log_package_test.dart +++ b/test/services/log_package_test.dart @@ -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); + expect(log, isNot(contains(_hex(latin1.encode('hunter2'))))); + expect(log, contains('[App] connected')); + final manifest = + jsonDecode(utf8.decode(zip.files[0].content as List)) + as Map; + 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']);