wpilog-parser 2.2.0 → 2.4.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/dist/cli.d.mts ADDED
@@ -0,0 +1 @@
1
+ export {}
package/dist/cli.mjs ADDED
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/env node
2
+ import { o as readRecords, t as catalogEntries, u as diagnostics } from "./catalog-BT6geQr0.mjs";
3
+ import { Diagnostic, formatDiagnostic } from "nostics";
4
+ import { cac } from "cac";
5
+ import { readFile } from "node:fs/promises";
6
+ import { encode } from "@toon-format/toon";
7
+ //#region package.json
8
+ var version = "2.4.0";
9
+ //#endregion
10
+ //#region src/cli/catalog.ts
11
+ async function catalogFile(filePath) {
12
+ let bytes;
13
+ try {
14
+ bytes = await readFile(filePath);
15
+ } catch (error) {
16
+ if (error instanceof Error && "code" in error && error.code === "ENOENT") throw diagnostics.WPILOG_C0003({
17
+ filePath,
18
+ cause: error
19
+ });
20
+ throw error;
21
+ }
22
+ try {
23
+ return Array.from(catalogEntries(readRecords(bytes)));
24
+ } catch (error) {
25
+ if (error instanceof Diagnostic) error.sources = [filePath, ...error.sources ?? []];
26
+ throw error;
27
+ }
28
+ }
29
+ function selectCatalogFormat(options) {
30
+ const selected = [
31
+ options.json && "json",
32
+ (options.jsonl || options.ndjson) && "jsonl",
33
+ options.csv && "csv"
34
+ ].filter((format) => Boolean(format));
35
+ if (selected.length > 1) throw diagnostics.WPILOG_C0002();
36
+ return selected[0] ?? "toon";
37
+ }
38
+ function escapeCsv(value) {
39
+ const text = String(value);
40
+ if (/[",\r\n]/.test(text)) return `"${text.replaceAll("\"", "\"\"")}"`;
41
+ return text;
42
+ }
43
+ /** Serialize catalog entries in the selected output format. */
44
+ function formatCatalog(entries, format = "toon") {
45
+ switch (format) {
46
+ case "toon": return encode(entries);
47
+ case "json": return JSON.stringify(entries, null, 2);
48
+ case "jsonl":
49
+ case "ndjson": return entries.map((entry) => JSON.stringify(entry)).join("\n");
50
+ case "csv": return ["entryId,name,type,metadata", ...entries.map((entry) => [
51
+ entry.entryId,
52
+ entry.name,
53
+ entry.type,
54
+ entry.metadata
55
+ ].map((value) => escapeCsv(value)).join(","))].join("\n");
56
+ }
57
+ }
58
+ //#endregion
59
+ //#region src/cli.ts
60
+ const cli = cac("wpilog");
61
+ cli.command("catalog <file>", "List the entries available in a WPILOG file").option("--json", "Output as JSON").option("--jsonl, --ndjson", "Output as newline-delimited JSON").option("--csv", "Output as CSV").example("wpilog catalog ./example.wpilog").example("wpilog catalog ./example.wpilog --json").action(async (file, options) => {
62
+ const format = selectCatalogFormat(options);
63
+ const entries = await catalogFile(file);
64
+ console.log(formatCatalog(entries, format));
65
+ });
66
+ cli.help();
67
+ cli.version(version);
68
+ try {
69
+ const parsed = cli.parse(process.argv, { run: false });
70
+ await cli.runMatchedCommand();
71
+ if (!cli.matchedCommand && !parsed.options.help && !parsed.options.version) {
72
+ if (parsed.args[0]) throw diagnostics.WPILOG_C0001({ command: parsed.args[0] });
73
+ cli.outputHelp();
74
+ }
75
+ } catch (error) {
76
+ const message = error instanceof Diagnostic ? formatDiagnostic(error) : error instanceof Error ? error.message : String(error);
77
+ console.error(error instanceof Diagnostic ? message : `wpilog: ${message}`);
78
+ process.exitCode = 1;
79
+ }
80
+ //#endregion
81
+ export {};
package/dist/index.d.mts CHANGED
@@ -8,14 +8,15 @@ type DataLogHeader = {
8
8
  extraHeader: string;
9
9
  };
10
10
  /** Control record types in the WPILOG format. */
11
- declare enum ControlRecordType {
12
- Start = 0,
13
- Finish = 1,
14
- SetMetadata = 2
15
- }
11
+ export declare const ControlRecordType: {
12
+ readonly Start: 0;
13
+ readonly Finish: 1;
14
+ readonly SetMetadata: 2;
15
+ };
16
+ export type ControlRecordType = (typeof ControlRecordType)[keyof typeof ControlRecordType];
16
17
  /** A control record that starts a new entry. */
17
18
  type StartControlRecord = {
18
- controlRecordType: ControlRecordType.Start;
19
+ controlRecordType: typeof ControlRecordType.Start;
19
20
  entryId: number;
20
21
  entryName: string;
21
22
  entryType: string;
@@ -23,12 +24,12 @@ type StartControlRecord = {
23
24
  };
24
25
  /** A control record that finishes an entry. */
25
26
  type FinishControlRecord = {
26
- controlRecordType: ControlRecordType.Finish;
27
+ controlRecordType: typeof ControlRecordType.Finish;
27
28
  entryId: number;
28
29
  };
29
30
  /** A control record that sets metadata on an entry. */
30
31
  type SetMetadataControlRecord = {
31
- controlRecordType: ControlRecordType.SetMetadata;
32
+ controlRecordType: typeof ControlRecordType.SetMetadata;
32
33
  entryId: number;
33
34
  entryMetadata: string;
34
35
  };
@@ -36,27 +37,29 @@ type SetMetadataControlRecord = {
36
37
  type ControlRecordPayload = StartControlRecord | FinishControlRecord | SetMetadataControlRecord;
37
38
  /** A raw (undecoded) data log record from the binary format. */
38
39
  type RawRecord = {
39
- entryId: number; /** Timestamp in microseconds. */
40
+ entryId: number;
41
+ /** Timestamp in microseconds. */
40
42
  timestamp: bigint;
41
43
  payload: Uint8Array;
42
44
  };
43
45
  /** Discriminated union tag for decoded record types. */
44
- declare enum RecordType {
45
- Control = "control",
46
- Boolean = "boolean",
47
- Int64 = "int64",
48
- Float = "float",
49
- Double = "double",
50
- String = "string",
51
- BooleanArray = "boolean[]",
52
- Int64Array = "int64[]",
53
- FloatArray = "float[]",
54
- DoubleArray = "double[]",
55
- StringArray = "string[]",
56
- Struct = "struct",
57
- StructArray = "struct[]",
58
- Raw = "raw"
59
- }
46
+ export declare const RecordType: {
47
+ readonly Control: 'control';
48
+ readonly Boolean: 'boolean';
49
+ readonly Int64: 'int64';
50
+ readonly Float: 'float';
51
+ readonly Double: 'double';
52
+ readonly String: 'string';
53
+ readonly BooleanArray: 'boolean[]';
54
+ readonly Int64Array: 'int64[]';
55
+ readonly FloatArray: 'float[]';
56
+ readonly DoubleArray: 'double[]';
57
+ readonly StringArray: 'string[]';
58
+ readonly Struct: 'struct';
59
+ readonly StructArray: 'struct[]';
60
+ readonly Raw: 'raw';
61
+ };
62
+ export type RecordType = (typeof RecordType)[keyof typeof RecordType];
60
63
  /** Decoded struct payload — a map of field names to values. */
61
64
  type StructPayload = Map<string, number | boolean | bigint | string | StructPayload | number[] | boolean[] | bigint[] | StructPayload[]>;
62
65
  /** A decoded data record (excludes control records). */
@@ -65,59 +68,60 @@ type DataRecord = Extract<DecodedRecord, {
65
68
  }>;
66
69
  /** A decoded control record. */
67
70
  type ControlRecord = Extract<DecodedRecord, {
68
- type: RecordType.Control;
71
+ type: typeof RecordType.Control;
69
72
  }>;
70
73
  /** Type guard that narrows a {@link DecodedRecord} to a {@link DataRecord}. */
71
- declare function isDataRecord(record: DecodedRecord): record is DataRecord;
74
+ export declare function isDataRecord(record: DecodedRecord): record is DataRecord;
72
75
  /** A decoded data log record. */
73
76
  type DecodedRecord = {
74
- entryId: number; /** Timestamp in microseconds. */
77
+ entryId: number;
78
+ /** Timestamp in microseconds. */
75
79
  timestamp: bigint;
76
80
  } & ({
77
- type: RecordType.Control;
81
+ type: typeof RecordType.Control;
78
82
  payload: ControlRecordPayload;
79
83
  } | ({
80
84
  name: string;
81
85
  metadata: string;
82
86
  } & ({
83
- type: RecordType.Raw;
87
+ type: typeof RecordType.Raw;
84
88
  payload: Uint8Array;
85
89
  } | {
86
- type: RecordType.Boolean;
90
+ type: typeof RecordType.Boolean;
87
91
  payload: boolean;
88
92
  } | {
89
- type: RecordType.Int64;
93
+ type: typeof RecordType.Int64;
90
94
  payload: bigint;
91
95
  } | {
92
- type: RecordType.Float;
96
+ type: typeof RecordType.Float;
93
97
  payload: number;
94
98
  } | {
95
- type: RecordType.Double;
99
+ type: typeof RecordType.Double;
96
100
  payload: number;
97
101
  } | {
98
- type: RecordType.String;
102
+ type: typeof RecordType.String;
99
103
  payload: string;
100
104
  } | {
101
- type: RecordType.BooleanArray;
105
+ type: typeof RecordType.BooleanArray;
102
106
  payload: boolean[];
103
107
  } | {
104
- type: RecordType.Int64Array;
108
+ type: typeof RecordType.Int64Array;
105
109
  payload: bigint[];
106
110
  } | {
107
- type: RecordType.FloatArray;
111
+ type: typeof RecordType.FloatArray;
108
112
  payload: number[];
109
113
  } | {
110
- type: RecordType.DoubleArray;
114
+ type: typeof RecordType.DoubleArray;
111
115
  payload: number[];
112
116
  } | {
113
- type: RecordType.StringArray;
117
+ type: typeof RecordType.StringArray;
114
118
  payload: string[];
115
119
  } | {
116
- type: RecordType.Struct;
120
+ type: typeof RecordType.Struct;
117
121
  structName: string;
118
122
  payload: StructPayload;
119
123
  } | {
120
- type: RecordType.StructArray;
124
+ type: typeof RecordType.StructArray;
121
125
  structName: string;
122
126
  payload: StructPayload[];
123
127
  })));
@@ -136,18 +140,19 @@ type ReadRecord = {
136
140
  kind: 'data';
137
141
  record: RawRecord;
138
142
  };
139
- /** Accepted input types for {@link readRecords}. */
143
+ /** Accepted input types for {@link readRecords} and `parseDataLog`. */
140
144
  type DataLogInput = Uint8Array | ArrayBuffer;
141
145
  /**
142
146
  * Read raw WPILOG records from an in-memory buffer.
143
147
  *
144
148
  * Yields a header record first, then control and data records in order.
145
- * Data record payloads are not decoded — use {@link decodeRecords} for that.
149
+ * Data record payloads are not decoded. Prefer `parseDataLog` when raw records
150
+ * or an intermediate transformation are not needed.
146
151
  */
147
- declare function readRecords(input: DataLogInput): Generator<ReadRecord>;
152
+ export declare function readRecords(input: DataLogInput): Generator<ReadRecord>;
148
153
  //#endregion
149
154
  //#region src/decode-records.d.ts
150
- /** Options for {@link decodeRecords}. */
155
+ /** Options for {@link decodeRecords} and {@link parseDataLog}. */
151
156
  type DecodeRecordsOptions = {
152
157
  /**
153
158
  * When `true`, throw if a data record references an entry ID with no preceding
@@ -165,11 +170,19 @@ type DecodeRecordsOptions = {
165
170
  * By default, data records that reference an entry ID with no preceding Start
166
171
  * control record are silently skipped. Pass `{ strict: true }` to throw instead.
167
172
  */
168
- declare function decodeRecords(records: Iterable<ReadRecord>, options?: DecodeRecordsOptions): Generator<DecodedRecord>;
173
+ export declare function decodeRecords(records: Iterable<ReadRecord>, options?: DecodeRecordsOptions): Generator<DecodedRecord>;
174
+ /**
175
+ * Read and decode a WPILOG buffer in a single pass.
176
+ *
177
+ * This is the preferred API for fully decoded records. It avoids allocating the
178
+ * intermediate records produced by composing {@link readRecords} and
179
+ * {@link decodeRecords} while preserving the same output and strict-mode behavior.
180
+ */
181
+ export declare function parseDataLog(input: DataLogInput, options?: DecodeRecordsOptions): Generator<DecodedRecord>;
169
182
  //#endregion
170
183
  //#region src/struct-payload-to-json.d.ts
171
184
  /** Convert a {@link StructPayload} Map to a plain JSON-compatible object. */
172
- declare function structPayloadToJson(payload: StructPayload): object;
185
+ export declare function structPayloadToJson(payload: StructPayload): object;
173
186
  //#endregion
174
187
  //#region src/catalog.d.ts
175
188
  /** An entry definition from the WPILOG catalog. */
@@ -184,30 +197,31 @@ type CatalogEntry = {
184
197
  *
185
198
  * Iterates through records, collecting Start control records.
186
199
  * Data record bytes are still read from the stream but not decoded,
187
- * making this much cheaper than full decoding via {@link decodeRecords}.
200
+ * making this much cheaper than full decoding via {@link parseDataLog}.
188
201
  */
189
- declare function catalogEntries(records: Iterable<ReadRecord>): Generator<CatalogEntry>;
202
+ export declare function catalogEntries(records: Iterable<ReadRecord>): Generator<CatalogEntry>;
190
203
  //#endregion
191
204
  //#region src/struct/types.d.ts
192
- declare enum KnownStructTypeName {
193
- Boolean = "bool",
194
- Character = "char",
195
- Int8 = "int8",
196
- Int16 = "int16",
197
- Int32 = "int32",
198
- Int64 = "int64",
199
- Uint8 = "uint8",
200
- Uint16 = "uint16",
201
- Uint32 = "uint32",
202
- Uint64 = "uint64",
203
- Float32 = "float32",
205
+ export declare const KnownStructTypeName: {
206
+ readonly Boolean: 'bool';
207
+ readonly Character: 'char';
208
+ readonly Int8: 'int8';
209
+ readonly Int16: 'int16';
210
+ readonly Int32: 'int32';
211
+ readonly Int64: 'int64';
212
+ readonly Uint8: 'uint8';
213
+ readonly Uint16: 'uint16';
214
+ readonly Uint32: 'uint32';
215
+ readonly Uint64: 'uint64';
216
+ readonly Float32: 'float32';
204
217
  /** Equivalent to {@link KnownStructTypeName.Float32} */
205
- Float = "float",
206
- Float64 = "float64",
218
+ readonly Float: 'float';
219
+ readonly Float64: 'float64';
207
220
  /** Equivalent to {@link KnownStructTypeName.Float64} */
208
- Double = "double"
209
- }
210
- type StructTypeName = KnownStructTypeName | string;
221
+ readonly Double: 'double';
222
+ };
223
+ export type KnownStructTypeName = (typeof KnownStructTypeName)[keyof typeof KnownStructTypeName];
224
+ type StructTypeName = string;
211
225
  type EnumSpecification = Map<string, bigint>;
212
226
  type StructSpecification = StructDeclaration[];
213
227
  type StructDeclaration = {
@@ -220,6 +234,6 @@ type StructDeclaration = {
220
234
  //#endregion
221
235
  //#region src/struct/parse-struct.d.ts
222
236
  /** Parse a WPILib struct specification string into declarations. */
223
- declare function parseStructSpecification(declaration: string): StructDeclaration[];
237
+ export declare function parseStructSpecification(declaration: string): StructDeclaration[];
224
238
  //#endregion
225
- export { type CatalogEntry, type ControlRecord, type ControlRecordPayload, ControlRecordType, type DataLogHeader, type DataLogInput, type DataRecord, type DecodeRecordsOptions, type DecodedRecord, type EnumSpecification, type FinishControlRecord, KnownStructTypeName, type RawRecord, type ReadRecord, RecordType, type SetMetadataControlRecord, type StartControlRecord, type StructDeclaration, type StructPayload, type StructSpecification, type StructTypeName, catalogEntries, decodeRecords, isDataRecord, parseStructSpecification, readRecords, structPayloadToJson };
239
+ export type { CatalogEntry, ControlRecord, ControlRecordPayload, DataLogHeader, DataLogInput, DataRecord, DecodeRecordsOptions, DecodedRecord, EnumSpecification, FinishControlRecord, RawRecord, ReadRecord, SetMetadataControlRecord, StartControlRecord, StructDeclaration, StructPayload, StructSpecification, StructTypeName };