From bcb35066411e46bde936bc4c83405b7bd280fb71 Mon Sep 17 00:00:00 2001 From: Paul Jolly Date: Sun, 19 Mar 2017 13:51:20 +0000 Subject: Fix #1562 by using goog.crypt.byteArrayToString instead of String.fromCharCode.apply --- js/binary/decoder.js | 2 +- js/binary/utils.js | 2 +- js/binary/utils_test.js | 24 ++++++++++++------------ 3 files changed, 14 insertions(+), 14 deletions(-) (limited to 'js') diff --git a/js/binary/decoder.js b/js/binary/decoder.js index ad9cb01b..6db28e7c 100644 --- a/js/binary/decoder.js +++ b/js/binary/decoder.js @@ -994,7 +994,7 @@ jspb.BinaryDecoder.prototype.readString = function(length) { codeUnits.length = 0; } } - result += String.fromCharCode.apply(null, codeUnits); + result += goog.crypt.byteArrayToString(codeUnits); this.cursor_ = cursor; return result; }; diff --git a/js/binary/utils.js b/js/binary/utils.js index 7702020b..df16249e 100644 --- a/js/binary/utils.js +++ b/js/binary/utils.js @@ -613,7 +613,7 @@ jspb.utils.decimalStringToHash64 = function(dec) { muladd(1, 1); } - return String.fromCharCode.apply(null, resultBytes); + return goog.crypt.byteArrayToString(resultBytes); }; diff --git a/js/binary/utils_test.js b/js/binary/utils_test.js index d27e5ea2..0a2f4f0a 100644 --- a/js/binary/utils_test.js +++ b/js/binary/utils_test.js @@ -205,31 +205,31 @@ describe('binaryUtilsTest', function() { var convert = jspb.utils.decimalStringToHash64; result = convert('0'); - assertEquals(String.fromCharCode.apply(null, + assertEquals(goog.crypt.byteArrayToString( [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), result); result = convert('-1'); - assertEquals(String.fromCharCode.apply(null, + assertEquals(goog.crypt.byteArrayToString( [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), result); result = convert('18446744073709551615'); - assertEquals(String.fromCharCode.apply(null, + assertEquals(goog.crypt.byteArrayToString( [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), result); result = convert('9223372036854775808'); - assertEquals(String.fromCharCode.apply(null, + assertEquals(goog.crypt.byteArrayToString( [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), result); result = convert('-9223372036854775808'); - assertEquals(String.fromCharCode.apply(null, + assertEquals(goog.crypt.byteArrayToString( [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]), result); result = convert('123456789123456789'); - assertEquals(String.fromCharCode.apply(null, + assertEquals(goog.crypt.byteArrayToString( [0x15, 0x5F, 0xD0, 0xAC, 0x4B, 0x9B, 0xB6, 0x01]), result); result = convert('-123456789123456789'); - assertEquals(String.fromCharCode.apply(null, + assertEquals(goog.crypt.byteArrayToString( [0xEB, 0xA0, 0x2F, 0x53, 0xB4, 0x64, 0x49, 0xFE]), result); }); @@ -259,21 +259,21 @@ describe('binaryUtilsTest', function() { var convert = jspb.utils.hexStringToHash64; result = convert('0x0000000000000000'); - assertEquals(String.fromCharCode.apply(null, + assertEquals(goog.crypt.byteArrayToString( [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), result); result = convert('0xffffffffffffffff'); - assertEquals(String.fromCharCode.apply(null, + assertEquals(goog.crypt.byteArrayToString( [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]), result); // Hex string is big-endian, hash string is little-endian. result = convert('0x123456789ABCDEF0'); - assertEquals(String.fromCharCode.apply(null, + assertEquals(goog.crypt.byteArrayToString( [0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12]), result); // Capitalization should not matter. result = convert('0x0000abcdefABCDEF'); - assertEquals(String.fromCharCode.apply(null, + assertEquals(goog.crypt.byteArrayToString( [0xEF, 0xCD, 0xAB, 0xEF, 0xCD, 0xAB, 0x00, 0x00]), result); }); @@ -643,7 +643,7 @@ describe('binaryUtilsTest', function() { var sourceBytes = new Uint8Array(sourceData); var sourceBuffer = sourceBytes.buffer; var sourceBase64 = goog.crypt.base64.encodeByteArray(sourceData); - var sourceString = String.fromCharCode.apply(null, sourceData); + var sourceString = goog.crypt.byteArrayToString(sourceData); function check(result) { assertEquals(Uint8Array, result.constructor); -- cgit v1.2.3 From f00e06c95bc117fb2ed0ca56c96041c93039f1fe Mon Sep 17 00:00:00 2001 From: Adam Cozzette Date: Tue, 2 May 2017 17:13:27 -0700 Subject: Removed mention of Buffer in byteSourceToUint8Array The Closure compiler complains about Buffer since that class exists only in Node. That logic does not seem to be needed (unit tests and conformance tests pass without it), so let's just remove it to solve the problem. --- js/binary/utils.js | 4 ---- 1 file changed, 4 deletions(-) (limited to 'js') diff --git a/js/binary/utils.js b/js/binary/utils.js index df16249e..58f11b54 100644 --- a/js/binary/utils.js +++ b/js/binary/utils.js @@ -970,10 +970,6 @@ jspb.utils.byteSourceToUint8Array = function(data) { return /** @type {!Uint8Array} */(new Uint8Array(data)); } - if (data.constructor === Buffer) { - return /** @type {!Uint8Array} */(new Uint8Array(data)); - } - if (data.constructor === Array) { data = /** @type {!Array.} */(data); return /** @type {!Uint8Array} */(new Uint8Array(data)); -- cgit v1.2.3 From 36fcc2a5d862742a83ee5c01c05c6be620d294b0 Mon Sep 17 00:00:00 2001 From: dylanetaft Date: Sat, 15 Jul 2017 01:52:07 -0400 Subject: Expand documentation in Readme.md Add some supporting documentation regarding Closure for those unfamiliar., Also substantiate details for "files in this directory" - including them all will result in a project that does not compile as some are used for test cases and aren't part of the core library. --- js/README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'js') diff --git a/js/README.md b/js/README.md index f4184621..24386dc7 100644 --- a/js/README.md +++ b/js/README.md @@ -19,7 +19,9 @@ resolve imports at compile time. To use Protocol Buffers with JavaScript, you need two main components: 1. The protobuf runtime library. You can install this with - `npm install google-protobuf`, or use the files in this directory. + `npm install google-protobuf`, or use the files in this directory. + If npm is not being used, as of 3.3.0, the files needed are located in binary subdirectory; + arith.js, constants.js, decoder.js, encoder.js, map.js, message.js, reader.js, utils.js, writer.js 2. The Protocol Compiler `protoc`. This translates `.proto` files into `.js` files. The compiler is not currently available via npm, but you can download a pre-built binary @@ -93,6 +95,12 @@ statements like: var message = proto.my.package.MyMessage(); +If unfamiliar with Closure or it's compiler, consider reviewing Closure documentation +https://developers.google.com/closure/library/docs/tutorial +https://developers.google.com/closure/library/docs/closurebuilder +https://developers.google.com/closure/library/docs/depswriter +At a high level, closurebuilder.py can walk dependencies, and compile your code, and all dependencies for Protobuf into a single .js file. Using depsbuilder.py to generate a dependency file can also be considered for non-production dev environments. + CommonJS imports ---------------- -- cgit v1.2.3