Skip to content

Commit

Permalink
Merges node-osrm into repository
Browse files Browse the repository at this point in the history
Build with

    cmake .. -DCMAKE_BUILD_TYPE=Release -DENABLE_NODE_BINDINGS=On -DENABLE_MASON=On
  • Loading branch information
daniel-j-h committed Mar 6, 2017
1 parent 07221f5 commit b2ac9dc
Show file tree
Hide file tree
Showing 27 changed files with 3,518 additions and 9 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Thumbs.db
/build/
/example/build/
/test/data/monaco*
/test/bindings/node/data/berlin*
/cmake/postinst

# Eclipse related files #
Expand Down Expand Up @@ -96,4 +97,7 @@ node_modules
*.swp

# local lua debugging file
debug.lua
debug.lua

# node-osrm artifacts
lib/binding
10 changes: 10 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*
!README.md
!CHANGELOG.md
!CONTRIBUTING.MD
!LICENCE.TXT
!package.json
!example
!lib/*.js
!profiles/*
!profiles/lib/*
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ matrix:
osx_image: xcode8.2
compiler: "mason-osx-release"
# we use the xcode provides clang and don't install our own
env: ENABLE_MASON=ON BUILD_TYPE='Release' CUCUMBER_TIMEOUT=60000 CCOMPILER='clang' CXXCOMPILER='clang++' ENABLE_ASSERTIONS=ON
env: ENABLE_MASON=ON BUILD_TYPE='Release' CUCUMBER_TIMEOUT=60000 CCOMPILER='clang' CXXCOMPILER='clang++' ENABLE_ASSERTIONS=ON ENABLE_NODE_BINDINGS=ON

# Release Builds
- os: linux
Expand All @@ -65,7 +65,7 @@ matrix:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['libstdc++-5-dev']
env: CLANG_VERSION='3.9.1' BUILD_TYPE='Release' ENABLE_MASON=ON RUN_CLANG_FORMAT=ON
env: CLANG_VERSION='3.9.1' BUILD_TYPE='Release' ENABLE_MASON=ON RUN_CLANG_FORMAT=ON ENABLE_NODE_BINDINGS=ON

- os: linux
compiler: "gcc-6-release"
Expand Down Expand Up @@ -164,7 +164,7 @@ install:
- export OSRM_BUILD_DIR="$(pwd)/build-osrm"
- mkdir ${OSRM_BUILD_DIR} && pushd ${OSRM_BUILD_DIR}
- export CC=${CCOMPILER} CXX=${CXXCOMPILER}
- cmake .. -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DENABLE_MASON=${ENABLE_MASON:-OFF} -DENABLE_ASSERTIONS=${ENABLE_ASSERTIONS:-OFF} -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS:-OFF} -DENABLE_COVERAGE=${ENABLE_COVERAGE:-OFF} -DENABLE_SANITIZER=${ENABLE_SANITIZER:-OFF} -DBUILD_TOOLS=ON -DBUILD_COMPONENTS=${BUILD_COMPONENTS:-OFF} -DENABLE_CCACHE=ON
- cmake .. -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DENABLE_MASON=${ENABLE_MASON:-OFF} -DENABLE_ASSERTIONS=${ENABLE_ASSERTIONS:-OFF} -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS:-OFF} -DENABLE_COVERAGE=${ENABLE_COVERAGE:-OFF} -DENABLE_SANITIZER=${ENABLE_SANITIZER:-OFF} -DBUILD_TOOLS=ON -DBUILD_COMPONENTS=${BUILD_COMPONENTS:-OFF} -DENABLE_CCACHE=ON -DENABLE_NODE_BINDINGS=${ENABLE_NODE_BINDINGS}
- echo "travis_fold:start:MAKE"
- make --jobs=${JOBS}
- make tests --jobs=${JOBS}
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
- Changes from 5.6
- Internals
- Shared memory notification via conditional variables on Linux or semaphore queue on OS X and Windows with a limit of 128 OSRM Engine instances
- NodeJs Bindings
- Merged https://github.com/Project-OSRM/node-osrm into repository. Build via `cmake .. -DCMAKE_BUILD_TYPE=Release -DENABLE_NODE_BINDINGS=On -DENABLE_MASON=On`.

# 5.6.0
- Changes from 5.5
Expand Down
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ option(ENABLE_SANITIZER "Use memory sanitizer for Debug build" OFF)
option(ENABLE_LTO "Use LTO if available" OFF)
option(ENABLE_FUZZING "Fuzz testing using LLVM's libFuzzer" OFF)
option(ENABLE_GOLD_LINKER "Use GNU gold linker if available" ON)
option(ENABLE_NODE_BINDINGS "Build NodeJs bindings" OFF)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

Expand Down Expand Up @@ -733,6 +734,11 @@ add_custom_target(uninstall
add_subdirectory(unit_tests)
add_subdirectory(src/benchmarks)

if (ENABLE_NODE_BINDINGS)
add_subdirectory(src/nodejs)
endif()


if (ENABLE_FUZZING)
# Requires libosrm being built with sanitizers; make configurable and default to ubsan
set(FUZZ_SANITIZER "undefined" CACHE STRING "Sanitizer to be used for Fuzz testing")
Expand Down
333 changes: 333 additions & 0 deletions docs/bindings/node/api.md

Large diffs are not rendered by default.

86 changes: 86 additions & 0 deletions docs/bindings/node/releasing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Releasing

Releasing a new version of `node-osrm` is mostly automated using Travis CI.

The version of `node-osrm` is locked to the same version as `osrm-backend`. Every `node-osrm` should have a `osrm-backend` release of the same version. Of course, only release a `node-osrm` after the release has been tagged in `osrm-backend`.

These steps all happen on `master`. After the release is out, create a branch using the MAJOR.MINOR version of the release to document code changes made for that version.

### Steps to release

1. Update the `osrm_release` field in `package.json` to the corresonding git tag in `osrm-backend.`

Confirm the desired OSRM branch and commit to `master`.

1. Bump node-osrm version

Update the `CHANGELOG.md` and the `package.json` version if needed.

1. Check that Travis CI [builds are passing](https://travis-ci.org/Project-OSRM/node-osrm) for the latest commit on `master`.

1. Publishing binaries

If travis builds are passing then it's time to publish binaries by committing with a message containing `[publish binary]`. Use an empty commit for this.

```
git commit --allow-empty -m "[publish binary] vMAJOR.MINOR.PATCH"
```

1. Test

Locally you can now test binaries. Cleanup, re-install, and run the tests like:

```
make clean
npm install # will pull remote binaries
npm ls # confirm deps are correct
make test
```

1. Tag

Once binaries are published for Linux and OS X then its time to tag a new release and add the changelog to the tag:

```
git tag vMAJOR.MINOR.PATCH -a
git push --tags
```

1. Publish node-osrm. **we only do this for stable releases**

First ensure your local `node-pre-gyp` is up to date:

```
npm ls
```

This is important because it is bundled during packaging.

If you see any errors then do:

```
rm -rf node_modules/node-pre-gyp
npm install node-pre-gyp
```

Now we're ready to publish `node-osrm` to <https://www.npmjs.org/package/osrm>:

```
npm publish
```

Dependent apps can now pull from the npm registry like:

```
"dependencies": {
"osrm": "^MAJOR.MINOR.PATCH"
}
```

Or can still pull from the github tag like:

```
"dependencies": {
"osrm": "https://github.com/Project-OSRM/node-osrm/archive/vMAJOR.MINOR.PATCH.tar.gz"
}
```
32 changes: 32 additions & 0 deletions example/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
process.env.UV_THREADPOOL_SIZE = Math.ceil(require('os').cpus().length * 1.5);

var express = require('express');
var OSRM = require('..');
var path = require('path');

var app = express();
var osrm = new OSRM(path.join(__dirname,"../test/data/monaco.osrm"));

// Accepts a query like:
// http://localhost:8888?start=13.438640,52.519930&end=13.415852,52.513191
app.get('/', function(req, res) {
if (!req.query.start || !req.query.end) {
return res.json({"error":"invalid start and end query"});
}
var coordinates = [];
var start = req.query.start.split(',');
coordinates.push([+start[0],+start[1]]);
var end = req.query.end.split(',');
coordinates.push([+end[0],+end[1]]);
var query = {
coordinates: coordinates,
alternateRoute: req.query.alternatives !== 'false'
};
osrm.route(query, function(err, result) {
if (err) return res.json({"error":err.message});
return res.json(result);
});
});

console.log('Listening on port: ' + 8888);
app.listen(8888);
65 changes: 65 additions & 0 deletions include/nodejs/json_v8_renderer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#ifndef OSRM_BINDINGS_NODE_JSON_V8_RENDERER_HPP
#define OSRM_BINDINGS_NODE_JSON_V8_RENDERER_HPP

#include "osrm/json_container.hpp"

#include <nan.h>

#include <functional>

namespace node_osrm
{

struct V8Renderer
{
explicit V8Renderer(v8::Local<v8::Value> &_out) : out(_out) {}

void operator()(const osrm::json::String &string) const
{
out = Nan::New(std::cref(string.value)).ToLocalChecked();
}

void operator()(const osrm::json::Number &number) const { out = Nan::New(number.value); }

void operator()(const osrm::json::Object &object) const
{
v8::Local<v8::Object> obj = Nan::New<v8::Object>();
for (const auto &keyValue : object.values)
{
v8::Local<v8::Value> child;
mapbox::util::apply_visitor(V8Renderer(child), keyValue.second);
obj->Set(Nan::New(keyValue.first).ToLocalChecked(), child);
}
out = obj;
}

void operator()(const osrm::json::Array &array) const
{
v8::Local<v8::Array> a = Nan::New<v8::Array>(array.values.size());
for (auto i = 0u; i < array.values.size(); ++i)
{
v8::Local<v8::Value> child;
mapbox::util::apply_visitor(V8Renderer(child), array.values[i]);
a->Set(i, child);
}
out = a;
}

void operator()(const osrm::json::True &) const { out = Nan::New(true); }

void operator()(const osrm::json::False &) const { out = Nan::New(false); }

void operator()(const osrm::json::Null &) const { out = Nan::Null(); }

private:
v8::Local<v8::Value> &out;
};

inline void renderToV8(v8::Local<v8::Value> &out, const osrm::json::Object &object)
{
osrm::json::Value value = object;
mapbox::util::apply_visitor(V8Renderer(out), value);
}
}

#endif // JSON_V8_RENDERER_HPP
41 changes: 41 additions & 0 deletions include/nodejs/node_osrm.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef OSRM_BINDINGS_NODE_HPP
#define OSRM_BINDINGS_NODE_HPP

#include "osrm/osrm_fwd.hpp"

#include <nan.h>

#include <memory>

namespace node_osrm
{

struct Engine final : public Nan::ObjectWrap
{
using Base = Nan::ObjectWrap;

static NAN_MODULE_INIT(Init);

static NAN_METHOD(New);

static NAN_METHOD(route);
static NAN_METHOD(nearest);
static NAN_METHOD(table);
static NAN_METHOD(tile);
static NAN_METHOD(match);
static NAN_METHOD(trip);

Engine(osrm::EngineConfig &config);

// Thread-safe singleton accessor
static Nan::Persistent<v8::Function> &constructor();

// Ref-counted OSRM alive even after shutdown until last callback is done
std::shared_ptr<osrm::OSRM> this_;
};

} // ns node_osrm

NODE_MODULE(osrm, node_osrm::Engine::Init)

#endif
Loading

0 comments on commit b2ac9dc

Please sign in to comment.