├── tests ├── importRoot │ ├── file3.proto │ ├── file1.proto │ └── file2.proto ├── import_common.proto ├── example1u.proto ├── T139.proto ├── proto2js │ ├── Foo.proto │ ├── Bar.proto │ └── Bar.json ├── import_a.proto ├── import_b.proto ├── dupimport │ ├── common.proto │ ├── dep1.proto │ ├── dep2.proto │ └── main.proto ├── packed.proto ├── field_name_same_as_package │ ├── sub.proto │ └── main.proto ├── proto3.proto ├── import_a_single_quote.proto ├── string_single_quote.proto ├── oneof.proto ├── example1.proto ├── imports-weak.proto ├── setarray.proto ├── example2.proto ├── example4.proto ├── toplevel.proto ├── example3.proto ├── imports.proto ├── comments.proto ├── inner.proto ├── negid.proto ├── optional.proto ├── imports-toplevel.proto ├── PingExample.proto ├── T263.proto ├── x64.proto ├── groups.proto ├── repeated.proto ├── camelcase.proto ├── extend.proto ├── protobufnet.proto ├── options.proto ├── numberformats.proto ├── example5.proto ├── suite.html ├── options.json ├── complex.proto ├── annotations.proto ├── nodeunit-browser │ ├── LICENSE │ └── nodeunit.css ├── services.js ├── custom-options.proto ├── bench.js ├── extend.json ├── imports.json ├── complex.json └── custom-options.json ├── .npmignore ├── index.js ├── examples ├── protoify │ ├── .gitignore │ ├── package.json │ ├── json.proto │ ├── README.md │ ├── test.js │ ├── json.json │ ├── json.js │ └── index.js └── websocket │ ├── .gitignore │ ├── www │ ├── example.proto │ └── index.html │ ├── README.md │ ├── package.json │ └── server.js ├── donate.png ├── .gitignore ├── protobuf.png ├── .travis.yml ├── dist ├── protobuf.min.js.gz ├── protobuf-light.min.js.gz └── README.md ├── bin └── pbjs ├── jsdoc.json ├── bower.json ├── src ├── bower.json ├── ProtoBuf │ ├── Builder │ │ ├── Enum.js │ │ └── Service.js │ ├── Reflect │ │ ├── Message │ │ │ ├── OneOf.js │ │ │ └── ExtensionField.js │ │ ├── Extension.js │ │ ├── Enum │ │ │ └── Value.js │ │ ├── Service │ │ │ ├── Method.js │ │ │ └── RPCMethod.js │ │ ├── Service.js │ │ ├── T.js │ │ ├── Enum.js │ │ └── Namespace.js │ ├── DotProto.js │ ├── Lang.js │ ├── Reflect.js │ ├── Util.js │ ├── DotProto │ │ └── Tokenizer.js │ └── Map.js ├── google │ └── protobuf │ │ └── README.md ├── wrap.js └── es5.js ├── docs ├── scripts │ ├── linenumber.js │ └── prettify │ │ └── lang-css.js ├── styles │ ├── prettify-jsdoc.css │ ├── prettify-tomorrow.css │ └── jsdoc-default.css ├── index.html ├── ProtoBuf.DotProto.html ├── ProtoBuf.Reflect.html └── ProtoBuf.Reflect.Extension.html ├── cli ├── pbjs │ ├── targets │ │ ├── commonjs.js │ │ ├── amd.js │ │ ├── js.js │ │ ├── json.js │ │ └── proto.js │ ├── sources │ │ ├── json.js │ │ ├── proto.js │ │ └── binary.js │ └── util.js └── pbjs.js ├── scripts └── build.js ├── package.json └── README.md /tests/importRoot/file3.proto: -------------------------------------------------------------------------------- 1 | message Test {} 2 | -------------------------------------------------------------------------------- /tests/import_common.proto: -------------------------------------------------------------------------------- 1 | message Common { 2 | } 3 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | .idea 2 | node_modules/ 3 | npm-debug.log 4 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require("./dist/protobuf.js"); 2 | -------------------------------------------------------------------------------- /examples/protoify/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /tests/importRoot/file1.proto: -------------------------------------------------------------------------------- 1 | import "importRoot/file2.proto"; 2 | -------------------------------------------------------------------------------- /tests/importRoot/file2.proto: -------------------------------------------------------------------------------- 1 | import "importRoot/file3.proto"; 2 | -------------------------------------------------------------------------------- /donate.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/englercj/ProtoBuf.js/HEAD/donate.png -------------------------------------------------------------------------------- /examples/websocket/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | npm-debug.log 3 | www/*.js 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | npm-debug.log 3 | .idea/ 4 | sandbox/ 5 | v8.log 6 | -------------------------------------------------------------------------------- /protobuf.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/englercj/ProtoBuf.js/HEAD/protobuf.png -------------------------------------------------------------------------------- /tests/example1u.proto: -------------------------------------------------------------------------------- 1 | message Test1u { 2 | required uint32 a = 1; 3 | } 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 0.12 4 | - 4.2 5 | sudo: false 6 | -------------------------------------------------------------------------------- /examples/websocket/www/example.proto: -------------------------------------------------------------------------------- 1 | message Message { 2 | required string text = 1; 3 | } 4 | -------------------------------------------------------------------------------- /dist/protobuf.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/englercj/ProtoBuf.js/HEAD/dist/protobuf.min.js.gz -------------------------------------------------------------------------------- /tests/T139.proto: -------------------------------------------------------------------------------- 1 | message T139 { 2 | required int32 a = 1; 3 | required uint32 b = 2; 4 | } 5 | -------------------------------------------------------------------------------- /tests/proto2js/Foo.proto: -------------------------------------------------------------------------------- 1 | // Foo.proto 2 | message Foo { 3 | required string fizz = 1; 4 | } 5 | -------------------------------------------------------------------------------- /bin/pbjs: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | process.exit(require(__dirname+"/../cli/pbjs.js").main(process.argv)); 3 | -------------------------------------------------------------------------------- /dist/protobuf-light.min.js.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/englercj/ProtoBuf.js/HEAD/dist/protobuf-light.min.js.gz -------------------------------------------------------------------------------- /tests/import_a.proto: -------------------------------------------------------------------------------- 1 | import "import_common.proto"; 2 | 3 | message A { 4 | optional Common common = 1; 5 | } 6 | -------------------------------------------------------------------------------- /tests/import_b.proto: -------------------------------------------------------------------------------- 1 | import "import_common.proto"; 2 | 3 | message B { 4 | optional Common common = 1; 5 | } 6 | -------------------------------------------------------------------------------- /tests/dupimport/common.proto: -------------------------------------------------------------------------------- 1 | package main; 2 | 3 | message Common { 4 | optional string commonField = 1; 5 | } 6 | -------------------------------------------------------------------------------- /tests/packed.proto: -------------------------------------------------------------------------------- 1 | message Message { 2 | repeated int32 a = 1 [packed = true]; 3 | repeated int32 b = 2; 4 | } 5 | -------------------------------------------------------------------------------- /tests/field_name_same_as_package/sub.proto: -------------------------------------------------------------------------------- 1 | package main.sub; 2 | 3 | message Sub { 4 | optional string test1 = 1; 5 | } 6 | -------------------------------------------------------------------------------- /tests/proto3.proto: -------------------------------------------------------------------------------- 1 | syntax = "proto3"; 2 | 3 | package test; 4 | 5 | message Foo { 6 | int32 optional_int32 = 1; 7 | } 8 | -------------------------------------------------------------------------------- /tests/import_a_single_quote.proto: -------------------------------------------------------------------------------- 1 | import 'import_common.proto'; 2 | 3 | message A { 4 | optional Common common = 1; 5 | } 6 | -------------------------------------------------------------------------------- /tests/string_single_quote.proto: -------------------------------------------------------------------------------- 1 | message TestSingleQuoteString { 2 | required string a = 1 [ default = 'hello world' ]; 3 | } 4 | -------------------------------------------------------------------------------- /tests/oneof.proto: -------------------------------------------------------------------------------- 1 | message MyOneOf { 2 | oneof my_oneof { 3 | uint32 id = 1; 4 | string name = 2; 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /tests/example1.proto: -------------------------------------------------------------------------------- 1 | message Test1 { 2 | required int32 a = 1; 3 | } 4 | 5 | // Test1 should encode to "08 96 01" with Test1#a=150 6 | -------------------------------------------------------------------------------- /tests/imports-weak.proto: -------------------------------------------------------------------------------- 1 | package My; 2 | 3 | import weak "example1.proto"; 4 | 5 | message Test2 { 6 | required Test1 test1 = 1; 7 | } -------------------------------------------------------------------------------- /tests/dupimport/dep1.proto: -------------------------------------------------------------------------------- 1 | package main; 2 | 3 | import "common.proto"; 4 | 5 | message Dep1 { 6 | optional main.Common test1 = 1; 7 | } 8 | -------------------------------------------------------------------------------- /tests/setarray.proto: -------------------------------------------------------------------------------- 1 | message Outer { 2 | repeated Inner inners = 4; 3 | } 4 | 5 | message Inner { 6 | required string str = 1; 7 | } 8 | -------------------------------------------------------------------------------- /tests/example2.proto: -------------------------------------------------------------------------------- 1 | message Test2 { 2 | required string b = 2; 3 | } 4 | 5 | // Test2 should encode to "12 07 74 65 73 74 69 6e 67" with Test2#b="testing" 6 | -------------------------------------------------------------------------------- /tests/field_name_same_as_package/main.proto: -------------------------------------------------------------------------------- 1 | package main; 2 | 3 | import "sub.proto"; 4 | 5 | message MainMessage { 6 | optional main.sub.Sub sub = 1; 7 | } 8 | -------------------------------------------------------------------------------- /tests/example4.proto: -------------------------------------------------------------------------------- 1 | message Test4 { 2 | repeated int32 d = 4 [packed=true]; 3 | } 4 | 5 | // Test4 should encode to "<22 06 03 8E 02 9E A7 05>" with d=[3, 270, 86942] 6 | -------------------------------------------------------------------------------- /tests/dupimport/dep2.proto: -------------------------------------------------------------------------------- 1 | package main; 2 | 3 | import "dep1.proto"; 4 | 5 | message Dep2 { 6 | optional main.Dep1 test1 = 1; 7 | optional main.Common test2 = 2; 8 | } 9 | -------------------------------------------------------------------------------- /tests/toplevel.proto: -------------------------------------------------------------------------------- 1 | package My; 2 | 3 | enum MyEnum { 4 | ONE = 1; 5 | TWO = 2; 6 | } 7 | 8 | message Test { 9 | required MyEnum num = 1 [default=ONE]; 10 | } 11 | -------------------------------------------------------------------------------- /tests/proto2js/Bar.proto: -------------------------------------------------------------------------------- 1 | // Bar.proto 2 | import "Foo.proto"; 3 | 4 | // really extend Bar but that produces an empty constructor. 5 | extend Foo { 6 | required string buzz = 2; 7 | } 8 | -------------------------------------------------------------------------------- /tests/example3.proto: -------------------------------------------------------------------------------- 1 | message Test1 { 2 | required int32 a = 1; 3 | } 4 | 5 | message Test3 { 6 | required Test1 c = 3; 7 | } 8 | 9 | // Test3 should encode to "1a 03 08 96 01" with Test1#a=150 10 | -------------------------------------------------------------------------------- /tests/imports.proto: -------------------------------------------------------------------------------- 1 | package My; 2 | 3 | import public "example1.proto"; 4 | import "example2.proto"; 5 | 6 | message Test3 { 7 | required Test1 test1 = 1; 8 | required .Test2 test2 = 2; 9 | } 10 | -------------------------------------------------------------------------------- /tests/comments.proto: -------------------------------------------------------------------------------- 1 | // single line comment 2 | 3 | /** 4 | * Naranjas. 5 | */ 6 | message TestC { // one more 7 | required /***** uncommon block ****/int32 a = 1; // another more 8 | } 9 | 10 | // the end. -------------------------------------------------------------------------------- /tests/inner.proto: -------------------------------------------------------------------------------- 1 | message Foo { 2 | required Bar bar = 1; 3 | required Baz baz = 2; 4 | } 5 | message Bar { 6 | required string blah = 1; 7 | } 8 | message Baz { 9 | required string blah = 1; 10 | } 11 | -------------------------------------------------------------------------------- /tests/negid.proto: -------------------------------------------------------------------------------- 1 | message Test { 2 | required LobbyType type = 1 [default=INVALID]; 3 | 4 | enum LobbyType { 5 | INVALID = -1; 6 | MATCH = 0; 7 | PRACTICE = 1; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tests/optional.proto: -------------------------------------------------------------------------------- 1 | message Test1 { 2 | optional int32 a = 1; 3 | optional Test2 b = 2; 4 | } 5 | 6 | message Test2 { 7 | optional int32 c = 1; 8 | optional Test3 d = 2; 9 | } 10 | 11 | message Test3 { 12 | } 13 | -------------------------------------------------------------------------------- /jsdoc.json: -------------------------------------------------------------------------------- 1 | { 2 | "tags": { 3 | "allowUnknownTags": true 4 | }, 5 | "source": { 6 | "include": ["dist/protobuf.js"] 7 | }, 8 | "opts": { 9 | "destination": "docs" 10 | }, 11 | "plugins": ["plugins/markdown"] 12 | } -------------------------------------------------------------------------------- /tests/imports-toplevel.proto: -------------------------------------------------------------------------------- 1 | import "toplevel.proto"; 2 | 3 | package My; 4 | 5 | enum MyEnum1 { 6 | ONE = 1; 7 | TWO = 2; 8 | } 9 | 10 | message Test1 { 11 | required MyEnum num = 1 [default=ONE]; 12 | required MyEnum1 num1 = 2 [default=ONE]; 13 | } 14 | -------------------------------------------------------------------------------- /tests/PingExample.proto: -------------------------------------------------------------------------------- 1 | message Message { 2 | optional Ping ping = 1; 3 | optional Pong pong = 2; 4 | 5 | message Ping { 6 | required uint32 time = 1; 7 | } 8 | 9 | message Pong { 10 | required uint32 time = 1; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/T263.proto: -------------------------------------------------------------------------------- 1 | package services; 2 | 3 | message resume { 4 | message Resume { 5 | repeated string experience = 1; 6 | } 7 | } 8 | 9 | message profile { 10 | message Profile { 11 | optional services.resume.Resume resume = 1; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/dupimport/main.proto: -------------------------------------------------------------------------------- 1 | package main; 2 | 3 | import "dep1.proto"; 4 | import "dep2.proto"; 5 | import "common.proto"; 6 | 7 | message MainMessage { 8 | optional main.Dep1 test1 = 1; 9 | optional main.Dep2 test2 = 2; 10 | optional main.Common test3 = 3; 11 | } 12 | -------------------------------------------------------------------------------- /tests/x64.proto: -------------------------------------------------------------------------------- 1 | message Test { 2 | required sfixed64 val = 1 [default=-1]; 3 | required fixed64 uval = 2 [default=1]; 4 | } 5 | 6 | message Test2 { 7 | required int64 val = 1 [default=-1]; 8 | required uint64 uval = 2 [default=1]; 9 | required sint64 sval = 3 [default=-2]; 10 | } 11 | -------------------------------------------------------------------------------- /tests/groups.proto: -------------------------------------------------------------------------------- 1 | message Outer { 2 | required string before = 1; 3 | repeated group MyInner = 2 [deprecated=true] { 4 | required string a = 3; 5 | } 6 | required string after = 4; 7 | optional MyInner myInner2 = 5; 8 | } 9 | 10 | message OuterSparse { 11 | required string before = 1; 12 | required string after = 4; 13 | } 14 | -------------------------------------------------------------------------------- /examples/protoify/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "protobufjs-protoify-example", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "dependencies": { 6 | "protobufjs": "~3.7" 7 | }, 8 | "engines": { 9 | "node": ">=0.8" 10 | }, 11 | "scripts": { 12 | "test": "node test.js" 13 | }, 14 | "private": true 15 | } 16 | -------------------------------------------------------------------------------- /examples/websocket/README.md: -------------------------------------------------------------------------------- 1 | ProtoBuf.js WebSocket example 2 | ============================= 3 | This example shows how to use binary websockets to transfer protocol buffers. 4 | 5 | Instructions 6 | ------------ 7 | 1. Set up dependencies: `npm install` 8 | 2. Run: `node server.js` 9 | 3. Open [http://localhost:8080/](http://localhost:8080/) in a recent browser 10 | -------------------------------------------------------------------------------- /examples/websocket/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "protobufjs-websocket-example", 3 | "version": "1.1.0", 4 | "dependencies": { 5 | "protobufjs": "~3", 6 | "ws": "~0.4", 7 | "open": "0.0.3" 8 | }, 9 | "engines": { 10 | "node": ">=0.8" 11 | }, 12 | "scripts": { 13 | "run": "node server.js" 14 | }, 15 | "private": true 16 | } 17 | -------------------------------------------------------------------------------- /tests/repeated.proto: -------------------------------------------------------------------------------- 1 | message Outer { 2 | // As of: https://developers.google.com/protocol-buffers/docs/encoding#optional 3 | 4 | repeated Inner inner = 1; 5 | // "If your message definition has repeated elements (without the [packed=true] option), the encoded message has 6 | // zero or more key-value pairs with the same tag number." 7 | 8 | } 9 | 10 | message Inner { 11 | optional uint32 inner_value = 1; 12 | } 13 | -------------------------------------------------------------------------------- /tests/camelcase.proto: -------------------------------------------------------------------------------- 1 | message Test { 2 | 3 | // Case 1: Revert 1st 4 | required int32 some_field = 1; // -> someField, reverted 5 | required int32 someField = 2; // kept 6 | 7 | // Case 2: Revert 2nd 8 | required int32 aField = 3; // kept 9 | required int32 a_field = 4; // -> aField, reverted 10 | 11 | // No revert 12 | required int32 its_a_field = 5; // -> itsAField 13 | 14 | } 15 | -------------------------------------------------------------------------------- /tests/extend.proto: -------------------------------------------------------------------------------- 1 | import "google/protobuf/descriptor.proto"; // Ignored 2 | // import "./google/protobuf/descriptor.proto"; // Not ignored 3 | 4 | extend google.protobuf.MessageOptions { 5 | optional int32 foo = 1001; 6 | } 7 | 8 | message Foo { 9 | extensions 2 to max; 10 | } 11 | 12 | extend Foo { 13 | optional string bar = 2; 14 | } 15 | 16 | message Bar { 17 | message Foo { 18 | } 19 | 20 | extend .Foo { 21 | optional Foo foo = 3; // references Bar.Foo 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tests/protobufnet.proto: -------------------------------------------------------------------------------- 1 | message IMeshImp { 2 | } 3 | message Mesh { 4 | optional IMeshImp _meshImp = 1; 5 | repeated float3 _vertices = 2; 6 | repeated uint32 _colors = 3; 7 | repeated float3 _normals = 4; 8 | repeated float2 _uvs = 5; 9 | repeated int32 _triangles = 6; 10 | } 11 | message float2 { 12 | optional float x = 1 [default = 0]; 13 | optional float y = 2 [default = 0]; 14 | } 15 | message float3 { 16 | optional float x = 1 [default = 0]; 17 | optional float y = 2 [default = 0]; 18 | optional float z = 3 [default = 0]; 19 | } 20 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "protobuf", 3 | "description": "Protocol Buffers for JavaScript. Finally.", 4 | "version": "5.0.1", 5 | "main": "dist/protobuf.js", 6 | "license": "Apache-2.0", 7 | "homepage": "http://dcode.io/", 8 | "dependencies": { 9 | "bytebuffer": "~3" 10 | }, 11 | "keywords": ["net", "buffer", "protobuf", "serialization", "bytebuffer", "websocket", "webrtc"], 12 | "ignore": [ 13 | "**/.*", 14 | "node_modules", 15 | "bower_components", 16 | "test", 17 | "tests" 18 | ] 19 | } 20 | -------------------------------------------------------------------------------- /src/bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "protobuf", 3 | "description": "Protocol Buffers for JavaScript. Finally.", 4 | "version": /*?== VERSION */, 5 | "main": "dist/protobuf.js", 6 | "license": "Apache-2.0", 7 | "homepage": "http://dcode.io/", 8 | "dependencies": { 9 | "bytebuffer": "~3" 10 | }, 11 | "keywords": ["net", "buffer", "protobuf", "serialization", "bytebuffer", "websocket", "webrtc"], 12 | "ignore": [ 13 | "**/.*", 14 | "node_modules", 15 | "bower_components", 16 | "test", 17 | "tests" 18 | ] 19 | } 20 | -------------------------------------------------------------------------------- /tests/options.proto: -------------------------------------------------------------------------------- 1 | option (toplevel_1) = 10; 2 | package My; 3 | option (toplevel_2) = "Hello world!"; 4 | 5 | message Test { 6 | optional string name = 1 [default="Max"]; 7 | optional string desc1 = 2 [default="Shouldn't mix quotes"]; 8 | optional string desc2 = 3 [default='Shouldn"t mix quotes']; 9 | optional string desc3 = 4 [(foo_options) = { opt1: 123 opt2: "baz" }]; 10 | optional string desc4 = 5 [(foo_options) = {options { opt1: 1234 opt2: "baz" } options { opt1: 4321 opt2: "foo" }} ]; 11 | option (inmessage) = My.Test; 12 | option (foo.my_option).bar = false; 13 | } 14 | -------------------------------------------------------------------------------- /tests/numberformats.proto: -------------------------------------------------------------------------------- 1 | enum Formats { 2 | DEC = 1; 3 | HEX = 0x1F; 4 | OCT = 017; 5 | } 6 | 7 | message Msg { 8 | required int32 dec = 1 [default=-1]; 9 | required int32 hex = 2 [default=-0x1F]; 10 | required int32 oct = 3 [default=-017]; 11 | required double exp = 4 [default=0.1e5]; 12 | required double nod = 5 [default=1.]; 13 | required double exn = 6 [default=1e8]; 14 | required double sp1 = 7 [default=inf]; 15 | required double sp2 = 8 [default=-inf]; 16 | required double sp3 = 9 [default=nan]; 17 | required int32 hexUC = 10 [default=0X209]; 18 | } 19 | -------------------------------------------------------------------------------- /src/ProtoBuf/Builder/Enum.js: -------------------------------------------------------------------------------- 1 | // This file is not included currently and exists for documentation purposes only. 2 | 3 | /*? 4 | // --- Scope ------------------ 5 | // T : Reflect.Enum instance 6 | */ 7 | 8 | /** 9 | * Constructs a new runtime Enum. 10 | * @name ProtoBuf.Builder.Enum 11 | * @class Barebone of all runtime enums. 12 | * @constructor 13 | */ 14 | var Enum = function() { 15 | ProtoBuf.Builder.Enum.call(this); 16 | }; 17 | 18 | /** 19 | * @alias ProtoBuf.Builder.Enum.prototype 20 | * @inner 21 | */ 22 | var EnumPrototype = Enum.prototype = Object.create(ProtoBuf.Builder.Enum.prototype); 23 | -------------------------------------------------------------------------------- /src/ProtoBuf/Reflect/Message/OneOf.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Constructs a new Message OneOf. 3 | * @exports ProtoBuf.Reflect.Message.OneOf 4 | * @param {!ProtoBuf.Builder} builder Builder reference 5 | * @param {!ProtoBuf.Reflect.Message} message Message reference 6 | * @param {string} name OneOf name 7 | * @constructor 8 | * @extends ProtoBuf.Reflect.T 9 | */ 10 | var OneOf = function(builder, message, name) { 11 | T.call(this, builder, message, name); 12 | 13 | /** 14 | * Enclosed fields. 15 | * @type {!Array.} 16 | * @expose 17 | */ 18 | this.fields = []; 19 | }; 20 | -------------------------------------------------------------------------------- /tests/example5.proto: -------------------------------------------------------------------------------- 1 | package Datastore; 2 | 3 | message LookupRequest { 4 | repeated string key = 1; 5 | } 6 | 7 | message LookupResponse { 8 | repeated string results = 1; 9 | } 10 | 11 | message RunQueryRequest { 12 | required string query = 1; 13 | } 14 | 15 | message RunQueryResponse { 16 | repeated string results = 1; 17 | } 18 | 19 | service LookupService { 20 | // Look up some entities by key. 21 | rpc Lookup(LookupRequest) returns (LookupResponse) { 22 | } 23 | } 24 | 25 | service RunQueryService { 26 | // Query for entities. 27 | rpc RunQuery(RunQueryRequest) returns (RunQueryResponse) { 28 | }; 29 | } 30 | -------------------------------------------------------------------------------- /tests/suite.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ProtoBuf.js Test Suite 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |

ProtoBuf.js Test Suite

14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/ProtoBuf/Reflect/Extension.js: -------------------------------------------------------------------------------- 1 | /** 2 | * An extension (field). 3 | * @exports ProtoBuf.Reflect.Extension 4 | * @constructor 5 | * @param {!ProtoBuf.Builder} builder Builder reference 6 | * @param {!ProtoBuf.Reflect.T} parent Parent object 7 | * @param {string} name Object name 8 | * @param {!ProtoBuf.Reflect.Message.Field} field Extension field 9 | */ 10 | var Extension = function(builder, parent, name, field) { 11 | T.call(this, builder, parent, name); 12 | 13 | /** 14 | * Extended message field. 15 | * @type {!ProtoBuf.Reflect.Message.Field} 16 | * @expose 17 | */ 18 | this.field = field; 19 | }; 20 | 21 | // Extends T 22 | Extension.prototype = Object.create(T.prototype); 23 | -------------------------------------------------------------------------------- /src/ProtoBuf/DotProto.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @alias ProtoBuf.DotProto 3 | * @expose 4 | */ 5 | ProtoBuf.DotProto = (function(ProtoBuf, Lang) { 6 | "use strict"; 7 | 8 | /** 9 | * Utilities to parse .proto files. 10 | * @exports ProtoBuf.DotProto 11 | * @namespace 12 | */ 13 | var DotProto = {}; 14 | 15 | //? include("DotProto/Tokenizer.js"); 16 | 17 | /** 18 | * @alias ProtoBuf.DotProto.Tokenizer 19 | * @expose 20 | */ 21 | DotProto.Tokenizer = Tokenizer; 22 | 23 | //? include("DotProto/Parser.js"); 24 | 25 | /** 26 | * @alias ProtoBuf.DotProto.Parser 27 | * @expose 28 | */ 29 | DotProto.Parser = Parser; 30 | 31 | return DotProto; 32 | 33 | })(ProtoBuf, ProtoBuf.Lang); 34 | -------------------------------------------------------------------------------- /src/ProtoBuf/Reflect/Enum/Value.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Constructs a new Enum Value. 3 | * @exports ProtoBuf.Reflect.Enum.Value 4 | * @param {!ProtoBuf.Builder} builder Builder reference 5 | * @param {!ProtoBuf.Reflect.Enum} enm Enum reference 6 | * @param {string} name Field name 7 | * @param {number} id Unique field id 8 | * @constructor 9 | * @extends ProtoBuf.Reflect.T 10 | */ 11 | var Value = function(builder, enm, name, id) { 12 | T.call(this, builder, enm, name); 13 | 14 | /** 15 | * @override 16 | */ 17 | this.className = "Enum.Value"; 18 | 19 | /** 20 | * Unique enum value id. 21 | * @type {number} 22 | * @expose 23 | */ 24 | this.id = id; 25 | }; 26 | 27 | // Extends T 28 | Value.prototype = Object.create(T.prototype); 29 | -------------------------------------------------------------------------------- /docs/scripts/linenumber.js: -------------------------------------------------------------------------------- 1 | /*global document */ 2 | (function() { 3 | var source = document.getElementsByClassName('prettyprint source linenums'); 4 | var i = 0; 5 | var lineNumber = 0; 6 | var lineId; 7 | var lines; 8 | var totalLines; 9 | var anchorHash; 10 | 11 | if (source && source[0]) { 12 | anchorHash = document.location.hash.substring(1); 13 | lines = source[0].getElementsByTagName('li'); 14 | totalLines = lines.length; 15 | 16 | for (; i < totalLines; i++) { 17 | lineNumber++; 18 | lineId = 'line' + lineNumber; 19 | lines[i].id = lineId; 20 | if (lineId === anchorHash) { 21 | lines[i].className += ' selected'; 22 | } 23 | } 24 | } 25 | })(); 26 | -------------------------------------------------------------------------------- /examples/protoify/json.proto: -------------------------------------------------------------------------------- 1 | // Everything below is located in the js-namespace 2 | package js; 3 | 4 | // Represents a JavaScript value. 5 | // Contains exactly one or zero fields. 6 | message Value { 7 | oneof type { 8 | sint32 integer = 1; 9 | double double = 2; 10 | string string = 3; 11 | bool boolean = 4; 12 | bool null = 5; 13 | Array array = 6; 14 | Object object = 7; 15 | // if none is set: undefined 16 | } 17 | } 18 | 19 | // Represents a JavaScript array. 20 | // Contains zero to N values. 21 | message Array { 22 | repeated Value values = 1; 23 | } 24 | 25 | // Represents a JavaScript object. 26 | // Contains zero to N keys with associated values. 27 | message Object { 28 | repeated Value keys = 1; 29 | repeated Value values = 2; 30 | } 31 | -------------------------------------------------------------------------------- /docs/scripts/prettify/lang-css.js: -------------------------------------------------------------------------------- 1 | PR.registerLangHandler(PR.createSimpleLexer([["pln",/^[\t\n\f\r ]+/,null," \t\r\n "]],[["str",/^"(?:[^\n\f\r"\\]|\\(?:\r\n?|\n|\f)|\\[\S\s])*"/,null],["str",/^'(?:[^\n\f\r'\\]|\\(?:\r\n?|\n|\f)|\\[\S\s])*'/,null],["lang-css-str",/^url\(([^"')]*)\)/i],["kwd",/^(?:url|rgb|!important|@import|@page|@media|@charset|inherit)(?=[^\w-]|$)/i,null],["lang-css-kw",/^(-?(?:[_a-z]|\\[\da-f]+ ?)(?:[\w-]|\\\\[\da-f]+ ?)*)\s*:/i],["com",/^\/\*[^*]*\*+(?:[^*/][^*]*\*+)*\//],["com", 2 | /^(?:<\!--|--\>)/],["lit",/^(?:\d+|\d*\.\d+)(?:%|[a-z]+)?/i],["lit",/^#[\da-f]{3,6}/i],["pln",/^-?(?:[_a-z]|\\[\da-f]+ ?)(?:[\w-]|\\\\[\da-f]+ ?)*/i],["pun",/^[^\s\w"']+/]]),["css"]);PR.registerLangHandler(PR.createSimpleLexer([],[["kwd",/^-?(?:[_a-z]|\\[\da-f]+ ?)(?:[\w-]|\\\\[\da-f]+ ?)*/i]]),["css-kw"]);PR.registerLangHandler(PR.createSimpleLexer([],[["str",/^[^"')]+/]]),["css-str"]); 3 | -------------------------------------------------------------------------------- /src/google/protobuf/README.md: -------------------------------------------------------------------------------- 1 | These files are by default not used by ProtoBuf.js and are here for the purpose of completeness. The library does not 2 | need and therefore does not use these files by design. In most use cases including the descriptor isn't even required 3 | and would just add about 10KB (minified JSON) to your application. 4 | 5 | ### Though it's possible to include them: 6 | 7 | 1. You may explicitly reference them by providing a relative or absolute path in your .proto files. E.g. use 8 | `./google/protobuf/descriptor.proto` and bundle the file with your application. 9 | 10 | 2. If you use the `proto2js` command line utility with the `-legacy` option and the descriptor namespace is explicitly 11 | referenced, it is included in the generated output. 12 | 13 | You are then able to work with it as if it'd be no more and no less than a standard import. 14 | -------------------------------------------------------------------------------- /tests/options.json: -------------------------------------------------------------------------------- 1 | { 2 | "package": "My", 3 | "messages": [ 4 | { 5 | "name": "Test", 6 | "fields": [ 7 | { 8 | "rule": "optional", 9 | "options": { 10 | "default": "Max" 11 | }, 12 | "type": "string", 13 | "name": "name", 14 | "id": 1 15 | } 16 | ], 17 | "enums": [], 18 | "messages": [], 19 | "options": { 20 | "(inmessage)": "My.Test", 21 | "(foo.my_option).bar": false 22 | } 23 | } 24 | ], 25 | "enums": [], 26 | "imports": [], 27 | "options": { 28 | "(toplevel_1)": 10, 29 | "(toplevel_2)": "Hello world!" 30 | }, 31 | "services": [] 32 | } 33 | -------------------------------------------------------------------------------- /tests/complex.proto: -------------------------------------------------------------------------------- 1 | package Game.Cars; 2 | 3 | // Car 4 | message Car { 5 | 6 | // Car Vendor 7 | message Vendor { 8 | required string name = 1; 9 | 10 | // Car Vendor Address 11 | message Address { 12 | required string country = 1; 13 | } 14 | 15 | optional Address address = 2; 16 | repeated string models = 3; // The models sold here. 17 | } 18 | 19 | required string model = 1; // Model name 20 | required Vendor vendor = 2; // Vendor information 21 | optional Speed speed = 3 [default=FAST]; // Car speed 22 | 23 | // Car speed enum 24 | enum Speed { 25 | FAST = 1; 26 | SUPERFAST = 2; 27 | } 28 | 29 | // Car Holder 30 | message Holder { 31 | optional string first_name = 1; 32 | required string last_name = 2; 33 | optional Vendor.Address address = 3; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/ProtoBuf/Reflect/Message/ExtensionField.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Constructs a new Message ExtensionField. 3 | * @exports ProtoBuf.Reflect.Message.ExtensionField 4 | * @param {!ProtoBuf.Builder} builder Builder reference 5 | * @param {!ProtoBuf.Reflect.Message} message Message reference 6 | * @param {string} rule Rule, one of requried, optional, repeated 7 | * @param {string} type Data type, e.g. int32 8 | * @param {string} name Field name 9 | * @param {number} id Unique field id 10 | * @param {!Object.=} options Options 11 | * @constructor 12 | * @extends ProtoBuf.Reflect.Message.Field 13 | */ 14 | var ExtensionField = function(builder, message, rule, type, name, id, options) { 15 | Field.call(this, builder, message, rule, /* keytype = */ null, type, name, id, options); 16 | 17 | /** 18 | * Extension reference. 19 | * @type {!ProtoBuf.Reflect.Extension} 20 | * @expose 21 | */ 22 | this.extension; 23 | }; 24 | 25 | // Extends Field 26 | ExtensionField.prototype = Object.create(Field.prototype); 27 | -------------------------------------------------------------------------------- /tests/annotations.proto: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2015, Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | syntax = "proto3"; 16 | 17 | package google.api; 18 | 19 | // import "google/api/http.proto"; 20 | // import "google/protobuf/descriptor.proto"; 21 | 22 | option java_multiple_files = true; 23 | option java_outer_classname = "AnnotationsProto"; 24 | option java_package = "com.google.api"; 25 | 26 | extend google.protobuf.MethodOptions { 27 | // See `HttpRule`. 28 | HttpRule http = 72295728; 29 | } -------------------------------------------------------------------------------- /src/ProtoBuf/Reflect/Service/Method.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Abstract service method. 3 | * @exports ProtoBuf.Reflect.Service.Method 4 | * @param {!ProtoBuf.Builder} builder Builder reference 5 | * @param {!ProtoBuf.Reflect.Service} svc Service 6 | * @param {string} name Method name 7 | * @param {Object.=} options Options 8 | * @constructor 9 | * @extends ProtoBuf.Reflect.T 10 | */ 11 | var Method = function(builder, svc, name, options) { 12 | T.call(this, builder, svc, name); 13 | 14 | /** 15 | * @override 16 | */ 17 | this.className = "Service.Method"; 18 | 19 | /** 20 | * Options. 21 | * @type {Object.} 22 | * @expose 23 | */ 24 | this.options = options || {}; 25 | }; 26 | 27 | /** 28 | * @alias ProtoBuf.Reflect.Service.Method.prototype 29 | * @inner 30 | */ 31 | var MethodPrototype = Method.prototype = Object.create(T.prototype); 32 | 33 | /** 34 | * Builds the method's '$options' property. 35 | * @name ProtoBuf.Reflect.Service.Method#buildOpt 36 | * @function 37 | * @return {Object.} 38 | */ 39 | MethodPrototype.buildOpt = NamespacePrototype.buildOpt; 40 | -------------------------------------------------------------------------------- /tests/nodeunit-browser/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010 Caolan McMahon 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /tests/proto2js/Bar.json: -------------------------------------------------------------------------------- 1 | { 2 | "package": null, 3 | "messages": [ 4 | { 5 | "ref": "Foo", 6 | "fields": [ 7 | { 8 | "rule": "required", 9 | "type": "string", 10 | "name": "buzz", 11 | "id": 2, 12 | "options": {} 13 | } 14 | ] 15 | } 16 | ], 17 | "enums": [], 18 | "imports": [ 19 | { 20 | "package": null, 21 | "messages": [ 22 | { 23 | "name": "Foo", 24 | "fields": [ 25 | { 26 | "rule": "required", 27 | "type": "string", 28 | "name": "fizz", 29 | "id": 1, 30 | "options": {} 31 | } 32 | ], 33 | "enums": [], 34 | "messages": [], 35 | "options": {} 36 | } 37 | ], 38 | "enums": [], 39 | "imports": [], 40 | "options": {}, 41 | "services": [] 42 | } 43 | ], 44 | "options": {}, 45 | "services": [] 46 | } 47 | -------------------------------------------------------------------------------- /tests/services.js: -------------------------------------------------------------------------------- 1 | var root = require("..").newBuilder({ 2 | "convertFieldsToCamelCase": true, 3 | "populateAccessors": false 4 | })['import']({ 5 | "package": "my.namespace", 6 | "messages": [ 7 | { 8 | "name": "something", 9 | "fields": [], 10 | "messages": [ 11 | { 12 | "name": "v1", 13 | "fields": [], 14 | "messages": [ 15 | { 16 | "name": "GetRequest", 17 | "fields": [] 18 | }, 19 | { 20 | "name": "GetResponse", 21 | "fields": [] 22 | } 23 | ], 24 | "enums": [], 25 | "services": [{ 26 | "name": "SomeService", 27 | "rpc": { 28 | "get": { 29 | "request": "GetRequest", 30 | "response": "GetResponse" 31 | } 32 | } 33 | }] 34 | } 35 | ] 36 | } 37 | ] 38 | }).build(); 39 | console.log(require("util").inspect(root, { depth: 10})); -------------------------------------------------------------------------------- /src/wrap.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Daniel Wirtz 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | /** 18 | * @license protobuf.js (c) 2013 Daniel Wirtz 19 | * Released under the Apache License, Version 2.0 20 | * see: https://github.com/dcodeIO/protobuf.js for details 21 | */ 22 | (function(global, factory) { 23 | 24 | /* AMD */ if (typeof define === 'function' && define["amd"]) 25 | define(["bytebuffer"], factory); 26 | /* CommonJS */ else if (typeof require === "function" && typeof module === "object" && module && module["exports"]) 27 | module["exports"] = factory(require("bytebuffer"), true); 28 | /* Global */ else 29 | (global["dcodeIO"] = global["dcodeIO"] || {})["ProtoBuf"] = factory(global["dcodeIO"]["ByteBuffer"]); 30 | 31 | })(this, function(ByteBuffer, isCommonJS) { 32 | "use strict"; 33 | 34 | //? include("protobuf.js"); 35 | 36 | return ProtoBuf; 37 | }); 38 | -------------------------------------------------------------------------------- /cli/pbjs/targets/commonjs.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Daniel Wirtz 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | var description = "Runtime structures as CommonJS module"; 17 | 18 | var util = require("../util.js"), 19 | js = require("./js.js"); 20 | 21 | /** 22 | * pbjs target: Runtime structures as a CommonJS module 23 | * @exports pbjs/targets/commonjs 24 | * @function 25 | * @param {!ProtoBuf.Builder} builder Builder 26 | * @param {!Object.=} options Options 27 | * @returns {string} 28 | */ 29 | var commonjs = module.exports = function(builder, options) { 30 | options = options || {}; 31 | return [ 32 | "module.exports", options.min ? "=" : " = ", 33 | "require(", JSON.stringify(options.dependency || "protobufjs"), ")", 34 | js.build(builder, options) 35 | ].join(''); 36 | }; 37 | 38 | /** 39 | * Module description. 40 | * @type {string} 41 | */ 42 | commonjs.description = description; 43 | -------------------------------------------------------------------------------- /tests/custom-options.proto: -------------------------------------------------------------------------------- 1 | import "google/protobuf/descriptor.proto"; 2 | 3 | extend google.protobuf.FileOptions { 4 | optional string my_file_option = 50000; 5 | } 6 | extend google.protobuf.MessageOptions { 7 | optional int32 my_message_option = 50001; 8 | } 9 | extend google.protobuf.FieldOptions { 10 | optional float my_field_option = 50002; 11 | } 12 | extend google.protobuf.EnumOptions { 13 | optional bool my_enum_option = 50003; 14 | } 15 | extend google.protobuf.EnumValueOptions { 16 | optional uint32 my_enum_value_option = 50004; 17 | } 18 | extend google.protobuf.ServiceOptions { 19 | optional MyEnum my_service_option = 50005; 20 | } 21 | extend google.protobuf.MethodOptions { 22 | optional MyMessage my_method_option = 50006; 23 | } 24 | 25 | option (my_file_option) = "Hello world!"; 26 | 27 | message MyMessage { 28 | option (my_message_option) = 1234; 29 | 30 | optional int32 foo = 1 [(my_field_option) = 4.5]; 31 | optional string bar = 2; 32 | } 33 | 34 | enum MyEnum { 35 | option (my_enum_option) = true; 36 | 37 | FOO = 1 [(my_enum_value_option) = 321]; 38 | BAR = 2; 39 | } 40 | 41 | message RequestType {} 42 | message ResponseType {} 43 | 44 | service MyService { 45 | option (my_service_option) = FOO; 46 | 47 | rpc MyMethod(RequestType) returns(ResponseType) { 48 | // Note: my_method_option has type MyMessage. We can set each field 49 | // within it using a separate "option" line. 50 | option (my_method_option).foo = 567; 51 | option (my_method_option).bar = "Some string"; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /examples/protoify/README.md: -------------------------------------------------------------------------------- 1 | ProtoBuf.js protoify example 2 | ============================ 3 | This example shows the general usage of ProtoBuf.js by converting JSON structures to protocol buffers and vice versa 4 | using a definition describing JSON itself. While this works as an example, it does not provide any real world benefits 5 | (well, this is if you are not building a protobuf-backed database for JSON data, using inter-field substitution to 6 | minimize redundancy - nevermind, forget that). 7 | 8 | Instructions 9 | ------------ 10 | 1. Set up dependencies: `npm install` 11 | 2. Run: `npm test` 12 | 13 | Now you know no more and no less than that it works and you might want to inspect the following files to get the 'how': 14 | 15 | * **[index.js](https://github.com/dcodeIO/ProtoBuf.js/blob/master/examples/protoify/index.js)** 16 | contains the sample's source code 17 | 18 | * **[json.proto](https://github.com/dcodeIO/ProtoBuf.js/blob/master/examples/protoify/json.proto)** 19 | contains the protobuf definition used 20 | 21 | * **[json.json](https://github.com/dcodeIO/ProtoBuf.js/blob/master/examples/protoify/json.json)** 22 | contains the protobuf definition converted through `proto2js json.proto > json.json` 23 | 24 | * **[json.js](https://github.com/dcodeIO/ProtoBuf.js/blob/master/examples/protoify/json.js)** 25 | contains the protobuf definition converted through `proto2js json.proto -commonjs=js > json.js` 26 | 27 | * **[test.js](https://github.com/dcodeIO/ProtoBuf.js/blob/master/examples/protoify/test.js)** 28 | contains our simple test suite 29 | -------------------------------------------------------------------------------- /src/ProtoBuf/Reflect/Service.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Constructs a new Service. 3 | * @exports ProtoBuf.Reflect.Service 4 | * @param {!ProtoBuf.Builder} builder Builder reference 5 | * @param {!ProtoBuf.Reflect.Namespace} root Root 6 | * @param {string} name Service name 7 | * @param {Object.=} options Options 8 | * @constructor 9 | * @extends ProtoBuf.Reflect.Namespace 10 | */ 11 | var Service = function(builder, root, name, options) { 12 | Namespace.call(this, builder, root, name, options); 13 | 14 | /** 15 | * @override 16 | */ 17 | this.className = "Service"; 18 | 19 | /** 20 | * Built runtime service class. 21 | * @type {?function(new:ProtoBuf.Builder.Service)} 22 | */ 23 | this.clazz = null; 24 | }; 25 | 26 | /** 27 | * @alias ProtoBuf.Reflect.Service.prototype 28 | * @inner 29 | */ 30 | var ServicePrototype = Service.prototype = Object.create(Namespace.prototype); 31 | 32 | /** 33 | * Builds the service and returns the runtime counterpart, which is a fully functional class. 34 | * @see ProtoBuf.Builder.Service 35 | * @param {boolean=} rebuild Whether to rebuild or not 36 | * @return {Function} Service class 37 | * @throws {Error} If the message cannot be built 38 | * @expose 39 | */ 40 | ServicePrototype.build = function(rebuild) { 41 | if (this.clazz && !rebuild) 42 | return this.clazz; 43 | 44 | // Create the runtime Service class in its own scope 45 | return this.clazz = (function(ProtoBuf, T) { 46 | 47 | //? include("../Builder/Service.js"); 48 | 49 | return Service; 50 | 51 | })(ProtoBuf, this); 52 | }; 53 | -------------------------------------------------------------------------------- /tests/bench.js: -------------------------------------------------------------------------------- 1 | var ProtoBuf = require("../index.js"); 2 | 3 | var sample = { 4 | id: 1, 5 | name: "John123", 6 | password: "helloworld" 7 | }; 8 | 9 | console.log("Sample: `"+JSON.stringify(sample, null, 4)); 10 | console.log("\n"); 11 | 12 | var proto = " message Sample {" + 13 | "required uint32 id = 1;" + 14 | "required string name = 2;" + 15 | "required string password = 3;" + 16 | "}"; 17 | 18 | var builder = ProtoBuf.loadProto(proto, "bench.proto"), 19 | Sample = builder.build("Sample"); 20 | 21 | // Compare size 22 | console.log("Encoding size"); 23 | console.log("-------------"); 24 | var jsonData = new Buffer(JSON.stringify(sample), "utf8"), 25 | protoData = new Sample(sample).toBuffer(); 26 | console.log("* Encoded sample size as JSON: "+jsonData.length+" bytes"); 27 | console.log("* Encoded sample size as protocol buffer: "+protoData.length+" bytes"); 28 | console.log(""); 29 | 30 | // Compare encoding speed 31 | console.log("Encoding speed"); 32 | console.log("--------------"); 33 | (function() { 34 | // Assuming that a receive buffer is used 35 | var buf = ProtoBuf.ByteBuffer.allocate(64); 36 | var protoSample = new Sample(sample), 37 | n = 100000, k = (n/1000)+'k'; 38 | console.time("* ProtoBuf encode "+k); 39 | for (var i=0; i 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | var description = "Runtime structures as AMD module"; 17 | 18 | var ProtoBuf = require("../../../index.js"), 19 | util = require("../util.js"), 20 | js = require("./js.js"); 21 | 22 | /** 23 | * pbjs target: Runtime structures as an AMD module 24 | * @exports pbjs/targets/amd 25 | * @function 26 | * @param {!ProtoBuf.Builder} builder Builder 27 | * @param {!Object.=} options Options 28 | * @returns {string} 29 | */ 30 | var amd = module.exports = function(builder, options) { 31 | options = options || {}; 32 | return [ 33 | "define([", JSON.stringify(options.dependency || "ProtoBuf"), "]", options.min ? "," : ", ", 34 | "function(ProtoBuf)", options.min ? "{" : " {\n ", 35 | "return ProtoBuf", 36 | util.indent(js.build(builder, options), options.min ? "" : " "), options.min ? "" : "\n", 37 | "});" 38 | ].join(''); 39 | }; 40 | 41 | /** 42 | * Module description. 43 | * @type {string} 44 | */ 45 | amd.description = description; 46 | -------------------------------------------------------------------------------- /tests/nodeunit-browser/nodeunit.css: -------------------------------------------------------------------------------- 1 | /*! 2 | * Styles taken from qunit.css 3 | */ 4 | 5 | h1#nodeunit-header, h1.nodeunit-header { 6 | padding: 15px; 7 | font-size: large; 8 | background-color: #06b; 9 | color: white; 10 | font-family: 'trebuchet ms', verdana, arial; 11 | margin: 0; 12 | } 13 | 14 | h1#nodeunit-header a { 15 | color: white; 16 | } 17 | 18 | h2#nodeunit-banner { 19 | height: 2em; 20 | border-bottom: 1px solid white; 21 | background-color: #eee; 22 | margin: 0; 23 | font-family: 'trebuchet ms', verdana, arial; 24 | } 25 | h2#nodeunit-banner.pass { 26 | background-color: green; 27 | } 28 | h2#nodeunit-banner.fail { 29 | background-color: red; 30 | } 31 | 32 | h2#nodeunit-userAgent, h2.nodeunit-userAgent { 33 | padding: 10px; 34 | background-color: #eee; 35 | color: black; 36 | margin: 0; 37 | font-size: small; 38 | font-weight: normal; 39 | font-family: 'trebuchet ms', verdana, arial; 40 | font-size: 10pt; 41 | } 42 | 43 | div#nodeunit-testrunner-toolbar { 44 | background: #eee; 45 | border-top: 1px solid black; 46 | padding: 10px; 47 | font-family: 'trebuchet ms', verdana, arial; 48 | margin: 0; 49 | font-size: 10pt; 50 | } 51 | 52 | ol#nodeunit-tests { 53 | font-family: 'trebuchet ms', verdana, arial; 54 | font-size: 10pt; 55 | } 56 | ol#nodeunit-tests li strong { 57 | cursor:pointer; 58 | } 59 | ol#nodeunit-tests .pass { 60 | color: green; 61 | } 62 | ol#nodeunit-tests .fail { 63 | color: red; 64 | } 65 | 66 | p#nodeunit-testresult { 67 | margin-left: 1em; 68 | font-size: 10pt; 69 | font-family: 'trebuchet ms', verdana, arial; 70 | } 71 | -------------------------------------------------------------------------------- /src/es5.js: -------------------------------------------------------------------------------- 1 | // Starting with ProtoBuf.js 4.X we are no longer bundling any ES5 polyfills with the library. 2 | // It is now up to the user to provide these as needed. For reference, this is what we use: 3 | 4 | // ref: https://developer.mozilla.org/de/docs/JavaScript/Reference/Global_Objects/Object/create 5 | if (!Object.create) 6 | /** @expose */ 7 | Object.create = function (o) { 8 | if (arguments.length > 1) 9 | throw Error('illegal number of arguments'); 10 | function F() {} 11 | F.prototype = o; 12 | return new F(); 13 | }; 14 | 15 | // ref: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray 16 | if (!Array.isArray) 17 | /** @expose */ 18 | Array.isArray = function(o) { 19 | return Object.prototype.toString.call(o) === "[object Array]"; 20 | }; 21 | 22 | // ref: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach 23 | if (!Array.prototype.forEach) 24 | /** @expose */ 25 | Array.prototype.forEach = function(callback, thisArg) { 26 | var T, k; 27 | if (this == null) 28 | throw new TypeError('this is null or not defined'); 29 | var O = Object(this); 30 | var len = O.length >>> 0; 31 | if (typeof callback !== "function") 32 | throw new TypeError(callback + ' is not a function'); 33 | if (arguments.length > 1) 34 | T = thisArg; 35 | k = 0; 36 | while (k < len) { 37 | var kValue; 38 | if (k in O) 39 | kValue = O[k], 40 | callback.call(T, kValue, k, O); 41 | k++; 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /dist/README.md: -------------------------------------------------------------------------------- 1 | Distributions 2 | ============= 3 | 4 | ProtoBuf.js is available either with or without the .proto parser included. 5 | 6 | ### Full build including .proto parser (.proto and JSON) 7 | 8 | * **[protobuf.js](https://raw.githubusercontent.com/dcodeIO/ProtoBuf.js/master/dist/protobuf.js)** 9 | contains the commented source code. 10 | 11 | * **[protobuf.min.js](https://raw.githubusercontent.com/dcodeIO/ProtoBuf.js/master/dist/protobuf.min.js)** 12 | has been compiled with Closure Compiler. 13 | 14 | * **[protobuf.min.js.gz](https://raw.githubusercontent.com/dcodeIO/ProtoBuf.js/master/dist/protobuf.min.js.gz)** 15 | has also been gzipped using `-9`. 16 | 17 | * **[protobuf.min.map](https://raw.githubusercontent.com/dcodeIO/ProtoBuf.js/master/dist/protobuf.min.map)** 18 | is the source map generated by Closure Compiler. 19 | 20 | ### Light build excluding .proto parser (JSON only) for use with [pbjs](https://github.com/dcodeIO/Protobuf.js/wiki/pbjs) 21 | 22 | * **[protobuf-light.js](https://raw.githubusercontent.com/dcodeIO/ProtoBuf.js/master/dist/protobuf-light.js)** 23 | contains the commented source code. 24 | 25 | * **[protobuf-light.min.js](https://raw.githubusercontent.com/dcodeIO/ProtoBuf.js/master/dist/protobuf-light.min.js)** 26 | has been compiled with Closure Compiler. 27 | 28 | * **[protobuf-light.min.js.gz](https://raw.githubusercontent.com/dcodeIO/ProtoBuf.js/master/dist/protobuf-light.min.js.gz)** 29 | has also been gzipped using `-9`. 30 | 31 | * **[protobuf-light.min.map](https://raw.githubusercontent.com/dcodeIO/ProtoBuf.js/master/dist/protobuf-light.min.map)** 32 | is the source map generated by Closure Compiler. 33 | 34 | When sending pull requests, please note that these files have been automatically generated from the sources located in 35 | [src/](https://github.com/dcodeIO/protobuf.js/tree/master/src). 36 | -------------------------------------------------------------------------------- /src/ProtoBuf/Lang.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Language expressions. 3 | * @type {!Object.} 4 | * @expose 5 | */ 6 | ProtoBuf.Lang = { 7 | 8 | // Characters always ending a statement 9 | DELIM: /[\s\{\}=;:\[\],'"\(\)<>]/g, 10 | 11 | // Field rules 12 | RULE: /^(?:required|optional|repeated|map)$/, 13 | 14 | // Field types 15 | TYPE: /^(?:double|float|int32|uint32|sint32|int64|uint64|sint64|fixed32|sfixed32|fixed64|sfixed64|bool|string|bytes)$/, 16 | 17 | // Names 18 | NAME: /^[a-zA-Z_][a-zA-Z_0-9]*$/, 19 | 20 | // Type definitions 21 | TYPEDEF: /^[a-zA-Z][a-zA-Z_0-9]*$/, 22 | 23 | // Type references 24 | TYPEREF: /^(?:\.?[a-zA-Z_][a-zA-Z_0-9]*)+$/, 25 | 26 | // Fully qualified type references 27 | FQTYPEREF: /^(?:\.[a-zA-Z][a-zA-Z_0-9]*)+$/, 28 | 29 | // All numbers 30 | NUMBER: /^-?(?:[1-9][0-9]*|0|0[xX][0-9a-fA-F]+|0[0-7]+|([0-9]*(\.[0-9]*)?([Ee][+-]?[0-9]+)?)|inf|nan)$/, 31 | 32 | // Decimal numbers 33 | NUMBER_DEC: /^(?:[1-9][0-9]*|0)$/, 34 | 35 | // Hexadecimal numbers 36 | NUMBER_HEX: /^0[xX][0-9a-fA-F]+$/, 37 | 38 | // Octal numbers 39 | NUMBER_OCT: /^0[0-7]+$/, 40 | 41 | // Floating point numbers 42 | NUMBER_FLT: /^([0-9]*(\.[0-9]*)?([Ee][+-]?[0-9]+)?|inf|nan)$/, 43 | 44 | // Booleans 45 | BOOL: /^(?:true|false)$/i, 46 | 47 | // Id numbers 48 | ID: /^(?:[1-9][0-9]*|0|0[xX][0-9a-fA-F]+|0[0-7]+)$/, 49 | 50 | // Negative id numbers (enum values) 51 | NEGID: /^\-?(?:[1-9][0-9]*|0|0[xX][0-9a-fA-F]+|0[0-7]+)$/, 52 | 53 | // Whitespaces 54 | WHITESPACE: /\s/, 55 | 56 | // All strings 57 | STRING: /(?:"([^"\\]*(?:\\.[^"\\]*)*)")|(?:'([^'\\]*(?:\\.[^'\\]*)*)')/g, 58 | 59 | // Double quoted strings 60 | STRING_DQ: /(?:"([^"\\]*(?:\\.[^"\\]*)*)")/g, 61 | 62 | // Single quoted strings 63 | STRING_SQ: /(?:'([^'\\]*(?:\\.[^'\\]*)*)')/g 64 | }; 65 | -------------------------------------------------------------------------------- /src/ProtoBuf/Reflect/Service/RPCMethod.js: -------------------------------------------------------------------------------- 1 | /** 2 | * RPC service method. 3 | * @exports ProtoBuf.Reflect.Service.RPCMethod 4 | * @param {!ProtoBuf.Builder} builder Builder reference 5 | * @param {!ProtoBuf.Reflect.Service} svc Service 6 | * @param {string} name Method name 7 | * @param {string} request Request message name 8 | * @param {string} response Response message name 9 | * @param {boolean} request_stream Whether requests are streamed 10 | * @param {boolean} response_stream Whether responses are streamed 11 | * @param {Object.=} options Options 12 | * @constructor 13 | * @extends ProtoBuf.Reflect.Service.Method 14 | */ 15 | var RPCMethod = function(builder, svc, name, request, response, request_stream, response_stream, options) { 16 | Method.call(this, builder, svc, name, options); 17 | 18 | /** 19 | * @override 20 | */ 21 | this.className = "Service.RPCMethod"; 22 | 23 | /** 24 | * Request message name. 25 | * @type {string} 26 | * @expose 27 | */ 28 | this.requestName = request; 29 | 30 | /** 31 | * Response message name. 32 | * @type {string} 33 | * @expose 34 | */ 35 | this.responseName = response; 36 | 37 | /** 38 | * Whether requests are streamed 39 | * @type {bool} 40 | * @expose 41 | */ 42 | this.requestStream = request_stream; 43 | 44 | /** 45 | * Whether responses are streamed 46 | * @type {bool} 47 | * @expose 48 | */ 49 | this.responseStream = response_stream; 50 | 51 | /** 52 | * Resolved request message type. 53 | * @type {ProtoBuf.Reflect.Message} 54 | * @expose 55 | */ 56 | this.resolvedRequestType = null; 57 | 58 | /** 59 | * Resolved response message type. 60 | * @type {ProtoBuf.Reflect.Message} 61 | * @expose 62 | */ 63 | this.resolvedResponseType = null; 64 | }; 65 | 66 | // Extends Method 67 | RPCMethod.prototype = Object.create(Method.prototype); 68 | -------------------------------------------------------------------------------- /examples/protoify/test.js: -------------------------------------------------------------------------------- 1 | var protoify = require("./index.js"), 2 | ByteBuffer = require("protobufjs").ByteBuffer, 3 | assert = require("assert"); 4 | 5 | // Array of samples to test 6 | var samples = [ 7 | 1, -1, 0x80000000|0, 0x7fffffff|0, // Integers 8 | 0.1, 0.2, 1.234, // Doubles 9 | "John", // String 10 | true, false, // Booleans 11 | null, // null 12 | [], // Array 13 | {}, // Object 14 | undefined, // undefined 15 | [ // Array holding each data type 16 | 1, 17 | 0.1, 18 | "John", 19 | true, 20 | false, 21 | null, 22 | [], 23 | {}, 24 | undefined 25 | ], 26 | { // Object holding each data type 27 | 1: 1, 28 | 0.1: 0.1, 29 | "John": "John", 30 | true: true, 31 | false: false, 32 | null: null, 33 | array: [], 34 | object: {}, 35 | undefined: undefined 36 | } 37 | ]; 38 | 39 | samples.forEach(function(sample) { 40 | // Encode each sample to a Buffer 41 | var buf = protoify(sample); 42 | 43 | // Print some nice debugging information 44 | console.log(JSON.stringify(sample)); 45 | console.log("-------------------------------------------------------------------"); 46 | console.log(ByteBuffer.wrap(buf).toDebug(true)); 47 | 48 | // Decode the Buffer back to JSON 49 | var decodedSample = protoify.parse(buf); 50 | 51 | // And assert that it's actually equal 52 | assert.deepEqual(decodedSample, sample); 53 | }); 54 | 55 | // If no assertion errors are thrown, print 56 | console.log("OK"); 57 | -------------------------------------------------------------------------------- /tests/extend.json: -------------------------------------------------------------------------------- 1 | { 2 | "package": null, 3 | "messages": [ 4 | { 5 | "ref": "google.protobuf.MessageOptions", 6 | "fields": [ 7 | { 8 | "rule": "optional", 9 | "options": {}, 10 | "type": "int32", 11 | "name": "foo", 12 | "id": 1001 13 | } 14 | ] 15 | }, 16 | { 17 | "name": "Foo", 18 | "fields": [], 19 | "enums": [], 20 | "messages": [], 21 | "options": {}, 22 | "extensions": [ 23 | 2, 24 | 536870911 25 | ] 26 | }, 27 | { 28 | "ref": "Foo", 29 | "fields": [ 30 | { 31 | "rule": "optional", 32 | "options": {}, 33 | "type": "string", 34 | "name": "bar", 35 | "id": 2 36 | } 37 | ] 38 | }, 39 | { 40 | "name": "Bar", 41 | "fields": [], 42 | "enums": [], 43 | "messages": [ 44 | { 45 | "name": "Foo", 46 | "fields": [], 47 | "enums": [], 48 | "messages": [], 49 | "options": {} 50 | }, 51 | { 52 | "ref": "Foo", 53 | "fields": [ 54 | { 55 | "rule": "optional", 56 | "options": {}, 57 | "type": "Foo", 58 | "name": "foo", 59 | "id": 3 60 | } 61 | ] 62 | } 63 | ], 64 | "options": {} 65 | } 66 | ], 67 | "enums": [], 68 | "imports": [], 69 | "options": {}, 70 | "services": [] 71 | } 72 | -------------------------------------------------------------------------------- /src/ProtoBuf/Reflect/T.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Constructs a Reflect base class. 3 | * @exports ProtoBuf.Reflect.T 4 | * @constructor 5 | * @abstract 6 | * @param {!ProtoBuf.Builder} builder Builder reference 7 | * @param {?ProtoBuf.Reflect.T} parent Parent object 8 | * @param {string} name Object name 9 | */ 10 | var T = function(builder, parent, name) { 11 | 12 | /** 13 | * Builder reference. 14 | * @type {!ProtoBuf.Builder} 15 | * @expose 16 | */ 17 | this.builder = builder; 18 | 19 | /** 20 | * Parent object. 21 | * @type {?ProtoBuf.Reflect.T} 22 | * @expose 23 | */ 24 | this.parent = parent; 25 | 26 | /** 27 | * Object name in namespace. 28 | * @type {string} 29 | * @expose 30 | */ 31 | this.name = name; 32 | 33 | /** 34 | * Fully qualified class name 35 | * @type {string} 36 | * @expose 37 | */ 38 | this.className; 39 | }; 40 | 41 | /** 42 | * @alias ProtoBuf.Reflect.T.prototype 43 | * @inner 44 | */ 45 | var TPrototype = T.prototype; 46 | 47 | /** 48 | * Returns the fully qualified name of this object. 49 | * @returns {string} Fully qualified name as of ".PATH.TO.THIS" 50 | * @expose 51 | */ 52 | TPrototype.fqn = function() { 53 | var name = this.name, 54 | ptr = this; 55 | do { 56 | ptr = ptr.parent; 57 | if (ptr == null) 58 | break; 59 | name = ptr.name+"."+name; 60 | } while (true); 61 | return name; 62 | }; 63 | 64 | /** 65 | * Returns a string representation of this Reflect object (its fully qualified name). 66 | * @param {boolean=} includeClass Set to true to include the class name. Defaults to false. 67 | * @return String representation 68 | * @expose 69 | */ 70 | TPrototype.toString = function(includeClass) { 71 | return (includeClass ? this.className + " " : "") + this.fqn(); 72 | }; 73 | 74 | /** 75 | * Builds this type. 76 | * @throws {Error} If this type cannot be built directly 77 | * @expose 78 | */ 79 | TPrototype.build = function() { 80 | throw Error(this.toString(true)+" cannot be built directly"); 81 | }; 82 | -------------------------------------------------------------------------------- /scripts/build.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Daniel Wirtz 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | var MetaScript = require("metascript"), 18 | path = require("path"), 19 | fs = require("fs"); 20 | 21 | var rootDir = path.join(__dirname, ".."), 22 | srcDir = path.join(rootDir, "src"), 23 | distDir = path.join(rootDir, "dist"), 24 | pkg = require(path.join(rootDir, "package.json")), 25 | filename; 26 | 27 | var scope = { 28 | VERSION: pkg.version, // Version 29 | DOTPROTO: true // Whether to include the ProtoBuf.DotProto package for .proto syntax support 30 | }; 31 | 32 | // Make full build 33 | console.log("Building protobuf.js with scope", JSON.stringify(scope, null, 2)); 34 | fs.writeFileSync( 35 | path.join(distDir, "protobuf.js"), 36 | MetaScript.transform(fs.readFileSync(filename = path.join(srcDir, "wrap.js")), filename, scope, srcDir) 37 | ); 38 | 39 | // Make light build 40 | scope.DOTPROTO = false; 41 | console.log("Building protobuf-light.js with scope", JSON.stringify(scope, null, 2)); 42 | fs.writeFileSync( 43 | path.join(distDir, "protobuf-light.js"), 44 | MetaScript.transform(fs.readFileSync(filename = path.join(srcDir, "wrap.js")), filename, scope, srcDir) 45 | ); 46 | 47 | // Update bower.json 48 | scope = { VERSION: pkg.version }; 49 | console.log("Updating bower.json with scope", JSON.stringify(scope, null, 2)); 50 | fs.writeFileSync( 51 | path.join(rootDir, "bower.json"), 52 | MetaScript.transform(fs.readFileSync(filename = path.join(srcDir, "bower.json")), filename, scope, srcDir) 53 | ); 54 | -------------------------------------------------------------------------------- /docs/styles/prettify-jsdoc.css: -------------------------------------------------------------------------------- 1 | /* JSDoc prettify.js theme */ 2 | 3 | /* plain text */ 4 | .pln { 5 | color: #000000; 6 | font-weight: normal; 7 | font-style: normal; 8 | } 9 | 10 | /* string content */ 11 | .str { 12 | color: #006400; 13 | font-weight: normal; 14 | font-style: normal; 15 | } 16 | 17 | /* a keyword */ 18 | .kwd { 19 | color: #000000; 20 | font-weight: bold; 21 | font-style: normal; 22 | } 23 | 24 | /* a comment */ 25 | .com { 26 | font-weight: normal; 27 | font-style: italic; 28 | } 29 | 30 | /* a type name */ 31 | .typ { 32 | color: #000000; 33 | font-weight: normal; 34 | font-style: normal; 35 | } 36 | 37 | /* a literal value */ 38 | .lit { 39 | color: #006400; 40 | font-weight: normal; 41 | font-style: normal; 42 | } 43 | 44 | /* punctuation */ 45 | .pun { 46 | color: #000000; 47 | font-weight: bold; 48 | font-style: normal; 49 | } 50 | 51 | /* lisp open bracket */ 52 | .opn { 53 | color: #000000; 54 | font-weight: bold; 55 | font-style: normal; 56 | } 57 | 58 | /* lisp close bracket */ 59 | .clo { 60 | color: #000000; 61 | font-weight: bold; 62 | font-style: normal; 63 | } 64 | 65 | /* a markup tag name */ 66 | .tag { 67 | color: #006400; 68 | font-weight: normal; 69 | font-style: normal; 70 | } 71 | 72 | /* a markup attribute name */ 73 | .atn { 74 | color: #006400; 75 | font-weight: normal; 76 | font-style: normal; 77 | } 78 | 79 | /* a markup attribute value */ 80 | .atv { 81 | color: #006400; 82 | font-weight: normal; 83 | font-style: normal; 84 | } 85 | 86 | /* a declaration */ 87 | .dec { 88 | color: #000000; 89 | font-weight: bold; 90 | font-style: normal; 91 | } 92 | 93 | /* a variable name */ 94 | .var { 95 | color: #000000; 96 | font-weight: normal; 97 | font-style: normal; 98 | } 99 | 100 | /* a function name */ 101 | .fun { 102 | color: #000000; 103 | font-weight: bold; 104 | font-style: normal; 105 | } 106 | 107 | /* Specify class=linenums on a pre to get line numbering */ 108 | ol.linenums { 109 | margin-top: 0; 110 | margin-bottom: 0; 111 | } 112 | -------------------------------------------------------------------------------- /cli/pbjs/targets/js.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Daniel Wirtz 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | var description = "Runtime structures"; 17 | 18 | var util = require("../util.js"), 19 | json = require("./json.js"); 20 | 21 | /** 22 | * pbjs target: Runtime structures 23 | * @exports pbjs/targets/js 24 | * @function 25 | * @param {!ProtoBuf.Builder} builder Builder 26 | * @param {!Object.=} options Options 27 | * @returns {string} 28 | */ 29 | var js = module.exports = function(builder, options) { 30 | options = options || {}; 31 | var varName = "_root"; 32 | if (options.exports) 33 | varName = options.exports.substring(options.exports.lastIndexOf(".")+1); 34 | return [ 35 | "var ", varName, options.min ? "=" : " = ", options.dependency || "dcodeIO.ProtoBuf", 36 | js.build(builder, options) 37 | ].join(''); 38 | }; 39 | 40 | /** 41 | * Builds the core js target. 42 | * @param {!ProtoBuf.Builder} builder Builder 43 | * @param {!Object.=} options Options 44 | * @returns {string} 45 | */ 46 | js.build = function(builder, options) { 47 | options = options || {}; 48 | return [ 49 | ".newBuilder(", 50 | JSON.stringify(util.getBuilderOptions(options, "use"), null, options.min ? 0 : 4), 51 | ")['import'](", 52 | json(builder, options), 53 | ").build(", 54 | typeof options.exports === 'string' ? JSON.stringify(options.exports.split(".")) : "", 55 | ");" 56 | ].join(''); 57 | }; 58 | 59 | /** 60 | * Module description. 61 | * @type {string} 62 | */ 63 | js.description = description; 64 | -------------------------------------------------------------------------------- /examples/websocket/www/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 15 | 16 | 17 |
18 | 19 | 20 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /src/ProtoBuf/Reflect/Enum.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Constructs a new Enum. 3 | * @exports ProtoBuf.Reflect.Enum 4 | * @param {!ProtoBuf.Builder} builder Builder reference 5 | * @param {!ProtoBuf.Reflect.T} parent Parent Reflect object 6 | * @param {string} name Enum name 7 | * @param {Object.=} options Enum options 8 | * @param {string?} syntax The syntax level (e.g., proto3) 9 | * @constructor 10 | * @extends ProtoBuf.Reflect.Namespace 11 | */ 12 | var Enum = function(builder, parent, name, options, syntax) { 13 | Namespace.call(this, builder, parent, name, options, syntax); 14 | 15 | /** 16 | * @override 17 | */ 18 | this.className = "Enum"; 19 | 20 | /** 21 | * Runtime enum object. 22 | * @type {Object.|null} 23 | * @expose 24 | */ 25 | this.object = null; 26 | }; 27 | 28 | /** 29 | * Gets the string name of an enum value. 30 | * @param {!ProtoBuf.Builder.Enum} enm Runtime enum 31 | * @param {number} value Enum value 32 | * @returns {?string} Name or `null` if not present 33 | * @expose 34 | */ 35 | Enum.getName = function(enm, value) { 36 | var keys = Object.keys(enm); 37 | for (var i=0, key; i} 53 | * @expose 54 | */ 55 | EnumPrototype.build = function(rebuild) { 56 | if (this.object && !rebuild) 57 | return this.object; 58 | var enm = new ProtoBuf.Builder.Enum(), 59 | values = this.getChildren(Enum.Value); 60 | for (var i=0, k=values.length; i", 6 | "contributors": [ 7 | "Frank Xu " 8 | ], 9 | "main": "./dist/protobuf.js", 10 | "bin": { 11 | "pbjs": "./bin/pbjs" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "https://github.com/dcodeIO/protobuf.js.git" 16 | }, 17 | "bugs": { 18 | "url": "https://github.com/dcodeIO/protobuf.js/issues" 19 | }, 20 | "homepage": "https://github.com/dcodeIO/protobuf.js", 21 | "keywords": [ 22 | "net", 23 | "buffer", 24 | "protobuf", 25 | "serialization", 26 | "bytebuffer", 27 | "websocket", 28 | "webrtc" 29 | ], 30 | "dependencies": { 31 | "ascli": "~1", 32 | "bytebuffer": "~5", 33 | "glob": "^5.0.10", 34 | "yargs": "^3.10.0" 35 | }, 36 | "devDependencies": { 37 | "testjs": "~1 >=1.0.4", 38 | "fixture-stdout": "^0.2.1", 39 | "metascript": ">=0.18 <1", 40 | "closurecompiler": "~1", 41 | "jsdoc": "~3.3.0-alpha10" 42 | }, 43 | "license": "Apache-2.0", 44 | "engines": { 45 | "node": ">=0.8" 46 | }, 47 | "browser": { 48 | "fs": false, 49 | "path": false 50 | }, 51 | "scripts": { 52 | "prepublish": "npm test", 53 | "test": "node bin/pbjs tests/complex.proto --target=json > tests/complex.json && node node_modules/testjs/bin/testjs tests/suite.js", 54 | "make": "npm run-script build && npm run-script compile && npm run-script descriptor2json && npm run-script compress && npm test && npm run-script jsdoc", 55 | "build": "node scripts/build.js", 56 | "descriptor2json": "node bin/pbjs src/google/protobuf/descriptor.proto --target=json > src/google/protobuf/descriptor.json", 57 | "compile": "npm run-script compile-full && npm run-script compile-light", 58 | "compile-full": "ccjs dist/protobuf.js --create_source_map=dist/protobuf.min.map --compilation_level=SIMPLE_OPTIMIZATIONS > dist/protobuf.min.js", 59 | "compile-light": "ccjs dist/protobuf-light.js --create_source_map=dist/protobuf-light.min.map --compilation_level=SIMPLE_OPTIMIZATIONS > dist/protobuf-light.min.js", 60 | "compress": "npm run-script compress-full && npm run-script compress-light", 61 | "compress-full": "gzip -c -9 dist/protobuf.min.js > dist/protobuf.min.js.gz", 62 | "compress-light": "gzip -c -9 dist/protobuf-light.min.js > dist/protobuf-light.min.js.gz", 63 | "jsdoc": "node node_modules/jsdoc/jsdoc.js -c jsdoc.json" 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /tests/imports.json: -------------------------------------------------------------------------------- 1 | { 2 | "package": "My", 3 | "messages": [ 4 | { 5 | "name": "Test3", 6 | "fields": [ 7 | { 8 | "rule": "required", 9 | "options": {}, 10 | "type": "Test1", 11 | "name": "test1", 12 | "id": 1 13 | }, 14 | { 15 | "rule": "required", 16 | "options": {}, 17 | "type": ".Test2", 18 | "name": "test2", 19 | "id": 2 20 | } 21 | ], 22 | "enums": [], 23 | "messages": [], 24 | "options": {}, 25 | "oneofs": {} 26 | } 27 | ], 28 | "enums": [], 29 | "imports": [ 30 | { 31 | "package": null, 32 | "messages": [ 33 | { 34 | "name": "Test1", 35 | "fields": [ 36 | { 37 | "rule": "required", 38 | "options": {}, 39 | "type": "int32", 40 | "name": "a", 41 | "id": 1 42 | } 43 | ], 44 | "enums": [], 45 | "messages": [], 46 | "options": {}, 47 | "oneofs": {} 48 | } 49 | ], 50 | "enums": [], 51 | "imports": [], 52 | "options": {}, 53 | "services": [] 54 | }, 55 | { 56 | "package": null, 57 | "messages": [ 58 | { 59 | "name": "Test2", 60 | "fields": [ 61 | { 62 | "rule": "required", 63 | "options": {}, 64 | "type": "string", 65 | "name": "b", 66 | "id": 2 67 | } 68 | ], 69 | "enums": [], 70 | "messages": [], 71 | "options": {}, 72 | "oneofs": {} 73 | } 74 | ], 75 | "enums": [], 76 | "imports": [], 77 | "options": {}, 78 | "services": [] 79 | } 80 | ], 81 | "options": {}, 82 | "services": [] 83 | } 84 | -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | JSDoc: Index 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 |

Index

21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 |

29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 |
49 | 50 | 53 | 54 |
55 | 56 |
57 | Documentation generated by JSDoc 3.3.0-alpha9 on Thu May 26 2016 19:11:32 GMT+0200 (Mitteleuropäische Sommerzeit) 58 |
59 | 60 | 61 | 62 | 63 | -------------------------------------------------------------------------------- /docs/styles/prettify-tomorrow.css: -------------------------------------------------------------------------------- 1 | /* Tomorrow Theme */ 2 | /* Original theme - https://github.com/chriskempson/tomorrow-theme */ 3 | /* Pretty printing styles. Used with prettify.js. */ 4 | /* SPAN elements with the classes below are added by prettyprint. */ 5 | /* plain text */ 6 | .pln { 7 | color: #4d4d4c; } 8 | 9 | @media screen { 10 | /* string content */ 11 | .str { 12 | color: #718c00; } 13 | 14 | /* a keyword */ 15 | .kwd { 16 | color: #8959a8; } 17 | 18 | /* a comment */ 19 | .com { 20 | color: #8e908c; } 21 | 22 | /* a type name */ 23 | .typ { 24 | color: #4271ae; } 25 | 26 | /* a literal value */ 27 | .lit { 28 | color: #f5871f; } 29 | 30 | /* punctuation */ 31 | .pun { 32 | color: #4d4d4c; } 33 | 34 | /* lisp open bracket */ 35 | .opn { 36 | color: #4d4d4c; } 37 | 38 | /* lisp close bracket */ 39 | .clo { 40 | color: #4d4d4c; } 41 | 42 | /* a markup tag name */ 43 | .tag { 44 | color: #c82829; } 45 | 46 | /* a markup attribute name */ 47 | .atn { 48 | color: #f5871f; } 49 | 50 | /* a markup attribute value */ 51 | .atv { 52 | color: #3e999f; } 53 | 54 | /* a declaration */ 55 | .dec { 56 | color: #f5871f; } 57 | 58 | /* a variable name */ 59 | .var { 60 | color: #c82829; } 61 | 62 | /* a function name */ 63 | .fun { 64 | color: #4271ae; } } 65 | /* Use higher contrast and text-weight for printable form. */ 66 | @media print, projection { 67 | .str { 68 | color: #060; } 69 | 70 | .kwd { 71 | color: #006; 72 | font-weight: bold; } 73 | 74 | .com { 75 | color: #600; 76 | font-style: italic; } 77 | 78 | .typ { 79 | color: #404; 80 | font-weight: bold; } 81 | 82 | .lit { 83 | color: #044; } 84 | 85 | .pun, .opn, .clo { 86 | color: #440; } 87 | 88 | .tag { 89 | color: #006; 90 | font-weight: bold; } 91 | 92 | .atn { 93 | color: #404; } 94 | 95 | .atv { 96 | color: #060; } } 97 | /* Style */ 98 | /* 99 | pre.prettyprint { 100 | background: white; 101 | font-family: Menlo, Monaco, Consolas, monospace; 102 | font-size: 12px; 103 | line-height: 1.5; 104 | border: 1px solid #ccc; 105 | padding: 10px; } 106 | */ 107 | 108 | /* Specify class=linenums on a pre to get line numbering */ 109 | ol.linenums { 110 | margin-top: 0; 111 | margin-bottom: 0; } 112 | 113 | /* IE indents via margin-left */ 114 | li.L0, 115 | li.L1, 116 | li.L2, 117 | li.L3, 118 | li.L4, 119 | li.L5, 120 | li.L6, 121 | li.L7, 122 | li.L8, 123 | li.L9 { 124 | /* */ } 125 | 126 | /* Alternate shading for lines */ 127 | li.L1, 128 | li.L3, 129 | li.L5, 130 | li.L7, 131 | li.L9 { 132 | /* */ } 133 | -------------------------------------------------------------------------------- /src/ProtoBuf/Reflect.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @alias ProtoBuf.Reflect 3 | * @expose 4 | */ 5 | ProtoBuf.Reflect = (function(ProtoBuf) { 6 | "use strict"; 7 | 8 | /** 9 | * Reflection types. 10 | * @exports ProtoBuf.Reflect 11 | * @namespace 12 | */ 13 | var Reflect = {}; 14 | 15 | //? include("Reflect/T.js"); 16 | 17 | /** 18 | * @alias ProtoBuf.Reflect.T 19 | * @expose 20 | */ 21 | Reflect.T = T; 22 | 23 | //? include("Reflect/Namespace.js"); 24 | 25 | /** 26 | * @alias ProtoBuf.Reflect.Namespace 27 | * @expose 28 | */ 29 | Reflect.Namespace = Namespace; 30 | 31 | //? include("Reflect/Element.js"); 32 | 33 | /** 34 | * @alias ProtoBuf.Reflect.Element 35 | * @expose 36 | */ 37 | Reflect.Element = Element; 38 | 39 | //? include("Reflect/Message.js"); 40 | 41 | /** 42 | * @alias ProtoBuf.Reflect.Message 43 | * @expose 44 | */ 45 | Reflect.Message = Message; 46 | 47 | //? include("Reflect/Message/Field.js"); 48 | 49 | /** 50 | * @alias ProtoBuf.Reflect.Message.Field 51 | * @expose 52 | */ 53 | Reflect.Message.Field = Field; 54 | 55 | //? include("Reflect/Message/ExtensionField.js"); 56 | 57 | /** 58 | * @alias ProtoBuf.Reflect.Message.ExtensionField 59 | * @expose 60 | */ 61 | Reflect.Message.ExtensionField = ExtensionField; 62 | 63 | //? include("Reflect/Message/OneOf.js"); 64 | 65 | /** 66 | * @alias ProtoBuf.Reflect.Message.OneOf 67 | * @expose 68 | */ 69 | Reflect.Message.OneOf = OneOf; 70 | 71 | //? include("Reflect/Enum.js"); 72 | 73 | /** 74 | * @alias ProtoBuf.Reflect.Enum 75 | * @expose 76 | */ 77 | Reflect.Enum = Enum; 78 | 79 | //? include("Reflect/Enum/Value.js"); 80 | 81 | /** 82 | * @alias ProtoBuf.Reflect.Enum.Value 83 | * @expose 84 | */ 85 | Reflect.Enum.Value = Value; 86 | 87 | //? include("Reflect/Extension.js"); 88 | 89 | /** 90 | * @alias ProtoBuf.Reflect.Extension 91 | * @expose 92 | */ 93 | Reflect.Extension = Extension; 94 | 95 | //? include("Reflect/Service.js"); 96 | 97 | /** 98 | * @alias ProtoBuf.Reflect.Service 99 | * @expose 100 | */ 101 | Reflect.Service = Service; 102 | 103 | //? include("Reflect/Service/Method.js"); 104 | 105 | /** 106 | * @alias ProtoBuf.Reflect.Service.Method 107 | * @expose 108 | */ 109 | Reflect.Service.Method = Method; 110 | 111 | //? include("Reflect/Service/RPCMethod.js"); 112 | 113 | /** 114 | * @alias ProtoBuf.Reflect.Service.RPCMethod 115 | * @expose 116 | */ 117 | Reflect.Service.RPCMethod = RPCMethod; 118 | 119 | return Reflect; 120 | 121 | })(ProtoBuf); 122 | -------------------------------------------------------------------------------- /cli/pbjs/sources/json.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Daniel Wirtz 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | var description = "Plain JSON descriptor"; 17 | 18 | var ProtoBuf = require(__dirname+"/../../../index.js"), 19 | util = require(__dirname+"/../util.js"), 20 | node_path = require("path"), 21 | fs = require("fs"); 22 | 23 | /** 24 | * pbjs source: Plain JSON descriptor 25 | * @exports pbjs/sources/json 26 | * @function 27 | * @param {!Array.} filenames Source files 28 | * @param {!Object.=} options Options 29 | * @returns {!ProtoBuf.Builder} 30 | */ 31 | var json = module.exports = function(filenames, options) { 32 | options = options || []; 33 | var builder = ProtoBuf.newBuilder(util.getBuilderOptions(options, "using")), 34 | loaded = []; 35 | filenames.forEach(function(filename) { 36 | var data = json.load(filename, options, loaded); 37 | builder["import"](data, filename); 38 | }); 39 | builder.resolveAll(); 40 | return builder; 41 | }; 42 | 43 | /** 44 | * Module description. 45 | * @type {string} 46 | */ 47 | json.description = description; 48 | 49 | /** 50 | * Loads a JSON descriptor including imports. 51 | * @param {string} filename Source file 52 | * @param {!Object.} options Options 53 | * @param {!Array.=} loaded An array of already loaded filenames 54 | * @returns {*} JSON descriptor 55 | */ 56 | json.load = function(filename, options, loaded) { 57 | filename = node_path.resolve(filename); 58 | loaded = loaded || []; 59 | if (loaded.indexOf(filename) >= 0) 60 | return {}; 61 | var data = JSON.parse(fs.readFileSync(filename).toString("utf8")), 62 | imports = data['imports']; 63 | loaded.push(filename); 64 | if (Array.isArray(imports)) { 65 | for (var i=0; i 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | var description = "Plain .proto descriptor"; 17 | 18 | var ProtoBuf = require(__dirname+"/../../../index.js"), 19 | util = require(__dirname+"/../util.js"), 20 | fs = require("fs"), 21 | node_path = require("path"); 22 | 23 | /** 24 | * pbjs source: Plain .proto descriptor 25 | * @exports pbjs/sources/proto 26 | * @function 27 | * @param {!Array.} filenames Source files 28 | * @param {!Object.=} options Options 29 | * @returns {!ProtoBuf.Builder} 30 | */ 31 | var proto = module.exports = function(filenames, options) { 32 | options = options || []; 33 | var builder = ProtoBuf.newBuilder(util.getBuilderOptions(options, "using")), 34 | loaded = []; 35 | filenames.forEach(function(filename) { 36 | var data = proto.load(filename, options, loaded); 37 | builder["import"](data, filename); 38 | }); 39 | builder.resolveAll(); 40 | return builder; 41 | }; 42 | 43 | /** 44 | * Module description. 45 | * @type {string} 46 | */ 47 | proto.description = description; 48 | 49 | /** 50 | * Loads a .proto descriptor including imports. 51 | * @param {string} filename Source file 52 | * @param {!Object.} options Options 53 | * @param {!Array.=} loaded An array of already loaded filenames 54 | * @returns {*} JSON descriptor 55 | */ 56 | proto.load = function(filename, options, loaded) { 57 | filename = node_path.resolve(filename); 58 | loaded = loaded || []; 59 | if (loaded.indexOf(filename) >= 0) 60 | return {}; 61 | var data = ProtoBuf.DotProto.Parser.parse(fs.readFileSync(filename).toString("utf8")); 62 | loaded.push(filename); 63 | if (Array.isArray(data['imports'])) { 64 | var imports = data['imports']; 65 | for (var i=0; i 53 | ``` 54 | With `VERSION` replaced by [a valid tag](https://github.com/dcodeIO/protobuf.js/releases) or just `master` for testing 55 | the latest master build. 56 | 57 | Contributors 58 | ------------ 59 | [Daniel Wirtz](https://github.com/dcodeIO) (maintainer), [Frank Xu](https://github.com/yyfrankyy), 60 | [Dretch](https://github.com/Dretch), [shirmin](https://github.com/shirmin), [Nikolai Vavilov](https://github.com/seishun) 61 | 62 | **License:** [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0.html) - Logo derived from [W3C HTML5 Logos](http://www.w3.org/html/logo/) (CC A 3.0) 63 | -------------------------------------------------------------------------------- /tests/complex.json: -------------------------------------------------------------------------------- 1 | { 2 | "package": "Game.Cars", 3 | "messages": [ 4 | { 5 | "name": "Car", 6 | "fields": [ 7 | { 8 | "rule": "required", 9 | "type": "string", 10 | "name": "model", 11 | "id": 1 12 | }, 13 | { 14 | "rule": "required", 15 | "type": "Vendor", 16 | "name": "vendor", 17 | "id": 2 18 | }, 19 | { 20 | "rule": "optional", 21 | "type": "Speed", 22 | "name": "speed", 23 | "id": 3, 24 | "options": { 25 | "default": "FAST" 26 | } 27 | } 28 | ], 29 | "messages": [ 30 | { 31 | "name": "Vendor", 32 | "fields": [ 33 | { 34 | "rule": "required", 35 | "type": "string", 36 | "name": "name", 37 | "id": 1 38 | }, 39 | { 40 | "rule": "optional", 41 | "type": "Address", 42 | "name": "address", 43 | "id": 2 44 | }, 45 | { 46 | "rule": "repeated", 47 | "type": "string", 48 | "name": "models", 49 | "id": 3 50 | } 51 | ], 52 | "messages": [ 53 | { 54 | "name": "Address", 55 | "fields": [ 56 | { 57 | "rule": "required", 58 | "type": "string", 59 | "name": "country", 60 | "id": 1 61 | } 62 | ] 63 | } 64 | ] 65 | }, 66 | { 67 | "name": "Holder", 68 | "fields": [ 69 | { 70 | "rule": "optional", 71 | "type": "string", 72 | "name": "first_name", 73 | "id": 1 74 | }, 75 | { 76 | "rule": "required", 77 | "type": "string", 78 | "name": "last_name", 79 | "id": 2 80 | }, 81 | { 82 | "rule": "optional", 83 | "type": "Vendor.Address", 84 | "name": "address", 85 | "id": 3 86 | } 87 | ] 88 | } 89 | ], 90 | "enums": [ 91 | { 92 | "name": "Speed", 93 | "values": [ 94 | { 95 | "name": "FAST", 96 | "id": 1 97 | }, 98 | { 99 | "name": "SUPERFAST", 100 | "id": 2 101 | } 102 | ] 103 | } 104 | ] 105 | } 106 | ] 107 | } -------------------------------------------------------------------------------- /examples/protoify/json.json: -------------------------------------------------------------------------------- 1 | { 2 | "package": "js", 3 | "messages": [ 4 | { 5 | "name": "Value", 6 | "fields": [ 7 | { 8 | "rule": "optional", 9 | "options": {}, 10 | "type": "sint32", 11 | "name": "integer", 12 | "id": 1, 13 | "oneof": "type" 14 | }, 15 | { 16 | "rule": "optional", 17 | "options": {}, 18 | "type": "double", 19 | "name": "double", 20 | "id": 2, 21 | "oneof": "type" 22 | }, 23 | { 24 | "rule": "optional", 25 | "options": {}, 26 | "type": "string", 27 | "name": "string", 28 | "id": 3, 29 | "oneof": "type" 30 | }, 31 | { 32 | "rule": "optional", 33 | "options": {}, 34 | "type": "bool", 35 | "name": "boolean", 36 | "id": 4, 37 | "oneof": "type" 38 | }, 39 | { 40 | "rule": "optional", 41 | "options": {}, 42 | "type": "bool", 43 | "name": "null", 44 | "id": 5, 45 | "oneof": "type" 46 | }, 47 | { 48 | "rule": "optional", 49 | "options": {}, 50 | "type": "Array", 51 | "name": "array", 52 | "id": 6, 53 | "oneof": "type" 54 | }, 55 | { 56 | "rule": "optional", 57 | "options": {}, 58 | "type": "Object", 59 | "name": "object", 60 | "id": 7, 61 | "oneof": "type" 62 | } 63 | ], 64 | "enums": [], 65 | "messages": [], 66 | "options": {}, 67 | "oneofs": { 68 | "type": [ 69 | 1, 70 | 2, 71 | 3, 72 | 4, 73 | 5, 74 | 6, 75 | 7 76 | ] 77 | } 78 | }, 79 | { 80 | "name": "Array", 81 | "fields": [ 82 | { 83 | "rule": "repeated", 84 | "options": {}, 85 | "type": "Value", 86 | "name": "values", 87 | "id": 1 88 | } 89 | ], 90 | "enums": [], 91 | "messages": [], 92 | "options": {}, 93 | "oneofs": {} 94 | }, 95 | { 96 | "name": "Object", 97 | "fields": [ 98 | { 99 | "rule": "repeated", 100 | "options": {}, 101 | "type": "Value", 102 | "name": "keys", 103 | "id": 1 104 | }, 105 | { 106 | "rule": "repeated", 107 | "options": {}, 108 | "type": "Value", 109 | "name": "values", 110 | "id": 2 111 | } 112 | ], 113 | "enums": [], 114 | "messages": [], 115 | "options": {}, 116 | "oneofs": {} 117 | } 118 | ], 119 | "enums": [], 120 | "imports": [], 121 | "options": {}, 122 | "services": [] 123 | } 124 | -------------------------------------------------------------------------------- /examples/protoify/json.js: -------------------------------------------------------------------------------- 1 | module.exports = require("protobufjs").newBuilder({})["import"]({ 2 | "package": "js", 3 | "messages": [ 4 | { 5 | "name": "Value", 6 | "fields": [ 7 | { 8 | "rule": "optional", 9 | "options": {}, 10 | "type": "sint32", 11 | "name": "integer", 12 | "id": 1, 13 | "oneof": "type" 14 | }, 15 | { 16 | "rule": "optional", 17 | "options": {}, 18 | "type": "double", 19 | "name": "double", 20 | "id": 2, 21 | "oneof": "type" 22 | }, 23 | { 24 | "rule": "optional", 25 | "options": {}, 26 | "type": "string", 27 | "name": "string", 28 | "id": 3, 29 | "oneof": "type" 30 | }, 31 | { 32 | "rule": "optional", 33 | "options": {}, 34 | "type": "bool", 35 | "name": "boolean", 36 | "id": 4, 37 | "oneof": "type" 38 | }, 39 | { 40 | "rule": "optional", 41 | "options": {}, 42 | "type": "bool", 43 | "name": "null", 44 | "id": 5, 45 | "oneof": "type" 46 | }, 47 | { 48 | "rule": "optional", 49 | "options": {}, 50 | "type": "Array", 51 | "name": "array", 52 | "id": 6, 53 | "oneof": "type" 54 | }, 55 | { 56 | "rule": "optional", 57 | "options": {}, 58 | "type": "Object", 59 | "name": "object", 60 | "id": 7, 61 | "oneof": "type" 62 | } 63 | ], 64 | "enums": [], 65 | "messages": [], 66 | "options": {}, 67 | "oneofs": { 68 | "type": [ 69 | 1, 70 | 2, 71 | 3, 72 | 4, 73 | 5, 74 | 6, 75 | 7 76 | ] 77 | } 78 | }, 79 | { 80 | "name": "Array", 81 | "fields": [ 82 | { 83 | "rule": "repeated", 84 | "options": {}, 85 | "type": "Value", 86 | "name": "values", 87 | "id": 1 88 | } 89 | ], 90 | "enums": [], 91 | "messages": [], 92 | "options": {}, 93 | "oneofs": {} 94 | }, 95 | { 96 | "name": "Object", 97 | "fields": [ 98 | { 99 | "rule": "repeated", 100 | "options": {}, 101 | "type": "Value", 102 | "name": "keys", 103 | "id": 1 104 | }, 105 | { 106 | "rule": "repeated", 107 | "options": {}, 108 | "type": "Value", 109 | "name": "values", 110 | "id": 2 111 | } 112 | ], 113 | "enums": [], 114 | "messages": [], 115 | "options": {}, 116 | "oneofs": {} 117 | } 118 | ], 119 | "enums": [], 120 | "imports": [], 121 | "options": {}, 122 | "services": [] 123 | }).build("js"); 124 | -------------------------------------------------------------------------------- /docs/ProtoBuf.DotProto.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | JSDoc: Namespace: DotProto 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 |

Namespace: DotProto

21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 |
29 |

30 | ProtoBuf. 31 | 32 | DotProto 33 |

34 | 35 |
36 | 37 |
38 |
39 | 40 | 41 | 42 | 43 |

Utilities to parse .proto files.

44 | 45 | 46 | 47 |
48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 |
Source:
68 |
71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 |
79 | 80 | 81 | 82 | 83 |
84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 |

Classes

93 | 94 |
95 |
Parser
96 |
97 | 98 |
Tokenizer
99 |
100 |
101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 |
115 | 116 |
117 | 118 | 119 | 120 | 121 |
122 | 123 | 126 | 127 |
128 | 129 |
130 | Documentation generated by JSDoc 3.3.0-alpha9 on Thu May 26 2016 19:11:32 GMT+0200 (Mitteleuropäische Sommerzeit) 131 |
132 | 133 | 134 | 135 | 136 | -------------------------------------------------------------------------------- /cli/pbjs/util.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Daniel Wirtz 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | var ProtoBuf = require("../../index.js"); 17 | 18 | /** 19 | * Utility namespace. 20 | * @exports pbjs/util 21 | * @namespace 22 | */ 23 | var util = module.exports = {}; 24 | 25 | /** 26 | * Extracts builder options with the specified prefix from a set of CLI options. 27 | * @param {!Object.} options CLI options 28 | * @param {string} prefix Prefix 29 | * @returns {!Object.} 30 | */ 31 | util.getBuilderOptions = function(options, prefix) { 32 | if (!options[prefix]) 33 | return {}; 34 | var builderOptions = {}; 35 | options[prefix].forEach(function(kv) { 36 | var key, val; 37 | var p = kv.indexOf("="); 38 | if (p < 0) { 39 | key = kv; 40 | val = true; 41 | } else { 42 | key = kv.substring(0, p); 43 | val = kv.substring(p+1); 44 | if (val === "true") 45 | val = true; 46 | else if (val === "false") 47 | val = false; 48 | else { 49 | var intval = parseInt(val, 10); 50 | if (intval == val) 51 | val = intval; 52 | } 53 | } 54 | builderOptions[key] = val; 55 | }); 56 | return builderOptions; 57 | }; 58 | 59 | /** 60 | * Pads a string to the specified length. 61 | * @param {string} str String to pad 62 | * @param {number} len Pad length 63 | * @param {boolean=} left Whether to pad to the left, defaults to `false` 64 | * @returns {string} 65 | */ 66 | util.pad = function(str, len, left) { 67 | while (str.length < len) 68 | left ? str = " "+str : str += " "; 69 | return str; 70 | }; 71 | 72 | /** 73 | * Indents a string by the specified whitespace. 74 | * @param {string} str String to indent 75 | * @param {string|number} ws Whitespace string or number of whitespaces 76 | * @returns {string} 77 | */ 78 | util.indent = function(str, ws) { 79 | if (ws === 0 || ws === "") 80 | return str; 81 | var lines = str.split(/\r?\n/); 82 | if (typeof ws === 'number') { 83 | var n = ws; ws = ""; 84 | while (ws.length < n) ws += " "; 85 | } 86 | for (var i=1; i} subject Subject to extend 94 | * @param {!Object.} extension Extensions to apply 95 | */ 96 | util.extend = function(subject, extension) { 97 | Object.keys(extension).forEach(function(key) { 98 | subject[key] = extension[key]; 99 | }); 100 | }; 101 | 102 | /** 103 | * Groups extensions by extended message. 104 | * @param {!ProtoBuf.Reflect.Namespace} ns Namespace 105 | * @returns {?Object.>} 106 | */ 107 | util.groupExtensions = function(ns) { 108 | var exts = {}, 109 | n = 0; 110 | ns.getChildren(ProtoBuf.Reflect.Extension).forEach(function(ext) { 111 | var msg = ext.field.parent, 112 | fqn = msg.fqn(); 113 | if (!exts[fqn]) 114 | exts[fqn] = []; 115 | exts[fqn].push(ext.field); 116 | n++; 117 | }); 118 | return n > 0 ? exts : null; 119 | }; 120 | 121 | /** 122 | * Tests if the specified import name is referencing an internal descriptor. 123 | * @param {string} name Import name 124 | * @returns {boolean} 125 | */ 126 | util.isDescriptor = function(name) { 127 | return /^google\/protobuf\/descriptor/.test(name); 128 | }; -------------------------------------------------------------------------------- /cli/pbjs/sources/binary.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Daniel Wirtz 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | var description = "Binary descriptor set"; 17 | 18 | var ProtoBuf = require(__dirname+"/../../../index.js"), 19 | util = require(__dirname+"/../util.js"), 20 | node_path = require("path"), 21 | fs = require("fs"); 22 | 23 | /** 24 | * pbjs source: Binary descriptor 25 | * @exports pbjs/sources/binary 26 | * @function 27 | * @param {!Array.} filenames Source files 28 | * @param {!Object.=} options Options 29 | * @returns {!ProtoBuf.Builder} 30 | */ 31 | var binary = module.exports = function(filenames, options) { 32 | options = options || []; 33 | var builder = ProtoBuf.newBuilder(util.getBuilderOptions(options, "using")), 34 | loaded = []; 35 | filenames.forEach(function(filename) { 36 | var data = binary.load(filename, options, loaded); 37 | builder["import"](data, filename); 38 | }); 39 | builder.resolveAll(); 40 | return builder; 41 | }; 42 | 43 | /** 44 | * Module description. 45 | * @type {string} 46 | */ 47 | binary.description = description; 48 | 49 | binary.exclude = true; // Unfinished 50 | 51 | /** 52 | * Loads a binary descriptor. 53 | * @param {string} filename Source file 54 | * @param {!Object.} options Options 55 | * @param {!Array.=} loaded An array of already loaded filenames 56 | * @returns {*} JSON descriptor 57 | */ 58 | binary.load = function(filename, options, loaded) { 59 | filename = node_path.resolve(filename); 60 | loaded = loaded || []; 61 | if (loaded.indexOf(filename) >= 0) 62 | return {}; 63 | var data = fs.readFileSync(filename); 64 | loaded.push(filename); 65 | var builder = ProtoBuf.loadProtoFile(node_path.join("..", "..", "..", "src", "google", "protobuf", "descriptor.proto")), 66 | FileDescriptorSet = builder.build("google.protobuf.FileDescriptorSet"); 67 | var fds = FileDescriptorSet.decode(data), 68 | imports = []; 69 | var json = { 70 | "package": null, 71 | "imports": imports 72 | }; 73 | fds.file.forEach(function(fdp) { 74 | imports.push(buildFileDescriptorProto(fdp)); 75 | }); 76 | return json; 77 | }; 78 | 79 | function buildFileDescriptorProto(fdp) { 80 | var pkg = fdp.package, 81 | messages = [], 82 | enums = [], 83 | services = [], 84 | extensions = [], 85 | options = {}, 86 | imports = []; 87 | fdp.message_type.forEach(function(dp) { 88 | messages.push(buildMessageDescriptorProto(dp)); 89 | }); 90 | fdp.enum_type.forEach(function(edp) { 91 | enums.push(buildEnumDescriptorProto(edp)); 92 | }); 93 | fdp.service.forEach(function(sdp) { 94 | enums.push(buildServiceDescriptorProto(sdp)); 95 | }); 96 | fdp.extension.forEach(function(fdp) { 97 | extensions.push(buildFieldDescriptorProtoAsExtension(fdp)); 98 | }); 99 | fdp.options.forEach(function(fo) { 100 | // TODO 101 | }); 102 | fdp.dependency.forEach(function(filename) { 103 | // TODO 104 | }); 105 | return { 106 | "package": pkg, 107 | "messages": messages, 108 | "enums": enums, 109 | "services": services, 110 | "extensions": extensions, 111 | "options": options, 112 | "imports": imports 113 | }; 114 | } 115 | 116 | function buildMessageDescriptorProto(mdp) { 117 | 118 | } 119 | 120 | function buildEnumDescriptorProto(edp) { 121 | 122 | } 123 | 124 | function buildServiceDescriptorProto(sdp) { 125 | 126 | } 127 | 128 | function buildFieldDescriptorProtoAsExtension(fdp) { 129 | 130 | } -------------------------------------------------------------------------------- /src/ProtoBuf/Util.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @alias ProtoBuf.Util 3 | * @expose 4 | */ 5 | ProtoBuf.Util = (function() { 6 | "use strict"; 7 | 8 | /** 9 | * ProtoBuf utilities. 10 | * @exports ProtoBuf.Util 11 | * @namespace 12 | */ 13 | var Util = {}; 14 | 15 | /** 16 | * Flag if running in node or not. 17 | * @type {boolean} 18 | * @const 19 | * @expose 20 | */ 21 | Util.IS_NODE = !!( 22 | typeof process === 'object' && process+'' === '[object process]' && !process['browser'] 23 | ); 24 | 25 | /** 26 | * Constructs a XMLHttpRequest object. 27 | * @return {XMLHttpRequest} 28 | * @throws {Error} If XMLHttpRequest is not supported 29 | * @expose 30 | */ 31 | Util.XHR = function() { 32 | // No dependencies please, ref: http://www.quirksmode.org/js/xmlhttp.html 33 | var XMLHttpFactories = [ 34 | function () {return new XMLHttpRequest()}, 35 | function () {return new ActiveXObject("Msxml2.XMLHTTP")}, 36 | function () {return new ActiveXObject("Msxml3.XMLHTTP")}, 37 | function () {return new ActiveXObject("Microsoft.XMLHTTP")} 38 | ]; 39 | /** @type {?XMLHttpRequest} */ 40 | var xhr = null; 41 | for (var i=0;i 2 | 3 | 4 | 5 | JSDoc: Namespace: Reflect 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 |

Namespace: Reflect

21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 |
29 |

30 | ProtoBuf. 31 | 32 | Reflect 33 |

34 | 35 |
36 | 37 |
38 |
39 | 40 | 41 | 42 | 43 |

Reflection types.

44 | 45 | 46 | 47 |
48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 |
Source:
68 |
71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 |
79 | 80 | 81 | 82 | 83 |
84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 |

Classes

93 | 94 |
95 |
Element
96 |
97 | 98 |
Enum
99 |
100 | 101 |
Extension
102 |
103 | 104 |
Message
105 |
106 | 107 |
Namespace
108 |
109 | 110 |
Service
111 |
112 | 113 |
T
114 |
115 |
116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 |
130 | 131 |
132 | 133 | 134 | 135 | 136 |
137 | 138 | 141 | 142 |
143 | 144 |
145 | Documentation generated by JSDoc 3.3.0-alpha9 on Thu May 26 2016 19:11:32 GMT+0200 (Mitteleuropäische Sommerzeit) 146 |
147 | 148 | 149 | 150 | 151 | -------------------------------------------------------------------------------- /tests/custom-options.json: -------------------------------------------------------------------------------- 1 | { 2 | "package": null, 3 | "messages": [ 4 | { 5 | "ref": "google.protobuf.FileOptions", 6 | "fields": [ 7 | { 8 | "rule": "optional", 9 | "options": {}, 10 | "type": "string", 11 | "name": "my_file_option", 12 | "id": 50000 13 | } 14 | ] 15 | }, 16 | { 17 | "ref": "google.protobuf.MessageOptions", 18 | "fields": [ 19 | { 20 | "rule": "optional", 21 | "options": {}, 22 | "type": "int32", 23 | "name": "my_message_option", 24 | "id": 50001 25 | } 26 | ] 27 | }, 28 | { 29 | "ref": "google.protobuf.FieldOptions", 30 | "fields": [ 31 | { 32 | "rule": "optional", 33 | "options": {}, 34 | "type": "float", 35 | "name": "my_field_option", 36 | "id": 50002 37 | } 38 | ] 39 | }, 40 | { 41 | "ref": "google.protobuf.EnumOptions", 42 | "fields": [ 43 | { 44 | "rule": "optional", 45 | "options": {}, 46 | "type": "bool", 47 | "name": "my_enum_option", 48 | "id": 50003 49 | } 50 | ] 51 | }, 52 | { 53 | "ref": "google.protobuf.EnumValueOptions", 54 | "fields": [ 55 | { 56 | "rule": "optional", 57 | "options": {}, 58 | "type": "uint32", 59 | "name": "my_enum_value_option", 60 | "id": 50004 61 | } 62 | ] 63 | }, 64 | { 65 | "ref": "google.protobuf.ServiceOptions", 66 | "fields": [ 67 | { 68 | "rule": "optional", 69 | "options": {}, 70 | "type": "MyEnum", 71 | "name": "my_service_option", 72 | "id": 50005 73 | } 74 | ] 75 | }, 76 | { 77 | "ref": "google.protobuf.MethodOptions", 78 | "fields": [ 79 | { 80 | "rule": "optional", 81 | "options": {}, 82 | "type": "MyMessage", 83 | "name": "my_method_option", 84 | "id": 50006 85 | } 86 | ] 87 | }, 88 | { 89 | "name": "MyMessage", 90 | "fields": [ 91 | { 92 | "rule": "optional", 93 | "options": { 94 | "(my_field_option)": 4.5 95 | }, 96 | "type": "int32", 97 | "name": "foo", 98 | "id": 1 99 | }, 100 | { 101 | "rule": "optional", 102 | "options": {}, 103 | "type": "string", 104 | "name": "bar", 105 | "id": 2 106 | } 107 | ], 108 | "enums": [], 109 | "messages": [], 110 | "options": { 111 | "(my_message_option)": 1234 112 | } 113 | }, 114 | { 115 | "name": "RequestType", 116 | "fields": [], 117 | "enums": [], 118 | "messages": [], 119 | "options": {} 120 | }, 121 | { 122 | "name": "ResponseType", 123 | "fields": [], 124 | "enums": [], 125 | "messages": [], 126 | "options": {} 127 | } 128 | ], 129 | "enums": [ 130 | { 131 | "name": "MyEnum", 132 | "values": [ 133 | { 134 | "name": "FOO", 135 | "id": 1 136 | }, 137 | { 138 | "name": "BAR", 139 | "id": 2 140 | } 141 | ], 142 | "options": { 143 | "(my_enum_option)": true 144 | } 145 | } 146 | ], 147 | "imports": [], 148 | "options": { 149 | "(my_file_option)": "Hello world!" 150 | }, 151 | "services": [ 152 | { 153 | "name": "MyService", 154 | "rpc": { 155 | "MyMethod": { 156 | "request": "RequestType", 157 | "response": "ResponseType", 158 | "options": { 159 | "(my_method_option).foo": 567, 160 | "(my_method_option).bar": "Some string" 161 | } 162 | } 163 | }, 164 | "options": { 165 | "(my_service_option)": "FOO" 166 | } 167 | } 168 | ] 169 | } 170 | -------------------------------------------------------------------------------- /src/ProtoBuf/DotProto/Tokenizer.js: -------------------------------------------------------------------------------- 1 | /*? 2 | // --- Scope ----------------- 3 | // Lang : Language expressions 4 | */ 5 | /** 6 | * Constructs a new Tokenizer. 7 | * @exports ProtoBuf.DotProto.Tokenizer 8 | * @class prototype tokenizer 9 | * @param {string} proto Proto to tokenize 10 | * @constructor 11 | */ 12 | var Tokenizer = function(proto) { 13 | 14 | /** 15 | * Source to parse. 16 | * @type {string} 17 | * @expose 18 | */ 19 | this.source = proto+""; 20 | 21 | /** 22 | * Current index. 23 | * @type {number} 24 | * @expose 25 | */ 26 | this.index = 0; 27 | 28 | /** 29 | * Current line. 30 | * @type {number} 31 | * @expose 32 | */ 33 | this.line = 1; 34 | 35 | /** 36 | * Token stack. 37 | * @type {!Array.} 38 | * @expose 39 | */ 40 | this.stack = []; 41 | 42 | /** 43 | * Opening character of the current string read, if any. 44 | * @type {?string} 45 | * @private 46 | */ 47 | this._stringOpen = null; 48 | }; 49 | 50 | /** 51 | * @alias ProtoBuf.DotProto.Tokenizer.prototype 52 | * @inner 53 | */ 54 | var TokenizerPrototype = Tokenizer.prototype; 55 | 56 | /** 57 | * Reads a string beginning at the current index. 58 | * @return {string} 59 | * @private 60 | */ 61 | TokenizerPrototype._readString = function() { 62 | var re = this._stringOpen === '"' 63 | ? Lang.STRING_DQ 64 | : Lang.STRING_SQ; 65 | re.lastIndex = this.index - 1; // Include the open quote 66 | var match = re.exec(this.source); 67 | if (!match) 68 | throw Error("unterminated string"); 69 | this.index = re.lastIndex; 70 | this.stack.push(this._stringOpen); 71 | this._stringOpen = null; 72 | return match[1]; 73 | }; 74 | 75 | /** 76 | * Gets the next token and advances by one. 77 | * @return {?string} Token or `null` on EOF 78 | * @expose 79 | */ 80 | TokenizerPrototype.next = function() { 81 | if (this.stack.length > 0) 82 | return this.stack.shift(); 83 | if (this.index >= this.source.length) 84 | return null; 85 | if (this._stringOpen !== null) 86 | return this._readString(); 87 | 88 | var repeat, 89 | prev, 90 | next; 91 | do { 92 | repeat = false; 93 | 94 | // Strip white spaces 95 | while (Lang.WHITESPACE.test(next = this.source.charAt(this.index))) { 96 | if (next === '\n') 97 | ++this.line; 98 | if (++this.index === this.source.length) 99 | return null; 100 | } 101 | 102 | // Strip comments 103 | if (this.source.charAt(this.index) === '/') { 104 | ++this.index; 105 | if (this.source.charAt(this.index) === '/') { // Line 106 | while (this.source.charAt(++this.index) !== '\n') 107 | if (this.index == this.source.length) 108 | return null; 109 | ++this.index; 110 | ++this.line; 111 | repeat = true; 112 | } else if ((next = this.source.charAt(this.index)) === '*') { /* Block */ 113 | do { 114 | if (next === '\n') 115 | ++this.line; 116 | if (++this.index === this.source.length) 117 | return null; 118 | prev = next; 119 | next = this.source.charAt(this.index); 120 | } while (prev !== '*' || next !== '/'); 121 | ++this.index; 122 | repeat = true; 123 | } else 124 | return '/'; 125 | } 126 | } while (repeat); 127 | 128 | if (this.index === this.source.length) 129 | return null; 130 | 131 | // Read the next token 132 | var end = this.index; 133 | Lang.DELIM.lastIndex = 0; 134 | var delim = Lang.DELIM.test(this.source.charAt(end++)); 135 | if (!delim) 136 | while(end < this.source.length && !Lang.DELIM.test(this.source.charAt(end))) 137 | ++end; 138 | var token = this.source.substring(this.index, this.index = end); 139 | if (token === '"' || token === "'") 140 | this._stringOpen = token; 141 | return token; 142 | }; 143 | 144 | /** 145 | * Peeks for the next token. 146 | * @return {?string} Token or `null` on EOF 147 | * @expose 148 | */ 149 | TokenizerPrototype.peek = function() { 150 | if (this.stack.length === 0) { 151 | var token = this.next(); 152 | if (token === null) 153 | return null; 154 | this.stack.push(token); 155 | } 156 | return this.stack[0]; 157 | }; 158 | 159 | /** 160 | * Skips a specific token and throws if it differs. 161 | * @param {string} expected Expected token 162 | * @throws {Error} If the actual token differs 163 | */ 164 | TokenizerPrototype.skip = function(expected) { 165 | var actual = this.next(); 166 | if (actual !== expected) 167 | throw Error("illegal '"+actual+"', '"+expected+"' expected"); 168 | }; 169 | 170 | /** 171 | * Omits an optional token. 172 | * @param {string} expected Expected optional token 173 | * @returns {boolean} `true` if the token exists 174 | */ 175 | TokenizerPrototype.omit = function(expected) { 176 | if (this.peek() === expected) { 177 | this.next(); 178 | return true; 179 | } 180 | return false; 181 | }; 182 | 183 | /** 184 | * Returns a string representation of this object. 185 | * @return {string} String representation as of "Tokenizer(index/length)" 186 | * @expose 187 | */ 188 | TokenizerPrototype.toString = function() { 189 | return "Tokenizer ("+this.index+"/"+this.source.length+" at line "+this.line+")"; 190 | }; 191 | -------------------------------------------------------------------------------- /src/ProtoBuf/Builder/Service.js: -------------------------------------------------------------------------------- 1 | /*? 2 | // --- Scope ------------------ 3 | // T : Reflect.Service instance 4 | */ 5 | /** 6 | * Constructs a new runtime Service. 7 | * @name ProtoBuf.Builder.Service 8 | * @param {function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))=} rpcImpl RPC implementation receiving the method name and the message 9 | * @class Barebone of all runtime services. 10 | * @constructor 11 | * @throws {Error} If the service cannot be created 12 | */ 13 | var Service = function(rpcImpl) { 14 | ProtoBuf.Builder.Service.call(this); 15 | 16 | /** 17 | * Service implementation. 18 | * @name ProtoBuf.Builder.Service#rpcImpl 19 | * @type {!function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))} 20 | * @expose 21 | */ 22 | this.rpcImpl = rpcImpl || function(name, msg, callback) { 23 | // This is what a user has to implement: A function receiving the method name, the actual message to 24 | // send (type checked) and the callback that's either provided with the error as its first 25 | // argument or null and the actual response message. 26 | setTimeout(callback.bind(this, Error("Not implemented, see: https://github.com/dcodeIO/ProtoBuf.js/wiki/Services")), 0); // Must be async! 27 | }; 28 | }; 29 | 30 | /** 31 | * @alias ProtoBuf.Builder.Service.prototype 32 | * @inner 33 | */ 34 | var ServicePrototype = Service.prototype = Object.create(ProtoBuf.Builder.Service.prototype); 35 | 36 | /** 37 | * Asynchronously performs an RPC call using the given RPC implementation. 38 | * @name ProtoBuf.Builder.Service.[Method] 39 | * @function 40 | * @param {!function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))} rpcImpl RPC implementation 41 | * @param {ProtoBuf.Builder.Message} req Request 42 | * @param {function(Error, (ProtoBuf.Builder.Message|ByteBuffer|Buffer|string)=)} callback Callback receiving 43 | * the error if any and the response either as a pre-parsed message or as its raw bytes 44 | * @abstract 45 | */ 46 | 47 | /** 48 | * Asynchronously performs an RPC call using the instance's RPC implementation. 49 | * @name ProtoBuf.Builder.Service#[Method] 50 | * @function 51 | * @param {ProtoBuf.Builder.Message} req Request 52 | * @param {function(Error, (ProtoBuf.Builder.Message|ByteBuffer|Buffer|string)=)} callback Callback receiving 53 | * the error if any and the response either as a pre-parsed message or as its raw bytes 54 | * @abstract 55 | */ 56 | 57 | var rpc = T.getChildren(ProtoBuf.Reflect.Service.RPCMethod); 58 | for (var i=0; i} 112 | * @expose 113 | */ 114 | var $optionsS; // cc needs this 115 | 116 | /** 117 | * Service options. 118 | * @name ProtoBuf.Builder.Service#$options 119 | * @type {Object.} 120 | * @expose 121 | */ 122 | var $options; 123 | 124 | /** 125 | * Reflection type. 126 | * @name ProtoBuf.Builder.Service.$type 127 | * @type {!ProtoBuf.Reflect.Service} 128 | * @expose 129 | */ 130 | var $typeS; 131 | 132 | /** 133 | * Reflection type. 134 | * @name ProtoBuf.Builder.Service#$type 135 | * @type {!ProtoBuf.Reflect.Service} 136 | * @expose 137 | */ 138 | var $type; 139 | 140 | if (Object.defineProperty) 141 | Object.defineProperty(Service, "$options", { "value": T.buildOpt() }), 142 | Object.defineProperty(ServicePrototype, "$options", { "value": Service["$options"] }), 143 | Object.defineProperty(Service, "$type", { "value": T }), 144 | Object.defineProperty(ServicePrototype, "$type", { "value": T }); 145 | -------------------------------------------------------------------------------- /examples/protoify/index.js: -------------------------------------------------------------------------------- 1 | var ProtoBuf = require("protobufjs"), 2 | ByteBuffer = ProtoBuf.ByteBuffer, // ProtoBuf.js uses and also exposes ByteBuffer.js 3 | Long = ProtoBuf.Long; // as well as Long.js (not used in this example) 4 | 5 | // Option 1: Loading the .proto file directly 6 | var builder = ProtoBuf.loadProtoFile("./json.proto"), // Creates the Builder 7 | JS = builder.build("js"); // Returns just the 'js' namespace if that's all we need 8 | 9 | // Option 2: Loading the .json file generated through 'proto2js json.proto > json.json' 10 | var root = ProtoBuf.loadJsonFile("./json.json").build(), // Here we make the Builder return the root namespace 11 | JS = root.js; // then we reference 'js' inside. Both is possible. 12 | 13 | // Option 3: Loading the module generated through 'proto2js json.proto -commonjs=js > json.js' 14 | var JS = require("./json.js"); // Returns what is specified with -commonjs[=XX] (omitted=root) 15 | 16 | // `JS` now contains the js namespace from json.proto: Value, Array and Object 17 | 18 | // This is how we use these classes: 19 | 20 | /** 21 | * Converts a JSON-like structure to JS-Namespace values. 22 | * @param {*} val JSON 23 | * @returns {!JS.Value} JS-Namespace value 24 | * @inner 25 | */ 26 | function _protoify(val) { 27 | switch (typeof val) { 28 | case 'number': 29 | if (val%1 === 0 && val >= (0x80000000|0) && val <= (0x7fffffff|0)) 30 | return new JS.Value(val); // sets the first field declared in .js.Value 31 | else 32 | return new JS.Value(null, val); // sets the second field 33 | case 'string': 34 | return new JS.Value({ 'string': val }); // uses object notation instead 35 | case 'boolean': 36 | return new JS.Value({ 'boolean': val }); 37 | case 'object': 38 | if (val === null) 39 | return new JS.Value({ 'null': true }); 40 | if (Object.prototype.toString.call(val) === "[object Array]") { 41 | var arr = new JS.Array(); 42 | for (var i=0; i 2048) 139 | tempBuffer = ByteBuffer.allocate(1024); 140 | // In case this module is running inside of a daemon, we'd just call this 141 | // method every now and then to discard the tempBuffer if it becomes too 142 | // large. This is just an example on how to reuse ByteBuffers effectively. 143 | // You may consider something like this for the performance benefit, which 144 | // is decreasing the memory allocation footprint of your app. 145 | }; 146 | 147 | // Have a nice day! 148 | -------------------------------------------------------------------------------- /docs/styles/jsdoc-default.css: -------------------------------------------------------------------------------- 1 | html 2 | { 3 | overflow: auto; 4 | background-color: #fff; 5 | } 6 | 7 | body 8 | { 9 | font: 14px "DejaVu Sans Condensed", "Liberation Sans", "Nimbus Sans L", Tahoma, Geneva, "Helvetica Neue", Helvetica, Arial, sans-serif; 10 | line-height: 130%; 11 | color: #000; 12 | background-color: #fff; 13 | } 14 | 15 | a { 16 | color: #444; 17 | } 18 | 19 | a:visited { 20 | color: #444; 21 | } 22 | 23 | a:active { 24 | color: #444; 25 | } 26 | 27 | header 28 | { 29 | display: block; 30 | padding: 6px 4px; 31 | } 32 | 33 | .class-description { 34 | font-style: italic; 35 | font-family: Palatino, 'Palatino Linotype', serif; 36 | font-size: 130%; 37 | line-height: 140%; 38 | margin-bottom: 1em; 39 | margin-top: 1em; 40 | } 41 | 42 | #main { 43 | float: left; 44 | width: 100%; 45 | } 46 | 47 | section 48 | { 49 | display: block; 50 | 51 | background-color: #fff; 52 | padding: 12px 24px; 53 | border-bottom: 1px solid #ccc; 54 | margin-right: 240px; 55 | } 56 | 57 | .variation { 58 | display: none; 59 | } 60 | 61 | .signature-attributes { 62 | font-size: 60%; 63 | color: #aaa; 64 | font-style: italic; 65 | font-weight: lighter; 66 | } 67 | 68 | nav 69 | { 70 | display: block; 71 | float: left; 72 | margin-left: -230px; 73 | margin-top: 28px; 74 | width: 220px; 75 | border-left: 1px solid #ccc; 76 | padding-left: 9px; 77 | } 78 | 79 | nav ul { 80 | font-family: 'Lucida Grande', 'Lucida Sans Unicode', arial, sans-serif; 81 | font-size: 100%; 82 | line-height: 17px; 83 | padding:0; 84 | margin:0; 85 | list-style-type:none; 86 | } 87 | 88 | nav h2 a, nav h2 a:visited { 89 | color: #A35A00; 90 | text-decoration: none; 91 | } 92 | 93 | nav h3 { 94 | margin-top: 12px; 95 | } 96 | 97 | nav li { 98 | margin-top: 6px; 99 | } 100 | 101 | nav a { 102 | color: #5C5954; 103 | } 104 | 105 | nav a:visited { 106 | color: #5C5954; 107 | } 108 | 109 | nav a:active { 110 | color: #5C5954; 111 | } 112 | 113 | footer { 114 | display: block; 115 | padding: 6px; 116 | margin-top: 12px; 117 | font-style: italic; 118 | font-size: 90%; 119 | } 120 | 121 | h1 122 | { 123 | font-size: 200%; 124 | font-weight: bold; 125 | letter-spacing: -0.01em; 126 | margin: 6px 0 9px 0; 127 | } 128 | 129 | h2 130 | { 131 | font-size: 170%; 132 | font-weight: bold; 133 | letter-spacing: -0.01em; 134 | margin: 6px 0 3px 0; 135 | } 136 | 137 | h3 138 | { 139 | font-size: 150%; 140 | font-weight: bold; 141 | letter-spacing: -0.01em; 142 | margin-top: 16px; 143 | margin: 6px 0 3px 0; 144 | } 145 | 146 | h4 147 | { 148 | font-size: 130%; 149 | font-weight: bold; 150 | letter-spacing: -0.01em; 151 | margin-top: 16px; 152 | margin: 18px 0 3px 0; 153 | color: #A35A00; 154 | } 155 | 156 | h5, .container-overview .subsection-title 157 | { 158 | font-size: 120%; 159 | font-weight: bold; 160 | letter-spacing: -0.01em; 161 | margin: 8px 0 3px -16px; 162 | } 163 | 164 | h6 165 | { 166 | font-size: 100%; 167 | letter-spacing: -0.01em; 168 | margin: 6px 0 3px 0; 169 | font-style: italic; 170 | } 171 | 172 | .ancestors { color: #999; } 173 | .ancestors a 174 | { 175 | color: #999 !important; 176 | text-decoration: none; 177 | } 178 | 179 | .important 180 | { 181 | font-weight: bold; 182 | color: #950B02; 183 | } 184 | 185 | .yes-def { 186 | text-indent: -1000px; 187 | } 188 | 189 | .type-signature { 190 | color: #aaa; 191 | } 192 | 193 | .name, .signature { 194 | font-family: Consolas, "Lucida Console", Monaco, monospace; 195 | } 196 | 197 | .details { margin-top: 14px; border-left: 2px solid #DDD; } 198 | .details dt { width:100px; float:left; padding-left: 10px; padding-top: 6px; } 199 | .details dd { margin-left: 50px; } 200 | .details ul { margin: 0; } 201 | .details ul { list-style-type: none; } 202 | .details li { margin-left: 30px; padding-top: 6px; } 203 | .details pre.prettyprint { margin: 0 } 204 | .details .object-value { padding-top: 0; } 205 | 206 | .description { 207 | margin-bottom: 1em; 208 | margin-left: -16px; 209 | margin-top: 1em; 210 | } 211 | 212 | .code-caption 213 | { 214 | font-style: italic; 215 | font-family: Palatino, 'Palatino Linotype', serif; 216 | font-size: 107%; 217 | margin: 0; 218 | } 219 | 220 | .prettyprint 221 | { 222 | border: 1px solid #ddd; 223 | width: 80%; 224 | overflow: auto; 225 | } 226 | 227 | .prettyprint.source { 228 | width: inherit; 229 | } 230 | 231 | .prettyprint code 232 | { 233 | font-family: Consolas, 'Lucida Console', Monaco, monospace; 234 | font-size: 100%; 235 | line-height: 18px; 236 | display: block; 237 | padding: 4px 12px; 238 | margin: 0; 239 | background-color: #fff; 240 | color: #000; 241 | } 242 | 243 | .prettyprint code span.line 244 | { 245 | display: inline-block; 246 | } 247 | 248 | .prettyprint.linenums 249 | { 250 | padding-left: 70px; 251 | -webkit-user-select: none; 252 | -moz-user-select: none; 253 | -ms-user-select: none; 254 | user-select: none; 255 | } 256 | 257 | .prettyprint.linenums ol 258 | { 259 | padding-left: 0; 260 | } 261 | 262 | .prettyprint.linenums li 263 | { 264 | border-left: 3px #ddd solid; 265 | } 266 | 267 | .prettyprint.linenums li.selected, 268 | .prettyprint.linenums li.selected * 269 | { 270 | background-color: lightyellow; 271 | } 272 | 273 | .prettyprint.linenums li * 274 | { 275 | -webkit-user-select: text; 276 | -moz-user-select: text; 277 | -ms-user-select: text; 278 | user-select: text; 279 | } 280 | 281 | .params, .props 282 | { 283 | border-spacing: 0; 284 | border: 0; 285 | border-collapse: collapse; 286 | } 287 | 288 | .params .name, .props .name, .name code { 289 | color: #A35A00; 290 | font-family: Consolas, 'Lucida Console', Monaco, monospace; 291 | font-size: 100%; 292 | } 293 | 294 | .params td, .params th, .props td, .props th 295 | { 296 | border: 1px solid #ddd; 297 | margin: 0px; 298 | text-align: left; 299 | vertical-align: top; 300 | padding: 4px 6px; 301 | display: table-cell; 302 | } 303 | 304 | .params thead tr, .props thead tr 305 | { 306 | background-color: #ddd; 307 | font-weight: bold; 308 | } 309 | 310 | .params .params thead tr, .props .props thead tr 311 | { 312 | background-color: #fff; 313 | font-weight: bold; 314 | } 315 | 316 | .params th, .props th { border-right: 1px solid #aaa; } 317 | .params thead .last, .props thead .last { border-right: 1px solid #ddd; } 318 | 319 | .params td.description > p:first-child 320 | { 321 | margin-top: 0; 322 | padding-top: 0; 323 | } 324 | 325 | .params td.description > p:last-child 326 | { 327 | margin-bottom: 0; 328 | padding-bottom: 0; 329 | } 330 | 331 | .disabled { 332 | color: #454545; 333 | } 334 | -------------------------------------------------------------------------------- /src/ProtoBuf/Map.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @alias ProtoBuf.Map 3 | * @expose 4 | */ 5 | ProtoBuf.Map = (function(ProtoBuf, Reflect) { 6 | "use strict"; 7 | 8 | /** 9 | * Constructs a new Map. A Map is a container that is used to implement map 10 | * fields on message objects. It closely follows the ES6 Map API; however, 11 | * it is distinct because we do not want to depend on external polyfills or 12 | * on ES6 itself. 13 | * 14 | * @exports ProtoBuf.Map 15 | * @param {!ProtoBuf.Reflect.Field} field Map field 16 | * @param {Object.=} contents Initial contents 17 | * @constructor 18 | */ 19 | var Map = function(field, contents) { 20 | if (!field.map) 21 | throw Error("field is not a map"); 22 | 23 | /** 24 | * The field corresponding to this map. 25 | * @type {!ProtoBuf.Reflect.Field} 26 | */ 27 | this.field = field; 28 | 29 | /** 30 | * Element instance corresponding to key type. 31 | * @type {!ProtoBuf.Reflect.Element} 32 | */ 33 | this.keyElem = new Reflect.Element(field.keyType, null, true, field.syntax); 34 | 35 | /** 36 | * Element instance corresponding to value type. 37 | * @type {!ProtoBuf.Reflect.Element} 38 | */ 39 | this.valueElem = new Reflect.Element(field.type, field.resolvedType, false, field.syntax); 40 | 41 | /** 42 | * Internal map: stores mapping of (string form of key) -> (key, value) 43 | * pair. 44 | * 45 | * We provide map semantics for arbitrary key types, but we build on top 46 | * of an Object, which has only string keys. In order to avoid the need 47 | * to convert a string key back to its native type in many situations, 48 | * we store the native key value alongside the value. Thus, we only need 49 | * a one-way mapping from a key type to its string form that guarantees 50 | * uniqueness and equality (i.e., str(K1) === str(K2) if and only if K1 51 | * === K2). 52 | * 53 | * @type {!Object} 54 | */ 55 | this.map = {}; 56 | 57 | /** 58 | * Returns the number of elements in the map. 59 | */ 60 | Object.defineProperty(this, "size", { 61 | get: function() { return Object.keys(this.map).length; } 62 | }); 63 | 64 | // Fill initial contents from a raw object. 65 | if (contents) { 66 | var keys = Object.keys(contents); 67 | for (var i = 0; i < keys.length; i++) { 68 | var key = this.keyElem.valueFromString(keys[i]); 69 | var val = this.valueElem.verifyValue(contents[keys[i]]); 70 | this.map[this.keyElem.valueToString(key)] = 71 | { key: key, value: val }; 72 | } 73 | } 74 | }; 75 | 76 | var MapPrototype = Map.prototype; 77 | 78 | /** 79 | * Helper: return an iterator over an array. 80 | * @param {!Array<*>} arr the array 81 | * @returns {!Object} an iterator 82 | * @inner 83 | */ 84 | function arrayIterator(arr) { 85 | var idx = 0; 86 | return { 87 | next: function() { 88 | if (idx < arr.length) 89 | return { done: false, value: arr[idx++] }; 90 | return { done: true }; 91 | } 92 | } 93 | } 94 | 95 | /** 96 | * Clears the map. 97 | */ 98 | MapPrototype.clear = function() { 99 | this.map = {}; 100 | }; 101 | 102 | /** 103 | * Deletes a particular key from the map. 104 | * @returns {boolean} Whether any entry with this key was deleted. 105 | */ 106 | MapPrototype["delete"] = function(key) { 107 | var keyValue = this.keyElem.valueToString(this.keyElem.verifyValue(key)); 108 | var hadKey = keyValue in this.map; 109 | delete this.map[keyValue]; 110 | return hadKey; 111 | }; 112 | 113 | /** 114 | * Returns an iterator over [key, value] pairs in the map. 115 | * @returns {Object} The iterator 116 | */ 117 | MapPrototype.entries = function() { 118 | var entries = []; 119 | var strKeys = Object.keys(this.map); 120 | for (var i = 0, entry; i < strKeys.length; i++) 121 | entries.push([(entry=this.map[strKeys[i]]).key, entry.value]); 122 | return arrayIterator(entries); 123 | }; 124 | 125 | /** 126 | * Returns an iterator over keys in the map. 127 | * @returns {Object} The iterator 128 | */ 129 | MapPrototype.keys = function() { 130 | var keys = []; 131 | var strKeys = Object.keys(this.map); 132 | for (var i = 0; i < strKeys.length; i++) 133 | keys.push(this.map[strKeys[i]].key); 134 | return arrayIterator(keys); 135 | }; 136 | 137 | /** 138 | * Returns an iterator over values in the map. 139 | * @returns {!Object} The iterator 140 | */ 141 | MapPrototype.values = function() { 142 | var values = []; 143 | var strKeys = Object.keys(this.map); 144 | for (var i = 0; i < strKeys.length; i++) 145 | values.push(this.map[strKeys[i]].value); 146 | return arrayIterator(values); 147 | }; 148 | 149 | /** 150 | * Iterates over entries in the map, calling a function on each. 151 | * @param {function(this:*, *, *, *)} cb The callback to invoke with value, key, and map arguments. 152 | * @param {Object=} thisArg The `this` value for the callback 153 | */ 154 | MapPrototype.forEach = function(cb, thisArg) { 155 | var strKeys = Object.keys(this.map); 156 | for (var i = 0, entry; i < strKeys.length; i++) 157 | cb.call(thisArg, (entry=this.map[strKeys[i]]).value, entry.key, this); 158 | }; 159 | 160 | /** 161 | * Sets a key in the map to the given value. 162 | * @param {*} key The key 163 | * @param {*} value The value 164 | * @returns {!ProtoBuf.Map} The map instance 165 | */ 166 | MapPrototype.set = function(key, value) { 167 | var keyValue = this.keyElem.verifyValue(key); 168 | var valValue = this.valueElem.verifyValue(value); 169 | this.map[this.keyElem.valueToString(keyValue)] = 170 | { key: keyValue, value: valValue }; 171 | return this; 172 | }; 173 | 174 | /** 175 | * Gets the value corresponding to a key in the map. 176 | * @param {*} key The key 177 | * @returns {*|undefined} The value, or `undefined` if key not present 178 | */ 179 | MapPrototype.get = function(key) { 180 | var keyValue = this.keyElem.valueToString(this.keyElem.verifyValue(key)); 181 | if (!(keyValue in this.map)) 182 | return undefined; 183 | return this.map[keyValue].value; 184 | }; 185 | 186 | /** 187 | * Determines whether the given key is present in the map. 188 | * @param {*} key The key 189 | * @returns {boolean} `true` if the key is present 190 | */ 191 | MapPrototype.has = function(key) { 192 | var keyValue = this.keyElem.valueToString(this.keyElem.verifyValue(key)); 193 | return (keyValue in this.map); 194 | }; 195 | 196 | return Map; 197 | })(ProtoBuf, ProtoBuf.Reflect); 198 | -------------------------------------------------------------------------------- /src/ProtoBuf/Reflect/Namespace.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Constructs a new Namespace. 3 | * @exports ProtoBuf.Reflect.Namespace 4 | * @param {!ProtoBuf.Builder} builder Builder reference 5 | * @param {?ProtoBuf.Reflect.Namespace} parent Namespace parent 6 | * @param {string} name Namespace name 7 | * @param {Object.=} options Namespace options 8 | * @param {string?} syntax The syntax level of this definition (e.g., proto3) 9 | * @constructor 10 | * @extends ProtoBuf.Reflect.T 11 | */ 12 | var Namespace = function(builder, parent, name, options, syntax) { 13 | T.call(this, builder, parent, name); 14 | 15 | /** 16 | * @override 17 | */ 18 | this.className = "Namespace"; 19 | 20 | /** 21 | * Children inside the namespace. 22 | * @type {!Array.} 23 | */ 24 | this.children = []; 25 | 26 | /** 27 | * Options. 28 | * @type {!Object.} 29 | */ 30 | this.options = options || {}; 31 | 32 | /** 33 | * Syntax level (e.g., proto2 or proto3). 34 | * @type {!string} 35 | */ 36 | this.syntax = syntax || "proto2"; 37 | }; 38 | 39 | /** 40 | * @alias ProtoBuf.Reflect.Namespace.prototype 41 | * @inner 42 | */ 43 | var NamespacePrototype = Namespace.prototype = Object.create(T.prototype); 44 | 45 | /** 46 | * Returns an array of the namespace's children. 47 | * @param {ProtoBuf.Reflect.T=} type Filter type (returns instances of this type only). Defaults to null (all children). 48 | * @return {Array.} 49 | * @expose 50 | */ 51 | NamespacePrototype.getChildren = function(type) { 52 | type = type || null; 53 | if (type == null) 54 | return this.children.slice(); 55 | var children = []; 56 | for (var i=0, k=this.children.length; i} qn Qualified name to resolve 99 | * @param {boolean=} excludeNonNamespace Excludes non-namespace types, defaults to `false` 100 | * @return {?ProtoBuf.Reflect.Namespace} The resolved type or null if not found 101 | * @expose 102 | */ 103 | NamespacePrototype.resolve = function(qn, excludeNonNamespace) { 104 | var part = typeof qn === 'string' ? qn.split(".") : qn, 105 | ptr = this, 106 | i = 0; 107 | if (part[i] === "") { // Fully qualified name, e.g. ".My.Message' 108 | while (ptr.parent !== null) 109 | ptr = ptr.parent; 110 | i++; 111 | } 112 | var child; 113 | do { 114 | do { 115 | if (!(ptr instanceof Reflect.Namespace)) { 116 | ptr = null; 117 | break; 118 | } 119 | child = ptr.getChild(part[i]); 120 | if (!child || !(child instanceof Reflect.T) || (excludeNonNamespace && !(child instanceof Reflect.Namespace))) { 121 | ptr = null; 122 | break; 123 | } 124 | ptr = child; i++; 125 | } while (i < part.length); 126 | if (ptr != null) 127 | break; // Found 128 | // Else search the parent 129 | if (this.parent !== null) 130 | return this.parent.resolve(qn, excludeNonNamespace); 131 | } while (ptr != null); 132 | return ptr; 133 | }; 134 | 135 | /** 136 | * Determines the shortest qualified name of the specified type, if any, relative to this namespace. 137 | * @param {!ProtoBuf.Reflect.T} t Reflection type 138 | * @returns {string} The shortest qualified name or, if there is none, the fqn 139 | * @expose 140 | */ 141 | NamespacePrototype.qn = function(t) { 142 | var part = [], ptr = t; 143 | do { 144 | part.unshift(ptr.name); 145 | ptr = ptr.parent; 146 | } while (ptr !== null); 147 | for (var len=1; len <= part.length; len++) { 148 | var qn = part.slice(part.length-len); 149 | if (t === this.resolve(qn, t instanceof Reflect.Namespace)) 150 | return qn.join("."); 151 | } 152 | return t.fqn(); 153 | }; 154 | 155 | /** 156 | * Builds the namespace and returns the runtime counterpart. 157 | * @return {Object.} Runtime namespace 158 | * @expose 159 | */ 160 | NamespacePrototype.build = function() { 161 | /** @dict */ 162 | var ns = {}; 163 | var children = this.children; 164 | for (var i=0, k=children.length, child; i} 177 | */ 178 | NamespacePrototype.buildOpt = function() { 179 | var opt = {}, 180 | keys = Object.keys(this.options); 181 | for (var i=0, k=keys.length; i}null} Option value or NULL if there is no such option 198 | */ 199 | NamespacePrototype.getOption = function(name) { 200 | if (typeof name === 'undefined') 201 | return this.options; 202 | return typeof this.options[name] !== 'undefined' ? this.options[name] : null; 203 | }; 204 | -------------------------------------------------------------------------------- /docs/ProtoBuf.Reflect.Extension.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | JSDoc: Class: Extension 6 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 |

Class: Extension

21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 |
29 |

30 | ProtoBuf.Reflect. 31 | 32 | Extension 33 |

34 | 35 |
36 | 37 |
38 |
39 | 40 | 41 | 42 | 43 |
44 |

new Extension(buildernon-null, parentnon-null, name, fieldnon-null)

45 | 46 | 47 |
48 |
49 | 50 | 51 |
52 |

An extension (field).

53 |
54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 |
Parameters:
62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 |
NameTypeDescription
builder 90 | 91 | 92 | ProtoBuf.Builder 93 | 94 | 95 | 96 |

Builder reference

parent 113 | 114 | 115 | ProtoBuf.Reflect.T 116 | 117 | 118 | 119 |

Parent object

name 136 | 137 | 138 | string 139 | 140 | 141 | 142 |

Object name

field 159 | 160 | 161 | ProtoBuf.Reflect.Message.Field 162 | 163 | 164 | 165 |

Extension field

177 | 178 | 179 | 180 |
181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 |
Source:
201 |
204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 |
212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 |
228 | 229 | 230 |
231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 |

Members

246 | 247 |
248 | 249 |
250 |

(non-null) field :ProtoBuf.Reflect.Message.Field

251 | 252 | 253 |
254 |
255 | 256 |
257 |

Extended message field.

258 |
259 | 260 | 261 | 262 |
Type:
263 | 271 | 272 | 273 | 274 |
275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 |
Source:
295 |
298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 |
306 | 307 | 308 | 309 | 310 | 311 |
312 | 313 |
314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 |
322 | 323 |
324 | 325 | 326 | 327 | 328 |
329 | 330 | 333 | 334 |
335 | 336 |
337 | Documentation generated by JSDoc 3.3.0-alpha9 on Thu May 26 2016 19:11:32 GMT+0200 (Mitteleuropäische Sommerzeit) 338 |
339 | 340 | 341 | 342 | 343 | -------------------------------------------------------------------------------- /cli/pbjs/targets/json.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Daniel Wirtz 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | var description = "Plain JSON descriptor"; 17 | 18 | var ProtoBuf = require(__dirname+"/../../../index.js"), 19 | util = require("../util.js"); 20 | 21 | /** 22 | * pbjs target: Plain JSON descriptor 23 | * @exports pbjs/targets/json 24 | * @function 25 | * @param {!ProtoBuf.Builder} builder Builder 26 | * @param {!Object.=} options Options 27 | * @returns {string} 28 | */ 29 | var json = module.exports = function(builder, options) { 30 | options = options || {}; 31 | builder.resolveAll(); 32 | 33 | // Set the pointer to the lowest common namespace (with options) 34 | var ptr = builder.ns; 35 | while (ptr.children.length === 1 && Object.keys(ptr.options).length === 0 && ptr.children[0].className === "Namespace") 36 | ptr = ptr.children[0]; 37 | 38 | // Start by building the package namespace 39 | var pkg = ptr.fqn().substring(1), 40 | out = { 41 | "package": pkg !== "" ? pkg : null 42 | }; 43 | buildNamespace(ptr, out); 44 | return JSON.stringify(out, null, options.min ? 0 : 4); 45 | }; 46 | 47 | /** 48 | * Module description. 49 | * @type {string} 50 | */ 51 | json.description = description; 52 | 53 | /** 54 | * Builds all structures in a namespace. 55 | * @param {!ProtoBuf.Reflect.Namespace} ns Namespace to build 56 | * @param {!Object.} out Extended output object 57 | */ 58 | function buildNamespace(ns, out) { 59 | var messages, enums, services; 60 | util.extend(out, { 61 | "options" : out.options || {}, 62 | "messages" : messages = [], 63 | "enums" : enums = [], 64 | "services" : services = [] 65 | }); 66 | util.extend(out["options"], buildOptions(ns.options)); 67 | ns.getChildren(ProtoBuf.Reflect.Enum).forEach(function(enm) { 68 | enums.push(buildEnum(enm)); 69 | }); 70 | if (enums.length === 0) 71 | delete out["enums"]; 72 | ns.getChildren(ProtoBuf.Reflect.Message).forEach(function(msg) { 73 | messages.push(buildMessage(msg)); 74 | }); 75 | ns.getChildren(ProtoBuf.Reflect.Service).forEach(function(svc) { 76 | services.push(buildService(svc)); 77 | }); 78 | if (services.length === 0) 79 | delete out["services"]; 80 | Array.prototype.push.apply(messages, buildExtensions(ns)); 81 | ns.getChildren(ProtoBuf.Reflect.Namespace).forEach(function(innerNs) { 82 | if (innerNs.className !== "Namespace") 83 | return; 84 | var emptyMessage = { 85 | "name": innerNs.name, 86 | "fields": [] 87 | }; 88 | buildNamespace(innerNs, emptyMessage); 89 | messages.push(emptyMessage); 90 | }); 91 | if (messages.length === 0) 92 | delete out["messages"]; 93 | if (Object.keys(out["options"]).length === 0) 94 | delete out["options"]; 95 | } 96 | 97 | /** 98 | * Builds extensions declared in the specified namespace. 99 | * @param {!ProtoBuf.Reflect.Namespace} ns Namespace 100 | * @returns {!Array.} 101 | */ 102 | function buildExtensions(ns) { 103 | var exts = util.groupExtensions(ns); 104 | if (exts === null) 105 | return []; 106 | var messages = []; 107 | Object.keys(exts).forEach(function(extFqn) { 108 | var extMsg = ns.resolve(extFqn), 109 | extFields = exts[extFqn]; 110 | var fields, ext = { 111 | "ref" : ns.qn(extMsg), 112 | "fields" : fields = [] 113 | }; 114 | extFields.forEach(function(extField) { 115 | fields.push(buildMessageField(extField)); 116 | }); 117 | messages.push(ext); 118 | }); 119 | return messages; 120 | } 121 | 122 | /** 123 | * Builds block-level options. 124 | * @param {!Object.} options Options 125 | * @returns {!Object.} 126 | */ 127 | function buildOptions(options) { 128 | Object.keys(options = options || {}).forEach(function(key) { 129 | var val = options[key]; 130 | switch (typeof val) { 131 | case 'string': 132 | case 'number': 133 | case 'boolean': 134 | case 'object': 135 | break; 136 | default: 137 | throw Error("Illegal option type: "+typeof(val)); 138 | } 139 | }); 140 | return options; 141 | } 142 | 143 | /** 144 | * Builds a message. 145 | * @param {!ProtoBuf.Reflect.Message} msg Message 146 | * @returns {!*} 147 | */ 148 | function buildMessage(msg) { 149 | var fields, oneofs; 150 | var out = { 151 | "name" : msg.name, 152 | "options" : {}, 153 | "fields" : fields = [], 154 | "oneofs" : oneofs = {} 155 | }; 156 | msg.getChildren(ProtoBuf.Reflect.Message.Field).forEach(function(fld) { 157 | if (fld instanceof ProtoBuf.Reflect.Message.ExtensionField) 158 | return; 159 | fields.push(buildMessageField(fld)); 160 | }); 161 | msg.getChildren(ProtoBuf.Reflect.Message.OneOf).forEach(function(oneof) { 162 | oneofs[oneof.name] = buildMessageOneof(oneof); 163 | }); 164 | if (msg.extensions) 165 | out["extensions"] = msg.extensions; 166 | if (Object.keys(oneofs).length === 0) 167 | delete out["oneofs"]; 168 | buildNamespace(msg, out); 169 | return out; 170 | } 171 | 172 | /** 173 | * Builds a message field. 174 | * @param {!ProtoBuf.Reflect.Message.Field} fld Message field 175 | * @returns {!*} 176 | */ 177 | function buildMessageField(fld) { 178 | return { 179 | "rule" : fld.map ? "map" : (fld.repeated ? "repeated" : (fld.required ? "required" : "optional")), 180 | "type" : fld.resolvedType ? fld.parent.qn(fld.resolvedType) : fld.type['name'], 181 | "keytype" : (typeof(fld.keyType) === 'string') ? fld.keyType : (fld.keyType !== null ? fld.keyType.name : undefined), 182 | "name" : fld instanceof ProtoBuf.Reflect.Message.ExtensionField ? fld.name.substring(fld.name.lastIndexOf(".")+1): fld.name, 183 | "id" : fld.id, 184 | "options" : Object.keys(fld.options).length > 0 ? buildOptions(fld.options) : undefined, 185 | "oneof" : fld.oneof ? fld.oneof.name : undefined 186 | }; 187 | } 188 | 189 | /** 190 | * Builds a message oneof. 191 | * @param {!ProtoBuf.Reflect.message.OneOf} oneof Message oneof 192 | * @returns {!Array.} 193 | */ 194 | function buildMessageOneof(oneof) { 195 | var out = []; 196 | oneof.fields.forEach(function(fld) { 197 | out.push(fld.id); 198 | }); 199 | return out; 200 | } 201 | 202 | /** 203 | * Builds an enum. 204 | * @param {!ProtoBuf.Reflect.Enum} enm Enum 205 | * @returns {!*} 206 | */ 207 | function buildEnum(enm) { 208 | var values; 209 | var out = { 210 | "name" : enm.name, 211 | "values" : values = [] 212 | }; 213 | enm.getChildren(ProtoBuf.Reflect.Enum.Value).forEach(function(val) { 214 | values.push(buildEnumValue(val)); 215 | }); 216 | if (Object.keys(enm.options).length > 0) 217 | out["options"] = buildOptions(enm.options); 218 | return out; 219 | } 220 | 221 | /** 222 | * Builds an enum value. 223 | * @param {!ProtoBuf.Reflect.Enum.Value} val Enum value 224 | * @returns {!*} 225 | */ 226 | function buildEnumValue(val) { 227 | return { 228 | "name" : val.name, 229 | "id" : val.id 230 | }; 231 | } 232 | 233 | /** 234 | * Builds a service. 235 | * @param {!ProtoBuf.Reflect.Service} svc Service 236 | * @returns {!*} 237 | */ 238 | function buildService(svc) { 239 | var rpc; 240 | var out = { 241 | "name": svc.name, 242 | "options": buildOptions(svc.options), 243 | "rpc": rpc = {} 244 | }; 245 | svc.getChildren(ProtoBuf.Reflect.Service.RPCMethod).forEach(function(mtd) { 246 | rpc[mtd.name] = { 247 | "request": svc.qn(mtd.resolvedRequestType), 248 | "response": svc.qn(mtd.resolvedResponseType), 249 | "options": buildOptions(mtd.options) 250 | }; 251 | }); 252 | return out; 253 | } 254 | -------------------------------------------------------------------------------- /cli/pbjs.js: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2013 Daniel Wirtz 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | var ProtoBuf = require(__dirname+"/../index.js"), 17 | fs = require("fs"), 18 | path = require("path"), 19 | cli = require("ascli")("pbjs"), 20 | yargs = require("yargs"), 21 | util = require("./pbjs/util.js"), 22 | glob = require("glob"), 23 | pkg = require("../package.json"); 24 | 25 | /** 26 | * pbjs namespace. 27 | * @exports pbjs 28 | * @namespace 29 | */ 30 | var pbjs = module.exports = {}; 31 | 32 | /** 33 | * @alias pbjs/util 34 | */ 35 | pbjs.util = util; 36 | 37 | /** 38 | * Source formats. 39 | * @type {!Object.)>} 40 | */ 41 | pbjs.sources = {}; 42 | fs.readdirSync(__dirname+"/pbjs/sources").forEach(function(source) { 43 | if (/\.js$/.test(source)) { 44 | var src = require(__dirname + "/pbjs/sources/" + source); 45 | if (!src.exclude) 46 | pbjs.sources[source.substring(0, source.lastIndexOf("."))] = src; 47 | } 48 | }); 49 | 50 | /** 51 | * Target formats. 52 | * @type {!Object.)>} 53 | */ 54 | pbjs.targets = {}; 55 | fs.readdirSync(__dirname+"/pbjs/targets").forEach(function(target) { 56 | if (/\.js$/.test(target)) 57 | pbjs.targets[target.substring(0, target.lastIndexOf("."))] = require(__dirname + "/pbjs/targets/" + target); 58 | }); 59 | 60 | /** 61 | * Status code: Operation successful 62 | * @type {number} 63 | */ 64 | pbjs.STATUS_OK = 0; 65 | 66 | /** 67 | * Status code: Displaying usage information 68 | * @type {number} 69 | */ 70 | pbjs.STATUS_USAGE = 1; 71 | 72 | /** 73 | * Status code: No such include directory 74 | * @type {number} 75 | */ 76 | pbjs.STATUS_ERR_INCLUDE_DIR = 2; 77 | 78 | /** 79 | * Status code: No such source format 80 | * @type {number} 81 | */ 82 | pbjs.STATUS_ERR_SOURCE_FORMAT = 3; 83 | 84 | /** 85 | * Status code: No such target format 86 | * @type {number} 87 | */ 88 | pbjs.STATUS_ERR_TARGET_FORMAT = 4; 89 | 90 | /** 91 | * Status code: No such namespace 92 | * @type {number} 93 | */ 94 | pbjs.STATUS_ERR_NAMESPACE = 5; 95 | 96 | /** 97 | * Status code: Illegal dependency 98 | * @type {number} 99 | */ 100 | pbjs.STATUS_ERR_DEPENDENCY = 6; 101 | 102 | /** 103 | * Status code: No matching source files 104 | * @type {number} 105 | */ 106 | pbjs.STATUS_ERR_NOSOURCE = 7; 107 | 108 | // Makes a table of available source or target formats 109 | function mkOptions(obj) { 110 | var str = ''; 111 | Object.keys(obj).forEach(function(key) { 112 | str += "\n "+util.pad(key, 10)+" "+obj[key].description; 113 | }); 114 | return str; 115 | } 116 | 117 | /** 118 | * Executes the program. 119 | * @param {!Array.} argv Command line arguments 120 | * @returns {number} Status code 121 | */ 122 | pbjs.main = function(argv) { 123 | var options = yargs 124 | .usage(cli("pb".white.bold+"js".green.bold, util.pad("ProtoBuf.js v"+pkg['version'], 31, true)+" "+pkg['homepage'].grey) + "\n" + 125 | "CLI utility to convert between .proto and JSON syntax / to generate classes.\n\n" + 126 | "Usage: ".white.bold+path.basename(argv[1]).green.bold+" [options] [> outFile]") 127 | .help("help") 128 | .version(pkg["version"]) 129 | .wrap(null) 130 | .options({ 131 | source: { 132 | alias: "s", 133 | describe: "Specifies the source format. Valid formats are:\n" + mkOptions(pbjs.sources)+"\n" 134 | }, 135 | target: { 136 | alias: "t", 137 | describe: "Specifies the target format. Valid formats are:\n" + mkOptions(pbjs.targets)+"\n" 138 | }, 139 | using: { 140 | alias: "u", 141 | describe: "Specifies an option to apply to the volatile builder\nloading the source, e.g. convertFieldsToCamelCase.", 142 | type: "array" 143 | }, 144 | min: { 145 | alias: "m", 146 | describe: "Minifies the output.", 147 | default: false 148 | }, 149 | path: { 150 | alias: "p", 151 | describe: "Adds a directory to the include path." 152 | }, 153 | legacy: { 154 | alias: "l", 155 | describe: "Includes legacy descriptors from google/protobuf/ if\nexplicitly referenced.", 156 | default: false 157 | }, 158 | quiet: { 159 | alias: "q", 160 | describe: "Suppresses any informatory output to stderr.", 161 | default: false 162 | }, 163 | out: { 164 | alias: "o", 165 | describe: "Send output to file instead of stdout.", 166 | }, 167 | use: { 168 | alias: "i", 169 | describe: "Specifies an option to apply to the emitted builder\nutilized by your program, e.g. populateAccessors.", 170 | type: "array" 171 | }, 172 | exports: { 173 | alias: "e", 174 | describe: "Specifies the namespace to export. Defaults to export\nthe root namespace." 175 | }, 176 | dependency: { 177 | alias: "d", 178 | describe: "Library dependency to use when generating classes.\nDefaults to 'protobufjs' for CommonJS, 'ProtoBuf' for\nAMD modules and 'dcodeIO.ProtoBuf' for classes." 179 | } 180 | }) 181 | .alias("help", "h") 182 | .alias("version", "v") 183 | .check(function (args) { 184 | if (args.source && typeof pbjs.sources[args.source] !== "function") { 185 | return "Unrecognized source format: '" + args.source + "'"; 186 | } 187 | 188 | if (args.target && typeof pbjs.targets[args.target] !== "function") { 189 | return "Unrecognized target format: '" + args.target + "'"; 190 | } 191 | 192 | if (args._.length < 3) { 193 | return "The filename to parse is required."; 194 | } 195 | 196 | return true; 197 | }) 198 | .parse(argv); 199 | 200 | var start = Date.now(), 201 | sourceFiles = options._.slice(2); 202 | 203 | // Expand glob expressions 204 | var sourceFilesExpand = []; 205 | for (var i=0; i 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | var description = "Plain .proto descriptor"; 17 | 18 | var ProtoBuf = require(__dirname+"/../../../index.js"), 19 | util = require("../util.js"); 20 | 21 | /** 22 | * pbjs target: Plain .proto descriptor 23 | * @exports pbjs/targets/proto 24 | * @function 25 | * @param {!ProtoBuf.Builder} builder Builder 26 | * @param {!Object.=} options Options 27 | * @returns {string} 28 | */ 29 | var proto = module.exports = function(builder, options) { 30 | options = options || {}; 31 | builder.resolveAll(); 32 | 33 | // Set the pointer to the lowest common namespace (with options) 34 | var ptr = builder.ns; 35 | while (ptr.children.length === 1 && Object.keys(ptr.options).length === 0 && ptr.children[0].className === "Namespace") 36 | ptr = ptr.children[0]; 37 | 38 | var out = []; 39 | 40 | function trim() { 41 | out[out.length-1] = out[out.length-1].replace(/\n{2,}$/, "\n"); 42 | } 43 | 44 | // Builds a set of top level options 45 | function buildOptions(opt, indent) { 46 | var keys; 47 | if ((keys = Object.keys(opt)).length === 0) 48 | return; 49 | keys.forEach(function(key) { 50 | if (!options.min) 51 | out.push(indent); 52 | out.push("option ", key, options.min ? "=" : " = ", value(opt[key]), options.min ? ";" : ";\n"); 53 | }); 54 | if (!options.min) 55 | out[out.length-1] += "\n"; 56 | } 57 | 58 | // Builds everything within a namespace 59 | function buildNamespace(ns, indent) { 60 | ns.getChildren(ProtoBuf.Reflect.Enum).forEach(function(enm) { 61 | buildEnum(enm, indent); 62 | }); 63 | ns.getChildren(ProtoBuf.Reflect.Message).forEach(function(msg) { 64 | if (!msg.isGroup) // legacy groups are build within the respective field 65 | buildMessage(msg, indent); 66 | }); 67 | var exts = util.groupExtensions(ns); 68 | if (exts !== null) { 69 | Object.keys(exts).forEach(function(extFqn) { 70 | var extMsg = ns.resolve(extFqn), 71 | extFields = exts[extFqn]; 72 | if (!options.min) 73 | out.push(indent); 74 | out.push("extend ", ns.qn(extMsg), options.min ? "{" : " {\n"); 75 | extFields.forEach(function(extField) { 76 | buildMessageField(ns, extField, indent+" ", false); 77 | }); 78 | if (!options.min) 79 | out.push(indent); 80 | out.push(options.min ? "}" : "}\n\n"); 81 | }); 82 | } 83 | ns.getChildren(ProtoBuf.Reflect.Service).forEach(function(svc) { 84 | buildService(svc, indent); 85 | }); 86 | ns.getChildren(ProtoBuf.Reflect.Namespace).forEach(function(innerNs) { 87 | if (innerNs.className !== "Namespace") 88 | return; 89 | if (!options.min) 90 | out.push(indent); 91 | out.push("message ", innerNs.name, options.min ? "{" : " {\n"); 92 | buildNamespace(innerNs, indent+" "); 93 | if (!options.min) 94 | out.push(indent); 95 | out.push(options.min ? "}" : "}\n"); 96 | }); 97 | trim(); 98 | } 99 | 100 | // Builds a message 101 | function buildMessage(msg, indent) { 102 | if (!msg.isGroup) { 103 | if (!options.min) 104 | out.push(indent); 105 | out.push("message ", msg.name); 106 | } 107 | out.push(options.min ? "{" : " {\n"); 108 | buildOptions(msg.options, indent+" "); 109 | var n = 0, oneofFields = []; 110 | msg.getChildren(ProtoBuf.Reflect.Message.OneOf).forEach(function(oneof) { 111 | if (!options.min) 112 | out.push(indent, " "); 113 | out.push("oneof ", oneof.name, options.min ? "{" : " {\n"); 114 | oneof.fields.forEach(function(fld) { 115 | buildMessageField(msg, fld, indent+" ", true); 116 | oneofFields.push(fld); 117 | }); 118 | if (!options.min) 119 | out.push(indent, " "); 120 | out.push(options.min ? "}" : "}\n"); 121 | }); 122 | msg.getChildren(ProtoBuf.Reflect.Message.Field).forEach(function(fld) { 123 | if (fld instanceof ProtoBuf.Reflect.Message.ExtensionField) 124 | return; 125 | if (oneofFields.indexOf(fld) >= 0) 126 | return; 127 | buildMessageField(msg, fld, indent+" ", false); 128 | n++; 129 | }); 130 | if (n > 0 && !options.min) 131 | out[out.length-1] += "\n"; 132 | if (msg.extensions) { // array of ranges 133 | if (!options.min) 134 | out.push(indent, " "); 135 | out.push("extensions "); 136 | msg.extensions.forEach(function(range, index) { 137 | if (index > 0) 138 | out.push(options.min ? "," : ", "); 139 | out.push(value(range[0])); 140 | if (range[1] !== range[0]) 141 | out.push(" to ", range[1] === ProtoBuf.ID_MAX ? "max" : value(range[1])); 142 | }); 143 | out.push(options.min ? ";" : ";\n\n"); 144 | } 145 | buildNamespace(msg, indent+" "); 146 | if (!options.min) 147 | out.push(indent); 148 | out.push(options.min ? "}" : "}\n\n"); 149 | } 150 | 151 | // Builds a message field 152 | function buildMessageField(msg, fld, indent, isOneOf) { 153 | var isGroup = false; 154 | if (!options.min) 155 | out.push(indent); 156 | if (!isOneOf) 157 | out.push(fld.required ? "required " : (fld.repeated ? "repeated " : "optional ")); 158 | if (fld.resolvedType !== null) { 159 | if (fld.resolvedType instanceof ProtoBuf.Reflect.Message && fld.resolvedType.isGroup) { 160 | // inline legacy groups 161 | out.push("group "); 162 | isGroup = true; 163 | } 164 | out.push(msg.qn(fld.resolvedType)); 165 | } else 166 | out.push(fld.type['name']); 167 | if (!isGroup) 168 | out.push(" ", fld instanceof ProtoBuf.Reflect.Message.ExtensionField ? fld.name.substring(fld.name.lastIndexOf(".")+1) : fld.name); 169 | out.push(options.min ? "=" : " = ", fld.id); 170 | if (isGroup) // inline 171 | buildMessage(fld.resolvedType, indent); 172 | else { 173 | var keys = Object.keys(fld.options); 174 | if (keys.length > 0) { 175 | out.push(options.min ? "[" : " ["); 176 | var n = 0; 177 | keys.forEach(function (key) { 178 | if (n > 0) 179 | out.push(options.min ? "," : ", "); 180 | out.push(key, options.min ? "=" : " = ", 181 | // BEWARE: Monkey patch for string enum defaults 182 | key === "default" && fld.type === ProtoBuf.TYPES["enum"] && typeof fld.options[key] === 'string' ? fld.options[key] : value(fld.options[key]) 183 | ); 184 | n++; 185 | }); 186 | out.push("]"); 187 | } 188 | out.push(options.min ? ";" : ";\n"); 189 | } 190 | } 191 | 192 | // Builds an enum 193 | function buildEnum(enm, indent) { 194 | if (!options.min) 195 | out.push(indent); 196 | out.push("enum ", enm.name, options.min ? "{" : " {\n"); 197 | buildOptions(enm.options, indent+" "); 198 | enm.getChildren(ProtoBuf.Reflect.Enum.Value).forEach(function(val) { 199 | if (!options.min) 200 | out.push(indent, " "); 201 | out.push(val.name, options.min ? "=" : " = ", val.id, options.min? ";" : ";\n"); 202 | }); 203 | if (!options.min) 204 | out.push(indent); 205 | out.push(options.min ? "}" : "}\n\n"); 206 | } 207 | 208 | // Builds a service 209 | function buildService(svc, indent) { 210 | if (!options.min) 211 | out.push(indent); 212 | out.push("service ", svc.name, options.min ? "{" : " {\n"); 213 | buildOptions(svc.options, indent+" "); 214 | svc.getChildren(ProtoBuf.Reflect.Service.RPCMethod).forEach(function(rpc) { 215 | if (!options.min) 216 | out.push(indent+" "); 217 | out.push("rpc ", rpc.name, "(", svc.qn(rpc.resolvedRequestType), ") returns(", svc.qn(rpc.resolvedResponseType), ")"); 218 | var keys = Object.keys(rpc.options); 219 | if (keys.length === 0) { 220 | out.push(options.min ? ";" : ";\n") 221 | } else { 222 | out.push(options.min ? "{" : " {\n"); 223 | buildOptions(rpc.options, indent+" "); 224 | trim(); 225 | if (!options.min) 226 | out.push(indent+" "); 227 | out.push(options.min ? "}" : "}\n"); 228 | } 229 | if (!options.min) 230 | out[out.length-1] += "\n"; 231 | }); 232 | trim(); 233 | out.push(options.min ? "}" : "}\n"); 234 | } 235 | 236 | // Start by building the package namespace 237 | var pkg = ptr.fqn().substring(1); 238 | if (pkg !== "") 239 | out.push("package ", pkg, options.min ? ";" : ";\n\n"); 240 | buildOptions(ptr.options, ""); 241 | buildNamespace(ptr, ""); 242 | return out.join(''); 243 | }; 244 | 245 | /** 246 | * Module description. 247 | * @type {string} 248 | */ 249 | proto.description = description; 250 | 251 | /** 252 | * Converts a JavaScript value to a .proto value. 253 | * @param {*} v Value 254 | * @returns {string} Dot proto value 255 | */ 256 | function value(v) { 257 | switch (typeof v) { 258 | case 'boolean': 259 | return v ? 'true' : 'false'; 260 | case 'number': 261 | return v.toString(); 262 | case 'string': 263 | return '"'+v.replace(/"/g, '\\"')+'"'; 264 | default: 265 | throw new Error("illegal value type: "+typeof(v)); 266 | } 267 | } 268 | --------------------------------------------------------------------------------