forked from kriszyp/ordered-binary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
191 lines (188 loc) · 5.4 KB
/
index.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
/*
control character types:
1 - metadata
14 - true
15 - false
17 - number <= -2^48
18 - -2^48 < number < 0
19 - 0 <= number < 2^48
20 - 2^48 <= number
27 - used for escaping control bytes in strings
30 - multipart separator
> 31 normal string characters
*/
const MAX_24_BITS = 2**24
const MAX_32_BITS = 2**32
const MAX_40_BITS = 2**40
const MAX_48_BITS = 2**48
/*
* Convert arbitrary scalar values to buffer bytes with type preservation and type-appropriate ordering
*/
exports.toBufferKey = function(key) {
if (typeof key === 'string') {
if (key.charCodeAt(0) < 32) {
return Buffer.from('\u001B' + key) // escape, if there is a control character that starts it
}
return Buffer.from(key)
} else if (typeof key === 'number') {
let negative = key < 0
if (negative) {
key = -key // do our serialization on the positive form
}
if (key < MAX_48_BITS) {
const getByte = (max) => {
let byte = (key / max) >>> 0
key = key - byte * max
return byte
}
let bufferArray = [
negative ? 18 : 19,
key >= MAX_40_BITS ? getByte(MAX_40_BITS) : 0,
key >= MAX_32_BITS ? getByte(MAX_32_BITS) : 0,
key >>> 24,
key >>> 16 & 255,
key >>> 8 & 255,
key & 255,
0]
let index = 7
if (key - (key >>> 0)) {
// handle the decimal/mantissa
let asString = key.toString() // we operate with string representation to try to preserve non-binary decimal state
let exponentPosition = asString.indexOf('e')
let mantissa
if (exponentPosition > -1) {
let exponent = Number(asString.slice(exponentPosition + 2)) - 2
let i
for (i = 0; i < exponent; i += 2) {
bufferArray[index++] = 1 // zeros with continuance bit
}
asString = asString.slice(0, exponentPosition).replace(/\./, '')
if (i == exponent) {
asString = '0' + asString
}
} else {
asString = asString.slice(asString.indexOf('.') + 1)
}
for (var i = 0, l = asString.length; i < l; i += 2) {
bufferArray[index++] = Number(asString[i] + (asString[i + 1] || 0)) * 2 + 1
}
bufferArray[index - 1]-- // remove the continuation bit on the last one
}
if (negative) {
// two's complement
for (let i = 1, l = bufferArray.length; i < l; i++) {
bufferArray[i] = bufferArray[i] ^ 255
}
}
return Buffer.from(bufferArray)
} else {
throw new Error('Unsupported number')
}
} else if (typeof key === 'boolean') {
let buffer = Buffer.allocUnsafe(1)
buffer[0] = key ? 15 : 14 // SHIFT IN/OUT control characters
return buffer
} else if (key instanceof Metadata) {
let buffer = Buffer.allocUnsafe(1)
buffer[0] = key.code
return buffer
} else {
throw new Error('Can not serialize key ' + key)
}
}
function fromBufferKey(buffer, multipart) {
let value, values
do {
let consumed, controlByte = buffer[0]
switch (controlByte) {
case 18:
// negative number
for (let i = 1; i < 7; i++) {
buffer[i] = buffer[i] ^ 255
}
// fall through
case 19: // number
value = (buffer[4] << 16) + (buffer[5] << 8) + (buffer[6])
if (buffer[3]) {
value += buffer[3] * MAX_24_BITS
}
if (buffer[2]) {
value += buffer[2] * MAX_32_BITS
}
if (buffer[1]) {
value += buffer[1] * MAX_40_BITS
}
consumed = 7
let negative = controlByte === 18
let decimal
do {
decimal = buffer[consumed++]
if (negative) {
decimal ^= 255
}
if (decimal)
value += (decimal >> 1) / 100**(consumed - 7)
} while (decimal & 1)
if (negative) {
value = -value
}
break
case 14: // boolean false
consumed = 1
value = false
break
case 15: // boolean true
consumed = 1
value = true
break
case 1: case 255:// metadata, return next byte as the code
consumed = 2
value = new Metadata(buffer[1])
break
default:
if (controlByte < 27) {
throw new Error('Unknown control byte ' + controlByte)
}
let strBuffer
if (multipart) {
consumed = buffer.indexOf(30)
if (consumed === -1) {
strBuffer = buffer
consumed = buffer.length
} else
strBuffer = buffer.slice(0, consumed)
} else
strBuffer = buffer
if (strBuffer[strBuffer.length - 1] == 27) {
// TODO: needs escaping here
value = strBuffer.toString()
} else {
value = strBuffer.toString()
}
}
if (multipart) {
if (!values) {
values = [value]
} else {
values.push(value)
}
if (buffer.length === consumed) {
return values // done, consumed all the values
}
if (buffer[consumed] !== 30)
throw new Error('Invalid separator byte')
buffer = buffer.slice(consumed + 1)
}
} while (multipart)
// single value mode
return value
}
// code can be one byte
function Metadata(code) {
this.code = code
}
Metadata.prototype.toString = function() {
return 'Metadata: ' + this.code
}
exports.fromBufferKey = fromBufferKey
exports.Metadata = Metadata