Sha256: 027acb4b6c577944deb7eb2768a361978b760c16b50d607feeb13810d2c762c5

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 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 :Runtime
    autoload :Url

    def initialize(app)
      @app = app

      chunks_path = Rails.public_path.join('assets').to_s
      headers = Rails.application.config.public_file_server.headers || {}
      @chunk_handler = ::ActionDispatch::FileHandler.new(chunks_path, headers: headers)
    end

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

      return @app.call(env) if !request.get? && !request.head?
      return @chunk_handler.attempt(request.env) if request.path.match?(%r{^/_asset_chunks/})

      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 Runtime if request.path.match?(%r{^/@proscenium/})

      Esbuild if Pathname.new(request.path).fnmatch?(app_path_glob, File::FNM_EXTGLOB)
    end

    def app_path_glob
      "/{#{Proscenium::ALLOWED_DIRECTORIES.join(',')}}/**.{#{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

4 entries across 4 versions & 1 rubygems

Version Path
proscenium-0.11.0.pre.7-x86_64-linux lib/proscenium/middleware.rb
proscenium-0.11.0.pre.7-aarch64-linux lib/proscenium/middleware.rb
proscenium-0.11.0.pre.7-arm64-darwin lib/proscenium/middleware.rb
proscenium-0.11.0.pre.7-x86_64-darwin lib/proscenium/middleware.rb