1
1
'use strict' ;
2
2
3
+ const uuidParse = require ( 'uuid-parse' ) ;
4
+ const uuidValidate = require ( 'uuid-validate' ) ;
5
+
3
6
angular
4
7
. module ( 'app' )
5
8
. controller ( 'ClientController' , ClientController ) ;
@@ -24,9 +27,13 @@ function ClientController (GrpcSvc, $stateParams, $scope) {
24
27
var json = { } ;
25
28
26
29
if ( field . type && field . type == "bytes" ) {
27
- transformers [ field . name ] = function ( str ) {
28
- var buffer = new Buffer ( str , "utf-8" ) ;
29
- return buffer ;
30
+ transformers [ field . name ] = function ( str ) {
31
+ if ( uuidValidate ( str ) ) {
32
+ var parsedUUID = uuidParse . parse ( str ) ;
33
+ return new Buffer ( parsedUUID ) ;
34
+ }
35
+
36
+ return new Buffer ( str , "base64" ) ;
30
37
}
31
38
return "" ;
32
39
}
@@ -77,7 +84,16 @@ function ClientController (GrpcSvc, $stateParams, $scope) {
77
84
var transform = function ( obj ) {
78
85
Object . keys ( obj ) . forEach ( function ( key ) {
79
86
if ( obj [ key ] instanceof Buffer ) {
80
- obj [ key ] = obj [ key ] . toString ( "utf-8" ) ;
87
+ // try to convert to UUID. If your data was exactly 16 bytes and not a UUID, uhm... sorry :/
88
+ if ( obj [ key ] . byteLength == 16 ) {
89
+ var parsed = uuidParse . unparse ( obj [ key ] ) ;
90
+ if ( uuidValidate ( parsed ) ) {
91
+ obj [ key ] = parsed ;
92
+ return
93
+ }
94
+ }
95
+
96
+ obj [ key ] = obj [ key ] . toString ( "base64" ) ;
81
97
}
82
98
if ( typeof obj [ key ] == "object" ) {
83
99
transform ( obj [ key ] ) ;
@@ -104,6 +120,9 @@ function ClientController (GrpcSvc, $stateParams, $scope) {
104
120
105
121
var transform = function ( obj ) {
106
122
Object . keys ( obj ) . forEach ( function ( key ) {
123
+ if ( typeof obj != "object" ) {
124
+ return
125
+ }
107
126
if ( transformers [ key ] ) {
108
127
obj [ key ] = transformers [ key ] ( obj [ key ] ) ;
109
128
}
0 commit comments