var cadence = require('cadence')
var abend = require('abend')
var coalesce = require('nascent.coalesce')
var Staccato = require('staccato')
var Interlocutor = require('interlocutor')
var protocols = { http: require('http'), https: require('https') }
var Upgrader = { Socket: require('nascent.upgrader/socket') }
var Reactor = require('reactor')
var Header = require('nascent.jacket')
var url = require('url')
var Multiplexer = require('conduit/multiplexer')
var Basin = require('conduit/basin')
var Procession = require('procession')
var delta = require('delta')
function Envoy (middleware) {
this._interlocutor = new Interlocutor(middleware)
this._header = new Header
this._reactor = new Reactor({ object: this, method: '_respond' })
}
Envoy.prototype._connect = cadence(function (async, socket) {
socket.spigot.emptyInto(new Response(this).basin)
return []
})
function Response (envoy) {
this._envoy = envoy
this.basin = new Basin(this)
}
Response.prototype._respond = cadence(function (async, cookie) {
async(function () {
delta(async()).ee(this._request).on('response')
}, function (response) {
async(function () {
this.basin.responses.enqueue({
module: 'rendezvous',
method: 'header',
body: {
cookie: cookie,
statusCode: response.statusCode,
statusMessage: response.statusMessage,
headers: response.headers
}
}, async())
}, function () {
var reader = new Staccato.Readable(response)
var loop = async(function () {
async(function () {
reader.read(async())
}, function (buffer) {
if (buffer == null) {
return [ loop.break ]
}
this.basin.responses.enqueue({
module: 'rendezvous',
method: 'chunk',
cookie: cookie,
body: buffer
}, async())
})
})()
}, function () {