Skip to content
This repository was archived by the owner on Jul 9, 2019. It is now read-only.

Commit a33452f

Browse files
committed
Update logging
1 parent a2fa4a2 commit a33452f

File tree

6 files changed

+105
-25
lines changed

6 files changed

+105
-25
lines changed

app/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Server } from "mosca";
1+
import { Server, ServerOpts } from "mosca";
22
import * as conf from "./config";
33
import { onReady } from "./on_ready";
44
import { maybeEnableSSL } from "./security/maybe_enable_ssl";
55
import { authorizePublish } from "./security/authorize_publish";
66
import { authorizeSubscribe } from "./security/authorize_subscribe";
77

8-
let input = {
8+
let input: ServerOpts = {
99
allowNonSecure: true,
1010
port: conf.mqttPort,
1111
http: { // for teh websockets
@@ -15,11 +15,11 @@ let input = {
1515
}
1616
};
1717

18-
maybeEnableSSL(input)
18+
maybeEnableSSL(input);
1919
let server = new Server(input);
2020

21-
server.on("ready", onReady(server)); //on init it fires up setup()
22-
server.on("ready", function() {
21+
server.on("ready", onReady(server));
22+
server.on("ready", function () {
2323
console.dir(input);
2424
});
2525
server.authorizePublish = authorizePublish;

app/mosca.d.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
declare module "mosca" {
2-
export class Server extends EventEmitter {
3-
constructor(opts: any);
2+
interface ServerOpts {
3+
allowNonSecure?: boolean,
4+
port?: number,
5+
logger?: {
6+
level: string
7+
},
8+
http?: {
9+
port?: number,
10+
bundle?: boolean,
11+
static?: string
12+
}
13+
}
14+
15+
export class Server extends NodeJS.EventEmitter {
16+
constructor(opts: ServerOpts);
417
public toString: () => string;
518
public subscribe: (topic, callback, done) => any;
619
public publish: (packet, client, callback) => any;
720
public authenticate: (client, username, password, callback) => any;
8-
public on: any;
921
public authorizePublish: any;
1022
public authorizeSubscribe: any;
1123
}

app/on_ready.ts

+1-16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { authenticate } from "./security/authenticate";
22
import { authorizePublish } from "./security/authorize_publish";
33
import { authorizeSubscribe } from "./security/authorize_subscribe";
44
import { log } from "./logger";
5+
import { startLogging } from "./start_logging";
56

67
export let onReady = (server) => () => {
78
log("Server online");
@@ -14,19 +15,3 @@ function startSecurity(server) {
1415
server.authorizePublish = authorizePublish;
1516
server.authorizeSubscribe = authorizeSubscribe;
1617
}
17-
18-
function startLogging(server) {
19-
[
20-
"clientConnected",
21-
"clientDisconnecting",
22-
"clientDisconnected",
23-
"published",
24-
"subscribed",
25-
"unsubscribed",
26-
"error"
27-
].forEach(function(event) {
28-
server.on(event, function() {
29-
log("" + event + " event.");
30-
});
31-
});
32-
}

app/security/authenticate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function authenticate(client, username: string, password: string, callbac
3131
log("AUTH FAIL " + username);
3232
log(error.message);
3333
log(error);
34-
client.authError = error
34+
client.authError = error;
3535
callback(null, false);
3636
});
3737
}

app/start_logging.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Server } from "mosca";
2+
import { log } from "./logger";
3+
4+
const EVENTS = [
5+
"clientConnected",
6+
"clientDisconnecting",
7+
"clientDisconnected",
8+
"published",
9+
"subscribed",
10+
"unsubscribed",
11+
"error"
12+
];
13+
14+
export function startLogging(server: Server) {
15+
EVENTS.map(event => server.on(event, () => log(event)));
16+
}

tslint.json

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"rules": {
3+
"max-line-length": [
4+
true,
5+
100
6+
],
7+
"no-inferrable-types": true,
8+
"class-name": true,
9+
"comment-format": [
10+
true,
11+
"check-space"
12+
],
13+
"indent": [
14+
true,
15+
"spaces"
16+
],
17+
"no-switch-case-fall-through": true,
18+
"eofline": true,
19+
"no-string-literal": true,
20+
"no-eval": true,
21+
"no-duplicate-variable": true,
22+
"no-arg": true,
23+
"no-internal-module": true,
24+
"no-trailing-whitespace": true,
25+
"no-bitwise": true,
26+
"no-var-requires": true,
27+
"no-shadowed-variable": true,
28+
"no-unused-expression": true,
29+
"no-unused-variable": true,
30+
"one-line": [
31+
true,
32+
"check-catch",
33+
"check-else",
34+
"check-open-brace",
35+
"check-whitespace"
36+
],
37+
"quotemark": [
38+
true,
39+
"double",
40+
"avoid-escape"
41+
],
42+
"semicolon": [
43+
true,
44+
"always"
45+
],
46+
"typedef-whitespace": [
47+
true,
48+
{
49+
"call-signature": "nospace",
50+
"index-signature": "nospace",
51+
"parameter": "nospace",
52+
"property-declaration": "nospace",
53+
"variable-declaration": "nospace"
54+
}
55+
],
56+
"curly": true,
57+
"whitespace": [
58+
true,
59+
"check-branch",
60+
"check-decl",
61+
"check-operator",
62+
"check-separator",
63+
"check-type"
64+
],
65+
"no-any": true
66+
}
67+
}

0 commit comments

Comments
 (0)