Sha256: 5e6db7b1dec60ec8210a617c43bbd95a77043a45f274c10f05847317c1044d87

Contents?: true

Size: 1.62 KB

Versions: 16

Compression:

Stored size: 1.62 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?(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

16 entries across 16 versions & 1 rubygems

Version Path
proscenium-0.11.0.pre.6-x86_64-linux lib/proscenium/middleware.rb
proscenium-0.11.0.pre.6-aarch64-linux lib/proscenium/middleware.rb
proscenium-0.11.0.pre.6-arm64-darwin lib/proscenium/middleware.rb
proscenium-0.11.0.pre.6-x86_64-darwin lib/proscenium/middleware.rb
proscenium-0.11.0.pre.5-x86_64-linux lib/proscenium/middleware.rb
proscenium-0.11.0.pre.5-aarch64-linux lib/proscenium/middleware.rb
proscenium-0.11.0.pre.5-arm64-darwin lib/proscenium/middleware.rb
proscenium-0.11.0.pre.5-x86_64-darwin lib/proscenium/middleware.rb
proscenium-0.11.0.pre.4-x86_64-linux lib/proscenium/middleware.rb
proscenium-0.11.0.pre.4-arm64-darwin lib/proscenium/middleware.rb
proscenium-0.11.0.pre.3-arm64-darwin lib/proscenium/middleware.rb
proscenium-0.11.0.pre.3-x86_64-linux lib/proscenium/middleware.rb
proscenium-0.11.0.pre.2-x86_64-linux lib/proscenium/middleware.rb
proscenium-0.11.0.pre.2-aarch64-linux lib/proscenium/middleware.rb
proscenium-0.11.0.pre.2-arm64-darwin lib/proscenium/middleware.rb
proscenium-0.11.0.pre.2-x86_64-darwin lib/proscenium/middleware.rb