#!/usr/bin/env bash # # Records real Jetstream frames into Tests/JetstreamKitTests/Fixtures for # JetstreamKit's wire-format tests. Needs websocat (brew install websocat). # # scripts/capture-frames.sh [seconds] [minutes of history to replay] # # Every capture starts from the same timestamp cursor, a few minutes in the # past, so that the two files from one Bluesky instance hold the same commits # on both wires. Frames over 8 KB (mostly org.tangled.git.refs snapshots) and # all but a few account and identity frames are left out to keep the fixtures small. set -euo pipefail seconds="${1:-60}" minutes_back="${2:-15}" max_frames=40 max_frame_bytes=8192 command -v websocat >/dev/null || { echo "websocat is not installed: brew install websocat" >&2; exit 1; } fixtures="$(cd "$(dirname "$0")/.." && pwd)/Tests/JetstreamKitTests/Fixtures" raw="$(mktemp -d)" trap 'rm -rf "$raw"' EXIT mkdir -p "$fixtures" cursor=$(( ($(date +%s) - minutes_back * 60) * 1000000 )) bluesky=wss://jetstream.us-east.bsky.network filter='org.tangled.*' pids=() capture() { # capture … local name="$1" shift # -B: websocat's default buffer splits messages over 64 KB across lines. websocat -t -U -B 8388608 "$@" > "$raw/$name" & pids+=($!) } capture frames-v2-bsky.jsonl --protocol xrpc.v1.json \ "$bluesky/xrpc/network.bsky.jetstream.subscribeEvents?kinds=commit&collections=$filter&cursor=$cursor" capture frames-v1-bsky.jsonl "$bluesky/subscribe?wantedCollections=$filter&cursor=$cursor" capture frames-v1-index.jsonl "wss://index.tangled.network/subscribe?wantedCollections=$filter&cursor=$cursor" sleep "$seconds" kill "${pids[@]}" 2>/dev/null || true wait 2>/dev/null || true for path in "$raw"/*.jsonl; do name="$(basename "$path")" awk -v limit="$max_frame_bytes" -v max="$max_frames" ' length($0) > limit { next } /"kind":"(identity|account)"/ && other++ >= 3 { next } { print; if (++kept == max) exit } ' "$path" > "$fixtures/$name" echo "$name: $(wc -l < "$fixtures/$name" | tr -d ' ') frames, $(wc -c < "$fixtures/$name" | tr -d ' ') bytes" >&2 done