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

Use closures instead of genfun? #18

Open
natevw opened this issue Dec 12, 2014 · 6 comments
Open

Use closures instead of genfun? #18

natevw opened this issue Dec 12, 2014 · 6 comments

Comments

@natevw
Copy link

natevw commented Dec 12, 2014

Your compile logic currently uses generate-function to build some code, e.g.

var encode = genfun()
.

This is neat (it looks like in some cases you are dynamically including/excluding entire sections of code!) — however it does mean that it won't work on the current Tessel runtime, where JavaScript code is actually compiled "offline" before it is pushed, and later eval/Function is not currently possible.

I know this is just one platform, and it's not really your fault that its "JavaScript" implementation is incomplete, but this was kind of a bummer. Protocol buffers seem like a great fit there. (FWIW, you may also run into this restriction in-browser with default Content Security Policy settings in affect.)

Anyway, just thought I'd mention this for future consideration. I know performance is a big focus here, but seems like simply returning closures instead of "generated functions" would bring compatibility (and readability) benefits, and [speculatively] might not be appreciably slower on modern optimizing JS engines.

@mafintosh
Copy link
Owner

We could bundle a command line tool that would allows you to compile the protobuf schema into a javascript file. I.e.

// schema.proto
message Test {
  required string hello = 1
}

Then run

protocol-buffers schema.proto > messages.js

Then in your js code you'll be able to

var messages = require('./messages')
var buf = messages.Test.encode({hello:'world'})
console.log(buf)

@brendan-ward
Copy link
Contributor

👍 on CLI for compiling protobuf schemas

We'd like to use this in the browser someday too, looks like compiling in advance using this approach would work for that case.

@mafintosh
Copy link
Owner

@brendan-ward i'm working on it. you can use brfs to browserify your schemas today though :)

@natevw
Copy link
Author

natevw commented Dec 17, 2014

Thanks, yeah…JS file generation would be great, and I think exactly what I'm requesting on #19 (I think I might have filed that as you were replying on this thread here ;-)

@mkamioner
Copy link
Contributor

Any update on this?

@vweevers
Copy link

A fun thought (just the fact that it's possible): one could also generate the Javascript code in a browserified Service Worker. Something like:

const protobuf = require('protocol-buffers')
const schema = require('fs').readFileSync(__dirname+'/schema.proto', 'utf8')

self.addEventListener('fetch', function (event) {
  const req = event.request

  if (/\/schema\.js$/.test(req.url)) {
    const js = protobuf.generate(schema)

    event.respondWith(new Response(js, {
      status: 200,
      statusText: 'OK',
      headers: {
        'Content-Type': 'application/javascript',
        'Content-Length': js.length.toString()
      }
    }))
  }
})

With some more code to cache the result and maybe wrap it. Then to use it somewhere in your app, you'd include it like <script src="schema.js"> or with some async module loader. Apart from the missing protobuf.generate() function, this works in Chrome today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants