io-stream 0.6.1 → 0.8.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4b66a5f21e372373267bc26a249194d093a07c9af7a53066dcd151662c0abda1
4
- data.tar.gz: '0944f1aafb0c79a93462d304e7e4ed722543609a2424f90ee2d045b70bf31751'
3
+ metadata.gz: 5f73e60fdaf4001c88fcfaa4ccb29bfc8ee90d2fc3ffc81408c8673b414038ae
4
+ data.tar.gz: 933183cda89e7d555239ba15a752dadccc938628d6197675b3bedb8cfeb60f43
5
5
  SHA512:
6
- metadata.gz: 75ab72272639271db26f079d7d8098c5a191db8f0321ffc1b82aaffd424511f697da8d3751c4f3bd86c1f2315b52ba02ba5a7ab21f7c6b19c3c39fd649dfe3ae
7
- data.tar.gz: e0a17ddca49522facbed0e91b46da1d185aa91c68364258c093c207780f1e7f320debac1f7583762757dfa45d0b79f7206f248911723c93fb8e23a962337ce16
6
+ metadata.gz: 732318c46b27af3ecabb0c99361b60de5ae3c2733013261ae3089fbaa0f4f2f542e70ee10c410227262a8d137a59d5f1e36ba9419307e801fefd0cfebb412ce5
7
+ data.tar.gz: 5ae80afa0d558011183648ce9d34c02dc52e2b8f0cf24bde53a319d1cba3dd188fa8705a477baa8c7fcd5afeb6ee487087e72c1a75d39c80566ea9c378d4e7b1
checksums.yaml.gz.sig CHANGED
Binary file
@@ -6,7 +6,13 @@
6
6
  require_relative "generic"
7
7
 
8
8
  module IO::Stream
9
+ # A buffered stream implementation that wraps an underlying IO object to provide efficient buffered reading and writing.
9
10
  class Buffered < Generic
11
+ # Open a file and wrap it in a buffered stream.
12
+ # @parameter path [String] The file path to open.
13
+ # @parameter mode [String] The file mode (e.g., "r+", "w", "a").
14
+ # @parameter options [Hash] Additional options passed to the stream constructor.
15
+ # @returns [IO::Stream::Buffered] A buffered stream wrapping the opened file.
10
16
  def self.open(path, mode = "r+", **options)
11
17
  stream = self.new(::File.open(path, mode), **options)
12
18
 
@@ -19,6 +25,10 @@ module IO::Stream
19
25
  end
20
26
  end
21
27
 
28
+ # Wrap an existing IO object in a buffered stream.
29
+ # @parameter io [IO] The IO object to wrap.
30
+ # @parameter options [Hash] Additional options passed to the stream constructor.
31
+ # @returns [IO::Stream::Buffered] A buffered stream wrapping the IO object.
22
32
  def self.wrap(io, **options)
23
33
  if io.respond_to?(:buffered=)
24
34
  io.buffered = false
@@ -37,6 +47,8 @@ module IO::Stream
37
47
  end
38
48
  end
39
49
 
50
+ # Initialize a new buffered stream.
51
+ # @parameter io [IO] The underlying IO object to wrap.
40
52
  def initialize(io, ...)
41
53
  super(...)
42
54
 
@@ -47,27 +59,36 @@ module IO::Stream
47
59
  @timeout = nil
48
60
  end
49
61
  end
50
-
62
+
63
+ # @attribute [IO] The wrapped IO object.
51
64
  attr :io
52
-
65
+
66
+ # Get the underlying IO object.
67
+ # @returns [IO] The underlying IO object.
53
68
  def to_io
54
69
  @io.to_io
55
70
  end
56
-
71
+
72
+ # Check if the stream is closed.
73
+ # @returns [Boolean] True if the stream is closed.
57
74
  def closed?
58
75
  @io.closed?
59
76
  end
60
-
77
+
78
+ # Close the read end of the stream.
61
79
  def close_read
62
80
  @io.close_read
63
81
  end
64
-
82
+
83
+ # Close the write end of the stream.
65
84
  def close_write
66
85
  super
67
86
  ensure
68
87
  @io.close_write
69
88
  end
70
-
89
+
90
+ # Check if the stream is readable.
91
+ # @returns [Boolean] True if the stream is readable.
71
92
  def readable?
72
93
  super && @io.readable?
73
94
  end
@@ -85,23 +106,26 @@ module IO::Stream
85
106
  end
86
107
  end
87
108
 
88
- def syswrite(buffer)
89
- # This fails due to re-entrancy issues with a concurrent call to `sysclose`.
90
- # return @io.write(buffer)
91
-
92
- while true
93
- result = @io.write_nonblock(buffer, exception: false)
94
-
95
- case result
96
- when :wait_readable
97
- @io.wait_readable(@io.timeout) or raise ::IO::TimeoutError, "read timeout"
98
- when :wait_writable
99
- @io.wait_writable(@io.timeout) or raise ::IO::TimeoutError, "write timeout"
100
- else
101
- if result == buffer.bytesize
102
- return
109
+ if RUBY_VERSION >= "3.3"
110
+ def syswrite(buffer)
111
+ return @io.write(buffer)
112
+ end
113
+ else
114
+ def syswrite(buffer)
115
+ while true
116
+ result = @io.write_nonblock(buffer, exception: false)
117
+
118
+ case result
119
+ when :wait_readable
120
+ @io.wait_readable(@io.timeout) or raise ::IO::TimeoutError, "read timeout"
121
+ when :wait_writable
122
+ @io.wait_writable(@io.timeout) or raise ::IO::TimeoutError, "write timeout"
103
123
  else
104
- buffer = buffer.byteslice(result, buffer.bytesize)
124
+ if result == buffer.bytesize
125
+ return
126
+ else
127
+ buffer = buffer.byteslice(result, buffer.bytesize)
128
+ end
105
129
  end
106
130
  end
107
131
  end
@@ -4,253 +4,38 @@
4
4
  # Copyright, 2023-2024, by Samuel Williams.
5
5
 
6
6
  require_relative "string_buffer"
7
+ require_relative "readable"
8
+ require_relative "writable"
7
9
 
8
10
  require_relative "shim/buffered"
9
11
  require_relative "shim/readable"
10
- require_relative "shim/timeout"
11
12
 
12
13
  require_relative "openssl"
13
14
 
14
15
  module IO::Stream
15
- # The default block size for IO buffers. Defaults to 64KB (typical pipe buffer size).
16
- BLOCK_SIZE = ENV.fetch("IO_STREAM_BLOCK_SIZE", 1024*64).to_i
17
-
18
- # The maximum read size when appending to IO buffers. Defaults to 8MB.
19
- MAXIMUM_READ_SIZE = ENV.fetch("IO_STREAM_MAXIMUM_READ_SIZE", BLOCK_SIZE * 128).to_i
20
-
21
- class LimitError < StandardError
22
- end
23
-
16
+ # Base class for stream implementations providing common functionality.
24
17
  class Generic
25
- def initialize(block_size: BLOCK_SIZE, maximum_read_size: MAXIMUM_READ_SIZE)
26
- @eof = false
27
-
28
- @writing = ::Thread::Mutex.new
29
-
30
- @block_size = block_size
31
- @maximum_read_size = maximum_read_size
32
-
33
- @read_buffer = StringBuffer.new
34
- @write_buffer = StringBuffer.new
35
-
36
- # Used as destination buffer for underlying reads.
37
- @input_buffer = StringBuffer.new
38
- end
39
-
40
- attr_accessor :block_size
41
-
42
- # Reads `size` bytes from the stream. If size is not specified, read until end of file.
43
- def read(size = nil)
44
- return String.new(encoding: Encoding::BINARY) if size == 0
45
-
46
- if size
47
- until @eof or @read_buffer.bytesize >= size
48
- # Compute the amount of data we need to read from the underlying stream:
49
- read_size = size - @read_buffer.bytesize
50
-
51
- # Don't read less than @block_size to avoid lots of small reads:
52
- fill_read_buffer(read_size > @block_size ? read_size : @block_size)
53
- end
54
- else
55
- until @eof
56
- fill_read_buffer
57
- end
58
- end
59
-
60
- return consume_read_buffer(size)
61
- end
62
-
63
- # Read at most `size` bytes from the stream. Will avoid reading from the underlying stream if possible.
64
- def read_partial(size = nil)
65
- return String.new(encoding: Encoding::BINARY) if size == 0
66
-
67
- if !@eof and @read_buffer.empty?
68
- fill_read_buffer
69
- end
70
-
71
- return consume_read_buffer(size)
72
- end
73
-
74
- def read_exactly(size, exception: EOFError)
75
- if buffer = read(size)
76
- if buffer.bytesize != size
77
- raise exception, "could not read enough data"
78
- end
79
-
80
- return buffer
81
- end
82
-
83
- raise exception, "encountered eof while reading data"
84
- end
85
-
86
- # This is a compatibility shim for existing code that uses `readpartial`.
87
- def readpartial(size = nil)
88
- read_partial(size) or raise EOFError, "Encountered eof while reading data!"
89
- end
90
-
91
- private def index_of(pattern, offset, limit)
92
- # We don't want to split on the pattern, so we subtract the size of the pattern.
93
- split_offset = pattern.bytesize - 1
94
-
95
- until index = @read_buffer.index(pattern, offset)
96
- offset = @read_buffer.bytesize - split_offset
97
-
98
- offset = 0 if offset < 0
99
-
100
- return nil if limit and offset >= limit
101
- return nil unless fill_read_buffer
102
- end
103
-
104
- return index
105
- end
106
-
107
- # Efficiently read data from the stream until encountering pattern.
108
- # @parameter pattern [String] The pattern to match.
109
- # @parameter offset [Integer] The offset to start searching from.
110
- # @parameter limit [Integer] The maximum number of bytes to read, including the pattern (even if chomped).
111
- # @returns [String | Nil] The contents of the stream up until the pattern, which is consumed but not returned.
112
- def read_until(pattern, offset = 0, limit: nil, chomp: true)
113
- if index = index_of(pattern, offset, limit)
114
- return nil if limit and index >= limit
115
-
116
- @read_buffer.freeze
117
- matched = @read_buffer.byteslice(0, index+(chomp ? 0 : pattern.bytesize))
118
- @read_buffer = @read_buffer.byteslice(index+pattern.bytesize, @read_buffer.bytesize)
119
-
120
- return matched
121
- end
122
- end
123
-
124
- def peek(size = nil)
125
- if size
126
- until @eof or @read_buffer.bytesize >= size
127
- # Compute the amount of data we need to read from the underlying stream:
128
- read_size = size - @read_buffer.bytesize
129
-
130
- # Don't read less than @block_size to avoid lots of small reads:
131
- fill_read_buffer(read_size > @block_size ? read_size : @block_size)
132
- end
133
- return @read_buffer[..([size, @read_buffer.size].min - 1)]
134
- end
135
- until (block_given? && yield(@read_buffer)) or @eof
136
- fill_read_buffer
137
- end
138
- return @read_buffer
139
- end
140
-
141
- def gets(separator = $/, limit = nil, chomp: false)
142
- # Compatibility with IO#gets:
143
- if separator.is_a?(Integer)
144
- limit = separator
145
- separator = $/
146
- end
147
-
148
- # We don't want to split in the middle of the separator, so we subtract the size of the separator from the start of the search:
149
- split_offset = separator.bytesize - 1
150
-
151
- offset = 0
152
-
153
- until index = @read_buffer.index(separator, offset)
154
- offset = @read_buffer.bytesize - split_offset
155
- offset = 0 if offset < 0
156
-
157
- # If a limit was given, and the offset is beyond the limit, we should return up to the limit:
158
- if limit and offset >= limit
159
- # As we didn't find the separator, there is nothing to chomp either.
160
- return consume_read_buffer(limit)
161
- end
162
-
163
- # If we can't read any more data, we should return what we have:
164
- return consume_read_buffer unless fill_read_buffer
165
- end
166
-
167
- # If the index of the separator was beyond the limit:
168
- if limit and index >= limit
169
- # Return up to the limit:
170
- return consume_read_buffer(limit)
171
- end
172
-
173
- # Freeze the read buffer, as this enables us to use byteslice without generating a hidden copy:
174
- @read_buffer.freeze
175
-
176
- line = @read_buffer.byteslice(0, index+(chomp ? 0 : separator.bytesize))
177
- @read_buffer = @read_buffer.byteslice(index+separator.bytesize, @read_buffer.bytesize)
178
-
179
- return line
180
- end
18
+ include Readable
19
+ include Writable
181
20
 
182
- private def drain(buffer)
183
- begin
184
- syswrite(buffer)
185
- ensure
186
- # If the write operation fails, we still need to clear this buffer, and the data is essentially lost.
187
- buffer.clear
188
- end
189
- end
190
-
191
- # Flushes buffered data to the stream.
192
- def flush
193
- return if @write_buffer.empty?
194
-
195
- @writing.synchronize do
196
- self.drain(@write_buffer)
197
- end
198
- end
199
-
200
- # Writes `string` to the buffer. When the buffer is full or #sync is true the
201
- # buffer is flushed to the underlying `io`.
202
- # @parameter string [String] the string to write to the buffer.
203
- # @returns [Integer] the number of bytes appended to the buffer.
204
- def write(string, flush: false)
205
- @writing.synchronize do
206
- @write_buffer << string
207
-
208
- flush |= (@write_buffer.bytesize >= @block_size)
209
-
210
- if flush
211
- self.drain(@write_buffer)
212
- end
213
- end
214
-
215
- return string.bytesize
216
- end
217
-
218
- # Writes `string` to the stream and returns self.
219
- def <<(string)
220
- write(string)
221
-
222
- return self
223
- end
224
-
225
- def puts(*arguments, separator: $/)
226
- return if arguments.empty?
227
-
228
- @writing.synchronize do
229
- arguments.each do |argument|
230
- @write_buffer << argument << separator
231
- end
232
-
233
- self.drain(@write_buffer)
234
- end
21
+ # Initialize a new generic stream.
22
+ # @parameter options [Hash] Options passed to included modules.
23
+ def initialize(**options)
24
+ super(**options)
235
25
  end
236
26
 
27
+ # Check if the stream is closed.
28
+ # @returns [Boolean] False by default, should be overridden by subclasses.
237
29
  def closed?
238
30
  false
239
31
  end
240
32
 
241
- def close_read
242
- end
243
-
244
- def close_write
245
- flush
246
- end
247
-
248
33
  # Best effort to flush any unwritten data, and then close the underling IO.
249
34
  def close
250
35
  return if closed?
251
36
 
252
37
  begin
253
- flush
38
+ self.flush
254
39
  rescue
255
40
  # We really can't do anything here unless we want #close to raise exceptions.
256
41
  ensure
@@ -258,115 +43,26 @@ module IO::Stream
258
43
  end
259
44
  end
260
45
 
261
- # Determins if the stream has consumed all available data. May block if the stream is not readable.
262
- # See {readable?} for a non-blocking alternative.
263
- #
264
- # @returns [Boolean] If the stream is at file which means there is no more data to be read.
265
- def eof?
266
- if !@read_buffer.empty?
267
- return false
268
- elsif @eof
269
- return true
270
- else
271
- return !self.fill_read_buffer
272
- end
273
- end
274
-
275
- def eof!
276
- @read_buffer.clear
277
- @eof = true
278
-
279
- raise EOFError
280
- end
281
-
282
- # Whether there is a chance that a read operation will succeed or not.
283
- # @returns [Boolean] If the stream is readable, i.e. a `read` operation has a chance of success.
284
- def readable?
285
- # If we are at the end of the file, we can't read any more data:
286
- if @eof
287
- return false
288
- end
289
-
290
- # If the read buffer is not empty, we can read more data:
291
- if !@read_buffer.empty?
292
- return true
293
- end
294
-
295
- # If the underlying stream is readable, we can read more data:
296
- return !closed?
297
- end
298
-
299
46
  protected
300
47
 
48
+ # Closes the underlying IO stream.
49
+ # This method should be implemented by subclasses to handle the specific closing logic.
301
50
  def sysclose
302
51
  raise NotImplementedError
303
52
  end
304
53
 
54
+ # Writes data to the underlying stream.
55
+ # This method should be implemented by subclasses to handle the specific writing logic.
56
+ # @parameter buffer [String] The data to write.
57
+ # @returns [Integer] The number of bytes written.
305
58
  def syswrite(buffer)
306
59
  raise NotImplementedError
307
60
  end
308
61
 
309
62
  # Reads data from the underlying stream as efficiently as possible.
63
+ # This method should be implemented by subclasses to handle the specific reading logic.
310
64
  def sysread(size, buffer)
311
65
  raise NotImplementedError
312
66
  end
313
-
314
- private
315
-
316
- # Fills the buffer from the underlying stream.
317
- def fill_read_buffer(size = @block_size)
318
- # We impose a limit because the underlying `read` system call can fail if we request too much data in one go.
319
- if size > @maximum_read_size
320
- size = @maximum_read_size
321
- end
322
-
323
- # This effectively ties the input and output stream together.
324
- flush
325
-
326
- if @read_buffer.empty?
327
- if sysread(size, @read_buffer)
328
- # Console.info(self, name: "read") {@read_buffer.inspect}
329
- return true
330
- end
331
- else
332
- if chunk = sysread(size, @input_buffer)
333
- @read_buffer << chunk
334
- # Console.info(self, name: "read") {@read_buffer.inspect}
335
-
336
- return true
337
- end
338
- end
339
-
340
- # else for both cases above:
341
- @eof = true
342
- return false
343
- end
344
-
345
- # Consumes at most `size` bytes from the buffer.
346
- # @parameter size [Integer|nil] The amount of data to consume. If nil, consume entire buffer.
347
- def consume_read_buffer(size = nil)
348
- # If we are at eof, and the read buffer is empty, we can't consume anything.
349
- return nil if @eof && @read_buffer.empty?
350
-
351
- result = nil
352
-
353
- if size.nil? or size >= @read_buffer.bytesize
354
- # Consume the entire read buffer:
355
- result = @read_buffer
356
- @read_buffer = StringBuffer.new
357
- else
358
- # This approach uses more memory.
359
- # result = @read_buffer.slice!(0, size)
360
-
361
- # We know that we are not going to reuse the original buffer.
362
- # But byteslice will generate a hidden copy. So let's freeze it first:
363
- @read_buffer.freeze
364
-
365
- result = @read_buffer.byteslice(0, size)
366
- @read_buffer = @read_buffer.byteslice(size, @read_buffer.bytesize)
367
- end
368
-
369
- return result
370
- end
371
67
  end
372
68
  end
@@ -5,52 +5,67 @@
5
5
 
6
6
  require "openssl"
7
7
 
8
+ # @namespace
8
9
  module OpenSSL
10
+ # @namespace
9
11
  module SSL
12
+ # SSL socket extensions for stream compatibility.
10
13
  class SSLSocket
11
14
  unless method_defined?(:close_read)
15
+ # Close the read end of the SSL socket.
12
16
  def close_read
13
17
  # Ignored.
14
18
  end
15
19
  end
16
20
 
17
21
  unless method_defined?(:close_write)
22
+ # Close the write end of the SSL socket.
18
23
  def close_write
19
24
  self.stop
20
25
  end
21
26
  end
22
27
 
23
28
  unless method_defined?(:wait_readable)
29
+ # Wait for the SSL socket to become readable.
24
30
  def wait_readable(...)
25
31
  to_io.wait_readable(...)
26
32
  end
27
33
  end
28
34
 
29
35
  unless method_defined?(:wait_writable)
36
+ # Wait for the SSL socket to become writable.
30
37
  def wait_writable(...)
31
38
  to_io.wait_writable(...)
32
39
  end
33
40
  end
34
41
 
35
42
  unless method_defined?(:timeout)
43
+ # Get the timeout for SSL socket operations.
44
+ # @returns [Numeric | Nil] The timeout value.
36
45
  def timeout
37
46
  to_io.timeout
38
47
  end
39
48
  end
40
49
 
41
50
  unless method_defined?(:timeout=)
51
+ # Set the timeout for SSL socket operations.
52
+ # @parameter value [Numeric | Nil] The timeout value.
42
53
  def timeout=(value)
43
54
  to_io.timeout = value
44
55
  end
45
56
  end
46
57
 
47
58
  unless method_defined?(:buffered?)
59
+ # Check if the SSL socket is buffered.
60
+ # @returns [Boolean] True if the SSL socket is buffered.
48
61
  def buffered?
49
62
  return to_io.buffered?
50
63
  end
51
64
  end
52
65
 
53
66
  unless method_defined?(:buffered=)
67
+ # Set the buffered state of the SSL socket.
68
+ # @parameter value [Boolean] True to enable buffering, false to disable.
54
69
  def buffered=(value)
55
70
  to_io.buffered = value
56
71
  end