@@ -37,6 +37,7 @@ const {
3737 isPayloadMeaningless,
3838 mapToHeaders,
3939 NghttpError,
40+ sessionName,
4041 toHeaderObject,
4142 updateOptionsBuffer,
4243 updateSettingsBuffer
@@ -87,7 +88,6 @@ const {
8788 NGHTTP2_CANCEL ,
8889 NGHTTP2_DEFAULT_WEIGHT ,
8990 NGHTTP2_FLAG_END_STREAM ,
90- NGHTTP2_HCAT_HEADERS ,
9191 NGHTTP2_HCAT_PUSH_RESPONSE ,
9292 NGHTTP2_HCAT_RESPONSE ,
9393 NGHTTP2_INTERNAL_ERROR ,
@@ -128,17 +128,6 @@ const {
128128 HTTP_STATUS_SWITCHING_PROTOCOLS
129129} = constants ;
130130
131- function sessionName ( type ) {
132- switch ( type ) {
133- case NGHTTP2_SESSION_CLIENT :
134- return 'client' ;
135- case NGHTTP2_SESSION_SERVER :
136- return 'server' ;
137- default :
138- return '<invalid>' ;
139- }
140- }
141-
142131// Top level to avoid creating a closure
143132function emit ( ) {
144133 this . emit . apply ( this , arguments ) ;
@@ -163,59 +152,43 @@ function onSessionHeaders(id, cat, flags, headers) {
163152 const obj = toHeaderObject ( headers ) ;
164153
165154 if ( stream === undefined ) {
166- switch ( owner [ kType ] ) {
167- case NGHTTP2_SESSION_SERVER :
168- stream = new ServerHttp2Stream ( owner , id ,
169- { readable : ! endOfStream } ,
170- obj ) ;
171- if ( obj [ HTTP2_HEADER_METHOD ] === HTTP2_METHOD_HEAD ) {
172- // For head requests, there must not be a body...
173- // end the writable side immediately.
174- stream . end ( ) ;
175- const state = stream [ kState ] ;
176- state . headRequest = true ;
177- }
178- break ;
179- case NGHTTP2_SESSION_CLIENT :
180- stream = new ClientHttp2Stream ( owner , id , { readable : ! endOfStream } ) ;
181- break ;
182- default :
183- assert . fail ( null , null ,
184- 'Internal HTTP/2 Error. Invalid session type. Please ' +
185- 'report this as a bug in Node.js' ) ;
155+ // owner[kType] can be only one of two possible values
156+ if ( owner [ kType ] === NGHTTP2_SESSION_SERVER ) {
157+ stream = new ServerHttp2Stream ( owner , id ,
158+ { readable : ! endOfStream } ,
159+ obj ) ;
160+ if ( obj [ HTTP2_HEADER_METHOD ] === HTTP2_METHOD_HEAD ) {
161+ // For head requests, there must not be a body...
162+ // end the writable side immediately.
163+ stream . end ( ) ;
164+ const state = stream [ kState ] ;
165+ state . headRequest = true ;
166+ }
167+ } else {
168+ stream = new ClientHttp2Stream ( owner , id , { readable : ! endOfStream } ) ;
186169 }
187170 streams . set ( id , stream ) ;
188171 process . nextTick ( emit . bind ( owner , 'stream' , stream , obj , flags , headers ) ) ;
189172 } else {
190173 let event ;
191- let status ;
192- switch ( cat ) {
193- case NGHTTP2_HCAT_RESPONSE :
194- status = obj [ HTTP2_HEADER_STATUS ] ;
195- if ( ! endOfStream &&
196- status !== undefined &&
197- status >= 100 &&
198- status < 200 ) {
199- event = 'headers' ;
200- } else {
201- event = 'response' ;
202- }
203- break ;
204- case NGHTTP2_HCAT_PUSH_RESPONSE :
205- event = 'push' ;
206- break ;
207- case NGHTTP2_HCAT_HEADERS :
208- status = obj [ HTTP2_HEADER_STATUS ] ;
209- if ( ! endOfStream && status !== undefined && status >= 200 ) {
210- event = 'response' ;
211- } else {
212- event = endOfStream ? 'trailers' : 'headers' ;
213- }
214- break ;
215- default :
216- assert . fail ( null , null ,
217- 'Internal HTTP/2 Error. Invalid headers category. Please ' +
218- 'report this as a bug in Node.js' ) ;
174+ const status = obj [ HTTP2_HEADER_STATUS ] ;
175+ if ( cat === NGHTTP2_HCAT_RESPONSE ) {
176+ if ( ! endOfStream &&
177+ status !== undefined &&
178+ status >= 100 &&
179+ status < 200 ) {
180+ event = 'headers' ;
181+ } else {
182+ event = 'response' ;
183+ }
184+ } else if ( cat === NGHTTP2_HCAT_PUSH_RESPONSE ) {
185+ event = 'push' ;
186+ } else { // cat === NGHTTP2_HCAT_HEADERS:
187+ if ( ! endOfStream && status !== undefined && status >= 200 ) {
188+ event = 'response' ;
189+ } else {
190+ event = endOfStream ? 'trailers' : 'headers' ;
191+ }
219192 }
220193 debug ( `[${ sessionName ( owner [ kType ] ) } ] emitting stream '${ event } ' event` ) ;
221194 process . nextTick ( emit . bind ( stream , event , obj , flags , headers ) ) ;
@@ -242,8 +215,10 @@ function onSessionTrailers(id) {
242215 'Internal HTTP/2 Failure. Stream does not exist. Please ' +
243216 'report this as a bug in Node.js' ) ;
244217 const getTrailers = stream [ kState ] . getTrailers ;
245- if ( typeof getTrailers !== 'function' )
246- return [ ] ;
218+ // We should not have been able to get here at all if getTrailers
219+ // was not a function, and there's no code that could have changed
220+ // it between there and here.
221+ assert . strictEqual ( typeof getTrailers , 'function' ) ;
247222 const trailers = Object . create ( null ) ;
248223 getTrailers . call ( stream , trailers ) ;
249224 const headersList = mapToHeaders ( trailers , assertValidPseudoHeaderTrailer ) ;
@@ -2480,9 +2455,10 @@ function connect(authority, options, listener) {
24802455}
24812456
24822457function createSecureServer ( options , handler ) {
2483- if ( typeof options === 'function' ) {
2484- handler = options ;
2485- options = Object . create ( null ) ;
2458+ if ( options == null || typeof options !== 'object' ) {
2459+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' ,
2460+ 'options' ,
2461+ 'object' ) ;
24862462 }
24872463 debug ( 'creating http2secureserver' ) ;
24882464 return new Http2SecureServer ( options , handler ) ;
0 commit comments