Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

is there an NPM wrapper? #86

Closed
dcsan opened this issue Sep 1, 2019 · 27 comments
Closed

is there an NPM wrapper? #86

dcsan opened this issue Sep 1, 2019 · 27 comments
Labels
FeatureRequest New feature or request FeedbackWanted Further information is requested

Comments

@dcsan
Copy link

dcsan commented Sep 1, 2019

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

@mpvl
Copy link
Contributor

mpvl commented Sep 2, 2019

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?

@dcsan
Copy link
Author

dcsan commented Sep 2, 2019

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

@rudolph9
Copy link
Contributor

rudolph9 commented Sep 2, 2019

@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

@mpvl
Copy link
Contributor

mpvl commented Sep 3, 2019

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?

@mpvl mpvl added the FeedbackWanted Further information is requested label Sep 3, 2019
@rudolph9
Copy link
Contributor

rudolph9 commented Sep 3, 2019

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.

@rudolph9
Copy link
Contributor

rudolph9 commented Sep 3, 2019

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

@rudolph9
Copy link
Contributor

rudolph9 commented Sep 3, 2019

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

@mpvl
Copy link
Contributor

mpvl commented Sep 3, 2019

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

@mpvl
Copy link
Contributor

mpvl commented Sep 3, 2019

I didn't, but created mpvl.

@mpvl
Copy link
Contributor

mpvl commented Sep 3, 2019

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

@rudolph9
Copy link
Contributor

rudolph9 commented Sep 3, 2019

I didn't, but created mpvl.

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

@rudolph9
Copy link
Contributor

rudolph9 commented Sep 3, 2019

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

👍

@mpvl
Copy link
Contributor

mpvl commented Sep 3, 2019

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

@marcus-sa
Copy link

I'd be really interested in this aswell.

@myitcv
Copy link
Contributor

myitcv commented Oct 3, 2019

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

@marcus-sa
Copy link

marcus-sa commented Oct 3, 2019

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

@rudolph9
Copy link
Contributor

rudolph9 commented Oct 3, 2019

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)

@myitcv
Copy link
Contributor

myitcv commented Oct 8, 2019

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

@rudolph9
Copy link
Contributor

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

@rudolph9
Copy link
Contributor

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

@rudolph9
Copy link
Contributor

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"

@myitcv
Copy link
Contributor

myitcv commented Nov 26, 2019

@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

@rudolph9
Copy link
Contributor

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.

@buremba
Copy link

buremba commented Aug 18, 2020

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.

@rudolph9
Copy link
Contributor

rudolph9 commented Aug 18, 2020

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

@buremba
Copy link

buremba commented Aug 18, 2020

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

@mpvl mpvl added the FeatureRequest New feature or request label Oct 2, 2020
@cueckoo
Copy link

cueckoo commented Jul 3, 2021

This issue has been migrated to cue-lang/cue#86.

For more details about CUE's migration to a new home, please see cue-lang/cue#1078.

@cueckoo cueckoo closed this as completed Jul 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FeatureRequest New feature or request FeedbackWanted Further information is requested
Projects
None yet
Development

No branches or pull requests

7 participants