Sha256: 58312d656f347b3794cb8a71d4d44af586c40ee572c7393c75ac19512142c8e3

Contents?: true

Size: 1.43 KB

Versions: 11

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module Proscenium
  class Middleware
    extend ActiveSupport::Autoload

    # Error when the build command fails.
    class BuildError < StandardError; end

    autoload :Base
    autoload :Esbuild
    autoload :Url

    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

    def attempt(request)
      return unless (type = find_type(request))

      # file_handler.attempt(request.env) || type.attempt(request)

      type.attempt(request)
    end

    # Returns the type of file being requested using Proscenium::MIDDLEWARE_GLOB_TYPES.
    def find_type(request)
      path = Pathname.new(request.path)

      return Url if request.path.match?(glob_types[:url])
      return Esbuild if path.fnmatch?(application_glob_type, File::FNM_EXTGLOB)
    end

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

    def glob_types
      @glob_types ||= Proscenium::MIDDLEWARE_GLOB_TYPES
    end

    def application_glob_type
      paths = Rails.application.config.proscenium.include_paths.join(',')
      "/{#{paths}}#{glob_types[:application]}"
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
proscenium-0.8.2-x86_64-linux lib/proscenium/middleware.rb
proscenium-0.8.2-aarch64-linux lib/proscenium/middleware.rb
proscenium-0.8.2-arm64-darwin lib/proscenium/middleware.rb
proscenium-0.8.2-x86_64-darwin lib/proscenium/middleware.rb
proscenium-0.8.0-aarch64-linux lib/proscenium/middleware.rb
proscenium-0.8.0-arm64-darwin lib/proscenium/middleware.rb
proscenium-0.8.0-x86_64-darwin lib/proscenium/middleware.rb
proscenium-0.7.0-x86_64-linux lib/proscenium/middleware.rb
proscenium-0.7.0-aarch64-linux lib/proscenium/middleware.rb
proscenium-0.7.0-arm64-darwin lib/proscenium/middleware.rb
proscenium-0.7.0-x86_64-darwin lib/proscenium/middleware.rb