Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: onInfo & onTrailers options
Browse files Browse the repository at this point in the history
Fixes: #179
Fixes: #197
Fixes: #196
ronag committed Aug 1, 2020

Verified

This commit was signed with the committer’s verified signature.
not-an-aardvark Teddy Katz
1 parent 4b25853 commit 62e056c
Showing 5 changed files with 67 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -102,6 +102,9 @@ Options:
If `false` the request won't be sent until all preceeding
requests in the pipeline has completed.
Default: `true` if `method` is `HEAD` or `GET`.
* `onInfo(statusCode, headers)`, function invoked for informational
1xx responses.
* `onTrailers(trailers)`, function invoked for trailers.

Headers are represented by an object like this:

21 changes: 21 additions & 0 deletions lib/client-pipeline.js
Original file line number Diff line number Diff line change
@@ -21,11 +21,28 @@ class PipelineRequest extends Request {
constructor (client, opts, callback) {
super(opts, client)

if (opts.onInfo && typeof opts.onInfo !== 'function') {
throw new InvalidArgumentError('invalid opts.onInfo')
}

if (opts.onTrailers && typeof opts.onTrailers !== 'function') {
throw new InvalidArgumentError('invalid opts.onTrailers')
}

this.callback = callback
this.aborted = false
this.onInfo = opts.onInfo
this.onTrailers = opts.onTrailers
}

_onHeaders (statusCode, headers, resume) {
if (statusCode < 200) {
if (this.onInfo) {
this.onInfo(statusCode, headers)
}
return
}

const { callback } = this

assert(callback)
@@ -48,6 +65,10 @@ class PipelineRequest extends Request {
const res = this.res
this.res = null
res(null, null)

if (this.onTrailers) {
this.onTrailers(trailers)
}
}

_onError (err) {
21 changes: 21 additions & 0 deletions lib/client-request.js
Original file line number Diff line number Diff line change
@@ -20,13 +20,30 @@ class RequestRequest extends Request {
throw new InvalidArgumentError('invalid method')
}

if (opts.onInfo && typeof opts.onInfo !== 'function') {
throw new InvalidArgumentError('invalid opts.onInfo')
}

if (opts.onTrailers && typeof opts.onTrailers !== 'function') {
throw new InvalidArgumentError('invalid opts.onTrailers')
}

super(opts, client)

this.callback = callback
this.res = null
this.onInfo = opts.onInfo
this.onTrailers = opts.onTrailers
}

_onHeaders (statusCode, headers, resume) {
if (statusCode < 200) {
if (this.onInfo) {
this.onInfo(statusCode, headers)
}
return
}

const { callback, opaque } = this

assert(callback)
@@ -64,6 +81,10 @@ class RequestRequest extends Request {

_onComplete (trailers) {
this.res.push(null)

if (this.onTrailers) {
this.onTrailers(trailers)
}
}

_onError (err) {
21 changes: 21 additions & 0 deletions lib/client-stream.js
Original file line number Diff line number Diff line change
@@ -24,14 +24,31 @@ class StreamRequest extends Request {
throw new InvalidArgumentError('invalid method')
}

if (opts.onInfo && typeof opts.onInfo !== 'function') {
throw new InvalidArgumentError('invalid opts.onInfo')
}

if (opts.onTrailers && typeof opts.onTrailers !== 'function') {
throw new InvalidArgumentError('invalid opts.onTrailers')
}

super(opts, client)

this.factory = factory
this.callback = callback
this.res = null
this.onInfo = opts.onInfo
this.onTrailers = opts.onTrailers
}

_onHeaders (statusCode, headers, resume) {
if (statusCode < 200) {
if (this.onInfo) {
this.onInfo(statusCode, headers)
}
return
}

const { factory, opaque } = this

assert(factory)
@@ -109,6 +126,10 @@ class StreamRequest extends Request {
if (this.res) {
this.res.end()
}

if (this.onTrailers) {
this.onTrailers(trailers)
}
}

_onError (err) {
6 changes: 1 addition & 5 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -508,11 +508,7 @@ class Parser extends HTTPParser {

// TODO: More statusCode validation?

if (statusCode >= 200) {
request.onHeaders(statusCode, headers, resumeSocket)
} else {
// TODO: Info
}
request.onHeaders(statusCode, headers, resumeSocket)

return request.method === 'HEAD' || statusCode < 200 ? 1 : 0
}

0 comments on commit 62e056c

Please sign in to comment.