Skip to content

Commit a67205c

Browse files
committed
what now?
1 parent 39b0f2d commit a67205c

File tree

9 files changed

+194
-173
lines changed

9 files changed

+194
-173
lines changed

lib/api/api-request.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,29 @@ class RequestHandler extends AsyncResource {
1616

1717
const { signal, method, opaque, body, onInfo, responseHeaders, highWaterMark } = opts
1818

19-
try {
20-
if (typeof callback !== 'function') {
21-
throw new InvalidArgumentError('invalid callback')
22-
}
23-
24-
if (highWaterMark && (typeof highWaterMark !== 'number' || highWaterMark < 0)) {
25-
throw new InvalidArgumentError('invalid highWaterMark')
26-
}
27-
28-
if (signal && typeof signal.on !== 'function' && typeof signal.addEventListener !== 'function') {
29-
throw new InvalidArgumentError('signal must be an EventEmitter or EventTarget')
30-
}
31-
32-
if (method === 'CONNECT') {
33-
throw new InvalidArgumentError('invalid method')
34-
}
19+
let error = null
3520

36-
if (onInfo && typeof onInfo !== 'function') {
37-
throw new InvalidArgumentError('invalid onInfo callback')
38-
}
21+
if (typeof callback !== 'function') {
22+
error = new InvalidArgumentError('invalid callback')
23+
} else if (highWaterMark && (typeof highWaterMark !== 'number' || highWaterMark < 0)) {
24+
error = new InvalidArgumentError('invalid highWaterMark')
25+
} else if (signal && typeof signal.on !== 'function' && typeof signal.addEventListener !== 'function') {
26+
error = new InvalidArgumentError('signal must be an EventEmitter or EventTarget')
27+
} else if (method === 'CONNECT') {
28+
error = new InvalidArgumentError('invalid method')
29+
} else if (onInfo && typeof onInfo !== 'function') {
30+
error = new InvalidArgumentError('invalid onInfo callback')
31+
}
3932

40-
super('UNDICI_REQUEST')
41-
} catch (err) {
33+
if (error) {
4234
if (util.isStream(body)) {
43-
util.destroy(body.on('error', noop), err)
35+
util.destroy(body.on('error', noop), error)
4436
}
45-
throw err
37+
throw error
4638
}
4739

40+
super('UNDICI_REQUEST')
41+
4842
this.method = method
4943
this.responseHeaders = responseHeaders || null
5044
this.opaque = opaque || null

lib/api/api-stream.js

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,28 @@ class StreamHandler extends AsyncResource {
1717

1818
const { signal, method, opaque, body, onInfo, responseHeaders } = opts
1919

20-
try {
21-
if (typeof callback !== 'function') {
22-
throw new InvalidArgumentError('invalid callback')
23-
}
24-
25-
if (typeof factory !== 'function') {
26-
throw new InvalidArgumentError('invalid factory')
27-
}
28-
29-
if (signal && typeof signal.on !== 'function' && typeof signal.addEventListener !== 'function') {
30-
throw new InvalidArgumentError('signal must be an EventEmitter or EventTarget')
31-
}
32-
33-
if (method === 'CONNECT') {
34-
throw new InvalidArgumentError('invalid method')
35-
}
36-
37-
if (onInfo && typeof onInfo !== 'function') {
38-
throw new InvalidArgumentError('invalid onInfo callback')
39-
}
20+
let error = null
21+
if (typeof callback !== 'function') {
22+
error = new InvalidArgumentError('invalid callback')
23+
} else if (typeof factory !== 'function') {
24+
error = new InvalidArgumentError('invalid factory')
25+
} else if (signal && typeof signal.on !== 'function' && typeof signal.addEventListener !== 'function') {
26+
error = new InvalidArgumentError('signal must be an EventEmitter or EventTarget')
27+
} else if (method === 'CONNECT') {
28+
error = new InvalidArgumentError('invalid method')
29+
} else if (onInfo && typeof onInfo !== 'function') {
30+
error = new InvalidArgumentError('invalid onInfo callback')
31+
}
4032

41-
super('UNDICI_STREAM')
42-
} catch (err) {
33+
if (error) {
4334
if (util.isStream(body)) {
44-
util.destroy(body.on('error', noop), err)
35+
util.destroy(body.on('error', noop), error)
4536
}
46-
throw err
37+
throw error
4738
}
4839

40+
super('UNDICI_STREAM')
41+
4942
this.responseHeaders = responseHeaders || null
5043
this.opaque = opaque || null
5144
this.factory = factory

lib/api/readable.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@ class BodyReadable extends Readable {
221221
* @see https://fetch.spec.whatwg.org/#dom-body-formdata
222222
* @throws {NotSupportedError}
223223
*/
224-
async formData () {
224+
formData () {
225225
// TODO: Implement.
226-
throw new NotSupportedError()
226+
return Promise.reject(new NotSupportedError())
227227
}
228228

229229
/**

lib/core/util.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -828,9 +828,9 @@ const setupConnectTimeout = process.platform === 'win32'
828828
})
829829
}, opts.timeout)
830830
return () => {
831-
timers.clearFastTimeout(fastTimer)
832831
clearImmediate(s1)
833832
clearImmediate(s2)
833+
timers.clearFastTimeout(fastTimer)
834834
}
835835
}
836836
: (socketWeakRef, opts) => {
@@ -846,8 +846,8 @@ const setupConnectTimeout = process.platform === 'win32'
846846
})
847847
}, opts.timeout)
848848
return () => {
849-
timers.clearFastTimeout(fastTimer)
850849
clearImmediate(s1)
850+
timers.clearFastTimeout(fastTimer)
851851
}
852852
}
853853

lib/dispatcher/client-h1.js

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ function lazyllhttp () {
176176
}
177177

178178
}
179-
})
179+
}).exports
180180
}
181181

182182
let 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

Comments
 (0)