File tree Expand file tree Collapse file tree 2 files changed +12
-6
lines changed Expand file tree Collapse file tree 2 files changed +12
-6
lines changed Original file line number Diff line number Diff line change @@ -1051,11 +1051,6 @@ jspb.utils.byteSourceToUint8Array = function(data) {
1051
1051
return /** @type {!Uint8Array } */ ( new Uint8Array ( data ) ) ;
1052
1052
}
1053
1053
1054
- if ( typeof Buffer != 'undefined' && data . constructor === Buffer ) {
1055
- return /** @type {!Uint8Array } */ (
1056
- new Uint8Array ( /** @type {? } */ ( data ) ) ) ;
1057
- }
1058
-
1059
1054
if ( data . constructor === Array ) {
1060
1055
data = /** @type {!Array<number> } */ ( data ) ;
1061
1056
return /** @type {!Uint8Array } */ ( new Uint8Array ( data ) ) ;
@@ -1066,6 +1061,15 @@ jspb.utils.byteSourceToUint8Array = function(data) {
1066
1061
return goog . crypt . base64 . decodeStringToUint8Array ( data ) ;
1067
1062
}
1068
1063
1064
+ if ( data instanceof Uint8Array ) {
1065
+ // Support types like nodejs Buffer and other subclasses of Uint8Array.
1066
+ data = /** @type {!Uint8Array } */ ( data ) ;
1067
+ // Make a shallow copy to ensure we only ever deal with Uint8Array
1068
+ // exactly to ensure monomorphism.
1069
+ return /** @type {!Uint8Array } */ (
1070
+ new Uint8Array ( data . buffer , data . byteOffset , data . byteLength ) ) ;
1071
+ }
1072
+
1069
1073
goog . asserts . fail ( 'Type not convertible to Uint8Array.' ) ;
1070
1074
return /** @type {!Uint8Array } */ ( new Uint8Array ( 0 ) ) ;
1071
1075
} ;
Original file line number Diff line number Diff line change @@ -719,7 +719,6 @@ describe('binaryUtilsTest', function() {
719
719
var sourceBytes = new Uint8Array ( sourceData ) ;
720
720
var sourceBuffer = sourceBytes . buffer ;
721
721
var sourceBase64 = goog . crypt . base64 . encodeByteArray ( sourceData ) ;
722
- var sourceString = goog . crypt . byteArrayToString ( sourceData ) ;
723
722
724
723
function check ( result ) {
725
724
expect ( result . constructor ) . toEqual ( Uint8Array ) ;
@@ -740,5 +739,8 @@ describe('binaryUtilsTest', function() {
740
739
741
740
// Converting base64-encoded strings into Uint8Arrays should work.
742
741
check ( convert ( sourceBase64 ) ) ;
742
+
743
+ // Existing Uint8Array into Uint8Arrays should work.
744
+ check ( convert ( sourceUint8Array ) ) ;
743
745
} ) ;
744
746
} ) ;
You can’t perform that action at this time.
0 commit comments