Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: ['3.3', '3.2', '3.1']
rails: ['7.0', '7.1']
ruby: ["3.4", "3.3", "3.2"]
rails: ["8.0", "8.1"]

runs-on: 'ubuntu-latest'
runs-on: "ubuntu-latest"
env:
ARUBA_TIMEOUT: 240
BUNDLE_GEMFILE: gemfiles/rails${{ matrix.rails }}.gemfile
Expand All @@ -22,7 +22,7 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: 18
cache: 'yarn'
cache: "yarn"
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
Expand Down
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ gemspec
gem "rake"
gem "rspec"
gem "byebug"
gem 'rails', '~> 7.0'
gem 'rails', '~> 8.0'
gem 'mutex_m'
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ Humid.configure do |config|
# `console.log` and friends (`warn`, `error`) are delegated to
# the respective logger levels on the ruby side.
#
# Defaults to `Logger.new(STDOUT)`
config.logger = Rails.logger
# Defaults to `nil`
config.logger = Rails.env.development? ? Rails.logger : nil

# Options passed to mini_racer.
#
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails7.0.gemfile → gemfiles/rails8.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ gemspec name: "humid", path: "../"
gem "rake"
gem "rspec"
gem "byebug"
gem 'rails', '~> 7.0.0'
gem 'rails', '~> 8.0.0'
2 changes: 1 addition & 1 deletion gemfiles/rails7.1.gemfile → gemfiles/rails8.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ gemspec name: "humid", path: "../"
gem "rake"
gem "rspec"
gem "byebug"
gem 'rails', '~> 7.1.0'
gem 'rails', '~> 8.1.0'
2 changes: 1 addition & 1 deletion humid.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ Gem::Specification.new do |s|
s.files = Dir["MIT-LICENSE", "README.md", "lib/**/*"]

s.add_dependency "mini_racer", ">= 0.4"
s.add_dependency "activesupport", ">= 7.0"
s.add_dependency "activesupport", "~> 8.0"
end
154 changes: 76 additions & 78 deletions lib/humid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,106 +6,104 @@
require "humid/controller_runtime"
require "humid/version"

module Humid
extend self
include ActiveSupport::Configurable
class Humid
@@context = nil

class RenderError < StandardError
end

class FileNotFound < StandardError
end

class_attribute :config

@@context = nil
self.config = ActiveSupport::OrderedOptions.new.merge({
raise_render_errors: true,
context_options: {},
})

config_accessor :application_path
config_accessor :source_map_path
class << self
def configure
yield config
end

config_accessor :raise_render_errors do
true
end
def remove_functions
<<~JS
delete this.setTimeout;
delete this.setInterval;
delete this.clearTimeout;
delete this.clearInterval;
delete this.setImmediate;
delete this.clearImmediate;
JS
end

config_accessor :logger do
Logger.new($stdout)
end
def logger
config.logger
end

config_accessor :context_options do
{}
end
def renderer
<<~JS
var __renderer;
function setHumidRenderer(fn) {
__renderer = fn;
}
JS
end

def remove_functions
<<~JS
delete this.setTimeout;
delete this.setInterval;
delete this.clearTimeout;
delete this.clearInterval;
delete this.setImmediate;
delete this.clearImmediate;
JS
end
def context
@@context
end

def logger
config.logger
end
def dispose
if @@context
@@context.dispose
@@context = nil
end
end

def renderer
<<~JS
var __renderer;
function setHumidRenderer(fn) {
__renderer = fn;
}
JS
end
def create_context
ctx = MiniRacer::Context.new(**config.context_options)

def context
@@context
end
if logger
ctx.attach("console.log", proc { |err| logger.debug(err.to_s) })
ctx.attach("console.info", proc { |err| logger.info(err.to_s) })
ctx.attach("console.error", proc { |err| logger.error(err.to_s) })
ctx.attach("console.warn", proc { |err| logger.warn(err.to_s) })
end

def dispose
if @@context
@@context.dispose
@@context = nil
end
end
js = ""
js << remove_functions
js << renderer
ctx.eval(js)

def create_context
ctx = MiniRacer::Context.new(**config.context_options)
ctx.attach("console.log", proc { |err| logger.debug(err.to_s) })
ctx.attach("console.info", proc { |err| logger.info(err.to_s) })
ctx.attach("console.error", proc { |err| logger.error(err.to_s) })
ctx.attach("console.warn", proc { |err| logger.warn(err.to_s) })
source_path = config.application_path
map_path = config.source_map_path

js = ""
js << remove_functions
js << renderer
ctx.eval(js)
if map_path
ctx.attach("readSourceMap", proc { File.read(map_path) })
end

source_path = config.application_path
map_path = config.source_map_path
filename = File.basename(source_path.to_s)
@@current_filename = filename
ctx.eval(File.read(source_path), filename: filename)

if map_path
ctx.attach("readSourceMap", proc { File.read(map_path) })
@@context = ctx
end

filename = File.basename(source_path.to_s)
@@current_filename = filename
ctx.eval(File.read(source_path), filename: filename)

@@context = ctx
end

def render(*args)
ActiveSupport::Notifications.instrument("render.humid") do
context.call("__renderer", *args)
rescue MiniRacer::RuntimeError => e
message = ([e.message] + e.backtrace.filter { |x| x.starts_with? "JavaScript" }).join("\n")
render_error = Humid::RenderError.new(message)

if config.raise_render_errors
raise render_error
else
config.logger.error(render_error.inspect)
""
def render(*args)
ActiveSupport::Notifications.instrument("render.humid") do
context.call("__renderer", *args)
rescue MiniRacer::RuntimeError => e
message = ([e.message] + e.backtrace.filter { |x| x.starts_with? "JavaScript" }).join("\n")
render_error = Humid::RenderError.new(message)

if config.raise_render_errors
raise render_error
else
config.logger.error(render_error.inspect)
""
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/humid/controller_runtime.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Humid
class Humid
module ControllerRuntime
extend ActiveSupport::Concern

Expand Down
2 changes: 1 addition & 1 deletion lib/humid/log_subscriber.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Humid
class Humid
class LogSubscriber < ActiveSupport::LogSubscriber
thread_cattr_accessor :humid_runtime

Expand Down
2 changes: 1 addition & 1 deletion lib/humid/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Humid
class Humid
VERSION = "0.0.6".freeze
end
13 changes: 13 additions & 0 deletions spec/render_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,23 @@

it "proxies to Rails logger" do
allow(Humid.config).to receive("application_path") { js_path "simple.js" }
Humid.config.logger = Logger.new($stdout)

Humid.create_context
expect(Humid.logger).to receive(:info).with("hello")

Humid.context.eval("console.info('hello')")
Humid.config.logger = nil
end

it "does not set the logger if none is configured" do
allow(Humid.config).to receive("application_path") { js_path "simple.js" }

Humid.create_context

expect {
Humid.context.eval("console.log('hello')")
}.not_to raise_error
end
end

Expand Down