Skip to content

Commit ba29a24

Browse files
committed
Merges node-osrm into repo.
1 parent 07221f5 commit ba29a24

File tree

23 files changed

+3094
-6
lines changed

23 files changed

+3094
-6
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/*

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

@@ -733,6 +734,11 @@ add_custom_target(uninstall
733734
add_subdirectory(unit_tests)
734735
add_subdirectory(src/benchmarks)
735736

737+
if (ENABLE_NODE_BINDINGS)
738+
add_subdirectory(src/bindings/node)
739+
endif()
740+
741+
736742
if (ENABLE_FUZZING)
737743
# Requires libosrm being built with sanitizers; make configurable and default to ubsan
738744
set(FUZZ_SANITIZER "undefined" CACHE STRING "Sanitizer to be used for Fuzz testing")

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);
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/bindings/node/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)