-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserializer.ts
260 lines (235 loc) · 6.22 KB
/
serializer.ts
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
// Experimental serializer / deserializer for modules
// not completely wired in yet, serialization is running, deserialization is not
const END = 0;
const LIST = 1;
const TUPLE = 2;
const INDUCT = 3;
const STRING = 4;
const NUMBER = 5;
const NULL = 6;
const te = new TextEncoder();
// TODO - next two functions are machine generated and need to be fixed
class DeserializationStream {
pos = 0;
buf: Uint8Array;
constructor(buf: Uint8Array) {
this.buf = buf;
}
readByte() {
return this.buf[this.pos++];
}
readVarint() {
let shift = 0;
let result = 0;
while (true) {
const byte = this.readByte();
result |= (byte & 0x7f) << shift;
if ((byte & 0x80) === 0) break;
shift += 7;
}
return result;
}
readSignedVarint() {
const n = this.readVarint();
return (n >>> 1) ^ -(n & 1);
}
readString() {
const length = this.readVarint();
const bytes = this.buf.slice(this.pos, this.pos + length);
this.pos += length;
return new TextDecoder().decode(bytes);
}
}
export class DecFile {
pool: string[] = [""];
buf: DeserializationStream;
static decode(encoded: Uint8Array) {
return new DecFile(encoded).read()
}
constructor(data: Uint8Array) {
this.buf = new DeserializationStream(data);
this.readPool();
}
readPool() {
while (true) {
let str = this.buf.readString();
if (!str.length) break
this.pool.push(str);
}
console.log('read pool', this.buf.pos)
}
read(): any {
const type = this.buf.readByte();
switch (type) {
case NULL:
return null;
case LIST: {
const list: any[] = [];
while (this.buf.buf[this.buf.pos] !== END) {
list.push(this.read());
}
this.buf.pos++;
let rval: any = { tag: "Nil", 'h0': null };
while (list.length)
rval = { tag: "_::_", h0: null, h1: list.pop(), h2: rval };
return rval;
}
case TUPLE: {
const tuple: any[] = [];
while (this.buf.buf[this.buf.pos] !== END) {
tuple.push(this.read());
}
this.buf.pos++;
let rval: any = tuple.pop();
while (tuple.length)
rval = { tag: "_,_", h0: null, h1: null, h2: tuple.pop(), h3: rval };
return rval;
}
case STRING:
return this.pool[this.buf.readVarint()];
case NUMBER:
return this.buf.readSignedVarint();
case INDUCT:
const tag = this.pool[this.buf.readVarint()];
const obj: any = { tag };
let i = 0;
while (this.buf.buf[this.buf.pos] !== END) {
obj[`h${i++}`] = this.read();
}
this.buf.pos++;
return obj;
default:
debugger
throw new Error(`Unknown type: ${type}`);
}
}
}
class SerializationStream {
pos = 0;
buf = new Uint8Array(1024 * 1024);
ensure(size: number) {
if (this.buf.length - this.pos < size) {
const tmp = new Uint8Array(this.buf.length * 1.5);
tmp.set(this.buf);
this.buf = tmp;
}
}
writeByte(n: number) {
this.ensure(1);
this.buf[this.pos++] = n % 256;
}
writeVarint(n: number) {
while (n > 127) {
this.writeByte((n & 0x7f) | 0x80);
n >>= 7;
}
this.writeByte(n & 0x7f);
}
writeSignedVarint(n: number) {
const zigzag = (n << 1) ^ (n >> 31);
this.writeVarint(zigzag);
}
writeString(s: string) {
let data = te.encode(s);
this.ensure(data.byteLength + 4);
this.writeVarint(data.byteLength);
this.buf.set(data, this.pos);
this.pos += data.byteLength;
}
toUint8Array() {
return this.buf.slice(0, this.pos);
}
}
export class EncFile {
poollen = 1;
pool = new SerializationStream();
buf = new SerializationStream();
pmap: Record<string, number> = { "": 0 };
static encode(data: any) {
let f = new EncFile()
f.write(data)
f.pool.writeVarint(0)
return f.toUint8Array()
}
writeString(s: string) {
let n = this.pmap[s];
if (n === undefined) {
n = this.poollen++;
this.pool.writeString(s);
this.pmap[s] = n;
}
this.buf.writeVarint(n);
}
write(a: any) {
// shouldn't happen?
if (a == null) {
this.buf.writeByte(NULL);
} else if (a.tag == "_::_") {
this.buf.writeByte(LIST);
for (; a.tag === "_::_"; a = a.h2) {
this.write(a.h1);
}
this.buf.writeByte(END);
} else if (a.tag == "_,_") {
this.buf.writeByte(TUPLE);
for (; a.tag === "_,_"; a = a.h3) {
this.write(a.h2);
}
this.write(a);
this.buf.writeByte(END);
} else if (typeof a === "string") {
this.buf.writeByte(STRING);
this.writeString(a);
} else if (typeof a === "number") {
this.buf.writeByte(NUMBER);
this.buf.writeSignedVarint(a);
} else if (a.tag) {
this.buf.writeByte(INDUCT);
this.writeString(a.tag);
// we're actually missing a bunch of data here...
// with null, hack is not needed.
let i = 0
for (; i <= 20; i++) {
let key = 'h' + i
let v = a[key]
if (v === undefined) break
this.write(v);
}
if (a['h' + (i + 1)] !== undefined) {
throw new Error("BOOM")
}
this.buf.writeByte(END);
} else {
throw new Error(`handle ${typeof a} ${a} ${Object.keys(a)}`);
}
}
toUint8Array() {
const poolArray = this.pool.toUint8Array();
const bufArray = this.buf.toUint8Array();
const rval = new Uint8Array(poolArray.length + bufArray.length);
// console.log('psize', poolArray.byteLength, poolArray.length)
rval.set(poolArray);
rval.set(bufArray, poolArray.length);
return rval;
}
}
function deepEqual(a: any, b: any): boolean {
if (a === b) return true;
if (typeof a !== typeof b) return false;
if (a == null || b == null) return false;
if (typeof a !== "object") return false;
if (Array.isArray(a)) {
if (!Array.isArray(b) || a.length !== b.length) return false;
for (let i = 0; i < a.length; i++) {
if (!deepEqual(a[i], b[i])) return false;
}
return true;
}
const keysA = Object.keys(a);
const keysB = Object.keys(b);
if (keysA.length !== keysB.length) return false;
for (const key of keysA) {
if (!deepEqual(a[key], b[key])) return false;
}
return true;
}