Remove cookies, replace debug with debug-ms
This commit is contained in:
parent
2ef7846b5f
commit
c9459b19ba
9 changed files with 12 additions and 202 deletions
|
@ -66,29 +66,6 @@ ctx.state.user = await User.find(id);
|
||||||
|
|
||||||
Koa applications extend an internal [EventEmitter](https://nodejs.org/dist/latest-v11.x/docs/api/events.html). `ctx.app.emit` emits an event with a type, defined by the first argument. For each event you can hook up "listeners", which is a function that is called when the event is emitted. Consult the [error handling docs](https://koajs.com/#error-handling) for more information.
|
Koa applications extend an internal [EventEmitter](https://nodejs.org/dist/latest-v11.x/docs/api/events.html). `ctx.app.emit` emits an event with a type, defined by the first argument. For each event you can hook up "listeners", which is a function that is called when the event is emitted. Consult the [error handling docs](https://koajs.com/#error-handling) for more information.
|
||||||
|
|
||||||
### ctx.cookies.get(name, [options])
|
|
||||||
|
|
||||||
Get cookie `name` with `options`:
|
|
||||||
|
|
||||||
- `signed` the cookie requested should be signed
|
|
||||||
|
|
||||||
Koa uses the [cookies](https://github.com/pillarjs/cookies) module where options are simply passed.
|
|
||||||
|
|
||||||
### ctx.cookies.set(name, value, [options])
|
|
||||||
|
|
||||||
Set cookie `name` to `value` with `options`:
|
|
||||||
|
|
||||||
- `maxAge` a number representing the milliseconds from Date.now() for expiry
|
|
||||||
- `signed` sign the cookie value
|
|
||||||
- `expires` a `Date` for cookie expiration
|
|
||||||
- `path` cookie path, `'/'` by default
|
|
||||||
- `domain` cookie domain
|
|
||||||
- `secure` secure cookie
|
|
||||||
- `httpOnly` server-accessible cookie, __true__ by default
|
|
||||||
- `overwrite` a boolean indicating whether to overwrite previously set cookies of the same name (__false__ by default). If this is true, all cookies set during the same request with the same name (regardless of path or domain) are filtered out of the Set-Cookie header when setting this cookie.
|
|
||||||
|
|
||||||
Koa uses the [cookies](https://github.com/pillarjs/cookies) module where options are simply passed.
|
|
||||||
|
|
||||||
### ctx.throw([status], [msg], [properties])
|
### ctx.throw([status], [msg], [properties])
|
||||||
|
|
||||||
Helper method to throw an error with a `.status` property
|
Helper method to throw an error with a `.status` property
|
||||||
|
|
|
@ -112,7 +112,6 @@ app.listen(3000);
|
||||||
the following are supported:
|
the following are supported:
|
||||||
|
|
||||||
- `app.env` defaulting to the __NODE_ENV__ or "development"
|
- `app.env` defaulting to the __NODE_ENV__ or "development"
|
||||||
- `app.keys` array of signed cookie keys
|
|
||||||
- `app.proxy` when true proxy header fields will be trusted
|
- `app.proxy` when true proxy header fields will be trusted
|
||||||
- `app.subdomainOffset` offset of `.subdomains` to ignore [2]
|
- `app.subdomainOffset` offset of `.subdomains` to ignore [2]
|
||||||
|
|
||||||
|
@ -176,26 +175,6 @@ https.createServer(app.callback()).listen(3001);
|
||||||
Add the given middleware function to this application. See [Middleware](https://github.com/koajs/koa/wiki#middleware) for
|
Add the given middleware function to this application. See [Middleware](https://github.com/koajs/koa/wiki#middleware) for
|
||||||
more information.
|
more information.
|
||||||
|
|
||||||
## app.keys=
|
|
||||||
|
|
||||||
Set signed cookie keys.
|
|
||||||
|
|
||||||
These are passed to [KeyGrip](https://github.com/crypto-utils/keygrip),
|
|
||||||
however you may also pass your own `KeyGrip` instance. For
|
|
||||||
example the following are acceptable:
|
|
||||||
|
|
||||||
```js
|
|
||||||
app.keys = ['im a newer secret', 'i like turtle'];
|
|
||||||
app.keys = new KeyGrip(['im a newer secret', 'i like turtle'], 'sha256');
|
|
||||||
```
|
|
||||||
|
|
||||||
These keys may be rotated and are used when signing cookies
|
|
||||||
with the `{ signed: true }` option:
|
|
||||||
|
|
||||||
```js
|
|
||||||
ctx.cookies.set('name', 'tobi', { signed: true });
|
|
||||||
```
|
|
||||||
|
|
||||||
## app.context
|
## app.context
|
||||||
|
|
||||||
`app.context` is the prototype from which `ctx` is created.
|
`app.context` is the prototype from which `ctx` is created.
|
||||||
|
|
|
@ -209,7 +209,7 @@ app.use(async function (ctx, next) {
|
||||||
|
|
||||||
## Debugging Koa
|
## Debugging Koa
|
||||||
|
|
||||||
Koa along with many of the libraries it's built with support the __DEBUG__ environment variable from [debug](https://github.com/visionmedia/debug) which provides simple conditional logging.
|
Koa along with many of the libraries it's built with support the __DEBUG__ environment variable from [debug](https://github.com/nfp-projects/debug-ms) which provides simple conditional logging.
|
||||||
|
|
||||||
For example
|
For example
|
||||||
to see all Koa-specific debugging information just pass `DEBUG=koa*` and upon boot you'll see the list of middleware used, among other things.
|
to see all Koa-specific debugging information just pass `DEBUG=koa*` and upon boot you'll see the list of middleware used, among other things.
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const isGeneratorFunction = require('is-generator-function');
|
const isGeneratorFunction = require('is-generator-function');
|
||||||
const debug = require('debug')('koa:application');
|
const debug = require('debug-ms')('koa:application');
|
||||||
const onFinished = require('on-finished');
|
const onFinished = require('on-finished');
|
||||||
const response = require('./response');
|
const response = require('./response');
|
||||||
const compose = require('koa-compose');
|
const compose = require('koa-compose');
|
||||||
|
@ -38,7 +38,6 @@ module.exports = class Application extends Emitter {
|
||||||
*
|
*
|
||||||
* @param {object} [options] Application options
|
* @param {object} [options] Application options
|
||||||
* @param {string} [options.env='development'] Environment
|
* @param {string} [options.env='development'] Environment
|
||||||
* @param {string[]} [options.keys] Signed cookie keys
|
|
||||||
* @param {boolean} [options.proxy] Trust proxy headers
|
* @param {boolean} [options.proxy] Trust proxy headers
|
||||||
* @param {number} [options.subdomainOffset] Subdomain offset
|
* @param {number} [options.subdomainOffset] Subdomain offset
|
||||||
*
|
*
|
||||||
|
@ -50,7 +49,6 @@ module.exports = class Application extends Emitter {
|
||||||
this.proxy = options.proxy || false;
|
this.proxy = options.proxy || false;
|
||||||
this.subdomainOffset = options.subdomainOffset || 2;
|
this.subdomainOffset = options.subdomainOffset || 2;
|
||||||
this.env = options.env || process.env.NODE_ENV || 'development';
|
this.env = options.env || process.env.NODE_ENV || 'development';
|
||||||
if (options.keys) this.keys = options.keys;
|
|
||||||
this.middleware = [];
|
this.middleware = [];
|
||||||
this.context = Object.create(context);
|
this.context = Object.create(context);
|
||||||
this.request = Object.create(request);
|
this.request = Object.create(request);
|
||||||
|
|
|
@ -10,9 +10,6 @@ const createError = require('http-errors');
|
||||||
const httpAssert = require('http-assert');
|
const httpAssert = require('http-assert');
|
||||||
const delegate = require('delegates');
|
const delegate = require('delegates');
|
||||||
const statuses = require('statuses');
|
const statuses = require('statuses');
|
||||||
const Cookies = require('cookies');
|
|
||||||
|
|
||||||
const COOKIES = Symbol('context#cookies');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Context prototype.
|
* Context prototype.
|
||||||
|
@ -156,20 +153,6 @@ const proto = module.exports = {
|
||||||
this.length = Buffer.byteLength(msg);
|
this.length = Buffer.byteLength(msg);
|
||||||
res.end(msg);
|
res.end(msg);
|
||||||
},
|
},
|
||||||
|
|
||||||
get cookies() {
|
|
||||||
if (!this[COOKIES]) {
|
|
||||||
this[COOKIES] = new Cookies(this.req, this.res, {
|
|
||||||
keys: this.app.keys,
|
|
||||||
secure: this.request.secure
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return this[COOKIES];
|
|
||||||
},
|
|
||||||
|
|
||||||
set cookies(_cookies) {
|
|
||||||
this[COOKIES] = _cookies;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -316,21 +316,21 @@ module.exports = {
|
||||||
type += '; charset=utf-8'
|
type += '; charset=utf-8'
|
||||||
}
|
}
|
||||||
this.set('Content-Type', type);
|
this.set('Content-Type', type);
|
||||||
} else if (type.indexOf('json')) {
|
} else if (type.indexOf('json') >= 0 || type.indexOf('css.map') >= 0 || type.indexOf('js.map') >= 0) {
|
||||||
this.set('Content-Type', 'application/json; charset=utf-8');
|
this.set('Content-Type', 'application/json; charset=utf-8');
|
||||||
} else if (type.indexOf('html') => 0) {
|
} else if (type.indexOf('html') >= 0) {
|
||||||
this.set('Content-Type', 'text/html; charset=utf-8');
|
this.set('Content-Type', 'text/html; charset=utf-8');
|
||||||
} else if (type.indexOf('css') => 0) {
|
} else if (type.indexOf('css') >= 0) {
|
||||||
this.set('Content-Type', 'text/css; charset=utf-8');
|
this.set('Content-Type', 'text/css; charset=utf-8');
|
||||||
} else if (type.indexOf('js') => 0 || type.indexOf('javascript') => 0) {
|
} else if (type.indexOf('js') >= 0 || type.indexOf('javascript') >= 0) {
|
||||||
this.set('Content-Type', 'application/javascript; charset=utf-8');
|
this.set('Content-Type', 'application/javascript; charset=utf-8');
|
||||||
} else if (type.indexOf('png') => 0) {
|
} else if (type.indexOf('png') >= 0) {
|
||||||
this.set('Content-Type', 'image/png');
|
this.set('Content-Type', 'image/png');
|
||||||
} else if (type.indexOf('jpg') => 0) {
|
} else if (type.indexOf('jpg') >= 0) {
|
||||||
this.set('Content-Type', 'image/jpeg');
|
this.set('Content-Type', 'image/jpeg');
|
||||||
} else if (type.indexOf('jpeg') => 0) {
|
} else if (type.indexOf('jpeg') >= 0) {
|
||||||
this.set('Content-Type', 'image/jpeg');
|
this.set('Content-Type', 'image/jpeg');
|
||||||
} else if (type.indexOf('gif') => 0) {
|
} else if (type.indexOf('gif') >= 0) {
|
||||||
this.set('Content-Type', 'image/gif');
|
this.set('Content-Type', 'image/gif');
|
||||||
} else if (type.indexOf('text')) {
|
} else if (type.indexOf('text')) {
|
||||||
this.set('Content-Type', 'text/plain; charset=utf-8');
|
this.set('Content-Type', 'text/plain; charset=utf-8');
|
||||||
|
|
|
@ -22,10 +22,8 @@
|
||||||
],
|
],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"content-disposition": "jharrilim/content-disposition#572383f
|
"content-disposition": "jharrilim/content-disposition#572383f",
|
||||||
",
|
"debug-ms": "~4.1.2",
|
||||||
"cookies": "~0.7.1",
|
|
||||||
"debug": "~3.1.0",
|
|
||||||
"delegates": "^1.0.0",
|
"delegates": "^1.0.0",
|
||||||
"depd": "^1.1.2",
|
"depd": "^1.1.2",
|
||||||
"destroy": "^1.0.4",
|
"destroy": "^1.0.4",
|
||||||
|
|
|
@ -66,12 +66,6 @@ describe('app', () => {
|
||||||
assert.strictEqual(app.proxy, proxy);
|
assert.strictEqual(app.proxy, proxy);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should set signed cookie keys from the constructor', () => {
|
|
||||||
const keys = ['customkey'];
|
|
||||||
const app = new Koa({ keys });
|
|
||||||
assert.strictEqual(app.keys, keys);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should set subdomainOffset from the constructor', () => {
|
it('should set subdomainOffset from the constructor', () => {
|
||||||
const subdomainOffset = 3;
|
const subdomainOffset = 3;
|
||||||
const app = new Koa({ subdomainOffset });
|
const app = new Koa({ subdomainOffset });
|
||||||
|
|
|
@ -1,119 +0,0 @@
|
||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
const assert = require('assert');
|
|
||||||
const request = require('supertest');
|
|
||||||
const Koa = require('../..');
|
|
||||||
|
|
||||||
describe('ctx.cookies', () => {
|
|
||||||
describe('ctx.cookies.set()', () => {
|
|
||||||
it('should set an unsigned cookie', async() => {
|
|
||||||
const app = new Koa();
|
|
||||||
|
|
||||||
app.use((ctx, next) => {
|
|
||||||
ctx.cookies.set('name', 'jon');
|
|
||||||
ctx.status = 204;
|
|
||||||
});
|
|
||||||
|
|
||||||
const server = app.listen();
|
|
||||||
|
|
||||||
const res = await request(server)
|
|
||||||
.get('/')
|
|
||||||
.expect(204);
|
|
||||||
|
|
||||||
const cookie = res.headers['set-cookie'].some(cookie => /^name=/.test(cookie));
|
|
||||||
assert.equal(cookie, true);
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('with .signed', () => {
|
|
||||||
describe('when no .keys are set', () => {
|
|
||||||
it('should error', () => {
|
|
||||||
const app = new Koa();
|
|
||||||
|
|
||||||
app.use((ctx, next) => {
|
|
||||||
try {
|
|
||||||
ctx.cookies.set('foo', 'bar', { signed: true });
|
|
||||||
} catch (err) {
|
|
||||||
ctx.body = err.message;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return request(app.callback())
|
|
||||||
.get('/')
|
|
||||||
.expect('.keys required for signed cookies');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should send a signed cookie', async() => {
|
|
||||||
const app = new Koa();
|
|
||||||
|
|
||||||
app.keys = ['a', 'b'];
|
|
||||||
|
|
||||||
app.use((ctx, next) => {
|
|
||||||
ctx.cookies.set('name', 'jon', { signed: true });
|
|
||||||
ctx.status = 204;
|
|
||||||
});
|
|
||||||
|
|
||||||
const server = app.listen();
|
|
||||||
|
|
||||||
const res = await request(server)
|
|
||||||
.get('/')
|
|
||||||
.expect(204);
|
|
||||||
|
|
||||||
const cookies = res.headers['set-cookie'];
|
|
||||||
|
|
||||||
assert.equal(cookies.some(cookie => /^name=/.test(cookie)), true);
|
|
||||||
assert.equal(cookies.some(cookie => /(,|^)name\.sig=/.test(cookie)), true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('with secure', () => {
|
|
||||||
it('should get secure from request', async() => {
|
|
||||||
const app = new Koa();
|
|
||||||
|
|
||||||
app.proxy = true;
|
|
||||||
app.keys = ['a', 'b'];
|
|
||||||
|
|
||||||
app.use(ctx => {
|
|
||||||
ctx.cookies.set('name', 'jon', { signed: true });
|
|
||||||
ctx.status = 204;
|
|
||||||
});
|
|
||||||
|
|
||||||
const server = app.listen();
|
|
||||||
|
|
||||||
const res = await request(server)
|
|
||||||
.get('/')
|
|
||||||
.set('x-forwarded-proto', 'https') // mock secure
|
|
||||||
.expect(204);
|
|
||||||
|
|
||||||
const cookies = res.headers['set-cookie'];
|
|
||||||
assert.equal(cookies.some(cookie => /^name=/.test(cookie)), true);
|
|
||||||
assert.equal(cookies.some(cookie => /(,|^)name\.sig=/.test(cookie)), true);
|
|
||||||
assert.equal(cookies.every(cookie => /secure/.test(cookie)), true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('ctx.cookies=', () => {
|
|
||||||
it('should override cookie work', async() => {
|
|
||||||
const app = new Koa();
|
|
||||||
|
|
||||||
app.use((ctx, next) => {
|
|
||||||
ctx.cookies = {
|
|
||||||
set(key, value){
|
|
||||||
ctx.set(key, value);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
ctx.cookies.set('name', 'jon');
|
|
||||||
ctx.status = 204;
|
|
||||||
});
|
|
||||||
|
|
||||||
const server = app.listen();
|
|
||||||
|
|
||||||
await request(server)
|
|
||||||
.get('/')
|
|
||||||
.expect('name', 'jon')
|
|
||||||
.expect(204);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
Loading…
Reference in a new issue