Skip to content

Commit befe8a2

Browse files
committed
Add BB2 string encoding (hex, update base64)
1 parent 441a312 commit befe8a2

32 files changed

+563
-138
lines changed

ProtoBuf.js

+32-6
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* @const
3939
* @expose
4040
*/
41-
ProtoBuf.VERSION = "1.5.1";
41+
ProtoBuf.VERSION = "1.5.2";
4242

4343
/**
4444
* Wire types.
@@ -326,7 +326,6 @@
326326
* @exports ProtoBuf.Lang
327327
* @type {Object.<string,string|RegExp>}
328328
* @namespace
329-
* @private
330329
* @expose
331330
*/
332331
var Lang = { // Look, so cute!
@@ -1819,9 +1818,9 @@
18191818
* Encodes the message.
18201819
* @name ProtoBuf.Builder.Message#encode
18211820
* @function
1822-
* @param {ByteBuffer=} buffer ByteBuffer to encode to. Will create a new one if omitted.
1821+
* @param {!ByteBuffer=} buffer ByteBuffer to encode to. Will create a new one if omitted.
18231822
* @param {boolean=} doNotThrow Forces encoding even if required fields are missing, defaults to false
1824-
* @return {ByteBuffer} Encoded message
1823+
* @return {!ByteBuffer} Encoded message as a ByteBuffer (bb#toArrayBuffer, bb#toBuffer, bb#toHex, bb#toBase64, ...)
18251824
* @throws {Error} If required fields are missing or the message cannot be encoded for another reason
18261825
* @expose
18271826
*/
@@ -1874,17 +1873,32 @@
18741873
return this.encode().toBase64();
18751874
};
18761875

1876+
/**
1877+
* Directly encodes the message to a hex encoded string.
1878+
* @name ProtoBuf.Builder.Message#toHex
1879+
* @function
1880+
* @return {string} Hex encoded string
1881+
* @throws {Error} If the underlying buffer cannot be encoded
1882+
* @expose
1883+
*/
1884+
Message.prototype.toHex = function() {
1885+
return this.encode().toHex();
1886+
};
1887+
18771888
/**
18781889
* Decodes the message from the specified ByteBuffer.
18791890
* @name ProtoBuf.Builder.Message.decode
18801891
* @function
18811892
* @param {!ByteBuffer|!ArrayBuffer|!Buffer} buffer ByteBuffer to decode from
1893+
* @param {string=} enc Encoding if buffer is a string: hex, base64, defaults to utf8 which you shouldn't use
18821894
* @return {!ProtoBuf.Builder.Message} Decoded message
18831895
* @throws {Error} If the message cannot be decoded
18841896
* @expose
1897+
* @see ProtoBuf.Builder.Message.decode64
1898+
* @see ProtoBuf.Builder.Message.decodeHex
18851899
*/
1886-
Message.decode = function(buffer) {
1887-
buffer = buffer ? (buffer instanceof ByteBuffer ? buffer : ByteBuffer.wrap(buffer)) : new ByteBuffer();
1900+
Message.decode = function(buffer, enc) {
1901+
buffer = buffer ? (buffer instanceof ByteBuffer ? buffer : ByteBuffer.wrap(buffer, enc)) : new ByteBuffer();
18881902
var le = buffer.littleEndian;
18891903
try {
18901904
var msg = T.decode(buffer.LE());
@@ -1909,6 +1923,18 @@
19091923
return Message.decode(ByteBuffer.decode64(str));
19101924
};
19111925

1926+
/**
1927+
* Decodes the message from the specified hex encoded string.
1928+
* @name ProtoBuf.Builder.Message.decodeHex
1929+
* @param {string} str String to decode from
1930+
* @return {!ProtoBuf.Builder.Message} Decoded message
1931+
* @throws {Error} If the message cannot be decoded
1932+
* @expose
1933+
*/
1934+
Message.decodeHex = function(str) {
1935+
return Message.decode(ByteBuffer.decodeHex(str));
1936+
};
1937+
19121938
// Utility
19131939

19141940
/**

ProtoBuf.min.js

+42-41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ProtoBuf.min.map

+3-3
Large diffs are not rendered by default.

ProtoBuf.noparse.js

+32-6
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
* @const
3939
* @expose
4040
*/
41-
ProtoBuf.VERSION = "1.5.1";
41+
ProtoBuf.VERSION = "1.5.2";
4242

4343
/**
4444
* Wire types.
@@ -326,7 +326,6 @@
326326
* @exports ProtoBuf.Lang
327327
* @type {Object.<string,string|RegExp>}
328328
* @namespace
329-
* @private
330329
* @expose
331330
*/
332331
var Lang = { // Look, so cute!
@@ -924,9 +923,9 @@
924923
* Encodes the message.
925924
* @name ProtoBuf.Builder.Message#encode
926925
* @function
927-
* @param {ByteBuffer=} buffer ByteBuffer to encode to. Will create a new one if omitted.
926+
* @param {!ByteBuffer=} buffer ByteBuffer to encode to. Will create a new one if omitted.
928927
* @param {boolean=} doNotThrow Forces encoding even if required fields are missing, defaults to false
929-
* @return {ByteBuffer} Encoded message
928+
* @return {!ByteBuffer} Encoded message as a ByteBuffer (bb#toArrayBuffer, bb#toBuffer, bb#toHex, bb#toBase64, ...)
930929
* @throws {Error} If required fields are missing or the message cannot be encoded for another reason
931930
* @expose
932931
*/
@@ -979,17 +978,32 @@
979978
return this.encode().toBase64();
980979
};
981980

981+
/**
982+
* Directly encodes the message to a hex encoded string.
983+
* @name ProtoBuf.Builder.Message#toHex
984+
* @function
985+
* @return {string} Hex encoded string
986+
* @throws {Error} If the underlying buffer cannot be encoded
987+
* @expose
988+
*/
989+
Message.prototype.toHex = function() {
990+
return this.encode().toHex();
991+
};
992+
982993
/**
983994
* Decodes the message from the specified ByteBuffer.
984995
* @name ProtoBuf.Builder.Message.decode
985996
* @function
986997
* @param {!ByteBuffer|!ArrayBuffer|!Buffer} buffer ByteBuffer to decode from
998+
* @param {string=} enc Encoding if buffer is a string: hex, base64, defaults to utf8 which you shouldn't use
987999
* @return {!ProtoBuf.Builder.Message} Decoded message
9881000
* @throws {Error} If the message cannot be decoded
9891001
* @expose
1002+
* @see ProtoBuf.Builder.Message.decode64
1003+
* @see ProtoBuf.Builder.Message.decodeHex
9901004
*/
991-
Message.decode = function(buffer) {
992-
buffer = buffer ? (buffer instanceof ByteBuffer ? buffer : ByteBuffer.wrap(buffer)) : new ByteBuffer();
1005+
Message.decode = function(buffer, enc) {
1006+
buffer = buffer ? (buffer instanceof ByteBuffer ? buffer : ByteBuffer.wrap(buffer, enc)) : new ByteBuffer();
9931007
var le = buffer.littleEndian;
9941008
try {
9951009
var msg = T.decode(buffer.LE());
@@ -1014,6 +1028,18 @@
10141028
return Message.decode(ByteBuffer.decode64(str));
10151029
};
10161030

1031+
/**
1032+
* Decodes the message from the specified hex encoded string.
1033+
* @name ProtoBuf.Builder.Message.decodeHex
1034+
* @param {string} str String to decode from
1035+
* @return {!ProtoBuf.Builder.Message} Decoded message
1036+
* @throws {Error} If the message cannot be decoded
1037+
* @expose
1038+
*/
1039+
Message.decodeHex = function(str) {
1040+
return Message.decode(ByteBuffer.decodeHex(str));
1041+
};
1042+
10171043
// Utility
10181044

10191045
/**

0 commit comments

Comments
 (0)