2021-01-26 12:04:53 +02:00
|
|
|
const crypto = require('crypto');
|
|
|
|
|
2021-02-03 18:27:08 +02:00
|
|
|
const constants = require('./constants');
|
2021-01-26 12:04:53 +02:00
|
|
|
|
|
|
|
const generateWebsocketAcceptValue = (websocketKey) => {
|
2021-02-03 18:27:08 +02:00
|
|
|
if (typeof websocketKey !== 'string' || typeof constants.handshakeGUID !== 'string') {
|
2021-01-26 12:04:53 +02:00
|
|
|
// TODO: maybe throw error?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-03 18:27:08 +02:00
|
|
|
const concatenated = websocketKey + constants.handshakeGUID;
|
2021-02-03 02:26:48 +02:00
|
|
|
const sha1HashInBase64 = crypto.createHash('sha1').update(concatenated, 'binary').digest('base64');
|
2021-01-26 12:04:53 +02:00
|
|
|
|
2021-02-03 02:26:48 +02:00
|
|
|
return sha1HashInBase64;
|
2021-01-26 12:04:53 +02:00
|
|
|
};
|
|
|
|
|
2021-02-03 18:27:08 +02:00
|
|
|
const validateHeaders = ({ upgradeHeader, websocketVersion }) => {
|
|
|
|
if (upgradeHeader !== constants.upgradeHeaderRequirement) return false;
|
|
|
|
if (websocketVersion !== constants.websocketVersionRequirement) return false;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = { generateWebsocketAcceptValue, validateHeaders };
|