Skip to content

Commit

Permalink
fix #529, possible fix for #528
Browse files Browse the repository at this point in the history
  • Loading branch information
mStirner committed Feb 3, 2025
1 parent de570bb commit 847a83b
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 35 deletions.
70 changes: 36 additions & 34 deletions components/endpoints/class.command.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ module.exports = class Command {
*/
trigger(params, cb) {

let { events, logger } = Command.scope;
logger.verbose(`Trigger command "${this.name}"`, this);

if (!cb && params instanceof Function) {
cb = params;
params = [];
Expand All @@ -218,57 +221,56 @@ module.exports = class Command {
let worker = this.#privates.get("handler");
let iface = interfaces.get(this.interface);

//console.log("params array:", this.params, params)
// moved up, and used as callback debounce function
// see #528, timeout helper has a internal "called" flag
let timer = _timeout(this.#privates.get("timeout"), (timedout, duration, args) => {
if (timedout) {

let valid = params.every(({ key, value }) => {
console.log("Command timedout! Execution was not successful, worker function:", worker);
cb(null, false);

let param = this.params.find((param) => {
return param.key === key;
});
} else {

if (!param) {
return false;
}
console.log("Command handler executed", duration, args);
cb(...args);

// auto convert "123" to 123
if (param.type === "number") {
value = Number(value);
}
});

return typeof (value) === param.type;
try {
params = params.map((obj) => {

});
let param = this.params.find((param) => {
return param.key === obj.key;
});

if (!iface) {
let err = new Error(`Interface "${this.interface}" not found, cant write to it.`);
err.code = "NO_INTERFACE";
return cb(err, false);
}
return Param.merge(param, obj);

if (!valid) {
let err = new Error(`Invalid parameter`);
err.code = "INVALID_PARAMETER";
// TODO: Should not be as second argument passed "false"?!
return cb(err);
}
});
} catch (err) {

let timer = _timeout(this.#privates.get("timeout"), (timedout, duration, args) => {
if (timedout) {
logger.warn(err, `Passed params to command "${this.name}" are invalid`, params);

console.log("Command timedout! Execution was not successful, worker function:", worker);
cb(null, false);
timer(err, false);
return;

} else {
}

console.log("Command handler executed", duration, args);
cb(...args);
// convert to params array with .lean method
params = new Params(...params);

}
});
if (!iface) {
let err = new Error(`Interface "${this.interface}" not found, cant write to it.`);
err.code = "NO_INTERFACE";
return timer(err, false);
}

// emit command event, see #529
events.emit("command", this, params);

// handle timeout stuff here?
// when so, timeout applys to custom functions too!
worker.call(this, this, iface, new Params(...params), timer);
worker.call(this, this, iface, params, timer);

}

Expand Down
24 changes: 24 additions & 0 deletions components/endpoints/class.param.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = class Param {

Object.defineProperty(this, "value", {
get() {
// NOTE: Autoconvert value here?
return obj.value;
},
set(val) {
Expand Down Expand Up @@ -36,6 +37,7 @@ module.exports = class Param {

static schema() {
return Joi.object({
// TODO: name: Joi.string().required();
type: Joi.string().valid("number", "string", "boolean").required(),
key: Joi.string().required()
}).when(".type", {
Expand Down Expand Up @@ -67,4 +69,26 @@ module.exports = class Param {
return Param.schema().validate(data);
}

static merge(self, obj) {

if (!self) {
throw new Error("Parameter not found, got " + self);
}

if (self.type === "number") {
obj.value = Number(obj.value);
}

Object.assign(self, obj);

let { error = null } = Param.validate(self);

if (error) {
throw error;
}

return self;

}

};
3 changes: 2 additions & 1 deletion components/endpoints/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class C_ENDPOINTS extends COMPONENT {

// inject logger, collection and schema object
super("endpoints", Endpoint.schema(), [
Endpoint
Endpoint,
Command
]);


Expand Down

0 comments on commit 847a83b

Please sign in to comment.