Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(*): replace ts-proto with protobuf-ts #13

Merged
merged 10 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"scripts": {
"dev": "ts-node src/index.ts",
"lint": "eslint \"src/**/*.ts\"",
"generate-proto": "protoc --plugin=./node_modules/.bin/protoc-gen-ts_proto --ts_proto_out=proto/ --ts_proto_opt=esModuleInterop=true --ts_proto_opt=exportCommonSymbols=false --ts_proto_opt=outputTypeRegistry=true Mumble.proto",
"generate-proto": "protoc --ts_out proto/ --ts_opt eslint_disable --proto_path . Mumble.proto",
"prebuild": "yarn generate-proto",
"build": "tsc --project tsconfig.build.json && tsc-alias -p tsconfig.build.json",
"test": "jest --watch",
Expand All @@ -35,11 +35,12 @@
"bot"
],
"dependencies": {
"@protobuf-ts/runtime": "2.5.0",
"lodash": "^4.17.21",
"rxjs": "7.5.5",
"ts-proto": "^1.112.1"
"rxjs": "7.5.5"
},
"devDependencies": {
"@protobuf-ts/plugin": "2.5.0",
"@release-it/conventional-changelog": "5.0.0",
"@tsconfig/node16": "1.0.2",
"@types/jest": "27.5.1",
Expand Down
24 changes: 16 additions & 8 deletions src/channel-manager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ChannelRemove, ChannelState, PermissionQuery } from '@proto/Mumble';
import { filter, map, tap } from 'rxjs';
import { tap } from 'rxjs';
import { Channel } from './channel';
import { Client } from './client';
import { filterPacket } from './rxjs-operators/filter-packet';
import { MumbleSocket } from './mumble-socket';

export class ChannelManager {
Expand All @@ -13,24 +14,21 @@ export class ChannelManager {

socket.packet
.pipe(
filter(packet => packet.$type === ChannelState.$type),
map(packet => packet as ChannelState),
filterPacket(ChannelState),
tap(channelState => this.syncChannelState(channelState)),
)
.subscribe();

socket.packet
.pipe(
filter(packet => packet.$type === ChannelRemove.$type),
map(packet => packet as ChannelRemove),
filterPacket(ChannelRemove),
tap(channelRemove => this.removeChannel(channelRemove)),
)
.subscribe();

socket.packet
.pipe(
filter(packet => packet.$type === PermissionQuery.$type),
map(packet => packet as PermissionQuery),
filterPacket(PermissionQuery),
tap(permissionQuery => this.syncChannelPermissions(permissionQuery)),
)
.subscribe();
Expand Down Expand Up @@ -89,9 +87,16 @@ export class ChannelManager {
}

private syncChannelState(channelState: ChannelState) {
if (channelState.channelId === undefined) {
return;
}

let channel = this.byId(channelState.channelId);
if (!channel) {
channel = new Channel(this.client, channelState);
channel = new Channel(
this.client,
channelState as ChannelState & { channelId: number },
);
this._channels.set(channel.id, channel);
/**
* Emitted whenever a channel is created.
Expand All @@ -105,6 +110,9 @@ export class ChannelManager {
}

private syncChannelPermissions(permissionQuery: PermissionQuery) {
if (permissionQuery.channelId === undefined) {
return;
}
this.byId(permissionQuery.channelId)?.sync(permissionQuery);
}

Expand Down
33 changes: 23 additions & 10 deletions src/channel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ jest.mock('./client');
jest.mock('./commands', () => ({
fetchChannelPermissions: jest
.fn()
.mockResolvedValue(
PermissionQuery.fromPartial({ permissions: 0x1 | 0x40 }),
),
.mockResolvedValue(PermissionQuery.create({ permissions: 0x1 | 0x40 })),
}));

describe('Channel', () => {
Expand All @@ -27,11 +25,11 @@ describe('Channel', () => {
it('should assign properties', () => {
const channel = new Channel(
client,
ChannelState.fromPartial({
ChannelState.create({
channelId: 7,
name: 'FAKE_CHANNEL_NAME',
parent: 6,
}),
}) as ChannelState & { channelId: number },
);
expect(channel.id).toBe(7);
expect(channel.name).toEqual('FAKE_CHANNEL_NAME');
Expand All @@ -41,16 +39,21 @@ describe('Channel', () => {
let channel: Channel;

beforeEach(() => {
channel = new Channel(client, ChannelState.fromPartial({}));
channel = new Channel(
client,
ChannelState.create({ channelId: 0 }) as ChannelState & {
channelId: number;
},
);
});

it('should update name', () => {
channel.sync(ChannelState.fromPartial({ name: 'NEW_CHANNEL_NAME' }));
channel.sync(ChannelState.create({ name: 'NEW_CHANNEL_NAME' }));
expect(channel.name).toEqual('NEW_CHANNEL_NAME');
});

it('should update parent', () => {
channel.sync(ChannelState.fromPartial({ parent: 10 }));
channel.sync(ChannelState.create({ parent: 10 }));
expect(channel.parent).toEqual(10);
});
});
Expand All @@ -59,7 +62,12 @@ describe('Channel', () => {
let channel: Channel;

beforeEach(() => {
channel = new Channel(client, ChannelState.fromPartial({ channelId: 7 }));
channel = new Channel(
client,
ChannelState.create({ channelId: 7 }) as ChannelState & {
channelId: number;
},
);
});

it('should attempt to create channel', async () => {
Expand All @@ -72,7 +80,12 @@ describe('Channel', () => {
let channel: Channel;

beforeEach(() => {
channel = new Channel(client, ChannelState.fromPartial({ channelId: 7 }));
channel = new Channel(
client,
ChannelState.create({ channelId: 7 }) as ChannelState & {
channelId: number;
},
);
});

it('should attempt to remove the channel', async () => {
Expand Down
41 changes: 18 additions & 23 deletions src/channel.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { ChannelState, PermissionQuery } from '@proto/Mumble';
import { UnknownMessage } from '@proto/typeRegistry';
import { isEmpty } from 'lodash';
import { Client } from './client';
import { fetchChannelPermissions } from './commands';
import { InsufficientPermissionsError } from './errors';
Expand All @@ -9,11 +7,14 @@ import { User } from './user';

export class Channel {
readonly id: number;
name: string;
parent: number;
name?: string;
parent?: number;
private permissions?: Permissions;

constructor(public readonly client: Client, channelState: ChannelState) {
constructor(
public readonly client: Client,
channelState: ChannelState & { channelId: number },
) {
this.id = channelState.channelId;
this.name = channelState.name;
this.parent = channelState.parent;
Expand All @@ -22,25 +23,18 @@ export class Channel {
/**
* @internal
*/
sync(message: UnknownMessage) {
switch (message.$type) {
case ChannelState.$type: {
const channelState = message as ChannelState;

if (!isEmpty(channelState.name)) {
this.name = channelState.name;
}

if (channelState.parent) {
this.parent = channelState.parent;
}
break;
sync(message: unknown) {
if (ChannelState.is(message)) {
if (message.name !== undefined) {
this.name = message.name;
}

case PermissionQuery.$type: {
const permissionQuery = message as PermissionQuery;
this.permissions = new Permissions(permissionQuery.permissions);
break;
if (message.parent !== undefined) {
this.parent = message.parent;
}
} else if (PermissionQuery.is(message)) {
if (message.permissions !== undefined) {
this.permissions = new Permissions(message.permissions);
}
}
}
Expand Down Expand Up @@ -81,7 +75,8 @@ export class Channel {
}

return new Permissions(
(await fetchChannelPermissions(this.client.socket, this.id)).permissions,
(await fetchChannelPermissions(this.client.socket, this.id))
.permissions ?? 0,
);
}
}
49 changes: 39 additions & 10 deletions src/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
ServerSync,
Version,
} from '@proto/Mumble';
import { UnknownMessage } from '@proto/typeRegistry';
import { Subject } from 'rxjs';
import { Client } from './client';
import { MumbleSocket } from './mumble-socket';
Expand Down Expand Up @@ -40,21 +39,51 @@ describe(Client.name, () => {
});

describe('when connected', () => {
let socket: jest.Mocked<MumbleSocket> & { packet: Subject<UnknownMessage> };
let socket: jest.Mocked<MumbleSocket> & { packet: Subject<unknown> };

beforeEach(async () => {
client.on('socketConnected', s => {
socket = s;

socket.send.mockImplementation(message => {
switch (message.$type) {
case Authenticate.$type:
socket.packet.next(Version.fromPartial({}));
socket.packet.next(ServerSync.fromPartial({ session: 1234 }));
socket.packet.next(ServerConfig.fromPartial({}));
socket.send.mockImplementation(type => {
switch (type.typeName) {
case Authenticate.typeName:
socket.packet.next(
Version.create({
version: 66790,
release: '1.4.230',
os: 'Linux',
osVersion: 'Ubuntu 20.04.4 LTS [x64]',
}),
);
socket.packet.next(
ServerSync.create({
session: 2,
maxBandwidth: 558000,
welcomeText: '',
permissions: BigInt(134744846),
}),
);
socket.packet.next(
ServerConfig.create({
allowHtml: true,
messageLength: 5000,
imageMessageLength: 131072,
maxUsers: 100,
}),
);
break;
case Ping.$type:
socket.packet.next(Ping.fromPartial({}));

case Ping.typeName:
socket.packet.next(
Ping.create({
timestamp: BigInt(0),
good: 0,
late: 0,
lost: 0,
resync: 0,
}),
);
break;
}

Expand Down
Loading