async-local-storage
Advanced tools
@@ -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(); |
+2
-0
@@ -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(); |
+4
-0
@@ -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 |
+38
-2
@@ -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; |
+1
-1
| { | ||
| "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": [ |
+9
-0
@@ -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 |
+12
-0
@@ -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() { |
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
20057
6.88%577
8.46%174
5.45%