Skip to content

Commit bd1391f

Browse files
daniel-j-hTheMarex
authored andcommitted
Exposes EngineConfig system-wide limits in Node.js bindings, resolves #4226
1 parent f546316 commit bd1391f

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

include/nodejs/node_osrm_support.hpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,64 @@ inline engine_config_ptr argumentsToEngineConfig(const Nan::FunctionCallbackInfo
176176
return engine_config_ptr();
177177
}
178178

179+
// Set EngineConfig system-wide limits on construction, if requested
180+
181+
auto max_locations_trip = params->Get(Nan::New("max_locations_trip").ToLocalChecked());
182+
auto max_locations_viaroute = params->Get(Nan::New("max_locations_viaroute").ToLocalChecked());
183+
auto max_locations_distance_table =
184+
params->Get(Nan::New("max_locations_distance_table").ToLocalChecked());
185+
auto max_locations_map_matching =
186+
params->Get(Nan::New("max_locations_map_matching").ToLocalChecked());
187+
auto max_results_nearest = params->Get(Nan::New("max_results_nearest").ToLocalChecked());
188+
auto max_alternatives = params->Get(Nan::New("max_alternatives").ToLocalChecked());
189+
190+
if (!max_locations_trip->IsUndefined() && !max_locations_trip->IsNumber())
191+
{
192+
Nan::ThrowError("max_locations_trip must be an integral number");
193+
return engine_config_ptr();
194+
}
195+
if (!max_locations_viaroute->IsUndefined() && !max_locations_viaroute->IsNumber())
196+
{
197+
Nan::ThrowError("max_locations_viaroute must be an integral number");
198+
return engine_config_ptr();
199+
}
200+
if (!max_locations_distance_table->IsUndefined() && !max_locations_distance_table->IsNumber())
201+
{
202+
Nan::ThrowError("max_locations_distance_table must be an integral number");
203+
return engine_config_ptr();
204+
}
205+
if (!max_locations_map_matching->IsUndefined() && !max_locations_map_matching->IsNumber())
206+
{
207+
Nan::ThrowError("max_locations_map_matching must be an integral number");
208+
return engine_config_ptr();
209+
}
210+
if (!max_results_nearest->IsUndefined() && !max_results_nearest->IsNumber())
211+
{
212+
Nan::ThrowError("max_results_nearest must be an integral number");
213+
return engine_config_ptr();
214+
}
215+
if (!max_alternatives->IsUndefined() && !max_alternatives->IsNumber())
216+
{
217+
Nan::ThrowError("max_alternatives must be an integral number");
218+
return engine_config_ptr();
219+
}
220+
221+
if (max_locations_trip->IsNumber())
222+
engine_config->max_locations_trip = static_cast<int>(max_locations_trip->NumberValue());
223+
if (max_locations_viaroute->IsNumber())
224+
engine_config->max_locations_viaroute =
225+
static_cast<int>(max_locations_viaroute->NumberValue());
226+
if (max_locations_distance_table->IsNumber())
227+
engine_config->max_locations_distance_table =
228+
static_cast<int>(max_locations_distance_table->NumberValue());
229+
if (max_locations_map_matching->IsNumber())
230+
engine_config->max_locations_map_matching =
231+
static_cast<int>(max_locations_map_matching->NumberValue());
232+
if (max_results_nearest->IsNumber())
233+
engine_config->max_results_nearest = static_cast<int>(max_results_nearest->NumberValue());
234+
if (max_alternatives->IsNumber())
235+
engine_config->max_alternatives = static_cast<int>(max_alternatives->NumberValue());
236+
179237
return engine_config;
180238
}
181239

test/nodejs/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,37 @@ test('constructor: throws if data doesn\'t match algorithm', function(assert) {
9898
assert.throws(function() { new OSRM({algorithm: 'MLD', path: monaco_path}); });
9999
});
100100

101+
test('constructor: parses custom limits', function(assert) {
102+
assert.plan(1);
103+
var osrm = new OSRM({
104+
path: monaco_mld_path,
105+
algorithm: 'MLD',
106+
max_locations_trip: 1,
107+
max_locations_viaroute: 1,
108+
max_locations_distance_table: 1,
109+
max_locations_map_matching: 1,
110+
max_results_nearest: 1,
111+
max_alternatives: 1,
112+
});
113+
assert.ok(osrm);
114+
});
115+
116+
test('constructor: throws on invalid custom limits', function(assert) {
117+
assert.plan(1);
118+
assert.throws(function() {
119+
var osrm = new OSRM({
120+
path: monaco_mld_path,
121+
algorithm: 'MLD',
122+
max_locations_trip: 'unlimited',
123+
max_locations_viaroute: true,
124+
max_locations_distance_table: false,
125+
max_locations_map_matching: 'a lot',
126+
max_results_nearest: null,
127+
max_alternatives: '10'
128+
})
129+
});
130+
});
131+
101132
require('./route.js');
102133
require('./trip.js');
103134
require('./match.js');

test/nodejs/route.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,3 +549,30 @@ test('route: throws on bad approaches', function(assert) {
549549
}, function(err, route) {}) },
550550
/Approach must be a string: \[curb, unrestricted\] or null/);
551551
});
552+
553+
test('route: routes Monaco with custom limits on MLD', function(assert) {
554+
assert.plan(2);
555+
var osrm = new OSRM({
556+
path: monaco_mld_path,
557+
algorithm: 'MLD',
558+
max_alternatives: 10,
559+
});
560+
osrm.route({coordinates: two_test_coordinates, alternatives: 10}, function(err, route) {
561+
assert.ifError(err);
562+
assert.ok(Array.isArray(route.routes));
563+
});
564+
});
565+
566+
test('route: in Monaco with custom limits on MLD', function(assert) {
567+
assert.plan(1);
568+
var osrm = new OSRM({
569+
path: monaco_mld_path,
570+
algorithm: 'MLD',
571+
max_alternatives: 10,
572+
});
573+
osrm.route({coordinates: two_test_coordinates, alternatives: 11}, function(err, route) {
574+
console.log(err)
575+
assert.equal(err.message, 'TooBig');
576+
});
577+
});
578+

0 commit comments

Comments
 (0)