Oxygène #
(n) Gaz incolore, inodore et sans saveur qui compose 1/5ème de l'air atmosphérique.
Various data decoding primitives for working with AT Protocol and the Atmosphere (including CAR, CID, CBOR).
Note
Part of ATProto Ruby SDK: ruby.sdk.blue
Purpose #
Oxygene includes various data decoding related primitives for working with AT Protocol repositories and firehose events. Provides at least partial implementations of standards such as CAR, CID and Base 32.
This gem was extracted from Skyfall and is mostly used there for the CBOR firehose decoding, but can also be used standalone for other ATProto data handling purposes, like decoding downloaded .car account repos.
The functionality currently includes:
- Base32 encoder/decoder
- CAR archive/repo parser
- STAR file parser
- CID wrapper
- varint reader
- and a CBOR decoder for decoding the ATProto subset of DRISL/CBOR
The gem intentionally only includes support for the parts of these standards that are in use in ATProto, i.e. it doesn't handle all possible types of CIDs defined in the CID standard, only generates lowercase Base32, and so on.
Installation #
To use Oxygene, you need a reasonably new version of Ruby – it should run on Ruby 2.6 and above, although it's recommended to use a version that's still getting maintainance updates, i.e. currently 3.3+. A compatible version should be preinstalled on macOS Big Sur and above and on many Linux systems. Otherwise, you can install one using tools such as RVM, asdf, ruby-install or ruby-build, or rpm or apt-get on Linux (see more installation options on ruby-lang.org).
To install the gem, run the command:
[sudo] gem install oxygene
Or add this to your app's Gemfile:
gem 'oxygene', '~> 0.1'
Note
The readme below is already updated for not yet released 0.2 version.
Usage #
CBOR #
Use Oxygene::CBOR to decode CBOR:
data = Oxygene::CBOR.decode(bytes)
The values are decoded into Ruby hashes, arrays, strings and so on. Binary strings are returned as strings with Encoding::BINARY, and tag 42 CID values as Oxygene::CID objects.
If you want the data in "ATProto JSON" form instead, with binary strings encoded as $bytes objects with Base64 data and CIDs as $link objects, pass the json: true option:
json = Oxygene::CBOR.decode(bytes, json: true)
For the firehose message frames, which use a kind of non-standard form of two top-level map items just concatenated together, use the #decode_sequence version (#decode requires the decoded result to be one complete item with no extra bytes after it):
objects = Oxygene::CBOR.decode_sequence(bytes)
raise DecodeError if objects.length != 2
header, payload = objects
Note
Oxygene 0.2+ includes a custom ATProto-specific CBOR/DRISL parser in pure Ruby, replacing the cbor gem with a native C module that was used earlier. The pure Ruby parser is almost as fast or slightly slower than the cbor gem when run on recent Ruby versions with YJIT enabled; however, it can be significantly slower (e.g. over 2× slower) when running without YJIT. That difference is for the CBOR decoding itself, which is normally only a small fraction of the work involved in firehose processing.
If performance matters to you, or you need to do some work where CBOR decoding is a significant % of the whole time spent, and you don't want to or can't use YJIT, you can optionally add the cbor gem explicitly to the Gemfile and require the opt-in oxygene/cbor_native module, which replaces the Oxygene::CBOR decoding methods with calls to the cbor gem; or, just use the cbor gem directly and skip Oxygene, depending on what you need. (But if performance matters to you, you should really be running the latest Ruby with YJIT enabled anyway.)
The cbor_native module should not be needed if you do have YJIT enabled, although you can always benchmark this for your specific use case. The advantage of the pure Ruby parser, apart from getting rid of one dependency, is that it does more strict validation of the input to reject any CBOR not valid in ATProto, e.g. it checks that maps have only string keys that are correctly sorted, rejects float numbers, tag values other than CID, non-canonical number encoding, and so on. The cbor gem is a general CBOR decoder, which does not constrain the input to only valid DAG-CBOR/DRISL.
CID #
The Oxygene::CID class is an immutable object wrapper for CIDs (Content Identifiers), both in the binary/CBOR form and in the Base32-encoded JSON string form. You will normally encounter those included in data decoded from CAR/CBOR, rather than creating one yourself.
If you need to create one manually, how you do it depends on which representation you got as the input.
To create a CID from raw binary bytes:
cid = Oxygene::CID.new(data, binary: true)
From binary data with a \x00 prefix, as decoded from CBOR tag 42:
cid = Oxygene::CID.new(data, binary: true, cbor_prefix: true)
To create one from a JSON representation:
cid = Oxygene::CID.from_json(value)
# or:
cid = Oxygene::CID.new(value, binary: false)
Both can be converted to the other representation:
cid.cbor_form
# => "\x00\x01q\x12 \x1A\x88\xF3Z\xFC..."
cid.raw_data
# same but without the 0 prefix
# => "\x01q\x12 \x1A\x88\xF3Z\xFC..."
cid.json_form
# => "bafyreia2rdzvv7fjlp..."
CAR archives & repos #
Oxygene::CARArchive is a wrapper for any CAR archive file, including e.g. in particular the kind that's contained in a blocks field in a :commit firehose message, or downloaded CAR archives of ATProto repos.
Create it passing it the binary data to decode:
car = Oxygene::CARArchive.new(message.blocks)
The decoder automatically parses the CAR file header containing the "root" CIDs, and makes those available as #roots array. The remaining sections are parsed lazily on demand.
You can parse and access the sections in the following ways:
#sectionsreturns an array of all sections in the archive, parsing them if needed#parsed_sectionsreturns the sections parsed so far (initially[])section_with_cid(cid)looks up a section by CID; if it was already parsed, it's returned immediately, otherwise remaining sections are parsed until a match is found (or the end of the file is reached)- Note: parsed sections are searched sequentially with
detect. If you need to look up multiple sections repeatedly, use the second variant below.
- Note: parsed sections are searched sequentially with
section_with_cid(cid, use_map: true)also looks up a section, but builds up and uses aHashmapping CIDs to sections for quick lookup. For performance, creating this section index is opt-in, since it's not needed if only one section will be looked up.
Sections are represented as Oxygene::CARSection objects. A section has a #cid, and can return the body decoded from the binary CBOR data in two ways:
#decoded_bodydecodes the CBOR into a RubyHash, but leaves binary strings as strings and returns CIDs asOxygene::CIDobjects (callingOxygene::CBOR.decodewithjson: false, see CBOR section)#json_bodyreturns data in the "ATProto JSON" form as described in ATProto Data Model:- CIDs are converted to
{ "$link": "(base32 representation)" } - binary strings are converted to
{ "$bytes": "(base64 encoded data)" }
- CIDs are converted to
For .car account repos, there is additionally a subclass of CARArchive called CARRepo, which lets you extract the repo's records from the archive's Merkle Search Tree.
Create a repo reader like this:
car = Oxygene::CARRepo.new(File.binread(repo_path))
Then, iterate over the records using the #walk_all_nodes method. The records are indexed by the record "path", which is the NSID collection + the rkey joined with a /, and are returned in alphabetical order sorted by that path key (so by collection first and then by rkey within a collection):
car.walk_all_nodes do |key, cid|
collection, rkey = key.split('/')
if collection == 'app.bsky.feed.post'
record = car.section_with_cid(cid, use_map: true)
# ... save or print record w/ rkey
end
end
Base32 #
To encode a binary string into Base32:
string = Oxygene::Base32.encode(data)
To decode Base32 into binary data:
data = Oxygene::Base32.decode(string)
As an optimization, both methods also allow you to specify an offset in the input string from which to start reading, and a prefix to put at the beginning of the output buffer, so you can avoid some string allocations in a hot code path:
# skip the \x00 and add a starting 'b'
json_cid = Oxygene::Base32.encode(cbor_tag_cid, 1, 'b')
# skip the 'b' and add a starting \x00
binary_cid = Oxygene::Base32.decode(json_cid, 1, "\x00")
Credits #
Copyright © 2026 Kuba Suder (@mackuba.eu).
The code is available under the terms of the zlib license (permissive, similar to MIT).