Research
Security News
Threat Actor Exposes Playbook for Exploiting npm to Build Blockchain-Powered Botnets
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
@types/cls-hooked
Advanced tools
@types/cls-hooked provides TypeScript type definitions for the cls-hooked package, which is used for context propagation across asynchronous calls in Node.js. It allows you to create a context that can be accessed throughout the lifecycle of a request, even across asynchronous boundaries.
Creating a Namespace
This feature allows you to create a new namespace. A namespace is a context that can be used to store and retrieve data across asynchronous calls.
const cls = require('cls-hooked');
const namespace = cls.createNamespace('myNamespace');
Setting and Getting Context
Within a namespace, you can set and get values that will be available throughout the lifecycle of the asynchronous operations.
namespace.run(() => {
namespace.set('key', 'value');
console.log(namespace.get('key')); // Outputs: value
});
Binding Functions to a Namespace
You can bind functions to a namespace so that they have access to the context set within that namespace.
const boundFunction = namespace.bind(() => {
console.log(namespace.get('key'));
});
namespace.run(() => {
namespace.set('key', 'value');
boundFunction(); // Outputs: value
});
The async_hooks module provides an API to track asynchronous resources in Node.js. It is a lower-level API compared to cls-hooked and requires more manual management of context propagation.
This package is similar to cls-hooked and provides a way to maintain context across asynchronous calls. However, it is older and less maintained compared to cls-hooked.
zone.js is a library for managing asynchronous context propagation in JavaScript. It is more commonly used in Angular applications but can be used in Node.js as well. It provides a more comprehensive solution for managing asynchronous contexts.
npm install --save @types/cls-hooked
This package contains type definitions for cls-hooked (https://github.com/jeff-lewis/cls-hooked).
Files were exported from https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/cls-hooked.
// Type definitions for cls-hooked 4.3
// Project: https://github.com/jeff-lewis/cls-hooked
// Definitions by: Leo Liang <https://github.com/aleung>
// Lucian Buzzo <https://github.com/LucianBuzzo>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node" />
import { EventEmitter } from 'events';
export interface Namespace<N = Record<string, any>> {
active: any;
set<K extends keyof N = keyof N>(key: K, value: N[K]): N[K];
get<K extends keyof N = keyof N>(key: K): N[K];
run(fn: (...args: any[]) => void): void;
runAndReturn<T>(fn: (...args: any[]) => T): T;
runPromise<T>(fn: (...args: any[]) => Promise<T>): Promise<T>;
bind<F extends Function>(fn: F, context?: any): F; // tslint:disable-line: ban-types
bindEmitter(emitter: EventEmitter): void;
createContext(): any;
enter(context: any): void;
exit(context: any): void;
}
// eslint-disable-next-line no-unnecessary-generics
export function createNamespace<N = Record<string, any>>(name: string): Namespace<N>;
// eslint-disable-next-line no-unnecessary-generics
export function getNamespace<N = Record<string, any>>(name: string): Namespace<N> | undefined;
export function destroyNamespace(name: string): void;
export function reset(): void;
These definitions were written by Leo Liang, and Lucian Buzzo.
FAQs
TypeScript definitions for cls-hooked
The npm package @types/cls-hooked receives a total of 0 weekly downloads. As such, @types/cls-hooked popularity was classified as not popular.
We found that @types/cls-hooked demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.