Socket
Socket
Sign inDemoInstall

async-local-storage

Package Overview
Dependencies
3
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.1.1 to 1.2.0

2

examples/express.js

@@ -34,2 +34,3 @@ const express = require('express');

setImmediate(() => {
als.set('immediate', true, true);
console.info(`set immediate function: ${als.get('name')}`);

@@ -74,2 +75,3 @@ });

return request.get('https://www.baidu.com/').then(() => {
console.info(`get immediate from top:${als.get('immediate')}`);
console.info(`promise function: ${als.get('name')}`);

@@ -76,0 +78,0 @@ next();

@@ -30,2 +30,3 @@ const Koa = require('koa');

setImmediate(() => {
als.set('immediate', true, true);
console.info(`set immediate function: ${als.get('name')}`);

@@ -67,2 +68,3 @@ });

return request.get('https://www.baidu.com/').then(() => {
console.info(`get immediate from top:${als.get('immediate')}`);
console.info(`promise function: ${als.get('name')}`);

@@ -69,0 +71,0 @@ return next();

@@ -0,3 +1,7 @@

# 1.2.0
* Support to setting all the data in top parent #6
# 1.1.1
* Fix the context is lost in process next tick

@@ -9,2 +9,4 @@ const asyncHooks = require('async_hooks');

let defaultLinkedTop = false;
function isUndefined(value) {

@@ -32,2 +34,13 @@ return value === undefined;

/**
* Get the top data
*/
function getTop(data) {
if (!data.parent) {
return data;
}
return getTop(data.parent);
}
let currentId = 0;

@@ -100,8 +113,24 @@ const hooks = asyncHooks.createHook({

/**
* Enable linked top
*/
exports.enableLinkedTop = () => {
defaultLinkedTop = true;
};
/**
* Disable linked top
*/
exports.disableLinkedTop = () => {
defaultLinkedTop = false;
};
/**
* Set the key/value for this score
* @param {String} key The key of value
* @param {String} value The value
* @param {Boolean} linkedTop The value linked to top
* @returns {Boolean} if success, will return true, otherwise false
*/
exports.set = function setValue(key, value) {
exports.set = function setValue(key, value, linkedTop) {
/* istanbul ignore if */

@@ -113,3 +142,3 @@ if (key === 'created' || key === 'parent') {

debug(`set ${key}:${value} to ${id}`);
const data = map.get(id);
let data = map.get(id);
/* istanbul ignore if */

@@ -119,2 +148,9 @@ if (!data) {

}
let setToLinkedTop = linkedTop;
if (isUndefined(linkedTop)) {
setToLinkedTop = defaultLinkedTop;
}
if (setToLinkedTop) {
data = getTop(data);
}
data[key] = value;

@@ -121,0 +157,0 @@ return true;

2

package.json
{
"name": "async-local-storage",
"description": "Get the value like thread-local storage in threaded programming",
"version": "1.1.1",
"version": "1.2.0",
"author": "Tree Xie <vicansocanbico@gmail.com>",

@@ -6,0 +6,0 @@ "keywords": [

@@ -69,2 +69,3 @@ # async-local-storage

- `value` the value
- `linkedTop` set the value linked to top

@@ -163,4 +164,12 @@ ```js

### enableLinkedTop
Enable all value linked to top
### disableLinkedTop
Disable all value linked to top
## License
MIT

@@ -140,2 +140,14 @@ const assert = require('assert');

it('set to linked top data', (done) => {
als.set('top', 'abc');
setImmediate(() => {
als.set('immediate', true, true);
});
setTimeout(() => {
assert.equal(als.get('top'), 'abc');
assert.equal(als.get('immediate'), true);
done();
}, 10);
});
it('get the size', (done) => {

@@ -142,0 +154,0 @@ setTimeout(function() {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc