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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
//! jetstream wire formats — JSON event encoding
//!
//! v1: one bare JSON object per websocket text frame (legacy-compatible; the
//! envelope carries "cursor" when the local seq is known, matching atmos's
//! JetstreamEvent). v2: network.bsky.jetstream.subscribeEvents — one
//! xrpc.v1.json message frame per event (atproto proposal 0015), payload
//! shapes and key order pinned to upstream's lexgen output (alphabetical).
//! record CBOR is rendered in the atproto data model's JSON representation
//! ({"$link": ...} for CIDs, {"$bytes": ...} for byte strings); the v2 wire
//! carries no duplicate base64 CBOR (upstream #327).
const std = @import("std");
const zat = @import("zat");
const cbor = zat.cbor;
const multibase = zat.multibase;
const Allocator = std.mem.Allocator;
pub const Kind = enum { commit, identity, account, sync };
/// Write a byte slice as a JSON *string*, always.
///
/// `Stringify.write` renders a []const u8 whose contents are not valid UTF-8
/// as an ARRAY OF NUMBERS (std/json/Stringify.zig: "If the content is not
/// valid UTF-8, rendered as an array of numbers instead"). A single bad byte
/// in a did/collection/rkey therefore changes the type of a wire field and
/// breaks every consumer parsing it — observed 2026-08-09 as a subscriber
/// receiving `did` as a list after fault injection tore a row.
///
/// Go's encoding/json substitutes U+FFFD instead, so upstream jetstream can
/// never emit this shape; match that. The valid case is the fast path and
/// keeps byte-for-byte parity.
fn writeString(s: *std.json.Stringify, bytes: []const u8) !void {
if (std.unicode.utf8ValidateSlice(bytes)) return s.write(bytes);
try s.beginWriteRaw();
const w = s.writer;
try w.writeByte('"');
var i: usize = 0;
while (i < bytes.len) {
const len = std.unicode.utf8ByteSequenceLength(bytes[i]) catch {
try w.writeAll("\u{FFFD}");
i += 1;
continue;
};
if (i + len > bytes.len or !std.unicode.utf8ValidateSlice(bytes[i..][0..len])) {
try w.writeAll("\u{FFFD}");
i += 1;
continue;
}
try std.json.Stringify.encodeJsonStringChars(bytes[i..][0..len], .{}, w);
i += len;
}
try w.writeByte('"');
s.endWriteRaw();
}
pub const CommitOp = struct {
did: []const u8,
time_us: i64,
/// local archive seq; 0 = unknown (the envelope omits "cursor", matching
/// atmos's omitempty so legacy frames never gain a misleading cursor:0)
seq: u64 = 0,
rev: []const u8,
operation: []const u8, // "create" | "update" | "delete"
collection: []const u8,
rkey: []const u8,
record: ?cbor.Value = null, // null for deletes
cid: ?cbor.Cid = null, // null for deletes
};
/// encode a commit-op event as a complete v1 JSON frame into `w`.
pub fn encodeCommit(allocator: Allocator, w: *std.Io.Writer, op: CommitOp) !void {
var s: std.json.Stringify = .{ .writer = w };
try s.beginObject();
try s.objectField("did");
try writeString(&s, op.did);
try s.objectField("time_us");
try s.write(op.time_us);
if (op.seq != 0) {
try s.objectField("cursor");
try s.write(op.seq);
}
try s.objectField("kind");
try s.write("commit");
try s.objectField("commit");
try s.beginObject();
try s.objectField("rev");
try writeString(&s, op.rev);
try s.objectField("operation");
try s.write(op.operation);
try s.objectField("collection");
try writeString(&s, op.collection);
try s.objectField("rkey");
try writeString(&s, op.rkey);
if (op.record) |record| {
try s.objectField("record");
try writeCborValue(&s, record);
}
if (op.cid) |cid| {
try s.objectField("cid");
const cid_str = try cidString(allocator, cid);
defer allocator.free(cid_str);
try s.write(cid_str);
}
try s.endObject();
try s.endObject();
}
/// encode an identity event. `seq` and `time` come from the upstream frame.
pub fn encodeIdentity(w: *std.Io.Writer, args: struct {
did: []const u8,
time_us: i64,
local_seq: u64 = 0,
seq: i64,
time: []const u8,
handle: ?[]const u8,
}) !void {
var s: std.json.Stringify = .{ .writer = w };
try s.beginObject();
try s.objectField("did");
try writeString(&s, args.did);
try s.objectField("time_us");
try s.write(args.time_us);
if (args.local_seq != 0) {
try s.objectField("cursor");
try s.write(args.local_seq);
}
try s.objectField("kind");
try s.write("identity");
try s.objectField("identity");
try s.beginObject();
try s.objectField("did");
try writeString(&s, args.did);
if (args.handle) |h| {
try s.objectField("handle");
try writeString(&s, h);
}
try s.objectField("seq");
try s.write(args.seq);
try s.objectField("time");
try s.write(args.time);
try s.endObject();
try s.endObject();
}
/// encode an account event.
pub fn encodeAccount(w: *std.Io.Writer, args: struct {
did: []const u8,
time_us: i64,
local_seq: u64 = 0,
seq: i64,
time: []const u8,
active: bool,
status: ?[]const u8,
}) !void {
var s: std.json.Stringify = .{ .writer = w };
try s.beginObject();
try s.objectField("did");
try writeString(&s, args.did);
try s.objectField("time_us");
try s.write(args.time_us);
if (args.local_seq != 0) {
try s.objectField("cursor");
try s.write(args.local_seq);
}
try s.objectField("kind");
try s.write("account");
try s.objectField("account");
try s.beginObject();
try s.objectField("active");
try s.write(args.active);
try s.objectField("did");
try writeString(&s, args.did);
try s.objectField("seq");
try s.write(args.seq);
if (args.status) |st| {
try s.objectField("status");
try s.write(st);
}
try s.objectField("time");
try s.write(args.time);
try s.endObject();
try s.endObject();
}
/// CIDv1 string form: multibase base32lower with 'b' prefix.
pub fn cidString(allocator: Allocator, cid: cbor.Cid) ![]u8 {
return multibase.encode(allocator, .base32lower, cid.raw);
}
/// render a decoded DAG-CBOR value in the atproto JSON representation.
fn writeCborValue(s: *std.json.Stringify, value: cbor.Value) !void {
switch (value) {
.unsigned => |v| try s.write(v),
.negative => |v| try s.write(v),
// upstream's cbor.ToJSON rejects floats as outside the atproto data
// model (atmos cbor/json.go), so the record is unencodable, not emitted
.float => return error.WriteFailed,
// record text can be any bytes a PDS chose to write; the same
// array-instead-of-string hazard applies to it as to the envelope
.text => |v| try writeString(s, v),
.boolean => |v| try s.write(v),
.null => try s.write(null),
.bytes => |v| {
try s.beginObject();
try s.objectField("$bytes");
try s.beginWriteRaw();
try s.writer.writeByte('"');
try writeBase64NoPad(s.writer, v);
try s.writer.writeByte('"');
s.endWriteRaw();
try s.endObject();
},
.cid => |cid| {
try s.beginObject();
try s.objectField("$link");
var buf: [128]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buf);
const str = cidString(fba.allocator(), cid) catch return error.WriteFailed;
try s.write(str);
try s.endObject();
},
.array => |items| {
try s.beginArray();
for (items) |item| try writeCborValue(s, item);
try s.endArray();
},
.map => |entries| {
try s.beginObject();
for (entries) |entry| {
try s.objectField(entry.key);
try writeCborValue(s, entry.value);
}
try s.endObject();
},
}
}
fn writeBase64NoPad(w: *std.Io.Writer, data: []const u8) !void {
const encoder = std.base64.standard_no_pad.Encoder;
var chunk: [4]u8 = undefined;
var i: usize = 0;
while (i < data.len) : (i += 3) {
const n = @min(3, data.len - i);
const out = encoder.encode(&chunk, data[i..][0..n]);
try w.writeAll(out);
}
}
// === tests ===
const testing = std.testing;
fn expectEncode(expected: []const u8, actual: []const u8) !void {
try testing.expectEqualStrings(expected, actual);
}
test "encodeCommit create with record" {
var out: std.Io.Writer.Allocating = .init(testing.allocator);
defer out.deinit();
const record: cbor.Value = .{ .map = &.{
.{ .key = "$type", .value = .{ .text = "app.bsky.feed.like" } },
.{ .key = "count", .value = .{ .unsigned = 3 } },
} };
// CIDv1 dag-cbor sha2-256 over "hi" (contents don't matter for encoding)
const cid = try cbor.Cid.forDagCbor(testing.allocator, "hi");
defer testing.allocator.free(cid.raw);
try encodeCommit(testing.allocator, &out.writer, .{
.did = "did:plc:abc",
.time_us = 1725911162329308,
.rev = "3l3qo2vutsw2b",
.operation = "create",
.collection = "app.bsky.feed.like",
.rkey = "3l3qo2vuowo2b",
.record = record,
.cid = cid,
});
const json = out.written();
// envelope fields present and ordered
try testing.expect(std.mem.startsWith(u8, json, "{\"did\":\"did:plc:abc\",\"time_us\":1725911162329308,\"kind\":\"commit\","));
try testing.expect(std.mem.indexOf(u8, json, "\"operation\":\"create\"") != null);
try testing.expect(std.mem.indexOf(u8, json, "\"record\":{\"$type\":\"app.bsky.feed.like\",\"count\":3}") != null);
try testing.expect(std.mem.indexOf(u8, json, "\"cid\":\"b") != null);
}
test "encodeCommit delete omits record and cid" {
var out: std.Io.Writer.Allocating = .init(testing.allocator);
defer out.deinit();
try encodeCommit(testing.allocator, &out.writer, .{
.did = "did:plc:abc",
.time_us = 1,
.rev = "3l3qo2vutsw2b",
.operation = "delete",
.collection = "app.bsky.feed.like",
.rkey = "xyz",
});
const json = out.written();
try testing.expect(std.mem.indexOf(u8, json, "record") == null);
try testing.expect(std.mem.indexOf(u8, json, "cid") == null);
try testing.expect(std.mem.indexOf(u8, json, "\"rkey\":\"xyz\"}}") != null);
}
test "encodeIdentity and encodeAccount" {
var out: std.Io.Writer.Allocating = .init(testing.allocator);
defer out.deinit();
try encodeIdentity(&out.writer, .{
.did = "did:plc:abc",
.time_us = 42,
.seq = 7,
.time = "2024-09-09T19:46:02.102Z",
.handle = "alice.test",
});
try expectEncode(
"{\"did\":\"did:plc:abc\",\"time_us\":42,\"kind\":\"identity\",\"identity\":{\"did\":\"did:plc:abc\",\"handle\":\"alice.test\",\"seq\":7,\"time\":\"2024-09-09T19:46:02.102Z\"}}",
out.written(),
);
out.clearRetainingCapacity();
try encodeAccount(&out.writer, .{
.did = "did:plc:abc",
.time_us = 42,
.seq = 7,
.time = "2024-09-09T19:46:02.102Z",
.active = false,
.status = "takendown",
});
try expectEncode(
"{\"did\":\"did:plc:abc\",\"time_us\":42,\"kind\":\"account\",\"account\":{\"active\":false,\"did\":\"did:plc:abc\",\"seq\":7,\"status\":\"takendown\",\"time\":\"2024-09-09T19:46:02.102Z\"}}",
out.written(),
);
}
test "record with cid link and bytes" {
var out: std.Io.Writer.Allocating = .init(testing.allocator);
defer out.deinit();
const cid = try cbor.Cid.forDagCbor(testing.allocator, "x");
defer testing.allocator.free(cid.raw);
const record: cbor.Value = .{ .map = &.{
.{ .key = "ref", .value = .{ .cid = cid } },
.{ .key = "blob", .value = .{ .bytes = "\x01\x02\x03" } },
} };
try encodeCommit(testing.allocator, &out.writer, .{
.did = "did:plc:abc",
.time_us = 1,
.rev = "3l3qo2vutsw2b",
.operation = "create",
.collection = "app.bsky.feed.post",
.rkey = "k",
.record = record,
.cid = cid,
});
const json = out.written();
try testing.expect(std.mem.indexOf(u8, json, "\"ref\":{\"$link\":\"b") != null);
try testing.expect(std.mem.indexOf(u8, json, "\"blob\":{\"$bytes\":\"AQID\"}") != null);
}
// Mirrors upstream: atmos decodes a finite float64 but cbor.ToJSON rejects it
// as outside the atproto data model, so the subscribe encoder errors instead
// of emitting a JSON number.
test "record with a float is unencodable" {
var out: std.Io.Writer.Allocating = .init(testing.allocator);
defer out.deinit();
const cid = try cbor.Cid.forDagCbor(testing.allocator, "x");
defer testing.allocator.free(cid.raw);
var arena = std.heap.ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const record = try cbor.decodeAll(arena.allocator(), "\xa1\x61x\xfb\x3f\xf0\x00\x00\x00\x00\x00\x00");
try testing.expectError(error.WriteFailed, encodeCommit(testing.allocator, &out.writer, .{
.did = "did:plc:abc",
.time_us = 1,
.rev = "3l3qo2vutsw2b",
.operation = "create",
.collection = "app.bsky.feed.post",
.rkey = "k",
.record = record,
.cid = cid,
}));
}
// === network.bsky.jetstream.subscribeEvents: proposal-0015 wire ===
// One xrpc.v1.json message frame per event:
// {"$type":"message","payload":{"$type":"network.bsky.jetstream.subscribeEvents#<kind>", ...}}
// Payload key order is pinned to upstream's lexgen output (alphabetical,
// $type first); `time` is RFC 3339 UTC with exactly six fractional digits
// (upstream encoder.go wireTimeLayout). No record_cbor (upstream #327).
const message_frame_prefix = "{\"$type\":\"message\",\"payload\":";
const nsid = "network.bsky.jetstream.subscribeEvents";
/// render a unix-microseconds timestamp as upstream's wireTimeLayout:
/// "2006-01-02T15:04:05.000000Z" — one rendering per instant, byte-stable.
pub fn writeWireTime(w: *std.Io.Writer, us: i64) !void {
const text = zat.Datetime.fromMicros(us).toString();
try w.writeAll(&text);
}
fn writePayloadType(s: *std.json.Stringify, fragment: []const u8) !void {
try s.objectField("$type");
try s.beginWriteRaw();
try s.writer.writeByte('"');
try s.writer.writeAll(nsid);
try s.writer.writeByte('#');
try s.writer.writeAll(fragment);
try s.writer.writeByte('"');
s.endWriteRaw();
}
fn writeEnvelopeTail(s: *std.json.Stringify, seq: u64, time_us: i64) !void {
try s.objectField("seq");
try s.write(seq);
try s.objectField("time");
try s.beginWriteRaw();
try s.writer.writeByte('"');
try writeWireTime(s.writer, time_us);
try s.writer.writeByte('"');
s.endWriteRaw();
}
/// {"$type":"message","payload":{"$type":"...#commit","cid"?,"collection",
/// "did","operation","record"?,"rev","rkey","seq","time"}}
pub fn encodeCommitV2(allocator: Allocator, w: *std.Io.Writer, op: CommitOp, seq: u64) !void {
try w.writeAll(message_frame_prefix);
var s: std.json.Stringify = .{ .writer = w };
try s.beginObject();
try writePayloadType(&s, "commit");
if (op.cid) |cid| {
try s.objectField("cid");
const cid_str = try cidString(allocator, cid);
defer allocator.free(cid_str);
try s.write(cid_str);
}
try s.objectField("collection");
try writeString(&s, op.collection);
try s.objectField("did");
try writeString(&s, op.did);
try s.objectField("operation");
try s.write(op.operation);
if (op.record) |record| {
try s.objectField("record");
try writeCborValue(&s, record);
}
try s.objectField("rev");
try writeString(&s, op.rev);
try s.objectField("rkey");
try writeString(&s, op.rkey);
try writeEnvelopeTail(&s, seq, op.time_us);
try s.endObject();
try w.writeByte('}');
}
/// {"$type":"message","payload":{"$type":"...#identity","did","identity":{...upstream verbatim},"seq","time"}}
pub fn encodeIdentityV2(w: *std.Io.Writer, args: anytype, seq: u64) !void {
try w.writeAll(message_frame_prefix);
var s: std.json.Stringify = .{ .writer = w };
try s.beginObject();
try writePayloadType(&s, "identity");
try s.objectField("did");
try writeString(&s, args.did);
try s.objectField("identity");
try s.beginObject();
try s.objectField("did");
try writeString(&s, args.did);
if (args.handle) |h| {
try s.objectField("handle");
try writeString(&s, h);
}
try s.objectField("seq");
try s.write(args.seq);
try s.objectField("time");
try s.write(args.time);
try s.endObject();
try writeEnvelopeTail(&s, seq, args.time_us);
try s.endObject();
try w.writeByte('}');
}
/// {"$type":"message","payload":{"$type":"...#account","account":{...},"did","seq","time"}}
pub fn encodeAccountV2(w: *std.Io.Writer, args: anytype, seq: u64) !void {
try w.writeAll(message_frame_prefix);
var s: std.json.Stringify = .{ .writer = w };
try s.beginObject();
try writePayloadType(&s, "account");
try s.objectField("account");
try s.beginObject();
try s.objectField("active");
try s.write(args.active);
try s.objectField("did");
try writeString(&s, args.did);
try s.objectField("seq");
try s.write(args.seq);
if (args.status) |st| {
try s.objectField("status");
try s.write(st);
}
try s.objectField("time");
try s.write(args.time);
try s.endObject();
try s.objectField("did");
try writeString(&s, args.did);
try writeEnvelopeTail(&s, seq, args.time_us);
try s.endObject();
try w.writeByte('}');
}
/// {"$type":"message","payload":{"$type":"...#sync","did","seq","sync":{...},"time"}}
pub fn encodeSyncV2(w: *std.Io.Writer, args: struct {
did: []const u8,
time_us: i64,
upstream_seq: i64,
rev: []const u8,
time: []const u8,
blocks: []const u8,
}, seq: u64) !void {
try w.writeAll(message_frame_prefix);
var s: std.json.Stringify = .{ .writer = w };
try s.beginObject();
try writePayloadType(&s, "sync");
try s.objectField("did");
try writeString(&s, args.did);
try s.objectField("seq");
try s.write(seq);
try s.objectField("sync");
try s.beginObject();
// lexicon "bytes" value: the {"$bytes":"<unpadded std base64>"} sentinel
// (atmos cbor.AppendJSONBytes) — a bare base64 string is rejected by
// conformant decoders (caught live by the d4dd2f0 differential oracle)
try s.objectField("blocks");
try s.beginObject();
try s.objectField("$bytes");
try s.beginWriteRaw();
try s.writer.writeByte('"');
try writeBase64NoPad(s.writer, args.blocks);
try s.writer.writeByte('"');
s.endWriteRaw();
try s.endObject();
try s.objectField("did");
try writeString(&s, args.did);
try s.objectField("rev");
try s.write(args.rev);
try s.objectField("seq");
try s.write(args.upstream_seq);
try s.objectField("time");
try s.write(args.time);
try s.endObject();
try s.objectField("time");
try s.beginWriteRaw();
try s.writer.writeByte('"');
try writeWireTime(s.writer, args.time_us);
try s.writer.writeByte('"');
s.endWriteRaw();
try s.endObject();
try w.writeByte('}');
}
/// #info advisory (seq-less): {"$type":"message","payload":{"$type":"...#info","message"?,"name"}}
pub fn encodeInfoV2(w: *std.Io.Writer, name: []const u8, message: ?[]const u8) !void {
try w.writeAll(message_frame_prefix);
var s: std.json.Stringify = .{ .writer = w };
try s.beginObject();
try writePayloadType(&s, "info");
if (message) |m| {
try s.objectField("message");
try s.write(m);
}
try s.objectField("name");
try s.write(name);
try s.endObject();
try w.writeByte('}');
}
/// terminal error frame: {"$type":"error","error":code,"message"?}. the
/// connection closes immediately after one is sent.
pub fn encodeErrorV2(w: *std.Io.Writer, code: []const u8, message: ?[]const u8) !void {
var s: std.json.Stringify = .{ .writer = w };
try s.beginObject();
try s.objectField("$type");
try s.write("error");
try s.objectField("error");
try s.write(code);
if (message) |m| {
try s.objectField("message");
try s.write(m);
}
try s.endObject();
}
test "encodeCommitV2 emits a proposal-0015 message frame in lexgen key order" {
var out: std.Io.Writer.Allocating = .init(testing.allocator);
defer out.deinit();
const record: cbor.Value = .{ .map = &.{
.{ .key = "$type", .value = .{ .text = "app.bsky.feed.like" } },
} };
const payload = "\xa1\x65\x24\x74\x79\x70\x65\x72app.bsky.feed.like";
const cid = try cbor.Cid.forDagCbor(testing.allocator, payload);
defer testing.allocator.free(cid.raw);
try encodeCommitV2(testing.allocator, &out.writer, .{
.did = "did:plc:abc",
.time_us = 1725911162329308,
.rev = "3l3qo2vutsw2b",
.operation = "create",
.collection = "app.bsky.feed.like",
.rkey = "k",
.record = record,
.cid = cid,
}, 42);
const json = out.written();
try testing.expect(std.mem.startsWith(u8, json, "{\"$type\":\"message\",\"payload\":{\"$type\":\"network.bsky.jetstream.subscribeEvents#commit\",\"cid\":\"b"));
// lexgen order: cid, collection, did, operation, record, rev, rkey, seq, time
const c = std.mem.indexOf(u8, json, "\"collection\":").?;
const d = std.mem.indexOf(u8, json, "\"did\":").?;
const o = std.mem.indexOf(u8, json, "\"operation\":").?;
const r = std.mem.indexOf(u8, json, "\"record\":").?;
try testing.expect(c < d and d < o and o < r);
try testing.expect(std.mem.indexOf(u8, json, "\"seq\":42,\"time\":\"2024-09-09T19:46:02.329308Z\"}}") != null);
try testing.expect(std.mem.indexOf(u8, json, "record_cbor") == null);
try testing.expect(std.mem.indexOf(u8, json, "time_us") == null);
}
test "encodeCommitV2 delete omits cid and record" {
var out: std.Io.Writer.Allocating = .init(testing.allocator);
defer out.deinit();
try encodeCommitV2(testing.allocator, &out.writer, .{
.did = "did:plc:abc",
.time_us = 0,
.rev = "r",
.operation = "delete",
.collection = "app.bsky.feed.like",
.rkey = "k",
}, 7);
const json = out.written();
try testing.expect(std.mem.indexOf(u8, json, "cid") == null);
try testing.expect(std.mem.indexOf(u8, json, "record") == null);
try testing.expect(std.mem.indexOf(u8, json, "\"time\":\"1970-01-01T00:00:00.000000Z\"") != null);
}
test "encodeInfoV2 and encodeErrorV2 frames" {
var out: std.Io.Writer.Allocating = .init(testing.allocator);
defer out.deinit();
try encodeInfoV2(&out.writer, "OutdatedCursor", "requested cursor was below retention; clamped");
try testing.expect(std.mem.startsWith(u8, out.written(), "{\"$type\":\"message\",\"payload\":{\"$type\":\"network.bsky.jetstream.subscribeEvents#info\",\"message\":"));
try testing.expect(std.mem.endsWith(u8, out.written(), "\"name\":\"OutdatedCursor\"}}"));
out.clearRetainingCapacity();
try encodeErrorV2(&out.writer, "ConsumerTooSlow", null);
try testing.expectEqualStrings("{\"$type\":\"error\",\"error\":\"ConsumerTooSlow\"}", out.written());
}
test "v1 envelope carries cursor when the seq is known, never when zero" {
var out: std.Io.Writer.Allocating = .init(testing.allocator);
defer out.deinit();
try encodeCommit(testing.allocator, &out.writer, .{
.did = "did:plc:abc",
.time_us = 1,
.seq = 42,
.rev = "r",
.operation = "delete",
.collection = "app.bsky.feed.like",
.rkey = "k",
});
try testing.expect(std.mem.startsWith(u8, out.written(), "{\"did\":\"did:plc:abc\",\"time_us\":1,\"cursor\":42,\"kind\":\"commit\","));
out.clearRetainingCapacity();
try encodeCommit(testing.allocator, &out.writer, .{
.did = "did:plc:abc",
.time_us = 1,
.rev = "r",
.operation = "delete",
.collection = "app.bsky.feed.like",
.rkey = "k",
});
try testing.expect(std.mem.indexOf(u8, out.written(), "cursor") == null);
}
test "invalid utf8 in a string field stays a JSON string (never an array)" {
// Regression: Stringify.write renders a non-UTF-8 []const u8 as an array
// of numbers, so one torn byte turned `did` into a list on the wire and
// broke consumers (2026-08-09, caught by the lifecycle oracle's fault
// injection). Go's encoder substitutes U+FFFD; so do we.
var out: std.Io.Writer.Allocating = .init(testing.allocator);
defer out.deinit();
try encodeCommit(testing.allocator, &out.writer, .{
.did = "did:plc:a\xffb",
.time_us = 1,
.rev = "rev1",
.operation = "create",
.collection = "app.bsky.feed.post",
.rkey = "k\xfe",
});
const json = out.writer.buffered();
// the shape a consumer depends on: a quoted string, not [100,105,...]
try testing.expect(std.mem.indexOf(u8, json, "\"did\":\"did:plc:a") != null);
try testing.expect(std.mem.indexOf(u8, json, "\"did\":[") == null);
try testing.expect(std.mem.indexOf(u8, json, "\"rkey\":[") == null);
// invalid bytes became the replacement character, and it still parses
try testing.expect(std.mem.indexOf(u8, json, "\u{FFFD}") != null);
var parsed = try std.json.parseFromSlice(std.json.Value, testing.allocator, json, .{});
defer parsed.deinit();
try testing.expect(parsed.value.object.get("did").? == .string);
}
test "valid utf8 fields are untouched (byte parity with upstream)" {
var out: std.Io.Writer.Allocating = .init(testing.allocator);
defer out.deinit();
try encodeCommit(testing.allocator, &out.writer, .{
.did = "did:plc:abc",
.time_us = 1,
.rev = "rev1",
.operation = "create",
.collection = "app.bsky.feed.post",
.rkey = "k1",
});
try testing.expect(std.mem.startsWith(u8, out.writer.buffered(), "{\"did\":\"did:plc:abc\","));
}
test "record text and keys with invalid utf8 still produce parseable JSON" {
// A PDS can put arbitrary bytes in a CBOR text value. Both the value and
// the map key must survive as JSON strings: an array in key position is
// not merely surprising, it is invalid JSON and breaks every parser.
var out: std.Io.Writer.Allocating = .init(testing.allocator);
defer out.deinit();
const record: cbor.Value = .{ .map = &.{
.{ .key = "text", .value = .{ .text = "hello \xff world" } },
} };
try encodeCommit(testing.allocator, &out.writer, .{
.did = "did:plc:abc",
.time_us = 1,
.rev = "rev1",
.operation = "create",
.collection = "app.bsky.feed.post",
.rkey = "k1",
.record = record,
});
const json = out.writer.buffered();
var parsed = try std.json.parseFromSlice(std.json.Value, testing.allocator, json, .{});
defer parsed.deinit();
const rec = parsed.value.object.get("commit").?.object.get("record").?;
try testing.expect(rec.object.get("text").? == .string);
}