wpilog-parser 1.0.1 → 2.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.
package/README.md CHANGED
@@ -11,13 +11,12 @@ npm install wpilog-parser
11
11
  ## Usage
12
12
 
13
13
  ```ts
14
- import { createReadStream } from 'node:fs';
15
- import { Readable } from 'node:stream';
14
+ import { readFile } from 'node:fs/promises';
16
15
  import { readRecords, decodeRecords } from 'wpilog-parser';
17
16
 
18
- const stream = Readable.toWeb(createReadStream('./example.wpilog'));
17
+ const bytes = await readFile('./example.wpilog');
19
18
 
20
- for await (const record of decodeRecords(readRecords(stream))) {
19
+ for (const record of decodeRecords(readRecords(bytes))) {
21
20
  console.log(record);
22
21
  }
23
22
  ```
package/dist/index.d.mts CHANGED
@@ -137,14 +137,14 @@ type ReadRecord = {
137
137
  record: RawRecord;
138
138
  };
139
139
  /** Accepted input types for {@link readRecords}. */
140
- type DataLogInput = ReadableStream<Uint8Array> | Uint8Array | ArrayBuffer;
140
+ type DataLogInput = Uint8Array | ArrayBuffer;
141
141
  /**
142
- * Read raw WPILOG records from a byte stream.
142
+ * Read raw WPILOG records from an in-memory buffer.
143
143
  *
144
144
  * Yields a header record first, then control and data records in order.
145
145
  * Data record payloads are not decoded — use {@link decodeRecords} for that.
146
146
  */
147
- declare function readRecords(input: DataLogInput): AsyncGenerator<ReadRecord>;
147
+ declare function readRecords(input: DataLogInput): Generator<ReadRecord>;
148
148
  //#endregion
149
149
  //#region src/decode-records.d.ts
150
150
  /**
@@ -153,7 +153,7 @@ declare function readRecords(input: DataLogInput): AsyncGenerator<ReadRecord>;
153
153
  * Accepts the output of {@link readRecords} and yields fully decoded records,
154
154
  * including struct decoding with dependency resolution.
155
155
  */
156
- declare function decodeRecords(records: AsyncIterable<ReadRecord>): AsyncGenerator<DecodedRecord>;
156
+ declare function decodeRecords(records: Iterable<ReadRecord>): Generator<DecodedRecord>;
157
157
  //#endregion
158
158
  //#region src/struct-payload-to-json.d.ts
159
159
  /** Convert a {@link StructPayload} Map to a plain JSON-compatible object. */
@@ -174,7 +174,7 @@ type CatalogEntry = {
174
174
  * Data record bytes are still read from the stream but not decoded,
175
175
  * making this much cheaper than full decoding via {@link decodeRecords}.
176
176
  */
177
- declare function catalogEntries(records: AsyncIterable<ReadRecord>): AsyncGenerator<CatalogEntry, void, never>;
177
+ declare function catalogEntries(records: Iterable<ReadRecord>): Generator<CatalogEntry>;
178
178
  //#endregion
179
179
  //#region src/struct/types.d.ts
180
180
  declare enum KnownStructTypeName {
package/dist/index.mjs CHANGED
@@ -25,38 +25,6 @@ var ByteOffset = class {
25
25
  }
26
26
  };
27
27
  //#endregion
28
- //#region src/input-stream.ts
29
- var StreamFinishedError = class extends Error {
30
- constructor(bytesRead) {
31
- super(`Reached end of stream after reading ${bytesRead} bytes`);
32
- }
33
- };
34
- var InputStream = class {
35
- reader;
36
- bytesRead = 0;
37
- buffer = new Uint8Array();
38
- constructor(stream) {
39
- this.reader = stream.getReader();
40
- }
41
- async readBytesAndAdvance(length) {
42
- while (this.buffer.byteLength < length) {
43
- const chunk = await this.reader.read();
44
- if (chunk.done) throw new StreamFinishedError(this.bytesRead);
45
- const newBuffer = new Uint8Array(this.buffer.byteLength + chunk.value.byteLength);
46
- newBuffer.set(this.buffer);
47
- newBuffer.set(chunk.value, this.buffer.byteLength);
48
- this.buffer = newBuffer;
49
- }
50
- return this.readAndAdvanceFromBuffer(length);
51
- }
52
- readAndAdvanceFromBuffer(length) {
53
- const result = this.buffer.slice(0, length);
54
- this.buffer = this.buffer.slice(length);
55
- this.bytesRead += length;
56
- return result;
57
- }
58
- };
59
- //#endregion
60
28
  //#region src/types.ts
61
29
  /** Control record types in the WPILOG format. */
62
30
  let ControlRecordType = /* @__PURE__ */ function(ControlRecordType) {
@@ -92,83 +60,6 @@ function isDataRecord(record) {
92
60
  const TEXT_ENCODER = new TextEncoder();
93
61
  const TEXT_DECODER$1 = new TextDecoder();
94
62
  const MAGIC = TEXT_ENCODER.encode("WPILOG");
95
- async function readHeader(buffer) {
96
- const magic = await buffer.readBytesAndAdvance(MAGIC.byteLength);
97
- for (let i = 0; i < MAGIC.byteLength; i++) if (magic[i] !== MAGIC[i]) throw new Error("Not a WPILOG file (invalid magic bytes)");
98
- const chunk = await buffer.readBytesAndAdvance(6);
99
- const view = new DataView(chunk.buffer, chunk.byteOffset, chunk.byteLength);
100
- const versionMinor = view.getUint8(0);
101
- const versionMajor = view.getUint8(1);
102
- const extraHeaderLength = view.getUint32(2, true);
103
- const extraHeader = await buffer.readBytesAndAdvance(extraHeaderLength);
104
- return {
105
- version: {
106
- major: versionMajor,
107
- minor: versionMinor
108
- },
109
- extraHeader: TEXT_DECODER$1.decode(extraHeader)
110
- };
111
- }
112
- async function readRecordHeaderLength(buffer) {
113
- const chunk = await buffer.readBytesAndAdvance(1);
114
- const bitfield = new DataView(chunk.buffer, chunk.byteOffset, chunk.byteLength).getUint8(0);
115
- return {
116
- entryIdLength: 1 + (bitfield & 3),
117
- payloadSizeLength: 1 + (bitfield >> 2 & 3),
118
- timestampLength: 1 + (bitfield >> 4 & 7)
119
- };
120
- }
121
- async function readVarInt(buffer, length) {
122
- const chunk = await buffer.readBytesAndAdvance(length);
123
- const view = new DataView(chunk.buffer, chunk.byteOffset, chunk.byteLength);
124
- switch (length) {
125
- case 1: return view.getUint8(0);
126
- case 2: return view.getUint16(0, true);
127
- case 3: return view.getUint8(2) << 16 | view.getUint8(1) << 8 | view.getUint8(0);
128
- case 4: return view.getUint32(0, true);
129
- default: throw new RangeError(`Invalid varint length ${length}`);
130
- }
131
- }
132
- async function readTimestamp(buffer, length) {
133
- const chunk = await buffer.readBytesAndAdvance(length);
134
- const view = new DataView(chunk.buffer, chunk.byteOffset, chunk.byteLength);
135
- switch (length) {
136
- case 1: return BigInt(view.getUint8(0));
137
- case 2: return BigInt(view.getUint16(0, true));
138
- case 3: {
139
- const b0 = BigInt(view.getUint8(0));
140
- const b1 = BigInt(view.getUint8(1));
141
- return BigInt(view.getUint8(2)) << 16n | b1 << 8n | b0;
142
- }
143
- case 4: return BigInt(view.getUint32(0, true));
144
- case 5: {
145
- const b0 = BigInt(view.getUint8(0));
146
- const b1 = BigInt(view.getUint8(1));
147
- const b2 = BigInt(view.getUint8(2));
148
- const b3 = BigInt(view.getUint8(3));
149
- return BigInt(view.getUint8(4)) << 32n | b3 << 24n | b2 << 16n | b1 << 8n | b0;
150
- }
151
- case 6: {
152
- const b0 = BigInt(view.getUint8(0));
153
- const b1 = BigInt(view.getUint8(1));
154
- const b2 = BigInt(view.getUint8(2));
155
- const b3 = BigInt(view.getUint8(3));
156
- const b4 = BigInt(view.getUint8(4));
157
- return BigInt(view.getUint8(5)) << 40n | b4 << 32n | b3 << 24n | b2 << 16n | b1 << 8n | b0;
158
- }
159
- case 7: {
160
- const b0 = BigInt(view.getUint8(0));
161
- const b1 = BigInt(view.getUint8(1));
162
- const b2 = BigInt(view.getUint8(2));
163
- const b3 = BigInt(view.getUint8(3));
164
- const b4 = BigInt(view.getUint8(4));
165
- const b5 = BigInt(view.getUint8(5));
166
- return BigInt(view.getUint8(6)) << 48n | b5 << 40n | b4 << 32n | b3 << 24n | b2 << 16n | b1 << 8n | b0;
167
- }
168
- case 8: return view.getBigUint64(0, true);
169
- default: throw new RangeError(`Invalid timestamp length ${length}`);
170
- }
171
- }
172
63
  function readControlRecordPayload(payload) {
173
64
  const offset = new ByteOffset();
174
65
  const view = new DataView(payload.buffer, payload.byteOffset, payload.byteLength);
@@ -222,34 +113,102 @@ function readControlRecordPayload(payload) {
222
113
  default: throw new RangeError(`Invalid control record type ${type}`);
223
114
  }
224
115
  }
225
- function toReadableStream(input) {
226
- if (input instanceof ReadableStream) return input;
227
- return new Blob([input]).stream();
116
+ function toUint8Array(input) {
117
+ if (input instanceof Uint8Array) return input;
118
+ return new Uint8Array(input);
119
+ }
120
+ function readVarInt(view, offset, length) {
121
+ switch (length) {
122
+ case 1: return view.getUint8(offset);
123
+ case 2: return view.getUint16(offset, true);
124
+ case 3: return view.getUint8(offset + 2) << 16 | view.getUint8(offset + 1) << 8 | view.getUint8(offset);
125
+ case 4: return view.getUint32(offset, true);
126
+ default: throw new RangeError(`Invalid varint length ${length}`);
127
+ }
128
+ }
129
+ function readTimestamp(view, offset, length) {
130
+ switch (length) {
131
+ case 1: return BigInt(view.getUint8(offset));
132
+ case 2: return BigInt(view.getUint16(offset, true));
133
+ case 3: {
134
+ const b0 = BigInt(view.getUint8(offset));
135
+ const b1 = BigInt(view.getUint8(offset + 1));
136
+ return BigInt(view.getUint8(offset + 2)) << 16n | b1 << 8n | b0;
137
+ }
138
+ case 4: return BigInt(view.getUint32(offset, true));
139
+ case 5: {
140
+ const b0 = BigInt(view.getUint8(offset));
141
+ const b1 = BigInt(view.getUint8(offset + 1));
142
+ const b2 = BigInt(view.getUint8(offset + 2));
143
+ const b3 = BigInt(view.getUint8(offset + 3));
144
+ return BigInt(view.getUint8(offset + 4)) << 32n | b3 << 24n | b2 << 16n | b1 << 8n | b0;
145
+ }
146
+ case 6: {
147
+ const b0 = BigInt(view.getUint8(offset));
148
+ const b1 = BigInt(view.getUint8(offset + 1));
149
+ const b2 = BigInt(view.getUint8(offset + 2));
150
+ const b3 = BigInt(view.getUint8(offset + 3));
151
+ const b4 = BigInt(view.getUint8(offset + 4));
152
+ return BigInt(view.getUint8(offset + 5)) << 40n | b4 << 32n | b3 << 24n | b2 << 16n | b1 << 8n | b0;
153
+ }
154
+ case 7: {
155
+ const b0 = BigInt(view.getUint8(offset));
156
+ const b1 = BigInt(view.getUint8(offset + 1));
157
+ const b2 = BigInt(view.getUint8(offset + 2));
158
+ const b3 = BigInt(view.getUint8(offset + 3));
159
+ const b4 = BigInt(view.getUint8(offset + 4));
160
+ const b5 = BigInt(view.getUint8(offset + 5));
161
+ return BigInt(view.getUint8(offset + 6)) << 48n | b5 << 40n | b4 << 32n | b3 << 24n | b2 << 16n | b1 << 8n | b0;
162
+ }
163
+ case 8: return view.getBigUint64(offset, true);
164
+ default: throw new RangeError(`Invalid timestamp length ${length}`);
165
+ }
228
166
  }
229
167
  /**
230
- * Read raw WPILOG records from a byte stream.
168
+ * Read raw WPILOG records from an in-memory buffer.
231
169
  *
232
170
  * Yields a header record first, then control and data records in order.
233
171
  * Data record payloads are not decoded — use {@link decodeRecords} for that.
234
172
  */
235
- async function* readRecords(input) {
236
- const inputStream = new InputStream(toReadableStream(input));
173
+ function* readRecords(input) {
174
+ const bytes = toUint8Array(input);
175
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
176
+ const total = bytes.byteLength;
177
+ if (total < 12) throw new Error("Not a WPILOG file (truncated header)");
178
+ for (let i = 0; i < MAGIC.byteLength; i++) if (bytes[i] !== MAGIC[i]) throw new Error("Not a WPILOG file (invalid magic bytes)");
179
+ const versionMinor = view.getUint8(6);
180
+ const versionMajor = view.getUint8(7);
181
+ const extraHeaderLength = view.getUint32(8, true);
182
+ let offset = 12;
183
+ if (offset + extraHeaderLength > total) throw new Error("Not a WPILOG file (truncated extra header)");
184
+ const extraHeader = TEXT_DECODER$1.decode(bytes.subarray(offset, offset + extraHeaderLength));
185
+ offset += extraHeaderLength;
237
186
  yield {
238
187
  kind: "header",
239
- header: await readHeader(inputStream)
240
- };
241
- while (true) {
242
- let headerLength;
243
- try {
244
- headerLength = await readRecordHeaderLength(inputStream);
245
- } catch (error) {
246
- if (error instanceof StreamFinishedError) return;
247
- throw error;
188
+ header: {
189
+ version: {
190
+ major: versionMajor,
191
+ minor: versionMinor
192
+ },
193
+ extraHeader
248
194
  }
249
- const entryId = await readVarInt(inputStream, headerLength.entryIdLength);
250
- const payloadSize = await readVarInt(inputStream, headerLength.payloadSizeLength);
251
- const timestamp = await readTimestamp(inputStream, headerLength.timestampLength);
252
- const payload = await inputStream.readBytesAndAdvance(payloadSize);
195
+ };
196
+ while (offset < total) {
197
+ const bitfield = view.getUint8(offset);
198
+ offset += 1;
199
+ const entryIdLength = 1 + (bitfield & 3);
200
+ const payloadSizeLength = 1 + (bitfield >> 2 & 3);
201
+ const timestampLength = 1 + (bitfield >> 4 & 7);
202
+ if (offset + entryIdLength + payloadSizeLength + timestampLength > total) return;
203
+ const entryId = readVarInt(view, offset, entryIdLength);
204
+ offset += entryIdLength;
205
+ const payloadSize = readVarInt(view, offset, payloadSizeLength);
206
+ offset += payloadSizeLength;
207
+ const timestamp = readTimestamp(view, offset, timestampLength);
208
+ offset += timestampLength;
209
+ if (offset + payloadSize > total) return;
210
+ const payload = bytes.subarray(offset, offset + payloadSize);
211
+ offset += payloadSize;
253
212
  if (entryId === 0) yield {
254
213
  kind: "control",
255
214
  entryId,
@@ -869,7 +828,7 @@ function normalizeEntryName(rawName) {
869
828
  * Accepts the output of {@link readRecords} and yields fully decoded records,
870
829
  * including struct decoding with dependency resolution.
871
830
  */
872
- async function* decodeRecords(records) {
831
+ function* decodeRecords(records) {
873
832
  const asyncDecodedStructs = [];
874
833
  const structDecodeQueue = new StructDecodeQueue((structName, queuedRecords) => {
875
834
  for (const raw of queuedRecords) {
@@ -907,7 +866,7 @@ async function* decodeRecords(records) {
907
866
  });
908
867
  const structRegistry = new StructRegistry(structDecodeQueue);
909
868
  const context = /* @__PURE__ */ new Map();
910
- for await (const readRecord of records) {
869
+ for (const readRecord of records) {
911
870
  if (readRecord.kind === "header") continue;
912
871
  if (readRecord.kind === "control") {
913
872
  const controlPayload = readRecord.payload;
@@ -1091,9 +1050,9 @@ function structPayloadToJson(payload) {
1091
1050
  * Data record bytes are still read from the stream but not decoded,
1092
1051
  * making this much cheaper than full decoding via {@link decodeRecords}.
1093
1052
  */
1094
- async function* catalogEntries(records) {
1053
+ function* catalogEntries(records) {
1095
1054
  const entries = /* @__PURE__ */ new Map();
1096
- for await (const record of records) {
1055
+ for (const record of records) {
1097
1056
  if (record.kind !== "control") continue;
1098
1057
  if (record.payload.controlRecordType === ControlRecordType.Start) {
1099
1058
  const entry = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wpilog-parser",
3
- "version": "1.0.1",
3
+ "version": "2.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",
@@ -25,6 +25,7 @@
25
25
  "access": "public"
26
26
  },
27
27
  "scripts": {
28
+ "bench": "vp test bench",
28
29
  "build": "vp pack",
29
30
  "check": "vp check",
30
31
  "dev": "vp pack --watch",
@@ -37,14 +38,18 @@
37
38
  "chevrotain": "^12.0.0"
38
39
  },
39
40
  "devDependencies": {
40
- "@types/node": "24.12.0",
41
- "@typescript/native-preview": "7.0.0-dev.20260401.1",
42
- "typescript": "6.0.2",
43
- "vite-plus": "0.1.15"
41
+ "@codspeed/vitest-plugin": "^5.2.0",
42
+ "@types/node": "24.12.2",
43
+ "@typescript/native-preview": "7.0.0-dev.20260416.2",
44
+ "typescript": "6.0.3",
45
+ "vite-plus": "0.1.18"
44
46
  },
45
47
  "overrides": {
46
48
  "vite": "npm:@voidzero-dev/vite-plus-core@latest",
47
49
  "vitest": "npm:@voidzero-dev/vite-plus-test@latest"
48
50
  },
49
- "packageManager": "bun@1.3.10"
51
+ "packageManager": "bun@1.3.10",
52
+ "patchedDependencies": {
53
+ "@codspeed/vitest-plugin@5.2.0": "patches/@codspeed%2Fvitest-plugin@5.2.0.patch"
54
+ }
50
55
  }