2014-10-29 23:04:51 +00:00
|
|
|
|
/*
|
|
|
|
|
Copyright 2013 Daniel Wirtz <dcode@dcode.io>
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-10 17:48:42 +00:00
|
|
|
|
* @license protobuf.js (c) 2013 Daniel Wirtz <dcode@dcode.io>
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* Released under the Apache License, Version 2.0
|
2020-07-10 17:48:42 +00:00
|
|
|
|
* see: https://github.com/dcodeIO/protobuf.js for details
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
(function(global, factory) {
|
|
|
|
|
|
|
|
|
|
/* AMD */ if (typeof define === 'function' && define["amd"])
|
2020-07-10 17:48:42 +00:00
|
|
|
|
define(["bytebuffer"], factory);
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/* CommonJS */ else if (typeof require === "function" && typeof module === "object" && module && module["exports"])
|
|
|
|
|
module["exports"] = factory(require("bytebuffer"), true);
|
|
|
|
|
/* Global */ else
|
|
|
|
|
(global["dcodeIO"] = global["dcodeIO"] || {})["ProtoBuf"] = factory(global["dcodeIO"]["ByteBuffer"]);
|
|
|
|
|
|
|
|
|
|
})(this, function(ByteBuffer, isCommonJS) {
|
2014-10-29 23:04:51 +00:00
|
|
|
|
"use strict";
|
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* The ProtoBuf namespace.
|
|
|
|
|
* @exports ProtoBuf
|
|
|
|
|
* @namespace
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
var ProtoBuf = {};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @type {!function(new: ByteBuffer, ...[*])}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.ByteBuffer = ByteBuffer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @type {?function(new: Long, ...[*])}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.Long = ByteBuffer.Long || null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ProtoBuf.js version.
|
|
|
|
|
* @type {string}
|
|
|
|
|
* @const
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-07-10 17:48:42 +00:00
|
|
|
|
ProtoBuf.VERSION = "5.0.1";
|
2020-02-07 19:37:04 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wire types.
|
|
|
|
|
* @type {Object.<string,number>}
|
|
|
|
|
* @const
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.WIRE_TYPES = {};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Varint wire type.
|
|
|
|
|
* @type {number}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.WIRE_TYPES.VARINT = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fixed 64 bits wire type.
|
|
|
|
|
* @type {number}
|
|
|
|
|
* @const
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.WIRE_TYPES.BITS64 = 1;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Length delimited wire type.
|
|
|
|
|
* @type {number}
|
|
|
|
|
* @const
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.WIRE_TYPES.LDELIM = 2;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Start group wire type.
|
|
|
|
|
* @type {number}
|
|
|
|
|
* @const
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.WIRE_TYPES.STARTGROUP = 3;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* End group wire type.
|
|
|
|
|
* @type {number}
|
|
|
|
|
* @const
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.WIRE_TYPES.ENDGROUP = 4;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fixed 32 bits wire type.
|
|
|
|
|
* @type {number}
|
|
|
|
|
* @const
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.WIRE_TYPES.BITS32 = 5;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Packable wire types.
|
|
|
|
|
* @type {!Array.<number>}
|
|
|
|
|
* @const
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.PACKABLE_WIRE_TYPES = [
|
|
|
|
|
ProtoBuf.WIRE_TYPES.VARINT,
|
|
|
|
|
ProtoBuf.WIRE_TYPES.BITS64,
|
|
|
|
|
ProtoBuf.WIRE_TYPES.BITS32
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Types.
|
|
|
|
|
* @dict
|
|
|
|
|
* @type {!Object.<string,{name: string, wireType: number, defaultValue: *}>}
|
|
|
|
|
* @const
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.TYPES = {
|
|
|
|
|
// According to the protobuf spec.
|
|
|
|
|
"int32": {
|
|
|
|
|
name: "int32",
|
|
|
|
|
wireType: ProtoBuf.WIRE_TYPES.VARINT,
|
|
|
|
|
defaultValue: 0
|
|
|
|
|
},
|
|
|
|
|
"uint32": {
|
|
|
|
|
name: "uint32",
|
|
|
|
|
wireType: ProtoBuf.WIRE_TYPES.VARINT,
|
|
|
|
|
defaultValue: 0
|
|
|
|
|
},
|
|
|
|
|
"sint32": {
|
|
|
|
|
name: "sint32",
|
|
|
|
|
wireType: ProtoBuf.WIRE_TYPES.VARINT,
|
|
|
|
|
defaultValue: 0
|
|
|
|
|
},
|
|
|
|
|
"int64": {
|
|
|
|
|
name: "int64",
|
|
|
|
|
wireType: ProtoBuf.WIRE_TYPES.VARINT,
|
|
|
|
|
defaultValue: ProtoBuf.Long ? ProtoBuf.Long.ZERO : undefined
|
|
|
|
|
},
|
|
|
|
|
"uint64": {
|
|
|
|
|
name: "uint64",
|
|
|
|
|
wireType: ProtoBuf.WIRE_TYPES.VARINT,
|
|
|
|
|
defaultValue: ProtoBuf.Long ? ProtoBuf.Long.UZERO : undefined
|
|
|
|
|
},
|
|
|
|
|
"sint64": {
|
|
|
|
|
name: "sint64",
|
|
|
|
|
wireType: ProtoBuf.WIRE_TYPES.VARINT,
|
|
|
|
|
defaultValue: ProtoBuf.Long ? ProtoBuf.Long.ZERO : undefined
|
|
|
|
|
},
|
|
|
|
|
"bool": {
|
|
|
|
|
name: "bool",
|
|
|
|
|
wireType: ProtoBuf.WIRE_TYPES.VARINT,
|
|
|
|
|
defaultValue: false
|
|
|
|
|
},
|
|
|
|
|
"double": {
|
|
|
|
|
name: "double",
|
|
|
|
|
wireType: ProtoBuf.WIRE_TYPES.BITS64,
|
|
|
|
|
defaultValue: 0
|
|
|
|
|
},
|
|
|
|
|
"string": {
|
|
|
|
|
name: "string",
|
|
|
|
|
wireType: ProtoBuf.WIRE_TYPES.LDELIM,
|
|
|
|
|
defaultValue: ""
|
|
|
|
|
},
|
|
|
|
|
"bytes": {
|
|
|
|
|
name: "bytes",
|
|
|
|
|
wireType: ProtoBuf.WIRE_TYPES.LDELIM,
|
|
|
|
|
defaultValue: null // overridden in the code, must be a unique instance
|
|
|
|
|
},
|
|
|
|
|
"fixed32": {
|
|
|
|
|
name: "fixed32",
|
|
|
|
|
wireType: ProtoBuf.WIRE_TYPES.BITS32,
|
|
|
|
|
defaultValue: 0
|
|
|
|
|
},
|
|
|
|
|
"sfixed32": {
|
|
|
|
|
name: "sfixed32",
|
|
|
|
|
wireType: ProtoBuf.WIRE_TYPES.BITS32,
|
|
|
|
|
defaultValue: 0
|
|
|
|
|
},
|
|
|
|
|
"fixed64": {
|
|
|
|
|
name: "fixed64",
|
|
|
|
|
wireType: ProtoBuf.WIRE_TYPES.BITS64,
|
|
|
|
|
defaultValue: ProtoBuf.Long ? ProtoBuf.Long.UZERO : undefined
|
|
|
|
|
},
|
|
|
|
|
"sfixed64": {
|
|
|
|
|
name: "sfixed64",
|
|
|
|
|
wireType: ProtoBuf.WIRE_TYPES.BITS64,
|
|
|
|
|
defaultValue: ProtoBuf.Long ? ProtoBuf.Long.ZERO : undefined
|
|
|
|
|
},
|
|
|
|
|
"float": {
|
|
|
|
|
name: "float",
|
|
|
|
|
wireType: ProtoBuf.WIRE_TYPES.BITS32,
|
|
|
|
|
defaultValue: 0
|
|
|
|
|
},
|
|
|
|
|
"enum": {
|
|
|
|
|
name: "enum",
|
|
|
|
|
wireType: ProtoBuf.WIRE_TYPES.VARINT,
|
|
|
|
|
defaultValue: 0
|
|
|
|
|
},
|
|
|
|
|
"message": {
|
|
|
|
|
name: "message",
|
|
|
|
|
wireType: ProtoBuf.WIRE_TYPES.LDELIM,
|
|
|
|
|
defaultValue: null
|
|
|
|
|
},
|
|
|
|
|
"group": {
|
|
|
|
|
name: "group",
|
|
|
|
|
wireType: ProtoBuf.WIRE_TYPES.STARTGROUP,
|
|
|
|
|
defaultValue: null
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Valid map key types.
|
|
|
|
|
* @type {!Array.<!Object.<string,{name: string, wireType: number, defaultValue: *}>>}
|
|
|
|
|
* @const
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.MAP_KEY_TYPES = [
|
|
|
|
|
ProtoBuf.TYPES["int32"],
|
|
|
|
|
ProtoBuf.TYPES["sint32"],
|
|
|
|
|
ProtoBuf.TYPES["sfixed32"],
|
|
|
|
|
ProtoBuf.TYPES["uint32"],
|
|
|
|
|
ProtoBuf.TYPES["fixed32"],
|
|
|
|
|
ProtoBuf.TYPES["int64"],
|
|
|
|
|
ProtoBuf.TYPES["sint64"],
|
|
|
|
|
ProtoBuf.TYPES["sfixed64"],
|
|
|
|
|
ProtoBuf.TYPES["uint64"],
|
|
|
|
|
ProtoBuf.TYPES["fixed64"],
|
|
|
|
|
ProtoBuf.TYPES["bool"],
|
|
|
|
|
ProtoBuf.TYPES["string"],
|
|
|
|
|
ProtoBuf.TYPES["bytes"]
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Minimum field id.
|
|
|
|
|
* @type {number}
|
|
|
|
|
* @const
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.ID_MIN = 1;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Maximum field id.
|
|
|
|
|
* @type {number}
|
|
|
|
|
* @const
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.ID_MAX = 0x1FFFFFFF;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If set to `true`, field names will be converted from underscore notation to camel case. Defaults to `false`.
|
|
|
|
|
* Must be set prior to parsing.
|
|
|
|
|
* @type {boolean}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.convertFieldsToCamelCase = false;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* By default, messages are populated with (setX, set_x) accessors for each field. This can be disabled by
|
|
|
|
|
* setting this to `false` prior to building messages.
|
|
|
|
|
* @type {boolean}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.populateAccessors = true;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* By default, messages are populated with default values if a field is not present on the wire. To disable
|
|
|
|
|
* this behavior, set this setting to `false`.
|
|
|
|
|
* @type {boolean}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.populateDefaults = true;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Util
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.Util = (function() {
|
|
|
|
|
"use strict";
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* ProtoBuf utilities.
|
|
|
|
|
* @exports ProtoBuf.Util
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @namespace
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var Util = {};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Flag if running in node or not.
|
|
|
|
|
* @type {boolean}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @const
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
Util.IS_NODE = !!(
|
|
|
|
|
typeof process === 'object' && process+'' === '[object process]' && !process['browser']
|
|
|
|
|
);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Constructs a XMLHttpRequest object.
|
|
|
|
|
* @return {XMLHttpRequest}
|
|
|
|
|
* @throws {Error} If XMLHttpRequest is not supported
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
Util.XHR = function() {
|
|
|
|
|
// No dependencies please, ref: http://www.quirksmode.org/js/xmlhttp.html
|
|
|
|
|
var XMLHttpFactories = [
|
|
|
|
|
function () {return new XMLHttpRequest()},
|
|
|
|
|
function () {return new ActiveXObject("Msxml2.XMLHTTP")},
|
|
|
|
|
function () {return new ActiveXObject("Msxml3.XMLHTTP")},
|
|
|
|
|
function () {return new ActiveXObject("Microsoft.XMLHTTP")}
|
|
|
|
|
];
|
|
|
|
|
/** @type {?XMLHttpRequest} */
|
|
|
|
|
var xhr = null;
|
|
|
|
|
for (var i=0;i<XMLHttpFactories.length;i++) {
|
|
|
|
|
try { xhr = XMLHttpFactories[i](); }
|
|
|
|
|
catch (e) { continue; }
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (!xhr)
|
|
|
|
|
throw Error("XMLHttpRequest is not supported");
|
|
|
|
|
return xhr;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Fetches a resource.
|
|
|
|
|
* @param {string} path Resource path
|
|
|
|
|
* @param {function(?string)=} callback Callback receiving the resource's contents. If omitted the resource will
|
|
|
|
|
* be fetched synchronously. If the request failed, contents will be null.
|
|
|
|
|
* @return {?string|undefined} Resource contents if callback is omitted (null if the request failed), else undefined.
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
Util.fetch = function(path, callback) {
|
|
|
|
|
if (callback && typeof callback != 'function')
|
|
|
|
|
callback = null;
|
|
|
|
|
if (Util.IS_NODE) {
|
|
|
|
|
var fs = require("fs");
|
|
|
|
|
if (callback) {
|
|
|
|
|
fs.readFile(path, function(err, data) {
|
|
|
|
|
if (err)
|
|
|
|
|
callback(null);
|
|
|
|
|
else
|
|
|
|
|
callback(""+data);
|
|
|
|
|
});
|
|
|
|
|
} else
|
|
|
|
|
try {
|
|
|
|
|
return fs.readFileSync(path);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
var xhr = Util.XHR();
|
|
|
|
|
xhr.open('GET', path, callback ? true : false);
|
|
|
|
|
// xhr.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
|
|
|
|
|
xhr.setRequestHeader('Accept', 'text/plain');
|
|
|
|
|
if (typeof xhr.overrideMimeType === 'function') xhr.overrideMimeType('text/plain');
|
|
|
|
|
if (callback) {
|
|
|
|
|
xhr.onreadystatechange = function() {
|
|
|
|
|
if (xhr.readyState != 4) return;
|
|
|
|
|
if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))
|
|
|
|
|
callback(xhr.responseText);
|
|
|
|
|
else
|
|
|
|
|
callback(null);
|
|
|
|
|
};
|
|
|
|
|
if (xhr.readyState == 4)
|
|
|
|
|
return;
|
|
|
|
|
xhr.send(null);
|
|
|
|
|
} else {
|
|
|
|
|
xhr.send(null);
|
|
|
|
|
if (/* remote */ xhr.status == 200 || /* local */ (xhr.status == 0 && typeof xhr.responseText === 'string'))
|
|
|
|
|
return xhr.responseText;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Converts a string to camel case.
|
|
|
|
|
* @param {string} str
|
|
|
|
|
* @returns {string}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
Util.toCamelCase = function(str) {
|
|
|
|
|
return str.replace(/_([a-zA-Z])/g, function ($0, $1) {
|
|
|
|
|
return $1.toUpperCase();
|
|
|
|
|
});
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
return Util;
|
|
|
|
|
})();
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Language expressions.
|
|
|
|
|
* @type {!Object.<string,!RegExp>}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.Lang = {
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Characters always ending a statement
|
|
|
|
|
DELIM: /[\s\{\}=;:\[\],'"\(\)<>]/g,
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Field rules
|
|
|
|
|
RULE: /^(?:required|optional|repeated|map)$/,
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Field types
|
|
|
|
|
TYPE: /^(?:double|float|int32|uint32|sint32|int64|uint64|sint64|fixed32|sfixed32|fixed64|sfixed64|bool|string|bytes)$/,
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Names
|
|
|
|
|
NAME: /^[a-zA-Z_][a-zA-Z_0-9]*$/,
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Type definitions
|
|
|
|
|
TYPEDEF: /^[a-zA-Z][a-zA-Z_0-9]*$/,
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Type references
|
|
|
|
|
TYPEREF: /^(?:\.?[a-zA-Z_][a-zA-Z_0-9]*)+$/,
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Fully qualified type references
|
|
|
|
|
FQTYPEREF: /^(?:\.[a-zA-Z][a-zA-Z_0-9]*)+$/,
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// All numbers
|
|
|
|
|
NUMBER: /^-?(?:[1-9][0-9]*|0|0[xX][0-9a-fA-F]+|0[0-7]+|([0-9]*(\.[0-9]*)?([Ee][+-]?[0-9]+)?)|inf|nan)$/,
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Decimal numbers
|
|
|
|
|
NUMBER_DEC: /^(?:[1-9][0-9]*|0)$/,
|
|
|
|
|
|
|
|
|
|
// Hexadecimal numbers
|
|
|
|
|
NUMBER_HEX: /^0[xX][0-9a-fA-F]+$/,
|
|
|
|
|
|
|
|
|
|
// Octal numbers
|
|
|
|
|
NUMBER_OCT: /^0[0-7]+$/,
|
|
|
|
|
|
|
|
|
|
// Floating point numbers
|
|
|
|
|
NUMBER_FLT: /^([0-9]*(\.[0-9]*)?([Ee][+-]?[0-9]+)?|inf|nan)$/,
|
|
|
|
|
|
|
|
|
|
// Booleans
|
|
|
|
|
BOOL: /^(?:true|false)$/i,
|
|
|
|
|
|
|
|
|
|
// Id numbers
|
|
|
|
|
ID: /^(?:[1-9][0-9]*|0|0[xX][0-9a-fA-F]+|0[0-7]+)$/,
|
|
|
|
|
|
|
|
|
|
// Negative id numbers (enum values)
|
|
|
|
|
NEGID: /^\-?(?:[1-9][0-9]*|0|0[xX][0-9a-fA-F]+|0[0-7]+)$/,
|
|
|
|
|
|
|
|
|
|
// Whitespaces
|
|
|
|
|
WHITESPACE: /\s/,
|
|
|
|
|
|
|
|
|
|
// All strings
|
|
|
|
|
STRING: /(?:"([^"\\]*(?:\\.[^"\\]*)*)")|(?:'([^'\\]*(?:\\.[^'\\]*)*)')/g,
|
|
|
|
|
|
|
|
|
|
// Double quoted strings
|
|
|
|
|
STRING_DQ: /(?:"([^"\\]*(?:\\.[^"\\]*)*)")/g,
|
|
|
|
|
|
|
|
|
|
// Single quoted strings
|
|
|
|
|
STRING_SQ: /(?:'([^'\\]*(?:\\.[^'\\]*)*)')/g
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.DotProto
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.DotProto = (function(ProtoBuf, Lang) {
|
|
|
|
|
"use strict";
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Utilities to parse .proto files.
|
|
|
|
|
* @exports ProtoBuf.DotProto
|
|
|
|
|
* @namespace
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var DotProto = {};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Constructs a new Tokenizer.
|
|
|
|
|
* @exports ProtoBuf.DotProto.Tokenizer
|
|
|
|
|
* @class prototype tokenizer
|
|
|
|
|
* @param {string} proto Proto to tokenize
|
|
|
|
|
* @constructor
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var Tokenizer = function(proto) {
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Source to parse.
|
|
|
|
|
* @type {string}
|
|
|
|
|
* @expose
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.source = proto+"";
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Current index.
|
|
|
|
|
* @type {number}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.index = 0;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Current line.
|
|
|
|
|
* @type {number}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.line = 1;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Token stack.
|
|
|
|
|
* @type {!Array.<string>}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.stack = [];
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Opening character of the current string read, if any.
|
|
|
|
|
* @type {?string}
|
|
|
|
|
* @private
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this._stringOpen = null;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.DotProto.Tokenizer.prototype
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
var TokenizerPrototype = Tokenizer.prototype;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Reads a string beginning at the current index.
|
|
|
|
|
* @return {string}
|
|
|
|
|
* @private
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
TokenizerPrototype._readString = function() {
|
|
|
|
|
var re = this._stringOpen === '"'
|
|
|
|
|
? Lang.STRING_DQ
|
|
|
|
|
: Lang.STRING_SQ;
|
|
|
|
|
re.lastIndex = this.index - 1; // Include the open quote
|
|
|
|
|
var match = re.exec(this.source);
|
|
|
|
|
if (!match)
|
|
|
|
|
throw Error("unterminated string");
|
|
|
|
|
this.index = re.lastIndex;
|
|
|
|
|
this.stack.push(this._stringOpen);
|
|
|
|
|
this._stringOpen = null;
|
|
|
|
|
return match[1];
|
2014-10-29 23:04:51 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Gets the next token and advances by one.
|
|
|
|
|
* @return {?string} Token or `null` on EOF
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
TokenizerPrototype.next = function() {
|
|
|
|
|
if (this.stack.length > 0)
|
|
|
|
|
return this.stack.shift();
|
|
|
|
|
if (this.index >= this.source.length)
|
|
|
|
|
return null;
|
|
|
|
|
if (this._stringOpen !== null)
|
|
|
|
|
return this._readString();
|
|
|
|
|
|
|
|
|
|
var repeat,
|
|
|
|
|
prev,
|
|
|
|
|
next;
|
|
|
|
|
do {
|
|
|
|
|
repeat = false;
|
|
|
|
|
|
|
|
|
|
// Strip white spaces
|
|
|
|
|
while (Lang.WHITESPACE.test(next = this.source.charAt(this.index))) {
|
|
|
|
|
if (next === '\n')
|
|
|
|
|
++this.line;
|
|
|
|
|
if (++this.index === this.source.length)
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Strip comments
|
|
|
|
|
if (this.source.charAt(this.index) === '/') {
|
|
|
|
|
++this.index;
|
|
|
|
|
if (this.source.charAt(this.index) === '/') { // Line
|
|
|
|
|
while (this.source.charAt(++this.index) !== '\n')
|
|
|
|
|
if (this.index == this.source.length)
|
|
|
|
|
return null;
|
|
|
|
|
++this.index;
|
|
|
|
|
++this.line;
|
|
|
|
|
repeat = true;
|
|
|
|
|
} else if ((next = this.source.charAt(this.index)) === '*') { /* Block */
|
|
|
|
|
do {
|
|
|
|
|
if (next === '\n')
|
|
|
|
|
++this.line;
|
|
|
|
|
if (++this.index === this.source.length)
|
|
|
|
|
return null;
|
|
|
|
|
prev = next;
|
|
|
|
|
next = this.source.charAt(this.index);
|
|
|
|
|
} while (prev !== '*' || next !== '/');
|
|
|
|
|
++this.index;
|
|
|
|
|
repeat = true;
|
|
|
|
|
} else
|
|
|
|
|
return '/';
|
|
|
|
|
}
|
|
|
|
|
} while (repeat);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (this.index === this.source.length)
|
|
|
|
|
return null;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Read the next token
|
|
|
|
|
var end = this.index;
|
|
|
|
|
Lang.DELIM.lastIndex = 0;
|
|
|
|
|
var delim = Lang.DELIM.test(this.source.charAt(end++));
|
|
|
|
|
if (!delim)
|
|
|
|
|
while(end < this.source.length && !Lang.DELIM.test(this.source.charAt(end)))
|
|
|
|
|
++end;
|
|
|
|
|
var token = this.source.substring(this.index, this.index = end);
|
|
|
|
|
if (token === '"' || token === "'")
|
|
|
|
|
this._stringOpen = token;
|
|
|
|
|
return token;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Peeks for the next token.
|
|
|
|
|
* @return {?string} Token or `null` on EOF
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
TokenizerPrototype.peek = function() {
|
|
|
|
|
if (this.stack.length === 0) {
|
|
|
|
|
var token = this.next();
|
|
|
|
|
if (token === null)
|
|
|
|
|
return null;
|
|
|
|
|
this.stack.push(token);
|
|
|
|
|
}
|
|
|
|
|
return this.stack[0];
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Skips a specific token and throws if it differs.
|
|
|
|
|
* @param {string} expected Expected token
|
|
|
|
|
* @throws {Error} If the actual token differs
|
|
|
|
|
*/
|
|
|
|
|
TokenizerPrototype.skip = function(expected) {
|
|
|
|
|
var actual = this.next();
|
|
|
|
|
if (actual !== expected)
|
|
|
|
|
throw Error("illegal '"+actual+"', '"+expected+"' expected");
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Omits an optional token.
|
|
|
|
|
* @param {string} expected Expected optional token
|
|
|
|
|
* @returns {boolean} `true` if the token exists
|
|
|
|
|
*/
|
|
|
|
|
TokenizerPrototype.omit = function(expected) {
|
|
|
|
|
if (this.peek() === expected) {
|
|
|
|
|
this.next();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns a string representation of this object.
|
|
|
|
|
* @return {string} String representation as of "Tokenizer(index/length)"
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
TokenizerPrototype.toString = function() {
|
|
|
|
|
return "Tokenizer ("+this.index+"/"+this.source.length+" at line "+this.line+")";
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.DotProto.Tokenizer
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
DotProto.Tokenizer = Tokenizer;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Constructs a new Parser.
|
|
|
|
|
* @exports ProtoBuf.DotProto.Parser
|
|
|
|
|
* @class prototype parser
|
|
|
|
|
* @param {string} source Source
|
|
|
|
|
* @constructor
|
|
|
|
|
*/
|
|
|
|
|
var Parser = function(source) {
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Tokenizer.
|
|
|
|
|
* @type {!ProtoBuf.DotProto.Tokenizer}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.tn = new Tokenizer(source);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Whether parsing proto3 or not.
|
|
|
|
|
* @type {boolean}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.proto3 = false;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.DotProto.Parser.prototype
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
var ParserPrototype = Parser.prototype;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Parses the source.
|
|
|
|
|
* @returns {!Object}
|
|
|
|
|
* @throws {Error} If the source cannot be parsed
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ParserPrototype.parse = function() {
|
|
|
|
|
var topLevel = {
|
|
|
|
|
"name": "[ROOT]", // temporary
|
|
|
|
|
"package": null,
|
|
|
|
|
"messages": [],
|
|
|
|
|
"enums": [],
|
|
|
|
|
"imports": [],
|
|
|
|
|
"options": {},
|
|
|
|
|
"services": []
|
|
|
|
|
// "syntax": undefined
|
2014-10-29 23:04:51 +00:00
|
|
|
|
};
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var token,
|
2020-07-10 17:48:42 +00:00
|
|
|
|
head = true,
|
|
|
|
|
weak;
|
2020-02-07 19:37:04 +00:00
|
|
|
|
try {
|
|
|
|
|
while (token = this.tn.next()) {
|
2014-10-29 23:04:51 +00:00
|
|
|
|
switch (token) {
|
|
|
|
|
case 'package':
|
|
|
|
|
if (!head || topLevel["package"] !== null)
|
2020-02-07 19:37:04 +00:00
|
|
|
|
throw Error("unexpected 'package'");
|
|
|
|
|
token = this.tn.next();
|
|
|
|
|
if (!Lang.TYPEREF.test(token))
|
|
|
|
|
throw Error("illegal package name: " + token);
|
|
|
|
|
this.tn.skip(";");
|
|
|
|
|
topLevel["package"] = token;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
break;
|
|
|
|
|
case 'import':
|
|
|
|
|
if (!head)
|
2020-02-07 19:37:04 +00:00
|
|
|
|
throw Error("unexpected 'import'");
|
|
|
|
|
token = this.tn.peek();
|
2020-07-10 17:48:42 +00:00
|
|
|
|
if (token === "public" || (weak = token === "weak")) // token ignored
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.tn.next();
|
|
|
|
|
token = this._readString();
|
|
|
|
|
this.tn.skip(";");
|
2020-07-10 17:48:42 +00:00
|
|
|
|
if (!weak) // import ignored
|
|
|
|
|
topLevel["imports"].push(token);
|
2020-02-07 19:37:04 +00:00
|
|
|
|
break;
|
|
|
|
|
case 'syntax':
|
|
|
|
|
if (!head)
|
|
|
|
|
throw Error("unexpected 'syntax'");
|
|
|
|
|
this.tn.skip("=");
|
|
|
|
|
if ((topLevel["syntax"] = this._readString()) === "proto3")
|
|
|
|
|
this.proto3 = true;
|
|
|
|
|
this.tn.skip(";");
|
2014-10-29 23:04:51 +00:00
|
|
|
|
break;
|
|
|
|
|
case 'message':
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this._parseMessage(topLevel, null);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
head = false;
|
|
|
|
|
break;
|
|
|
|
|
case 'enum':
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this._parseEnum(topLevel);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
head = false;
|
|
|
|
|
break;
|
|
|
|
|
case 'option':
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this._parseOption(topLevel);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
break;
|
|
|
|
|
case 'service':
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this._parseService(topLevel);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
break;
|
|
|
|
|
case 'extend':
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this._parseExtend(topLevel);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2020-02-07 19:37:04 +00:00
|
|
|
|
throw Error("unexpected '" + token + "'");
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
} catch (e) {
|
|
|
|
|
e.message = "Parse error at line "+this.tn.line+": " + e.message;
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
delete topLevel["name"];
|
|
|
|
|
return topLevel;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Parses the specified source.
|
|
|
|
|
* @returns {!Object}
|
|
|
|
|
* @throws {Error} If the source cannot be parsed
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Parser.parse = function(source) {
|
|
|
|
|
return new Parser(source).parse();
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// ----- Conversion ------
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Converts a numerical string to an id.
|
|
|
|
|
* @param {string} value
|
|
|
|
|
* @param {boolean=} mayBeNegative
|
|
|
|
|
* @returns {number}
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
function mkId(value, mayBeNegative) {
|
|
|
|
|
var id = -1,
|
|
|
|
|
sign = 1;
|
|
|
|
|
if (value.charAt(0) == '-') {
|
|
|
|
|
sign = -1;
|
|
|
|
|
value = value.substring(1);
|
|
|
|
|
}
|
|
|
|
|
if (Lang.NUMBER_DEC.test(value))
|
|
|
|
|
id = parseInt(value);
|
|
|
|
|
else if (Lang.NUMBER_HEX.test(value))
|
|
|
|
|
id = parseInt(value.substring(2), 16);
|
|
|
|
|
else if (Lang.NUMBER_OCT.test(value))
|
|
|
|
|
id = parseInt(value.substring(1), 8);
|
|
|
|
|
else
|
|
|
|
|
throw Error("illegal id value: " + (sign < 0 ? '-' : '') + value);
|
|
|
|
|
id = (sign*id)|0; // Force to 32bit
|
|
|
|
|
if (!mayBeNegative && id < 0)
|
|
|
|
|
throw Error("illegal id value: " + (sign < 0 ? '-' : '') + value);
|
|
|
|
|
return id;
|
|
|
|
|
}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Converts a numerical string to a number.
|
|
|
|
|
* @param {string} val
|
|
|
|
|
* @returns {number}
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
function mkNumber(val) {
|
|
|
|
|
var sign = 1;
|
|
|
|
|
if (val.charAt(0) == '-') {
|
|
|
|
|
sign = -1;
|
|
|
|
|
val = val.substring(1);
|
|
|
|
|
}
|
|
|
|
|
if (Lang.NUMBER_DEC.test(val))
|
|
|
|
|
return sign * parseInt(val, 10);
|
|
|
|
|
else if (Lang.NUMBER_HEX.test(val))
|
|
|
|
|
return sign * parseInt(val.substring(2), 16);
|
|
|
|
|
else if (Lang.NUMBER_OCT.test(val))
|
|
|
|
|
return sign * parseInt(val.substring(1), 8);
|
|
|
|
|
else if (val === 'inf')
|
|
|
|
|
return sign * Infinity;
|
|
|
|
|
else if (val === 'nan')
|
|
|
|
|
return NaN;
|
|
|
|
|
else if (Lang.NUMBER_FLT.test(val))
|
|
|
|
|
return sign * parseFloat(val);
|
|
|
|
|
throw Error("illegal number value: " + (sign < 0 ? '-' : '') + val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----- Reading ------
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Reads a string.
|
|
|
|
|
* @returns {string}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
ParserPrototype._readString = function() {
|
|
|
|
|
var value = "",
|
|
|
|
|
token,
|
|
|
|
|
delim;
|
|
|
|
|
do {
|
|
|
|
|
delim = this.tn.next();
|
|
|
|
|
if (delim !== "'" && delim !== '"')
|
|
|
|
|
throw Error("illegal string delimiter: "+delim);
|
|
|
|
|
value += this.tn.next();
|
|
|
|
|
this.tn.skip(delim);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
token = this.tn.peek();
|
2020-02-07 19:37:04 +00:00
|
|
|
|
} while (token === '"' || token === '"'); // multi line?
|
|
|
|
|
return value;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Reads a value.
|
|
|
|
|
* @param {boolean=} mayBeTypeRef
|
|
|
|
|
* @returns {number|boolean|string}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
ParserPrototype._readValue = function(mayBeTypeRef) {
|
|
|
|
|
var token = this.tn.peek(),
|
|
|
|
|
value;
|
|
|
|
|
if (token === '"' || token === "'")
|
|
|
|
|
return this._readString();
|
|
|
|
|
this.tn.next();
|
|
|
|
|
if (Lang.NUMBER.test(token))
|
|
|
|
|
return mkNumber(token);
|
|
|
|
|
if (Lang.BOOL.test(token))
|
|
|
|
|
return (token.toLowerCase() === 'true');
|
|
|
|
|
if (mayBeTypeRef && Lang.TYPEREF.test(token))
|
|
|
|
|
return token;
|
|
|
|
|
throw Error("illegal value: "+token);
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ----- Parsing constructs -----
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parses a namespace option.
|
|
|
|
|
* @param {!Object} parent Parent definition
|
|
|
|
|
* @param {boolean=} isList
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
ParserPrototype._parseOption = function(parent, isList) {
|
|
|
|
|
var token = this.tn.next(),
|
|
|
|
|
custom = false;
|
|
|
|
|
if (token === '(') {
|
|
|
|
|
custom = true;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
token = this.tn.next();
|
2020-02-07 19:37:04 +00:00
|
|
|
|
}
|
|
|
|
|
if (!Lang.TYPEREF.test(token))
|
|
|
|
|
// we can allow options of the form google.protobuf.* since they will just get ignored anyways
|
|
|
|
|
// if (!/google\.protobuf\./.test(token)) // FIXME: Why should that not be a valid typeref?
|
|
|
|
|
throw Error("illegal option name: "+token);
|
|
|
|
|
var name = token;
|
|
|
|
|
if (custom) { // (my_method_option).foo, (my_method_option), some_method_option, (foo.my_option).bar
|
|
|
|
|
this.tn.skip(')');
|
|
|
|
|
name = '('+name+')';
|
2014-10-29 23:04:51 +00:00
|
|
|
|
token = this.tn.peek();
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (Lang.FQTYPEREF.test(token)) {
|
|
|
|
|
name += token;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
this.tn.next();
|
2020-02-07 19:37:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.tn.skip('=');
|
|
|
|
|
this._parseOptionValue(parent, name);
|
|
|
|
|
if (!isList)
|
|
|
|
|
this.tn.skip(";");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets an option on the specified options object.
|
|
|
|
|
* @param {!Object.<string,*>} options
|
|
|
|
|
* @param {string} name
|
|
|
|
|
* @param {string|number|boolean} value
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
function setOption(options, name, value) {
|
|
|
|
|
if (typeof options[name] === 'undefined')
|
|
|
|
|
options[name] = value;
|
|
|
|
|
else {
|
|
|
|
|
if (!Array.isArray(options[name]))
|
|
|
|
|
options[name] = [ options[name] ];
|
|
|
|
|
options[name].push(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parses an option value.
|
|
|
|
|
* @param {!Object} parent
|
|
|
|
|
* @param {string} name
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
ParserPrototype._parseOptionValue = function(parent, name) {
|
|
|
|
|
var token = this.tn.peek();
|
|
|
|
|
if (token !== '{') { // Plain value
|
|
|
|
|
setOption(parent["options"], name, this._readValue(true));
|
|
|
|
|
} else { // Aggregate options
|
|
|
|
|
this.tn.skip("{");
|
|
|
|
|
while ((token = this.tn.next()) !== '}') {
|
|
|
|
|
if (!Lang.NAME.test(token))
|
|
|
|
|
throw Error("illegal option name: " + name + "." + token);
|
|
|
|
|
if (this.tn.omit(":"))
|
|
|
|
|
setOption(parent["options"], name + "." + token, this._readValue(true));
|
2014-10-29 23:04:51 +00:00
|
|
|
|
else
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this._parseOptionValue(parent, name + "." + token);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Parses a service definition.
|
|
|
|
|
* @param {!Object} parent Parent definition
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
ParserPrototype._parseService = function(parent) {
|
|
|
|
|
var token = this.tn.next();
|
|
|
|
|
if (!Lang.NAME.test(token))
|
|
|
|
|
throw Error("illegal service name at line "+this.tn.line+": "+token);
|
|
|
|
|
var name = token;
|
|
|
|
|
var svc = {
|
|
|
|
|
"name": name,
|
|
|
|
|
"rpc": {},
|
|
|
|
|
"options": {}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
};
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.tn.skip("{");
|
|
|
|
|
while ((token = this.tn.next()) !== '}') {
|
|
|
|
|
if (token === "option")
|
|
|
|
|
this._parseOption(svc);
|
|
|
|
|
else if (token === 'rpc')
|
|
|
|
|
this._parseServiceRPC(svc);
|
|
|
|
|
else
|
|
|
|
|
throw Error("illegal service token: "+token);
|
|
|
|
|
}
|
|
|
|
|
this.tn.omit(";");
|
|
|
|
|
parent["services"].push(svc);
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Parses a RPC service definition of the form ['rpc', name, (request), 'returns', (response)].
|
|
|
|
|
* @param {!Object} svc Service definition
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
ParserPrototype._parseServiceRPC = function(svc) {
|
|
|
|
|
var type = "rpc",
|
2014-10-29 23:04:51 +00:00
|
|
|
|
token = this.tn.next();
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (!Lang.NAME.test(token))
|
|
|
|
|
throw Error("illegal rpc service method name: "+token);
|
|
|
|
|
var name = token;
|
|
|
|
|
var method = {
|
|
|
|
|
"request": null,
|
|
|
|
|
"response": null,
|
|
|
|
|
"request_stream": false,
|
|
|
|
|
"response_stream": false,
|
|
|
|
|
"options": {}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
};
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.tn.skip("(");
|
|
|
|
|
token = this.tn.next();
|
|
|
|
|
if (token.toLowerCase() === "stream") {
|
|
|
|
|
method["request_stream"] = true;
|
|
|
|
|
token = this.tn.next();
|
|
|
|
|
}
|
|
|
|
|
if (!Lang.TYPEREF.test(token))
|
|
|
|
|
throw Error("illegal rpc service request type: "+token);
|
|
|
|
|
method["request"] = token;
|
|
|
|
|
this.tn.skip(")");
|
|
|
|
|
token = this.tn.next();
|
|
|
|
|
if (token.toLowerCase() !== "returns")
|
|
|
|
|
throw Error("illegal rpc service request type delimiter: "+token);
|
|
|
|
|
this.tn.skip("(");
|
|
|
|
|
token = this.tn.next();
|
|
|
|
|
if (token.toLowerCase() === "stream") {
|
|
|
|
|
method["response_stream"] = true;
|
|
|
|
|
token = this.tn.next();
|
|
|
|
|
}
|
|
|
|
|
method["response"] = token;
|
|
|
|
|
this.tn.skip(")");
|
|
|
|
|
token = this.tn.peek();
|
|
|
|
|
if (token === '{') {
|
|
|
|
|
this.tn.next();
|
|
|
|
|
while ((token = this.tn.next()) !== '}') {
|
|
|
|
|
if (token === 'option')
|
|
|
|
|
this._parseOption(method);
|
|
|
|
|
else
|
|
|
|
|
throw Error("illegal rpc service token: " + token);
|
|
|
|
|
}
|
|
|
|
|
this.tn.omit(";");
|
|
|
|
|
} else
|
|
|
|
|
this.tn.skip(";");
|
|
|
|
|
if (typeof svc[type] === 'undefined')
|
|
|
|
|
svc[type] = {};
|
|
|
|
|
svc[type][name] = method;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Parses a message definition.
|
|
|
|
|
* @param {!Object} parent Parent definition
|
|
|
|
|
* @param {!Object=} fld Field definition if this is a group
|
|
|
|
|
* @returns {!Object}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
ParserPrototype._parseMessage = function(parent, fld) {
|
|
|
|
|
var isGroup = !!fld,
|
2014-10-29 23:04:51 +00:00
|
|
|
|
token = this.tn.next();
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var msg = {
|
|
|
|
|
"name": "",
|
|
|
|
|
"fields": [],
|
|
|
|
|
"enums": [],
|
|
|
|
|
"messages": [],
|
|
|
|
|
"options": {},
|
2020-07-10 17:48:42 +00:00
|
|
|
|
"services": [],
|
2020-02-07 19:37:04 +00:00
|
|
|
|
"oneofs": {}
|
|
|
|
|
// "extensions": undefined
|
2014-10-29 23:04:51 +00:00
|
|
|
|
};
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (!Lang.NAME.test(token))
|
|
|
|
|
throw Error("illegal "+(isGroup ? "group" : "message")+" name: "+token);
|
|
|
|
|
msg["name"] = token;
|
|
|
|
|
if (isGroup) {
|
|
|
|
|
this.tn.skip("=");
|
|
|
|
|
fld["id"] = mkId(this.tn.next());
|
|
|
|
|
msg["isGroup"] = true;
|
|
|
|
|
}
|
|
|
|
|
token = this.tn.peek();
|
|
|
|
|
if (token === '[' && fld)
|
|
|
|
|
this._parseFieldOptions(fld);
|
|
|
|
|
this.tn.skip("{");
|
|
|
|
|
while ((token = this.tn.next()) !== '}') {
|
|
|
|
|
if (Lang.RULE.test(token))
|
|
|
|
|
this._parseMessageField(msg, token);
|
|
|
|
|
else if (token === "oneof")
|
|
|
|
|
this._parseMessageOneOf(msg);
|
|
|
|
|
else if (token === "enum")
|
|
|
|
|
this._parseEnum(msg);
|
|
|
|
|
else if (token === "message")
|
|
|
|
|
this._parseMessage(msg);
|
|
|
|
|
else if (token === "option")
|
|
|
|
|
this._parseOption(msg);
|
2020-07-10 17:48:42 +00:00
|
|
|
|
else if (token === "service")
|
|
|
|
|
this._parseService(msg);
|
2020-02-07 19:37:04 +00:00
|
|
|
|
else if (token === "extensions")
|
2020-07-10 17:48:42 +00:00
|
|
|
|
msg["extensions"] = this._parseExtensionRanges();
|
|
|
|
|
else if (token === "reserved")
|
|
|
|
|
this._parseIgnored(); // TODO
|
2020-02-07 19:37:04 +00:00
|
|
|
|
else if (token === "extend")
|
|
|
|
|
this._parseExtend(msg);
|
|
|
|
|
else if (Lang.TYPEREF.test(token)) {
|
|
|
|
|
if (!this.proto3)
|
|
|
|
|
throw Error("illegal field rule: "+token);
|
|
|
|
|
this._parseMessageField(msg, "optional", token);
|
|
|
|
|
} else
|
|
|
|
|
throw Error("illegal message token: "+token);
|
|
|
|
|
}
|
|
|
|
|
this.tn.omit(";");
|
|
|
|
|
parent["messages"].push(msg);
|
|
|
|
|
return msg;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-07-10 17:48:42 +00:00
|
|
|
|
/**
|
|
|
|
|
* Parses an ignored statement.
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
ParserPrototype._parseIgnored = function() {
|
|
|
|
|
while (this.tn.peek() !== ';')
|
|
|
|
|
this.tn.next();
|
|
|
|
|
this.tn.skip(";");
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Parses a message field.
|
|
|
|
|
* @param {!Object} msg Message definition
|
|
|
|
|
* @param {string} rule Field rule
|
|
|
|
|
* @param {string=} type Field type if already known (never known for maps)
|
|
|
|
|
* @returns {!Object} Field descriptor
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
ParserPrototype._parseMessageField = function(msg, rule, type) {
|
|
|
|
|
if (!Lang.RULE.test(rule))
|
|
|
|
|
throw Error("illegal message field rule: "+rule);
|
|
|
|
|
var fld = {
|
|
|
|
|
"rule": rule,
|
|
|
|
|
"type": "",
|
|
|
|
|
"name": "",
|
|
|
|
|
"options": {},
|
|
|
|
|
"id": 0
|
2014-10-29 23:04:51 +00:00
|
|
|
|
};
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var token;
|
|
|
|
|
if (rule === "map") {
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (type)
|
|
|
|
|
throw Error("illegal type: " + type);
|
|
|
|
|
this.tn.skip('<');
|
|
|
|
|
token = this.tn.next();
|
|
|
|
|
if (!Lang.TYPE.test(token) && !Lang.TYPEREF.test(token))
|
|
|
|
|
throw Error("illegal message field type: " + token);
|
|
|
|
|
fld["keytype"] = token;
|
|
|
|
|
this.tn.skip(',');
|
|
|
|
|
token = this.tn.next();
|
|
|
|
|
if (!Lang.TYPE.test(token) && !Lang.TYPEREF.test(token))
|
|
|
|
|
throw Error("illegal message field: " + token);
|
|
|
|
|
fld["type"] = token;
|
|
|
|
|
this.tn.skip('>');
|
2014-10-29 23:04:51 +00:00
|
|
|
|
token = this.tn.next();
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (!Lang.NAME.test(token))
|
|
|
|
|
throw Error("illegal message field name: " + token);
|
|
|
|
|
fld["name"] = token;
|
|
|
|
|
this.tn.skip("=");
|
|
|
|
|
fld["id"] = mkId(this.tn.next());
|
|
|
|
|
token = this.tn.peek();
|
|
|
|
|
if (token === '[')
|
|
|
|
|
this._parseFieldOptions(fld);
|
|
|
|
|
this.tn.skip(";");
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
type = typeof type !== 'undefined' ? type : this.tn.next();
|
|
|
|
|
|
|
|
|
|
if (type === "group") {
|
|
|
|
|
|
2014-10-29 23:04:51 +00:00
|
|
|
|
// "A [legacy] group simply combines a nested message type and a field into a single declaration. In your
|
|
|
|
|
// code, you can treat this message just as if it had a Result type field called result (the latter name is
|
|
|
|
|
// converted to lower-case so that it does not conflict with the former)."
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var grp = this._parseMessage(msg, fld);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
if (!/^[A-Z]/.test(grp["name"]))
|
2020-02-07 19:37:04 +00:00
|
|
|
|
throw Error('illegal group name: '+grp["name"]);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
fld["type"] = grp["name"];
|
|
|
|
|
fld["name"] = grp["name"].toLowerCase();
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.tn.omit(";");
|
|
|
|
|
|
2014-10-29 23:04:51 +00:00
|
|
|
|
} else {
|
2020-02-07 19:37:04 +00:00
|
|
|
|
|
|
|
|
|
if (!Lang.TYPE.test(type) && !Lang.TYPEREF.test(type))
|
|
|
|
|
throw Error("illegal message field type: " + type);
|
|
|
|
|
fld["type"] = type;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
token = this.tn.next();
|
|
|
|
|
if (!Lang.NAME.test(token))
|
2020-02-07 19:37:04 +00:00
|
|
|
|
throw Error("illegal message field name: " + token);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
fld["name"] = token;
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.tn.skip("=");
|
|
|
|
|
fld["id"] = mkId(this.tn.next());
|
|
|
|
|
token = this.tn.peek();
|
|
|
|
|
if (token === "[")
|
|
|
|
|
this._parseFieldOptions(fld);
|
|
|
|
|
this.tn.skip(";");
|
|
|
|
|
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
}
|
|
|
|
|
msg["fields"].push(fld);
|
|
|
|
|
return fld;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parses a message oneof.
|
|
|
|
|
* @param {!Object} msg Message definition
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
ParserPrototype._parseMessageOneOf = function(msg) {
|
|
|
|
|
var token = this.tn.next();
|
|
|
|
|
if (!Lang.NAME.test(token))
|
|
|
|
|
throw Error("illegal oneof name: "+token);
|
|
|
|
|
var name = token,
|
|
|
|
|
fld;
|
|
|
|
|
var fields = [];
|
|
|
|
|
this.tn.skip("{");
|
|
|
|
|
while ((token = this.tn.next()) !== "}") {
|
|
|
|
|
fld = this._parseMessageField(msg, "optional", token);
|
|
|
|
|
fld["oneof"] = name;
|
|
|
|
|
fields.push(fld["id"]);
|
|
|
|
|
}
|
|
|
|
|
this.tn.omit(";");
|
|
|
|
|
msg["oneofs"][name] = fields;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parses a set of field option definitions.
|
|
|
|
|
* @param {!Object} fld Field definition
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
ParserPrototype._parseFieldOptions = function(fld) {
|
|
|
|
|
this.tn.skip("[");
|
|
|
|
|
var token,
|
|
|
|
|
first = true;
|
|
|
|
|
while ((token = this.tn.peek()) !== ']') {
|
|
|
|
|
if (!first)
|
|
|
|
|
this.tn.skip(",");
|
|
|
|
|
this._parseOption(fld, true);
|
|
|
|
|
first = false;
|
|
|
|
|
}
|
|
|
|
|
this.tn.next();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parses an enum.
|
|
|
|
|
* @param {!Object} msg Message definition
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
ParserPrototype._parseEnum = function(msg) {
|
|
|
|
|
var enm = {
|
|
|
|
|
"name": "",
|
|
|
|
|
"values": [],
|
|
|
|
|
"options": {}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
};
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var token = this.tn.next();
|
|
|
|
|
if (!Lang.NAME.test(token))
|
|
|
|
|
throw Error("illegal name: "+token);
|
|
|
|
|
enm["name"] = token;
|
|
|
|
|
this.tn.skip("{");
|
|
|
|
|
while ((token = this.tn.next()) !== '}') {
|
|
|
|
|
if (token === "option")
|
|
|
|
|
this._parseOption(enm);
|
|
|
|
|
else {
|
|
|
|
|
if (!Lang.NAME.test(token))
|
|
|
|
|
throw Error("illegal name: "+token);
|
|
|
|
|
this.tn.skip("=");
|
|
|
|
|
var val = {
|
|
|
|
|
"name": token,
|
|
|
|
|
"id": mkId(this.tn.next(), true)
|
|
|
|
|
};
|
|
|
|
|
token = this.tn.peek();
|
|
|
|
|
if (token === "[")
|
|
|
|
|
this._parseFieldOptions({ "options": {} });
|
|
|
|
|
this.tn.skip(";");
|
|
|
|
|
enm["values"].push(val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.tn.omit(";");
|
|
|
|
|
msg["enums"].push(enm);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-10 17:48:42 +00:00
|
|
|
|
* Parses extension / reserved ranges.
|
|
|
|
|
* @returns {!Array.<!Array.<number>>}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* @private
|
|
|
|
|
*/
|
2020-07-10 17:48:42 +00:00
|
|
|
|
ParserPrototype._parseExtensionRanges = function() {
|
|
|
|
|
var ranges = [];
|
|
|
|
|
var token,
|
|
|
|
|
range,
|
|
|
|
|
value;
|
|
|
|
|
do {
|
2020-02-07 19:37:04 +00:00
|
|
|
|
range = [];
|
2020-07-10 17:48:42 +00:00
|
|
|
|
while (true) {
|
|
|
|
|
token = this.tn.next();
|
|
|
|
|
switch (token) {
|
|
|
|
|
case "min":
|
|
|
|
|
value = ProtoBuf.ID_MIN;
|
|
|
|
|
break;
|
|
|
|
|
case "max":
|
|
|
|
|
value = ProtoBuf.ID_MAX;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
value = mkNumber(token);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
range.push(value);
|
|
|
|
|
if (range.length === 2)
|
|
|
|
|
break;
|
|
|
|
|
if (this.tn.peek() !== "to") {
|
|
|
|
|
range.push(value);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
this.tn.next();
|
|
|
|
|
}
|
|
|
|
|
ranges.push(range);
|
|
|
|
|
} while (this.tn.omit(","));
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.tn.skip(";");
|
2020-07-10 17:48:42 +00:00
|
|
|
|
return ranges;
|
2020-02-07 19:37:04 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Parses an extend block.
|
|
|
|
|
* @param {!Object} parent Parent object
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
ParserPrototype._parseExtend = function(parent) {
|
|
|
|
|
var token = this.tn.next();
|
|
|
|
|
if (!Lang.TYPEREF.test(token))
|
|
|
|
|
throw Error("illegal extend reference: "+token);
|
|
|
|
|
var ext = {
|
|
|
|
|
"ref": token,
|
|
|
|
|
"fields": []
|
|
|
|
|
};
|
|
|
|
|
this.tn.skip("{");
|
|
|
|
|
while ((token = this.tn.next()) !== '}') {
|
|
|
|
|
if (Lang.RULE.test(token))
|
|
|
|
|
this._parseMessageField(ext, token);
|
|
|
|
|
else if (Lang.TYPEREF.test(token)) {
|
|
|
|
|
if (!this.proto3)
|
|
|
|
|
throw Error("illegal field rule: "+token);
|
|
|
|
|
this._parseMessageField(ext, "optional", token);
|
|
|
|
|
} else
|
|
|
|
|
throw Error("illegal extend token: "+token);
|
|
|
|
|
}
|
|
|
|
|
this.tn.omit(";");
|
|
|
|
|
parent["messages"].push(ext);
|
|
|
|
|
return ext;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ----- General -----
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a string representation of this parser.
|
|
|
|
|
* @returns {string}
|
|
|
|
|
*/
|
|
|
|
|
ParserPrototype.toString = function() {
|
|
|
|
|
return "Parser at line "+this.tn.line;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.DotProto.Parser
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
DotProto.Parser = Parser;
|
|
|
|
|
|
|
|
|
|
return DotProto;
|
|
|
|
|
|
|
|
|
|
})(ProtoBuf, ProtoBuf.Lang);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.Reflect = (function(ProtoBuf) {
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reflection types.
|
|
|
|
|
* @exports ProtoBuf.Reflect
|
|
|
|
|
* @namespace
|
|
|
|
|
*/
|
|
|
|
|
var Reflect = {};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a Reflect base class.
|
|
|
|
|
* @exports ProtoBuf.Reflect.T
|
|
|
|
|
* @constructor
|
|
|
|
|
* @abstract
|
|
|
|
|
* @param {!ProtoBuf.Builder} builder Builder reference
|
|
|
|
|
* @param {?ProtoBuf.Reflect.T} parent Parent object
|
|
|
|
|
* @param {string} name Object name
|
|
|
|
|
*/
|
|
|
|
|
var T = function(builder, parent, name) {
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Builder reference.
|
|
|
|
|
* @type {!ProtoBuf.Builder}
|
|
|
|
|
* @expose
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.builder = builder;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Parent object.
|
|
|
|
|
* @type {?ProtoBuf.Reflect.T}
|
|
|
|
|
* @expose
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.parent = parent;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Object name in namespace.
|
|
|
|
|
* @type {string}
|
|
|
|
|
* @expose
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.name = name;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Fully qualified class name
|
|
|
|
|
* @type {string}
|
|
|
|
|
* @expose
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.className;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect.T.prototype
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
var TPrototype = T.prototype;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the fully qualified name of this object.
|
|
|
|
|
* @returns {string} Fully qualified name as of ".PATH.TO.THIS"
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
TPrototype.fqn = function() {
|
|
|
|
|
var name = this.name,
|
|
|
|
|
ptr = this;
|
|
|
|
|
do {
|
|
|
|
|
ptr = ptr.parent;
|
|
|
|
|
if (ptr == null)
|
|
|
|
|
break;
|
|
|
|
|
name = ptr.name+"."+name;
|
|
|
|
|
} while (true);
|
|
|
|
|
return name;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a string representation of this Reflect object (its fully qualified name).
|
|
|
|
|
* @param {boolean=} includeClass Set to true to include the class name. Defaults to false.
|
|
|
|
|
* @return String representation
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
TPrototype.toString = function(includeClass) {
|
|
|
|
|
return (includeClass ? this.className + " " : "") + this.fqn();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Builds this type.
|
|
|
|
|
* @throws {Error} If this type cannot be built directly
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
TPrototype.build = function() {
|
|
|
|
|
throw Error(this.toString(true)+" cannot be built directly");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect.T
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Reflect.T = T;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a new Namespace.
|
|
|
|
|
* @exports ProtoBuf.Reflect.Namespace
|
|
|
|
|
* @param {!ProtoBuf.Builder} builder Builder reference
|
|
|
|
|
* @param {?ProtoBuf.Reflect.Namespace} parent Namespace parent
|
|
|
|
|
* @param {string} name Namespace name
|
|
|
|
|
* @param {Object.<string,*>=} options Namespace options
|
|
|
|
|
* @param {string?} syntax The syntax level of this definition (e.g., proto3)
|
|
|
|
|
* @constructor
|
|
|
|
|
* @extends ProtoBuf.Reflect.T
|
|
|
|
|
*/
|
|
|
|
|
var Namespace = function(builder, parent, name, options, syntax) {
|
|
|
|
|
T.call(this, builder, parent, name);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* @override
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.className = "Namespace";
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Children inside the namespace.
|
|
|
|
|
* @type {!Array.<ProtoBuf.Reflect.T>}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.children = [];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Options.
|
|
|
|
|
* @type {!Object.<string, *>}
|
|
|
|
|
*/
|
|
|
|
|
this.options = options || {};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Syntax level (e.g., proto2 or proto3).
|
|
|
|
|
* @type {!string}
|
|
|
|
|
*/
|
|
|
|
|
this.syntax = syntax || "proto2";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect.Namespace.prototype
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
var NamespacePrototype = Namespace.prototype = Object.create(T.prototype);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns an array of the namespace's children.
|
|
|
|
|
* @param {ProtoBuf.Reflect.T=} type Filter type (returns instances of this type only). Defaults to null (all children).
|
|
|
|
|
* @return {Array.<ProtoBuf.Reflect.T>}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
NamespacePrototype.getChildren = function(type) {
|
|
|
|
|
type = type || null;
|
|
|
|
|
if (type == null)
|
|
|
|
|
return this.children.slice();
|
|
|
|
|
var children = [];
|
|
|
|
|
for (var i=0, k=this.children.length; i<k; ++i)
|
|
|
|
|
if (this.children[i] instanceof type)
|
|
|
|
|
children.push(this.children[i]);
|
|
|
|
|
return children;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds a child to the namespace.
|
|
|
|
|
* @param {ProtoBuf.Reflect.T} child Child
|
|
|
|
|
* @throws {Error} If the child cannot be added (duplicate)
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
NamespacePrototype.addChild = function(child) {
|
|
|
|
|
var other;
|
|
|
|
|
if (other = this.getChild(child.name)) {
|
|
|
|
|
// Try to revert camelcase transformation on collision
|
|
|
|
|
if (other instanceof Message.Field && other.name !== other.originalName && this.getChild(other.originalName) === null)
|
|
|
|
|
other.name = other.originalName; // Revert previous first (effectively keeps both originals)
|
|
|
|
|
else if (child instanceof Message.Field && child.name !== child.originalName && this.getChild(child.originalName) === null)
|
|
|
|
|
child.name = child.originalName;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
else
|
2020-02-07 19:37:04 +00:00
|
|
|
|
throw Error("Duplicate name in namespace "+this.toString(true)+": "+child.name);
|
|
|
|
|
}
|
|
|
|
|
this.children.push(child);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets a child by its name or id.
|
|
|
|
|
* @param {string|number} nameOrId Child name or id
|
|
|
|
|
* @return {?ProtoBuf.Reflect.T} The child or null if not found
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
NamespacePrototype.getChild = function(nameOrId) {
|
|
|
|
|
var key = typeof nameOrId === 'number' ? 'id' : 'name';
|
|
|
|
|
for (var i=0, k=this.children.length; i<k; ++i)
|
|
|
|
|
if (this.children[i][key] === nameOrId)
|
|
|
|
|
return this.children[i];
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resolves a reflect object inside of this namespace.
|
|
|
|
|
* @param {string|!Array.<string>} qn Qualified name to resolve
|
|
|
|
|
* @param {boolean=} excludeNonNamespace Excludes non-namespace types, defaults to `false`
|
|
|
|
|
* @return {?ProtoBuf.Reflect.Namespace} The resolved type or null if not found
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
NamespacePrototype.resolve = function(qn, excludeNonNamespace) {
|
|
|
|
|
var part = typeof qn === 'string' ? qn.split(".") : qn,
|
|
|
|
|
ptr = this,
|
|
|
|
|
i = 0;
|
|
|
|
|
if (part[i] === "") { // Fully qualified name, e.g. ".My.Message'
|
|
|
|
|
while (ptr.parent !== null)
|
|
|
|
|
ptr = ptr.parent;
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
var child;
|
|
|
|
|
do {
|
|
|
|
|
do {
|
|
|
|
|
if (!(ptr instanceof Reflect.Namespace)) {
|
|
|
|
|
ptr = null;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
child = ptr.getChild(part[i]);
|
|
|
|
|
if (!child || !(child instanceof Reflect.T) || (excludeNonNamespace && !(child instanceof Reflect.Namespace))) {
|
|
|
|
|
ptr = null;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
ptr = child; i++;
|
|
|
|
|
} while (i < part.length);
|
|
|
|
|
if (ptr != null)
|
|
|
|
|
break; // Found
|
|
|
|
|
// Else search the parent
|
|
|
|
|
if (this.parent !== null)
|
|
|
|
|
return this.parent.resolve(qn, excludeNonNamespace);
|
|
|
|
|
} while (ptr != null);
|
|
|
|
|
return ptr;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Determines the shortest qualified name of the specified type, if any, relative to this namespace.
|
|
|
|
|
* @param {!ProtoBuf.Reflect.T} t Reflection type
|
|
|
|
|
* @returns {string} The shortest qualified name or, if there is none, the fqn
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
NamespacePrototype.qn = function(t) {
|
|
|
|
|
var part = [], ptr = t;
|
|
|
|
|
do {
|
|
|
|
|
part.unshift(ptr.name);
|
|
|
|
|
ptr = ptr.parent;
|
|
|
|
|
} while (ptr !== null);
|
|
|
|
|
for (var len=1; len <= part.length; len++) {
|
|
|
|
|
var qn = part.slice(part.length-len);
|
|
|
|
|
if (t === this.resolve(qn, t instanceof Reflect.Namespace))
|
|
|
|
|
return qn.join(".");
|
|
|
|
|
}
|
|
|
|
|
return t.fqn();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Builds the namespace and returns the runtime counterpart.
|
|
|
|
|
* @return {Object.<string,Function|Object>} Runtime namespace
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
NamespacePrototype.build = function() {
|
|
|
|
|
/** @dict */
|
|
|
|
|
var ns = {};
|
|
|
|
|
var children = this.children;
|
|
|
|
|
for (var i=0, k=children.length, child; i<k; ++i) {
|
|
|
|
|
child = children[i];
|
|
|
|
|
if (child instanceof Namespace)
|
|
|
|
|
ns[child.name] = child.build();
|
|
|
|
|
}
|
|
|
|
|
if (Object.defineProperty)
|
|
|
|
|
Object.defineProperty(ns, "$options", { "value": this.buildOpt() });
|
|
|
|
|
return ns;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Builds the namespace's '$options' property.
|
|
|
|
|
* @return {Object.<string,*>}
|
|
|
|
|
*/
|
|
|
|
|
NamespacePrototype.buildOpt = function() {
|
|
|
|
|
var opt = {},
|
|
|
|
|
keys = Object.keys(this.options);
|
|
|
|
|
for (var i=0, k=keys.length; i<k; ++i) {
|
|
|
|
|
var key = keys[i],
|
|
|
|
|
val = this.options[keys[i]];
|
|
|
|
|
// TODO: Options are not resolved, yet.
|
|
|
|
|
// if (val instanceof Namespace) {
|
|
|
|
|
// opt[key] = val.build();
|
|
|
|
|
// } else {
|
|
|
|
|
opt[key] = val;
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
return opt;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the value assigned to the option with the specified name.
|
|
|
|
|
* @param {string=} name Returns the option value if specified, otherwise all options are returned.
|
|
|
|
|
* @return {*|Object.<string,*>}null} Option value or NULL if there is no such option
|
|
|
|
|
*/
|
|
|
|
|
NamespacePrototype.getOption = function(name) {
|
|
|
|
|
if (typeof name === 'undefined')
|
|
|
|
|
return this.options;
|
|
|
|
|
return typeof this.options[name] !== 'undefined' ? this.options[name] : null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect.Namespace
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Reflect.Namespace = Namespace;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a new Element implementation that checks and converts values for a
|
|
|
|
|
* particular field type, as appropriate.
|
|
|
|
|
*
|
|
|
|
|
* An Element represents a single value: either the value of a singular field,
|
|
|
|
|
* or a value contained in one entry of a repeated field or map field. This
|
|
|
|
|
* class does not implement these higher-level concepts; it only encapsulates
|
|
|
|
|
* the low-level typechecking and conversion.
|
|
|
|
|
*
|
|
|
|
|
* @exports ProtoBuf.Reflect.Element
|
|
|
|
|
* @param {{name: string, wireType: number}} type Resolved data type
|
|
|
|
|
* @param {ProtoBuf.Reflect.T|null} resolvedType Resolved type, if relevant
|
|
|
|
|
* (e.g. submessage field).
|
|
|
|
|
* @param {boolean} isMapKey Is this element a Map key? The value will be
|
|
|
|
|
* converted to string form if so.
|
|
|
|
|
* @param {string} syntax Syntax level of defining message type, e.g.,
|
|
|
|
|
* proto2 or proto3.
|
|
|
|
|
* @constructor
|
|
|
|
|
*/
|
|
|
|
|
var Element = function(type, resolvedType, isMapKey, syntax) {
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Element type, as a string (e.g., int32).
|
|
|
|
|
* @type {{name: string, wireType: number}}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.type = type;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Element type reference to submessage or enum definition, if needed.
|
|
|
|
|
* @type {ProtoBuf.Reflect.T|null}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.resolvedType = resolvedType;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Element is a map key.
|
|
|
|
|
* @type {boolean}
|
|
|
|
|
*/
|
|
|
|
|
this.isMapKey = isMapKey;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Syntax level of defining message type, e.g., proto2 or proto3.
|
|
|
|
|
* @type {string}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.syntax = syntax;
|
|
|
|
|
|
|
|
|
|
if (isMapKey && ProtoBuf.MAP_KEY_TYPES.indexOf(type) < 0)
|
|
|
|
|
throw Error("Invalid map key type: " + type.name);
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var ElementPrototype = Element.prototype;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Obtains a (new) default value for the specified type.
|
|
|
|
|
* @param type {string|{name: string, wireType: number}} Field type
|
|
|
|
|
* @returns {*} Default value
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
function mkDefault(type) {
|
|
|
|
|
if (typeof type === 'string')
|
|
|
|
|
type = ProtoBuf.TYPES[type];
|
|
|
|
|
if (typeof type.defaultValue === 'undefined')
|
|
|
|
|
throw Error("default value for type "+type.name+" is not supported");
|
|
|
|
|
if (type == ProtoBuf.TYPES["bytes"])
|
|
|
|
|
return new ByteBuffer(0);
|
|
|
|
|
return type.defaultValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the default value for this field in proto3.
|
|
|
|
|
* @function
|
|
|
|
|
* @param type {string|{name: string, wireType: number}} the field type
|
|
|
|
|
* @returns {*} Default value
|
|
|
|
|
*/
|
|
|
|
|
Element.defaultFieldValue = mkDefault;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Makes a Long from a value.
|
|
|
|
|
* @param {{low: number, high: number, unsigned: boolean}|string|number} value Value
|
|
|
|
|
* @param {boolean=} unsigned Whether unsigned or not, defaults to reuse it from Long-like objects or to signed for
|
|
|
|
|
* strings and numbers
|
|
|
|
|
* @returns {!Long}
|
|
|
|
|
* @throws {Error} If the value cannot be converted to a Long
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
function mkLong(value, unsigned) {
|
|
|
|
|
if (value && typeof value.low === 'number' && typeof value.high === 'number' && typeof value.unsigned === 'boolean'
|
|
|
|
|
&& value.low === value.low && value.high === value.high)
|
|
|
|
|
return new ProtoBuf.Long(value.low, value.high, typeof unsigned === 'undefined' ? value.unsigned : unsigned);
|
|
|
|
|
if (typeof value === 'string')
|
|
|
|
|
return ProtoBuf.Long.fromString(value, unsigned || false, 10);
|
|
|
|
|
if (typeof value === 'number')
|
|
|
|
|
return ProtoBuf.Long.fromNumber(value, unsigned || false);
|
|
|
|
|
throw Error("not convertible to Long");
|
|
|
|
|
}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Checks if the given value can be set for an element of this type (singular
|
|
|
|
|
* field or one element of a repeated field or map).
|
|
|
|
|
* @param {*} value Value to check
|
|
|
|
|
* @return {*} Verified, maybe adjusted, value
|
|
|
|
|
* @throws {Error} If the value cannot be verified for this element slot
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
ElementPrototype.verifyValue = function(value) {
|
2020-07-10 17:48:42 +00:00
|
|
|
|
var self = this;
|
|
|
|
|
function fail(val, msg) {
|
|
|
|
|
throw Error("Illegal value for "+self.toString(true)+" of type "+self.type.name+": "+val+" ("+msg+")");
|
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
switch (this.type) {
|
|
|
|
|
// Signed 32bit
|
|
|
|
|
case ProtoBuf.TYPES["int32"]:
|
|
|
|
|
case ProtoBuf.TYPES["sint32"]:
|
|
|
|
|
case ProtoBuf.TYPES["sfixed32"]:
|
|
|
|
|
// Account for !NaN: value === value
|
|
|
|
|
if (typeof value !== 'number' || (value === value && value % 1 !== 0))
|
|
|
|
|
fail(typeof value, "not an integer");
|
|
|
|
|
return value > 4294967295 ? value | 0 : value;
|
|
|
|
|
|
|
|
|
|
// Unsigned 32bit
|
|
|
|
|
case ProtoBuf.TYPES["uint32"]:
|
|
|
|
|
case ProtoBuf.TYPES["fixed32"]:
|
|
|
|
|
if (typeof value !== 'number' || (value === value && value % 1 !== 0))
|
|
|
|
|
fail(typeof value, "not an integer");
|
|
|
|
|
return value < 0 ? value >>> 0 : value;
|
|
|
|
|
|
|
|
|
|
// Signed 64bit
|
|
|
|
|
case ProtoBuf.TYPES["int64"]:
|
|
|
|
|
case ProtoBuf.TYPES["sint64"]:
|
|
|
|
|
case ProtoBuf.TYPES["sfixed64"]: {
|
|
|
|
|
if (ProtoBuf.Long)
|
|
|
|
|
try {
|
|
|
|
|
return mkLong(value, false);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
fail(typeof value, e.message);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
fail(typeof value, "requires Long.js");
|
|
|
|
|
}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Unsigned 64bit
|
|
|
|
|
case ProtoBuf.TYPES["uint64"]:
|
|
|
|
|
case ProtoBuf.TYPES["fixed64"]: {
|
|
|
|
|
if (ProtoBuf.Long)
|
|
|
|
|
try {
|
|
|
|
|
return mkLong(value, true);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
fail(typeof value, e.message);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
fail(typeof value, "requires Long.js");
|
|
|
|
|
}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Bool
|
|
|
|
|
case ProtoBuf.TYPES["bool"]:
|
|
|
|
|
if (typeof value !== 'boolean')
|
|
|
|
|
fail(typeof value, "not a boolean");
|
|
|
|
|
return value;
|
|
|
|
|
|
|
|
|
|
// Float
|
|
|
|
|
case ProtoBuf.TYPES["float"]:
|
|
|
|
|
case ProtoBuf.TYPES["double"]:
|
|
|
|
|
if (typeof value !== 'number')
|
|
|
|
|
fail(typeof value, "not a number");
|
|
|
|
|
return value;
|
|
|
|
|
|
|
|
|
|
// Length-delimited string
|
|
|
|
|
case ProtoBuf.TYPES["string"]:
|
|
|
|
|
if (typeof value !== 'string' && !(value && value instanceof String))
|
|
|
|
|
fail(typeof value, "not a string");
|
|
|
|
|
return ""+value; // Convert String object to string
|
|
|
|
|
|
|
|
|
|
// Length-delimited bytes
|
|
|
|
|
case ProtoBuf.TYPES["bytes"]:
|
|
|
|
|
if (ByteBuffer.isByteBuffer(value))
|
|
|
|
|
return value;
|
|
|
|
|
return ByteBuffer.wrap(value, "base64");
|
|
|
|
|
|
|
|
|
|
// Constant enum value
|
|
|
|
|
case ProtoBuf.TYPES["enum"]: {
|
|
|
|
|
var values = this.resolvedType.getChildren(ProtoBuf.Reflect.Enum.Value);
|
|
|
|
|
for (i=0; i<values.length; i++)
|
|
|
|
|
if (values[i].name == value)
|
|
|
|
|
return values[i].id;
|
|
|
|
|
else if (values[i].id == value)
|
|
|
|
|
return values[i].id;
|
|
|
|
|
|
|
|
|
|
if (this.syntax === 'proto3') {
|
|
|
|
|
// proto3: just make sure it's an integer.
|
|
|
|
|
if (typeof value !== 'number' || (value === value && value % 1 !== 0))
|
|
|
|
|
fail(typeof value, "not an integer");
|
|
|
|
|
if (value > 4294967295 || value < 0)
|
|
|
|
|
fail(typeof value, "not in range for uint32")
|
|
|
|
|
return value;
|
|
|
|
|
} else {
|
|
|
|
|
// proto2 requires enum values to be valid.
|
|
|
|
|
fail(value, "not a valid enum value");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Embedded message
|
|
|
|
|
case ProtoBuf.TYPES["group"]:
|
|
|
|
|
case ProtoBuf.TYPES["message"]: {
|
|
|
|
|
if (!value || typeof value !== 'object')
|
|
|
|
|
fail(typeof value, "object expected");
|
|
|
|
|
if (value instanceof this.resolvedType.clazz)
|
|
|
|
|
return value;
|
|
|
|
|
if (value instanceof ProtoBuf.Builder.Message) {
|
|
|
|
|
// Mismatched type: Convert to object (see: https://github.com/dcodeIO/ProtoBuf.js/issues/180)
|
|
|
|
|
var obj = {};
|
|
|
|
|
for (var i in value)
|
|
|
|
|
if (value.hasOwnProperty(i))
|
|
|
|
|
obj[i] = value[i];
|
|
|
|
|
value = obj;
|
|
|
|
|
}
|
|
|
|
|
// Else let's try to construct one from a key-value object
|
|
|
|
|
return new (this.resolvedType.clazz)(value); // May throw for a hundred of reasons
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// We should never end here
|
|
|
|
|
throw Error("[INTERNAL] Illegal value for "+this.toString(true)+": "+value+" (undefined type "+this.type+")");
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Calculates the byte length of an element on the wire.
|
|
|
|
|
* @param {number} id Field number
|
|
|
|
|
* @param {*} value Field value
|
|
|
|
|
* @returns {number} Byte length
|
|
|
|
|
* @throws {Error} If the value cannot be calculated
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ElementPrototype.calculateLength = function(id, value) {
|
|
|
|
|
if (value === null) return 0; // Nothing to encode
|
|
|
|
|
// Tag has already been written
|
|
|
|
|
var n;
|
|
|
|
|
switch (this.type) {
|
|
|
|
|
case ProtoBuf.TYPES["int32"]:
|
|
|
|
|
return value < 0 ? ByteBuffer.calculateVarint64(value) : ByteBuffer.calculateVarint32(value);
|
|
|
|
|
case ProtoBuf.TYPES["uint32"]:
|
|
|
|
|
return ByteBuffer.calculateVarint32(value);
|
|
|
|
|
case ProtoBuf.TYPES["sint32"]:
|
|
|
|
|
return ByteBuffer.calculateVarint32(ByteBuffer.zigZagEncode32(value));
|
|
|
|
|
case ProtoBuf.TYPES["fixed32"]:
|
|
|
|
|
case ProtoBuf.TYPES["sfixed32"]:
|
|
|
|
|
case ProtoBuf.TYPES["float"]:
|
|
|
|
|
return 4;
|
|
|
|
|
case ProtoBuf.TYPES["int64"]:
|
|
|
|
|
case ProtoBuf.TYPES["uint64"]:
|
|
|
|
|
return ByteBuffer.calculateVarint64(value);
|
|
|
|
|
case ProtoBuf.TYPES["sint64"]:
|
|
|
|
|
return ByteBuffer.calculateVarint64(ByteBuffer.zigZagEncode64(value));
|
|
|
|
|
case ProtoBuf.TYPES["fixed64"]:
|
|
|
|
|
case ProtoBuf.TYPES["sfixed64"]:
|
|
|
|
|
return 8;
|
|
|
|
|
case ProtoBuf.TYPES["bool"]:
|
|
|
|
|
return 1;
|
|
|
|
|
case ProtoBuf.TYPES["enum"]:
|
|
|
|
|
return ByteBuffer.calculateVarint32(value);
|
|
|
|
|
case ProtoBuf.TYPES["double"]:
|
|
|
|
|
return 8;
|
|
|
|
|
case ProtoBuf.TYPES["string"]:
|
|
|
|
|
n = ByteBuffer.calculateUTF8Bytes(value);
|
|
|
|
|
return ByteBuffer.calculateVarint32(n) + n;
|
|
|
|
|
case ProtoBuf.TYPES["bytes"]:
|
|
|
|
|
if (value.remaining() < 0)
|
|
|
|
|
throw Error("Illegal value for "+this.toString(true)+": "+value.remaining()+" bytes remaining");
|
|
|
|
|
return ByteBuffer.calculateVarint32(value.remaining()) + value.remaining();
|
|
|
|
|
case ProtoBuf.TYPES["message"]:
|
|
|
|
|
n = this.resolvedType.calculate(value);
|
|
|
|
|
return ByteBuffer.calculateVarint32(n) + n;
|
|
|
|
|
case ProtoBuf.TYPES["group"]:
|
|
|
|
|
n = this.resolvedType.calculate(value);
|
|
|
|
|
return n + ByteBuffer.calculateVarint32((id << 3) | ProtoBuf.WIRE_TYPES.ENDGROUP);
|
|
|
|
|
}
|
|
|
|
|
// We should never end here
|
|
|
|
|
throw Error("[INTERNAL] Illegal value to encode in "+this.toString(true)+": "+value+" (unknown type)");
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Encodes a value to the specified buffer. Does not encode the key.
|
|
|
|
|
* @param {number} id Field number
|
|
|
|
|
* @param {*} value Field value
|
|
|
|
|
* @param {ByteBuffer} buffer ByteBuffer to encode to
|
|
|
|
|
* @return {ByteBuffer} The ByteBuffer for chaining
|
|
|
|
|
* @throws {Error} If the value cannot be encoded
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ElementPrototype.encodeValue = function(id, value, buffer) {
|
|
|
|
|
if (value === null) return buffer; // Nothing to encode
|
|
|
|
|
// Tag has already been written
|
|
|
|
|
|
|
|
|
|
switch (this.type) {
|
|
|
|
|
// 32bit signed varint
|
|
|
|
|
case ProtoBuf.TYPES["int32"]:
|
|
|
|
|
// "If you use int32 or int64 as the type for a negative number, the resulting varint is always ten bytes
|
|
|
|
|
// long – it is, effectively, treated like a very large unsigned integer." (see #122)
|
|
|
|
|
if (value < 0)
|
|
|
|
|
buffer.writeVarint64(value);
|
|
|
|
|
else
|
|
|
|
|
buffer.writeVarint32(value);
|
|
|
|
|
break;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// 32bit unsigned varint
|
|
|
|
|
case ProtoBuf.TYPES["uint32"]:
|
|
|
|
|
buffer.writeVarint32(value);
|
|
|
|
|
break;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// 32bit varint zig-zag
|
|
|
|
|
case ProtoBuf.TYPES["sint32"]:
|
|
|
|
|
buffer.writeVarint32ZigZag(value);
|
|
|
|
|
break;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Fixed unsigned 32bit
|
|
|
|
|
case ProtoBuf.TYPES["fixed32"]:
|
|
|
|
|
buffer.writeUint32(value);
|
|
|
|
|
break;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Fixed signed 32bit
|
|
|
|
|
case ProtoBuf.TYPES["sfixed32"]:
|
|
|
|
|
buffer.writeInt32(value);
|
|
|
|
|
break;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// 64bit varint as-is
|
|
|
|
|
case ProtoBuf.TYPES["int64"]:
|
|
|
|
|
case ProtoBuf.TYPES["uint64"]:
|
|
|
|
|
buffer.writeVarint64(value); // throws
|
|
|
|
|
break;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// 64bit varint zig-zag
|
|
|
|
|
case ProtoBuf.TYPES["sint64"]:
|
|
|
|
|
buffer.writeVarint64ZigZag(value); // throws
|
|
|
|
|
break;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Fixed unsigned 64bit
|
|
|
|
|
case ProtoBuf.TYPES["fixed64"]:
|
|
|
|
|
buffer.writeUint64(value); // throws
|
|
|
|
|
break;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Fixed signed 64bit
|
|
|
|
|
case ProtoBuf.TYPES["sfixed64"]:
|
|
|
|
|
buffer.writeInt64(value); // throws
|
|
|
|
|
break;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Bool
|
|
|
|
|
case ProtoBuf.TYPES["bool"]:
|
|
|
|
|
if (typeof value === 'string')
|
|
|
|
|
buffer.writeVarint32(value.toLowerCase() === 'false' ? 0 : !!value);
|
|
|
|
|
else
|
|
|
|
|
buffer.writeVarint32(value ? 1 : 0);
|
|
|
|
|
break;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Constant enum value
|
|
|
|
|
case ProtoBuf.TYPES["enum"]:
|
|
|
|
|
buffer.writeVarint32(value);
|
|
|
|
|
break;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// 32bit float
|
|
|
|
|
case ProtoBuf.TYPES["float"]:
|
|
|
|
|
buffer.writeFloat32(value);
|
|
|
|
|
break;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// 64bit float
|
|
|
|
|
case ProtoBuf.TYPES["double"]:
|
|
|
|
|
buffer.writeFloat64(value);
|
|
|
|
|
break;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Length-delimited string
|
|
|
|
|
case ProtoBuf.TYPES["string"]:
|
|
|
|
|
buffer.writeVString(value);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Length-delimited bytes
|
|
|
|
|
case ProtoBuf.TYPES["bytes"]:
|
|
|
|
|
if (value.remaining() < 0)
|
|
|
|
|
throw Error("Illegal value for "+this.toString(true)+": "+value.remaining()+" bytes remaining");
|
|
|
|
|
var prevOffset = value.offset;
|
|
|
|
|
buffer.writeVarint32(value.remaining());
|
|
|
|
|
buffer.append(value);
|
|
|
|
|
value.offset = prevOffset;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Embedded message
|
|
|
|
|
case ProtoBuf.TYPES["message"]:
|
|
|
|
|
var bb = new ByteBuffer().LE();
|
|
|
|
|
this.resolvedType.encode(value, bb);
|
|
|
|
|
buffer.writeVarint32(bb.offset);
|
|
|
|
|
buffer.append(bb.flip());
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Legacy group
|
|
|
|
|
case ProtoBuf.TYPES["group"]:
|
|
|
|
|
this.resolvedType.encode(value, buffer);
|
|
|
|
|
buffer.writeVarint32((id << 3) | ProtoBuf.WIRE_TYPES.ENDGROUP);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// We should never end here
|
|
|
|
|
throw Error("[INTERNAL] Illegal value to encode in "+this.toString(true)+": "+value+" (unknown type)");
|
|
|
|
|
}
|
|
|
|
|
return buffer;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Decode one element value from the specified buffer.
|
|
|
|
|
* @param {ByteBuffer} buffer ByteBuffer to decode from
|
|
|
|
|
* @param {number} wireType The field wire type
|
|
|
|
|
* @param {number} id The field number
|
|
|
|
|
* @return {*} Decoded value
|
|
|
|
|
* @throws {Error} If the field cannot be decoded
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ElementPrototype.decode = function(buffer, wireType, id) {
|
|
|
|
|
if (wireType != this.type.wireType)
|
|
|
|
|
throw Error("Unexpected wire type for element");
|
|
|
|
|
|
|
|
|
|
var value, nBytes;
|
|
|
|
|
switch (this.type) {
|
|
|
|
|
// 32bit signed varint
|
|
|
|
|
case ProtoBuf.TYPES["int32"]:
|
|
|
|
|
return buffer.readVarint32() | 0;
|
|
|
|
|
|
|
|
|
|
// 32bit unsigned varint
|
|
|
|
|
case ProtoBuf.TYPES["uint32"]:
|
|
|
|
|
return buffer.readVarint32() >>> 0;
|
|
|
|
|
|
|
|
|
|
// 32bit signed varint zig-zag
|
|
|
|
|
case ProtoBuf.TYPES["sint32"]:
|
|
|
|
|
return buffer.readVarint32ZigZag() | 0;
|
|
|
|
|
|
|
|
|
|
// Fixed 32bit unsigned
|
|
|
|
|
case ProtoBuf.TYPES["fixed32"]:
|
|
|
|
|
return buffer.readUint32() >>> 0;
|
|
|
|
|
|
|
|
|
|
case ProtoBuf.TYPES["sfixed32"]:
|
|
|
|
|
return buffer.readInt32() | 0;
|
|
|
|
|
|
|
|
|
|
// 64bit signed varint
|
|
|
|
|
case ProtoBuf.TYPES["int64"]:
|
|
|
|
|
return buffer.readVarint64();
|
|
|
|
|
|
|
|
|
|
// 64bit unsigned varint
|
|
|
|
|
case ProtoBuf.TYPES["uint64"]:
|
|
|
|
|
return buffer.readVarint64().toUnsigned();
|
|
|
|
|
|
|
|
|
|
// 64bit signed varint zig-zag
|
|
|
|
|
case ProtoBuf.TYPES["sint64"]:
|
|
|
|
|
return buffer.readVarint64ZigZag();
|
|
|
|
|
|
|
|
|
|
// Fixed 64bit unsigned
|
|
|
|
|
case ProtoBuf.TYPES["fixed64"]:
|
|
|
|
|
return buffer.readUint64();
|
|
|
|
|
|
|
|
|
|
// Fixed 64bit signed
|
|
|
|
|
case ProtoBuf.TYPES["sfixed64"]:
|
|
|
|
|
return buffer.readInt64();
|
|
|
|
|
|
|
|
|
|
// Bool varint
|
|
|
|
|
case ProtoBuf.TYPES["bool"]:
|
|
|
|
|
return !!buffer.readVarint32();
|
|
|
|
|
|
|
|
|
|
// Constant enum value (varint)
|
|
|
|
|
case ProtoBuf.TYPES["enum"]:
|
|
|
|
|
// The following Builder.Message#set will already throw
|
|
|
|
|
return buffer.readVarint32();
|
|
|
|
|
|
|
|
|
|
// 32bit float
|
|
|
|
|
case ProtoBuf.TYPES["float"]:
|
|
|
|
|
return buffer.readFloat();
|
|
|
|
|
|
|
|
|
|
// 64bit float
|
|
|
|
|
case ProtoBuf.TYPES["double"]:
|
|
|
|
|
return buffer.readDouble();
|
|
|
|
|
|
|
|
|
|
// Length-delimited string
|
|
|
|
|
case ProtoBuf.TYPES["string"]:
|
|
|
|
|
return buffer.readVString();
|
|
|
|
|
|
|
|
|
|
// Length-delimited bytes
|
|
|
|
|
case ProtoBuf.TYPES["bytes"]: {
|
|
|
|
|
nBytes = buffer.readVarint32();
|
|
|
|
|
if (buffer.remaining() < nBytes)
|
|
|
|
|
throw Error("Illegal number of bytes for "+this.toString(true)+": "+nBytes+" required but got only "+buffer.remaining());
|
|
|
|
|
value = buffer.clone(); // Offset already set
|
|
|
|
|
value.limit = value.offset+nBytes;
|
|
|
|
|
buffer.offset += nBytes;
|
|
|
|
|
return value;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
|
|
|
|
|
// Length-delimited embedded message
|
|
|
|
|
case ProtoBuf.TYPES["message"]: {
|
|
|
|
|
nBytes = buffer.readVarint32();
|
|
|
|
|
return this.resolvedType.decode(buffer, nBytes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Legacy group
|
|
|
|
|
case ProtoBuf.TYPES["group"]:
|
|
|
|
|
return this.resolvedType.decode(buffer, -1, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We should never end here
|
|
|
|
|
throw Error("[INTERNAL] Illegal decode type");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Converts a value from a string to the canonical element type.
|
|
|
|
|
*
|
|
|
|
|
* Legal only when isMapKey is true.
|
|
|
|
|
*
|
|
|
|
|
* @param {string} str The string value
|
|
|
|
|
* @returns {*} The value
|
|
|
|
|
*/
|
|
|
|
|
ElementPrototype.valueFromString = function(str) {
|
|
|
|
|
if (!this.isMapKey) {
|
|
|
|
|
throw Error("valueFromString() called on non-map-key element");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (this.type) {
|
|
|
|
|
case ProtoBuf.TYPES["int32"]:
|
|
|
|
|
case ProtoBuf.TYPES["sint32"]:
|
|
|
|
|
case ProtoBuf.TYPES["sfixed32"]:
|
|
|
|
|
case ProtoBuf.TYPES["uint32"]:
|
|
|
|
|
case ProtoBuf.TYPES["fixed32"]:
|
|
|
|
|
return this.verifyValue(parseInt(str));
|
|
|
|
|
|
|
|
|
|
case ProtoBuf.TYPES["int64"]:
|
|
|
|
|
case ProtoBuf.TYPES["sint64"]:
|
|
|
|
|
case ProtoBuf.TYPES["sfixed64"]:
|
|
|
|
|
case ProtoBuf.TYPES["uint64"]:
|
|
|
|
|
case ProtoBuf.TYPES["fixed64"]:
|
|
|
|
|
// Long-based fields support conversions from string already.
|
|
|
|
|
return this.verifyValue(str);
|
|
|
|
|
|
|
|
|
|
case ProtoBuf.TYPES["bool"]:
|
|
|
|
|
return str === "true";
|
|
|
|
|
|
|
|
|
|
case ProtoBuf.TYPES["string"]:
|
|
|
|
|
return this.verifyValue(str);
|
|
|
|
|
|
|
|
|
|
case ProtoBuf.TYPES["bytes"]:
|
|
|
|
|
return ByteBuffer.fromBinary(str);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Converts a value from the canonical element type to a string.
|
|
|
|
|
*
|
|
|
|
|
* It should be the case that `valueFromString(valueToString(val))` returns
|
|
|
|
|
* a value equivalent to `verifyValue(val)` for every legal value of `val`
|
|
|
|
|
* according to this element type.
|
|
|
|
|
*
|
|
|
|
|
* This may be used when the element must be stored or used as a string,
|
|
|
|
|
* e.g., as a map key on an Object.
|
|
|
|
|
*
|
|
|
|
|
* Legal only when isMapKey is true.
|
|
|
|
|
*
|
|
|
|
|
* @param {*} val The value
|
|
|
|
|
* @returns {string} The string form of the value.
|
|
|
|
|
*/
|
|
|
|
|
ElementPrototype.valueToString = function(value) {
|
|
|
|
|
if (!this.isMapKey) {
|
|
|
|
|
throw Error("valueToString() called on non-map-key element");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.type === ProtoBuf.TYPES["bytes"]) {
|
|
|
|
|
return value.toString("binary");
|
|
|
|
|
} else {
|
|
|
|
|
return value.toString();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect.Element
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Reflect.Element = Element;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a new Message.
|
|
|
|
|
* @exports ProtoBuf.Reflect.Message
|
|
|
|
|
* @param {!ProtoBuf.Builder} builder Builder reference
|
|
|
|
|
* @param {!ProtoBuf.Reflect.Namespace} parent Parent message or namespace
|
|
|
|
|
* @param {string} name Message name
|
|
|
|
|
* @param {Object.<string,*>=} options Message options
|
|
|
|
|
* @param {boolean=} isGroup `true` if this is a legacy group
|
|
|
|
|
* @param {string?} syntax The syntax level of this definition (e.g., proto3)
|
|
|
|
|
* @constructor
|
|
|
|
|
* @extends ProtoBuf.Reflect.Namespace
|
|
|
|
|
*/
|
|
|
|
|
var Message = function(builder, parent, name, options, isGroup, syntax) {
|
|
|
|
|
Namespace.call(this, builder, parent, name, options, syntax);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* @override
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.className = "Message";
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Extensions range.
|
2020-07-10 17:48:42 +00:00
|
|
|
|
* @type {!Array.<number>|undefined}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-07-10 17:48:42 +00:00
|
|
|
|
this.extensions = undefined;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Runtime message class.
|
|
|
|
|
* @type {?function(new:ProtoBuf.Builder.Message)}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.clazz = null;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Whether this is a legacy group or not.
|
|
|
|
|
* @type {boolean}
|
|
|
|
|
* @expose
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.isGroup = !!isGroup;
|
|
|
|
|
|
|
|
|
|
// The following cached collections are used to efficiently iterate over or look up fields when decoding.
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Cached fields.
|
|
|
|
|
* @type {?Array.<!ProtoBuf.Reflect.Message.Field>}
|
|
|
|
|
* @private
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this._fields = null;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Cached fields by id.
|
|
|
|
|
* @type {?Object.<number,!ProtoBuf.Reflect.Message.Field>}
|
|
|
|
|
* @private
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this._fieldsById = null;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Cached fields by name.
|
|
|
|
|
* @type {?Object.<string,!ProtoBuf.Reflect.Message.Field>}
|
|
|
|
|
* @private
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this._fieldsByName = null;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect.Message.prototype
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
var MessagePrototype = Message.prototype = Object.create(Namespace.prototype);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Builds the message and returns the runtime counterpart, which is a fully functional class.
|
|
|
|
|
* @see ProtoBuf.Builder.Message
|
|
|
|
|
* @param {boolean=} rebuild Whether to rebuild or not, defaults to false
|
|
|
|
|
* @return {ProtoBuf.Reflect.Message} Message class
|
|
|
|
|
* @throws {Error} If the message cannot be built
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.build = function(rebuild) {
|
|
|
|
|
if (this.clazz && !rebuild)
|
|
|
|
|
return this.clazz;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Create the runtime Message class in its own scope
|
|
|
|
|
var clazz = (function(ProtoBuf, T) {
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var fields = T.getChildren(ProtoBuf.Reflect.Message.Field),
|
|
|
|
|
oneofs = T.getChildren(ProtoBuf.Reflect.Message.OneOf);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Constructs a new runtime Message.
|
|
|
|
|
* @name ProtoBuf.Builder.Message
|
|
|
|
|
* @class Barebone of all runtime messages.
|
|
|
|
|
* @param {!Object.<string,*>|string} values Preset values
|
|
|
|
|
* @param {...string} var_args
|
|
|
|
|
* @constructor
|
|
|
|
|
* @throws {Error} If the message cannot be created
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var Message = function(values, var_args) {
|
|
|
|
|
ProtoBuf.Builder.Message.call(this);
|
|
|
|
|
|
|
|
|
|
// Create virtual oneof properties
|
|
|
|
|
for (var i=0, k=oneofs.length; i<k; ++i)
|
|
|
|
|
this[oneofs[i].name] = null;
|
|
|
|
|
// Create fields and set default values
|
|
|
|
|
for (i=0, k=fields.length; i<k; ++i) {
|
|
|
|
|
var field = fields[i];
|
|
|
|
|
this[field.name] =
|
|
|
|
|
field.repeated ? [] :
|
|
|
|
|
(field.map ? new ProtoBuf.Map(field) : null);
|
|
|
|
|
if ((field.required || T.syntax === 'proto3') &&
|
|
|
|
|
field.defaultValue !== null)
|
|
|
|
|
this[field.name] = field.defaultValue;
|
|
|
|
|
}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (arguments.length > 0) {
|
|
|
|
|
var value;
|
|
|
|
|
// Set field values from a values object
|
|
|
|
|
if (arguments.length === 1 && values !== null && typeof values === 'object' &&
|
|
|
|
|
/* not _another_ Message */ (typeof values.encode !== 'function' || values instanceof Message) &&
|
|
|
|
|
/* not a repeated field */ !Array.isArray(values) &&
|
|
|
|
|
/* not a Map */ !(values instanceof ProtoBuf.Map) &&
|
|
|
|
|
/* not a ByteBuffer */ !ByteBuffer.isByteBuffer(values) &&
|
|
|
|
|
/* not an ArrayBuffer */ !(values instanceof ArrayBuffer) &&
|
|
|
|
|
/* not a Long */ !(ProtoBuf.Long && values instanceof ProtoBuf.Long)) {
|
|
|
|
|
this.$set(values);
|
|
|
|
|
} else // Set field values from arguments, in declaration order
|
|
|
|
|
for (i=0, k=arguments.length; i<k; ++i)
|
|
|
|
|
if (typeof (value = arguments[i]) !== 'undefined')
|
|
|
|
|
this.$set(fields[i].name, value); // May throw
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* @alias ProtoBuf.Builder.Message.prototype
|
|
|
|
|
* @inner
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var MessagePrototype = Message.prototype = Object.create(ProtoBuf.Builder.Message.prototype);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-07-10 17:48:42 +00:00
|
|
|
|
|
|
|
|
|
Object.defineProperty(MessagePrototype, '__unknownFields', {
|
|
|
|
|
configurable: true,
|
|
|
|
|
enumerable: false,
|
|
|
|
|
value: null,
|
|
|
|
|
writable: true,
|
|
|
|
|
});
|
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Adds a value to a repeated field.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#add
|
|
|
|
|
* @function
|
|
|
|
|
* @param {string} key Field name
|
|
|
|
|
* @param {*} value Value to add
|
|
|
|
|
* @param {boolean=} noAssert Whether to assert the value or not (asserts by default)
|
|
|
|
|
* @returns {!ProtoBuf.Builder.Message} this
|
|
|
|
|
* @throws {Error} If the value cannot be added
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.add = function(key, value, noAssert) {
|
|
|
|
|
var field = T._fieldsByName[key];
|
|
|
|
|
if (!noAssert) {
|
|
|
|
|
if (!field)
|
|
|
|
|
throw Error(this+"#"+key+" is undefined");
|
|
|
|
|
if (!(field instanceof ProtoBuf.Reflect.Message.Field))
|
|
|
|
|
throw Error(this+"#"+key+" is not a field: "+field.toString(true)); // May throw if it's an enum or embedded message
|
|
|
|
|
if (!field.repeated)
|
|
|
|
|
throw Error(this+"#"+key+" is not a repeated field");
|
|
|
|
|
value = field.verifyValue(value, true);
|
|
|
|
|
}
|
|
|
|
|
if (this[key] === null)
|
|
|
|
|
this[key] = [];
|
|
|
|
|
this[key].push(value);
|
|
|
|
|
return this;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Adds a value to a repeated field. This is an alias for {@link ProtoBuf.Builder.Message#add}.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#$add
|
|
|
|
|
* @function
|
|
|
|
|
* @param {string} key Field name
|
|
|
|
|
* @param {*} value Value to add
|
|
|
|
|
* @param {boolean=} noAssert Whether to assert the value or not (asserts by default)
|
|
|
|
|
* @returns {!ProtoBuf.Builder.Message} this
|
|
|
|
|
* @throws {Error} If the value cannot be added
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.$add = MessagePrototype.add;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Sets a field's value.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#set
|
|
|
|
|
* @function
|
|
|
|
|
* @param {string|!Object.<string,*>} keyOrObj String key or plain object holding multiple values
|
|
|
|
|
* @param {(*|boolean)=} value Value to set if key is a string, otherwise omitted
|
|
|
|
|
* @param {boolean=} noAssert Whether to not assert for an actual field / proper value type, defaults to `false`
|
|
|
|
|
* @returns {!ProtoBuf.Builder.Message} this
|
|
|
|
|
* @throws {Error} If the value cannot be set
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.set = function(keyOrObj, value, noAssert) {
|
|
|
|
|
if (keyOrObj && typeof keyOrObj === 'object') {
|
|
|
|
|
noAssert = value;
|
|
|
|
|
for (var ikey in keyOrObj)
|
|
|
|
|
if (keyOrObj.hasOwnProperty(ikey) && typeof (value = keyOrObj[ikey]) !== 'undefined')
|
|
|
|
|
this.$set(ikey, value, noAssert);
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
var field = T._fieldsByName[keyOrObj];
|
|
|
|
|
if (!noAssert) {
|
|
|
|
|
if (!field)
|
|
|
|
|
throw Error(this+"#"+keyOrObj+" is not a field: undefined");
|
2014-10-29 23:04:51 +00:00
|
|
|
|
if (!(field instanceof ProtoBuf.Reflect.Message.Field))
|
2020-02-07 19:37:04 +00:00
|
|
|
|
throw Error(this+"#"+keyOrObj+" is not a field: "+field.toString(true));
|
|
|
|
|
this[field.name] = (value = field.verifyValue(value)); // May throw
|
|
|
|
|
} else
|
|
|
|
|
this[keyOrObj] = value;
|
|
|
|
|
if (field && field.oneof) { // Field is part of an OneOf (not a virtual OneOf field)
|
|
|
|
|
var currentField = this[field.oneof.name]; // Virtual field references currently set field
|
|
|
|
|
if (value !== null) {
|
|
|
|
|
if (currentField !== null && currentField !== field.name)
|
|
|
|
|
this[currentField] = null; // Clear currently set field
|
|
|
|
|
this[field.oneof.name] = field.name; // Point virtual field at this field
|
|
|
|
|
} else if (/* value === null && */currentField === keyOrObj)
|
|
|
|
|
this[field.oneof.name] = null; // Clear virtual field (current field explicitly cleared)
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Sets a field's value. This is an alias for [@link ProtoBuf.Builder.Message#set}.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#$set
|
|
|
|
|
* @function
|
|
|
|
|
* @param {string|!Object.<string,*>} keyOrObj String key or plain object holding multiple values
|
|
|
|
|
* @param {(*|boolean)=} value Value to set if key is a string, otherwise omitted
|
|
|
|
|
* @param {boolean=} noAssert Whether to not assert the value, defaults to `false`
|
|
|
|
|
* @throws {Error} If the value cannot be set
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.$set = MessagePrototype.set;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Gets a field's value.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#get
|
|
|
|
|
* @function
|
|
|
|
|
* @param {string} key Key
|
|
|
|
|
* @param {boolean=} noAssert Whether to not assert for an actual field, defaults to `false`
|
|
|
|
|
* @return {*} Value
|
|
|
|
|
* @throws {Error} If there is no such field
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.get = function(key, noAssert) {
|
|
|
|
|
if (noAssert)
|
|
|
|
|
return this[key];
|
|
|
|
|
var field = T._fieldsByName[key];
|
|
|
|
|
if (!field || !(field instanceof ProtoBuf.Reflect.Message.Field))
|
|
|
|
|
throw Error(this+"#"+key+" is not a field: undefined");
|
|
|
|
|
if (!(field instanceof ProtoBuf.Reflect.Message.Field))
|
|
|
|
|
throw Error(this+"#"+key+" is not a field: "+field.toString(true));
|
|
|
|
|
return this[field.name];
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Gets a field's value. This is an alias for {@link ProtoBuf.Builder.Message#$get}.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#$get
|
|
|
|
|
* @function
|
|
|
|
|
* @param {string} key Key
|
|
|
|
|
* @return {*} Value
|
|
|
|
|
* @throws {Error} If there is no such field
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.$get = MessagePrototype.get;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Getters and setters
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
for (var i=0; i<fields.length; i++) {
|
|
|
|
|
var field = fields[i];
|
|
|
|
|
// no setters for extension fields as these are named by their fqn
|
|
|
|
|
if (field instanceof ProtoBuf.Reflect.Message.ExtensionField)
|
|
|
|
|
continue;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (T.builder.options['populateAccessors'])
|
|
|
|
|
(function(field) {
|
|
|
|
|
// set/get[SomeValue]
|
|
|
|
|
var Name = field.originalName.replace(/(_[a-zA-Z])/g, function(match) {
|
|
|
|
|
return match.toUpperCase().replace('_','');
|
|
|
|
|
});
|
|
|
|
|
Name = Name.substring(0,1).toUpperCase() + Name.substring(1);
|
|
|
|
|
|
|
|
|
|
// set/get_[some_value] FIXME: Do we really need these?
|
|
|
|
|
var name = field.originalName.replace(/([A-Z])/g, function(match) {
|
|
|
|
|
return "_"+match;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The current field's unbound setter function.
|
|
|
|
|
* @function
|
|
|
|
|
* @param {*} value
|
|
|
|
|
* @param {boolean=} noAssert
|
|
|
|
|
* @returns {!ProtoBuf.Builder.Message}
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
var setter = function(value, noAssert) {
|
|
|
|
|
this[field.name] = noAssert ? value : field.verifyValue(value);
|
|
|
|
|
return this;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* The current field's unbound getter function.
|
|
|
|
|
* @function
|
|
|
|
|
* @returns {*}
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
var getter = function() {
|
|
|
|
|
return this[field.name];
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (T.getChild("set"+Name) === null)
|
2014-10-29 23:04:51 +00:00
|
|
|
|
/**
|
|
|
|
|
* Sets a value. This method is present for each field, but only if there is no name conflict with
|
|
|
|
|
* another field.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#set[SomeField]
|
|
|
|
|
* @function
|
|
|
|
|
* @param {*} value Value to set
|
|
|
|
|
* @param {boolean=} noAssert Whether to not assert the value, defaults to `false`
|
|
|
|
|
* @returns {!ProtoBuf.Builder.Message} this
|
|
|
|
|
* @abstract
|
|
|
|
|
* @throws {Error} If the value cannot be set
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
MessagePrototype["set"+Name] = setter;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (T.getChild("set_"+name) === null)
|
2014-10-29 23:04:51 +00:00
|
|
|
|
/**
|
|
|
|
|
* Sets a value. This method is present for each field, but only if there is no name conflict with
|
|
|
|
|
* another field.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#set_[some_field]
|
|
|
|
|
* @function
|
|
|
|
|
* @param {*} value Value to set
|
|
|
|
|
* @param {boolean=} noAssert Whether to not assert the value, defaults to `false`
|
|
|
|
|
* @returns {!ProtoBuf.Builder.Message} this
|
|
|
|
|
* @abstract
|
|
|
|
|
* @throws {Error} If the value cannot be set
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
MessagePrototype["set_"+name] = setter;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (T.getChild("get"+Name) === null)
|
2014-10-29 23:04:51 +00:00
|
|
|
|
/**
|
|
|
|
|
* Gets a value. This method is present for each field, but only if there is no name conflict with
|
|
|
|
|
* another field.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#get[SomeField]
|
|
|
|
|
* @function
|
|
|
|
|
* @abstract
|
|
|
|
|
* @return {*} The value
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
MessagePrototype["get"+Name] = getter;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (T.getChild("get_"+name) === null)
|
2014-10-29 23:04:51 +00:00
|
|
|
|
/**
|
|
|
|
|
* Gets a value. This method is present for each field, but only if there is no name conflict with
|
|
|
|
|
* another field.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#get_[some_field]
|
|
|
|
|
* @function
|
|
|
|
|
* @return {*} The value
|
|
|
|
|
* @abstract
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
MessagePrototype["get_"+name] = getter;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
})(field);
|
|
|
|
|
}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// En-/decoding
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Encodes the message.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#$encode
|
|
|
|
|
* @function
|
|
|
|
|
* @param {(!ByteBuffer|boolean)=} buffer ByteBuffer to encode to. Will create a new one and flip it if omitted.
|
|
|
|
|
* @param {boolean=} noVerify Whether to not verify field values, defaults to `false`
|
|
|
|
|
* @return {!ByteBuffer} Encoded message as a ByteBuffer
|
|
|
|
|
* @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
|
|
|
|
|
* returns the encoded ByteBuffer in the `encoded` property on the error.
|
|
|
|
|
* @expose
|
|
|
|
|
* @see ProtoBuf.Builder.Message#encode64
|
|
|
|
|
* @see ProtoBuf.Builder.Message#encodeHex
|
|
|
|
|
* @see ProtoBuf.Builder.Message#encodeAB
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.encode = function(buffer, noVerify) {
|
|
|
|
|
if (typeof buffer === 'boolean')
|
|
|
|
|
noVerify = buffer,
|
|
|
|
|
buffer = undefined;
|
|
|
|
|
var isNew = false;
|
|
|
|
|
if (!buffer)
|
|
|
|
|
buffer = new ByteBuffer(),
|
|
|
|
|
isNew = true;
|
|
|
|
|
var le = buffer.littleEndian;
|
|
|
|
|
try {
|
|
|
|
|
T.encode(this, buffer.LE(), noVerify);
|
|
|
|
|
return (isNew ? buffer.flip() : buffer).LE(le);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
buffer.LE(le);
|
|
|
|
|
throw(e);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Encodes a message using the specified data payload.
|
|
|
|
|
* @param {!Object.<string,*>} data Data payload
|
|
|
|
|
* @param {(!ByteBuffer|boolean)=} buffer ByteBuffer to encode to. Will create a new one and flip it if omitted.
|
|
|
|
|
* @param {boolean=} noVerify Whether to not verify field values, defaults to `false`
|
|
|
|
|
* @return {!ByteBuffer} Encoded message as a ByteBuffer
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Message.encode = function(data, buffer, noVerify) {
|
|
|
|
|
return new Message(data).encode(buffer, noVerify);
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Calculates the byte length of the message.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#calculate
|
|
|
|
|
* @function
|
|
|
|
|
* @returns {number} Byte length
|
|
|
|
|
* @throws {Error} If the message cannot be calculated or if required fields are missing.
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.calculate = function() {
|
|
|
|
|
return T.calculate(this);
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Encodes the varint32 length-delimited message.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#encodeDelimited
|
|
|
|
|
* @function
|
|
|
|
|
* @param {(!ByteBuffer|boolean)=} buffer ByteBuffer to encode to. Will create a new one and flip it if omitted.
|
2020-07-10 17:48:42 +00:00
|
|
|
|
* @param {boolean=} noVerify Whether to not verify field values, defaults to `false`
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* @return {!ByteBuffer} Encoded message as a ByteBuffer
|
|
|
|
|
* @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
|
|
|
|
|
* returns the encoded ByteBuffer in the `encoded` property on the error.
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-07-10 17:48:42 +00:00
|
|
|
|
MessagePrototype.encodeDelimited = function(buffer, noVerify) {
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var isNew = false;
|
|
|
|
|
if (!buffer)
|
|
|
|
|
buffer = new ByteBuffer(),
|
|
|
|
|
isNew = true;
|
|
|
|
|
var enc = new ByteBuffer().LE();
|
2020-07-10 17:48:42 +00:00
|
|
|
|
T.encode(this, enc, noVerify).flip();
|
2020-02-07 19:37:04 +00:00
|
|
|
|
buffer.writeVarint32(enc.remaining());
|
|
|
|
|
buffer.append(enc);
|
|
|
|
|
return isNew ? buffer.flip() : buffer;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Directly encodes the message to an ArrayBuffer.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#encodeAB
|
|
|
|
|
* @function
|
|
|
|
|
* @return {ArrayBuffer} Encoded message as ArrayBuffer
|
|
|
|
|
* @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
|
|
|
|
|
* returns the encoded ArrayBuffer in the `encoded` property on the error.
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.encodeAB = function() {
|
|
|
|
|
try {
|
|
|
|
|
return this.encode().toArrayBuffer();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (e["encoded"]) e["encoded"] = e["encoded"].toArrayBuffer();
|
|
|
|
|
throw(e);
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the message as an ArrayBuffer. This is an alias for {@link ProtoBuf.Builder.Message#encodeAB}.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#toArrayBuffer
|
|
|
|
|
* @function
|
|
|
|
|
* @return {ArrayBuffer} Encoded message as ArrayBuffer
|
|
|
|
|
* @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
|
|
|
|
|
* returns the encoded ArrayBuffer in the `encoded` property on the error.
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.toArrayBuffer = MessagePrototype.encodeAB;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Directly encodes the message to a node Buffer.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#encodeNB
|
|
|
|
|
* @function
|
|
|
|
|
* @return {!Buffer}
|
|
|
|
|
* @throws {Error} If the message cannot be encoded, not running under node.js or if required fields are
|
|
|
|
|
* missing. The later still returns the encoded node Buffer in the `encoded` property on the error.
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.encodeNB = function() {
|
|
|
|
|
try {
|
|
|
|
|
return this.encode().toBuffer();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (e["encoded"]) e["encoded"] = e["encoded"].toBuffer();
|
|
|
|
|
throw(e);
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the message as a node Buffer. This is an alias for {@link ProtoBuf.Builder.Message#encodeNB}.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#toBuffer
|
|
|
|
|
* @function
|
|
|
|
|
* @return {!Buffer}
|
|
|
|
|
* @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
|
|
|
|
|
* returns the encoded node Buffer in the `encoded` property on the error.
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.toBuffer = MessagePrototype.encodeNB;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Directly encodes the message to a base64 encoded string.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#encode64
|
|
|
|
|
* @function
|
|
|
|
|
* @return {string} Base64 encoded string
|
|
|
|
|
* @throws {Error} If the underlying buffer cannot be encoded or if required fields are missing. The later
|
|
|
|
|
* still returns the encoded base64 string in the `encoded` property on the error.
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.encode64 = function() {
|
|
|
|
|
try {
|
|
|
|
|
return this.encode().toBase64();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (e["encoded"]) e["encoded"] = e["encoded"].toBase64();
|
|
|
|
|
throw(e);
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the message as a base64 encoded string. This is an alias for {@link ProtoBuf.Builder.Message#encode64}.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#toBase64
|
|
|
|
|
* @function
|
|
|
|
|
* @return {string} Base64 encoded string
|
|
|
|
|
* @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
|
|
|
|
|
* returns the encoded base64 string in the `encoded` property on the error.
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.toBase64 = MessagePrototype.encode64;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Directly encodes the message to a hex encoded string.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#encodeHex
|
|
|
|
|
* @function
|
|
|
|
|
* @return {string} Hex encoded string
|
|
|
|
|
* @throws {Error} If the underlying buffer cannot be encoded or if required fields are missing. The later
|
|
|
|
|
* still returns the encoded hex string in the `encoded` property on the error.
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.encodeHex = function() {
|
|
|
|
|
try {
|
|
|
|
|
return this.encode().toHex();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (e["encoded"]) e["encoded"] = e["encoded"].toHex();
|
|
|
|
|
throw(e);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the message as a hex encoded string. This is an alias for {@link ProtoBuf.Builder.Message#encodeHex}.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#toHex
|
|
|
|
|
* @function
|
|
|
|
|
* @return {string} Hex encoded string
|
|
|
|
|
* @throws {Error} If the message cannot be encoded or if required fields are missing. The later still
|
|
|
|
|
* returns the encoded hex string in the `encoded` property on the error.
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.toHex = MessagePrototype.encodeHex;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clones a message object or field value to a raw object.
|
|
|
|
|
* @param {*} obj Object to clone
|
|
|
|
|
* @param {boolean} binaryAsBase64 Whether to include binary data as base64 strings or as a buffer otherwise
|
|
|
|
|
* @param {boolean} longsAsStrings Whether to encode longs as strings
|
|
|
|
|
* @param {!ProtoBuf.Reflect.T=} resolvedType The resolved field type if a field
|
|
|
|
|
* @returns {*} Cloned object
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
function cloneRaw(obj, binaryAsBase64, longsAsStrings, resolvedType) {
|
|
|
|
|
if (obj === null || typeof obj !== 'object') {
|
|
|
|
|
// Convert enum values to their respective names
|
|
|
|
|
if (resolvedType && resolvedType instanceof ProtoBuf.Reflect.Enum) {
|
|
|
|
|
var name = ProtoBuf.Reflect.Enum.getName(resolvedType.object, obj);
|
|
|
|
|
if (name !== null)
|
|
|
|
|
return name;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Pass-through string, number, boolean, null...
|
|
|
|
|
return obj;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Convert ByteBuffers to raw buffer or strings
|
|
|
|
|
if (ByteBuffer.isByteBuffer(obj))
|
|
|
|
|
return binaryAsBase64 ? obj.toBase64() : obj.toBuffer();
|
|
|
|
|
// Convert Longs to proper objects or strings
|
|
|
|
|
if (ProtoBuf.Long.isLong(obj))
|
2020-07-10 17:48:42 +00:00
|
|
|
|
return longsAsStrings ? obj.toString() : ProtoBuf.Long.fromValue(obj);
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var clone;
|
|
|
|
|
// Clone arrays
|
|
|
|
|
if (Array.isArray(obj)) {
|
|
|
|
|
clone = [];
|
|
|
|
|
obj.forEach(function(v, k) {
|
|
|
|
|
clone[k] = cloneRaw(v, binaryAsBase64, longsAsStrings, resolvedType);
|
|
|
|
|
});
|
|
|
|
|
return clone;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
clone = {};
|
|
|
|
|
// Convert maps to objects
|
|
|
|
|
if (obj instanceof ProtoBuf.Map) {
|
|
|
|
|
var it = obj.entries();
|
|
|
|
|
for (var e = it.next(); !e.done; e = it.next())
|
|
|
|
|
clone[obj.keyElem.valueToString(e.value[0])] = cloneRaw(e.value[1], binaryAsBase64, longsAsStrings, obj.valueElem.resolvedType);
|
|
|
|
|
return clone;
|
|
|
|
|
}
|
|
|
|
|
// Everything else is a non-null object
|
|
|
|
|
var type = obj.$type,
|
|
|
|
|
field = undefined;
|
|
|
|
|
for (var i in obj)
|
|
|
|
|
if (obj.hasOwnProperty(i)) {
|
|
|
|
|
if (type && (field = type.getChild(i)))
|
|
|
|
|
clone[i] = cloneRaw(obj[i], binaryAsBase64, longsAsStrings, field.resolvedType);
|
|
|
|
|
else
|
|
|
|
|
clone[i] = cloneRaw(obj[i], binaryAsBase64, longsAsStrings);
|
|
|
|
|
}
|
|
|
|
|
return clone;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the message's raw payload.
|
|
|
|
|
* @param {boolean=} binaryAsBase64 Whether to include binary data as base64 strings instead of Buffers, defaults to `false`
|
|
|
|
|
* @param {boolean} longsAsStrings Whether to encode longs as strings
|
|
|
|
|
* @returns {Object.<string,*>} Raw payload
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.toRaw = function(binaryAsBase64, longsAsStrings) {
|
|
|
|
|
return cloneRaw(this, !!binaryAsBase64, !!longsAsStrings, this.$type);
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Encodes a message to JSON.
|
|
|
|
|
* @returns {string} JSON string
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.encodeJSON = function() {
|
|
|
|
|
return JSON.stringify(
|
|
|
|
|
cloneRaw(this,
|
|
|
|
|
/* binary-as-base64 */ true,
|
|
|
|
|
/* longs-as-strings */ true,
|
|
|
|
|
this.$type
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Decodes a message from the specified buffer or string.
|
|
|
|
|
* @name ProtoBuf.Builder.Message.decode
|
|
|
|
|
* @function
|
|
|
|
|
* @param {!ByteBuffer|!ArrayBuffer|!Buffer|string} buffer Buffer to decode from
|
2020-07-10 17:48:42 +00:00
|
|
|
|
* @param {(number|string)=} length Message length. Defaults to decode all the remainig data.
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* @param {string=} enc Encoding if buffer is a string: hex, utf8 (not recommended), defaults to base64
|
|
|
|
|
* @return {!ProtoBuf.Builder.Message} Decoded message
|
|
|
|
|
* @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
|
|
|
|
|
* returns the decoded message with missing fields in the `decoded` property on the error.
|
|
|
|
|
* @expose
|
|
|
|
|
* @see ProtoBuf.Builder.Message.decode64
|
|
|
|
|
* @see ProtoBuf.Builder.Message.decodeHex
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-07-10 17:48:42 +00:00
|
|
|
|
Message.decode = function(buffer, length, enc) {
|
|
|
|
|
if (typeof length === 'string')
|
|
|
|
|
enc = length,
|
|
|
|
|
length = -1;
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (typeof buffer === 'string')
|
|
|
|
|
buffer = ByteBuffer.wrap(buffer, enc ? enc : "base64");
|
|
|
|
|
buffer = ByteBuffer.isByteBuffer(buffer) ? buffer : ByteBuffer.wrap(buffer); // May throw
|
|
|
|
|
var le = buffer.littleEndian;
|
|
|
|
|
try {
|
|
|
|
|
var msg = T.decode(buffer.LE());
|
|
|
|
|
buffer.LE(le);
|
|
|
|
|
return msg;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
buffer.LE(le);
|
|
|
|
|
throw(e);
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Decodes a varint32 length-delimited message from the specified buffer or string.
|
|
|
|
|
* @name ProtoBuf.Builder.Message.decodeDelimited
|
|
|
|
|
* @function
|
|
|
|
|
* @param {!ByteBuffer|!ArrayBuffer|!Buffer|string} buffer Buffer to decode from
|
|
|
|
|
* @param {string=} enc Encoding if buffer is a string: hex, utf8 (not recommended), defaults to base64
|
|
|
|
|
* @return {ProtoBuf.Builder.Message} Decoded message or `null` if not enough bytes are available yet
|
|
|
|
|
* @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
|
|
|
|
|
* returns the decoded message with missing fields in the `decoded` property on the error.
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
Message.decodeDelimited = function(buffer, enc) {
|
|
|
|
|
if (typeof buffer === 'string')
|
|
|
|
|
buffer = ByteBuffer.wrap(buffer, enc ? enc : "base64");
|
|
|
|
|
buffer = ByteBuffer.isByteBuffer(buffer) ? buffer : ByteBuffer.wrap(buffer); // May throw
|
|
|
|
|
if (buffer.remaining() < 1)
|
|
|
|
|
return null;
|
|
|
|
|
var off = buffer.offset,
|
|
|
|
|
len = buffer.readVarint32();
|
|
|
|
|
if (buffer.remaining() < len) {
|
|
|
|
|
buffer.offset = off;
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
var msg = T.decode(buffer.slice(buffer.offset, buffer.offset + len).LE());
|
|
|
|
|
buffer.offset += len;
|
|
|
|
|
return msg;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
buffer.offset += len;
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Decodes the message from the specified base64 encoded string.
|
|
|
|
|
* @name ProtoBuf.Builder.Message.decode64
|
|
|
|
|
* @function
|
|
|
|
|
* @param {string} str String to decode from
|
|
|
|
|
* @return {!ProtoBuf.Builder.Message} Decoded message
|
|
|
|
|
* @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
|
|
|
|
|
* returns the decoded message with missing fields in the `decoded` property on the error.
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
Message.decode64 = function(str) {
|
|
|
|
|
return Message.decode(str, "base64");
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Decodes the message from the specified hex encoded string.
|
|
|
|
|
* @name ProtoBuf.Builder.Message.decodeHex
|
|
|
|
|
* @function
|
|
|
|
|
* @param {string} str String to decode from
|
|
|
|
|
* @return {!ProtoBuf.Builder.Message} Decoded message
|
|
|
|
|
* @throws {Error} If the message cannot be decoded or if required fields are missing. The later still
|
|
|
|
|
* returns the decoded message with missing fields in the `decoded` property on the error.
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
Message.decodeHex = function(str) {
|
|
|
|
|
return Message.decode(str, "hex");
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Decodes the message from a JSON string.
|
|
|
|
|
* @name ProtoBuf.Builder.Message.decodeJSON
|
|
|
|
|
* @function
|
|
|
|
|
* @param {string} str String to decode from
|
|
|
|
|
* @return {!ProtoBuf.Builder.Message} Decoded message
|
|
|
|
|
* @throws {Error} If the message cannot be decoded or if required fields are
|
|
|
|
|
* missing.
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
Message.decodeJSON = function(str) {
|
|
|
|
|
return new Message(JSON.parse(str));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Utility
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Returns a string representation of this Message.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#toString
|
|
|
|
|
* @function
|
|
|
|
|
* @return {string} String representation as of ".Fully.Qualified.MessageName"
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
MessagePrototype.toString = function() {
|
|
|
|
|
return T.toString();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Properties
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Message options.
|
|
|
|
|
* @name ProtoBuf.Builder.Message.$options
|
|
|
|
|
* @type {Object.<string,*>}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var $optionsS; // cc needs this
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Message options.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#$options
|
|
|
|
|
* @type {Object.<string,*>}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var $options;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Reflection type.
|
|
|
|
|
* @name ProtoBuf.Builder.Message.$type
|
|
|
|
|
* @type {!ProtoBuf.Reflect.Message}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var $typeS;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Reflection type.
|
|
|
|
|
* @name ProtoBuf.Builder.Message#$type
|
|
|
|
|
* @type {!ProtoBuf.Reflect.Message}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var $type;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (Object.defineProperty)
|
|
|
|
|
Object.defineProperty(Message, '$options', { "value": T.buildOpt() }),
|
|
|
|
|
Object.defineProperty(MessagePrototype, "$options", { "value": Message["$options"] }),
|
|
|
|
|
Object.defineProperty(Message, "$type", { "value": T }),
|
|
|
|
|
Object.defineProperty(MessagePrototype, "$type", { "value": T });
|
|
|
|
|
|
|
|
|
|
return Message;
|
|
|
|
|
|
|
|
|
|
})(ProtoBuf, this);
|
|
|
|
|
|
|
|
|
|
// Static enums and prototyped sub-messages / cached collections
|
|
|
|
|
this._fields = [];
|
|
|
|
|
this._fieldsById = {};
|
|
|
|
|
this._fieldsByName = {};
|
|
|
|
|
for (var i=0, k=this.children.length, child; i<k; i++) {
|
|
|
|
|
child = this.children[i];
|
|
|
|
|
if (child instanceof Enum || child instanceof Message || child instanceof Service) {
|
|
|
|
|
if (clazz.hasOwnProperty(child.name))
|
|
|
|
|
throw Error("Illegal reflect child of "+this.toString(true)+": "+child.toString(true)+" cannot override static property '"+child.name+"'");
|
|
|
|
|
clazz[child.name] = child.build();
|
|
|
|
|
} else if (child instanceof Message.Field)
|
|
|
|
|
child.build(),
|
|
|
|
|
this._fields.push(child),
|
|
|
|
|
this._fieldsById[child.id] = child,
|
|
|
|
|
this._fieldsByName[child.name] = child;
|
|
|
|
|
else if (!(child instanceof Message.OneOf) && !(child instanceof Extension)) // Not built
|
|
|
|
|
throw Error("Illegal reflect child of "+this.toString(true)+": "+this.children[i].toString(true));
|
|
|
|
|
}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
return this.clazz = clazz;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Encodes a runtime message's contents to the specified buffer.
|
|
|
|
|
* @param {!ProtoBuf.Builder.Message} message Runtime message to encode
|
|
|
|
|
* @param {ByteBuffer} buffer ByteBuffer to write to
|
|
|
|
|
* @param {boolean=} noVerify Whether to not verify field values, defaults to `false`
|
|
|
|
|
* @return {ByteBuffer} The ByteBuffer for chaining
|
|
|
|
|
* @throws {Error} If required fields are missing or the message cannot be encoded for another reason
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.encode = function(message, buffer, noVerify) {
|
|
|
|
|
var fieldMissing = null,
|
|
|
|
|
field;
|
|
|
|
|
for (var i=0, k=this._fields.length, val; i<k; ++i) {
|
|
|
|
|
field = this._fields[i];
|
|
|
|
|
val = message[field.name];
|
|
|
|
|
if (field.required && val === null) {
|
|
|
|
|
if (fieldMissing === null)
|
|
|
|
|
fieldMissing = field;
|
|
|
|
|
} else
|
|
|
|
|
field.encode(noVerify ? val : field.verifyValue(val), buffer, message);
|
|
|
|
|
}
|
|
|
|
|
if (fieldMissing !== null) {
|
|
|
|
|
var err = Error("Missing at least one required field for "+this.toString(true)+": "+fieldMissing);
|
|
|
|
|
err["encoded"] = buffer; // Still expose what we got
|
|
|
|
|
throw(err);
|
|
|
|
|
}
|
2020-07-10 17:48:42 +00:00
|
|
|
|
if (message.__unknownFields) {
|
|
|
|
|
buffer.append(message.__unknownFields);
|
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
return buffer;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Calculates a runtime message's byte length.
|
|
|
|
|
* @param {!ProtoBuf.Builder.Message} message Runtime message to encode
|
|
|
|
|
* @returns {number} Byte length
|
|
|
|
|
* @throws {Error} If required fields are missing or the message cannot be calculated for another reason
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.calculate = function(message) {
|
|
|
|
|
for (var n=0, i=0, k=this._fields.length, field, val; i<k; ++i) {
|
|
|
|
|
field = this._fields[i];
|
|
|
|
|
val = message[field.name];
|
|
|
|
|
if (field.required && val === null)
|
|
|
|
|
throw Error("Missing at least one required field for "+this.toString(true)+": "+field);
|
|
|
|
|
else
|
|
|
|
|
n += field.calculate(val, message);
|
|
|
|
|
}
|
|
|
|
|
return n;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Skips all data until the end of the specified group has been reached.
|
|
|
|
|
* @param {number} expectedId Expected GROUPEND id
|
|
|
|
|
* @param {!ByteBuffer} buf ByteBuffer
|
|
|
|
|
* @returns {boolean} `true` if a value as been skipped, `false` if the end has been reached
|
|
|
|
|
* @throws {Error} If it wasn't possible to find the end of the group (buffer overrun or end tag mismatch)
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
function skipTillGroupEnd(expectedId, buf) {
|
|
|
|
|
var tag = buf.readVarint32(), // Throws on OOB
|
|
|
|
|
wireType = tag & 0x07,
|
|
|
|
|
id = tag >>> 3;
|
|
|
|
|
switch (wireType) {
|
|
|
|
|
case ProtoBuf.WIRE_TYPES.VARINT:
|
|
|
|
|
do tag = buf.readUint8();
|
|
|
|
|
while ((tag & 0x80) === 0x80);
|
|
|
|
|
break;
|
|
|
|
|
case ProtoBuf.WIRE_TYPES.BITS64:
|
|
|
|
|
buf.offset += 8;
|
|
|
|
|
break;
|
|
|
|
|
case ProtoBuf.WIRE_TYPES.LDELIM:
|
|
|
|
|
tag = buf.readVarint32(); // reads the varint
|
|
|
|
|
buf.offset += tag; // skips n bytes
|
|
|
|
|
break;
|
|
|
|
|
case ProtoBuf.WIRE_TYPES.STARTGROUP:
|
|
|
|
|
skipTillGroupEnd(id, buf);
|
|
|
|
|
break;
|
|
|
|
|
case ProtoBuf.WIRE_TYPES.ENDGROUP:
|
|
|
|
|
if (id === expectedId)
|
|
|
|
|
return false;
|
|
|
|
|
else
|
|
|
|
|
throw Error("Illegal GROUPEND after unknown group: "+id+" ("+expectedId+" expected)");
|
|
|
|
|
case ProtoBuf.WIRE_TYPES.BITS32:
|
|
|
|
|
buf.offset += 4;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw Error("Illegal wire type in unknown group "+expectedId+": "+wireType);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Decodes an encoded message and returns the decoded message.
|
|
|
|
|
* @param {ByteBuffer} buffer ByteBuffer to decode from
|
2020-07-10 17:48:42 +00:00
|
|
|
|
* @param {number=} length Message length. Defaults to decode all remaining data.
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* @param {number=} expectedGroupEndId Expected GROUPEND id if this is a legacy group
|
|
|
|
|
* @return {ProtoBuf.Builder.Message} Decoded message
|
|
|
|
|
* @throws {Error} If the message cannot be decoded
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
MessagePrototype.decode = function(buffer, length, expectedGroupEndId) {
|
|
|
|
|
length = typeof length === 'number' ? length : -1;
|
|
|
|
|
var start = buffer.offset,
|
|
|
|
|
msg = new (this.clazz)(),
|
|
|
|
|
tag, wireType, id, field;
|
|
|
|
|
while (buffer.offset < start+length || (length === -1 && buffer.remaining() > 0)) {
|
|
|
|
|
tag = buffer.readVarint32();
|
|
|
|
|
wireType = tag & 0x07;
|
|
|
|
|
id = tag >>> 3;
|
|
|
|
|
if (wireType === ProtoBuf.WIRE_TYPES.ENDGROUP) {
|
|
|
|
|
if (id !== expectedGroupEndId)
|
|
|
|
|
throw Error("Illegal group end indicator for "+this.toString(true)+": "+id+" ("+(expectedGroupEndId ? expectedGroupEndId+" expected" : "not a group")+")");
|
|
|
|
|
break;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-07-10 17:48:42 +00:00
|
|
|
|
// "messages created by your new code can be parsed by your old code: old binaries simply append the buffer to unknownFields when parsing.
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (!(field = this._fieldsById[id])) {
|
2020-07-10 17:48:42 +00:00
|
|
|
|
// Finds the starting offset to slice
|
|
|
|
|
let start = buffer.offset;
|
|
|
|
|
do {
|
|
|
|
|
--start;
|
|
|
|
|
buffer.offset = start;
|
|
|
|
|
} while (buffer.readVarint32() !== tag);
|
|
|
|
|
|
|
|
|
|
// Skip the piece in the buffer
|
2020-02-07 19:37:04 +00:00
|
|
|
|
switch (wireType) {
|
|
|
|
|
case ProtoBuf.WIRE_TYPES.VARINT:
|
|
|
|
|
buffer.readVarint32();
|
|
|
|
|
break;
|
|
|
|
|
case ProtoBuf.WIRE_TYPES.BITS32:
|
|
|
|
|
buffer.offset += 4;
|
|
|
|
|
break;
|
|
|
|
|
case ProtoBuf.WIRE_TYPES.BITS64:
|
|
|
|
|
buffer.offset += 8;
|
|
|
|
|
break;
|
|
|
|
|
case ProtoBuf.WIRE_TYPES.LDELIM:
|
|
|
|
|
var len = buffer.readVarint32();
|
|
|
|
|
buffer.offset += len;
|
|
|
|
|
break;
|
|
|
|
|
case ProtoBuf.WIRE_TYPES.STARTGROUP:
|
|
|
|
|
while (skipTillGroupEnd(id, buffer)) {}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw Error("Illegal wire type for unknown field "+id+" in "+this.toString(true)+"#decode: "+wireType);
|
|
|
|
|
}
|
2020-07-10 17:48:42 +00:00
|
|
|
|
|
|
|
|
|
// Slice the part of the buffer we can't parse and add it to unknownFields
|
|
|
|
|
const unknownFields = msg.__unknownFields ? msg.__unknownFields : new ByteBuffer(0);
|
|
|
|
|
const slicedBuffer = buffer.slice(start, buffer.offset);
|
|
|
|
|
msg.__unknownFields = ByteBuffer.concat([
|
|
|
|
|
unknownFields,
|
|
|
|
|
slicedBuffer
|
|
|
|
|
]);
|
2020-02-07 19:37:04 +00:00
|
|
|
|
continue;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (field.repeated && !field.options["packed"]) {
|
|
|
|
|
msg[field.name].push(field.decode(wireType, buffer));
|
|
|
|
|
} else if (field.map) {
|
|
|
|
|
var keyval = field.decode(wireType, buffer);
|
|
|
|
|
msg[field.name].set(keyval[0], keyval[1]);
|
|
|
|
|
} else {
|
|
|
|
|
msg[field.name] = field.decode(wireType, buffer);
|
|
|
|
|
if (field.oneof) { // Field is part of an OneOf (not a virtual OneOf field)
|
|
|
|
|
var currentField = msg[field.oneof.name]; // Virtual field references currently set field
|
|
|
|
|
if (currentField !== null && currentField !== field.name)
|
|
|
|
|
msg[currentField] = null; // Clear currently set field
|
|
|
|
|
msg[field.oneof.name] = field.name; // Point virtual field at this field
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Check if all required fields are present and set default values for optional fields that are not
|
|
|
|
|
for (var i=0, k=this._fields.length; i<k; ++i) {
|
|
|
|
|
field = this._fields[i];
|
|
|
|
|
if (msg[field.name] === null) {
|
|
|
|
|
if (this.syntax === "proto3") { // Proto3 sets default values by specification
|
|
|
|
|
msg[field.name] = field.defaultValue;
|
|
|
|
|
} else if (field.required) {
|
|
|
|
|
var err = Error("Missing at least one required field for " + this.toString(true) + ": " + field.name);
|
|
|
|
|
err["decoded"] = msg; // Still expose what we got
|
|
|
|
|
throw(err);
|
|
|
|
|
} else if (ProtoBuf.populateDefaults && field.defaultValue !== null)
|
|
|
|
|
msg[field.name] = field.defaultValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return msg;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect.Message
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Reflect.Message = Message;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Constructs a new Message Field.
|
|
|
|
|
* @exports ProtoBuf.Reflect.Message.Field
|
|
|
|
|
* @param {!ProtoBuf.Builder} builder Builder reference
|
|
|
|
|
* @param {!ProtoBuf.Reflect.Message} message Message reference
|
|
|
|
|
* @param {string} rule Rule, one of requried, optional, repeated
|
|
|
|
|
* @param {string?} keytype Key data type, if any.
|
|
|
|
|
* @param {string} type Data type, e.g. int32
|
|
|
|
|
* @param {string} name Field name
|
|
|
|
|
* @param {number} id Unique field id
|
|
|
|
|
* @param {Object.<string,*>=} options Options
|
|
|
|
|
* @param {!ProtoBuf.Reflect.Message.OneOf=} oneof Enclosing OneOf
|
|
|
|
|
* @param {string?} syntax The syntax level of this definition (e.g., proto3)
|
|
|
|
|
* @constructor
|
|
|
|
|
* @extends ProtoBuf.Reflect.T
|
|
|
|
|
*/
|
|
|
|
|
var Field = function(builder, message, rule, keytype, type, name, id, options, oneof, syntax) {
|
|
|
|
|
T.call(this, builder, message, name);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* @override
|
|
|
|
|
*/
|
|
|
|
|
this.className = "Message.Field";
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Message field required flag.
|
|
|
|
|
* @type {boolean}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
this.required = rule === "required";
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Message field repeated flag.
|
|
|
|
|
* @type {boolean}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.repeated = rule === "repeated";
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Message field map flag.
|
|
|
|
|
* @type {boolean}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.map = rule === "map";
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Message field key type. Type reference string if unresolved, protobuf
|
|
|
|
|
* type if resolved. Valid only if this.map === true, null otherwise.
|
|
|
|
|
* @type {string|{name: string, wireType: number}|null}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
this.keyType = keytype || null;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Message field type. Type reference string if unresolved, protobuf type if
|
|
|
|
|
* resolved. In a map field, this is the value type.
|
|
|
|
|
* @type {string|{name: string, wireType: number}}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
this.type = type;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Resolved type reference inside the global namespace.
|
|
|
|
|
* @type {ProtoBuf.Reflect.T|null}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
this.resolvedType = null;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Unique message field id.
|
|
|
|
|
* @type {number}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
this.id = id;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Message field options.
|
|
|
|
|
* @type {!Object.<string,*>}
|
|
|
|
|
* @dict
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
this.options = options || {};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Default value.
|
|
|
|
|
* @type {*}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
this.defaultValue = null;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Enclosing OneOf.
|
|
|
|
|
* @type {?ProtoBuf.Reflect.Message.OneOf}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
this.oneof = oneof || null;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Syntax level of this definition (e.g., proto3).
|
|
|
|
|
* @type {string}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
this.syntax = syntax || 'proto2';
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Original field name.
|
|
|
|
|
* @type {string}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
this.originalName = this.name; // Used to revert camelcase transformation on naming collisions
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Element implementation. Created in build() after types are resolved.
|
|
|
|
|
* @type {ProtoBuf.Element}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
this.element = null;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Key element implementation, for map fields. Created in build() after
|
|
|
|
|
* types are resolved.
|
|
|
|
|
* @type {ProtoBuf.Element}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
this.keyElement = null;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Convert field names to camel case notation if the override is set
|
|
|
|
|
if (this.builder.options['convertFieldsToCamelCase'] && !(this instanceof Message.ExtensionField))
|
|
|
|
|
this.name = ProtoBuf.Util.toCamelCase(this.name);
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect.Message.Field.prototype
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
var FieldPrototype = Field.prototype = Object.create(T.prototype);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Builds the field.
|
|
|
|
|
* @override
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
FieldPrototype.build = function() {
|
|
|
|
|
this.element = new Element(this.type, this.resolvedType, false, this.syntax);
|
|
|
|
|
if (this.map)
|
|
|
|
|
this.keyElement = new Element(this.keyType, undefined, true, this.syntax);
|
|
|
|
|
|
|
|
|
|
// In proto3, fields do not have field presence, and every field is set to
|
|
|
|
|
// its type's default value ("", 0, 0.0, or false).
|
|
|
|
|
if (this.syntax === 'proto3' && !this.repeated && !this.map)
|
|
|
|
|
this.defaultValue = Element.defaultFieldValue(this.type);
|
|
|
|
|
|
|
|
|
|
// Otherwise, default values are present when explicitly specified
|
|
|
|
|
else if (typeof this.options['default'] !== 'undefined')
|
|
|
|
|
this.defaultValue = this.verifyValue(this.options['default']);
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Checks if the given value can be set for this field.
|
|
|
|
|
* @param {*} value Value to check
|
|
|
|
|
* @param {boolean=} skipRepeated Whether to skip the repeated value check or not. Defaults to false.
|
|
|
|
|
* @return {*} Verified, maybe adjusted, value
|
|
|
|
|
* @throws {Error} If the value cannot be set for this field
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
FieldPrototype.verifyValue = function(value, skipRepeated) {
|
|
|
|
|
skipRepeated = skipRepeated || false;
|
2020-07-10 17:48:42 +00:00
|
|
|
|
var self = this;
|
|
|
|
|
function fail(val, msg) {
|
|
|
|
|
throw Error("Illegal value for "+self.toString(true)+" of type "+self.type.name+": "+val+" ("+msg+")");
|
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (value === null) { // NULL values for optional fields
|
|
|
|
|
if (this.required)
|
|
|
|
|
fail(typeof value, "required");
|
|
|
|
|
if (this.syntax === 'proto3' && this.type !== ProtoBuf.TYPES["message"])
|
|
|
|
|
fail(typeof value, "proto3 field without field presence cannot be null");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var i;
|
|
|
|
|
if (this.repeated && !skipRepeated) { // Repeated values as arrays
|
|
|
|
|
if (!Array.isArray(value))
|
|
|
|
|
value = [value];
|
|
|
|
|
var res = [];
|
|
|
|
|
for (i=0; i<value.length; i++)
|
|
|
|
|
res.push(this.element.verifyValue(value[i]));
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
if (this.map && !skipRepeated) { // Map values as objects
|
|
|
|
|
if (!(value instanceof ProtoBuf.Map)) {
|
|
|
|
|
// If not already a Map, attempt to convert.
|
|
|
|
|
if (!(value instanceof Object)) {
|
|
|
|
|
fail(typeof value,
|
|
|
|
|
"expected ProtoBuf.Map or raw object for map field");
|
|
|
|
|
}
|
|
|
|
|
return new ProtoBuf.Map(this, value);
|
|
|
|
|
} else {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// All non-repeated fields expect no array
|
|
|
|
|
if (!this.repeated && Array.isArray(value))
|
|
|
|
|
fail(typeof value, "no array expected");
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
return this.element.verifyValue(value);
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Determines whether the field will have a presence on the wire given its
|
|
|
|
|
* value.
|
|
|
|
|
* @param {*} value Verified field value
|
|
|
|
|
* @param {!ProtoBuf.Builder.Message} message Runtime message
|
|
|
|
|
* @return {boolean} Whether the field will be present on the wire
|
|
|
|
|
*/
|
|
|
|
|
FieldPrototype.hasWirePresence = function(value, message) {
|
|
|
|
|
if (this.syntax !== 'proto3')
|
|
|
|
|
return (value !== null);
|
|
|
|
|
if (this.oneof && message[this.oneof.name] === this.name)
|
|
|
|
|
return true;
|
|
|
|
|
switch (this.type) {
|
|
|
|
|
case ProtoBuf.TYPES["int32"]:
|
|
|
|
|
case ProtoBuf.TYPES["sint32"]:
|
|
|
|
|
case ProtoBuf.TYPES["sfixed32"]:
|
|
|
|
|
case ProtoBuf.TYPES["uint32"]:
|
|
|
|
|
case ProtoBuf.TYPES["fixed32"]:
|
|
|
|
|
return value !== 0;
|
|
|
|
|
|
|
|
|
|
case ProtoBuf.TYPES["int64"]:
|
|
|
|
|
case ProtoBuf.TYPES["sint64"]:
|
|
|
|
|
case ProtoBuf.TYPES["sfixed64"]:
|
|
|
|
|
case ProtoBuf.TYPES["uint64"]:
|
|
|
|
|
case ProtoBuf.TYPES["fixed64"]:
|
|
|
|
|
return value.low !== 0 || value.high !== 0;
|
|
|
|
|
|
|
|
|
|
case ProtoBuf.TYPES["bool"]:
|
|
|
|
|
return value;
|
|
|
|
|
|
|
|
|
|
case ProtoBuf.TYPES["float"]:
|
|
|
|
|
case ProtoBuf.TYPES["double"]:
|
|
|
|
|
return value !== 0.0;
|
|
|
|
|
|
|
|
|
|
case ProtoBuf.TYPES["string"]:
|
|
|
|
|
return value.length > 0;
|
|
|
|
|
|
|
|
|
|
case ProtoBuf.TYPES["bytes"]:
|
|
|
|
|
return value.remaining() > 0;
|
|
|
|
|
|
|
|
|
|
case ProtoBuf.TYPES["enum"]:
|
|
|
|
|
return value !== 0;
|
|
|
|
|
|
|
|
|
|
case ProtoBuf.TYPES["message"]:
|
|
|
|
|
return value !== null;
|
|
|
|
|
default:
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Encodes the specified field value to the specified buffer.
|
|
|
|
|
* @param {*} value Verified field value
|
|
|
|
|
* @param {ByteBuffer} buffer ByteBuffer to encode to
|
|
|
|
|
* @param {!ProtoBuf.Builder.Message} message Runtime message
|
|
|
|
|
* @return {ByteBuffer} The ByteBuffer for chaining
|
|
|
|
|
* @throws {Error} If the field cannot be encoded
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
FieldPrototype.encode = function(value, buffer, message) {
|
|
|
|
|
if (this.type === null || typeof this.type !== 'object')
|
|
|
|
|
throw Error("[INTERNAL] Unresolved type in "+this.toString(true)+": "+this.type);
|
|
|
|
|
if (value === null || (this.repeated && value.length == 0))
|
|
|
|
|
return buffer; // Optional omitted
|
|
|
|
|
try {
|
|
|
|
|
if (this.repeated) {
|
|
|
|
|
var i;
|
|
|
|
|
// "Only repeated fields of primitive numeric types (types which use the varint, 32-bit, or 64-bit wire
|
|
|
|
|
// types) can be declared 'packed'."
|
|
|
|
|
if (this.options["packed"] && ProtoBuf.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType) >= 0) {
|
|
|
|
|
// "All of the elements of the field are packed into a single key-value pair with wire type 2
|
|
|
|
|
// (length-delimited). Each element is encoded the same way it would be normally, except without a
|
|
|
|
|
// tag preceding it."
|
|
|
|
|
buffer.writeVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.LDELIM);
|
|
|
|
|
buffer.ensureCapacity(buffer.offset += 1); // We do not know the length yet, so let's assume a varint of length 1
|
|
|
|
|
var start = buffer.offset; // Remember where the contents begin
|
|
|
|
|
for (i=0; i<value.length; i++)
|
|
|
|
|
this.element.encodeValue(this.id, value[i], buffer);
|
|
|
|
|
var len = buffer.offset-start,
|
|
|
|
|
varintLen = ByteBuffer.calculateVarint32(len);
|
|
|
|
|
if (varintLen > 1) { // We need to move the contents
|
|
|
|
|
var contents = buffer.slice(start, buffer.offset);
|
|
|
|
|
start += varintLen-1;
|
|
|
|
|
buffer.offset = start;
|
|
|
|
|
buffer.append(contents);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
buffer.writeVarint32(len, start-varintLen);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
} else {
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// "If your message definition has repeated elements (without the [packed=true] option), the encoded
|
|
|
|
|
// message has zero or more key-value pairs with the same tag number"
|
|
|
|
|
for (i=0; i<value.length; i++)
|
|
|
|
|
buffer.writeVarint32((this.id << 3) | this.type.wireType),
|
|
|
|
|
this.element.encodeValue(this.id, value[i], buffer);
|
|
|
|
|
}
|
|
|
|
|
} else if (this.map) {
|
|
|
|
|
// Write out each map entry as a submessage.
|
|
|
|
|
value.forEach(function(val, key, m) {
|
|
|
|
|
// Compute the length of the submessage (key, val) pair.
|
|
|
|
|
var length =
|
|
|
|
|
ByteBuffer.calculateVarint32((1 << 3) | this.keyType.wireType) +
|
|
|
|
|
this.keyElement.calculateLength(1, key) +
|
|
|
|
|
ByteBuffer.calculateVarint32((2 << 3) | this.type.wireType) +
|
|
|
|
|
this.element.calculateLength(2, val);
|
|
|
|
|
|
|
|
|
|
// Submessage with wire type of length-delimited.
|
|
|
|
|
buffer.writeVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.LDELIM);
|
|
|
|
|
buffer.writeVarint32(length);
|
|
|
|
|
|
|
|
|
|
// Write out the key and val.
|
|
|
|
|
buffer.writeVarint32((1 << 3) | this.keyType.wireType);
|
|
|
|
|
this.keyElement.encodeValue(1, key, buffer);
|
|
|
|
|
buffer.writeVarint32((2 << 3) | this.type.wireType);
|
|
|
|
|
this.element.encodeValue(2, val, buffer);
|
|
|
|
|
}, this);
|
|
|
|
|
} else {
|
|
|
|
|
if (this.hasWirePresence(value, message)) {
|
|
|
|
|
buffer.writeVarint32((this.id << 3) | this.type.wireType);
|
|
|
|
|
this.element.encodeValue(this.id, value, buffer);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
} catch (e) {
|
|
|
|
|
throw Error("Illegal value for "+this.toString(true)+": "+value+" ("+e+")");
|
|
|
|
|
}
|
|
|
|
|
return buffer;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Calculates the length of this field's value on the network level.
|
|
|
|
|
* @param {*} value Field value
|
|
|
|
|
* @param {!ProtoBuf.Builder.Message} message Runtime message
|
|
|
|
|
* @returns {number} Byte length
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
FieldPrototype.calculate = function(value, message) {
|
|
|
|
|
value = this.verifyValue(value); // May throw
|
|
|
|
|
if (this.type === null || typeof this.type !== 'object')
|
|
|
|
|
throw Error("[INTERNAL] Unresolved type in "+this.toString(true)+": "+this.type);
|
|
|
|
|
if (value === null || (this.repeated && value.length == 0))
|
|
|
|
|
return 0; // Optional omitted
|
|
|
|
|
var n = 0;
|
|
|
|
|
try {
|
|
|
|
|
if (this.repeated) {
|
|
|
|
|
var i, ni;
|
|
|
|
|
if (this.options["packed"] && ProtoBuf.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType) >= 0) {
|
|
|
|
|
n += ByteBuffer.calculateVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.LDELIM);
|
|
|
|
|
ni = 0;
|
|
|
|
|
for (i=0; i<value.length; i++)
|
|
|
|
|
ni += this.element.calculateLength(this.id, value[i]);
|
|
|
|
|
n += ByteBuffer.calculateVarint32(ni);
|
|
|
|
|
n += ni;
|
|
|
|
|
} else {
|
|
|
|
|
for (i=0; i<value.length; i++)
|
|
|
|
|
n += ByteBuffer.calculateVarint32((this.id << 3) | this.type.wireType),
|
|
|
|
|
n += this.element.calculateLength(this.id, value[i]);
|
|
|
|
|
}
|
|
|
|
|
} else if (this.map) {
|
|
|
|
|
// Each map entry becomes a submessage.
|
|
|
|
|
value.forEach(function(val, key, m) {
|
|
|
|
|
// Compute the length of the submessage (key, val) pair.
|
|
|
|
|
var length =
|
|
|
|
|
ByteBuffer.calculateVarint32((1 << 3) | this.keyType.wireType) +
|
|
|
|
|
this.keyElement.calculateLength(1, key) +
|
|
|
|
|
ByteBuffer.calculateVarint32((2 << 3) | this.type.wireType) +
|
|
|
|
|
this.element.calculateLength(2, val);
|
|
|
|
|
|
|
|
|
|
n += ByteBuffer.calculateVarint32((this.id << 3) | ProtoBuf.WIRE_TYPES.LDELIM);
|
|
|
|
|
n += ByteBuffer.calculateVarint32(length);
|
|
|
|
|
n += length;
|
|
|
|
|
}, this);
|
|
|
|
|
} else {
|
|
|
|
|
if (this.hasWirePresence(value, message)) {
|
|
|
|
|
n += ByteBuffer.calculateVarint32((this.id << 3) | this.type.wireType);
|
|
|
|
|
n += this.element.calculateLength(this.id, value);
|
|
|
|
|
}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
} catch (e) {
|
|
|
|
|
throw Error("Illegal value for "+this.toString(true)+": "+value+" ("+e+")");
|
|
|
|
|
}
|
|
|
|
|
return n;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Decode the field value from the specified buffer.
|
|
|
|
|
* @param {number} wireType Leading wire type
|
|
|
|
|
* @param {ByteBuffer} buffer ByteBuffer to decode from
|
|
|
|
|
* @param {boolean=} skipRepeated Whether to skip the repeated check or not. Defaults to false.
|
|
|
|
|
* @return {*} Decoded value: array for packed repeated fields, [key, value] for
|
|
|
|
|
* map fields, or an individual value otherwise.
|
|
|
|
|
* @throws {Error} If the field cannot be decoded
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
FieldPrototype.decode = function(wireType, buffer, skipRepeated) {
|
|
|
|
|
var value, nBytes;
|
|
|
|
|
|
|
|
|
|
// We expect wireType to match the underlying type's wireType unless we see
|
|
|
|
|
// a packed repeated field, or unless this is a map field.
|
|
|
|
|
var wireTypeOK =
|
|
|
|
|
(!this.map && wireType == this.type.wireType) ||
|
|
|
|
|
(!skipRepeated && this.repeated && this.options["packed"] &&
|
|
|
|
|
wireType == ProtoBuf.WIRE_TYPES.LDELIM) ||
|
|
|
|
|
(this.map && wireType == ProtoBuf.WIRE_TYPES.LDELIM);
|
|
|
|
|
if (!wireTypeOK)
|
|
|
|
|
throw Error("Illegal wire type for field "+this.toString(true)+": "+wireType+" ("+this.type.wireType+" expected)");
|
|
|
|
|
|
|
|
|
|
// Handle packed repeated fields.
|
|
|
|
|
if (wireType == ProtoBuf.WIRE_TYPES.LDELIM && this.repeated && this.options["packed"] && ProtoBuf.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType) >= 0) {
|
|
|
|
|
if (!skipRepeated) {
|
|
|
|
|
nBytes = buffer.readVarint32();
|
|
|
|
|
nBytes = buffer.offset + nBytes; // Limit
|
|
|
|
|
var values = [];
|
|
|
|
|
while (buffer.offset < nBytes)
|
|
|
|
|
values.push(this.decode(this.type.wireType, buffer, true));
|
|
|
|
|
return values;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Read the next value otherwise...
|
|
|
|
|
}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Handle maps.
|
|
|
|
|
if (this.map) {
|
|
|
|
|
// Read one (key, value) submessage, and return [key, value]
|
|
|
|
|
var key = Element.defaultFieldValue(this.keyType);
|
|
|
|
|
value = Element.defaultFieldValue(this.type);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Read the length
|
|
|
|
|
nBytes = buffer.readVarint32();
|
|
|
|
|
if (buffer.remaining() < nBytes)
|
|
|
|
|
throw Error("Illegal number of bytes for "+this.toString(true)+": "+nBytes+" required but got only "+buffer.remaining());
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Get a sub-buffer of this key/value submessage
|
|
|
|
|
var msgbuf = buffer.clone();
|
|
|
|
|
msgbuf.limit = msgbuf.offset + nBytes;
|
|
|
|
|
buffer.offset += nBytes;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
while (msgbuf.remaining() > 0) {
|
|
|
|
|
var tag = msgbuf.readVarint32();
|
|
|
|
|
wireType = tag & 0x07;
|
|
|
|
|
var id = tag >>> 3;
|
|
|
|
|
if (id === 1) {
|
|
|
|
|
key = this.keyElement.decode(msgbuf, wireType, id);
|
|
|
|
|
} else if (id === 2) {
|
|
|
|
|
value = this.element.decode(msgbuf, wireType, id);
|
|
|
|
|
} else {
|
|
|
|
|
throw Error("Unexpected tag in map field key/value submessage");
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
return [key, value];
|
|
|
|
|
}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Handle singular and non-packed repeated field values.
|
|
|
|
|
return this.element.decode(buffer, wireType, this.id);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect.Message.Field
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Reflect.Message.Field = Field;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Constructs a new Message ExtensionField.
|
|
|
|
|
* @exports ProtoBuf.Reflect.Message.ExtensionField
|
|
|
|
|
* @param {!ProtoBuf.Builder} builder Builder reference
|
|
|
|
|
* @param {!ProtoBuf.Reflect.Message} message Message reference
|
|
|
|
|
* @param {string} rule Rule, one of requried, optional, repeated
|
|
|
|
|
* @param {string} type Data type, e.g. int32
|
|
|
|
|
* @param {string} name Field name
|
|
|
|
|
* @param {number} id Unique field id
|
|
|
|
|
* @param {!Object.<string,*>=} options Options
|
|
|
|
|
* @constructor
|
|
|
|
|
* @extends ProtoBuf.Reflect.Message.Field
|
|
|
|
|
*/
|
|
|
|
|
var ExtensionField = function(builder, message, rule, type, name, id, options) {
|
|
|
|
|
Field.call(this, builder, message, rule, /* keytype = */ null, type, name, id, options);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Extension reference.
|
|
|
|
|
* @type {!ProtoBuf.Reflect.Extension}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.extension;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Extends Field
|
|
|
|
|
ExtensionField.prototype = Object.create(Field.prototype);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect.Message.ExtensionField
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Reflect.Message.ExtensionField = ExtensionField;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Constructs a new Message OneOf.
|
|
|
|
|
* @exports ProtoBuf.Reflect.Message.OneOf
|
|
|
|
|
* @param {!ProtoBuf.Builder} builder Builder reference
|
|
|
|
|
* @param {!ProtoBuf.Reflect.Message} message Message reference
|
|
|
|
|
* @param {string} name OneOf name
|
|
|
|
|
* @constructor
|
|
|
|
|
* @extends ProtoBuf.Reflect.T
|
|
|
|
|
*/
|
|
|
|
|
var OneOf = function(builder, message, name) {
|
|
|
|
|
T.call(this, builder, message, name);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Enclosed fields.
|
|
|
|
|
* @type {!Array.<!ProtoBuf.Reflect.Message.Field>}
|
|
|
|
|
* @expose
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.fields = [];
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect.Message.OneOf
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Reflect.Message.OneOf = OneOf;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Constructs a new Enum.
|
|
|
|
|
* @exports ProtoBuf.Reflect.Enum
|
|
|
|
|
* @param {!ProtoBuf.Builder} builder Builder reference
|
|
|
|
|
* @param {!ProtoBuf.Reflect.T} parent Parent Reflect object
|
|
|
|
|
* @param {string} name Enum name
|
|
|
|
|
* @param {Object.<string,*>=} options Enum options
|
|
|
|
|
* @param {string?} syntax The syntax level (e.g., proto3)
|
|
|
|
|
* @constructor
|
|
|
|
|
* @extends ProtoBuf.Reflect.Namespace
|
|
|
|
|
*/
|
|
|
|
|
var Enum = function(builder, parent, name, options, syntax) {
|
|
|
|
|
Namespace.call(this, builder, parent, name, options, syntax);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* @override
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.className = "Enum";
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Runtime enum object.
|
|
|
|
|
* @type {Object.<string,number>|null}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.object = null;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Gets the string name of an enum value.
|
|
|
|
|
* @param {!ProtoBuf.Builder.Enum} enm Runtime enum
|
|
|
|
|
* @param {number} value Enum value
|
|
|
|
|
* @returns {?string} Name or `null` if not present
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Enum.getName = function(enm, value) {
|
|
|
|
|
var keys = Object.keys(enm);
|
|
|
|
|
for (var i=0, key; i<keys.length; ++i)
|
|
|
|
|
if (enm[key = keys[i]] === value)
|
|
|
|
|
return key;
|
|
|
|
|
return null;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect.Enum.prototype
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
var EnumPrototype = Enum.prototype = Object.create(Namespace.prototype);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Builds this enum and returns the runtime counterpart.
|
|
|
|
|
* @param {boolean} rebuild Whether to rebuild or not, defaults to false
|
|
|
|
|
* @returns {!Object.<string,number>}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
EnumPrototype.build = function(rebuild) {
|
|
|
|
|
if (this.object && !rebuild)
|
|
|
|
|
return this.object;
|
|
|
|
|
var enm = new ProtoBuf.Builder.Enum(),
|
|
|
|
|
values = this.getChildren(Enum.Value);
|
|
|
|
|
for (var i=0, k=values.length; i<k; ++i)
|
|
|
|
|
enm[values[i]['name']] = values[i]['id'];
|
|
|
|
|
if (Object.defineProperty)
|
|
|
|
|
Object.defineProperty(enm, '$options', {
|
|
|
|
|
"value": this.buildOpt(),
|
|
|
|
|
"enumerable": false
|
|
|
|
|
});
|
|
|
|
|
return this.object = enm;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect.Enum
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Reflect.Enum = Enum;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Constructs a new Enum Value.
|
|
|
|
|
* @exports ProtoBuf.Reflect.Enum.Value
|
|
|
|
|
* @param {!ProtoBuf.Builder} builder Builder reference
|
|
|
|
|
* @param {!ProtoBuf.Reflect.Enum} enm Enum reference
|
|
|
|
|
* @param {string} name Field name
|
|
|
|
|
* @param {number} id Unique field id
|
|
|
|
|
* @constructor
|
|
|
|
|
* @extends ProtoBuf.Reflect.T
|
|
|
|
|
*/
|
|
|
|
|
var Value = function(builder, enm, name, id) {
|
|
|
|
|
T.call(this, builder, enm, name);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* @override
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.className = "Enum.Value";
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Unique enum value id.
|
|
|
|
|
* @type {number}
|
|
|
|
|
* @expose
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.id = id;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Extends T
|
|
|
|
|
Value.prototype = Object.create(T.prototype);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect.Enum.Value
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Reflect.Enum.Value = Value;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* An extension (field).
|
|
|
|
|
* @exports ProtoBuf.Reflect.Extension
|
|
|
|
|
* @constructor
|
|
|
|
|
* @param {!ProtoBuf.Builder} builder Builder reference
|
|
|
|
|
* @param {!ProtoBuf.Reflect.T} parent Parent object
|
|
|
|
|
* @param {string} name Object name
|
|
|
|
|
* @param {!ProtoBuf.Reflect.Message.Field} field Extension field
|
|
|
|
|
*/
|
|
|
|
|
var Extension = function(builder, parent, name, field) {
|
|
|
|
|
T.call(this, builder, parent, name);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Extended message field.
|
|
|
|
|
* @type {!ProtoBuf.Reflect.Message.Field}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.field = field;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Extends T
|
|
|
|
|
Extension.prototype = Object.create(T.prototype);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect.Extension
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Reflect.Extension = Extension;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Constructs a new Service.
|
|
|
|
|
* @exports ProtoBuf.Reflect.Service
|
|
|
|
|
* @param {!ProtoBuf.Builder} builder Builder reference
|
|
|
|
|
* @param {!ProtoBuf.Reflect.Namespace} root Root
|
|
|
|
|
* @param {string} name Service name
|
|
|
|
|
* @param {Object.<string,*>=} options Options
|
|
|
|
|
* @constructor
|
|
|
|
|
* @extends ProtoBuf.Reflect.Namespace
|
|
|
|
|
*/
|
|
|
|
|
var Service = function(builder, root, name, options) {
|
|
|
|
|
Namespace.call(this, builder, root, name, options);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* @override
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.className = "Service";
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Built runtime service class.
|
|
|
|
|
* @type {?function(new:ProtoBuf.Builder.Service)}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.clazz = null;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect.Service.prototype
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
var ServicePrototype = Service.prototype = Object.create(Namespace.prototype);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Builds the service and returns the runtime counterpart, which is a fully functional class.
|
|
|
|
|
* @see ProtoBuf.Builder.Service
|
|
|
|
|
* @param {boolean=} rebuild Whether to rebuild or not
|
|
|
|
|
* @return {Function} Service class
|
|
|
|
|
* @throws {Error} If the message cannot be built
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ServicePrototype.build = function(rebuild) {
|
|
|
|
|
if (this.clazz && !rebuild)
|
|
|
|
|
return this.clazz;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Create the runtime Service class in its own scope
|
|
|
|
|
return this.clazz = (function(ProtoBuf, T) {
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Constructs a new runtime Service.
|
|
|
|
|
* @name ProtoBuf.Builder.Service
|
|
|
|
|
* @param {function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))=} rpcImpl RPC implementation receiving the method name and the message
|
|
|
|
|
* @class Barebone of all runtime services.
|
|
|
|
|
* @constructor
|
|
|
|
|
* @throws {Error} If the service cannot be created
|
|
|
|
|
*/
|
|
|
|
|
var Service = function(rpcImpl) {
|
|
|
|
|
ProtoBuf.Builder.Service.call(this);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Service implementation.
|
|
|
|
|
* @name ProtoBuf.Builder.Service#rpcImpl
|
|
|
|
|
* @type {!function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))}
|
|
|
|
|
* @expose
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.rpcImpl = rpcImpl || function(name, msg, callback) {
|
|
|
|
|
// This is what a user has to implement: A function receiving the method name, the actual message to
|
|
|
|
|
// send (type checked) and the callback that's either provided with the error as its first
|
|
|
|
|
// argument or null and the actual response message.
|
|
|
|
|
setTimeout(callback.bind(this, Error("Not implemented, see: https://github.com/dcodeIO/ProtoBuf.js/wiki/Services")), 0); // Must be async!
|
|
|
|
|
};
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Builder.Service.prototype
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
var ServicePrototype = Service.prototype = Object.create(ProtoBuf.Builder.Service.prototype);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Asynchronously performs an RPC call using the given RPC implementation.
|
|
|
|
|
* @name ProtoBuf.Builder.Service.[Method]
|
|
|
|
|
* @function
|
|
|
|
|
* @param {!function(string, ProtoBuf.Builder.Message, function(Error, ProtoBuf.Builder.Message=))} rpcImpl RPC implementation
|
|
|
|
|
* @param {ProtoBuf.Builder.Message} req Request
|
|
|
|
|
* @param {function(Error, (ProtoBuf.Builder.Message|ByteBuffer|Buffer|string)=)} callback Callback receiving
|
|
|
|
|
* the error if any and the response either as a pre-parsed message or as its raw bytes
|
|
|
|
|
* @abstract
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Asynchronously performs an RPC call using the instance's RPC implementation.
|
|
|
|
|
* @name ProtoBuf.Builder.Service#[Method]
|
|
|
|
|
* @function
|
|
|
|
|
* @param {ProtoBuf.Builder.Message} req Request
|
|
|
|
|
* @param {function(Error, (ProtoBuf.Builder.Message|ByteBuffer|Buffer|string)=)} callback Callback receiving
|
|
|
|
|
* the error if any and the response either as a pre-parsed message or as its raw bytes
|
|
|
|
|
* @abstract
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var rpc = T.getChildren(ProtoBuf.Reflect.Service.RPCMethod);
|
|
|
|
|
for (var i=0; i<rpc.length; i++) {
|
|
|
|
|
(function(method) {
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// service#Method(message, callback)
|
|
|
|
|
ServicePrototype[method.name] = function(req, callback) {
|
|
|
|
|
try {
|
|
|
|
|
try {
|
|
|
|
|
// If given as a buffer, decode the request. Will throw a TypeError if not a valid buffer.
|
|
|
|
|
req = method.resolvedRequestType.clazz.decode(ByteBuffer.wrap(req));
|
|
|
|
|
} catch (err) {
|
|
|
|
|
if (!(err instanceof TypeError))
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
if (req === null || typeof req !== 'object')
|
|
|
|
|
throw Error("Illegal arguments");
|
|
|
|
|
if (!(req instanceof method.resolvedRequestType.clazz))
|
|
|
|
|
req = new method.resolvedRequestType.clazz(req);
|
|
|
|
|
this.rpcImpl(method.fqn(), req, function(err, res) { // Assumes that this is properly async
|
|
|
|
|
if (err) {
|
|
|
|
|
callback(err);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-07-10 17:48:42 +00:00
|
|
|
|
// Coalesce to empty string when service response has empty content
|
|
|
|
|
if (res === null)
|
|
|
|
|
res = ''
|
2020-02-07 19:37:04 +00:00
|
|
|
|
try { res = method.resolvedResponseType.clazz.decode(res); } catch (notABuffer) {}
|
|
|
|
|
if (!res || !(res instanceof method.resolvedResponseType.clazz)) {
|
|
|
|
|
callback(Error("Illegal response type received in service method "+ T.name+"#"+method.name));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
callback(null, res);
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setTimeout(callback.bind(this, err), 0);
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Service.Method(rpcImpl, message, callback)
|
|
|
|
|
Service[method.name] = function(rpcImpl, req, callback) {
|
|
|
|
|
new Service(rpcImpl)[method.name](req, callback);
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (Object.defineProperty)
|
|
|
|
|
Object.defineProperty(Service[method.name], "$options", { "value": method.buildOpt() }),
|
|
|
|
|
Object.defineProperty(ServicePrototype[method.name], "$options", { "value": Service[method.name]["$options"] });
|
|
|
|
|
})(rpc[i]);
|
|
|
|
|
}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Properties
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Service options.
|
|
|
|
|
* @name ProtoBuf.Builder.Service.$options
|
|
|
|
|
* @type {Object.<string,*>}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var $optionsS; // cc needs this
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Service options.
|
|
|
|
|
* @name ProtoBuf.Builder.Service#$options
|
|
|
|
|
* @type {Object.<string,*>}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var $options;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Reflection type.
|
|
|
|
|
* @name ProtoBuf.Builder.Service.$type
|
|
|
|
|
* @type {!ProtoBuf.Reflect.Service}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var $typeS;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Reflection type.
|
|
|
|
|
* @name ProtoBuf.Builder.Service#$type
|
|
|
|
|
* @type {!ProtoBuf.Reflect.Service}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var $type;
|
|
|
|
|
|
|
|
|
|
if (Object.defineProperty)
|
|
|
|
|
Object.defineProperty(Service, "$options", { "value": T.buildOpt() }),
|
|
|
|
|
Object.defineProperty(ServicePrototype, "$options", { "value": Service["$options"] }),
|
|
|
|
|
Object.defineProperty(Service, "$type", { "value": T }),
|
|
|
|
|
Object.defineProperty(ServicePrototype, "$type", { "value": T });
|
|
|
|
|
|
|
|
|
|
return Service;
|
|
|
|
|
|
|
|
|
|
})(ProtoBuf, this);
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect.Service
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Reflect.Service = Service;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Abstract service method.
|
|
|
|
|
* @exports ProtoBuf.Reflect.Service.Method
|
|
|
|
|
* @param {!ProtoBuf.Builder} builder Builder reference
|
|
|
|
|
* @param {!ProtoBuf.Reflect.Service} svc Service
|
|
|
|
|
* @param {string} name Method name
|
|
|
|
|
* @param {Object.<string,*>=} options Options
|
|
|
|
|
* @constructor
|
|
|
|
|
* @extends ProtoBuf.Reflect.T
|
|
|
|
|
*/
|
|
|
|
|
var Method = function(builder, svc, name, options) {
|
|
|
|
|
T.call(this, builder, svc, name);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @override
|
|
|
|
|
*/
|
|
|
|
|
this.className = "Service.Method";
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Options.
|
|
|
|
|
* @type {Object.<string, *>}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.options = options || {};
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect.Service.Method.prototype
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
var MethodPrototype = Method.prototype = Object.create(T.prototype);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Builds the method's '$options' property.
|
|
|
|
|
* @name ProtoBuf.Reflect.Service.Method#buildOpt
|
|
|
|
|
* @function
|
|
|
|
|
* @return {Object.<string,*>}
|
|
|
|
|
*/
|
|
|
|
|
MethodPrototype.buildOpt = NamespacePrototype.buildOpt;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* @alias ProtoBuf.Reflect.Service.Method
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
Reflect.Service.Method = Method;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* RPC service method.
|
|
|
|
|
* @exports ProtoBuf.Reflect.Service.RPCMethod
|
|
|
|
|
* @param {!ProtoBuf.Builder} builder Builder reference
|
|
|
|
|
* @param {!ProtoBuf.Reflect.Service} svc Service
|
|
|
|
|
* @param {string} name Method name
|
|
|
|
|
* @param {string} request Request message name
|
|
|
|
|
* @param {string} response Response message name
|
|
|
|
|
* @param {boolean} request_stream Whether requests are streamed
|
|
|
|
|
* @param {boolean} response_stream Whether responses are streamed
|
|
|
|
|
* @param {Object.<string,*>=} options Options
|
|
|
|
|
* @constructor
|
|
|
|
|
* @extends ProtoBuf.Reflect.Service.Method
|
|
|
|
|
*/
|
|
|
|
|
var RPCMethod = function(builder, svc, name, request, response, request_stream, response_stream, options) {
|
|
|
|
|
Method.call(this, builder, svc, name, options);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* @override
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.className = "Service.RPCMethod";
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Request message name.
|
|
|
|
|
* @type {string}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
this.requestName = request;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Response message name.
|
|
|
|
|
* @type {string}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
this.responseName = response;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Whether requests are streamed
|
|
|
|
|
* @type {bool}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
this.requestStream = request_stream;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether responses are streamed
|
|
|
|
|
* @type {bool}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
this.responseStream = response_stream;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resolved request message type.
|
|
|
|
|
* @type {ProtoBuf.Reflect.Message}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
this.resolvedRequestType = null;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resolved response message type.
|
|
|
|
|
* @type {ProtoBuf.Reflect.Message}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
this.resolvedResponseType = null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Extends Method
|
|
|
|
|
RPCMethod.prototype = Object.create(Method.prototype);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Reflect.Service.RPCMethod
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Reflect.Service.RPCMethod = RPCMethod;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
return Reflect;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
})(ProtoBuf);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Builder
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.Builder = (function(ProtoBuf, Lang, Reflect) {
|
|
|
|
|
"use strict";
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Constructs a new Builder.
|
|
|
|
|
* @exports ProtoBuf.Builder
|
|
|
|
|
* @class Provides the functionality to build protocol messages.
|
|
|
|
|
* @param {Object.<string,*>=} options Options
|
|
|
|
|
* @constructor
|
|
|
|
|
*/
|
|
|
|
|
var Builder = function(options) {
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Namespace.
|
|
|
|
|
* @type {ProtoBuf.Reflect.Namespace}
|
|
|
|
|
* @expose
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.ns = new Reflect.Namespace(this, null, ""); // Global namespace
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Namespace pointer.
|
|
|
|
|
* @type {ProtoBuf.Reflect.T}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.ptr = this.ns;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Resolved flag.
|
|
|
|
|
* @type {boolean}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.resolved = false;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* The current building result.
|
|
|
|
|
* @type {Object.<string,ProtoBuf.Builder.Message|Object>|null}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.result = null;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Imported files.
|
|
|
|
|
* @type {Array.<string>}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.files = {};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Import root override.
|
|
|
|
|
* @type {?string}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.importRoot = null;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Options.
|
|
|
|
|
* @type {!Object.<string, *>}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
* @expose
|
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.options = options || {};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Builder.prototype
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
var BuilderPrototype = Builder.prototype;
|
|
|
|
|
|
|
|
|
|
// ----- Definition tests -----
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests if a definition most likely describes a message.
|
|
|
|
|
* @param {!Object} def
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Builder.isMessage = function(def) {
|
|
|
|
|
// Messages require a string name
|
|
|
|
|
if (typeof def["name"] !== 'string')
|
|
|
|
|
return false;
|
|
|
|
|
// Messages do not contain values (enum) or rpc methods (service)
|
|
|
|
|
if (typeof def["values"] !== 'undefined' || typeof def["rpc"] !== 'undefined')
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests if a definition most likely describes a message field.
|
|
|
|
|
* @param {!Object} def
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Builder.isMessageField = function(def) {
|
|
|
|
|
// Message fields require a string rule, name and type and an id
|
|
|
|
|
if (typeof def["rule"] !== 'string' || typeof def["name"] !== 'string' || typeof def["type"] !== 'string' || typeof def["id"] === 'undefined')
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests if a definition most likely describes an enum.
|
|
|
|
|
* @param {!Object} def
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Builder.isEnum = function(def) {
|
|
|
|
|
// Enums require a string name
|
|
|
|
|
if (typeof def["name"] !== 'string')
|
|
|
|
|
return false;
|
|
|
|
|
// Enums require at least one value
|
|
|
|
|
if (typeof def["values"] === 'undefined' || !Array.isArray(def["values"]) || def["values"].length === 0)
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests if a definition most likely describes a service.
|
|
|
|
|
* @param {!Object} def
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Builder.isService = function(def) {
|
|
|
|
|
// Services require a string name and an rpc object
|
|
|
|
|
if (typeof def["name"] !== 'string' || typeof def["rpc"] !== 'object' || !def["rpc"])
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests if a definition most likely describes an extended message
|
|
|
|
|
* @param {!Object} def
|
|
|
|
|
* @returns {boolean}
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
Builder.isExtend = function(def) {
|
|
|
|
|
// Extends rquire a string ref
|
|
|
|
|
if (typeof def["ref"] !== 'string')
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ----- Building -----
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resets the pointer to the root namespace.
|
|
|
|
|
* @returns {!ProtoBuf.Builder} this
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
BuilderPrototype.reset = function() {
|
|
|
|
|
this.ptr = this.ns;
|
|
|
|
|
return this;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Defines a namespace on top of the current pointer position and places the pointer on it.
|
|
|
|
|
* @param {string} namespace
|
|
|
|
|
* @return {!ProtoBuf.Builder} this
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
BuilderPrototype.define = function(namespace) {
|
|
|
|
|
if (typeof namespace !== 'string' || !Lang.TYPEREF.test(namespace))
|
|
|
|
|
throw Error("illegal namespace: "+namespace);
|
|
|
|
|
namespace.split(".").forEach(function(part) {
|
|
|
|
|
var ns = this.ptr.getChild(part);
|
|
|
|
|
if (ns === null) // Keep existing
|
|
|
|
|
this.ptr.addChild(ns = new Reflect.Namespace(this, this.ptr, part));
|
|
|
|
|
this.ptr = ns;
|
|
|
|
|
}, this);
|
|
|
|
|
return this;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates the specified definitions at the current pointer position.
|
|
|
|
|
* @param {!Array.<!Object>} defs Messages, enums or services to create
|
|
|
|
|
* @returns {!ProtoBuf.Builder} this
|
|
|
|
|
* @throws {Error} If a message definition is invalid
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
BuilderPrototype.create = function(defs) {
|
|
|
|
|
if (!defs)
|
|
|
|
|
return this; // Nothing to create
|
|
|
|
|
if (!Array.isArray(defs))
|
|
|
|
|
defs = [defs];
|
|
|
|
|
else {
|
|
|
|
|
if (defs.length === 0)
|
2014-10-29 23:04:51 +00:00
|
|
|
|
return this;
|
2020-02-07 19:37:04 +00:00
|
|
|
|
defs = defs.slice();
|
|
|
|
|
}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// It's quite hard to keep track of scopes and memory here, so let's do this iteratively.
|
|
|
|
|
var stack = [defs];
|
|
|
|
|
while (stack.length > 0) {
|
|
|
|
|
defs = stack.pop();
|
|
|
|
|
|
|
|
|
|
if (!Array.isArray(defs)) // Stack always contains entire namespaces
|
|
|
|
|
throw Error("not a valid namespace: "+JSON.stringify(defs));
|
|
|
|
|
|
|
|
|
|
while (defs.length > 0) {
|
|
|
|
|
var def = defs.shift(); // Namespaces always contain an array of messages, enums and services
|
|
|
|
|
|
|
|
|
|
if (Builder.isMessage(def)) {
|
|
|
|
|
var obj = new Reflect.Message(this, this.ptr, def["name"], def["options"], def["isGroup"], def["syntax"]);
|
|
|
|
|
|
|
|
|
|
// Create OneOfs
|
|
|
|
|
var oneofs = {};
|
|
|
|
|
if (def["oneofs"])
|
|
|
|
|
Object.keys(def["oneofs"]).forEach(function(name) {
|
|
|
|
|
obj.addChild(oneofs[name] = new Reflect.Message.OneOf(this, obj, name));
|
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
|
|
// Create fields
|
|
|
|
|
if (def["fields"])
|
|
|
|
|
def["fields"].forEach(function(fld) {
|
|
|
|
|
if (obj.getChild(fld["id"]|0) !== null)
|
|
|
|
|
throw Error("duplicate or invalid field id in "+obj.name+": "+fld['id']);
|
|
|
|
|
if (fld["options"] && typeof fld["options"] !== 'object')
|
|
|
|
|
throw Error("illegal field options in "+obj.name+"#"+fld["name"]);
|
|
|
|
|
var oneof = null;
|
|
|
|
|
if (typeof fld["oneof"] === 'string' && !(oneof = oneofs[fld["oneof"]]))
|
|
|
|
|
throw Error("illegal oneof in "+obj.name+"#"+fld["name"]+": "+fld["oneof"]);
|
|
|
|
|
fld = new Reflect.Message.Field(this, obj, fld["rule"], fld["keytype"], fld["type"], fld["name"], fld["id"], fld["options"], oneof, def["syntax"]);
|
|
|
|
|
if (oneof)
|
|
|
|
|
oneof.fields.push(fld);
|
|
|
|
|
obj.addChild(fld);
|
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
|
|
// Push children to stack
|
|
|
|
|
var subObj = [];
|
|
|
|
|
if (def["enums"])
|
|
|
|
|
def["enums"].forEach(function(enm) {
|
|
|
|
|
subObj.push(enm);
|
|
|
|
|
});
|
|
|
|
|
if (def["messages"])
|
|
|
|
|
def["messages"].forEach(function(msg) {
|
|
|
|
|
subObj.push(msg);
|
|
|
|
|
});
|
|
|
|
|
if (def["services"])
|
|
|
|
|
def["services"].forEach(function(svc) {
|
|
|
|
|
subObj.push(svc);
|
|
|
|
|
});
|
|
|
|
|
|
2020-07-10 17:48:42 +00:00
|
|
|
|
// Set extension ranges
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (def["extensions"]) {
|
2020-07-10 17:48:42 +00:00
|
|
|
|
if (typeof def["extensions"][0] === 'number') // pre 5.0.1
|
|
|
|
|
obj.extensions = [ def["extensions"] ];
|
|
|
|
|
else
|
|
|
|
|
obj.extensions = def["extensions"];
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
|
|
|
|
|
// Create on top of current namespace
|
|
|
|
|
this.ptr.addChild(obj);
|
|
|
|
|
if (subObj.length > 0) {
|
|
|
|
|
stack.push(defs); // Push the current level back
|
|
|
|
|
defs = subObj; // Continue processing sub level
|
|
|
|
|
subObj = null;
|
|
|
|
|
this.ptr = obj; // And move the pointer to this namespace
|
|
|
|
|
obj = null;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
subObj = null;
|
|
|
|
|
|
|
|
|
|
} else if (Builder.isEnum(def)) {
|
|
|
|
|
|
|
|
|
|
obj = new Reflect.Enum(this, this.ptr, def["name"], def["options"], def["syntax"]);
|
|
|
|
|
def["values"].forEach(function(val) {
|
|
|
|
|
obj.addChild(new Reflect.Enum.Value(this, obj, val["name"], val["id"]));
|
|
|
|
|
}, this);
|
|
|
|
|
this.ptr.addChild(obj);
|
|
|
|
|
|
|
|
|
|
} else if (Builder.isService(def)) {
|
|
|
|
|
|
|
|
|
|
obj = new Reflect.Service(this, this.ptr, def["name"], def["options"]);
|
|
|
|
|
Object.keys(def["rpc"]).forEach(function(name) {
|
|
|
|
|
var mtd = def["rpc"][name];
|
|
|
|
|
obj.addChild(new Reflect.Service.RPCMethod(this, obj, name, mtd["request"], mtd["response"], !!mtd["request_stream"], !!mtd["response_stream"], mtd["options"]));
|
|
|
|
|
}, this);
|
|
|
|
|
this.ptr.addChild(obj);
|
|
|
|
|
|
|
|
|
|
} else if (Builder.isExtend(def)) {
|
|
|
|
|
|
|
|
|
|
obj = this.ptr.resolve(def["ref"], true);
|
|
|
|
|
if (obj) {
|
|
|
|
|
def["fields"].forEach(function(fld) {
|
|
|
|
|
if (obj.getChild(fld['id']|0) !== null)
|
|
|
|
|
throw Error("duplicate extended field id in "+obj.name+": "+fld['id']);
|
2020-07-10 17:48:42 +00:00
|
|
|
|
// Check if field id is allowed to be extended
|
|
|
|
|
if (obj.extensions) {
|
|
|
|
|
var valid = false;
|
|
|
|
|
obj.extensions.forEach(function(range) {
|
|
|
|
|
if (fld["id"] >= range[0] && fld["id"] <= range[1])
|
|
|
|
|
valid = true;
|
|
|
|
|
});
|
|
|
|
|
if (!valid)
|
|
|
|
|
throw Error("illegal extended field id in "+obj.name+": "+fld['id']+" (not within valid ranges)");
|
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Convert extension field names to camel case notation if the override is set
|
|
|
|
|
var name = fld["name"];
|
|
|
|
|
if (this.options['convertFieldsToCamelCase'])
|
|
|
|
|
name = ProtoBuf.Util.toCamelCase(name);
|
|
|
|
|
// see #161: Extensions use their fully qualified name as their runtime key and...
|
|
|
|
|
var field = new Reflect.Message.ExtensionField(this, obj, fld["rule"], fld["type"], this.ptr.fqn()+'.'+name, fld["id"], fld["options"]);
|
|
|
|
|
// ...are added on top of the current namespace as an extension which is used for
|
|
|
|
|
// resolving their type later on (the extension always keeps the original name to
|
|
|
|
|
// prevent naming collisions)
|
|
|
|
|
var ext = new Reflect.Extension(this, this.ptr, fld["name"], field);
|
|
|
|
|
field.extension = ext;
|
|
|
|
|
this.ptr.addChild(ext);
|
|
|
|
|
obj.addChild(field);
|
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
|
|
} else if (!/\.?google\.protobuf\./.test(def["ref"])) // Silently skip internal extensions
|
|
|
|
|
throw Error("extended message "+def["ref"]+" is not defined");
|
|
|
|
|
|
2014-10-29 23:04:51 +00:00
|
|
|
|
} else
|
2020-02-07 19:37:04 +00:00
|
|
|
|
throw Error("not a valid definition: "+JSON.stringify(def));
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
def = null;
|
|
|
|
|
obj = null;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Break goes here
|
|
|
|
|
defs = null;
|
|
|
|
|
this.ptr = this.ptr.parent; // Namespace done, continue at parent
|
|
|
|
|
}
|
|
|
|
|
this.resolved = false; // Require re-resolve
|
|
|
|
|
this.result = null; // Require re-build
|
|
|
|
|
return this;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Propagates syntax to all children.
|
|
|
|
|
* @param {!Object} parent
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
function propagateSyntax(parent) {
|
|
|
|
|
if (parent['messages']) {
|
|
|
|
|
parent['messages'].forEach(function(child) {
|
|
|
|
|
child["syntax"] = parent["syntax"];
|
|
|
|
|
propagateSyntax(child);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (parent['enums']) {
|
|
|
|
|
parent['enums'].forEach(function(child) {
|
|
|
|
|
child["syntax"] = parent["syntax"];
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Imports another definition into this builder.
|
|
|
|
|
* @param {Object.<string,*>} json Parsed import
|
|
|
|
|
* @param {(string|{root: string, file: string})=} filename Imported file name
|
|
|
|
|
* @returns {!ProtoBuf.Builder} this
|
|
|
|
|
* @throws {Error} If the definition or file cannot be imported
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
BuilderPrototype["import"] = function(json, filename) {
|
|
|
|
|
var delim = '/';
|
|
|
|
|
|
|
|
|
|
// Make sure to skip duplicate imports
|
|
|
|
|
|
|
|
|
|
if (typeof filename === 'string') {
|
|
|
|
|
|
|
|
|
|
if (ProtoBuf.Util.IS_NODE)
|
|
|
|
|
filename = require("path")['resolve'](filename);
|
|
|
|
|
if (this.files[filename] === true)
|
|
|
|
|
return this.reset();
|
|
|
|
|
this.files[filename] = true;
|
|
|
|
|
|
|
|
|
|
} else if (typeof filename === 'object') { // Object with root, file.
|
|
|
|
|
|
|
|
|
|
var root = filename.root;
|
|
|
|
|
if (ProtoBuf.Util.IS_NODE)
|
|
|
|
|
root = require("path")['resolve'](root);
|
|
|
|
|
if (root.indexOf("\\") >= 0 || filename.file.indexOf("\\") >= 0)
|
|
|
|
|
delim = '\\';
|
|
|
|
|
var fname = root + delim + filename.file;
|
|
|
|
|
if (this.files[fname] === true)
|
|
|
|
|
return this.reset();
|
|
|
|
|
this.files[fname] = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Import imports
|
|
|
|
|
|
|
|
|
|
if (json['imports'] && json['imports'].length > 0) {
|
|
|
|
|
var importRoot,
|
|
|
|
|
resetRoot = false;
|
|
|
|
|
|
|
|
|
|
if (typeof filename === 'object') { // If an import root is specified, override
|
|
|
|
|
|
|
|
|
|
this.importRoot = filename["root"]; resetRoot = true; // ... and reset afterwards
|
|
|
|
|
importRoot = this.importRoot;
|
|
|
|
|
filename = filename["file"];
|
|
|
|
|
if (importRoot.indexOf("\\") >= 0 || filename.indexOf("\\") >= 0)
|
|
|
|
|
delim = '\\';
|
|
|
|
|
|
|
|
|
|
} else if (typeof filename === 'string') {
|
|
|
|
|
|
|
|
|
|
if (this.importRoot) // If import root is overridden, use it
|
2014-10-29 23:04:51 +00:00
|
|
|
|
importRoot = this.importRoot;
|
2020-02-07 19:37:04 +00:00
|
|
|
|
else { // Otherwise compute from filename
|
|
|
|
|
if (filename.indexOf("/") >= 0) { // Unix
|
|
|
|
|
importRoot = filename.replace(/\/[^\/]*$/, "");
|
|
|
|
|
if (/* /file.proto */ importRoot === "")
|
|
|
|
|
importRoot = "/";
|
|
|
|
|
} else if (filename.indexOf("\\") >= 0) { // Windows
|
|
|
|
|
importRoot = filename.replace(/\\[^\\]*$/, "");
|
|
|
|
|
delim = '\\';
|
|
|
|
|
} else
|
|
|
|
|
importRoot = ".";
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
|
|
|
|
|
} else
|
|
|
|
|
importRoot = null;
|
|
|
|
|
|
|
|
|
|
for (var i=0; i<json['imports'].length; i++) {
|
|
|
|
|
if (typeof json['imports'][i] === 'string') { // Import file
|
|
|
|
|
if (!importRoot)
|
|
|
|
|
throw Error("cannot determine import root");
|
|
|
|
|
var importFilename = json['imports'][i];
|
|
|
|
|
if (importFilename === "google/protobuf/descriptor.proto")
|
|
|
|
|
continue; // Not needed and therefore not used
|
|
|
|
|
importFilename = importRoot + delim + importFilename;
|
|
|
|
|
if (this.files[importFilename] === true)
|
|
|
|
|
continue; // Already imported
|
|
|
|
|
if (/\.proto$/i.test(importFilename) && !ProtoBuf.DotProto) // If this is a light build
|
|
|
|
|
importFilename = importFilename.replace(/\.proto$/, ".json"); // always load the JSON file
|
|
|
|
|
var contents = ProtoBuf.Util.fetch(importFilename);
|
|
|
|
|
if (contents === null)
|
|
|
|
|
throw Error("failed to import '"+importFilename+"' in '"+filename+"': file not found");
|
|
|
|
|
if (/\.json$/i.test(importFilename)) // Always possible
|
|
|
|
|
this["import"](JSON.parse(contents+""), importFilename); // May throw
|
|
|
|
|
else
|
|
|
|
|
this["import"](ProtoBuf.DotProto.Parser.parse(contents), importFilename); // May throw
|
|
|
|
|
} else // Import structure
|
|
|
|
|
if (!filename)
|
|
|
|
|
this["import"](json['imports'][i]);
|
|
|
|
|
else if (/\.(\w+)$/.test(filename)) // With extension: Append _importN to the name portion to make it unique
|
|
|
|
|
this["import"](json['imports'][i], filename.replace(/^(.+)\.(\w+)$/, function($0, $1, $2) { return $1+"_import"+i+"."+$2; }));
|
|
|
|
|
else // Without extension: Append _importN to make it unique
|
|
|
|
|
this["import"](json['imports'][i], filename+"_import"+i);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (resetRoot) // Reset import root override when all imports are done
|
|
|
|
|
this.importRoot = null;
|
|
|
|
|
}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
// Import structures
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
if (json['package'])
|
|
|
|
|
this.define(json['package']);
|
|
|
|
|
if (json['syntax'])
|
|
|
|
|
propagateSyntax(json);
|
|
|
|
|
var base = this.ptr;
|
|
|
|
|
if (json['options'])
|
|
|
|
|
Object.keys(json['options']).forEach(function(key) {
|
|
|
|
|
base.options[key] = json['options'][key];
|
|
|
|
|
});
|
|
|
|
|
if (json['messages'])
|
|
|
|
|
this.create(json['messages']),
|
|
|
|
|
this.ptr = base;
|
|
|
|
|
if (json['enums'])
|
|
|
|
|
this.create(json['enums']),
|
|
|
|
|
this.ptr = base;
|
|
|
|
|
if (json['services'])
|
|
|
|
|
this.create(json['services']),
|
|
|
|
|
this.ptr = base;
|
|
|
|
|
if (json['extends'])
|
|
|
|
|
this.create(json['extends']);
|
|
|
|
|
|
|
|
|
|
return this.reset();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resolves all namespace objects.
|
|
|
|
|
* @throws {Error} If a type cannot be resolved
|
|
|
|
|
* @returns {!ProtoBuf.Builder} this
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
BuilderPrototype.resolveAll = function() {
|
|
|
|
|
// Resolve all reflected objects
|
|
|
|
|
var res;
|
|
|
|
|
if (this.ptr == null || typeof this.ptr.type === 'object')
|
|
|
|
|
return this; // Done (already resolved)
|
|
|
|
|
|
|
|
|
|
if (this.ptr instanceof Reflect.Namespace) { // Resolve children
|
|
|
|
|
|
|
|
|
|
this.ptr.children.forEach(function(child) {
|
|
|
|
|
this.ptr = child;
|
|
|
|
|
this.resolveAll();
|
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
|
|
} else if (this.ptr instanceof Reflect.Message.Field) { // Resolve type
|
|
|
|
|
|
|
|
|
|
if (!Lang.TYPE.test(this.ptr.type)) {
|
|
|
|
|
if (!Lang.TYPEREF.test(this.ptr.type))
|
|
|
|
|
throw Error("illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.type);
|
|
|
|
|
res = (this.ptr instanceof Reflect.Message.ExtensionField ? this.ptr.extension.parent : this.ptr.parent).resolve(this.ptr.type, true);
|
|
|
|
|
if (!res)
|
|
|
|
|
throw Error("unresolvable type reference in "+this.ptr.toString(true)+": "+this.ptr.type);
|
|
|
|
|
this.ptr.resolvedType = res;
|
|
|
|
|
if (res instanceof Reflect.Enum) {
|
|
|
|
|
this.ptr.type = ProtoBuf.TYPES["enum"];
|
|
|
|
|
if (this.ptr.syntax === 'proto3' && res.syntax !== 'proto3')
|
|
|
|
|
throw Error("proto3 message cannot reference proto2 enum");
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
else if (res instanceof Reflect.Message)
|
|
|
|
|
this.ptr.type = res.isGroup ? ProtoBuf.TYPES["group"] : ProtoBuf.TYPES["message"];
|
|
|
|
|
else
|
|
|
|
|
throw Error("illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.type);
|
|
|
|
|
} else
|
|
|
|
|
this.ptr.type = ProtoBuf.TYPES[this.ptr.type];
|
|
|
|
|
|
|
|
|
|
// If it's a map field, also resolve the key type. The key type can be only a numeric, string, or bool type
|
|
|
|
|
// (i.e., no enums or messages), so we don't need to resolve against the current namespace.
|
|
|
|
|
if (this.ptr.map) {
|
|
|
|
|
if (!Lang.TYPE.test(this.ptr.keyType))
|
|
|
|
|
throw Error("illegal key type for map field in "+this.ptr.toString(true)+": "+this.ptr.keyType);
|
|
|
|
|
this.ptr.keyType = ProtoBuf.TYPES[this.ptr.keyType];
|
2014-10-29 23:04:51 +00:00
|
|
|
|
}
|
2020-02-07 19:37:04 +00:00
|
|
|
|
|
|
|
|
|
} else if (this.ptr instanceof ProtoBuf.Reflect.Service.Method) {
|
|
|
|
|
|
|
|
|
|
if (this.ptr instanceof ProtoBuf.Reflect.Service.RPCMethod) {
|
|
|
|
|
res = this.ptr.parent.resolve(this.ptr.requestName, true);
|
|
|
|
|
if (!res || !(res instanceof ProtoBuf.Reflect.Message))
|
|
|
|
|
throw Error("Illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.requestName);
|
|
|
|
|
this.ptr.resolvedRequestType = res;
|
|
|
|
|
res = this.ptr.parent.resolve(this.ptr.responseName, true);
|
|
|
|
|
if (!res || !(res instanceof ProtoBuf.Reflect.Message))
|
|
|
|
|
throw Error("Illegal type reference in "+this.ptr.toString(true)+": "+this.ptr.responseName);
|
|
|
|
|
this.ptr.resolvedResponseType = res;
|
|
|
|
|
} else // Should not happen as nothing else is implemented
|
|
|
|
|
throw Error("illegal service type in "+this.ptr.toString(true));
|
|
|
|
|
|
|
|
|
|
} else if (
|
|
|
|
|
!(this.ptr instanceof ProtoBuf.Reflect.Message.OneOf) && // Not built
|
|
|
|
|
!(this.ptr instanceof ProtoBuf.Reflect.Extension) && // Not built
|
|
|
|
|
!(this.ptr instanceof ProtoBuf.Reflect.Enum.Value) // Built in enum
|
|
|
|
|
)
|
|
|
|
|
throw Error("illegal object in namespace: "+typeof(this.ptr)+": "+this.ptr);
|
|
|
|
|
|
|
|
|
|
return this.reset();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Builds the protocol. This will first try to resolve all definitions and, if this has been successful,
|
|
|
|
|
* return the built package.
|
|
|
|
|
* @param {(string|Array.<string>)=} path Specifies what to return. If omitted, the entire namespace will be returned.
|
|
|
|
|
* @returns {!ProtoBuf.Builder.Message|!Object.<string,*>}
|
|
|
|
|
* @throws {Error} If a type could not be resolved
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
BuilderPrototype.build = function(path) {
|
|
|
|
|
this.reset();
|
|
|
|
|
if (!this.resolved)
|
|
|
|
|
this.resolveAll(),
|
|
|
|
|
this.resolved = true,
|
|
|
|
|
this.result = null; // Require re-build
|
|
|
|
|
if (this.result === null) // (Re-)Build
|
|
|
|
|
this.result = this.ns.build();
|
|
|
|
|
if (!path)
|
|
|
|
|
return this.result;
|
|
|
|
|
var part = typeof path === 'string' ? path.split(".") : path,
|
|
|
|
|
ptr = this.result; // Build namespace pointer (no hasChild etc.)
|
|
|
|
|
for (var i=0; i<part.length; i++)
|
|
|
|
|
if (ptr[part[i]])
|
|
|
|
|
ptr = ptr[part[i]];
|
|
|
|
|
else {
|
|
|
|
|
ptr = null;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return ptr;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Similar to {@link ProtoBuf.Builder#build}, but looks up the internal reflection descriptor.
|
|
|
|
|
* @param {string=} path Specifies what to return. If omitted, the entire namespace wiil be returned.
|
|
|
|
|
* @param {boolean=} excludeNonNamespace Excludes non-namespace types like fields, defaults to `false`
|
|
|
|
|
* @returns {?ProtoBuf.Reflect.T} Reflection descriptor or `null` if not found
|
|
|
|
|
*/
|
|
|
|
|
BuilderPrototype.lookup = function(path, excludeNonNamespace) {
|
|
|
|
|
return path ? this.ns.resolve(path, excludeNonNamespace) : this.ns;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a string representation of this object.
|
|
|
|
|
* @return {string} String representation as of "Builder"
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
BuilderPrototype.toString = function() {
|
|
|
|
|
return "Builder";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ----- Base classes -----
|
|
|
|
|
// Exist for the sole purpose of being able to "... instanceof ProtoBuf.Builder.Message" etc.
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Builder.Message
|
|
|
|
|
*/
|
|
|
|
|
Builder.Message = function() {};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Builder.Enum
|
|
|
|
|
*/
|
|
|
|
|
Builder.Enum = function() {};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Builder.Message
|
|
|
|
|
*/
|
|
|
|
|
Builder.Service = function() {};
|
|
|
|
|
|
|
|
|
|
return Builder;
|
|
|
|
|
|
|
|
|
|
})(ProtoBuf, ProtoBuf.Lang, ProtoBuf.Reflect);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @alias ProtoBuf.Map
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.Map = (function(ProtoBuf, Reflect) {
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a new Map. A Map is a container that is used to implement map
|
|
|
|
|
* fields on message objects. It closely follows the ES6 Map API; however,
|
|
|
|
|
* it is distinct because we do not want to depend on external polyfills or
|
|
|
|
|
* on ES6 itself.
|
|
|
|
|
*
|
|
|
|
|
* @exports ProtoBuf.Map
|
|
|
|
|
* @param {!ProtoBuf.Reflect.Field} field Map field
|
|
|
|
|
* @param {Object.<string,*>=} contents Initial contents
|
|
|
|
|
* @constructor
|
|
|
|
|
*/
|
|
|
|
|
var Map = function(field, contents) {
|
|
|
|
|
if (!field.map)
|
|
|
|
|
throw Error("field is not a map");
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* The field corresponding to this map.
|
|
|
|
|
* @type {!ProtoBuf.Reflect.Field}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.field = field;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Element instance corresponding to key type.
|
|
|
|
|
* @type {!ProtoBuf.Reflect.Element}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.keyElem = new Reflect.Element(field.keyType, null, true, field.syntax);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Element instance corresponding to value type.
|
|
|
|
|
* @type {!ProtoBuf.Reflect.Element}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.valueElem = new Reflect.Element(field.type, field.resolvedType, false, field.syntax);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Internal map: stores mapping of (string form of key) -> (key, value)
|
|
|
|
|
* pair.
|
|
|
|
|
*
|
|
|
|
|
* We provide map semantics for arbitrary key types, but we build on top
|
|
|
|
|
* of an Object, which has only string keys. In order to avoid the need
|
|
|
|
|
* to convert a string key back to its native type in many situations,
|
|
|
|
|
* we store the native key value alongside the value. Thus, we only need
|
|
|
|
|
* a one-way mapping from a key type to its string form that guarantees
|
|
|
|
|
* uniqueness and equality (i.e., str(K1) === str(K2) if and only if K1
|
|
|
|
|
* === K2).
|
|
|
|
|
*
|
|
|
|
|
* @type {!Object<string, {key: *, value: *}>}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
this.map = {};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns the number of elements in the map.
|
|
|
|
|
*/
|
|
|
|
|
Object.defineProperty(this, "size", {
|
|
|
|
|
get: function() { return Object.keys(this.map).length; }
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Fill initial contents from a raw object.
|
|
|
|
|
if (contents) {
|
|
|
|
|
var keys = Object.keys(contents);
|
|
|
|
|
for (var i = 0; i < keys.length; i++) {
|
|
|
|
|
var key = this.keyElem.valueFromString(keys[i]);
|
|
|
|
|
var val = this.valueElem.verifyValue(contents[keys[i]]);
|
|
|
|
|
this.map[this.keyElem.valueToString(key)] =
|
|
|
|
|
{ key: key, value: val };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
var MapPrototype = Map.prototype;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Helper: return an iterator over an array.
|
|
|
|
|
* @param {!Array<*>} arr the array
|
|
|
|
|
* @returns {!Object} an iterator
|
|
|
|
|
* @inner
|
|
|
|
|
*/
|
|
|
|
|
function arrayIterator(arr) {
|
|
|
|
|
var idx = 0;
|
|
|
|
|
return {
|
|
|
|
|
next: function() {
|
|
|
|
|
if (idx < arr.length)
|
|
|
|
|
return { done: false, value: arr[idx++] };
|
|
|
|
|
return { done: true };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Clears the map.
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
MapPrototype.clear = function() {
|
|
|
|
|
this.map = {};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Deletes a particular key from the map.
|
|
|
|
|
* @returns {boolean} Whether any entry with this key was deleted.
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
MapPrototype["delete"] = function(key) {
|
|
|
|
|
var keyValue = this.keyElem.valueToString(this.keyElem.verifyValue(key));
|
|
|
|
|
var hadKey = keyValue in this.map;
|
|
|
|
|
delete this.map[keyValue];
|
|
|
|
|
return hadKey;
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Returns an iterator over [key, value] pairs in the map.
|
|
|
|
|
* @returns {Object} The iterator
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
MapPrototype.entries = function() {
|
|
|
|
|
var entries = [];
|
|
|
|
|
var strKeys = Object.keys(this.map);
|
|
|
|
|
for (var i = 0, entry; i < strKeys.length; i++)
|
|
|
|
|
entries.push([(entry=this.map[strKeys[i]]).key, entry.value]);
|
|
|
|
|
return arrayIterator(entries);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Returns an iterator over keys in the map.
|
|
|
|
|
* @returns {Object} The iterator
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
MapPrototype.keys = function() {
|
|
|
|
|
var keys = [];
|
|
|
|
|
var strKeys = Object.keys(this.map);
|
|
|
|
|
for (var i = 0; i < strKeys.length; i++)
|
|
|
|
|
keys.push(this.map[strKeys[i]].key);
|
|
|
|
|
return arrayIterator(keys);
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Returns an iterator over values in the map.
|
|
|
|
|
* @returns {!Object} The iterator
|
|
|
|
|
*/
|
|
|
|
|
MapPrototype.values = function() {
|
|
|
|
|
var values = [];
|
|
|
|
|
var strKeys = Object.keys(this.map);
|
|
|
|
|
for (var i = 0; i < strKeys.length; i++)
|
|
|
|
|
values.push(this.map[strKeys[i]].value);
|
|
|
|
|
return arrayIterator(values);
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Iterates over entries in the map, calling a function on each.
|
|
|
|
|
* @param {function(this:*, *, *, *)} cb The callback to invoke with value, key, and map arguments.
|
|
|
|
|
* @param {Object=} thisArg The `this` value for the callback
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
MapPrototype.forEach = function(cb, thisArg) {
|
|
|
|
|
var strKeys = Object.keys(this.map);
|
|
|
|
|
for (var i = 0, entry; i < strKeys.length; i++)
|
|
|
|
|
cb.call(thisArg, (entry=this.map[strKeys[i]]).value, entry.key, this);
|
2014-10-29 23:04:51 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Sets a key in the map to the given value.
|
|
|
|
|
* @param {*} key The key
|
|
|
|
|
* @param {*} value The value
|
|
|
|
|
* @returns {!ProtoBuf.Map} The map instance
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
MapPrototype.set = function(key, value) {
|
|
|
|
|
var keyValue = this.keyElem.verifyValue(key);
|
|
|
|
|
var valValue = this.valueElem.verifyValue(value);
|
|
|
|
|
this.map[this.keyElem.valueToString(keyValue)] =
|
|
|
|
|
{ key: keyValue, value: valValue };
|
|
|
|
|
return this;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2020-02-07 19:37:04 +00:00
|
|
|
|
* Gets the value corresponding to a key in the map.
|
|
|
|
|
* @param {*} key The key
|
|
|
|
|
* @returns {*|undefined} The value, or `undefined` if key not present
|
2014-10-29 23:04:51 +00:00
|
|
|
|
*/
|
2020-02-07 19:37:04 +00:00
|
|
|
|
MapPrototype.get = function(key) {
|
|
|
|
|
var keyValue = this.keyElem.valueToString(this.keyElem.verifyValue(key));
|
|
|
|
|
if (!(keyValue in this.map))
|
|
|
|
|
return undefined;
|
|
|
|
|
return this.map[keyValue].value;
|
2014-10-29 23:04:51 +00:00
|
|
|
|
};
|
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
/**
|
|
|
|
|
* Determines whether the given key is present in the map.
|
|
|
|
|
* @param {*} key The key
|
|
|
|
|
* @returns {boolean} `true` if the key is present
|
|
|
|
|
*/
|
|
|
|
|
MapPrototype.has = function(key) {
|
|
|
|
|
var keyValue = this.keyElem.valueToString(this.keyElem.verifyValue(key));
|
|
|
|
|
return (keyValue in this.map);
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
return Map;
|
|
|
|
|
})(ProtoBuf, ProtoBuf.Reflect);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Loads a .proto string and returns the Builder.
|
|
|
|
|
* @param {string} proto .proto file contents
|
|
|
|
|
* @param {(ProtoBuf.Builder|string|{root: string, file: string})=} builder Builder to append to. Will create a new one if omitted.
|
|
|
|
|
* @param {(string|{root: string, file: string})=} filename The corresponding file name if known. Must be specified for imports.
|
|
|
|
|
* @return {ProtoBuf.Builder} Builder to create new messages
|
|
|
|
|
* @throws {Error} If the definition cannot be parsed or built
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.loadProto = function(proto, builder, filename) {
|
|
|
|
|
if (typeof builder === 'string' || (builder && typeof builder["file"] === 'string' && typeof builder["root"] === 'string'))
|
|
|
|
|
filename = builder,
|
|
|
|
|
builder = undefined;
|
|
|
|
|
return ProtoBuf.loadJson(ProtoBuf.DotProto.Parser.parse(proto), builder, filename);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Loads a .proto string and returns the Builder. This is an alias of {@link ProtoBuf.loadProto}.
|
|
|
|
|
* @function
|
|
|
|
|
* @param {string} proto .proto file contents
|
|
|
|
|
* @param {(ProtoBuf.Builder|string)=} builder Builder to append to. Will create a new one if omitted.
|
|
|
|
|
* @param {(string|{root: string, file: string})=} filename The corresponding file name if known. Must be specified for imports.
|
|
|
|
|
* @return {ProtoBuf.Builder} Builder to create new messages
|
|
|
|
|
* @throws {Error} If the definition cannot be parsed or built
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.protoFromString = ProtoBuf.loadProto; // Legacy
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Loads a .proto file and returns the Builder.
|
|
|
|
|
* @param {string|{root: string, file: string}} filename Path to proto file or an object specifying 'file' with
|
|
|
|
|
* an overridden 'root' path for all imported files.
|
|
|
|
|
* @param {function(?Error, !ProtoBuf.Builder=)=} callback Callback that will receive `null` as the first and
|
|
|
|
|
* the Builder as its second argument on success, otherwise the error as its first argument. If omitted, the
|
|
|
|
|
* file will be read synchronously and this function will return the Builder.
|
|
|
|
|
* @param {ProtoBuf.Builder=} builder Builder to append to. Will create a new one if omitted.
|
|
|
|
|
* @return {?ProtoBuf.Builder|undefined} The Builder if synchronous (no callback specified, will be NULL if the
|
|
|
|
|
* request has failed), else undefined
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.loadProtoFile = function(filename, callback, builder) {
|
|
|
|
|
if (callback && typeof callback === 'object')
|
|
|
|
|
builder = callback,
|
|
|
|
|
callback = null;
|
|
|
|
|
else if (!callback || typeof callback !== 'function')
|
|
|
|
|
callback = null;
|
|
|
|
|
if (callback)
|
|
|
|
|
return ProtoBuf.Util.fetch(typeof filename === 'string' ? filename : filename["root"]+"/"+filename["file"], function(contents) {
|
|
|
|
|
if (contents === null) {
|
|
|
|
|
callback(Error("Failed to fetch file"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
callback(null, ProtoBuf.loadProto(contents, builder, filename));
|
|
|
|
|
} catch (e) {
|
|
|
|
|
callback(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
var contents = ProtoBuf.Util.fetch(typeof filename === 'object' ? filename["root"]+"/"+filename["file"] : filename);
|
|
|
|
|
return contents === null ? null : ProtoBuf.loadProto(contents, builder, filename);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Loads a .proto file and returns the Builder. This is an alias of {@link ProtoBuf.loadProtoFile}.
|
|
|
|
|
* @function
|
|
|
|
|
* @param {string|{root: string, file: string}} filename Path to proto file or an object specifying 'file' with
|
|
|
|
|
* an overridden 'root' path for all imported files.
|
|
|
|
|
* @param {function(?Error, !ProtoBuf.Builder=)=} callback Callback that will receive `null` as the first and
|
|
|
|
|
* the Builder as its second argument on success, otherwise the error as its first argument. If omitted, the
|
|
|
|
|
* file will be read synchronously and this function will return the Builder.
|
|
|
|
|
* @param {ProtoBuf.Builder=} builder Builder to append to. Will create a new one if omitted.
|
|
|
|
|
* @return {!ProtoBuf.Builder|undefined} The Builder if synchronous (no callback specified, will be NULL if the
|
|
|
|
|
* request has failed), else undefined
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.protoFromFile = ProtoBuf.loadProtoFile; // Legacy
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a new empty Builder.
|
|
|
|
|
* @param {Object.<string,*>=} options Builder options, defaults to global options set on ProtoBuf
|
|
|
|
|
* @return {!ProtoBuf.Builder} Builder
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.newBuilder = function(options) {
|
|
|
|
|
options = options || {};
|
|
|
|
|
if (typeof options['convertFieldsToCamelCase'] === 'undefined')
|
|
|
|
|
options['convertFieldsToCamelCase'] = ProtoBuf.convertFieldsToCamelCase;
|
|
|
|
|
if (typeof options['populateAccessors'] === 'undefined')
|
|
|
|
|
options['populateAccessors'] = ProtoBuf.populateAccessors;
|
|
|
|
|
return new ProtoBuf.Builder(options);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Loads a .json definition and returns the Builder.
|
|
|
|
|
* @param {!*|string} json JSON definition
|
|
|
|
|
* @param {(ProtoBuf.Builder|string|{root: string, file: string})=} builder Builder to append to. Will create a new one if omitted.
|
|
|
|
|
* @param {(string|{root: string, file: string})=} filename The corresponding file name if known. Must be specified for imports.
|
|
|
|
|
* @return {ProtoBuf.Builder} Builder to create new messages
|
|
|
|
|
* @throws {Error} If the definition cannot be parsed or built
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.loadJson = function(json, builder, filename) {
|
|
|
|
|
if (typeof builder === 'string' || (builder && typeof builder["file"] === 'string' && typeof builder["root"] === 'string'))
|
|
|
|
|
filename = builder,
|
|
|
|
|
builder = null;
|
|
|
|
|
if (!builder || typeof builder !== 'object')
|
|
|
|
|
builder = ProtoBuf.newBuilder();
|
|
|
|
|
if (typeof json === 'string')
|
|
|
|
|
json = JSON.parse(json);
|
|
|
|
|
builder["import"](json, filename);
|
|
|
|
|
builder.resolveAll();
|
|
|
|
|
return builder;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Loads a .json file and returns the Builder.
|
|
|
|
|
* @param {string|!{root: string, file: string}} filename Path to json file or an object specifying 'file' with
|
|
|
|
|
* an overridden 'root' path for all imported files.
|
|
|
|
|
* @param {function(?Error, !ProtoBuf.Builder=)=} callback Callback that will receive `null` as the first and
|
|
|
|
|
* the Builder as its second argument on success, otherwise the error as its first argument. If omitted, the
|
|
|
|
|
* file will be read synchronously and this function will return the Builder.
|
|
|
|
|
* @param {ProtoBuf.Builder=} builder Builder to append to. Will create a new one if omitted.
|
|
|
|
|
* @return {?ProtoBuf.Builder|undefined} The Builder if synchronous (no callback specified, will be NULL if the
|
|
|
|
|
* request has failed), else undefined
|
|
|
|
|
* @expose
|
|
|
|
|
*/
|
|
|
|
|
ProtoBuf.loadJsonFile = function(filename, callback, builder) {
|
|
|
|
|
if (callback && typeof callback === 'object')
|
|
|
|
|
builder = callback,
|
|
|
|
|
callback = null;
|
|
|
|
|
else if (!callback || typeof callback !== 'function')
|
|
|
|
|
callback = null;
|
|
|
|
|
if (callback)
|
|
|
|
|
return ProtoBuf.Util.fetch(typeof filename === 'string' ? filename : filename["root"]+"/"+filename["file"], function(contents) {
|
|
|
|
|
if (contents === null) {
|
|
|
|
|
callback(Error("Failed to fetch file"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
callback(null, ProtoBuf.loadJson(JSON.parse(contents), builder, filename));
|
|
|
|
|
} catch (e) {
|
|
|
|
|
callback(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
var contents = ProtoBuf.Util.fetch(typeof filename === 'object' ? filename["root"]+"/"+filename["file"] : filename);
|
|
|
|
|
return contents === null ? null : ProtoBuf.loadJson(JSON.parse(contents), builder, filename);
|
|
|
|
|
};
|
2014-10-29 23:04:51 +00:00
|
|
|
|
|
2020-02-07 19:37:04 +00:00
|
|
|
|
return ProtoBuf;
|
|
|
|
|
});
|