1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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>