diff --git a/examples/streamplace_chat.zig b/examples/streamplace_chat.zig index 9fbcae0..36a1358 100644 --- a/examples/streamplace_chat.zig +++ b/examples/streamplace_chat.zig @@ -2,6 +2,9 @@ //! //! run: zig build example-streamplace-chat -- iame.li //! zig build example-streamplace-chat -- at://did:plc:.../place.stream.livestream/3... +//! +//! the archive endpoints are bearer-gated; set JETSTREAM_ARCHIVE_API_KEY or +//! the example skips the archive and replays over the live socket only. const std = @import("std"); const zat = @import("zat"); @@ -92,6 +95,7 @@ const ChatPrinter = struct { pub fn main(init: std.process.Init) !void { const allocator = init.gpa; const args = try init.minimal.args.toSlice(init.arena.allocator()); + const api_key = init.minimal.environ.getAlloc(init.arena.allocator(), "JETSTREAM_ARCHIVE_API_KEY") catch null; if (args.len != 2) { std.debug.print("usage: zig build example-streamplace-chat -- \n", .{}); return error.MissingStream; @@ -210,9 +214,14 @@ pub fn main(init: std.process.Init) !void { // capture began replays completely — including messages later deleted. // The witnessed-time window maps to plan seq bounds, which keeps the // fetch to the handful of blocks the session actually touches. - const bounds = try jetstream_sdk.ArchiveBackfill.fetchSeqBounds(init.io, allocator, archive_host, start_us, end_cursor, null); + const bounds: jetstream_sdk.ArchiveBackfill.SeqBounds = if (api_key != null) + try jetstream_sdk.ArchiveBackfill.fetchSeqBounds(init.io, allocator, archive_host, start_us, end_cursor, api_key) + else + .{ .covered = false, .after_seq = 0, .before_seq = null }; var archive_boundary_us: ?i64 = null; - if (bounds.covered) { + if (api_key == null) { + std.debug.print("JETSTREAM_ARCHIVE_API_KEY unset; skipping the archive. Live replay may not include the full session history.\n", .{}); + } else if (bounds.covered) { std.debug.print("replaying from the archive ({s})...\n\n", .{archive_host}); const result = try jetstream_sdk.ArchiveBackfill.run(init.io, allocator, .{ .host = archive_host, @@ -220,6 +229,7 @@ pub fn main(init: std.process.Init) !void { .after_seq = bounds.after_seq, .before_seq = bounds.before_seq, .concurrency = 6, + .api_key = api_key, }, &printer); archive_boundary_us = result.last_time_us; std.debug.print("\narchive replay: {d} messages from {d} blocks\n", .{ printer.count, result.blocks_decoded });