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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//! publish zat's docs to its ATProto repo as `site.standard` records.
//!
//! a small but complete showcase of the library: resolve the account's PDS
//! from its DID document, authenticate, upsert the current document set with
//! stable record keys, and prune anything left over — all through zat's checked
//! XRPC API, so protocol errors arrive as typed `XrpcError`s, not opaque bodies.
const std = @import("std");
const zat = @import("zat");
const Allocator = std.mem.Allocator;
const handle = "zat.dev";
const publication_collection = "site.standard.publication";
const document_collection = "site.standard.document";
/// a document to publish: its record key, site path, and source file.
const DocEntry = struct { rkey: []const u8, path: []const u8, file: []const u8 };
/// top-level docs, with stable record keys derived from their site path.
const docs = [_]DocEntry{
.{ .rkey = "home", .path = "/", .file = "README.md" },
.{ .rkey = "changelog", .path = "/changelog", .file = "CHANGELOG.md" },
};
const Publication = struct {
@"$type": []const u8 = publication_collection,
url: []const u8,
name: []const u8,
description: ?[]const u8 = null,
};
const Document = struct {
@"$type": []const u8 = document_collection,
site: []const u8,
title: []const u8,
path: ?[]const u8 = null,
textContent: ?[]const u8 = null,
publishedAt: []const u8,
};
pub fn main(init: std.process.Init) !void {
// short-lived CLI: an arena frees everything at exit, so the body can focus
// on the protocol rather than on lifetimes.
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
const io = init.io;
const password = init.environ_map.get("ATPROTO_PASSWORD") orelse {
std.debug.print("error: ATPROTO_PASSWORD not set\n", .{});
return error.MissingEnv;
};
// resolve the account's current PDS from its DID document so a future PDS
// migration can't point this at a stale host. ATPROTO_PDS overrides.
const pds = if (init.environ_map.get("ATPROTO_PDS")) |p|
p
else
try resolvePds(allocator, io, handle);
std.debug.print("publishing to pds {s}\n", .{pds});
var client = zat.XrpcClient.init(io, allocator, pds);
defer client.deinit();
const did = try login(&client, allocator, handle, password);
std.debug.print("authenticated as {s}\n", .{did});
// the two publications documents are grouped under, with stable rkeys.
const site_uri = try std.fmt.allocPrint(allocator, "at://{s}/{s}/site", .{ did, publication_collection });
const devlog_uri = try std.fmt.allocPrint(allocator, "at://{s}/{s}/devlog", .{ did, publication_collection });
const published_at = try nowIso8601(allocator, io);
// upsert publications, then every document, tracking the keys we write so
// we can prune anything stale afterward. putRecord is an upsert, so re-runs
// are idempotent and writing-before-pruning never leaves the repo empty.
try putRecord(&client, allocator, did, publication_collection, "site", Publication{
.url = "https://zat.dev",
.name = "zat",
.description = "AT Protocol building blocks for zig",
});
try putRecord(&client, allocator, did, publication_collection, "devlog", Publication{
// root URL, not the #devlog/index hash route: indexers derive doc
// links as url-host + document path, and a fragment in the base
// produces zat.dev/#devlog/index/devlog/NNN (a broken SPA route)
.url = "https://zat.dev",
.name = "zat devlog",
.description = "building zat in public",
});
var doc_keys: std.ArrayList([]const u8) = .empty;
for (&docs) |entry| {
if (try publishDocument(&client, allocator, io, did, entry, site_uri, published_at))
try doc_keys.append(allocator, entry.rkey);
}
for (try discoverDevlog(allocator, io)) |entry| {
if (try publishDocument(&client, allocator, io, did, entry, devlog_uri, published_at))
try doc_keys.append(allocator, entry.rkey);
}
std.debug.print("published {d} document(s)\n", .{doc_keys.items.len});
// remove records we no longer publish — including any under an older key
// scheme — so the repo matches exactly what we just wrote.
try pruneStale(&client, allocator, did, publication_collection, &.{ "site", "devlog" });
try pruneStale(&client, allocator, did, document_collection, doc_keys.items);
}
/// resolve the PDS service endpoint for a handle via its DID document.
fn resolvePds(allocator: Allocator, io: std.Io, handle_str: []const u8) ![]const u8 {
const parsed_handle = zat.Handle.parse(handle_str) orelse return error.InvalidHandle;
var handle_resolver = zat.HandleResolver.init(io, allocator);
defer handle_resolver.deinit();
const did_str = try handle_resolver.resolve(parsed_handle);
const did = zat.Did.parse(did_str) orelse return error.InvalidDid;
var did_resolver = zat.DidResolver.init(io, allocator);
defer did_resolver.deinit();
var doc = try did_resolver.resolve(did);
defer doc.deinit();
const pds = doc.pdsEndpoint() orelse return error.NoPdsEndpoint;
return allocator.dupe(u8, pds);
}
/// authenticate with an app password; returns the account DID and sets auth.
fn login(client: *zat.XrpcClient, allocator: Allocator, identifier: []const u8, password: []const u8) ![]const u8 {
const body = try std.fmt.allocPrint(allocator, "{f}", .{std.json.fmt(.{
.identifier = identifier,
.password = password,
}, .{})});
const nsid = zat.Nsid.parse("com.atproto.server.createSession").?;
var result = try client.procedureChecked(nsid, body, .{});
defer result.deinit();
switch (result) {
.err => |e| return reportXrpcError("createSession", e),
.ok => |resp| {
var parsed = try resp.json();
defer parsed.deinit();
const did = zat.json.getString(parsed.value, "did") orelse return error.MissingDid;
const token = zat.json.getString(parsed.value, "accessJwt") orelse return error.MissingToken;
client.setAuth(try allocator.dupe(u8, token));
return allocator.dupe(u8, did);
},
}
}
/// read a document's source, extract its title, and upsert it. returns false
/// (and warns) if the source can't be read, so a missing file isn't fatal.
fn publishDocument(
client: *zat.XrpcClient,
allocator: Allocator,
io: std.Io,
did: []const u8,
entry: DocEntry,
site_uri: []const u8,
published_at: []const u8,
) !bool {
const content = std.Io.Dir.cwd().readFileAlloc(io, entry.file, allocator, .limited(1024 * 1024)) catch |err| {
std.debug.print("warning: could not read {s}: {}\n", .{ entry.file, err });
return false;
};
try putRecord(client, allocator, did, document_collection, entry.rkey, Document{
.site = site_uri,
.title = extractTitle(content) orelse entry.file,
.path = entry.path,
.textContent = content,
.publishedAt = published_at,
});
return true;
}
/// discover devlog entries from the `devlog/` dir so the published records stay
/// in sync with the site builder — a hard-coded list silently drifts.
fn discoverDevlog(allocator: Allocator, io: std.Io) ![]DocEntry {
var dir = try std.Io.Dir.cwd().openDir(io, "devlog", .{ .iterate = true });
defer dir.close(io);
var names: std.ArrayList([]const u8) = .empty;
var it = dir.iterate();
while (try it.next(io)) |entry| {
if (entry.kind != .file) continue;
if (!std.mem.endsWith(u8, entry.name, ".md")) continue;
if (entry.name.len == 0 or !std.ascii.isDigit(entry.name[0])) continue;
try names.append(allocator, try allocator.dupe(u8, entry.name));
}
// sort by filename so the `NNN-` numeric prefix orders chronologically
std.mem.sort([]const u8, names.items, {}, struct {
fn lessThan(_: void, a: []const u8, b: []const u8) bool {
return std.mem.lessThan(u8, a, b);
}
}.lessThan);
var entries: std.ArrayList(DocEntry) = .empty;
for (names.items) |name| {
const dash = std.mem.indexOfScalar(u8, name, '-') orelse continue;
const num = name[0..dash];
try entries.append(allocator, .{
.rkey = try std.fmt.allocPrint(allocator, "devlog-{s}", .{num}),
.path = try std.fmt.allocPrint(allocator, "/devlog/{s}", .{num}),
.file = try std.fmt.allocPrint(allocator, "devlog/{s}", .{name}),
});
}
return entries.toOwnedSlice(allocator);
}
/// create-or-replace a record. omits null optionals so records carry absent
/// fields, not explicit nulls, which ATProto lexicon validation distinguishes.
fn putRecord(client: *zat.XrpcClient, allocator: Allocator, repo: []const u8, collection: []const u8, rkey: []const u8, value: anytype) !void {
const record = try std.fmt.allocPrint(allocator, "{f}", .{std.json.fmt(value, .{ .emit_null_optional_fields = false })});
const body = try std.fmt.allocPrint(
allocator,
"{{\"repo\":\"{s}\",\"collection\":\"{s}\",\"rkey\":\"{s}\",\"record\":{s}}}",
.{ repo, collection, rkey, record },
);
const nsid = zat.Nsid.parse("com.atproto.repo.putRecord").?;
var result = try client.procedureChecked(nsid, body, .{});
defer result.deinit();
switch (result) {
.ok => {},
.err => |e| return reportXrpcError("putRecord", e),
}
}
/// delete every record in `collection` whose rkey is not in `keep`.
fn pruneStale(client: *zat.XrpcClient, allocator: Allocator, repo: []const u8, collection: []const u8, keep: []const []const u8) !void {
var params = std.StringHashMap([]const u8).init(allocator);
defer params.deinit();
try params.put("repo", repo);
try params.put("collection", collection);
try params.put("limit", "100");
const list_nsid = zat.Nsid.parse("com.atproto.repo.listRecords").?;
var result = try client.queryChecked(list_nsid, params, .{});
defer result.deinit();
const resp = switch (result) {
.err => |e| return reportXrpcError("listRecords", e),
.ok => |r| r,
};
var parsed = try resp.json();
defer parsed.deinit();
const records = zat.json.getArray(parsed.value, "records") orelse return;
for (records) |record| {
const uri = zat.json.getString(record, "uri") orelse continue;
const rkey = (zat.AtUri.parse(uri) orelse continue).rkey() orelse continue;
if (contains(keep, rkey)) continue;
try deleteRecord(client, allocator, repo, collection, rkey);
std.debug.print("pruned stale {s}/{s}\n", .{ collection, rkey });
}
}
fn deleteRecord(client: *zat.XrpcClient, allocator: Allocator, repo: []const u8, collection: []const u8, rkey: []const u8) !void {
const body = try std.fmt.allocPrint(
allocator,
"{{\"repo\":\"{s}\",\"collection\":\"{s}\",\"rkey\":\"{s}\"}}",
.{ repo, collection, rkey },
);
const nsid = zat.Nsid.parse("com.atproto.repo.deleteRecord").?;
var result = try client.procedureChecked(nsid, body, .{});
defer result.deinit();
switch (result) {
.ok => {},
.err => |e| return reportXrpcError("deleteRecord", e),
}
}
fn contains(haystack: []const []const u8, needle: []const u8) bool {
for (haystack) |s| if (std.mem.eql(u8, s, needle)) return true;
return false;
}
/// log a typed XRPC error and surface it as a Zig error.
fn reportXrpcError(op: []const u8, e: zat.XrpcClient.XrpcError) error{XrpcCallFailed} {
std.debug.print("{s} failed: {d} {s}: {s}\n", .{
op,
@backingInt(e.status),
e.error_name orelse "(no error code)",
e.message orelse e.body,
});
return error.XrpcCallFailed;
}
fn extractTitle(content: []const u8) ?[]const u8 {
var lines = std.mem.splitScalar(u8, content, '\n');
while (lines.next()) |line| {
const trimmed = std.mem.trim(u8, line, " \t\r");
if (trimmed.len > 2 and trimmed[0] == '#' and trimmed[1] == ' ') {
var title = trimmed[2..];
// strip markdown link: [text](url) -> text
if (std.mem.indexOf(u8, title, "](")) |bracket| {
if (title[0] == '[') title = title[1..bracket];
}
return title;
}
}
return null;
}
/// current time as an RFC-3339 / ISO-8601 UTC timestamp for `publishedAt`.
fn nowIso8601(allocator: Allocator, io: std.Io) ![]const u8 {
const ns = std.Io.Timestamp.now(io, .real).nanoseconds;
const secs: u64 = @intCast(@divFloor(ns, std.time.ns_per_s));
const es = std.time.epoch.EpochSeconds{ .secs = secs };
const yd = es.getEpochDay().calculateYearDay();
const md = yd.calculateMonthDay();
const ds = es.getDaySeconds();
return std.fmt.allocPrint(allocator, "{d:0>4}-{d:0>2}-{d:0>2}T{d:0>2}:{d:0>2}:{d:0>2}Z", .{
yd.year,
@as(u32, md.month.numeric()),
@as(u32, md.day_index) + 1,
@as(u32, ds.getHoursIntoDay()),
@as(u32, ds.getMinutesIntoHour()),
@as(u32, ds.getSecondsIntoMinute()),
});
}