add tips for handling auth and add typings for ws

This commit is contained in:
Kevin Jahns 2024-03-27 16:10:00 +01:00
parent af5c36d03a
commit 7945a5ba12
3 changed files with 14 additions and 6 deletions

View File

@ -1,8 +1,5 @@
#!/usr/bin/env node #!/usr/bin/env node
/**
* @type {any}
*/
const WebSocket = require('ws') const WebSocket = require('ws')
const http = require('http') const http = require('http')
const number = require('lib0/number') const number = require('lib0/number')
@ -20,9 +17,9 @@ const server = http.createServer((_request, response) => {
wss.on('connection', setupWSConnection) wss.on('connection', setupWSConnection)
server.on('upgrade', (request, socket, head) => { server.on('upgrade', (request, socket, head) => {
// You may check auth of request here.. Call `wss.HandleUpgrade` *after* you // You may check auth of request here..
// checked whether the client has access (e.g. by checking cookies, or url // Call `wss.HandleUpgrade` *after* you checked whether the client has access
// parameters). // (e.g. by checking cookies, or url parameters).
// See https://github.com/websockets/ws#client-authentication // See https://github.com/websockets/ws#client-authentication
wss.handleUpgrade(request, socket, head, /** @param {any} ws */ ws => { wss.handleUpgrade(request, socket, head, /** @param {any} ws */ ws => {
wss.emit('connection', ws, request) wss.emit('connection', ws, request)

10
package-lock.json generated
View File

@ -19,6 +19,7 @@
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^18.15.0", "@types/node": "^18.15.0",
"@types/ws": "^8.5.10",
"rollup": "^3.19.1", "rollup": "^3.19.1",
"standard": "^12.0.1", "standard": "^12.0.1",
"typescript": "^4.9.5", "typescript": "^4.9.5",
@ -46,6 +47,15 @@
"integrity": "sha512-z6nr0TTEOBGkzLGmbypWOGnpSpSIBorEhC4L+4HeQ2iezKCi4f77kyslRwvHeNitymGQ+oFyIWGP96l/DPSV9w==", "integrity": "sha512-z6nr0TTEOBGkzLGmbypWOGnpSpSIBorEhC4L+4HeQ2iezKCi4f77kyslRwvHeNitymGQ+oFyIWGP96l/DPSV9w==",
"dev": true "dev": true
}, },
"node_modules/@types/ws": {
"version": "8.5.10",
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.10.tgz",
"integrity": "sha512-vmQSUcfalpIq0R9q7uTo2lXs6eGIpt9wtnLdMv9LVpIjCA/+ufZRozlVoVelIYixx1ugCBKDhn89vnsEGOCx9A==",
"dev": true,
"dependencies": {
"@types/node": "*"
}
},
"node_modules/abstract-leveldown": { "node_modules/abstract-leveldown": {
"version": "6.2.3", "version": "6.2.3",
"resolved": "https://registry.npmjs.org/abstract-leveldown/-/abstract-leveldown-6.2.3.tgz", "resolved": "https://registry.npmjs.org/abstract-leveldown/-/abstract-leveldown-6.2.3.tgz",

View File

@ -65,6 +65,7 @@
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^18.15.0", "@types/node": "^18.15.0",
"@types/ws": "^8.5.10",
"rollup": "^3.19.1", "rollup": "^3.19.1",
"standard": "^12.0.1", "standard": "^12.0.1",
"typescript": "^4.9.5", "typescript": "^4.9.5",