Skip to content

Commit

Permalink
Merge branch 'use-koa-2-tmp' into use-koa-2
Browse files Browse the repository at this point in the history
  • Loading branch information
nickmccurdy committed Sep 3, 2017
2 parents d6cb062 + 81078d0 commit e12524f
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 72 deletions.
72 changes: 37 additions & 35 deletions lib/index.js
@@ -1,4 +1,6 @@

'use strict';

var Koa = require('koa');
var merge = require('merge-descriptors');

Expand All @@ -8,42 +10,42 @@ var req = require('./request');
var res = require('./response');
var App = require('./app');

module.exports = function koala(options) {
options = options || {};

var app = new Koa();

middleware(app, options);
merge(app.context, ctx);
merge(app.request, req);
merge(app.response, res);
App(app);

// handlers
app.errorHandler = require('koa-error')(options.error);
app.pageNotFoundHandler = pageNotFoundHandler;

// properties
app.jsonStrict = options.jsonStrict !== false;

// proto stuff
require('koa-csrf')(app);
require('koa-trace')(app);
require('koa-body-parsers')(app);
// nested query string support
if (options.qs) {
require('koa-qs')(app);
app.querystring = require('qs');
}

// jsonp support
if (options.jsonp) {
require('koa-safe-jsonp')(app, options.jsonp);
module.exports = class Koala extends Koa {
constructor(options) {
super();

options = options || {};

middleware(this, options);
merge(this.context, ctx);
merge(this.request, req);
merge(this.response, res);
App(this);

// handlers
this.errorHandler = require('koa-error')(options.error);
this.pageNotFoundHandler = pageNotFoundHandler;

// properties
this.jsonStrict = options.jsonStrict !== false;

// proto stuff
require('koa-csrf')(this);
require('koa-trace')(this);
require('koa-body-parsers')(this);
// nested query string support
if (options.qs) {
require('koa-qs')(this);
this.querystring = require('qs');
}

// jsonp support
if (options.jsonp) {
require('koa-safe-jsonp')(this, options.jsonp);
}

if (process.env.NODE_ENV !== 'production') this.debug();
}

if (process.env.NODE_ENV !== 'production') app.debug();

return app;
}

function* pageNotFoundHandler(next) {
Expand Down
2 changes: 1 addition & 1 deletion test/app/basic-auth.js
@@ -1,7 +1,7 @@

describe('Basic Auth', function () {
it('should return the value', function (done) {
var app = koala()
var app = new Koala()
app.use(function* (next) {
this.body = this.request.basicAuth
})
Expand Down
22 changes: 11 additions & 11 deletions test/app/body-parsing.js
Expand Up @@ -2,7 +2,7 @@
describe('Body Parsing', function () {
describe('.request.json()', function () {
it('should parse a json body', function (done) {
var app = koala()
var app = new Koala()
app.use(function* () {
this.body = yield* this.request.json()
})
Expand All @@ -17,7 +17,7 @@ describe('Body Parsing', function () {
})

it('should throw on non-objects in strict mode', function (done) {
var app = koala()
var app = new Koala()
app.use(function* () {
this.body = yield* this.request.json()
})
Expand All @@ -29,7 +29,7 @@ describe('Body Parsing', function () {
})

it('should not throw on non-objects in non-strict mode', function (done) {
var app = koala()
var app = new Koala()
app.jsonStrict = false
app.use(function* () {
this.body = yield* this.request.json()
Expand All @@ -45,7 +45,7 @@ describe('Body Parsing', function () {

describe('.request.urlencoded()', function () {
it('should parse a urlencoded body', function (done) {
var app = koala()
var app = new Koala()
app.use(function* () {
this.body = yield* this.request.urlencoded()
})
Expand All @@ -58,7 +58,7 @@ describe('Body Parsing', function () {
})

it('should not support nested query strings by default', function (done) {
var app = koala()
var app = new Koala()
app.use(function* () {
this.body = yield* this.request.urlencoded()
})
Expand All @@ -75,7 +75,7 @@ describe('Body Parsing', function () {
})

it('should support nested query strings with options.qs=true', function (done) {
var app = koala({
var app = new Koala({
qs: true
})
app.use(function* () {
Expand All @@ -100,7 +100,7 @@ describe('Body Parsing', function () {

describe('.request.text()', function () {
it('should get the raw text body', function (done) {
var app = koala()
var app = new Koala()
app.use(function* () {
this.body = yield* this.request.text()
assert.equal('string', typeof this.body)
Expand All @@ -113,7 +113,7 @@ describe('Body Parsing', function () {
})

it('should throw if the body is too large', function (done) {
var app = koala();
var app = new Koala();
app.use(function* () {
yield* this.request.text('1kb')
this.body = 204
Expand All @@ -127,7 +127,7 @@ describe('Body Parsing', function () {

describe('.request.buffer()', function () {
it('should get the raw buffer body', function (done) {
var app = koala()
var app = new Koala()
app.use(function* () {
this.body = yield* this.request.buffer()
assert(Buffer.isBuffer(this.body))
Expand All @@ -140,7 +140,7 @@ describe('Body Parsing', function () {
})

it('should throw if the body is too large', function (done) {
var app = koala();
var app = new Koala();
app.use(function* () {
yield* this.request.buffer('1kb')
this.body = 204
Expand All @@ -158,7 +158,7 @@ describe('Body Parsing', function () {

describe('Expect: 100-continue', function () {
it('should send 100-continue', function (done) {
var app = koala();
var app = new Koala();
app.use(function* () {
this.body = yield* this.request.json()
})
Expand Down
18 changes: 9 additions & 9 deletions test/app/cache-control.js
Expand Up @@ -2,7 +2,7 @@
describe('Cache-Control', function () {
describe('should be available as', function () {
it('this.cc()', function (done) {
var app = koala()
var app = new Koala()
app.use(function* (next) {
this.cc(1000)
this.status = 204
Expand All @@ -12,7 +12,7 @@ describe('Cache-Control', function () {
})

it('this.cacheControl()', function (done) {
var app = koala()
var app = new Koala()
app.use(function* (next) {
this.cacheControl(1000)
this.status = 204
Expand All @@ -22,7 +22,7 @@ describe('Cache-Control', function () {
})

it('this.response.cc()', function (done) {
var app = koala()
var app = new Koala()
app.use(function* (next) {
this.response.cc(1000)
this.status = 204
Expand All @@ -32,7 +32,7 @@ describe('Cache-Control', function () {
})

it('this.cacheControl()', function (done) {
var app = koala()
var app = new Koala()
app.use(function* (next) {
this.response.cacheControl(1000)
this.status = 204
Expand All @@ -44,7 +44,7 @@ describe('Cache-Control', function () {

describe('when the value is a number', function () {
it('should set "public, max-age="', function (done) {
var app = koala()
var app = new Koala()
app.use(function* (next) {
this.response.cacheControl(1000000)
this.status = 204
Expand All @@ -56,7 +56,7 @@ describe('Cache-Control', function () {

describe('when the value is a time string', function () {
it('should set "public, max-age="', function (done) {
var app = koala()
var app = new Koala()
app.use(function* (next) {
this.response.cacheControl('1 hour')
this.status = 204
Expand All @@ -68,7 +68,7 @@ describe('Cache-Control', function () {

describe('when the value is "false"', function () {
it('should set "private, no-cache"', function (done) {
var app = koala()
var app = new Koala()
app.use(function* (next) {
this.response.cacheControl(false)
this.status = 204
Expand All @@ -80,7 +80,7 @@ describe('Cache-Control', function () {

describe('when the value is a string', function () {
it('should juset set it', function (done) {
var app = koala()
var app = new Koala()
app.use(function* (next) {
this.response.cacheControl('lol')
this.status = 204
Expand All @@ -92,7 +92,7 @@ describe('Cache-Control', function () {

describe('when the value is anything else', function () {
it('should throw', function (done) {
var app = koala()
var app = new Koala()
app.use(function* (next) {
this.response.cacheControl(true)
this.status = 204
Expand Down
2 changes: 1 addition & 1 deletion test/app/conditional-get.js
Expand Up @@ -3,7 +3,7 @@ describe('Conditional-Get', function () {
describe('when a body is set', function () {
var etag

var app = koala()
var app = new Koala()
app.use(function* (next) {
this.body = 'hello'
})
Expand Down
12 changes: 6 additions & 6 deletions test/app/headers.js
Expand Up @@ -2,7 +2,7 @@
describe('Set headers', function () {
describe('X-Response-Time', function() {
it('should get X-Response-Time correctly by default', function(done) {
var app = koala()
var app = new Koala()

request(app.listen())
.get('/')
Expand All @@ -11,7 +11,7 @@ describe('Set headers', function () {
.end(done)
})
it('should not get X-Response-Time by options.responseTime = false', function(done) {
var app = koala({
var app = new Koala({
responseTime: false
})

Expand All @@ -27,7 +27,7 @@ describe('Set headers', function () {

describe('X-Frame-Options', function() {
it('should get X-Frame-Options DENY by default', function(done) {
var app = koala()
var app = new Koala()

request(app.listen())
.get('/')
Expand All @@ -36,7 +36,7 @@ describe('Set headers', function () {
.end(done)
})
it('should not get X-Frame-Options by xframe = false', function(done) {
var app = koala({
var app = new Koala({
security: {
xframe: false
}
Expand All @@ -51,7 +51,7 @@ describe('Set headers', function () {
})
})
it('should get X-Frame-Options DENY by xframe = true', function(done) {
var app = koala({
var app = new Koala({
security: {
xframe: true
}
Expand All @@ -64,7 +64,7 @@ describe('Set headers', function () {
.end(done)
})
it('should get X-Frame-Options SAMEORIGIN by xframe = same', function(done) {
var app = koala({
var app = new Koala({
security: {
xframe: 'same'
}
Expand Down
2 changes: 1 addition & 1 deletion test/app/index.js
Expand Up @@ -3,7 +3,7 @@ http = require('http')
assert = require('assert')
request = require('supertest')

koala = require('../..')
Koala = require('../..')

require('fs').readdirSync(__dirname).forEach(function (name) {
if (name[0] === '.') return
Expand Down
4 changes: 2 additions & 2 deletions test/app/jsonp.js
@@ -1,7 +1,7 @@

describe('jsonp', function () {
it('should return jsonp response', function (done) {
var app = koala({
var app = new Koala({
jsonp: {
callback: '_callback'
}
Expand All @@ -17,7 +17,7 @@ describe('jsonp', function () {
})

it('should return json response', function (done) {
var app = koala({
var app = new Koala({
jsonp: {
callback: '_callback'
}
Expand Down
4 changes: 2 additions & 2 deletions test/app/middleware.js
Expand Up @@ -2,7 +2,7 @@
describe('Middlewares', function () {
describe('Session', function() {
it('should has this.session by default', function(done) {
var app = koala()
var app = new Koala()

app.use(function *() {
this.body = this.session;
Expand All @@ -14,7 +14,7 @@ describe('Middlewares', function () {
.end(done)
})
it('should has no this.session by options.session = false', function(done) {
var app = koala({
var app = new Koala({
session: false
})

Expand Down
2 changes: 1 addition & 1 deletion test/app/object-streams.js
Expand Up @@ -3,7 +3,7 @@ var PassThrough = require('stream').PassThrough

describe('Object Streams', function () {
it('should be supported', function (done) {
var app = koala()
var app = new Koala()
app.use(function* (next) {
var body = this.body = new PassThrough({
objectMode: true
Expand Down
2 changes: 1 addition & 1 deletion test/app/polyfill.js
Expand Up @@ -2,7 +2,7 @@
describe.skip('Polyfills', function () {
describe('GET /polyfill.js', function () {
it('should return the polyfill', function (done) {
var app = koala()
var app = new Koala()
request(app.listen())
.get('/polyfill.js')
.expect(200)
Expand Down

0 comments on commit e12524f

Please sign in to comment.