cue-lang / cue

The home of the CUE language! Validate and define text-based and dynamic configuration
https://cuelang.org
Apache License 2.0
5.15k stars 296 forks source link

is there an NPM wrapper? #86

Open cueckoo opened 3 years ago

cueckoo commented 3 years ago

Originally opened by @dcsan in https://github.com/cuelang/cue/issues/86

I'd like to use this as a replacement for YAML in some javascript/ts projects, is there a wrapper/package for NPM?

cueckoo commented 3 years ago

Original reply by @mpvl in https://github.com/cuelang/cue/issues/86#issuecomment-527239546

No, but there is an excellent Go to JS transpiler (GopherJS). I tried it a while back and it works great for CUE. So if anyone wants to define a JS API, I'll be happy to get people started and include a separate repo in this org.

What kind of functionality were you thinking of?

cueckoo commented 3 years ago

Original reply by @dcsan in https://github.com/cuelang/cue/issues/86#issuecomment-527244824

i just want to start with basic read/write to replace some yaml and json files...

cueckoo commented 3 years ago

Original reply by @rudolph9 in https://github.com/cuelang/cue/issues/86#issuecomment-527259705

@mpvl would you mind adding me to a repo for a js API rin this org? I have need for this my self.

I actually did a quick test of this my self this morning. I following this tutorial:

package main

import (
    "fmt"
    "math/big"

    "cuelang.org/go/cue"
)

func main() {
    const config = `
TimeSeries: {
  "2019-09-01T07:00:00Z": 36
}
TimeSeries: {
  "2019-09-01T07:10:59Z": 200
}
`
    var r cue.Runtime

    instance, err := r.Compile("test", config)
    if err != nil {
        fmt.Println(err)
    }

    var bigInt big.Int
    instance.Lookup("TimeSeries").Lookup("2019-09-01T07:10:59Z").Int(&bigInt)
    fmt.Println(bigInt.String())

}

GOARCH=wasm GOOS=js go build -o lib.wasm main.go builds no problem.

Here is a little gist with everything needed if anyone else is interested https://gist.github.com/rudolph9/dd5e3dbcbf762abdb3f662e39dacfa7f

cueckoo commented 3 years ago

Original reply by @mpvl in https://github.com/cuelang/cue/issues/86#issuecomment-527482187

This is fine with me.

Two questions:

cueckoo commented 3 years ago

Original reply by @rudolph9 in https://github.com/cuelang/cue/issues/86#issuecomment-527496653

This is fine with me.

Two questions:

* Preference for Github or Gerrit workflow? (The CI is better set up for Github, at this point)

Github

* Does the name `lang-js` work?

I think that works but only other thing I would consider is perhaps there will multiple js repos that result in an npm package. The impl I'm building will be a wasm wrapper, perhaps in the future there will be an cue FFI compatible with nodejs, and perhaps in the future too the golang compiler may target javascript and we would want a package for that too??

Pragmatically speaking though, lang-js is probably fine.

cueckoo commented 3 years ago

Original reply by @rudolph9 in https://github.com/cuelang/cue/issues/86#issuecomment-527498302

@mpvl Speaking of CI, have you requested beta access to github actions for the cuelang org? Haven't worked with them extensively but look convenient and might be helpful.

cueckoo commented 3 years ago

Original reply by @rudolph9 in https://github.com/cuelang/cue/issues/86#issuecomment-527501711

@mpvl I created the @cuelang npmjs.com org (see doc here) Do you have an npmjs.com user name I could add as an owner of the org?

cueckoo commented 3 years ago

Original reply by @mpvl in https://github.com/cuelang/cue/issues/86#issuecomment-527524648

@rudolph9 that seems useful indeed, especially wider OS coverage. I added cuelang. We'll see.

cueckoo commented 3 years ago

Original reply by @mpvl in https://github.com/cuelang/cue/issues/86#issuecomment-527525830

I didn't, but created mpvl.

cueckoo commented 3 years ago

Original reply by @mpvl in https://github.com/cuelang/cue/issues/86#issuecomment-527527398

@rudolph9 Then maybe lang-wasm?

BTW, while you're at it, another very useful wrapper would be cue/format.Node. Would come in handy for, say, an in-browser CUE demo. :)

cueckoo commented 3 years ago

Original reply by @rudolph9 in https://github.com/cuelang/cue/issues/86#issuecomment-527533961

I didn't, but created mpvl.

@mpvl I invited that user and will be able to make you owner once you accept the invite.

cueckoo commented 3 years ago

Original reply by @rudolph9 in https://github.com/cuelang/cue/issues/86#issuecomment-527543506

@rudolph9 Then maybe lang-wasm?

Can we call it lang-wasm-js? Since it's providing a js wrapper around a wasm compile target.

BTW, while you're at it, another very useful wrapper would be cue/format.Node. Would come in handy for, say, an in-browser CUE demo. :)

:+1:

cueckoo commented 3 years ago

Original reply by @mpvl in https://github.com/cuelang/cue/issues/86#issuecomment-527550444

@rudolph9 Then maybe lang-wasm?

Can we call it lang-wasm-js? Since it's providing a js wrapper around a wasm compile target. That seems to get a bit hairy. Let's stick with lang-js.

Created it and added you as collaborator.

cueckoo commented 3 years ago

Original reply by @marcus-sa in https://github.com/cuelang/cue/issues/86#issuecomment-537711864

I'd be really interested in this aswell.

cueckoo commented 3 years ago

Original reply by @myitcv in https://github.com/cuelang/cue/issues/86#issuecomment-538059819

cc @mvdan who has experience of this sort of thing in https://github.com/mvdan/sh

cueckoo commented 3 years ago

Original reply by @marcus-sa in https://github.com/cuelang/cue/issues/86#issuecomment-538069532

As a workaround for now, I'm currently thinking about either: 1) Wrapping a gRPC server around the CUE package 2) Executing CLI commands from Node.js

cueckoo commented 3 years ago

Original reply by @rudolph9 in https://github.com/cuelang/cue/issues/86#issuecomment-538156152

I did a little investigating on this today to explore using gopherjs to compile directly to javascript but unfortunately it seems it don't fully support go mod files yet and is choking on the cockroachdb/ada dependency

cuelang/lang-js [implement-binding●] » gopherjs run --verbose lang-js.go
cannot find package "github.com/cockroachdb/apd/v2" in any of:
        /usr/local/go/src/github.com/cockroachdb/apd/v2 (from $GOROOT)
        /home/username/.golang/src/github.com/cockroachdb/apd/v2 (from $GOPATH)
cueckoo commented 3 years ago

Original reply by @myitcv in https://github.com/cuelang/cue/issues/86#issuecomment-539410466

@rudolph9 my fork of GopherJS does have module support https://github.com/myitcv/gopherjs

https://github.com/myitcv/gopherjs/pull/50 is tracking Go 1.13 support, but things work with Go 1.12, as demonstrated by the proof of concept CUE playground: https://blog.myitcv.io/gopherjs_examples_sites/cueplayground/

cueckoo commented 3 years ago

Original reply by @rudolph9 in https://github.com/cuelang/cue/issues/86#issuecomment-555596668

I didn't, but created mpvl.

@mpvl I invited that user and will be able to make you owner once you accept the invite.

Hey @mpvl I'm circling back to this ticket and and just want to make sure all the ducks are in a row. Regardless where you decide we should publish the cuelange npm package it would be good for you to be the owner of the npm org.

image

cueckoo commented 3 years ago

Original reply by @rudolph9 in https://github.com/cuelang/cue/issues/86#issuecomment-556860421

@myitcv thanks for the tip, I was able to successfully target JS using your fork of gopherjs. Still pretty rough, still need to properly bundle it as a package but here it is.

I got a wasm version of it going initially using golang-wasm-async-loader but spun my wheels for far too long trying to package the wasm :angry:

I could see use cases for both a pure js impl and a wasm binding version as an app that interacts with cue often pass lots of data to be evaluated or receiving lots of data after evaluation likely would be more performant using js impl where as a wasm binding would likely better in the case of less frequent but lots of state in the cuelang runtime case a likely would be a good condidate to run in a web worker.

@myitcv does gopherjs have any tooling around interacting targeting and packaging a wasm or is the community mostly focused on compiling golang to js?

@mpvl I probably have an npm package for you to review in the near future, the gopher approach is kinda big (8MB unminified) but works pretty seamlessly in firefox, chrome, and nodejs 11 on my x230 thinkpad.

I think an initial js use-case that will really take off is the use of cue in redux where you basically define one action type and one reducer where you define a cue spec that has emit which serves as the next state and each time an action is received by the reducer it evaluated by cue along previous state. In turn the the iterative union a set cue constraints (effectively the reducer), the previous state (initially {} but always concrete and the emit each time an action is processed i.e. each iteration) and an action (some json triggered most often by user interactions with the UI) become a state machine. Hopefully will expose cue to a much wider audience.

cueckoo commented 3 years ago

Original reply by @rudolph9 in https://github.com/cuelang/cue/issues/86#issuecomment-558396917

If anyone is interested I created a draft package to check out:

https://github.com/rudolph9/lang-js/packages/63456

fyi github packages is a little finicky and wound up doing to the following to get it to properly install with yarn:

 yarn --registry=https://npm.pkg.github.com/ --update-checksums add @rudolph9/cue@0.0.0 
tmp/something [master●] » node
> require('@rudolph9/cue')
{ Cue: [Function: Cue],
  Instance: [Function: Instance],
  Value: [Function: Value] }
> const { Cue } = require('@rudolph9/cue')
undefined
> cue = new Cue()
Cue {
  __cue__:
   { __internal_object__: { '$val': [Circular], runtime: [Object] },
     Compile: [Function] } }
> cue.compile('test', 'hello: "world"').then(i => i.value()).then(v => v.toString()).then(console.log)
Promise { <pending> }
> {
        hello: "world"
cueckoo commented 3 years ago

Original reply by @myitcv in https://github.com/cuelang/cue/issues/86#issuecomment-558648159

@rudolph9

does gopherjs have any tooling around interacting targeting and packaging a wasm or is the community mostly focused on compiling golang to js?

No, GopherJS is focussed entirely on JS.

For an example of bootstrapping WASM in a webpage see https://github.com/golang/go/tree/master/misc/wasm

cueckoo commented 3 years ago

Original reply by @rudolph9 in https://github.com/cuelang/cue/issues/86#issuecomment-558755269

No, GopherJS is focussed entirely on JS.

For an example of bootstrapping WASM in a webpage see https://github.com/golang/go/tree/master/misc/wasm

@myitcv thank you for the feedback. Since gopherjs supports "system/js" I think I'll work on swapping out what is defined with "gopher/js" and then both can be supported.

cueckoo commented 3 years ago

Original reply by @buremba in https://github.com/cuelang/cue/issues/86#issuecomment-675422006

We're thinking of building an online IDE for Cue based on Monaco Editor (https://microsoft.github.io/monaco-editor/). It would be great to have a JS compiler for Cue as it can be used as an embedded language server running via Web Workers.

cueckoo commented 3 years ago

Original reply by @rudolph9 in https://github.com/cuelang/cue/issues/86#issuecomment-675517632

@buremba It's out of date but you could give https://www.npmjs.com/package/@rudolph9/cue a try. I've been meaning to build some automation into around https://github.com/rudolph9/lang-js and open a pull request into the main project.

Let me know what you think of the interface? I have some time this week, maybe I can button it up (proper doc, .github automation, etc) and request it get merged into the official cuelang org repo.

cueckoo commented 3 years ago

Original reply by @buremba in https://github.com/cuelang/cue/issues/86#issuecomment-675522768

@rudolph9 Thanks for the response, I'm trying out your port. Since the IDE will support multiple files and we now support injecting external data see here, I believe that the interface should have a signature something like this:

compile(filepath : string, files : object<string, string>, inject object<string, string>) 
keeganmccallum commented 2 years ago

I made https://github.com/dclareio/cue-wasm for my own purposes and it works pretty well if anyone is interested. Feel free to leave any suggestions/feedback as issues on the repo!