• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

poelstra / mhub / 116

pending completion
116

cron

travis-ci-com

poelstra
all: Mass reformat using Prettier, add to lint check.

201 of 302 branches covered (66.56%)

Branch coverage included in aggregate %.

69 of 69 new or added lines in 10 files covered. (100.0%)

540 of 652 relevant lines covered (82.82%)

97.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.12
/src/nodeclient.ts
1
/**
2
 * MHub client library for Node.JS, using einaros/ws for WebSockets.
3
 */
4

5
import * as events from "events";
1✔
6
import * as tls from "tls";
7
import * as ws from "ws";
1✔
8

9
import { BaseClient, BaseClientOptions, Connection } from "./baseclient";
1✔
10
import * as protocol from "./protocol";
11

12
const DEFAULT_PORT_WS = 13900;
1✔
13
const DEFAULT_PORT_WSS = 13901;
1✔
14

15
function noop(): void {
16
        /* no operation */
17
}
18

19
/**
20
 * Options to be passed to MClient constructor.
21
 */
22
export interface MClientOptions extends BaseClientOptions, tls.TlsOptions {
23
        /**
24
         * When true, will not automatically connect in the
25
         * constructor. Connect explicitly using `#connect()`.
26
         */
27
        noImplicitConnect?: boolean;
28
}
29

30
export const defaultClientOptions: MClientOptions = {
1✔
31
        noImplicitConnect: false,
32
};
33

34
class WebSocketConnection extends events.EventEmitter implements Connection {
35
        private _socket: ws;
36
        private _connected: boolean = false;
8✔
37

38
        constructor(url: string, options?: MClientOptions) {
39
                super();
8✔
40

41
                this._socket = new ws(url, <any>options);
8✔
42
                this._socket.on("error", (e: any) => this.emit("error", e));
8✔
43
                this._socket.on("open", () => {
8✔
44
                        this.emit("open");
8✔
45
                        this._connected = true;
8✔
46
                });
47
                this._socket.on("close", () => {
8✔
48
                        this._connected = false;
8✔
49
                        this.emit("close");
8✔
50
                });
51
                this._socket.on("message", (data: string) => {
8✔
52
                        if (!data) {
1!
53
                                // Ignore empty 'lines'
54
                                return;
×
55
                        }
56
                        const response = JSON.parse(data);
1✔
57
                        this.emit("message", response);
1✔
58
                });
59
        }
60

61
        /**
62
         * Transmit data object.
63
         * @return Promise that resolves when transmit is accepted (i.e. not necessarily
64
         * arrived at other side, can be e.g. queued).
65
         */
66
        public send(data: protocol.Command): Promise<void> {
67
                return new Promise<void>((resolve, reject) => {
1✔
68
                        this._socket.send(JSON.stringify(data), (err?: Error) => {
1✔
69
                                if (err) {
1!
70
                                        reject(err);
×
71
                                } else {
72
                                        resolve(undefined);
1✔
73
                                }
74
                        });
75
                });
76
        }
77

78
        /**
79
         * Gracefully close connection, i.e. allow pending transmissions
80
         * to be completed.
81
         * @return Promise that resolves when connection is succesfully closed.
82
         */
83
        public close(): Promise<void> {
84
                let result: Promise<void>;
85
                if (!this._connected) {
6!
86
                        result = Promise.resolve();
×
87
                } else {
88
                        result = new Promise<void>((resolve) => {
6✔
89
                                this._socket.once("close", resolve);
6✔
90
                        });
91
                }
92
                this._socket.close();
6✔
93
                return result;
6✔
94
        }
95

96
        /**
97
         * Forcefully close connection.
98
         * @return Promise that resolves when connection is succesfully closed.
99
         */
100
        public terminate(): Promise<void> {
101
                let result: Promise<void>;
102
                if (!this._connected) {
2!
103
                        result = Promise.resolve();
2✔
104
                } else {
105
                        result = new Promise<void>((resolve) => {
×
106
                                this._socket.once("close", resolve);
×
107
                        });
108
                }
109
                this._socket.terminate();
2✔
110
                return result;
2✔
111
        }
112
}
113

114
/**
115
 * MHub client using server-side WebSocket.
116
 *
117
 * Allows subscribing and publishing to MHub server nodes.
118
 *
119
 * @event open() Emitted when connection was established.
120
 * @event close() Emitted when connection was closed.
121
 * @event error(e: Error) Emitted when there was a connection, server or protocol error.
122
 * @event message(m: Message) Emitted when message was received (due to subscription).
123
 */
124
export class MClient extends BaseClient {
1✔
125
        private _url: string;
126

127
        /**
128
         * Create new connection to MServer.
129
         * @param url Websocket URL of MServer, e.g. ws://localhost:13900
130
         * @param options Optional TLS settings and other options (see
131
         *        https://nodejs.org/dist/latest-v6.x/docs/api/tls.html#tls_tls_connect_port_host_options_callback
132
         *        for the TLS settings, and `MClientOptions` for other options)
133
         */
134
        constructor(url: string, options?: MClientOptions) {
135
                // Ensure options is an object and fill in defaults
136
                options = { ...defaultClientOptions, ...options };
6✔
137

138
                // Prefix URL with "ws://" or "wss://" if needed
139
                if (url.indexOf("://") < 0) {
6!
140
                        if (options.key || options.pfx) {
×
141
                                url = "wss://" + url;
×
142
                        } else {
143
                                url = "ws://" + url;
×
144
                        }
145
                }
146
                // Append default port if necessary
147
                if (!url.match(":\\d+$")) {
6!
148
                        const useTls = url.indexOf("wss://") === 0;
×
149
                        url = url + ":" + (useTls ? DEFAULT_PORT_WSS : DEFAULT_PORT_WS);
×
150
                }
151

152
                super(() => new WebSocketConnection(url, options), options);
8✔
153

154
                this._url = url;
6✔
155

156
                if (!options.noImplicitConnect) {
6!
157
                        this.connect().catch(noop);
6✔
158
                }
159
        }
160

161
        /**
162
         * Full URL of MHub connection.
163
         */
164
        public get url(): string {
165
                return this._url;
×
166
        }
167
}
168

169
export default MClient;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc