achingbrain / nat-port-mapper

:left_right_arrow: Fast port mapping with UPnP and NAT-PMP
Other
1 stars 4 forks source link

Types for PortMappingOptions are boolean, but cast to Number() #67

Closed MichaelJCole closed 1 month ago

MichaelJCole commented 7 months ago

A quick look at the pmp client has these types boolean:

https://github.com/achingbrain/nat-port-mapper/blob/master/src/pmp/index.ts#L32

export interface PortMappingOptions {
  type?: 'tcp' | 'udp'
  ttl?: number
  public?: boolean
  private?: boolean
  internal?: boolean
  external?: boolean
}

But then cast with Number() https://github.com/achingbrain/nat-port-mapper/blob/master/src/pmp/index.ts#L186

  request (op: number, obj: PortMappingOptions, deferred: DeferredPromise<any>): void {
        ...
        internal = Number(obj.private ?? obj.internal ?? 0)
        if (internal !== (internal | 0) ?? internal < 0) {
          throw new Error('the "private" port must be a whole integer >= 0')
        }

        external = Number(obj.public ?? obj.external ?? 0)
        if (external !== (external | 0) ?? external < 0) {
          throw new Error('the "public" port must be a whole integer >= 0')
        }