Sha256: 3cb42985337a72ddefe4695bf7b7c73b233550096e6544f101f204e747046db5

Contents?: true

Size: 1.75 KB

Versions: 6

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

module Proscenium
  class Middleware
    extend ActiveSupport::Autoload

    autoload :Base
    autoload :Esbuild
    autoload :ParcelCss
    autoload :Runtime

    MIDDLEWARE_CLASSES = {
      esbuild: Esbuild,
      parcelcss: ParcelCss,
      runtime: Runtime
    }.freeze

    def initialize(app)
      @app = app
    end

    def call(env)
      request = Rack::Request.new(env)

      return @app.call(env) if !request.get? && !request.head?

      attempt(request) || @app.call(env)
    end

    private

    # Look for the precompiled file in public/assets first, then fallback to the Proscenium
    # middleware that matches the type of file requested, ie: .js => esbuild.
    # See Rails.application.config.proscenium.glob_types.
    def attempt(request)
      return unless (type = find_type(request))

      file_handler.attempt(request.env) || MIDDLEWARE_CLASSES[type].attempt(request)
    end

    # Returns the type of file being requested using Rails.application.config.proscenium.glob_types.
    def find_type(request)
      return :runtime if request.path_info.start_with?('/proscenium-runtime/')

      path = Rails.root.join(request.path[1..])

      type, = glob_types.find do |_, globs|
        # TODO: Look for the precompiled file in public/assets first
        #   globs.any? { |glob| Rails.public_path.join('assets').glob(glob).any?(path) }

        globs.any? { |glob| Rails.root.glob(glob).any?(path) }
      end

      type
    end

    def file_handler
      ::ActionDispatch::FileHandler.new Rails.public_path.join('assets').to_s,
                                        headers: { 'X-Proscenium-Middleware' => 'precompiled' }
    end

    def glob_types
      Rails.application.config.proscenium.glob_types
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
proscenium-0.1.0.alpha2-x86_64-linux lib/proscenium/middleware.rb
proscenium-0.1.0.alpha2-x86_64-darwin lib/proscenium/middleware.rb
proscenium-0.1.0.alpha2-arm64-darwin lib/proscenium/middleware.rb
proscenium-0.1.0.alpha1-x86_64-linux lib/proscenium/middleware.rb
proscenium-0.1.0.alpha1-x86_64-darwin lib/proscenium/middleware.rb
proscenium-0.1.0.alpha1-arm64-darwin lib/proscenium/middleware.rb