e621stream 0.0.0 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b16a81259148bd2d44ea56a857d2aeefbf15029660f8bd79d219198e3231711e
4
- data.tar.gz: d24d99e59eb909cbf80f39108c9d90835022f2d886b4f89060245e8c6ceb86c7
3
+ metadata.gz: dc6c807b32f91dc3e755c6f97ca580b91cae5c6f2e3c18ee1db04b0b2e5420c9
4
+ data.tar.gz: 633f8cb92b1c9e65f87bb0d03011a7680ecca1fbcfc15c64cc60445a2430dc00
5
5
  SHA512:
6
- metadata.gz: d8b860f937490e74e90af663866c043b9923321ef85a6d2a84a7a8ba57571605079c71fcaf9153f28343262583647ceb1bd4b3c659cb1aa0346e8d2581c1cd47
7
- data.tar.gz: aa52fe6a7af0ac4f3bf7e0adff246b800b16b8350a12ea27feb55c4d389c06bdacd54e17c03e62f84d11bc40cbb7c2d49b5b1b62dd9df16b894b5e82fcabd83c
6
+ metadata.gz: 01727d098cd998cdf67b6c6d79dac43d1caa156cef0f398fff2dfd1094ce3e5540842727cc188bda0cddd8e88d9c1394aa70af28d456daf2a044899280faaefc
7
+ data.tar.gz: 8cb060095b29f36cff0c5242896bf8e9310febc92854db10fe5a8cb365b1df95032f8ce5b3105c22cbcfe6f1c1056ed1b155a1f26ce46583ea3157c6f71b5d92
data/README.md CHANGED
@@ -1,13 +1,13 @@
1
1
  # e621stream
2
2
 
3
- A Ruby client for [E621Stream](https://e621.stream), a live WebSocket stream of e621 activity. It's built on [async-websocket](https://github.com/socketry/async-websocket) and typed with Sorbet.
3
+ A client for [E621Stream](https://e621.stream), using [async-websocket](https://github.com/socketry/async-websocket) and typed with sorbet.
4
4
 
5
- ```bash
5
+ ```sh
6
6
  gem install e621stream
7
7
  ```
8
8
 
9
9
  ```ruby
10
- require "e621stream"
10
+ require("e621stream")
11
11
 
12
12
  client = E621Stream::Client.new(
13
13
  username: "your_name",
@@ -22,14 +22,14 @@ client.on(:error) { |error| warn(error.message) }
22
22
  client.run
23
23
  ```
24
24
 
25
- - **Events:** `on("<event type>")` for one type, or `on(:dispatch)` for everything. Each is an `E621Stream::Event` with `id`, `type`, `time` and `data`, which is the record as e621's api returns it. See [Events](https://e621.stream/events/) for what each one is.
26
- - **Running:** `run` blocks. Inside a reactor already, it runs as a task, so `client.close` from another task stops it.
27
- - **Reconnecting:** the connection is reopened with backoff when it drops, and resumes from the last event it got. It isn't reopened after a close retrying won't fix, like bad credentials. `reconnect: false` turns it off.
28
- - **Resuming:** pass `cursor:` (an event id, or a millisecond timestamp) to replay from there. `client.cursor` is the last event received.
29
- - **Changing subscriptions:** `client.subscribe([...])`.
30
- - **Private events:** like `dmail.create`, these are polled with your own credentials and have to be subscribed to by name.
31
- - **Other events:** `:ready`, `:replay_complete`, `:subscribed`, `:reconnecting`, `:close` and `:error`.
25
+ Instead of `username:` & `api_key:`, you can pass `token:` with a token from [/authorize](https://e621.stream/authorize).
32
26
 
33
- ```bash
34
- bundle exec rake # rubocop, zeitwerk, tests and sorbet
27
+ `run` blocks, unless you're already in a reactor, then it runs as a task and `client.close` stops it.
28
+
29
+ `on("post.create")` listens for one type, `on(:dispatch)` for everything. Events have `id`, `type`, `time` and `data` (the record as the api returns it), see [Events](https://e621.stream/events/). There's also `:ready`, `:replay_complete`, `:subscribed`, `:reconnecting`, `:close` and `:error`.
30
+
31
+ It reconnects when the connection drops (`reconnect: false` turns that off), and picks up from `client.cursor`. Pass `cursor:` to replay from an event id or ms timestamp. `url:` is the site (`https://e621.stream`), the websocket url comes from its `/info`, or pass a `ws://`/`wss://` url to connect to it directly. `client.subscribe([...])` changes subscriptions. Private events like `dmail.create` have to be subscribed to by name.
32
+
33
+ ```sh
34
+ bundle exec rake # rubocop, zeitwerk, tests & sorbet
35
35
  ```
@@ -8,7 +8,10 @@ module E621Stream
8
8
  # Your e621 username and an api key, from Account > Manage API Access.
9
9
  const(:username, T.nilable(String), default: nil)
10
10
  const(:api_key, T.nilable(String), default: nil)
11
- const(:url, String, default: "wss://e621.stream/ws")
11
+ # A token from https://e621.stream/authorize, instead of a username & api key.
12
+ const(:token, T.nilable(String), default: nil)
13
+ # The site, the websocket url comes from its /info. A ws:// or wss:// url is connected to directly.
14
+ const(:url, String, default: "https://e621.stream")
12
15
  # A name for your client, shown in the server's logs.
13
16
  const(:client, T.nilable(String), default: nil)
14
17
  # An event id (or a millisecond timestamp) to replay from. After that, reconnecting resumes from the last event.
@@ -3,6 +3,7 @@
3
3
 
4
4
  require("async")
5
5
  require("async/http/endpoint")
6
+ require("async/http/internet")
6
7
  require("async/websocket/client")
7
8
 
8
9
  module E621Stream
@@ -18,7 +19,7 @@ module E621Stream
18
19
  # An event type, `<resource>.*`, `*`, or `{ "type" => ..., "filter" => { ... } }`.
19
20
  Subscription = T.type_alias { T.any(String, T::Hash[T.any(String, Symbol), T.untyped]) }
20
21
 
21
- # Opens the real connection, tests swap it for a fake one.
22
+ # Tests swap this out.
22
23
  class Connector
23
24
  extend(T::Sig)
24
25
 
@@ -27,10 +28,28 @@ module E621Stream
27
28
  endpoint = Async::HTTP::Endpoint.parse(url, alpn_protocols: Async::HTTP::Protocol::HTTP11.names)
28
29
  Async::WebSocket::Client.connect(endpoint, &block)
29
30
  end
31
+
32
+ # Gets the websocket url from the site's /info, unless it's one already.
33
+ sig { params(url: String).returns(String) }
34
+ def websocket_url(url)
35
+ return url if url.match?(%r{\Awss?://}i)
36
+
37
+ info = URI.join(url, "/info").to_s
38
+ internet = Async::HTTP::Internet.new
39
+ response = internet.get(info)
40
+ raise("#{info} returned #{response.status}") unless response.success?
41
+
42
+ websocket = JSON.parse(response.read)["websocket"]
43
+ raise("#{info} didn't have a websocket url") unless websocket.is_a?(String)
44
+
45
+ URI.join(url, websocket).to_s
46
+ ensure
47
+ response&.close
48
+ internet&.close
49
+ end
30
50
  end
31
51
 
32
52
  HEARTBEAT_SECONDS = 30
33
- # The server answers heartbeats and pings, nothing for this long means the connection is gone without having closed.
34
53
  SILENT_SECONDS = 75
35
54
  MAX_DELAY_SECONDS = 60
36
55
 
@@ -46,6 +65,7 @@ module E621Stream
46
65
  subscriptions: T::Array[Subscription],
47
66
  username: T.nilable(String),
48
67
  api_key: T.nilable(String),
68
+ token: T.nilable(String),
49
69
  url: String,
50
70
  client: T.nilable(String),
51
71
  cursor: T.nilable(T.any(String, Integer)),
@@ -53,8 +73,8 @@ module E621Stream
53
73
  connector: Connector,
54
74
  ).void
55
75
  end
56
- def initialize(subscriptions:, username: nil, api_key: nil, url: "wss://e621.stream/ws", client: nil, cursor: nil, reconnect: true, connector: Connector.new)
57
- @options = T.let(Options.new(subscriptions: subscriptions, username: username, api_key: api_key, url: url, client: client, cursor: cursor, reconnect: reconnect), Options)
76
+ def initialize(subscriptions:, username: nil, api_key: nil, token: nil, url: "https://e621.stream", client: nil, cursor: nil, reconnect: true, connector: Connector.new)
77
+ @options = T.let(Options.new(subscriptions: subscriptions, username: username, api_key: api_key, token: token, url: url, client: client, cursor: cursor, reconnect: reconnect), Options)
58
78
  @connector = connector
59
79
  @cursor = T.let(cursor&.to_s, T.nilable(String))
60
80
  @listeners = T.let(Hash.new { |hash, key| hash[key] = [] }, T::Hash[String, T::Array[Listener]])
@@ -89,9 +109,8 @@ module E621Stream
89
109
  break unless reconnecting
90
110
 
91
111
  @attempt += 1
92
- # a server that's rate limiting this connection wants a longer break
93
- base = code == CloseCodes::RATE_LIMITED ? 30 : 1
94
- delay = [base * (2**(@attempt - 1)), MAX_DELAY_SECONDS].min * rand(0.75..1.25)
112
+ delay = code == CloseCodes::RATE_LIMITED ? 30 : [@attempt**2, MAX_DELAY_SECONDS].min
113
+ delay += rand
95
114
  emit("reconnecting", { attempt: @attempt, delay: delay })
96
115
  sleep(delay)
97
116
  end
@@ -114,11 +133,10 @@ module E621Stream
114
133
 
115
134
  private
116
135
 
117
- # resolves to the close code and reason
118
136
  sig { returns([Integer, String]) }
119
137
  def connect_once
120
- result = T.let([1000, ""], [Integer, String])
121
- @connector.connect(options.url) do |connection|
138
+ # fetched every time, so it can move
139
+ @connector.connect(@connector.websocket_url(options.url)) do |connection|
122
140
  @connection = connection
123
141
  @last_message = now
124
142
  heartbeat = Async { heartbeat_loop(connection) }
@@ -132,11 +150,11 @@ module E621Stream
132
150
  @connection = nil
133
151
  end
134
152
  end
135
- result
153
+ # anything but a normal close raises out of read
154
+ [1000, ""]
136
155
  rescue Protocol::WebSocket::ClosedError => e
137
156
  [e.code || 1006, e.message]
138
157
  rescue StandardError => e
139
- # closing tears the connection down under the read, which isn't worth reporting
140
158
  return [1000, "closed by client"] if closing?
141
159
 
142
160
  emit("error", e)
@@ -148,7 +166,7 @@ module E621Stream
148
166
  loop do
149
167
  sleep(HEARTBEAT_SECONDS)
150
168
  if now - @last_message > SILENT_SECONDS
151
- connection.close(4999, "no response from the server")
169
+ connection.close(1001, "timed out")
152
170
  break
153
171
  end
154
172
  send_message({ op: "heartbeat" })
@@ -161,7 +179,11 @@ module E621Stream
161
179
  case message["op"]
162
180
  when "hello"
163
181
  identify = { subscriptions: options.subscriptions }
164
- identify[:auth] = { username: options.username, api_key: options.api_key } if options.username && options.api_key
182
+ if options.token
183
+ identify[:auth] = { token: options.token }
184
+ elsif options.username && options.api_key
185
+ identify[:auth] = { username: options.username, api_key: options.api_key }
186
+ end
165
187
  identify[:client] = options.client if options.client
166
188
  identify[:cursor] = @cursor if @cursor
167
189
  send_message({ op: "identify", d: identify })
@@ -170,7 +192,6 @@ module E621Stream
170
192
  emit("ready", data)
171
193
  when "dispatch"
172
194
  event = Event.new(id: message["id"], type: message["type"], time: message["time"], data: message["data"], private: message["private"] == true)
173
- # private events aren't in the stream, so they can't be resumed from
174
195
  @cursor = event.id unless event.private?
175
196
  emit("dispatch", event)
176
197
  emit(event.type, event)
@@ -197,7 +218,7 @@ module E621Stream
197
218
  @listeners[name]&.each { |listener| listener.call(value) }
198
219
  end
199
220
 
200
- # read through a method, since #close changes it from another fiber while #run is waiting
221
+ # #close changes this from another fiber
201
222
  sig { returns(T::Boolean) }
202
223
  def closing?
203
224
  @closing
@@ -16,8 +16,10 @@ module E621Stream
16
16
  SLOW_CONSUMER = 4009
17
17
  TOO_MANY_CONNECTIONS = 4010
18
18
  AUTHENTICATION_EXPIRED = 4011
19
+ DISCONNECTED = 4012
20
+ BANNED = 4013
19
21
 
20
22
  # Closing for any of these again would just get the same result, so they aren't reconnected from.
21
- FATAL = T.let([UNKNOWN_OP, DECODE_ERROR, AUTHENTICATION_FAILED, ALREADY_IDENTIFIED, INVALID_SUBSCRIPTION, INVALID_CURSOR, AUTHENTICATION_EXPIRED].freeze, T::Array[Integer])
23
+ FATAL = T.let([UNKNOWN_OP, DECODE_ERROR, AUTHENTICATION_FAILED, ALREADY_IDENTIFIED, INVALID_SUBSCRIPTION, INVALID_CURSOR, AUTHENTICATION_EXPIRED, DISCONNECTED, BANNED].freeze, T::Array[Integer])
22
24
  end
23
25
  end
@@ -3,7 +3,7 @@
3
3
 
4
4
  module E621Stream
5
5
  module Events
6
- # Kept identical to the server's src/events.ts, the repository's tests check it.
6
+ # Generated by scripts/clientTypes.ts in the E621Stream repository, from the server's src/events.ts.
7
7
  EVENT_TYPES = %w[
8
8
  artist.create artist.delete artist.update artist_url.create artist_version.create avoid_posting.create
9
9
  avoid_posting.delete avoid_posting.destroy avoid_posting.undelete avoid_posting.update
@@ -12,18 +12,18 @@ module E621Stream
12
12
  forum_post.create forum_post.delete forum_post.hide forum_post.unhide forum_post.update forum_topic.create
13
13
  forum_topic.delete forum_topic.hide forum_topic.unhide forum_topic.update help_page.create help_page.update
14
14
  mod_action.create news_update.create news_update.update note.create note.update note_version.create
15
- pool.add_posts pool.create pool.delete pool.remove_posts pool.update pool_version.create post.create post.update
16
- post_approval.create post_event.create post_flag.create post_flag.update post_replacement.create
17
- post_set.add_posts post_set.create post_set.delete post_set.remove_posts post_set.update post_version.create
18
- post_version.hide post_version.unhide tag.create tag.delete tag.update tag_alias.create tag_alias.update
19
- tag_implication.create tag_implication.update tag_type_version.create takedown.create takedown.delete
20
- takedown.update upload_karma_event.create user.create user.delete user.update user_feedback.create
21
- user_feedback.delete user_feedback.destroy user_feedback.undelete user_feedback.update wiki_page.create
22
- wiki_page.delete wiki_page.update wiki_page_version.create
15
+ pool.add_posts pool.create pool.delete pool.remove_posts pool.update pool_version.create post.create
16
+ post.destroy post.stats post.update post_approval.create post_event.create post_flag.create post_flag.update
17
+ post_replacement.create post_replacement.destroy post_replacement.update post_set.add_posts post_set.create
18
+ post_set.delete post_set.remove_posts post_set.update post_version.create post_version.hide post_version.unhide
19
+ tag.create tag.delete tag.update tag_alias.create tag_alias.update tag_implication.create tag_implication.update
20
+ tag_type_version.create takedown.create takedown.delete takedown.update upload_karma_event.create user.create
21
+ user.delete user.update user_feedback.create user_feedback.delete user_feedback.destroy user_feedback.undelete
22
+ user_feedback.update wiki_page.create wiki_page.delete wiki_page.update wiki_page_version.create
23
23
  ].freeze
24
24
 
25
25
  # Polled with your own credentials and only sent to you. They have to be subscribed to by name, `*` doesn't include
26
26
  # them.
27
- PRIVATE_EVENT_TYPES = %w[dmail.create].freeze
27
+ PRIVATE_EVENT_TYPES = %w[dmail.create private_post_set.add_posts private_post_set.remove_posts].freeze
28
28
  end
29
29
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module E621Stream
4
- VERSION = "0.0.0"
4
+ VERSION = "0.0.3"
5
5
  end
data/lib/e621stream.rb CHANGED
@@ -4,8 +4,7 @@ require("json")
4
4
  require("zeitwerk")
5
5
  require("sorbet-runtime")
6
6
 
7
- # Loaded eagerly (rather than left to Zeitwerk) because the gemspec already requires version.rb to read the version,
8
- # which defines E621Stream before Zeitwerk::Loader#setup runs.
7
+ # required here since the gemspec loads version.rb before zeitwerk is set up
9
8
  require_relative("e621stream/version")
10
9
 
11
10
  loader = Zeitwerk::Loader.for_gem
@@ -11,12 +11,12 @@
11
11
  # Released under the MIT License.
12
12
  # Copyright, 2025, by Samuel Williams.
13
13
  #
14
- # pkg:gem/async-http#lib/async/http/protocol/defaulton.rb:6
14
+ # pkg:gem/async-http#lib/async/http/protocol/configurable.rb:6
15
15
  module Async; end
16
16
 
17
17
  # @namespace
18
18
  #
19
- # pkg:gem/async-http#lib/async/http/protocol/defaulton.rb:7
19
+ # pkg:gem/async-http#lib/async/http/protocol/configurable.rb:7
20
20
  module Async::HTTP; end
21
21
 
22
22
  # @namespace
@@ -313,9 +313,88 @@ end
313
313
  # pkg:gem/async-http#lib/async/http/endpoint.rb:24
314
314
  Async::HTTP::Endpoint::SCHEMES = T.let(T.unsafe(nil), Hash)
315
315
 
316
+ # A high-level HTTP client for making requests to any URL, managing a pool of persistent connections keyed by host.
317
+ #
318
+ # pkg:gem/async-http#lib/async/http/internet.rb:17
319
+ class Async::HTTP::Internet
320
+ # Initialize the internet client.
321
+ # @parameter options [Hash] Options to pass to the underlying {Client} instances.
322
+ #
323
+ # pkg:gem/async-http#lib/async/http/internet.rb:20
324
+ def initialize(**options); end
325
+
326
+ # Make a request to the internet with the given `method` and `url`.
327
+ #
328
+ # If you provide non-frozen headers, they may be mutated.
329
+ #
330
+ # @parameter method [String] The request method, e.g. `GET`.
331
+ # @parameter url [String] The URL to request, e.g. `https://www.codeotaku.com`.
332
+ # @parameter headers [Hash | Protocol::HTTP::Headers] The headers to send with the request.
333
+ # @parameter body [String | Protocol::HTTP::Body] The body to send with the request.
334
+ #
335
+ # pkg:gem/async-http#lib/async/http/internet.rb:48
336
+ def call(verb, url, *arguments, **options, &block); end
337
+
338
+ # Get or create a client for the given endpoint.
339
+ # @parameter endpoint [Endpoint] The endpoint to connect to.
340
+ # @returns [Client] A client suitable for making requests to the endpoint.
341
+ #
342
+ # pkg:gem/async-http#lib/async/http/internet.rb:32
343
+ def client_for(endpoint); end
344
+
345
+ # A cache of clients.
346
+ # @attribute [Hash(URI, Client)]
347
+ #
348
+ # pkg:gem/async-http#lib/async/http/internet.rb:27
349
+ def clients; end
350
+
351
+ # Close all cached clients and release their resources.
352
+ #
353
+ # pkg:gem/async-http#lib/async/http/internet.rb:69
354
+ def close; end
355
+
356
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
357
+ def connect(url, *arguments, **options, &block); end
358
+
359
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
360
+ def delete(url, *arguments, **options, &block); end
361
+
362
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
363
+ def get(url, *arguments, **options, &block); end
364
+
365
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
366
+ def head(url, *arguments, **options, &block); end
367
+
368
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
369
+ def options(url, *arguments, **options, &block); end
370
+
371
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
372
+ def patch(url, *arguments, **options, &block); end
373
+
374
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
375
+ def post(url, *arguments, **options, &block); end
376
+
377
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
378
+ def put(url, *arguments, **options, &block); end
379
+
380
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
381
+ def query(url, *arguments, **options, &block); end
382
+
383
+ # pkg:gem/async-http#lib/async/http/internet.rb:78
384
+ def trace(url, *arguments, **options, &block); end
385
+
386
+ protected
387
+
388
+ # pkg:gem/async-http#lib/async/http/internet.rb:91
389
+ def host_key(endpoint); end
390
+
391
+ # pkg:gem/async-http#lib/async/http/internet.rb:85
392
+ def make_client(endpoint); end
393
+ end
394
+
316
395
  # A protocol specifies a way in which to communicate with a remote peer.
317
396
  #
318
- # pkg:gem/async-http#lib/async/http/protocol/defaulton.rb:8
397
+ # pkg:gem/async-http#lib/async/http/protocol/configurable.rb:8
319
398
  module Async::HTTP::Protocol; end
320
399
 
321
400
  # Provides a `new` method that creates a {Configured} wrapper, allowing protocols to be instantiated with custom options.
@@ -2,3 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  # Add your extra requires here (`bin/tapioca require` can be used to bootstrap this list)
5
+
6
+ require("async/http/internet")
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: e621stream
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Donovan_DMC
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2026-11-09 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: async-websocket
@@ -169,7 +169,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
169
  - !ruby/object:Gem::Version
170
170
  version: '0'
171
171
  requirements: []
172
- rubygems_version: 3.6.2
172
+ rubygems_version: 3.6.9
173
173
  specification_version: 4
174
174
  summary: A client for E621Stream, a live WebSocket stream of e621 activity.
175
175
  test_files: []