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
|
@@ -28,11 +28,11 @@ module Skyfall
|
|
|
28
28
|
|
|
29
29
|
#
|
|
30
30
|
# @param message [Skyfall::Firehose::Message] commit message the operation is included in
|
|
31
|
-
# @param
|
|
31
|
+
# @param data [Hash] operation data
|
|
32
32
|
#
|
|
33
|
-
def initialize(message,
|
|
33
|
+
def initialize(message, data)
|
|
34
34
|
@message = message
|
|
35
|
-
@
|
|
35
|
+
@data = data
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
# @return [String] DID of the account/repository in which the operation happened
|
|
@@ -44,18 +44,18 @@ module Skyfall
|
|
|
44
44
|
|
|
45
45
|
# @return [String] path part of the record URI (collection + rkey)
|
|
46
46
|
def path
|
|
47
|
-
@
|
|
47
|
+
@data['path']
|
|
48
48
|
end
|
|
49
49
|
|
|
50
50
|
# @return [Symbol] type of the operation (`:create`, `:update` or `:delete`)
|
|
51
51
|
def action
|
|
52
|
-
@action ||= @
|
|
52
|
+
@action ||= @data['action'].to_sym
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
# @return [String] record collection NSID
|
|
56
56
|
def collection
|
|
57
57
|
@collection ||= begin
|
|
58
|
-
path = @
|
|
58
|
+
path = @data['path']
|
|
59
59
|
slash = path.index('/')
|
|
60
60
|
raise DecodeError, "Path doesn't contain a /: #{path}" if slash.nil?
|
|
61
61
|
|
|
@@ -66,7 +66,7 @@ module Skyfall
|
|
|
66
66
|
# @return [String] record rkey
|
|
67
67
|
def rkey
|
|
68
68
|
@rkey ||= begin
|
|
69
|
-
path = @
|
|
69
|
+
path = @data['path']
|
|
70
70
|
slash = path.index('/')
|
|
71
71
|
raise DecodeError, "Path doesn't contain a /: #{path}" if slash.nil?
|
|
72
72
|
|
|
@@ -81,7 +81,7 @@ module Skyfall
|
|
|
81
81
|
|
|
82
82
|
# @return [Oxygene::CID, nil] CID (Content Identifier) of the record (nil for delete operations)
|
|
83
83
|
def cid
|
|
84
|
-
@cid ||= @
|
|
84
|
+
@cid ||= @data['cid'] && Oxygene::CID.from_cbor_tag(@data['cid'])
|
|
85
85
|
end
|
|
86
86
|
|
|
87
87
|
# @return [Hash, nil] record data as a plain Ruby Hash (nil for delete operations)
|
|
@@ -28,11 +28,16 @@ module Skyfall
|
|
|
28
28
|
check_if_not_nil 'seq', 'did', 'blocks', 'rev', 'time'
|
|
29
29
|
end
|
|
30
30
|
|
|
31
|
+
# @return [String] current revision of the repo
|
|
31
32
|
def rev
|
|
32
33
|
@rev ||= @data_object['rev']
|
|
33
34
|
end
|
|
34
35
|
|
|
35
36
|
# @return [Oxygene::CARArchive] commit data in the form of a parsed CAR archive
|
|
37
|
+
#
|
|
38
|
+
# @raise [Oxygene::DecodeError] if the archive header has missing or invalid fields
|
|
39
|
+
# @raise [Oxygene::UnsupportedError] if the archive uses an unsupported CAR version
|
|
40
|
+
#
|
|
36
41
|
def blocks
|
|
37
42
|
@blocks ||= Oxygene::CARArchive.new(@data_object['blocks'])
|
|
38
43
|
end
|
data/lib/skyfall/firehose.rb
CHANGED
|
@@ -129,8 +129,8 @@ module Skyfall
|
|
|
129
129
|
@handlers[:raw_message]&.call(data)
|
|
130
130
|
|
|
131
131
|
if @handlers[:message]
|
|
132
|
-
atp_message = Message.new(data)
|
|
133
|
-
@cursor = atp_message.
|
|
132
|
+
atp_message = Firehose::Message.new(data)
|
|
133
|
+
@cursor = atp_message.cursor if atp_message.cursor
|
|
134
134
|
@handlers[:message].call(atp_message)
|
|
135
135
|
else
|
|
136
136
|
@cursor = nil
|
|
@@ -22,7 +22,10 @@ module Skyfall
|
|
|
22
22
|
# @raise [DecodeError] if the message doesn't include required data
|
|
23
23
|
#
|
|
24
24
|
def initialize(json)
|
|
25
|
-
raise DecodeError.new("Missing
|
|
25
|
+
raise DecodeError.new("Missing account object") if json['account'].nil?
|
|
26
|
+
raise DecodeError.new("Invalid account object") unless json['account'].is_a?(Hash)
|
|
27
|
+
raise DecodeError.new("Missing event details (account.active)") if json['account']['active'].nil?
|
|
28
|
+
|
|
26
29
|
super
|
|
27
30
|
end
|
|
28
31
|
|
|
@@ -20,13 +20,21 @@ module Skyfall
|
|
|
20
20
|
# @raise [DecodeError] if the message doesn't include required data
|
|
21
21
|
#
|
|
22
22
|
def initialize(json)
|
|
23
|
-
raise DecodeError.new("Missing
|
|
23
|
+
raise DecodeError.new("Missing commit object") if json['commit'].nil?
|
|
24
|
+
raise DecodeError.new("Invalid commit object") unless json['commit'].is_a?(Hash)
|
|
24
25
|
|
|
25
|
-
%w(collection rkey operation).each
|
|
26
|
+
%w(collection rkey operation rev).each do |f|
|
|
27
|
+
raise DecodeError.new("Missing event details (commit.#{f})") if json['commit'][f].nil?
|
|
28
|
+
end
|
|
26
29
|
|
|
27
30
|
super
|
|
28
31
|
end
|
|
29
32
|
|
|
33
|
+
# @return [String] current revision of the repo
|
|
34
|
+
def rev
|
|
35
|
+
json['commit']['rev']
|
|
36
|
+
end
|
|
37
|
+
|
|
30
38
|
# Returns the record operation included in the commit.
|
|
31
39
|
# @return [Jetstream::Operation]
|
|
32
40
|
#
|
|
@@ -24,7 +24,9 @@ module Skyfall
|
|
|
24
24
|
# @raise [DecodeError] if the message doesn't include required data
|
|
25
25
|
#
|
|
26
26
|
def initialize(json)
|
|
27
|
-
raise DecodeError.new("Missing
|
|
27
|
+
raise DecodeError.new("Missing identity object") if json['identity'].nil?
|
|
28
|
+
raise DecodeError.new("Invalid identity object") unless json['identity'].is_a?(Hash)
|
|
29
|
+
|
|
28
30
|
super
|
|
29
31
|
end
|
|
30
32
|
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require_relative '../errors'
|
|
4
4
|
require_relative '../jetstream'
|
|
5
5
|
|
|
6
|
+
require 'json'
|
|
6
7
|
require 'time'
|
|
7
8
|
|
|
8
9
|
module Skyfall
|
|
@@ -31,19 +32,41 @@ module Skyfall
|
|
|
31
32
|
# @return [String]
|
|
32
33
|
attr_reader :did
|
|
33
34
|
|
|
34
|
-
# Server timestamp of the message
|
|
35
|
-
#
|
|
36
|
-
#
|
|
35
|
+
# Server timestamp of the message in Unix microseconds.
|
|
36
|
+
#
|
|
37
|
+
# This field generally serves as a cursor when reconnecting in Jetstream v1; however,
|
|
38
|
+
# when connecting to v2 sources through the v1 compatibility API, an additional `cursor`
|
|
39
|
+
# field is added which holds a sequential number, and that value is used instead as the cursor.
|
|
40
|
+
# Use {#seq} or {#cursor} to always get the recommended value to use for reconnecting
|
|
41
|
+
# (although the v1 compatibility API accepts both a seq cursor and a time_us cursor).
|
|
42
|
+
#
|
|
37
43
|
# @return [Integer]
|
|
44
|
+
#
|
|
38
45
|
attr_reader :time_us
|
|
39
46
|
|
|
40
47
|
alias repo did
|
|
41
|
-
alias seq time_us
|
|
42
48
|
alias kind type
|
|
43
49
|
|
|
44
50
|
# The raw JSON of the message as parsed from the websocket packet.
|
|
51
|
+
# @return [Hash]
|
|
45
52
|
attr_reader :json
|
|
46
53
|
|
|
54
|
+
# Cursor value of the event to be used when reconnecting.
|
|
55
|
+
#
|
|
56
|
+
# Normally in Jetstream v1, the cursor is the {#time_us} field. However, when connecting
|
|
57
|
+
# to a Jetstream v2 service through the v1 compatibility API, it includes an additional
|
|
58
|
+
# `cursor` field with a sequential number, so in this case that value is returned here instead.
|
|
59
|
+
# The v1 compatibility API accepts both the seq cursor and the {#time_us} value as a cursor
|
|
60
|
+
# when reconnecting.
|
|
61
|
+
#
|
|
62
|
+
# @return [Integer]
|
|
63
|
+
#
|
|
64
|
+
def seq
|
|
65
|
+
@json['cursor'] || time_us
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
alias cursor seq
|
|
69
|
+
|
|
47
70
|
#
|
|
48
71
|
# Parses the JSON data from a websocket message and returns an instance of an appropriate subclass.
|
|
49
72
|
#
|
|
@@ -51,10 +74,11 @@ module Skyfall
|
|
|
51
74
|
#
|
|
52
75
|
# @param data [String] plain text payload of a Jetstream websocket message
|
|
53
76
|
# @return [Skyfall::Jetstream::Message]
|
|
54
|
-
# @raise [DecodeError] if the message doesn't include required data
|
|
77
|
+
# @raise [DecodeError] if the message is malformed or doesn't include required data
|
|
55
78
|
#
|
|
56
79
|
def self.new(data)
|
|
57
80
|
json = JSON.parse(data)
|
|
81
|
+
raise DecodeError, "Expected a JSON object" unless json.is_a?(Hash)
|
|
58
82
|
|
|
59
83
|
message_class = case json['kind']
|
|
60
84
|
when 'account' then Jetstream::AccountMessage
|
|
@@ -71,6 +95,8 @@ module Skyfall
|
|
|
71
95
|
message = message_class.allocate
|
|
72
96
|
message.send(:initialize, json)
|
|
73
97
|
message
|
|
98
|
+
rescue JSON::ParserError => e
|
|
99
|
+
raise DecodeError, "Invalid JSON message: #{e.message}"
|
|
74
100
|
end
|
|
75
101
|
|
|
76
102
|
#
|
|
@@ -105,8 +131,8 @@ module Skyfall
|
|
|
105
131
|
alias op operation
|
|
106
132
|
|
|
107
133
|
# List of operations on records included in the message. Only `:commit` messages include
|
|
108
|
-
#
|
|
109
|
-
# in
|
|
134
|
+
# an operation and only one, but for symmetry with {Skyfall::Firehose::Message} the array method is
|
|
135
|
+
# also included here and returns either a one-item array in commit messages or an empty array in others.
|
|
110
136
|
#
|
|
111
137
|
# @return [Array<Jetstream::Operation>]
|
|
112
138
|
#
|
|
@@ -120,8 +146,7 @@ module Skyfall
|
|
|
120
146
|
# Timestamp decoded from the message.
|
|
121
147
|
#
|
|
122
148
|
# Note: the time is read from the {#time_us} field, which stores the event time as an integer in
|
|
123
|
-
# Unix time microseconds
|
|
124
|
-
# in CBOR firehose messages. This timestamp represents the time when the message was received
|
|
149
|
+
# Unix time microseconds. This timestamp represents the time when the message was received
|
|
125
150
|
# and stored by Jetstream, which might differ a lot from the `created_at` time saved in the
|
|
126
151
|
# record data, e.g. if user's local time is set incorrectly or if an archive of existing posts
|
|
127
152
|
# was imported from another platform. It will also differ (usually only slightly) from the
|
|
@@ -130,7 +155,7 @@ module Skyfall
|
|
|
130
155
|
# @return [Time]
|
|
131
156
|
#
|
|
132
157
|
def time
|
|
133
|
-
@time ||= Time.at(@time_us / 1_000_000
|
|
158
|
+
@time ||= Time.at(@time_us / 1_000_000, @time_us % 1_000_000)
|
|
134
159
|
end
|
|
135
160
|
end
|
|
136
161
|
end
|
|
@@ -63,7 +63,7 @@ module Skyfall
|
|
|
63
63
|
|
|
64
64
|
# @return [String] full AT URI of the record
|
|
65
65
|
def uri
|
|
66
|
-
"at://#{repo}/#{collection}/#{rkey}"
|
|
66
|
+
@uri ||= "at://#{repo}/#{collection}/#{rkey}"
|
|
67
67
|
end
|
|
68
68
|
|
|
69
69
|
# @return [Oxygene::CID, nil] CID (Content Identifier) of the record (nil for delete operations)
|
data/lib/skyfall/jetstream.rb
CHANGED
|
@@ -5,6 +5,7 @@ require_relative 'stream'
|
|
|
5
5
|
require 'json'
|
|
6
6
|
require 'time'
|
|
7
7
|
require 'uri'
|
|
8
|
+
require 'zstd-ruby'
|
|
8
9
|
|
|
9
10
|
module Skyfall
|
|
10
11
|
|
|
@@ -18,15 +19,18 @@ module Skyfall
|
|
|
18
19
|
#
|
|
19
20
|
# To connect to a Jetstream websocket, you need to:
|
|
20
21
|
#
|
|
21
|
-
# * create an instance of Jetstream, passing it the hostname/URL of the server,
|
|
22
|
-
# parameters such as cursor or collection/DID filters
|
|
22
|
+
# * create an instance of {Skyfall::Jetstream}, passing it the hostname/URL of the server,
|
|
23
|
+
# and optionally parameters such as cursor or collection/DID filters
|
|
23
24
|
# * set up callbacks to be run when connecting, disconnecting, when a message is received etc.
|
|
24
25
|
# (you need to set at least a message handler)
|
|
25
26
|
# * call {#connect} to start the connection
|
|
26
27
|
# * handle the received messages
|
|
27
28
|
#
|
|
29
|
+
# Note: The Jetstream server starts streaming from the passed cursor *inclusively*,
|
|
30
|
+
# so the first event you receive will likely be one you've already processed before.
|
|
31
|
+
#
|
|
28
32
|
# @example
|
|
29
|
-
# client = Skyfall::Jetstream.new('
|
|
33
|
+
# client = Skyfall::Jetstream.new('frankfurt.firehose.stream', {
|
|
30
34
|
# wanted_collections: 'app.bsky.feed.post',
|
|
31
35
|
# wanted_dids: @dids
|
|
32
36
|
# })
|
|
@@ -57,8 +61,17 @@ module Skyfall
|
|
|
57
61
|
|
|
58
62
|
class Jetstream < Stream
|
|
59
63
|
|
|
60
|
-
#
|
|
64
|
+
# Maximum allowed value for :maxMessageSizeBytes (max value of uint32).
|
|
65
|
+
MAX_MAX_BYTES = 4_294_967_295
|
|
66
|
+
|
|
67
|
+
JETSTREAM_V1_DICTIONARY = File.join(__dir__, '../../data/jetstream_zstd_dictionary')
|
|
68
|
+
private_constant :JETSTREAM_V1_DICTIONARY
|
|
69
|
+
|
|
70
|
+
# Current cursor (time in microseconds of the last seen message, or a
|
|
71
|
+
# sequential number on a v2 server using the v1 compatibility API).
|
|
72
|
+
#
|
|
61
73
|
# @return [Integer, nil]
|
|
74
|
+
#
|
|
62
75
|
attr_accessor :cursor
|
|
63
76
|
|
|
64
77
|
#
|
|
@@ -67,15 +80,27 @@ module Skyfall
|
|
|
67
80
|
# @param params [Hash] options, see below:
|
|
68
81
|
#
|
|
69
82
|
# @option params [Integer] :cursor
|
|
70
|
-
# cursor from which to resume
|
|
83
|
+
# cursor from which to resume (seq number or time in microseconds)
|
|
71
84
|
#
|
|
72
|
-
# @option params [Array<String>] :wanted_dids
|
|
73
|
-
# DID filter to pass to the server (`:wantedDids` is also accepted);
|
|
85
|
+
# @option params [String, Array<String>] :wanted_dids
|
|
86
|
+
# DID filter to pass to the server (`:wantedDids` or `:dids` is also accepted);
|
|
74
87
|
# value should be a DID string or an array of those
|
|
75
88
|
#
|
|
76
|
-
# @option params [Array<String, Symbol>] :wanted_collections
|
|
77
|
-
# collection filter to pass to the server (`:wantedCollections` is also accepted);
|
|
78
|
-
# value should be an
|
|
89
|
+
# @option params [String, Symbol, Array<String, Symbol>] :wanted_collections
|
|
90
|
+
# collection filter to pass to the server (`:wantedCollections` or `:collections` is also accepted);
|
|
91
|
+
# value should be: an string with a concrete NSID or a prefix and wildcard, a symbol shorthand,
|
|
92
|
+
# or an array of those
|
|
93
|
+
#
|
|
94
|
+
# @option params [Integer, String] :max_message_size_bytes
|
|
95
|
+
# server-side message size filter (`:maxMessageSizeBytes` is also accepted);
|
|
96
|
+
# tells the server to skip events larger than the given number of bytes in size
|
|
97
|
+
# (0 is the default and means no limit). **Note:** When compression is enabled,
|
|
98
|
+
# Jetstream v1 servers compare this against the *compressed* message size, while
|
|
99
|
+
# Jetstream v2 servers (both in the v2 API and the v1 compatibility API) count the
|
|
100
|
+
# *uncompressed* message size.
|
|
101
|
+
#
|
|
102
|
+
# @option params [Boolean] :compress
|
|
103
|
+
# enable zstd compression (default: false), using the bundled dictionary
|
|
79
104
|
#
|
|
80
105
|
# @raise [ArgumentError] if the server parameter or the options are invalid
|
|
81
106
|
#
|
|
@@ -85,6 +110,7 @@ module Skyfall
|
|
|
85
110
|
|
|
86
111
|
@params = check_params(params)
|
|
87
112
|
@cursor = @params.delete(:cursor)
|
|
113
|
+
@compress = @params.delete(:compress)
|
|
88
114
|
@root_url = ensure_empty_path(@root_url)
|
|
89
115
|
end
|
|
90
116
|
|
|
@@ -95,7 +121,10 @@ module Skyfall
|
|
|
95
121
|
# @return [String]
|
|
96
122
|
|
|
97
123
|
def build_websocket_url
|
|
98
|
-
params = @
|
|
124
|
+
params = @params.dup
|
|
125
|
+
params[:cursor] = @cursor if @cursor
|
|
126
|
+
params[:compress] = true if @compress
|
|
127
|
+
|
|
99
128
|
query = URI.encode_www_form(params)
|
|
100
129
|
|
|
101
130
|
@root_url + "/subscribe" + (query.length > 0 ? "?#{query}" : '')
|
|
@@ -103,8 +132,8 @@ module Skyfall
|
|
|
103
132
|
|
|
104
133
|
# Processes a single message received from the websocket. Passes the received data to the
|
|
105
134
|
# {#on_raw_message} handler, builds a {Skyfall::Jetstream::Message} object, and passes it to
|
|
106
|
-
# the {#on_message} handler (if defined). Also updates the {#cursor} to
|
|
107
|
-
#
|
|
135
|
+
# the {#on_message} handler (if defined). Also updates the {#cursor} to the `cursor` or `time_us`
|
|
136
|
+
# value of this message (note: this is skipped if {#on_message} is not set).
|
|
108
137
|
#
|
|
109
138
|
# @param msg
|
|
110
139
|
# {https://rubydoc.info/gems/faye-websocket/Faye/WebSocket/API/MessageEvent Faye::WebSocket::API::MessageEvent}
|
|
@@ -115,14 +144,21 @@ module Skyfall
|
|
|
115
144
|
@handlers[:raw_message]&.call(data)
|
|
116
145
|
|
|
117
146
|
if @handlers[:message]
|
|
118
|
-
jet_message = Message.new(data)
|
|
119
|
-
@cursor = jet_message.
|
|
147
|
+
jet_message = Jetstream::Message.new(decode_message_data(data))
|
|
148
|
+
@cursor = jet_message.cursor
|
|
120
149
|
@handlers[:message].call(jet_message)
|
|
121
150
|
else
|
|
122
151
|
@cursor = nil
|
|
123
152
|
end
|
|
124
153
|
end
|
|
125
154
|
|
|
155
|
+
# Returns the Zstd dictionary used for decompressing messages if :compress option is enabled.
|
|
156
|
+
# @return [Zstd::DDict]
|
|
157
|
+
|
|
158
|
+
def compression_dictionary
|
|
159
|
+
@compression_dictionary ||= Zstd::DDict.new(File.binread(JETSTREAM_V1_DICTIONARY))
|
|
160
|
+
end
|
|
161
|
+
|
|
126
162
|
|
|
127
163
|
private
|
|
128
164
|
|
|
@@ -133,56 +169,72 @@ module Skyfall
|
|
|
133
169
|
raise ArgumentError.new("Params should be a hash") unless params.is_a?(Hash)
|
|
134
170
|
|
|
135
171
|
params.each do |k, v|
|
|
136
|
-
|
|
172
|
+
k = normalize_params_key(k)
|
|
173
|
+
k, v = check_option(k, v)
|
|
174
|
+
processed[k] = v
|
|
175
|
+
end
|
|
137
176
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
elsif !k.is_a?(String)
|
|
141
|
-
raise ArgumentError.new("Invalid params key: #{k.inspect}")
|
|
142
|
-
end
|
|
177
|
+
processed
|
|
178
|
+
end
|
|
143
179
|
|
|
144
|
-
|
|
145
|
-
|
|
180
|
+
def normalize_params_key(key)
|
|
181
|
+
if key.is_a?(Symbol)
|
|
182
|
+
key = key.to_s
|
|
183
|
+
elsif !key.is_a?(String)
|
|
184
|
+
raise ArgumentError.new("Invalid params key: #{key.inspect}")
|
|
146
185
|
end
|
|
147
186
|
|
|
148
|
-
|
|
187
|
+
key.gsub(/_([a-zA-Z])/) { $1.upcase }.to_sym
|
|
149
188
|
end
|
|
150
189
|
|
|
151
190
|
def check_option(k, v)
|
|
152
191
|
case k
|
|
153
|
-
when :wantedCollections
|
|
154
|
-
check_wanted_collections(v)
|
|
155
|
-
when :wantedDids
|
|
156
|
-
check_wanted_dids(v)
|
|
192
|
+
when :collections, :wantedCollections
|
|
193
|
+
[:wantedCollections, check_wanted_collections(v)]
|
|
194
|
+
when :dids, :wantedDids
|
|
195
|
+
[:wantedDids, check_wanted_dids(v)]
|
|
157
196
|
when :cursor
|
|
158
|
-
check_cursor(v)
|
|
159
|
-
when :
|
|
197
|
+
[:cursor, check_cursor(v)]
|
|
198
|
+
when :maxMessageSizeBytes
|
|
199
|
+
[:maxMessageSizeBytes, check_max_message_size_bytes(v)]
|
|
200
|
+
when :compress
|
|
201
|
+
[:compress, check_compress(v)]
|
|
202
|
+
when :requireHello
|
|
160
203
|
raise ArgumentError.new("Skyfall::Jetstream doesn't support the #{k.inspect} option yet")
|
|
204
|
+
when :kinds
|
|
205
|
+
raise ArgumentError.new("The :kinds option is only supported in Jetstream v2")
|
|
161
206
|
else
|
|
162
207
|
raise ArgumentError.new("Unknown option: #{k.inspect}")
|
|
163
208
|
end
|
|
164
209
|
end
|
|
165
210
|
|
|
166
211
|
def check_wanted_collections(list)
|
|
212
|
+
raise ArgumentError, "Collections filter must not be empty" if list.nil? || list == []
|
|
213
|
+
|
|
167
214
|
list = [list] unless list.is_a?(Array)
|
|
215
|
+
list.map { |c| check_collection_param(c) }
|
|
216
|
+
end
|
|
168
217
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
end
|
|
218
|
+
def check_collection_param(value)
|
|
219
|
+
if value.is_a?(String)
|
|
220
|
+
# TODO: more validation
|
|
221
|
+
value
|
|
222
|
+
elsif value.is_a?(Symbol)
|
|
223
|
+
Collection.from_short_code(value) or raise ArgumentError.new("Unknown collection symbol: #{value.inspect}")
|
|
224
|
+
else
|
|
225
|
+
raise ArgumentError.new("Invalid collection argument: #{value.inspect}")
|
|
178
226
|
end
|
|
179
227
|
end
|
|
180
228
|
|
|
181
|
-
def check_wanted_dids(
|
|
182
|
-
|
|
229
|
+
def check_wanted_dids(value)
|
|
230
|
+
raise ArgumentError, "DIDs filter must not be empty" if value.nil? || value == []
|
|
231
|
+
|
|
232
|
+
list = value.is_a?(Array) ? value : [value]
|
|
183
233
|
|
|
184
|
-
|
|
185
|
-
|
|
234
|
+
list.each do |did|
|
|
235
|
+
unless did.is_a?(String) && did =~ /\Adid:[a-z]+:/
|
|
236
|
+
raise ArgumentError.new("Invalid DID argument: #{did.inspect}")
|
|
237
|
+
end
|
|
186
238
|
end
|
|
187
239
|
|
|
188
240
|
# TODO: more validation
|
|
@@ -190,7 +242,41 @@ module Skyfall
|
|
|
190
242
|
end
|
|
191
243
|
|
|
192
244
|
def check_cursor(cursor)
|
|
193
|
-
cursor
|
|
245
|
+
cursor&.to_i
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def check_compress(value)
|
|
249
|
+
unless value == true || value == false
|
|
250
|
+
raise ArgumentError, "Invalid compress argument: #{value.inspect} (expected true or false)"
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
value
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
def decode_message_data(data)
|
|
257
|
+
return data unless @compress
|
|
258
|
+
|
|
259
|
+
dictionary = compression_dictionary
|
|
260
|
+
|
|
261
|
+
begin
|
|
262
|
+
Zstd.decompress(data, dict: dictionary)
|
|
263
|
+
rescue RuntimeError => e
|
|
264
|
+
raise DecodeError, "Zstd decompression failed: #{e.message}"
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
def check_max_message_size_bytes(value)
|
|
269
|
+
if value.is_a?(String) && value.match?(/\A[0-9]+\z/)
|
|
270
|
+
value = value.to_i
|
|
271
|
+
elsif !value.is_a?(Integer)
|
|
272
|
+
raise ArgumentError, "Invalid maxMessageSizeBytes argument: #{value.inspect}"
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
unless value.between?(0, MAX_MAX_BYTES)
|
|
276
|
+
raise ArgumentError, "Invalid maxMessageSizeBytes argument: #{value.inspect} (expected an integer between 0 and #{MAX_MAX_BYTES})"
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
value
|
|
194
280
|
end
|
|
195
281
|
end
|
|
196
282
|
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../errors'
|
|
4
|
+
require_relative 'message'
|
|
5
|
+
|
|
6
|
+
module Skyfall
|
|
7
|
+
|
|
8
|
+
#
|
|
9
|
+
# Jetstream message sent when the status of an account changes. This can be:
|
|
10
|
+
#
|
|
11
|
+
# - an account being created, sending its initial state (should be active)
|
|
12
|
+
# - an account being deactivated or suspended
|
|
13
|
+
# - an account being restored back to an active state from deactivation/suspension
|
|
14
|
+
# - an account being deleted (the status returning `:deleted`)
|
|
15
|
+
#
|
|
16
|
+
|
|
17
|
+
class JetstreamV2::AccountMessage < JetstreamV2::Message
|
|
18
|
+
|
|
19
|
+
#
|
|
20
|
+
# @param json [Hash] message JSON decoded from the websocket message
|
|
21
|
+
# @raise [DecodeError] if the message doesn't include required data
|
|
22
|
+
#
|
|
23
|
+
def initialize(json)
|
|
24
|
+
super
|
|
25
|
+
|
|
26
|
+
raise DecodeError, "Missing account object" if @payload['account'].nil?
|
|
27
|
+
raise DecodeError, "Invalid account object" unless @payload['account'].is_a?(Hash)
|
|
28
|
+
raise DecodeError, "Missing event details (account.active)" if @payload['account']['active'].nil?
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# @return [Boolean] true if the account is active, false if it's deactivated/suspended etc.
|
|
32
|
+
def active?
|
|
33
|
+
@payload['account']['active']
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# @return [Symbol, nil] for inactive accounts, specifies the exact state; nil for active accounts
|
|
37
|
+
def status
|
|
38
|
+
@payload['account']['status']&.to_sym
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'message'
|
|
4
|
+
require_relative '../jetstream/operation'
|
|
5
|
+
|
|
6
|
+
module Skyfall
|
|
7
|
+
|
|
8
|
+
#
|
|
9
|
+
# Jetstream message which includes a single operation on a record in the repo (a record was
|
|
10
|
+
# created, updated or deleted). Most of the messages received from Jetstream are of this type,
|
|
11
|
+
# and this is the type you will usually be most interested in.
|
|
12
|
+
#
|
|
13
|
+
|
|
14
|
+
class JetstreamV2::CommitMessage < JetstreamV2::Message
|
|
15
|
+
|
|
16
|
+
#
|
|
17
|
+
# @param json [Hash] message JSON decoded from the websocket message
|
|
18
|
+
# @raise [DecodeError] if the message doesn't include required data
|
|
19
|
+
#
|
|
20
|
+
def initialize(json)
|
|
21
|
+
super
|
|
22
|
+
check_if_not_nil 'rev', 'collection', 'rkey', 'operation'
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# @return [String] current revision of the repo
|
|
26
|
+
def rev
|
|
27
|
+
@payload['rev']
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Returns the record operation included in the commit.
|
|
31
|
+
# @return [Jetstream::Operation]
|
|
32
|
+
#
|
|
33
|
+
def operation
|
|
34
|
+
@operation ||= Jetstream::Operation.new(self, @payload)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
alias op operation
|
|
38
|
+
|
|
39
|
+
# Returns record operations included in the commit. Currently a `:commit` message from
|
|
40
|
+
# Jetstream always includes exactly one operation, but for compatibility with
|
|
41
|
+
# {Skyfall::Firehose}'s API it's also returned in an array here.
|
|
42
|
+
#
|
|
43
|
+
# @return [Array<Jetstream::Operation>]
|
|
44
|
+
#
|
|
45
|
+
def operations
|
|
46
|
+
[operation]
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
alias ops operations
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative '../errors'
|
|
4
|
+
require_relative 'message'
|
|
5
|
+
|
|
6
|
+
module Skyfall
|
|
7
|
+
|
|
8
|
+
#
|
|
9
|
+
# Jetstream message sent when a new DID is created or when the details of someone's DID document
|
|
10
|
+
# are changed (usually either a handle change or a migration to a different PDS). The message
|
|
11
|
+
# should include currently assigned handle (though the field is not required).
|
|
12
|
+
#
|
|
13
|
+
# Note: the message is originally emitted from the account's PDS and is passed as is by relays,
|
|
14
|
+
# which means you can't fully trust that the handle is actually correctly assigned to the DID
|
|
15
|
+
# and verified by DNS or well-known. To confirm that, use `DID.resolve_handle` from
|
|
16
|
+
# [DIDKit](https://ruby.sdk.blue/didkit/).
|
|
17
|
+
#
|
|
18
|
+
|
|
19
|
+
class JetstreamV2::IdentityMessage < JetstreamV2::Message
|
|
20
|
+
|
|
21
|
+
#
|
|
22
|
+
# @param json [Hash] message JSON decoded from the websocket message
|
|
23
|
+
# @raise [DecodeError] if the message doesn't include required data
|
|
24
|
+
#
|
|
25
|
+
def initialize(json)
|
|
26
|
+
super
|
|
27
|
+
|
|
28
|
+
raise DecodeError, "Missing identity object" if @payload['identity'].nil?
|
|
29
|
+
raise DecodeError, "Invalid identity object" unless @payload['identity'].is_a?(Hash)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# @return [String, nil] current handle assigned to the DID
|
|
33
|
+
def handle
|
|
34
|
+
@payload['identity']['handle']
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|