ruby-brs 1.3.2 → 1.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,174 +1,16 @@
1
1
  # Ruby bindings for brotli library.
2
2
  # Copyright (c) 2019 AUTHORS, MIT License.
3
3
 
4
- require_relative "abstract"
4
+ require "adsp/stream/writer"
5
+
5
6
  require_relative "raw/compressor"
6
- require_relative "writer_helpers"
7
7
 
8
8
  module BRS
9
9
  module Stream
10
- class Writer < Abstract
11
- include WriterHelpers
12
-
13
- def initialize(destination_io, options = {}, *args)
14
- @options = options
15
-
16
- super destination_io, *args
17
- end
18
-
19
- protected def create_raw_stream
20
- Raw::Compressor.new @options
21
- end
22
-
23
- # -- synchronous --
24
-
25
- def write(*objects)
26
- validate_write
27
-
28
- write_remaining_buffer
29
-
30
- bytes_written = 0
31
-
32
- objects.each do |object|
33
- source = transcode object.to_s
34
- bytes_written += raw_wrapper :write, source
35
- end
36
-
37
- @pos += bytes_written
38
-
39
- bytes_written
40
- end
41
-
42
- def flush
43
- validate_write
44
-
45
- finish :flush
46
-
47
- @io.flush if @io.respond_to? :flush
48
-
49
- self
50
- end
51
-
52
- def rewind
53
- validate_write
54
-
55
- finish :close
56
-
57
- super
58
- end
59
-
60
- def close
61
- validate_write
62
-
63
- finish :close
64
-
65
- super
66
- end
67
-
68
- protected def finish(method_name)
69
- write_remaining_buffer
70
-
71
- raw_wrapper method_name
72
- end
73
-
74
- protected def write_remaining_buffer
75
- return nil if @buffer.bytesize.zero?
76
-
77
- @io.write @buffer
78
-
79
- reset_buffer
80
- end
81
-
82
- protected def raw_wrapper(method_name, *args)
83
- @raw_stream.send(method_name, *args) { |portion| @io.write portion }
84
- end
85
-
86
- def validate_write
87
- raise ValidateError, "io should be responsible to write" unless @io.respond_to? :write
88
- end
89
-
90
- # -- asynchronous --
91
-
92
- # IO write nonblock can raise wait writable error.
93
- # After resolving this error user may provide same content again.
94
- # It is not possible to revert accepted content after error.
95
- # So we have to accept content after processing IO write nonblock.
96
- # It means that first write nonblock won't call IO write nonblock.
97
- def write_nonblock(object, *options)
98
- validate_write_nonblock
99
-
100
- return 0 unless write_remaining_buffer_nonblock(*options)
101
-
102
- source = transcode object.to_s
103
- bytes_written = raw_nonblock_wrapper :write, source
104
- @pos += bytes_written
105
-
106
- bytes_written
107
- end
108
-
109
- def flush_nonblock(*options)
110
- validate_write_nonblock
111
-
112
- return false unless finish_nonblock :flush, *options
113
-
114
- @io.flush if @io.respond_to? :flush
115
-
116
- true
117
- end
118
-
119
- def rewind_nonblock(*options)
120
- validate_write_nonblock
121
-
122
- return false unless finish_nonblock :close, *options
123
-
124
- method(:rewind).super_method.call
125
-
126
- true
127
- end
128
-
129
- def close_nonblock(*options)
130
- validate_write_nonblock
131
-
132
- return false unless finish_nonblock :close, *options
133
-
134
- method(:close).super_method.call
135
-
136
- true
137
- end
138
-
139
- protected def finish_nonblock(method_name, *options)
140
- return false unless write_remaining_buffer_nonblock(*options)
141
-
142
- raw_nonblock_wrapper method_name
143
-
144
- write_remaining_buffer_nonblock(*options)
145
- end
146
-
147
- protected def write_remaining_buffer_nonblock(*options)
148
- return true if @buffer.bytesize.zero?
149
-
150
- bytes_written = @io.write_nonblock @buffer, *options
151
- return false if bytes_written.zero?
152
-
153
- @buffer = @buffer.byteslice bytes_written, @buffer.bytesize - bytes_written
154
-
155
- @buffer.bytesize.zero?
156
- end
157
-
158
- protected def raw_nonblock_wrapper(method_name, *args)
159
- @raw_stream.send(method_name, *args) { |portion| @buffer << portion }
160
- end
161
-
162
- def validate_write_nonblock
163
- raise ValidateError, "io should be responsible to write nonblock" unless @io.respond_to? :write_nonblock
164
- end
165
-
166
- # -- common --
167
-
168
- protected def transcode(data)
169
- data = data.encode @external_encoding, **@transcode_options unless @external_encoding.nil?
170
- data
171
- end
10
+ # BRS::Stream::Writer class.
11
+ class Writer < ADSP::Stream::Writer
12
+ # Current raw stream class.
13
+ RawCompressor = Raw::Compressor
172
14
  end
173
15
  end
174
16
  end
data/lib/brs/string.rb CHANGED
@@ -1,15 +1,22 @@
1
1
  # Ruby bindings for brotli library.
2
2
  # Copyright (c) 2019 AUTHORS, MIT License.
3
3
 
4
+ require "adsp/string"
4
5
  require "brs_ext"
5
6
 
6
7
  require_relative "option"
7
8
  require_relative "validation"
8
9
 
9
10
  module BRS
10
- module String
11
- BUFFER_LENGTH_NAMES = %i[destination_buffer_length].freeze
12
-
11
+ # BRS::String class.
12
+ class String < ADSP::String
13
+ # Current option class.
14
+ Option = BRS::Option
15
+
16
+ # Compresses +source+ string using +options+.
17
+ # Option: +:destination_buffer_length+ destination buffer length.
18
+ # Option: +:size_hint+ source bytesize.
19
+ # Returns compressed string.
13
20
  def self.compress(source, options = {})
14
21
  Validation.validate_string source
15
22
 
@@ -17,15 +24,17 @@ module BRS
17
24
 
18
25
  options[:size_hint] = source.bytesize
19
26
 
20
- BRS._native_compress_string source, options
27
+ super source, options
21
28
  end
22
29
 
23
- def self.decompress(source, options = {})
24
- Validation.validate_string source
25
-
26
- options = Option.get_decompressor_options options, BUFFER_LENGTH_NAMES
30
+ # Bypasses native compress.
31
+ def self.native_compress_string(*args)
32
+ BRS._native_compress_string(*args)
33
+ end
27
34
 
28
- BRS._native_decompress_string source, options
35
+ # Bypasses native decompress.
36
+ def self.native_decompress_string(*args)
37
+ BRS._native_decompress_string(*args)
29
38
  end
30
39
  end
31
40
  end
@@ -1,38 +1,14 @@
1
1
  # Ruby bindings for brotli library.
2
2
  # Copyright (c) 2019 AUTHORS, MIT License.
3
3
 
4
- require_relative "error"
4
+ require "adsp/validation"
5
5
 
6
6
  module BRS
7
- module Validation
7
+ # BRS::Validation class.
8
+ class Validation < ADSP::Validation
9
+ # Raises error when +value+ is not boolean.
8
10
  def self.validate_bool(value)
9
11
  raise ValidateError, "invalid bool" unless value.is_a?(::TrueClass) || value.is_a?(::FalseClass)
10
12
  end
11
-
12
- def self.validate_hash(value)
13
- raise ValidateError, "invalid hash" unless value.is_a? ::Hash
14
- end
15
-
16
- def self.validate_not_negative_integer(value)
17
- raise ValidateError, "invalid not negative integer" unless value.is_a?(::Integer) && value >= 0
18
- end
19
-
20
- def self.validate_positive_integer(value)
21
- raise ValidateError, "invalid positive integer" unless value.is_a?(::Integer) && value.positive?
22
- end
23
-
24
- def self.validate_proc(value)
25
- unless value.is_a?(::Proc) || value.is_a?(::Method) || value.is_a?(::UnboundMethod)
26
- raise ValidateError, "invalid proc"
27
- end
28
- end
29
-
30
- def self.validate_string(value)
31
- raise ValidateError, "invalid string" unless value.is_a? ::String
32
- end
33
-
34
- def self.validate_symbol(value)
35
- raise ValidateError, "invalid symbol" unless value.is_a? ::Symbol
36
- end
37
13
  end
38
14
  end
data/lib/brs/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # Copyright (c) 2019 AUTHORS, MIT License.
3
3
 
4
4
  module BRS
5
- VERSION = "1.3.2".freeze
5
+ VERSION = "1.3.3".freeze
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-brs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Aladjev
@@ -9,8 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-03-18 00:00:00.000000000 Z
12
+ date: 2022-07-06 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: adsp
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.0'
14
28
  - !ruby/object:Gem::Dependency
15
29
  name: codecov
16
30
  requirement: !ruby/object:Gem::Requirement
@@ -59,14 +73,14 @@ dependencies:
59
73
  requirements:
60
74
  - - "~>"
61
75
  - !ruby/object:Gem::Version
62
- version: '5.15'
76
+ version: '5.16'
63
77
  type: :development
64
78
  prerelease: false
65
79
  version_requirements: !ruby/object:Gem::Requirement
66
80
  requirements:
67
81
  - - "~>"
68
82
  - !ruby/object:Gem::Version
69
- version: '5.15'
83
+ version: '5.16'
70
84
  - !ruby/object:Gem::Dependency
71
85
  name: ocg
72
86
  requirement: !ruby/object:Gem::Requirement
@@ -123,48 +137,62 @@ dependencies:
123
137
  - - ">="
124
138
  - !ruby/object:Gem::Version
125
139
  version: '0'
140
+ - !ruby/object:Gem::Dependency
141
+ name: rdoc
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - ">="
145
+ - !ruby/object:Gem::Version
146
+ version: '0'
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
126
154
  - !ruby/object:Gem::Dependency
127
155
  name: rubocop
128
156
  requirement: !ruby/object:Gem::Requirement
129
157
  requirements:
130
158
  - - "~>"
131
159
  - !ruby/object:Gem::Version
132
- version: '1.26'
160
+ version: '1.31'
133
161
  type: :development
134
162
  prerelease: false
135
163
  version_requirements: !ruby/object:Gem::Requirement
136
164
  requirements:
137
165
  - - "~>"
138
166
  - !ruby/object:Gem::Version
139
- version: '1.26'
167
+ version: '1.31'
140
168
  - !ruby/object:Gem::Dependency
141
169
  name: rubocop-minitest
142
170
  requirement: !ruby/object:Gem::Requirement
143
171
  requirements:
144
172
  - - "~>"
145
173
  - !ruby/object:Gem::Version
146
- version: '0.17'
174
+ version: '0.20'
147
175
  type: :development
148
176
  prerelease: false
149
177
  version_requirements: !ruby/object:Gem::Requirement
150
178
  requirements:
151
179
  - - "~>"
152
180
  - !ruby/object:Gem::Version
153
- version: '0.17'
181
+ version: '0.20'
154
182
  - !ruby/object:Gem::Dependency
155
183
  name: rubocop-performance
156
184
  requirement: !ruby/object:Gem::Requirement
157
185
  requirements:
158
186
  - - "~>"
159
187
  - !ruby/object:Gem::Version
160
- version: '1.13'
188
+ version: '1.14'
161
189
  type: :development
162
190
  prerelease: false
163
191
  version_requirements: !ruby/object:Gem::Requirement
164
192
  requirements:
165
193
  - - "~>"
166
194
  - !ruby/object:Gem::Version
167
- version: '1.13'
195
+ version: '1.14'
168
196
  - !ruby/object:Gem::Dependency
169
197
  name: rubocop-rake
170
198
  requirement: !ruby/object:Gem::Requirement
@@ -226,16 +254,10 @@ files:
226
254
  - lib/brs/error.rb
227
255
  - lib/brs/file.rb
228
256
  - lib/brs/option.rb
229
- - lib/brs/stream/abstract.rb
230
- - lib/brs/stream/delegates.rb
231
- - lib/brs/stream/raw/abstract.rb
232
257
  - lib/brs/stream/raw/compressor.rb
233
258
  - lib/brs/stream/raw/decompressor.rb
234
259
  - lib/brs/stream/reader.rb
235
- - lib/brs/stream/reader_helpers.rb
236
- - lib/brs/stream/stat.rb
237
260
  - lib/brs/stream/writer.rb
238
- - lib/brs/stream/writer_helpers.rb
239
261
  - lib/brs/string.rb
240
262
  - lib/brs/validation.rb
241
263
  - lib/brs/version.rb
@@ -252,7 +274,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
252
274
  requirements:
253
275
  - - ">="
254
276
  - !ruby/object:Gem::Version
255
- version: '2.5'
277
+ version: '2.6'
256
278
  required_rubygems_version: !ruby/object:Gem::Requirement
257
279
  requirements:
258
280
  - - ">="
@@ -1,156 +0,0 @@
1
- # Ruby bindings for brotli library.
2
- # Copyright (c) 2019 AUTHORS, MIT License.
3
-
4
- require_relative "delegates"
5
- require_relative "stat"
6
- require_relative "../error"
7
- require_relative "../validation"
8
-
9
- module BRS
10
- module Stream
11
- class Abstract
12
- # Native stream is not seekable by design.
13
- # Related methods like "seek" and "pos=" can't be implemented.
14
-
15
- # It is not possible to maintain correspondance between bytes
16
- # consumed from source and bytes written to destination by design.
17
- # We will consume all source bytes and maintain buffer with remaining destination data.
18
-
19
- include Delegates
20
-
21
- attr_reader :io, :stat, :external_encoding, :internal_encoding, :transcode_options, :pos
22
-
23
- alias tell pos
24
-
25
- def initialize(io, options = {})
26
- @raw_stream = create_raw_stream
27
- @io = io
28
-
29
- @stat = Stat.new @io.stat if @io.respond_to? :stat
30
-
31
- set_encoding options[:external_encoding], options[:internal_encoding], options[:transcode_options]
32
- reset_buffer
33
- reset_io_advise
34
-
35
- @pos = 0
36
- end
37
-
38
- # -- buffer --
39
-
40
- protected def reset_buffer
41
- @buffer = ::String.new :encoding => ::Encoding::BINARY
42
- end
43
-
44
- # -- advise --
45
-
46
- protected def reset_io_advise
47
- # Both compressor and decompressor need sequential io access.
48
- @io.advise :sequential if @io.respond_to? :advise
49
- rescue ::Errno::ESPIPE
50
- # ok
51
- end
52
-
53
- def advise
54
- # Noop
55
- nil
56
- end
57
-
58
- # -- encoding --
59
-
60
- def set_encoding(*args)
61
- external_encoding, internal_encoding, transcode_options = process_set_encoding_arguments(*args)
62
-
63
- set_target_encoding :@external_encoding, external_encoding
64
- set_target_encoding :@internal_encoding, internal_encoding
65
- @transcode_options = transcode_options
66
-
67
- self
68
- end
69
-
70
- protected def process_set_encoding_arguments(*args)
71
- external_encoding = args[0]
72
-
73
- unless external_encoding.nil? || external_encoding.is_a?(::Encoding)
74
- Validation.validate_string external_encoding
75
-
76
- # First argument can be "external_encoding:internal_encoding".
77
- match = %r{(.+?):(.+)}.match external_encoding
78
-
79
- unless match.nil?
80
- external_encoding = match[0]
81
- internal_encoding = match[1]
82
-
83
- transcode_options = args[1]
84
- Validation.validate_hash transcode_options unless transcode_options.nil?
85
-
86
- return [external_encoding, internal_encoding, transcode_options]
87
- end
88
- end
89
-
90
- internal_encoding = args[1]
91
- unless internal_encoding.nil? || internal_encoding.is_a?(::Encoding)
92
- Validation.validate_string internal_encoding
93
- end
94
-
95
- transcode_options = args[2]
96
- Validation.validate_hash transcode_options unless transcode_options.nil?
97
-
98
- [external_encoding, internal_encoding, transcode_options]
99
- end
100
-
101
- protected def set_target_encoding(name, value)
102
- unless value.nil? || value.is_a?(::Encoding)
103
- begin
104
- value = ::Encoding.find value
105
- rescue ::ArgumentError
106
- raise ValidateError, "invalid #{name} encoding"
107
- end
108
- end
109
-
110
- instance_variable_set name, value
111
- end
112
-
113
- protected def target_encoding
114
- return @internal_encoding unless @internal_encoding.nil?
115
- return @external_encoding unless @external_encoding.nil?
116
-
117
- ::Encoding::BINARY
118
- end
119
-
120
- # -- etc --
121
-
122
- def rewind
123
- @raw_stream = create_raw_stream
124
-
125
- @io.rewind if @io.respond_to? :rewind
126
-
127
- reset_buffer
128
- reset_io_advise
129
-
130
- @pos = 0
131
-
132
- 0
133
- end
134
-
135
- def close
136
- @io.close if @io.respond_to? :close
137
-
138
- nil
139
- end
140
-
141
- def closed?
142
- return false unless @raw_stream.closed?
143
-
144
- if @io.respond_to? :closed
145
- @io.closed?
146
- else
147
- true
148
- end
149
- end
150
-
151
- def to_io
152
- self
153
- end
154
- end
155
- end
156
- end
@@ -1,36 +0,0 @@
1
- # Ruby bindings for brotli library.
2
- # Copyright (c) 2019 AUTHORS, MIT License.
3
-
4
- require "forwardable"
5
-
6
- module BRS
7
- module Stream
8
- module Delegates
9
- DELEGATES = %i[
10
- autoclose=
11
- autoclose?
12
- binmode
13
- binmode?
14
- close_on_exec=
15
- close_on_exec?
16
- fcntl
17
- fdatasync
18
- fileno
19
- fsync
20
- ioctl
21
- isatty
22
- pid
23
- sync
24
- sync=
25
- to_i
26
- tty?
27
- ]
28
- .freeze
29
-
30
- def self.included(klass)
31
- klass.extend ::Forwardable
32
- klass.def_delegators :@io, *DELEGATES
33
- end
34
- end
35
- end
36
- end
@@ -1,59 +0,0 @@
1
- # Ruby bindings for brotli library.
2
- # Copyright (c) 2019 AUTHORS, MIT License.
3
-
4
- require "brs_ext"
5
-
6
- require_relative "../../error"
7
- require_relative "../../validation"
8
-
9
- module BRS
10
- module Stream
11
- module Raw
12
- class Abstract
13
- def initialize(native_stream)
14
- @native_stream = native_stream
15
- @is_closed = false
16
- end
17
-
18
- # -- write --
19
-
20
- def flush(&writer)
21
- write_result(&writer)
22
-
23
- nil
24
- end
25
-
26
- protected def more_destination(&writer)
27
- result_bytesize = write_result(&writer)
28
- raise NotEnoughDestinationError, "not enough destination" if result_bytesize.zero?
29
- end
30
-
31
- protected def write_result(&_writer)
32
- result = @native_stream.read_result
33
- yield result
34
-
35
- result.bytesize
36
- end
37
-
38
- # -- close --
39
-
40
- protected def do_not_use_after_close
41
- raise UsedAfterCloseError, "used after close" if closed?
42
- end
43
-
44
- def close(&writer)
45
- write_result(&writer)
46
-
47
- @native_stream.close
48
- @is_closed = true
49
-
50
- nil
51
- end
52
-
53
- def closed?
54
- @is_closed
55
- end
56
- end
57
- end
58
- end
59
- end