Bump version to 1.4.1 and tighten client handling
This commit is contained in:
+16
-64
@@ -1,5 +1,6 @@
|
||||
const crypto = require('crypto');
|
||||
const { WebSocketServer, WebSocket } = require('ws');
|
||||
const { isClientNameAvailable } = require('../client-name-check');
|
||||
|
||||
function createPlayerRuntime(options) {
|
||||
const pool = options && options.pool ? options.pool : null;
|
||||
@@ -134,76 +135,25 @@ function createPlayerRuntime(options) {
|
||||
});
|
||||
}
|
||||
|
||||
async function isClientNameAvailableOnScreen(poolArg, clientName, excludeDeviceId) {
|
||||
const normalizedName = String(clientName || '').trim();
|
||||
if (!normalizedName) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const normalizedDeviceId = normalizeDeviceId(excludeDeviceId);
|
||||
const lowerName = normalizedName.toLowerCase();
|
||||
const liveDeviceIds = new Set();
|
||||
const liveClientIds = new Set();
|
||||
|
||||
function snapshotAllConnections() {
|
||||
const allConnections = [];
|
||||
for (const bucket of connectionsBySlug.values()) {
|
||||
if (!bucket || typeof bucket.values !== 'function') {
|
||||
continue;
|
||||
}
|
||||
for (const connection of bucket.values()) {
|
||||
const existingDeviceId = normalizeDeviceId(connection && connection.deviceId ? connection.deviceId : '');
|
||||
const existingClientId = normalizeDeviceId(connection && connection.clientId ? connection.clientId : '');
|
||||
if (existingDeviceId) {
|
||||
liveDeviceIds.add(existingDeviceId);
|
||||
}
|
||||
if (existingClientId) {
|
||||
liveClientIds.add(existingClientId);
|
||||
}
|
||||
const existingName = String(connection && connection.clientName ? connection.clientName : '').trim();
|
||||
if (!existingName) {
|
||||
continue;
|
||||
}
|
||||
if (existingName.toLowerCase() !== lowerName) {
|
||||
continue;
|
||||
}
|
||||
if (normalizedDeviceId && existingDeviceId && existingDeviceId === normalizedDeviceId) {
|
||||
continue;
|
||||
}
|
||||
return false;
|
||||
allConnections.push({
|
||||
clientId: connection.clientId || null,
|
||||
clientName: connection.clientName || null,
|
||||
deviceId: connection.deviceId || null
|
||||
});
|
||||
}
|
||||
}
|
||||
return allConnections;
|
||||
}
|
||||
|
||||
const activePool = poolArg || pool;
|
||||
if (!activePool || (!liveDeviceIds.size && !liveClientIds.size)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
const [deviceRows] = await activePool.query(
|
||||
`SELECT device_id
|
||||
FROM player_onboarding_devices
|
||||
WHERE client_name IS NOT NULL
|
||||
AND TRIM(client_name) <> ''
|
||||
AND LOWER(TRIM(client_name)) = LOWER(TRIM(?))`,
|
||||
[normalizedName]
|
||||
);
|
||||
|
||||
for (let i = 0; i < deviceRows.length; i += 1) {
|
||||
const deviceId = normalizeDeviceId(deviceRows[i] && deviceRows[i].device_id ? deviceRows[i].device_id : '');
|
||||
if (!deviceId) {
|
||||
continue;
|
||||
}
|
||||
if (normalizedDeviceId && deviceId === normalizedDeviceId) {
|
||||
continue;
|
||||
}
|
||||
if (liveDeviceIds.has(deviceId)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (_error) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
async function isClientNameAvailableOnScreen(poolArg, clientName, excludeDeviceId) {
|
||||
return isClientNameAvailable(poolArg || pool, clientName, excludeDeviceId, snapshotAllConnections());
|
||||
}
|
||||
|
||||
function broadcastConnectionSnapshot(slug) {
|
||||
@@ -227,7 +177,7 @@ function createPlayerRuntime(options) {
|
||||
});
|
||||
}
|
||||
|
||||
function sendCommandToConnection(slug, connectionId, commandOrPayload) {
|
||||
async function sendCommandToConnection(slug, connectionId, commandOrPayload) {
|
||||
const bucket = connectionsBySlug.get(String(slug || '').trim());
|
||||
if (!bucket || !bucket.size) {
|
||||
return 0;
|
||||
@@ -249,7 +199,7 @@ function createPlayerRuntime(options) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
function broadcastCommand(slug, commandOrPayload) {
|
||||
async function broadcastCommand(slug, commandOrPayload) {
|
||||
const bucket = connectionsBySlug.get(String(slug || '').trim());
|
||||
if (!bucket || !bucket.size) {
|
||||
return 0;
|
||||
@@ -269,6 +219,7 @@ function createPlayerRuntime(options) {
|
||||
connection.socket.send(JSON.stringify(payload));
|
||||
sent += 1;
|
||||
});
|
||||
|
||||
return sent;
|
||||
}
|
||||
|
||||
@@ -405,6 +356,7 @@ function createPlayerRuntime(options) {
|
||||
return {
|
||||
installWebsocket: installWebsocket,
|
||||
snapshotConnections: snapshotConnections,
|
||||
snapshotAllConnections: snapshotAllConnections,
|
||||
isClientNameAvailableOnScreen: isClientNameAvailableOnScreen,
|
||||
sendCommandToConnection: sendCommandToConnection,
|
||||
broadcastCommand: broadcastCommand
|
||||
|
||||
Reference in New Issue
Block a user