55} from 'js-beautify/js/lib/beautifier' ;
66import * as beautifyXml from 'xml-beautifier' ;
77
8+ import { Headers } from '../types' ;
89import { bufferToHex , bufferToString , getReadableSize } from '../util/buffer' ;
910import { parseRawProtobuf , extractProtobufFromGrpc } from '../util/protobuf' ;
1011
@@ -13,10 +14,25 @@ const FIVE_MB = 1024 * 1024 * 5;
1314
1415export type WorkerFormatterKey = keyof typeof WorkerFormatters ;
1516
16- export function formatBuffer ( buffer : ArrayBuffer , format : WorkerFormatterKey ) : string {
17- return WorkerFormatters [ format ] ( Buffer . from ( buffer ) ) ;
17+ export function formatBuffer ( buffer : ArrayBuffer , format : WorkerFormatterKey , headers ?: Headers ) : string {
18+ return WorkerFormatters [ format ] ( Buffer . from ( buffer ) , headers ) ;
1819}
1920
21+ const prettyProtobufView = ( data : any ) => JSON . stringify ( data , ( _key , value ) => {
22+ // Buffers have toJSON defined, so arrive here in JSONified form:
23+ if ( value . type === 'Buffer' && Array . isArray ( value . data ) ) {
24+ const buffer = Buffer . from ( value . data ) ;
25+
26+ return {
27+ "Type" : `Buffer (${ getReadableSize ( buffer ) } )` ,
28+ "As string" : bufferToString ( buffer , 'detect-encoding' ) ,
29+ "As hex" : bufferToHex ( buffer )
30+ }
31+ } else {
32+ return value ;
33+ }
34+ } , 2 ) ;
35+
2036// A subset of all possible formatters (those allowed by body-formatting), which require
2137// non-trivial processing, and therefore need to be processed async.
2238const WorkerFormatters = {
@@ -76,44 +92,15 @@ const WorkerFormatters = {
7692 } ) ;
7793 } ,
7894 protobuf : ( content : Buffer ) => {
79- const data = parseRawProtobuf ( content , {
80- prefix : ''
81- } ) ;
82-
83- return JSON . stringify ( data , ( _key , value ) => {
84- // Buffers have toJSON defined, so arrive here in JSONified form:
85- if ( value . type === 'Buffer' && Array . isArray ( value . data ) ) {
86- const buffer = Buffer . from ( value . data ) ;
87-
88- return {
89- "Type" : `Buffer (${ getReadableSize ( buffer ) } )` ,
90- "As string" : bufferToString ( buffer , 'detect-encoding' ) ,
91- "As hex" : bufferToHex ( buffer )
92- }
93- } else {
94- return value ;
95- }
96- } , 2 ) ;
95+ const data = parseRawProtobuf ( content , { prefix : '' } ) ;
96+ return prettyProtobufView ( data ) ;
9797 } ,
98- 'grpc-proto' : ( content : Buffer ) => {
99- const protobufMessages = extractProtobufFromGrpc ( content ) ;
98+ 'grpc-proto' : ( content : Buffer , headers ?: Headers ) => {
99+ const protobufMessages = extractProtobufFromGrpc ( content , headers ?? { } ) ;
100100
101101 let data = protobufMessages . map ( ( msg ) => parseRawProtobuf ( msg , { prefix : '' } ) ) ;
102102 if ( data . length === 1 ) data = data [ 0 ] ;
103103
104- return JSON . stringify ( data , ( _key , value ) => {
105- // Buffers have toJSON defined, so arrive here in JSONified form:
106- if ( value . type === 'Buffer' && Array . isArray ( value . data ) ) {
107- const buffer = Buffer . from ( value . data ) ;
108-
109- return {
110- "Type" : `Buffer (${ getReadableSize ( buffer ) } )` ,
111- "As string" : bufferToString ( buffer , 'detect-encoding' ) ,
112- "As hex" : bufferToHex ( buffer )
113- }
114- } else {
115- return value ;
116- }
117- } , 2 ) ;
104+ return prettyProtobufView ( data ) ;
118105 }
119106} as const ;
0 commit comments