mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-11-19 11:38:13 +00:00
Add randomized client identifier to reduce chance of grabbing the incorrect subscription ID
This commit is contained in:
@@ -16,6 +16,7 @@ import request from '../../utils/request-middleware.js';
|
|||||||
|
|
||||||
import { DEFAULT_BREW_LOAD } from '../../../../server/brewDefaults.js';
|
import { DEFAULT_BREW_LOAD } from '../../../../server/brewDefaults.js';
|
||||||
import { printCurrentBrew, fetchThemeBundle } from '@shared/helpers.js';
|
import { printCurrentBrew, fetchThemeBundle } from '@shared/helpers.js';
|
||||||
|
import { nanoid } from 'nanoid';
|
||||||
|
|
||||||
const SharePage = (props)=>{
|
const SharePage = (props)=>{
|
||||||
const { disableMeta = false } = props;
|
const { disableMeta = false } = props;
|
||||||
@@ -53,14 +54,19 @@ const SharePage = (props)=>{
|
|||||||
document.addEventListener('keydown', handleControlKeys);
|
document.addEventListener('keydown', handleControlKeys);
|
||||||
fetchThemeBundle(undefined, setThemeBundle, currentBrew.renderer, currentBrew.theme);
|
fetchThemeBundle(undefined, setThemeBundle, currentBrew.renderer, currentBrew.theme);
|
||||||
|
|
||||||
|
// generate unique fingerprint
|
||||||
|
const clientFingerprint = nanoid(24);
|
||||||
|
|
||||||
// listen for changes in the brew version
|
// listen for changes in the brew version
|
||||||
const eventSource = new EventSource(`/api/stream/${props.brew.shareId}`);
|
const eventSource = new EventSource(`/api/stream/${props.brew.shareId}?client=${clientFingerprint}`);
|
||||||
|
|
||||||
let subId = '';
|
let subId = '';
|
||||||
eventSource.addEventListener('subscribe', (evt)=>{
|
eventSource.addEventListener('subscribe', (evt)=>{
|
||||||
const messageData = JSON.parse(evt.data);
|
const messageData = JSON.parse(evt.data);
|
||||||
|
|
||||||
subId = messageData.id;
|
if(messageData.client == clientFingerprint){
|
||||||
|
subId = messageData.id;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
eventSource.addEventListener('brewUpdated', (evt)=>{
|
eventSource.addEventListener('brewUpdated', (evt)=>{
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ const Stream = new EventEmitter;
|
|||||||
let subscribers = [];
|
let subscribers = [];
|
||||||
|
|
||||||
// Listener functions
|
// Listener functions
|
||||||
const subscribe = function(shareId, res){
|
const subscribe = function(shareId, res, clientFingerprint){
|
||||||
const id = nanoid(24); // NanoID is assumed globally unique;
|
const id = nanoid(24); // NanoID is assumed globally unique;
|
||||||
const subscriber = {
|
const subscriber = {
|
||||||
id,
|
id,
|
||||||
@@ -32,7 +32,7 @@ const subscribe = function(shareId, res){
|
|||||||
|
|
||||||
subscribers.push(subscriber);
|
subscribers.push(subscriber);
|
||||||
|
|
||||||
Stream.emit('sendUpdate', 'subscribe', { id: subscriber.id, shareId: subscriber.shareId, time: subscriber.time });
|
Stream.emit('sendUpdate', 'subscribe', { id: subscriber.id, shareId: subscriber.shareId, client: clientFingerprint, time: subscriber.time });
|
||||||
|
|
||||||
return subscriber.id;
|
return subscriber.id;
|
||||||
};
|
};
|
||||||
@@ -41,6 +41,7 @@ const unsubscribe = function(id){
|
|||||||
subscribers
|
subscribers
|
||||||
.filter((sub)=>{return sub.id == id;})
|
.filter((sub)=>{return sub.id == id;})
|
||||||
.forEach((sub)=>{
|
.forEach((sub)=>{
|
||||||
|
clearTimeout(sub.unsubTimer);
|
||||||
Stream.emit('sendUpdate', 'unsubscribe', { id: sub.id, shareId: sub.shareId, time: new Date });
|
Stream.emit('sendUpdate', 'unsubscribe', { id: sub.id, shareId: sub.shareId, time: new Date });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ app.get('/api/stream/:id', (req, res)=>{
|
|||||||
'Content-Encoding' : 'none'
|
'Content-Encoding' : 'none'
|
||||||
});
|
});
|
||||||
;
|
;
|
||||||
Stream.subscribe(req.params.id, res);
|
Stream.subscribe(req.params.id, res, req.query?.client);
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/api/stream/unsubscribe/:id', (req, res)=>{
|
app.get('/api/stream/unsubscribe/:id', (req, res)=>{
|
||||||
|
|||||||
Reference in New Issue
Block a user