-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathVersionedStruct.js
139 lines (114 loc) · 3.01 KB
/
VersionedStruct.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
import {Struct} from './Struct.js';
const getPath = (object, pathArray) => {
return pathArray.reduce((prevObj, key) => prevObj && prevObj[key], object);
};
export class VersionedStruct extends Struct {
constructor(type, versions = {}) {
super();
this.type = type;
this.versions = versions;
if (typeof type === 'string') {
this.versionPath = type.split('.');
}
}
decode(stream, parent, length = 0) {
const res = this._setup(stream, parent, length);
if (typeof this.type === 'string') {
res.version = getPath(parent, this.versionPath);
} else {
res.version = this.type.decode(stream);
}
if (this.versions.header) {
this._parseFields(stream, res, this.versions.header);
}
const fields = this.versions[res.version];
if ((fields == null)) {
throw new Error(`Unknown version ${res.version}`);
}
if (fields instanceof VersionedStruct) {
return fields.decode(stream, parent);
}
this._parseFields(stream, res, fields);
if (this.process != null) {
this.process.call(res, stream);
}
return res;
}
size(val, parent, includePointers = true) {
let key, type;
if (!val) {
throw new Error('Not a fixed size');
}
if (this.preEncode != null) {
this.preEncode.call(val);
}
const ctx = {
parent,
val,
pointerSize: 0
};
let size = 0;
if (typeof this.type !== 'string') {
size += this.type.size(val.version, ctx);
}
if (this.versions.header) {
for (key in this.versions.header) {
type = this.versions.header[key];
if (type.size != null) {
size += type.size(val[key], ctx);
}
}
}
const fields = this.versions[val.version];
if ((fields == null)) {
throw new Error(`Unknown version ${val.version}`);
}
for (key in fields) {
type = fields[key];
if (type.size != null) {
size += type.size(val[key], ctx);
}
}
if (includePointers) {
size += ctx.pointerSize;
}
return size;
}
encode(stream, val, parent) {
let key, type;
if (this.preEncode != null) {
this.preEncode.call(val, stream);
}
const ctx = {
pointers: [],
startOffset: stream.pos,
parent,
val,
pointerSize: 0
};
ctx.pointerOffset = stream.pos + this.size(val, ctx, false);
if (typeof this.type !== 'string') {
this.type.encode(stream, val.version);
}
if (this.versions.header) {
for (key in this.versions.header) {
type = this.versions.header[key];
if (type.encode != null) {
type.encode(stream, val[key], ctx);
}
}
}
const fields = this.versions[val.version];
for (key in fields) {
type = fields[key];
if (type.encode != null) {
type.encode(stream, val[key], ctx);
}
}
let i = 0;
while (i < ctx.pointers.length) {
const ptr = ctx.pointers[i++];
ptr.type.encode(stream, ptr.val, ptr.parent);
}
}
}