forked from multiformats/js-multiaddr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec.js
More file actions
222 lines (180 loc) · 4.58 KB
/
codec.js
File metadata and controls
222 lines (180 loc) · 4.58 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
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
218
219
220
221
222
'use strict'
const { Buffer } = require('buffer')
const convert = require('./convert')
const protocols = require('./protocols-table')
const varint = require('varint')
// export codec
module.exports = {
stringToStringTuples,
stringTuplesToString,
tuplesToStringTuples,
stringTuplesToTuples,
bufferToTuples,
tuplesToBuffer,
bufferToString,
stringToBuffer,
fromString,
fromBuffer,
validateBuffer,
isValidBuffer,
cleanPath,
ParseError,
protoFromTuple,
sizeForAddr
}
// string -> [[str name, str addr]... ]
function stringToStringTuples (str) {
const tuples = []
const parts = str.split('/').slice(1) // skip first empty elem
if (parts.length === 1 && parts[0] === '') {
return []
}
for (let p = 0; p < parts.length; p++) {
const part = parts[p]
const proto = protocols(part)
if (proto.size === 0) {
tuples.push([part])
continue
}
p++ // advance addr part
if (p >= parts.length) {
throw ParseError('invalid address: ' + str)
}
// if it's a path proto, take the rest
if (proto.path) {
tuples.push([
part,
// TODO: 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
cleanPath(parts.slice(p).join('/'))
])
break
}
tuples.push([part, parts[p]])
}
return tuples
}
// [[str name, str addr]... ] -> string
function stringTuplesToString (tuples) {
const parts = []
tuples.map(tup => {
const proto = protoFromTuple(tup)
parts.push(proto.name)
if (tup.length > 1) {
parts.push(tup[1])
}
})
return cleanPath(parts.join('/'))
}
// [[str name, str addr]... ] -> [[int code, Buffer]... ]
function stringTuplesToTuples (tuples) {
return tuples.map(tup => {
if (!Array.isArray(tup)) {
tup = [tup]
}
const proto = protoFromTuple(tup)
if (tup.length > 1) {
return [proto.code, convert.toBuffer(proto.code, tup[1])]
}
return [proto.code]
})
}
// [[int code, Buffer]... ] -> [[str name, str addr]... ]
function tuplesToStringTuples (tuples) {
return tuples.map(tup => {
const proto = protoFromTuple(tup)
if (tup.length > 1) {
return [proto.code, convert.toString(proto.code, tup[1])]
}
return [proto.code]
})
}
// [[int code, Buffer ]... ] -> Buffer
function tuplesToBuffer (tuples) {
return fromBuffer(Buffer.concat(tuples.map(tup => {
const proto = protoFromTuple(tup)
let buf = Buffer.from(varint.encode(proto.code))
if (tup.length > 1) {
buf = Buffer.concat([buf, tup[1]]) // add address buffer
}
return buf
})))
}
function sizeForAddr (p, addr) {
if (p.size > 0) {
return p.size / 8
} else if (p.size === 0) {
return 0
} else {
const size = varint.decode(addr)
return size + varint.decode.bytes
}
}
// Buffer -> [[int code, Buffer ]... ]
function bufferToTuples (buf) {
const tuples = []
let i = 0
while (i < buf.length) {
const code = varint.decode(buf, i)
const n = varint.decode.bytes
const p = protocols(code)
const size = sizeForAddr(p, buf.slice(i + n))
if (size === 0) {
tuples.push([code])
i += n
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 ParseError('Invalid address buffer: ' + buf.toString('hex'))
}
// ok, tuple seems good.
tuples.push([code, addr])
}
return tuples
}
// Buffer -> String
function bufferToString (buf) {
const a = bufferToTuples(buf)
const b = tuplesToStringTuples(a)
return stringTuplesToString(b)
}
// String -> Buffer
function stringToBuffer (str) {
str = cleanPath(str)
const a = stringToStringTuples(str)
const b = stringTuplesToTuples(a)
return tuplesToBuffer(b)
}
// String -> Buffer
function fromString (str) {
return stringToBuffer(str)
}
// Buffer -> Buffer
function fromBuffer (buf) {
const err = validateBuffer(buf)
if (err) throw err
return Buffer.from(buf) // copy
}
function validateBuffer (buf) {
try {
bufferToTuples(buf) // try to parse. will throw if breaks
} catch (err) {
return err
}
}
function isValidBuffer (buf) {
return validateBuffer(buf) === undefined
}
function cleanPath (str) {
return '/' + str.trim().split('/').filter(a => a).join('/')
}
function ParseError (str) {
return new Error('Error parsing address: ' + str)
}
function protoFromTuple (tup) {
const proto = protocols(tup[0])
return proto
}