skyfall 0.7.2 → 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 +45 -22
- 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/info_message.rb +1 -1
- data/lib/skyfall/firehose/message.rb +23 -6
- data/lib/skyfall/firehose/operation.rb +8 -8
- 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 +10 -2
- data/lib/skyfall/jetstream/identity_message.rb +3 -1
- data/lib/skyfall/jetstream/message.rb +35 -10
- data/lib/skyfall/jetstream/operation.rb +1 -1
- 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 +27 -3
- data/sig/skyfall.rbs +0 -4
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'message'
|
|
4
|
+
|
|
5
|
+
module Skyfall
|
|
6
|
+
|
|
7
|
+
#
|
|
8
|
+
# An informational message from the Jetstream service itself, unrelated to any repos.
|
|
9
|
+
#
|
|
10
|
+
# Currently there are two types of info messages defined:
|
|
11
|
+
#
|
|
12
|
+
# * `"OutdatedCursor"`, sent when the client connects with a timestamp cursor that is older than
|
|
13
|
+
# the oldest event currently kept in the backfill buffer, which means that you're likely missing
|
|
14
|
+
# some events that were sent since the last time the client was connected but which were already
|
|
15
|
+
# deleted from the buffer
|
|
16
|
+
# * `"FutureCursor"`, sent when the client connects with a seq cursor that is higher than the
|
|
17
|
+
# current latest cursor on the server
|
|
18
|
+
#
|
|
19
|
+
# Note: the {#did}, {#seq}, {#time} and {#time_us} properties are always `nil` for `#info` messages.
|
|
20
|
+
#
|
|
21
|
+
|
|
22
|
+
class JetstreamV2::InfoMessage < JetstreamV2::Message
|
|
23
|
+
|
|
24
|
+
# @return [String] short machine-readable code of the info message
|
|
25
|
+
attr_reader :name
|
|
26
|
+
|
|
27
|
+
# @return [String, nil] a human-readable description
|
|
28
|
+
attr_reader :message
|
|
29
|
+
|
|
30
|
+
# Message which means that the timestamp cursor passed when connecting is older than the oldest event
|
|
31
|
+
# currently kept in the backfill buffer, and that you've likely missed some events that have
|
|
32
|
+
# already been deleted.
|
|
33
|
+
OUTDATED_CURSOR = 'OutdatedCursor'
|
|
34
|
+
|
|
35
|
+
# Message which means that the seq cursor passed when connecting is higher than the
|
|
36
|
+
# current latest cursor on the server.
|
|
37
|
+
FUTURE_CURSOR = 'FutureCursor'
|
|
38
|
+
|
|
39
|
+
#
|
|
40
|
+
# @param json [Hash] message JSON decoded from the websocket message
|
|
41
|
+
# @raise [DecodeError] if the message doesn't include required data
|
|
42
|
+
#
|
|
43
|
+
def initialize(json)
|
|
44
|
+
super
|
|
45
|
+
check_if_not_nil 'name'
|
|
46
|
+
|
|
47
|
+
@name = @payload['name']
|
|
48
|
+
@message = @payload['message']
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# @return [String] a formatted summary
|
|
52
|
+
def to_s
|
|
53
|
+
@message ? "#{@name}: #{@message}" : @name
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../errors'
|
|
4
|
+
require_relative '../jetstream_v2'
|
|
5
|
+
|
|
6
|
+
require 'json'
|
|
7
|
+
require 'time'
|
|
8
|
+
|
|
9
|
+
module Skyfall
|
|
10
|
+
|
|
11
|
+
# @abstract
|
|
12
|
+
# Abstract base class representing a Jetstream v2 message.
|
|
13
|
+
#
|
|
14
|
+
# Actual messages are returned as instances of one of the subclasses of this class,
|
|
15
|
+
# depending on the type of message, most commonly as {Skyfall::JetstreamV2::CommitMessage}.
|
|
16
|
+
#
|
|
17
|
+
# The {new} method is overridden here so that it can be called with a JSON message from
|
|
18
|
+
# the websocket, and it parses the type from the JSON and builds an instance of a matching
|
|
19
|
+
# subclass.
|
|
20
|
+
#
|
|
21
|
+
# You normally don't need to call this class directly, unless you're building a custom
|
|
22
|
+
# subclass of {Skyfall::Stream} or reading raw data packets from the websocket through
|
|
23
|
+
# the {Skyfall::Stream#on_raw_message} event handler.
|
|
24
|
+
|
|
25
|
+
class JetstreamV2::Message
|
|
26
|
+
|
|
27
|
+
# Type of the message (e.g. `:commit`, `:identity` etc.)
|
|
28
|
+
# @return [Symbol]
|
|
29
|
+
attr_reader :type
|
|
30
|
+
|
|
31
|
+
# DID of the account (repo) that the event is sent by
|
|
32
|
+
# @return [String, nil]
|
|
33
|
+
attr_reader :did
|
|
34
|
+
|
|
35
|
+
# Cursor value of the event to be used when reconnecting (a sequential number).
|
|
36
|
+
# @return [Integer, nil]
|
|
37
|
+
attr_reader :seq
|
|
38
|
+
|
|
39
|
+
alias cursor seq
|
|
40
|
+
alias repo did
|
|
41
|
+
alias kind type
|
|
42
|
+
|
|
43
|
+
# The raw JSON of the message as parsed from the websocket packet.
|
|
44
|
+
# @return [Hash]
|
|
45
|
+
attr_reader :json
|
|
46
|
+
|
|
47
|
+
#
|
|
48
|
+
# Parses the JSON data from a websocket message and returns an instance of an appropriate subclass.
|
|
49
|
+
#
|
|
50
|
+
# {Skyfall::JetstreamV2::UnknownMessage} is returned if the message type is not recognized.
|
|
51
|
+
#
|
|
52
|
+
# @param data [String] plain text payload of a Jetstream websocket message
|
|
53
|
+
# @return [Skyfall::JetstreamV2::Message]
|
|
54
|
+
# @raise [Skyfall::DecodeError] if the message is malformed or doesn't include required data
|
|
55
|
+
# @raise [Skyfall::SubscriptionError] if the data contains an error message from the server
|
|
56
|
+
#
|
|
57
|
+
def self.new(data)
|
|
58
|
+
json = JSON.parse(data)
|
|
59
|
+
raise DecodeError, "Expected a JSON object" unless json.is_a?(Hash)
|
|
60
|
+
|
|
61
|
+
if json['$type'] == 'error'
|
|
62
|
+
raise SubscriptionError.new(json['error'], json['message'])
|
|
63
|
+
elsif json['$type'] != 'message'
|
|
64
|
+
raise DecodeError, "Unknown Jetstream v2 frame type: #{json['$type'].inspect}"
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
raise DecodeError, "Missing message payload" if json['payload'].nil?
|
|
68
|
+
raise DecodeError, "Invalid message payload" unless json['payload'].is_a?(Hash)
|
|
69
|
+
|
|
70
|
+
event_type = json['payload']['$type']
|
|
71
|
+
raise DecodeError, "Invalid event type: #{event_type.inspect}" unless event_type.is_a?(String)
|
|
72
|
+
|
|
73
|
+
parts = event_type.split('#')
|
|
74
|
+
prefix = 'network.bsky.jetstream.subscribeEvents'
|
|
75
|
+
raise DecodeError, "Invalid event type: #{event_type.inspect}" unless parts.length == 2 && parts[0] == prefix
|
|
76
|
+
|
|
77
|
+
kind = parts[1]
|
|
78
|
+
|
|
79
|
+
message_class = case kind
|
|
80
|
+
when 'account' then JetstreamV2::AccountMessage
|
|
81
|
+
when 'commit' then JetstreamV2::CommitMessage
|
|
82
|
+
when 'identity' then JetstreamV2::IdentityMessage
|
|
83
|
+
when 'sync' then JetstreamV2::SyncMessage
|
|
84
|
+
when 'info' then JetstreamV2::InfoMessage
|
|
85
|
+
else JetstreamV2::UnknownMessage
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
if self != JetstreamV2::Message && self != message_class
|
|
89
|
+
expected_type = self.name.split('::').last.gsub(/Message$/, '').downcase
|
|
90
|
+
raise DecodeError, "Expected '#{expected_type}' message, got '#{kind}'"
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
message = message_class.allocate
|
|
94
|
+
message.send(:initialize, json)
|
|
95
|
+
message
|
|
96
|
+
rescue JSON::ParserError => e
|
|
97
|
+
raise DecodeError, "Invalid JSON message: #{e.message}"
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
#
|
|
101
|
+
# @param json [Hash] message JSON decoded from the websocket message
|
|
102
|
+
# @raise [DecodeError] if the message doesn't include required data
|
|
103
|
+
#
|
|
104
|
+
def initialize(json)
|
|
105
|
+
@json = json
|
|
106
|
+
@payload = json['payload']
|
|
107
|
+
|
|
108
|
+
@type = @payload['$type'].split('#')[1].to_sym
|
|
109
|
+
@did = @payload['did']
|
|
110
|
+
@seq = @payload['seq']
|
|
111
|
+
|
|
112
|
+
unless @type == :info
|
|
113
|
+
check_if_not_nil 'did', 'seq', 'time'
|
|
114
|
+
raise DecodeError, "Invalid event sequence number" unless @seq.is_a?(Integer)
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
#
|
|
119
|
+
# @return [Boolean] true if the message is {JetstreamV2::UnknownMessage} (of unrecognized type)
|
|
120
|
+
#
|
|
121
|
+
def unknown?
|
|
122
|
+
self.is_a?(JetstreamV2::UnknownMessage)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Returns a record operation included in the message. Only `:commit` messages include
|
|
126
|
+
# operations, but for convenience the method is declared here and returns nil in other messages.
|
|
127
|
+
#
|
|
128
|
+
# @return [nil]
|
|
129
|
+
#
|
|
130
|
+
def operation
|
|
131
|
+
nil
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
alias op operation
|
|
135
|
+
|
|
136
|
+
# List of operations on records included in the message. Only `:commit` messages include
|
|
137
|
+
# an operation and only one, but for symmetry with {Skyfall::Firehose::Message} the array method is
|
|
138
|
+
# also included here and returns either a one-item array in commit messages or an empty array in others.
|
|
139
|
+
#
|
|
140
|
+
# @return [Array<JetstreamV2::Operation>]
|
|
141
|
+
#
|
|
142
|
+
def operations
|
|
143
|
+
[]
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
alias ops operations
|
|
147
|
+
|
|
148
|
+
# Timestamp decoded from the message.
|
|
149
|
+
#
|
|
150
|
+
# Note: this timestamp represents the time when the message was received and stored by Jetstream,
|
|
151
|
+
# which might differ a lot from the `created_at` time saved in the record data, e.g. if user's
|
|
152
|
+
# local time is set incorrectly or if an archive of existing posts was imported from another
|
|
153
|
+
# platform. It will also differ (usually only slightly) from the timestamp of the original CBOR
|
|
154
|
+
# message emitted from the PDS and passed through the relay.
|
|
155
|
+
#
|
|
156
|
+
# @return [Time, nil]
|
|
157
|
+
#
|
|
158
|
+
def time
|
|
159
|
+
@time ||= parse_time(@payload['time'])
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# Timestamp of the event in Unix microseconds (see {#time}).
|
|
163
|
+
#
|
|
164
|
+
# Unlike in Jetstream v1, the standard cursor used in Jetstream v2 API is the {#seq} / {#cursor}
|
|
165
|
+
# value, which is a sequential number like in a CBOR firehose, although the API also accepts
|
|
166
|
+
# a microsecond timestamp returned from this method as a cursor when reconnecting.
|
|
167
|
+
#
|
|
168
|
+
# @return [Integer, nil]
|
|
169
|
+
#
|
|
170
|
+
def time_us
|
|
171
|
+
time && (time.to_r * 1_000_000).to_i
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# The `witnessed_at` timestamp of when the event was stored on the Jetstream server. Currently,
|
|
175
|
+
# this is always the same as {#time} (may be nil on Jetstream servers running older versions of v2).
|
|
176
|
+
#
|
|
177
|
+
# @return [Time, nil]
|
|
178
|
+
#
|
|
179
|
+
def witnessed_at
|
|
180
|
+
@witnessed_at ||= parse_time(@payload['witnessedAt'])
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
private
|
|
185
|
+
|
|
186
|
+
def check_if_not_nil(*fields)
|
|
187
|
+
fields.each do |field|
|
|
188
|
+
raise DecodeError, "Missing event details (#{field})" if @payload[field].nil?
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def parse_time(timestamp)
|
|
193
|
+
timestamp && Time.iso8601(timestamp)
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# Much faster version for Ruby 3.2+
|
|
197
|
+
|
|
198
|
+
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.2')
|
|
199
|
+
def parse_time(timestamp)
|
|
200
|
+
timestamp && Time.new(timestamp)
|
|
201
|
+
end
|
|
202
|
+
end
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# need to be at the end because of a circular dependency
|
|
207
|
+
|
|
208
|
+
require_relative 'account_message'
|
|
209
|
+
require_relative 'commit_message'
|
|
210
|
+
require_relative 'identity_message'
|
|
211
|
+
require_relative 'sync_message'
|
|
212
|
+
require_relative 'info_message'
|
|
213
|
+
require_relative 'unknown_message'
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'message'
|
|
4
|
+
require 'base64'
|
|
5
|
+
require 'oxygene'
|
|
6
|
+
|
|
7
|
+
module Skyfall
|
|
8
|
+
|
|
9
|
+
#
|
|
10
|
+
# Firehose message which declares the current state of the repository. The message is meant to
|
|
11
|
+
# trigger a resynchronization of the repository from a receiving consumer, if the consumer detects
|
|
12
|
+
# from the message rev that it must have missed some events from that repository.
|
|
13
|
+
#
|
|
14
|
+
# The sync message can be emitted by a PDS or relay to force a repair of a broken account state,
|
|
15
|
+
# or e.g. when an account is created, migrated or recovered from a CAR backup.
|
|
16
|
+
#
|
|
17
|
+
|
|
18
|
+
class JetstreamV2::SyncMessage < JetstreamV2::Message
|
|
19
|
+
|
|
20
|
+
#
|
|
21
|
+
# @param json [Hash] message JSON decoded from the websocket message
|
|
22
|
+
# @raise [DecodeError] if the message doesn't include required data
|
|
23
|
+
#
|
|
24
|
+
def initialize(json)
|
|
25
|
+
super
|
|
26
|
+
|
|
27
|
+
sync = @payload['sync']
|
|
28
|
+
|
|
29
|
+
raise DecodeError, "Missing sync object" if sync.nil?
|
|
30
|
+
raise DecodeError, "Invalid sync object" unless sync.is_a?(Hash)
|
|
31
|
+
|
|
32
|
+
raise DecodeError, "Missing event details (sync.rev)" if sync['rev'].nil?
|
|
33
|
+
raise DecodeError, "Invalid sync.rev field" unless sync['rev'].is_a?(String)
|
|
34
|
+
|
|
35
|
+
raise DecodeError, "Missing event details (sync.blocks)" if sync['blocks'].nil?
|
|
36
|
+
raise DecodeError, "Invalid sync.blocks field" unless sync['blocks'].is_a?(Hash)
|
|
37
|
+
|
|
38
|
+
raise DecodeError, "Missing event details (sync.blocks.$bytes)" if sync['blocks']['$bytes'].nil?
|
|
39
|
+
raise DecodeError, "Invalid sync.blocks.$bytes field" unless sync['blocks']['$bytes'].is_a?(String)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# @return [String] current revision of the repo
|
|
43
|
+
def rev
|
|
44
|
+
@payload['sync']['rev']
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# @return [Oxygene::CARArchive] commit data in the form of a parsed CAR archive
|
|
48
|
+
#
|
|
49
|
+
# @raise [Oxygene::DecodeError] if the archive header has missing or invalid fields
|
|
50
|
+
# @raise [Oxygene::UnsupportedError] if the archive uses an unsupported CAR version
|
|
51
|
+
# @raise [DecodeError] if the blocks data is not encoded with valid Base64
|
|
52
|
+
#
|
|
53
|
+
def blocks
|
|
54
|
+
return @blocks if @blocks
|
|
55
|
+
|
|
56
|
+
encoded_bytes = @payload['sync']['blocks']['$bytes']
|
|
57
|
+
padded_bytes = encoded_bytes + '=' * ((4 - encoded_bytes.length % 4) % 4)
|
|
58
|
+
|
|
59
|
+
@blocks = Oxygene::CARArchive.new(Base64.strict_decode64(padded_bytes))
|
|
60
|
+
rescue ArgumentError => e
|
|
61
|
+
raise DecodeError, "Invalid sync blocks: #{e.message}"
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -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
|
|
@@ -57,6 +57,20 @@ dependencies:
|
|
|
57
57
|
- - "~>"
|
|
58
58
|
- !ruby/object:Gem::Version
|
|
59
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
|
+
- - "~>"
|
|
72
|
+
- !ruby/object:Gem::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: []
|