Skip to content

Commit

Permalink
I167 jsonnet to npm (#8)
Browse files Browse the repository at this point in the history
* Add a description and repository links.

* Add keywords.

* Reordered README, add how to load in the browser.

* Summary.

* deepl.

* Update dist README.

* Relative links to absolute urls.

* Update changelog.

* Use the npm version in nodejs example.

* Upgrade "api".

* Upgrade packages, swith to npmjs tplfa-jsonnet.

* Update changelog of tplfa.

* Updgrade in-action/nodejs-tplfa.

* "tplfa" should be of the version 1.1.2.
  • Loading branch information
olpa authored Oct 14, 2024
1 parent 61f25d6 commit 9ee4558
Show file tree
Hide file tree
Showing 21 changed files with 236 additions and 81 deletions.
13 changes: 5 additions & 8 deletions apis/anthropic/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apis/anthropic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"tplfa-jsonnet": "file:../../jsonnet-js-ts/dist",
"tplfa-jsonnet": "^1.2.0",
"ts-node": "^10.9.2"
},
"devDependencies": {
Expand Down
13 changes: 5 additions & 8 deletions apis/openai/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apis/openai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"tplfa-jsonnet": "file:../../jsonnet-js-ts/dist",
"tplfa-jsonnet": "^1.2.0",
"ts-node": "^10.9.2"
},
"devDependencies": {
Expand Down
13 changes: 5 additions & 8 deletions in-action/nodejs-jsonnet/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion in-action/nodejs-jsonnet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"start": "ts-node index.ts"
},
"dependencies": {
"tplfa-jsonnet": "file:../../jsonnet-js-ts/dist",
"tplfa-jsonnet": "^1.2.0",
"ts-node": "^10.9.2",
"typescript": "^5.5.4"
}
Expand Down
21 changes: 9 additions & 12 deletions in-action/nodejs-tplfa/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion in-action/nodejs-tplfa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"ajv": "^8.12.0",
"tplfa": "file:../../tplfa-js/dist",
"tplfa-apis": "file:../../apis/dist",
"tplfa-jsonnet": "file:../../jsonnet-js-ts/dist",
"tplfa-jsonnet": "^1.2.0",
"ts-node": "^10.9.2",
"typescript": "^5.4.3",
"yargs": "^17.7.2"
Expand Down
90 changes: 80 additions & 10 deletions jsonnet-js-ts/README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
# jsonnet for TypeScript and JavaScript
# Jsonnet for TypeScript and JavaScript

For sample usage, see
[Jsonnet](https://jsonnet.org/) is a templating extension of JSON.

- [../in-action/web-jsonnet/](../in-action/web-jsonnet/)
- [../in-action/nodejs-jsonnet/](../in-action/nodejs-jsonnet/)
> 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.
Installation:
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](https://github.com/olpa/templating-for-api)", a project to provide a unified interface to generative AI providers. `tplfa-jsonnet` is the foundation part of the larger project.

## Installation

```
npm install https://github.com/olpa/templating-for-api/releases/download/jsonnet-v1.1.0/jsonnet-v1.1.0.tar.gz
npm install tplfa-jsonnet
```

In code:
## Usage

Load the library in Node.js:

```
import 'tplfa-jsonnet/wasm_exec.js';
Expand All @@ -24,11 +29,76 @@ const jsonnetWasm = await fs.promises.readFile(
jsonnet = await getJsonnet(jsonnetWasm);
```

The object `jsonnet` provides two functions:
Load the library in the browser:

```
<script src="/js/wasm_exec.js"></script>
<script src="/js/jsonnet-web.js"></script>
<script>
const jsonnetPromise = (async () => {
console.log('Loading libjsonnet: started');
const jnWasm = fetch('/js/libjsonnet.wasm');
const jn = await getJsonnet(jnWasm);
console.log('Loading libjsonnet: done');
return jn;
})();
</script>
```

The `jsonnet` object provides two functions:

- `jsonnet_evaluate_snippet`: Wrapper for the corresponding jsonnet library function
- `evaluate`: Simplified interface. Only code, its external variables, and library files

Compatibility note:
The function signature:

```
evaluate: (
//
// Jsonnet code
//
code: string,
//
// Optional variables.
// The values should be stringified.
// For use in the Jsonnet code:
//
// std.extVar("<var name>")
// std.parseJson(std.extVar("<var name>"))
//
extrStrs?: Record<string, string>,
//
// Optional library code. For use in the Jsonnet code:
//
// import "<file name>"
//
files?: Record<string, string>
//
// Output: serialized JSON. Use `JSON.parse` to deserialize.
//
) => Promise<string>;
```

### Full examples

See:

- Browser environment: [../in-action/web-jsonnet/](../in-action/web-jsonnet/)
- Node.js environment: [../in-action/nodejs-jsonnet/](../in-action/nodejs-jsonnet/)


## Compatibility note

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: <https://github.com/golang/go/issues/53128>.


## License, contact

MIT

Jsonnet: Apache license 2.0

For old versions of node you need to edit the file `./dist/wasm_exec.js`. After the line `use strict` add line `globalThis.crypto ??= require('crypto');`. Maybe more changes are needed: <https://github.com/golang/go/issues/53128>.
Oleg Parashchenko <olpa@uucode.com>
1 change: 1 addition & 0 deletions jsonnet-js-ts/build-dist.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ tsc /src/jsonnet.ts --outDir /dist/ --target es2017 --module commonjs --declarat
"

cp README.md changelog.md ./dist/
sed --in-place 's#\.\./#https://github.com/olpa/templating-for-api/tree/master/#g' ./dist/README.md
5 changes: 5 additions & 0 deletions jsonnet-js-ts/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog for jsonnet-js-ts

## [1.2.0] - 2024-10-11

- Pack as a npm package `tplfa-jsonnet`


## [1.1.0] - 2024-10-02

- Expose the argument for the library files
Expand Down
Loading

0 comments on commit 9ee4558

Please sign in to comment.