Skip to content

Commit 87b474f

Browse files
SiarheiFedartsoumattwigway
authored andcommitted
Use node-api instead of NAN (Project-OSRM#6452)
1 parent 60d6673 commit 87b474f

File tree

13 files changed

+789
-837
lines changed

13 files changed

+789
-837
lines changed

.github/workflows/osrm-backend.yml

Lines changed: 37 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ env:
2020
ENABLE_NODE_BINDINGS: "ON"
2121

2222
jobs:
23-
windows:
23+
windows-release-node:
2424
needs: format-taginfo-docs
2525
runs-on: windows-2022
2626
continue-on-error: false
@@ -264,83 +264,7 @@ jobs:
264264
CXXCOMPILER: g++-8
265265
CXXFLAGS: -Wno-cast-function-type
266266

267-
- name: conan-macos-x64-release-node-16
268-
build_node_package: true
269-
continue-on-error: false
270-
node: 16
271-
runs-on: macos-11
272-
BUILD_TOOLS: ON
273-
BUILD_TYPE: Release
274-
CCOMPILER: clang
275-
CXXCOMPILER: clang++
276-
CUCUMBER_TIMEOUT: 60000
277-
ENABLE_ASSERTIONS: ON
278-
ENABLE_CONAN: ON
279-
280-
- name: conan-macos-arm64-release-node-16
281-
build_node_package: true
282-
continue-on-error: false
283-
node: 16
284-
runs-on: macos-11
285-
BUILD_TOOLS: ON
286-
BUILD_TYPE: Release
287-
CCOMPILER: clang
288-
CXXCOMPILER: clang++
289-
CUCUMBER_TIMEOUT: 60000
290-
ENABLE_ASSERTIONS: ON
291-
ENABLE_CONAN: ON
292-
ENABLE_APPLE_SILICON: ON
293-
294-
- name: conan-macos-x64-release-node-18
295-
build_node_package: true
296-
continue-on-error: false
297-
node: 18
298-
runs-on: macos-11
299-
BUILD_TOOLS: ON
300-
BUILD_TYPE: Release
301-
CCOMPILER: clang
302-
CXXCOMPILER: clang++
303-
CUCUMBER_TIMEOUT: 60000
304-
ENABLE_ASSERTIONS: ON
305-
ENABLE_CONAN: ON
306-
307-
- name: conan-macos-arm64-release-node-18
308-
build_node_package: true
309-
continue-on-error: false
310-
node: 18
311-
runs-on: macos-11
312-
BUILD_TOOLS: ON
313-
BUILD_TYPE: Release
314-
CCOMPILER: clang
315-
CXXCOMPILER: clang++
316-
CUCUMBER_TIMEOUT: 60000
317-
ENABLE_ASSERTIONS: ON
318-
ENABLE_CONAN: ON
319-
ENABLE_APPLE_SILICON: ON
320-
321-
- name: node-16-conan-linux-release
322-
build_node_package: true
323-
continue-on-error: false
324-
node: 16
325-
runs-on: ubuntu-20.04
326-
BUILD_TYPE: Release
327-
CCOMPILER: clang-6.0
328-
CXXCOMPILER: clang++-6.0
329-
ENABLE_CONAN: ON
330-
NODE_PACKAGE_TESTS_ONLY: ON
331-
332-
- name: node-16-conan-linux-debug
333-
build_node_package: true
334-
continue-on-error: false
335-
node: 16
336-
runs-on: ubuntu-20.04
337-
BUILD_TYPE: Debug
338-
CCOMPILER: clang-6.0
339-
CXXCOMPILER: clang++-6.0
340-
ENABLE_CONAN: ON
341-
NODE_PACKAGE_TESTS_ONLY: ON
342-
343-
- name: node-18-conan-linux-release
267+
- name: conan-linux-release-node
344268
build_node_package: true
345269
continue-on-error: false
346270
node: 18
@@ -351,7 +275,7 @@ jobs:
351275
ENABLE_CONAN: ON
352276
NODE_PACKAGE_TESTS_ONLY: ON
353277

354-
- name: node-18-conan-linux-debug
278+
- name: conan-linux-debug-node
355279
build_node_package: true
356280
continue-on-error: false
357281
node: 18
@@ -362,10 +286,10 @@ jobs:
362286
ENABLE_CONAN: ON
363287
NODE_PACKAGE_TESTS_ONLY: ON
364288

365-
- name: conan-macos-x64-release-node-latest
289+
- name: conan-macos-x64-release-node
366290
build_node_package: true
367291
continue-on-error: true
368-
node: latest
292+
node: 18
369293
runs-on: macos-11
370294
BUILD_TYPE: Release
371295
CCOMPILER: clang
@@ -374,10 +298,10 @@ jobs:
374298
ENABLE_ASSERTIONS: ON
375299
ENABLE_CONAN: ON
376300

377-
- name: conan-macos-arm64-release-node-latest
301+
- name: conan-macos-arm64-release-node
378302
build_node_package: true
379303
continue-on-error: true
380-
node: latest
304+
node: 18
381305
runs-on: macos-11
382306
BUILD_TYPE: Release
383307
CCOMPILER: clang
@@ -387,28 +311,6 @@ jobs:
387311
ENABLE_CONAN: ON
388312
ENABLE_APPLE_SILICON: ON
389313

390-
- name: node-latest-conan-linux-release
391-
build_node_package: true
392-
continue-on-error: true
393-
node: latest
394-
runs-on: ubuntu-20.04
395-
BUILD_TYPE: Release
396-
CCOMPILER: clang-6.0
397-
CXXCOMPILER: clang++-6.0
398-
ENABLE_CONAN: ON
399-
NODE_PACKAGE_TESTS_ONLY: ON
400-
401-
- name: node-latest-conan-linux-debug
402-
build_node_package: true
403-
continue-on-error: true
404-
node: latest
405-
runs-on: ubuntu-20.04
406-
BUILD_TYPE: Debug
407-
CCOMPILER: clang-6.0
408-
CXXCOMPILER: clang++-6.0
409-
ENABLE_CONAN: ON
410-
NODE_PACKAGE_TESTS_ONLY: ON
411-
412314
name: ${{ matrix.name}}
413315
continue-on-error: ${{ matrix.continue-on-error }}
414316
runs-on: ${{ matrix.runs-on }}
@@ -627,10 +529,38 @@ jobs:
627529
./src/benchmarks/packedvector-bench
628530
./src/benchmarks/rtree-bench ../test/data/monaco.osrm.ramIndex ../test/data/monaco.osrm.fileIndex ../test/data/monaco.osrm.nbg_nodes
629531
popd
630-
- name: Run Node package tests only
532+
533+
- name: Use Node 16
534+
if: ${{ matrix.NODE_PACKAGE_TESTS_ONLY == 'ON' && matrix.ENABLE_APPLE_SILICON != 'ON' }}
535+
uses: actions/setup-node@v3
536+
with:
537+
node-version: 16
538+
- name: Run Node package tests on Node 16
631539
if: ${{ matrix.NODE_PACKAGE_TESTS_ONLY == 'ON' && matrix.ENABLE_APPLE_SILICON != 'ON' }}
632540
run: |
541+
node --version
633542
npm run nodejs-tests
543+
- name: Use Node 18
544+
if: ${{ matrix.NODE_PACKAGE_TESTS_ONLY == 'ON' && matrix.ENABLE_APPLE_SILICON != 'ON' }}
545+
uses: actions/setup-node@v3
546+
with:
547+
node-version: 18
548+
- name: Run Node package tests on Node 18
549+
if: ${{ matrix.NODE_PACKAGE_TESTS_ONLY == 'ON' && matrix.ENABLE_APPLE_SILICON != 'ON' }}
550+
run: |
551+
node --version
552+
npm run nodejs-tests
553+
- name: Use Node latest
554+
if: ${{ matrix.NODE_PACKAGE_TESTS_ONLY == 'ON' && matrix.ENABLE_APPLE_SILICON != 'ON' }}
555+
uses: actions/setup-node@v3
556+
with:
557+
node-version: latest
558+
- name: Run Node package tests on Node-latest
559+
if: ${{ matrix.NODE_PACKAGE_TESTS_ONLY == 'ON' && matrix.ENABLE_APPLE_SILICON != 'ON' }}
560+
run: |
561+
node --version
562+
npm run nodejs-tests
563+
634564
- name: Upload test logs
635565
uses: actions/upload-artifact@v3
636566
if: failure()
@@ -681,6 +611,6 @@ jobs:
681611

682612
ci-complete:
683613
runs-on: ubuntu-22.04
684-
needs: [build-test-publish, docker-image, windows]
614+
needs: [build-test-publish, docker-image, windows-release-node]
685615
steps:
686616
- run: echo "CI complete"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,4 @@ debug.lua
105105

106106
# node-osrm artifacts
107107
lib/binding
108+

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
- CHANGED: Upgrade to clang-tidy 15. [#6439](https://github.com/Project-OSRM/osrm-backend/pull/6439)
55
- CHANGED: Update actions/cache to v3. [#6420](https://github.com/Project-OSRM/osrm-backend/pull/6420)
66
- REMOVED: Drop support of Node 12 & 14. [#6431](https://github.com/Project-OSRM/osrm-backend/pull/6431)
7+
- NodeJS:
8+
- CHANGED: Use node-api instead of NAN. [#6452](https://github.com/Project-OSRM/osrm-backend/pull/6452)
79
- Misc:
810
- REMOVED: Get rid of unused functions in util/json_util.hpp. [#6446](https://github.com/Project-OSRM/osrm-backend/pull/6446)
911
- FIXED: Apply workaround for Conan installation issue on CI. [#6442](https://github.com/Project-OSRM/osrm-backend/pull/6442)

include/nodejs/json_v8_renderer.hpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
#define OSRM_BINDINGS_NODE_JSON_V8_RENDERER_HPP
33

44
#include "osrm/json_container.hpp"
5-
6-
#pragma GCC diagnostic push
7-
#pragma GCC diagnostic ignored "-Wunused-parameter"
8-
#include <nan.h>
9-
#pragma GCC diagnostic pop
5+
#include <napi.h>
106

117
#include <functional>
128

@@ -15,52 +11,56 @@ namespace node_osrm
1511

1612
struct V8Renderer
1713
{
18-
explicit V8Renderer(v8::Local<v8::Value> &_out) : out(_out) {}
14+
explicit V8Renderer(const Napi::Env &env, Napi::Value &out) : env(env), out(out) {}
1915

2016
void operator()(const osrm::json::String &string) const
2117
{
22-
out = Nan::New(std::cref(string.value)).ToLocalChecked();
18+
out = Napi::String::New(env, string.value);
2319
}
2420

25-
void operator()(const osrm::json::Number &number) const { out = Nan::New(number.value); }
21+
void operator()(const osrm::json::Number &number) const
22+
{
23+
out = Napi::Number::New(env, number.value);
24+
}
2625

2726
void operator()(const osrm::json::Object &object) const
2827
{
29-
v8::Local<v8::Object> obj = Nan::New<v8::Object>();
28+
Napi::Object obj = Napi::Object::New(env);
3029
for (const auto &keyValue : object.values)
3130
{
32-
v8::Local<v8::Value> child;
33-
mapbox::util::apply_visitor(V8Renderer(child), keyValue.second);
34-
Nan::Set(obj, Nan::New(keyValue.first).ToLocalChecked(), child);
31+
Napi::Value child;
32+
mapbox::util::apply_visitor(V8Renderer(env, child), keyValue.second);
33+
obj.Set(keyValue.first, child);
3534
}
3635
out = obj;
3736
}
3837

3938
void operator()(const osrm::json::Array &array) const
4039
{
41-
v8::Local<v8::Array> a = Nan::New<v8::Array>(array.values.size());
40+
Napi::Array a = Napi::Array::New(env, array.values.size());
4241
for (auto i = 0u; i < array.values.size(); ++i)
4342
{
44-
v8::Local<v8::Value> child;
45-
mapbox::util::apply_visitor(V8Renderer(child), array.values[i]);
46-
Nan::Set(a, i, child);
43+
Napi::Value child;
44+
mapbox::util::apply_visitor(V8Renderer(env, child), array.values[i]);
45+
a.Set(i, child);
4746
}
4847
out = a;
4948
}
5049

51-
void operator()(const osrm::json::True &) const { out = Nan::New(true); }
50+
void operator()(const osrm::json::True &) const { out = Napi::Boolean::New(env, true); }
5251

53-
void operator()(const osrm::json::False &) const { out = Nan::New(false); }
52+
void operator()(const osrm::json::False &) const { out = Napi::Boolean::New(env, false); }
5453

55-
void operator()(const osrm::json::Null &) const { out = Nan::Null(); }
54+
void operator()(const osrm::json::Null &) const { out = env.Null(); }
5655

5756
private:
58-
v8::Local<v8::Value> &out;
57+
const Napi::Env &env;
58+
Napi::Value &out;
5959
};
6060

61-
inline void renderToV8(v8::Local<v8::Value> &out, const osrm::json::Object &object)
61+
inline void renderToV8(const Napi::Env &env, Napi::Value &out, const osrm::json::Object &object)
6262
{
63-
V8Renderer renderer(out);
63+
V8Renderer renderer(env, out);
6464
renderer(object);
6565
}
6666
} // namespace node_osrm

include/nodejs/node_osrm.hpp

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,30 @@
33

44
#include "osrm/osrm_fwd.hpp"
55

6-
#pragma GCC diagnostic push
7-
#pragma GCC diagnostic ignored "-Wunused-parameter"
8-
#include <nan.h>
9-
#pragma GCC diagnostic pop
6+
#include <napi.h>
107

118
#include <memory>
129

1310
namespace node_osrm
1411
{
1512

16-
struct Engine final : public Nan::ObjectWrap
13+
class Engine final : public Napi::ObjectWrap<Engine>
1714
{
18-
using Base = Nan::ObjectWrap;
15+
public:
16+
static Napi::Object Init(Napi::Env env, Napi::Object exports);
17+
Engine(const Napi::CallbackInfo &info);
1918

20-
static NAN_MODULE_INIT(Init);
21-
22-
static NAN_METHOD(New);
23-
24-
static NAN_METHOD(route);
25-
static NAN_METHOD(nearest);
26-
static NAN_METHOD(table);
27-
static NAN_METHOD(tile);
28-
static NAN_METHOD(match);
29-
static NAN_METHOD(trip);
30-
31-
Engine(osrm::EngineConfig &config);
32-
33-
// Thread-safe singleton accessor
34-
static Nan::Persistent<v8::Function> &constructor();
35-
36-
// Ref-counted OSRM alive even after shutdown until last callback is done
3719
std::shared_ptr<osrm::OSRM> this_;
20+
21+
private:
22+
Napi::Value route(const Napi::CallbackInfo &info);
23+
Napi::Value nearest(const Napi::CallbackInfo &info);
24+
Napi::Value table(const Napi::CallbackInfo &info);
25+
Napi::Value tile(const Napi::CallbackInfo &info);
26+
Napi::Value match(const Napi::CallbackInfo &info);
27+
Napi::Value trip(const Napi::CallbackInfo &info);
3828
};
3929

4030
} // namespace node_osrm
4131

42-
#pragma GCC diagnostic push
43-
#pragma GCC diagnostic ignored "-Wunused-parameter"
44-
NAN_MODULE_WORKER_ENABLED(osrm, node_osrm::Engine::Init)
45-
#pragma GCC diagnostic pop
46-
4732
#endif

0 commit comments

Comments
 (0)