Skip to content

NPM package for Kuneiform Parser. Works in NodeJS and Browser.

License

Notifications You must be signed in to change notification settings

kwilteam/kuneiform-parser-ts

Repository files navigation

Kwil Kuneiform Parser (JS / TS)

This package allows developers to compile raw Kuneiform code in Browser and NodeJS applications.

The Kuneiform Parser parses raw Kuneiform to JSON, which can be used to deploy a database on Kwil with the Kwil-JS SDK. This is an alternative to using the Kuneiform IDE or Kwil CLI to deploy a database on Kwil.

How it works

This package downloads the latest production-version WASM binary from the Kuneifrom Release Repo, instantiate the binary, and calls the .parseKuneiform method. Please note the size of the WASM binary (~10 mb) and account for how that may affect your application.

Installation

npm i kuneiform-parser

Initialization

The parser has a slightly different initialization depending on NodeJs or Browser.

Configuration Options

Within the .load() method in both the WebParser and NodeParser classes, you can pass an optional configuration option. If using WebParser, you should pass a CORS proxy, otherwise your browser will block the WASM Download.

const nodeConfig: NodeConfig = {
    cacheTtl?: number, // optional number of seconds for the WASM file to be cached. Default is 1 day.
    logger?: (msg: string) => void // optional a logging function to capture all logs
}

const webConfig: WebConfig = {
    corsProxyUrl: string // URL for CORS proxy
    cacheTtl?: number, // same as node
    logger?: (msg: string) => void // same as node
}

Web - Initialize

import { WebParser, WebConfig} from 'kuneiform-parser';

const config: WebParserConfig = {
    corsProxyUrl: 'https://cors-anywhere.herokuapp.com/'
};

async function parse() {
    const parser = await WebParser.load(config);
}

NodeJS - Initialize

import { NodeParser } from 'kuneiform-parser';


async function loadParser() {
    const parser = await NodeParser.load(config);
}

Parsing

Once the parser has loaded, you can call .parse() to parse your raw Kuneiform to JSON.

import { NodeParser, ParseRes } from 'kuneiform-parser';
import kuneiform from 'some_kf_file.kf';

async function loadParser() {
    /* previous code */
    const res = await parser.parse<ParseRes>(kuneiform)

    /*
        res = {
            json: Stringified<KuneiformObject>
        }
    */
}

And that is it! The json property that is returned can be passed to the KwilJS DBBuilder Payload to create a database on the Kwil Network.

Other Resources

License

MIT License

Help / Feedback

Any questions and or feedback is welcomed in the Kwil Discord or to [email protected].

About

NPM package for Kuneiform Parser. Works in NodeJS and Browser.

Resources

License

Stars

Watchers

Forks

Packages

No packages published