@libp2p/peer-collections 5.2.9-3c8dd5bbf → 5.2.9-5214dec4a

Sign up to get free protection for your applications and to get access to all the features.
package/src/list.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { peerIdFromString } from '@libp2p/peer-id'
2
1
  import { mapIterable } from './util.js'
3
2
  import type { PeerId } from '@libp2p/interface'
4
3
 
@@ -20,23 +19,23 @@ import type { PeerId } from '@libp2p/interface'
20
19
  * ```
21
20
  */
22
21
  export class PeerList {
23
- private list: string[]
22
+ private list: PeerId[]
24
23
 
25
24
  constructor (list?: PeerList | Iterable<PeerId>) {
26
25
  this.list = []
27
26
 
28
27
  if (list != null) {
29
28
  for (const value of list) {
30
- this.list.push(value.toString())
29
+ this.list.push(value)
31
30
  }
32
31
  }
33
32
  }
34
33
 
35
34
  [Symbol.iterator] (): IterableIterator<PeerId> {
36
- return mapIterable<[number, string], PeerId>(
35
+ return mapIterable<[number, PeerId], PeerId>(
37
36
  this.list.entries(),
38
37
  (val) => {
39
- return peerIdFromString(val[1])
38
+ return val[1]
40
39
  }
41
40
  )
42
41
  }
@@ -52,26 +51,24 @@ export class PeerList {
52
51
  }
53
52
 
54
53
  entries (): IterableIterator<[number, PeerId]> {
55
- return mapIterable<[number, string], [number, PeerId]>(
54
+ return mapIterable<[number, PeerId], [number, PeerId]>(
56
55
  this.list.entries(),
57
56
  (val) => {
58
- return [val[0], peerIdFromString(val[1])]
57
+ return [val[0], val[1]]
59
58
  }
60
59
  )
61
60
  }
62
61
 
63
62
  every (predicate: (peerId: PeerId, index: number, arr: PeerList) => boolean): boolean {
64
- return this.list.every((str, index) => {
65
- return predicate(peerIdFromString(str), index, this)
63
+ return this.list.every((peerId, index) => {
64
+ return predicate(peerId, index, this)
66
65
  })
67
66
  }
68
67
 
69
68
  filter (predicate: (peerId: PeerId, index: number, arr: PeerList) => boolean): PeerList {
70
69
  const output = new PeerList()
71
70
 
72
- this.list.forEach((str, index) => {
73
- const peerId = peerIdFromString(str)
74
-
71
+ this.list.forEach((peerId, index) => {
75
72
  if (predicate(peerId, index, this)) {
76
73
  output.push(peerId)
77
74
  }
@@ -81,68 +78,68 @@ export class PeerList {
81
78
  }
82
79
 
83
80
  find (predicate: (peerId: PeerId, index: number, arr: PeerList) => boolean): PeerId | undefined {
84
- const str = this.list.find((str, index) => {
85
- return predicate(peerIdFromString(str), index, this)
81
+ const peerId = this.list.find((peerId, index) => {
82
+ return predicate(peerId, index, this)
86
83
  })
87
84
 
88
- if (str == null) {
85
+ if (peerId == null) {
89
86
  return undefined
90
87
  }
91
88
 
92
- return peerIdFromString(str)
89
+ return peerId
93
90
  }
94
91
 
95
92
  findIndex (predicate: (peerId: PeerId, index: number, arr: PeerList) => boolean): number {
96
- return this.list.findIndex((str, index) => {
97
- return predicate(peerIdFromString(str), index, this)
93
+ return this.list.findIndex((peerId, index) => {
94
+ return predicate(peerId, index, this)
98
95
  })
99
96
  }
100
97
 
101
98
  forEach (predicate: (peerId: PeerId, index: number, arr: PeerList) => void): void {
102
- this.list.forEach((str, index) => {
103
- predicate(peerIdFromString(str), index, this)
99
+ this.list.forEach((peerId, index) => {
100
+ predicate(peerId, index, this)
104
101
  })
105
102
  }
106
103
 
107
104
  includes (peerId: PeerId): boolean {
108
- return this.list.includes(peerId.toString())
105
+ return this.includes(peerId)
109
106
  }
110
107
 
111
108
  indexOf (peerId: PeerId): number {
112
- return this.list.indexOf(peerId.toString())
109
+ return this.list.findIndex(id => id.equals(peerId))
113
110
  }
114
111
 
115
112
  pop (): PeerId | undefined {
116
- const str = this.list.pop()
113
+ const peerId = this.list.pop()
117
114
 
118
- if (str == null) {
115
+ if (peerId == null) {
119
116
  return undefined
120
117
  }
121
118
 
122
- return peerIdFromString(str)
119
+ return peerId
123
120
  }
124
121
 
125
122
  push (...peerIds: PeerId[]): void {
126
123
  for (const peerId of peerIds) {
127
- this.list.push(peerId.toString())
124
+ this.list.push(peerId)
128
125
  }
129
126
  }
130
127
 
131
128
  shift (): PeerId | undefined {
132
- const str = this.list.shift()
129
+ const peerId = this.list.shift()
133
130
 
134
- if (str == null) {
131
+ if (peerId == null) {
135
132
  return undefined
136
133
  }
137
134
 
138
- return peerIdFromString(str)
135
+ return peerId
139
136
  }
140
137
 
141
138
  unshift (...peerIds: PeerId[]): number {
142
139
  let len = this.list.length
143
140
 
144
141
  for (let i = peerIds.length - 1; i > -1; i--) {
145
- len = this.list.unshift(peerIds[i].toString())
142
+ len = this.list.unshift(peerIds[i])
146
143
  }
147
144
 
148
145
  return len
package/src/map.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { peerIdFromString } from '@libp2p/peer-id'
2
1
  import { mapIterable } from './util.js'
3
2
  import type { PeerId } from '@libp2p/interface'
4
3
 
@@ -20,14 +19,14 @@ import type { PeerId } from '@libp2p/interface'
20
19
  * ```
21
20
  */
22
21
  export class PeerMap <T> {
23
- private readonly map: Map<string, T>
22
+ private readonly map: Map<string, { key: PeerId, value: T }>
24
23
 
25
24
  constructor (map?: PeerMap<T>) {
26
25
  this.map = new Map()
27
26
 
28
27
  if (map != null) {
29
28
  for (const [key, value] of map.entries()) {
30
- this.map.set(key.toString(), value)
29
+ this.map.set(key.toString(), { key, value })
31
30
  }
32
31
  }
33
32
  }
@@ -45,22 +44,22 @@ export class PeerMap <T> {
45
44
  }
46
45
 
47
46
  entries (): IterableIterator<[PeerId, T]> {
48
- return mapIterable<[string, T], [PeerId, T]>(
47
+ return mapIterable<[string, { key: PeerId, value: T }], [PeerId, T]>(
49
48
  this.map.entries(),
50
49
  (val) => {
51
- return [peerIdFromString(val[0]), val[1]]
50
+ return [val[1].key, val[1].value]
52
51
  }
53
52
  )
54
53
  }
55
54
 
56
55
  forEach (fn: (value: T, key: PeerId, map: PeerMap<T>) => void): void {
57
56
  this.map.forEach((value, key) => {
58
- fn(value, peerIdFromString(key), this)
57
+ fn(value.value, value.key, this)
59
58
  })
60
59
  }
61
60
 
62
61
  get (peer: PeerId): T | undefined {
63
- return this.map.get(peer.toString())
62
+ return this.map.get(peer.toString())?.value
64
63
  }
65
64
 
66
65
  has (peer: PeerId): boolean {
@@ -68,20 +67,20 @@ export class PeerMap <T> {
68
67
  }
69
68
 
70
69
  set (peer: PeerId, value: T): void {
71
- this.map.set(peer.toString(), value)
70
+ this.map.set(peer.toString(), { key: peer, value })
72
71
  }
73
72
 
74
73
  keys (): IterableIterator<PeerId> {
75
- return mapIterable<string, PeerId>(
76
- this.map.keys(),
74
+ return mapIterable<{ key: PeerId, value: T }, PeerId>(
75
+ this.map.values(),
77
76
  (val) => {
78
- return peerIdFromString(val)
77
+ return val.key
79
78
  }
80
79
  )
81
80
  }
82
81
 
83
82
  values (): IterableIterator<T> {
84
- return this.map.values()
83
+ return mapIterable(this.map.values(), (val) => val.value)
85
84
  }
86
85
 
87
86
  get size (): number {
package/src/set.ts CHANGED
@@ -1,5 +1,4 @@
1
- import { peerIdFromString } from '@libp2p/peer-id'
2
- import { mapIterable } from './util.js'
1
+ import { mapIterable, peerIdFromString } from './util.js'
3
2
  import type { PeerId } from '@libp2p/interface'
4
3
 
5
4
  /**
@@ -65,9 +64,9 @@ export class PeerSet {
65
64
 
66
65
  forEach (predicate: (peerId: PeerId, index: PeerId, set: PeerSet) => void): void {
67
66
  this.set.forEach((str) => {
68
- const id = peerIdFromString(str)
67
+ const peerId = peerIdFromString(str)
69
68
 
70
- predicate(id, id, this)
69
+ predicate(peerId, peerId, this)
71
70
  })
72
71
  }
73
72
 
package/src/util.ts CHANGED
@@ -1,3 +1,8 @@
1
+ import { peerIdFromMultihash } from '@libp2p/peer-id'
2
+ import { base58btc } from 'multiformats/bases/base58'
3
+ import * as Digest from 'multiformats/hashes/digest'
4
+ import type { PeerId } from '@libp2p/interface'
5
+
1
6
  /**
2
7
  * Calls the passed map function on every entry of the passed iterable iterator
3
8
  */
@@ -28,3 +33,8 @@ export function mapIterable <T, R> (iter: IterableIterator<T>, map: (val: T) =>
28
33
 
29
34
  return iterator
30
35
  }
36
+
37
+ export function peerIdFromString (str: string): PeerId {
38
+ const multihash = Digest.decode(base58btc.decode(`z${str}`))
39
+ return peerIdFromMultihash(multihash)
40
+ }