Skip to content

Commit 83339e3

Browse files
committed
Refactor some code for better quality
1 parent b85fd72 commit 83339e3

9 files changed

Lines changed: 56 additions & 96 deletions

File tree

OpenFlow/src/Auth.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,9 @@ const Semaphore = (n) => ({
107107
this.n++;
108108
},
109109
async wait() {
110-
if (this.n <= 0) return await new Promise((res, req) => {
110+
if (this.n <= 0) return new Promise((res, req) => {
111111
setImmediate(async () => res(await this.wait()))
112112
});
113-
return;
114113
},
115114
});
116115
const semaphore = Semaphore(1);

OpenFlow/src/Config.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ export class Config {
8484
Config.amqp_force_sender_has_read = Config.parseBoolean(Config.getEnv("amqp_force_sender_has_read", "true"));
8585
Config.amqp_enabled_exchange = Config.parseBoolean(Config.getEnv("amqp_enabled_exchange", "false"));
8686
Config.amqp_url = Config.getEnv("amqp_url", "amqp://localhost"); // used to register queues and by personal nodered
87+
Config.amqp_username = Config.getEnv("amqp_username", "guest"); // used to talk wth rabbitmq api, used if not present in amqp_url
88+
Config.amqp_password = Config.getEnv("amqp_password", "guest"); // used to talk wth rabbitmq api, used if not present in amqp_url
8789
Config.amqp_check_for_consumer = Config.parseBoolean(Config.getEnv("amqp_check_for_consumer", "true"));
8890
Config.amqp_check_for_consumer_count = Config.parseBoolean(Config.getEnv("amqp_check_for_consumer_count", "false"));
8991
Config.amqp_default_expiration = parseInt(Config.getEnv("amqp_default_expiration", "10000")); // 10 seconds
@@ -107,7 +109,6 @@ export class Config {
107109
Config.downloadtoken_expires_in = Config.getEnv("downloadtoken_expires_in", "15m");
108110
Config.personalnoderedtoken_expires_in = Config.getEnv("personalnoderedtoken_expires_in", "365d");
109111

110-
// Config.nodered_image = Config.getEnv("nodered_image", "openiap/nodered");
111112
Config.nodered_images = JSON.parse(Config.getEnv("nodered_images", "[{\"name\":\"Latest Plain Nodered\", \"image\":\"openiap/nodered\"}]"));
112113
Config.saml_federation_metadata = Config.getEnv("saml_federation_metadata", "");
113114
Config.api_ws_url = Config.getEnv("api_ws_url", "");
@@ -214,6 +215,9 @@ export class Config {
214215
public static amqp_force_sender_has_read: boolean = Config.parseBoolean(Config.getEnv("amqp_force_sender_has_read", "true"));
215216
public static amqp_enabled_exchange: boolean = Config.parseBoolean(Config.getEnv("amqp_enabled_exchange", "false"));
216217
public static amqp_url: string = Config.getEnv("amqp_url", "amqp://localhost"); // used to register queues and by personal nodered
218+
public static amqp_username: string = Config.getEnv("amqp_username", "guest"); // used to talk wth rabbitmq api
219+
public static amqp_password: string = Config.getEnv("amqp_password", "guest"); // used to talk wth rabbitmq api
220+
217221
public static amqp_check_for_consumer: boolean = Config.parseBoolean(Config.getEnv("amqp_check_for_consumer", "true"));
218222
public static amqp_check_for_consumer_count: boolean = Config.parseBoolean(Config.getEnv("amqp_check_for_consumer_count", "false"));
219223
public static amqp_default_expiration: number = parseInt(Config.getEnv("amqp_default_expiration", (60 * 1000).toString())); // 1 min
@@ -239,7 +243,7 @@ export class Config {
239243
public static personalnoderedtoken_expires_in: string = Config.getEnv("personalnoderedtoken_expires_in", "365d");
240244

241245
// public static nodered_image: string = Config.getEnv("nodered_image", "openiap/nodered");
242-
public static nodered_images: nodered_image[] = JSON.parse(Config.getEnv("nodered_images", "[{\"name\":\"Latest Plain Nodered\", \"image\":\"openiap/nodered\"}]"));
246+
public static nodered_images: NoderedImage[] = JSON.parse(Config.getEnv("nodered_images", "[{\"name\":\"Latest Plain Nodered\", \"image\":\"openiap/nodered\"}]"));
243247
public static saml_federation_metadata: string = Config.getEnv("saml_federation_metadata", "");
244248
public static api_ws_url: string = Config.getEnv("api_ws_url", "");
245249
public static nodered_ws_url: string = Config.getEnv("nodered_ws_url", "");
@@ -300,7 +304,7 @@ export class Config {
300304
}
301305
public static async parse_federation_metadata(url: string): Promise<any> {
302306
// if anything throws, we retry
303-
const metadata: any = await retry(async bail => {
307+
return retry(async bail => {
304308
const reader: any = await fetch({ url });
305309
if (NoderedUtil.IsNullUndefinded(reader)) { bail(new Error("Failed getting result")); return; }
306310
const config: any = toPassportConfig(reader);
@@ -315,7 +319,6 @@ export class Config {
315319
Logger.instanse.warn("retry " + count + " error " + error.message + " getting " + url);
316320
}
317321
});
318-
return metadata;
319322
}
320323
public static parseArray(s: string): string[] {
321324
let arr = s.split(",");
@@ -342,7 +345,7 @@ export class Config {
342345
}
343346

344347
}
345-
export class nodered_image {
348+
export class NoderedImage {
346349
public name: string;
347350
public image: string;
348351
}

OpenFlow/src/Crypt.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,18 @@ export class Crypt {
100100
const key = Crypt.encryption_key;
101101
if (NoderedUtil.IsNullEmpty(Config.aes_secret)) throw new Exception("Config missing aes_secret");
102102
if (NoderedUtil.IsNullEmpty(key)) throw new Exception("Config missing aes_secret");
103-
const token: string = jsonwebtoken.sign({ data: user }, key,
103+
return jsonwebtoken.sign({ data: user }, key,
104104
{ expiresIn: expiresIn }); // 60 (seconds), "2 days", "10h", "7d"
105-
return token;
106105
}
107106
static verityToken(token: string): TokenUser {
108107
if (NoderedUtil.IsNullEmpty(token)) {
109108
throw new Error('jwt must be provided');
110109
}
111110
const o: any = jsonwebtoken.verify(token, Crypt.encryption_key);
112-
o.data = TokenUser.assign(o.data);
113-
return o.data;
111+
return TokenUser.assign(o.data);
112+
114113
}
115114
static decryptToken(token: string): any {
116-
const o: any = jsonwebtoken.verify(token, Crypt.encryption_key);
117-
return o;
115+
return jsonwebtoken.verify(token, Crypt.encryption_key);
118116
}
119117
}

OpenFlow/src/DBHelper.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Span } from "@opentelemetry/api";
55
import { Logger } from "./Logger";
66

77
export class DBHelper {
8-
public static async FindByUsername(username: string, jwt: string = null, parent: Span): Promise<User> {
8+
public static async FindByUsername(username: string, jwt: string, parent: Span): Promise<User> {
99
const span: Span = Logger.otel.startSubSpan("dbhelper.FindByUsername", parent);
1010
try {
1111
const byuser = { username: new RegExp(["^", username, "$"].join(""), "i") };
@@ -24,7 +24,7 @@ export class DBHelper {
2424
Logger.otel.endSpan(span);
2525
}
2626
}
27-
public static async FindById(_id: string, jwt: string = null, parent: Span): Promise<User> {
27+
public static async FindById(_id: string, jwt: string, parent: Span): Promise<User> {
2828
const span: Span = Logger.otel.startSubSpan("dbhelper.FindById", parent);
2929
try {
3030
if (jwt === null || jwt == undefined || jwt == "") { jwt = Crypt.rootToken(); }
@@ -84,14 +84,12 @@ export class DBHelper {
8484
const query: any = { "members": { "$elemMatch": { _id: _id } } };
8585
const ids: string[] = [];
8686
const _roles: Role[] = await Config.db.query<Role>(query, null, Config.expected_max_roles, 0, null, "users", Crypt.rootToken(), undefined, undefined, span);
87-
for (let i = 0; i < _roles.length; i++) {
88-
const role = _roles[i];
87+
for (let role of _roles) {
8988
if (ids.indexOf(role._id) == -1) {
9089
ids.push(role._id);
9190
result.push(role);
9291
const _subroles: Role[] = await this.GetRoles(role._id, ident + 1, span);
93-
for (let y = 0; y < _subroles.length; y++) {
94-
const subrole = _subroles[y];
92+
for (let subrole of _subroles) {
9593
if (ids.indexOf(subrole._id) == -1) {
9694
ids.push(subrole._id);
9795
result.push(subrole);
@@ -168,24 +166,17 @@ export class DBHelper {
168166
public static async FindRoleByName(name: string, parent: Span): Promise<Role> {
169167
const items: Role[] = await Config.db.query<Role>({ name: name }, null, 1, 0, null, "users", Crypt.rootToken(), undefined, undefined, parent);
170168
if (items === null || items === undefined || items.length === 0) { return null; }
171-
const result: Role = Role.assign(items[0]);
172-
return result;
169+
return Role.assign(items[0]);
173170
}
174171
public static async FindRoleByNameOrId(name: string, id: string, parent: Span): Promise<Role> {
175172
const jwt = Crypt.rootToken();
176173
const items: Role[] = await Config.db.query<Role>({ $or: [{ name: name }, { _id: id }] }, null, 1, 0, null, "users", jwt, undefined, undefined, parent);
177174
if (items === null || items === undefined || items.length === 0) { return null; }
178-
const result: Role = Role.assign(items[0]);
179-
return result;
175+
return Role.assign(items[0]);
180176
}
181177
public static async Save(item: User | Role, jwt: string, parent: Span): Promise<void> {
182178
await Config.db._UpdateOne(null, item, "users", 0, false, jwt, parent);
183179
}
184-
185-
186-
187-
188-
189180
public static async EnsureRole(jwt: string, name: string, id: string, parent: Span): Promise<Role> {
190181
const span: Span = Logger.otel.startSubSpan("dbhelper.EnsureRole", parent);
191182
try {

0 commit comments

Comments
 (0)