Skip to content

Commit

Permalink
Revert to CJS version of NDC now it's available
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Jan 13, 2025
1 parent 27512ee commit 6c95232
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 26 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
"graphql": "^15.8.0",
"graphql-subscriptions": "^1.2.1",
"graphql-tag": "^2.12.6",
"import-sync": "^2.2.0",
"lodash": "^4.17.21",
"mockttp": "^3.2.1",
"node-datachannel": "^0.12.0",
Expand Down
33 changes: 8 additions & 25 deletions src/webrtc/rtc-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,7 @@ import { randomUUID } from 'crypto';
import { EventEmitter } from 'events';
import * as SDP from 'sdp-transform';

import {
PeerConnection as NDCPeerConnection,
Track as NDCTrack,
DataChannel as NDCDataChannel,
Direction as NDCDirection
} from 'node-datachannel';

// Node-DataChannel is now an ES6 module, but we're not. In all but the
// latest node, that breaks require(). Work around that with a synchronous
// import wrapper if required:
let NodeDataChannel: typeof import('node-datachannel');
try {
NodeDataChannel = require('node-datachannel');
} catch (e) {
const importSync = require('import-sync');
NodeDataChannel = importSync('node-datachannel') as
typeof import('node-datachannel');
}
import * as NodeDataChannel from 'node-datachannel';

import type { MockRTCSessionDescription } from '../mockrtc';
import {
Expand Down Expand Up @@ -53,7 +36,7 @@ export class RTCConnection extends EventEmitter {

// Set to null when the connection is closed, as otherwise calling any method (including checking
// the connection state) will segfault the process.
private rawConn: NDCPeerConnection | null
private rawConn: NodeDataChannel.PeerConnection | null
= new NodeDataChannel.PeerConnection("MockRTCConnection", { iceServers: [], forceMediaTransport: true });

private remoteDescription: RTCSessionDescriptionInit & ParsedSDP | undefined;
Expand Down Expand Up @@ -111,7 +94,7 @@ export class RTCConnection extends EventEmitter {
this.trackNewChannel(channel, { isLocal: false });
});

this.rawConn!.onTrack((track: NDCTrack) => {
this.rawConn!.onTrack((track: NodeDataChannel.Track) => {
if (!this.rawConn) return; // https://github.com/murat-dogan/node-datachannel/issues/103

this.trackNewMediaTrack(track, { isLocal: false });
Expand Down Expand Up @@ -140,7 +123,7 @@ export class RTCConnection extends EventEmitter {
return this.trackNewChannel(channel, { isLocal: true });
}

protected trackNewChannel(channel: NDCDataChannel, options: { isLocal: boolean }) {
protected trackNewChannel(channel: NodeDataChannel.DataChannel, options: { isLocal: boolean }) {
const channelStream = new DataChannelStream(channel);
this.trackedChannels.push({ stream: channelStream, isLocal: options.isLocal });

Expand All @@ -165,7 +148,7 @@ export class RTCConnection extends EventEmitter {
return channelStream;
}

protected trackNewMediaTrack(track: NDCTrack, options: { isLocal: boolean }) {
protected trackNewMediaTrack(track: NodeDataChannel.Track, options: { isLocal: boolean }) {
const trackStream = new MediaTrackStream(track);
this.trackedMediaTracks.push({ stream: trackStream, isLocal: options.isLocal });

Expand Down Expand Up @@ -211,7 +194,7 @@ export class RTCConnection extends EventEmitter {
async buildLocalDescription(): Promise<MockRTCSessionDescription> {
if (!this.rawConn) throw new Error("Can't get local description after connection is closed");

let setupChannel: NDCDataChannel | undefined;
let setupChannel: NodeDataChannel.DataChannel | undefined;
if (this.rawConn.gatheringState() === 'new') {
// We can't create an offer until we have something to negotiate, but we don't want to
// negotiate ourselves when we don't really know what's being negotiated here. To work
Expand Down Expand Up @@ -322,7 +305,7 @@ export class RTCConnection extends EventEmitter {
this.trackNewMediaTrack(track, { isLocal: true });
});

let setupChannel: NDCDataChannel | undefined;
let setupChannel: NodeDataChannel.DataChannel | undefined;
const channelRequiredForDescription = this.rawConn.gatheringState() === 'new' &&
!mediaStreamsToMirror.length;
if (shouldMirrorDataStream || channelRequiredForDescription || options.addDataStream) {
Expand Down Expand Up @@ -454,7 +437,7 @@ export class RTCConnection extends EventEmitter {

}

function sdpDirectionToNDCDirection(direction: SDP.SharedAttributes['direction']): NDCDirection {
function sdpDirectionToNDCDirection(direction: SDP.SharedAttributes['direction']): NodeDataChannel.Direction {
if (direction === 'inactive') return NodeDataChannel.Direction.Inactive;
else if (direction?.length === 8) {
return direction[0].toUpperCase() +
Expand Down

0 comments on commit 6c95232

Please sign in to comment.