@@ -553,102 +553,100 @@ 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
+ async 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 ) ;
575
578
576
- function onData ( chunk : Buffer ) {
577
- receivedLength += chunk . byteLength ;
579
+ reject ( {
580
+ code : Status . RESOURCE_EXHAUSTED ,
581
+ details : `Received message larger than max (${ receivedLength } vs. ${ limit } )` ,
582
+ } ) ;
583
+ return ;
584
+ }
585
+
586
+ body . push ( chunk ) ;
587
+ }
578
588
579
- if ( limit !== - 1 && receivedLength > limit ) {
589
+ async 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
-
590
- body . push ( chunk ) ;
591
- }
592
593
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
+ call . safeDeserializeMessage ( decompressedMessage , resolve , reject ) ;
619
+ return ;
620
+ }
620
621
621
- if ( Buffer . isBuffer ( decompressedMessage ) ) {
622
- call . safeDeserializeMessage ( decompressedMessage , next ) ;
623
- return ;
622
+ decompressedMessage . then (
623
+ decompressed =>
624
+ call . safeDeserializeMessage ( decompressed , resolve , reject ) ,
625
+ ( err : any ) =>
626
+ reject (
627
+ err . code
628
+ ? err
629
+ : {
630
+ code : Status . INTERNAL ,
631
+ details : `Received "grpc-encoding" header "${ encoding } " but ${ encoding } decompression failed` ,
632
+ }
633
+ )
634
+ ) ;
624
635
}
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
- }
636
+ } ) ;
639
637
}
640
638
641
639
private safeDeserializeMessage (
642
640
buffer : Buffer ,
643
- next : (
644
- err : Partial < ServerStatusResponse > | null ,
645
- request ?: RequestType
646
- ) => void
641
+ resolve : (
642
+ value : void | RequestType | PromiseLike < void | RequestType >
643
+ ) => void ,
644
+ reject : ( reason : any ) => void
647
645
) {
648
646
try {
649
- next ( null , this . deserializeMessage ( buffer ) ) ;
647
+ resolve ( this . deserializeMessage ( buffer ) ) ;
650
648
} catch ( err ) {
651
- next ( {
649
+ reject ( {
652
650
details : getErrorMessage ( err ) ,
653
651
code : Status . INTERNAL ,
654
652
} ) ;
0 commit comments