aws-sdk-core 3.165.0 → 3.201.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +462 -0
  3. data/VERSION +1 -1
  4. data/lib/aws-defaults/default_configuration.rb +4 -4
  5. data/lib/aws-sdk-core/binary/decode_handler.rb +3 -9
  6. data/lib/aws-sdk-core/binary/encode_handler.rb +1 -1
  7. data/lib/aws-sdk-core/binary/event_builder.rb +34 -37
  8. data/lib/aws-sdk-core/binary/event_stream_decoder.rb +1 -0
  9. data/lib/aws-sdk-core/binary/event_stream_encoder.rb +4 -3
  10. data/lib/aws-sdk-core/cbor/cbor_engine.rb +19 -0
  11. data/lib/aws-sdk-core/cbor/decoder.rb +310 -0
  12. data/lib/aws-sdk-core/cbor/encoder.rb +243 -0
  13. data/lib/aws-sdk-core/cbor.rb +106 -0
  14. data/lib/aws-sdk-core/client_stubs.rb +18 -14
  15. data/lib/aws-sdk-core/credential_provider.rb +4 -1
  16. data/lib/aws-sdk-core/credential_provider_chain.rb +8 -5
  17. data/lib/aws-sdk-core/ec2_metadata.rb +1 -1
  18. data/lib/aws-sdk-core/ecs_credentials.rb +178 -53
  19. data/lib/aws-sdk-core/endpoints/condition.rb +5 -0
  20. data/lib/aws-sdk-core/endpoints/endpoint_rule.rb +5 -1
  21. data/lib/aws-sdk-core/endpoints/error_rule.rb +5 -0
  22. data/lib/aws-sdk-core/endpoints/function.rb +5 -0
  23. data/lib/aws-sdk-core/endpoints/matchers.rb +18 -10
  24. data/lib/aws-sdk-core/endpoints/reference.rb +5 -0
  25. data/lib/aws-sdk-core/endpoints/rule.rb +5 -0
  26. data/lib/aws-sdk-core/endpoints/rule_set.rb +5 -0
  27. data/lib/aws-sdk-core/endpoints/rules_provider.rb +5 -0
  28. data/lib/aws-sdk-core/endpoints/templater.rb +6 -0
  29. data/lib/aws-sdk-core/endpoints/tree_rule.rb +5 -0
  30. data/lib/aws-sdk-core/endpoints/url.rb +1 -0
  31. data/lib/aws-sdk-core/endpoints.rb +69 -19
  32. data/lib/aws-sdk-core/error_handler.rb +41 -0
  33. data/lib/aws-sdk-core/errors.rb +12 -3
  34. data/lib/aws-sdk-core/event_emitter.rb +0 -16
  35. data/lib/aws-sdk-core/ini_parser.rb +7 -0
  36. data/lib/aws-sdk-core/instance_profile_credentials.rb +55 -32
  37. data/lib/aws-sdk-core/json/builder.rb +8 -1
  38. data/lib/aws-sdk-core/json/error_handler.rb +29 -14
  39. data/lib/aws-sdk-core/json/handler.rb +12 -6
  40. data/lib/aws-sdk-core/json/json_engine.rb +3 -1
  41. data/lib/aws-sdk-core/json/oj_engine.rb +7 -1
  42. data/lib/aws-sdk-core/json/parser.rb +33 -3
  43. data/lib/aws-sdk-core/json.rb +43 -14
  44. data/lib/aws-sdk-core/log/formatter.rb +6 -0
  45. data/lib/aws-sdk-core/lru_cache.rb +75 -0
  46. data/lib/aws-sdk-core/pageable_response.rb +3 -1
  47. data/lib/aws-sdk-core/param_validator.rb +9 -4
  48. data/lib/aws-sdk-core/plugins/bearer_authorization.rb +2 -0
  49. data/lib/aws-sdk-core/plugins/checksum_algorithm.rb +7 -4
  50. data/lib/aws-sdk-core/plugins/client_metrics_plugin.rb +1 -0
  51. data/lib/aws-sdk-core/plugins/client_metrics_send_plugin.rb +14 -2
  52. data/lib/aws-sdk-core/plugins/credentials_configuration.rb +2 -0
  53. data/lib/aws-sdk-core/plugins/global_configuration.rb +8 -9
  54. data/lib/aws-sdk-core/plugins/http_checksum.rb +2 -1
  55. data/lib/aws-sdk-core/plugins/invocation_id.rb +1 -11
  56. data/lib/aws-sdk-core/plugins/logging.rb +2 -0
  57. data/lib/aws-sdk-core/plugins/protocols/api_gateway.rb +3 -1
  58. data/lib/aws-sdk-core/plugins/protocols/ec2.rb +2 -24
  59. data/lib/aws-sdk-core/plugins/protocols/json_rpc.rb +6 -8
  60. data/lib/aws-sdk-core/plugins/protocols/query.rb +4 -2
  61. data/lib/aws-sdk-core/plugins/protocols/rest_json.rb +3 -15
  62. data/lib/aws-sdk-core/plugins/protocols/rest_xml.rb +3 -0
  63. data/lib/aws-sdk-core/plugins/protocols/rpc_v2.rb +17 -0
  64. data/lib/aws-sdk-core/plugins/regional_endpoint.rb +140 -35
  65. data/lib/aws-sdk-core/plugins/request_compression.rb +226 -0
  66. data/lib/aws-sdk-core/plugins/retries/error_inspector.rb +2 -1
  67. data/lib/aws-sdk-core/plugins/retry_errors.rb +12 -3
  68. data/lib/aws-sdk-core/plugins/sign.rb +43 -19
  69. data/lib/aws-sdk-core/plugins/signature_v2.rb +2 -1
  70. data/lib/aws-sdk-core/plugins/signature_v4.rb +2 -1
  71. data/lib/aws-sdk-core/plugins/stub_responses.rb +1 -0
  72. data/lib/aws-sdk-core/plugins/transfer_encoding.rb +16 -9
  73. data/lib/aws-sdk-core/plugins/user_agent.rb +152 -14
  74. data/lib/aws-sdk-core/process_credentials.rb +45 -27
  75. data/lib/aws-sdk-core/query/ec2_handler.rb +27 -0
  76. data/lib/aws-sdk-core/query/ec2_param_builder.rb +5 -7
  77. data/lib/aws-sdk-core/query/handler.rb +4 -4
  78. data/lib/aws-sdk-core/query/param_builder.rb +2 -2
  79. data/lib/aws-sdk-core/query.rb +2 -1
  80. data/lib/aws-sdk-core/refreshing_credentials.rb +12 -12
  81. data/lib/aws-sdk-core/rest/content_type_handler.rb +60 -0
  82. data/lib/aws-sdk-core/rest/handler.rb +3 -4
  83. data/lib/aws-sdk-core/rest/request/body.rb +32 -5
  84. data/lib/aws-sdk-core/rest/request/endpoint.rb +24 -4
  85. data/lib/aws-sdk-core/rest/request/headers.rb +15 -7
  86. data/lib/aws-sdk-core/rest/request/querystring_builder.rb +62 -36
  87. data/lib/aws-sdk-core/rest/response/body.rb +15 -1
  88. data/lib/aws-sdk-core/rest/response/header_list_parser.rb +79 -0
  89. data/lib/aws-sdk-core/rest/response/headers.rb +8 -3
  90. data/lib/aws-sdk-core/rest.rb +1 -0
  91. data/lib/aws-sdk-core/rpc_v2/builder.rb +62 -0
  92. data/lib/aws-sdk-core/rpc_v2/content_type_handler.rb +45 -0
  93. data/lib/aws-sdk-core/rpc_v2/error_handler.rb +84 -0
  94. data/lib/aws-sdk-core/rpc_v2/handler.rb +74 -0
  95. data/lib/aws-sdk-core/rpc_v2/parser.rb +90 -0
  96. data/lib/aws-sdk-core/rpc_v2.rb +6 -0
  97. data/lib/aws-sdk-core/shared_config.rb +77 -18
  98. data/lib/aws-sdk-core/sso_credentials.rb +80 -45
  99. data/lib/aws-sdk-core/sso_token_provider.rb +3 -2
  100. data/lib/aws-sdk-core/stubbing/protocols/rpc_v2.rb +41 -0
  101. data/lib/aws-sdk-core/stubbing/stub_data.rb +11 -0
  102. data/lib/aws-sdk-core/util.rb +39 -0
  103. data/lib/aws-sdk-core/waiters/poller.rb +4 -2
  104. data/lib/aws-sdk-core/xml/builder.rb +17 -9
  105. data/lib/aws-sdk-core/xml/error_handler.rb +32 -42
  106. data/lib/aws-sdk-core/xml/parser/frame.rb +4 -20
  107. data/lib/aws-sdk-core/xml/parser/{engines/oga.rb → oga_engine.rb} +2 -0
  108. data/lib/aws-sdk-core/xml/parser/stack.rb +2 -0
  109. data/lib/aws-sdk-core/xml/parser.rb +2 -6
  110. data/lib/aws-sdk-core.rb +8 -2
  111. data/lib/aws-sdk-sso/client.rb +103 -46
  112. data/lib/aws-sdk-sso/client_api.rb +6 -0
  113. data/lib/aws-sdk-sso/endpoint_provider.rb +41 -96
  114. data/lib/aws-sdk-sso/endpoints.rb +1 -0
  115. data/lib/aws-sdk-sso/plugins/endpoints.rb +4 -2
  116. data/lib/aws-sdk-sso/types.rb +0 -35
  117. data/lib/aws-sdk-sso.rb +1 -1
  118. data/lib/aws-sdk-ssooidc/client.rb +488 -74
  119. data/lib/aws-sdk-ssooidc/client_api.rb +82 -1
  120. data/lib/aws-sdk-ssooidc/endpoint_provider.rb +41 -95
  121. data/lib/aws-sdk-ssooidc/endpoints.rb +15 -0
  122. data/lib/aws-sdk-ssooidc/errors.rb +52 -0
  123. data/lib/aws-sdk-ssooidc/plugins/endpoints.rb +6 -2
  124. data/lib/aws-sdk-ssooidc/types.rb +372 -83
  125. data/lib/aws-sdk-ssooidc.rb +1 -1
  126. data/lib/aws-sdk-sts/client.rb +361 -288
  127. data/lib/aws-sdk-sts/client_api.rb +24 -11
  128. data/lib/aws-sdk-sts/endpoint_provider.rb +96 -213
  129. data/lib/aws-sdk-sts/endpoints.rb +1 -0
  130. data/lib/aws-sdk-sts/plugins/endpoints.rb +4 -2
  131. data/lib/aws-sdk-sts/presigner.rb +1 -1
  132. data/lib/aws-sdk-sts/types.rb +128 -197
  133. data/lib/aws-sdk-sts.rb +1 -1
  134. data/lib/seahorse/client/async_base.rb +1 -1
  135. data/lib/seahorse/client/async_response.rb +19 -0
  136. data/lib/seahorse/client/base.rb +18 -7
  137. data/lib/seahorse/client/configuration.rb +0 -4
  138. data/lib/seahorse/client/h2/connection.rb +12 -11
  139. data/lib/seahorse/client/h2/handler.rb +1 -0
  140. data/lib/seahorse/client/handler.rb +1 -1
  141. data/lib/seahorse/client/net_http/connection_pool.rb +3 -9
  142. data/lib/seahorse/client/net_http/patches.rb +1 -4
  143. data/lib/seahorse/client/plugin.rb +9 -0
  144. data/lib/seahorse/client/plugins/endpoint.rb +0 -1
  145. data/lib/seahorse/client/plugins/h2.rb +3 -3
  146. data/lib/seahorse/client/plugins/net_http.rb +48 -16
  147. data/lib/seahorse/client/plugins/request_callback.rb +31 -0
  148. data/lib/seahorse/client/response.rb +6 -0
  149. data/lib/seahorse/model/operation.rb +3 -0
  150. data/lib/seahorse/model/shapes.rb +2 -2
  151. data/sig/aws-sdk-core/client_stubs.rbs +10 -0
  152. data/sig/aws-sdk-core/errors.rbs +22 -0
  153. data/sig/aws-sdk-core/resources/collection.rbs +21 -0
  154. data/sig/aws-sdk-core/structure.rbs +4 -0
  155. data/sig/aws-sdk-core/waiters/errors.rbs +20 -0
  156. data/sig/aws-sdk-core.rbs +7 -0
  157. data/sig/seahorse/client/base.rbs +25 -0
  158. data/sig/seahorse/client/handler_builder.rbs +16 -0
  159. data/sig/seahorse/client/response.rbs +61 -0
  160. metadata +44 -17
  161. /data/lib/aws-sdk-core/xml/parser/{engines/libxml.rb → libxml_engine.rb} +0 -0
  162. /data/lib/aws-sdk-core/xml/parser/{engines/nokogiri.rb → nokogiri_engine.rb} +0 -0
  163. /data/lib/aws-sdk-core/xml/parser/{engines/ox.rb → ox_engine.rb} +0 -0
  164. /data/lib/aws-sdk-core/xml/parser/{engines/rexml.rb → rexml_engine.rb} +0 -0
@@ -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
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aws
4
+ # @api private
5
+ module Cbor
6
+
7
+ # CBOR Tagged data (Major type 6).
8
+ # A Tag consists of a tag number and a value.
9
+ # In the extended generic data model, a tag number's definition
10
+ # describes the additional semantics conveyed with the tag number.
11
+ # # @!method initialize(*args)
12
+ # @option args [Integer] :tag The tag number.
13
+ # @option args [Object] :value The tag's content.
14
+ # @!attribute tag
15
+ # The tag number.
16
+ # @return [Integer]
17
+ # @!attribute value
18
+ # The tag's content.
19
+ # @return [Object]
20
+ Tagged = Struct.new(:tag, :value)
21
+
22
+ class Error < StandardError; end
23
+
24
+ class OutOfBytesError < Error
25
+ def initialize(n, left)
26
+ super("Out of bytes. Trying to read #{n} bytes but buffer contains only #{left}")
27
+ end
28
+ end
29
+
30
+ class UnknownTypeError < Error
31
+ def initialize(type)
32
+ super("Unable to encode #{type}")
33
+ end
34
+ end
35
+
36
+ class ExtraBytesError < Error
37
+ def initialize(pos, size)
38
+ super("Extra bytes follow after decoding item. Read #{pos} / #{size} bytes")
39
+ end
40
+ end
41
+
42
+ class UnexpectedBreakCodeError < Error; end
43
+
44
+ class UnexpectedAdditionalInformationError < Error
45
+ def initialize(add_info)
46
+ super("Unexpected additional information: #{add_info}")
47
+ end
48
+ end
49
+
50
+ class << self
51
+ # @param [Symbol,Class] engine
52
+ # Must be one of the following values:
53
+ #
54
+ # * :cbor
55
+ #
56
+ def engine=(engine)
57
+ @engine = Class === engine ? engine : load_engine(engine)
58
+ end
59
+
60
+ # @return [Class] Returns the default engine.
61
+ # One of:
62
+ #
63
+ # * {CborEngine}
64
+ #
65
+ def engine
66
+ set_default_engine unless @engine
67
+ @engine
68
+ end
69
+
70
+ def encode(data)
71
+ @engine.encode(data)
72
+ end
73
+
74
+ def decode(bytes)
75
+ bytes.force_encoding(Encoding::BINARY)
76
+ @engine.decode(bytes)
77
+ end
78
+
79
+ def set_default_engine
80
+ [:cbor].each do |name|
81
+ @engine ||= try_load_engine(name)
82
+ end
83
+
84
+ unless @engine
85
+ raise 'Unable to find a compatible cbor library.'
86
+ end
87
+ end
88
+
89
+ private
90
+
91
+ def load_engine(name)
92
+ require "aws-sdk-core/cbor/#{name}_engine"
93
+ const_name = name[0].upcase + name[1..-1] + 'Engine'
94
+ const_get(const_name)
95
+ end
96
+
97
+ def try_load_engine(name)
98
+ load_engine(name)
99
+ rescue LoadError
100
+ false
101
+ end
102
+ end
103
+
104
+ set_default_engine
105
+ end
106
+ end