Skip to content

Commit

Permalink
fix: convert enums to types
Browse files Browse the repository at this point in the history
In murat-dogan#40 enums had `const` added to them.  [const enums](https://www.typescriptlang.org/docs/handbook/enums.html#const-enums)
are removed at compile time and replaced with numbers.

Here we require strings to be passed to `libdatachannel`, so in murat-dogan#259
we exported objects with values that correspond to the enums so consuming
code can import something they can use in `PeerConnection` method
invocations, e.g.:

```js
import { DescriptionType, PeerConnection } from 'node-datachannel'

const pc = new PeerConnection()

pc.setLocalDescription(DescriptionType.Offer, {
  // ...
})
```

In murat-dogan#278 the codebase was converted to TypeScript and a non-const
enum crept back in.  Now all the enums live in `types.ts` and since

Rollup was introduced as part of murat-dogan#278, but if you check the `esm`
and `cjs` output dirs, `tests.ts` is not being transpiled to `tests.js`
(`types/lib/types.d.ts` is present though, so `tsc` is doing it's job)
and the re-export of the exports from `tests.ts` is being removed
so enums are broken again at runtime.

The fix here is to give up on enums since they are a constant source
of pain for consumers and change them to be types:

```js
import { PeerConnection } from 'node-datachannel'

const pc = new PeerConnection()

pc.setLocalDescription('offer', {
  // ...
})
```
  • Loading branch information
achingbrain committed Jan 15, 2025
1 parent b0caa62 commit 8b98401
Showing 1 changed file with 3 additions and 19 deletions.
22 changes: 3 additions & 19 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ export interface ProxyServer {
password?: string;
}

export const enum RelayType {
TurnUdp = 'TurnUdp',
TurnTcp = 'TurnTcp',
TurnTls = 'TurnTls',
}
export type RelayType = 'TurnUdp' | 'TurnTcp' | 'TurnTls'

export interface IceServer {
hostname: string;
Expand Down Expand Up @@ -80,13 +76,7 @@ export interface RtcConfig {
}

// Lowercase to match the description type string from libdatachannel
export enum DescriptionType {
Unspec = 'unspec',
Offer = 'offer',
Answer = 'answer',
Pranswer = 'pranswer',
Rollback = 'rollback',
}
export type DescriptionType = 'unspec' | 'offer' | 'answer' | 'pranswer' | 'rollback'

export type RTCSdpType = 'answer' | 'offer' | 'pranswer' | 'rollback';

Expand Down Expand Up @@ -118,10 +108,4 @@ export interface SelectedCandidateInfo {
}

// Must be same as rtc enum class Direction
export const enum Direction {
SendOnly = 'SendOnly',
RecvOnly = 'RecvOnly',
SendRecv = 'SendRecv',
Inactive = 'Inactive',
Unknown = 'Unknown',
}
export type Direction = 'SendOnly' | 'RecvOnly' | 'SendRecv' | 'Inactive' | 'Unknown'

0 comments on commit 8b98401

Please sign in to comment.