-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathmultiaddr.ts
More file actions
169 lines (136 loc) · 3.91 KB
/
Copy pathmultiaddr.ts
File metadata and controls
169 lines (136 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
import { bytesToComponents, componentsToBytes, componentsToString, stringToComponents } from './components.ts'
import { InvalidMultiaddrError, InvalidParametersError } from './errors.ts'
import { isMultiaddr } from './index.ts'
import { registry } from './registry.ts'
import type { MultiaddrInput, Multiaddr as MultiaddrInterface, Component } from './index.ts'
const inspect = Symbol.for('nodejs.util.inspect.custom')
export const symbol = Symbol.for('@multiformats/multiaddr')
function toComponents (addr: MultiaddrInput): Component[] {
if (addr == null) {
addr = '/'
}
if (isMultiaddr(addr)) {
return addr.getComponents()
}
if (addr instanceof Uint8Array) {
return bytesToComponents(addr)
}
if (typeof addr === 'string') {
addr = addr
.replace(/\/(\/)+/, '/')
.replace(/(\/)+$/, '')
if (addr === '') {
addr = '/'
}
return stringToComponents(addr)
}
if (Array.isArray(addr)) {
return addr
}
throw new InvalidMultiaddrError('Must be a string, Uint8Array, Component[], or another Multiaddr')
}
interface MultiaddrOptions {
validate?: boolean
}
/**
* Creates a {@link Multiaddr} from a {@link MultiaddrInput}
*/
export class Multiaddr implements MultiaddrInterface {
[symbol]: boolean = true
readonly #components: Component[]
// cache string representation
#string: string | undefined
// cache byte representation
#bytes: Uint8Array<ArrayBuffer> | undefined
constructor (addr: MultiaddrInput | Component[] = '/', options: MultiaddrOptions = {}) {
this.#components = toComponents(addr)
if (options.validate !== false) {
validate(this)
}
}
get bytes (): Uint8Array<ArrayBuffer> {
if (this.#bytes == null) {
this.#bytes = componentsToBytes(this.#components)
}
return this.#bytes
}
toString (): string {
if (this.#string == null) {
this.#string = componentsToString(this.#components)
}
return this.#string
}
toJSON (): string {
return this.toString()
}
getComponents (): Component[] {
return [
...this.#components.map(c => ({ ...c }))
]
}
encapsulate (addr: MultiaddrInput): MultiaddrInterface {
const ma = new Multiaddr(addr)
return new Multiaddr([
...this.#components,
...ma.getComponents()
], {
validate: false
})
}
decapsulate (addr: Multiaddr | string): MultiaddrInterface {
const addrString = addr.toString()
const s = this.toString()
const i = s.lastIndexOf(addrString)
if (i < 0) {
throw new InvalidParametersError(`Address ${this.toString()} does not contain subaddress: ${addrString}`)
}
return new Multiaddr(s.slice(0, i), {
validate: false
})
}
decapsulateCode (code: number): Multiaddr {
let index
for (let i = this.#components.length - 1; i > -1; i--) {
if (this.#components[i].code === code) {
index = i
break
}
}
return new Multiaddr(this.#components.slice(0, index), {
validate: false
})
}
equals (addr: { bytes: Uint8Array }): boolean {
return uint8ArrayEquals(this.bytes, addr.bytes)
}
/**
* Returns Multiaddr as a human-readable string
* https://nodejs.org/api/util.html#utilinspectcustom
*
* @example
* ```js
* import { multiaddr } from '@multiformats/multiaddr'
*
* console.info(multiaddr('/ip4/127.0.0.1/tcp/4001'))
* // 'Multiaddr(/ip4/127.0.0.1/tcp/4001)'
* ```
*/
[inspect] (): string {
return `Multiaddr(${this.toString()})`
}
}
/**
* Ensures all multiaddr tuples are correct. Throws if any invalid protocols or
* values are encountered.
*/
export function validate (addr: Multiaddr): void {
addr.getComponents()
.forEach(component => {
const codec = registry.getProtocol(component.code)
if (component.value == null) {
return
}
codec.validate?.(component.value)
})
}