blob: 3635028fd5d7d6e628704ad99e4f6f3cdb4d6c50 [file] [log] [blame] [view]
Josh Haberman5195b7f2016-02-05 18:05:11 -08001Protocol Buffers - Google's data interchange format
2===================================================
Josh Habermane9cf31e2015-12-21 15:18:17 -08003
Feng Xiao2dcd6ae2018-07-22 17:14:24 -07004[![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/linux-javascript.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fubuntu%2Fjavascript%2Fcontinuous) [![Build status](https://storage.googleapis.com/protobuf-kokoro-results/status-badge/macos-javascript.png)](https://fusion.corp.google.com/projectanalysis/current/KOKORO/prod:protobuf%2Fgithub%2Fmaster%2Fmacos%2Fjavascript%2Fcontinuous)
Josh Habermane9cf31e2015-12-21 15:18:17 -08005
Josh Haberman5195b7f2016-02-05 18:05:11 -08006Copyright 2008 Google Inc.
Josh Habermane9cf31e2015-12-21 15:18:17 -08007
Josh Haberman5195b7f2016-02-05 18:05:11 -08008This directory contains the JavaScript Protocol Buffers runtime library.
Josh Habermane9cf31e2015-12-21 15:18:17 -08009
Josh Haberman5195b7f2016-02-05 18:05:11 -080010The 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
Xiang Daie4794102019-02-21 11:28:50 +080022 `npm install google-protobuf`, or use the files in this directory.
23 If npm is not being used, as of 3.3.0, the files needed are located in binary subdirectory;
dylanetaft36fcc2a2017-07-15 01:52:07 -040024 arith.js, constants.js, decoder.js, encoder.js, map.js, message.js, reader.js, utils.js, writer.js
Josh Haberman5195b7f2016-02-05 18:05:11 -0800252. The Protocol Compiler `protoc`. This translates `.proto` files
26 into `.js` files. The compiler is not currently available via
Josh Haberman7726cd22016-02-16 15:29:49 -080027 npm, but you can download a pre-built binary
Feng Xiaoafe98de2018-08-22 11:55:30 -070028 [on GitHub](https://github.com/protocolbuffers/protobuf/releases)
Josh Haberman7726cd22016-02-16 15:29:49 -080029 (look for the `protoc-*.zip` files under **Downloads**).
Josh Haberman5195b7f2016-02-05 18:05:11 -080030
31
32Setup
33=====
34
Josh Habermanc348af22016-02-17 17:06:46 -080035First, obtain the Protocol Compiler. The easiest way is to download
Feng Xiaoafe98de2018-08-22 11:55:30 -070036a pre-built binary from [https://github.com/protocolbuffers/protobuf/releases](https://github.com/protocolbuffers/protobuf/releases).
Josh Haberman5195b7f2016-02-05 18:05:11 -080037
Josh Habermanc348af22016-02-17 17:06:46 -080038If you want, you can compile `protoc` from source instead. To do this
39follow the instructions in [the top-level
Feng Xiaoafe98de2018-08-22 11:55:30 -070040README](https://github.com/protocolbuffers/protobuf/blob/master/src/README.md).
Josh Haberman5195b7f2016-02-05 18:05:11 -080041
42Once you have `protoc` compiled, you can run the tests by typing:
43
Josh Habermanc348af22016-02-17 17:06:46 -080044 $ cd js
Josh Haberman5195b7f2016-02-05 18:05:11 -080045 $ npm install
46 $ npm test
47
Josh Haberman24c54242016-02-19 11:46:03 -080048 # If your protoc is somewhere else than ../src/protoc, instead do this.
49 # But make sure your protoc is the same version as this (or compatible)!
50 $ PROTOC=/usr/local/bin/protoc npm test
51
Josh Haberman5195b7f2016-02-05 18:05:11 -080052This will run two separate copies of the tests: one that uses
53Closure Compiler style imports and one that uses CommonJS imports.
54You can see all the CommonJS files in `commonjs_out/`.
55If all of these tests pass, you know you have a working setup.
56
57
58Using Protocol Buffers in your own project
59==========================================
60
61To use Protocol Buffers in your own project, you need to integrate
62the Protocol Compiler into your build system. The details are a
63little different depending on whether you are using Closure imports
64or CommonJS imports:
65
66Closure Imports
67---------------
68
69If you want to use Closure imports, your build should run a command
70like this:
71
72 $ protoc --js_out=library=myproto_libs,binary:. messages.proto base.proto
73
74For Closure imports, `protoc` will generate a single output file
75(`myproto_libs.js` in this example). The generated file will `goog.provide()`
76all of the types defined in your .proto files. For example, for the unit
77tests the generated files contain many `goog.provide` statements like:
78
79 goog.provide('proto.google.protobuf.DescriptorProto');
80 goog.provide('proto.google.protobuf.DescriptorProto.ExtensionRange');
81 goog.provide('proto.google.protobuf.DescriptorProto.ReservedRange');
82 goog.provide('proto.google.protobuf.EnumDescriptorProto');
83 goog.provide('proto.google.protobuf.EnumOptions');
84
85The generated code will also `goog.require()` many types in the core library,
86and they will require many types in the Google Closure library. So make sure
87that your `goog.provide()` / `goog.require()` setup can find all of your
88generated code, the core library `.js` files in this directory, and the
89Google Closure library itself.
90
91Once you've done this, you should be able to import your types with
92statements like:
93
94 goog.require('proto.my.package.MyMessage');
95
96 var message = proto.my.package.MyMessage();
97
dylanetaft36fcc2a2017-07-15 01:52:07 -040098If unfamiliar with Closure or it's compiler, consider reviewing Closure documentation
99https://developers.google.com/closure/library/docs/tutorial
100https://developers.google.com/closure/library/docs/closurebuilder
101https://developers.google.com/closure/library/docs/depswriter
102At a high level, closurebuilder.py can walk dependencies, and compile your code, and all dependencies for Protobuf into a single .js file. Using depsbuilder.py to generate a dependency file can also be considered for non-production dev environments.
103
Josh Haberman5195b7f2016-02-05 18:05:11 -0800104CommonJS imports
105----------------
106
107If you want to use CommonJS imports, your build should run a command
108like this:
109
110 $ protoc --js_out=import_style=commonjs,binary:. messages.proto base.proto
111
112For CommonJS imports, `protoc` will spit out one file per input file
113(so `messages_pb.js` and `base_pb.js` in this example). The generated
114code will depend on the core runtime, which should be in a file called
115`google-protobuf.js`. If you are installing from `npm`, this file should
116already be built and available. If you are running from GitHub, you need
117to build it first by running:
118
119 $ gulp dist
120
121Once you've done this, you should be able to import your types with
122statements like:
123
124 var messages = require('./messages_pb');
125
126 var message = new messages.MyMessage();
127
Josh Haberman24c54242016-02-19 11:46:03 -0800128The `--js_out` flag
129-------------------
130
131The syntax of the `--js_out` flag is:
132
133 --js_out=[OPTIONS:]output_dir
134
135Where `OPTIONS` are separated by commas. Options are either `opt=val` or
136just `opt` (for options that don't take a value). The available options
137are specified and documented in the `GeneratorOptions` struct in
Feng Xiaoafe98de2018-08-22 11:55:30 -0700138[src/google/protobuf/compiler/js/js_generator.h](https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/compiler/js/js_generator.h#L53).
Josh Haberman24c54242016-02-19 11:46:03 -0800139
140Some examples:
141
142- `--js_out=library=myprotos_lib.js,binary:.`: this contains the options
143 `library=myprotos.lib.js` and `binary` and outputs to the current directory.
144 The `import_style` option is left to the default, which is `closure`.
145- `--js_out=import_style=commonjs,binary:protos`: this contains the options
146 `import_style=commonjs` and `binary` and outputs to the directory `protos`.
Peter Marton1062d982018-02-08 14:07:15 -0800147 `import_style=commonjs_strict` doesn't expose the output on the global scope.
Josh Haberman24c54242016-02-19 11:46:03 -0800148
Josh Haberman5195b7f2016-02-05 18:05:11 -0800149API
150===
151
152The API is not well-documented yet. Here is a quick example to give you an
153idea of how the library generally works:
154
155 var message = new MyMessage();
156
157 message.setName("John Doe");
158 message.setAge(25);
159 message.setPhoneNumbers(["800-555-1212", "800-555-0000"]);
160
161 // Serializes to a UInt8Array.
Stefan Huber44daa592017-10-24 14:01:22 +0200162 var bytes = message.serializeBinary();
Josh Haberman5195b7f2016-02-05 18:05:11 -0800163
sheffatguidance9a11ab42016-07-29 12:53:59 -0500164 var message2 = MyMessage.deserializeBinary(bytes);
Josh Haberman5195b7f2016-02-05 18:05:11 -0800165
166For more examples, see the tests. You can also look at the generated code
167to see what methods are defined for your generated messages.