New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

heimdalljs-fs-monitor

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

heimdalljs-fs-monitor - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

README.md

204

index.js
'use strict';
var fs = require('fs');
var heimdall = require('heimdalljs');
var logger = require('heimdalljs-logger')('heimdalljs-fs-monitor');
module.exports = FSMonitor;
const fs = require('fs');
const heimdall = require('heimdalljs');
const logger = require('heimdalljs-logger')('heimdalljs-fs-monitor');
// It is possible for this module to be evaluated more than once in the same
// heimdall session. In that case, we need to guard against double-counting by
// making other instances of FSMonitor inert.
var isMonitorRegistrant = false;
var isFirstInstance = true;
let isMonitorRegistrant = false;
let hasActiveInstance = false;
function FSMonitor() {
this.state = 'idle';
this.blacklist = ['createReadStream', 'createWriteStream', 'ReadStream', 'WriteStream'];
this._isEnabled = isMonitorRegistrant && isFirstInstance;
class FSMonitor {
constructor() {
this.state = 'idle';
this.blacklist = ['createReadStream', 'createWriteStream', 'ReadStream', 'WriteStream'];
}
// Flip this to false because we want other instances from the same module to
// be inert.
isFirstInstance = false;
}
start() {
if (isMonitorRegistrant && !hasActiveInstance) {
this.state = 'active';
this._attach();
hasActiveInstance = true;
} else {
logger.warn('Multiple instances of heimdalljs-fs-monitor have been created'
+ ' in the same session. Since this can cause fs operations to be counted'
+ ' multiple times, this instance has been disabled.');
}
}
FSMonitor.prototype.start = function() {
if (this._isEnabled) {
this.state = 'active';
this._attach();
} else {
logger.warn('Multiple instances of heimdalljs-fs-monitor have been created'
+ ' in the same session. Since this can cause fs operations to be counted'
+ ' multiple times, this instance has been disabled.');
stop() {
if (this.state === 'active') {
this.state = 'idle';
this._detach();
hasActiveInstance = false;
}
}
};
FSMonitor.prototype.stop = function() {
if (this._isEnabled) {
this.state = 'idle';
this._detach();
shouldMeasure() {
return this.state === 'active';
}
};
FSMonitor.prototype.shouldMeasure = function() {
return this.state === 'active';
};
_measure(name, original, context, args) {
if (this.state !== 'active') {
throw new Error('Cannot measure if the monitor is not active');
}
if (!heimdall.hasMonitor('fs')) {
heimdall.registerMonitor('fs', function FSSchema() {});
isMonitorRegistrant = true;
}
let metrics = heimdall.statsFor('fs');
let m = metrics[name] = metrics[name] || new Metric();
function Metric() {
this.count = 0;
this.time = 0;
this.startTime = undefined;
}
m.start();
Metric.prototype.start = function() {
this.startTime = process.hrtime();
this.count++;
};
// TODO: handle async
try {
return original.apply(context, args);
} finally {
m.stop();
}
}
Metric.prototype.stop = function() {
var now = process.hrtime();
_attach() {
let monitor = this;
this.time += (now[0] - this.startTime[0]) * 1e9 + (now[1] - this.startTime[1]);
this.startTime = undefined;
};
for (let member in fs) {
if (this.blacklist.indexOf(member) === -1) {
let old = fs[member];
if (typeof old === 'function') {
fs[member] = (function(old, member) {
return function() {
if (monitor.shouldMeasure()) {
let args = new Array(arguments.length);
for (let i = 0; i < arguments.length; i++) {
args[i] = arguments[i];
}
Metric.prototype.toJSON = function() {
return {
count: this.count,
time: this.time
};
};
return monitor._measure(member, old, fs, args);
} else {
return old.apply(fs, arguments);
}
};
}(old, member));
FSMonitor.prototype._measure = function(name, original, context, args) {
if (this.state !== 'active') {
throw new Error('Cannot measure if the monitor is not active');
fs[member].__restore = function() {
fs[member] = old;
};
}
}
}
}
var metrics = heimdall.statsFor('fs');
var m = metrics[name] = metrics[name] || new Metric();
_detach() {
for (let member in fs) {
let maybeFunction = fs[member];
if (typeof maybeFunction === 'function' && typeof maybeFunction.__restore === 'function') {
maybeFunction.__restore();
}
}
}
}
m.start();
module.exports = FSMonitor;
// TODO: handle async
try {
return original.apply(context, args);
} finally {
m.stop();
if (!heimdall.hasMonitor('fs')) {
heimdall.registerMonitor('fs', function FSSchema() {});
isMonitorRegistrant = true;
}
class Metric {
constructor() {
this.count = 0;
this.time = 0;
this.startTime = undefined;
}
};
FSMonitor.prototype._attach = function() {
var monitor = this;
start() {
this.startTime = process.hrtime();
this.count++;
}
for (var member in fs) {
if (this.blacklist.indexOf(member) === -1) {
var old = fs[member];
if (typeof old === 'function') {
fs[member] = (function(old, member) {
return function() {
if (monitor.shouldMeasure()) {
var args = new Array(arguments.length);
for (var i = 0; i < arguments.length; i++) {
args[i] = arguments[i];
}
stop() {
let now = process.hrtime();
return monitor._measure(member, old, fs, args);
} else {
return old.apply(fs, arguments);
}
};
}(old, member));
this.time += (now[0] - this.startTime[0]) * 1e9 + (now[1] - this.startTime[1]);
this.startTime = undefined;
}
fs[member].__restore = function() {
fs[member] = old;
};
}
}
toJSON() {
return {
count: this.count,
time: this.time
};
}
};
}
FSMonitor.prototype._detach = function() {
for (var member in fs) {
var maybeFunction = fs[member];
if (typeof maybeFunction === 'function' && typeof maybeFunction.__restore === 'function') {
maybeFunction.__restore();
}
}
};
{
"name": "heimdalljs-fs-monitor",
"version": "0.1.0",
"version": "0.2.0",
"description": "fs operation monitoring for heimdalljs",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "mocha tests.js",
"test:debug": "mocha debug tests.js"
},

@@ -15,6 +16,11 @@ "repository": {

"license": "ISC",
"files": [
"index.js"
],
"dependencies": {
"chai": "^3.5.0",
"heimdalljs": "^0.2.0",
"heimdalljs-logger": "^0.1.7"
"heimdalljs-logger": "^0.1.7",
"mocha": "^3.2.0"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc