aws-eventstream 1.0.3 → 1.3.2

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
- SHA1:
3
- metadata.gz: f7aee8044bf87469f60f9a7fa8a7b28069a28a72
4
- data.tar.gz: ab363b06b8d2e45da754e5dffc707dd68fdac1f9
2
+ SHA256:
3
+ metadata.gz: 9f7607916d853599e76825eca92307793809152833c3e03d46668641662ed989
4
+ data.tar.gz: 7af13e44c46b8428daf19e6ecb42af386ec1b99ec49dbd692969d3a4ef161469
5
5
  SHA512:
6
- metadata.gz: 8da3c6a510ab3355814264258350c8930847ddc0c1197ecfb59508eb9e37e97297572e0e7ea429953d3a1ac05cb5b070201c7054915986f1d630b4ec9da4dc3f
7
- data.tar.gz: 2aff7fe4e8b4a0c07d7a2a35946066c376b5341a983a2da13059c8bdf8ba40add81f9c19a2e0b7b2441f375b43e3ee12a6e84681506b350befa2b0208a4e5486
6
+ metadata.gz: 7dc49237b6a85046c15d076ff8c7245745ae661970686ead73c1975cd801802fc8d65255657e41c957cc7d05809820608ac5c4f0f18518387aa2390e6a731e6a
7
+ data.tar.gz: 184af3549253e984f4b7e64db8dd59216ed42e0b5c945fcf79bff50706acdc53b57707ded32dac101a6078dc09335d3ce763fb7049e95d9fa75620049a16b144
data/CHANGELOG.md ADDED
@@ -0,0 +1,52 @@
1
+ Unreleased Changes
2
+ ------------------
3
+
4
+ 1.3.2 (2025-03-06)
5
+ ------------------
6
+
7
+ * Issue - Add back event stream max payload size and headers length checks with a 24MB limit.
8
+
9
+ 1.3.1 (2025-02-13)
10
+ ------------------
11
+
12
+ * Issue - Remove event stream max payload size and headers length checks.
13
+
14
+ 1.3.0 (2023-11-22)
15
+ ------------------
16
+
17
+ * Feature - AWS SDK for Ruby no longer supports Ruby runtime versions 2.3 and 2.4.
18
+
19
+ 1.2.0 (2021-09-01)
20
+ ------------------
21
+
22
+ * Feature - AWS SDK for Ruby no longer supports Ruby runtime versions 1.9, 2.0, 2.1, and 2.2.
23
+
24
+ 1.1.1 (2021-03-04)
25
+ ------------------
26
+
27
+ * Issue - Include LICENSE, CHANGELOG, and VERSION files with this gem.
28
+
29
+ 1.1.0 (2020-04-08)
30
+ ------------------
31
+
32
+ * Feature - Remove internal ByteBuffer and replace with String to remove dup and string mutation.
33
+
34
+ 1.0.3 (2019-04-24)
35
+ ------------------
36
+
37
+ * Issue - Use single quotes for string where interpolation is not done.
38
+
39
+ 1.0.2 (2019-03-11)
40
+ ------------------
41
+
42
+ * Issue - public #encode_headers method
43
+
44
+ 1.0.1 (2018-06-15)
45
+ ------------------
46
+
47
+ * Issue - #decode_chunk buffers insufficient prelude message
48
+
49
+ 1.0.0 (2018-05-10)
50
+ ------------------
51
+
52
+ * Feature - Initial release of `aws-eventstream` gem.
data/LICENSE.txt ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.3.2
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'stringio'
2
4
  require 'tempfile'
3
5
  require 'zlib'
4
6
 
5
7
  module Aws
6
- module EventStream
8
+ module EventStream
7
9
 
8
10
  # This class provides method for decoding binary inputs into
9
11
  # single or multiple messages (Aws::EventStream::Message).
@@ -28,7 +30,7 @@ module Aws
28
30
  # message_pool = decoder.decode(io)
29
31
  # message_pool.next
30
32
  # # => Aws::EventStream::Message
31
- #
33
+ #
32
34
  # * {#decode_chunk} - decodes a single message from a chunk of data,
33
35
  # returning message object followed by boolean(indicating eof status
34
36
  # of data) in an array object
@@ -63,27 +65,26 @@ module Aws
63
65
  include Enumerable
64
66
 
65
67
  ONE_MEGABYTE = 1024 * 1024
68
+ private_constant :ONE_MEGABYTE
66
69
 
67
70
  # bytes of prelude part, including 4 bytes of
68
71
  # total message length, headers length and crc checksum of prelude
69
72
  PRELUDE_LENGTH = 12
73
+ private_constant :PRELUDE_LENGTH
70
74
 
71
- # bytes of total overhead in a message, including prelude
72
- # and 4 bytes total message crc checksum
73
- OVERHEAD_LENGTH = 16
75
+ # 4 bytes message crc checksum
76
+ CRC32_LENGTH = 4
77
+ private_constant :CRC32_LENGTH
74
78
 
75
- # @options options [Boolean] format (true) When `false`
76
- # disable user-friendly formatting for message header values
79
+ # @param [Hash] options The initialization options.
80
+ # @option options [Boolean] :format (true) When `false` it
81
+ # disables user-friendly formatting for message header values
77
82
  # including timestamp and uuid etc.
78
- #
79
83
  def initialize(options = {})
80
84
  @format = options.fetch(:format, true)
81
- @message_buffer = BytesBuffer.new('')
85
+ @message_buffer = ''
82
86
  end
83
87
 
84
- # @returns [BytesBuffer]
85
- attr_reader :message_buffer
86
-
87
88
  # Decodes messages from a binary stream
88
89
  #
89
90
  # @param [IO#read] io An IO-like object
@@ -93,12 +94,12 @@ module Aws
93
94
  # @return [Enumerable<Message>, nil] Returns a new Enumerable
94
95
  # containing decoded messages if no block is given
95
96
  def decode(io, &block)
96
- io = BytesBuffer.new(io.read)
97
- return decode_io(io) unless block_given?
98
- until io.eof?
99
- # fetch message only
100
- yield(decode_message(io).first)
101
- end
97
+ raw_message = io.read
98
+ decoded_message = decode_message(raw_message)
99
+ return wrap_as_enumerator(decoded_message) unless block_given?
100
+ # fetch message only
101
+ raw_event, _eof = decoded_message
102
+ block.call(raw_event)
102
103
  end
103
104
 
104
105
  # Decodes a single message from a chunk of string
@@ -111,95 +112,81 @@ module Aws
111
112
  # and boolean pair, the boolean flag indicates whether this chunk
112
113
  # has been fully consumed, unused data is tracked at #message_buffer
113
114
  def decode_chunk(chunk = nil)
114
- @message_buffer.write(chunk) if chunk
115
- @message_buffer.rewind
115
+ @message_buffer = [@message_buffer, chunk].pack('a*a*') if chunk
116
116
  decode_message(@message_buffer)
117
117
  end
118
118
 
119
119
  private
120
120
 
121
- def decode_io(io)
122
- ::Enumerator.new {|e| e << decode_message(io) unless io.eof? }
121
+ # exposed via object.send for testing
122
+ attr_reader :message_buffer
123
+
124
+ def wrap_as_enumerator(decoded_message)
125
+ Enumerator.new do |yielder|
126
+ yielder << decoded_message
127
+ end
123
128
  end
124
129
 
125
- def decode_message(io)
126
- # incomplete message prelude received, leave it in the buffer
127
- return [nil, true] if io.bytesize < PRELUDE_LENGTH
130
+ def decode_message(raw_message)
131
+ # incomplete message prelude received
132
+ return [nil, true] if raw_message.bytesize < PRELUDE_LENGTH
133
+
134
+ prelude, content = raw_message.unpack("a#{PRELUDE_LENGTH}a*")
128
135
 
129
136
  # decode prelude
130
- total_len, headers_len, prelude_buffer = prelude(io)
137
+ total_length, header_length = decode_prelude(prelude)
131
138
 
132
139
  # incomplete message received, leave it in the buffer
133
- return [nil, true] if io.bytesize < total_len
140
+ return [nil, true] if raw_message.bytesize < total_length
134
141
 
135
- # decode headers and payload
136
- headers, payload = context(io, total_len, headers_len, prelude_buffer)
137
-
138
- # track extra message data in the buffer if exists
139
- # for #decode_chunk, io is @message_buffer
140
- if eof = io.eof?
141
- @message_buffer.clear!
142
- else
143
- @message_buffer = BytesBuffer.new(@message_buffer.read)
142
+ content, checksum, remaining = content.unpack("a#{total_length - PRELUDE_LENGTH - CRC32_LENGTH}Na*")
143
+ unless Zlib.crc32([prelude, content].pack('a*a*')) == checksum
144
+ raise Errors::MessageChecksumError
144
145
  end
145
146
 
146
- [Message.new(headers: headers, payload: payload), eof]
147
+ # decode headers and payload
148
+ headers, payload = decode_context(content, header_length)
149
+
150
+ @message_buffer = remaining
151
+
152
+ [Message.new(headers: headers, payload: payload), remaining.empty?]
147
153
  end
148
154
 
149
- def prelude(io)
150
- # buffer prelude into bytes buffer
155
+ def decode_prelude(prelude)
151
156
  # prelude contains length of message and headers,
152
157
  # followed with CRC checksum of itself
153
- buffer = BytesBuffer.new(io.read(PRELUDE_LENGTH))
154
-
155
- # prelude checksum takes last 4 bytes
156
- checksum = Zlib.crc32(buffer.read(PRELUDE_LENGTH - 4))
157
- unless checksum == unpack_uint32(buffer)
158
- raise Errors::PreludeChecksumError
159
- end
160
-
161
- buffer.rewind
162
- total_len, headers_len, _ = buffer.read.unpack('N*')
163
- [total_len, headers_len, buffer]
158
+ content, checksum = prelude.unpack("a#{PRELUDE_LENGTH - CRC32_LENGTH}N")
159
+ raise Errors::PreludeChecksumError unless Zlib.crc32(content) == checksum
160
+ content.unpack('N*')
164
161
  end
165
162
 
166
- def context(io, total_len, headers_len, prelude_buffer)
167
- # buffer rest of the message except prelude length
168
- # including context and total message checksum
169
- buffer = BytesBuffer.new(io.read(total_len - PRELUDE_LENGTH))
170
- context_len = total_len - OVERHEAD_LENGTH
171
-
172
- prelude_buffer.rewind
173
- checksum = Zlib.crc32(prelude_buffer.read << buffer.read(context_len))
174
- unless checksum == unpack_uint32(buffer)
175
- raise Errors::MessageChecksumError
176
- end
177
-
178
- buffer.rewind
163
+ def decode_context(content, header_length)
164
+ encoded_header, encoded_payload = content.unpack("a#{header_length}a*")
179
165
  [
180
- extract_headers(BytesBuffer.new(buffer.read(headers_len))),
181
- extract_payload(BytesBuffer.new(buffer.read(context_len - headers_len)))
166
+ extract_headers(encoded_header),
167
+ extract_payload(encoded_payload)
182
168
  ]
183
169
  end
184
170
 
185
171
  def extract_headers(buffer)
172
+ scanner = buffer
186
173
  headers = {}
187
- until buffer.eof?
174
+ until scanner.bytesize == 0
188
175
  # header key
189
- key_len = unpack_uint8(buffer)
190
- key = buffer.read(key_len)
176
+ key_length, scanner = scanner.unpack('Ca*')
177
+ key, scanner = scanner.unpack("a#{key_length}a*")
191
178
 
192
179
  # header value
193
- value_type = Types.types[unpack_uint8(buffer)]
194
- unpack_pattern, value_len, _ = Types.pattern[value_type]
195
- if !!unpack_pattern == unpack_pattern
180
+ type_index, scanner = scanner.unpack('Ca*')
181
+ value_type = Types.types[type_index]
182
+ unpack_pattern, value_length = Types.pattern[value_type]
183
+ value = if !!unpack_pattern == unpack_pattern
196
184
  # boolean types won't have value specified
197
- value = unpack_pattern
185
+ unpack_pattern
198
186
  else
199
- value_len = unpack_uint16(buffer) unless value_len
200
- value = unpack_pattern ?
201
- buffer.read(value_len).unpack(unpack_pattern)[0] :
202
- buffer.read(value_len)
187
+ value_length, scanner = scanner.unpack('S>a*') unless value_length
188
+ unpacked_value, scanner = scanner.unpack("#{unpack_pattern || "a#{value_length}"}a*")
189
+ unpacked_value
203
190
  end
204
191
 
205
192
  headers[key] = HeaderValue.new(
@@ -211,40 +198,23 @@ module Aws
211
198
  headers
212
199
  end
213
200
 
214
- def extract_payload(buffer)
215
- buffer.bytesize <= ONE_MEGABYTE ?
216
- payload_stringio(buffer) :
217
- payload_tempfile(buffer)
201
+ def extract_payload(encoded)
202
+ encoded.bytesize <= ONE_MEGABYTE ?
203
+ payload_stringio(encoded) :
204
+ payload_tempfile(encoded)
218
205
  end
219
206
 
220
- def payload_stringio(buffer)
221
- StringIO.new(buffer.read)
207
+ def payload_stringio(encoded)
208
+ StringIO.new(encoded)
222
209
  end
223
210
 
224
- def payload_tempfile(buffer)
211
+ def payload_tempfile(encoded)
225
212
  payload = Tempfile.new
226
213
  payload.binmode
227
- until buffer.eof?
228
- payload.write(buffer.read(ONE_MEGABYTE))
229
- end
214
+ payload.write(encoded)
230
215
  payload.rewind
231
216
  payload
232
217
  end
233
-
234
- # overhead decode helpers
235
-
236
- def unpack_uint32(buffer)
237
- buffer.read(4).unpack('N')[0]
238
- end
239
-
240
- def unpack_uint16(buffer)
241
- buffer.read(2).unpack('S>')[0]
242
- end
243
-
244
- def unpack_uint8(buffer)
245
- buffer.readbyte.unpack('C')[0]
246
- end
247
218
  end
248
-
249
219
  end
250
220
  end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'zlib'
2
4
 
3
5
  module Aws
4
- module EventStream
6
+ module EventStream
5
7
 
6
8
  # This class provides #encode method for encoding
7
9
  # Aws::EventStream::Message into binary.
@@ -40,10 +42,10 @@ module Aws
40
42
  OVERHEAD_LENGTH = 16
41
43
 
42
44
  # Maximum header length allowed (after encode) 128kb
43
- MAX_HEADERS_LENGTH = 131072
45
+ MAX_HEADERS_LENGTH = 1024 * 128
44
46
 
45
- # Maximum payload length allowed (after encode) 16mb
46
- MAX_PAYLOAD_LENGTH = 16777216
47
+ # Maximum payload length allowed (after encode) 24mb
48
+ MAX_PAYLOAD_LENGTH = 1024 * 1024 * 24
47
49
 
48
50
  # Encodes Aws::EventStream::Message to output IO when
49
51
  # provided, else return the encoded binary string
@@ -59,7 +61,7 @@ module Aws
59
61
  # will be returned. Else, encoded binary string is
60
62
  # returned.
61
63
  def encode(message, io = nil)
62
- encoded = encode_message(message).read
64
+ encoded = encode_message(message)
63
65
  if io
64
66
  io.write(encoded)
65
67
  io.close
@@ -69,92 +71,72 @@ module Aws
69
71
  end
70
72
 
71
73
  # Encodes an Aws::EventStream::Message
72
- # into Aws::EventStream::BytesBuffer
74
+ # into String
73
75
  #
74
- # @param [Aws::EventStream::Message] msg
76
+ # @param [Aws::EventStream::Message] message
75
77
  #
76
- # @return [Aws::EventStream::BytesBuffer]
78
+ # @return [String]
77
79
  def encode_message(message)
78
80
  # create context buffer with encode headers
79
- ctx_buffer = encode_headers(message)
80
- headers_len = ctx_buffer.bytesize
81
+ encoded_header = encode_headers(message)
82
+ header_length = encoded_header.bytesize
81
83
  # encode payload
82
84
  if message.payload.length > MAX_PAYLOAD_LENGTH
83
85
  raise Aws::EventStream::Errors::EventPayloadLengthExceedError.new
84
86
  end
85
- ctx_buffer << message.payload.read
86
- total_len = ctx_buffer.bytesize + OVERHEAD_LENGTH
87
+ encoded_payload = message.payload.read
88
+ total_length = header_length + encoded_payload.bytesize + OVERHEAD_LENGTH
87
89
 
88
90
  # create message buffer with prelude section
89
- buffer = prelude(total_len, headers_len)
91
+ encoded_prelude = encode_prelude(total_length, header_length)
90
92
 
91
93
  # append message context (headers, payload)
92
- buffer << ctx_buffer.read
94
+ encoded_content = [
95
+ encoded_prelude,
96
+ encoded_header,
97
+ encoded_payload,
98
+ ].pack('a*a*a*')
93
99
  # append message checksum
94
- buffer << pack_uint32(Zlib.crc32(buffer.read))
95
-
96
- # write buffered message to io
97
- buffer.rewind
98
- buffer
100
+ message_checksum = Zlib.crc32(encoded_content)
101
+ [encoded_content, message_checksum].pack('a*N')
99
102
  end
100
103
 
101
104
  # Encodes headers part of an Aws::EventStream::Message
102
- # into Aws::EventStream::BytesBuffer
105
+ # into String
103
106
  #
104
- # @param [Aws::EventStream::Message] msg
107
+ # @param [Aws::EventStream::Message] message
105
108
  #
106
- # @return [Aws::EventStream::BytesBuffer]
107
- def encode_headers(msg)
108
- buffer = BytesBuffer.new('')
109
- msg.headers.each do |k, v|
110
- # header key
111
- buffer << pack_uint8(k.bytesize)
112
- buffer << k
109
+ # @return [String]
110
+ def encode_headers(message)
111
+ header_entries = message.headers.map do |key, value|
112
+ encoded_key = [key.bytesize, key].pack('Ca*')
113
113
 
114
114
  # header value
115
- pattern, val_len, idx = Types.pattern[v.type]
116
- buffer << pack_uint8(idx)
115
+ pattern, value_length, type_index = Types.pattern[value.type]
116
+ encoded_value = [type_index].pack('C')
117
117
  # boolean types doesn't need to specify value
118
- next if !!pattern == pattern
119
- buffer << pack_uint16(v.value.bytesize) unless val_len
120
- pattern ? buffer << [v.value].pack(pattern) :
121
- buffer << v.value
118
+ next [encoded_key, encoded_value].pack('a*a*') if !!pattern == pattern
119
+ encoded_value = [encoded_value, value.value.bytesize].pack('a*S>') unless value_length
120
+
121
+ [
122
+ encoded_key,
123
+ encoded_value,
124
+ pattern ? [value.value].pack(pattern) : value.value,
125
+ ].pack('a*a*a*')
122
126
  end
123
- if buffer.bytesize > MAX_HEADERS_LENGTH
127
+ header_entries.join.tap do |encoded_header|
128
+ break encoded_header if encoded_header.bytesize <= MAX_HEADERS_LENGTH
124
129
  raise Aws::EventStream::Errors::EventHeadersLengthExceedError.new
125
130
  end
126
- buffer
127
131
  end
128
132
 
129
133
  private
130
134
 
131
- def prelude(total_len, headers_len)
132
- BytesBuffer.new(pack_uint32([
133
- total_len,
134
- headers_len,
135
- Zlib.crc32(pack_uint32([total_len, headers_len]))
136
- ]))
135
+ def encode_prelude(total_length, headers_length)
136
+ prelude_body = [total_length, headers_length].pack('NN')
137
+ checksum = Zlib.crc32(prelude_body)
138
+ [prelude_body, checksum].pack('a*N')
137
139
  end
138
-
139
- # overhead encode helpers
140
-
141
- def pack_uint8(val)
142
- [val].pack('C')
143
- end
144
-
145
- def pack_uint16(val)
146
- [val].pack('S>')
147
- end
148
-
149
- def pack_uint32(val)
150
- if val.respond_to?(:each)
151
- val.pack('N*')
152
- else
153
- [val].pack('N')
154
- end
155
- end
156
-
157
140
  end
158
-
159
141
  end
160
142
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Aws
2
4
  module EventStream
3
5
  module Errors
@@ -6,7 +8,7 @@ module Aws
6
8
  class ReadBytesExceedLengthError < RuntimeError
7
9
  def initialize(target_byte, total_len)
8
10
  msg = "Attempting reading bytes to offset #{target_byte} exceeds"\
9
- " buffer length of #{total_len}"
11
+ " buffer length of #{total_len}"
10
12
  super(msg)
11
13
  end
12
14
  end
@@ -32,7 +34,7 @@ module Aws
32
34
 
33
35
  class EventPayloadLengthExceedError < RuntimeError
34
36
  def initialize(*args)
35
- super("Payload length of a message should be under 16mb.")
37
+ super("Payload length of a message should be under 24mb.")
36
38
  end
37
39
  end
38
40
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Aws
2
4
  module EventStream
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Aws
2
4
  module EventStream
3
5
  class Message
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Aws
2
4
  module EventStream
3
5
 
@@ -1,7 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'aws-eventstream/decoder'
2
4
  require_relative 'aws-eventstream/encoder'
3
5
 
4
- require_relative 'aws-eventstream/bytes_buffer'
5
6
  require_relative 'aws-eventstream/message'
6
7
  require_relative 'aws-eventstream/header_value'
7
8
  require_relative 'aws-eventstream/types'
metadata CHANGED
@@ -1,24 +1,26 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-eventstream
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amazon Web Services
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-24 00:00:00.000000000 Z
11
+ date: 2025-03-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Amazon Web Services event stream library. Decodes and encodes binary
14
14
  stream under `vnd.amazon.event-stream` content-type
15
- email:
15
+ email:
16
16
  executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - CHANGELOG.md
21
+ - LICENSE.txt
22
+ - VERSION
20
23
  - lib/aws-eventstream.rb
21
- - lib/aws-eventstream/bytes_buffer.rb
22
24
  - lib/aws-eventstream/decoder.rb
23
25
  - lib/aws-eventstream/encoder.rb
24
26
  - lib/aws-eventstream/errors.rb
@@ -29,9 +31,9 @@ homepage: https://github.com/aws/aws-sdk-ruby
29
31
  licenses:
30
32
  - Apache-2.0
31
33
  metadata:
32
- source_code_uri: https://github.com/aws/aws-sdk-ruby/tree/master/gems/aws-eventstream
33
- changelog_uri: https://github.com/aws/aws-sdk-ruby/tree/master/gems/aws-eventstream/CHANGELOG.md
34
- post_install_message:
34
+ source_code_uri: https://github.com/aws/aws-sdk-ruby/tree/version-3/gems/aws-eventstream
35
+ changelog_uri: https://github.com/aws/aws-sdk-ruby/tree/version-3/gems/aws-eventstream/CHANGELOG.md
36
+ post_install_message:
35
37
  rdoc_options: []
36
38
  require_paths:
37
39
  - lib
@@ -39,16 +41,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
39
41
  requirements:
40
42
  - - ">="
41
43
  - !ruby/object:Gem::Version
42
- version: '0'
44
+ version: '2.5'
43
45
  required_rubygems_version: !ruby/object:Gem::Requirement
44
46
  requirements:
45
47
  - - ">="
46
48
  - !ruby/object:Gem::Version
47
49
  version: '0'
48
50
  requirements: []
49
- rubyforge_project:
50
- rubygems_version: 2.5.2.3
51
- signing_key:
51
+ rubygems_version: 3.4.10
52
+ signing_key:
52
53
  specification_version: 4
53
54
  summary: AWS Event Stream Library
54
55
  test_files: []
@@ -1,66 +0,0 @@
1
- module Aws
2
- module EventStream
3
-
4
- # @api private
5
- class BytesBuffer
6
-
7
- # This Util class is for Decoder/Encoder usage only
8
- # Not for public common bytes buffer usage
9
- def initialize(data)
10
- @data = data
11
- @pos = 0
12
- end
13
-
14
- def read(len = nil, offset = 0)
15
- return '' if len == 0 || bytesize == 0
16
- unless eof?
17
- start_byte = @pos + offset
18
- end_byte = len ?
19
- start_byte + len - 1 :
20
- bytesize - 1
21
-
22
- error = Errors::ReadBytesExceedLengthError.new(end_byte, bytesize)
23
- raise error if end_byte >= bytesize
24
-
25
- @pos = end_byte + 1
26
- @data[start_byte..end_byte]
27
- end
28
- end
29
-
30
- def readbyte
31
- unless eof?
32
- @pos += 1
33
- @data[@pos - 1]
34
- end
35
- end
36
-
37
- def write(bytes)
38
- @data <<= bytes
39
- bytes.bytesize
40
- end
41
- alias_method :<<, :write
42
-
43
- def rewind
44
- @pos = 0
45
- end
46
-
47
- def eof?
48
- @pos == bytesize
49
- end
50
-
51
- def bytesize
52
- @data.bytesize
53
- end
54
-
55
- def tell
56
- @pos
57
- end
58
-
59
- def clear!
60
- @data = ''
61
- @pos = 0
62
- end
63
- end
64
-
65
- end
66
- end