-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
haoxin
committed
May 16, 2017
1 parent
fb947af
commit 28c4a0b
Showing
6 changed files
with
235 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
extends: standard | ||
env: | ||
mocha: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,74 @@ | ||
|
||
const logger = require('./'); | ||
const Koa = require('koa'); | ||
const compress = require('koa-compress')(); | ||
const app = new Koa(); | ||
const logger = require('./') | ||
const Koa = require('koa') | ||
const compress = require('koa-compress')() | ||
const app = new Koa() | ||
|
||
// wrap subsequent middleware in a logger | ||
|
||
app.use(logger()); | ||
app.use(logger()) | ||
|
||
// 204 | ||
|
||
app.use(function(ctx, next){ | ||
if ('/204' == ctx.path) ctx.status = 204; | ||
else return next(); | ||
app.use(function (ctx, next) { | ||
if (ctx.path === '/204') ctx.status = 204 | ||
else return next() | ||
}) | ||
|
||
// 404 | ||
|
||
app.use(function(ctx, next){ | ||
if ('/404' == ctx.path) return; | ||
return next(); | ||
app.use(function (ctx, next) { | ||
if (ctx.path === '/404') return | ||
return next() | ||
}) | ||
|
||
// destroy | ||
|
||
app.use(function(ctx, next){ | ||
if ('/close' == ctx.path) return ctx.req.destroy(); | ||
return next(); | ||
app.use(function (ctx, next) { | ||
if (ctx.path === '/close') return ctx.req.destroy() | ||
return next() | ||
}) | ||
|
||
// compress the response 1/2 the time to calculate the stream length | ||
|
||
app.use(function(ctx, next){ | ||
app.use(function (ctx, next) { | ||
if (Math.random() > 0.5) { | ||
return next(); | ||
return next() | ||
} else { | ||
return compress(ctx, next); | ||
return compress(ctx, next) | ||
} | ||
}) | ||
|
||
// response middleware | ||
|
||
app.use(function(ctx, next){ | ||
app.use(function (ctx, next) { | ||
// yield control downstream | ||
next().then(function() { | ||
next().then(function () { | ||
// sleep for 0-2s | ||
return sleep(Math.random() * 2000 | 0); | ||
}).then(function() { | ||
|
||
return sleep(Math.random() * 2000 | 0) | ||
}).then(function () { | ||
// error | ||
if (Math.random() > .75) { | ||
const err = new Error('boom'); | ||
err.status = 500; | ||
throw err; | ||
if (Math.random() > 0.75) { | ||
const err = new Error('boom') | ||
err.status = 500 | ||
throw err | ||
} | ||
|
||
// random body | ||
const body = Array(Math.random() * 5 * 1024 | 9).join('a'); | ||
ctx.status = 200; | ||
ctx.body = body; | ||
|
||
}); | ||
}); | ||
const body = Array(Math.random() * 5 * 1024 | 9).join('a') | ||
ctx.status = 200 | ||
ctx.body = body | ||
}) | ||
}) | ||
|
||
const port = process.env.PORT || 3000; | ||
app.listen(port); | ||
console.log('listening on port ' + port); | ||
const port = process.env.PORT || 3000 | ||
app.listen(port) | ||
console.log('listening on port ' + port) | ||
|
||
// sleep helper | ||
|
||
function sleep(ms) { | ||
return new Promise(function(resolve){ | ||
setTimeout(resolve, ms); | ||
}); | ||
function sleep (ms) { | ||
return new Promise(function (resolve) { | ||
setTimeout(resolve, ms) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
What is a purpose of removing listeners? After the response has been sent, Context object will be destructed!