
Product
Introducing Socket Firewall Enterprise: Flexible, Configurable Protection for Modern Package Ecosystems
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.
@loongsuite/cms_context
Advanced tools
CMS Context 提供基于 Node.js AsyncLocalStorage 和 async_hooks 的上下文管理器实现,用于在异步调用链中传递和保持上下文信息。这是实现分布式追踪上下文传播的关键组件。
# 使用 anpm (推荐)
anpm add @loongsuite/cms_context
# 或使用 npm
npm install @loongsuite/cms_context
上下文管理器负责在异步调用中维护和传递上下文信息。CMS Context 提供了两种实现:
这是默认的上下文管理器实现,基于 Node.js 12.17+ 的 AsyncLocalStorage API。
import { AsyncLocalStorageContextManager } from '@loongsuite/cms_context';
const contextManager = new AsyncLocalStorageContextManager();
// 获取当前上下文
const currentContext = contextManager.active();
// 在指定上下文中执行代码
const newContext = { traceId: 'abc123', spanId: 'def456' };
contextManager.with(newContext, () => {
// 在此作用域内,上下文为新上下文
const activeContext = contextManager.active();
console.log(activeContext); // { traceId: 'abc123', spanId: 'def456' }
// 异步操作也会保持上下文
setTimeout(() => {
const asyncContext = contextManager.active();
console.log(asyncContext); // 仍然是 { traceId: 'abc123', spanId: 'def456' }
}, 100);
});
基于 async_hooks 的上下文管理器,兼容更老的 Node.js 版本。
import { AsyncHooksContextManager } from '@loongsuite/cms_context';
const contextManager = new AsyncHooksContextManager();
// 使用方式与 AsyncLocalStorageContextManager 相同
const context = { userId: 'user123', requestId: 'req456' };
contextManager.with(context, () => {
// 业务逻辑
processAsyncOperation();
});
import {
AsyncLocalStorageContextManager,
AsyncHooksContextManager,
IContextManager,
Context
} from '@loongsuite/cms_context';
import { AsyncLocalStorageContextManager } from '@loongsuite/cms_context';
const contextManager = new AsyncLocalStorageContextManager();
// 设置初始上下文
const initialContext = {
traceId: 'trace-123',
spanId: 'span-456',
userId: 'user-789'
};
contextManager.with(initialContext, () => {
console.log('Initial context:', contextManager.active());
// 嵌套的异步操作
setTimeout(() => {
console.log('Async context:', contextManager.active());
// 更深层的异步操作
Promise.resolve().then(() => {
console.log('Promise context:', contextManager.active());
});
}, 100);
});
import { AsyncLocalStorageContextManager } from '@loongsuite/cms_context';
import { TracerManager } from '@loongsuite/cms_trace';
const contextManager = new AsyncLocalStorageContextManager();
const tracerManager = new TracerManager({
contextManager,
// ... 其他配置
});
const tracer = tracerManager.getTracer('my-service');
// 创建 span 并设置到上下文
const span = tracer.startSpan('operation');
const contextWithSpan = tracerManager.setSpan(contextManager.active(), span);
contextManager.with(contextWithSpan, () => {
// 在此上下文中,可以获取到当前的 span
const currentSpan = tracerManager.getActiveSpan(contextManager.active());
console.log('Current span:', currentSpan);
// 异步操作会保持 span 上下文
setTimeout(() => {
const asyncSpan = tracerManager.getActiveSpan(contextManager.active());
console.log('Async span:', asyncSpan); // 仍然是同一个 span
}, 100);
});
import { AsyncLocalStorageContextManager } from '@loongsuite/cms_context';
const contextManager = new AsyncLocalStorageContextManager();
// 绑定上下文到函数
const context = { requestId: 'req-123' };
const boundFunction = contextManager.bind(context, (message: string) => {
const activeContext = contextManager.active();
console.log(`[${activeContext.requestId}] ${message}`);
});
// 即使在不同上下文中调用,也会使用绑定的上下文
contextManager.with({ requestId: 'req-456' }, () => {
boundFunction('Hello World'); // 输出: [req-123] Hello World
});
import { AsyncLocalStorageContextManager } from '@loongsuite/cms_context';
const contextManager = new AsyncLocalStorageContextManager();
const context = { operationId: 'op-123' };
try {
contextManager.with(context, () => {
// 可能抛出错误的操作
throw new Error('Something went wrong');
});
} catch (error) {
// 错误处理时仍然可以访问上下文
const errorContext = contextManager.active();
console.log(`Error in context ${errorContext.operationId}:`, error.message);
}
// 好的做法:批量操作
contextManager.with(context, () => {
// 执行多个相关操作
operation1();
operation2();
operation3();
});
// 避免:频繁切换上下文
contextManager.with(context1, () => operation1());
contextManager.with(context2, () => operation2());
contextManager.with(context3, () => operation3());
NodeSDK 默认使用 AsyncLocalStorageContextManager,通常无需手动配置:
import { NodeSDK } from '@loongsuite/cms_node_sdk';
const sdk = new NodeSDK({
serviceName: 'my-service',
// contextManager 会自动使用 AsyncLocalStorageContextManager
});
sdk.start();
// 获取上下文管理器
const contextManager = sdk.getContextManager();
console.log(contextManager instanceof AsyncLocalStorageContextManager); // true
import { AsyncLocalStorageContextManager } from '@loongsuite/cms_context';
const contextManager = new AsyncLocalStorageContextManager();
// 添加调试信息
const debugContext = {
...context,
_debug: {
timestamp: Date.now(),
stack: new Error().stack
}
};
contextManager.with(debugContext, () => {
console.log('Context debug info:', contextManager.active()._debug);
});
@loongsuite/cms_core: 核心接口和类型定义MIT License
FAQs
## 简介
We found that @loongsuite/cms_context demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers 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
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.

Security News
Open source dashboard CNAPulse tracks CVE Numbering Authorities’ publishing activity, highlighting trends and transparency across the CVE ecosystem.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.