Swiftpack.co - Package - tris-foundation/node

Node

Embedding Node.jsĀ® in Swift application

Package.swift

.package(url: "https://github.com/tris-foundation/node.git", .branch("master"))

Usage

import Node

let context = JSContext()
try context.evaluate("40 + 2")

try context.createFunction(name: "getResult") {
    return .string("result string")
}
let result = try context.evaluate("getResult()")
assertTrue(result.isString)
assertEqual("\(result)", "result string")

CURRENT LIMITATIONS:

  1. There's only one shared Node.js context (no matter how many JSContext's you create).
    If you need to isolate your js you should use vm.createContext and vm.runInContext

macOS

Install

Using Brew

brew tap tris-brew/macos
brew install libnode

Precompiled binaries

curl -L https://github.com/tris-foundation/node/releases/download/v10.10.0/node-macos.zip | tar -C / -xmf -

Building from source

git clone https://github.com/nodejs/node.git
cd node && git checkout v10.10.0
./configure --shared --without-inspector
make -j8 && make install

SwiftPM arguments

swift build -Xcc -I/usr/local/include/node -Xlinker /usr/local/lib/libnode.64.dylib
swift test -Xcc -I/usr/local/include/node -Xlinker /usr/local/lib/libnode.64.dylib

swift package \
 -Xcc -I/usr/local/include/node \
 -Xlinker -L/usr/local/lib \
 -Xlinker /usr/local/lib/libnode.64.dylib \
 generate-xcodeproj

swift test \
 -Xcc -I/usr/local/include/node \
 -Xlinker /usr/local/lib/libnode.64.dylib \
 --generate-linuxmain

Linux

Using Linuxbrew

brew tap tris-brew/linux
brew install libnode

Precompiled binaries

wget https://github.com/tris-foundation/node/releases/download/v10.10.0/node-linux.tar.gz -O - | tar -C / -xzf -

Building from source

git clone https://github.com/nodejs/node.git
cd node && git checkout v10.10.0 && rm -rf .git # fix for zsh
./configure --shared --without-inspector
make -j8 && make install

SwiftPM arguments

# Fix 'error while loading shared libraries: libnode.so.64'
# https://www.gnu.org/software/gsl/manual/html_node/Shared-Libraries.html
export LD_LIBRARY_PATH=/usr/local/lib

swift build -Xcc -I/usr/local/include/node -Xlinker /usr/local/lib/libnode.so.64
swift test -Xcc -I/usr/local/include/node -Xlinker /usr/local/lib/libnode.so.64

Github

link
Stars: 1
Help us keep the lights on

Releases

v10.10.0 - Sep 13, 2018

Precompiled Node.js v10.10.0 shared library

brew - May 17, 2018