skyfall 0.7.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +56 -21
- data/README.md +134 -30
- data/SECURITY.md +16 -0
- data/data/jetstream_zstd_dictionary +0 -0
- data/lib/skyfall/collection.rb +8 -1
- data/lib/skyfall/errors.rb +6 -0
- data/lib/skyfall/firehose/commit_message.rb +5 -2
- data/lib/skyfall/firehose/info_message.rb +1 -1
- data/lib/skyfall/firehose/labels_message.rb +4 -1
- data/lib/skyfall/firehose/message.rb +32 -13
- data/lib/skyfall/firehose/operation.rb +22 -17
- data/lib/skyfall/firehose/sync_message.rb +5 -0
- data/lib/skyfall/firehose.rb +2 -2
- data/lib/skyfall/jetstream/account_message.rb +4 -1
- data/lib/skyfall/jetstream/commit_message.rb +12 -2
- data/lib/skyfall/jetstream/identity_message.rb +3 -1
- data/lib/skyfall/jetstream/message.rb +37 -10
- data/lib/skyfall/jetstream/operation.rb +2 -10
- data/lib/skyfall/jetstream.rb +130 -44
- data/lib/skyfall/jetstream_v2/account_message.rb +41 -0
- data/lib/skyfall/jetstream_v2/commit_message.rb +51 -0
- data/lib/skyfall/jetstream_v2/identity_message.rb +37 -0
- data/lib/skyfall/jetstream_v2/info_message.rb +56 -0
- data/lib/skyfall/jetstream_v2/message.rb +213 -0
- data/lib/skyfall/jetstream_v2/sync_message.rb +64 -0
- data/lib/skyfall/jetstream_v2/unknown_message.rb +13 -0
- data/lib/skyfall/jetstream_v2.rb +257 -0
- data/lib/skyfall/version.rb +1 -1
- data/lib/skyfall.rb +2 -0
- metadata +31 -7
- data/sig/skyfall.rbs +0 -4
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'jetstream'
|
|
4
|
+
|
|
5
|
+
require 'net/http'
|
|
6
|
+
require 'uri'
|
|
7
|
+
|
|
8
|
+
module Skyfall
|
|
9
|
+
|
|
10
|
+
#
|
|
11
|
+
# Client of a Jetstream v2 service (JSON-based firehose with backfill capability).
|
|
12
|
+
#
|
|
13
|
+
# This is a subclass of {Skyfall::Jetstream}, customizing it for the version 2 API.
|
|
14
|
+
# Note: backfill API is not supported yet, only live streaming is implemented currently.
|
|
15
|
+
#
|
|
16
|
+
# A Jetstream v2 server should accept connections from both {Skyfall::Jetstream} and
|
|
17
|
+
# {Skyfall::JetstreamV2}, since it provides a backwards-compatible implementation of the
|
|
18
|
+
# v1 streaming API at the `/subscribe` endpoint; a Jetstream v1 server will only accept
|
|
19
|
+
# connections from {Skyfall::Jetstream}, and using {Skyfall::JetstreamV2} with a v1 server
|
|
20
|
+
# will result in an error.
|
|
21
|
+
#
|
|
22
|
+
# The message callback returns instances of subclasses of {Skyfall::JetstreamV2::Message}, which
|
|
23
|
+
# are generally equivalent, but independent from the respective {Skyfall::Jetstream::Message} variants
|
|
24
|
+
# due to the different structure of the JSON responses in the v2 API.
|
|
25
|
+
#
|
|
26
|
+
# Other differences from {Skyfall::Jetstream}:
|
|
27
|
+
#
|
|
28
|
+
# * the `kinds` option
|
|
29
|
+
# * `:sync` and `:info` message types
|
|
30
|
+
# * `msg.cursor` is always a sequential number
|
|
31
|
+
# * Jetstream v2 can also return protocol error frames, which are represented as {Skyfall::SubscriptionError}
|
|
32
|
+
#
|
|
33
|
+
# To connect to a Jetstream v2 websocket, you need to:
|
|
34
|
+
#
|
|
35
|
+
# * create an instance of {Skyfall::JetstreamV2}, passing it the hostname/URL of the server,
|
|
36
|
+
# and optionally parameters such as cursor or collection/DID/kinds filters
|
|
37
|
+
# * set up callbacks to be run when connecting, disconnecting, when a message is received etc.
|
|
38
|
+
# (you need to set at least a message handler)
|
|
39
|
+
# * call {#connect} to start the connection
|
|
40
|
+
# * handle the received messages
|
|
41
|
+
#
|
|
42
|
+
# Note: The Jetstream server starts streaming from the passed cursor *inclusively*,
|
|
43
|
+
# so the first event you receive will likely be one you've already processed before.
|
|
44
|
+
#
|
|
45
|
+
# @example
|
|
46
|
+
# client = Skyfall::JetstreamV2.new('jetstream.us-east.bsky.network', {
|
|
47
|
+
# collections: 'app.bsky.feed.post',
|
|
48
|
+
# dids: @dids
|
|
49
|
+
# })
|
|
50
|
+
#
|
|
51
|
+
# client.on_message do |msg|
|
|
52
|
+
# next unless msg.type == :commit
|
|
53
|
+
#
|
|
54
|
+
# op = msg.operation
|
|
55
|
+
#
|
|
56
|
+
# if op.type == :bsky_post && op.action == :create
|
|
57
|
+
# puts "[#{msg.time}] #{msg.repo}: #{op.raw_record['text']}"
|
|
58
|
+
# end
|
|
59
|
+
# end
|
|
60
|
+
#
|
|
61
|
+
# client.connect
|
|
62
|
+
#
|
|
63
|
+
# # You might also want to set some or all of these lifecycle callback handlers:
|
|
64
|
+
#
|
|
65
|
+
# client.on_connecting { |url| puts "Connecting to #{url}..." }
|
|
66
|
+
# client.on_connect { puts "Connected" }
|
|
67
|
+
# client.on_disconnect { puts "Disconnected" }
|
|
68
|
+
# client.on_reconnect { puts "Connection lost, trying to reconnect..." }
|
|
69
|
+
# client.on_timeout { puts "Connection stalled, triggering a reconnect..." }
|
|
70
|
+
# client.on_error { |e| puts "ERROR: #{e}" }
|
|
71
|
+
#
|
|
72
|
+
# @note Most of the methods of this class that you might want to use are defined in {Skyfall::Stream}.
|
|
73
|
+
#
|
|
74
|
+
|
|
75
|
+
class JetstreamV2 < Jetstream
|
|
76
|
+
|
|
77
|
+
# Current cursor (always a sequential number in the v2 API).
|
|
78
|
+
#
|
|
79
|
+
# @return [Integer, nil]
|
|
80
|
+
#
|
|
81
|
+
attr_accessor :cursor
|
|
82
|
+
|
|
83
|
+
#
|
|
84
|
+
# @param server [String] Address of the server to connect to.
|
|
85
|
+
# Expects a string with either just a hostname, or a ws:// or wss:// URL with no path.
|
|
86
|
+
# @param params [Hash] options, see below:
|
|
87
|
+
#
|
|
88
|
+
# @option params [Integer] :cursor
|
|
89
|
+
# cursor from which to resume (a sequential number)
|
|
90
|
+
#
|
|
91
|
+
# @option params [String, Symbol, Array<String, Symbol>] :kinds
|
|
92
|
+
# event kind filter to pass to the server; accepts one or more of `:commit`, `:identity`,
|
|
93
|
+
# `:account` or `:sync`. Omit the option to receive all event kinds.
|
|
94
|
+
#
|
|
95
|
+
# @option params [String, Array<String>] :dids
|
|
96
|
+
# DID filter to pass to the server (`:wanted_dids` or `:wantedDids` is also accepted);
|
|
97
|
+
# value should be a DID string or an array of those
|
|
98
|
+
#
|
|
99
|
+
# @option params [String, Symbol, Array<String, Symbol>] :collections
|
|
100
|
+
# collection filter to pass to the server (`:wanted_collections` or `:wantedCollections` is also accepted);
|
|
101
|
+
# value should be: an string with a concrete NSID or a prefix and wildcard, a symbol shorthand,
|
|
102
|
+
# or an array of those
|
|
103
|
+
#
|
|
104
|
+
# @option params [Integer, String] :max_message_size_bytes
|
|
105
|
+
# server-side message size filter (`:maxMessageSizeBytes` is also accepted);
|
|
106
|
+
# tells the server to skip events larger than the given number of bytes in size
|
|
107
|
+
# (0 is the default and means no limit). **Note:** When compression is enabled,
|
|
108
|
+
# Jetstream v2 servers compare this against the *uncompressed* message size, while
|
|
109
|
+
# Jetstream v1 servers count the *compressed* message size.
|
|
110
|
+
#
|
|
111
|
+
# @option params [Boolean] :compress
|
|
112
|
+
# enable zstd compression (default: false); this option is not passed to the server in the URL,
|
|
113
|
+
# but instead makes the client fetch the current Zstd compression dictionary first and then pass
|
|
114
|
+
# its ID to the server when connecting. The dictionary is cached in memory for this stream's
|
|
115
|
+
# subsequent connections.
|
|
116
|
+
#
|
|
117
|
+
# @raise [ArgumentError] if the server parameter or the options are invalid
|
|
118
|
+
#
|
|
119
|
+
def initialize(server, params = {})
|
|
120
|
+
require_relative 'jetstream_v2/message'
|
|
121
|
+
super(server, params)
|
|
122
|
+
|
|
123
|
+
collections = @params[:collections] || []
|
|
124
|
+
kinds = @params[:kinds] || []
|
|
125
|
+
|
|
126
|
+
if collections.length > 0 && kinds.length > 0 && !kinds.include?('commit')
|
|
127
|
+
raise ArgumentError, "Kinds filter must include :commit if collections filter is included"
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
protected
|
|
133
|
+
|
|
134
|
+
# Returns the full URL of the websocket endpoint to connect to.
|
|
135
|
+
# @return [String]
|
|
136
|
+
|
|
137
|
+
def build_websocket_url
|
|
138
|
+
params = @params.dup
|
|
139
|
+
params[:cursor] = @cursor if @cursor
|
|
140
|
+
|
|
141
|
+
if @compress
|
|
142
|
+
fetch_zstd_dictionary unless @compression_dictionary_id
|
|
143
|
+
params[:zstdDictionary] = @compression_dictionary_id
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
query = URI.encode_www_form(params)
|
|
147
|
+
|
|
148
|
+
@root_url + "/xrpc/network.bsky.jetstream.subscribeEvents" + (query.length > 0 ? "?#{query}" : '')
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# Processes a single message received from the websocket. Passes the received data to the
|
|
152
|
+
# {#on_raw_message} handler, builds a {Skyfall::JetstreamV2::Message} object, and passes it to
|
|
153
|
+
# the {#on_message} handler (if defined). Also updates the {#cursor} to this message's
|
|
154
|
+
# sequence number (note: this is skipped if {#on_message} is not set).
|
|
155
|
+
#
|
|
156
|
+
# @param msg
|
|
157
|
+
# {https://rubydoc.info/gems/faye-websocket/Faye/WebSocket/API/MessageEvent Faye::WebSocket::API::MessageEvent}
|
|
158
|
+
# @return [nil]
|
|
159
|
+
|
|
160
|
+
def handle_message(msg)
|
|
161
|
+
data = msg.data
|
|
162
|
+
@handlers[:raw_message]&.call(data)
|
|
163
|
+
|
|
164
|
+
if @handlers[:message]
|
|
165
|
+
jet_message = JetstreamV2::Message.new(decode_message_data(data))
|
|
166
|
+
@cursor = jet_message.cursor if jet_message.cursor
|
|
167
|
+
@handlers[:message].call(jet_message)
|
|
168
|
+
else
|
|
169
|
+
@cursor = nil
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# Returns the Zstd dictionary used for decompressing messages if :compress option is enabled.
|
|
174
|
+
#
|
|
175
|
+
# @return [Zstd::DDict]
|
|
176
|
+
# @raise [SubscriptionError] if the dictionary can't be fetched
|
|
177
|
+
# @raise [DecodeError] if the dictionary or its ID is invalid
|
|
178
|
+
|
|
179
|
+
def compression_dictionary
|
|
180
|
+
fetch_zstd_dictionary unless @compression_dictionary
|
|
181
|
+
@compression_dictionary
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
private
|
|
186
|
+
|
|
187
|
+
def check_option(k, v)
|
|
188
|
+
case k
|
|
189
|
+
when :collections, :wantedCollections
|
|
190
|
+
[:collections, check_wanted_collections(v)]
|
|
191
|
+
when :dids, :wantedDids
|
|
192
|
+
[:dids, check_wanted_dids(v)]
|
|
193
|
+
when :cursor
|
|
194
|
+
[:cursor, check_cursor(v)]
|
|
195
|
+
when :maxMessageSizeBytes
|
|
196
|
+
[:maxMessageSizeBytes, check_max_message_size_bytes(v)]
|
|
197
|
+
when :kinds
|
|
198
|
+
[:kinds, check_kinds(v)]
|
|
199
|
+
when :compress
|
|
200
|
+
[:compress, check_compress(v)]
|
|
201
|
+
when :requireHello
|
|
202
|
+
raise ArgumentError.new("The #{k.inspect} option does not exist in Jetstream v2")
|
|
203
|
+
else
|
|
204
|
+
raise ArgumentError.new("Unknown option: #{k.inspect}")
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def check_kinds(value)
|
|
209
|
+
raise ArgumentError, "Kinds filter must not be empty" if value.nil? || value == []
|
|
210
|
+
|
|
211
|
+
kinds = value.is_a?(Array) ? value : [value]
|
|
212
|
+
|
|
213
|
+
kinds = kinds.map { |kind|
|
|
214
|
+
unless (kind.is_a?(String) || kind.is_a?(Symbol)) && %w(commit identity account sync).include?(kind.to_s)
|
|
215
|
+
raise ArgumentError, "Invalid event kind: #{kind.inspect}"
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
kind.to_s
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
kinds.uniq
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
def fetch_zstd_dictionary
|
|
225
|
+
uri = URI(@root_url)
|
|
226
|
+
uri.scheme = (uri.scheme == 'wss') ? 'https' : 'http'
|
|
227
|
+
uri.path = '/xrpc/network.bsky.jetstream.getZstdDictionary'
|
|
228
|
+
uri = URI(uri.to_s)
|
|
229
|
+
|
|
230
|
+
request = Net::HTTP::Get.new(uri)
|
|
231
|
+
request['User-Agent'] = user_agent
|
|
232
|
+
|
|
233
|
+
opts = { use_ssl: uri.scheme == 'https', open_timeout: 10, read_timeout: 10 }
|
|
234
|
+
|
|
235
|
+
response = Net::HTTP.start(uri.host, uri.port, opts) { |http| http.request(request) }
|
|
236
|
+
|
|
237
|
+
unless response.is_a?(Net::HTTPSuccess)
|
|
238
|
+
raise DictionaryError, "Unable to fetch Jetstream zstd dictionary: HTTP #{response.code}"
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
dictionary_id = response['X-Zstd-Dictionary-Id']
|
|
242
|
+
|
|
243
|
+
unless dictionary_id && dictionary_id.match?(/\A[0-9]+\z/) && dictionary_id.to_i > 0
|
|
244
|
+
raise DictionaryError, "Invalid Jetstream zstd dictionary ID: #{dictionary_id.inspect}"
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
begin
|
|
248
|
+
dict = Zstd::DDict.new(response.body)
|
|
249
|
+
rescue RuntimeError => e
|
|
250
|
+
raise DictionaryError, "Invalid Jetstream zstd dictionary: #{e.message}"
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
@compression_dictionary_id = dictionary_id.to_i
|
|
254
|
+
@compression_dictionary = dict
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
end
|
data/lib/skyfall/version.rb
CHANGED
data/lib/skyfall.rb
CHANGED
|
@@ -4,4 +4,6 @@ require_relative 'skyfall/firehose'
|
|
|
4
4
|
require_relative 'skyfall/firehose/message'
|
|
5
5
|
require_relative 'skyfall/jetstream'
|
|
6
6
|
require_relative 'skyfall/jetstream/message'
|
|
7
|
+
require_relative 'skyfall/jetstream_v2'
|
|
8
|
+
require_relative 'skyfall/jetstream_v2/message'
|
|
7
9
|
require_relative 'skyfall/version'
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: skyfall
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.8.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Kuba Suder
|
|
@@ -47,16 +47,30 @@ dependencies:
|
|
|
47
47
|
name: oxygene
|
|
48
48
|
requirement: !ruby/object:Gem::Requirement
|
|
49
49
|
requirements:
|
|
50
|
-
- - "
|
|
50
|
+
- - "~>"
|
|
51
51
|
- !ruby/object:Gem::Version
|
|
52
|
-
version: 0.0
|
|
52
|
+
version: 0.1.0
|
|
53
53
|
type: :runtime
|
|
54
54
|
prerelease: false
|
|
55
55
|
version_requirements: !ruby/object:Gem::Requirement
|
|
56
56
|
requirements:
|
|
57
|
-
- - "
|
|
57
|
+
- - "~>"
|
|
58
|
+
- !ruby/object:Gem::Version
|
|
59
|
+
version: 0.1.0
|
|
60
|
+
- !ruby/object:Gem::Dependency
|
|
61
|
+
name: zstd-ruby
|
|
62
|
+
requirement: !ruby/object:Gem::Requirement
|
|
63
|
+
requirements:
|
|
64
|
+
- - "~>"
|
|
65
|
+
- !ruby/object:Gem::Version
|
|
66
|
+
version: '2.0'
|
|
67
|
+
type: :runtime
|
|
68
|
+
prerelease: false
|
|
69
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
70
|
+
requirements:
|
|
71
|
+
- - "~>"
|
|
58
72
|
- !ruby/object:Gem::Version
|
|
59
|
-
version:
|
|
73
|
+
version: '2.0'
|
|
60
74
|
description: "\n Skyfall is a Ruby gem for connecting to the \"firehose\" of the
|
|
61
75
|
AT Protocol network that Bluesky is built on,\n i.e. a websocket which streams
|
|
62
76
|
all new posts and everything else happening on the network in real time. The code\n
|
|
@@ -73,6 +87,8 @@ files:
|
|
|
73
87
|
- CHANGELOG.md
|
|
74
88
|
- LICENSE.txt
|
|
75
89
|
- README.md
|
|
90
|
+
- SECURITY.md
|
|
91
|
+
- data/jetstream_zstd_dictionary
|
|
76
92
|
- lib/skyfall.rb
|
|
77
93
|
- lib/skyfall/collection.rb
|
|
78
94
|
- lib/skyfall/errors.rb
|
|
@@ -94,10 +110,17 @@ files:
|
|
|
94
110
|
- lib/skyfall/jetstream/message.rb
|
|
95
111
|
- lib/skyfall/jetstream/operation.rb
|
|
96
112
|
- lib/skyfall/jetstream/unknown_message.rb
|
|
113
|
+
- lib/skyfall/jetstream_v2.rb
|
|
114
|
+
- lib/skyfall/jetstream_v2/account_message.rb
|
|
115
|
+
- lib/skyfall/jetstream_v2/commit_message.rb
|
|
116
|
+
- lib/skyfall/jetstream_v2/identity_message.rb
|
|
117
|
+
- lib/skyfall/jetstream_v2/info_message.rb
|
|
118
|
+
- lib/skyfall/jetstream_v2/message.rb
|
|
119
|
+
- lib/skyfall/jetstream_v2/sync_message.rb
|
|
120
|
+
- lib/skyfall/jetstream_v2/unknown_message.rb
|
|
97
121
|
- lib/skyfall/label.rb
|
|
98
122
|
- lib/skyfall/stream.rb
|
|
99
123
|
- lib/skyfall/version.rb
|
|
100
|
-
- sig/skyfall.rbs
|
|
101
124
|
homepage: https://ruby.sdk.blue
|
|
102
125
|
licenses:
|
|
103
126
|
- Zlib
|
|
@@ -105,6 +128,7 @@ metadata:
|
|
|
105
128
|
bug_tracker_uri: https://tangled.org/mackuba.eu/skyfall/issues
|
|
106
129
|
changelog_uri: https://tangled.org/mackuba.eu/skyfall/blob/master/CHANGELOG.md
|
|
107
130
|
source_code_uri: https://tangled.org/mackuba.eu/skyfall
|
|
131
|
+
rubygems_mfa_required: 'true'
|
|
108
132
|
rdoc_options: []
|
|
109
133
|
require_paths:
|
|
110
134
|
- lib
|
|
@@ -119,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
119
143
|
- !ruby/object:Gem::Version
|
|
120
144
|
version: '0'
|
|
121
145
|
requirements: []
|
|
122
|
-
rubygems_version:
|
|
146
|
+
rubygems_version: 4.0.23
|
|
123
147
|
specification_version: 4
|
|
124
148
|
summary: A Ruby gem for streaming data from the Bluesky/ATProto firehose
|
|
125
149
|
test_files: []
|
data/sig/skyfall.rbs
DELETED