
Security News
Open Source Maintainers Feeling the Weight of the EU’s Cyber Resilience Act
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
async-local-storage
Advanced tools
I want something like thread-local
storage in threaded programming and async_hooks
is usable in node.js 8.0
, so there is an easy way to use thread-local
.
const als = require('async-local-storage');
als.enable();
setTimeout(() => {
const id = randomBytes(8);
als.set('id', id);
delay().then(() => {
assert.equal(als.get('id'), id);
return readfilePromise(__filename);
}).then(() => {
assert.equal(als.get('id'), id);
return superagent.get('http://www.baidu.com/');
}).then(() => {
assert.equal(als.get('id'), id);
});
}, 100);
enable the async hooks
const als = require('async-local-storage');
als.enable();
disable the async hooks
const als = require('async-local-storage');
als.enable();
setTimeout(() => {
als.disable();
}, 100);
get the size of storage
const als = require('async-local-storage');
als.enable();
setTimeout(() => {
console.info(als.size());
}, 100);
set the value by key for the current id
key
the keyvalue
the valueconst assert = require('assert');
const als = require('async-local-storage');
const crypto = require('crypto');
const randomBytes = length => crypto.randomBytes(length).toString('hex')
als.enable()
setTimeout(() => {
const id = randomBytes(8);
als.set('id', id);
const check = (times, id, subId) => {
assert.equal(als.get('id'), id);
assert.equal(als.get('subId'), subId);
if (times < 0) {
done();
} else {
setTimeout(() => {
check(times - 1, id, subId);
}, 10);
}
};
setTimeout(() => {
const subId = randomBytes(8);
als.set('subId', subId);
assert.equal(als.get('id'), id);
check(3, id, subId);
}, 10);
}, 10);
get the value by key, if will find from parent, self --> parent --> parent, until the value is not undefined
key
the keyconst assert = require('assert');
const als = require('async-local-storage');
const crypto = require('crypto');
const randomBytes = length => crypto.randomBytes(length).toString('hex')
als.enable()
setTimeout(() => {
const id = randomBytes(8);
als.set('id', id);
const check = (times, id, subId) => {
assert.equal(als.get('id'), id);
assert.equal(als.get('subId'), subId);
if (times < 0) {
done();
} else {
setTimeout(() => {
check(times - 1, id, subId);
}, 10);
}
};
setTimeout(() => {
const subId = randomBytes(8);
als.set('subId', subId);
assert.equal(als.get('id'), id);
check(3, id, subId);
}, 10);
}, 10);
Get the current id
const assert = require('assert');
const als = require('async-local-storage');
als.enable();
setTimeout(() => {
assert(als.currentId());
}, 10);
id
The tigger id, default is als.currentId()
const assert = require('assert');
const als = require('async-local-storage');
als.enable();
setTimeout(() => {
assert(als.use());
}, 10);
MIT
FAQs
Get the value like thread-local storage in threaded programming
The npm package async-local-storage receives a total of 4,542 weekly downloads. As such, async-local-storage popularity was classified as popular.
We found that async-local-storage demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.