-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
171 lines (159 loc) · 4.32 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
const { List, Bytes, IntMap, StringMap, Enum, Class } = require('haxe-type');
const BaseCode = require('haxe-basecode');
class Serializer {
constructor(
useCache = false,
useEnumIndex = false
) {
this.StringCache = [];
this.ObjectCache = [];
this.USE_CACHE = useCache;
this.USE_ENUM_INDEX = useEnumIndex;
}
typeof(v) {
/* eslint-disable indent */
switch (true) {
case v instanceof Bytes : return 'Bytes';
case v instanceof Enum : return 'Enum';
case v instanceof Class : return 'Object';
default:
let type = Object.prototype.toString.call(v);
return type.replace(/^.*? (.*?)\]$/, '$1');
}
/* eslint-enable indent */
}
static serialize(data) {
let _s = new Serializer();
return _s.run(data);
}
run(data) {
let type = this.typeof(data);
/* eslint-disable indent */
switch(type) {
case 'Array' : return this.serializeObject(data);
case 'Boolean' : return data ? 't' : 'f';
case 'Bytes' : return this.serializeBytes(data);
case 'Date' : return this.serializeDate(data);
case 'Null' : return 'n';
case 'Number' : return this.serializeNumber(data);
case 'Object' : return this.serializeObject(data);
case 'String' : return this.serializeString(data);
case 'Enum' : return this.serializeEnum(data);
case 'Error' : return `x${this.run(data.message)}`;
default:
throw `Unknow type ${type}`;
}
/* eslint-enable indent */
}
cache(type, v) {
let buffer = type == "String" ? "R" : "r";
if(this.USE_CACHE || type == "String") {
let i = this[`${type}Cache`].indexOf(v);
if(i > -1) {
return buffer + i;
} else {
this[`${type}Cache`].push(v);
}
}
return false;
}
serializeString(s) {
let cached = this.cache('String', s);
if(cached != false) {
return cached;
}
s = encodeURIComponent(s);
return `y${s.length}:${s}`;
}
serializeDate(d) {
let Y = "0" + d.getFullYear(),
M = "0" + (d.getMonth() + 1),
D = "0" + d.getDate(),
H = "0" + d.getHours(),
m = "0" + d.getMinutes(),
s = "0" + d.getSeconds();
// eslint-disable-next-line max-len
return `v${Y.slice(-4)}-${M.slice(-2)}-${D.slice(-2)} ${H.slice(-2)}:${m.slice(-2)}:${s.slice(-2)}`;
}
serializeNumber(n) {
if(Number.isNaN(n)) {
return "k";
}
if(!Number.isFinite(n)) {
return (Math.sign(n) > 0) ? "p" : "m";
}
if(!Number.isSafeInteger(n)) {
if(Number.isInteger(n)) {
return `d${n.toExponential()}`;
} return `d${n}`;
}
return (n == 0) ? 'z' : `i${n}`;
}
serializeObject(o, type) {
let cached = this.cache('Object', o);
if(cached != false) {
return cached;
}
let buffer = o instanceof Class ? this.run(o.constructor.name) : "",
consecutive_null = 0;
for(let [k, v] of Object.entries(o)) {
if(o instanceof Class && k == '__name__') {
continue;
}
/* if(o instanceof ObjectMap) {} else */
if(o instanceof Array) {
let t = this.typeof(v);
if(t == "Null") {
consecutive_null++;
if((!(o instanceof List)) && ((o.length - 1) > k)) {
continue;
}
}
if(consecutive_null > 0) {
buffer += consecutive_null > 1 ? `u${consecutive_null}` : 'n';
consecutive_null = 0;
}
if(t == "Null") {
continue;
}
} else {
buffer += o instanceof IntMap ? `:${k}` : this.run(k);
}
buffer += this.run(v);
}
/* eslint-disable indent */
switch (true) {
case o instanceof Class : return `c${buffer}g`;
case o instanceof IntMap : return `q${buffer}h`;
// case o instanceof ObjectMap : return `M${buffer}h`;
case o instanceof StringMap : return `b${buffer}h`;
case o instanceof List : return `l${buffer}h`;
case o instanceof Array : return `a${buffer}h`;
default:
return `o${buffer}g`;
}
/* eslint-enable indent */
}
serializeEnum(e) {
let cached = this.cache('Object', e);
if(cached != false) {
return cached;
}
let buffer = this.USE_ENUM_INDEX ? "j" : "w",
i = 2;
buffer += this.run(e.constructor.name);
buffer += this.USE_ENUM_INDEX ? `:${e[1]}` : this.run(e[0]);
buffer += `:${e.length - 2}`;
while(i < e.length) {
buffer += this.run(e[i++]);
}
return buffer;
}
serializeBytes(b) {
// eslint-disable-next-line max-len
let b64 = new BaseCode(Bytes.ofString("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%:")),
r = b64.encodeBytes(b);
return `s${r.length}:${r}`;
}
}
module.exports = Serializer;