async-local-storage
Advanced tools
Comparing version 1.0.0 to 1.0.1
47
index.js
@@ -33,3 +33,3 @@ const asyncHooks = require('async_hooks'); | ||
const hooks = asyncHooks.createHook({ | ||
init: (id, type, triggerId) => { | ||
init: function init(id, type, triggerId) { | ||
if (type === 'TickObject') { | ||
@@ -56,3 +56,3 @@ return; | ||
*/ | ||
before: (id) => { | ||
before: function before(id) { | ||
currentId = id; | ||
@@ -63,3 +63,3 @@ }, | ||
*/ | ||
destroy: (id) => { | ||
destroy: function destroy(id) { | ||
if (!map.has(id)) { | ||
@@ -74,15 +74,22 @@ return; | ||
/** | ||
* Enable the async hook | ||
* Get the current id | ||
*/ | ||
exports.enable = () => { | ||
hooks.enable(); | ||
}; | ||
function getCurrentId() { | ||
return asyncHooks.currentId() || currentId; | ||
} | ||
/** | ||
* Get the current id | ||
*/ | ||
exports.currentId = getCurrentId; | ||
/** | ||
* Enable the async hook | ||
*/ | ||
exports.enable = () => hooks.enable(); | ||
/** | ||
* Disable the async hook | ||
*/ | ||
exports.disable = () => { | ||
hooks.disable(); | ||
}; | ||
exports.disable = () => hooks.disable(); | ||
@@ -100,3 +107,3 @@ /** | ||
*/ | ||
exports.set = (key, value) => { | ||
exports.set = function setValue(key, value) { | ||
/* istanbul ignore if */ | ||
@@ -106,3 +113,3 @@ if (key === 'created' || key === 'paraent') { | ||
} | ||
const id = asyncHooks.currentId() || currentId; | ||
const id = getCurrentId(); | ||
debug(`set ${key}:${value} to ${id}`); | ||
@@ -122,4 +129,4 @@ const data = map.get(id); | ||
*/ | ||
exports.get = (key) => { | ||
const data = map.get(asyncHooks.currentId() || currentId); | ||
exports.get = function getValue(key) { | ||
const data = map.get(getCurrentId()); | ||
const value = get(data, key); | ||
@@ -133,4 +140,4 @@ debug(`get ${key}:${value} from ${currentId}`); | ||
*/ | ||
exports.remove = () => { | ||
const id = asyncHooks.currentId() || currentId; | ||
exports.remove = function removeValue() { | ||
const id = getCurrentId(); | ||
if (id) { | ||
@@ -142,6 +149,8 @@ map.delete(id); | ||
/** | ||
* Get the use the of current id | ||
* Get the use the of id | ||
* @param {Number} id The tigger id, is optional, default is `als.currentId()` | ||
* @returns {Number} The use time(ns) of the current id | ||
*/ | ||
exports.use = () => { | ||
const data = map.get(asyncHooks.currentId() || currentId); | ||
exports.use = function getUse(id) { | ||
const data = map.get(id || getCurrentId()); | ||
/* istanbul ignore if */ | ||
@@ -148,0 +157,0 @@ if (!data) { |
{ | ||
"name": "async-local-storage", | ||
"description": "Get the value like thread-local storage in threaded programming", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"author": "Tree Xie <vicansocanbico@gmail.com>", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
# async-local-storage | ||
[![Build Status](https://travis-ci.org/vicanso/async-local-storage.svg?branch=master)](https://travis-ci.org/vicanso/async-local-storage) | ||
[![Coverage Status](https://img.shields.io/coveralls/vicanso/async-local-storag/master.svg?style=flat)](https://coveralls.io/r/vicanso/async-local-storage?branch=master) | ||
[![Coverage Status](https://img.shields.io/coveralls/vicanso/async-local-storage/master.svg?style=flat)](https://coveralls.io/r/vicanso/async-local-storage?branch=master) | ||
[![npm](http://img.shields.io/npm/v/async-local-storage.svg?style=flat-square)](https://www.npmjs.org/package/async-local-storage) | ||
@@ -136,4 +136,30 @@ [![Github Releases](https://img.shields.io/npm/dm/async-local-storage.svg?style=flat-square)](https://github.com/vicanso/async-local-storage) | ||
### currentId | ||
Get the current id | ||
```js | ||
const assert = require('assert'); | ||
const als = require('async-local-storage'); | ||
als.enable(); | ||
setTimeout(() => { | ||
assert(als.currentId()); | ||
}, 10); | ||
``` | ||
### use | ||
- `id` The tigger id, default is `als.currentId()` | ||
```js | ||
const assert = require('assert'); | ||
const als = require('async-local-storage'); | ||
als.enable(); | ||
setTimeout(() => { | ||
assert(als.use()); | ||
}, 10); | ||
``` | ||
## License | ||
MIT | ||
MIT |
const assert = require('assert'); | ||
const Koa = require('koa'); | ||
const Redis = require('ioredis'); | ||
const crypto = require('crypto'); | ||
const request = require('supertest'); | ||
const fs = require('fs'); | ||
const util = require('util'); | ||
const superagent = require('superagent'); | ||
const crypto = require('crypto'); | ||
const util = require('util'); | ||
const koaSession = require('koa-session'); | ||
const fs = require('fs'); | ||
const als = require('..'); | ||
const readfilePromise = util.promisify(fs.readFile); | ||
als.enable(); | ||
const als = require('..'); | ||
@@ -19,83 +15,6 @@ const randomBytes = length => crypto.randomBytes(length).toString('hex'); | ||
class SessionStore { | ||
constructor(redisClient) { | ||
this.redisClient = redisClient; | ||
} | ||
async get(key) { | ||
const data = await this.redisClient.get(key); | ||
if (!data) { | ||
return null; | ||
} | ||
return JSON.parse(data); | ||
} | ||
async set(key, json, maxAge) { | ||
await this.redisClient.psetex(key, maxAge, JSON.stringify(json)); | ||
} | ||
async destroy(key) { | ||
await this.redisClient.del(key); | ||
} | ||
} | ||
const { | ||
server, | ||
} = require('./support/server'); | ||
const app = new Koa(); | ||
const sessionMiddleware = koaSession(app, { | ||
store: new SessionStore(new Redis(6379, '127.0.0.1')), | ||
}); | ||
app.use(async (ctx, next) => { | ||
const id = ctx.get('X-Request-Id'); | ||
als.set('id', id); | ||
await next(); | ||
assert(als.use()); | ||
als.remove(); | ||
}); | ||
// fs | ||
app.use(async (ctx, next) => { | ||
assert.equal(als.get('id'), ctx.get('X-Request-Id')); | ||
if (ctx.query.fs) { | ||
await readfilePromise(__filename); | ||
} | ||
return next(); | ||
}); | ||
// delay | ||
app.use(async (ctx, next) => { | ||
assert.equal(als.get('id'), ctx.get('X-Request-Id')); | ||
if (ctx.query.delay) { | ||
await delay(100); | ||
} | ||
return next(); | ||
}); | ||
// session | ||
app.use(async (ctx, next) => { | ||
assert.equal(als.get('id'), ctx.get('X-Request-Id')); | ||
if (ctx.query.session) { | ||
return sessionMiddleware(ctx, next); | ||
} | ||
return next(); | ||
}); | ||
// http | ||
app.use(async (ctx, next) => { | ||
assert.equal(als.get('id'), ctx.get('X-Request-Id')); | ||
if (ctx.query.http) { | ||
return superagent.get('http://www.baidu.com/').then(() => { | ||
assert.equal(als.get('id'), ctx.get('X-Request-Id')); | ||
return next(); | ||
}); | ||
} | ||
return next(); | ||
}); | ||
app.use((ctx) => { | ||
if (ctx.query.session) { | ||
assert(ctx.session); | ||
} | ||
ctx.body = als.get('id'); | ||
}); | ||
const server = app.listen(); | ||
describe('async-local-storage', () => { | ||
@@ -210,2 +129,14 @@ const check = (url) => { | ||
it('get the custom id\'s use time', (done) => { | ||
let currentId = 0; | ||
delay(30).then(() => { | ||
currentId = als.currentId(); | ||
return delay(500); | ||
}).then(() => { | ||
assert(als.use(currentId)); | ||
assert(als.use()); | ||
done(); | ||
}); | ||
}); | ||
it('get the size', (done) => { | ||
@@ -212,0 +143,0 @@ setTimeout(function() { |
14809
9
384
165
3