Something went wrong. Try again.
Build Reactive Signals for Bluesky's AT Protocol Firehose in Laravel
Something went wrong. Try again.
2.1 kB · 85 lines
PHP
at dev
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586<?php
declare(strict_types=1);
namespace SocialDept\AtpSignals\Core;
use SocialDept\AtpSignals\CAR\BlockReader;
/** * CAR (Content Addressable aRchive) facade. * * Provides static methods for parsing CAR data from AT Protocol Firehose. */class CAR{ /** * Parse CAR blocks. * * Returns array of blocks keyed by CID string. * The blocks contain raw CBOR data, not decoded. * * @param string $data Binary CAR data * @param string|null $did DID for constructing URIs (not used, kept for compatibility) * @return array<string, string> Map of CID string => block data */ public static function blockMap(string $data, ?string $did = null): array { // Read all blocks from CAR $blockReader = new BlockReader($data);
return $blockReader->getBlockMap(); }
/** * Extract DID from commit block. */ private static function extractDidFromBlocks(array $blocks): ?string { // The first block is typically the commit $firstBlock = reset($blocks);
if ($firstBlock === false) { return null; }
$decoded = CBOR::decode($firstBlock);
if (! is_array($decoded)) { return null; }
return $decoded['did'] ?? null; }
/** * Find MST root CID from blocks. */ private static function findMstRoot(array $blocks, array $cids): ?CID { // Try to parse commit block to get data CID $firstBlock = reset($blocks);
if ($firstBlock === false) { return null; }
$commit = CBOR::decode($firstBlock);
if (! is_array($commit)) { return null; }
// MST root is in the 'data' field of commit if (isset($commit['data']) && $commit['data'] instanceof CID) { return $commit['data']; }
// Fallback: second block is often the MST root if (count($cids) >= 2) { return CID::fromString($cids[1]); }
return null; }}