-
Notifications
You must be signed in to change notification settings - Fork 20
/
types.js
350 lines (303 loc) · 9.53 KB
/
types.js
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
const BN = require('bn.js')
const types = {
Bytes: () => [bytebuf],
String: () => [string],
Vector: type => [vector, {type}],
Optional: type => [optional, {type}],
Time: () => [time],
FixedString16: () => [string, {maxLen: 16}],
FixedString32: () => [string, {maxLen: 32}],
FixedBytes16: () => [bytebuf, {len: 16}],
FixedBytes28: () => [bytebuf, {len: 28}],
FixedBytes32: () => [bytebuf, {len: 32}],
FixedBytes33: () => [bytebuf, {len: 33}],
FixedBytes64: () => [bytebuf, {len: 64}],
FixedBytes65: () => [bytebuf, {len: 65}],
UInt8: () => [intbuf, {bits: 8}],
UInt16: () => [intbuf, {bits: 16}],
UInt32: () => [intbuf, {bits: 32}],
UInt64: () => [intbuf, {bits: 64}],
// ,128,224,256,512 TODO
Int8: () => [intbuf, {signed: true, bits: 8}],
Int16: () => [intbuf, {signed: true, bits: 16}],
Int32: () => [intbuf, {signed: true, bits: 32}],
Int64: () => [intbuf, {signed: true, bits: 64}]
// ,128,224,256,512 TODO
// VarInt32: ()=> [intbuf, {signed: true, bits: 32}],
}
/*
@arg {SerializerConfig} config
@return {object} {[typeName]: function(args)}
*/
module.exports = config => {
config = Object.assign({defaults: false, debug: false, customTypes: {}}, config)
const allTypes = Object.assign(types, config.customTypes)
const createTypeReducer = (map, name) => {
map[name] = (...args) => {
const type = createType(name, config, ...args)
return type
}
return map
}
const typeMap = Object.keys(allTypes).reduce(createTypeReducer, {})
typeMap.config = config
return typeMap
}
/**
@args {string} typeName - matches types[]
@args {string} config - Additional arguments for types
*/
function createType (typeName, config, ...args) {
const Type = types[typeName]
const [fn, v = {}] = Type(...args)
const validation = Object.assign(v, config)
validation.typeName = typeName
// if(typeName === 'Vector') console.log('typeName', validation)
const type = fn(validation)
return type
}
const isSerializer = type =>
typeof type === 'object' &&
typeof type.fromByteBuffer === 'function' &&
typeof type.appendByteBuffer === 'function' &&
typeof type.fromObject === 'function' &&
typeof type.toObject === 'function'
const vector = validation => {
if (!isSerializer(validation.type)) { throw new TypeError('Vector type should be a serializer') }
return {
fromByteBuffer (b) {
const size = b.readVarint32()
// if (validation.debug) {
// console.log("constint32 size = " + size.toString(16))
// }
const result = []
for (let i = 0; i < size; i++) {
result.push(validation.type.fromByteBuffer(b))
}
return result
},
appendByteBuffer (b, value) {
validate(value, validation)
b.writeVarint32(value.length)
for (const o of value) {
validation.type.appendByteBuffer(b, o)
}
},
fromObject (value) {
validate(value, validation)
const result = []
for (const o of value) {
result.push(validation.type.fromObject(o))
}
return result
},
toObject (value) {
if (validation.defaults && value == null) {
return [validation.type.toObject(value)]
}
validate(value, validation)
const result = []
for (const o of value) {
result.push(validation.type.toObject(o))
}
return result
}
}
}
const optional = validation => {
const {type} = validation
if (!isSerializer(type)) { throw new TypeError('Optional parameter should be a serializer') }
return {
fromByteBuffer (b) {
if (!(b.readUint8() === 1)) {
return null
}
return type.fromByteBuffer(b)
},
appendByteBuffer (b, value) {
if (value != null) {
b.writeUint8(1)
type.appendByteBuffer(b, value)
} else {
b.writeUint8(0)
}
},
fromObject (value) {
if (value == null) {
return null
}
return type.fromObject(value)
},
toObject (value) {
// toObject is only null save if defaults is true
let resultValue
if (value == null && !validation.defaults) {
resultValue = null
} else {
resultValue = type.toObject(value)
}
return resultValue
}
}
}
const intbufType = ({signed = false, bits}) =>
// variable ? `${signed ? 'Varint' : 'Uint'}${bits}` : // Varint32 was used at some point
`${signed ? 'Int' : 'Uint'}${bits}`
const intbuf = (validation) => ({
fromByteBuffer (b) {
return b[`read${intbufType(validation)}`]()
},
appendByteBuffer (b, value) {
// noOverflow(value, validation)
b[`write${intbufType(validation)}`](value)
},
fromObject (value) {
// if(validation.bits > 53 && typeof value === 'number')
// value = String(value)
noOverflow(value, validation)
return value
},
toObject (value) {
if (validation.defaults && value == null) {
return validation.bits > 53 ? '0' : 0
}
// if(validation.bits > 53 && typeof value === 'number')
// value = String(value)
noOverflow(value, validation)
return value.toString ? value.toString() : value
}
})
const bytebuf = (validation) => {
const _bytebuf = {
fromByteBuffer (b) {
const {len} = validation
let bCopy
if (len == null) {
const lenPrefix = b.readVarint32()
bCopy = b.copy(b.offset, b.offset + lenPrefix)
b.skip(lenPrefix)
} else {
bCopy = b.copy(b.offset, b.offset + len)
b.skip(len)
}
return Buffer.from(bCopy.toBinary(), 'binary')
},
appendByteBuffer (b, value) {
// value = _bytebuf.fromObject(value)
const {len} = validation
if (len == null) {
b.writeVarint32(value.length)
}
b.append(value.toString('binary'), 'binary')
},
fromObject (value) {
if (typeof value === 'string') { value = Buffer.from(value, 'hex') }
validate(value, validation)
return value
},
toObject (value) {
const {defaults, len} = validation
if (defaults && value == null) {
return Array(len ? len + 1 : 1).join('00')
}
validate(value, validation)
return value.toString('hex')
}
}
return _bytebuf
}
const string = (validation) => ({
fromByteBuffer (b) {
return b.readVString()
},
appendByteBuffer (b, value) {
validate(value, validation)
b.writeVString(value.toString())
},
fromObject (value) {
validate(value, validation)
return value
},
toObject (value) {
if (validation.defaults && value == null) {
return ''
}
validate(value, validation)
return value
}
})
const time = (validation) => {
const _time = {
fromByteBuffer (b) {
return b.readUint32()
},
appendByteBuffer (b, value) {
// if(typeof value !== "number")
// value = _time.fromObject(value)
validate(value, validation)
b.writeUint32(value)
},
fromObject (value) {
validate(value, validation)
if (typeof value === 'number') { return value }
if (value.getTime) { return Math.floor(value.getTime() / 1000) }
if (typeof value !== 'string') { throw new Error('Unknown date type: ' + value) }
// Chrome assumes Zulu when missing, Firefox does not
if (typeof value === 'string' && !/Z$/.test(value)) { value += 'Z' }
return Math.floor(new Date(value).getTime() / 1000)
},
toObject (value) {
if (validation.defaults && value == null) { return (new Date(0)).toISOString().split('.')[0] + 'Z' }
validate(value, validation)
// if(typeof value === "string") {
// if(!/Z$/.test(value))
// value += "Z"
//
// return value
// }
// if(value.getTime)
// return value.toISOString().split('.')[0] + 'Z'
noOverflow(value, spread(validation, {bits: 32}))
const int = parseInt(value)
return (new Date(int * 1000)).toISOString().split('.')[0] + 'Z'
}
}
return _time
}
const validate = (value, validation) => {
if (isEmpty(value)) {
throw new Error(`Required ${validation.typeName}`)
}
if (validation.len != null) {
if (value.length == null) { throw new Error(`len validation requries a "length" property`) }
const {len} = validation
if (value.length !== len) { throw new Error(`${validation.typeName} length ${value.length} does not equal ${len}`) }
}
if (validation.maxLen != null) {
const {maxLen} = validation
if (value.length == null) { throw new Error(`maxLen validation requries a "length" property`) }
if (value.length > maxLen) { throw new Error(`${validation.typeName} length ${value.length} exceeds maxLen ${maxLen}`) }
}
}
const ZERO = new BN()
const ONE = new BN('1')
function noOverflow (value, validation) {
if (isEmpty(value)) {
throw new Error(`Required ${validation.typeName}`)
}
const {signed = false, bits = 54} = validation
const max = signed ? maxSigned(bits) : maxUnsigned(bits)
const min = signed ? minSigned(bits) : ZERO
const i = new BN(String(value))
// console.log('i.toString(), min.toString()', i.toString(), min.toString())
if (i.cmp(min) < 0 || i.cmp(max) > 0) {
throw new Error(`Overflow ${validation.typeName} ${value}, ` +
`max ${max.toString()}, min ${min.toString()}, signed ${signed}, bits ${bits}`)
}
}
const spread = (...args) => Object.assign(...args)
const isEmpty = value => value == null
// 1 << N === Math.pow(2, N)
const maxUnsigned = bits => new BN(1).ishln(bits).isub(ONE)
const maxSigned = bits => new BN(1).ishln(bits - 1).isub(ONE)
const minSigned = bits => new BN(1).ishln(bits - 1).ineg()