Skip to content

Commit

Permalink
Add new transport.type getter than returns `'webrtc' | 'plain' | 'p…
Browse files Browse the repository at this point in the history
…ipe' | 'direct'`
  • Loading branch information
ibc committed Nov 11, 2024
1 parent dc614c1 commit aa5853c
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 13 deletions.
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ const config = tsEslint.config(
},
},
rules: {
'@typescript-eslint/class-literal-property-style': [2, 'getters'],
'@typescript-eslint/consistent-generic-constructors': [
2,
'type-annotation',
Expand Down
4 changes: 4 additions & 0 deletions node/src/DirectTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export class DirectTransportImpl<
this.handleListenerError();
}

get type(): 'direct' {
return 'direct';
}

get observer(): DirectTransportObserver {
return super.observer;
}
Expand Down
7 changes: 7 additions & 0 deletions node/src/DirectTransportTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ export interface DirectTransport<
DirectTransportEvents,
DirectTransportObserver
> {
/**
* Transport type.
*
* @override
*/
get type(): 'direct';

/**
* Observer.
*
Expand Down
4 changes: 4 additions & 0 deletions node/src/PipeTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export class PipeTransportImpl<PipeTransportAppData extends AppData = AppData>
this.handleListenerError();
}

get type(): 'pipe' {
return 'pipe';
}

get observer(): PipeTransportObserver {
return super.observer;
}
Expand Down
7 changes: 7 additions & 0 deletions node/src/PipeTransportTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ export interface PipeTransport<PipeTransportAppData extends AppData = AppData>
PipeTransportEvents,
PipeTransportObserver
> {
/**
* Transport type.
*
* @override
*/
get type(): 'pipe';

/**
* Observer.
*
Expand Down
4 changes: 4 additions & 0 deletions node/src/PlainTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ export class PlainTransportImpl<PlainTransportAppData extends AppData = AppData>
this.handleListenerError();
}

get type(): 'plain' {
return 'plain';
}

get observer(): PlainTransportObserver {
return super.observer;
}
Expand Down
7 changes: 7 additions & 0 deletions node/src/PlainTransportTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ export interface PlainTransport<PlainTransportAppData extends AppData = AppData>
PlainTransportEvents,
PlainTransportObserver
> {
/**
* Transport type.
*
* @override
*/
get type(): 'plain';

/**
* Observer.
*
Expand Down
13 changes: 6 additions & 7 deletions node/src/Transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EnhancedEventEmitter } from './enhancedEvents';
import * as ortc from './ortc';
import type {
Transport,
TransportType,
TransportProtocol,
TransportPortRange,
TransportSocketFlags,
Expand Down Expand Up @@ -208,6 +209,8 @@ export abstract class TransportImpl<
return this.#closed;
}

abstract get type(): TransportType;

get appData(): TransportAppData {
return this.#appData;
}
Expand Down Expand Up @@ -481,8 +484,7 @@ export abstract class TransportImpl<

// Don't do this in PipeTransports since there we must keep CNAME value in
// each Producer.
// TODO: This is error prune if we rename the class name.
if (this.constructor.name !== 'PipeTransportImpl') {
if (this.type !== 'pipe') {
// If CNAME is given and we don't have yet a CNAME for Producers in this
// Transport, take it.
if (!this.#cnameForProducers && clonedRtpParameters.rtcp?.cname) {
Expand Down Expand Up @@ -724,8 +726,7 @@ export abstract class TransportImpl<
>(sctpStreamParameters);

// If this is not a DirectTransport, sctpStreamParameters are required.
// TODO: This is error prune if we rename the class name.
if (this.constructor.name !== 'DirectTransportImpl') {
if (this.type !== 'direct') {
type = 'sctp';

// This may throw.
Expand Down Expand Up @@ -831,8 +832,7 @@ export abstract class TransportImpl<

// If this is not a DirectTransport, use sctpStreamParameters from the
// DataProducer (if type 'sctp') unless they are given in method parameters.
// TODO: This is error prune if we rename the class name.
if (this.constructor.name !== 'DirectTransportImpl') {
if (this.type !== 'direct') {
type = 'sctp';

sctpStreamParameters =
Expand Down Expand Up @@ -1151,7 +1151,6 @@ export function parseBaseTransportDump(

return {
id: binary.id()!,
direct: binary.direct(),
producerIds: producerIds,
consumerIds: consumerIds,
mapSsrcConsumerId: mapSsrcConsumerId,
Expand Down
15 changes: 14 additions & 1 deletion node/src/TransportTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import type { DataConsumer, DataConsumerOptions } from './DataConsumerTypes';
import type { SctpParameters } from './sctpParametersTypes';
import type { AppData } from './types';

/**
* Transport type.
*/
export type TransportType = 'webrtc' | 'plain' | 'pipe' | 'direct';

export type TransportListenInfo = {
/**
* Network protocol.
Expand Down Expand Up @@ -145,7 +150,6 @@ export type RecvRtpHeaderExtensions = {

export type BaseTransportDump = {
id: string;
direct: boolean;
producerIds: string[];
consumerIds: string[];
mapSsrcConsumerId: { key: number; value: string }[];
Expand Down Expand Up @@ -258,6 +262,15 @@ export interface Transport<
*/
get closed(): boolean;

/**
* Transport type.
*
* @virtual
* @privateRemarks
* - It's marked as virtual getter since each Transport class overrides it.
*/
get type(): TransportType;

/**
* App custom data.
*/
Expand Down
4 changes: 4 additions & 0 deletions node/src/WebRtcTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ export class WebRtcTransportImpl<
this.handleListenerError();
}

get type(): 'webrtc' {
return 'webrtc';
}

get observer(): WebRtcTransportObserver {
return super.observer;
}
Expand Down
7 changes: 7 additions & 0 deletions node/src/WebRtcTransportTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ export interface WebRtcTransport<
WebRtcTransportEvents,
WebRtcTransportObserver
> {
/**
* Transport type.
*
* @override
*/
get type(): 'webrtc';

/**
* Observer.
*
Expand Down
2 changes: 1 addition & 1 deletion node/src/test/test-DirectTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ test('router.createDirectTransport() succeeds', async () => {
expect(onObserverNewTransport).toHaveBeenCalledTimes(1);
expect(onObserverNewTransport).toHaveBeenCalledWith(directTransport);
expect(typeof directTransport.id).toBe('string');
expect(directTransport.type).toBe('direct');
expect(directTransport.closed).toBe(false);
expect(directTransport.appData).toEqual({ foo: 'bar' });

const dump = await directTransport.dump();

expect(dump.id).toBe(directTransport.id);
expect(dump.direct).toBe(true);
expect(dump.producerIds).toEqual([]);
expect(dump.consumerIds).toEqual([]);
expect(dump.dataProducerIds).toEqual([]);
Expand Down
2 changes: 2 additions & 0 deletions node/src/test/test-PipeTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ test('router.createPipeTransport() with enableRtx succeeds', async () => {
enableRtx: true,
});

expect(pipeTransport.type).toBe('pipe');

const pipeConsumer = await pipeTransport.consume({
producerId: ctx.videoProducer!.id,
});
Expand Down
5 changes: 2 additions & 3 deletions node/src/test/test-PlainTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ test('router.createPlainTransport() succeeds', async () => {
expect(onObserverNewTransport).toHaveBeenCalledWith(plainTransport2);
expect(typeof plainTransport2.id).toBe('string');
expect(plainTransport2.closed).toBe(false);
expect(plainTransport2.type).toBe('plain');
expect(plainTransport2.appData).toEqual({ foo: 'bar' });
expect(typeof plainTransport2.tuple).toBe('object');
// @deprecated Use tuple.localAddress instead.
Expand All @@ -111,7 +112,6 @@ test('router.createPlainTransport() succeeds', async () => {
const dump1 = await plainTransport2.dump();

expect(dump1.id).toBe(plainTransport2.id);
expect(dump1.direct).toBe(false);
expect(dump1.producerIds).toEqual([]);
expect(dump1.consumerIds).toEqual([]);
expect(dump1.tuple).toEqual(plainTransport2.tuple);
Expand Down Expand Up @@ -166,7 +166,6 @@ test('router.createPlainTransport() succeeds', async () => {
const dump2 = await transport2.dump();

expect(dump2.id).toBe(transport2.id);
expect(dump2.direct).toBe(false);
expect(dump2.tuple).toEqual(transport2.tuple);
expect(dump2.rtcpTuple).toEqual(transport2.rtcpTuple);
expect(dump2.sctpState).toBeUndefined();
Expand Down Expand Up @@ -196,7 +195,7 @@ test('router.createPlainTransport() with wrong arguments rejects with TypeError'
).rejects.toThrow(TypeError);

await expect(
ctx.router!.createPipeTransport({
ctx.router!.createPlainTransport({
listenInfo: { protocol: 'tcp', ip: '127.0.0.1' },
})
).rejects.toThrow(TypeError);
Expand Down
2 changes: 1 addition & 1 deletion node/src/test/test-WebRtcTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ test('router.createWebRtcTransport() succeeds', async () => {
expect(onObserverNewTransport).toHaveBeenCalledWith(webRtcTransport);
expect(typeof webRtcTransport.id).toBe('string');
expect(webRtcTransport.closed).toBe(false);
expect(webRtcTransport.type).toBe('webrtc');
expect(webRtcTransport.appData).toEqual({ foo: 'bar' });
expect(webRtcTransport.iceRole).toBe('controlled');
expect(typeof webRtcTransport.iceParameters).toBe('object');
Expand Down Expand Up @@ -184,7 +185,6 @@ test('router.createWebRtcTransport() succeeds', async () => {
const dump = await webRtcTransport.dump();

expect(dump.id).toBe(webRtcTransport.id);
expect(dump.direct).toBe(false);
expect(dump.producerIds).toEqual([]);
expect(dump.consumerIds).toEqual([]);
expect(dump.iceRole).toBe(webRtcTransport.iceRole);
Expand Down

0 comments on commit aa5853c

Please sign in to comment.