Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is there an NPM wrapper? #86

Open
cueckoo opened this issue Jul 3, 2021 · 27 comments
Open

is there an NPM wrapper? #86

cueckoo opened this issue Jul 3, 2021 · 27 comments
Labels
FeatureRequest New feature or request FeedbackWanted Further information is requested

Comments

@cueckoo
Copy link
Collaborator

cueckoo commented Jul 3, 2021

Originally opened by @dcsan in cuelang/cue#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 cueckoo added FeatureRequest New feature or request FeedbackWanted Further information is requested labels Jul 3, 2021
@cueckoo
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @mpvl in cuelang/cue#86 (comment)

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
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @dcsan in cuelang/cue#86 (comment)

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

@cueckoo
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @rudolph9 in cuelang/cue#86 (comment)

@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
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @mpvl in cuelang/cue#86 (comment)

This is fine with me.

Two questions:

  • Preference for Github or Gerrit workflow? (The CI is better set up for Github, at this point)
  • Does the name lang-js work?

@cueckoo
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @rudolph9 in cuelang/cue#86 (comment)

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
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @rudolph9 in cuelang/cue#86 (comment)

@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
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @rudolph9 in cuelang/cue#86 (comment)

@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
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @mpvl in cuelang/cue#86 (comment)

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

@cueckoo
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @mpvl in cuelang/cue#86 (comment)

I didn't, but created mpvl.

@cueckoo
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @mpvl in cuelang/cue#86 (comment)

@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
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @rudolph9 in cuelang/cue#86 (comment)

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
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @rudolph9 in cuelang/cue#86 (comment)

@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. :)

👍

@cueckoo
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @mpvl in cuelang/cue#86 (comment)

@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
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @marcus-sa in cuelang/cue#86 (comment)

I'd be really interested in this aswell.

@cueckoo
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @myitcv in cuelang/cue#86 (comment)

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

@cueckoo
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @marcus-sa in cuelang/cue#86 (comment)

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
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @rudolph9 in cuelang/cue#86 (comment)

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
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @myitcv in cuelang/cue#86 (comment)

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

myitcv/gopherjs#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
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @rudolph9 in cuelang/cue#86 (comment)

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
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @rudolph9 in cuelang/cue#86 (comment)

@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 😠

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
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @rudolph9 in cuelang/cue#86 (comment)

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
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @myitcv in cuelang/cue#86 (comment)

@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
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @rudolph9 in cuelang/cue#86 (comment)

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
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @buremba in cuelang/cue#86 (comment)

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
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @rudolph9 in cuelang/cue#86 (comment)

@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
Copy link
Collaborator Author

cueckoo commented Jul 3, 2021

Original reply by @buremba in cuelang/cue#86 (comment)

@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
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest New feature or request FeedbackWanted Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants