1
- import { Metadata , status as Status } from " @grpc/grpc-js" ;
1
+ import { Metadata , status as Status } from ' @grpc/grpc-js' ;
2
2
3
3
export interface GrpcErrorResponse {
4
4
status : Status ;
@@ -22,7 +22,7 @@ function fourBytesLength(sized: { length: number }): Uint8Array {
22
22
23
23
export function decodeGrpcWebBody ( bodyBuffer : Buffer ) : GrpcResponse {
24
24
if ( bodyBuffer . length === 0 ) {
25
- throw new Error ( " Body has zero length, cannot decode!" ) ;
25
+ throw new Error ( ' Body has zero length, cannot decode!' ) ;
26
26
}
27
27
28
28
const bodyRaw = new Uint8Array ( bodyBuffer ) ;
@@ -53,7 +53,7 @@ export function decodeGrpcWebBody(bodyBuffer: Buffer): GrpcResponse {
53
53
const trailersHeader = 0x80 ;
54
54
55
55
if ( bodyRaw . at ( offset ++ ) !== trailersHeader ) {
56
- throw new Error ( " Expected trailers header 0x80" ) ;
56
+ throw new Error ( ' Expected trailers header 0x80' ) ;
57
57
}
58
58
59
59
const trailersLength = readInt32Length ( bodyRaw , offset ) ;
@@ -66,16 +66,16 @@ export function decodeGrpcWebBody(bodyBuffer: Buffer): GrpcResponse {
66
66
67
67
const trailers = new Metadata ( ) ;
68
68
69
- trailersString . split ( " \r\n" ) . forEach ( ( trailer ) => {
70
- const [ key , value ] = trailer . split ( ":" , 2 ) ;
69
+ trailersString . split ( ' \r\n' ) . forEach ( ( trailer ) => {
70
+ const [ key , value ] = trailer . split ( ':' , 2 ) ;
71
71
trailers . set ( key , value ) ;
72
72
} ) ;
73
73
74
74
if ( status !== Status . OK ) {
75
75
return {
76
76
status,
77
77
trailers,
78
- detail : trailers . get ( " grpc-message" ) [ 0 ] as string | undefined ,
78
+ detail : trailers . get ( ' grpc-message' ) [ 0 ] as string | undefined ,
79
79
} ;
80
80
}
81
81
@@ -99,19 +99,17 @@ export class GrpcUnknownStatus extends Error {
99
99
100
100
export function grpcResponseToBuffer ( response : GrpcResponse ) : Buffer {
101
101
// error messages need to have a zero length message field to be considered valid
102
- const message = " message" in response ? response . message : new Uint8Array ( ) ;
102
+ const message = ' message' in response ? response . message : new Uint8Array ( ) ;
103
103
104
104
// all success responses have status OK
105
- const status = " status" in response ? response . status : Status . OK ;
105
+ const status = ' status' in response ? response . status : Status . OK ;
106
106
// error statuses may the detail field to denote a custom error message, otherwise use the string version of the status
107
107
let grpcMessage : string | undefined ;
108
108
109
- if ( " detail" in response ) {
109
+ if ( ' detail' in response ) {
110
110
grpcMessage = response . detail ;
111
111
} else {
112
- const currentStatus = Object . entries ( Status ) . find (
113
- ( [ , code ] ) => code === status
114
- ) ;
112
+ const currentStatus = Object . entries ( Status ) . find ( ( [ , code ] ) => code === status ) ;
115
113
116
114
if ( ! currentStatus ) {
117
115
throw new GrpcUnknownStatus ( status ) ;
0 commit comments