Skip to content

Commit

Permalink
Merge pull request #437 from mStirner/dev
Browse files Browse the repository at this point in the history
Working on issues
  • Loading branch information
mStirner authored Feb 28, 2024
2 parents 5a09bf3 + 949e356 commit 29f6017
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 41 deletions.
12 changes: 9 additions & 3 deletions components/endpoints/class.command.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const _timeout = require("../../helper/timeout.js");
const { interfaces } = require("../../system/shared.js");

const Param = require("./class.param.js");
const Params = require("./class.params.js");

/**
* @description
Expand Down Expand Up @@ -211,6 +212,10 @@ module.exports = class Command {
return param.key === key;
});

if (!param) {
return false;
}

// auto convert "123" to 123
if (param.type === "number") {
value = Number(value);
Expand All @@ -227,8 +232,9 @@ module.exports = class Command {
}

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

Expand All @@ -248,7 +254,7 @@ module.exports = class Command {

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

}

Expand Down
14 changes: 14 additions & 0 deletions components/endpoints/class.params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = class Params extends Array {

constructor(...args) {
super(...args);
}

lean() {
return this.reduce((obj, { key, value }) => {
obj[key] = value;
return obj;
}, {});
}

};
1 change: 1 addition & 0 deletions components/vault/class.vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ module.exports = class Vault extends Item {
}),
name: Joi.string().required(),
identifier: Joi.string().required(),
description: Joi.string().allow(null).default(null),
secrets: Joi.array().items(Secret.schema()).default([])
});
}
Expand Down
1 change: 1 addition & 0 deletions helper/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function timeout(time, cb) {
if (!called) {

called = true;
// NOTE: Deconstructing/array recreating needed?!
cb(false, Date.now() - start, [...args]);

}
Expand Down
7 changes: 6 additions & 1 deletion routes/router.api.devices.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ module.exports = (app, router) => {

} else {


// TODO: This should be moved into the device component
// A websocket server should be created in the class.interface.js file
// Static method can return them e.g. Interface.servers() = returns array of wss
// Goal should be:
// - to eliminate the need of "shared.js"
// - handle in router.get only ws handshake: "wss.handleUpgrade(...)"
if (!interfaceServer.has(req.params._iid)) {

let wss = new WebSocket.Server({
Expand Down
73 changes: 36 additions & 37 deletions routes/router.api.mdns.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,54 +46,53 @@ module.exports = (app, router) => {
});


// http route handler
// TODO: Reformat to match router.api.mdns.js code style/if-else
router.get("/", (req, res, next) => {
// listen for websockt clients
// keep sending new log entrys to client
wss.once("connection", (ws) => {

// check if connection is a simple get request or ws client
if ((!req.headers["upgrade"] || !req.headers["connection"])) {
//return res.status(403).end();
next(); // let the rest-handler.js do its job
return;
}
C_MDNS.events.emit("connected", ws);

// listen for websockt clients
// keep sending new log entrys to client
// TODO: Move this outside the get handler
// see: #426
wss.once("connection", (ws) => {
ws.on("message", (msg) => {
C_MDNS.events.emit("message", decode(msg), msg);
});

C_MDNS.events.emit("connected", ws);

ws.on("message", (msg) => {
C_MDNS.events.emit("message", decode(msg), msg);
});
// QUERY LOCAL DNS
// TODO: Move this into the mdns component
/*
setInterval(() => {
console.log("Query for HTTP Server");
let msg = encode({
type: "query",
id: 1,
flags: RECURSION_DESIRED,
questions: [{
type: "A",
//name: '_http._tcp.local'
name: "*"
}]
});
// QUERY LOCAL DNS
// TODO: Move this into the mdns component
/*
setInterval(() => {
ws.send(msg);
console.log("Query for HTTP Server");
}, 30_000);
*/

let msg = encode({
type: "query",
id: 1,
flags: RECURSION_DESIRED,
questions: [{
type: "A",
//name: '_http._tcp.local'
name: "*"
}]
});
});

ws.send(msg);

}, 30_000);
*/
// http route handler
// TODO: Reformat to match router.api.mdns.js code style/if-else
router.get("/", (req, res, next) => {

});
// check if connection is a simple get request or ws client
if ((!req.headers["upgrade"] || !req.headers["connection"])) {
//return res.status(403).end();
next(); // let the rest-handler.js do its job
return;
}

// handle request as websocket
// perform websocket handshake
Expand Down

0 comments on commit 29f6017

Please sign in to comment.