Skip to content

Commit

Permalink
feat: make sure session id is global unique (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored and dead-horse committed Apr 29, 2019
1 parent c2b4259 commit b79134d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -3,5 +3,7 @@ language: node_js
node_js:
- '7'
- '8'
- '10'
- '12'
script: 'npm run test-travis'
after_script: 'npm install coveralls@2 && cat ./coverage/lcov.info | coveralls'
4 changes: 2 additions & 2 deletions Readme.md
Expand Up @@ -113,7 +113,7 @@ console.log('listening on port 3000');
Once you pass `options.store`, session storage is dependent on your external store -- you can't access the session if your external store is down. **Use external session stores only if necessary, avoid using session as a cache, keep the session lean, and store it in a cookie if possible!**


The way of generating external session id is controlled by the `options.genid`, which defaults to `Date.now() + '-' + uid.sync(24)`.
The way of generating external session id is controlled by the `options.genid`, which defaults to `uuid.v4()`.

If you want to add prefix for all external session id, you can use `options.prefix`, it will not work if `options.genid` present.

Expand All @@ -127,7 +127,7 @@ console.log('listening on port 3000');
- `session:invalid`: session value is invalid.
- `session:expired`: session value is expired.

### Custom External Key
### Custom External Key

External key is used the cookie by default, but you can use `options.externalKey` to customize your own external key methods. `options.externalKey` with two methods:

Expand Down
6 changes: 3 additions & 3 deletions index.js
Expand Up @@ -4,7 +4,7 @@ const debug = require('debug')('koa-session');
const ContextSession = require('./lib/context');
const util = require('./lib/util');
const assert = require('assert');
const uid = require('uid-safe');
const uuid = require('uuid/v4');
const is = require('is-type-of');

const CONTEXT_SESSION = Symbol('context#contextSession');
Expand Down Expand Up @@ -103,8 +103,8 @@ function formatOpts(opts) {
}

if (!opts.genid) {
if (opts.prefix) opts.genid = () => `${opts.prefix}${Date.now()}-${uid.sync(24)}`;
else opts.genid = () => `${Date.now()}-${uid.sync(24)}`;
if (opts.prefix) opts.genid = () => `${opts.prefix}${uuid()}`;
else opts.genid = uuid;
}

return opts;
Expand Down
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -14,6 +14,7 @@
"lib"
],
"devDependencies": {
"benchmark": "^2.1.4",
"eslint": "3",
"eslint-config-egg": "3",
"istanbul": "0",
Expand All @@ -22,6 +23,7 @@
"mocha": "^5.2.0",
"mz-modules": "^2.0.0",
"pedding": "^1.1.0",
"uid-safe": "^2.1.3",
"should": "8",
"supertest": "^3.3.0"
},
Expand All @@ -30,7 +32,7 @@
"crc": "^3.4.4",
"debug": "^3.1.0",
"is-type-of": "^1.0.0",
"uid-safe": "^2.1.3"
"uuid": "^3.3.2"
},
"engines": {
"node": ">=7.6"
Expand Down
45 changes: 45 additions & 0 deletions test/genid_bench.js
@@ -0,0 +1,45 @@
'use strict';

const Benchmark = require('benchmark');
const uuid = require('uuid');
const uid = require('uid-safe');

const suite = new Benchmark.Suite();

const genidByUid = () => `${Date.now()}-${uid.sync(24)}`;
const genidByUuidV1 = () => uuid.v1();
const genidByUuidV4 = () => uuid.v4();

console.log('genidByUid() => %s', genidByUid());
console.log('genidByUuidV1() => %s', genidByUuidV1());
console.log('genidByUuidV4() => %s', genidByUuidV4());

// add tests
suite
.add('uid()', function() {
genidByUid();
})
.add('genidByUuidV1()', function() {
genidByUuidV1();
})
.add('genidByUuidV4()', function() {
genidByUuidV4();
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({ async: true });

// genidByUid() => 1556529339180-DRnQyEqlYjGr_Zq_42fHpdFMlBfVlAoG
// genidByUuidV1() => 5af3b830-6a5f-11e9-91fb-abc918efca3d
// genidByUuidV4() => 27088fa8-8436-4c8b-aae7-76ba316db9e3
//
// uid() x 260,850 ops/sec ±1.50% (84 runs sampled)
// genidByUuidV1() x 1,181,483 ops/sec ±0.93% (86 runs sampled)
// genidByUuidV4() x 301,840 ops/sec ±1.40% (83 runs sampled)
// Fastest is genidByUuidV1()
2 changes: 1 addition & 1 deletion test/store.test.js
Expand Up @@ -448,7 +448,7 @@ describe('Koa Session External Store', () => {
done = pedding(done, 2);
const app = App({ maxAge: 100 });
app.on('session:expired', args => {
assert(args.key.match(/^\d+-/));
assert(args.key.match(/^\w+-/));
assert(args.value);
assert(args.ctx);
done();
Expand Down

0 comments on commit b79134d

Please sign in to comment.