• Stars
    star
    169
  • Rank 217,368 (Top 5 %)
  • Language
    Elixir
  • License
    MIT License
  • Created almost 6 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An Elixir API for calling Node.js functions

NodeJS

Build Status Hex.pm License: MIT Coverage Status

Provides an Elixir API for calling Node.js functions.

Documentation

The docs can be found at https://hexdocs.pm/nodejs.

Prerequisites

  • Elixir >= 1.7
  • NodeJS >= 10

Installation

def deps do
  [
    {:nodejs, "~> 2.0"}
  ]
end

Starting the service

Add NodeJS to your Supervisor as a child, pointing the required path option at the directory containing your JavaScript modules.

supervisor(NodeJS, [[path: "/node_app_root", pool_size: 4]])

Calling JavaScript module functions with NodeJS.call(module, args \\ []).

If the module exports a function directly, like this:

module.exports = (x) => x

You can call it like this:

NodeJS.call("echo", ["hello"]) #=> {:ok, "hello"}

There is also a call! form that throws on error instead of returning a tuple:

NodeJS.call!("echo", ["hello"]) #=> "hello"

If the module exports an object with named functions like:

exports.add = (a, b) => a + b
exports.sub = (a, b) => a - b

You can call them like this:

NodeJS.call({"math", :add}, [1, 2]) # => {:ok, 3}
NodeJS.call({"math", :sub}, [1, 2]) # => {:ok, -1}

In order to cope with Unicode character it is necessary to specify the binary option:

NodeJS.call("echo", ["’"], binary: true) # => {:ok, "’"}

There Are Rules & Limitations (Unfortunately)

  • Function arguments must be serializable to JSON.
  • Return values must be serializable to JSON. (Objects with circular references will definitely fail.)
  • Modules must be requested relative to the path that was given to the Supervisor. E.g., for a path of /node_app_root and a file /node_app_root/foo/index.js your module request should be for "foo/index.js" or "foo/index" or "foo".

Running the tests

Since the test suite requires npm dependencies before you can run the tests you will first need to run

cd test/js && npm install && cd ../..

After that you should be able to run

mix test

Handling Callbacks and Promises

You can see examples of using promises in the tests here:

https://github.com/revelrylabs/elixir-nodejs/blob/master/test/nodejs_test.exs#L125

and from the JavaScript code here:

module.exports = async function echo(x, delay = 1000) {
  return new Promise((resolve) => setTimeout(() => resolve(x), delay))
}

https://github.com/revelrylabs/elixir-nodejs/blob/master/test/js/slow-async-echo.js

More Repositories

1

elixir_react_render

React SSR Framework for Elixir
Elixir
111
star
2

ecto_soft_delete

Soft Deletion for Ecto
Elixir
106
star
3

harmonium

An opinionated React component framework for teams that move fast.
JavaScript
34
star
4

revelry_phoenix_app_template

App Template for Revelry Phoenix
Elixir
32
star
5

text_chunker_ex

A library for semantically coherent text chunking
Elixir
27
star
6

ecto_explain

Elixir
25
star
7

elixir-stellar-client

Elixir Client for Stellar
Elixir
21
star
8

sassy-npm-importer

Import SASS from npm via a customizable prefix.
JavaScript
18
star
9

adminable

Allows creating admin interfaces through implementing the `Adminable` behaviour
Elixir
15
star
10

hubkit

Hubkit provides methods for querying the github API at a higher level than making individual API calls. Think of it like an ORM for the github API.
Ruby
15
star
11

slax

Elixir Slack slash command handler
Elixir
13
star
12

ecto_filters

Elixir
12
star
13

liveview_web3

Elixir
10
star
14

phoenix_harmonium

Phoenix Framework view helpers for Harmonium-style components (https://harmonium.revelry.co)
Elixir
10
star
15

react-uniqueid

Provider component and connect function for generating unique identifiers in React.
JavaScript
8
star
16

storybot-ai

AI Generated User Stories from your CLI
JavaScript
6
star
17

aws_parameter_store_config_provider

Elixir
5
star
18

ecto_sort

Elixir
5
star
19

transmit

Plug for handling the creation of presigned urls for direct client-side uploading
Elixir
4
star
20

zoom-archive-scraper

Some example elixir code for scraping Zoom's Recording Management section for old recordings
Elixir
4
star
21

exdr

Elixir
4
star
22

bn-mobile-react

BigNeon React Native Repo
JavaScript
2
star
23

velocity_calculator

Elixir
2
star
24

tenk

An unofficial Ruby API wrapper for working with the 10k Plans (http://www.10000ft.com/).
Ruby
2
star
25

moondog-engine

An opinionated bootstrapper for Goodβ„’ Kubernetes clusters.
Shell
2
star
26

metairie

Set of modules for monitoring Elixir/Phoenix apps and sending them to a statsD compatible client
Elixir
2
star
27

Harmonium-RN

Reactive Native Harmonium Styles
JavaScript
1
star
28

react-native-duration-picker

A duration picker component for React Native
JavaScript
1
star
29

revelry-koans

Elixir
1
star
30

journal

Versioned key/value store with multiple backend support
Elixir
1
star
31

ex_selfie

Provides a single way to access both a struct's fields and its associated module's functions.
Elixir
1
star
32

slush-revelry

A generator for Revelry Node.js apps that use React, Redux, and more.
JavaScript
1
star
33

helm-charts

Our public helm charts
Smarty
1
star
34

review_bot_operator

Elixir
1
star
35

tracing

Contact Tracing Prototype
1
star