@@ -7,11 +7,13 @@ const Net = require('net');
7
7
const Path = require ( 'path' ) ;
8
8
const Stream = require ( 'stream' ) ;
9
9
const Zlib = require ( 'zlib' ) ;
10
+ const Events = require ( 'events' ) ;
10
11
11
12
const Boom = require ( '@hapi/boom' ) ;
12
13
const Code = require ( '@hapi/code' ) ;
13
14
const Hapi = require ( '..' ) ;
14
15
const Hoek = require ( '@hapi/hoek' ) ;
16
+ const Bounce = require ( '@hapi/bounce' ) ;
15
17
const Inert = require ( '@hapi/inert' ) ;
16
18
const Lab = require ( '@hapi/lab' ) ;
17
19
const LegacyReadableStream = require ( 'legacy-readable-stream' ) ;
@@ -1921,6 +1923,63 @@ describe('transmission', () => {
1921
1923
const res = await server . inject ( '/' ) ;
1922
1924
expect ( res . statusCode ) . to . equal ( 500 ) ;
1923
1925
} ) ;
1926
+
1927
+ it ( 'permits ending reading request stream while transmitting response.' , async ( flags ) => {
1928
+
1929
+ const server = Hapi . server ( ) ;
1930
+
1931
+ server . route ( {
1932
+ method : 'post' ,
1933
+ path : '/' ,
1934
+ options : {
1935
+ payload : {
1936
+ output : 'stream'
1937
+ }
1938
+ } ,
1939
+ handler : ( request , h ) => {
1940
+
1941
+ const stream = new Stream . PassThrough ( ) ;
1942
+
1943
+ // Start transmitting stream response...
1944
+ stream . push ( 'hello ' ) ;
1945
+
1946
+ Bounce . background ( async ( ) => {
1947
+
1948
+ await Events . once ( request . raw . res , 'pipe' ) ;
1949
+
1950
+ // ...but also only read and end the request once the response is transmitting...
1951
+ request . raw . req . on ( 'data' , Hoek . ignore ) ;
1952
+ await Events . once ( request . raw . req , 'end' ) ;
1953
+
1954
+ // ...and finally end the intended response once the request stream has ended.
1955
+ stream . end ( 'world' ) ;
1956
+ } ) ;
1957
+
1958
+ return h . response ( stream ) ;
1959
+ }
1960
+ } ) ;
1961
+
1962
+ flags . onCleanup = ( ) => server . stop ( ) ;
1963
+ await server . start ( ) ;
1964
+
1965
+ const req = Http . request ( {
1966
+ hostname : 'localhost' ,
1967
+ port : server . info . port ,
1968
+ method : 'post'
1969
+ } ) ;
1970
+
1971
+ req . end ( '{}' ) ;
1972
+
1973
+ const [ res ] = await Events . once ( req , 'response' ) ;
1974
+
1975
+ let result = '' ;
1976
+ for await ( const chunk of res ) {
1977
+ result += chunk . toString ( ) ;
1978
+ }
1979
+
1980
+ // If not permitted then result will be "hello " without "world"
1981
+ expect ( result ) . to . equal ( 'hello world' ) ;
1982
+ } ) ;
1924
1983
} ) ;
1925
1984
1926
1985
describe ( 'length()' , ( ) => {
0 commit comments