~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zat

atproto utils for zig · mirror of https://tangled.org/zat.dev/zat

0
0
97 lines · 3.9 KB Raw History
# write a STAR archive

write a STAR archive, read it back, and see a truncated one refused. runs offline.

```zig
const std = @import("std");
const zat = @import("zat");

pub fn main(init: std.process.Init) !void {
    const allocator = init.gpa;

    // a STAR archive is a metadata map, then key/value pairs, then an end byte
    var out: std.Io.Writer.Allocating = .init(allocator);
    defer out.deinit();
    const metadata: zat.cbor.Value = .{ .map = &.{
        .{ .key = "$type", .value = .{ .text = "com.example.greetings" } },
    } };
    // ordered_keys refuses a key that doesn't sort after the one before it,
    // which repository archives require
    const options: zat.star.Writer.Options = .{ .ordered_keys = true };
    var writer = try zat.star.Writer.init(allocator, &out.writer, metadata, options);
    defer writer.deinit();
    try writer.writePair("greeting/en", "hello");
    try writer.writePair("greeting/es", "hola");
    try writer.writePair("greeting/fr", "bonjour");
    try writer.finish(); // writes the end byte
    const archive = out.written();
    std.debug.print("wrote {d} bytes\n", .{archive.len});

    // read it back: the metadata first, then each pair in order
    var input: std.Io.Reader = .fixed(archive);
    var reader = try zat.star.Reader.init(allocator, &input, .{});
    defer reader.deinit();
    std.debug.print("$type: {s}\n", .{reader.metadata.getString("$type").?});
    var value: [64]u8 = undefined;
    while (try reader.next()) |entry| {
        if (entry.value_len > value.len) return error.ValueTooLong;
        const n = try reader.readValue(value[0..@intCast(entry.value_len)]);
        std.debug.print("{s} = {s}\n", .{ entry.key, value[0..n] });
    }

    // drop the last byte: the archive is now cut off, and reading it fails
    // instead of quietly returning fewer pairs
    var cut: std.Io.Reader = .fixed(archive[0 .. archive.len - 1]);
    var truncated = try zat.star.Reader.init(allocator, &cut, .{});
    defer truncated.deinit();
    var pairs: usize = 0;
    while (truncated.next() catch |err| {
        std.debug.print("truncated archive: error.{t} after {d} pairs\n", .{ err, pairs });
        return;
    }) |entry| {
        _ = try truncated.readValue(value[0..@intCast(entry.value_len)]);
        pairs += 1;
    }
    return error.TruncationNotDetected;
}
```

```
wrote 93 bytes
$type: com.example.greetings
greeting/en = hello
greeting/es = hola
greeting/fr = bonjour
truncated archive: error.EndOfStream after 3 pairs
```

This is `examples/08_write_a_star_archive.zig` verbatim — copy it and `zig build example-write-a-star-archive`.

[STAR](https://dasl.ing/star.html) is a streaming archive: a magic, a metadata
map naming the archive's `$type`, then key/value pairs, then a single end byte.
Keys and values are plain bytes, and a reader handles one pair at a time, so an
archive can be far larger than memory.

The end byte is the point of the last block above. A truncated archive reads
every pair it still has and then fails, so a cut-off download can't pass for a
complete one.

atproto uses STAR to move repositories: repository exports, account migration,
and permissioned spaces, where [zds](https://tangled.org/zat.dev/zds) serves a
space repo as a STAR archive whose metadata is the repo's commit and whose pairs
are its records. For a public repository archive, `zat.star.PublicRepoReader`
rebuilds the MST from the pairs and checks it against the commit's root:
`zig build example-verify-star -- repository.star`.

**See also:** the [STAR spec](https://dasl.ing/star.html) and Bryan Newbold's [cobalt](https://tangled.org/bnewbold.net/cobalt), whose interop fixtures zat's reader and writer pass.

<details>
<summary>wiring zat into your own <code>build.zig</code></summary>

```zig
const zat = b.dependency("zat", .{}).module("zat");
exe.root_module.addImport("zat", zat);
```

after `zig fetch --save https://tangled.org/zat.dev/zat/archive/main`.
</details>