Skip to content

Commit 05473bc

Browse files
committed
refactor(protocol): remove readMessageType function and add encodeQueryAwareness function
1 parent 5ea14c7 commit 05473bc

File tree

2 files changed

+18
-48
lines changed

2 files changed

+18
-48
lines changed

packages/epicenter/src/server/sync/protocol.test.ts

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import {
1616
decodeSyncMessage,
1717
encodeAwareness,
1818
encodeAwarenessStates,
19+
encodeQueryAwareness,
1920
encodeSyncStep1,
2021
encodeSyncStep2,
2122
encodeSyncUpdate,
2223
handleSyncMessage,
2324
MESSAGE_TYPE,
24-
readMessageType,
2525
SYNC_MESSAGE_TYPE,
2626
} from './protocol';
2727

@@ -241,27 +241,6 @@ describe('MESSAGE_SYNC', () => {
241241
});
242242
});
243243

244-
describe('readMessageType', () => {
245-
test('reads sync message type', () => {
246-
const doc = createDoc();
247-
const message = encodeSyncStep1({ doc });
248-
249-
expect(readMessageType({ data: message })).toBe(MESSAGE_TYPE.SYNC);
250-
});
251-
252-
test('reads awareness message type', () => {
253-
const doc = createDoc();
254-
const awareness = new awarenessProtocol.Awareness(doc);
255-
awareness.setLocalState({ name: 'Test' });
256-
257-
const message = encodeAwarenessStates({
258-
awareness,
259-
clients: [awareness.clientID],
260-
});
261-
262-
expect(readMessageType({ data: message })).toBe(MESSAGE_TYPE.AWARENESS);
263-
});
264-
});
265244
});
266245

267246
// ============================================================================
@@ -394,9 +373,7 @@ describe('MESSAGE_AWARENESS', () => {
394373

395374
describe('MESSAGE_QUERY_AWARENESS', () => {
396375
test('query awareness message is single byte', () => {
397-
const message = encoding.encode((encoder) => {
398-
encoding.writeVarUint(encoder, MESSAGE_TYPE.QUERY_AWARENESS);
399-
});
376+
const message = encodeQueryAwareness();
400377

401378
expect(message.length).toBe(1);
402379
expect(message[0]).toBe(MESSAGE_TYPE.QUERY_AWARENESS);
@@ -500,19 +477,10 @@ describe('decodeMessageType', () => {
500477
});
501478

502479
test('decodes QUERY_AWARENESS message type', () => {
503-
const message = encoding.encode((encoder) => {
504-
encoding.writeVarUint(encoder, MESSAGE_TYPE.QUERY_AWARENESS);
505-
});
480+
const message = encodeQueryAwareness();
506481
expect(decodeMessageType(message)).toBe(MESSAGE_TYPE.QUERY_AWARENESS);
507482
});
508483

509-
test('matches readMessageType behavior', () => {
510-
const doc = createDoc();
511-
const message = encodeSyncStep1({ doc });
512-
513-
// Both functions should return the same result
514-
expect(decodeMessageType(message)).toBe(readMessageType({ data: message }));
515-
});
516484
});
517485

518486
// ============================================================================

packages/epicenter/src/server/sync/protocol.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,14 @@ export const MESSAGE_TYPE = {
4040
export type MessageType = (typeof MESSAGE_TYPE)[keyof typeof MESSAGE_TYPE];
4141

4242
/**
43-
* Reads the message type from raw message data.
43+
* Decodes the top-level message type from raw message data.
4444
*
4545
* The first varint in any y-websocket message is the message type:
4646
* - 0: MESSAGE_SYNC (document sync)
4747
* - 1: MESSAGE_AWARENESS (user presence)
4848
* - 2: MESSAGE_AUTH (authentication, reserved)
4949
* - 3: MESSAGE_QUERY_AWARENESS (request awareness states)
5050
*
51-
* @param options.data - Raw message bytes received from WebSocket
52-
* @returns The message type constant
53-
*/
54-
export function readMessageType({ data }: { data: Uint8Array }): number {
55-
const decoder = decoding.createDecoder(data);
56-
return decoding.readVarUint(decoder);
57-
}
58-
59-
/**
60-
* Decodes the top-level message type from raw message data.
61-
*
62-
* Equivalent to readMessageType but follows the decode* naming convention.
6351
* Useful for quickly determining message type before full parsing.
6452
*
6553
* @param data - Raw message bytes
@@ -271,3 +259,17 @@ export function encodeAwarenessStates({
271259
update: awarenessProtocol.encodeAwarenessUpdate(awareness, clients),
272260
});
273261
}
262+
263+
/**
264+
* Encodes a query awareness message.
265+
*
266+
* This message requests all current awareness states from the server.
267+
* Typically sent by clients that need to refresh their view of other users.
268+
*
269+
* @returns Encoded message ready to send over WebSocket
270+
*/
271+
export function encodeQueryAwareness(): Uint8Array {
272+
return encoding.encode((encoder) => {
273+
encoding.writeVarUint(encoder, MESSAGE_TYPE.QUERY_AWARENESS);
274+
});
275+
}

0 commit comments

Comments
 (0)