wpilog-parser 3.2.2 → 4.0.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.
@@ -201,18 +201,14 @@ function readVarInt(view, offset, length) {
201
201
  }
202
202
  function readTimestamp(view, offset, length) {
203
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);
204
+ case 1: return view.getUint8(offset);
205
+ case 2: return view.getUint16(offset, true);
206
+ case 3: return view.getUint16(offset, true) + view.getUint8(offset + 2) * 2 ** 16;
207
+ case 4: return view.getUint32(offset, true);
208
+ case 5: return view.getUint32(offset, true) + view.getUint8(offset + 4) * 2 ** 32;
209
+ case 6: return view.getUint32(offset, true) + view.getUint16(offset + 4, true) * 2 ** 32;
210
+ case 7: return view.getUint32(offset, true) + (view.getUint16(offset + 4, true) + view.getUint8(offset + 6) * 2 ** 16) * 2 ** 32;
211
+ case 8: return view.getUint32(offset, true) + view.getUint32(offset + 4, true) * 2 ** 32;
216
212
  default: throw new RangeError(`Invalid timestamp length ${length}`);
217
213
  }
218
214
  }
@@ -222,7 +218,7 @@ var RecordCursor = class {
222
218
  view;
223
219
  header;
224
220
  entryId = 0;
225
- timestamp = 0n;
221
+ timestamp = 0;
226
222
  payloadOffset = 0;
227
223
  payloadSize = 0;
228
224
  offset;
@@ -395,12 +391,10 @@ const STRUCT_SCHEMA_NAME_PREFIX = "/.schema/struct:";
395
391
  * Build a non-struct data record as one flat object literal.
396
392
  * Struct records are built inline with the same field order plus `structName`.
397
393
  */
398
- function dataRecord(entryId, timestamp, name, metadata, type, payload) {
394
+ function dataRecord(entry, timestamp, type, payload) {
399
395
  return {
400
- entryId,
396
+ entry,
401
397
  timestamp,
402
- name,
403
- metadata,
404
398
  type,
405
399
  payload
406
400
  };
@@ -418,8 +412,8 @@ function normalizeEntryName(rawName) {
418
412
  }
419
413
  var RecordDecoder = class {
420
414
  delayedRecords = [];
421
- context = /* @__PURE__ */ new Map();
422
- recordContexts = /* @__PURE__ */ new WeakMap();
415
+ entries = /* @__PURE__ */ new Map();
416
+ recordEntries = /* @__PURE__ */ new WeakMap();
423
417
  structDecodeQueue;
424
418
  structRegistry;
425
419
  options;
@@ -427,17 +421,14 @@ var RecordDecoder = class {
427
421
  this.options = options;
428
422
  this.structDecodeQueue = new StructDecodeQueue((_structName, queuedRecords) => {
429
423
  for (const raw of queuedRecords) {
430
- const context = this.recordContexts.get(raw);
431
- if (!context) continue;
432
- const { entryType, name, metadata } = context;
433
- const structName = entryType.slice(7);
434
- if (entryType.endsWith(STRUCT_ARRAY_SUFFIX)) {
424
+ const entry = this.recordEntries.get(raw);
425
+ if (!entry) continue;
426
+ const structName = entry.type.slice(7);
427
+ if (entry.type.endsWith(STRUCT_ARRAY_SUFFIX)) {
435
428
  const payload = this.structRegistry.decodeArray(structName, raw.payload);
436
429
  if (typeof payload !== "string") this.delayedRecords.push({
437
- entryId: raw.entryId,
430
+ entry,
438
431
  timestamp: raw.timestamp,
439
- name,
440
- metadata,
441
432
  type: RecordType.StructArray,
442
433
  structName,
443
434
  payload
@@ -445,10 +436,8 @@ var RecordDecoder = class {
445
436
  } else {
446
437
  const payload = this.structRegistry.decode(structName, raw.payload);
447
438
  if (typeof payload !== "string") this.delayedRecords.push({
448
- entryId: raw.entryId,
439
+ entry,
449
440
  timestamp: raw.timestamp,
450
- name,
451
- metadata,
452
441
  type: RecordType.Struct,
453
442
  structName,
454
443
  payload
@@ -461,18 +450,19 @@ var RecordDecoder = class {
461
450
  decodeControl(entryId, timestamp, payload) {
462
451
  switch (payload.controlRecordType) {
463
452
  case ControlRecordType.Start:
464
- this.context.set(payload.entryId, {
453
+ this.entries.set(payload.entryId, {
454
+ entryId: payload.entryId,
465
455
  name: normalizeEntryName(payload.entryName),
466
- entryType: payload.entryType,
456
+ type: payload.entryType,
467
457
  metadata: payload.entryMetadata
468
458
  });
469
459
  break;
470
460
  case ControlRecordType.Finish:
471
- this.context.delete(payload.entryId);
461
+ this.entries.delete(payload.entryId);
472
462
  break;
473
463
  case ControlRecordType.SetMetadata: {
474
- const existing = this.context.get(payload.entryId);
475
- if (existing) this.context.set(payload.entryId, {
464
+ const existing = this.entries.get(payload.entryId);
465
+ if (existing) this.entries.set(payload.entryId, {
476
466
  ...existing,
477
467
  metadata: payload.entryMetadata
478
468
  });
@@ -487,17 +477,16 @@ var RecordDecoder = class {
487
477
  };
488
478
  }
489
479
  decodeData(entryId, timestamp, bytes, view, payloadOffset, payloadSize) {
490
- const context = this.context.get(entryId);
491
- if (!context) {
480
+ const entry = this.entries.get(entryId);
481
+ if (!entry) {
492
482
  if (this.options.strict) throw diagnostics.WPILOG_R0006({ entryId });
493
483
  return;
494
484
  }
495
- const { name, metadata } = context;
496
- switch (context.entryType) {
497
- case "boolean": return dataRecord(entryId, timestamp, name, metadata, RecordType.Boolean, byteToBoolean(view.getUint8(payloadOffset)));
498
- case "int64": return dataRecord(entryId, timestamp, name, metadata, RecordType.Int64, view.getBigInt64(payloadOffset, true));
499
- case "float": return dataRecord(entryId, timestamp, name, metadata, RecordType.Float, view.getFloat32(payloadOffset, true));
500
- case "double": return dataRecord(entryId, timestamp, name, metadata, RecordType.Double, view.getFloat64(payloadOffset, true));
485
+ switch (entry.type) {
486
+ case "boolean": return dataRecord(entry, timestamp, RecordType.Boolean, byteToBoolean(view.getUint8(payloadOffset)));
487
+ case "int64": return dataRecord(entry, timestamp, RecordType.Int64, view.getBigInt64(payloadOffset, true));
488
+ case "float": return dataRecord(entry, timestamp, RecordType.Float, view.getFloat32(payloadOffset, true));
489
+ case "double": return dataRecord(entry, timestamp, RecordType.Double, view.getFloat64(payloadOffset, true));
501
490
  }
502
491
  const payload = payloadOffset === 0 && payloadSize === bytes.byteLength ? bytes : bytes.subarray(payloadOffset, payloadOffset + payloadSize);
503
492
  const raw = {
@@ -505,9 +494,8 @@ var RecordDecoder = class {
505
494
  timestamp,
506
495
  payload
507
496
  };
508
- const payloadView = new DataView(payload.buffer, payload.byteOffset, payload.byteLength);
509
- const decoded = decodeNonScalarPayload(raw, context.entryType, context.name, context.metadata, payloadView, this.structRegistry, this.structDecodeQueue);
510
- if (!decoded) this.recordContexts.set(raw, context);
497
+ const decoded = decodeNonScalarPayload(raw, entry, new DataView(payload.buffer, payload.byteOffset, payload.byteLength), this.structRegistry, this.structDecodeQueue);
498
+ if (!decoded) this.recordEntries.set(raw, entry);
511
499
  return decoded;
512
500
  }
513
501
  };
@@ -565,29 +553,30 @@ function* parseDataLog(input, options = { strict: false }) {
565
553
  * Decode non-scalar payloads that require a byte slice or payload-local view.
566
554
  * Fixed-width scalar types return from {@link RecordDecoder#decodeData} before this function so their payloads can be read directly from the input buffer.
567
555
  */
568
- function decodeNonScalarPayload(raw, entryType, name, metadata, view, structRegistry, structDecodeQueue) {
569
- const { entryId, timestamp } = raw;
556
+ function decodeNonScalarPayload(raw, entry, view, structRegistry, structDecodeQueue) {
557
+ const { timestamp } = raw;
558
+ const { type: entryType, name } = entry;
570
559
  switch (entryType) {
571
- case "string": return dataRecord(entryId, timestamp, name, metadata, RecordType.String, TEXT_DECODER.decode(raw.payload));
560
+ case "string": return dataRecord(entry, timestamp, RecordType.String, TEXT_DECODER.decode(raw.payload));
572
561
  case "boolean[]": {
573
562
  const payload = [];
574
563
  for (let i = 0; i < raw.payload.byteLength; i++) payload.push(byteToBoolean(view.getUint8(i)));
575
- return dataRecord(entryId, timestamp, name, metadata, RecordType.BooleanArray, payload);
564
+ return dataRecord(entry, timestamp, RecordType.BooleanArray, payload);
576
565
  }
577
566
  case "int64[]": {
578
567
  const payload = [];
579
568
  for (let i = 0; i < raw.payload.byteLength; i += 8) payload.push(view.getBigUint64(i, true));
580
- return dataRecord(entryId, timestamp, name, metadata, RecordType.Int64Array, payload);
569
+ return dataRecord(entry, timestamp, RecordType.Int64Array, payload);
581
570
  }
582
571
  case "float[]": {
583
572
  const payload = [];
584
573
  for (let i = 0; i < raw.payload.byteLength; i += 4) payload.push(view.getFloat32(i, true));
585
- return dataRecord(entryId, timestamp, name, metadata, RecordType.FloatArray, payload);
574
+ return dataRecord(entry, timestamp, RecordType.FloatArray, payload);
586
575
  }
587
576
  case "double[]": {
588
577
  const payload = [];
589
578
  for (let i = 0; i < raw.payload.byteLength; i += 8) payload.push(view.getFloat64(i, true));
590
- return dataRecord(entryId, timestamp, name, metadata, RecordType.DoubleArray, payload);
579
+ return dataRecord(entry, timestamp, RecordType.DoubleArray, payload);
591
580
  }
592
581
  case "string[]": {
593
582
  const payload = [];
@@ -601,13 +590,13 @@ function decodeNonScalarPayload(raw, entryType, name, metadata, view, structRegi
601
590
  offset.advance(stringLength);
602
591
  payload.push(string);
603
592
  }
604
- return dataRecord(entryId, timestamp, name, metadata, RecordType.StringArray, payload);
593
+ return dataRecord(entry, timestamp, RecordType.StringArray, payload);
605
594
  }
606
595
  case "structschema": {
607
596
  const structName = name.slice(name.indexOf(STRUCT_SCHEMA_NAME_PREFIX) + 16);
608
597
  const payload = TEXT_DECODER.decode(raw.payload);
609
598
  structRegistry.register(structName, payload);
610
- return dataRecord(entryId, timestamp, name, metadata, RecordType.String, payload);
599
+ return dataRecord(entry, timestamp, RecordType.String, payload);
611
600
  }
612
601
  default:
613
602
  if (entryType.startsWith(STRUCT_PREFIX)) {
@@ -619,10 +608,8 @@ function decodeNonScalarPayload(raw, entryType, name, metadata, view, structRegi
619
608
  return;
620
609
  }
621
610
  return {
622
- entryId,
611
+ entry,
623
612
  timestamp,
624
- name,
625
- metadata,
626
613
  type: RecordType.StructArray,
627
614
  structName,
628
615
  payload: decoded
@@ -634,16 +621,14 @@ function decodeNonScalarPayload(raw, entryType, name, metadata, view, structRegi
634
621
  return;
635
622
  }
636
623
  return {
637
- entryId,
624
+ entry,
638
625
  timestamp,
639
- name,
640
- metadata,
641
626
  type: RecordType.Struct,
642
627
  structName,
643
628
  payload: decoded
644
629
  };
645
630
  }
646
- return dataRecord(entryId, timestamp, name, metadata, RecordType.Raw, new Uint8Array(raw.payload));
631
+ return dataRecord(entry, timestamp, RecordType.Raw, new Uint8Array(raw.payload));
647
632
  }
648
633
  }
649
634
  // src/catalog.ts
package/dist/cli.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { a as readRecords, l as diagnostics, t as catalogEntries } from "./catalog-Dz8xPVYl.mjs";
2
+ import { a as readRecords, l as diagnostics, t as catalogEntries } from "./catalog-BPg-SswK.mjs";
3
3
  import { Diagnostic, formatDiagnostic } from "nostics";
4
4
  import { RootCommand, script } from "@bomb.sh/tab";
5
5
  import { boolean, command, commandsInfo, positional, run } from "@drizzle-team/brocli";
@@ -7,7 +7,7 @@ import oscStatus from "osc-status";
7
7
  import { readFile } from "node:fs/promises";
8
8
  import { encode } from "@toon-format/toon";
9
9
  // package.json
10
- var version = "3.2.2";
10
+ var version = "4.0.0";
11
11
  var description = "Read WPILib wpilog data log files in TypeScript & JavaScript";
12
12
  // src/cli/catalog.ts
13
13
  async function catalogFile(filePath) {
package/dist/index.d.mts CHANGED
@@ -35,11 +35,18 @@ type SetMetadataControlRecord = {
35
35
  };
36
36
  /** A control record payload (start, finish, or set metadata). */
37
37
  type ControlRecordPayload = StartControlRecord | FinishControlRecord | SetMetadataControlRecord;
38
+ /** An entry definition, from its Start control record and any later SetMetadata control records. */
39
+ type CatalogEntry = {
40
+ entryId: number;
41
+ name: string;
42
+ type: string;
43
+ metadata: string;
44
+ };
38
45
  /** A raw (undecoded) data log record from the binary format. */
39
46
  type RawRecord = {
40
47
  entryId: number;
41
48
  /** Timestamp in microseconds. */
42
- timestamp: bigint;
49
+ timestamp: number;
43
50
  payload: Uint8Array;
44
51
  };
45
52
  /** Discriminated union tag for decoded record types. */
@@ -62,7 +69,7 @@ export declare const RecordType: {
62
69
  export type RecordType = (typeof RecordType)[keyof typeof RecordType];
63
70
  /** A decoded data record (excludes control records). */
64
71
  type DataRecord = Extract<DecodedRecord, {
65
- name: string;
72
+ entry: CatalogEntry;
66
73
  }>;
67
74
  /** A decoded control record. */
68
75
  type ControlRecord = Extract<DecodedRecord, {
@@ -72,15 +79,18 @@ type ControlRecord = Extract<DecodedRecord, {
72
79
  export declare function isDataRecord(record: DecodedRecord): record is DataRecord;
73
80
  /** A decoded data log record. */
74
81
  type DecodedRecord = {
75
- entryId: number;
76
82
  /** Timestamp in microseconds. */
77
- timestamp: bigint;
83
+ timestamp: number;
78
84
  } & ({
85
+ entryId: number;
79
86
  type: typeof RecordType.Control;
80
87
  payload: ControlRecordPayload;
81
88
  } | ({
82
- name: string;
83
- metadata: string;
89
+ /**
90
+ * The entry this record belongs to.
91
+ * Records from the same entry share one object until a SetMetadata control record replaces it.
92
+ */
93
+ entry: CatalogEntry;
84
94
  } & ({
85
95
  type: typeof RecordType.Raw;
86
96
  payload: Uint8Array;
@@ -130,7 +140,7 @@ type ReadRecord = {
130
140
  } | {
131
141
  kind: 'control';
132
142
  entryId: number;
133
- timestamp: bigint;
143
+ timestamp: number;
134
144
  payload: ControlRecordPayload;
135
145
  } | {
136
146
  kind: 'data';
@@ -173,13 +183,6 @@ export declare function decodeRecords(records: Iterable<ReadRecord>, options?: D
173
183
  * {@link decodeRecords} while preserving the same output and strict-mode behavior.
174
184
  */
175
185
  export declare function parseDataLog(input: DataLogInput, options?: DecodeRecordsOptions): Generator<DecodedRecord>;
176
- /** An entry definition from the WPILOG catalog. */
177
- type CatalogEntry = {
178
- entryId: number;
179
- name: string;
180
- type: string;
181
- metadata: string;
182
- };
183
186
  /**
184
187
  * Collect all entry definitions from a WPILOG record stream.
185
188
  *
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { a as readRecords, c as isDataRecord, i as withStructDiagnostics, n as decodeRecords, o as ControlRecordType, r as parseDataLog, s as RecordType, t as catalogEntries } from "./catalog-Dz8xPVYl.mjs";
1
+ import { a as readRecords, c as isDataRecord, i as withStructDiagnostics, n as decodeRecords, o as ControlRecordType, r as parseDataLog, s as RecordType, t as catalogEntries } from "./catalog-BPg-SswK.mjs";
2
2
  import { KnownStructTypeName, parseStructSpecification as parseStructSpecification$1 } from "wpilib-struct";
3
3
  // src/struct/parse-struct.ts
4
4
  /** Parse a WPILib struct specification string into declarations. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wpilog-parser",
3
- "version": "3.2.2",
3
+ "version": "4.0.0",
4
4
  "description": "Read WPILib wpilog data log files in TypeScript & JavaScript",
5
5
  "homepage": "https://github.com/jonahsnider/wpilog-parser#readme",
6
6
  "license": "MIT",
@@ -22,7 +22,7 @@ WPILOG is the binary log format used by logging libraries in the FRC ecosystem.
22
22
 
23
23
  ## Analyzing logs
24
24
 
25
- Use `parseDataLog` to parse DataLog bytes. Filter by `name` and narrow on `type`:
25
+ Use `parseDataLog` to parse DataLog bytes. Filter by `entry.name` and narrow on `type`:
26
26
 
27
27
  ```ts
28
28
  import { readFile } from 'node:fs/promises';
@@ -33,7 +33,7 @@ const bytes = await readFile('./example.wpilog');
33
33
  for (const record of parseDataLog(bytes)) {
34
34
  if (!isDataRecord(record)) continue;
35
35
 
36
- if (record.name === '/Robot/Intake/Voltage' && record.type === RecordType.Double) {
36
+ if (record.entry.name === '/Robot/Intake/Voltage' && record.type === RecordType.Double) {
37
37
  console.log(record.timestamp, record.payload);
38
38
  }
39
39
  }
@@ -45,9 +45,9 @@ for (const record of parseDataLog(bytes)) {
45
45
  import { parseDataLog, RecordType } from 'wpilog-parser';
46
46
 
47
47
  for (const record of parseDataLog(bytes)) {
48
- if (record.type === RecordType.Struct && record.name === '/Robot/Localization/EstimatedPose') {
48
+ if (record.type === RecordType.Struct && record.entry.name === '/Robot/Localization/EstimatedPose') {
49
49
  console.log(record.timestamp, record.payload);
50
- // 18688018n {
50
+ // 18688018 {
51
51
  // translation: { x: 10.289, y: 0.47 },
52
52
  // rotation: { value: 1.5707961072652017 },
53
53
  // }
@@ -74,7 +74,7 @@ When DriverStation logging is on, every WPILOG has these `boolean` entries:
74
74
  | `/DS:test` | `true` during test mode. |
75
75
  | `/DS:estop` | `true` if e-stopped. |
76
76
 
77
- The mode flags (`autonomous`, `test`) reflect what the DS _would_ run if enabled, so check `/DS:enabled` together with them. Timestamps are microseconds (`bigint`).
77
+ The mode flags (`autonomous`, `test`) reflect what the DS _would_ run if enabled, so check `/DS:enabled` together with them. Timestamps are microseconds (`number`).
78
78
 
79
79
  ### Tracking enable/mode durations
80
80
 
@@ -91,8 +91,8 @@ function modeOf(auto: boolean, test: boolean): Mode {
91
91
  let enabled = false;
92
92
  let auto = false;
93
93
  let test = false;
94
- let enabledSince: bigint | null = null;
95
- const totals = { auto: 0n, teleop: 0n, test: 0n };
94
+ let enabledSince: number | null = null;
95
+ const totals = { auto: 0, teleop: 0, test: 0 };
96
96
 
97
97
  for (const r of parseDataLog(bytes)) {
98
98
  if (!isDataRecord(r) || r.type !== RecordType.Boolean) continue;
@@ -100,9 +100,9 @@ for (const r of parseDataLog(bytes)) {
100
100
  const wasEnabled = enabled;
101
101
  const prevMode = modeOf(auto, test);
102
102
 
103
- if (r.name === '/DS:enabled') enabled = r.payload;
104
- else if (r.name === '/DS:autonomous') auto = r.payload;
105
- else if (r.name === '/DS:test') test = r.payload;
103
+ if (r.entry.name === '/DS:enabled') enabled = r.payload;
104
+ else if (r.entry.name === '/DS:autonomous') auto = r.payload;
105
+ else if (r.entry.name === '/DS:test') test = r.payload;
106
106
  else continue;
107
107
 
108
108
  if (wasEnabled && (!enabled || modeOf(auto, test) !== prevMode)) {
@@ -113,6 +113,6 @@ for (const r of parseDataLog(bytes)) {
113
113
  }
114
114
  }
115
115
 
116
- // Durations in microseconds; divide by 1_000_000n for seconds.
116
+ // Durations in microseconds; divide by 1e6 for seconds.
117
117
  console.log(totals);
118
118
  ```