Class: Isomorfeus::AssetManager::RackMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/isomorfeus/asset_manager/rack_middleware.rb

Constant Summary collapse

WS_RESPONSE =
[0, {}, []].freeze
C_ENCODINGS =
%w[br gzip].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RackMiddleware

Returns a new instance of RackMiddleware.



11
12
13
14
15
16
17
18
# File 'lib/isomorfeus/asset_manager/rack_middleware.rb', line 11

def initialize(app)
  @app = app
  @asset_manager = Isomorfeus::AssetManager.new
  @assets_path = Isomorfeus.assets_path.end_with?('/') ? Isomorfeus.assets_path : "#{Isomorfeus.assets_path}/"
  @assets_path_size = @assets_path.size
rescue Exception => e
  STDERR.puts "#{e.message}\n#{e.backtrace&.join("\n")}\n"
end

Instance Attribute Details

#asset_managerObject (readonly)

Returns the value of attribute asset_manager.



9
10
11
# File 'lib/isomorfeus/asset_manager/rack_middleware.rb', line 9

def asset_manager
  @asset_manager
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/isomorfeus/asset_manager/rack_middleware.rb', line 20

def call(env)
  path_info = env['PATH_INFO']
  if path_info.start_with?(@assets_path)

    if !Isomorfeus.production?
      if path_info.end_with?('.rb.js')
        asset_key_module_name = path_info[@assets_path_size..-7]
        asset_key, module_name = asset_key_module_name.split('/')
        asset_key += '.js'
        if Isomorfeus.assets.key?(asset_key)
          asset = Isomorfeus.assets[asset_key]
          if asset && asset.browser?
            if asset.bundle_ruby_modules.key?(module_name)
              headers = { Rack::CONTENT_TYPE => 'application/javascript' }
              asset_manager.transition(asset_key, asset)
              return [200, headers, get_content(env, headers, asset, asset_key, asset.bundle_ruby_modules[module_name][:js])]
            end
          end
        end

      elsif path_info.end_with?('.rb.js.map')
        asset_key_module_name = path_info[@assets_path_size..-11]
        asset_key, module_name = asset_key_module_name.split('/')
        asset_key += '.js'
        asset = Isomorfeus.assets[asset_key]
        if asset && asset.browser?
          if asset.bundle_ruby_modules.key?(module_name)
            headers = { Rack::CONTENT_TYPE => 'application/json' }
            asset_manager.transition(asset_key, asset)
            return [200, headers, get_content(env, headers, asset, asset_key, asset.bundle_ruby_modules[module_name][:map])]
          end
        end

      elsif path_info.end_with?('.js.map')
        asset_key = path_info[@assets_path_size..-5]
        asset = Isomorfeus.assets[asset_key]
        if asset && asset.browser?
          headers = { Rack::CONTENT_TYPE => 'application/json' }
          asset_manager.transition(asset_key, asset)
          return [200, headers, get_content(env, headers, asset, asset_key, asset.hash[:js][:map])]
        end

      elsif path_info.end_with?('.css.map')
        # get css source map
        asset_key = path_info[@assets_path_size..-9] + '.js'
        asset = Isomorfeus.assets[asset_key]
        if asset && asset.browser?
          headers = { Rack::CONTENT_TYPE => 'application/json' }
          asset_manager.transition(asset_key, asset)
          return [200, headers, get_content(env, headers, asset, asset_key, asset.hash[:css][:map])]
        end
      end
    end

    if path_info.end_with?('.js')
      asset_key = path_info[@assets_path_size..-1]
      if Isomorfeus.assets.key?(asset_key)
        asset = Isomorfeus.assets[asset_key]
        if asset && asset.browser?
          headers = { Rack::CONTENT_TYPE => 'application/javascript' }
          asset_manager.transition(asset_key, asset)
          return [200, headers, get_content(env, headers, asset, asset_key, asset.hash[:js][:js])]
        end
      end

    elsif path_info.end_with?('.css')
      asset_key = path_info[@assets_path_size..-5] + '.js'
      asset = Isomorfeus.assets[asset_key]
      if asset && asset.browser?
        headers = { Rack::CONTENT_TYPE => 'text/css' }
        asset_manager.transition(asset_key, asset)
        return [200, headers, get_content(env, headers, asset, asset_key, asset.hash[:css][:css])]
      end
    end

    return [404, {}, 'not found']
  # hot reloading subscription
  elsif Isomorfeus.development? && path_info == Isomorfeus.hmr_websocket_path
    if env['rack.upgrade?'] == :websocket
      env['rack.upgrade'] = Isomorfeus::AssetManager::ServerSocketProcessor.new
    end
    WS_RESPONSE
  else
    @app.call(env)
  end
rescue Exception => e
  STDERR.puts "#{e.message}\n#{e.backtrace&.join("\n")}\n"
  @app.call(env)
end

#get_content(env, headers, asset, asset_key, content_hash) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/isomorfeus/asset_manager/rack_middleware.rb', line 110

def get_content(env, headers, asset, asset_key, content_hash)
  request = Rack::Request.new(env)
  best_encoding = Rack::Utils.select_best_encoding(C_ENCODINGS, request.accept_encoding)
  content = ''
  case best_encoding
  when 'gzip'
    headers['Content-Encoding'] = "gzip"
    unless content_hash.key?(:gzip)
      asset.mutex.synchronize { content_hash[:gzip] = Zlib::gzip(content_hash[:raw], level: Zlib::BEST_COMPRESSION) unless content_hash.key?(:gzip) }
    end
    content = content_hash[:gzip]
  when 'br'
    headers['Content-Encoding'] = "br"
    unless content_hash.key?(:br)
      asset.mutex.synchronize { content_hash[:br] = Brotli.deflate(content_hash[:raw], quality: Isomorfeus.production? ? 9 : 5) unless content_hash.key?(:br) }
    end
    content = content_hash[:br]
  else
    content = content_hash[:raw]
  end
  headers[Rack::CONTENT_LENGTH] = content.size
  headers['Last-Modified'] = asset.mtime
  [content]
end