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(*): fix commands' error handling #39

Merged
merged 1 commit into from
May 25, 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
51 changes: 51 additions & 0 deletions e2e/creates-and-removes-channels.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Channel, Client } from '@';
import { readFile } from 'fs/promises';
import { join } from 'path';
import { waitABit } from './utils/wait-a-bit';

describe('Creates and removes channels (e2e)', () => {
let client: Client;

beforeAll(async () => {
const key = (await readFile(join(__dirname, 'tester-key.pem'))).toString();
const cert = (
await readFile(join(__dirname, 'tester-cert.pem'))
).toString();

client = new Client({
host: 'localhost',
port: 64738,
username: 'tester',
key,
cert,
rejectUnauthorized: false,
});
await client.connect();
await waitABit(1000);

await client.user?.moveToChannel(
(client.channels.byName('one') as Channel).id,
);
});

afterAll(async () => {
await waitABit(1000);
client.disconnect();
});

it('should create channels', async () => {
const channel = client.user?.channel as Channel;
expect(channel).toBeTruthy();
const sub1 = await channel.createSubChannel('sub1');
expect(sub1.parent).toEqual(channel.id);

const sub2 = await sub1.createSubChannel('sub2');
expect(sub2.parent).toEqual(sub1.id);
});

it('should remove channels', async () => {
const channel = client.channels.byName('sub1');
await channel?.remove();
expect(client.channels.byName('sub1')).toBe(undefined);
});
});
Binary file modified e2e/mumble-data/murmur.sqlite
Binary file not shown.
52 changes: 33 additions & 19 deletions src/commands/create-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@ import { CommandTimedOutError, PermissionDeniedError } from '@/errors';
import { MumbleSocket } from '@/mumble-socket';
import { filterPacket } from '@/rxjs-operators/filter-packet';
import { ChannelState, PermissionDenied } from '@tf2pickup-org/mumble-protocol';
import { filter, race, take, timer } from 'rxjs';
import {
concatMap,
filter,
lastValueFrom,
map,
race,
take,
throwError,
timer,
} from 'rxjs';

export const createChannel = async (
socket: MumbleSocket,
parentChannelId: number,
channelName: string,
): Promise<number> =>
new Promise((resolve, reject) => {
): Promise<number> => {
const ret = lastValueFrom(
race(
socket.packet.pipe(
filterPacket(ChannelState),
Expand All @@ -20,21 +29,26 @@ export const createChannel = async (
channelState.name === channelName,
),
take(1),
map(channelState => channelState.channelId as number),
),
socket.packet.pipe(filterPacket(PermissionDenied), take(1)),
timer(CommandTimeout),
).subscribe(packet => {
if (PermissionDenied.is(packet)) {
reject(new PermissionDeniedError(packet));
} else if (ChannelState.is(packet)) {
resolve(packet.channelId as number);
} else {
reject(new CommandTimedOutError('createChannel'));
}
});
socket.packet.pipe(
filterPacket(PermissionDenied),
take(1),
concatMap(permissionDenied =>
throwError(() => new PermissionDeniedError(permissionDenied)),
),
),
timer(CommandTimeout).pipe(
concatMap(() =>
throwError(() => new CommandTimedOutError('createChannel')),
),
),
),
);

socket.send(
ChannelState,
ChannelState.create({ parent: parentChannelId, name: channelName }),
);
});
socket.send(
ChannelState,
ChannelState.create({ parent: parentChannelId, name: channelName }),
);
return ret;
};
44 changes: 34 additions & 10 deletions src/commands/link-channels.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,50 @@
import { CommandTimeout } from '@/config';
import { CommandTimedOutError, PermissionDeniedError } from '@/errors';
import { MumbleSocket } from '@/mumble-socket';
import { filterPacket } from '@/rxjs-operators/filter-packet';
import { ChannelState } from '@tf2pickup-org/mumble-protocol';
import { filter, race, take, timer } from 'rxjs';
import { ChannelState, PermissionDenied } from '@tf2pickup-org/mumble-protocol';
import {
concatMap,
filter,
lastValueFrom,
map,
race,
take,
throwError,
timer,
} from 'rxjs';

export const linkChannels = async (
socket: MumbleSocket,
channelId: number,
targetChannelId: number,
): Promise<void> => {
return new Promise(resolve => {
const ret = lastValueFrom(
race(
socket.packet.pipe(
filterPacket(ChannelState),
filter(channelSync => channelSync.channelId === channelId),
take(1),
map(() => void 0),
),
timer(CommandTimeout),
).subscribe(() => resolve());
socket.send(
ChannelState,
ChannelState.create({ channelId, linksAdd: [targetChannelId] }),
);
});
socket.packet.pipe(
filterPacket(PermissionDenied),
filter(permissionDenied => permissionDenied.channelId === channelId),
take(1),
concatMap(permissionDenied =>
throwError(() => new PermissionDeniedError(permissionDenied)),
),
),
timer(CommandTimeout).pipe(
concatMap(() =>
throwError(() => new CommandTimedOutError('linkChannels')),
),
),
),
);
socket.send(
ChannelState,
ChannelState.create({ channelId, linksAdd: [targetChannelId] }),
);
return ret;
};
42 changes: 25 additions & 17 deletions src/commands/move-user-to-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@ import { PermissionDeniedError } from '@/errors';
import { MumbleSocket } from '@/mumble-socket';
import { filterPacket } from '@/rxjs-operators/filter-packet';
import { PermissionDenied, UserState } from '@tf2pickup-org/mumble-protocol';
import { filter, race, take } from 'rxjs';
import {
concatMap,
filter,
lastValueFrom,
map,
race,
take,
throwError,
} from 'rxjs';

export const moveUserToChannel = async (
socket: MumbleSocket,
userSession: number,
channelId: number,
): Promise<void> =>
new Promise((resolve, reject) => {
): Promise<void> => {
const ret = lastValueFrom(
race(
socket.packet.pipe(
filterPacket(UserState),
Expand All @@ -18,18 +26,18 @@ export const moveUserToChannel = async (
userState.session === userSession &&
userState.channelId === channelId,
),
take(1),
map(() => void 0),
),
socket.packet.pipe(filterPacket(PermissionDenied), take(1)),
).subscribe(packet => {
if (PermissionDenied.is(packet)) {
reject(new PermissionDeniedError(packet));
} else {
resolve();
}
});

socket.send(
UserState,
UserState.create({ session: userSession, channelId }),
);
});
socket.packet.pipe(
filterPacket(PermissionDenied),
take(1),
concatMap(permissionDenied =>
throwError(() => new PermissionDeniedError(permissionDenied)),
),
),
),
);
socket.send(UserState, UserState.create({ session: userSession, channelId }));
return ret;
};
53 changes: 37 additions & 16 deletions src/commands/remove-channel.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,49 @@
import { PermissionDeniedError } from '@/errors';
import { CommandTimeout } from '@/config';
import { CommandTimedOutError, PermissionDeniedError } from '@/errors';
import { MumbleSocket } from '@/mumble-socket';
import { filterPacket } from '@/rxjs-operators/filter-packet';
import { ChannelRemove, PermissionDenied } from '@tf2pickup-org/mumble-protocol';
import { filter, race, take } from 'rxjs';
import {
ChannelRemove,
PermissionDenied,
} from '@tf2pickup-org/mumble-protocol';
import {
concatMap,
filter,
lastValueFrom,
map,
race,
take,
throwError,
timer,
} from 'rxjs';

export const removeChannel = async (
socket: MumbleSocket,
channelId: number,
): Promise<number> =>
new Promise((resolve, reject) => {
): Promise<number> => {
const ret = lastValueFrom(
race(
socket.packet.pipe(
filterPacket(ChannelRemove),
filter(channelRemove => channelRemove.channelId === channelId),
take(1),
map(channelRemove => channelRemove.channelId),
),
socket.packet.pipe(filterPacket(PermissionDenied), take(1)),
).subscribe(packet => {
if (PermissionDenied.is(packet)) {
reject(new PermissionDeniedError(packet));
} else {
resolve(packet.channelId as number);
}
});

socket.send(ChannelRemove, ChannelRemove.create({ channelId }));
});
socket.packet.pipe(
filterPacket(PermissionDenied),
filter(permissionDenied => permissionDenied.channelId === channelId),
take(1),
concatMap(permissionDenied =>
throwError(() => new PermissionDeniedError(permissionDenied)),
),
),
timer(CommandTimeout).pipe(
concatMap(() =>
throwError(() => new CommandTimedOutError('removeChannel')),
),
),
),
);
socket.send(ChannelRemove, ChannelRemove.create({ channelId }));
return ret;
};
50 changes: 38 additions & 12 deletions src/commands/unlink-channels.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,50 @@
import { CommandTimeout } from '@/config';
import { CommandTimedOutError, PermissionDeniedError } from '@/errors';
import { MumbleSocket } from '@/mumble-socket';
import { filterPacket } from '@/rxjs-operators/filter-packet';
import { ChannelState } from '@tf2pickup-org/mumble-protocol';
import { filter, take } from 'rxjs';
import { ChannelState, PermissionDenied } from '@tf2pickup-org/mumble-protocol';
import {
concatMap,
filter,
lastValueFrom,
map,
race,
take,
throwError,
timer,
} from 'rxjs';

export const unlinkChannels = async (
socket: MumbleSocket,
channelId: number,
targetChannelId: number,
): Promise<void> => {
return new Promise(resolve => {
socket.packet
.pipe(
const ret = lastValueFrom(
race(
socket.packet.pipe(
filterPacket(ChannelState),
filter(channelSync => channelSync.channelId === channelId),
take(1),
)
.subscribe(() => resolve());
socket.send(
ChannelState,
ChannelState.create({ channelId, linksRemove: [targetChannelId] }),
);
});
map(() => void 0),
),
socket.packet.pipe(
filterPacket(PermissionDenied),
filter(permissionDenied => permissionDenied.channelId === channelId),
take(1),
concatMap(permissionDenied =>
throwError(() => new PermissionDeniedError(permissionDenied)),
),
),
timer(CommandTimeout).pipe(
concatMap(() =>
throwError(() => new CommandTimedOutError('linkChannels')),
),
),
),
);
socket.send(
ChannelState,
ChannelState.create({ channelId, linksRemove: [targetChannelId] }),
);
return ret;
};
5 changes: 4 additions & 1 deletion src/errors/permission-denied.error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { PermissionDenied, PermissionDenied_DenyType } from '@tf2pickup-org/mumble-protocol';
import {
PermissionDenied,
PermissionDenied_DenyType,
} from '@tf2pickup-org/mumble-protocol';

const toString = (permissionDenied: PermissionDenied): string => {
switch (permissionDenied.type) {
Expand Down