-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add browser polyfill to upnp-nat (#2959)
To give more helpful error messages, add a browser shim for upnp-nat, similar to `@libp2p/tcp` - without this the user gets errors about not being able to load node api modules, instead throw a specific error that tells them their configuration is invalid.
- Loading branch information
1 parent
2f5e2aa
commit d188511
Showing
3 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { serviceCapabilities, serviceDependencies } from '@libp2p/interface' | ||
import type { UPnPNATClient, UPnPNAT as UPnPNATInterface } from './index.js' | ||
|
||
export class UPnPNAT implements UPnPNATInterface { | ||
public portMappingClient: UPnPNATClient | ||
|
||
constructor () { | ||
throw new Error('UPnPNAT is not supported in browsers') | ||
} | ||
|
||
readonly [Symbol.toStringTag] = '@libp2p/upnp-nat' | ||
|
||
readonly [serviceCapabilities]: string[] = [ | ||
'@libp2p/nat-traversal' | ||
] | ||
|
||
get [serviceDependencies] (): string[] { | ||
return [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { generateKeyPair } from '@libp2p/crypto/keys' | ||
import { TypedEventEmitter } from '@libp2p/interface' | ||
import { defaultLogger } from '@libp2p/logger' | ||
import { peerIdFromPrivateKey } from '@libp2p/peer-id' | ||
import { expect } from 'aegir/chai' | ||
import { stubInterface } from 'sinon-ts' | ||
import { isBrowser, isWebWorker } from 'wherearewe' | ||
import { uPnPNAT } from '../src/index.js' | ||
import type { AddressManager } from '@libp2p/interface-internal' | ||
|
||
describe('browser non-support', () => { | ||
it('should throw in browsers', async function () { | ||
if (!isBrowser && !isWebWorker) { | ||
return this.skip() | ||
} | ||
|
||
const components = { | ||
peerId: peerIdFromPrivateKey(await generateKeyPair('Ed25519')), | ||
nodeInfo: { name: 'test', version: 'test', userAgent: 'test' }, | ||
logger: defaultLogger(), | ||
addressManager: stubInterface<AddressManager>(), | ||
events: new TypedEventEmitter() | ||
} | ||
|
||
expect(() => { | ||
uPnPNAT()(components) | ||
}).to.throw() | ||
}) | ||
}) |