@@ -176,7 +176,7 @@ function lazyllhttp () {
176176 }
177177
178178 }
179- } )
179+ } ) . exports
180180}
181181
182182let llhttpInstance = null
@@ -210,8 +210,8 @@ class Parser {
210210 * @param {import('net').Socket } socket
211211 * @param {* } llhttp
212212 */
213- constructor ( client , socket , { exports } ) {
214- this . llhttp = exports
213+ constructor ( client , socket , llhttp ) {
214+ this . llhttp = llhttp
215215 this . ptr = this . llhttp . llhttp_alloc ( constants . TYPE . RESPONSE )
216216 this . client = client
217217 /**
@@ -329,46 +329,44 @@ class Parser {
329329
330330 new Uint8Array ( llhttp . memory . buffer , currentBufferPtr , currentBufferSize ) . set ( chunk )
331331
332+ let ret
333+
334+ try {
335+ currentBufferRef = chunk
336+ currentParser = this
337+ ret = llhttp . llhttp_execute ( this . ptr , currentBufferPtr , chunk . length )
338+ } catch ( err ) {
339+ util . destroy ( socket , err )
340+ } finally {
341+ currentParser = null
342+ currentBufferRef = null
343+ }
344+
332345 // Call `execute` on the wasm parser.
333346 // We pass the `llhttp_parser` pointer address, the pointer address of buffer view data,
334347 // and finally the length of bytes to parse.
335348 // The return value is an error code or `constants.ERROR.OK`.
336- try {
337- let ret
338-
339- try {
340- currentBufferRef = chunk
341- currentParser = this
342- ret = llhttp . llhttp_execute ( this . ptr , currentBufferPtr , chunk . length )
343- } finally {
344- currentParser = null
345- currentBufferRef = null
346- }
347-
348- if ( ret !== constants . ERROR . OK ) {
349- const data = chunk . subarray ( llhttp . llhttp_get_error_pos ( this . ptr ) - currentBufferPtr )
350-
351- if ( ret === constants . ERROR . PAUSED_UPGRADE ) {
352- this . onUpgrade ( data )
353- } else if ( ret === constants . ERROR . PAUSED ) {
354- this . paused = true
355- socket . unshift ( data )
356- } else {
357- const ptr = llhttp . llhttp_get_error_reason ( this . ptr )
358- let message = ''
359- /* istanbul ignore else: difficult to make a test case for */
360- if ( ptr ) {
361- const len = new Uint8Array ( llhttp . memory . buffer , ptr ) . indexOf ( 0 )
362- message =
363- 'Response does not match the HTTP/1.1 protocol (' +
364- Buffer . from ( llhttp . memory . buffer , ptr , len ) . toString ( ) +
365- ')'
366- }
367- throw new HTTPParserError ( message , constants . ERROR [ ret ] , data )
349+ if ( ret !== constants . ERROR . OK ) {
350+ const data = chunk . subarray ( llhttp . llhttp_get_error_pos ( this . ptr ) - currentBufferPtr )
351+
352+ if ( ret === constants . ERROR . PAUSED_UPGRADE ) {
353+ this . onUpgrade ( data )
354+ } else if ( ret === constants . ERROR . PAUSED ) {
355+ this . paused = true
356+ socket . unshift ( data )
357+ } else {
358+ const ptr = llhttp . llhttp_get_error_reason ( this . ptr )
359+ let message = ''
360+ /* istanbul ignore else: difficult to make a test case for */
361+ if ( ptr ) {
362+ const len = new Uint8Array ( llhttp . memory . buffer , ptr ) . indexOf ( 0 )
363+ message =
364+ 'Response does not match the HTTP/1.1 protocol (' +
365+ Buffer . from ( llhttp . memory . buffer , ptr , len ) . toString ( ) +
366+ ')'
368367 }
368+ util . destroy ( socket , new HTTPParserError ( message , constants . ERROR [ ret ] , data ) )
369369 }
370- } catch ( err ) {
371- util . destroy ( socket , err )
372370 }
373371 }
374372
@@ -1342,7 +1340,7 @@ function writeBuffer (abort, body, client, request, socket, contentLength, heade
13421340 socket . cork ( )
13431341 socket . write ( `${ header } content-length: ${ contentLength } \r\n\r\n` , 'latin1' )
13441342 socket . write ( body )
1345- socket . uncork ( )
1343+ process . nextTick ( ( ) => socket . uncork ( ) )
13461344 request . onBodySent ( body )
13471345
13481346 if ( ! expectsPayload && request . reset !== false ) {
@@ -1366,31 +1364,35 @@ function writeBuffer (abort, body, client, request, socket, contentLength, heade
13661364 * @param {number } contentLength
13671365 * @param {string } header
13681366 * @param {boolean } expectsPayload
1369- * @returns {Promise< void> }
1367+ * @returns {void }
13701368 */
1371- async function writeBlob ( abort , body , client , request , socket , contentLength , header , expectsPayload ) {
1369+ function writeBlob ( abort , body , client , request , socket , contentLength , header , expectsPayload ) {
13721370 assert ( contentLength === body . size , 'blob body must have content length' )
13731371
13741372 try {
13751373 if ( contentLength != null && contentLength !== body . size ) {
13761374 throw new RequestContentLengthMismatchError ( )
13771375 }
13781376
1379- const buffer = Buffer . from ( await body . arrayBuffer ( ) )
1377+ body . arrayBuffer ( ) . then ( arrayBuffer => {
1378+ const buffer = Buffer . from ( arrayBuffer )
13801379
1381- socket . cork ( )
1382- socket . write ( `${ header } content-length: ${ contentLength } \r\n\r\n` , 'latin1' )
1383- socket . write ( buffer )
1384- socket . uncork ( )
1380+ socket . cork ( )
1381+ socket . write ( `${ header } content-length: ${ contentLength } \r\n\r\n` , 'latin1' )
1382+ socket . write ( buffer )
1383+ process . nextTick ( ( ) => socket . uncork ( ) )
13851384
1386- request . onBodySent ( buffer )
1387- request . onRequestSent ( )
1385+ request . onBodySent ( buffer )
1386+ request . onRequestSent ( )
13881387
1389- if ( ! expectsPayload && request . reset !== false ) {
1390- socket [ kReset ] = true
1391- }
1388+ if ( ! expectsPayload && request . reset !== false ) {
1389+ socket [ kReset ] = true
1390+ }
13921391
1393- client [ kResume ] ( )
1392+ client [ kResume ] ( )
1393+ } ) . catch ( err => {
1394+ abort ( err )
1395+ } )
13941396 } catch ( err ) {
13951397 abort ( err )
13961398 }
@@ -1532,7 +1534,7 @@ class AsyncWriter {
15321534
15331535 const ret = socket . write ( chunk )
15341536
1535- socket . uncork ( )
1537+ process . nextTick ( ( ) => socket . uncork ( ) )
15361538
15371539 request . onBodySent ( chunk )
15381540
@@ -1565,6 +1567,7 @@ class AsyncWriter {
15651567 return
15661568 }
15671569
1570+ socket . cork ( )
15681571 if ( bytesWritten === 0 ) {
15691572 if ( expectsPayload ) {
15701573 // https://tools.ietf.org/html/rfc7230#section-3.3.2
@@ -1579,6 +1582,7 @@ class AsyncWriter {
15791582 } else if ( contentLength === null ) {
15801583 socket . write ( '\r\n0\r\n\r\n' , 'latin1' )
15811584 }
1585+ process . nextTick ( ( ) => socket . uncork ( ) )
15821586
15831587 if ( contentLength !== null && bytesWritten !== contentLength ) {
15841588 if ( client [ kStrictContentLength ] ) {
0 commit comments