proxy connection errors no longer crash the server, fixes #37
This commit is contained in:
11
bin/cli.js
11
bin/cli.js
@ -42,15 +42,10 @@ if (options.misc.help) {
|
|||||||
const _use = app.use
|
const _use = app.use
|
||||||
app.use = x => _use.call(app, convert(x))
|
app.use = x => _use.call(app, convert(x))
|
||||||
|
|
||||||
// app.use((ctx, next) => {
|
|
||||||
// return next()
|
|
||||||
// .catch(err => {
|
|
||||||
// console.error('FUKKK', err)
|
|
||||||
// })
|
|
||||||
// })
|
|
||||||
|
|
||||||
app.on('error', err => {
|
app.on('error', err => {
|
||||||
console.error('ERROROO', err)
|
if (options.server['log-format']) {
|
||||||
|
console.error(ansi.format(err.message, 'red'))
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const ws = localWebServer({
|
const ws = localWebServer({
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
"rewrite": [
|
"rewrite": [
|
||||||
{ "from": "/css/*", "to": "/build/styles/$1" },
|
{ "from": "/css/*", "to": "/build/styles/$1" },
|
||||||
{ "from": "/npm/*", "to": "http://registry.npmjs.org/$1" },
|
{ "from": "/npm/*", "to": "http://registry.npmjs.org/$1" },
|
||||||
|
{ "from": "/broken/*", "to": "http://localhost:9999" },
|
||||||
{ "from": "/:user/repos/:name", "to": "https://api.github.com/repos/:user/:name" }
|
{ "from": "/:user/repos/:name", "to": "https://api.github.com/repos/:user/:name" }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
"rewrite": [
|
"rewrite": [
|
||||||
{ "from": "/css/*", "to": "/build/styles/$1" },
|
{ "from": "/css/*", "to": "/build/styles/$1" },
|
||||||
{ "from": "/npm/*", "to": "http://registry.npmjs.org/$1" },
|
{ "from": "/npm/*", "to": "http://registry.npmjs.org/$1" },
|
||||||
|
{ "from": "/broken/*", "to": "http://localhost:9999" },
|
||||||
{ "from": "/:user/repos/:name", "to": "https://api.github.com/repos/:user/:name" }
|
{ "from": "/:user/repos/:name", "to": "https://api.github.com/repos/:user/:name" }
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@ -18,5 +19,6 @@
|
|||||||
<ul>
|
<ul>
|
||||||
<li><a href="/css/style.css">/css/style.css</li>
|
<li><a href="/css/style.css">/css/style.css</li>
|
||||||
<li><a href="/npm/local-web-server">/npm/local-web-server</a></li>
|
<li><a href="/npm/local-web-server">/npm/local-web-server</a></li>
|
||||||
|
<li><a href="/broken/">/broken/</a></li>
|
||||||
<li><a href="/75lb/repos/work">/75lb/repos/work</a></li>
|
<li><a href="/75lb/repos/work">/75lb/repos/work</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
@ -66,7 +66,6 @@ function localWebServer (options) {
|
|||||||
options.mocks = arrayify(options.mocks)
|
options.mocks = arrayify(options.mocks)
|
||||||
|
|
||||||
const debug = require('debug')('local-web-server')
|
const debug = require('debug')('local-web-server')
|
||||||
const Koa = require('koa')
|
|
||||||
const convert = require('koa-convert')
|
const convert = require('koa-convert')
|
||||||
const cors = require('kcors')
|
const cors = require('kcors')
|
||||||
const _ = require('koa-route')
|
const _ = require('koa-route')
|
||||||
@ -74,19 +73,13 @@ function localWebServer (options) {
|
|||||||
const bodyParser = require('koa-bodyparser')
|
const bodyParser = require('koa-bodyparser')
|
||||||
const mw = require('./middleware')
|
const mw = require('./middleware')
|
||||||
|
|
||||||
let middlewares = []
|
let middlewareStack = []
|
||||||
|
|
||||||
// const app = new Koa()
|
|
||||||
// const _use = app.use
|
|
||||||
// app.use = x => _use.call(app, convert(x))
|
|
||||||
|
|
||||||
/* CORS: allow from any origin */
|
/* CORS: allow from any origin */
|
||||||
// app.use(cors())
|
middlewareStack.push(cors())
|
||||||
middlewares.push(cors())
|
|
||||||
|
|
||||||
/* pretty print JSON */
|
/* pretty print JSON */
|
||||||
// app.use(json())
|
middlewareStack.push(json())
|
||||||
middlewares.push(json())
|
|
||||||
|
|
||||||
/* rewrite rules */
|
/* rewrite rules */
|
||||||
if (options.rewrite && options.rewrite.length) {
|
if (options.rewrite && options.rewrite.length) {
|
||||||
@ -95,53 +88,45 @@ function localWebServer (options) {
|
|||||||
/* `to` address is remote if the url specifies a host */
|
/* `to` address is remote if the url specifies a host */
|
||||||
if (url.parse(route.to).host) {
|
if (url.parse(route.to).host) {
|
||||||
debug('proxy rewrite', `${route.from} -> ${route.to}`)
|
debug('proxy rewrite', `${route.from} -> ${route.to}`)
|
||||||
// app.use(_.all(route.from, mw.proxyRequest(route)))
|
middlewareStack.push(_.all(route.from, mw.proxyRequest(route)))
|
||||||
middlewares.push(_.all(route.from, mw.proxyRequest(route)))
|
|
||||||
} else {
|
} else {
|
||||||
const rewrite = require('koa-rewrite')
|
const rewrite = require('koa-rewrite')
|
||||||
const rmw = rewrite(route.from, route.to)
|
const rmw = rewrite(route.from, route.to)
|
||||||
rmw._name = 'rewrite'
|
rmw._name = 'rewrite'
|
||||||
// app.use(rmw)
|
middlewareStack.push(rmw)
|
||||||
middlewares.push(rmw)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/* must come after rewrite. See https://github.com/nodejitsu/node-http-proxy/issues/180. */
|
/* must come after rewrite. See https://github.com/nodejitsu/node-http-proxy/issues/180. */
|
||||||
// app.use(bodyParser())
|
middlewareStack.push(bodyParser())
|
||||||
middlewares.push(bodyParser())
|
|
||||||
|
|
||||||
/* path blacklist */
|
/* path blacklist */
|
||||||
if (options.forbid.length) {
|
if (options.forbid.length) {
|
||||||
debug('forbid', options.forbid.join(', '))
|
debug('forbid', options.forbid.join(', '))
|
||||||
// app.use(mw.blacklist(options.forbid))
|
middlewareStack.push(mw.blacklist(options.forbid))
|
||||||
middlewares.push(mw.blacklist(options.forbid))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* cache */
|
/* cache */
|
||||||
if (!options['no-cache']) {
|
if (!options['no-cache']) {
|
||||||
const conditional = require('koa-conditional-get')
|
const conditional = require('koa-conditional-get')
|
||||||
const etag = require('koa-etag')
|
const etag = require('koa-etag')
|
||||||
// app.use(conditional())
|
middlewareStack.push(conditional())
|
||||||
// app.use(etag())
|
middlewareStack.push(etag())
|
||||||
middlewares.push(conditional())
|
|
||||||
middlewares.push(etag())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* mime-type overrides */
|
/* mime-type overrides */
|
||||||
if (options.mime) {
|
if (options.mime) {
|
||||||
debug('mime override', JSON.stringify(options.mime))
|
debug('mime override', JSON.stringify(options.mime))
|
||||||
// app.use(mw.mime(options.mime))
|
middlewareStack.push(mw.mime(options.mime))
|
||||||
middlewares.push(mw.mime(options.mime))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* compress response */
|
/* compress response */
|
||||||
if (options.compress) {
|
if (options.compress) {
|
||||||
const compress = require('koa-compress')
|
const compress = require('koa-compress')
|
||||||
debug('compression', 'enabled')
|
debug('compression', 'enabled')
|
||||||
// app.use(compress())
|
middlewareStack.push(compress())
|
||||||
middlewares.push(compress())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Logging */
|
/* Logging */
|
||||||
@ -151,15 +136,12 @@ function localWebServer (options) {
|
|||||||
if (!log.format) {
|
if (!log.format) {
|
||||||
const streamLogStats = require('stream-log-stats')
|
const streamLogStats = require('stream-log-stats')
|
||||||
log.options.stream = streamLogStats({ refreshRate: 500 })
|
log.options.stream = streamLogStats({ refreshRate: 500 })
|
||||||
// app.use(morgan('common', log.options))
|
middlewareStack.push(morgan('common', log.options))
|
||||||
middlewares.push(morgan('common', log.options))
|
|
||||||
} else if (log.format === 'logstalgia') {
|
} else if (log.format === 'logstalgia') {
|
||||||
morgan.token('date', logstalgiaDate)
|
morgan.token('date', logstalgiaDate)
|
||||||
// app.use(morgan('combined', log.options))
|
middlewareStack.push(morgan('combined', log.options))
|
||||||
middlewares.push(morgan('combined', log.options))
|
|
||||||
} else {
|
} else {
|
||||||
// app.use(morgan(log.format, log.options))
|
middlewareStack.push(morgan(log.format, log.options))
|
||||||
middlewares.push(morgan(log.format, log.options))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -170,15 +152,13 @@ function localWebServer (options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (mock.responses) {
|
if (mock.responses) {
|
||||||
// app.use(mw.mockResponses(mock.route, mock.responses))
|
middlewareStack.push(mw.mockResponses(mock.route, mock.responses))
|
||||||
middlewares.push(mw.mockResponses(mock.route, mock.responses))
|
|
||||||
} else if (mock.response) {
|
} else if (mock.response) {
|
||||||
mock.target = {
|
mock.target = {
|
||||||
request: mock.request,
|
request: mock.request,
|
||||||
response: mock.response
|
response: mock.response
|
||||||
}
|
}
|
||||||
// app.use(mw.mockResponses(mock.route, mock.target))
|
middlewareStack.push(mw.mockResponses(mock.route, mock.target))
|
||||||
middlewares.push(mw.mockResponses(mock.route, mock.target))
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -186,11 +166,7 @@ function localWebServer (options) {
|
|||||||
if (options.spa) {
|
if (options.spa) {
|
||||||
const historyApiFallback = require('koa-connect-history-api-fallback')
|
const historyApiFallback = require('koa-connect-history-api-fallback')
|
||||||
debug('SPA', options.spa)
|
debug('SPA', options.spa)
|
||||||
// app.use(historyApiFallback({
|
middlewareStack.push(historyApiFallback({
|
||||||
// index: options.spa,
|
|
||||||
// verbose: options.verbose
|
|
||||||
// }))
|
|
||||||
middlewares.push(historyApiFallback({
|
|
||||||
index: options.spa,
|
index: options.spa,
|
||||||
verbose: options.verbose
|
verbose: options.verbose
|
||||||
}))
|
}))
|
||||||
@ -199,20 +175,18 @@ function localWebServer (options) {
|
|||||||
/* serve static files */
|
/* serve static files */
|
||||||
if (options.static.root) {
|
if (options.static.root) {
|
||||||
const serve = require('koa-static')
|
const serve = require('koa-static')
|
||||||
// app.use(serve(options.static.root, options.static.options))
|
middlewareStack.push(serve(options.static.root, options.static.options))
|
||||||
middlewares.push(serve(options.static.root, options.static.options))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* serve directory index */
|
/* serve directory index */
|
||||||
if (options.serveIndex.path) {
|
if (options.serveIndex.path) {
|
||||||
const serveIndex = require('koa-serve-index')
|
const serveIndex = require('koa-serve-index')
|
||||||
// app.use(serveIndex(options.serveIndex.path, options.serveIndex.options))
|
middlewareStack.push(serveIndex(options.serveIndex.path, options.serveIndex.options))
|
||||||
middlewares.push(serveIndex(options.serveIndex.path, options.serveIndex.options))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const compose = require('koa-compose')
|
const compose = require('koa-compose')
|
||||||
middlewares = middlewares.map(convert)
|
middlewareStack = middlewareStack.map(convert)
|
||||||
return compose(middlewares)
|
return compose(middlewareStack)
|
||||||
}
|
}
|
||||||
|
|
||||||
function logstalgiaDate () {
|
function logstalgiaDate () {
|
||||||
|
@ -22,7 +22,6 @@ function proxyRequest (route) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
return function proxyMiddleware () {
|
return function proxyMiddleware () {
|
||||||
const next = arguments[arguments.length - 1]
|
|
||||||
const keys = []
|
const keys = []
|
||||||
route.re = pathToRegexp(route.from, keys)
|
route.re = pathToRegexp(route.from, keys)
|
||||||
route.new = this.url.replace(route.re, route.to)
|
route.new = this.url.replace(route.re, route.to)
|
||||||
@ -33,17 +32,19 @@ function proxyRequest (route) {
|
|||||||
.replace(re, arguments[index + 1] || '')
|
.replace(re, arguments[index + 1] || '')
|
||||||
})
|
})
|
||||||
|
|
||||||
this.response = false
|
|
||||||
debug('proxy request', `from: ${this.path}, to: ${url.parse(route.new).href}`)
|
debug('proxy request', `from: ${this.path}, to: ${url.parse(route.new).href}`)
|
||||||
|
|
||||||
proxy.once('error', err => {
|
return new Promise((resolve, reject) => {
|
||||||
this.throw(500, `[PROXY] ${err.message}: ${route.new}`)
|
proxy.once('error', err => {
|
||||||
|
err.message = `[PROXY] Error: ${err.message} Target: ${route.new}`
|
||||||
|
reject(err)
|
||||||
|
})
|
||||||
|
proxy.once('proxyReq', function (proxyReq) {
|
||||||
|
proxyReq.path = url.parse(route.new).path
|
||||||
|
})
|
||||||
|
proxy.once('close', resolve)
|
||||||
|
proxy.web(this.req, this.res, { target: route.new })
|
||||||
})
|
})
|
||||||
proxy.once('proxyReq', function (proxyReq) {
|
|
||||||
proxyReq.path = url.parse(route.new).path
|
|
||||||
})
|
|
||||||
|
|
||||||
proxy.web(this.req, this.res, { target: route.new })
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user