wpilog-parser 3.1.2 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -9
- package/dist/catalog-hof8DZhi.mjs +714 -0
- package/dist/cli.mjs +2 -2
- package/dist/index.d.mts +5 -37
- package/dist/index.mjs +8 -1
- package/dist/tsdoc-metadata.json +1 -1
- package/package.json +55 -68
- package/dist/catalog-BRtbeKXA.mjs +0 -1414
|
@@ -0,0 +1,714 @@
|
|
|
1
|
+
import { defineDiagnostics } from "nostics";
|
|
2
|
+
import { StructDependencyGraph, StructError, StructRegistry } from "wpilib-struct";
|
|
3
|
+
// src/byte-offset.ts
|
|
4
|
+
var ByteOffset = class {
|
|
5
|
+
offset;
|
|
6
|
+
constructor(offset = 0) {
|
|
7
|
+
this.offset = offset;
|
|
8
|
+
}
|
|
9
|
+
get() {
|
|
10
|
+
return this.offset;
|
|
11
|
+
}
|
|
12
|
+
advance(bytes) {
|
|
13
|
+
this.offset += bytes;
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
advance8() {
|
|
17
|
+
return this.advance(1);
|
|
18
|
+
}
|
|
19
|
+
advance16() {
|
|
20
|
+
return this.advance(2);
|
|
21
|
+
}
|
|
22
|
+
advance32() {
|
|
23
|
+
return this.advance(4);
|
|
24
|
+
}
|
|
25
|
+
advance64() {
|
|
26
|
+
return this.advance(8);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
// src/diagnostics.ts
|
|
30
|
+
/** Stable codes for errors callers can act on. */
|
|
31
|
+
const diagnostics = defineDiagnostics({ codes: {
|
|
32
|
+
WPILOG_C0002: {
|
|
33
|
+
why: "Output flags cannot be combined",
|
|
34
|
+
fix: "Choose one of --json, --jsonl, --ndjson, or --csv."
|
|
35
|
+
},
|
|
36
|
+
WPILOG_C0003: {
|
|
37
|
+
why: (p) => `File not found: ${p.filePath}`,
|
|
38
|
+
fix: "Check the path and try again with an existing WPILOG file."
|
|
39
|
+
},
|
|
40
|
+
WPILOG_R0001: {
|
|
41
|
+
why: (p) => `Invalid control record type ${p.type}`,
|
|
42
|
+
fix: "Check that the input is an uncorrupted WPILOG file."
|
|
43
|
+
},
|
|
44
|
+
WPILOG_R0002: {
|
|
45
|
+
why: "Not a WPILOG file (truncated header)",
|
|
46
|
+
fix: "Provide a complete WPILOG file."
|
|
47
|
+
},
|
|
48
|
+
WPILOG_R0003: {
|
|
49
|
+
why: "Not a WPILOG file (invalid magic bytes)",
|
|
50
|
+
fix: "Provide a file in WPILOG format."
|
|
51
|
+
},
|
|
52
|
+
WPILOG_R0004: {
|
|
53
|
+
why: "Not a WPILOG file (truncated extra header)",
|
|
54
|
+
fix: "Provide a complete WPILOG file."
|
|
55
|
+
},
|
|
56
|
+
WPILOG_R0005: {
|
|
57
|
+
why: (p) => `Invalid boolean value ${p.byte}`,
|
|
58
|
+
fix: "Use 0 or 1 for boolean values in the log."
|
|
59
|
+
},
|
|
60
|
+
WPILOG_R0006: {
|
|
61
|
+
why: (p) => `No type registered for entry ID ${p.entryId}`,
|
|
62
|
+
fix: "Include a Start control record for this entry before its data records."
|
|
63
|
+
},
|
|
64
|
+
WPILOG_R0007: {
|
|
65
|
+
why: "Failed to lex struct specification",
|
|
66
|
+
fix: "Check the struct specification for invalid characters or tokens."
|
|
67
|
+
},
|
|
68
|
+
WPILOG_R0008: {
|
|
69
|
+
why: "Failed to parse struct specification",
|
|
70
|
+
fix: "Check the struct specification syntax."
|
|
71
|
+
},
|
|
72
|
+
WPILOG_R0009: {
|
|
73
|
+
why: "Enums must be integers",
|
|
74
|
+
fix: "Use an integer type for enum members."
|
|
75
|
+
},
|
|
76
|
+
WPILOG_R0010: {
|
|
77
|
+
why: (p) => `Invalid ${p.width}-bit field for ${p.type} (maximum ${p.maxBits} bits)`,
|
|
78
|
+
fix: (p) => `Use a bit width from 1 to ${p.maxBits}.`
|
|
79
|
+
},
|
|
80
|
+
WPILOG_R0011: {
|
|
81
|
+
why: "Bit-field members must be integers or booleans",
|
|
82
|
+
fix: "Use an integer or boolean type for bit-field members."
|
|
83
|
+
},
|
|
84
|
+
WPILOG_R0012: {
|
|
85
|
+
why: (p) => `Cycle detected in the dependency graph for ${p.name}`,
|
|
86
|
+
fix: "Remove the circular struct reference."
|
|
87
|
+
},
|
|
88
|
+
WPILOG_R0013: {
|
|
89
|
+
why: (p) => `Unknown struct definition: ${p.name}`,
|
|
90
|
+
fix: "Register the struct definition before decoding its records."
|
|
91
|
+
}
|
|
92
|
+
} });
|
|
93
|
+
// src/types.ts
|
|
94
|
+
/** Control record types in the WPILOG format. */
|
|
95
|
+
const ControlRecordType = {
|
|
96
|
+
Start: 0,
|
|
97
|
+
Finish: 1,
|
|
98
|
+
SetMetadata: 2
|
|
99
|
+
};
|
|
100
|
+
/** Discriminated union tag for decoded record types. */
|
|
101
|
+
const RecordType = {
|
|
102
|
+
Control: "control",
|
|
103
|
+
Boolean: "boolean",
|
|
104
|
+
Int64: "int64",
|
|
105
|
+
Float: "float",
|
|
106
|
+
Double: "double",
|
|
107
|
+
String: "string",
|
|
108
|
+
BooleanArray: "boolean[]",
|
|
109
|
+
Int64Array: "int64[]",
|
|
110
|
+
FloatArray: "float[]",
|
|
111
|
+
DoubleArray: "double[]",
|
|
112
|
+
StringArray: "string[]",
|
|
113
|
+
Struct: "struct",
|
|
114
|
+
StructArray: "struct[]",
|
|
115
|
+
Raw: "raw"
|
|
116
|
+
};
|
|
117
|
+
/** Type guard that narrows a {@link DecodedRecord} to a {@link DataRecord}. */
|
|
118
|
+
function isDataRecord(record) {
|
|
119
|
+
return record.type !== RecordType.Control;
|
|
120
|
+
}
|
|
121
|
+
// src/read-records.ts
|
|
122
|
+
const TEXT_ENCODER = new TextEncoder();
|
|
123
|
+
const TEXT_DECODER$1 = new TextDecoder();
|
|
124
|
+
const MAGIC = TEXT_ENCODER.encode("WPILOG");
|
|
125
|
+
function readString(payload, view, offset) {
|
|
126
|
+
const length = view.getUint32(offset.get(), true);
|
|
127
|
+
offset.advance32();
|
|
128
|
+
const value = TEXT_DECODER$1.decode(payload.subarray(offset.get(), offset.get() + length));
|
|
129
|
+
offset.advance(length);
|
|
130
|
+
return value;
|
|
131
|
+
}
|
|
132
|
+
function readControlRecordPayload(payload) {
|
|
133
|
+
const offset = new ByteOffset();
|
|
134
|
+
const view = new DataView(payload.buffer, payload.byteOffset, payload.byteLength);
|
|
135
|
+
const type = view.getUint8(offset.get());
|
|
136
|
+
offset.advance8();
|
|
137
|
+
switch (type) {
|
|
138
|
+
case ControlRecordType.Start: {
|
|
139
|
+
const entryId = view.getUint32(offset.get(), true);
|
|
140
|
+
offset.advance32();
|
|
141
|
+
return {
|
|
142
|
+
controlRecordType: type,
|
|
143
|
+
entryId,
|
|
144
|
+
entryName: readString(payload, view, offset),
|
|
145
|
+
entryType: readString(payload, view, offset),
|
|
146
|
+
entryMetadata: readString(payload, view, offset)
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
case ControlRecordType.Finish: {
|
|
150
|
+
const entryId = view.getUint32(offset.get(), true);
|
|
151
|
+
offset.advance32();
|
|
152
|
+
return {
|
|
153
|
+
controlRecordType: type,
|
|
154
|
+
entryId
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
case ControlRecordType.SetMetadata: {
|
|
158
|
+
const entryId = view.getUint32(offset.get(), true);
|
|
159
|
+
offset.advance32();
|
|
160
|
+
return {
|
|
161
|
+
controlRecordType: type,
|
|
162
|
+
entryId,
|
|
163
|
+
entryMetadata: readString(payload, view, offset)
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
default: throw diagnostics.WPILOG_R0001({ type });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function toUint8Array(input) {
|
|
170
|
+
if (input instanceof Uint8Array) return input;
|
|
171
|
+
return new Uint8Array(input);
|
|
172
|
+
}
|
|
173
|
+
function openDataLog(input) {
|
|
174
|
+
const bytes = toUint8Array(input);
|
|
175
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
176
|
+
if (bytes.byteLength < 12) throw diagnostics.WPILOG_R0002();
|
|
177
|
+
for (let i = 0; i < MAGIC.byteLength; i++) if (bytes[i] !== MAGIC[i]) throw diagnostics.WPILOG_R0003();
|
|
178
|
+
const recordsOffset = 12 + view.getUint32(8, true);
|
|
179
|
+
if (recordsOffset > bytes.byteLength) throw diagnostics.WPILOG_R0004();
|
|
180
|
+
return {
|
|
181
|
+
bytes,
|
|
182
|
+
view,
|
|
183
|
+
header: {
|
|
184
|
+
version: {
|
|
185
|
+
major: view.getUint8(7),
|
|
186
|
+
minor: view.getUint8(6)
|
|
187
|
+
},
|
|
188
|
+
extraHeader: TEXT_DECODER$1.decode(bytes.subarray(12, recordsOffset))
|
|
189
|
+
},
|
|
190
|
+
recordsOffset
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
function readVarInt(view, offset, length) {
|
|
194
|
+
switch (length) {
|
|
195
|
+
case 1: return view.getUint8(offset);
|
|
196
|
+
case 2: return view.getUint16(offset, true);
|
|
197
|
+
case 3: return view.getUint8(offset + 2) << 16 | view.getUint8(offset + 1) << 8 | view.getUint8(offset);
|
|
198
|
+
case 4: return view.getUint32(offset, true);
|
|
199
|
+
default: throw new RangeError(`Invalid varint length ${length}`);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
function readTimestamp(view, offset, length) {
|
|
203
|
+
switch (length) {
|
|
204
|
+
case 1: return BigInt(view.getUint8(offset));
|
|
205
|
+
case 2: return BigInt(view.getUint16(offset, true));
|
|
206
|
+
case 3: return BigInt(view.getUint16(offset, true) + view.getUint8(offset + 2) * 2 ** 16);
|
|
207
|
+
case 4: return BigInt(view.getUint32(offset, true));
|
|
208
|
+
case 5: return BigInt(view.getUint32(offset, true) + view.getUint8(offset + 4) * 2 ** 32);
|
|
209
|
+
case 6: return BigInt(view.getUint32(offset, true) + view.getUint16(offset + 4, true) * 2 ** 32);
|
|
210
|
+
case 7: {
|
|
211
|
+
const low = view.getUint32(offset, true);
|
|
212
|
+
const high = view.getUint16(offset + 4, true) + view.getUint8(offset + 6) * 2 ** 16;
|
|
213
|
+
return BigInt(high) << 32n | BigInt(low);
|
|
214
|
+
}
|
|
215
|
+
case 8: return view.getBigUint64(offset, true);
|
|
216
|
+
default: throw new RangeError(`Invalid timestamp length ${length}`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
/** Mutable record cursor used by the fused decoder to avoid intermediate record allocations. */
|
|
220
|
+
var RecordCursor = class {
|
|
221
|
+
bytes;
|
|
222
|
+
view;
|
|
223
|
+
header;
|
|
224
|
+
entryId = 0;
|
|
225
|
+
timestamp = 0n;
|
|
226
|
+
payloadOffset = 0;
|
|
227
|
+
payloadSize = 0;
|
|
228
|
+
offset;
|
|
229
|
+
constructor(input) {
|
|
230
|
+
const source = openDataLog(input);
|
|
231
|
+
this.bytes = source.bytes;
|
|
232
|
+
this.view = source.view;
|
|
233
|
+
this.header = source.header;
|
|
234
|
+
this.offset = source.recordsOffset;
|
|
235
|
+
}
|
|
236
|
+
next() {
|
|
237
|
+
const total = this.bytes.byteLength;
|
|
238
|
+
let offset = this.offset;
|
|
239
|
+
if (offset >= total) return false;
|
|
240
|
+
const view = this.view;
|
|
241
|
+
const bitfield = view.getUint8(offset++);
|
|
242
|
+
const entryIdLength = 1 + (bitfield & 3);
|
|
243
|
+
const payloadSizeLength = 1 + (bitfield >> 2 & 3);
|
|
244
|
+
const timestampLength = 1 + (bitfield >> 4 & 7);
|
|
245
|
+
if (offset + entryIdLength + payloadSizeLength + timestampLength > total) {
|
|
246
|
+
this.offset = total;
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
const entryId = readVarInt(view, offset, entryIdLength);
|
|
250
|
+
offset += entryIdLength;
|
|
251
|
+
const payloadSize = readVarInt(view, offset, payloadSizeLength);
|
|
252
|
+
offset += payloadSizeLength;
|
|
253
|
+
const timestamp = readTimestamp(view, offset, timestampLength);
|
|
254
|
+
offset += timestampLength;
|
|
255
|
+
if (offset + payloadSize > total) {
|
|
256
|
+
this.offset = total;
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
this.entryId = entryId;
|
|
260
|
+
this.timestamp = timestamp;
|
|
261
|
+
this.payloadOffset = offset;
|
|
262
|
+
this.payloadSize = payloadSize;
|
|
263
|
+
this.offset = offset + payloadSize;
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
getPayload() {
|
|
267
|
+
return this.bytes.subarray(this.payloadOffset, this.payloadOffset + this.payloadSize);
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
/**
|
|
271
|
+
* Read raw WPILOG records from an in-memory buffer.
|
|
272
|
+
*
|
|
273
|
+
* Yields a header record first, then control and data records in order.
|
|
274
|
+
* Data record payloads are not decoded. Prefer `parseDataLog` when raw records
|
|
275
|
+
* or an intermediate transformation are not needed.
|
|
276
|
+
*/
|
|
277
|
+
function* readRecords(input) {
|
|
278
|
+
const { bytes, view, header, recordsOffset } = openDataLog(input);
|
|
279
|
+
const total = bytes.byteLength;
|
|
280
|
+
let offset = recordsOffset;
|
|
281
|
+
yield {
|
|
282
|
+
kind: "header",
|
|
283
|
+
header
|
|
284
|
+
};
|
|
285
|
+
while (offset < total) {
|
|
286
|
+
const bitfield = view.getUint8(offset++);
|
|
287
|
+
const entryIdLength = 1 + (bitfield & 3);
|
|
288
|
+
const payloadSizeLength = 1 + (bitfield >> 2 & 3);
|
|
289
|
+
const timestampLength = 1 + (bitfield >> 4 & 7);
|
|
290
|
+
if (offset + entryIdLength + payloadSizeLength + timestampLength > total) return;
|
|
291
|
+
const entryId = readVarInt(view, offset, entryIdLength);
|
|
292
|
+
offset += entryIdLength;
|
|
293
|
+
const payloadSize = readVarInt(view, offset, payloadSizeLength);
|
|
294
|
+
offset += payloadSizeLength;
|
|
295
|
+
const timestamp = readTimestamp(view, offset, timestampLength);
|
|
296
|
+
offset += timestampLength;
|
|
297
|
+
if (offset + payloadSize > total) return;
|
|
298
|
+
const payload = bytes.subarray(offset, offset + payloadSize);
|
|
299
|
+
offset += payloadSize;
|
|
300
|
+
if (entryId === 0) yield {
|
|
301
|
+
kind: "control",
|
|
302
|
+
entryId,
|
|
303
|
+
timestamp,
|
|
304
|
+
payload: readControlRecordPayload(payload)
|
|
305
|
+
};
|
|
306
|
+
else yield {
|
|
307
|
+
kind: "data",
|
|
308
|
+
record: {
|
|
309
|
+
entryId,
|
|
310
|
+
timestamp,
|
|
311
|
+
payload
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
// src/struct/diagnostics.ts
|
|
317
|
+
/** Preserve the published WPILOG diagnostics at the protocol boundary. */
|
|
318
|
+
function withStructDiagnostics(action) {
|
|
319
|
+
try {
|
|
320
|
+
return action();
|
|
321
|
+
} catch (error) {
|
|
322
|
+
if (!(error instanceof StructError)) throw error;
|
|
323
|
+
switch (error.code) {
|
|
324
|
+
case "STRUCT_R0007": throw diagnostics.WPILOG_R0007({ cause: error.cause });
|
|
325
|
+
case "STRUCT_R0008": throw diagnostics.WPILOG_R0008({ cause: error.cause });
|
|
326
|
+
case "STRUCT_R0009": throw diagnostics.WPILOG_R0009();
|
|
327
|
+
case "STRUCT_R0010": throw diagnostics.WPILOG_R0010({
|
|
328
|
+
type: error.details.type,
|
|
329
|
+
width: error.details.width,
|
|
330
|
+
maxBits: error.details.maxBits
|
|
331
|
+
});
|
|
332
|
+
case "STRUCT_R0011": throw diagnostics.WPILOG_R0011();
|
|
333
|
+
case "STRUCT_R0012": throw diagnostics.WPILOG_R0012({ name: error.details.name });
|
|
334
|
+
case "STRUCT_R0013": throw diagnostics.WPILOG_R0013({ name: error.details.name });
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
// src/struct/struct-decode-queue.ts
|
|
339
|
+
var StructDecodeQueue = class {
|
|
340
|
+
graph = new StructDependencyGraph();
|
|
341
|
+
queuedRecords = /* @__PURE__ */ new Map();
|
|
342
|
+
onStructDefined;
|
|
343
|
+
constructor(onStructDefined) {
|
|
344
|
+
this.onStructDefined = onStructDefined;
|
|
345
|
+
}
|
|
346
|
+
queueStructRecord(structName, record) {
|
|
347
|
+
const existing = this.queuedRecords.get(structName);
|
|
348
|
+
if (existing) existing.push(record);
|
|
349
|
+
else this.queuedRecords.set(structName, [record]);
|
|
350
|
+
}
|
|
351
|
+
registerSchema(structName, innerStructNames) {
|
|
352
|
+
withStructDiagnostics(() => this.graph.registerSchema(structName, innerStructNames));
|
|
353
|
+
this.checkDecodableStructs();
|
|
354
|
+
}
|
|
355
|
+
checkDecodableStructs() {
|
|
356
|
+
for (const structName of this.graph.getDecodableStructs()) {
|
|
357
|
+
const records = this.queuedRecords.get(structName);
|
|
358
|
+
if (records) {
|
|
359
|
+
this.onStructDefined(structName, records);
|
|
360
|
+
this.queuedRecords.delete(structName);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
// src/struct/struct-registry.ts
|
|
366
|
+
/** WPILOG schema registration and queued-record policy stay outside the shared codec. */
|
|
367
|
+
var StructRegistry$1 = class {
|
|
368
|
+
registry = new StructRegistry();
|
|
369
|
+
queue;
|
|
370
|
+
constructor(queue) {
|
|
371
|
+
this.queue = queue;
|
|
372
|
+
}
|
|
373
|
+
register(name, definition) {
|
|
374
|
+
withStructDiagnostics(() => this.registry.register(name, definition));
|
|
375
|
+
this.queue.registerSchema(name, this.registry.getDefinition(name).map((member) => member.value));
|
|
376
|
+
}
|
|
377
|
+
getByteLength(name) {
|
|
378
|
+
return this.registry.getByteLength(name);
|
|
379
|
+
}
|
|
380
|
+
decode(name, payload, offset) {
|
|
381
|
+
const result = this.registry.decode(name, payload, offset?.get());
|
|
382
|
+
if (typeof result !== "string") offset?.advance(this.registry.getByteLength(name));
|
|
383
|
+
return result;
|
|
384
|
+
}
|
|
385
|
+
decodeArray(name, payload) {
|
|
386
|
+
return this.registry.decodeArray(name.slice(0, -2), payload);
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
// src/decode-records.ts
|
|
390
|
+
const TEXT_DECODER = new TextDecoder();
|
|
391
|
+
const STRUCT_PREFIX = "struct:";
|
|
392
|
+
const STRUCT_ARRAY_SUFFIX = "[]";
|
|
393
|
+
const STRUCT_SCHEMA_NAME_PREFIX = "/.schema/struct:";
|
|
394
|
+
function byteToBoolean(byte) {
|
|
395
|
+
switch (byte) {
|
|
396
|
+
case 0: return false;
|
|
397
|
+
case 1: return true;
|
|
398
|
+
default: throw diagnostics.WPILOG_R0005({ byte });
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
function normalizeEntryName(rawName) {
|
|
402
|
+
if (rawName.startsWith("/")) return rawName;
|
|
403
|
+
return `/${rawName}`;
|
|
404
|
+
}
|
|
405
|
+
var RecordDecoder = class {
|
|
406
|
+
delayedRecords = [];
|
|
407
|
+
context = /* @__PURE__ */ new Map();
|
|
408
|
+
recordContexts = /* @__PURE__ */ new WeakMap();
|
|
409
|
+
structDecodeQueue;
|
|
410
|
+
structRegistry;
|
|
411
|
+
options;
|
|
412
|
+
constructor(options) {
|
|
413
|
+
this.options = options;
|
|
414
|
+
this.structDecodeQueue = new StructDecodeQueue((_structName, queuedRecords) => {
|
|
415
|
+
for (const raw of queuedRecords) {
|
|
416
|
+
const context = this.recordContexts.get(raw);
|
|
417
|
+
if (!context) continue;
|
|
418
|
+
const { entryType, name, metadata } = context;
|
|
419
|
+
const structName = entryType.slice(7);
|
|
420
|
+
const base = {
|
|
421
|
+
entryId: raw.entryId,
|
|
422
|
+
timestamp: raw.timestamp,
|
|
423
|
+
name,
|
|
424
|
+
metadata,
|
|
425
|
+
structName
|
|
426
|
+
};
|
|
427
|
+
if (entryType.endsWith(STRUCT_ARRAY_SUFFIX)) {
|
|
428
|
+
const payload = this.structRegistry.decodeArray(structName, raw.payload);
|
|
429
|
+
if (typeof payload !== "string") this.delayedRecords.push({
|
|
430
|
+
...base,
|
|
431
|
+
type: RecordType.StructArray,
|
|
432
|
+
payload
|
|
433
|
+
});
|
|
434
|
+
} else {
|
|
435
|
+
const payload = this.structRegistry.decode(structName, raw.payload);
|
|
436
|
+
if (typeof payload !== "string") this.delayedRecords.push({
|
|
437
|
+
...base,
|
|
438
|
+
type: RecordType.Struct,
|
|
439
|
+
payload
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
});
|
|
444
|
+
this.structRegistry = new StructRegistry$1(this.structDecodeQueue);
|
|
445
|
+
}
|
|
446
|
+
decodeControl(entryId, timestamp, payload) {
|
|
447
|
+
switch (payload.controlRecordType) {
|
|
448
|
+
case ControlRecordType.Start:
|
|
449
|
+
this.context.set(payload.entryId, {
|
|
450
|
+
name: normalizeEntryName(payload.entryName),
|
|
451
|
+
entryType: payload.entryType,
|
|
452
|
+
metadata: payload.entryMetadata
|
|
453
|
+
});
|
|
454
|
+
break;
|
|
455
|
+
case ControlRecordType.Finish:
|
|
456
|
+
this.context.delete(payload.entryId);
|
|
457
|
+
break;
|
|
458
|
+
case ControlRecordType.SetMetadata: {
|
|
459
|
+
const existing = this.context.get(payload.entryId);
|
|
460
|
+
if (existing) this.context.set(payload.entryId, {
|
|
461
|
+
...existing,
|
|
462
|
+
metadata: payload.entryMetadata
|
|
463
|
+
});
|
|
464
|
+
break;
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
return {
|
|
468
|
+
entryId,
|
|
469
|
+
timestamp,
|
|
470
|
+
type: RecordType.Control,
|
|
471
|
+
payload
|
|
472
|
+
};
|
|
473
|
+
}
|
|
474
|
+
decodeData(entryId, timestamp, bytes, view, payloadOffset, payloadSize) {
|
|
475
|
+
const context = this.context.get(entryId);
|
|
476
|
+
if (!context) {
|
|
477
|
+
if (this.options.strict) throw diagnostics.WPILOG_R0006({ entryId });
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
const base = {
|
|
481
|
+
entryId,
|
|
482
|
+
timestamp,
|
|
483
|
+
name: context.name,
|
|
484
|
+
metadata: context.metadata
|
|
485
|
+
};
|
|
486
|
+
switch (context.entryType) {
|
|
487
|
+
case "boolean": return {
|
|
488
|
+
...base,
|
|
489
|
+
type: RecordType.Boolean,
|
|
490
|
+
payload: byteToBoolean(view.getUint8(payloadOffset))
|
|
491
|
+
};
|
|
492
|
+
case "int64": return {
|
|
493
|
+
...base,
|
|
494
|
+
type: RecordType.Int64,
|
|
495
|
+
payload: view.getBigInt64(payloadOffset, true)
|
|
496
|
+
};
|
|
497
|
+
case "float": return {
|
|
498
|
+
...base,
|
|
499
|
+
type: RecordType.Float,
|
|
500
|
+
payload: view.getFloat32(payloadOffset, true)
|
|
501
|
+
};
|
|
502
|
+
case "double": return {
|
|
503
|
+
...base,
|
|
504
|
+
type: RecordType.Double,
|
|
505
|
+
payload: view.getFloat64(payloadOffset, true)
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
const payload = payloadOffset === 0 && payloadSize === bytes.byteLength ? bytes : bytes.subarray(payloadOffset, payloadOffset + payloadSize);
|
|
509
|
+
const raw = {
|
|
510
|
+
entryId,
|
|
511
|
+
timestamp,
|
|
512
|
+
payload
|
|
513
|
+
};
|
|
514
|
+
const payloadView = new DataView(payload.buffer, payload.byteOffset, payload.byteLength);
|
|
515
|
+
const decoded = decodeNonScalarPayload(raw, context.entryType, context.name, context.metadata, payloadView, this.structRegistry, this.structDecodeQueue);
|
|
516
|
+
if (!decoded) this.recordContexts.set(raw, context);
|
|
517
|
+
return decoded;
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
/**
|
|
521
|
+
* Decode raw WPILOG records into typed values.
|
|
522
|
+
*
|
|
523
|
+
* Accepts the output of {@link readRecords} and yields fully decoded records,
|
|
524
|
+
* including struct decoding with dependency resolution.
|
|
525
|
+
*
|
|
526
|
+
* By default, data records that reference an entry ID with no preceding Start
|
|
527
|
+
* control record are silently skipped. Pass `{ strict: true }` to throw instead.
|
|
528
|
+
*/
|
|
529
|
+
function* decodeRecords(records, options = { strict: false }) {
|
|
530
|
+
const decoder = new RecordDecoder(options);
|
|
531
|
+
for (const readRecord of records) {
|
|
532
|
+
if (readRecord.kind === "header") continue;
|
|
533
|
+
if (readRecord.kind === "control") {
|
|
534
|
+
yield decoder.decodeControl(readRecord.entryId, readRecord.timestamp, readRecord.payload);
|
|
535
|
+
continue;
|
|
536
|
+
}
|
|
537
|
+
const raw = readRecord.record;
|
|
538
|
+
const view = new DataView(raw.payload.buffer, raw.payload.byteOffset, raw.payload.byteLength);
|
|
539
|
+
const decoded = decoder.decodeData(raw.entryId, raw.timestamp, raw.payload, view, 0, raw.payload.byteLength);
|
|
540
|
+
if (decoded) yield decoded;
|
|
541
|
+
if (decoder.delayedRecords.length > 0) {
|
|
542
|
+
yield* decoder.delayedRecords;
|
|
543
|
+
decoder.delayedRecords.length = 0;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Read and decode a WPILOG buffer in a single pass.
|
|
549
|
+
*
|
|
550
|
+
* This is the preferred API for fully decoded records. It avoids allocating the
|
|
551
|
+
* intermediate records produced by composing {@link readRecords} and
|
|
552
|
+
* {@link decodeRecords} while preserving the same output and strict-mode behavior.
|
|
553
|
+
*/
|
|
554
|
+
function* parseDataLog(input, options = { strict: false }) {
|
|
555
|
+
const cursor = new RecordCursor(input);
|
|
556
|
+
const decoder = new RecordDecoder(options);
|
|
557
|
+
while (cursor.next()) {
|
|
558
|
+
if (cursor.entryId === 0) {
|
|
559
|
+
yield decoder.decodeControl(cursor.entryId, cursor.timestamp, readControlRecordPayload(cursor.getPayload()));
|
|
560
|
+
continue;
|
|
561
|
+
}
|
|
562
|
+
const decoded = decoder.decodeData(cursor.entryId, cursor.timestamp, cursor.bytes, cursor.view, cursor.payloadOffset, cursor.payloadSize);
|
|
563
|
+
if (decoded) yield decoded;
|
|
564
|
+
if (decoder.delayedRecords.length > 0) {
|
|
565
|
+
yield* decoder.delayedRecords;
|
|
566
|
+
decoder.delayedRecords.length = 0;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
/**
|
|
571
|
+
* Decode non-scalar payloads that require a byte slice or payload-local view.
|
|
572
|
+
* Fixed-width scalar types return from {@link RecordDecoder#decodeData} before this function so their payloads can be read directly from the input buffer.
|
|
573
|
+
*/
|
|
574
|
+
function decodeNonScalarPayload(raw, entryType, name, metadata, view, structRegistry, structDecodeQueue) {
|
|
575
|
+
const base = {
|
|
576
|
+
entryId: raw.entryId,
|
|
577
|
+
timestamp: raw.timestamp,
|
|
578
|
+
name,
|
|
579
|
+
metadata
|
|
580
|
+
};
|
|
581
|
+
switch (entryType) {
|
|
582
|
+
case "string": return {
|
|
583
|
+
...base,
|
|
584
|
+
type: RecordType.String,
|
|
585
|
+
payload: TEXT_DECODER.decode(raw.payload)
|
|
586
|
+
};
|
|
587
|
+
case "boolean[]": {
|
|
588
|
+
const payload = [];
|
|
589
|
+
for (let i = 0; i < raw.payload.byteLength; i++) payload.push(byteToBoolean(view.getUint8(i)));
|
|
590
|
+
return {
|
|
591
|
+
...base,
|
|
592
|
+
type: RecordType.BooleanArray,
|
|
593
|
+
payload
|
|
594
|
+
};
|
|
595
|
+
}
|
|
596
|
+
case "int64[]": {
|
|
597
|
+
const payload = [];
|
|
598
|
+
for (let i = 0; i < raw.payload.byteLength; i += 8) payload.push(view.getBigUint64(i, true));
|
|
599
|
+
return {
|
|
600
|
+
...base,
|
|
601
|
+
type: RecordType.Int64Array,
|
|
602
|
+
payload
|
|
603
|
+
};
|
|
604
|
+
}
|
|
605
|
+
case "float[]": {
|
|
606
|
+
const payload = [];
|
|
607
|
+
for (let i = 0; i < raw.payload.byteLength; i += 4) payload.push(view.getFloat32(i, true));
|
|
608
|
+
return {
|
|
609
|
+
...base,
|
|
610
|
+
type: RecordType.FloatArray,
|
|
611
|
+
payload
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
case "double[]": {
|
|
615
|
+
const payload = [];
|
|
616
|
+
for (let i = 0; i < raw.payload.byteLength; i += 8) payload.push(view.getFloat64(i, true));
|
|
617
|
+
return {
|
|
618
|
+
...base,
|
|
619
|
+
type: RecordType.DoubleArray,
|
|
620
|
+
payload
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
case "string[]": {
|
|
624
|
+
const payload = [];
|
|
625
|
+
const offset = new ByteOffset();
|
|
626
|
+
const arrayLength = view.getUint32(offset.get(), true);
|
|
627
|
+
offset.advance32();
|
|
628
|
+
for (let i = 0; i < arrayLength; i++) {
|
|
629
|
+
const stringLength = view.getUint32(offset.get(), true);
|
|
630
|
+
offset.advance32();
|
|
631
|
+
const string = TEXT_DECODER.decode(raw.payload.subarray(offset.get(), offset.get() + stringLength));
|
|
632
|
+
offset.advance(stringLength);
|
|
633
|
+
payload.push(string);
|
|
634
|
+
}
|
|
635
|
+
return {
|
|
636
|
+
...base,
|
|
637
|
+
type: RecordType.StringArray,
|
|
638
|
+
payload
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
case "structschema": {
|
|
642
|
+
const structName = name.slice(name.indexOf(STRUCT_SCHEMA_NAME_PREFIX) + 16);
|
|
643
|
+
const payload = TEXT_DECODER.decode(raw.payload);
|
|
644
|
+
structRegistry.register(structName, payload);
|
|
645
|
+
return {
|
|
646
|
+
...base,
|
|
647
|
+
type: RecordType.String,
|
|
648
|
+
payload
|
|
649
|
+
};
|
|
650
|
+
}
|
|
651
|
+
default:
|
|
652
|
+
if (entryType.startsWith(STRUCT_PREFIX)) {
|
|
653
|
+
const structName = entryType.slice(7);
|
|
654
|
+
if (entryType.endsWith(STRUCT_ARRAY_SUFFIX)) {
|
|
655
|
+
const decoded = structRegistry.decodeArray(structName, raw.payload);
|
|
656
|
+
if (typeof decoded === "string") {
|
|
657
|
+
structDecodeQueue.queueStructRecord(decoded, raw);
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
return {
|
|
661
|
+
...base,
|
|
662
|
+
type: RecordType.StructArray,
|
|
663
|
+
structName,
|
|
664
|
+
payload: decoded
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
const decoded = structRegistry.decode(structName, raw.payload);
|
|
668
|
+
if (typeof decoded === "string") {
|
|
669
|
+
structDecodeQueue.queueStructRecord(decoded, raw);
|
|
670
|
+
return;
|
|
671
|
+
}
|
|
672
|
+
return {
|
|
673
|
+
...base,
|
|
674
|
+
type: RecordType.Struct,
|
|
675
|
+
structName,
|
|
676
|
+
payload: decoded
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
return {
|
|
680
|
+
...base,
|
|
681
|
+
type: RecordType.Raw,
|
|
682
|
+
payload: raw.payload
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
// src/catalog.ts
|
|
687
|
+
/**
|
|
688
|
+
* Collect all entry definitions from a WPILOG record stream.
|
|
689
|
+
*
|
|
690
|
+
* Iterates through records, collecting Start control records.
|
|
691
|
+
* Data record bytes are still read from the stream but not decoded,
|
|
692
|
+
* making this much cheaper than full decoding via {@link parseDataLog}.
|
|
693
|
+
*/
|
|
694
|
+
function* catalogEntries(records) {
|
|
695
|
+
const entries = /* @__PURE__ */ new Map();
|
|
696
|
+
for (const record of records) {
|
|
697
|
+
if (record.kind !== "control") continue;
|
|
698
|
+
if (record.payload.controlRecordType === ControlRecordType.Start) {
|
|
699
|
+
const entry = {
|
|
700
|
+
entryId: record.payload.entryId,
|
|
701
|
+
name: normalizeEntryName(record.payload.entryName),
|
|
702
|
+
type: record.payload.entryType,
|
|
703
|
+
metadata: record.payload.entryMetadata
|
|
704
|
+
};
|
|
705
|
+
entries.set(record.payload.entryId, entry);
|
|
706
|
+
yield entry;
|
|
707
|
+
}
|
|
708
|
+
if (record.payload.controlRecordType === ControlRecordType.SetMetadata) {
|
|
709
|
+
const existing = entries.get(record.payload.entryId);
|
|
710
|
+
if (existing) existing.metadata = record.payload.entryMetadata;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
export { readRecords as a, isDataRecord as c, withStructDiagnostics as i, diagnostics as l, decodeRecords as n, ControlRecordType as o, parseDataLog as r, RecordType as s, catalogEntries as t };
|