Skip to content

Commit 8b98401

Browse files
committed
fix: convert enums to types
In #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 #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 #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 #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', { // ... }) ```
1 parent b0caa62 commit 8b98401

File tree

1 file changed

+3
-19
lines changed

1 file changed

+3
-19
lines changed

src/lib/types.ts

+3-19
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,7 @@ export interface ProxyServer {
4747
password?: string;
4848
}
4949

50-
export const enum RelayType {
51-
TurnUdp = 'TurnUdp',
52-
TurnTcp = 'TurnTcp',
53-
TurnTls = 'TurnTls',
54-
}
50+
export type RelayType = 'TurnUdp' | 'TurnTcp' | 'TurnTls'
5551

5652
export interface IceServer {
5753
hostname: string;
@@ -80,13 +76,7 @@ export interface RtcConfig {
8076
}
8177

8278
// Lowercase to match the description type string from libdatachannel
83-
export enum DescriptionType {
84-
Unspec = 'unspec',
85-
Offer = 'offer',
86-
Answer = 'answer',
87-
Pranswer = 'pranswer',
88-
Rollback = 'rollback',
89-
}
79+
export type DescriptionType = 'unspec' | 'offer' | 'answer' | 'pranswer' | 'rollback'
9080

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

@@ -118,10 +108,4 @@ export interface SelectedCandidateInfo {
118108
}
119109

120110
// Must be same as rtc enum class Direction
121-
export const enum Direction {
122-
SendOnly = 'SendOnly',
123-
RecvOnly = 'RecvOnly',
124-
SendRecv = 'SendRecv',
125-
Inactive = 'Inactive',
126-
Unknown = 'Unknown',
127-
}
111+
export type Direction = 'SendOnly' | 'RecvOnly' | 'SendRecv' | 'Inactive' | 'Unknown'

0 commit comments

Comments
 (0)