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

@itentialopensource/adapter-utils

Package Overview
Dependencies
Maintainers
3
Versions
219
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@itentialopensource/adapter-utils - npm Package Compare versions

Comparing version 4.21.1 to 4.22.0

10

CHANGELOG.md
## 4.22.0 [10-09-2019]
* Resolve PH-43704 "Minor/"
Closes PH-43704
See merge request itentialopensource/adapter-utils!129
---
## 4.21.1 [10-09-2019]

@@ -3,0 +13,0 @@

304

lib/dbUtil.js

@@ -22,3 +22,3 @@ /* Required libraries. */

function getFromJson(fileName, ent, act) {
function getFromJson(fileName, ent, act, filter) {
if (!fs.existsSync(adapterDir)) {

@@ -48,12 +48,30 @@ log.warn('Could not find adapter base directory');

// }
if (fs.existsSync(`${adapterDir}/storage/${fileName}`)) {
if (fs.existsSync(`${adapterDir}/storage/${fileName}.json`)) {
// have to read, append & save
const content = JSON.parse(fs.readFileSync(fileName, 'utf-8'));
const content = JSON.parse(fs.readFileSync(`${adapterDir}/storage/${fileName}.json`, 'utf-8'));
const toReturn = [];
content.table.forEach((item) => {
if (ent && act && ent === item.entity && act === item.action) {
toReturn.push(item);
}
if (ent && act) {
content.table.forEach((item) => {
if (ent === item.entity && act === item.action) {
toReturn.push(item);
}
});
} else {
const key = Object.keys(filter);
content.table.forEach((item) => {
let push = true;
key.forEach((fil) => {
if (filter[fil] !== item[fil]) push = false;
});
if (push) toReturn.push(item);
});
}
const filtered = content.table.filter((el) => {
Object.keys(filter).forEach((obj) => {
if (el[obj] !== filter[obj]) {
return false;
}
});
return true;
});
return toReturn;

@@ -64,2 +82,46 @@ }

function countJSON(fileName, ent, act, filter) {
if (!fs.existsSync(adapterDir)) {
log.warn('Could not find adapter base directory');
return null;
}
if (!fs.existsSync(`${adapterDir}/storage`)) {
log.warn('Could not find adapter storage directory');
return null;
}
if (!fileName) {
log.warn('Must provide a file name');
return null;
}
if (fs.existsSync(`${adapterDir}/storage/${fileName}.json`)) {
const data = getFromJson(fileName, ent, act, filter);
if (data) {
return data.length;
}
return -1;
}
}
function deleteJSON(fileName) {
if (!fs.existsSync(adapterDir)) {
log.warn('Could not find adapter base directory');
return null;
}
if (!fs.existsSync(`${adapterDir}/storage`)) {
log.warn('Could not find adapter storage directory');
return null;
}
if (!fileName) {
log.warn('Must provide a file name');
return null;
}
if (fs.existsSync(`${adapterDir}/storage/${fileName}.json`)) {
fs.remove(`${adapterDir}/storage/${fileName}.json`).catch((some) => {
log.info(some);
fs.rmdirSync(`${adapterDir}/storage/${fileName}.json`);
});
}
return fileName;
}
function removeFromJSON(fileName, ent, act, filter, multiple) {

@@ -78,38 +140,48 @@ if (!fs.existsSync(adapterDir)) {

}
if (fs.existsSync(`${adapterDir}/storage/${fileName}`)) {
const content = JSON.parse(fs.readFileSync(fileName, 'utf-8'));
if (fs.existsSync(`${adapterDir}/storage/${fileName}.json`)) {
const content = JSON.parse(fs.readFileSync(`${adapterDir}/storage/${fileName}.json`, 'utf-8'));
const toReturn = [];
content.table.forEach((item) => {
if (ent && act && ent !== item.entity && act !== item.action) {
toReturn.push(item);
}
});
const removed = content.table.filter((el) => {
Object.keys(filter).forEach((obj) => {
if (el[obj] !== filter[obj]) {
if (ent && act) {
content.table = content.table.filter((item) => {
if (ent === item.entity && act === item.action) {
toReturn.push(item);
return false;
}
return true;
});
return true;
});
content.table = content.table.filter((el) => {
Object.keys(filter).forEach((obj) => {
if (el[obj] !== filter[obj]) {
return true;
} else {
let ctr = 0;
const removed = content.table.filter((el) => {
Object.keys(filter).forEach((obj) => {
if (el[obj] !== filter[obj]) {
return false;
}
});
ctr += 1;
if (!multiple && ctr > 1) {
return false;
}
return true;
});
return false;
});
if (!multiple) {
removed.forEach((i, elem) => {
if (i !== 0) {
content.table.push(elem);
let ctr1 = 0;
content.table = content.table.filter((el, i) => {
Object.keys(filter).forEach((obj) => {
if (el[obj] !== filter[obj]) {
return true;
}
});
ctr1 += 1;
if (!multiple && ctr1 > 1) {
return true;
}
return false;
});
fs.writeFileSync(`${adapterDir}/storage/${fileName}`, JSON.stringify(content, null, 2));
return removed[0];
fs.writeFileSync(`${adapterDir}/storage/${fileName}.json`, JSON.stringify(content, null, 2));
return removed;
}
fs.writeFileSync(`${adapterDir}/storage/${fileName}`, JSON.stringify(content, null, 2));
return removed;
fs.writeFileSync(`${adapterDir}/storage/${fileName}.json`, JSON.stringify(content, null, 2));
return toReturn;
}
log.error(`Collection ${fileName} does not exist`);
return null;

@@ -144,5 +216,5 @@ }

// }
if (fs.existsSync(`${adapterDir}/storage/${fileName}`)) {
if (fs.existsSync(`${adapterDir}/storage/${fileName}.json`)) {
// have to read, append & save
const content = JSON.parse(fs.readFileSync(`${adapterDir}/storage/${fileName}`, 'utf-8'));
const content = JSON.parse(fs.readFileSync(`${adapterDir}/storage/${fileName}.json`, 'utf-8'));
let exists = false;

@@ -181,3 +253,3 @@ content.table.forEach((item) => {

}
fs.writeFileSync(`${adapterDir}/storage/${fileName}`, JSON.stringify(content, null, 2));
fs.writeFileSync(`${adapterDir}/storage/${fileName}.json`, JSON.stringify(content, null, 2));
return data;

@@ -195,3 +267,3 @@ }

obj.table.push(toPush);
fs.writeFileSync(`${adapterDir}/storage/${fileName}`, JSON.stringify(obj, null, 2));
fs.writeFileSync(`${adapterDir}/storage/${fileName}.json`, JSON.stringify(obj, null, 2));
return data;

@@ -270,11 +342,13 @@ }

log.error('database connection failed, making new file');
if (!fs.existsSync(`${adapterDir}/storage/${collectionName}`)) {
if (!fs.existsSync(`${adapterDir}/storage`)) {
if (!fs.existsSync(`${adapterDir}`)) {
fs.mkdirSync(`${adapterDir}`);
}
fs.mkdirSync(`${adapterDir}/storage`);
if (fs.existsSync(`${adapterDir}/storage/${collectionName}`)) {
log.info('this collection already exists');
return callback(null, 'Duplication collection attempt');
}
if (!fs.existsSync(`${adapterDir}/storage`)) {
if (!fs.existsSync(`${adapterDir}`)) {
fs.mkdirSync(`${adapterDir}`);
}
fs.mkdirSync(`${adapterDir}/storage/${collectionName}`);
fs.mkdirSync(`${adapterDir}/storage`);
}
fs.mkdirSync(`${adapterDir}/storage/${collectionName}`);
return callback(null, collectionName);

@@ -285,6 +359,66 @@ }

if (err) {
console.log(`${origin}: ${err}`);
if (err.codeName === 'NamespaceExists') {
log.info('Duplicate collection attempt; new one was not created');
return callback(null, 'Duplication collection attempt');
}
log.error(`${origin}: ${err}`);
return callback(err, null);
}
log.info(`collection ${collectionName} created`);
return db.db(this.database).listCollections().toArray((err1, result) => {
if (err1) {
log.error(`${origin}: ${err1}`);
return callback(err1, null);
}
db.close();
result.forEach((thing) => {
if (thing.name === collectionName);
return callback(null, 'Duplication collection attempt');
});
log.info(`collection ${collectionName} created`);
return callback(null, collectionName);
});
});
});
} catch (ex) {
log.warn(`${origin}: Caught Exception - ${ex}`);
return callback(`Caught Exception - ${ex}`, null);
}
}
removeCollection(collectionName, callback) {
const origin = `${this.myid}-dbUtil-removeCollection`;
log.trace(origin);
try {
// verify the required data has been provided
if (!collectionName) {
log.warn(`${origin}: Missing Collection Name`);
return callback('Missing Collection Name', null);
}
return MongoClient.connect(this.uri, (err, db) => {
if (err) {
log.info('looking to remove a JSON');
const deld = deleteJSON(collectionName);
if (deld) {
return callback(null, deld);
}
return callback('unsuccessful JSON deletion', null);
}
db.db(this.database).listCollections().toArray((error, result) => {
if (error) {
log.error(`${origin}: ${error}`);
return callback(error, null);
}
result.forEach((elem) => {
if (elem.name === collectionName) {
return db.db(this.database).collection(collectionName).drop({}, (err1, res) => {
if (err1) {
log.error(`${origin}: ${err1}`);
return callback(err1, null);
}
db.close();
return callback(null, collectionName);
});
}
});
db.close();

@@ -378,5 +512,5 @@ return callback(null, collectionName);

log.error('No database connection, not making an index');
return callback('No database, no index creation', null);
return callback(null, 'No database, no index creation');
}
return db.db(this.database).collection(collectionName).createIndex(fieldOrSpec, options, (err, res) => {
return db.db(this.database).collection(collectionName).createIndex(fieldOrSpec, options || {}, (err, res) => {
db.close();

@@ -409,6 +543,10 @@ if (error) {

// No Database connection available
log.error('No database connection, not counting documents');
return callback('No database connection', null);
// log.error('No database connection, not counting documents');
const data = countJSON(collectionName, null, null, query);
if (!data || data === -1) {
log.error('Could not count from JSON');
}
return callback(null, data);
}
return db.db(this.database).collection(collectionName).countDocuments(query, options, (err, res) => {
return db.db(this.database).collection(collectionName).count(query, options, (err, res) => {
db.close();

@@ -448,2 +586,6 @@ if (err) {

log.error('No database connection, deleting from JSON.');
if (!fs.existsSync(`${adapterDir}/storage/${collectionName}.json`)) {
log.error(`Collection ${collectionName} does not exist`);
return callback(null, 'Nonexistent collection');
}
const deld = removeFromJSON(collectionName, null, null, filter, multiple);

@@ -501,3 +643,11 @@ if (!deld) {

log.error('No database connection, not replacing');
return callback('No database connection', null);
const rem = removeFromJSON(collectionName, null, null, filter, false);
if (rem) {
const sav = saveAsJson(collectionName, doc, null, null);
if (sav) {
return callback(null, sav);
}
return callback(`${origin}: Could not save doc into JSON`, null);
}
return callback(`${origin}: Could not delete from JSON`, null);
}

@@ -558,12 +708,17 @@ return db.db(this.database).collection(collectionName).replaceOne(filter, doc, options, (err, res) => {

// get the collection so we can run the remove on the collection
const start = options.start || 0;
const filter = options.filter || {};
const sort = options.sort || {};
let filter = {};
let start = 0;
let sort = {};
let limit = 10;
if (options) {
filter = options.filter || {};
start = options.start || 0;
sort = options.sort || {};
if (Object.hasOwnProperty.call(options, 'limit')) {
({ limit } = options);
}
}
// If limit is not specified, default to 10.
// Note: limit may be 0, which is equivalent to setting no limit.
if (Object.hasOwnProperty.call(options, 'limit')) {
({ limit } = options);
}

@@ -593,3 +748,3 @@ // Replace filter with regex to allow for substring lookup

// Find it from file in the adapter
let toReturn = getFromJson(collectionName, entity, action);
let toReturn = getFromJson(collectionName, entity, action, filter);
if (toReturn && toReturn.length > limit) {

@@ -606,3 +761,4 @@ let curEnd = start + limit;

// Find the data in the database
const collection = db.db(this.database).collection(entity);
const col = entity || 'metrics';
const collection = db.db(this.database).collection(col);
return collection.find(filter).sort(sort).skip(start).limit(limit)

@@ -654,8 +810,20 @@ .toArray()

const keysArray = Object.keys(data);
const ent = data[keysArray[1]].entity;
const act = data[keysArray[1]].action;
if ((ent && !act) || (!ent && act)) {
log.warn(`${origin}: provided with one of entity/action, require both or neither`);
return callback('Inconsistent entity/action set', null);
let ent = null;
let act = null;
if (keysArray.length >= 2) {
ent = data[keysArray[1]].entity;
act = data[keysArray[1]].action || undefined;
}
if (!ent) {
log.warn(`${origin}: Missing Entity`);
return callback('Missing Entity', null);
}
if (!act) {
log.warn(`${origin}: Missing Action`);
return callback('Missing Action', null);
}
// if ((ent && !act) || (!ent && act)) {
// log.warn(`${origin}: provided with one of entity/action, require both or neither`);
// return callback('Inconsistent entity/action set', null);
// } findAndModify is not used in throttle.js so action & entity still apply.
// get the collection so we can run the remove on the collection

@@ -703,3 +871,3 @@ return MongoClient.connect(this.uri || '', (err, db) => {

let url = '';
let url = this.uri;
let hostIndex = 0;

@@ -714,4 +882,2 @@ const db = 'dbUtil';

}
} else {
url = 'mongodb://localhost:27017';
}

@@ -718,0 +884,0 @@

{
"name": "@itentialopensource/adapter-utils",
"version": "4.21.1",
"version": "4.22.0",
"description": "Itential Adapter Utility Libraries",

@@ -9,3 +9,3 @@ "scripts": {

"lint:errors": "eslint --quiet . --ext .json --ext .js",
"test:unit": "mocha test/unit/lib/requestHandlerTest.js --LOG=error && mocha test/unit/lib/restHandlerTest.js --LOG=error && mocha test/unit/lib/propertyUtilTest.js --LOG=error && mocha test/unit/lib/translatorUtilTest.js --LOG=error",
"test:unit": "mocha test/unit/lib/requestHandlerTest.js --LOG=error && mocha test/unit/lib/restHandlerTest.js --LOG=error && mocha test/unit/lib/propertyUtilTest.js --LOG=error && mocha test/unit/lib/translatorUtilTest.js --LOG=error && mocha test/unit/lib/dbUtilTest.js --LOG=error",
"test:integration": "mocha test/integration/lib/requestHandlerTest.js --LOG=error && mocha test/integration/lib/restHandlerTest.js --LOG=error",

@@ -12,0 +12,0 @@ "test:cover": "nyc --reporter html --reporter text mocha --recursive --reporter dot test/*",

Sorry, the diff of this file is too big to display

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