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

Evening Hack: generate browser-compatible javascript #31

Closed
wants to merge 1 commit into from
Closed

Evening Hack: generate browser-compatible javascript #31

wants to merge 1 commit into from

Conversation

Xe
Copy link

@Xe Xe commented Jan 19, 2018

This generates reasonably sane javascript that is fairly universally compatible. Usage is simple:

Given this protobuf:

// printerfacts.proto
syntax = "proto3";

package us.xeserv.api.printerfacts;
option go_package = "proto";

service Printerfacts {
      rpc Fact(FactParams) returns (Facts) {};
}

message FactParams {
      int32 count = 1;
}

message Facts {
      repeated string facts = 1;
}

this tool will generate output javascript such as
https://xena.greedo.xeserv.us/files/twirp_printerfacts/printerfacts_twirp.js

usage is simple:

#!/bin/sh
# regen.sh
# regenerates protocol buffer generated code

protoc  --proto_path=$GOPATH/src:. \
        --twirp_out=. \
        --twirp_jsbrowser_out=. \
        --go_out=. \
        ./printerfacts.proto

Usage from HTML is simple:

<script src="https://xena.greedo.xeserv.us/files/twirp_printerfacts/printerfacts_twirp.js"></script>
<script>
 let onSuccess = (x) => {
     document.write(x.facts[0]);
 };
 let onError = (x) => {
     console.log(x);
 };

 Printerfacts_fact("https://printerfacts.herokuapp.com", {"count": 1}, onSuccess, onError);
</script>

Ref #19

See example here. (source code at https://github.com/Xe/printerfacts)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

This generates reasonably sane javascript that is fairly universally
compatible. Usage is simple:

Given this protobuf:

```protobuf
syntax = "proto3";

package us.xeserv.api.printerfacts;
option go_package = "proto";

service Printerfacts {
      rpc Fact(FactParams) returns (Facts) {};
}

message FactParams {
      int32 count = 1;
}

message Facts {
      repeated string facts = 1;
}
```

this tool will generate output javascript such as
https://xena.greedo.xeserv.us/files/twirp_printerfacts/printerfacts_twirp.js

usage is simple:

```shell

protoc  --proto_path=$GOPATH/src:. \
        --twirp_out=. \
        --twirp_jsbrowser_out=. \
        --go_out=. \
        ./printerfacts.proto
```

Usage from HTML is simple:

```html
<script src="https://xena.greedo.xeserv.us/files/twirp_printerfacts/printerfacts_twirp.js"></script>
<script>
 let onSuccess = (x) => {
     document.write(x.facts[0]);
 };
 let onError = (x) => {
     console.log(x);
 };

 Printerfacts_fact("https://printerfacts.herokuapp.com", {"count": 1}, onSuccess, onError);
</script>
```
@amerine
Copy link

amerine commented Jan 19, 2018

Nice

@spenczar
Copy link
Contributor

This is very, very cool, and impressively lightweight. Thanks for sharing it!

I think I'd like to keep the bar for inclusion in this repo pretty high. Clients here should do all of the following:

  • feel very idiomatic to experts familiar with the language they're targeting
  • minimize external dependencies
  • speak protobuf to the server, not just JSON
  • play well with the existing official protobuf compiler for the language, if one exists
  • handle errors correctly

I can't say whether this is idiomatic - I'm not really a JavaScript pro. It clearly doesn't have any dependencies. But I'd like to see it speak in protobuf before accepting it.

There's an official JavaScript protobuf compiler these days. I think a generated JavaScript client should depend upon its output.

It looks like the official compiler has options that change the output, letting you pick how you want to import the generated code. The official Twirp JS generator should accept identically-named options to be compatible.

I want to reiterate that this is just the bar for getting included in github.com/twitchtv/twirp. I don't think you should feel the bar is so high to host your generator elsewhere, but I'd like to keep the ones in here polished (and in fact the Python one needs a lot more polish right now).

In order to make it easier for others to write generators and host them elsewhere, I'm strongly considering making github.com/twitchtv/twirp/internal/gen public, moving it to github.com/twitchtv/twirp/protocgen or something.

@joneskoo
Copy link

I'm also looking to use twirp with a client from browser without dependencies - for a simple API I'm fine with a JSON only client.

I would also like the typescript definition file to be generated. That way it's possible to have amazing quality of life as the need to write JavaScript to include a web frontend for a Go project is minimal, and my editor can tell me what fields are available and their types.

If this doesn't fit the twirp proper, the twirp documentation/README should have a blurp link to how to find these extensions.

@Xe
Copy link
Author

Xe commented Jan 24, 2018

@joneskoo I've moved this binary into https://github.com/Xe/twirp-codegens, I don't personally like typescript but I'd be more than happy to accept a PR that generates typescript type definitions.

@thechriswalker
Copy link

thechriswalker commented Jan 24, 2018

@spenczar I took your suggestions and produced this: thechriswalker@c379f7e

I haven't made a PR yet, but would be interested in feedback. It takes the official google protobuf javascript compiler output and creates the twirp api client on the side. It generates code for both node and the browser, speaking JSON and protobuf and the node version passes the clientcompat tests.

It does only generate commonjs modules currently.

Edit: I have updated that fork since, and a more valid comment is in the issue: #61

@spenczar
Copy link
Contributor

spenczar commented Feb 2, 2018

https://github.com/twitchtv/protogen now exists, so you can import Twirp's protoc plugin library. Hooray!

I'm going to close this issue as community-maintained generators seem like the best way forward right now.

@spenczar spenczar closed this Feb 2, 2018
@Xe Xe deleted the Xe/feat/browser-js-protoc-gen branch February 4, 2018 04:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants