1Protocol Buffers - Google's data interchange format 2=================================================== 3 4Copyright 2008 Google Inc. 5 6This directory contains the JavaScript Protocol Buffers runtime library. 7 8The library is currently compatible with: 9 101. CommonJS-style imports (eg. `var protos = require('my-protos');`) 112. Closure-style imports (eg. `goog.require('my.package.MyProto');`) 12 13Support for ES6-style imports is not implemented yet. Browsers can 14be supported by using Browserify, webpack, Closure Compiler, etc. to 15resolve imports at compile time. 16 17To use Protocol Buffers with JavaScript, you need two main components: 18 191. The protobuf runtime library. You can install this with 20 `npm install google-protobuf`, or use the files in this directory. 21 If npm is not being used, as of 3.3.0, the files needed are located in binary subdirectory; 22 arith.js, constants.js, decoder.js, encoder.js, map.js, message.js, reader.js, utils.js, writer.js 232. The Protocol Compiler `protoc`. This translates `.proto` files 24 into `.js` files. The compiler is not currently available via 25 npm, but you can download a pre-built binary 26 [on GitHub](https://github.com/protocolbuffers/protobuf/releases) 27 (look for the `protoc-*.zip` files under **Downloads**). 28 29 30Setup 31===== 32 33First, obtain the Protocol Compiler. The easiest way is to download 34a pre-built binary from [https://github.com/protocolbuffers/protobuf/releases](https://github.com/protocolbuffers/protobuf/releases). 35 36If you want, you can compile `protoc` from source instead. To do this 37follow the instructions in [the top-level 38README](https://github.com/protocolbuffers/protobuf/blob/master/src/README.md). 39 40Once you have `protoc` compiled, you can run the tests provided along with our project to examine whether it can run successfully. In order to do this, you should download the Protocol Buffer source code from the release page with the link above. Then extract the source code and navigate to the folder named `js` containing a `package.json` file and a series of test files. In this folder, you can run the commands below to run the tests automatically. 41 42 $ npm install 43 $ npm test 44 45 # If your protoc is somewhere else than ../src/protoc, instead do this. 46 # But make sure your protoc is the same version as this (or compatible)! 47 $ PROTOC=/usr/local/bin/protoc npm test 48 49This will run two separate copies of the tests: one that uses 50Closure Compiler style imports and one that uses CommonJS imports. 51You can see all the CommonJS files in `commonjs_out/`. 52If all of these tests pass, you know you have a working setup. 53 54 55Using Protocol Buffers in your own project 56========================================== 57 58To use Protocol Buffers in your own project, you need to integrate 59the Protocol Compiler into your build system. The details are a 60little different depending on whether you are using Closure imports 61or CommonJS imports: 62 63Closure Imports 64--------------- 65 66If you want to use Closure imports, your build should run a command 67like this: 68 69 $ protoc --js_out=library=myproto_libs,binary:. messages.proto base.proto 70 71For Closure imports, `protoc` will generate a single output file 72(`myproto_libs.js` in this example). The generated file will `goog.provide()` 73all of the types defined in your .proto files. For example, for the unit 74tests the generated files contain many `goog.provide` statements like: 75 76 goog.provide('proto.google.protobuf.DescriptorProto'); 77 goog.provide('proto.google.protobuf.DescriptorProto.ExtensionRange'); 78 goog.provide('proto.google.protobuf.DescriptorProto.ReservedRange'); 79 goog.provide('proto.google.protobuf.EnumDescriptorProto'); 80 goog.provide('proto.google.protobuf.EnumOptions'); 81 82The generated code will also `goog.require()` many types in the core library, 83and they will require many types in the Google Closure library. So make sure 84that your `goog.provide()` / `goog.require()` setup can find all of your 85generated code, the core library `.js` files in this directory, and the 86Google Closure library itself. 87 88Once you've done this, you should be able to import your types with 89statements like: 90 91 goog.require('proto.my.package.MyMessage'); 92 93 var message = proto.my.package.MyMessage(); 94 95If unfamiliar with Closure or its compiler, consider reviewing 96[Closure documentation](https://developers.google.com/closure/library). 97 98CommonJS imports 99---------------- 100 101If you want to use CommonJS imports, your build should run a command 102like this: 103 104 $ protoc --js_out=import_style=commonjs,binary:. messages.proto base.proto 105 106For CommonJS imports, `protoc` will spit out one file per input file 107(so `messages_pb.js` and `base_pb.js` in this example). The generated 108code will depend on the core runtime, which should be in a file called 109`google-protobuf.js`. If you are installing from `npm`, this file should 110already be built and available. If you are running from GitHub, you need 111to build it first by running: 112 113 $ gulp dist 114 115Once you've done this, you should be able to import your types with 116statements like: 117 118 var messages = require('./messages_pb'); 119 120 var message = new messages.MyMessage(); 121 122The `--js_out` flag 123------------------- 124 125The syntax of the `--js_out` flag is: 126 127 --js_out=[OPTIONS:]output_dir 128 129Where `OPTIONS` are separated by commas. Options are either `opt=val` or 130just `opt` (for options that don't take a value). The available options 131are specified and documented in the `GeneratorOptions` struct in 132[src/google/protobuf/compiler/js/js_generator.h](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/compiler/js/js_generator.h#L53). 133 134Some examples: 135 136- `--js_out=library=myprotos_lib.js,binary:.`: this contains the options 137 `library=myprotos.lib.js` and `binary` and outputs to the current directory. 138 The `import_style` option is left to the default, which is `closure`. 139- `--js_out=import_style=commonjs,binary:protos`: this contains the options 140 `import_style=commonjs` and `binary` and outputs to the directory `protos`. 141 `import_style=commonjs_strict` doesn't expose the output on the global scope. 142 143API 144=== 145 146The API is not well-documented yet. Here is a quick example to give you an 147idea of how the library generally works: 148 149 var message = new MyMessage(); 150 151 message.setName("John Doe"); 152 message.setAge(25); 153 message.setPhoneNumbers(["800-555-1212", "800-555-0000"]); 154 155 // Serializes to a UInt8Array. 156 var bytes = message.serializeBinary(); 157 158 var message2 = MyMessage.deserializeBinary(bytes); 159 160For more examples, see the tests. You can also look at the generated code 161to see what methods are defined for your generated messages. 162