Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
async-hooks-map
Advanced tools
A Thread-local storage (TLS) like Map implementation, base on node async hooks, support nodejs & typescript
Readme
npm install async-hooks-map
// typescript
import { AsyncHookMap } from 'async-hooks-map';
// javascript
const { AsyncHookMap } = require('async-hooks-map')
typescript:
import { AsyncHookMap } from 'async-hooks-map'
// import asyncHookMap from 'async-hooks-map'
// import global instance which is lazy initialize
// Object.defineProperty(exports, 'default', {
// get () {}
// })
const scope = new AsyncHookMap()
Promise.resolve().then(() => {
scope.set('aa', 'first')
scope.alias('ccc')
assert.equal(scope.get('aa'), 'first')
return Promise.resolve().then(() => {
assert(scope.has('aa'), 'should has the key')
assert(!scope.has('not'), 'should not has the key')
assert(!scope.has('aa', false), 'should not has the key in this scope')
assert.equal(scope.get('aa'), 'first')
scope.set('aa', 'second')
assert.equal(scope.get('aa'), 'second')
}).then(() => {
assert.equal(scope.get('aa'), 'second')
assert.equal(scope.closest('ccc').get('aa'), 'first')
// 'root' as alias of 'ccc'
assert.equal(scope.closest('root').get('aa'), 'first')
scope.closest().delete('aa')
// parent scope 'aa' has been delete, 'aa' will be first
assert.equal(scope.get('aa'), 'first')
scope.closest('ccc').set('bb', 'bb')
assert.equal(scope.get('bb'), 'bb')
scope.delete('bb')
// can not be deleted ,because bb is set to "ccc" scope
assert.equal(scope.get('bb'), 'bb')
})
})
})
Api:
export class AsyncHookMap<K=any, V=any>{
/**
* alias to asynchooks.executionAsyncId()
*
* @returns {number}
* @memberof AsyncHookMap
*/
executionAsyncId (): number {
return this._asyncHooks.executionAsyncId()
}
/**
* get the current AsyncMapNode
*
* @returns {AsyncMapNode<K, V>}
* @memberof AsyncHookMap
*/
current (): AsyncMapNode<K, V>
/**
* add alias of AsyncNode, a AsyncNode can own multi names
*
* @param {string} name
* @returns {this}
*/
alias (name: string): this
/**
* check the alias name
*
* @param {string} name
* @returns {boolean}
*/
hasName (name: string): boolean
/**
* get parent AsyncMapNode
* if name provided , return the named closest AsyncMapNode
* this method will throw an error if there is no parent
*
* @param {string} [name]
* @returns {AsyncMapNode<K, V>}
* @memberof AsyncStorageInterface
*/
parent (name?: string): AsyncMapNode<K, V> | undefined
/**
* alias of parent
*
* @param {string} name
* @returns {AsyncMapNode<K, V>}
* @memberof AsyncStorage
*/
closest (name: string): AsyncMapNode<K, V>
/**
* get from AsyncStorage
*
* @param {K} key
* @returns {(V | undefined)}
* @memberof AsyncStorage
*/
get (key: K): V | undefined
/**
* check key
* @param key
* @param recursion check forefathers?
*/
has (key: K, recursion = true): boolean
/**
* set value to current async AsyncNode
* effect current AsyncNode and children
*
* @param {K} key
* @param {V} value
* @returns {this}
* @memberof AsyncStorage
*/
set (key: K, value: V): this
/**
* delete the value of current AsyncNode
*
* @param {K} key
* @returns {boolean}
* @memberof AsyncStorage
*/
delete (key: K): boolean
/**
* clear the current AsyncNode
*
* @memberof AsyncStorage
*/
clear (): void
/**
* print the async path
*
* @memberof AsyncHookMap
*/
printPath (): void
/**
* get the distance of scope which has the key
*
* @param {K} key
* @returns {number}
* @memberof AsyncHookMap
*/
distance (key: K): number
}
export interface AsyncMapNode<K, V> {
hasName (name: string): boolean
alias (name: string): this
parent (name?: string): AsyncMapNode<K, V> | undefined
closest (name: string): AsyncMapNode<K, V>
has (key: K, recurse?: boolean): boolean
get (key: K): V | undefined
set (key: K, value: V): this
clear (): void
delete (key: K): boolean
}
FAQs
A Thread-local storage (TLS) like Map implementation, base on node async hooks, support nodejs & typescript
The npm package async-hooks-map receives a total of 77 weekly downloads. As such, async-hooks-map popularity was classified as not popular.
We found that async-hooks-map 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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.