Skip to content

Commit

Permalink
Merge pull request #2 from serkan-ozal/improvement/introduce-disable-…
Browse files Browse the repository at this point in the history
…flag

Introduce flag by `TREQ_DISABLE` environment variable to disable `treq`
  • Loading branch information
Serkan ÖZAL authored Mar 29, 2022
2 parents 9b51148 + fb38a66 commit fcead13
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<a name="0.0.2"></a>
# 0.0.2 (2022-03-29)

### Features

* Introduce flag by `TREQ_DISABLE` environment variable to disable `treq`

<a name="0.0.1"></a>
# 0.0.1 (2022-03-29)

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -14,7 +15,7 @@ class RequireTrace {

const activeReqs = [];

Module.prototype.require = function traceRequires(id) {
function traceRequire(id) {
// Get current time
const ts = process.hrtime();

Expand Down Expand Up @@ -51,7 +52,7 @@ Module.prototype.require = function traceRequires(id) {
}

return res;
};
}

function dumpRequireStack(reqTrace, depth = 0) {
if (reqTrace.duration && reqTrace.duration >= DURATION_THRESHOLD) {
Expand All @@ -70,3 +71,7 @@ function dumpRequireStack(reqTrace, depth = 0) {
}
}
}

if (!DISABLED) {
Module.prototype.require = traceRequire;
}

0 comments on commit fcead13

Please sign in to comment.