Skip to content

Commit

Permalink
Merge pull request #237 from mStirner/dev
Browse files Browse the repository at this point in the history
V2 is comming!
  • Loading branch information
mStirner authored Nov 27, 2022
2 parents 8024519 + c499adb commit a05ddd0
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 44 deletions.
35 changes: 19 additions & 16 deletions components/endpoints/class.state.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,30 @@ module.exports = class State {
name: Joi.string().required(),
description: Joi.string().allow(null).default(null),
alias: Joi.string().required(),
value: Joi.when("type", {
is: "number",
then: Joi.number()
}).when("type", {
is: "string",
then: Joi.string()
}).when("type", {
is: "boolean",
then: Joi.boolean()
}).allow(null).default(null),
type: Joi.string().valid("number", "string", "boolean").required(),
identifier: Joi.string().allow(null).default(null),
timestamps: Joi.object({
created: Joi.number().allow(null),
updated: Joi.number().allow(null)
}).default(() => {
return {
created: Date.now(),
updated: null
};
})
}).when(".type", {
switch: [{
is: "number",
then: Joi.object({
value: Joi.number().default(null).allow(null),
min: Joi.number().default(0),
max: Joi.number().default(100)
})
}, {
is: "string",
then: Joi.object({
value: Joi.string().default(null).allow(null)
})
}, {
is: "boolean",
then: Joi.object({
value: Joi.boolean().default(null).allow(null)
})
}]
});
}

Expand Down
36 changes: 20 additions & 16 deletions components/vault/class.vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const { EventEmitter } = require("events");
const mongodb = require("mongodb");
const Joi = require("joi");

const _debounce = require("../../helper/debounce.js");

const Secret = require("./class.secret.js");

/**
Expand Down Expand Up @@ -30,31 +32,33 @@ class Vault {
let events = new EventEmitter();
this.#privates.set("events", events);

Object.assign(this, obj);
this._id = String(obj._id);
let changed = _debounce(async (secret) => {
try {

this.secrets = obj.secrets.map((data) => {
return new Secret(data, async () => {
try {
// feedback
scope.logger.debug(`Secret "${secret.key}" changed`);

// feedback
scope.logger.debug(`Secret "${data.name}" value changed`);
// update item in database
await scope.update(this._id, this);

// update item in database
await scope.update(this._id, this);
} catch (err) {

events.emit("changed", this);
scope.logger.warn(err, `Could not update secret value. (${obj._id}) ${secret.key}=${secret.value}`);

} catch (err) {
} finally {

scope.logger.warn(err, `Could not update secret value. (${obj._id}) ${data.key}=${data.value}`);
// notify for changes
events.emit("changed", this);

} finally {
}
}, Number(process.env.DATABASE_UPDATE_DEBOUNCE_TIMER));

// notify for changes
events.emit("changed", data.key, data.value);
Object.assign(this, obj);
this._id = String(obj._id);

}
this.secrets = obj.secrets.map((secret) => {
return new Secret(secret, () => {
changed(secret);
});
});

Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ process.env = Object.assign({
//DATABASE_TIMEOUT: "5", // #8
DATABASE_URL: "",
DATABASE_WATCH_CHANGES: "false",
DATABASE_UPDATE_DEBOUNCE_TIMER: "15",
HTTP_PORT: "8080",
HTTP_ADDRESS: "0.0.0.0",
HTTP_SOCKET: "",
Expand Down Expand Up @@ -84,6 +85,12 @@ if (process.env.NODE_ENV === "development") {
}


// implement #195
if (process.env.NODE_ENV === "production") {
console.log = () => { };
}


// init feedback
//process.stdout.write("\033c"); // use console.clear()?!
// https://stackoverflow.com/a/41407246/5781499
Expand Down
50 changes: 38 additions & 12 deletions system/component/class.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const _merge = require("../../helper/merge");

const COMMON = require("./class.common.js");

//const PENDING_CHANGE_EVENTS = new Set();
const PENDING_CHANGE_EVENTS = new Set();

/**
* @description
Expand Down Expand Up @@ -130,6 +130,10 @@ module.exports = class COMPONENT extends COMMON {
});

changeStream.on("change", (event) => {

// feedback
this.logger.trace("Change event triggerd", event);

// replace is used when updated via mongodb compass
// updated is used when called via api/`.update` method
if (event.operationType === "replace") {
Expand All @@ -145,6 +149,19 @@ module.exports = class COMPONENT extends COMMON {
return;
}

// when a change was initialized local, ignore changes from mongodb
if (PENDING_CHANGE_EVENTS.has(target._id)) {

// feedback
this.logger.trace("Local change detected, ignore event from change stream");

// cleanup
PENDING_CHANGE_EVENTS.delete(target._id);

return;

}

// get original property descriptor
//let descriptor = Object.getOwnPropertyDescriptors(target);
//console.log("replace event", descriptor.config)
Expand Down Expand Up @@ -186,18 +203,17 @@ module.exports = class COMPONENT extends COMMON {
}

// when a change was initialized local, ignore changes from mongodb
/*
if (PENDING_CHANGE_EVENTS.has(target._id)) {

// feedback
this.logger.verbose("Local change detected, ignore event from change stream");
this.logger.trace("Local change detected, ignore event from change stream");

// cleanup
PENDING_CHANGE_EVENTS.delete(target._id);

return;

}
*/

// event now contain dot notation partial update
// to make things simpler, just fetch the doc and merge it
Expand Down Expand Up @@ -226,6 +242,7 @@ module.exports = class COMPONENT extends COMMON {
this.logger.verbose(`$watch operation (${event.operationType}) not implemented!`);

}

});

} catch (err) {
Expand Down Expand Up @@ -274,6 +291,9 @@ module.exports = class COMPONENT extends COMMON {
// override string with ObjectId, see #175
result.value._id = new mongodb.ObjectId(result.value._id);

// add id to pending change events
PENDING_CHANGE_EVENTS.add(result.value._id);

this.collection.insertOne(result.value, (err, result) => {
if (err) {
if (err.code === 11000 && options.returnDuplicate) {
Expand All @@ -296,8 +316,8 @@ module.exports = class COMPONENT extends COMMON {
*/
});

// add id to pending change events
//PENDING_CHANGE_EVENTS.add(item._id);
// remove id when error occurs
PENDING_CHANGE_EVENTS.delete(result.value._id);

if (item) {
resolve([item]);
Expand Down Expand Up @@ -398,21 +418,24 @@ module.exports = class COMPONENT extends COMMON {
return obj._id === _id;
});

// add id to pending change events
PENDING_CHANGE_EVENTS.add(target._id);

this.collection.deleteOne({
_id: new mongodb.ObjectId(_id)
}, (err, result) => {
if (err) {

// remove id when error occurs
PENDING_CHANGE_EVENTS.delete(result.value._id);

reject(err);

} else {

//if (result.n === 1 && result.ok === 1 && target) {
if (result.acknowledged && result.deletedCount > 0) {

// add id to pending change events
//PENDING_CHANGE_EVENTS.add(target._id);

resolve([target, result, _id]);

} else {
Expand Down Expand Up @@ -472,6 +495,9 @@ module.exports = class COMPONENT extends COMMON {
// _id is immutable. remove it
delete validation.value._id;

// add id to pending change events
PENDING_CHANGE_EVENTS.add(target._id);

this.collection.findOneAndUpdate({
// casting problem, see #175
_id: new mongodb.ObjectId(_id)
Expand All @@ -485,6 +511,9 @@ module.exports = class COMPONENT extends COMMON {
}, (err) => {
if (err) {

// remove id when error occurs
PENDING_CHANGE_EVENTS.delete(target._id);

//console.log("4tpoiwrejtkwienrut", err)
reject(err);

Expand All @@ -502,9 +531,6 @@ module.exports = class COMPONENT extends COMMON {
// Umwandlung von object/string zu/von object/string
// muss in middlware erflogen!!!!!!!!!!!!!!

// add id to pending change events
//PENDING_CHANGE_EVENTS.add(target._id);

// TODO CHECK RESUTL!
// extend exisiting object in items array
//_extend(target, validation.value);
Expand Down

0 comments on commit a05ddd0

Please sign in to comment.