Skip to content

Commit 09a7273

Browse files
committed
Merges node-osrm into repository
Build with cmake .. -DCMAKE_BUILD_TYPE=Release -DENABLE_NODE_BINDINGS=On -DENABLE_MASON=On
1 parent 0770d8b commit 09a7273

27 files changed

+3518
-9
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Thumbs.db
4747
/build/
4848
/example/build/
4949
/test/data/monaco*
50+
/test/bindings/node/data/berlin*
5051
/cmake/postinst
5152

5253
# Eclipse related files #
@@ -96,4 +97,7 @@ node_modules
9697
*.swp
9798

9899
# local lua debugging file
99-
debug.lua
100+
debug.lua
101+
102+
# node-osrm artifacts
103+
lib/binding

.npmignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*
2+
!README.md
3+
!CHANGELOG.md
4+
!CONTRIBUTING.MD
5+
!LICENCE.TXT
6+
!package.json
7+
!example
8+
!lib/*.js
9+
!profiles/*
10+
!profiles/lib/*

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ matrix:
5858
apt:
5959
sources: ['ubuntu-toolchain-r-test']
6060
packages: ['libstdc++-5-dev']
61-
env: CLANG_VERSION='3.9.1' BUILD_TYPE='Release' ENABLE_MASON=ON ENABLE_SANITIZER=ON
61+
env: CLANG_VERSION='3.9.1' BUILD_TYPE='Release' ENABLE_MASON=ON ENABLE_SANITIZER=ON ENABLE_NODE_BINDINGS=ON
6262

6363
# Release Builds
6464
- os: linux
@@ -67,7 +67,7 @@ matrix:
6767
apt:
6868
sources: ['ubuntu-toolchain-r-test']
6969
packages: ['libstdc++-5-dev']
70-
env: CLANG_VERSION='3.9.1' BUILD_TYPE='Release' ENABLE_MASON=ON RUN_CLANG_FORMAT=ON
70+
env: CLANG_VERSION='3.9.1' BUILD_TYPE='Release' ENABLE_MASON=ON RUN_CLANG_FORMAT=ON ENABLE_NODE_BINDINGS=ON
7171

7272
- os: linux
7373
compiler: "gcc-6-release"
@@ -173,7 +173,7 @@ install:
173173
- export OSRM_BUILD_DIR="$(pwd)/build-osrm"
174174
- mkdir ${OSRM_BUILD_DIR} && pushd ${OSRM_BUILD_DIR}
175175
- export CC=${CCOMPILER} CXX=${CXXCOMPILER}
176-
- 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
176+
- 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}
177177
- echo "travis_fold:start:MAKE"
178178
- make --jobs=${JOBS}
179179
- make tests --jobs=${JOBS}

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
- Changes from 5.6
33
- Internals
44
- Shared memory notification via conditional variables on Linux or semaphore queue on OS X and Windows with a limit of 128 OSRM Engine instances
5+
- NodeJs Bindings
6+
- Merged https://github.com/Project-OSRM/node-osrm into repository. Build via `cmake .. -DCMAKE_BUILD_TYPE=Release -DENABLE_NODE_BINDINGS=On -DENABLE_MASON=On`.
57

68
# 5.6.2
79
- Changes from 5.6.0

CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ option(ENABLE_SANITIZER "Use memory sanitizer for Debug build" OFF)
2424
option(ENABLE_LTO "Use LTO if available" OFF)
2525
option(ENABLE_FUZZING "Fuzz testing using LLVM's libFuzzer" OFF)
2626
option(ENABLE_GOLD_LINKER "Use GNU gold linker if available" ON)
27+
option(ENABLE_NODE_BINDINGS "Build NodeJs bindings" OFF)
2728

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

@@ -766,6 +767,11 @@ add_custom_target(uninstall
766767
add_subdirectory(unit_tests)
767768
add_subdirectory(src/benchmarks)
768769

770+
if (ENABLE_NODE_BINDINGS)
771+
add_subdirectory(src/nodejs)
772+
endif()
773+
774+
769775
if (ENABLE_FUZZING)
770776
# Requires libosrm being built with sanitizers; make configurable and default to ubsan
771777
set(FUZZ_SANITIZER "undefined" CACHE STRING "Sanitizer to be used for Fuzz testing")

docs/bindings/node/api.md

Lines changed: 333 additions & 0 deletions
Large diffs are not rendered by default.

docs/bindings/node/releasing.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Releasing
2+
3+
Releasing a new version of `node-osrm` is mostly automated using Travis CI.
4+
5+
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`.
6+
7+
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.
8+
9+
### Steps to release
10+
11+
1. Update the `osrm_release` field in `package.json` to the corresonding git tag in `osrm-backend.`
12+
13+
Confirm the desired OSRM branch and commit to `master`.
14+
15+
1. Bump node-osrm version
16+
17+
Update the `CHANGELOG.md` and the `package.json` version if needed.
18+
19+
1. Check that Travis CI [builds are passing](https://travis-ci.org/Project-OSRM/node-osrm) for the latest commit on `master`.
20+
21+
1. Publishing binaries
22+
23+
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.
24+
25+
```
26+
git commit --allow-empty -m "[publish binary] vMAJOR.MINOR.PATCH"
27+
```
28+
29+
1. Test
30+
31+
Locally you can now test binaries. Cleanup, re-install, and run the tests like:
32+
33+
```
34+
make clean
35+
npm install # will pull remote binaries
36+
npm ls # confirm deps are correct
37+
make test
38+
```
39+
40+
1. Tag
41+
42+
Once binaries are published for Linux and OS X then its time to tag a new release and add the changelog to the tag:
43+
44+
```
45+
git tag vMAJOR.MINOR.PATCH -a
46+
git push --tags
47+
```
48+
49+
1. Publish node-osrm. **we only do this for stable releases**
50+
51+
First ensure your local `node-pre-gyp` is up to date:
52+
53+
```
54+
npm ls
55+
```
56+
57+
This is important because it is bundled during packaging.
58+
59+
If you see any errors then do:
60+
61+
```
62+
rm -rf node_modules/node-pre-gyp
63+
npm install node-pre-gyp
64+
```
65+
66+
Now we're ready to publish `node-osrm` to <https://www.npmjs.org/package/osrm>:
67+
68+
```
69+
npm publish
70+
```
71+
72+
Dependent apps can now pull from the npm registry like:
73+
74+
```
75+
"dependencies": {
76+
"osrm": "^MAJOR.MINOR.PATCH"
77+
}
78+
```
79+
80+
Or can still pull from the github tag like:
81+
82+
```
83+
"dependencies": {
84+
"osrm": "https://github.com/Project-OSRM/node-osrm/archive/vMAJOR.MINOR.PATCH.tar.gz"
85+
}
86+
```

example/example.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
process.env.UV_THREADPOOL_SIZE = Math.ceil(require('os').cpus().length * 1.5);
2+
3+
var express = require('express');
4+
var OSRM = require('..');
5+
var path = require('path');
6+
7+
var app = express();
8+
var osrm = new OSRM(path.join(__dirname,"../test/data/monaco.osrm"));
9+
10+
// Accepts a query like:
11+
// http://localhost:8888?start=13.438640,52.519930&end=13.415852,52.513191
12+
app.get('/', function(req, res) {
13+
if (!req.query.start || !req.query.end) {
14+
return res.json({"error":"invalid start and end query"});
15+
}
16+
var coordinates = [];
17+
var start = req.query.start.split(',');
18+
coordinates.push([+start[0],+start[1]]);
19+
var end = req.query.end.split(',');
20+
coordinates.push([+end[0],+end[1]]);
21+
var query = {
22+
coordinates: coordinates,
23+
alternateRoute: req.query.alternatives !== 'false'
24+
};
25+
osrm.route(query, function(err, result) {
26+
if (err) return res.json({"error":err.message});
27+
return res.json(result);
28+
});
29+
});
30+
31+
console.log('Listening on port: ' + 8888);
32+
app.listen(8888);

include/nodejs/json_v8_renderer.hpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#ifndef OSRM_BINDINGS_NODE_JSON_V8_RENDERER_HPP
2+
#define OSRM_BINDINGS_NODE_JSON_V8_RENDERER_HPP
3+
4+
#include "osrm/json_container.hpp"
5+
6+
#include <nan.h>
7+
8+
#include <functional>
9+
10+
namespace node_osrm
11+
{
12+
13+
struct V8Renderer
14+
{
15+
explicit V8Renderer(v8::Local<v8::Value> &_out) : out(_out) {}
16+
17+
void operator()(const osrm::json::String &string) const
18+
{
19+
out = Nan::New(std::cref(string.value)).ToLocalChecked();
20+
}
21+
22+
void operator()(const osrm::json::Number &number) const { out = Nan::New(number.value); }
23+
24+
void operator()(const osrm::json::Object &object) const
25+
{
26+
v8::Local<v8::Object> obj = Nan::New<v8::Object>();
27+
for (const auto &keyValue : object.values)
28+
{
29+
v8::Local<v8::Value> child;
30+
mapbox::util::apply_visitor(V8Renderer(child), keyValue.second);
31+
obj->Set(Nan::New(keyValue.first).ToLocalChecked(), child);
32+
}
33+
out = obj;
34+
}
35+
36+
void operator()(const osrm::json::Array &array) const
37+
{
38+
v8::Local<v8::Array> a = Nan::New<v8::Array>(array.values.size());
39+
for (auto i = 0u; i < array.values.size(); ++i)
40+
{
41+
v8::Local<v8::Value> child;
42+
mapbox::util::apply_visitor(V8Renderer(child), array.values[i]);
43+
a->Set(i, child);
44+
}
45+
out = a;
46+
}
47+
48+
void operator()(const osrm::json::True &) const { out = Nan::New(true); }
49+
50+
void operator()(const osrm::json::False &) const { out = Nan::New(false); }
51+
52+
void operator()(const osrm::json::Null &) const { out = Nan::Null(); }
53+
54+
private:
55+
v8::Local<v8::Value> &out;
56+
};
57+
58+
inline void renderToV8(v8::Local<v8::Value> &out, const osrm::json::Object &object)
59+
{
60+
osrm::json::Value value = object;
61+
mapbox::util::apply_visitor(V8Renderer(out), value);
62+
}
63+
}
64+
65+
#endif // JSON_V8_RENDERER_HPP

include/nodejs/node_osrm.hpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef OSRM_BINDINGS_NODE_HPP
2+
#define OSRM_BINDINGS_NODE_HPP
3+
4+
#include "osrm/osrm_fwd.hpp"
5+
6+
#include <nan.h>
7+
8+
#include <memory>
9+
10+
namespace node_osrm
11+
{
12+
13+
struct Engine final : public Nan::ObjectWrap
14+
{
15+
using Base = Nan::ObjectWrap;
16+
17+
static NAN_MODULE_INIT(Init);
18+
19+
static NAN_METHOD(New);
20+
21+
static NAN_METHOD(route);
22+
static NAN_METHOD(nearest);
23+
static NAN_METHOD(table);
24+
static NAN_METHOD(tile);
25+
static NAN_METHOD(match);
26+
static NAN_METHOD(trip);
27+
28+
Engine(osrm::EngineConfig &config);
29+
30+
// Thread-safe singleton accessor
31+
static Nan::Persistent<v8::Function> &constructor();
32+
33+
// Ref-counted OSRM alive even after shutdown until last callback is done
34+
std::shared_ptr<osrm::OSRM> this_;
35+
};
36+
37+
} // ns node_osrm
38+
39+
NODE_MODULE(osrm, node_osrm::Engine::Init)
40+
41+
#endif

0 commit comments

Comments
 (0)