Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Sep 22, 2024
1 parent 9392d57 commit 387b7b9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 59 deletions.
28 changes: 15 additions & 13 deletions lib/api/api-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ class RequestHandler extends AsyncResource {
this.callback = callback
this.res = null
this.abort = null
this.resume = null
this.body = body
this.trailers = {}
this.context = null
this.trailers = {}
this.onInfo = onInfo || null
this.highWaterMark = highWaterMark
this.reason = null
Expand Down Expand Up @@ -85,10 +86,12 @@ class RequestHandler extends AsyncResource {
this.context = context
}

onHeaders (statusCode, rawHeaders, resume, statusMessage) {
const { callback, opaque, abort, context, responseHeaders, highWaterMark } = this
onResponseStart (resume) {
this.resume = resume
}

const headers = responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
onResponseHeaders (headers, statusCode) {
const { callback, opaque, abort, highWaterMark } = this

if (statusCode < 200) {
if (this.onInfo) {
Expand All @@ -97,11 +100,10 @@ class RequestHandler extends AsyncResource {
return
}

const parsedHeaders = responseHeaders === 'raw' ? util.parseHeaders(rawHeaders) : headers
const contentType = parsedHeaders['content-type']
const contentLength = parsedHeaders['content-length']
const contentType = headers['content-type']
const contentLength = headers['content-length']
const res = new Readable({
resume,
resume: this.resume,
abort,
contentType,
contentLength: this.method !== 'HEAD' && contentLength
Expand All @@ -124,21 +126,21 @@ class RequestHandler extends AsyncResource {
trailers: this.trailers,
opaque,
body: res,
context
context: this.context
})
}
}

onData (chunk) {
onResponseData (chunk) {
return this.res.push(chunk)
}

onComplete (trailers) {
util.parseHeaders(trailers, this.trailers)
onResponseEnd (trailers) {
Object.assign(this.trailers, trailers)
this.res.push(null)
}

onError (err) {
onResponseError (err) {
const { res, callback, body, opaque } = this

if (callback) {
Expand Down
10 changes: 5 additions & 5 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,11 @@ function assertRequestHandler (handler, method, upgrade) {
throw new InvalidArgumentError('handler must be an object')
}

if (typeof handler.onConnect !== 'function') {
if (typeof handler.onConnect !== 'function' && typeof handler.onRequestStart !== 'function') {
throw new InvalidArgumentError('invalid onConnect method')
}

if (typeof handler.onError !== 'function') {
if (typeof handler.onError !== 'function' && typeof handler.onResponseError !== 'function') {
throw new InvalidArgumentError('invalid onError method')
}

Expand All @@ -517,15 +517,15 @@ function assertRequestHandler (handler, method, upgrade) {
throw new InvalidArgumentError('invalid onUpgrade method')
}
} else {
if (typeof handler.onHeaders !== 'function') {
if (typeof handler.onHeaders !== 'function' && typeof handler.onResponseHeaders !== 'function') {
throw new InvalidArgumentError('invalid onHeaders method')
}

if (typeof handler.onData !== 'function') {
if (typeof handler.onData !== 'function' && typeof handler.onResponseData !== 'function') {
throw new InvalidArgumentError('invalid onData method')
}

if (typeof handler.onComplete !== 'function') {
if (typeof handler.onComplete !== 'function' && typeof handler.onResponseEnd !== 'function') {
throw new InvalidArgumentError('invalid onComplete method')
}
}
Expand Down
7 changes: 4 additions & 3 deletions lib/dispatcher/dispatcher-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,12 @@ class DispatcherBase extends Dispatcher {

return this[kDispatch](opts, handler)
} catch (err) {
if (typeof handler.onError !== 'function') {
throw new InvalidArgumentError('invalid onError method')
if (typeof handler.onError !== 'function' && typeof handler.onResponseError !== 'function') {
throw new InvalidArgumentError('invalid onResponseError method')
}

handler.onError(err)
handler.onError?.(err)
handler.onResponseError?.(err)

return false
}
Expand Down
5 changes: 4 additions & 1 deletion lib/handler/redirect-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { kBodyUsed } = require('../core/symbols')
const assert = require('node:assert')
const { InvalidArgumentError } = require('../core/errors')
const EE = require('node:events')
const DecoratorHandler = require('./decorator-handler')

const redirectableStatusCodes = [300, 301, 302, 303, 307, 308]

Expand Down Expand Up @@ -45,7 +46,9 @@ class RedirectHandler {
this.abort = null
this.opts = { ...opts, maxRedirections: 0 } // opts must be a copy
this.maxRedirections = maxRedirections
this.handler = handler
this.handler = handler instanceof DecoratorHandler
? handler
: new DecoratorHandler(handler)
this.history = []
this.redirectionLimitReached = false

Expand Down
37 changes: 0 additions & 37 deletions test/client-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -881,43 +881,6 @@ test('request onInfo callback headers parsing', async (t) => {
t.ok(true, 'pass')
})

test('request raw responseHeaders', async (t) => {
t = tspl(t, { plan: 4 })
const infos = []

const server = net.createServer((socket) => {
const lines = [
'HTTP/1.1 103 Early Hints',
'Link: </style.css>; rel=preload; as=style',
'',
'HTTP/1.1 200 OK',
'Date: Sat, 09 Oct 2010 14:28:02 GMT',
'Connection: close',
'',
'the body'
]
socket.end(lines.join('\r\n'))
})
after(() => server.close())

await promisify(server.listen.bind(server))(0)

const client = new Client(`http://localhost:${server.address().port}`)
after(() => client.close())

const { body, headers } = await client.request({
path: '/',
method: 'GET',
responseHeaders: 'raw',
onInfo: (x) => { infos.push(x) }
})
await body.dump()
t.strictEqual(infos.length, 1)
t.deepStrictEqual(infos[0].headers, ['Link', '</style.css>; rel=preload; as=style'])
t.deepStrictEqual(headers, ['Date', 'Sat, 09 Oct 2010 14:28:02 GMT', 'Connection', 'close'])
t.ok(true, 'pass')
})

test('request formData', async (t) => {
t = tspl(t, { plan: 1 })

Expand Down

0 comments on commit 387b7b9

Please sign in to comment.