Skip to content

Commit

Permalink
[flatbuffers] Improve Node imports (#1193)
Browse files Browse the repository at this point in the history
- Add `node:` prefix to all built-in Node imports (replaces PR #1142).
- Replace `uuid` package with built-in `crypto.randomUUID()`(replaces PR #1140).
- Cosmetic changes in TS files:
  - Avoid unnecessary `const { createWorker } = mediasoup` in almost all tests.
  - Move all FBS related imports to the bottom.
  - Some formating fixes that bypassed our ESLint rules.
  • Loading branch information
ibc authored Oct 25, 2023
1 parent b4c80d8 commit 53dbfce
Show file tree
Hide file tree
Showing 31 changed files with 118 additions and 166 deletions.
9 changes: 5 additions & 4 deletions node/src/Channel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as os from 'os';
import { Duplex } from 'stream';
import * as os from 'node:os';
import { Duplex } from 'node:stream';
import * as flatbuffers from 'flatbuffers';
import { Logger } from './Logger';
import { EnhancedEventEmitter } from './EnhancedEventEmitter';
Expand All @@ -10,7 +10,8 @@ import { Message, Type as MessageType, Body as MessageBody } from './fbs/message
import { Notification, Body as NotificationBody, Event } from './fbs/notification';
import { Log } from './fbs/log';

const littleEndian = os.endianness() == 'LE';
const IS_LITTLE_ENDIAN = os.endianness() === 'LE';

const logger = new Logger('Channel');

type Sent =
Expand Down Expand Up @@ -110,7 +111,7 @@ export class Channel extends EnhancedEventEmitter
const dataView = new DataView(
this.#recvBuffer.buffer,
this.#recvBuffer.byteOffset + msgStart);
const msgLen = dataView.getUint32(0, littleEndian);
const msgLen = dataView.getUint32(0, IS_LITTLE_ENDIAN);

if (readLen < 4 + msgLen)
{
Expand Down
2 changes: 1 addition & 1 deletion node/src/DataConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { Channel } from './Channel';
import { TransportInternal } from './Transport';
import { parseSctpStreamParameters, SctpStreamParameters } from './SctpParameters';
import { AppData } from './types';
import * as utils from './utils';
import { Event, Notification } from './fbs/notification';
import * as FbsTransport from './fbs/transport';
import * as FbsRequest from './fbs/request';
import * as FbsDataConsumer from './fbs/data-consumer';
import * as FbsDataProducer from './fbs/data-producer';
import * as utils from './utils';

export type DataConsumerOptions<DataConsumerAppData extends AppData = AppData> =
{
Expand Down
2 changes: 1 addition & 1 deletion node/src/EnhancedEventEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventEmitter } from 'events';
import { EventEmitter } from 'node:events';
import { Logger } from './Logger';

const logger = new Logger('EnhancedEventEmitter');
Expand Down
4 changes: 2 additions & 2 deletions node/src/PipeTransport.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { v4 as uuidv4 } from 'uuid';
import * as flatbuffers from 'flatbuffers';
import { Logger } from './Logger';
import * as ortc from './ortc';
Expand Down Expand Up @@ -29,6 +28,7 @@ import {
SrtpParameters
} from './SrtpParameters';
import { AppData, Either } from './types';
import { generateUUIDv4 } from './utils';
import { MediaKind as FbsMediaKind } from './fbs/rtp-parameters/media-kind';
import * as FbsRtpParameters from './fbs/rtp-parameters';
import { Event, Notification } from './fbs/notification';
Expand Down Expand Up @@ -368,7 +368,7 @@ export class PipeTransport<PipeTransportAppData extends AppData = AppData>
}
);

const consumerId = uuidv4();
const consumerId = generateUUIDv4();

const consumeRequestOffset = createConsumeRequest({
builder : this.channel.bufferBuilder,
Expand Down
16 changes: 8 additions & 8 deletions node/src/Router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { v4 as uuidv4 } from 'uuid';
import { Logger } from './Logger';
import { EnhancedEventEmitter } from './EnhancedEventEmitter';
import * as ortc from './ortc';
Expand All @@ -22,9 +21,10 @@ import { RtpObserver } from './RtpObserver';
import { ActiveSpeakerObserver, ActiveSpeakerObserverOptions } from './ActiveSpeakerObserver';
import { AudioLevelObserver, AudioLevelObserverOptions } from './AudioLevelObserver';
import { RtpCapabilities, RtpCodecCapability } from './RtpParameters';
import { cryptoSuiteToFbs } from './SrtpParameters';
import { NumSctpStreams } from './SctpParameters';
import { AppData, Either } from './types';
import { cryptoSuiteToFbs } from './SrtpParameters';
import { generateUUIDv4 } from './utils';
import * as FbsActiveSpeakerObserver from './fbs/active-speaker-observer';
import * as FbsAudioLevelObserver from './fbs/audio-level-observer';
import * as FbsRequest from './fbs/request';
Expand Down Expand Up @@ -507,7 +507,7 @@ export class Router<RouterAppData extends AppData = AppData>
}
}

const transportId = uuidv4();
const transportId = generateUUIDv4();

/* Build Request. */
let webRtcTransportListenServer:
Expand Down Expand Up @@ -689,7 +689,7 @@ export class Router<RouterAppData extends AppData = AppData>
};
}

const transportId = uuidv4();
const transportId = generateUUIDv4();

/* Build Request. */
const baseTransportOptions = new FbsTransport.OptionsT(
Expand Down Expand Up @@ -837,7 +837,7 @@ export class Router<RouterAppData extends AppData = AppData>
};
}

const transportId = uuidv4();
const transportId = generateUUIDv4();

/* Build Request. */
const baseTransportOptions = new FbsTransport.OptionsT(
Expand Down Expand Up @@ -946,7 +946,7 @@ export class Router<RouterAppData extends AppData = AppData>
throw new TypeError('if given, appData must be an object');
}

const transportId = uuidv4();
const transportId = generateUUIDv4();

/* Build Request. */
const baseTransportOptions = new FbsTransport.OptionsT(
Expand Down Expand Up @@ -1404,7 +1404,7 @@ export class Router<RouterAppData extends AppData = AppData>
throw new TypeError('if given, appData must be an object');
}

const rtpObserverId = uuidv4();
const rtpObserverId = generateUUIDv4();

/* Build Request. */
const activeRtpObserverOptions =
Expand Down Expand Up @@ -1482,7 +1482,7 @@ export class Router<RouterAppData extends AppData = AppData>
throw new TypeError('if given, appData must be an object');
}

const rtpObserverId = uuidv4();
const rtpObserverId = generateUUIDv4();

/* Build Request. */
const audioLevelObserverOptions =
Expand Down
11 changes: 7 additions & 4 deletions node/src/RtpStream.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as FbsRtpStream from './fbs/rtp-stream';
import * as FbsRtpParameters from './fbs/rtp-parameters';

export type RtpStreamRecvStats = BaseRtpStreamStats & {
export type RtpStreamRecvStats = BaseRtpStreamStats &
{
type: string;
jitter: number;
packetCount: number;
Expand All @@ -10,14 +11,16 @@ export type RtpStreamRecvStats = BaseRtpStreamStats & {
bitrateByLayer?: any;
};

export type RtpStreamSendStats = BaseRtpStreamStats & {
export type RtpStreamSendStats = BaseRtpStreamStats &
{
type: string;
packetCount: number;
byteCount: number;
bitrate: number;
};

type BaseRtpStreamStats = {
type BaseRtpStreamStats =
{
timestamp: number;
ssrc: number;
rtxSsrc?: number;
Expand Down Expand Up @@ -128,7 +131,7 @@ function parseBitrateByLayer(binary: FbsRtpStream.RecvStats): any

const bitRateByLayer: {[key: string]: number} = {};

for (let i=0; i < binary.bitrateByLayerLength(); ++i)
for (let i = 0; i < binary.bitrateByLayerLength(); ++i)
{
const layer: string = binary.bitrateByLayer(i)!.layer()!;
const bitrate = binary.bitrateByLayer(i)!.bitrate();
Expand Down
20 changes: 10 additions & 10 deletions node/src/Transport.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { v4 as uuidv4 } from 'uuid';
import * as flatbuffers from 'flatbuffers';
import { Logger } from './Logger';
import { EnhancedEventEmitter } from './EnhancedEventEmitter';
import * as utils from './utils';
import * as ortc from './ortc';
import { Channel } from './Channel';
import { RouterInternal } from './Router';
Expand Down Expand Up @@ -40,6 +38,7 @@ import {
SctpStreamParameters
} from './SctpParameters';
import { AppData } from './types';
import * as utils from './utils';
import { TraceDirection as FbsTraceDirection } from './fbs/common';
import * as FbsRequest from './fbs/request';
import { MediaKind as FbsMediaKind } from './fbs/rtp-parameters/media-kind';
Expand Down Expand Up @@ -251,7 +250,8 @@ type SctpListenerDump =
streamIdTable : {key: number; value: string}[];
};

type RecvRtpHeaderExtensions = {
type RecvRtpHeaderExtensions =
{
mid?: number;
rid?: number;
rrid?: number;
Expand Down Expand Up @@ -736,11 +736,11 @@ export class Transport
{
this.#cnameForProducers = rtpParameters.rtcp.cname;
}
// Otherwise if we don't have yet a CNAME for Producers and the RTP parameters
// do not include CNAME, create a random one.
// Otherwise if we don't have yet a CNAME for Producers and the RTP
// parameters do not include CNAME, create a random one.
else if (!this.#cnameForProducers)
{
this.#cnameForProducers = uuidv4().substr(0, 8);
this.#cnameForProducers = utils.generateUUIDv4().substr(0, 8);
}

// Override Producer's CNAME.
Expand All @@ -758,7 +758,7 @@ export class Transport
const consumableRtpParameters = ortc.getConsumableRtpParameters(
kind, rtpParameters, routerRtpCapabilities, rtpMapping);

const producerId = id || uuidv4();
const producerId = id || utils.generateUUIDv4();
const requestOffset = createProduceRequest({
builder : this.channel.bufferBuilder,
producerId,
Expand Down Expand Up @@ -901,7 +901,7 @@ export class Transport
}
}

const consumerId = uuidv4();
const consumerId = utils.generateUUIDv4();
const requestOffset = createConsumeRequest({
builder : this.channel.bufferBuilder,
producer,
Expand Down Expand Up @@ -1015,7 +1015,7 @@ export class Transport
}
}

const dataProducerId = id || uuidv4();
const dataProducerId = id || utils.generateUUIDv4();
const requestOffset = createProduceDataRequest({
builder : this.channel.bufferBuilder,
dataProducerId,
Expand Down Expand Up @@ -1158,7 +1158,7 @@ export class Transport
}

const { label, protocol } = dataProducer;
const dataConsumerId = uuidv4();
const dataConsumerId = utils.generateUUIDv4();

const requestOffset = createConsumeDataRequest({
builder : this.channel.bufferBuilder,
Expand Down
12 changes: 6 additions & 6 deletions node/src/Worker.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as process from 'process';
import * as path from 'path';
import { spawn, ChildProcess } from 'child_process';
import { v4 as uuidv4 } from 'uuid';
import * as process from 'node:process';
import * as path from 'node:path';
import { spawn, ChildProcess } from 'node:child_process';
import { Logger } from './Logger';
import { EnhancedEventEmitter } from './EnhancedEventEmitter';
import * as ortc from './ortc';
import { Channel } from './Channel';
import { Router, RouterOptions } from './Router';
import { WebRtcServer, WebRtcServerOptions } from './WebRtcServer';
import { AppData } from './types';
import { generateUUIDv4 } from './utils';
import { Event } from './fbs/notification';
import * as FbsRequest from './fbs/request';
import * as FbsWorker from './fbs/worker';
Expand Down Expand Up @@ -695,7 +695,7 @@ export class Worker<WorkerAppData extends AppData = AppData>
);
}

const webRtcServerId = uuidv4();
const webRtcServerId = generateUUIDv4();

const createWebRtcServerRequestOffset = new FbsWorker.CreateWebRtcServerRequestT(
webRtcServerId, fbsListenInfos
Expand Down Expand Up @@ -742,7 +742,7 @@ export class Worker<WorkerAppData extends AppData = AppData>
// This may throw.
const rtpCapabilities = ortc.generateRouterRtpCapabilities(mediaCodecs);

const routerId = uuidv4();
const routerId = generateUUIDv4();

// Get flatbuffer builder.
const createRouterRequestOffset =
Expand Down
3 changes: 1 addition & 2 deletions node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import * as utils from './utils';
import { supportedRtpCapabilities } from './supportedRtpCapabilities';
import { RtpCapabilities } from './RtpParameters';
import * as types from './types';
import { AppData } from './types';

/**
* Expose all types.
Expand Down Expand Up @@ -44,7 +43,7 @@ export { workerBin };
/**
* Create a Worker.
*/
export async function createWorker<WorkerAppData extends AppData = AppData>(
export async function createWorker<WorkerAppData extends types.AppData = types.AppData>(
{
logLevel = 'error',
logTags,
Expand Down
4 changes: 2 additions & 2 deletions node/src/ortc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as h264 from 'h264-profile-level-id';
import * as flatbuffers from 'flatbuffers';
import * as utils from './utils';
import { UnsupportedError } from './errors';
import { supportedRtpCapabilities } from './supportedRtpCapabilities';
import { parse as parseScalabilityMode } from './scalabilityModes';
import {
Expand All @@ -22,6 +20,8 @@ import {
SctpParameters,
SctpStreamParameters
} from './SctpParameters';
import * as utils from './utils';
import { UnsupportedError } from './errors';
import * as FbsRtpParameters from './fbs/rtp-parameters';

export type RtpMapping =
Expand Down
4 changes: 1 addition & 3 deletions node/src/tests/test-ActiveSpeakerObserver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as mediasoup from '../';

const { createWorker } = mediasoup;

let worker: mediasoup.types.Worker;
let router: mediasoup.types.Router;
let activeSpeakerObserver: mediasoup.types.ActiveSpeakerObserver;
Expand All @@ -23,7 +21,7 @@ const mediaCodecs: mediasoup.types.RtpCodecCapability[] =

beforeAll(async () =>
{
worker = await createWorker();
worker = await mediasoup.createWorker();
router = await worker.createRouter({ mediaCodecs });
});

Expand Down
4 changes: 1 addition & 3 deletions node/src/tests/test-AudioLevelObserver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as mediasoup from '../';

const { createWorker } = mediasoup;

let worker: mediasoup.types.Worker;
let router: mediasoup.types.Router;
let audioLevelObserver: mediasoup.types.AudioLevelObserver;
Expand All @@ -23,7 +21,7 @@ const mediaCodecs: mediasoup.types.RtpCodecCapability[] =

beforeAll(async () =>
{
worker = await createWorker();
worker = await mediasoup.createWorker();
router = await worker.createRouter({ mediaCodecs });
});

Expand Down
4 changes: 1 addition & 3 deletions node/src/tests/test-Consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { UnsupportedError } from '../errors';
import { Notification, Body as NotificationBody, Event } from '../fbs/notification';
import * as FbsConsumer from '../fbs/consumer';

const { createWorker } = mediasoup;

let worker: mediasoup.types.Worker;
let router: mediasoup.types.Router;
let transport1: mediasoup.types.WebRtcTransport;
Expand Down Expand Up @@ -247,7 +245,7 @@ const consumerDeviceCapabilities: mediasoup.types.RtpCapabilities =

beforeAll(async () =>
{
worker = await createWorker();
worker = await mediasoup.createWorker();
router = await worker.createRouter({ mediaCodecs });
transport1 = await router.createWebRtcTransport(
{
Expand Down
4 changes: 1 addition & 3 deletions node/src/tests/test-DataConsumer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as mediasoup from '../';

const { createWorker } = mediasoup;

let worker: mediasoup.types.Worker;
let router: mediasoup.types.Router;
let transport1: mediasoup.types.WebRtcTransport;
Expand All @@ -25,7 +23,7 @@ const dataProducerParameters: mediasoup.types.DataProducerOptions =

beforeAll(async () =>
{
worker = await createWorker();
worker = await mediasoup.createWorker();
router = await worker.createRouter();
transport1 = await router.createWebRtcTransport(
{
Expand Down
Loading

0 comments on commit 53dbfce

Please sign in to comment.