TS and JS bindings for wasm-compiled-from-go [Jsonnet](https://jsonnet.org/) json templating. Works in Node.js and browser environments.
npm install tplfa-jsonnetJsonnet is a templating extension of JSON.
> The design is influenced by several configuration languages internal to Google, and embodies years of experience configuring some of the world's most complex IT systems. Jsonnet is now used by many companies and projects.
For the tplfa-jsonnet package, Jsonnet is compiled from Go to WASM and wrapped with helper functions.
The letters "tplfa" stay for "Templating for API", a project to provide a unified interface to generative AI providers. tplfa-jsonnet is the foundation part of the larger project.
```
npm install tplfa-jsonnet
Load the library in Node.js:
`
import 'tplfa-jsonnet/wasm_exec.js';
import { Jsonnet, getJsonnet } from 'tplfa-jsonnet/jsonnet';
...
const jsonnetWasm = await fs.promises.readFile(
require.resolve('tplfa-jsonnet/libjsonnet.wasm')
);
jsonnet = await getJsonnet(jsonnetWasm);
`
Load the library in the browser:
``
The jsonnet object provides two functions:
- jsonnet_evaluate_snippet: Wrapper for the corresponding jsonnet library functionevaluate
- : Simplified interface. Only code, its external variables, and library files
The function signature:
`
evaluate: (
//
// Jsonnet code
//
code: string,
//
// Optional variables.
// The values should be stringified.
// For use in the Jsonnet code:
//
// std.extVar("")
// std.parseJson(std.extVar(""))
//
extrStrs?: Record
//
// Optional library code. For use in the Jsonnet code:
//
// import "
//
files?: Record
//
// Output: serialized JSON. Use JSON.parse to deserialize.`
//
) => Promise
See:
- Browser environment: https://github.com/olpa/templating-for-api/tree/master/in-action/web-jsonnet/
- Node.js environment: https://github.com/olpa/templating-for-api/tree/master/in-action/nodejs-jsonnet/
For old node versions, you need to edit the file wasm_exec.js. After the line use strict add line globalThis.crypto ??= require('crypto');`. Maybe more changes are needed:
MIT
Jsonnet: Apache license 2.0
Oleg Parashchenko