diff --git a/bower.json b/bower.json index a0fa75486..dccf15f8b 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "protobuf", "description": "Protocol Buffers for JavaScript. Finally.", - "version": "3.7.0", + "version": "3.8.0", "main": "dist/ProtoBuf.js", "license": "Apache-2.0", "homepage": "http://dcode.io/", diff --git a/dist/ProtoBuf.js b/dist/ProtoBuf.js index 80c9e0c27..3ce174049 100644 --- a/dist/ProtoBuf.js +++ b/dist/ProtoBuf.js @@ -38,7 +38,7 @@ * @const * @expose */ - ProtoBuf.VERSION = "3.7.0"; + ProtoBuf.VERSION = "3.8.0"; /** * Wire types. @@ -2139,33 +2139,33 @@ /** * Clones a message object to a raw object. * @param {*} obj Object to clone - * @param {boolean} includeBuffers Whether to include native buffer data or not + * @param {boolean} includeBinaryAsBase64 Whether to include binary data as base64 strings or not * @returns {*} Cloned object * @inner */ - function cloneRaw(obj, includeBuffers) { + function cloneRaw(obj, includeBinaryAsBase64) { var clone = {}; for (var i in obj) if (obj.hasOwnProperty(i)) { if (obj[i] === null || typeof obj[i] !== 'object') clone[i] = obj[i]; else if (obj[i] instanceof ByteBuffer) { - if (includeBuffers) - clone[i] = obj.toBuffer(); + if (includeBinaryAsBase64) + clone[i] = obj[i].toBase64(); } else // is a non-null object - clone[i] = cloneRaw(obj[i], includeBuffers); + clone[i] = cloneRaw(obj[i], includeBinaryAsBase64); } return clone; } /** * Returns the message's raw payload. - * @param {boolean=} includeBuffers Whether to include native buffer data or not, defaults to `false` + * @param {boolean=} includeBinaryAsBase64 Whether to include binary data as base64 strings or not, defaults to `false` * @returns {Object.} Raw payload * @expose */ - MessagePrototype.toRaw = function(includeBuffers) { - return cloneRaw(this, !!includeBuffers); + MessagePrototype.toRaw = function(includeBinaryAsBase64) { + return cloneRaw(this, !!includeBinaryAsBase64); }; /** @@ -2723,9 +2723,9 @@ // Length-delimited bytes case ProtoBuf.TYPES["bytes"]: - return value && value instanceof ByteBuffer - ? value - : ByteBuffer.wrap(value); + if (ByteBuffer.isByteBuffer(value)) + return value; + return ByteBuffer.wrap(value, "base64"); // Constant enum value case ProtoBuf.TYPES["enum"]: { @@ -4326,7 +4326,7 @@ return ProtoBuf; } - /* CommonJS */ if (typeof require === 'function' && typeof module === 'object' && module && module.id && typeof exports === 'object' && exports) + /* CommonJS */ if (typeof require === 'function' && typeof module === 'object' && module && typeof exports === 'object' && exports) module['exports'] = init(require("bytebuffer")); /* AMD */ else if (typeof define === 'function' && define["amd"]) define(["ByteBuffer"], init); diff --git a/dist/ProtoBuf.min.js b/dist/ProtoBuf.min.js index aff2debc8..ff992341f 100644 --- a/dist/ProtoBuf.min.js +++ b/dist/ProtoBuf.min.js @@ -3,9 +3,9 @@ Released under the Apache License, Version 2.0 see: https://github.com/dcodeIO/ProtoBuf.js for details */ -(function(s){function u(m){var g={VERSION:"3.7.0",WIRE_TYPES:{}};g.WIRE_TYPES.VARINT=0;g.WIRE_TYPES.BITS64=1;g.WIRE_TYPES.LDELIM=2;g.WIRE_TYPES.STARTGROUP=3;g.WIRE_TYPES.ENDGROUP=4;g.WIRE_TYPES.BITS32=5;g.PACKABLE_WIRE_TYPES=[g.WIRE_TYPES.VARINT,g.WIRE_TYPES.BITS64,g.WIRE_TYPES.BITS32];g.TYPES={int32:{name:"int32",wireType:g.WIRE_TYPES.VARINT},uint32:{name:"uint32",wireType:g.WIRE_TYPES.VARINT},sint32:{name:"sint32",wireType:g.WIRE_TYPES.VARINT},int64:{name:"int64",wireType:g.WIRE_TYPES.VARINT},uint64:{name:"uint64", +(function(s){function u(l){var g={VERSION:"3.8.0",WIRE_TYPES:{}};g.WIRE_TYPES.VARINT=0;g.WIRE_TYPES.BITS64=1;g.WIRE_TYPES.LDELIM=2;g.WIRE_TYPES.STARTGROUP=3;g.WIRE_TYPES.ENDGROUP=4;g.WIRE_TYPES.BITS32=5;g.PACKABLE_WIRE_TYPES=[g.WIRE_TYPES.VARINT,g.WIRE_TYPES.BITS64,g.WIRE_TYPES.BITS32];g.TYPES={int32:{name:"int32",wireType:g.WIRE_TYPES.VARINT},uint32:{name:"uint32",wireType:g.WIRE_TYPES.VARINT},sint32:{name:"sint32",wireType:g.WIRE_TYPES.VARINT},int64:{name:"int64",wireType:g.WIRE_TYPES.VARINT},uint64:{name:"uint64", wireType:g.WIRE_TYPES.VARINT},sint64:{name:"sint64",wireType:g.WIRE_TYPES.VARINT},bool:{name:"bool",wireType:g.WIRE_TYPES.VARINT},"double":{name:"double",wireType:g.WIRE_TYPES.BITS64},string:{name:"string",wireType:g.WIRE_TYPES.LDELIM},bytes:{name:"bytes",wireType:g.WIRE_TYPES.LDELIM},fixed32:{name:"fixed32",wireType:g.WIRE_TYPES.BITS32},sfixed32:{name:"sfixed32",wireType:g.WIRE_TYPES.BITS32},fixed64:{name:"fixed64",wireType:g.WIRE_TYPES.BITS64},sfixed64:{name:"sfixed64",wireType:g.WIRE_TYPES.BITS64}, -"float":{name:"float",wireType:g.WIRE_TYPES.BITS32},"enum":{name:"enum",wireType:g.WIRE_TYPES.VARINT},message:{name:"message",wireType:g.WIRE_TYPES.LDELIM},group:{name:"group",wireType:g.WIRE_TYPES.STARTGROUP}};g.ID_MIN=1;g.ID_MAX=536870911;g.ByteBuffer=m;g.Long=m.Long||null;g.convertFieldsToCamelCase=!1;g.populateAccessors=!0;g.Util=function(){Object.create||(Object.create=function(e){function d(){}if(1a.remaining())return null;var l=a.offset,e=a.readVarint32();if(a.remaining()a.remaining())return null;var d=a.offset,e=a.readVarint32();if(a.remaining()>3;if(p===e.WIRE_TYPES.ENDGROUP){if(k!==c)throw Error("Illegal group end indicator for "+this.toString(!0)+": "+k+" ("+(c?c+" expected":"not a group")+")");break}if(h=this._fieldsById[k])h.repeated&&!h.options.packed?g[h.name].push(h.decode(p,a)):(g[h.name]=h.decode(p,a),h.oneof&&(null!== +return this.clazz=b};c.encode=function(a,b,c){for(var d=null,e,f=0,g=this._fields.length,h;f>3;if(p===e.WIRE_TYPES.ENDGROUP){if(k!==c)throw Error("Illegal group end indicator for "+this.toString(!0)+": "+k+" ("+(c?c+" expected":"not a group")+")");break}if(h=this._fieldsById[k])h.repeated&&!h.options.packed?g[h.name].push(h.decode(p,a)):(g[h.name]=h.decode(p,a),h.oneof&&(null!== this[h.oneof.name]&&(this[this[h.oneof.name]]=null),g[h.oneof.name]=h.name));else switch(p){case e.WIRE_TYPES.VARINT:a.readVarint32();break;case e.WIRE_TYPES.BITS32:a.offset+=4;break;case e.WIRE_TYPES.BITS64:a.offset+=8;break;case e.WIRE_TYPES.LDELIM:h=a.readVarint32();a.offset+=h;break;case e.WIRE_TYPES.STARTGROUP:for(;d(k,a););break;default:throw Error("Illegal wire type for unknown field "+k+" in "+this.toString(!0)+"#decode: "+p);}}a=0;for(b=this._fields.length;aa?a>>>0:a;case e.TYPES.int64:case e.TYPES.sint64:case e.TYPES.sfixed64:if(e.Long)try{return g(a,!1)}catch(f){c(typeof a,f.message)}else c(typeof a,"requires Long.js");case e.TYPES.uint64:case e.TYPES.fixed64:if(e.Long)try{return g(a,!0)}catch(h){c(typeof a,h.message)}else c(typeof a,"requires Long.js");case e.TYPES.bool:return"boolean"!==typeof a&&c(typeof a,"not a boolean"),a;case e.TYPES["float"]:case e.TYPES["double"]:return"number"!==typeof a&&c(typeof a, -"not a number"),a;case e.TYPES.string:return"string"===typeof a||a&&a instanceof String||c(typeof a,"not a string"),""+a;case e.TYPES.bytes:return a&&a instanceof m?a:m.wrap(a);case e.TYPES["enum"]:var k=this.resolvedType.getChildren(p.Value);for(d=0;da?b.writeVarint64(a):b.writeVarint32(a);break;case e.TYPES.uint32:b.writeVarint32(a);break;case e.TYPES.sint32:b.writeVarint32ZigZag(a);break;case e.TYPES.fixed32:b.writeUint32(a);break;case e.TYPES.sfixed32:b.writeInt32(a);break;case e.TYPES.int64:case e.TYPES.uint64:b.writeVarint64(a);break;case e.TYPES.sint64:b.writeVarint64ZigZag(a);break;case e.TYPES.fixed64:b.writeUint64(a);break;case e.TYPES.sfixed64:b.writeInt64(a); break;case e.TYPES.bool:"string"===typeof a?b.writeVarint32("false"===a.toLowerCase()?0:!!a):b.writeVarint32(a?1:0);break;case e.TYPES["enum"]:b.writeVarint32(a);break;case e.TYPES["float"]:b.writeFloat32(a);break;case e.TYPES["double"]:b.writeFloat64(a);break;case e.TYPES.string:b.writeVString(a);break;case e.TYPES.bytes:if(0>a.remaining())throw Error("Illegal value for "+this.toString(!0)+": "+a.remaining()+" bytes remaining");var c=a.offset;b.writeVarint32(a.remaining());b.append(a);a.offset=c; -break;case e.TYPES.message:c=(new m).LE();this.resolvedType.encode(a,c);b.writeVarint32(c.offset);b.append(c.flip());break;case e.TYPES.group:this.resolvedType.encode(a,b);b.writeVarint32(this.id<<3|e.WIRE_TYPES.ENDGROUP);break;default:throw Error("[INTERNAL] Illegal value to encode in "+this.toString(!0)+": "+a+" (unknown type)");}return b};c.calculate=function(a){a=this.verifyValue(a);if(null===this.type||"object"!==typeof this.type)throw Error("[INTERNAL] Unresolved type in "+this.toString(!0)+ -": "+this.type);if(null===a||this.repeated&&0==a.length)return 0;var b=0;try{if(this.repeated){var c,d;if(this.options.packed&&0<=e.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType)){b+=m.calculateVarint32(this.id<<3|e.WIRE_TYPES.LDELIM);for(c=d=0;ca?m.calculateVarint64(a):m.calculateVarint32(a);case e.TYPES.uint32:return m.calculateVarint32(a);case e.TYPES.sint32:return m.calculateVarint32(m.zigZagEncode32(a));case e.TYPES.fixed32:case e.TYPES.sfixed32:case e.TYPES["float"]:return 4;case e.TYPES.int64:case e.TYPES.uint64:return m.calculateVarint64(a);case e.TYPES.sint64:return m.calculateVarint64(m.zigZagEncode64(a)); -case e.TYPES.fixed64:case e.TYPES.sfixed64:return 8;case e.TYPES.bool:return 1;case e.TYPES["enum"]:return m.calculateVarint32(a);case e.TYPES["double"]:return 8;case e.TYPES.string:return a=m.calculateUTF8Bytes(a),m.calculateVarint32(a)+a;case e.TYPES.bytes:if(0>a.remaining())throw Error("Illegal value for "+this.toString(!0)+": "+a.remaining()+" bytes remaining");return m.calculateVarint32(a.remaining())+a.remaining();case e.TYPES.message:return a=this.resolvedType.calculate(a),m.calculateVarint32(a)+ -a;case e.TYPES.group:return a=this.resolvedType.calculate(a),a+m.calculateVarint32(this.id<<3|e.WIRE_TYPES.ENDGROUP)}throw Error("[INTERNAL] Illegal value to encode in "+this.toString(!0)+": "+a+" (unknown type)");};c.decode=function(a,b,c){if(a!=this.type.wireType&&(c||a!=e.WIRE_TYPES.LDELIM||!this.repeated))throw Error("Illegal wire type for field "+this.toString(!0)+": "+a+" ("+this.type.wireType+" expected)");if(a==e.WIRE_TYPES.LDELIM&&this.repeated&&this.options.packed&&0<=e.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType)&& +break;case e.TYPES.message:c=(new l).LE();this.resolvedType.encode(a,c);b.writeVarint32(c.offset);b.append(c.flip());break;case e.TYPES.group:this.resolvedType.encode(a,b);b.writeVarint32(this.id<<3|e.WIRE_TYPES.ENDGROUP);break;default:throw Error("[INTERNAL] Illegal value to encode in "+this.toString(!0)+": "+a+" (unknown type)");}return b};c.calculate=function(a){a=this.verifyValue(a);if(null===this.type||"object"!==typeof this.type)throw Error("[INTERNAL] Unresolved type in "+this.toString(!0)+ +": "+this.type);if(null===a||this.repeated&&0==a.length)return 0;var b=0;try{if(this.repeated){var c,d;if(this.options.packed&&0<=e.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType)){b+=l.calculateVarint32(this.id<<3|e.WIRE_TYPES.LDELIM);for(c=d=0;ca?l.calculateVarint64(a):l.calculateVarint32(a);case e.TYPES.uint32:return l.calculateVarint32(a);case e.TYPES.sint32:return l.calculateVarint32(l.zigZagEncode32(a));case e.TYPES.fixed32:case e.TYPES.sfixed32:case e.TYPES["float"]:return 4;case e.TYPES.int64:case e.TYPES.uint64:return l.calculateVarint64(a);case e.TYPES.sint64:return l.calculateVarint64(l.zigZagEncode64(a)); +case e.TYPES.fixed64:case e.TYPES.sfixed64:return 8;case e.TYPES.bool:return 1;case e.TYPES["enum"]:return l.calculateVarint32(a);case e.TYPES["double"]:return 8;case e.TYPES.string:return a=l.calculateUTF8Bytes(a),l.calculateVarint32(a)+a;case e.TYPES.bytes:if(0>a.remaining())throw Error("Illegal value for "+this.toString(!0)+": "+a.remaining()+" bytes remaining");return l.calculateVarint32(a.remaining())+a.remaining();case e.TYPES.message:return a=this.resolvedType.calculate(a),l.calculateVarint32(a)+ +a;case e.TYPES.group:return a=this.resolvedType.calculate(a),a+l.calculateVarint32(this.id<<3|e.WIRE_TYPES.ENDGROUP)}throw Error("[INTERNAL] Illegal value to encode in "+this.toString(!0)+": "+a+" (unknown type)");};c.decode=function(a,b,c){if(a!=this.type.wireType&&(c||a!=e.WIRE_TYPES.LDELIM||!this.repeated))throw Error("Illegal wire type for field "+this.toString(!0)+": "+a+" ("+this.type.wireType+" expected)");if(a==e.WIRE_TYPES.LDELIM&&this.repeated&&this.options.packed&&0<=e.PACKABLE_WIRE_TYPES.indexOf(this.type.wireType)&& !c){a=b.readVarint32();a=b.offset+a;for(c=[];b.offset>>0;case e.TYPES.sint32:return b.readVarint32ZigZag()|0;case e.TYPES.fixed32:return b.readUint32()>>>0;case e.TYPES.sfixed32:return b.readInt32()|0;case e.TYPES.int64:return b.readVarint64();case e.TYPES.uint64:return b.readVarint64().toUnsigned();case e.TYPES.sint64:return b.readVarint64ZigZag(); case e.TYPES.fixed64:return b.readUint64();case e.TYPES.sfixed64:return b.readInt64();case e.TYPES.bool:return!!b.readVarint32();case e.TYPES["enum"]:return b.readVarint32();case e.TYPES["float"]:return b.readFloat();case e.TYPES["double"]:return b.readDouble();case e.TYPES.string:return b.readVString();case e.TYPES.bytes:a=b.readVarint32();if(b.remaining()e.ID_MAX&&(c.extensions[1]=e.ID_MAX));this.ptr.addChild(c);0c.extensions[1])throw Error("Illegal extended field id in message "+c.name+": "+a.fields[h].id+" ("+c.extensions.join(" to ")+ " expected)");n=a.fields[h].name;this.options.convertFieldsToCamelCase&&(n=g.Message.Field._toCamelCase(a.fields[h].name));p=new g.Message.ExtensionField(this,c,a.fields[h].rule,a.fields[h].type,this.ptr.fqn()+"."+n,a.fields[h].id,a.fields[h].options);n=new g.Extension(this,this.ptr,a.fields[h].name,p);p.extension=n;this.ptr.addChild(n);c.addChild(p)}else{if(!/\.?google\.protobuf\./.test(a.ref))throw Error("Extended message "+a.ref+" is not defined");}else throw Error("Not a valid definition: "+JSON.stringify(a)); @@ -96,8 +96,8 @@ var f;if("undefined"!==typeof b.fields){if(!e.Util.isArray(b.fields))return!1;va e.TYPES[this.ptr.type];else{if(!d.TYPEREF.test(this.ptr.type))throw Error("Illegal type reference in "+this.ptr.toString(!0)+": "+this.ptr.type);b=(this.ptr instanceof g.Message.ExtensionField?this.ptr.extension.parent:this.ptr.parent).resolve(this.ptr.type,!0);if(!b)throw Error("Unresolvable type reference in "+this.ptr.toString(!0)+": "+this.ptr.type);this.ptr.resolvedType=b;if(b instanceof g.Enum)this.ptr.type=e.TYPES["enum"];else if(b instanceof g.Message)this.ptr.type=b.isGroup?e.TYPES.group: e.TYPES.message;else throw Error("Illegal type reference in "+this.ptr.toString(!0)+": "+this.ptr.type);}else if(!(this.ptr instanceof e.Reflect.Enum.Value))if(this.ptr instanceof e.Reflect.Service.Method)if(this.ptr instanceof e.Reflect.Service.RPCMethod){b=this.ptr.parent.resolve(this.ptr.requestName);if(!(b&&b instanceof e.Reflect.Message))throw Error("Illegal type reference in "+this.ptr.toString(!0)+": "+this.ptr.requestName);this.ptr.resolvedRequestType=b;b=this.ptr.parent.resolve(this.ptr.responseName); if(!(b&&b instanceof e.Reflect.Message))throw Error("Illegal type reference in "+this.ptr.toString(!0)+": "+this.ptr.responseName);this.ptr.resolvedResponseType=b}else throw Error("Illegal service type in "+this.ptr.toString(!0));else if(!(this.ptr instanceof e.Reflect.Message.OneOf||this.ptr instanceof e.Reflect.Extension))throw Error("Illegal object in namespace: "+typeof this.ptr+":"+this.ptr);this.reset()}};n.build=function(b){this.reset();this.resolved||(this.resolveAll(),this.resolved=!0,this.result= -null);null==this.result&&(this.result=this.ns.build());if(b){b=b.split(".");for(var d=this.result,a=0;a} Raw payload * @expose */ - MessagePrototype.toRaw = function(includeBuffers) { - return cloneRaw(this, !!includeBuffers); + MessagePrototype.toRaw = function(includeBinaryAsBase64) { + return cloneRaw(this, !!includeBinaryAsBase64); }; /** @@ -1811,9 +1811,9 @@ // Length-delimited bytes case ProtoBuf.TYPES["bytes"]: - return value && value instanceof ByteBuffer - ? value - : ByteBuffer.wrap(value); + if (ByteBuffer.isByteBuffer(value)) + return value; + return ByteBuffer.wrap(value, "base64"); // Constant enum value case ProtoBuf.TYPES["enum"]: { @@ -3336,7 +3336,7 @@ return ProtoBuf; } - /* CommonJS */ if (typeof require === 'function' && typeof module === 'object' && module && module.id && typeof exports === 'object' && exports) + /* CommonJS */ if (typeof require === 'function' && typeof module === 'object' && module && typeof exports === 'object' && exports) module['exports'] = init(require("bytebuffer")); /* AMD */ else if (typeof define === 'function' && define["amd"]) define(["ByteBuffer"], init); diff --git a/dist/ProtoBuf.noparse.min.js b/dist/ProtoBuf.noparse.min.js index a4bd2ff12..04556ef84 100644 --- a/dist/ProtoBuf.noparse.min.js +++ b/dist/ProtoBuf.noparse.min.js @@ -3,7 +3,7 @@ Released under the Apache License, Version 2.0 see: https://github.com/dcodeIO/ProtoBuf.js for details */ -(function(w){function x(k){var g={VERSION:"3.7.0",WIRE_TYPES:{}};g.WIRE_TYPES.VARINT=0;g.WIRE_TYPES.BITS64=1;g.WIRE_TYPES.LDELIM=2;g.WIRE_TYPES.STARTGROUP=3;g.WIRE_TYPES.ENDGROUP=4;g.WIRE_TYPES.BITS32=5;g.PACKABLE_WIRE_TYPES=[g.WIRE_TYPES.VARINT,g.WIRE_TYPES.BITS64,g.WIRE_TYPES.BITS32];g.TYPES={int32:{name:"int32",wireType:g.WIRE_TYPES.VARINT},uint32:{name:"uint32",wireType:g.WIRE_TYPES.VARINT},sint32:{name:"sint32",wireType:g.WIRE_TYPES.VARINT},int64:{name:"int64",wireType:g.WIRE_TYPES.VARINT},uint64:{name:"uint64", +(function(w){function x(k){var g={VERSION:"3.8.0",WIRE_TYPES:{}};g.WIRE_TYPES.VARINT=0;g.WIRE_TYPES.BITS64=1;g.WIRE_TYPES.LDELIM=2;g.WIRE_TYPES.STARTGROUP=3;g.WIRE_TYPES.ENDGROUP=4;g.WIRE_TYPES.BITS32=5;g.PACKABLE_WIRE_TYPES=[g.WIRE_TYPES.VARINT,g.WIRE_TYPES.BITS64,g.WIRE_TYPES.BITS32];g.TYPES={int32:{name:"int32",wireType:g.WIRE_TYPES.VARINT},uint32:{name:"uint32",wireType:g.WIRE_TYPES.VARINT},sint32:{name:"sint32",wireType:g.WIRE_TYPES.VARINT},int64:{name:"int64",wireType:g.WIRE_TYPES.VARINT},uint64:{name:"uint64", wireType:g.WIRE_TYPES.VARINT},sint64:{name:"sint64",wireType:g.WIRE_TYPES.VARINT},bool:{name:"bool",wireType:g.WIRE_TYPES.VARINT},"double":{name:"double",wireType:g.WIRE_TYPES.BITS64},string:{name:"string",wireType:g.WIRE_TYPES.LDELIM},bytes:{name:"bytes",wireType:g.WIRE_TYPES.LDELIM},fixed32:{name:"fixed32",wireType:g.WIRE_TYPES.BITS32},sfixed32:{name:"sfixed32",wireType:g.WIRE_TYPES.BITS32},fixed64:{name:"fixed64",wireType:g.WIRE_TYPES.BITS64},sfixed64:{name:"sfixed64",wireType:g.WIRE_TYPES.BITS64}, "float":{name:"float",wireType:g.WIRE_TYPES.BITS32},"enum":{name:"enum",wireType:g.WIRE_TYPES.VARINT},message:{name:"message",wireType:g.WIRE_TYPES.LDELIM},group:{name:"group",wireType:g.WIRE_TYPES.STARTGROUP}};g.ID_MIN=1;g.ID_MAX=536870911;g.ByteBuffer=k;g.Long=k.Long||null;g.convertFieldsToCamelCase=!1;g.populateAccessors=!0;g.Util=function(){Object.create||(Object.create=function(c){function g(){}if(1a?a>>>0:a;case c.TYPES.int64:case c.TYPES.sint64:case c.TYPES.sfixed64:if(c.Long)try{return t(a,!1)}catch(d){l(typeof a,d.message)}else l(typeof a,"requires Long.js");case c.TYPES.uint64:case c.TYPES.fixed64:if(c.Long)try{return t(a,!0)}catch(f){l(typeof a,f.message)}else l(typeof a,"requires Long.js");case c.TYPES.bool:return"boolean"!== -typeof a&&l(typeof a,"not a boolean"),a;case c.TYPES["float"]:case c.TYPES["double"]:return"number"!==typeof a&&l(typeof a,"not a number"),a;case c.TYPES.string:return"string"===typeof a||a&&a instanceof String||l(typeof a,"not a string"),""+a;case c.TYPES.bytes:return a&&a instanceof k?a:k.wrap(a);case c.TYPES["enum"]:var m=this.resolvedType.getChildren(q.Value);for(e=0;ea?b.writeVarint64(a):b.writeVarint32(a);break;case c.TYPES.uint32:b.writeVarint32(a);break;case c.TYPES.sint32:b.writeVarint32ZigZag(a);break;case c.TYPES.fixed32:b.writeUint32(a);break;case c.TYPES.sfixed32:b.writeInt32(a);break;case c.TYPES.int64:case c.TYPES.uint64:b.writeVarint64(a); @@ -71,5 +71,5 @@ k.Message)this.ptr.type=b.isGroup?c.TYPES.group:c.TYPES.message;else throw Error b;b=this.ptr.parent.resolve(this.ptr.responseName);if(!(b&&b instanceof c.Reflect.Message))throw Error("Illegal type reference in "+this.ptr.toString(!0)+": "+this.ptr.responseName);this.ptr.resolvedResponseType=b}else throw Error("Illegal service type in "+this.ptr.toString(!0));else if(!(this.ptr instanceof c.Reflect.Message.OneOf||this.ptr instanceof c.Reflect.Extension))throw Error("Illegal object in namespace: "+typeof this.ptr+":"+this.ptr);this.reset()}};r.build=function(b){this.reset();this.resolved|| (this.resolveAll(),this.resolved=!0,this.result=null);null==this.result&&(this.result=this.ns.build());if(b){b=b.split(".");for(var c=this.result,f=0;fReturns:
-

toRaw(includeBuffersopt) → {Object.<string, *>}

+

toRaw(includeBinaryAsBase64opt) → {Object.<string, *>}

@@ -4595,7 +4595,7 @@
Parameters:
- includeBuffers + includeBinaryAsBase64 @@ -4621,7 +4621,7 @@
Parameters:
-

Whether to include native buffer data or not, defaults to false

+

Whether to include binary data as base64 strings or not, defaults to false

@@ -4816,7 +4816,7 @@

Index

Classes

  • diff --git a/docs/ProtoBuf.Builder.Service.html b/docs/ProtoBuf.Builder.Service.html index 1ac343fb3..cc5016e27 100644 --- a/docs/ProtoBuf.Builder.Service.html +++ b/docs/ProtoBuf.Builder.Service.html @@ -619,7 +619,7 @@

    Index

    Classes

    • diff --git a/docs/ProtoBuf.Builder.html b/docs/ProtoBuf.Builder.html index 3635aefec..688d01ff8 100644 --- a/docs/ProtoBuf.Builder.html +++ b/docs/ProtoBuf.Builder.html @@ -2585,7 +2585,7 @@

      Index

      Classes

      • diff --git a/docs/ProtoBuf.DotProto.Parser.html b/docs/ProtoBuf.DotProto.Parser.html index 0e3dc26ec..ca88ac67c 100644 --- a/docs/ProtoBuf.DotProto.Parser.html +++ b/docs/ProtoBuf.DotProto.Parser.html @@ -480,7 +480,7 @@

        Index

        Classes

        • diff --git a/docs/ProtoBuf.DotProto.Tokenizer.html b/docs/ProtoBuf.DotProto.Tokenizer.html index 62903261e..a152d36e7 100644 --- a/docs/ProtoBuf.DotProto.Tokenizer.html +++ b/docs/ProtoBuf.DotProto.Tokenizer.html @@ -934,7 +934,7 @@

          Index

          Classes

          • diff --git a/docs/ProtoBuf.DotProto.html b/docs/ProtoBuf.DotProto.html index 3e9a2db5c..2338287af 100644 --- a/docs/ProtoBuf.DotProto.html +++ b/docs/ProtoBuf.DotProto.html @@ -127,7 +127,7 @@

            Index

            Classes

            • diff --git a/docs/ProtoBuf.Reflect.Enum.Value.html b/docs/ProtoBuf.Reflect.Enum.Value.html index 6ab7c617a..0afc10009 100644 --- a/docs/ProtoBuf.Reflect.Enum.Value.html +++ b/docs/ProtoBuf.Reflect.Enum.Value.html @@ -956,7 +956,7 @@

              Index

              Classes

              • diff --git a/docs/ProtoBuf.Reflect.Enum.html b/docs/ProtoBuf.Reflect.Enum.html index aca2d372d..12a7b8152 100644 --- a/docs/ProtoBuf.Reflect.Enum.html +++ b/docs/ProtoBuf.Reflect.Enum.html @@ -2028,7 +2028,7 @@

                Index

                Classes

                • diff --git a/docs/ProtoBuf.Reflect.Extension.html b/docs/ProtoBuf.Reflect.Extension.html index d2bdebbbf..3e56a2288 100644 --- a/docs/ProtoBuf.Reflect.Extension.html +++ b/docs/ProtoBuf.Reflect.Extension.html @@ -334,7 +334,7 @@

                  Index

                  Classes

                  • diff --git a/docs/ProtoBuf.Reflect.Message.ExtensionField.html b/docs/ProtoBuf.Reflect.Message.ExtensionField.html index 69a6c28e4..8e01d3011 100644 --- a/docs/ProtoBuf.Reflect.Message.ExtensionField.html +++ b/docs/ProtoBuf.Reflect.Message.ExtensionField.html @@ -2882,7 +2882,7 @@

                    Index

                    Classes

                    • diff --git a/docs/ProtoBuf.Reflect.Message.Field.html b/docs/ProtoBuf.Reflect.Message.Field.html index e61f3cd94..84edfdc02 100644 --- a/docs/ProtoBuf.Reflect.Message.Field.html +++ b/docs/ProtoBuf.Reflect.Message.Field.html @@ -2764,7 +2764,7 @@

                      Index

                      Classes

                      • diff --git a/docs/ProtoBuf.Reflect.Message.OneOf.html b/docs/ProtoBuf.Reflect.Message.OneOf.html index db68fed91..0914b3bc4 100644 --- a/docs/ProtoBuf.Reflect.Message.OneOf.html +++ b/docs/ProtoBuf.Reflect.Message.OneOf.html @@ -952,7 +952,7 @@

                        Index

                        Classes

                        • diff --git a/docs/ProtoBuf.Reflect.Message.html b/docs/ProtoBuf.Reflect.Message.html index f94a43b24..83c9bb7ab 100644 --- a/docs/ProtoBuf.Reflect.Message.html +++ b/docs/ProtoBuf.Reflect.Message.html @@ -2948,7 +2948,7 @@

                          Index

                          Classes

                          • diff --git a/docs/ProtoBuf.Reflect.Namespace.html b/docs/ProtoBuf.Reflect.Namespace.html index ebeb47b7f..0408adb45 100644 --- a/docs/ProtoBuf.Reflect.Namespace.html +++ b/docs/ProtoBuf.Reflect.Namespace.html @@ -1918,7 +1918,7 @@

                            Index

                            Classes

                            • diff --git a/docs/ProtoBuf.Reflect.Service.Method.html b/docs/ProtoBuf.Reflect.Service.Method.html index 775d9097a..02286ca60 100644 --- a/docs/ProtoBuf.Reflect.Service.Method.html +++ b/docs/ProtoBuf.Reflect.Service.Method.html @@ -1081,7 +1081,7 @@

                              Index

                              Classes

                              • diff --git a/docs/ProtoBuf.Reflect.Service.RPCMethod.html b/docs/ProtoBuf.Reflect.Service.RPCMethod.html index 46ed1e9ca..df3594939 100644 --- a/docs/ProtoBuf.Reflect.Service.RPCMethod.html +++ b/docs/ProtoBuf.Reflect.Service.RPCMethod.html @@ -1417,7 +1417,7 @@

                                Index

                                Classes

                                • diff --git a/docs/ProtoBuf.Reflect.Service.html b/docs/ProtoBuf.Reflect.Service.html index d9ca095d7..3aba6736e 100644 --- a/docs/ProtoBuf.Reflect.Service.html +++ b/docs/ProtoBuf.Reflect.Service.html @@ -2126,7 +2126,7 @@

                                  Index

                                  Classes

                                  • diff --git a/docs/ProtoBuf.Reflect.T.html b/docs/ProtoBuf.Reflect.T.html index c92361779..3a86c3f7a 100644 --- a/docs/ProtoBuf.Reflect.T.html +++ b/docs/ProtoBuf.Reflect.T.html @@ -873,7 +873,7 @@

                                    Index

                                    Classes

                                    • diff --git a/docs/ProtoBuf.Reflect.html b/docs/ProtoBuf.Reflect.html index 36d483cae..77895ec73 100644 --- a/docs/ProtoBuf.Reflect.html +++ b/docs/ProtoBuf.Reflect.html @@ -139,7 +139,7 @@

                                      Index

                                      Classes

                                      • diff --git a/docs/ProtoBuf.Util.html b/docs/ProtoBuf.Util.html index 1b789d2c2..7eaf1db59 100644 --- a/docs/ProtoBuf.Util.html +++ b/docs/ProtoBuf.Util.html @@ -636,7 +636,7 @@

                                        Index

                                        Classes

                                        • diff --git a/docs/ProtoBuf.html b/docs/ProtoBuf.html index 45a697a68..6ffb15072 100644 --- a/docs/ProtoBuf.html +++ b/docs/ProtoBuf.html @@ -2852,7 +2852,7 @@

                                          Index

                                          Classes

                                          • diff --git a/docs/ProtoBuf.js.html b/docs/ProtoBuf.js.html index 02c3a397d..e750edbbf 100644 --- a/docs/ProtoBuf.js.html +++ b/docs/ProtoBuf.js.html @@ -65,7 +65,7 @@

                                            Source: ProtoBuf.js

                                            * @const * @expose */ - ProtoBuf.VERSION = "3.7.0"; + ProtoBuf.VERSION = "3.8.0"; /** * Wire types. @@ -2166,33 +2166,33 @@

                                            Source: ProtoBuf.js

                                            /** * Clones a message object to a raw object. * @param {*} obj Object to clone - * @param {boolean} includeBuffers Whether to include native buffer data or not + * @param {boolean} includeBinaryAsBase64 Whether to include binary data as base64 strings or not * @returns {*} Cloned object * @inner */ - function cloneRaw(obj, includeBuffers) { + function cloneRaw(obj, includeBinaryAsBase64) { var clone = {}; for (var i in obj) if (obj.hasOwnProperty(i)) { if (obj[i] === null || typeof obj[i] !== 'object') clone[i] = obj[i]; else if (obj[i] instanceof ByteBuffer) { - if (includeBuffers) - clone[i] = obj.toBuffer(); + if (includeBinaryAsBase64) + clone[i] = obj[i].toBase64(); } else // is a non-null object - clone[i] = cloneRaw(obj[i], includeBuffers); + clone[i] = cloneRaw(obj[i], includeBinaryAsBase64); } return clone; } /** * Returns the message's raw payload. - * @param {boolean=} includeBuffers Whether to include native buffer data or not, defaults to `false` + * @param {boolean=} includeBinaryAsBase64 Whether to include binary data as base64 strings or not, defaults to `false` * @returns {Object.<string,*>} Raw payload * @expose */ - MessagePrototype.toRaw = function(includeBuffers) { - return cloneRaw(this, !!includeBuffers); + MessagePrototype.toRaw = function(includeBinaryAsBase64) { + return cloneRaw(this, !!includeBinaryAsBase64); }; /** @@ -2750,9 +2750,9 @@

                                            Source: ProtoBuf.js

                                            // Length-delimited bytes case ProtoBuf.TYPES["bytes"]: - return value && value instanceof ByteBuffer - ? value - : ByteBuffer.wrap(value); + if (ByteBuffer.isByteBuffer(value)) + return value; + return ByteBuffer.wrap(value, "base64"); // Constant enum value case ProtoBuf.TYPES["enum"]: { @@ -4353,7 +4353,7 @@

                                            Source: ProtoBuf.js

                                            return ProtoBuf; } - /* CommonJS */ if (typeof require === 'function' && typeof module === 'object' && module && module.id && typeof exports === 'object' && exports) + /* CommonJS */ if (typeof require === 'function' && typeof module === 'object' && module && typeof exports === 'object' && exports) module['exports'] = init(require("bytebuffer")); /* AMD */ else if (typeof define === 'function' && define["amd"]) define(["ByteBuffer"], init); @@ -4377,7 +4377,7 @@

                                            Index

                                            Classes

                                            • diff --git a/docs/index.html b/docs/index.html index e8ae81183..0b3841e17 100644 --- a/docs/index.html +++ b/docs/index.html @@ -54,7 +54,7 @@

                                              Index

                                              Classes

                                              • diff --git a/package.json b/package.json index ebdaca33a..1a475819f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "protobufjs", - "version": "3.7.0", + "version": "3.8.0", "description": "Protocol Buffers for JavaScript. Finally.", "author": "Daniel Wirtz ", "contributors": [ diff --git a/src/ProtoBuf.js b/src/ProtoBuf.js index 2ccd5b1bb..d789e1681 100644 --- a/src/ProtoBuf.js +++ b/src/ProtoBuf.js @@ -401,7 +401,7 @@ return ProtoBuf; } - /* CommonJS */ if (typeof require === 'function' && typeof module === 'object' && module && module.id && typeof exports === 'object' && exports) + /* CommonJS */ if (typeof require === 'function' && typeof module === 'object' && module && typeof exports === 'object' && exports) module['exports'] = init(require("bytebuffer")); /* AMD */ else if (typeof define === 'function' && define["amd"]) define(["ByteBuffer"], init); diff --git a/src/ProtoBuf/Builder/Message.js b/src/ProtoBuf/Builder/Message.js index 9fb21c4d4..6d11fd7d4 100644 --- a/src/ProtoBuf/Builder/Message.js +++ b/src/ProtoBuf/Builder/Message.js @@ -455,33 +455,33 @@ MessagePrototype.toHex = MessagePrototype.encodeHex; /** * Clones a message object to a raw object. * @param {*} obj Object to clone - * @param {boolean} includeBuffers Whether to include native buffer data or not + * @param {boolean} includeBinaryAsBase64 Whether to include binary data as base64 strings or not * @returns {*} Cloned object * @inner */ -function cloneRaw(obj, includeBuffers) { +function cloneRaw(obj, includeBinaryAsBase64) { var clone = {}; for (var i in obj) if (obj.hasOwnProperty(i)) { if (obj[i] === null || typeof obj[i] !== 'object') clone[i] = obj[i]; else if (obj[i] instanceof ByteBuffer) { - if (includeBuffers) - clone[i] = obj.toBuffer(); + if (includeBinaryAsBase64) + clone[i] = obj[i].toBase64(); } else // is a non-null object - clone[i] = cloneRaw(obj[i], includeBuffers); + clone[i] = cloneRaw(obj[i], includeBinaryAsBase64); } return clone; } /** * Returns the message's raw payload. - * @param {boolean=} includeBuffers Whether to include native buffer data or not, defaults to `false` + * @param {boolean=} includeBinaryAsBase64 Whether to include binary data as base64 strings or not, defaults to `false` * @returns {Object.} Raw payload * @expose */ -MessagePrototype.toRaw = function(includeBuffers) { - return cloneRaw(this, !!includeBuffers); +MessagePrototype.toRaw = function(includeBinaryAsBase64) { + return cloneRaw(this, !!includeBinaryAsBase64); }; /** diff --git a/src/ProtoBuf/Reflect/Message/Field.js b/src/ProtoBuf/Reflect/Message/Field.js index 35ad0b545..81e5202c4 100644 --- a/src/ProtoBuf/Reflect/Message/Field.js +++ b/src/ProtoBuf/Reflect/Message/Field.js @@ -233,9 +233,9 @@ FieldPrototype.verifyValue = function(value, skipRepeated) { // Length-delimited bytes case ProtoBuf.TYPES["bytes"]: - return value && value instanceof ByteBuffer - ? value - : ByteBuffer.wrap(value); + if (ByteBuffer.isByteBuffer(value)) + return value; + return ByteBuffer.wrap(value, "base64"); // Constant enum value case ProtoBuf.TYPES["enum"]: { diff --git a/tests/suite.js b/tests/suite.js index 2dcced24f..7ba738bfa 100644 --- a/tests/suite.js +++ b/tests/suite.js @@ -1469,14 +1469,12 @@ "toRaw": function(test) { try { - var builder = ProtoBuf.loadProto("message MyMessage { required int32 a = 1; required int32 b = 2; }"), + var builder = ProtoBuf.loadProto("message MyMessage { required int32 a = 1; required int32 b = 2; required bytes c = 3; }"), MyMessage = builder.build("MyMessage"); - var raw = { a: 1, b: 2 }, + var raw = { a: 1, b: 2, c: "YWJj" }, myMessage = new MyMessage(raw); - test.deepEqual(myMessage, raw); - test.strictEqual(myMessage.encode, MyMessage.prototype.encode); - test.deepEqual(myMessage.toRaw(), raw); - test.strictEqual(myMessage.toRaw().encode, undefined); + test.deepEqual(myMessage.c.toBase64(), raw.c); + test.deepEqual(myMessage.toRaw(true), raw); } catch (e) { fail(e); }