Skip to content

Commit

Permalink
Merge pull request #18 from postmanlabs/feature/bindOn
Browse files Browse the repository at this point in the history
Added feature to bind on stream emits via options 🎉
  • Loading branch information
kunagpal committed Jul 9, 2018
2 parents ace4f31 + e9797e7 commit 156ae1b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -7,7 +7,7 @@
"util",
"utility"
],
"version": "2.86.1-postman.1",
"version": "2.86.1-postman.2-beta.1",
"repository": {
"type": "git",
"url": "https://github.com/postmanlabs/postman-request.git"
Expand Down
38 changes: 32 additions & 6 deletions request.js
Expand Up @@ -133,11 +133,13 @@ util.inherits(Request, stream.Stream)

// Debugging
Request.debug = process.env.NODE_DEBUG && /\brequest\b/.test(process.env.NODE_DEBUG)

function debug () {
if (Request.debug) {
console.error('REQUEST %s', util.format.apply(util, arguments))
}
}

Request.prototype.debug = debug

Request.prototype.init = function (options) {
Expand All @@ -150,6 +152,26 @@ Request.prototype.init = function (options) {
}
self.headers = self.headers ? copy(self.headers) : {}

// additional postman feature starts
// bind default events sent via options
if (options.bindOn) {
Object.keys(options.bindOn).forEach(function (eventName) {
!Array.isArray(options.bindOn[eventName]) && (options.bindOn[eventName] = [options.bindOn[eventName]])
options.bindOn[eventName].forEach(function (listener) {
self.on(eventName, listener)
})
})
}
if (options.once) {
Object.keys(options.once).forEach(function (eventName) {
!Array.isArray(options.bindOnce[eventName]) && (options.bindOnce[eventName] = [options.bindOnce[eventName]])
options.bindOnce[eventName].forEach(function (listener) {
self.once(eventName, listener)
})
})
}
// additional postman feature ends

// Delete headers with value undefined since they break
// ClientRequest.OutgoingMessage.setHeader in node 0.12
for (var headerName in self.headers) {
Expand Down Expand Up @@ -436,6 +458,7 @@ Request.prototype.init = function (options) {
}
}
}

if (self.body && !isstream(self.body)) {
setContentLength()
}
Expand Down Expand Up @@ -513,9 +536,9 @@ Request.prototype.init = function (options) {
}
}

// self.on('pipe', function () {
// console.error('You have already piped to this stream. Pipeing twice is likely to break the request.')
// })
// self.on('pipe', function () {
// console.error('You have already piped to this stream. Pipeing twice is likely to break the request.')
// })
})

defer(function () {
Expand Down Expand Up @@ -735,6 +758,9 @@ Request.prototype.start = function () {
return
}

// postman: emit start event
self.emit('start')

self._started = true
self.method = self.method || 'GET'
self.href = self.uri.href
Expand Down Expand Up @@ -887,7 +913,7 @@ Request.prototype.onRequestError = function (error) {
}
if (self.req && self.req._reusedSocket && error.code === 'ECONNRESET' &&
self.agent.addRequestNoreuse) {
self.agent = { addRequest: self.agent.addRequestNoreuse.bind(self.agent) }
self.agent = {addRequest: self.agent.addRequestNoreuse.bind(self.agent)}
self.start()
self.req.end()
return
Expand Down Expand Up @@ -964,7 +990,7 @@ Request.prototype.onRequestResponse = function (response) {
// XXX This is different on 0.10, because SSL is strict by default
if (self.httpModule === https &&
self.strictSSL && (!response.hasOwnProperty('socket') ||
!response.socket.authorized)) {
!response.socket.authorized)) {
debug('strict ssl error', self.uri.href)
var sslErr = response.hasOwnProperty('socket') ? response.socket.authorizationError : self.uri.href + ' does not support SSL'
self.emit('error', new Error('SSL Error: ' + sslErr))
Expand Down Expand Up @@ -1122,7 +1148,7 @@ Request.prototype.onRequestResponse = function (response) {

Request.prototype.readResponseBody = function (response) {
var self = this
debug("reading response's body")
debug('reading response\'s body')
var buffers = []
var bufferLength = 0
var strings = []
Expand Down

0 comments on commit 156ae1b

Please sign in to comment.