skyfall 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 677e5fd4ee93debfd3495c7c3504642c603eeda97ddba4273d1848d3240892f6
4
+ data.tar.gz: 0db72ec396022df1fb970a614bf217c2f20a04d843e6558eabc39dd74ddb8979
5
+ SHA512:
6
+ metadata.gz: dc9c6ea0447c1f4310c1772ee093e88f98666ee925f6b81219c60b09a8a761f6c05a9a56449af57d270109148c3fd1a4d1420ceb8cd9a39f27d74d21c430e9cf
7
+ data.tar.gz: db9097d342156c20e043b59da778fc11cb2d9281fa2c3c432744b6b4fa81e0a505dda6a4670357bdbfb435f87e7436a77fb80953a74bb0c40a7a3df457a5259e
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-05-31
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in skyfall.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,38 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ skyfall (0.1.0)
5
+ base32 (>= 0.3.4)
6
+ cbor (>= 0.5.9.6)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ base32 (0.3.4)
12
+ cbor (0.5.9.6)
13
+ diff-lcs (1.5.0)
14
+ rake (13.0.6)
15
+ rspec (3.12.0)
16
+ rspec-core (~> 3.12.0)
17
+ rspec-expectations (~> 3.12.0)
18
+ rspec-mocks (~> 3.12.0)
19
+ rspec-core (3.12.2)
20
+ rspec-support (~> 3.12.0)
21
+ rspec-expectations (3.12.3)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.12.0)
24
+ rspec-mocks (3.12.5)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.12.0)
27
+ rspec-support (3.12.0)
28
+
29
+ PLATFORMS
30
+ arm64-darwin-21
31
+
32
+ DEPENDENCIES
33
+ rake (~> 13.0)
34
+ rspec (~> 3.0)
35
+ skyfall!
36
+
37
+ BUNDLED WITH
38
+ 2.4.13
data/MIT-LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Kuba Suder
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Skyfall
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/skyfall`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/skyfall.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,81 @@
1
+ require_relative 'cid'
2
+ require_relative 'errors'
3
+ require_relative 'extensions'
4
+
5
+ require 'cbor'
6
+ require 'stringio'
7
+
8
+ # CAR v1: https://ipld.io/specs/transport/car/carv1/
9
+ # multicodec codes: https://github.com/multiformats/multicodec/blob/master/table.csv
10
+
11
+ module Skyfall
12
+ class CarSection
13
+ attr_reader :cid, :body
14
+
15
+ def initialize(cid, body)
16
+ @cid = cid
17
+ @body = body
18
+ end
19
+ end
20
+
21
+ class CarArchive
22
+ using Skyfall::Extensions
23
+
24
+ attr_reader :roots, :sections
25
+
26
+ def initialize(data)
27
+ @sections = []
28
+
29
+ buffer = StringIO.new(data)
30
+ read_header(buffer)
31
+ read_section(buffer) until buffer.eof?
32
+ end
33
+
34
+
35
+ private
36
+
37
+ def read_header(buffer)
38
+ len = buffer.read_varint
39
+
40
+ header_data = buffer.read(len)
41
+ raise DecodeError.new("Header too short: #{header_data}") unless header_data.length == len
42
+
43
+ header = CBOR.decode(header_data)
44
+ raise UnsupportedError.new("Unexpected CAR version: #{header['version']}") unless header['version'] == 1
45
+ @roots = header['roots'].map { |x| CID.from_cbor_tag(x) }
46
+ end
47
+
48
+ def read_section(buffer)
49
+ len = buffer.read_varint
50
+
51
+ section_data = buffer.read(len)
52
+ raise DecodeError.new("Section too short: #{section_data}") unless section_data.length == len
53
+
54
+ sbuffer = StringIO.new(section_data)
55
+
56
+ version = sbuffer.read_varint
57
+ raise UnsupportedError.new("Unexpected CID version: #{version}") unless version == 1
58
+
59
+ codec = sbuffer.read_varint
60
+ raise UnsupportedError.new("Unexpected CID codec: #{codec}") unless codec == 0x71 # dag-cbor
61
+
62
+ hash = sbuffer.read_varint
63
+ raise UnsupportedError.new("Unexpected CID hash: #{hash}") unless hash == 0x12 # sha2-256
64
+
65
+ clen = sbuffer.read_varint
66
+ raise UnsupportedError.new("Unexpected CID length: #{clen}") unless clen == 32
67
+
68
+ prefix = section_data[0...sbuffer.pos]
69
+
70
+ cid_data = sbuffer.read(clen)
71
+ raise DecodeError.new("CID too short: #{cid_data}") unless cid_data.length == clen
72
+
73
+ cid = CID.new(prefix + cid_data)
74
+
75
+ body_data = sbuffer.read
76
+ body = CBOR.decode(body_data)
77
+
78
+ @sections << CarSection.new(cid, body)
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,35 @@
1
+ require_relative 'errors'
2
+
3
+ require 'base32'
4
+
5
+ # CIDs in DAG-CBOR: https://ipld.io/specs/codecs/dag-cbor/spec/
6
+ # CIDs in JSON: https://ipld.io/specs/codecs/dag-json/spec/
7
+ # multibase: https://github.com/multiformats/multibase
8
+
9
+ module Skyfall
10
+ class CID
11
+ attr_reader :data
12
+
13
+ def self.from_cbor_tag(tag)
14
+ data = tag.value
15
+ raise DecodeError.new("Unexpected first byte of CID: #{data[0]}") unless data[0] == "\x00"
16
+ CID.new(data[1..-1])
17
+ end
18
+
19
+ def initialize(data)
20
+ @data = data
21
+ end
22
+
23
+ def to_s
24
+ 'b' + Base32.encode(@data).downcase.gsub(/=+$/, '')
25
+ end
26
+
27
+ def inspect
28
+ "CID(\"#{to_s}\")"
29
+ end
30
+
31
+ def ==(other)
32
+ other.is_a?(CID) && @data == other.data
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,11 @@
1
+ module Skyfall
2
+ module Collection
3
+ BSKY_POST = "app.bsky.feed.post"
4
+ BSKY_LIKE = "app.bsky.feed.like"
5
+ BSKY_FOLLOW = "app.bsky.graph.follow"
6
+ BSKY_REPOST = "app.bsky.feed.repost"
7
+ BSKY_BLOCK = "app.bsky.graph.block"
8
+ BSKY_PROFILE = "app.bsky.actor.profile"
9
+ BSKY_LISTITEM = "app.bsky.graph.listitem"
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module Skyfall
2
+ class DecodeError < StandardError
3
+ end
4
+
5
+ class UnsupportedError < StandardError
6
+ end
7
+ end
@@ -0,0 +1,32 @@
1
+ require 'cbor'
2
+ require 'stringio'
3
+
4
+ module Skyfall
5
+ module Extensions
6
+
7
+ refine StringIO do
8
+ # https://en.wikipedia.org/wiki/LEB128
9
+ def read_varint
10
+ shift = 1
11
+ value = 0
12
+
13
+ loop do
14
+ byte = self.readbyte
15
+ value += byte % 128 * shift
16
+ break if byte < 128
17
+ shift *= 128
18
+ end
19
+
20
+ value
21
+ end
22
+ end
23
+
24
+ refine CBOR.singleton_class do
25
+ def decode_sequence(data)
26
+ unpacker = CBOR::Unpacker.new(StringIO.new(data))
27
+ unpacker.each.to_a
28
+ end
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,36 @@
1
+ require_relative 'collection'
2
+
3
+ module Skyfall
4
+ class Operation
5
+ attr_reader :repo, :path, :action, :cid, :record
6
+
7
+ def initialize(repo, path, action, cid, record)
8
+ @repo = repo
9
+ @path = path
10
+ @action = action.to_sym
11
+ @cid = cid
12
+ @record = record
13
+ end
14
+
15
+ def uri
16
+ "at://#{repo}/#{path}"
17
+ end
18
+
19
+ def collection
20
+ path.split('/').first
21
+ end
22
+
23
+ def type
24
+ case collection
25
+ when Collection::BSKY_POST then :bsky_post
26
+ when Collection::BSKY_LIKE then :bsky_like
27
+ when Collection::BSKY_FOLLOW then :bsky_follow
28
+ when Collection::BSKY_REPOST then :bsky_repost
29
+ when Collection::BSKY_BLOCK then :bsky_block
30
+ when Collection::BSKY_PROFILE then :bsky_profile
31
+ when Collection::BSKY_LISTITEM then :bsky_listitem
32
+ else :unknown
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Skyfall
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,48 @@
1
+ require_relative 'car_archive'
2
+ require_relative 'cid'
3
+ require_relative 'errors'
4
+ require_relative 'extensions'
5
+ require_relative 'operation'
6
+
7
+ require 'cbor'
8
+ require 'time'
9
+
10
+ module Skyfall
11
+ class WebsocketMessage
12
+ using Skyfall::Extensions
13
+
14
+ attr_reader :type, :repo, :time, :seq, :commit, :blocks, :operations
15
+
16
+ def initialize(data)
17
+ objects = CBOR.decode_sequence(data)
18
+ raise DecodeError.new("Invalid number of objects: #{objects.length}") unless objects.length == 2
19
+
20
+ @type_object, @data_object = objects
21
+ raise DecodeError.new("Invalid object type: #{@type_object}") unless @type_object.is_a?(Hash)
22
+ raise DecodeError.new("Invalid object type: #{@data_object}") unless @data_object.is_a?(Hash)
23
+ raise DecodeError.new("Missing data: #{@type_object}") unless @type_object['op'] && @type_object['t']
24
+ raise DecodeError.new("Invalid message type: #{@type_object['t']}") unless @type_object['t'].start_with?('#')
25
+ raise UnsupportedError.new("Unexpected CBOR object: #{@type_object}") unless @type_object['op'] == 1
26
+
27
+ @type = @type_object['t'][1..-1].to_sym
28
+
29
+ @repo = @data_object['repo']
30
+ @time = Time.parse(@data_object['time'])
31
+ @seq = @data_object['seq']
32
+
33
+ return unless @type == :commit
34
+
35
+ @commit = CID.from_cbor_tag(@data_object['commit'])
36
+ @blocks = CarArchive.new(@data_object['blocks'])
37
+
38
+ @operations = @data_object['ops'].map { |op|
39
+ cid = op['cid'] && CID.from_cbor_tag(op['cid'])
40
+ path = op['path']
41
+ action = op['action']
42
+ record = cid && @blocks.sections.detect { |s| s.cid == cid }.body
43
+
44
+ Operation.new(@repo, path, action, cid, record)
45
+ }
46
+ end
47
+ end
48
+ end
data/lib/skyfall.rb ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'skyfall/websocket_message'
4
+ require_relative 'skyfall/version'
data/sig/skyfall.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Skyfall
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
data/skyfall.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/skyfall/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "skyfall"
7
+ spec.version = Skyfall::VERSION
8
+ spec.authors = ["Kuba Suder"]
9
+ spec.email = ["jakub.suder@gmail.com"]
10
+
11
+ spec.summary = "A Ruby gem for streaming data from the Bluesky/AtProto firehose"
12
+ spec.homepage = "https://github.com/mackuba/skyfall"
13
+
14
+ # spec.description = "TODO: Write a longer description or delete this line."
15
+
16
+ spec.license = "MIT"
17
+ spec.required_ruby_version = ">= 2.6.0"
18
+
19
+ # spec.metadata["homepage_uri"] = spec.homepage
20
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
21
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
28
+ end
29
+ end
30
+
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency 'cbor', '>= 0.5.9.6'
34
+ spec.add_dependency 'base32', '>= 0.3.4'
35
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: skyfall
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kuba Suder
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-06-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cbor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.5.9.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.5.9.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: base32
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.4
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.3.4
41
+ description:
42
+ email:
43
+ - jakub.suder@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - CHANGELOG.md
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - MIT-LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - lib/skyfall.rb
56
+ - lib/skyfall/car_archive.rb
57
+ - lib/skyfall/cid.rb
58
+ - lib/skyfall/collection.rb
59
+ - lib/skyfall/errors.rb
60
+ - lib/skyfall/extensions.rb
61
+ - lib/skyfall/operation.rb
62
+ - lib/skyfall/version.rb
63
+ - lib/skyfall/websocket_message.rb
64
+ - sig/skyfall.rbs
65
+ - skyfall.gemspec
66
+ homepage: https://github.com/mackuba/skyfall
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 2.6.0
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.4.10
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: A Ruby gem for streaming data from the Bluesky/AtProto firehose
89
+ test_files: []