Skip to content

Commit

Permalink
Only use import-sync when ESM module require is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
pimterry committed Nov 6, 2024
1 parent 590bc97 commit 29be6bd
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/webrtc/rtc-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ import {
Direction as NDCDirection
} from 'node-datachannel';

// Node-DataChannel is now an ES6 module, but we're not - work around
// that with a synchronous import wrapper:
const importSync = require('import-sync');
const NodeDataChannel = importSync('node-datachannel') as
typeof import('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 type { MockRTCSessionDescription } from '../mockrtc';
import {
Expand Down

0 comments on commit 29be6bd

Please sign in to comment.