Skip to content

Commit

Permalink
Rename User to YRedisUser.
Browse files Browse the repository at this point in the history
  • Loading branch information
CeEv committed Dec 9, 2024
1 parent 8b0bf5c commit cbdd9ac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
42 changes: 21 additions & 21 deletions src/infra/y-redis/ws.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { YRedisClient } from './y-redis.client.js';
import { computeRedisRoomStreamName } from './helper.js';
import * as protocol from './protocol.js';
import { Subscriber } from './subscriber.service.js';
import { closeCallback, messageCallback, openCallback, upgradeCallback, User } from './ws.service.js';
import { closeCallback, messageCallback, openCallback, upgradeCallback, YRedisUser } from './ws.service.js';

describe('ws service', () => {
beforeEach(() => {
Expand Down Expand Up @@ -120,7 +120,7 @@ describe('ws service', () => {

describe('openCallback', () => {
const buildParams = () => {
const ws = createMock<uws.WebSocket<User>>();
const ws = createMock<uws.WebSocket<YRedisUser>>();
const subscriber = createMock<Subscriber>();
const client = createMock<YRedisClient>({ redisPrefix: 'prefix' });
const redisMessageSubscriber = jest.fn();
Expand All @@ -144,7 +144,7 @@ describe('ws service', () => {

const code = 111;
const reason = 'reason';
const user = createMock<User>({ error: { code, reason } });
const user = createMock<YRedisUser>({ error: { code, reason } });
ws.getUserData.mockReturnValue(user);

return { ws, subscriber, client, redisMessageSubscriber, openWsCallback, initDocCallback, code, reason };
Expand Down Expand Up @@ -172,7 +172,7 @@ describe('ws service', () => {
const setup = () => {
const { ws, subscriber, client, redisMessageSubscriber, openWsCallback, initDocCallback } = buildParams();

const user = createMock<User>({ room: null, error: null });
const user = createMock<YRedisUser>({ room: null, error: null });
ws.getUserData.mockReturnValue(user);

return { ws, subscriber, client, redisMessageSubscriber, openWsCallback, initDocCallback };
Expand All @@ -199,7 +199,7 @@ describe('ws service', () => {
const setup = () => {
const { ws, subscriber, client, redisMessageSubscriber, openWsCallback, initDocCallback } = buildParams();

const user = createMock<User>({ userid: null, error: null });
const user = createMock<YRedisUser>({ userid: null, error: null });
ws.getUserData.mockReturnValue(user);

return { ws, subscriber, client, redisMessageSubscriber, openWsCallback, initDocCallback };
Expand All @@ -226,7 +226,7 @@ describe('ws service', () => {
const setup = () => {
const { ws, subscriber, client, redisMessageSubscriber, openWsCallback, initDocCallback } = buildParams();

const user = createMock<User>({ error: null, room: 'room', isClosed: false });
const user = createMock<YRedisUser>({ error: null, room: 'room', isClosed: false });
ws.getUserData.mockReturnValue(user);

const redisStream = computeRedisRoomStreamName(user.room ?? '', 'index', client.redisPrefix);
Expand Down Expand Up @@ -432,7 +432,7 @@ describe('ws service', () => {
const setup = () => {
const { ws, subscriber, client, redisMessageSubscriber } = buildParams();

const user = createMock<User>({ error: null, room: 'room', isClosed: true });
const user = createMock<YRedisUser>({ error: null, room: 'room', isClosed: true });
ws.getUserData.mockReturnValue(user);

return { ws, subscriber, client, redisMessageSubscriber };
Expand All @@ -450,7 +450,7 @@ describe('ws service', () => {

describe('messageCallback', () => {
const buildParams = () => {
const ws = createMock<uws.WebSocket<User>>();
const ws = createMock<uws.WebSocket<YRedisUser>>();
const client = createMock<YRedisClient>({ redisPrefix: 'prefix' });

return { ws, client };
Expand Down Expand Up @@ -488,7 +488,7 @@ describe('ws service', () => {
describe('when message is awareness update and users awarenessid is null', () => {
const setup = () => {
const { ws, client } = buildParams();
const user = createMock<User>({
const user = createMock<YRedisUser>({
hasWriteAccess: true,
room: 'room',
awarenessId: null,
Expand Down Expand Up @@ -527,7 +527,7 @@ describe('ws service', () => {
describe('when message is awareness update and users awarenessid is messages awarenessid', () => {
const setup = () => {
const { ws, client } = buildParams();
const user = createMock<User>({
const user = createMock<YRedisUser>({
hasWriteAccess: true,
room: 'room',
awarenessId: 75,
Expand Down Expand Up @@ -566,7 +566,7 @@ describe('ws service', () => {
describe('when message is sync update', () => {
const setup = () => {
const { ws, client } = buildParams();
const user = createMock<User>({
const user = createMock<YRedisUser>({
hasWriteAccess: true,
room: 'room',
awarenessId: null,
Expand Down Expand Up @@ -605,7 +605,7 @@ describe('ws service', () => {
describe('when message is sync step 2 update', () => {
const setup = () => {
const { ws, client } = buildParams();
const user = createMock<User>({
const user = createMock<YRedisUser>({
hasWriteAccess: true,
room: 'room',
awarenessId: null,
Expand Down Expand Up @@ -644,7 +644,7 @@ describe('ws service', () => {
describe('when message is sync step 1 update', () => {
const setup = () => {
const { ws, client } = buildParams();
const user = createMock<User>({
const user = createMock<YRedisUser>({
hasWriteAccess: true,
room: 'room',
awarenessId: null,
Expand Down Expand Up @@ -683,7 +683,7 @@ describe('ws service', () => {
describe('when message is of unknown type', () => {
const setup = () => {
const { ws, client } = buildParams();
const user = createMock<User>({
const user = createMock<YRedisUser>({
hasWriteAccess: true,
room: 'room',
awarenessId: null,
Expand Down Expand Up @@ -724,7 +724,7 @@ describe('ws service', () => {
describe('when message is awareness update', () => {
const setup = () => {
const { ws, client } = buildParams();
const user = createMock<User>({
const user = createMock<YRedisUser>({
hasWriteAccess: true,
room: null,
awarenessId: null,
Expand Down Expand Up @@ -767,7 +767,7 @@ describe('ws service', () => {
describe('when message is awareness update', () => {
const setup = () => {
const { ws, client } = buildParams();
const user = createMock<User>({
const user = createMock<YRedisUser>({
hasWriteAccess: false,
room: 'room',
awarenessId: null,
Expand Down Expand Up @@ -808,7 +808,7 @@ describe('ws service', () => {

describe('closeCallback', () => {
const buildParams = () => {
const ws = createMock<uws.WebSocket<User>>();
const ws = createMock<uws.WebSocket<YRedisUser>>();
const client = createMock<YRedisClient>({ redisPrefix: 'prefix' });
const app = createMock<uws.TemplatedApp>();
const subscriber = createMock<Subscriber>();
Expand Down Expand Up @@ -850,7 +850,7 @@ describe('ws service', () => {
const { ws, client, app, subscriber } = buildParams();
app.numSubscribers.mockReturnValue(0);

const user = createMock<User>({
const user = createMock<YRedisUser>({
room: 'room',
awarenessId: 22,
awarenessLastClock: 1,
Expand Down Expand Up @@ -914,7 +914,7 @@ describe('ws service', () => {
const setup = () => {
const { ws, client, app, subscriber } = buildParams();
app.numSubscribers.mockReturnValue(1);
const user = createMock<User>({
const user = createMock<YRedisUser>({
room: 'room',
awarenessId: 22,
awarenessLastClock: 1,
Expand Down Expand Up @@ -949,7 +949,7 @@ describe('ws service', () => {
const setup = () => {
const { ws, client, app, subscriber } = buildParams();
app.numSubscribers.mockReturnValueOnce(0);
const user = createMock<User>({
const user = createMock<YRedisUser>({
room: 'room',
awarenessId: null,
awarenessLastClock: 1,
Expand Down Expand Up @@ -985,7 +985,7 @@ describe('ws service', () => {
const { ws, client, app, subscriber } = buildParams();
app.numSubscribers.mockReturnValue(0);

const user = createMock<User>({
const user = createMock<YRedisUser>({
room: null,
awarenessId: 22,
awarenessLastClock: 1,
Expand Down
20 changes: 12 additions & 8 deletions src/infra/y-redis/ws.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class YWebsocketServer {

let _idCnt = 0;

export class User {
export class YRedisUser {
public subs: Set<string>;
public id: number;
public awarenessId: number | null;
Expand All @@ -67,7 +67,7 @@ export class User {
/**
* This is just an identifier to keep track of the user for logging purposes.
*/
this.id = _idCnt++;
this.id = _idCnt++; // TODO
this.awarenessId = null;
this.awarenessLastClock = 0;
this.isClosed = false;
Expand All @@ -93,7 +93,7 @@ export const upgradeCallback = async (
if (aborted) return;
res.cork(() => {
res.upgrade(
new User(room, hasWriteAccess, userid, error),
new YRedisUser(room, hasWriteAccess, userid, error),
headerWsKey,
headerWsProtocol,
headerWsExtensions,
Expand All @@ -109,11 +109,11 @@ export const upgradeCallback = async (
};

export const openCallback = async (
ws: uws.WebSocket<User>,
ws: uws.WebSocket<YRedisUser>,
subscriber: Subscriber,
client: YRedisClient,
redisMessageSubscriber: (stream: string, messages: Uint8Array[]) => void,
openWsCallback?: (ws: uws.WebSocket<User>) => void,
openWsCallback?: (ws: uws.WebSocket<YRedisUser>) => void,
initDocCallback?: (room: string, docname: string, client: YRedisClient) => void,
): Promise<void> => {
try {
Expand Down Expand Up @@ -177,7 +177,11 @@ const isSyncUpdateOrSyncStep2OrAwarenessUpdate = (message: Buffer): boolean =>
(message[1] === protocol.messageSyncUpdate || message[1] === protocol.messageSyncStep2)) ||
isAwarenessUpdate(message);

export const messageCallback = (ws: uws.WebSocket<User>, messageBuffer: ArrayBuffer, client: YRedisClient): void => {
export const messageCallback = (
ws: uws.WebSocket<YRedisUser>,
messageBuffer: ArrayBuffer,
client: YRedisClient,
): void => {
try {
const user = ws.getUserData();
// don't read any messages from users without write access
Expand Down Expand Up @@ -218,13 +222,13 @@ export const messageCallback = (ws: uws.WebSocket<User>, messageBuffer: ArrayBuf

export const closeCallback = (
app: uws.TemplatedApp,
ws: uws.WebSocket<User>,
ws: uws.WebSocket<YRedisUser>,
client: YRedisClient,
subscriber: Subscriber,
code: number,
message: ArrayBuffer,
redisMessageSubscriber: (stream: string, messages: Uint8Array[]) => void,
closeWsCallback?: (ws: uws.WebSocket<User>, code: number, message: ArrayBuffer) => void,
closeWsCallback?: (ws: uws.WebSocket<YRedisUser>, code: number, message: ArrayBuffer) => void,
): void => {
try {
const user = ws.getUserData();
Expand Down
4 changes: 2 additions & 2 deletions src/modules/server/api/websocket.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
messageCallback,
openCallback,
upgradeCallback,
User,
YRedisUser,
} from '../../../infra/y-redis/ws.service.js';
import { REDIS_FOR_SUBSCRIBE_OF_DELETION, UWS } from '../server.const.js';
import { TldrawServerConfig } from '../tldraw-server.config.js';
Expand Down Expand Up @@ -47,7 +47,7 @@ export class WebsocketGateway implements OnModuleInit, OnModuleDestroy {
idleTimeout: 60,
sendPingsAutomatically: true,
upgrade: (res, req, context) => upgradeCallback(res, req, context, checkAuth),
open: (ws: WebSocket<User>) =>
open: (ws: WebSocket<YRedisUser>) =>
openCallback(ws, this.subscriberService, this.yRedisClient, this.redisMessageSubscriber, () =>
MetricsService.openConnectionsGauge.inc(),
),
Expand Down

0 comments on commit cbdd9ac

Please sign in to comment.