socialdept/atp-cbor 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

socialdept/atp-cbor

Composer 安装命令:

composer require socialdept/atp-cbor

包简介

CBOR, CAR, and CID binary encoding for AT Protocol in PHP

README 文档

README

CBOR Header

CBOR, CAR, and CID binary encoding for AT Protocol in PHP.


What is ATP CBOR?

ATP CBOR is a standalone PHP library for decoding CBOR (Concise Binary Object Representation), CAR (Content Addressable aRchive), and CID (Content Identifier) binary data from the AT Protocol network. It powers the binary decoding layer used by firehose consumers and repository loaders across the Social Dept. AT Protocol packages.

No Laravel dependency required — this is a pure PHP package.

Quick Example

use SocialDept\AtpCbor\Core\CBOR;
use SocialDept\AtpCbor\Core\CAR;
use SocialDept\AtpCbor\Core\CID;

// Decode CBOR data from a firehose event
$decoded = CBOR::decode($binaryData);

// Parse CAR blocks from a repository export
$blocks = CAR::blockMap($carData);

// Parse a CID string
$cid = CID::fromString('bafyreie5cvv4h45feadgeuwhbcutmh6t7ceseocckahdoe6uat64zmz454a');
echo $cid->version; // 1

Installation

composer require socialdept/atp-cbor

Requires the gmp PHP extension for large integer handling.

Usage

Decoding CBOR

The CBOR facade provides three methods for decoding binary CBOR data:

use SocialDept\AtpCbor\Core\CBOR;

// Decode a single CBOR item
$value = CBOR::decode($data);

// Decode the first item and get remaining data
[$value, $remaining] = CBOR::decodeFirst($data);

// Decode all sequential CBOR items
$items = CBOR::decodeAll($data);

Supports all CBOR major types including unsigned/negative integers, byte strings, text strings, arrays, maps, tagged values (including DAG-CBOR tag 42 for CID links), and special values (booleans, null, floats).

Parsing CAR Archives

CAR (Content Addressable aRchive) files contain blocks of CBOR data indexed by CID:

use SocialDept\AtpCbor\Core\CAR;
use SocialDept\AtpCbor\CAR\RecordExtractor;

// Get all blocks as a CID => data map
$blocks = CAR::blockMap($carData);

// Extract records from AT Protocol MST blocks
$extractor = new RecordExtractor($blocks, 'did:plc:xyz');
foreach ($extractor->extractRecords($rootCid) as $path => $record) {
    echo $record['uri'];       // at://did:plc:xyz/app.bsky.feed.post/3k4...
    echo $record['cid'];       // bafyrei...
    print_r($record['value']); // Decoded record data
}

For memory-efficient processing, use the BlockReader generator directly:

use SocialDept\AtpCbor\CAR\BlockReader;

$reader = new BlockReader($carData);
foreach ($reader->blocks() as [$cid, $blockData]) {
    // Process each block individually
}

Working with CIDs

Content Identifiers can be parsed from strings or binary data:

use SocialDept\AtpCbor\Core\CID;

// Parse from string (auto-detects v0 vs v1)
$cid = CID::fromString('bafyreie5cvv4h45feadgeuwhbcutmh6t7ceseocckahdoe6uat64zmz454a');

// Parse from binary data
$cid = CID::fromBinary($bytes);

// Access properties
$cid->version; // 0 or 1
$cid->codec;   // Codec identifier
$cid->hash;    // Raw multihash bytes

// Convert back
$cid->toString();  // Base32 (v1) or base58btc (v0)
$cid->toBinary();  // Raw binary

Low-level Binary Reading

For custom binary parsing, use the Reader and Varint utilities:

use SocialDept\AtpCbor\Binary\Reader;
use SocialDept\AtpCbor\Binary\Varint;

// Stream-style binary reader
$reader = new Reader($data);
$byte = $reader->readByte();
$chunk = $reader->readBytes(32);
$varint = $reader->readVarint();

// Standalone varint decoding
$offset = 0;
$value = Varint::decode($data, $offset);

API Reference

Core Facades

Class Method Description
CBOR decode(string $data) Decode complete CBOR data
CBOR decodeFirst(string $data) Decode first item, return remaining
CBOR decodeAll(string $data) Decode all sequential items
CAR blockMap(string $data, ?string $did) Parse CAR into CID => block map

Value Objects

Class Description
CID Immutable Content Identifier (v0 and v1)

Readers

Class Description
BlockReader Generator-based CAR block reader
RecordExtractor AT Protocol MST record walker
Decoder RFC 8949 CBOR decoder with DAG-CBOR
Reader Stream-style binary data reader
Varint Variable-length integer decoder

Supported Standards

  • CBOR - RFC 8949 (Concise Binary Object Representation)
  • DAG-CBOR - IPLD extension with tag 42 for CID links
  • CAR - Content Addressable aRchive (v1)
  • CID - IPLD Content Identifiers (v0 and v1)
  • Multihash - SHA-256 and other hash types
  • Varint - Protocol Buffers-style variable-length integers

Requirements

  • PHP 8.2+
  • ext-gmp

Resources

Support & Contributing

Found a bug or have a feature request? Open an issue.

Want to contribute? We'd love your help! Check out the contribution guidelines.

Credits

License

ATP CBOR is open-source software licensed under the MIT license.

Built for the Atmosphere • By Social Dept.

socialdept/atp-cbor 适用场景与选型建议

socialdept/atp-cbor 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 702 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「encoder」 「decoder」 「laravel」 「binary」 「car」 「cbor」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 socialdept/atp-cbor 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 socialdept/atp-cbor 我们能提供哪些服务?
定制开发 / 二次开发

基于 socialdept/atp-cbor 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 702
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 39
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-07