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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RackMiddleware

Returns a new instance of RackMiddleware.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/isomorfeus/asset_manager/rack_middleware.rb', line 9

def initialize(app)
  @app = app
  @asset_manager = Isomorfeus::AssetManager.new
  @compressible_types = %w[application/javascript text/javascript text/css]
  if Isomorfeus.assets_path.end_with?('/')
    @assets_path = Isomorfeus.assets_path
    @assets_path_size = @assets_path.size
  else
    @assets_path = Isomorfeus.assets_path + '/'
    @assets_path_size = @assets_path.size
  end
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.



7
8
9
# File 'lib/isomorfeus/asset_manager/rack_middleware.rb', line 7

def asset_manager
  @asset_manager
end

Instance Method Details

#call(env) ⇒ Object



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
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/isomorfeus/asset_manager/rack_middleware.rb', line 24

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

    if path_info.end_with?('.js')
      # get js

      asset_key = path_info[@assets_path_size..-1]
      if Isomorfeus.assets.key?(asset_key)
        asset = Isomorfeus.assets[asset_key]
        if asset && asset.target != :node
          asset_manager.transition(asset_key, asset)
          headers = {}
          headers['Last-Modified'] = asset.mtime
          headers[Rack::CONTENT_TYPE] = 'application/javascript'
          if should_gzip?(env)
            headers['Content-Encoding'] = "gzip"
            headers[Rack::CONTENT_LENGTH] = asset.bundle_gz_size
            return [200, headers, asset.bundle_gz]
          else
            headers[Rack::CONTENT_LENGTH] = asset.bundle_size
            return [200, headers, asset.bundle]
          end
        end
      end

    elsif path_info.end_with?('.js.map')
      # get js source map

      asset_key = path_info[@assets_path_size..-5]
      asset = Isomorfeus.assets[asset_key]
      if asset && asset.target != :node
        asset_manager.transition(asset_key, asset) unless asset.bundled?
        headers = {}
        headers['Last-Modified'] = asset.mtime
        headers[Rack::CONTENT_TYPE] = 'application/json'
        if should_gzip?(env)
          headers['Content-Encoding'] = "gzip"
          headers[Rack::CONTENT_LENGTH] = asset.bundle_map_gz_size
          return [200, headers, asset.bundle_map_gz]
        else
          headers[Rack::CONTENT_LENGTH] = asset.bundle_map_size
          return [200, headers, asset.bundle_map]
        end
      end

    elsif path_info.end_with?('.css')
      # get css

      asset_key = path_info[@assets_path_size..-5] + '.js'
      asset = Isomorfeus.assets[asset_key]
      if asset && asset.target != :node
        asset_manager.transition(asset_key, asset)
        headers = {}
        headers['Last-Modified'] = asset.mtime
        headers[Rack::CONTENT_TYPE] = 'text/css'
        if should_gzip?(env)
          headers['Content-Encoding'] = "gzip"
          headers[Rack::CONTENT_LENGTH] = asset.css_gz_size
          return [200, headers, asset.css_gz]
        else
          headers[Rack::CONTENT_LENGTH] = asset.css_size
          return [200, headers, asset.css]
        end
      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.target != :node
        asset_manager.transition(asset_key, asset) unless asset.bundled?
        headers = {}
        headers['Last-Modified'] = asset.mtime
        headers[Rack::CONTENT_TYPE] = 'application/json'
        if should_gzip?(env)
          headers['Content-Encoding'] = "gzip"
          headers[Rack::CONTENT_LENGTH] = asset.css_map_gz_size
          return [200, headers, asset.css_map_gz]
        else
          headers[Rack::CONTENT_LENGTH] = asset.css_map_size
          return [200, headers, asset.css_map]
        end
      end
    end

    return [404, {}, 'not found']
  # hot reloading subscription

  elsif Isomorfeus.development? && path_info == Isomorfeus.assets_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

#should_gzip?(env) ⇒ Boolean

Returns:

  • (Boolean)


122
123
124
125
126
127
# File 'lib/isomorfeus/asset_manager/rack_middleware.rb', line 122

def should_gzip?(env)
  return true if env.key?('HTTP_ACCEPT_ENCODING') && env['HTTP_ACCEPT_ENCODING'].match?(/\bgzip\b/)
  return false if /\bno-transform\b/.match?(env[Rack::CACHE_CONTROL].to_s) || env['Content-Encoding']&.!~(/\bidentity\b/)
  return false if @compressible_types && !(env.key?(Rack::CONTENT_TYPE) && @compressible_types.include?(env[Rack::CONTENT_TYPE][/[^;]*/]))
  true
end