aws-sdk-core 3.196.1 → 3.199.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.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +39 -0
  3. data/VERSION +1 -1
  4. data/lib/aws-sdk-core/binary/decode_handler.rb +3 -4
  5. data/lib/aws-sdk-core/binary/encode_handler.rb +1 -1
  6. data/lib/aws-sdk-core/binary/event_stream_decoder.rb +1 -0
  7. data/lib/aws-sdk-core/binary/event_stream_encoder.rb +4 -3
  8. data/lib/aws-sdk-core/cbor/cbor_engine.rb +19 -0
  9. data/lib/aws-sdk-core/cbor/decoder.rb +310 -0
  10. data/lib/aws-sdk-core/cbor/encoder.rb +243 -0
  11. data/lib/aws-sdk-core/cbor.rb +106 -0
  12. data/lib/aws-sdk-core/client_stubs.rb +3 -2
  13. data/lib/aws-sdk-core/endpoints/matchers.rb +5 -1
  14. data/lib/aws-sdk-core/error_handler.rb +41 -0
  15. data/lib/aws-sdk-core/json/error_handler.rb +6 -8
  16. data/lib/aws-sdk-core/json/handler.rb +5 -6
  17. data/lib/aws-sdk-core/json/json_engine.rb +3 -1
  18. data/lib/aws-sdk-core/json/oj_engine.rb +7 -1
  19. data/lib/aws-sdk-core/json/parser.rb +2 -0
  20. data/lib/aws-sdk-core/json.rb +43 -14
  21. data/lib/aws-sdk-core/pageable_response.rb +1 -1
  22. data/lib/aws-sdk-core/plugins/client_metrics_send_plugin.rb +14 -2
  23. data/lib/aws-sdk-core/plugins/global_configuration.rb +8 -9
  24. data/lib/aws-sdk-core/plugins/protocols/api_gateway.rb +3 -1
  25. data/lib/aws-sdk-core/plugins/protocols/ec2.rb +2 -24
  26. data/lib/aws-sdk-core/plugins/protocols/json_rpc.rb +6 -8
  27. data/lib/aws-sdk-core/plugins/protocols/query.rb +4 -2
  28. data/lib/aws-sdk-core/plugins/protocols/rest_json.rb +4 -3
  29. data/lib/aws-sdk-core/plugins/protocols/rest_xml.rb +5 -1
  30. data/lib/aws-sdk-core/plugins/protocols/rpc_v2.rb +17 -0
  31. data/lib/aws-sdk-core/plugins/request_compression.rb +10 -1
  32. data/lib/aws-sdk-core/plugins/retry_errors.rb +10 -3
  33. data/lib/aws-sdk-core/plugins/user_agent.rb +58 -24
  34. data/lib/aws-sdk-core/process_credentials.rb +45 -27
  35. data/lib/aws-sdk-core/query/ec2_handler.rb +27 -0
  36. data/lib/aws-sdk-core/query/handler.rb +4 -4
  37. data/lib/aws-sdk-core/query.rb +2 -1
  38. data/lib/aws-sdk-core/rest/{request/content_type.rb → content_type_handler.rb} +1 -1
  39. data/lib/aws-sdk-core/rest/handler.rb +3 -4
  40. data/lib/aws-sdk-core/rest/request/endpoint.rb +3 -1
  41. data/lib/aws-sdk-core/rest.rb +1 -1
  42. data/lib/aws-sdk-core/rpc_v2/builder.rb +62 -0
  43. data/lib/aws-sdk-core/rpc_v2/content_type_handler.rb +45 -0
  44. data/lib/aws-sdk-core/rpc_v2/error_handler.rb +84 -0
  45. data/lib/aws-sdk-core/rpc_v2/handler.rb +74 -0
  46. data/lib/aws-sdk-core/rpc_v2/parser.rb +90 -0
  47. data/lib/aws-sdk-core/rpc_v2.rb +6 -0
  48. data/lib/aws-sdk-core/stubbing/protocols/rpc_v2.rb +41 -0
  49. data/lib/aws-sdk-core/util.rb +4 -4
  50. data/lib/aws-sdk-core/waiters/poller.rb +1 -1
  51. data/lib/aws-sdk-core/xml/error_handler.rb +11 -37
  52. data/lib/aws-sdk-core/xml/parser.rb +2 -6
  53. data/lib/aws-sdk-core.rb +6 -2
  54. data/lib/aws-sdk-sso/client.rb +6 -3
  55. data/lib/aws-sdk-sso.rb +1 -1
  56. data/lib/aws-sdk-ssooidc/client.rb +6 -3
  57. data/lib/aws-sdk-ssooidc.rb +1 -1
  58. data/lib/aws-sdk-sts/client.rb +6 -3
  59. data/lib/aws-sdk-sts.rb +1 -1
  60. data/lib/seahorse/client/base.rb +17 -7
  61. data/lib/seahorse/client/handler.rb +1 -1
  62. data/lib/seahorse/client/plugins/endpoint.rb +0 -1
  63. metadata +22 -8
  64. /data/lib/aws-sdk-core/xml/parser/{engines/libxml.rb → libxml_engine.rb} +0 -0
  65. /data/lib/aws-sdk-core/xml/parser/{engines/nokogiri.rb → nokogiri_engine.rb} +0 -0
  66. /data/lib/aws-sdk-core/xml/parser/{engines/oga.rb → oga_engine.rb} +0 -0
  67. /data/lib/aws-sdk-core/xml/parser/{engines/ox.rb → ox_engine.rb} +0 -0
  68. /data/lib/aws-sdk-core/xml/parser/{engines/rexml.rb → rexml_engine.rb} +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b77f2eeb94a52526e385b422b59703e582c75af70402ab009b66d542694259c8
4
- data.tar.gz: e97d33313f38d3b6dd565a02b3c1ccdc34d5960cd076ace3449fe50f7f75f2b6
3
+ metadata.gz: d895f327aaebb1d2a4a50f3fd1c4e76e36104438bbf02f6d04399480561b4ef8
4
+ data.tar.gz: a060bd7053cb741fed767a7e71033dee9c8dd1dfd96fe31e4fa2d8525c0c61f9
5
5
  SHA512:
6
- metadata.gz: 2efdc811c495c91558cf5144599eff67f2e828a94d90d4fd31d49575a9bff4456e7f237aa79853cdb6f8fba016b9011cc8f6d8677596c85a9e6179f606431961
7
- data.tar.gz: e933ae0dc9b2d77e0455a1af5f5c635833b394ec2f70f90133af52993992134b56481952124d5a2448182a6c180c4cb1483114667ea5d2437891e22a739e3f44
6
+ metadata.gz: 4f91a98d16fc772e4ea6500b6ae6cbc1440666d7adee2bbbe2ed2f3fc5b9bae3ddfc31434c61ba01f109555a17730d0ff2429d5268e39c90eac91e9bb1cf40bc
7
+ data.tar.gz: c5f0a55469f8b2800ba5b380da353cee13e4e876174cb1e1c0959c192ae1281379074255fc095f64cfa3ef1fd2d7926a74b681523256e28e2b42c2019bffe498
data/CHANGELOG.md CHANGED
@@ -1,6 +1,45 @@
1
1
  Unreleased Changes
2
2
  ------------------
3
3
 
4
+ 3.199.0 (2024-06-25)
5
+ ------------------
6
+
7
+ * Feature - Support RpcV2 protocol.
8
+
9
+ * Feature - Add CBOR encoder and decoder.
10
+
11
+ * Issue - Enhance, refactor, and rebase protocols to be consistent. Ensure protocol tests are run for each engine.
12
+
13
+ 3.198.0 (2024-06-24)
14
+ ------------------
15
+
16
+ * Feature - Updated Aws::STS::Client with the latest API changes.
17
+
18
+ * Feature - Updated Aws::SSOOIDC::Client with the latest API changes.
19
+
20
+ * Feature - Updated Aws::SSO::Client with the latest API changes.
21
+
22
+ * Feature - Support `:plugins` option on all Clients or using `Aws.config[:plugins]`.
23
+
24
+ 3.197.2 (2024-06-20)
25
+ ------------------
26
+
27
+ * Issue - fix issue in Endpoint `attr` matcher when path is only an array index.
28
+
29
+ * Issue - Fix trailing slash in endpoint URLs for rest-json and rest-xml services.
30
+
31
+ 3.197.1 (2024-06-19)
32
+ ------------------
33
+
34
+ * Issue - Support an array of string arguments for `Aws::ProcessCredentials` to be executed by `system`.
35
+
36
+ 3.197.0 (2024-06-05)
37
+ ------------------
38
+
39
+ * Issue - Ensure no UTC offset when deserializing `iso8601` timestamp format values.
40
+
41
+ * Feature - Bump User Agent to version 2.1 to track business metrics. This changes the User Agent plugin order to be just before sending.
42
+
4
43
  3.196.1 (2024-05-14)
5
44
  ------------------
6
45
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 3.196.1
1
+ 3.199.0
@@ -22,12 +22,11 @@ module Aws
22
22
  end
23
23
 
24
24
  def attach_eventstream_listeners(context, rules)
25
-
26
25
  context.http_response.on_headers(200) do
27
- protocol = context.config.api.metadata['protocol']
28
- output_handler = context[:output_event_stream_handler] || context[:event_stream_handler]
26
+ output_handler = context[:output_event_stream_handler] ||
27
+ context[:event_stream_handler]
29
28
  context.http_response.body = EventStreamDecoder.new(
30
- protocol,
29
+ context.config.protocol,
31
30
  rules,
32
31
  context.operation.output,
33
32
  context.operation.errors,
@@ -10,7 +10,7 @@ module Aws
10
10
  if eventstream_member = eventstream_input?(context)
11
11
  input_es_handler = context[:input_event_stream_handler]
12
12
  input_es_handler.event_emitter.encoder = EventStreamEncoder.new(
13
- context.config.api.metadata['protocol'],
13
+ context.config.protocol,
14
14
  eventstream_member,
15
15
  context.operation.input,
16
16
  signer_for(context)
@@ -47,6 +47,7 @@ module Aws
47
47
  when 'rest-xml' then Aws::Xml::Parser
48
48
  when 'rest-json' then Aws::Json::Parser
49
49
  when 'json' then Aws::Json::Parser
50
+ when 'smithy-rpc-v2-cbor' then Aws::RpcV2::Parser
50
51
  else raise "unsupported protocol #{protocol} for event stream"
51
52
  end
52
53
  end
@@ -43,9 +43,10 @@ module Aws
43
43
 
44
44
  def serializer_class(protocol)
45
45
  case protocol
46
- when 'rest-xml' then Xml::Builder
47
- when 'rest-json' then Json::Builder
48
- when 'json' then Json::Builder
46
+ when 'rest-xml' then Aws::Xml::Builder
47
+ when 'rest-json' then Aws::Json::Builder
48
+ when 'json' then Aws::Json::Builder
49
+ when 'smithy-rpc-v2-cbor' then Aws::RpcV2::Builder
49
50
  else raise "unsupported protocol #{protocol} for event stream"
50
51
  end
51
52
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'encoder'
4
+ require_relative 'decoder'
5
+
6
+ module Aws
7
+ module Cbor
8
+ # Pure Ruby implementation of CBOR encode and decode
9
+ module CborEngine
10
+ def self.encode(data)
11
+ Encoder.new.add(data).bytes
12
+ end
13
+
14
+ def self.decode(bytes)
15
+ Decoder.new(bytes.force_encoding(Encoding::BINARY)).decode
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,310 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aws
4
+ module Cbor
5
+ # Pure Ruby implementation of CBOR Decoder
6
+ class Decoder
7
+ def initialize(bytes)
8
+ @buffer = bytes
9
+ @pos = 0
10
+ end
11
+
12
+ def decode
13
+ return nil if @buffer.nil? || @buffer.empty?
14
+
15
+ val = decode_item
16
+ return val unless @pos != @buffer.size
17
+
18
+ raise ExtraBytesError.new(@pos, @buffer.size)
19
+ end
20
+
21
+ private
22
+
23
+ FIVE_BIT_MASK = 0x1F
24
+ TAG_TYPE_EPOCH = 1
25
+ TAG_TYPE_BIGNUM = 2
26
+ TAG_TYPE_NEG_BIGNUM = 3
27
+ TAG_TYPE_BIGDEC = 4
28
+
29
+ # high level, generic decode. Based on the next type. Consumes and returns
30
+ # the next item as a ruby object.
31
+ def decode_item
32
+ case (next_type = peek_type)
33
+ when :array
34
+ read_array.times.map { decode_item }
35
+ when :map
36
+ read_map.times.map { [read_string, decode_item] }.to_h
37
+ when :indefinite_array
38
+ read_start_indefinite_array
39
+ value = []
40
+ value << decode_item until peek_type == :break_stop_code
41
+ read_end_indefinite_collection
42
+ value
43
+ when :indefinite_map
44
+ read_start_indefinite_map
45
+ value = {}
46
+ value[read_string] = decode_item until peek_type == :break_stop_code
47
+ read_end_indefinite_collection
48
+ value
49
+ when :indefinite_binary_string
50
+ read_info
51
+ value = String.new
52
+ value << read_binary_string until peek_type == :break_stop_code
53
+ read_end_indefinite_collection
54
+ value
55
+ when :indefinite_string
56
+ read_info
57
+ value = String.new
58
+ value << read_string until peek_type == :break_stop_code
59
+ read_end_indefinite_collection
60
+ value.force_encoding(Encoding::UTF_8)
61
+ when :tag
62
+ case (tag = read_tag)
63
+ when TAG_TYPE_EPOCH
64
+ type = peek_type
65
+ item = decode_item
66
+ item /= 1000.0 if type == :integer
67
+ Time.at(item)
68
+ when TAG_TYPE_BIGNUM, TAG_TYPE_NEG_BIGNUM
69
+ read_bignum(tag)
70
+ when TAG_TYPE_BIGDEC
71
+ read_big_decimal
72
+ else
73
+ Tagged.new(tag, decode_item)
74
+ end
75
+ when :break_stop_code
76
+ raise UnexpectedBreakCodeError
77
+ else
78
+ send("read_#{next_type}")
79
+ end
80
+ end
81
+
82
+ # low level streaming interface
83
+ def peek_type
84
+ ib = peek(1).ord
85
+ add_info = ib & FIVE_BIT_MASK
86
+ major_type = ib >> 5
87
+ case major_type
88
+ when 0, 1 then :integer
89
+ when 2
90
+ add_info == 31 ? :indefinite_binary_string : :binary_string
91
+ when 3
92
+ add_info == 31 ? :indefinite_string : :string
93
+ when 4
94
+ add_info == 31 ? :indefinite_array : :array
95
+ when 5
96
+ add_info == 31 ? :indefinite_map : :map
97
+ when 6 then :tag
98
+ when 7 # simple or float
99
+ case add_info
100
+ when 20, 21 then :boolean
101
+ when 22 then :nil
102
+ when 23 then :undefined # for smithy, this should be parsed as nil
103
+ when 25 then :half
104
+ when 26 then :float
105
+ when 27 then :double
106
+ when 31 then :break_stop_code
107
+ else
108
+ :reserved_undefined
109
+ end
110
+ end
111
+ end
112
+
113
+ def read_break_stop_code
114
+ read_info
115
+ :break_stop_code
116
+ end
117
+
118
+ def read_integer
119
+ major_type, add_info = read_info
120
+
121
+ val = read_count(add_info)
122
+ case major_type
123
+ when 0 then val
124
+ when 1 then -1 - val
125
+ else
126
+ raise Error,
127
+ "Expected Integer (0,1) got major type: #{major_type}"
128
+ end
129
+ end
130
+
131
+ def read_binary_string
132
+ _major_type, add_info = read_info
133
+ take(read_count(add_info)).force_encoding(Encoding::BINARY)
134
+ end
135
+
136
+ def read_string
137
+ _major_type, add_info = read_info
138
+ take(read_count(add_info)).force_encoding(Encoding::UTF_8)
139
+ end
140
+
141
+ # returns only the length of the array, caller must read the correct number of values after this
142
+ def read_array
143
+ _major_type, add_info = read_info
144
+ read_count(add_info)
145
+ end
146
+
147
+ # returns nothing but consumes and checks the type/info.
148
+ # Caller must keep reading until encountering the stop sequence
149
+ def read_start_indefinite_array
150
+ read_info
151
+ end
152
+
153
+ # returns nothing but consumes and checks the type/info.
154
+ # Caller must keep reading until encountering the stop sequence
155
+ def read_start_indefinite_map
156
+ read_info
157
+ end
158
+
159
+ # returns nothing but consumes and checks the type/info.
160
+ def read_end_indefinite_collection
161
+ read_info
162
+ end
163
+
164
+ # returns only the length of the array, caller must read the correct number of key value pairs after this
165
+ def read_map
166
+ _major_type, add_info = read_info
167
+ read_count(add_info)
168
+ end
169
+
170
+ # returns only the tag, caller must interpret the tag and read another value as appropriate
171
+ def read_tag
172
+ _major_type, add_info = read_info
173
+ read_count(add_info)
174
+ end
175
+
176
+ def read_reserved_undefined
177
+ _major_type, add_info = read_info
178
+ raise Error,
179
+ "Undefined reserved additional information: #{add_info}"
180
+ end
181
+
182
+ def read_boolean
183
+ _major_type, add_info = read_info
184
+ case add_info
185
+ when 20 then false
186
+ when 21 then true
187
+ else
188
+ raise Error,
189
+ 'Invalid Boolean simple type, expected add_info of 20 or 21, ' \
190
+ "got: #{add_info}"
191
+ end
192
+ end
193
+
194
+ def read_nil
195
+ read_info
196
+ nil
197
+ end
198
+
199
+ def read_undefined
200
+ read_info
201
+ :undefined
202
+ end
203
+
204
+ # 16 bit IEEE 754 half-precision floats
205
+ # Support decoding only
206
+ # format:
207
+ # sign - 1 bit
208
+ # exponent - 5 bits
209
+ # precision - 10 bits
210
+ def read_half
211
+ read_info
212
+ b16 = take(2).unpack1('n')
213
+ exp = (b16 >> 10) & 0x1f
214
+ mant = b16 & 0x3ff
215
+ val =
216
+ case exp
217
+ when 0
218
+ Math.ldexp(mant, -24)
219
+ when 31
220
+ mant.zero? ? Float::INFINITY : Float::NAN
221
+ else
222
+ # exp bias is 15, but to use ldexp we divide by 1024 (2^10) to get
223
+ # exp-15-10
224
+ Math.ldexp(1024 + mant, exp - 25)
225
+ end
226
+ if (b16[15]).zero?
227
+ val
228
+ else
229
+ -val
230
+ end
231
+ end
232
+
233
+ def read_float
234
+ read_info
235
+ take(4).unpack1('g')
236
+ end
237
+
238
+ def read_double
239
+ read_info
240
+ take(8).unpack1('G')
241
+ end
242
+
243
+ # tag type 2 or 3
244
+ def read_bignum(tag_value)
245
+ _major_type, add_info = read_info
246
+ bstr = take(read_count(add_info))
247
+ v = bstr.bytes.inject(0) do |sum, b|
248
+ sum <<= 8
249
+ sum + b
250
+ end
251
+ case tag_value
252
+ when 2 then v
253
+ when 3 then -1 - v
254
+ else
255
+ raise Error,
256
+ 'Invalid Tag value for BigNum, ' \
257
+ "expected 2 or 3, got: #{tag_value}"
258
+ end
259
+ end
260
+
261
+ # A decimal fraction or a bigfloat is represented as a tagged array
262
+ # that contains exactly two integer numbers:
263
+ # an exponent e and a mantissa m
264
+ # See: https://www.rfc-editor.org/rfc/rfc8949.html#name-decimal-fractions-and-bigfl
265
+ def read_big_decimal
266
+ unless (s = read_array) == 2
267
+ raise Error, "Expected array of length 2 but length is: #{s}"
268
+ end
269
+
270
+ e = read_integer
271
+ m = read_integer
272
+ BigDecimal(m) * (BigDecimal(10)**BigDecimal(e))
273
+ end
274
+
275
+ # return a tuple of major_type, add_info
276
+ def read_info
277
+ ib = take(1).ord
278
+ [ib >> 5, ib & FIVE_BIT_MASK]
279
+ end
280
+
281
+ def read_count(add_info)
282
+ case add_info
283
+ when 0..23 then add_info
284
+ when 24 then take(1).ord
285
+ when 25 then take(2).unpack1('n')
286
+ when 26 then take(4).unpack1('N')
287
+ when 27 then take(8).unpack1('Q>')
288
+ when 28 then take(16).unpack1('Q>')
289
+ when 29 then take(32).unpack1('Q>')
290
+ else raise UnexpectedAdditionalInformationError, add_info
291
+ end
292
+ end
293
+
294
+ def take(n_bytes)
295
+ opos = @pos
296
+ @pos += n_bytes
297
+
298
+ return @buffer[opos, n_bytes] if @pos <= @buffer.bytesize
299
+
300
+ raise OutOfBytesError.new(n_bytes, @buffer.bytesize - @pos)
301
+ end
302
+
303
+ def peek(n_bytes)
304
+ return @buffer[@pos, n_bytes] if (@pos + n_bytes) <= @buffer.bytesize
305
+
306
+ raise OutOfBytesError.new(n_bytes, @buffer.bytesize - @pos)
307
+ end
308
+ end
309
+ end
310
+ end
@@ -0,0 +1,243 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bigdecimal'
4
+
5
+ module Aws
6
+ module Cbor
7
+ # Pure ruby implementation of CBOR encoder.
8
+ class Encoder
9
+ def initialize
10
+ @buffer = String.new
11
+ end
12
+
13
+ # @return the encoded bytes in CBOR format for all added data
14
+ def bytes
15
+ @buffer
16
+ end
17
+
18
+ # generic method for adding generic Ruby data based on its type
19
+ def add(value)
20
+ case value
21
+ when BigDecimal then add_big_decimal(value)
22
+ when Integer then add_auto_integer(value)
23
+ when Numeric then add_auto_float(value)
24
+ when Symbol then add_string(value.to_s)
25
+ when true, false then add_boolean(value)
26
+ when nil then add_nil
27
+ when Tagged
28
+ add_tag(value.tag)
29
+ add(value.value)
30
+ when String
31
+ if value.encoding == Encoding::BINARY
32
+ add_byte_string(value)
33
+ else
34
+ add_string(value)
35
+ end
36
+ when Array
37
+ start_array(value.size)
38
+ value.each { |di| add(di) }
39
+ when Hash
40
+ start_map(value.size)
41
+ value.each do |k, v|
42
+ add(k)
43
+ add(v)
44
+ end
45
+ when Time
46
+ add_time(value)
47
+ else
48
+ raise UnknownTypeError, value
49
+ end
50
+ self
51
+ end
52
+
53
+ private
54
+
55
+ MAJOR_TYPE_UNSIGNED_INT = 0x00 # 000_00000 - Major Type 0 - unsigned int
56
+ MAJOR_TYPE_NEGATIVE_INT = 0x20 # 001_00000 - Major Type 1 - negative int
57
+ MAJOR_TYPE_BYTE_STR = 0x40 # 010_00000 - Major Type 2 (Byte String)
58
+ MAJOR_TYPE_STR = 0x60 # 011_00000 - Major Type 3 (Text String)
59
+ MAJOR_TYPE_ARRAY = 0x80 # 100_00000 - Major Type 4 (Array)
60
+ MAJOR_TYPE_MAP = 0xa0 # 101_00000 - Major Type 5 (Map)
61
+ MAJOR_TYPE_TAG = 0xc0 # 110_00000 - Major type 6 (Tag)
62
+ MAJOR_TYPE_SIMPLE = 0xe0 # 111_00000 - Major type 7 (111) + 5 bit 0
63
+
64
+ FLOAT_BYTES = 0xfa # 111_11010 - Major type 7 (Float) + value: 26
65
+ DOUBLE_BYTES = 0xfb # 111_ 11011 - Major type 7 (Float) + value: 26
66
+
67
+ # https://www.rfc-editor.org/rfc/rfc8949.html#tags
68
+ TAG_TYPE_EPOCH = 1
69
+ TAG_BIGNUM_BASE = 2
70
+ TAG_TYPE_BIGDEC = 4
71
+
72
+ MAX_INTEGER = 18_446_744_073_709_551_616 # 2^64
73
+
74
+ def head(major_type, value)
75
+ @buffer <<
76
+ case value
77
+ when 0...24
78
+ [major_type + value].pack('C') # 8-bit unsigned
79
+ when 0...256
80
+ [major_type + 24, value].pack('CC')
81
+ when 0...65_536
82
+ [major_type + 25, value].pack('Cn')
83
+ when 0...4_294_967_296
84
+ [major_type + 26, value].pack('CN')
85
+ when 0...MAX_INTEGER
86
+ [major_type + 27, value].pack('CQ>')
87
+ else
88
+ raise Error, "Value is too large to encode: #{value}"
89
+ end
90
+ end
91
+
92
+ # streaming style, lower level interface
93
+ def add_integer(value)
94
+ major_type =
95
+ if value.negative?
96
+ value = -1 - value
97
+ MAJOR_TYPE_NEGATIVE_INT
98
+ else
99
+ MAJOR_TYPE_UNSIGNED_INT
100
+ end
101
+ head(major_type, value)
102
+ end
103
+
104
+ def add_bignum(value)
105
+ major_type =
106
+ if value.negative?
107
+ value = -1 - value
108
+ MAJOR_TYPE_NEGATIVE_INT
109
+ else
110
+ MAJOR_TYPE_UNSIGNED_INT
111
+ end
112
+ s = bignum_to_bytes(value)
113
+ head(MAJOR_TYPE_TAG, TAG_BIGNUM_BASE + (major_type >> 5))
114
+ head(MAJOR_TYPE_BYTE_STR, s.bytesize)
115
+ @buffer << s
116
+ end
117
+
118
+ # A decimal fraction or a bigfloat is represented as a tagged array
119
+ # that contains exactly two integer numbers:
120
+ # an exponent e and a mantissa m
121
+ # decimal fractions are always represented with a base of 10
122
+ # See: https://www.rfc-editor.org/rfc/rfc8949.html#name-decimal-fractions-and-bigfl
123
+ def add_big_decimal(value)
124
+ if value.infinite? == 1
125
+ return add_float(value.infinite? * Float::INFINITY)
126
+ elsif value.nan?
127
+ return add_float(Float::NAN)
128
+ end
129
+
130
+ head(MAJOR_TYPE_TAG, TAG_TYPE_BIGDEC)
131
+ sign, digits, base, exp = value.split
132
+ # Ruby BigDecimal digits of XXX are used as 0.XXX, convert
133
+ exp = exp - digits.size
134
+ digits = sign * digits.to_i
135
+ start_array(2)
136
+ add_auto_integer(exp)
137
+ add_auto_integer(digits)
138
+ end
139
+
140
+ def add_auto_integer(value)
141
+ major_type =
142
+ if value.negative?
143
+ value = -1 - value
144
+ MAJOR_TYPE_NEGATIVE_INT
145
+ else
146
+ MAJOR_TYPE_UNSIGNED_INT
147
+ end
148
+
149
+ if value >= MAX_INTEGER
150
+ s = bignum_to_bytes(value)
151
+ head(MAJOR_TYPE_TAG, TAG_BIGNUM_BASE + (major_type >> 5))
152
+ head(MAJOR_TYPE_BYTE_STR, s.bytesize)
153
+ @buffer << s
154
+ else
155
+ head(major_type, value)
156
+ end
157
+ end
158
+
159
+ def add_float(value)
160
+ @buffer << [FLOAT_BYTES, value].pack('Cg') # single-precision
161
+ end
162
+
163
+ def add_double(value)
164
+ @buffer << [DOUBLE_BYTES, value].pack('CG') # double-precision
165
+ end
166
+
167
+ def add_auto_float(value)
168
+ if value.nan?
169
+ @buffer << FLOAT_BYTES << [value].pack('g')
170
+ else
171
+ ss = [value].pack('g') # single-precision
172
+ if ss.unpack1('g') == value
173
+ @buffer << FLOAT_BYTES << ss
174
+ else
175
+ @buffer << [DOUBLE_BYTES, value].pack('CG') # double-precision
176
+ end
177
+ end
178
+ end
179
+
180
+ def add_nil
181
+ head(MAJOR_TYPE_SIMPLE, 22)
182
+ end
183
+
184
+ def add_boolean(value)
185
+ value ? head(MAJOR_TYPE_SIMPLE, 21) : head(MAJOR_TYPE_SIMPLE, 20)
186
+ end
187
+
188
+ # Encoding MUST already be Encoding::BINARY
189
+ def add_byte_string(value)
190
+ head(MAJOR_TYPE_BYTE_STR, value.bytesize)
191
+ @buffer << value
192
+ end
193
+
194
+ def add_string(value)
195
+ value = value.encode(Encoding::UTF_8).force_encoding(Encoding::BINARY)
196
+ head(MAJOR_TYPE_STR, value.bytesize)
197
+ @buffer << value
198
+ end
199
+
200
+ # caller is responsible for adding length values
201
+ def start_array(length)
202
+ head(MAJOR_TYPE_ARRAY, length)
203
+ end
204
+
205
+ def start_indefinite_array
206
+ head(MAJOR_TYPE_ARRAY + 31, 0)
207
+ end
208
+
209
+ # caller is responsible for adding length key/value pairs
210
+ def start_map(length)
211
+ head(MAJOR_TYPE_MAP, length)
212
+ end
213
+
214
+ def start_indefinite_map
215
+ head(MAJOR_TYPE_MAP + 31, 0)
216
+ end
217
+
218
+ def end_indefinite_collection
219
+ # write the stop sequence
220
+ head(MAJOR_TYPE_SIMPLE + 31, 0)
221
+ end
222
+
223
+ def add_tag(tag)
224
+ head(MAJOR_TYPE_TAG, tag)
225
+ end
226
+
227
+ def add_time(value)
228
+ head(MAJOR_TYPE_TAG, TAG_TYPE_EPOCH)
229
+ epoch_ms = (value.to_f * 1000).to_i
230
+ add_integer(epoch_ms)
231
+ end
232
+
233
+ def bignum_to_bytes(value)
234
+ s = String.new
235
+ while value != 0
236
+ s << (value & 0xFF)
237
+ value >>= 8
238
+ end
239
+ s.reverse!
240
+ end
241
+ end
242
+ end
243
+ end