Comparing version 4.2.5 to 5.0.0
@@ -1,1 +0,2 @@ | ||
export declare const ROARR_VERSION = "4.1.5"; | ||
export declare const ROARR_VERSION = "5.0.0"; | ||
export declare const ROARR_LOG_FORMAT_VERSION = "1.0.0"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.ROARR_VERSION = void 0; | ||
exports.ROARR_LOG_FORMAT_VERSION = exports.ROARR_VERSION = void 0; | ||
// This needs to be updated manually because there is no way | ||
// to know the package version at the build time. | ||
exports.ROARR_VERSION = '4.1.5'; | ||
exports.ROARR_VERSION = '5.0.0'; | ||
exports.ROARR_LOG_FORMAT_VERSION = '1.0.0'; |
@@ -6,3 +6,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const detect_node_1 = __importDefault(require("detect-node")); | ||
const fast_printf_1 = require("fast-printf"); | ||
@@ -12,54 +11,13 @@ const globalthis_1 = __importDefault(require("globalthis")); | ||
const json_stringify_safe_1 = __importDefault(require("json-stringify-safe")); | ||
const config_1 = require("../config"); | ||
const constants_1 = require("../constants"); | ||
const globalThis = globalthis_1.default(); | ||
let domain; | ||
if (detect_node_1.default) { | ||
// eslint-disable-next-line @typescript-eslint/no-require-imports | ||
domain = require('domain'); | ||
} | ||
const getParentDomainContext = () => { | ||
const getAsyncLocalContext = () => { | ||
var _a; | ||
if (!domain) { | ||
const asyncLocalStorage = globalThis.ROARR.asyncLocalStorage; | ||
if (!asyncLocalStorage) { | ||
return {}; | ||
} | ||
const parentRoarrContexts = []; | ||
let currentDomain = process.domain; | ||
if (!currentDomain || !currentDomain.parentDomain) { | ||
return {}; | ||
} | ||
while (currentDomain === null || currentDomain === void 0 ? void 0 : currentDomain.parentDomain) { | ||
currentDomain = currentDomain.parentDomain; | ||
if ((_a = currentDomain === null || currentDomain === void 0 ? void 0 : currentDomain.roarr) === null || _a === void 0 ? void 0 : _a.context) { | ||
parentRoarrContexts.push(currentDomain.roarr.context); | ||
} | ||
} | ||
let domainContext = {}; | ||
for (const parentRoarrContext of parentRoarrContexts) { | ||
domainContext = { | ||
...domainContext, | ||
...parentRoarrContext, | ||
}; | ||
} | ||
return domainContext; | ||
return ((_a = asyncLocalStorage.getStore()) === null || _a === void 0 ? void 0 : _a.context) || {}; | ||
}; | ||
const getFirstParentDomainContext = () => { | ||
var _a, _b; | ||
if (!domain) { | ||
return {}; | ||
} | ||
let currentDomain = process.domain; | ||
if ((_a = currentDomain === null || currentDomain === void 0 ? void 0 : currentDomain.roarr) === null || _a === void 0 ? void 0 : _a.context) { | ||
return currentDomain.roarr.context; | ||
} | ||
if (!currentDomain || !currentDomain.parentDomain) { | ||
return {}; | ||
} | ||
while (currentDomain === null || currentDomain === void 0 ? void 0 : currentDomain.parentDomain) { | ||
currentDomain = currentDomain.parentDomain; | ||
if ((_b = currentDomain === null || currentDomain === void 0 ? void 0 : currentDomain.roarr) === null || _b === void 0 ? void 0 : _b.context) { | ||
return currentDomain.roarr.context; | ||
} | ||
} | ||
return {}; | ||
}; | ||
const defaultContext = {}; | ||
@@ -70,18 +28,19 @@ const createLogger = (onMessage, parentContext) => { | ||
const sequence = globalThis.ROARR.sequence++; | ||
const asyncLocalStorage = globalThis.ROARR.asyncLocalStorage; | ||
let context; | ||
let message; | ||
if (typeof a === 'string') { | ||
if (!domain || process.domain === null) { | ||
context = parentContext || defaultContext; | ||
} | ||
else { | ||
if (asyncLocalStorage) { | ||
context = { | ||
...getFirstParentDomainContext(), | ||
...getAsyncLocalContext(), | ||
...parentContext, | ||
}; | ||
} | ||
else { | ||
context = parentContext || defaultContext; | ||
} | ||
} | ||
else { | ||
context = { | ||
...getFirstParentDomainContext(), | ||
...getAsyncLocalContext(), | ||
...parentContext, | ||
@@ -111,3 +70,3 @@ ...a, | ||
time, | ||
version: '1.0.0', | ||
version: config_1.ROARR_LOG_FORMAT_VERSION, | ||
}); | ||
@@ -126,3 +85,3 @@ }; | ||
return createLogger(onMessage, { | ||
...getFirstParentDomainContext(), | ||
...getAsyncLocalContext(), | ||
...parentContext, | ||
@@ -134,3 +93,3 @@ ...context, | ||
return { | ||
...getFirstParentDomainContext(), | ||
...getAsyncLocalContext(), | ||
...parentContext || defaultContext, | ||
@@ -140,14 +99,13 @@ }; | ||
log.adopt = async (routine, context) => { | ||
if (!domain) { | ||
var _a; | ||
const asyncLocalStorage = globalThis.ROARR.asyncLocalStorage; | ||
if (!asyncLocalStorage) { | ||
return routine(); | ||
} | ||
const adoptedDomain = domain.create(); | ||
return adoptedDomain | ||
.run(() => { | ||
adoptedDomain.roarr = { | ||
context: { | ||
...getParentDomainContext(), | ||
...context, | ||
}, | ||
}; | ||
return asyncLocalStorage.run({ | ||
context: { | ||
...(_a = asyncLocalStorage.getStore()) === null || _a === void 0 ? void 0 : _a.context, | ||
...context, | ||
}, | ||
}, () => { | ||
return routine(); | ||
@@ -154,0 +112,0 @@ }); |
@@ -26,5 +26,9 @@ "use strict"; | ||
if (detect_node_1.default && (currentIsLatestVersion || !newState.write)) { | ||
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires | ||
const AsyncLocalStorage = require('async_hooks').AsyncLocalStorage; | ||
const asyncLocalStorage = new AsyncLocalStorage(); | ||
newState = { | ||
...newState, | ||
...createNodeWriter_1.default(), | ||
asyncLocalStorage, | ||
}; | ||
@@ -31,0 +35,0 @@ } |
@@ -0,5 +1,9 @@ | ||
/// <reference types="node" /> | ||
import type { AsyncLocalStorage } from 'async_hooks'; | ||
export declare type Writer = { | ||
write: (message: string) => void; | ||
}; | ||
export declare type MessageContext = any; | ||
export declare type RoarrGlobalState = Writer & { | ||
asyncLocalStorage?: AsyncLocalStorage<MessageContext>; | ||
sequence: number; | ||
@@ -9,3 +13,2 @@ versions: readonly string[]; | ||
export declare type SprintfArgument = boolean | number | string | null; | ||
export declare type MessageContext = any; | ||
export declare type Message = { | ||
@@ -12,0 +15,0 @@ readonly context: MessageContext; |
@@ -51,3 +51,3 @@ { | ||
"engines": { | ||
"node": ">=10.0" | ||
"node": ">=12.0" | ||
}, | ||
@@ -81,3 +81,3 @@ "husky": { | ||
"typings": "./dist/src/log.d.ts", | ||
"version": "4.2.5" | ||
"version": "5.0.0" | ||
} |
@@ -272,3 +272,3 @@ <a name="roarr"></a> | ||
`adopt` function uses Node.js [`domain`](https://nodejs.org/api/domain.html) to pass-down context properties. | ||
`adopt` function uses Node.js [`async_context`](https://nodejs.org/api/async_context.html) to pass-down context properties. | ||
@@ -305,3 +305,2 @@ When using `adopt`, context properties will be added to all _all_ Roarr messages within the same asynchronous context, e.g. | ||
* `adopt` method only works in Node.js. | ||
* You must shim Node.js using [`domain-parent`](https://github.com/gajus/domain-parent). | ||
@@ -308,0 +307,0 @@ <a name="roarr-api-child"></a> |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
57874
1028
706
3