From fb38a66a7ac03a74b950db4ace587eb723432bdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Serkan=20=C3=96ZAL?= Date: Tue, 29 Mar 2022 08:15:17 +0300 Subject: [PATCH] Introduce flag by `TREQ_DISABLE` environment variable to disable `treq` --- CHANGELOG.md | 7 +++++++ README.md | 5 +++++ src/index.js | 9 +++++++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b73d71..ae37945 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. + +# 0.0.2 (2022-03-29) + +### Features + +* Introduce flag by `TREQ_DISABLE` environment variable to disable `treq` + # 0.0.1 (2022-03-29) diff --git a/README.md b/README.md index 7ef3de9..4b1aaa8 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,11 @@ NODE_OPTIONS=-r treq Requires/imports take lower than the threshold are ignored from tracing. By default, the threshold is `10` milliseconds and it can be configured by `TREQ_DURATION_THRESHOLD` environment variable. +* **Optionally**, you can disable `treq` by setting `TREQ_DISABLE` environment variable to `true`. +``` +TREQ_DISABLE=true +``` + ## Contributing diff --git a/src/index.js b/src/index.js index b1e8fa8..b114309 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,7 @@ const originalRequire = Module.prototype.require; const DURATION_THRESHOLD = parseInt(process.env['TREQ_DURATION_THRESHOLD']) || 10; +const DISABLED = process.env['TREQ_DISABLE'] === 'true'; class RequireTrace { constructor(moduleId) { @@ -14,7 +15,7 @@ class RequireTrace { const activeReqs = []; -Module.prototype.require = function traceRequires(id) { +function traceRequire(id) { // Get current time const ts = process.hrtime(); @@ -51,7 +52,7 @@ Module.prototype.require = function traceRequires(id) { } return res; -}; +} function dumpRequireStack(reqTrace, depth = 0) { if (reqTrace.duration && reqTrace.duration >= DURATION_THRESHOLD) { @@ -70,3 +71,7 @@ function dumpRequireStack(reqTrace, depth = 0) { } } } + +if (!DISABLED) { + Module.prototype.require = traceRequire; +}