Class: Dommy::CompressionStream

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/compression_streams.rb

Overview

‘CompressionStream` / `DecompressionStream` — wrap `TransformStream` over Ruby’s ‘Zlib` to gzip/deflate/raw-deflate byte chunks. Each `write(chunk)` accumulates into an internal buffer; `close()` finalizes and emits the compressed/decompressed bytes downstream.

Spec: wicg.github.io/compression/

Constant Summary collapse

SUPPORTED =
%w[gzip deflate deflate-raw].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window, format) ⇒ CompressionStream

Returns a new instance of CompressionStream.

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dommy/compression_streams.rb', line 18

def initialize(window, format)
  raise ArgumentError, "unsupported format #{format.inspect}" unless SUPPORTED.include?(format.to_s)

  @buffer = +""
  compressor = build_compressor(format.to_s)

  @readable = ReadableStream.new(window)
  controller = TransformStreamDefaultController.new(@readable)

  @writable = WritableStream.new(
    window,
    {
      "write" => proc { |chunk| @buffer << coerce(chunk) },
      "close" => proc do
        compressed = compressor.call(@buffer)
        controller.enqueue(compressed)
        @readable.__close__
      end,
      "abort" => proc { |r| @readable.__error__(r) }
    }
  )
end

Instance Attribute Details

#readableObject (readonly)

Returns the value of attribute readable.



16
17
18
# File 'lib/dommy/compression_streams.rb', line 16

def readable
  @readable
end

#writableObject (readonly)

Returns the value of attribute writable.



16
17
18
# File 'lib/dommy/compression_streams.rb', line 16

def writable
  @writable
end

Instance Method Details

#__js_get__(key) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/dommy/compression_streams.rb', line 41

def __js_get__(key)
  case key
  when "readable"
    @readable
  when "writable"
    @writable
  end
end