Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

detect-invalid-requires

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

detect-invalid-requires - npm Package Compare versions

Comparing version 0.0.1 to 0.1.0

9

lib/cli.js

@@ -5,7 +5,6 @@ #!/usr/bin/env node

var detector = require('./Index');
var invalid = detector(process.argv.slice(2));
var detector = require('./index');
function outputInvalid(source) {
if ('path' in source)
if (typeof source.path !== 'undefined')
console.log('Invalid require: '.red + source.path.yellow + ' in ' + source.file.yellow);

@@ -16,2 +15,4 @@ else

invalid.forEach(outputInvalid);
detector(process.argv[2], function (invalid) {
invalid.forEach(outputInvalid);
});
'use strict';
var esprima = require('esprima');
var esquery = require('esquery');
var fs = require('fs');
var path = require('path');
require('colors');
var walker = require('walker');

@@ -12,20 +14,2 @@ function getDirOf(file) {

// Stolen from https://github.com/ariya/esprima/blob/master/examples/findbooleantrap.js
// Executes visitor on the object and its children (recursively).
function traverse(object, visitor) {
var key, child;
if (visitor.call(null, object) === false) {
return;
}
for (key in object) {
if (object.hasOwnProperty(key)) {
child = object[key];
if (typeof child === 'object' && child !== null) {
traverse(child, visitor);
}
}
}
}
function chompShebang(script) {

@@ -35,34 +19,35 @@ return script.replace(/^\#\![^\n]+/, '');

module.exports = function (files) {
if (!Array.isArray(files)) files = [files];
module.exports = function (inputPath, callback) {
var invalid = [];
files.filter(function (file) {
if (!fs.existsSync(getDirOf(file))) {
invalid.push({ file: file });
return false;
}
return true;
}).forEach(function (file) {
var script = fs.readFileSync(getDirOf(file), 'utf-8');
inputPath = path.resolve(process.cwd(), inputPath || '.');
walker(inputPath).on('file', function (file) {
if (!file.match(/\.js$/)) return;
var script = fs.readFileSync(file, { encoding: 'utf-8' });
var content = esprima.parse(chompShebang(script), { tolerant: true });
traverse(content, function (node) {
if (node.type === 'CallExpression' && node.callee.name === 'require') {
if (!node.arguments.length || !node.arguments[0].value) return;
var requireStr = node.arguments[0].value;
//remove non-relative paths
if (['.', '\\', '/'].indexOf(requireStr.slice(0, 1)) === -1) return;
var targetDir = path.join(path.dirname(file), path.dirname(requireStr));
var requires = esquery(content, 'CallExpression[callee.name="require"]');
//error out invalid path
if (!fs.existsSync(targetDir)) return invalid.push({file: file, path: requireStr });
requires.forEach(function (node) {
var requireStr = node.arguments[0].value;
var dirFiles = fs.readdirSync(targetDir);
if (dirFiles.indexOf(path.basename(requireStr)) !== -1 || dirFiles.indexOf(path.basename(requireStr) + '.js') !== -1) return;
//remove non-relative paths
if (typeof requireStr === 'string' && ['.', '\\', '/'].indexOf(requireStr.slice(0, 1)) === -1) return;
var targetDir = path.resolve(path.dirname(file), path.dirname(requireStr));
//error out invalid path
if (!fs.existsSync(targetDir)) {
invalid.push({file: file, path: requireStr });
return;
}
var dirFiles = fs.readdirSync(targetDir);
if (dirFiles.indexOf(path.basename(requireStr)) !== -1 || dirFiles.indexOf(path.basename(requireStr) + '.js') !== -1) return;
invalid.push({file: file, path: requireStr });
});
}).on('end', function() {
callback(invalid);
});
return invalid;
};
{
"name": "detect-invalid-requires",
"version": "0.0.1",
"version": "0.1.0",
"description": "utility to detect requires that are not legal in a case-sensitive OS",

@@ -12,8 +12,12 @@ "main": "index.js",

"bin": "lib/cli.js",
"repository": "https://github.com/justinjmoses/detect-invalid-requires",
"dependencies": {
"esprima": "=1.2.x"
"esprima": "1.2.x",
"esquery": "0.3.x",
"walker": "1.0.x"
},
"devDependencies": {
"chai": "=1.9.x"
"chai": "1.9.x",
"mocha": "1.21.x"
}
}

Sorry, the diff of this file is not supported yet

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