chat_xdk 0.7.10-aarch64-linux
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 +7 -0
- data/LICENSE +21 -0
- data/README.md +150 -0
- data/THIRD_PARTY_NOTICES.md +570 -0
- data/lib/chat_xdk/chat.rb +1253 -0
- data/lib/chat_xdk/errors.rb +83 -0
- data/lib/chat_xdk/feature_fetch.rb +249 -0
- data/lib/chat_xdk/feature_http.rb +416 -0
- data/lib/chat_xdk/file_decryptor.rb +102 -0
- data/lib/chat_xdk/file_encryptor.rb +100 -0
- data/lib/chat_xdk/native/linux-arm64/libchat_xdk_ffi.so +0 -0
- data/lib/chat_xdk/native.rb +266 -0
- data/lib/chat_xdk/types.rb +1860 -0
- data/lib/chat_xdk/utilities.rb +82 -0
- data/lib/chat_xdk/version.rb +7 -0
- data/lib/chat_xdk.rb +12 -0
- metadata +79 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module ChatXdk
|
|
6
|
+
# Base error raised when the SDK fails. Every failure carries a {#kind}
|
|
7
|
+
# symbol matching the core error kind; an {InvalidPinError} also carries
|
|
8
|
+
# {#guesses_remaining} when Juicebox reports the attempts left.
|
|
9
|
+
class Error < StandardError
|
|
10
|
+
# What kind of failure this is: one of :invalid_input, :invalid_state,
|
|
11
|
+
# :parse, :decryption, :verification, :invalid_pin, :juicebox, :internal.
|
|
12
|
+
#
|
|
13
|
+
# @return [Symbol]
|
|
14
|
+
attr_reader :kind
|
|
15
|
+
|
|
16
|
+
# The PIN attempts left after an :invalid_pin failure, when Juicebox
|
|
17
|
+
# reports them (0 means the budget is exhausted and the stored keys are
|
|
18
|
+
# locked). nil for every other failure.
|
|
19
|
+
#
|
|
20
|
+
# @return [Integer, nil]
|
|
21
|
+
attr_reader :guesses_remaining
|
|
22
|
+
|
|
23
|
+
def initialize(message, kind: :internal, guesses_remaining: nil)
|
|
24
|
+
super(message)
|
|
25
|
+
@kind = kind
|
|
26
|
+
@guesses_remaining = guesses_remaining
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Parse an FFI JSON error document into the matching Error subclass.
|
|
30
|
+
#
|
|
31
|
+
# @param document [String]
|
|
32
|
+
# @return [Error]
|
|
33
|
+
def self.from_document(document)
|
|
34
|
+
parsed = JSON.parse(document)
|
|
35
|
+
kind_str = parsed["kind"].to_s
|
|
36
|
+
message = parsed["message"].to_s
|
|
37
|
+
guesses = parsed["guesses_remaining"]
|
|
38
|
+
guesses = nil unless guesses.is_a?(Integer)
|
|
39
|
+
|
|
40
|
+
klass, kind = KIND_MAP.fetch(kind_str, [Error, :internal])
|
|
41
|
+
klass.new(message, kind: kind, guesses_remaining: guesses)
|
|
42
|
+
rescue JSON::ParserError
|
|
43
|
+
new(document.to_s, kind: :internal)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# A parameter is missing or invalid; the message names it.
|
|
48
|
+
class InvalidInputError < Error; end
|
|
49
|
+
|
|
50
|
+
# A value was neither passed nor available from the chat's state, such as
|
|
51
|
+
# no keys loaded, no session identity, or no Juicebox config.
|
|
52
|
+
class InvalidStateError < Error; end
|
|
53
|
+
|
|
54
|
+
# Event or other wire data is malformed.
|
|
55
|
+
class ParseError < Error; end
|
|
56
|
+
|
|
57
|
+
# Ciphertext did not decrypt, or no key for it was available.
|
|
58
|
+
class DecryptionError < Error; end
|
|
59
|
+
|
|
60
|
+
# A signature did not verify, or could not be verified while unverified
|
|
61
|
+
# events are rejected.
|
|
62
|
+
class VerificationError < Error; end
|
|
63
|
+
|
|
64
|
+
# Juicebox rejected the PIN; see {#guesses_remaining}.
|
|
65
|
+
class InvalidPinError < Error; end
|
|
66
|
+
|
|
67
|
+
# Another Juicebox key-storage failure.
|
|
68
|
+
class JuiceboxError < Error; end
|
|
69
|
+
|
|
70
|
+
class Error
|
|
71
|
+
KIND_MAP = {
|
|
72
|
+
"invalid_input" => [InvalidInputError, :invalid_input],
|
|
73
|
+
"invalid_state" => [InvalidStateError, :invalid_state],
|
|
74
|
+
"parse" => [ParseError, :parse],
|
|
75
|
+
"decryption" => [DecryptionError, :decryption],
|
|
76
|
+
"verification" => [VerificationError, :verification],
|
|
77
|
+
"invalid_pin" => [InvalidPinError, :invalid_pin],
|
|
78
|
+
"juicebox" => [JuiceboxError, :juicebox],
|
|
79
|
+
"internal" => [Error, :internal]
|
|
80
|
+
}.freeze
|
|
81
|
+
private_constant :KIND_MAP
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module ChatXdk
|
|
6
|
+
# Drives a chat's background feature-switch reads (GET /2/chat/features).
|
|
7
|
+
# The native core holds the schedule, sharing, retry, and validation policy;
|
|
8
|
+
# this module performs each GET the core hands out after its delay and reports
|
|
9
|
+
# the status and body back. Nothing here is public API.
|
|
10
|
+
module FeatureFetch
|
|
11
|
+
@mutex = Mutex.new
|
|
12
|
+
@cond = ConditionVariable.new
|
|
13
|
+
@waiting = []
|
|
14
|
+
@timer = nil
|
|
15
|
+
@in_flight = {}
|
|
16
|
+
@generation = 0
|
|
17
|
+
@pid = Process.pid
|
|
18
|
+
|
|
19
|
+
class << self
|
|
20
|
+
# Start a chat's feature-switch reads for its current identity.
|
|
21
|
+
def start(chat_handle)
|
|
22
|
+
reset_if_forked!
|
|
23
|
+
json = Native.consume(Native.chat_xdk_internal_feature_start(chat_handle))
|
|
24
|
+
schedule(json)
|
|
25
|
+
rescue StandardError
|
|
26
|
+
# Feature switches never fail or block SDK calls.
|
|
27
|
+
nil
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Configure a chat's feature-switch endpoint/timings (tests only).
|
|
31
|
+
def configure(chat_handle, endpoint, timeout_ms, refresh_ms)
|
|
32
|
+
Native.consume(
|
|
33
|
+
Native.chat_xdk_internal_feature_configure(
|
|
34
|
+
chat_handle,
|
|
35
|
+
Native.utf8_ptr(endpoint),
|
|
36
|
+
timeout_ms,
|
|
37
|
+
refresh_ms
|
|
38
|
+
)
|
|
39
|
+
)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Set the default feature-switch endpoint for new chats (tests only).
|
|
43
|
+
def set_default_endpoint(endpoint)
|
|
44
|
+
ptr = endpoint.nil? ? FFI::Pointer::NULL : Native.utf8_ptr(endpoint)
|
|
45
|
+
Native.consume(Native.chat_xdk_internal_feature_set_default_endpoint(ptr))
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Stop the current generation's scheduled reads, abort any in-flight
|
|
49
|
+
# sockets, and wait briefly for worker threads so process exit does not
|
|
50
|
+
# race OpenSSL cleanup. Using a generation counter instead of a permanent
|
|
51
|
+
# flag allows later at_exit hooks (such as Minitest/RSpec) to keep working.
|
|
52
|
+
def shutdown
|
|
53
|
+
return if Process.pid != @pid
|
|
54
|
+
|
|
55
|
+
threads = nil
|
|
56
|
+
timer = nil
|
|
57
|
+
@mutex.synchronize do
|
|
58
|
+
@generation += 1
|
|
59
|
+
@waiting.clear
|
|
60
|
+
@cond.broadcast
|
|
61
|
+
timer = @timer
|
|
62
|
+
@timer = nil
|
|
63
|
+
@in_flight.each_value do |sock|
|
|
64
|
+
close_socket(sock)
|
|
65
|
+
end
|
|
66
|
+
threads = @in_flight.keys
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + 2.0
|
|
70
|
+
threads.each do |t|
|
|
71
|
+
rem = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
72
|
+
t.join(rem > 0 ? rem : 0)
|
|
73
|
+
end
|
|
74
|
+
if timer && timer.alive?
|
|
75
|
+
rem = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
76
|
+
timer.join(rem > 0 ? rem : 0)
|
|
77
|
+
end
|
|
78
|
+
rescue StandardError
|
|
79
|
+
nil
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
private
|
|
83
|
+
|
|
84
|
+
def reset_if_forked!
|
|
85
|
+
return if Process.pid == @pid
|
|
86
|
+
|
|
87
|
+
@mutex = Mutex.new
|
|
88
|
+
@cond = ConditionVariable.new
|
|
89
|
+
@waiting = []
|
|
90
|
+
@timer = nil
|
|
91
|
+
@in_flight = {}
|
|
92
|
+
@generation = 0
|
|
93
|
+
@pid = Process.pid
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def schedule(json, gen = nil)
|
|
97
|
+
return if json.nil? || json.empty?
|
|
98
|
+
|
|
99
|
+
fetch = JSON.parse(json)
|
|
100
|
+
id = fetch.fetch("id").to_i
|
|
101
|
+
url = fetch.fetch("url").to_s
|
|
102
|
+
timeout_ms = fetch.fetch("timeout_ms").to_i
|
|
103
|
+
max_bytes = fetch.fetch("max_bytes").to_i
|
|
104
|
+
delay_ms = fetch.fetch("delay_ms", 0).to_i
|
|
105
|
+
|
|
106
|
+
current_gen = @mutex.synchronize do
|
|
107
|
+
return if gen && gen != @generation
|
|
108
|
+
|
|
109
|
+
@generation
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
if delay_ms <= 0
|
|
113
|
+
spawn_worker(id, url, timeout_ms, max_bytes, current_gen)
|
|
114
|
+
return
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
due = Process.clock_gettime(Process::CLOCK_MONOTONIC) + (delay_ms.to_f / 1000.0)
|
|
118
|
+
@mutex.synchronize do
|
|
119
|
+
return if current_gen != @generation
|
|
120
|
+
|
|
121
|
+
@waiting << [due, id, url, timeout_ms, max_bytes, current_gen]
|
|
122
|
+
@waiting.sort_by! { |entry| entry[0] }
|
|
123
|
+
ensure_timer_locked(current_gen)
|
|
124
|
+
@cond.signal
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def ensure_timer_locked(gen)
|
|
129
|
+
return if @timer && @timer.alive?
|
|
130
|
+
|
|
131
|
+
@timer = Thread.new(gen) { |g| timer_loop(g) }
|
|
132
|
+
@timer.name = "chat-xdk-features-timer" if @timer.respond_to?(:name=)
|
|
133
|
+
@timer.report_on_exception = false if @timer.respond_to?(:report_on_exception=)
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def timer_loop(gen)
|
|
137
|
+
loop do
|
|
138
|
+
entry = nil
|
|
139
|
+
@mutex.synchronize do
|
|
140
|
+
loop do
|
|
141
|
+
return if gen != @generation
|
|
142
|
+
|
|
143
|
+
if @waiting.empty?
|
|
144
|
+
@cond.wait(@mutex)
|
|
145
|
+
else
|
|
146
|
+
wait_sec = @waiting[0][0] - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
147
|
+
if wait_sec <= 0
|
|
148
|
+
entry = @waiting.shift
|
|
149
|
+
break
|
|
150
|
+
end
|
|
151
|
+
@cond.wait(@mutex, wait_sec)
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
_, id, url, timeout_ms, max_bytes, entry_gen = entry
|
|
157
|
+
spawn_worker(id, url, timeout_ms, max_bytes, entry_gen)
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def spawn_worker(id, url, timeout_ms, max_bytes, gen)
|
|
162
|
+
t = Thread.new do
|
|
163
|
+
perform(id, url, timeout_ms, max_bytes, gen)
|
|
164
|
+
end
|
|
165
|
+
t.name = "chat-xdk-features" if t.respond_to?(:name=)
|
|
166
|
+
t.report_on_exception = false if t.respond_to?(:report_on_exception=)
|
|
167
|
+
rescue ThreadError
|
|
168
|
+
complete(id, 0, FeatureHttp::EMPTY, gen)
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def perform(id, url, timeout_ms, max_bytes, gen)
|
|
172
|
+
status = 0
|
|
173
|
+
body = FeatureHttp::EMPTY
|
|
174
|
+
thread = Thread.current
|
|
175
|
+
|
|
176
|
+
@mutex.synchronize do
|
|
177
|
+
return if gen != @generation
|
|
178
|
+
|
|
179
|
+
@in_flight[thread] = nil
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
register = lambda do |sock|
|
|
183
|
+
aborted = false
|
|
184
|
+
@mutex.synchronize do
|
|
185
|
+
if gen != @generation
|
|
186
|
+
aborted = true
|
|
187
|
+
else
|
|
188
|
+
@in_flight[thread] = sock
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
if aborted
|
|
192
|
+
close_socket(sock)
|
|
193
|
+
raise IOError, "exiting"
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
unregister = lambda do
|
|
198
|
+
@mutex.synchronize { @in_flight.delete(thread) }
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
begin
|
|
202
|
+
answer = FeatureHttp.get(
|
|
203
|
+
url,
|
|
204
|
+
timeout_ms,
|
|
205
|
+
max_bytes,
|
|
206
|
+
register_socket: register,
|
|
207
|
+
unregister_socket: unregister
|
|
208
|
+
)
|
|
209
|
+
status = answer.status
|
|
210
|
+
body = answer.body
|
|
211
|
+
rescue StandardError
|
|
212
|
+
status = 0
|
|
213
|
+
body = FeatureHttp::EMPTY
|
|
214
|
+
ensure
|
|
215
|
+
@mutex.synchronize { @in_flight.delete(thread) }
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
return if @mutex.synchronize { gen != @generation }
|
|
219
|
+
|
|
220
|
+
complete(id, status, body, gen)
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def complete(id, status, body, gen)
|
|
224
|
+
next_json = Native.with_bytes(body) do |ptr, len|
|
|
225
|
+
Native.consume(Native.chat_xdk_internal_feature_complete(id, status, ptr, len))
|
|
226
|
+
end
|
|
227
|
+
schedule(next_json, gen)
|
|
228
|
+
rescue StandardError
|
|
229
|
+
nil
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def close_socket(sock)
|
|
233
|
+
return unless sock
|
|
234
|
+
|
|
235
|
+
begin
|
|
236
|
+
sock.shutdown(Socket::SHUT_RDWR) if sock.respond_to?(:shutdown)
|
|
237
|
+
rescue StandardError
|
|
238
|
+
nil
|
|
239
|
+
end
|
|
240
|
+
sock.close unless sock.closed?
|
|
241
|
+
rescue StandardError
|
|
242
|
+
nil
|
|
243
|
+
end
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
private_constant :FeatureFetch
|
|
247
|
+
|
|
248
|
+
at_exit { ChatXdk.const_get(:FeatureFetch, false).shutdown }
|
|
249
|
+
end
|