Skip to content

Commit

Permalink
feat: onInfo & onTrailers options
Browse files Browse the repository at this point in the history
Fixes: #179
Fixes: #197
Fixes: #196
  • Loading branch information
ronag committed Aug 1, 2020
1 parent 4b25853 commit a6990ab
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
20 changes: 20 additions & 0 deletions lib/client-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,24 @@ 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
}

_onInfo (statusCode, headers) {
if (this.onInfo) {
this.onInfo(statusCode, headers)
}
}

_onHeaders (statusCode, headers, resume) {
Expand All @@ -48,6 +64,10 @@ class PipelineRequest extends Request {
const res = this.res
this.res = null
res(null, null)

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

_onError (err) {
Expand Down
20 changes: 20 additions & 0 deletions lib/client-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,26 @@ 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
}

_onInfo (statusCode, headers) {
if (this.onInfo) {
this.onInfo(statusCode, headers)
}
}

_onHeaders (statusCode, headers, resume) {
Expand Down Expand Up @@ -64,6 +80,10 @@ class RequestRequest extends Request {

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

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

_onError (err) {
Expand Down
20 changes: 20 additions & 0 deletions lib/client-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,27 @@ 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
}

_onInfo (statusCode, headers) {
if (this.onInfo) {
this.onInfo(statusCode, headers)
}
}

_onHeaders (statusCode, headers, resume) {
Expand Down Expand Up @@ -109,6 +125,10 @@ class StreamRequest extends Request {
if (this.res) {
this.res.end()
}

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

_onError (err) {
Expand Down
6 changes: 6 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ class Request extends AsyncResource {
: null
}

onInfo (statusCode, headers) {
if (this._onInfo) {
this.runInAsyncScope(this._onInfo, this, statusCode, headers)
}
}

onUpgrade (statusCode, headers, socket) {
assert(this.upgrade || this.method === 'CONNECT')

Expand Down

0 comments on commit a6990ab

Please sign in to comment.