1Protocol Buffers - Google's data interchange format 2=================================================== 3 4[![Build Status](https://travis-ci.org/google/protobuf.svg?branch=master)](https://travis-ci.org/google/protobuf) 5 6Copyright 2008 Google Inc. 7 8This directory contains the JavaScript Protocol Buffers runtime library. 9 10The library is currently compatible with: 11 121. CommonJS-style imports (eg. `var protos = require('my-protos');`) 132. Closure-style imports (eg. `goog.require('my.package.MyProto');`) 14 15Support for ES6-style imports is not implemented yet. Browsers can 16be supported by using Browserify, webpack, Closure Compiler, etc. to 17resolve imports at compile time. 18 19To use Protocol Buffers with JavaScript, you need two main components: 20 211. The protobuf runtime library. You can install this with 22 `npm install google-protobuf`, or use the files in this directory. 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/google/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/google/protobuf/releases](https://github.com/google/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/google/protobuf/blob/master/src/README.md). 39 40Once you have `protoc` compiled, you can run the tests by typing: 41 42 $ cd js 43 $ npm install 44 $ npm test 45 46 # If your protoc is somewhere else than ../src/protoc, instead do this. 47 # But make sure your protoc is the same version as this (or compatible)! 48 $ PROTOC=/usr/local/bin/protoc npm test 49 50This will run two separate copies of the tests: one that uses 51Closure Compiler style imports and one that uses CommonJS imports. 52You can see all the CommonJS files in `commonjs_out/`. 53If all of these tests pass, you know you have a working setup. 54 55 56Using Protocol Buffers in your own project 57========================================== 58 59To use Protocol Buffers in your own project, you need to integrate 60the Protocol Compiler into your build system. The details are a 61little different depending on whether you are using Closure imports 62or CommonJS imports: 63 64Closure Imports 65--------------- 66 67If you want to use Closure imports, your build should run a command 68like this: 69 70 $ protoc --js_out=library=myproto_libs,binary:. messages.proto base.proto 71 72For Closure imports, `protoc` will generate a single output file 73(`myproto_libs.js` in this example). The generated file will `goog.provide()` 74all of the types defined in your .proto files. For example, for the unit 75tests the generated files contain many `goog.provide` statements like: 76 77 goog.provide('proto.google.protobuf.DescriptorProto'); 78 goog.provide('proto.google.protobuf.DescriptorProto.ExtensionRange'); 79 goog.provide('proto.google.protobuf.DescriptorProto.ReservedRange'); 80 goog.provide('proto.google.protobuf.EnumDescriptorProto'); 81 goog.provide('proto.google.protobuf.EnumOptions'); 82 83The generated code will also `goog.require()` many types in the core library, 84and they will require many types in the Google Closure library. So make sure 85that your `goog.provide()` / `goog.require()` setup can find all of your 86generated code, the core library `.js` files in this directory, and the 87Google Closure library itself. 88 89Once you've done this, you should be able to import your types with 90statements like: 91 92 goog.require('proto.my.package.MyMessage'); 93 94 var message = proto.my.package.MyMessage(); 95 96CommonJS imports 97---------------- 98 99If you want to use CommonJS imports, your build should run a command 100like this: 101 102 $ protoc --js_out=import_style=commonjs,binary:. messages.proto base.proto 103 104For CommonJS imports, `protoc` will spit out one file per input file 105(so `messages_pb.js` and `base_pb.js` in this example). The generated 106code will depend on the core runtime, which should be in a file called 107`google-protobuf.js`. If you are installing from `npm`, this file should 108already be built and available. If you are running from GitHub, you need 109to build it first by running: 110 111 $ gulp dist 112 113Once you've done this, you should be able to import your types with 114statements like: 115 116 var messages = require('./messages_pb'); 117 118 var message = new messages.MyMessage(); 119 120The `--js_out` flag 121------------------- 122 123The syntax of the `--js_out` flag is: 124 125 --js_out=[OPTIONS:]output_dir 126 127Where `OPTIONS` are separated by commas. Options are either `opt=val` or 128just `opt` (for options that don't take a value). The available options 129are specified and documented in the `GeneratorOptions` struct in 130[src/google/protobuf/compiler/js/js_generator.h](https://github.com/google/protobuf/blob/master/src/google/protobuf/compiler/js/js_generator.h#L53). 131 132Some examples: 133 134- `--js_out=library=myprotos_lib.js,binary:.`: this contains the options 135 `library=myprotos.lib.js` and `binary` and outputs to the current directory. 136 The `import_style` option is left to the default, which is `closure`. 137- `--js_out=import_style=commonjs,binary:protos`: this contains the options 138 `import_style=commonjs` and `binary` and outputs to the directory `protos`. 139 140API 141=== 142 143The API is not well-documented yet. Here is a quick example to give you an 144idea of how the library generally works: 145 146 var message = new MyMessage(); 147 148 message.setName("John Doe"); 149 message.setAge(25); 150 message.setPhoneNumbers(["800-555-1212", "800-555-0000"]); 151 152 // Serializes to a UInt8Array. 153 bytes = message.serializeBinary(); 154 155 var message2 = new MyMessage(); 156 message2.deserializeBinary(bytes); 157 158For more examples, see the tests. You can also look at the generated code 159to see what methods are defined for your generated messages. 160