append static method
Records payloadJson, the exact bytes sent or about to be sent; an exclusive file lock keeps concurrent servers from forking the chain or losing lines. Throws on filesystem failure.
Implementation
static AuditEntry append(String dataDir, String payloadJson) {
final file = io.File(_filePath(dataDir));
if (!file.parent.existsSync()) file.parent.createSync(recursive: true);
final raf = file.openSync(mode: io.FileMode.append);
try {
raf.lockSync(io.FileLock.blockingExclusive);
final prevHash = _lastHash(raf) ?? _zeroHash;
final ts = DateTime.now().toUtc().toIso8601String();
final payloadB64 = base64.encode(utf8.encode(payloadJson));
final preimage = '$ts|$prevHash|$payloadB64';
final thisHash = sha256.convert(utf8.encode(preimage)).toString();
// FileMode.append positions at open time, not per write; another server may have appended while this one waited for the lock.
raf.setPositionSync(raf.lengthSync());
raf.writeStringSync('$preimage|$thisHash\n');
raf.flushSync();
return AuditEntry(
ts: DateTime.parse(ts),
prevHash: prevHash,
payloadB64: payloadB64,
thisHash: thisHash,
);
} finally {
raf.closeSync();
}
}