Skip to content
This repository was archived by the owner on Mar 20, 2025. It is now read-only.

Commit 9f3b158

Browse files
authored
add typescript dependency, check jsdoc types (#38)
* add typescript dependency, check jsdoc types - Add typescript dependency - Add test-types npm script - Call npm test-types in npm test script - Add CommandServer class extends WebSocketServer - Add @types for dependencies that need them * fixup! add typescript dependency, check jsdoc types
1 parent 673946c commit 9f3b158

File tree

6 files changed

+117
-22
lines changed

6 files changed

+117
-22
lines changed

lib/cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const DEFAULT_PORT = 4382;
1919
const log = (...args) => console.error(new Date().toISOString(), ...args);
2020

2121
module.exports = async process => {
22-
const argv = yargs(hideBin(process.argv))
22+
const argv = await yargs(hideBin(process.argv))
2323
.option('port', {
2424
coerce(string) {
2525
if (!/^(0|[1-9][0-9]*)$/.test(string)) {

lib/create-command-server.js

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
'use strict';
2+
23
const { WebSocketServer } = require('ws');
34
const interactionModule = require('./modules/interaction');
45
const sessionModule = require('./modules/session');
56

6-
const broadcast = (server, message) => {
7-
const packedMessage = JSON.stringify(message);
8-
server.clients.forEach(websocket => {
9-
if (websocket.sessionId) {
10-
websocket.send(packedMessage, error => {
11-
if (error) {
12-
server.emit('error', `error sending message: ${error}`);
13-
}
14-
});
15-
}
16-
});
17-
};
7+
/** @typedef {import("ws").WebSocket} WebSocket */
8+
9+
/**
10+
* @typedef WebSocketData
11+
* @property {string} [sessionId]
12+
*/
13+
14+
/**
15+
* @typedef {WebSocket & WebSocketData} WebSocketWithData
16+
*/
1817

1918
const methodHandlers = {
2019
...interactionModule,
@@ -70,26 +69,44 @@ const onConnection = websocket => {
7069
});
7170
};
7271

72+
class CommandServer extends WebSocketServer {
73+
/**
74+
* Broadcast message to all clients.
75+
* @param message
76+
*/
77+
broadcast(message) {
78+
const packedMessage = JSON.stringify(message);
79+
/** @type {Set<WebSocketWithData>} */ (this.clients).forEach(websocket => {
80+
if (websocket.sessionId) {
81+
websocket.send(packedMessage, error => {
82+
if (error) {
83+
this.emit('error', `error sending message: ${error}`);
84+
}
85+
});
86+
}
87+
});
88+
}
89+
}
90+
7391
/**
7492
* Create a server which communicates with external clients using the WebSocket
7593
* protocol described in the project README.md file.
7694
*
7795
* @param {number} port - the port on which the server should listen for new
7896
* connections
7997
*
80-
* @returns {Promise<EventEmitter>} an eventual value which is fulfilled when
81-
* the server has successfully bound to the
82-
* requested port
98+
* @returns {Promise<CommandServer>} an eventual value which is fulfilled when
99+
* the server has successfully bound to the
100+
* requested port
83101
*/
84102
module.exports = async function createWebSocketServer(port) {
85-
const server = new WebSocketServer({
103+
const server = new CommandServer({
86104
clientTracking: true,
87105
path: '/session',
88106
port,
89107
});
90108
await new Promise(resolve => server.once('listening', resolve));
91109

92-
server.broadcast = broadcast.bind(null, server);
93110
server.on('connection', onConnection);
94111

95112
return server;

lib/create-voice-server.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
const net = require('net');
44

5+
/** @typedef {import("events").EventEmitter} EventEmitter */
6+
57
const onConnection = (server, socket) => {
68
let emitted = '';
79
socket.on('data', buffer => (emitted += buffer.toString()));

package-lock.json

Lines changed: 64 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
"postinstall": "Release\\MakeVoice.exe",
1111
"postuninstall": "Release\\MakeVoice.exe /u",
1212
"prettier": "prettier --write lib test",
13-
"test": "prettier --check lib test && npm run test-unit",
14-
"test-unit": "mocha --ui tdd test"
13+
"test": "prettier --check lib test && npm run test-types && npm run test-unit",
14+
"test-unit": "mocha --ui tdd test",
15+
"test-types": "tsc -p tsconfig.json"
1516
},
1617
"files": [
1718
"lib",
@@ -22,8 +23,13 @@
2223
"author": "",
2324
"license": "MIT",
2425
"devDependencies": {
26+
"@types/mocha": "^10.0.6",
27+
"@types/node": "^20.11.17",
28+
"@types/ws": "^8.5.10",
29+
"@types/yargs": "^17.0.32",
2530
"mocha": "^9.1.2",
26-
"prettier": "^3.2.4"
31+
"prettier": "^3.2.4",
32+
"typescript": "^5.3.3"
2733
},
2834
"dependencies": {
2935
"robotjs": "^0.6.0",

tsconfig.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"checkJs": true,
4+
"noEmit": true
5+
},
6+
"include": ["lib", "test"]
7+
}

0 commit comments

Comments
 (0)