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

fs-merger

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fs-merger - npm Package Compare versions

Comparing version 2.0.1 to 2.0.2

57

index.js

@@ -6,22 +6,10 @@ "use strict";

const nodefs = require('fs');
const WRITEOPERATION = new Set([
'write',
'writeSync',
'writeFile',
'writeFileSync',
'writev',
'writevSync',
'appendFileSync',
'appendFile',
'rmdir',
'rmdirSync',
'mkdir',
'mkdirSync'
const WHITELISTEDOPERATION = new Set([
'readFileSync',
'existsSync',
'lstatSync',
'statSync',
'readdirSync'
]);
const READDIR = new Set([
'readdirSync',
'readdir'
]);
function getRootAndPrefix(tree) {

@@ -61,23 +49,12 @@ let root = '';

get(target, propertyName) {
if(!WRITEOPERATION.has(propertyName)) {
if (READDIR.has(propertyName)) {
return function() {
let [relativePath] = arguments;
if (path.isAbsolute(relativePath) && relativePath.trim() != '/') {
return target[propertyName](...arguments);
}
return self[propertyName](...arguments);
}
} else if (self[propertyName] && !target[propertyName]) { // when fsMerger.fs.hasOwnProperty is accessed we shouldn't be hijacking it to self.hasOwnProperty
return function() {
return self[propertyName](...arguments);
}
} else if (typeof target[propertyName] !== 'function') {
return target[propertyName];
}
if(WHITELISTEDOPERATION.has(propertyName) || self[propertyName]) {
return function() {
let [relativePath] = arguments;
let { _dirList } = self;
let fullPath = relativePath;
if (!path.isAbsolute(relativePath)) {
// if property is present in the FSMerge do not hijack it with fs operations
if (self[propertyName]) {
return self[propertyName](...arguments);
}
let { _dirList } = self;
let fullPath = relativePath;
for (let i=0; i < _dirList.length; i++) {

@@ -90,8 +67,10 @@ let { root } = getRootAndPrefix(_dirList[i]);

}
arguments[0] = fullPath;
return target[propertyName](...arguments);
} else {
throw new Error(`Relative path is expected, path ${relativePath} is an absolute path. inputPath gets prefixed to the reltivePath provided.`);
}
arguments[0] = fullPath;
return target[propertyName](...arguments);
}
} else {
throw new Error(`Operation ${propertyName} is a write operation, not allowed with FSMerger.fs`);
throw new Error(`Operation ${propertyName} is not allowed with FSMerger.fs. Whitelisted operations are ${Array.from(WHITELISTEDOPERATION).toString()}`);
}

@@ -98,0 +77,0 @@ }

{
"name": "fs-merger",
"version": "2.0.1",
"version": "2.0.2",
"description": "Reads files from a real location",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -156,9 +156,9 @@ "use strict";

describe('read / folder', function() {
describe('read ./ folder', function() {
it('readdirsync', function() {
let content = fsMerger.fs.readdirSync('/');
let content = fsMerger.fs.readdirSync('./');
expect(content).to.be.deep.equal([ 'a.txt', 'test-1', 'x.txt','c.txt', 'test-sub-1', 'test-sub-2', 'b.txt', 'd.txt']);
});
it('readdir', function(done) {
fsMerger.fs.readdir('/', function (err, content) {
fsMerger.fs.readdir('./', function (err, content) {
expect(content).to.have.all.members([ 'a.txt', 'test-1', 'x.txt','c.txt', 'test-sub-1', 'test-sub-2', 'b.txt', 'd.txt']);

@@ -170,20 +170,6 @@ done();

describe('reading folder invalid folder will throw with absolute path', function() {
it('readdirsync', function() {
expect(() => {
fsMerger.fs.readdirSync('/sfsd')
}).throw(/ENOENT\: no such file or directory, scandir.*/);
});
it('readdir', function(done) {
fsMerger.fs.readdir('/sfsd', function (err) {
expect(err.message).to.be.contains(`ENOENT: no such file or directory, scandir`);
done();
});
});
});
describe('reading folder invalid folder will throw', function() {
it('readdirsync', function() {
expect(() => {
fsMerger.fs.readdirSync('/sfsd')
fsMerger.fs.readdirSync('sfsd')
}).throw(/ENOENT\: no such file or directory, scandir.*/);

@@ -247,24 +233,7 @@ });

it('hasOwnProperty works', function() {
debugger
let content = fsMerger.fs.hasOwnProperty('existsSync');
expect(content).to.be.true;
it('absolute path throws error', function() {
let filepath = `${__dirname}/../fixtures/test-1`;
expect( ()=>{fsMerger.fs.existsSync(filepath)}).to.throw(`Relative path is expected, path ${filepath} is an absolute path. inputPath gets prefixed to the reltivePath provided.`);
});
it('non function property works', function() {
expect(fsMerger.fs.F_OK).to.be.equal(fs.F_OK);
});
it('exists works', function(done) {
fsMerger.fs.exists('test-1', function (content) {
expect(content).to.be.true;
done();
});
});
it('absolute path works', function() {
let exist = fsMerger.fs.existsSync(`${__dirname}/../fixtures/test-1`);
expect(exist).to.be.true;
});
it('writeFileSync operation must throw error', function () {

@@ -274,3 +243,3 @@ let fsMerger = new FSMerge(['fixtures/test-1']);

fsMerger.fs.writeFileSync('read.md', 'test');
}).to.throw(`Operation writeFileSync is a write operation, not allowed with FSMerger.fs`)
}).to.throw(`Operation writeFileSync is not allowed with FSMerger.fs. Whitelisted operations are readFileSync,existsSync,lstatSync,statSync,readdirSync`);
});

@@ -322,3 +291,3 @@ });

fsMerger = new FSMerge(['fixtures/test-1', 'fixtures/test-2', 'fixtures/test-3']).fs;
let fsEntries = fsMerger.entries();
let fsEntries = fsMerger.entries('./');
let fileList = [];

@@ -325,0 +294,0 @@ let walkList = ['a.txt', 'b.txt', 'c.txt', 'd.txt', 'test-1/', 'test-1/b.txt', 'test-sub-1/', 'test-sub-1/sub-b.txt', 'test-sub-1/sub-c.txt', 'test-sub-1/test-sub-sub-1/', 'test-sub-1/test-sub-sub-1/sub-sub-b.txt', 'test-sub-1/test-sub-sub-1/sub-sub-c.txt', 'test-sub-2/' ,'x.txt'];

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