@garmin/fitsdk 21.158.0 → 21.161.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.
@@ -0,0 +1,269 @@
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.161.0Release
9
+ // Tag = production/release/21.161.0-0-g58854c0
10
+ /////////////////////////////////////////////////////////////////////////////////////////////
11
+
12
+
13
+ import FIT from "./fit.js";
14
+ import Profile from "./profile.js";
15
+
16
+ const textEncoder = new TextEncoder();
17
+
18
+ class MesgDefinition {
19
+ globalMesgNumber;
20
+ localMesgNum;
21
+ fieldDefinitions = [];
22
+ developerFieldDefinitions = [];
23
+
24
+ constructor(mesgNum, mesg, { fieldDescriptions = null, } = {}) {
25
+ try {
26
+ if (mesg == null) {
27
+ throw new Error("mesg is missing or null");
28
+ }
29
+
30
+ if (mesgNum == null) {
31
+ throw new Error("mesgNum is missing or null");
32
+ }
33
+
34
+ const mesgProfile = Profile.messages[mesgNum];
35
+
36
+ if (mesgProfile == null) {
37
+ throw new Error(`mesgNum: ${mesgNum} could not be found in the Profile`);
38
+ }
39
+
40
+ this.globalMesgNumber = mesgNum;
41
+ this.localMesgNum = 0;
42
+
43
+ Object.keys(mesg).forEach((fieldName) => {
44
+ if (mesg[fieldName] == null) {
45
+ return;
46
+ }
47
+
48
+ const fieldProfile = Object.entries(mesgProfile.fields).find(([, fieldProfile,]) => {
49
+ return fieldProfile.name === fieldName;
50
+ });
51
+
52
+ if (fieldProfile == null) {
53
+ return;
54
+ }
55
+
56
+ const baseType = FIT.FieldTypeToBaseType[fieldProfile[1].baseType];
57
+ const baseTypeDef = FIT.BaseTypeDefinitions[baseType];
58
+
59
+ this.fieldDefinitions.push({
60
+ name: fieldName,
61
+ num: fieldProfile[1].num,
62
+ size: this.#fieldSize(mesg[fieldName], baseTypeDef),
63
+ baseType: baseType,
64
+ type: fieldProfile[1].type,
65
+ scale: fieldProfile[1].scale,
66
+ offset: fieldProfile[1].offset,
67
+ });
68
+ });
69
+
70
+ Object.keys(mesg.developerFields ?? {})?.sort()?.forEach((key) => {
71
+ const { developerDataIdMesg, fieldDescriptionMesg, } = this.#fieldDescriptionForKey(fieldDescriptions, key);
72
+
73
+ const baseTypeDef = FIT.BaseTypeDefinitions[fieldDescriptionMesg.fitBaseTypeId];
74
+
75
+ this.developerFieldDefinitions.push({
76
+ key,
77
+ baseType: fieldDescriptionMesg.fitBaseTypeId,
78
+ fieldDefinitionNumber: fieldDescriptionMesg.fieldDefinitionNumber,
79
+ size: this.#fieldSize(mesg.developerFields[key], baseTypeDef),
80
+ developerDataIndex: developerDataIdMesg.developerDataIndex,
81
+ });
82
+ });
83
+
84
+ if (this.fieldDefinitions.length === 0) {
85
+ throw new Error("No valid fields were found in the message");
86
+ }
87
+
88
+ if (this.fieldDefinitions.some((fieldDefinition) => {
89
+ return fieldDefinition.size > FIT.MAX_FIELD_SIZE;
90
+ })) {
91
+ throw new Error(`Some field sizes are greater than ${FIT.MAX_FIELD_SIZE}`, { cause: this.fieldDefinitions, });
92
+ }
93
+ }
94
+ catch (error) {
95
+ throw new Error(
96
+ "Could not construct MesgDefinition from Message", {
97
+ cause: {
98
+ cause: {
99
+ message: error.message,
100
+ cause: error.cause,
101
+ },
102
+ },
103
+ }
104
+ );
105
+ }
106
+ }
107
+
108
+ write(outputStream) {
109
+ // Header
110
+ let headerByte = FIT.MESG_DEFINITION_MASK | (this.localMesgNum & FIT.LOCAL_MESG_NUM_MASK);
111
+ if (this.developerFieldDefinitions.length > 0) {
112
+ headerByte |= FIT.DEV_DATA_MASK;
113
+ }
114
+
115
+ outputStream.writeUInt8(headerByte);
116
+
117
+ // Reserved Byte
118
+ outputStream.writeUInt8(0x00);
119
+
120
+ // Architecture
121
+ outputStream.writeUInt8(FIT.ARCH_LITTLE_ENDIAN);
122
+
123
+ // Global Message Number
124
+ outputStream.writeUInt16(this.globalMesgNumber);
125
+
126
+ // Field Count
127
+ outputStream.writeUInt8(this.fieldDefinitions.length);
128
+
129
+ // Field Definitions
130
+ this.fieldDefinitions.forEach((fieldDefinition) => {
131
+ outputStream.writeUInt8(fieldDefinition.num);
132
+ outputStream.writeUInt8(fieldDefinition.size);
133
+ outputStream.writeUInt8(fieldDefinition.baseType);
134
+ });
135
+
136
+ // Developer Field Definitions
137
+ if (this.developerFieldDefinitions.length > 0) {
138
+ outputStream.writeUInt8(this.developerFieldDefinitions.length);
139
+
140
+ this.developerFieldDefinitions.forEach((developerFieldDefinition) => {
141
+ outputStream.writeUInt8(developerFieldDefinition.fieldDefinitionNumber);
142
+ outputStream.writeUInt8(developerFieldDefinition.size);
143
+ outputStream.writeUInt8(developerFieldDefinition.developerDataIndex);
144
+ });
145
+ }
146
+ }
147
+
148
+ equals(other) {
149
+ if (this.globalMessageNumber !== other.globalMessageNumber
150
+ || this.fieldDefinitions.length !== other.fieldDefinitions.length
151
+ || this.developerFieldDefinitions.length !== other.developerFieldDefinitions.length) {
152
+ return false;
153
+ }
154
+
155
+ // Field Definitions
156
+ for (let i = 0; i < this.fieldDefinitions.length; i++) {
157
+ const lhs = this.fieldDefinitions[i];
158
+
159
+ if (null == other.fieldDefinitions.find((rhs) => {
160
+ return lhs.num === rhs.num
161
+ && lhs.size === rhs.size
162
+ && lhs.baseType === rhs.baseType;
163
+ })) {
164
+ return false;
165
+ }
166
+ }
167
+
168
+ // Developer Field Definitions
169
+ for (let i = 0; i < this.developerFieldDefinitions.length; i++) {
170
+ const lhs = this.developerFieldDefinitions[i];
171
+
172
+ if (null == other.developerFieldDefinitions.find((rhs) => {
173
+ return lhs.fieldDefinitionNumber === rhs.fieldDefinitionNumber
174
+ && lhs.size === rhs.size
175
+ && lhs.developerDataIndex === rhs.developerDataIndex;
176
+ })) {
177
+ return false;
178
+ }
179
+ }
180
+
181
+ return true;
182
+ }
183
+
184
+ #fieldSize(value, baseTypeDef) {
185
+ const values = Array.isArray(value) ? value : [value,];
186
+
187
+ if (baseTypeDef.type === FIT.BaseType.STRING) {
188
+ const size = values.reduce(
189
+ (accumulator, currentValue) => {
190
+ return accumulator + textEncoder.encode(currentValue).length + 1;
191
+ },
192
+ 0
193
+ );
194
+
195
+ return size;
196
+ }
197
+
198
+ return baseTypeDef.size * values.length;
199
+ };
200
+
201
+ /**
202
+ * Look up the field description for the key, and validate the required fields.
203
+ */
204
+ #fieldDescriptionForKey(fieldDescriptions, key) {
205
+ try {
206
+ if (fieldDescriptions == null) {
207
+ throw new Error("no developer data field descriptions provided", {
208
+ cause: {
209
+ fieldDescriptions,
210
+ },
211
+ });
212
+ }
213
+
214
+ const { developerDataIdMesg, fieldDescriptionMesg, } = fieldDescriptions?.[key] ?? {};
215
+
216
+ if (developerDataIdMesg == null || fieldDescriptionMesg == null) {
217
+ throw new Error(`could not find a developer field description for key ${key}`);
218
+ }
219
+
220
+ const errors = [];
221
+
222
+ if (fieldDescriptionMesg.fitBaseTypeId == null) {
223
+ errors.push(`fieldDescriptionMesg fitBaseTypeId is ${fieldDescriptionMesg.fitBaseTypeId}`);
224
+ }
225
+ if (fieldDescriptionMesg.fieldDefinitionNumber == null) {
226
+ errors.push(`fieldDescriptionMesg fieldDefinitionNumber is ${fieldDescriptionMesg.fieldDefinitionNumber}`);
227
+ }
228
+ if (fieldDescriptionMesg.developerDataIndex == null) {
229
+ errors.push(`fieldDescriptionMesg developerDataIndex is ${fieldDescriptionMesg.developerDataIndex}`);
230
+ }
231
+ if (developerDataIdMesg.developerDataIndex == null) {
232
+ errors.push(`developerDataIdMesg developerDataIndex is ${developerDataIdMesg.developerDataIndex}`);
233
+ }
234
+
235
+ if (developerDataIdMesg.developerDataIndex !== fieldDescriptionMesg.developerDataIndex) {
236
+ errors.push("developerDataIndex values do not match in fieldDescription"
237
+ + ` ${developerDataIdMesg.developerDataIndex} != ${fieldDescriptionMesg.developerDataIndex}`
238
+ );
239
+ }
240
+
241
+ if (errors.length > 0) {
242
+ throw new Error("missing or invalid values in the fieldDescription,", {
243
+ cause: {
244
+ developerDataIdMesg,
245
+ fieldDescriptionMesg,
246
+ cause: errors,
247
+ },
248
+ });
249
+ }
250
+
251
+ return { key, developerDataIdMesg, fieldDescriptionMesg, };
252
+ }
253
+ catch (error) {
254
+ throw new Error(
255
+ `invalid field description for key ${key}`, {
256
+ cause: {
257
+ key,
258
+ cause: {
259
+ message: error.message,
260
+ cause: error.cause,
261
+ },
262
+ },
263
+ }
264
+ );
265
+ }
266
+ }
267
+ }
268
+
269
+ export default MesgDefinition;
@@ -0,0 +1,220 @@
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.161.0Release
9
+ // Tag = production/release/21.161.0-0-g58854c0
10
+ /////////////////////////////////////////////////////////////////////////////////////////////
11
+
12
+
13
+ import FIT from "./fit.js";
14
+
15
+ const ONE_MEGABYTE = 1048576;
16
+ const TEN_MEGABYTES = ONE_MEGABYTE * 10;
17
+ const HALF_MEGABYTE = ONE_MEGABYTE / 2;
18
+
19
+ class OutputStream {
20
+ #arrayBuffer = null;
21
+ #dataView = null;
22
+ #byteOffset = 0;
23
+ #resizeByBytes = 0;
24
+ #baseTypeDefinitions = null;
25
+ #textEncoder = new TextEncoder();
26
+
27
+ /**
28
+ * Creates an OutputStream
29
+ * @constructor
30
+ * @param {Object=} [options] - Read options (optional)
31
+ * @param {Number} [options.initialByteLength=0.5MB] - (optional, default 0.5 MB)
32
+ * @param {Number} [options.maxByteLength=2MB] - (optional, default 2 MB)
33
+ * @param {Number} [options.resizeByBytes=0.5MB] - (optional, default 0.5 MB)
34
+ */
35
+ constructor({
36
+ initialByteLength = HALF_MEGABYTE,
37
+ maxByteLength = TEN_MEGABYTES,
38
+ resizeByBytes = HALF_MEGABYTE,
39
+ } = {}) {
40
+ this.#arrayBuffer = new ArrayBuffer(initialByteLength, { maxByteLength, });
41
+ this.#dataView = new DataView(this.#arrayBuffer);
42
+
43
+ this.#resizeByBytes = resizeByBytes;
44
+
45
+ this.#baseTypeDefinitions = {
46
+ [FIT.BaseType.ENUM]: { setValue: this.#dataView.setUint8.bind(this.#dataView), size: 1, mask: 0xFF, },
47
+ [FIT.BaseType.UINT8]: { setValue: this.#dataView.setUint8.bind(this.#dataView), size: 1, mask: 0xFF, },
48
+ [FIT.BaseType.UINT16]: { setValue: this.#dataView.setUint16.bind(this.#dataView), size: 2, mask: 0xFFFF, },
49
+ [FIT.BaseType.UINT32]: { setValue: this.#dataView.setUint32.bind(this.#dataView), size: 4, mask: 0xFFFFFFFF, },
50
+ [FIT.BaseType.UINT64]: { setValue: this.#dataView.setBigUint64.bind(this.#dataView), size: 8, mask: 0xFFFFFFFFFFFFFFFFn, },
51
+ [FIT.BaseType.SINT8]: { setValue: this.#dataView.setInt8.bind(this.#dataView), size: 1, mask: 0xFF, },
52
+ [FIT.BaseType.SINT16]: { setValue: this.#dataView.setInt16.bind(this.#dataView), size: 2, mask: 0xFFFF, },
53
+ [FIT.BaseType.SINT32]: { setValue: this.#dataView.setInt32.bind(this.#dataView), size: 4, mask: 0xFFFFFFFF, },
54
+ [FIT.BaseType.SINT64]: { setValue: this.#dataView.setBigInt64.bind(this.#dataView), size: 8, mask: 0xFFFFFFFFFFFFFFFFn, },
55
+ [FIT.BaseType.FLOAT32]: { setValue: this.#dataView.setFloat32.bind(this.#dataView), size: 4, },
56
+ [FIT.BaseType.FLOAT64]: { setValue: this.#dataView.setFloat64.bind(this.#dataView), size: 8, },
57
+ [FIT.BaseType.UINT8Z]: { setValue: this.#dataView.setUint8.bind(this.#dataView), size: 1, mask: 0xFF, },
58
+ [FIT.BaseType.UINT16Z]: { setValue: this.#dataView.setUint16.bind(this.#dataView), size: 2, mask: 0xFFFF, },
59
+ [FIT.BaseType.UINT32Z]: { setValue: this.#dataView.setUint32.bind(this.#dataView), size: 4, mask: 0xFFFFFFFF, },
60
+ [FIT.BaseType.UINT64Z]: { setValue: this.#dataView.setBigUint64.bind(this.#dataView), size: 8, mask: 0xFFFFFFFFFFFFFFFFn, },
61
+ [FIT.BaseType.BYTE]: { setValue: this.#dataView.setUint8.bind(this.#dataView), size: 1, mask: 0xFF, },
62
+ };
63
+ }
64
+
65
+ get length() {
66
+ return this.#byteOffset;
67
+ }
68
+ get uint8Array() {
69
+ return new Uint8Array(this.#arrayBuffer.slice(0, this.#byteOffset));
70
+ }
71
+
72
+ writeUInt8(value) {
73
+ return this.write(value, FIT.BaseType.UINT8);
74
+ }
75
+
76
+ writeUInt16(value) {
77
+ return this.write(value, FIT.BaseType.UINT16);
78
+ }
79
+
80
+ writeUInt32(value) {
81
+ return this.write(value, FIT.BaseType.UINT32);
82
+ }
83
+
84
+ writeUInt64(value) {
85
+ return this.write(value, FIT.BaseType.UINT64);
86
+ }
87
+
88
+ writeSInt8(value) {
89
+ return this.write(value, FIT.BaseType.SINT8);
90
+ }
91
+
92
+ writeSInt16(value) {
93
+ return this.write(value, FIT.BaseType.SINT16);
94
+ }
95
+
96
+ writeSInt32(value) {
97
+ return this.write(value, FIT.BaseType.SINT32);
98
+ }
99
+
100
+ writeSInt64(value) {
101
+ return this.write(value, FIT.BaseType.SINT64);
102
+ }
103
+
104
+ writeFloat32(value) {
105
+ return this.write(value, FIT.BaseType.FLOAT32);
106
+ }
107
+
108
+ writeFloat64(value) {
109
+ return this.write(value, FIT.BaseType.FLOAT64);
110
+ }
111
+
112
+ writeUInt8z(value) {
113
+ return this.write(value, FIT.BaseType.UINT8Z);
114
+ }
115
+
116
+ writeUInt16z(value) {
117
+ return this.write(value, FIT.BaseType.UINT16Z);
118
+ }
119
+
120
+ writeUInt32z(value) {
121
+ return this.write(value, FIT.BaseType.UINT32Z);
122
+ }
123
+
124
+ writeUInt64z(value) {
125
+ return this.write(value, FIT.BaseType.UINT64Z);
126
+ }
127
+
128
+ writeByte(value) {
129
+ return this.write(value, FIT.BaseType.BYTE);
130
+ }
131
+
132
+ writeString(text) {
133
+ const bytes = this.#textEncoder.encode(text);
134
+
135
+ this.#resizeIfNeeded(bytes.byteLength);
136
+
137
+ const uint8Array = new Uint8Array(this.#arrayBuffer, this.#byteOffset, bytes.byteLength);
138
+ uint8Array.set(bytes);
139
+
140
+ this.#byteOffset += bytes.byteLength;
141
+
142
+ // Add a null terminator
143
+ this.writeUInt8(0);
144
+
145
+ return this;
146
+ }
147
+
148
+ write(value, baseType) {
149
+ if (baseType === FIT.BaseType.STRING) {
150
+ return this.writeString(value);
151
+ }
152
+
153
+ this.#setValues(baseType, value);
154
+
155
+ return;
156
+ }
157
+
158
+ set(typedarray, targetOffset = 0) {
159
+ this.#resizeIfNeeded(typedarray.byteLength + targetOffset);
160
+
161
+ const uint8Array = new Uint8Array(this.#arrayBuffer);
162
+ uint8Array.set(typedarray, targetOffset);
163
+
164
+ this.#byteOffset = Math.max(this.#byteOffset, typedarray.byteLength + targetOffset);
165
+
166
+ return this;
167
+ }
168
+
169
+ [Symbol.iterator]() {
170
+ let start = 0;
171
+ const end = this.#byteOffset;
172
+ const dataView = this.#dataView;
173
+
174
+ return {
175
+ next() {
176
+ if (start < end) {
177
+ return { value: dataView.getUint8(start++), done: false, };
178
+ }
179
+ else {
180
+ return { done: true, };
181
+ }
182
+ },
183
+ };
184
+ }
185
+
186
+ #setValues(baseType, value) {
187
+ const values = Array.isArray(value) ? value : [value,];
188
+
189
+ values.forEach((value) => {
190
+ return this.#setValue(baseType, value);
191
+ });
192
+ }
193
+
194
+ #setValue(baseType, value) {
195
+ const def = this.#baseTypeDefinitions[baseType];
196
+
197
+ this.#resizeIfNeeded(def.size);
198
+
199
+ const val = def.mask == null ? value : value & def.mask;
200
+
201
+ def.setValue(this.#byteOffset, val, true);
202
+ this.#byteOffset += def.size;
203
+
204
+ return this;
205
+ }
206
+
207
+ #resizeIfNeeded(byteCount = 1) {
208
+ if (this.#arrayBuffer.byteLength - this.#byteOffset >= byteCount) {
209
+ return;
210
+ }
211
+
212
+ if (!this.#arrayBuffer.resizable) {
213
+ throw new Error("Can not resize OutputStream. Set a larger initial size.");
214
+ }
215
+
216
+ this.#arrayBuffer.resize(this.#arrayBuffer.byteLength + Math.max(this.#resizeByBytes, byteCount));
217
+ }
218
+ }
219
+
220
+ export default OutputStream;