forked from ifElser/bit-filament
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbit-filament.js
276 lines (219 loc) · 8.19 KB
/
bit-filament.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
const isClient = typeof window !== 'undefined'
const TextAPI = isClient ? {
encode: text => (new TextEncoder()).encode(text),
decode: data => (new TextDecoder()).decode(Uint8Array.from(data))
} : {
encode: text => Buffer.from(text),
decode: data => Buffer.from(data).toString('utf8')
}
const GET_INFO = 0x0F // 0000 1111
const GET_TYPE = 0xF0 // 1111 0000
const NUMBER = 0x10 // 00 01 0000
const UINT_8 = 0x00 // 0000 0000
const UINT_16 = 0x01 // 0000 0001
const UINT_32 = 0x02 // 0000 0010
const INT_8 = 0x03 // 0000 0011
const INT_16 = 0x04 // 0000 0100
const INT_32 = 0x05 // 0000 0101
const FLOAT_32 = 0x06 // 0000 0110
const FLOAT_64 = 0x07 // 0000 0111
const FALSE = 0x0E // 0000 1110
const TRUE = 0x0F // 0000 1111
const STRING = 0x20 // 0010 0000
const REGEXP = 0x30 // 0011 0000
const OBJECT = 0x40 // 0100 0000
const ARRAY = 0x50 // 0101 0000
const BUFFER = 0x60 // 0110 0000
class BitFilament {
constructor (customTypes) {
this.types = {
[NUMBER] : {
twist: n => {
if(n.constructor.name === 'Boolean') return [NUMBER | (n ? TRUE : FALSE)]
const s = n.toString(16)
let NumberInfo = [Float64Array, FLOAT_64]
if( !/\./.test(s) ) {
if( n < 0 ) {
/* 0111 0001 */
if( -0x81 < n ) NumberInfo = [Int8Array , INT_8 ]; else
/* 0111 0000 0000 0001 */
if( -0x8001 < n ) NumberInfo = [Int16Array , INT_16]; else
/* 0111 0000 0000 0000 0000 0000 0000 0001 */
if( -0x80000001 < n ) NumberInfo = [Int32Array , INT_32];
} else {
if( n < 0x100 ) NumberInfo = [Uint8Array , UINT_8 ]; else
if( n < 0x10000 ) NumberInfo = [Uint16Array , UINT_16]; else
if( n < 0x100000000 ) NumberInfo = [Uint32Array , UINT_32];
}
}
const r = Array.from(new Uint8Array(NumberInfo[0].from([n]).buffer))
r.unshift(NUMBER | NumberInfo[1])
return r
},
untwist: (info, buffer, offset) => {
return ({
[UINT_8] : (buffer, offset) => [buffer[offset], offset + 1],
[UINT_16] : (buffer, offset) => {
const arrayBuffer = new Uint8Array(buffer.slice(offset, offset + 2)).buffer
return [new Uint16Array(arrayBuffer)[0], offset + 2]
},
[UINT_32] : (buffer, offset) => {
const arrayBuffer = new Uint8Array(buffer.slice(offset, offset + 4)).buffer
return [new Uint32Array(arrayBuffer)[0], offset + 4]
},
[INT_8] : (buffer, offset) => [buffer[offset], offset + 1],
[INT_16] : (buffer, offset) => {
const arrayBuffer = new Uint8Array(buffer.slice(offset, offset + 2)).buffer
return [new Int16Array(arrayBuffer)[0], offset + 2]
},
[INT_32] : (buffer, offset) => {
const arrayBuffer = new Uint8Array(buffer.slice(offset, offset + 4)).buffer
return [new Int32Array(arrayBuffer)[0], offset + 4]
},
[FLOAT_32] : (buffer, offset) => {
const arrayBuffer = new Uint8Array(buffer.slice(offset, offset + 4)).buffer
return [new Float32Array(arrayBuffer)[0], offset + 4]
},
[FLOAT_64] : (buffer, offset) => {
const arrayBuffer = new Uint8Array(buffer.slice(offset, offset + 8)).buffer
return [new Float64Array(arrayBuffer)[0], offset + 8]
},
[FALSE] : () => [false, offset],
[TRUE] : () => [true , offset]
})[info](buffer, offset)
}
},
[STRING] : {
twist: s => {
const arrayBuffer = Array.from(TextAPI.encode(s))
const l = []
const mask = 0xff
let len = arrayBuffer.length
while(len) {
l.unshift(len & mask)
len >>= 8
}
return [STRING | l.length].concat(l).concat(arrayBuffer)
},
untwist: (len, buffer, offset) => {
let l = buffer.slice(offset, offset + len).reduce((l, byte) => (l << 8) | byte, 0)
let s = TextAPI.decode(new Uint8Array(buffer.slice(offset + len, offset + len + l)))
return [s, offset + len + l]
}
},
[REGEXP] : {
twist: r => {
const ret = this.types[STRING].twist(r.toString())
ret[0] = REGEXP | (ret[0] & GET_INFO)
return ret
},
untwist: (len, buffer, offset) => {
const ret = this.types[STRING].untwist(len, buffer, offset)
ret[0] = new RegExp(ret[0].replace(/^\/(.*)\/g?i?m?u?y?$/, '$1'), ret[0].replace(/^\/.*\/(g?i?m?u?y?)$/, '$1'))
return ret
}
},
[OBJECT] : {
twist: o => {
o = Object.entries(o)
const l = []
const mask = 0xff
let len = o.length
while(len) {
l.unshift(len & mask)
len >>= 8
}
return o.reduce((filament, [key, value]) => {
key = this.types[STRING].twist(key)
return filament.concat(key).concat(this.twist(value))
}, [OBJECT | l.length].concat(l))
},
untwist: (len, buffer, offset) => {
let l = buffer.slice(offset, offset + len).reduce((l, byte) => (l << 8) | byte, 0)
offset += len
const obj = {}
while(l--){
const [key, offs] = this.untwist(buffer, offset)
const [v, o] = this.untwist(buffer, offs)
obj[key] = v
offset = o
}
return [obj, offset]
}
},
[ARRAY] : {
twist: a => {
a = Array.from(a)
const l = []
const mask = 0xff
let len = a.length
while(len) {
l.unshift(len & mask)
len >>= 8
}
return a.reduce((filament, el) => filament.concat(this.twist(el)), [ARRAY | l.length].concat(l))
},
untwist: (len, buffer, offset) => {
let l = buffer.slice(offset, offset + len).reduce((l, byte) => (l << 8) | byte, 0)
offset += len
const arr = []
while(l--){
const [v, o] = this.untwist(buffer, offset)
arr.push(v)
offset = o
}
return [arr, offset]
}
},
[BUFFER] : {
twist: b => {
b = Array.from(b)
const l = []
const mask = 0xff
let len = b.length
while(len) {
l.unshift(len & mask)
len >>= 8
}
return [BUFFER | l.length].concat(l).concat(b)
},
untwist: (len, buffer, offset) => {
let l = buffer.slice(offset, offset + len).reduce((l, byte) => (l << 8) | byte, 0)
let b = buffer.slice(offset + len, offset + len + l)
return [b, offset + len + l]
}
}
}
}
inspect (arg) {
if(arg === null || typeof arg === 'undefined') return null;
if(typeof arg === 'string') return STRING;
if(typeof arg === 'number' || typeof arg === 'boolean') return NUMBER;
if(arg instanceof Uint8Array || (typeof Buffer !== 'undefined' && arg instanceof Buffer)) return BUFFER;
if(arg instanceof RegExp) return REGEXP;
if(
arg instanceof Array ||
arg instanceof Uint16Array ||
arg instanceof Uint32Array ||
arg instanceof Int8Array ||
arg instanceof Int16Array ||
arg instanceof Int32Array ||
arg instanceof Float32Array ||
arg instanceof Float64Array
) return ARRAY;
return OBJECT;
}
twist (arg) {
return this.types[this.inspect(arg)].twist(arg)
}
untwist (buffer, offset = 0) {
if(buffer instanceof ArrayBuffer) buffer = new Uint8Array(buffer); else
if(!(buffer instanceof Uint8Array)) buffer = Uint8Array.from(buffer)
const infoByte = buffer[offset]
const type = infoByte & GET_TYPE
const info = infoByte & GET_INFO
// console.log({type, info, buffer})
return this.types[type].untwist(info, buffer, offset + 1)
}
}
module.exports = BitFilament