From f03e0cfa12f060fdfd70d7a6fce709a3cfbbd978 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Sat, 10 Jul 2021 16:01:54 -0500 Subject: [PATCH] feat: add node n-api bindings --- dub.sdl | 10 ++++++++++ dub.selections.json | 1 + package.json | 3 +++ pnpm-lock.yaml | 8 ++++++++ src/native/lib.d | 4 ++-- src/native/libc.d | 4 +--- src/node/node.d | 7 +++++++ 7 files changed, 32 insertions(+), 5 deletions(-) create mode 100644 src/node/node.d diff --git a/dub.sdl b/dub.sdl index ae0980f..e49d607 100644 --- a/dub.sdl +++ b/dub.sdl @@ -17,10 +17,20 @@ configuration "executable" { } configuration "library" { + mainSourceFile "./src/native/lib.d" + excludedSourceFiles "./src/native/cli.d" targetType "library" targetName "minijson" } +configuration "node" { + mainSourceFile "./src/node/node.d" + excludedSourceFiles "./src/native/cli.d" "./src/native/libc.d" + dependency "node_dlang" version="0.4.11" + targetType "dynamicLibrary" + targetName "minijson.node" +} + # -------- Build Options and configurations -------- // enables all disp except =nosharedaccess diff --git a/dub.selections.json b/dub.selections.json index 3f9d51f..a73b4c9 100644 --- a/dub.selections.json +++ b/dub.selections.json @@ -2,6 +2,7 @@ "fileVersion": 1, "versions": { "automem": "0.6.6", + "node_dlang": "0.4.11", "test_allocator": "0.3.3", "unit-threaded": "2.0.0" } diff --git a/package.json b/package.json index e1a36da..3c813d3 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,9 @@ "zip": "zip -9 -j ./dist/minijson-windows-x64.zip ./dist/win32-x64/minijson.exe && zip -9 -j ./dist/minijson-macos-x64.zip ./dist/darwin-x64/minijson && zip -9 -j ./dist/minijson-linux-x64.zip ./dist/linux-x64/minijson", "prepublishOnly": "shx rm -rf ./dist/tsconfig.tsbuildinfo ./dist/build.*" }, + "dependencies": { + "node-addon-api": "^4.0.0" + }, "devDependencies": { "@types/jasmine": "^3.7.7", "@types/node": "16.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a4ce890..4a42f3e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,12 +8,16 @@ specifiers: jasmine-spec-reporter: ^7.0.0 jsonminify: ^0.4.1 mjs-dirname: ^1.0.0 + node-addon-api: ^4.0.0 parcel: ^2.0.0-beta.3.1 prettier-config-atomic: ^2.0.5 servor: ^4.0.2 shx: 0.3.3 strip-json-comments: ^3.1.1 +dependencies: + node-addon-api: 4.0.0 + devDependencies: '@types/jasmine': 3.7.7 '@types/node': 16.0.0 @@ -4427,6 +4431,10 @@ packages: resolution: {integrity: sha512-mmcei9JghVNDYydghQmeDX8KoAm0FAiYyIcUt/N4nhyAipB17pllZQDOJD2fotxABnt4Mdz+dKTO7eftLg4d0A==} dev: true + /node-addon-api/4.0.0: + resolution: {integrity: sha512-ALmRVBFzfwldBfk3SbKfl6+PVMXiCPKZBEfsJqB/EjXAMAI+MfFrEHR+GMRBuI162DihZ1QjEZ8ieYKuRCJ8Hg==} + dev: false + /node-forge/0.10.0: resolution: {integrity: sha512-PPmu8eEeG9saEUvI97fm4OYxXVB6bFvyNTyiUOBichBpFG8A1Ljw3bY62+5oOjDEMHRnd0Y7HQ+x7uzxOzC6JA==} engines: {node: '>= 6.0.0'} diff --git a/src/native/lib.d b/src/native/lib.d index fdee184..e071eb8 100644 --- a/src/native/lib.d +++ b/src/native/lib.d @@ -17,7 +17,7 @@ const spaceOrBreakRegex = ctRegex!(`\s`); Return: the minified json string */ -string minifyString(in string jsonString, in bool hasComment = false) @trusted +extern (C) string minifyString(string jsonString, bool hasComment = false) @trusted { auto in_string = false; auto in_multiline_comment = false; @@ -133,7 +133,7 @@ private bool notSlashAndNoSpaceOrBreak(in string matchFrontHit) @safe files = the paths to the files. hasComment = a boolean to support comments in json. Default: `false`. */ -void minifyFiles(in string[] files, in bool hasComment = false) +extern (C) void minifyFiles(string[] files, bool hasComment = false) { import std.parallelism : parallel; import std.file : readText, write; diff --git a/src/native/libc.d b/src/native/libc.d index 59ee634..dc2e1e8 100644 --- a/src/native/libc.d +++ b/src/native/libc.d @@ -2,8 +2,6 @@ module minijson.libc; import minijson.lib : minifyString; -extern (C): - /** Minify the given JSON string using C ABI. @@ -14,7 +12,7 @@ extern (C): Return: the minified json string */ -auto minifyString(in char* jsonCString, in bool hasComment = false) +extern (C) auto c_minifyString(in char* jsonCString, in bool hasComment = false) { import std : fromStringz, toStringz; diff --git a/src/node/node.d b/src/node/node.d new file mode 100644 index 0000000..66683bb --- /dev/null +++ b/src/node/node.d @@ -0,0 +1,7 @@ +module minijson.node; + +import node_dlang; + +import minijson.lib: minifyString; + +mixin exportToJs! (minifyString);