Async 磁共振
为Node.js应用程序使用Aync钩实施轻量级,型号安全的Mapped诊断上下文(MDC). 这个库为管理跨同步操作的上下文数据提供了一个干净的方法,使它能完美地用于请求追踪,记录,并在整个呼叫链中保持应用状态.
特征
- 🚀 无依赖 - 在Node.js本土建造
AsyncLocalStorage - 🔒 类型安全 - 配备自定义存储界面的完整类型脚本支持
- 🛡️ 安全作业 - 所有业务的投放和非投放变体
- 🔄 锁定集成 - 与皮诺、布尼扬、温斯顿和其他伐木工无缝合作
安装
$ npm i --save @bluegroundltd/async-mdc # NPM
$ yarn add --save @bluegroundltd/async-mdc # Yarn
$ bun add --save @bluegroundltd/async-mdc # Bun
$ pnpm add --save @bluegroundltd/async-mdc # PNPM
基本用途
// mdc.ts
import {MDC} from '@bluegroundltd/async-mdc';
export {MDC};
// You can define your own MDC store or use the default Record<string, any>
export interface MDCStore extends Record<string, unknown> {
correlationId: string;
user: {
userId: string;
email: string;
};
ip: string;
meta?: {
foo?: string;
bar?: string;
};
}
export const mdc = new MDC<MDCStore>();
// middleware.ts
import {mdc} from 'mdc.js';
app.get((req, res, next) => {
mdc.set('user', req.session.user);
next();
});
// service.ts
function foo() {
// access user
const user = mdc.safeGet('user');
}
API 密码
新建 MDC( [存储] ): MDC
import {MDC} from '@bluegroundltd/async-mdc';
export {MDC};
// You can define your own MDC store or use the default Record<string, any>
export interface MDCStore extends Record<string, unknown> {
color: string;
height?: number;
}
const mdc = new MDC<MDCStore>();
// or define your own storage
const asl = new AsyncLocalStorage<MDCStore>();
const mdc = new MDC<MDCStore>(asl);
获取( 键, [默认值])
获取与之相关的值 key。 。 。 .
get 如果下方为 本地同步组件 未初始化
mdc.get('color');
mdc.get('color', 'black');
安全获取( 密钥, [默认值])
获取与之相关的值 key。 。 。 .
safeGet 永不掷出并返回 undefined 或者说 defaultValue 如果给定则
mdc.safeGet('color');
mdc.safeGet('color', 'black');
设置( 键, 值)
在此 mdc 中添加或更新一个带有指定密钥和值的条目.
set 如果下方为 本地同步组件 未初始化
mdc.set('color', 'white');
安全设置( 键, 值)
在此 mdc 中添加或更新一个带有指定密钥和值的条目.
safeSet 永远不要抛出和行为 作为一个 noop 代替
mdc.safeSet('color', 'white');
获取 CopyOfsStore (): 存储
getCopyOfStore 返回 aync 上下文存储的浅色副本
mdc.getCopyOfStore();
清除( c)
通过去除商店的所有密钥来清除商店
mdc.clear();
运行( store: store, callback : () QQ R, ... args): R
基层机构的代表 aync 本地服务器
用于伐木机
皮诺
https://github.com/pinojs/pino (英语)
// logger.ts
import {pino} from 'pino';
import {MDC} from '@bluegroundltd/async-mdc';
// Import from another module e.g. mdc.js
import {mdc} from 'mdc.js';
// Or create and export from here
export const mdc = new MDC();
// Enrich every log message with bindings from the MDC store
// @see {@link https://github.com/pinojs/pino/blob/main/docs/api.md#mixin-function}
function mixin() {
return mdc.getCopyOfStore();
}
export const logger = pino({
mixin
});
邦扬
https://github.com/trentm/node-bunyan 页面存档备份,存于互联网档案馆
// logger.ts
import bunyan from 'bunyan';
import {MDC} from '@bluegroundltd/async-mdc';
// Import from another module e.g. mdc.js
import {mdc} from 'mdc.js';
// Or create and export from here
export const mdc = new MDC();
const logger = bunyan.createLogger({
name: 'myapp',
mdc,
serializers: {
...bunyan.stdSerializers,
mdc(_mdc) {
return _mdc.getCopyOfStore();
}
}
});
温斯顿 3.x
https://github.com/winstonjs/winston (英语)
// logger.ts
import winston from 'winston';
import {MDC} from '@bluegroundltd/async-mdc';
// Import from another module e.g. mdc.js
import {mdc} from 'mdc.js';
// Or create and export from here
export const mdc = new MDC();
const mdcFormat = winston.format((info, opts) => {
return {
...mdc.getCopyOfStore(),
...info
};
});
const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(winston.format.json(), mdcFormat)
});
许可证
这个项目根据麻省理工学院许可证颁发——见 自由主义 详细文件.
MIT 许可证
特此准许任何人免费获得本软件和相关文件文件("软件")的副本,不受限制地在软件中进行交易,包括不受限制地享有软件的使用、复制、修改、合并、出版、分发、分版和(或)销售的权利,并允许软件获得者这样做,但须符合下列条件: 1
上述版权通知和许可通知应载入软件的所有副本或大部分.
软件是提供的"AS IS", 没有任何种类的恐惧, 释放或被卷入, 包括但不限制在 危机的保障, 证明一个特定的目的和无关。 任何索赔、索赔或其他负债,无论是合同、交易或其他行为,产生于、产生于或涉及软件或软件的使用或其他交易的行为,都不得作为担保.
版权:"2025"蓝地有限公司. 版权所有.