2013-08-09 19:13:44 +02:00

81 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var Writable = require('stream').Writable,
common = require('../common'),
http = require('http'),
https = require('https');
module.exports = ForwardStream;
/**
* Forwards the request to the external target specified in options
*
* Examples:
*
* new ForwardStream(options)
* // => { ... }
*
* @param {Object} Options Config object passed to the proxy
* 
* @return {ForwardStream} Stream A clone of ForwardStream
*
* @api private
*/
function ForwardStream() {
var self = this;
Writable.call(this);
this.once('pipe', function() { self.onPipe() });
this.once('finish', function() { self.onFinish() });
}
require('util').inherits(ForwardStream, Writable);
/**
* Fires up the request to the external target
*
* Examples:
*
* (new ForwardStream(options)).onPipe(req)
* // => undefined
*
* @param {HttpRequest} Req Request object
*
* @api private
*/
ForwardStream.prototype.onPipe = function(request) {
this.forwardReq = (options.ssl ? https : http).request(
common.setupOutgoing(options.ssl || {}, options, request)
);
};
/**
* Closes forwarded request when `pipe` is finished
*
* Examples:
*
* (new ForwardStream(options)).onFinish()
* // => undefined
*
* @api private
*/
ForwardStream.prototype.onFinish = function() {
this.forwardReq.end();
};
/**
* Implements `stream.Writable`, writes to the forwarded request
*
* Examples:
*
* (new ForwardStream(options))._write(chunk, encoding, clb)
* // => undefined
*
* @api private
*/
ForwardStream.prototype._write = function(chunk, encoding, clb) {
this.forwardReq.write(chunk, encoding, clb);
};