Coder Social home page Coder Social logo

hanazuki / node-jsonnet Goto Github PK

View Code? Open in Web Editor NEW
31.0 2.0 1.0 19.99 MB

libjsonnet binding for Node.js

Home Page: https://www.npmjs.com/package/@hanazuki/node-jsonnet

License: MIT License

CMake 1.93% C++ 64.10% TypeScript 2.45% JavaScript 29.01% Shell 2.51%
node-addon jsonnet

node-jsonnet's Introduction

node-jsonnet

@hanazuki/node-jsonnet is a libjsonnet binding for Node.js (native addon), which supports all the basic Jsonnet functionality including import and native callbacks (std.native). It also comes with TypeScript type definitions.

Synopsis

import { Jsonnet } from "@hanazuki/node-jsonnet";
const jsonnet = new Jsonnet();

// Evaluates a simple Jsonnet program into a JSON value
jsonnet.evaluateSnippet(`{a: 1 + 2, b: self.a * 3}`)
       .then(json => console.log(JSON.parse(json)));  // => { a: 3, b: 9 }

// Jsonnet programs can use JavaScript values through external variables (std.extVar)
// and native callbacks (std.native).
jsonnet.extCode("x", "4")
       .nativeCallback("add", (a, b) => Number(a) + Number(b), "a", "b")
       .evaluateSnippet(`std.extVar("x") * std.native("add")(1, 2)`)
       .then(json => console.log(JSON.parse(json)));  // => 12

The library is documented in the TypeScript type definitions at types/index.d.ts, and HTML documentation is also available online.

Installation Requirements

  • Node.js v14 or later
  • GCC or Clang C++ compiler that supports C++17
  • CMake 3.8 or later

License

@hanazuki/node-jsonnet is licensed under the MIT License (See LICENSE file for the terms). libjsonnet, whose source code is bundled in the distributed NPM packages of @hanazuki/node-jsonnet, is licensed under the Apache License, Version 2.0 (See third_party/jsonnet/LICENSE in this package or LICENSE file in the original repository). libjsonnet also bundles third-party software that is subject to other OSS licenses.

node-jsonnet's People

Contributors

dependabot[bot] avatar hanazuki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

osherg

node-jsonnet's Issues

How to use node-jsonnet as React app dependency

I created a react application from template with:

yarn create react-app --template cra-template-typescript my-app

Then I added "@hanazuki/node-jsonnet": "^0.3.0" as dependency to package.json code sample of the readme to App.tsx, run yarn install then yarn start. The served page indicates the following errors:

TypeError: Error.captureStackTrace is not a function
getFileName
node_modules/bindings/bindings.js:169

  166 | };
  167 | 
  168 | // run the 'prepareStackTrace' function above
> 169 | Error.captureStackTrace(dummy);
      | ^  170 | dummy.stack;
  171 | 
  172 | // cleanup

bindings
node_modules/bindings/bindings.js:82

  79 | 
  80 | // Get the module root
  81 | if (!opts.module_root) {
> 82 |   opts.module_root = exports.getRoot(exports.getFileName());
     | ^  83 | }
  84 | 
  85 | // Ensure the given bindings name ends with .node

./node_modules/@hanazuki/node-jsonnet/lib/index.js
node_modules/@hanazuki/node-jsonnet/lib/index.js:2

  1 | /*! SPDX-License-Identifier: MIT */
> 2 | module.exports.Jsonnet = require('bindings')('node-jsonnet');
  3 | 

__webpack_require__
/home/philippe/temp/react/my-app/webpack/bootstrap:784

  781 | };
  782 | 
  783 | // Execute the module function
> 784 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
      | ^  785 | 
  786 | // Flag the module as loaded
  787 | module.l = true;

fn
/home/philippe/temp/react/my-app/webpack/bootstrap:150

  147 | 		);
  148 | 		hotCurrentParents = [];
  149 | 	}
> 150 | 	return __webpack_require__(request);
      | ^  151 | };
  152 | var ObjectFactory = function ObjectFactory(name) {
  153 | 	return {

./src/App.tsx
http://localhost:3000/static/js/main.chunk.js:109:99
__webpack_require__
/home/philippe/temp/react/my-app/webpack/bootstrap:784

  781 | };
  782 | 
  783 | // Execute the module function
> 784 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
      | ^  785 | 
  786 | // Flag the module as loaded
  787 | module.l = true;

fn
/home/philippe/temp/react/my-app/webpack/bootstrap:150

  147 | 		);
  148 | 		hotCurrentParents = [];
  149 | 	}
> 150 | 	return __webpack_require__(request);
      | ^  151 | };
  152 | var ObjectFactory = function ObjectFactory(name) {
  153 | 	return {

./src/index.tsx
http://localhost:3000/static/js/main.chunk.js:253:81
__webpack_require__
/home/philippe/temp/react/my-app/webpack/bootstrap:784

  781 | };
  782 | 
  783 | // Execute the module function
> 784 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
      | ^  785 | 
  786 | // Flag the module as loaded
  787 | module.l = true;

fn
/home/philippe/temp/react/my-app/webpack/bootstrap:150

  147 | 		);
  148 | 		hotCurrentParents = [];
  149 | 	}
> 150 | 	return __webpack_require__(request);
      | ^  151 | };
  152 | var ObjectFactory = function ObjectFactory(name) {
  153 | 	return {

1
http://localhost:3000/static/js/main.chunk.js:406:18
__webpack_require__
/home/philippe/temp/react/my-app/webpack/bootstrap:784

  781 | };
  782 | 
  783 | // Execute the module function
> 784 | modules[moduleId].call(module.exports, module, module.exports, hotCreateRequire(moduleId));
      | ^  785 | 
  786 | // Flag the module as loaded
  787 | module.l = true;

checkDeferredModules
/home/philippe/temp/react/my-app/webpack/bootstrap:45

  42 | 	}
  43 | 	if(fulfilled) {
  44 | 		deferredModules.splice(i--, 1);
> 45 | 		result = __webpack_require__(__webpack_require__.s = deferredModule[0]);
     | ^  46 | 	}
  47 | }
  48 | 

webpackJsonpCallback
/home/philippe/temp/react/my-app/webpack/bootstrap:32

  29 | 	deferredModules.push.apply(deferredModules, executeModules || []);
  30 | 
  31 | 	// run deferred modules when all chunks ready
> 32 | 	return checkDeferredModules();
     | ^  33 | };
  34 | function checkDeferredModules() {
  35 | 	var result;

(anonymous function)
http://localhost:3000/static/js/main.chunk.js:1:65

node_modules/@hanazuki/node-jsonnet/node_modules where empty. I run yarn install from that directory but it still doesn't work.

How can I use node-jsonnet in my application?
Thank you.

node version: v13.13.0
cmake version: 3.10.2
yarn version: 1.22.4

std.parseJson with invalid input is killing process

Code like std.parseJson("$%^&") will result with this log in console:

Something went wrong during jsonnet_evaluate_snippet, please report this: [json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - invalid literal; last read: 'd'

and process will get killed.

Instead promise should be rejected. so one can handle this in catch instead of process shutting down.

related google/jsonnet#680

Custom Functions

  • How to implement custom jsonnet functions for this library?
  • If there is any way for creating custom jsonnet function than please mention it in documantation.

Build fails during install with GCC 4.9

[ 71%] Built target libjsonnet
Scanning dependencies of target node-jsonnet
[ 76%] Building CXX object CMakeFiles/node-jsonnet.dir/src/JsonValueConverter.cpp.o
In file included from /usr/src/app/node_modules/@hanazuki/node-jsonnet/src/JsonValueConverter.hpp:5:0,
                 from /usr/src/app/node_modules/@hanazuki/node-jsonnet/src/JsonValueConverter.cpp:4:
/usr/src/app/node_modules/@hanazuki/node-jsonnet/src/JsonnetVm.hpp:10:20: fatal error: optional: No such file or directory
 #include <optional>
                    ^
compilation terminated.
CMakeFiles/node-jsonnet.dir/build.make:82: recipe for target 'CMakeFiles/node-jsonnet.dir/src/JsonValueConverter.cpp.o' failed
make[2]: *** [CMakeFiles/node-jsonnet.dir/src/JsonValueConverter.cpp.o] Error 1
make[1]: *** [CMakeFiles/node-jsonnet.dir/all] Error 2
CMakeFiles/Makefile2:288: recipe for target 'CMakeFiles/node-jsonnet.dir/all' failed
Makefile:149: recipe for target 'all' failed
make: *** [all] Error 2

import statement from example does not work

When trying to use the import statement from the example:

import {Jsonnet} from '@hanazuki/node-jsonnet'; 

I get this error:

import {Jsonnet} from '@hanazuki/node-jsonnet';
        ^^^^^^^
SyntaxError: The requested module '@hanazuki/node-jsonnet' is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export.
For example:
import pkg from '@hanazuki/node-jsonnet';
const {Jsonnet} = pkg;

Using nodejs v12.19.0

Install fails on Windows/MSVC

Running npm install in an empty project yields the following output:

C:\dev\jsonnet-test>npm install @hanazuki/node-jsonnet

> @hanazuki/[email protected] install C:\dev\jsonnet-test\node_modules\@hanazuki\node-jsonnet
> cmake-js build --CDCMAKE_EXPORT_COMPILE_COMMANDS=1

[
  'C:\\Program Files\\nodejs\\node.exe',
  'C:\\dev\\jsonnet-test\\node_modules\\cmake-js\\bin\\cmake-js',
  'build',
  '--CDCMAKE_EXPORT_COMPILE_COMMANDS=1'
]
info TOOL Using Visual Studio 16 2019 generator.
info CMD CONFIGURE
info RUN [
info RUN   'cmake',
info RUN   'C:\\dev\\jsonnet-test\\node_modules\\@hanazuki\\node-jsonnet',
info RUN   '--no-warn-unused-cli',
info RUN   '-G',
info RUN   'Visual Studio 16 2019',
info RUN   '-A',
info RUN   'x64',
info RUN   '-DCMAKE_JS_VERSION=6.2.1',
info RUN   '-DCMAKE_BUILD_TYPE=Release',
info RUN   '-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=C:\\dev\\jsonnet-test\\node_modules\\@hanazuki\\node-jsonnet\\build',
info RUN   '-DCMAKE_JS_INC=<REDACTED>\\.cmake-js\\node-x64\\v14.17.0\\include\\node',
info RUN   '-DCMAKE_JS_SRC=C:/dev/jsonnet-test/node_modules/cmake-js/lib/cpp/win_delay_load_hook.cc',
info RUN   '-DNODE_RUNTIME=node',
info RUN   '-DNODE_RUNTIMEVERSION=14.17.0',
info RUN   '-DNODE_ARCH=x64',
info RUN   '-DCMAKE_JS_LIB=<REDACTED>\\.cmake-js\\node-x64\\v14.17.0\\win-x64\\node.lib',
info RUN   '-DCMAKE_EXPORT_COMPILE_COMMANDS=1',
info RUN   '-DCMAKE_SHARED_LINKER_FLAGS=/DELAYLOAD:NODE.EXE'
info RUN ]
Not searching for unused variables given on the command line.
-- Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19042.
-- The C compiler identification is MSVC 19.29.30040.0
-- The CXX compiler identification is MSVC 19.29.30040.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.29.30037/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.29.30037/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Deprecation Warning at third_party/jsonnet/CMakeLists.txt:2 (cmake_minimum_required):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

  Update the VERSION argument <min> value or use a ...<max> suffix to tell
  CMake that the project does not need compatibility with older versions.


CMake Error at third_party/jsonnet/CMakeLists.txt:43 (message):
  Compiler MSVC not supported


-- Configuring incomplete, errors occurred!
See also "C:/dev/jsonnet-test/node_modules/@hanazuki/node-jsonnet/build/CMakeFiles/CMakeOutput.log".
ERR! OMG Process terminated: 1
npm WARN @hanazuki/[email protected] requires a peer of @types/node@* but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] No description
npm WARN [email protected] No repository field.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @hanazuki/[email protected] install: `cmake-js build --CDCMAKE_EXPORT_COMPILE_COMMANDS=1`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the @hanazuki/[email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     <REDACTED>\AppData\Roaming\npm-cache\_logs\2021-08-10T14_14_02_481Z-debug.log

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.