-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add typescript dependency, check jsdoc types #38
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,9 @@ | ||
'use strict'; | ||
|
||
const { WebSocketServer } = require('ws'); | ||
const interactionModule = require('./modules/interaction'); | ||
const sessionModule = require('./modules/session'); | ||
|
||
const broadcast = (server, message) => { | ||
const packedMessage = JSON.stringify(message); | ||
server.clients.forEach(websocket => { | ||
if (websocket.sessionId) { | ||
websocket.send(packedMessage, error => { | ||
if (error) { | ||
server.emit('error', `error sending message: ${error}`); | ||
} | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
const methodHandlers = { | ||
...interactionModule, | ||
...sessionModule, | ||
|
@@ -70,27 +58,56 @@ const onConnection = websocket => { | |
}); | ||
}; | ||
|
||
class CommandServer extends WebSocketServer { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Beautiful |
||
/** | ||
* Broadcast message to all clients. | ||
* @param message | ||
*/ | ||
broadcast(message) { | ||
const packedMessage = JSON.stringify(message); | ||
/** @type {Set<WebSocketWithData>} */ (this.clients).forEach(websocket => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This kind of makes me feel like we should be using a WeakMap to associate the session ID. Not in this patch, of course. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah that sounds like a good idea. Adding post-hoc properties like |
||
if (websocket.sessionId) { | ||
websocket.send(packedMessage, error => { | ||
if (error) { | ||
this.emit('error', `error sending message: ${error}`); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Create a server which communicates with external clients using the WebSocket | ||
* protocol described in the project README.md file. | ||
* | ||
* @param {number} port - the port on which the server should listen for new | ||
* connections | ||
* | ||
* @returns {Promise<EventEmitter>} an eventual value which is fulfilled when | ||
* the server has successfully bound to the | ||
* requested port | ||
* @returns {Promise<CommandServer>} an eventual value which is fulfilled when | ||
* the server has successfully bound to the | ||
* requested port | ||
*/ | ||
module.exports = async function createWebSocketServer(port) { | ||
const server = new WebSocketServer({ | ||
const server = new CommandServer({ | ||
clientTracking: true, | ||
path: '/session', | ||
port, | ||
}); | ||
await new Promise(resolve => server.once('listening', resolve)); | ||
|
||
server.broadcast = broadcast.bind(null, server); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding a key not defined on the type (in this case |
||
server.on('connection', onConnection); | ||
|
||
return server; | ||
}; | ||
|
||
/** @typedef {import("ws").WebSocket} WebSocket */ | ||
|
||
/** | ||
* @typedef WebSocketData | ||
* @property {string} [sessionId] | ||
*/ | ||
|
||
/** | ||
* @typedef {WebSocket & WebSocketData} WebSocketWithData | ||
*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,3 +45,5 @@ module.exports = function createVoiceServer(handle) { | |
server.on('connection', onConnection.bind(null, server)); | ||
}); | ||
}; | ||
|
||
/** @typedef {import("events").EventEmitter} EventEmitter */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But that alternative leaves a dangling import. Since EventEmitter isn't used in the code, only in comments. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for offering alternatives, but I agree with the choice you've made here. I prefer declaring types at the top of the file, though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer declaring types at the top as well. Applying that how should we manage what the comment modifies? Like in this file if it is put after the imports, it would be documenting I see two solutions for that while keeping the type in the file:
To make the PR smaller and easier to review I chose the second.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hrm. Are you asking for a change to be made here? On second review of your comment I'm not sure if you are commenting on it or asking for a change. If so, I'd be happy to do the stroke-out section I stated:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was asking for a change, although I thought diff --git a/lib/create-voice-server.js b/lib/create-voice-server.js
index c1386c4..e14f78d 100644
--- a/lib/create-voice-server.js
+++ b/lib/create-voice-server.js
@@ -2,6 +2,8 @@
const net = require('net');
+/** @typedef {import("events").EventEmitter} EventEmitter */
+
const onConnection = (server, socket) => {
let emitted = '';
socket.on('data', buffer => (emitted += buffer.toString()));
@@ -46,4 +48,3 @@ module.exports = function createVoiceServer(handle) {
});
};
-/** @typedef {import("events").EventEmitter} EventEmitter */ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could also be wrong and remembering something from an older version of typescript? I retract that stuff I said. (It's pretty nice to see I'm wrong on this. I super thought this was a thing.) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"checkJs": true, | ||
"noEmit": true | ||
}, | ||
"include": ["lib", "test"] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yargs
can return a promise. So I added anawait
here in case that happens.