Skip to content

Commit 424cb20

Browse files
authored
Node: Expose Index interface (#1485)
1 parent cef8391 commit 424cb20

File tree

4 files changed

+62
-25
lines changed

4 files changed

+62
-25
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### NEXT
44

5+
- Node: Expose `Index` interface in `types.indexTypes` or via `import { Index as MediasoupIndex } from 'mediasoup/lib/indexTypes'` ([PR #1485](https://github.com/versatica/mediasoup/pull/1485)).
6+
57
### 3.15.2
68

79
- `Worker`: Fix crash when using colliding `portRange` values in different transports ([PR #1469](https://github.com/versatica/mediasoup/pull/1469)).

node/src/index.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
import { Logger, LoggerEmitter } from './Logger';
22
import { EnhancedEventEmitter } from './enhancedEvents';
3+
import type {
4+
Observer,
5+
ObserverEvents,
6+
LogEventListeners,
7+
Index,
8+
} from './indexTypes';
39
import type { Worker, WorkerSettings } from './WorkerTypes';
410
import { WorkerImpl, workerBin } from './Worker';
511
import { supportedRtpCapabilities } from './supportedRtpCapabilities';
612
import type { RtpCapabilities } from './rtpParametersTypes';
7-
import type * as types from './types';
13+
import { parseScalabilityMode } from './scalabilityModesUtils';
14+
import type { AppData } from './types';
815
import * as utils from './utils';
916

1017
/**
1118
* Expose all types.
1219
*/
13-
export { types };
20+
export type * as types from './types';
1421

1522
/**
1623
* Expose mediasoup version.
1724
*/
1825
// eslint-disable-next-line @typescript-eslint/no-require-imports
1926
export const version: string = require('../../package.json').version;
2027

21-
export type Observer = EnhancedEventEmitter<ObserverEvents>;
22-
23-
export type ObserverEvents = {
24-
newworker: [Worker];
25-
};
26-
2728
const observer: Observer = new EnhancedEventEmitter<ObserverEvents>();
2829

2930
/**
@@ -59,9 +60,7 @@ const logger = new Logger();
5960
* });
6061
* ```
6162
*/
62-
export function setLogEventListeners(
63-
listeners?: types.LogEventListeners
64-
): void {
63+
export function setLogEventListeners(listeners?: LogEventListeners): void {
6564
logger.debug('setLogEventListeners()');
6665

6766
let debugLogEmitter: LoggerEmitter | undefined;
@@ -92,9 +91,7 @@ export function setLogEventListeners(
9291
/**
9392
* Create a Worker.
9493
*/
95-
export async function createWorker<
96-
WorkerAppData extends types.AppData = types.AppData,
97-
>({
94+
export async function createWorker<WorkerAppData extends AppData = AppData>({
9895
logLevel = 'error',
9996
logTags,
10097
rtcMinPort = 10000,
@@ -145,9 +142,24 @@ export function getSupportedRtpCapabilities(): RtpCapabilities {
145142
/**
146143
* Expose parseScalabilityMode() function.
147144
*/
148-
export { parseScalabilityMode } from './scalabilityModesUtils';
145+
export { parseScalabilityMode };
149146

150147
/**
151148
* Expose extras module.
152149
*/
153150
export * as extras from './extras';
151+
152+
// NOTE: This constant of type Index is created just to check at TypeScript
153+
// level that everything exported here (all but TS types) matches the Index
154+
// interface exposed by indexTypes.ts.
155+
//
156+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
157+
const indexImpl: Index = {
158+
version,
159+
observer,
160+
workerBin,
161+
setLogEventListeners,
162+
createWorker,
163+
getSupportedRtpCapabilities,
164+
parseScalabilityMode,
165+
};

node/src/indexTypes.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type { EnhancedEventEmitter } from './enhancedEvents';
2+
import type { Worker, WorkerSettings } from './WorkerTypes';
3+
import type { RtpCapabilities } from './rtpParametersTypes';
4+
import type { parseScalabilityMode } from './scalabilityModesUtils';
5+
import type { AppData } from './types';
6+
7+
export type ObserverEvents = {
8+
newworker: [Worker];
9+
};
10+
11+
export type Observer = EnhancedEventEmitter<ObserverEvents>;
12+
13+
/**
14+
* Event listeners for mediasoup generated logs.
15+
*/
16+
export type LogEventListeners = {
17+
ondebug?: (namespace: string, log: string) => void;
18+
onwarn?: (namespace: string, log: string) => void;
19+
onerror?: (namespace: string, log: string, error?: Error) => void;
20+
};
21+
22+
export interface Index {
23+
version: string;
24+
observer: EnhancedEventEmitter<ObserverEvents>;
25+
workerBin: string;
26+
setLogEventListeners: (listeners?: LogEventListeners) => void;
27+
createWorker: <WorkerAppData extends AppData = AppData>(
28+
options?: WorkerSettings<WorkerAppData>
29+
) => Promise<Worker<WorkerAppData>>;
30+
getSupportedRtpCapabilities: () => RtpCapabilities;
31+
parseScalabilityMode: typeof parseScalabilityMode;
32+
}

node/src/types.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type { Observer, ObserverEvents } from './index';
1+
export type * from './indexTypes';
22
export type * from './WorkerTypes';
33
export type * from './WebRtcServerTypes';
44
export type * from './RouterTypes';
@@ -32,12 +32,3 @@ export type Either<T, U> = Only<T, U> | Only<U, T>;
3232
export type AppData = {
3333
[key: string]: unknown;
3434
};
35-
36-
/**
37-
* Event listeners for mediasoup generated logs.
38-
*/
39-
export type LogEventListeners = {
40-
ondebug?: (namespace: string, log: string) => void;
41-
onwarn?: (namespace: string, log: string) => void;
42-
onerror?: (namespace: string, log: string, error?: Error) => void;
43-
};

0 commit comments

Comments
 (0)