Skip to content

Commit

Permalink
Rename to Nodo
Browse files Browse the repository at this point in the history
  • Loading branch information
mtgrosser committed Oct 2, 2021
1 parent ef3ad4b commit 3ee918c
Show file tree
Hide file tree
Showing 25 changed files with 300 additions and 219 deletions.
57 changes: 40 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
[![Gem Version](https://badge.fury.io/rb/jass-core.svg)](http://badge.fury.io/rb/jass-core)
[![Gem Version](https://badge.fury.io/rb/nodo.svg)](http://badge.fury.io/rb/nodo)

# Jass::Core – call Node.js from Ruby
# Nōdo – call Node.js from Ruby

Jass::Core provides a Ruby environment to call JavaScript running inside a Node process.
`Nodo` provides a Ruby environment to interact with JavaScript running inside a Node process.
ノード means "node" in Japanese.

## Why Nodo?

Nodo will dispatch all JS function calls to a single long-running Node process.

JavaScript code is run in a namespaced environment, where you can access your initialized
JS objects during sequential function calls without having to re-initialize them.

IPC is done via unix sockets, greatly improving performance over classic process/eval solutions.

## Installation

In your Gemfile:

```ruby
gem 'jass-core'
gem 'nodo'
```

### Node.js

Jass requires a working installation of Node.js.
Nodo requires a working installation of Node.js.

If the executable is located in your `PATH`, no configuration is required. Otherwise, the path to to binary can be set using:

```ruby
Nodo.binary = '/usr/local/bin/node'
```

## Usage

In Nodo, you define JS functions as you would define Ruby methods:

```ruby
class Foo < Jass::Core
class Foo < Nodo::Core

function :say_hi, <<~JS
(name) => {
Expand All @@ -30,8 +48,8 @@ class Foo < Jass::Core
end

foo = Foo.new
foo.say_hi('Jass')
=> "Hello Jass!"
foo.say_hi('Nodo')
=> "Hello Nodo!"
```

### Using npm modules
Expand All @@ -45,7 +63,7 @@ $ yarn add uuid
Then `require` your dependencies:

```ruby
class Bar < Jass::Core
class Bar < Nodo::Core
require :uuid

function :v4, <<~JS
Expand All @@ -56,13 +74,13 @@ class Bar < Jass::Core
end

bar = Bar.new
bar.v4 => '"b305f5c4-db9a-4504-b0c3-4e097a5ec8b9"
bar.v4 => "b305f5c4-db9a-4504-b0c3-4e097a5ec8b9"
```

### Aliasing requires

```ruby
class FooBar < Jass::Core
class FooBar < Nodo::Core
require commonjs: '@rollup/plugin-commonjs'
end
```
Expand All @@ -73,32 +91,37 @@ By default, `./node_modules` is used as the `NODE_PATH`.

To set a custom path:
```ruby
Jass.modules_root = 'path/to/node_modules'
Nodo.modules_root = 'path/to/node_modules'
```

For Rails:
For Rails applications, it will be set to `vendor/node_modules`.
To use the Rails 6 default of putting `node_modules` to `RAILS_ROOT`:

```ruby
# config/initializers/jass.rb
Jass.modules_root = Rails.root.join('vendor', 'node_modules')
# config/initializers/nodo.rb
Nodo.modules_root = Rails.root.join('node_modules')
```

### Defining JS constants

```ruby
class BarFoo < Jass::Core
class BarFoo < Nodo::Core
const :HELLO, "World"
end
```

### Execute some custom JS during initialization

```ruby
class BarFoo < Jass::Core
class BarFoo < Nodo::Core

script <<~JS
// some custom JS
// to be executed during initialization
JS
end
```

### Inheritance

Subclasses will inherit functions, constants, dependencies and scripts from their superclasses, while only functions can be overwritten.
2 changes: 1 addition & 1 deletion bin/console
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "jass/core"
require "nodo"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand Down
3 changes: 0 additions & 3 deletions lib/jass-core.rb

This file was deleted.

36 changes: 0 additions & 36 deletions lib/jass/core/client.rb

This file was deleted.

11 changes: 0 additions & 11 deletions lib/jass/core/constant.rb

This file was deleted.

11 changes: 0 additions & 11 deletions lib/jass/core/dependency.rb

This file was deleted.

11 changes: 0 additions & 11 deletions lib/jass/core/function.rb

This file was deleted.

10 changes: 0 additions & 10 deletions lib/jass/core/railtie.rb

This file was deleted.

11 changes: 0 additions & 11 deletions lib/jass/core/script.rb

This file was deleted.

5 changes: 0 additions & 5 deletions lib/jass/core/version.rb

This file was deleted.

24 changes: 24 additions & 0 deletions lib/nodo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'pathname'
require 'json'
require 'fileutils'
require 'tmpdir'

module Nodo
class << self
attr_accessor :modules_root, :env, :binary
end
self.modules_root = './node_modules'
self.env = {}
self.binary = 'node'
end

require_relative 'nodo/version'
require_relative 'nodo/errors'
require_relative 'nodo/dependency'
require_relative 'nodo/function'
require_relative 'nodo/script'
require_relative 'nodo/constant'
require_relative 'nodo/client'
require_relative 'nodo/core'

require_relative 'nodo/railtie' if defined?(Rails)
38 changes: 38 additions & 0 deletions lib/nodo/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'net/http'

module Nodo
class Client < Net::HTTP
UNIX_REGEXP = /\Aunix:\/\//i

def initialize(address, port = nil)
super(address, port)
case address
when UNIX_REGEXP
@socket_type = 'unix'
@socket_path = address.sub(UNIX_REGEXP, '')
# Host header is required for HTTP/1.1
@address = 'localhost'
@port = 80
else
@socket_type = 'inet'
end
end

def connect
if @socket_type == 'unix'
connect_unix
else
super
end
end

def connect_unix
s = Timeout.timeout(@open_timeout) { UNIXSocket.open(@socket_path) }
@socket = Net::BufferedIO.new(s)
@socket.read_timeout = @read_timeout
@socket.continue_timeout = @continue_timeout
@socket.debug_output = @debug_output
on_connect
end
end
end
13 changes: 13 additions & 0 deletions lib/nodo/constant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Nodo
class Constant
attr_reader :name, :value

def initialize(name, value)
@name, @value = name, value
end

def to_js
"const #{name} = #{value.to_json};\n"
end
end
end
Loading

0 comments on commit 3ee918c

Please sign in to comment.