@libp2p/upnp-nat 3.0.5 → 3.1.0-34b3c14b8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +40 -0
  2. package/dist/src/check-external-address.d.ts.map +1 -1
  3. package/dist/src/check-external-address.js +0 -2
  4. package/dist/src/check-external-address.js.map +1 -1
  5. package/dist/src/constants.d.ts +4 -0
  6. package/dist/src/constants.d.ts.map +1 -1
  7. package/dist/src/constants.js +4 -0
  8. package/dist/src/constants.js.map +1 -1
  9. package/dist/src/index.d.ts +125 -0
  10. package/dist/src/index.d.ts.map +1 -1
  11. package/dist/src/index.js +40 -0
  12. package/dist/src/index.js.map +1 -1
  13. package/dist/src/search-gateway-finder.d.ts +27 -0
  14. package/dist/src/search-gateway-finder.d.ts.map +1 -0
  15. package/dist/src/search-gateway-finder.js +66 -0
  16. package/dist/src/search-gateway-finder.js.map +1 -0
  17. package/dist/src/static-gateway-finder.d.ts +23 -0
  18. package/dist/src/static-gateway-finder.d.ts.map +1 -0
  19. package/dist/src/static-gateway-finder.js +42 -0
  20. package/dist/src/static-gateway-finder.js.map +1 -0
  21. package/dist/src/upnp-nat.d.ts +6 -1
  22. package/dist/src/upnp-nat.d.ts.map +1 -1
  23. package/dist/src/upnp-nat.js +20 -5
  24. package/dist/src/upnp-nat.js.map +1 -1
  25. package/dist/src/upnp-port-mapper.d.ts +1 -0
  26. package/dist/src/upnp-port-mapper.d.ts.map +1 -1
  27. package/dist/src/upnp-port-mapper.js +15 -10
  28. package/dist/src/upnp-port-mapper.js.map +1 -1
  29. package/package.json +7 -7
  30. package/src/check-external-address.ts +0 -2
  31. package/src/constants.ts +5 -0
  32. package/src/index.ts +133 -0
  33. package/src/search-gateway-finder.ts +94 -0
  34. package/src/static-gateway-finder.ts +60 -0
  35. package/src/upnp-nat.ts +28 -6
  36. package/src/upnp-port-mapper.ts +16 -10
  37. package/dist/src/gateway-finder.d.ts +0 -26
  38. package/dist/src/gateway-finder.d.ts.map +0 -1
  39. package/dist/src/gateway-finder.js +0 -53
  40. package/dist/src/gateway-finder.js.map +0 -1
  41. package/dist/typedoc-urls.json +0 -12
  42. package/src/gateway-finder.ts +0 -75
@@ -1,75 +0,0 @@
1
- import { TypedEventEmitter, start, stop } from '@libp2p/interface'
2
- import { repeatingTask } from '@libp2p/utils/repeating-task'
3
- import { DEFAULT_GATEWAY_SEARCH_INTERVAL, DEFAULT_GATEWAY_SEARCH_TIMEOUT } from './constants.js'
4
- import type { Gateway, UPnPNAT } from '@achingbrain/nat-port-mapper'
5
- import type { ComponentLogger, Logger } from '@libp2p/interface'
6
- import type { RepeatingTask } from '@libp2p/utils/repeating-task'
7
-
8
- export interface GatewayFinderComponents {
9
- logger: ComponentLogger
10
- }
11
-
12
- export interface GatewayFinderInit {
13
- portMappingClient: UPnPNAT
14
- }
15
-
16
- export interface GatewayFinderEvents {
17
- 'gateway': CustomEvent<Gateway>
18
- }
19
-
20
- export class GatewayFinder extends TypedEventEmitter<GatewayFinderEvents> {
21
- private readonly log: Logger
22
- private readonly gateways: Gateway[]
23
- private readonly findGateways: RepeatingTask
24
- private readonly portMappingClient: UPnPNAT
25
- private started: boolean
26
-
27
- constructor (components: GatewayFinderComponents, init: GatewayFinderInit) {
28
- super()
29
-
30
- this.log = components.logger.forComponent('libp2p:upnp-nat')
31
- this.portMappingClient = init.portMappingClient
32
- this.started = false
33
- this.gateways = []
34
-
35
- // every five minutes, search for network gateways for one minute
36
- this.findGateways = repeatingTask(async (options) => {
37
- for await (const gateway of this.portMappingClient.findGateways({
38
- ...options,
39
- searchInterval: 10000
40
- })) {
41
- if (this.gateways.some(g => {
42
- return g.id === gateway.id && g.family === gateway.family
43
- })) {
44
- // already seen this gateway
45
- continue
46
- }
47
-
48
- this.gateways.push(gateway)
49
- this.safeDispatchEvent('gateway', {
50
- detail: gateway
51
- })
52
- }
53
- }, DEFAULT_GATEWAY_SEARCH_INTERVAL, {
54
- runImmediately: true,
55
- timeout: DEFAULT_GATEWAY_SEARCH_TIMEOUT
56
- })
57
- }
58
-
59
- async start (): Promise<void> {
60
- if (this.started) {
61
- return
62
- }
63
-
64
- this.started = true
65
- await start(this.findGateways)
66
- }
67
-
68
- /**
69
- * Stops the NAT manager
70
- */
71
- async stop (): Promise<void> {
72
- await stop(this.findGateways)
73
- this.started = false
74
- }
75
- }