Sha256: 3694041df12ca63ae47b28396a0f04a5e98e3de2f3280607ef2e68069b45950c

Contents?: true

Size: 1.24 KB

Versions: 8

Compression:

Stored size: 1.24 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

    def find_type(request)
      return Url if request.path.match?(%r{^/https?%3A%2F%2F})
      return Esbuild if Pathname.new(request.path).fnmatch?(path_glob, File::FNM_EXTGLOB)
    end

    def path_glob
      paths = Rails.application.config.proscenium.include_paths.join(',')
      "/{#{paths}}/**.{#{FILE_EXTENSIONS.join(',')}}"
    end

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
proscenium-0.9.1-x86_64-linux lib/proscenium/middleware.rb
proscenium-0.9.1-aarch64-linux lib/proscenium/middleware.rb
proscenium-0.9.1-arm64-darwin lib/proscenium/middleware.rb
proscenium-0.9.1-x86_64-darwin lib/proscenium/middleware.rb
proscenium-0.9.0-x86_64-linux lib/proscenium/middleware.rb
proscenium-0.9.0-aarch64-linux lib/proscenium/middleware.rb
proscenium-0.9.0-arm64-darwin lib/proscenium/middleware.rb
proscenium-0.9.0-x86_64-darwin lib/proscenium/middleware.rb