@garmin/fitsdk 21.168.0 → 21.169.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/src/stream.js CHANGED
@@ -1,259 +1,259 @@
1
- /////////////////////////////////////////////////////////////////////////////////////////////
2
- // Copyright 2025 Garmin International, Inc.
3
- // Licensed under the Flexible and Interoperable Data Transfer (FIT) Protocol License; you
4
- // may not use this file except in compliance with the Flexible and Interoperable Data
5
- // Transfer (FIT) Protocol License.
6
- /////////////////////////////////////////////////////////////////////////////////////////////
7
- // ****WARNING**** This file is auto-generated! Do NOT edit this file.
8
- // Profile Version = 21.168.0Release
9
- // Tag = production/release/21.168.0-0-gb831b31
10
- /////////////////////////////////////////////////////////////////////////////////////////////
11
-
12
-
13
- import FIT from "./fit.js";
14
- import UtilsInternal from "./utils-internal.js";
15
-
16
- class Stream {
17
- static LITTLE_ENDIAN = true;
18
- static BIG_ENDIAN = false;
19
-
20
- #position = 0;
21
- #arrayBuffer = null;
22
- #textDecoder = new TextDecoder("utf-8", { fatal: false, ignoreBOM: true });
23
- #crcCalculator = null;
24
-
25
- /**
26
- * Convenience method for creating a Stream from a byte array
27
- * @param {Array<number>} data An array of bytes
28
- * @returns {Stream} A new Stream object
29
- * @static
30
- */
31
- static fromByteArray(data) {
32
- const buf = new Uint8Array(data);
33
- return this.fromArrayBuffer(buf.buffer);
34
- }
35
-
36
- /**
37
- * Convenience method for creating a Stream from a Node Buffer
38
- * @param {Buffer} buffer - Node Buffer of bytes
39
- * @returns {Stream} A new Stream object
40
- * @static
41
- */
42
- static fromBuffer(buffer) {
43
- const arrayBuffer = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
44
- return this.fromArrayBuffer(arrayBuffer);
45
- }
46
-
47
- /**
48
- * Convenience method for creating a Stream from an ArrayBuffer
49
- * @param {ArrayBuffer} arrayBuffer - An ArrayBuffer of bytes
50
- * @returns {Stream} A new Stream object
51
- * @static
52
- */
53
- static fromArrayBuffer(arrayBuffer) {
54
- const stream = new Stream(arrayBuffer);
55
- return stream;
56
- }
57
-
58
- /**
59
- * Creates a Stream containing a FIT file
60
- * @constructor
61
- * @param {ArrayBuffer} stream - ArrayBuffer containing a FIT file
62
- */
63
- constructor(arrayBuffer) {
64
- this.#position = 0;
65
- this.#arrayBuffer = arrayBuffer;
66
- }
67
-
68
- get length() {
69
- return this.#arrayBuffer.byteLength;
70
- }
71
-
72
- get bytesRead() {
73
- return this.#position;
74
- }
75
-
76
- get position() {
77
- return this.#position;
78
- }
79
-
80
- get crcCalculator() {
81
- return this.#crcCalculator;
82
- }
83
-
84
- set crcCalculator(crcCalculator) {
85
- this.#crcCalculator = crcCalculator;
86
- }
87
-
88
- reset() {
89
- this.seek(0);
90
- }
91
-
92
- seek(position) {
93
- this.#position = position;
94
- }
95
-
96
- slice(begin, end) {
97
- return this.#arrayBuffer.slice(begin, end);
98
- }
99
-
100
- peekByte() {
101
- const arrayBuffer = this.#arrayBuffer.slice(this.#position, this.#position + 1);
102
- const dataView = new DataView(arrayBuffer);
103
- return dataView.getUint8(0);
104
- }
105
-
106
- readByte() {
107
- return this.readUInt8();
108
- }
109
-
110
- readBytes(size) {
111
- if (this.#position + size > this.#arrayBuffer.byteLength) {
112
- throw Error(`FIT Runtime Error end of stream at byte ${this.#position}`);
113
- }
114
-
115
- const bytes = new Uint8Array(this.#arrayBuffer, this.#position, size);
116
- this.#position += size;
117
-
118
- this.#crcCalculator?.addBytes(bytes, 0, size);
119
-
120
- return bytes;
121
- }
122
-
123
- readUInt8(opts) {
124
- return this.readValue(FIT.BaseType.UINT8, 1, { convertInvalidToNull: false, ...opts });
125
- }
126
-
127
- readInt8(opts) {
128
- return this.readValue(FIT.BaseType.SINT8, 1, { convertInvalidToNull: false, ...opts });
129
- }
130
-
131
- readUInt16(opts) {
132
- return this.readValue(FIT.BaseType.UINT16, 2, { convertInvalidToNull: false, ...opts });
133
- }
134
-
135
- readInt16(opts) {
136
- return this.readValue(FIT.BaseType.SINT16, 2, { convertInvalidToNull: false, ...opts });
137
- }
138
-
139
- readUInt32(opts) {
140
- return this.readValue(FIT.BaseType.UINT32, 4, { convertInvalidToNull: false, ...opts });
141
- }
142
-
143
- readInt32(opts) {
144
- return this.readValue(FIT.BaseType.SINT32, 4, { convertInvalidToNull: false, ...opts });
145
- }
146
-
147
- readUInt64(opts) {
148
- return this.readValue(FIT.BaseType.UINT64, 8, { convertInvalidToNull: false, ...opts });
149
- }
150
-
151
- readInt64(opts) {
152
- return this.readValue(FIT.BaseType.SINT64, 8, { convertInvalidToNull: false, ...opts });
153
- }
154
-
155
- readFloat32(opts) {
156
- return this.readValue(FIT.BaseType.FLOAT32, 4, { convertInvalidToNull: false, ...opts });
157
- }
158
-
159
- readFloat64(opts) {
160
- return this.readValue(FIT.BaseType.FLOAT64, 8, { convertInvalidToNull: false, ...opts });
161
- }
162
-
163
- readString(strlen) {
164
- return this.readValue(FIT.BaseType.STRING, strlen);
165
- }
166
-
167
- readValue(baseType, size, { endianness = Stream.LITTLE_ENDIAN, convertInvalidToNull = true } = {}) {
168
- const baseTypeSize = FIT.BaseTypeDefinitions[baseType].size;
169
- const baseTypeInvalid = FIT.BaseTypeDefinitions[baseType].invalid;
170
-
171
- const bytes = this.readBytes(size);
172
-
173
- if (size % baseTypeSize !== 0) {
174
- return convertInvalidToNull ? null : baseTypeInvalid;
175
- }
176
-
177
- if (baseType === FIT.BaseType.STRING) {
178
- const string = this.#textDecoder.decode(bytes).replace(/\uFFFD/g, "");
179
- const strings = string.split('\0');
180
-
181
- while (strings[strings.length - 1] === "") {
182
- strings.pop();
183
- }
184
-
185
- if (strings.length === 0) {
186
- return null;
187
- }
188
-
189
- return strings.length === 1 ? strings[0] : strings;
190
- }
191
-
192
- const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
193
- let values = [];
194
-
195
- const count = size / baseTypeSize;
196
-
197
- for (let i = 0; i < count; i++) {
198
-
199
- switch (baseType) {
200
- case FIT.BaseType.BYTE:
201
- case FIT.BaseType.ENUM:
202
- case FIT.BaseType.UINT8:
203
- case FIT.BaseType.UINT8Z:
204
- values.push(dataView.getUint8(i * baseTypeSize));
205
- break;
206
-
207
- case FIT.BaseType.SINT8:
208
- values.push(dataView.getInt8(i * baseTypeSize));
209
- break;
210
-
211
- case FIT.BaseType.UINT16:
212
- case FIT.BaseType.UINT16Z:
213
- values.push(dataView.getUint16(i * baseTypeSize, endianness));
214
- break;
215
-
216
- case FIT.BaseType.SINT16:
217
- values.push(dataView.getInt16(i * baseTypeSize, endianness));
218
- break;
219
-
220
- case FIT.BaseType.UINT32:
221
- case FIT.BaseType.UINT32Z:
222
- values.push(dataView.getUint32(i * baseTypeSize, endianness));
223
- break;
224
-
225
- case FIT.BaseType.SINT32:
226
- values.push(dataView.getInt32(i * baseTypeSize, endianness));
227
- break;
228
-
229
- case FIT.BaseType.UINT64:
230
- case FIT.BaseType.UINT64Z:
231
- values.push(dataView.getBigUint64(i * baseTypeSize, endianness));
232
- break;
233
- case FIT.BaseType.SINT64:
234
- values.push(dataView.getBigInt64(i * baseTypeSize, endianness));
235
- break;
236
-
237
- case FIT.BaseType.FLOAT32:
238
- values.push(dataView.getFloat32(i * baseTypeSize, endianness));
239
- break;
240
-
241
- case FIT.BaseType.FLOAT64:
242
- values.push(dataView.getFloat64(i * baseTypeSize, endianness));
243
- break;
244
- }
245
- }
246
-
247
- if (baseType === FIT.BaseType.BYTE) {
248
- return UtilsInternal.onlyInvalidValues(values, baseTypeInvalid) ? null : values;
249
- }
250
-
251
- if (convertInvalidToNull) {
252
- values = values.map(value => value === baseTypeInvalid ? null : value);
253
- }
254
-
255
- return UtilsInternal.sanitizeValues(values);
256
- }
257
- }
258
-
1
+ /////////////////////////////////////////////////////////////////////////////////////////////
2
+ // Copyright 2025 Garmin International, Inc.
3
+ // Licensed under the Flexible and Interoperable Data Transfer (FIT) Protocol License; you
4
+ // may not use this file except in compliance with the Flexible and Interoperable Data
5
+ // Transfer (FIT) Protocol License.
6
+ /////////////////////////////////////////////////////////////////////////////////////////////
7
+ // ****WARNING**** This file is auto-generated! Do NOT edit this file.
8
+ // Profile Version = 21.169.0Release
9
+ // Tag = production/release/21.169.0-0-g7105132
10
+ /////////////////////////////////////////////////////////////////////////////////////////////
11
+
12
+
13
+ import FIT from "./fit.js";
14
+ import UtilsInternal from "./utils-internal.js";
15
+
16
+ class Stream {
17
+ static LITTLE_ENDIAN = true;
18
+ static BIG_ENDIAN = false;
19
+
20
+ #position = 0;
21
+ #arrayBuffer = null;
22
+ #textDecoder = new TextDecoder("utf-8", { fatal: false, ignoreBOM: true });
23
+ #crcCalculator = null;
24
+
25
+ /**
26
+ * Convenience method for creating a Stream from a byte array
27
+ * @param {Array<number>} data An array of bytes
28
+ * @returns {Stream} A new Stream object
29
+ * @static
30
+ */
31
+ static fromByteArray(data) {
32
+ const buf = new Uint8Array(data);
33
+ return this.fromArrayBuffer(buf.buffer);
34
+ }
35
+
36
+ /**
37
+ * Convenience method for creating a Stream from a Node Buffer
38
+ * @param {Buffer} buffer - Node Buffer of bytes
39
+ * @returns {Stream} A new Stream object
40
+ * @static
41
+ */
42
+ static fromBuffer(buffer) {
43
+ const arrayBuffer = buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
44
+ return this.fromArrayBuffer(arrayBuffer);
45
+ }
46
+
47
+ /**
48
+ * Convenience method for creating a Stream from an ArrayBuffer
49
+ * @param {ArrayBuffer} arrayBuffer - An ArrayBuffer of bytes
50
+ * @returns {Stream} A new Stream object
51
+ * @static
52
+ */
53
+ static fromArrayBuffer(arrayBuffer) {
54
+ const stream = new Stream(arrayBuffer);
55
+ return stream;
56
+ }
57
+
58
+ /**
59
+ * Creates a Stream containing a FIT file
60
+ * @constructor
61
+ * @param {ArrayBuffer} stream - ArrayBuffer containing a FIT file
62
+ */
63
+ constructor(arrayBuffer) {
64
+ this.#position = 0;
65
+ this.#arrayBuffer = arrayBuffer;
66
+ }
67
+
68
+ get length() {
69
+ return this.#arrayBuffer.byteLength;
70
+ }
71
+
72
+ get bytesRead() {
73
+ return this.#position;
74
+ }
75
+
76
+ get position() {
77
+ return this.#position;
78
+ }
79
+
80
+ get crcCalculator() {
81
+ return this.#crcCalculator;
82
+ }
83
+
84
+ set crcCalculator(crcCalculator) {
85
+ this.#crcCalculator = crcCalculator;
86
+ }
87
+
88
+ reset() {
89
+ this.seek(0);
90
+ }
91
+
92
+ seek(position) {
93
+ this.#position = position;
94
+ }
95
+
96
+ slice(begin, end) {
97
+ return this.#arrayBuffer.slice(begin, end);
98
+ }
99
+
100
+ peekByte() {
101
+ const arrayBuffer = this.#arrayBuffer.slice(this.#position, this.#position + 1);
102
+ const dataView = new DataView(arrayBuffer);
103
+ return dataView.getUint8(0);
104
+ }
105
+
106
+ readByte() {
107
+ return this.readUInt8();
108
+ }
109
+
110
+ readBytes(size) {
111
+ if (this.#position + size > this.#arrayBuffer.byteLength) {
112
+ throw Error(`FIT Runtime Error end of stream at byte ${this.#position}`);
113
+ }
114
+
115
+ const bytes = new Uint8Array(this.#arrayBuffer, this.#position, size);
116
+ this.#position += size;
117
+
118
+ this.#crcCalculator?.addBytes(bytes, 0, size);
119
+
120
+ return bytes;
121
+ }
122
+
123
+ readUInt8(opts) {
124
+ return this.readValue(FIT.BaseType.UINT8, 1, { convertInvalidToNull: false, ...opts });
125
+ }
126
+
127
+ readInt8(opts) {
128
+ return this.readValue(FIT.BaseType.SINT8, 1, { convertInvalidToNull: false, ...opts });
129
+ }
130
+
131
+ readUInt16(opts) {
132
+ return this.readValue(FIT.BaseType.UINT16, 2, { convertInvalidToNull: false, ...opts });
133
+ }
134
+
135
+ readInt16(opts) {
136
+ return this.readValue(FIT.BaseType.SINT16, 2, { convertInvalidToNull: false, ...opts });
137
+ }
138
+
139
+ readUInt32(opts) {
140
+ return this.readValue(FIT.BaseType.UINT32, 4, { convertInvalidToNull: false, ...opts });
141
+ }
142
+
143
+ readInt32(opts) {
144
+ return this.readValue(FIT.BaseType.SINT32, 4, { convertInvalidToNull: false, ...opts });
145
+ }
146
+
147
+ readUInt64(opts) {
148
+ return this.readValue(FIT.BaseType.UINT64, 8, { convertInvalidToNull: false, ...opts });
149
+ }
150
+
151
+ readInt64(opts) {
152
+ return this.readValue(FIT.BaseType.SINT64, 8, { convertInvalidToNull: false, ...opts });
153
+ }
154
+
155
+ readFloat32(opts) {
156
+ return this.readValue(FIT.BaseType.FLOAT32, 4, { convertInvalidToNull: false, ...opts });
157
+ }
158
+
159
+ readFloat64(opts) {
160
+ return this.readValue(FIT.BaseType.FLOAT64, 8, { convertInvalidToNull: false, ...opts });
161
+ }
162
+
163
+ readString(strlen) {
164
+ return this.readValue(FIT.BaseType.STRING, strlen);
165
+ }
166
+
167
+ readValue(baseType, size, { endianness = Stream.LITTLE_ENDIAN, convertInvalidToNull = true } = {}) {
168
+ const baseTypeSize = FIT.BaseTypeDefinitions[baseType].size;
169
+ const baseTypeInvalid = FIT.BaseTypeDefinitions[baseType].invalid;
170
+
171
+ const bytes = this.readBytes(size);
172
+
173
+ if (size % baseTypeSize !== 0) {
174
+ return convertInvalidToNull ? null : baseTypeInvalid;
175
+ }
176
+
177
+ if (baseType === FIT.BaseType.STRING) {
178
+ const string = this.#textDecoder.decode(bytes).replace(/\uFFFD/g, "");
179
+ const strings = string.split('\0');
180
+
181
+ while (strings[strings.length - 1] === "") {
182
+ strings.pop();
183
+ }
184
+
185
+ if (strings.length === 0) {
186
+ return null;
187
+ }
188
+
189
+ return strings.length === 1 ? strings[0] : strings;
190
+ }
191
+
192
+ const dataView = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
193
+ let values = [];
194
+
195
+ const count = size / baseTypeSize;
196
+
197
+ for (let i = 0; i < count; i++) {
198
+
199
+ switch (baseType) {
200
+ case FIT.BaseType.BYTE:
201
+ case FIT.BaseType.ENUM:
202
+ case FIT.BaseType.UINT8:
203
+ case FIT.BaseType.UINT8Z:
204
+ values.push(dataView.getUint8(i * baseTypeSize));
205
+ break;
206
+
207
+ case FIT.BaseType.SINT8:
208
+ values.push(dataView.getInt8(i * baseTypeSize));
209
+ break;
210
+
211
+ case FIT.BaseType.UINT16:
212
+ case FIT.BaseType.UINT16Z:
213
+ values.push(dataView.getUint16(i * baseTypeSize, endianness));
214
+ break;
215
+
216
+ case FIT.BaseType.SINT16:
217
+ values.push(dataView.getInt16(i * baseTypeSize, endianness));
218
+ break;
219
+
220
+ case FIT.BaseType.UINT32:
221
+ case FIT.BaseType.UINT32Z:
222
+ values.push(dataView.getUint32(i * baseTypeSize, endianness));
223
+ break;
224
+
225
+ case FIT.BaseType.SINT32:
226
+ values.push(dataView.getInt32(i * baseTypeSize, endianness));
227
+ break;
228
+
229
+ case FIT.BaseType.UINT64:
230
+ case FIT.BaseType.UINT64Z:
231
+ values.push(dataView.getBigUint64(i * baseTypeSize, endianness));
232
+ break;
233
+ case FIT.BaseType.SINT64:
234
+ values.push(dataView.getBigInt64(i * baseTypeSize, endianness));
235
+ break;
236
+
237
+ case FIT.BaseType.FLOAT32:
238
+ values.push(dataView.getFloat32(i * baseTypeSize, endianness));
239
+ break;
240
+
241
+ case FIT.BaseType.FLOAT64:
242
+ values.push(dataView.getFloat64(i * baseTypeSize, endianness));
243
+ break;
244
+ }
245
+ }
246
+
247
+ if (baseType === FIT.BaseType.BYTE) {
248
+ return UtilsInternal.onlyInvalidValues(values, baseTypeInvalid) ? null : values;
249
+ }
250
+
251
+ if (convertInvalidToNull) {
252
+ values = values.map(value => value === baseTypeInvalid ? null : value);
253
+ }
254
+
255
+ return UtilsInternal.sanitizeValues(values);
256
+ }
257
+ }
258
+
259
259
  export default Stream;