Socket
Socket
Sign inDemoInstall

@mayahq/maya-db

Package Overview
Dependencies
Maintainers
2
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@mayahq/maya-db - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

lib/test.d.ts

66

lib/db.js

@@ -7,5 +7,5 @@ "use strict";

exports.localDb = void 0;
var path_1 = __importDefault(require("path"));
var fileio_1 = require("./io/fileio");
var collection_1 = require("./storage/collection");
var path_1 = __importDefault(require("path"));
function localDb(_a) {

@@ -18,3 +18,3 @@ var encryptionKey = _a.encryptionKey, root = _a.root;

exports.localDb = localDb;
var dbroot = path_1.default.resolve(process.env.HOME, 'maya/testdb');
var dbroot = path_1.default.resolve(process.env.HOME, 'maya/testdb1');
var db = localDb({

@@ -24,5 +24,59 @@ encryptionKey: 'eda344e1ab8b9e122aab3350eec33e95802c7fe68aac8ad85c5c64d97e45ef1a',

});
var block = db.createNewCollection('tokens').createNewBlock('spotify');
block.set({ bruh: 'moment' }, {
overwrite: true
});
var targetJson = {
name: 'Dushyant',
education: {
college: 'BITS Pilani',
graduation: 2022,
degree: {
type: 'single',
branch: 'CS'
}
},
address: 'C-154 HKM Nagar'
};
var tree = {
tokens: [
{
safe: [
{
spotify: 'BLOCK',
},
{
google: 'ENCRYPTED_BLOCK',
},
{
testcol: []
}
]
},
{
unsafe: [
{
spotify: 'ENCRYPTED_BLOCK',
},
{
testcol: []
}
]
}
],
global: 'BLOCK'
};
// console.log(db.collection('global').getAllBlocks())
db.ensureHierarchy(tree);
// const block = db.createNewCollection('global').createNewBlock('spotify', {encrypted: false})
// const block = db.block('global/spotify')
// const block = db.collection('global').block('spotify')
// block.set(targetJson, {
// overwrite: true
// })
// const result = block.get({
// name: null,
// lastName: 'Jain',
// education: {
// college: null,
// degreeType: 'single'
// }
// }).then((result) => {
// console.log(result)
// })
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateSecretKey = exports.localDb = void 0;
var db_1 = require("./db");
Object.defineProperty(exports, "localDb", { enumerable: true, get: function () { return db_1.localDb; } });
var encrypt_1 = require("./io/encrypt");
Object.defineProperty(exports, "generateSecretKey", { enumerable: true, get: function () { return encrypt_1.generateSecretKey; } });

@@ -6,1 +6,2 @@ export declare function encrypt(text: string, key: string): {

export declare function decrypt(encryptedText: string, key: string, iv: string): string;
export declare function generateSecretKey(): string;

@@ -6,3 +6,3 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.decrypt = exports.encrypt = void 0;
exports.generateSecretKey = exports.decrypt = exports.encrypt = void 0;
var crypto_1 = __importDefault(require("crypto"));

@@ -31,1 +31,6 @@ var ALGORITHM = 'aes-256-cbc';

exports.decrypt = decrypt;
function generateSecretKey() {
var newKey = crypto_1.default.randomBytes(32).toString('hex');
return newKey;
}
exports.generateSecretKey = generateSecretKey;

@@ -1,2 +0,2 @@

import { StorageBlock, StorageCollection } from '../storage/storage';
import { StorageBlock, StorageCollection, DatabaseTree } from '../storage/storage';
import { AsyncFunction, blockCreateOpts, ioClient } from "./io";

@@ -8,3 +8,7 @@ export declare class FileIOClient implements ioClient {

});
ensureHierarchy(tree: DatabaseTree, absPath: string): void;
validateBlockPath(blockPath: string): void;
validateCollectionPath(absPath: string): void;
includesCollection(absPath: string): boolean;
includesBlock(blockPath: string): boolean;
readFromBlock(blockPath: string): Promise<any>;

@@ -11,0 +15,0 @@ writeToBlock(blockPath: string, data: any): Promise<any>;

82

lib/io/fileio.js

@@ -59,2 +59,35 @@ "use strict";

}
FileIOClient.prototype.ensureHierarchy = function (tree, absPath) {
for (var _i = 0, _a = Object.entries(tree); _i < _a.length; _i++) {
var _b = _a[_i], key = _b[0], val = _b[1];
if (Array.isArray(val)) {
var dirPath = path_1.default.join(absPath, key);
if (!this.includesCollection(dirPath)) {
fs_1.default.mkdirSync(dirPath, { recursive: true });
}
for (var _c = 0, val_1 = val; _c < val_1.length; _c++) {
var subtree = val_1[_c];
this.ensureHierarchy(subtree, dirPath);
}
}
else if (val === 'BLOCK' || val === 'ENCRYPTED_BLOCK') {
var shouldBeEncrypted = val.includes('ENCRYPTED');
var blockPath = path_1.default.join(absPath, key);
if (this.includesBlock(blockPath)) {
var block = require(blockPath + ".json");
if (block.__meta.encrypted !== shouldBeEncrypted) {
var err = new Error("Encryption constraint failed for block at " + path_1.default.join(absPath, key));
err.name = 'FAILED_TO_ENSURE_HIERARCHY';
throw err;
}
}
else {
this.createBlock(blockPath, { encrypted: shouldBeEncrypted });
}
}
else {
throw new Error("Invalid database hierarchy specification at path: " + path_1.default.join(absPath, key));
}
}
};
FileIOClient.prototype.validateBlockPath = function (blockPath) {

@@ -67,2 +100,27 @@ if (!fs_1.default.existsSync(blockPath + ".json")) {

};
FileIOClient.prototype.validateCollectionPath = function (absPath) {
if (!fs_1.default.existsSync(absPath) || !fs_1.default.lstatSync(absPath).isDirectory()) {
var err = new Error("No collection exists at path: " + absPath);
err.name = 'COLLECTION_NOT_FOUND';
throw err;
}
};
FileIOClient.prototype.includesCollection = function (absPath) {
try {
this.validateCollectionPath(absPath);
return true;
}
catch (e) {
return false;
}
};
FileIOClient.prototype.includesBlock = function (blockPath) {
try {
this.validateBlockPath(blockPath);
return true;
}
catch (e) {
return false;
}
};
FileIOClient.prototype.readFromBlock = function (blockPath) {

@@ -180,7 +238,3 @@ return __awaiter(this, void 0, void 0, function () {

var _this = this;
if (!fs_1.default.existsSync(absPath) || !fs_1.default.lstatSync(absPath).isDirectory()) {
var err = new Error("Invalid path: " + absPath + " is not a directory");
err.name = 'INVALID_PATH';
throw err;
}
this.validateCollectionPath(absPath);
var blocks = fs_1.default.readdirSync(absPath, { withFileTypes: true })

@@ -200,7 +254,3 @@ .filter(function (dirent) { return dirent.isFile() && dirent.name.endsWith('.json'); })

FileIOClient.prototype.getCollection = function (absPath) {
if (!fs_1.default.existsSync(absPath) || !fs_1.default.lstatSync(absPath).isDirectory()) {
var err = new Error("No collection exists at path: " + absPath);
err.name = 'COLLECTION_NOT_FOUND';
throw err;
}
this.validateCollectionPath(absPath);
var collection = new collection_1.Collection({ absPath: absPath, io: this });

@@ -211,7 +261,3 @@ return collection;

var _this = this;
if (!fs_1.default.existsSync(absPath) || !fs_1.default.lstatSync(absPath).isDirectory()) {
var err = new Error("Invalid path: " + absPath + " is not a directory");
err.name = 'INVALID_PATH';
throw err;
}
this.validateCollectionPath(absPath);
var collections = fs_1.default.readdirSync(absPath, { withFileTypes: true })

@@ -226,7 +272,3 @@ .filter(function (dirent) { return dirent.isDirectory(); })

FileIOClient.prototype.deleteCollection = function (absPath) {
if (!fs_1.default.existsSync(absPath) || !fs_1.default.lstatSync(absPath).isDirectory()) {
var err = new Error("No collection found at " + absPath);
err.name = 'COLLECTION_NOT_FOUND';
throw err;
}
this.validateCollectionPath(absPath);
fs_1.default.rmSync(absPath, { recursive: true, force: true });

@@ -233,0 +275,0 @@ };

@@ -1,2 +0,2 @@

import { StorageBlock, StorageCollection } from "../storage/storage";
import { DatabaseTree, StorageBlock, StorageCollection } from "../storage/storage";
export interface blockCreateOpts {

@@ -17,3 +17,6 @@ encrypted: boolean;

deleteCollection(absPath: string): void;
includesCollection(absPath: string): boolean;
includesBlock(absPath: string): boolean;
acquireLockOnBlock(blockPath: string, callback: AsyncFunction): Promise<any>;
ensureHierarchy(tree: DatabaseTree, absPath: string): void;
}

@@ -1,2 +0,2 @@

import { ioClient } from "../io/io";
import { AsyncFunction, ioClient } from "../io/io";
import { SetQueryOpts, StorageBlock } from "./storage";

@@ -13,2 +13,6 @@ export declare class Block implements StorageBlock {

update(updates: any): Promise<any>;
acquireLock(func: AsyncFunction): Promise<any>;
lockAndGet(query: any): Promise<any>;
lockAndSet(query: any, opts?: SetQueryOpts): Promise<any>;
lockAndUpdate(updates: any): Promise<any>;
}

@@ -52,4 +52,3 @@ "use strict";

Block.prototype.get = function (query) {
var _this = this;
return this.io.acquireLockOnBlock(this.absPath, function () { return __awaiter(_this, void 0, void 0, function () {
return __awaiter(this, void 0, void 0, function () {
var data, result;

@@ -65,8 +64,7 @@ return __generator(this, function (_a) {

});
}); });
});
};
Block.prototype.set = function (query, opts) {
var _this = this;
if (opts === void 0) { opts = { overwrite: false }; }
return this.io.acquireLockOnBlock(this.absPath, function () { return __awaiter(_this, void 0, void 0, function () {
return __awaiter(this, void 0, void 0, function () {
var data, result;

@@ -91,7 +89,6 @@ return __generator(this, function (_a) {

});
}); });
});
};
Block.prototype.update = function (updates) {
var _this = this;
return this.io.acquireLockOnBlock(this.absPath, function () { return __awaiter(_this, void 0, void 0, function () {
return __awaiter(this, void 0, void 0, function () {
var data, result;

@@ -110,6 +107,52 @@ return __generator(this, function (_a) {

});
});
};
Block.prototype.acquireLock = function (func) {
return this.io.acquireLockOnBlock(this.absPath, func);
};
Block.prototype.lockAndGet = function (query) {
var _this = this;
return this.io.acquireLockOnBlock(this.absPath, function () { return __awaiter(_this, void 0, void 0, function () {
var result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.get(query)];
case 1:
result = _a.sent();
return [2 /*return*/, result];
}
});
}); });
};
Block.prototype.lockAndSet = function (query, opts) {
var _this = this;
if (opts === void 0) { opts = { overwrite: false }; }
return this.io.acquireLockOnBlock(this.absPath, function () { return __awaiter(_this, void 0, void 0, function () {
var result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.set(query, opts)];
case 1:
result = _a.sent();
return [2 /*return*/, result];
}
});
}); });
};
Block.prototype.lockAndUpdate = function (updates) {
var _this = this;
return this.io.acquireLockOnBlock(this.absPath, function () { return __awaiter(_this, void 0, void 0, function () {
var result;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.update(updates)];
case 1:
result = _a.sent();
return [2 /*return*/, result];
}
});
}); });
};
return Block;
}());
exports.Block = Block;
import { blockCreateOpts, ioClient } from "../io/io";
import { StorageCollection, StorageBlock } from "./storage";
import { StorageCollection, StorageBlock, DatabaseTree } from "./storage";
export declare class Collection implements StorageCollection {

@@ -10,2 +10,3 @@ absPath: string;

});
ensureHierarchy(tree: DatabaseTree): void;
collection(relativePath: string): StorageCollection;

@@ -17,2 +18,4 @@ getAllCollections(): StorageCollection[];

createNewCollection(name: string): StorageCollection;
deleteBlock(relativePath: string): void;
deleteCollection(relativePath: string): void;
}

@@ -17,2 +17,5 @@ "use strict";

}
Collection.prototype.ensureHierarchy = function (tree) {
return this.io.ensureHierarchy(tree, this.absPath);
};
Collection.prototype.collection = function (relativePath) {

@@ -41,4 +44,12 @@ var childPath = path_1.default.join(this.absPath, relativePath);

};
Collection.prototype.deleteBlock = function (relativePath) {
var childPath = path_1.default.join(this.absPath, relativePath);
this.io.deleteBlock(childPath);
};
Collection.prototype.deleteCollection = function (relativePath) {
var childPath = path_1.default.join(this.absPath, relativePath);
this.io.deleteCollection(childPath);
};
return Collection;
}());
exports.Collection = Collection;

@@ -5,6 +5,8 @@ "use strict";

function execGetQuery(targetJson, query) {
// console.log('query', query)
// console.log('target', targetJson)
var result = {};
if (Object.keys(query).length === 0) {
return targetJson;
}
var result = {};
Object.entries(query).forEach(function (_a) {

@@ -16,3 +18,3 @@ var key = _a[0], val = _a[1];

}
if (typeof val === 'object') {
if (typeof val === 'object' && val !== null && val !== undefined) {
result[key] = execGetQuery(targetJson[key], val);

@@ -19,0 +21,0 @@ return;

@@ -1,5 +0,8 @@

import { blockCreateOpts, ioClient } from "../io/io";
import { AsyncFunction, blockCreateOpts, ioClient } from "../io/io";
export declare type SetQueryOpts = {
overwrite: boolean;
};
export declare type DatabaseTree = {
[key: string]: DatabaseTree[] | 'BLOCK' | 'ENCRYPTED_BLOCK';
};
export interface StorageBlock {

@@ -11,2 +14,6 @@ absPath: string;

update(updates: any): Promise<any>;
lockAndGet(query: any): Promise<any>;
lockAndSet(query: any, opts?: SetQueryOpts): Promise<any>;
lockAndUpdate(updates: any): Promise<any>;
acquireLock(func: AsyncFunction): Promise<any>;
}

@@ -22,2 +29,5 @@ export interface StorageCollection {

createNewCollection(name: string): StorageCollection;
deleteBlock(relativePath: string): void;
deleteCollection(relativePath: string): void;
ensureHierarchy(tree: DatabaseTree): void;
}
{
"name": "@mayahq/maya-db",
"version": "0.0.1",
"version": "0.0.2",
"description": "A small, encrypted simple document database meant for client-side use.",

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

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