class Isomorfeus::AssetManager::RackMiddleware

Constants

WS_RESPONSE

Attributes

asset_manager[R]

Public Class Methods

new(app) click to toggle source
# File lib/isomorfeus/asset_manager/rack_middleware.rb, line 9
def initialize(app)
  STDERR.puts "asset manager"
  @app = app
  @asset_manager = Isomorfeus::AssetManager.new
  @compressible_types = %w[application/javascript text/javascript]
  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

Public Instance Methods

call(env) click to toggle source
# File lib/isomorfeus/asset_manager/rack_middleware.rb, line 25
def call(env)
  path_info = env['PATH_INFO']
  if path_info.start_with?(@assets_path)
    asset_key = path_info[@assets_path_size..-1]

    # get js
    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, {}, asset.bundle]
        end              
      end
    
    # get source map
    elsif asset_key.end_with?('.js.map')
      asset = Isomorfeus.assets[asset_key[0..-5]]
      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, {}, asset.bundle_map]
        end 
      end
    end

  # 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) click to toggle source
# File lib/isomorfeus/asset_manager/rack_middleware.rb, line 81
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