forked from geckosio/typed-array-buffer-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.ts
196 lines (174 loc) · 5.43 KB
/
schema.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
import {SchemaDefinition} from './types';
import {isTypedArrayView, stringToHash} from './utils';
export class Schema<T = Record<string, any>> {
private static _schemas: Map<string, Schema> = new Map();
public startsAt?: number;
private _bytes: number = 0;
private _id: string;
private _name: string;
private _struct: SchemaDefinition<T>;
public get id(): string {
return this._id;
}
public get name(): string {
return this._name;
}
public get struct(): SchemaDefinition<T> {
return this._struct;
}
public get bytes(): number {
return this._bytes;
}
public constructor(name: string, struct: SchemaDefinition<T>) {
this._name = name;
this._struct = struct;
this._id = Schema.newHash(name, struct);
// Schema.Validation(_struct);
this.calcBytes();
Schema._schemas.set(this._id, this);
}
public static getInstanceById(id: string): Schema | undefined {
return this._schemas.get(id);
}
// public static Validation(struct: Object) {
// // do all the validation here (as static me)
// }
private static newHash<T>(name: string, struct: SchemaDefinition<T>) {
const hash = stringToHash(JSON.stringify(struct) + name);
if (hash.length !== 4) {
throw new Error('Hash has not length of 4');
}
return `#${hash}`;
}
// in the outer schema, call this to reconstruct all the inner schemas
public reconstruct(): any {
// const reconstructed = {};
// const remaining: any[] = [];
// // const keys = Object.keys(this.struct);
// remaining.push(...Object.values(this._struct));
// for (const key in this._struct) {
// const current = this._struct[key];
// if (isTypedArrayView(current)) {
// reconstructed[key] =
// }
// }
// return reconstructed;
}
public deserialize(view: DataView, bytesRef: {bytes: number}): any {
let data = {};
let bytes = bytesRef.bytes;
for (const key in this._struct) {
if (!Object.prototype.hasOwnProperty.call(this._struct, key)) {
continue;
}
const prop = this._struct[key];
console.log('prop:', prop);
// Handle specialTypes (e.g. x: {type: int16, digits: 2})
let specialTypes;
if (prop?.type?._type && prop?.type?._bytes) {
specialTypes = prop;
prop._type = prop.type._type;
prop._bytes = prop.type._bytes;
}
if (prop?._type && prop?._bytes) {
const _type = prop._type;
const _bytes = prop._bytes;
let value;
if (_type === 'String8') {
value = '';
const length = prop.length || 12;
for (let i = 0; i < length; i++) {
const char = String.fromCharCode(view.getUint8(bytes));
value += char;
bytes++;
}
}
if (_type === 'String16') {
value = '';
const length = prop.length || 12;
for (let i = 0; i < length; i++) {
const char = String.fromCharCode(view.getUint16(bytes));
value += char;
bytes += 2;
}
}
if (_type === 'Int8Array') {
value = view.getInt8(bytes);
bytes += _bytes;
}
if (_type === 'Uint8Array') {
value = view.getUint8(bytes);
bytes += _bytes;
}
if (_type === 'Int16Array') {
value = view.getInt16(bytes);
bytes += _bytes;
}
if (_type === 'Uint16Array') {
value = view.getUint16(bytes);
bytes += _bytes;
}
if (_type === 'Int32Array') {
value = view.getInt32(bytes);
bytes += _bytes;
}
if (_type === 'Uint32Array') {
value = view.getUint32(bytes);
bytes += _bytes;
}
if (_type === 'BigInt64Array') {
value = parseInt(view.getBigInt64(bytes).toString());
bytes += _bytes;
}
if (_type === 'BigUint64Array') {
value = parseInt(view.getBigUint64(bytes).toString());
bytes += _bytes;
}
if (_type === 'Float32Array') {
value = view.getFloat32(bytes);
bytes += _bytes;
}
if (_type === 'Float64Array') {
value = view.getFloat64(bytes);
bytes += _bytes;
}
// apply special types options
if (typeof value === 'number' && specialTypes?.digits) {
value *= Math.pow(10, -specialTypes.digits);
value = parseFloat(value.toFixed(specialTypes.digits));
}
data = {...data, [key]: value};
}
}
bytesRef.bytes = bytes;
return data;
}
private calcBytes() {
const iterate = (obj: Record<any, any>) => {
for (const property in obj) {
const type = obj?._type || obj?.type?._type;
const bytes = obj._bytes || obj.type?._bytes;
if (!type && Object.prototype.hasOwnProperty.call(obj, property)) {
if (typeof obj[property] === 'object') {
iterate(obj[property]);
}
} else {
if (property !== '_type' && property !== 'type') {
return;
}
if (!bytes) {
return;
}
// we multiply the bytes by the String8 / String16 length.
if (type === 'String8' || type === 'String16') {
const length = obj.length || 12;
this._bytes += bytes * length;
} else {
this._bytes += bytes;
}
}
}
};
iterate(this._struct);
}
}