Transport.ts has a too restricitive type-definition
sashabjorkman opened this issue · comments
Bug Report
Your environment
- mediasoup-client version: 3.6.82
Issue description
IceCandidate
is currently defined to have a property called tcpType
having one of three string literal values. The mediasoup Rust library (when used with serde) however creates an IceCandidate object which does not contain tcpType
if the candidate is a UDP candidate (the library is in perfect accordance with the WebRTC spec in this regard). When creating the signaling code for my project, I noticed that this inconsistency between mediasoup and mediasoup-client is rather annoying. The problem boils down to mediasoup-client always expecting there to be a tcpType
. As such it is impossible to satisfy both the JSON validator (Zod in my instance) and the type-system.
Possible solutions:
A) define IceCandidate as a union type between IceCandidateGeneric and IceCandidateTCP (and let IceCandidateTCP have a literal protocol: 'tcp'
and tcpType: ...
)?
B) just let tcpType be undefined (probably the best and simplest solution)
mediasoup-client/src/Transport.ts
Line 111 in b5cadb0
tcpType?: 'active' | 'passive' | 'so';
I would normally create a PR for such a case (and I am willing to do so if told what solution you like the best) but thought that the necessary change is so small that it would just be unnecessary effort for the maintainers to merge that in.
The related Zod code that doesn't play well with current type definitions:
iceCandidates: z.array(z.object({
foundation: z.string(),
priority: z.number(),
ip: z.string(),
protocol: z.enum(['udp', 'tcp']),
port: z.number(),
type: z.enum(['host', 'srflx', 'prflx', 'relay']),
tcpType: z.enum(['active', 'passive', 'so']).optional(), // Adding .optional causes issues
})),
For now, one can probably just provide a default value for tcpType in the validator and everything should work fine as tcpType
is only left empty when not using a TCP candidate.
Warm regards,
Alexander B
tcpType
is not even required in the API docs so this is obviously a bug. Fixing.
https://mediasoup.org/documentation/v3/mediasoup/api/#WebRtcTransportIceCandidate
Released in 3.6.83, thanks for reporting.