Skip to content

Commit

Permalink
feat: Allow to validate sent packets using zod
Browse files Browse the repository at this point in the history
  • Loading branch information
tshashkova committed Oct 31, 2024
1 parent b67f093 commit 93d6aaa
Show file tree
Hide file tree
Showing 14 changed files with 283 additions and 62 deletions.
1 change: 1 addition & 0 deletions packages/web-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Server to Client log events
- Latency reporting support
- S2C logs added to conversation history
- Allow to validate sent packets using zod

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const createWebSocket = (props: {
onReady,
onMessage: props.onMessage || onMessage,
onDisconnect,
eventFactory,
});
};

Expand Down Expand Up @@ -473,6 +474,7 @@ describe('open', () => {
onReady,
onMessage,
onDisconnect,
eventFactory,
});
jest.spyOn(WebSocket.prototype, 'send').mockImplementation(jest.fn());

Expand Down
16 changes: 8 additions & 8 deletions packages/web-core/__tests__/factories/event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ describe('event types', () => {
});

test('should generate session control capabilities', () => {
const event = EventFactory.sessionControl({
const event = factory.sessionControl({
capabilities: capabilitiesProps,
});

Expand All @@ -317,7 +317,7 @@ describe('event types', () => {

test('should generate session control session configuration', () => {
const sessionConfiguration = { gameSessionId: v4() };
const event = EventFactory.sessionControl({ sessionConfiguration });
const event = factory.sessionControl({ sessionConfiguration });

expect(event).toHaveProperty('routing');
expect(event).toHaveProperty('timestamp');
Expand All @@ -337,7 +337,7 @@ describe('event types', () => {
version: v4(),
description: v4(),
};
const event = EventFactory.sessionControl({ clientConfiguration });
const event = factory.sessionControl({ clientConfiguration });

expect(event).toHaveProperty('routing');
expect(event).toHaveProperty('timestamp');
Expand All @@ -357,7 +357,7 @@ describe('event types', () => {
fullName: v4(),
profile: { fields: [{ id: v4(), value: v4() }] },
};
const event = EventFactory.sessionControl({ userConfiguration });
const event = factory.sessionControl({ userConfiguration });

expect(event).toHaveProperty('routing');
expect(event).toHaveProperty('timestamp');
Expand All @@ -384,7 +384,7 @@ describe('event types', () => {
continuationType:
ContinuationContinuationType.CONTINUATION_TYPE_DIALOG_HISTORY,
};
const event = EventFactory.sessionControl({ continuation });
const event = factory.sessionControl({ continuation });

expect(event).toHaveProperty('routing');
expect(event).toHaveProperty('timestamp');
Expand All @@ -404,7 +404,7 @@ describe('event types', () => {
continuationType:
ContinuationContinuationType.CONTINUATION_TYPE_EXTERNALLY_SAVED_STATE,
};
const event = EventFactory.sessionControl({ continuation });
const event = factory.sessionControl({ continuation });

expect(event).toHaveProperty('routing');
expect(event).toHaveProperty('timestamp');
Expand All @@ -420,7 +420,7 @@ describe('event types', () => {

test('should generate session control history', () => {
const sessionHistory = {};
const event = EventFactory.sessionControl({ sessionHistory });
const event = factory.sessionControl({ sessionHistory });

expect(event).toHaveProperty('routing');
expect(event).toHaveProperty('timestamp');
Expand All @@ -434,7 +434,7 @@ describe('event types', () => {

test('should generate conversation start event', () => {
const characters = [v4(), v4()];
const event = EventFactory.conversation(characters, {
const event = factory.conversation(characters, {
conversationId,
});
expect(event.control?.action).toEqual(
Expand Down
9 changes: 6 additions & 3 deletions packages/web-core/__tests__/services/entity.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ beforeEach(() => {
});

test('should create or update items', async () => {
const createOrUpdateItems = jest.spyOn(EventFactory, 'createOrUpdateItems');
const createOrUpdateItems = jest.spyOn(
EventFactory.prototype,
'createOrUpdateItems',
);

const items = [
{
Expand Down Expand Up @@ -67,7 +70,7 @@ test('should create or update items', async () => {
});

test('should remove items', async () => {
const removeItems = jest.spyOn(EventFactory, 'removeItems');
const removeItems = jest.spyOn(EventFactory.prototype, 'removeItems');

const ids = [v4(), v4()];

Expand All @@ -82,7 +85,7 @@ test.each([
ItemsInEntitiesOperationType.REMOVE,
ItemsInEntitiesOperationType.REPLACE,
])('shout execute $type', async (type) => {
const itemsInEntities = jest.spyOn(EventFactory, 'itemsInEntities');
const itemsInEntities = jest.spyOn(EventFactory.prototype, 'itemsInEntities');

const itemIds = [v4(), v4()];
const entityNames = [v4(), v4()];
Expand Down
1 change: 1 addition & 0 deletions packages/web-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"dependencies": {
"base64-arraybuffer": "^1.0.2",
"defer-promise": "^3.0.0",
"snakecase-keys": "^8.0.1",
"uuid": "^9.0.0",
"zod": "^3.23.8"
}
Expand Down
2 changes: 2 additions & 0 deletions packages/web-core/src/common/data_structures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export interface ClientConfiguration {
capabilities?: Capabilities;
audioPlayback?: AudioPlaybackConfig;
history?: HistoryConfig;
validateData?: boolean;
}

export interface InternalClientConfiguration {
Expand All @@ -115,6 +116,7 @@ export interface InternalClientConfiguration {
capabilities: CapabilitiesConfiguration;
audioPlayback?: AudioPlaybackConfig;
history?: HistoryConfig;
validateData?: boolean;
}

export interface CancelResponses {
Expand Down
15 changes: 11 additions & 4 deletions packages/web-core/src/connection/web-socket.connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ interface ConnectionProps<
onError: (err: InworldError) => Awaitable<void>;
onMessage: (packet: ProtoPacket) => Awaitable<void>;
extension: Extension<InworldPacketT, HistoryItemT>;
eventFactory: EventFactory;
}

export interface QueueItem<InworldPacketT> {
Expand Down Expand Up @@ -302,7 +303,11 @@ export class WebSocketConnection<
resolve: (value: LoadedScene) => void;
reject: (reason: InworldError) => void;
}) {
const { parseEvent, onMessage } = this;
const {
parseEvent,
onMessage,
connectionProps: { eventFactory },
} = this;
let historyLoaded = true;
let sceneStatus: CurrentSceneStatus;

Expand All @@ -329,7 +334,7 @@ export class WebSocketConnection<
if (!!sceneStatus && !historyLoaded && needHistory) {
write({
getPacket: () =>
EventFactory.sessionControl({ sessionHistory: {} }),
eventFactory.sessionControl({ sessionHistory: {} }),
});
} else {
ws.removeEventListener('message', this);
Expand All @@ -353,12 +358,14 @@ export class WebSocketConnection<
gameSessionId?: string;
useDefaultClient?: boolean;
}) {
const { eventFactory } = this.connectionProps;

const continuation = this.getContinuation({
sessionContinuation: props.sessionContinuation,
});

const packets: ProtoPacket[] = [
EventFactory.sessionControl({
eventFactory.sessionControl({
...(props.capabilities && {
capabilities: props.capabilities,
}),
Expand All @@ -375,7 +382,7 @@ export class WebSocketConnection<
}),
...(continuation && { continuation }),
}),
EventFactory.loadScene(props.name),
eventFactory.loadScene(props.name),
];

return this.extension.beforeLoadScene?.(packets) || packets;
Expand Down
Loading

0 comments on commit 93d6aaa

Please sign in to comment.