-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathcodec.ts
217 lines (180 loc) · 5.55 KB
/
codec.ts
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import * as varint from 'uint8-varint'
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import { convertToBytes, convertToString } from './convert.js'
import { getProtocol } from './protocols-table.js'
import type { StringTuple, Tuple, Protocol } from './index.js'
export interface MultiaddrParts {
bytes: Uint8Array
string: string
tuples: Tuple[]
stringTuples: StringTuple[]
path: string | null
}
export function stringToMultiaddrParts (str: string): MultiaddrParts {
str = cleanPath(str)
const tuples: Tuple[] = []
const stringTuples: StringTuple[] = []
let path: string | null = null
const parts = str.split('/').slice(1)
if (parts.length === 1 && parts[0] === '') {
return {
bytes: new Uint8Array(),
string: '/',
tuples: [],
stringTuples: [],
path: null
}
}
for (let p = 0; p < parts.length; p++) {
const part = parts[p]
const proto = getProtocol(part)
if (proto.size === 0) {
tuples.push([proto.code])
stringTuples.push([proto.code])
// eslint-disable-next-line no-continue
continue
}
p++ // advance addr part
if (p >= parts.length) {
throw new ParseError('invalid address: ' + str)
}
// if it's a path proto, take the rest
if (proto.path === true) {
// should we need to check each path part to see if it's a proto?
// This would allow for other protocols to be added after a unix path,
// however it would have issues if the path had a protocol name in the path
path = cleanPath(parts.slice(p).join('/'))
tuples.push([proto.code, convertToBytes(proto.code, path)])
stringTuples.push([proto.code, path])
break
}
const bytes = convertToBytes(proto.code, parts[p])
tuples.push([proto.code, bytes])
stringTuples.push([proto.code, convertToString(proto.code, bytes)])
}
return {
string: stringTuplesToString(stringTuples),
bytes: tuplesToBytes(tuples),
tuples,
stringTuples,
path
}
}
export function bytesToMultiaddrParts (bytes: Uint8Array): MultiaddrParts {
const tuples: Tuple[] = []
const stringTuples: StringTuple[] = []
let path: string | null = null
let i = 0
while (i < bytes.length) {
const code = varint.decode(bytes, i)
const n = varint.encodingLength(code)
const p = getProtocol(code)
const size = sizeForAddr(p, bytes.slice(i + n))
if (size === 0) {
tuples.push([code])
stringTuples.push([code])
i += n
// eslint-disable-next-line no-continue
continue
}
const addr = bytes.slice(i + n, i + n + size)
i += (size + n)
if (i > bytes.length) { // did not end _exactly_ at buffer.length
throw new ParseError('Invalid address Uint8Array: ' + uint8ArrayToString(bytes, 'base16'))
}
// ok, tuple seems good.
tuples.push([code, addr])
const stringAddr = convertToString(code, addr)
stringTuples.push([code, stringAddr])
if (p.path === true) {
// should we need to check each path part to see if it's a proto?
// This would allow for other protocols to be added after a unix path,
// however it would have issues if the path had a protocol name in the path
path = stringAddr
break
}
}
return {
bytes: Uint8Array.from(bytes),
string: stringTuplesToString(stringTuples),
tuples,
stringTuples,
path
}
}
/**
* [[str name, str addr]... ] -> string
*/
function stringTuplesToString (tuples: StringTuple[]): string {
const parts: string[] = []
tuples.map((tup) => {
const proto = getProtocol(tup[0])
parts.push(proto.name)
if (tup.length > 1 && tup[1] != null) {
parts.push(tup[1])
}
return null
})
return cleanPath(parts.join('/'))
}
/**
* [[int code, Uint8Array ]... ] -> Uint8Array
*/
export function tuplesToBytes (tuples: Tuple[]): Uint8Array {
return uint8ArrayConcat(tuples.map((tup) => {
const proto = getProtocol(tup[0])
let buf = Uint8Array.from(varint.encode(proto.code))
if (tup.length > 1 && tup[1] != null) {
buf = uint8ArrayConcat([buf, tup[1]]) // add address buffer
}
return buf
}))
}
/**
* For the passed address, return the serialized size
*/
function sizeForAddr (p: Protocol, addr: Uint8Array | number[]): number {
if (p.size > 0) {
return p.size / 8
} else if (p.size === 0) {
return 0
} else {
const size = varint.decode(addr instanceof Uint8Array ? addr : Uint8Array.from(addr))
return size + varint.encodingLength(size)
}
}
export function bytesToTuples (buf: Uint8Array): Tuple[] {
const tuples: Array<[number, Uint8Array?]> = []
let i = 0
while (i < buf.length) {
const code = varint.decode(buf, i)
const n = varint.encodingLength(code)
const p = getProtocol(code)
const size = sizeForAddr(p, buf.slice(i + n))
if (size === 0) {
tuples.push([code])
i += n
// eslint-disable-next-line no-continue
continue
}
const addr = buf.slice(i + n, i + n + size)
i += (size + n)
if (i > buf.length) { // did not end _exactly_ at buffer.length
throw new ParseError('Invalid address Uint8Array: ' + uint8ArrayToString(buf, 'base16'))
}
// ok, tuple seems good.
tuples.push([code, addr])
}
return tuples
}
export function cleanPath (str: string): string {
return '/' + str.trim().split('/').filter((a) => a).join('/')
}
export class ParseError extends Error {
static name = 'ParseError'
name = 'ParseError'
constructor (str: string) {
super(`Error parsing address: ${str}`)
}
}