Protobuf with JS

361 views
Skip to first unread message

Dylan Taft

unread,
Jul 11, 2017, 1:14:28 AM7/11/17
to Protocol Buffers
I'm having a heck of a time getting this to work.
Let me back up, I _did_ get it to work, but I don't know if it was the right process.

I am not using node.js or anything.
The documentation https://github.com/google/protobuf/blob/master/js/README.md isn't super clear, it seems to assume familiarity with closure.  I have no familiarity, this was pieced together from Closure's website.
I've had no problems with ProtoBuf in Java or C++, JS is throwing me for a loop.

To keep it short, I'll put what I did that worked first.

1) This DID work, but it doesn't seem _right_
I grabbed https://github.com/google/protobuf/releases the javascript release
I copied js to a subfolder in my project's path.

dylanetaft@dylanetaft-iMac:~/Projects/RClient$ ./lib/closure-library/closure/bin/build/closurebuilder.py --root=. --input client.js -o script > out.js
./lib/closure-library/closure/bin/build/closurebuilder.py: Scanning paths...
./lib/closure-library/closure/bin/build/closurebuilder.py: 1600 sources scanned.
./lib/closure-library/closure/bin/build/closurebuilder.py: Building dependency tree..
Traceback (most recent call last):
  File "./lib/closure-library/closure/bin/build/closurebuilder.py", line 293, in <module>
    main()
  File "./lib/closure-library/closure/bin/build/closurebuilder.py", line 229, in main
    tree = depstree.DepsTree(sources)
  File "/home/dylanetaft/Projects/RClient/lib/closure-library/closure/bin/build/depstree.py", line 56, in __init__
    raise NamespaceNotFoundError(require, source)
depstree.NamespaceNotFoundError: Namespace "proto.jspb.test.TestProto3" never provided. Required in PathSource lib/protobuf/proto3_test.js

If I delete *test* in "build" and "js" - it works.  I can use protobuf in my JavaScript scripts.  
This doesn't seem like a "normal" way to include it?  What is the _right_ way without Node.JS?


2) The first thing I tried was I downloaded https://unpkg.com/google-...@3.3.0 to protobuf.js - this doesn't work

dylanetaft@dylanetaft-iMac:~/Projects/RClient$ ./lib/closure-library/closure/bin/build/closurebuilder.py --root=. --input client.js -o script > out.js
./lib/closure-library/closure/bin/build/closurebuilder.py: Scanning paths...
./lib/closure-library/closure/bin/build/closurebuilder.py: 1572 sources scanned.
./lib/closure-library/closure/bin/build/closurebuilder.py: Building dependency tree..
Traceback (most recent call last):
  File "./lib/closure-library/closure/bin/build/closurebuilder.py", line 293, in <module>
    main()
  File "./lib/closure-library/closure/bin/build/closurebuilder.py", line 229, in main
    tree = depstree.DepsTree(sources)
  File "/home/dylanetaft/Projects/RClient/lib/closure-library/closure/bin/build/depstree.py", line 56, in __init__
    raise NamespaceNotFoundError(require, source)
depstree.NamespaceNotFoundError: Namespace "jspb.BinaryReader" never provided. Required in PathSource rprotocol.js

No dice here.


I'm not using npm because I'm not using node.js?

Do I need to use closurebuilder.py to combine everything into a single js? 
I see also depswriter.py - but that seems to recursively search and build a js file I can include to include every single js under the path I am in.

There's also a closure compiler?

What if at this stage in testing I don't want to compile, or combine to a single js?




Dylan Taft

unread,
Jul 11, 2017, 2:09:47 AM7/11/17
to Protocol Buffers
These files seem to be all that is required for closurebuilder.py to do it's thing.

arith.js  constants.js  decoder.js  encoder.js  map.js  message.js  reader.js  utils.js  writer.js

If there isn't a better way to do it - I can submit pull req to https://github.com/google/protobuf/blob/master/js/README.md
"The generated code will also goog.require() many types in the core library, and they will require many types in the Google Closure library. So make sure that your goog.provide() / goog.require() setup can find all of your generated code, the core library .js files in this directory, and the Google Closure library itself."
Specifically this verbiage is confusing.  The best I can come up with is using closurebuilder.py to find the dependencies and output code, or compile it with closure.

I can't find anywhere what Closure's "search path" is, or how it handles dependency walking for including files.  


Dylan Taft

unread,
Jul 11, 2017, 9:23:06 AM7/11/17
to Protocol Buffers
I keep answering my own questions.
https://developers.google.com/closure/library/docs/depswriter

Closure doesn't handle dependency walking in the browser, you either compile your scripts for prod or build a dependency file which depswriter can help with.
Perfect.

I am going to digest this a bit, type up brief instructions, and submit a pull request for ProtoBuf's readme documentation for JS.
Reply all
Reply to author
Forward
0 new messages