@@ -553,105 +553,99 @@ export class Http2ServerCallStream<
553
553
return metadata ;
554
554
}
555
555
556
- receiveUnaryMessage (
557
- encoding : string ,
558
- next : (
559
- err : Partial < ServerStatusResponse > | null ,
560
- request ?: RequestType
561
- ) => void
562
- ) : void {
563
- const { stream } = this ;
556
+ receiveUnaryMessage ( encoding : string ) : Promise < RequestType | void > {
557
+ return new Promise ( ( resolve , reject ) => {
558
+ const { stream } = this ;
559
+
560
+ let receivedLength = 0 ;
561
+
562
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
563
+ const call = this ;
564
+ const body : Buffer [ ] = [ ] ;
565
+ const limit = this . maxReceiveMessageSize ;
564
566
565
- let receivedLength = 0 ;
567
+ this . stream . on ( 'data' , onData ) ;
568
+ this . stream . on ( 'end' , onEnd ) ;
569
+ this . stream . on ( 'error' , onEnd ) ;
566
570
567
- // eslint-disable-next-line @typescript-eslint/no-this-alias
568
- const call = this ;
569
- const body : Buffer [ ] = [ ] ;
570
- const limit = this . maxReceiveMessageSize ;
571
+ function onData ( chunk : Buffer ) {
572
+ receivedLength += chunk . byteLength ;
571
573
572
- stream . on ( 'data' , onData ) ;
573
- stream . on ( 'end' , onEnd ) ;
574
- stream . on ( 'error' , onEnd ) ;
574
+ if ( limit !== - 1 && receivedLength > limit ) {
575
+ stream . removeListener ( 'data' , onData ) ;
576
+ stream . removeListener ( 'end' , onEnd ) ;
577
+ stream . removeListener ( 'error' , onEnd ) ;
578
+
579
+ reject ( {
580
+ code : Status . RESOURCE_EXHAUSTED ,
581
+ details : `Received message larger than max (${ receivedLength } vs. ${ limit } )` ,
582
+ } ) ;
583
+ return ;
584
+ }
575
585
576
- function onData ( chunk : Buffer ) {
577
- receivedLength += chunk . byteLength ;
586
+ body . push ( chunk ) ;
587
+ }
578
588
579
- if ( limit !== - 1 && receivedLength > limit ) {
589
+ function onEnd ( err ?: Error ) {
580
590
stream . removeListener ( 'data' , onData ) ;
581
591
stream . removeListener ( 'end' , onEnd ) ;
582
592
stream . removeListener ( 'error' , onEnd ) ;
583
- next ( {
584
- code : Status . RESOURCE_EXHAUSTED ,
585
- details : `Received message larger than max (${ receivedLength } vs. ${ limit } )` ,
586
- } ) ;
587
- return ;
588
- }
589
593
590
- body . push ( chunk ) ;
591
- }
592
-
593
- function onEnd ( err ?: Error ) {
594
- stream . removeListener ( 'data' , onData ) ;
595
- stream . removeListener ( 'end' , onEnd ) ;
596
- stream . removeListener ( 'error' , onEnd ) ;
594
+ if ( err !== undefined ) {
595
+ reject ( { code : Status . INTERNAL , details : err . message } ) ;
596
+ return ;
597
+ }
597
598
598
- if ( err !== undefined ) {
599
- next ( { code : Status . INTERNAL , details : err . message } ) ;
600
- return ;
601
- }
599
+ if ( receivedLength === 0 ) {
600
+ reject ( {
601
+ code : Status . INTERNAL ,
602
+ details : 'received empty unary message' ,
603
+ } ) ;
604
+ return ;
605
+ }
602
606
603
- if ( receivedLength === 0 ) {
604
- next ( {
605
- code : Status . INTERNAL ,
606
- details : 'received empty unary message' ,
607
- } ) ;
608
- return ;
609
- }
607
+ call . emit ( 'receiveMessage' ) ;
610
608
611
- call . emit ( 'receiveMessage' ) ;
609
+ const requestBytes = Buffer . concat ( body , receivedLength ) ;
610
+ const compressed = requestBytes . readUInt8 ( 0 ) === 1 ;
611
+ const compressedMessageEncoding = compressed ? encoding : 'identity' ;
612
+ const decompressedMessage = call . getDecompressedMessage (
613
+ requestBytes ,
614
+ compressedMessageEncoding
615
+ ) ;
612
616
613
- const requestBytes = Buffer . concat ( body , receivedLength ) ;
614
- const compressed = requestBytes . readUInt8 ( 0 ) === 1 ;
615
- const compressedMessageEncoding = compressed ? encoding : 'identity' ;
616
- const decompressedMessage = call . getDecompressedMessage (
617
- requestBytes ,
618
- compressedMessageEncoding
619
- ) ;
617
+ if ( Buffer . isBuffer ( decompressedMessage ) ) {
618
+ resolve (
619
+ call . deserializeMessageWithInternalError ( decompressedMessage )
620
+ ) ;
621
+ return ;
622
+ }
620
623
621
- if ( Buffer . isBuffer ( decompressedMessage ) ) {
622
- call . safeDeserializeMessage ( decompressedMessage , next ) ;
623
- return ;
624
+ decompressedMessage . then (
625
+ decompressed =>
626
+ resolve ( call . deserializeMessageWithInternalError ( decompressed ) ) ,
627
+ ( err : any ) =>
628
+ reject (
629
+ err . code
630
+ ? err
631
+ : {
632
+ code : Status . INTERNAL ,
633
+ details : `Received "grpc-encoding" header "${ encoding } " but ${ encoding } decompression failed` ,
634
+ }
635
+ )
636
+ ) ;
624
637
}
625
-
626
- decompressedMessage . then (
627
- decompressed => call . safeDeserializeMessage ( decompressed , next ) ,
628
- ( err : any ) =>
629
- next (
630
- err . code
631
- ? err
632
- : {
633
- code : Status . INTERNAL ,
634
- details : `Received "grpc-encoding" header "${ encoding } " but ${ encoding } decompression failed` ,
635
- }
636
- )
637
- ) ;
638
- }
638
+ } ) ;
639
639
}
640
640
641
- private safeDeserializeMessage (
642
- buffer : Buffer ,
643
- next : (
644
- err : Partial < ServerStatusResponse > | null ,
645
- request ?: RequestType
646
- ) => void
647
- ) {
641
+ private async deserializeMessageWithInternalError ( buffer : Buffer ) {
648
642
try {
649
- next ( null , this . deserializeMessage ( buffer ) ) ;
643
+ return this . deserializeMessage ( buffer ) ;
650
644
} catch ( err ) {
651
- next ( {
645
+ throw {
652
646
details : getErrorMessage ( err ) ,
653
647
code : Status . INTERNAL ,
654
- } ) ;
648
+ } ;
655
649
}
656
650
}
657
651
0 commit comments