@2702rebels/wpidata 1.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.
Files changed (80) hide show
  1. package/LICENSE +28 -0
  2. package/README.md +5 -0
  3. package/dist/abstractions.cjs +0 -0
  4. package/dist/abstractions.d.cts +246 -0
  5. package/dist/abstractions.d.cts.map +1 -0
  6. package/dist/abstractions.d.mts +246 -0
  7. package/dist/abstractions.d.mts.map +1 -0
  8. package/dist/abstractions.mjs +1 -0
  9. package/dist/formats/json.cjs +32 -0
  10. package/dist/formats/json.d.cts +14 -0
  11. package/dist/formats/json.d.cts.map +1 -0
  12. package/dist/formats/json.d.mts +14 -0
  13. package/dist/formats/json.d.mts.map +1 -0
  14. package/dist/formats/json.mjs +33 -0
  15. package/dist/formats/json.mjs.map +1 -0
  16. package/dist/formats/msgpack.cjs +30 -0
  17. package/dist/formats/msgpack.d.cts +14 -0
  18. package/dist/formats/msgpack.d.cts.map +1 -0
  19. package/dist/formats/msgpack.d.mts +14 -0
  20. package/dist/formats/msgpack.d.mts.map +1 -0
  21. package/dist/formats/msgpack.mjs +31 -0
  22. package/dist/formats/msgpack.mjs.map +1 -0
  23. package/dist/formats/protobuf.cjs +130 -0
  24. package/dist/formats/protobuf.d.cts +68 -0
  25. package/dist/formats/protobuf.d.cts.map +1 -0
  26. package/dist/formats/protobuf.d.mts +68 -0
  27. package/dist/formats/protobuf.d.mts.map +1 -0
  28. package/dist/formats/protobuf.mjs +128 -0
  29. package/dist/formats/protobuf.mjs.map +1 -0
  30. package/dist/formats/struct.cjs +593 -0
  31. package/dist/formats/struct.d.cts +134 -0
  32. package/dist/formats/struct.d.cts.map +1 -0
  33. package/dist/formats/struct.d.mts +134 -0
  34. package/dist/formats/struct.d.mts.map +1 -0
  35. package/dist/formats/struct.mjs +591 -0
  36. package/dist/formats/struct.mjs.map +1 -0
  37. package/dist/sink.cjs +360 -0
  38. package/dist/sink.d.cts +93 -0
  39. package/dist/sink.d.cts.map +1 -0
  40. package/dist/sink.d.mts +93 -0
  41. package/dist/sink.d.mts.map +1 -0
  42. package/dist/sink.mjs +361 -0
  43. package/dist/sink.mjs.map +1 -0
  44. package/dist/types/protobuf.cjs +0 -0
  45. package/dist/types/protobuf.d.cts +302 -0
  46. package/dist/types/protobuf.d.cts.map +1 -0
  47. package/dist/types/protobuf.d.mts +302 -0
  48. package/dist/types/protobuf.d.mts.map +1 -0
  49. package/dist/types/protobuf.mjs +1 -0
  50. package/dist/types/sendable.cjs +0 -0
  51. package/dist/types/sendable.d.cts +225 -0
  52. package/dist/types/sendable.d.cts.map +1 -0
  53. package/dist/types/sendable.d.mts +225 -0
  54. package/dist/types/sendable.d.mts.map +1 -0
  55. package/dist/types/sendable.mjs +1 -0
  56. package/dist/types/struct.cjs +0 -0
  57. package/dist/types/struct.d.cts +304 -0
  58. package/dist/types/struct.d.cts.map +1 -0
  59. package/dist/types/struct.d.mts +304 -0
  60. package/dist/types/struct.d.mts.map +1 -0
  61. package/dist/types/struct.mjs +1 -0
  62. package/dist/utils.cjs +140 -0
  63. package/dist/utils.d.cts +40 -0
  64. package/dist/utils.d.cts.map +1 -0
  65. package/dist/utils.d.mts +40 -0
  66. package/dist/utils.d.mts.map +1 -0
  67. package/dist/utils.mjs +135 -0
  68. package/dist/utils.mjs.map +1 -0
  69. package/package.json +51 -0
  70. package/src/abstractions.ts +308 -0
  71. package/src/formats/json.ts +53 -0
  72. package/src/formats/msgpack.ts +42 -0
  73. package/src/formats/protobuf.ts +213 -0
  74. package/src/formats/struct.test.ts +814 -0
  75. package/src/formats/struct.ts +992 -0
  76. package/src/sink.ts +611 -0
  77. package/src/types/protobuf.ts +334 -0
  78. package/src/types/sendable.ts +244 -0
  79. package/src/types/struct.ts +333 -0
  80. package/src/utils.ts +241 -0
@@ -0,0 +1,333 @@
1
+ /**
2
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/struct/VectorStruct.java
3
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/struct/VectorStruct.h
4
+ */
5
+ export interface VectorStruct {
6
+ readonly data: ReadonlyArray<number>;
7
+ }
8
+
9
+ /**
10
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/struct/MatrixStruct.java
11
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/struct/MatrixStruct.h
12
+ */
13
+ export interface MatrixStruct {
14
+ readonly data: ReadonlyArray<number>;
15
+ }
16
+
17
+ /**
18
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/geometry/struct/Translation2dStruct.java
19
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/geometry/struct/Translation2dStruct.h
20
+ */
21
+ export interface Translation2dStruct {
22
+ readonly x: number;
23
+ readonly y: number;
24
+ }
25
+
26
+ /**
27
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/geometry/struct/Translation3dStruct.java
28
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/geometry/struct/Translation3dStruct.h
29
+ */
30
+ export interface Translation3dStruct {
31
+ readonly x: number;
32
+ readonly y: number;
33
+ readonly z: number;
34
+ }
35
+
36
+ /**
37
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/geometry/struct/Twist2dStruct.java
38
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/geometry/struct/Twist2dStruct.h
39
+ */
40
+ export interface Twist2dStruct {
41
+ readonly dx: number;
42
+ readonly dy: number;
43
+ readonly dtheta: number;
44
+ }
45
+
46
+ /**
47
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/geometry/struct/Twist3dStruct.java
48
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/geometry/struct/Twist3dStruct.h
49
+ */
50
+ export interface Twist3dStruct {
51
+ readonly dx: number;
52
+ readonly dy: number;
53
+ readonly dz: number;
54
+ readonly rx: number;
55
+ readonly ry: number;
56
+ readonly rz: number;
57
+ }
58
+
59
+ /**
60
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/geometry/struct/QauternionStruct.java
61
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/geometry/struct/QuaternionStruct.h
62
+ */
63
+ export interface QuaternionStruct {
64
+ readonly w: number;
65
+ readonly x: number;
66
+ readonly y: number;
67
+ readonly z: number;
68
+ }
69
+
70
+ /**
71
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/geometry/struct/Rotation2dStruct.java
72
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/geometry/struct/Rotation2dStruct.h
73
+ */
74
+ export interface Rotation2dStruct {
75
+ readonly value: number;
76
+ }
77
+
78
+ /**
79
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/geometry/struct/Rotation3dStruct.java
80
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/geometry/struct/Rotation3dStruct.h
81
+ */
82
+ export interface Rotation3dStruct {
83
+ readonly q: QuaternionStruct;
84
+ }
85
+
86
+ /**
87
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/geometry/struct/Transform2dStruct.java
88
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/geometry/struct/Transform2dStruct.h
89
+ */
90
+ export interface Transform2dStruct {
91
+ readonly translation: Translation2dStruct;
92
+ readonly rotation: Rotation2dStruct;
93
+ }
94
+
95
+ /**
96
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/geometry/struct/Transform3dStruct.java
97
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/geometry/struct/Transform3dStruct.h
98
+ */
99
+ export interface Transform3dStruct {
100
+ readonly translation: Translation3dStruct;
101
+ readonly rotation: Rotation3dStruct;
102
+ }
103
+
104
+ /**
105
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/geometry/struct/Pose2dStruct.java
106
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/geometry/struct/Pose2dStruct.h
107
+ */
108
+ export interface Pose2dStruct {
109
+ readonly translation: Translation2dStruct;
110
+ readonly rotation: Rotation2dStruct;
111
+ }
112
+
113
+ /**
114
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/geometry/struct/Pose3dStruct.java
115
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/geometry/struct/Pose3dStruct.h
116
+ */
117
+ export interface Pose3dStruct {
118
+ readonly translation: Translation3dStruct;
119
+ readonly rotation: Rotation3dStruct;
120
+ }
121
+
122
+ /**
123
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/geometry/struct/Ellipse2dStruct.java
124
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/geometry/struct/Ellipse2dStruct.h
125
+ */
126
+ export interface Ellipse2dStruct {
127
+ readonly center: Pose2dStruct;
128
+ readonly xSemiAxis: number;
129
+ readonly ySemiAxis: number;
130
+ }
131
+
132
+ /**
133
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/geometry/struct/Rectangle2dStruct.java
134
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/geometry/struct/Rectangle2dStruct.h
135
+ */
136
+ export interface Rectangle2dStruct {
137
+ readonly center: Pose2dStruct;
138
+ readonly xWidth: number;
139
+ readonly yWidth: number;
140
+ }
141
+
142
+ /**
143
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/controller/struct/ArmFeedforwardStruct.java
144
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/controller/struct/ArmFeedforwardStruct.h
145
+ */
146
+ export interface ArmFeedforwardStruct {
147
+ readonly ks: number;
148
+ readonly kg: number;
149
+ readonly kv: number;
150
+ readonly ka: number;
151
+ readonly dt: number;
152
+ }
153
+
154
+ /**
155
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/controller/struct/DifferentialDriveFeedforwardStruct.java
156
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/controller/struct/DifferentialDriveFeedforwardStruct.h
157
+ */
158
+ export interface DifferentialDriveFeedforwardStruct {
159
+ readonly kVLinear: number;
160
+ readonly kALinear: number;
161
+ readonly kVAngular: number;
162
+ readonly kAAngular: number;
163
+ }
164
+
165
+ /**
166
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/controller/struct/DifferentialDriveWheelVoltagesStruct.java
167
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/controller/struct/DifferentialDriveWheelVoltagesStruct.h
168
+ */
169
+ export interface DifferentialDriveWheelVoltagesStruct {
170
+ readonly left: number;
171
+ readonly right: number;
172
+ }
173
+
174
+ /**
175
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/controller/struct/ElevatorFeedforwardStruct.java
176
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/controller/struct/ElevatorFeedforwardStruct.h
177
+ */
178
+ export interface ElevatorFeedforwardStruct {
179
+ readonly ks: number;
180
+ readonly kg: number;
181
+ readonly kv: number;
182
+ readonly ka: number;
183
+ readonly dt: number;
184
+ }
185
+
186
+ /**
187
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/controller/struct/SimpleMotorFeedforwardStruct.java
188
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/controller/struct/SimpleMotorFeedforwardStruct.h
189
+ */
190
+ export interface SimpleMotorFeedforwardStruct {
191
+ readonly ks: number;
192
+ readonly kv: number;
193
+ readonly ka: number;
194
+ readonly dt: number;
195
+ }
196
+
197
+ /**
198
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/kinematics/struct/ChassisSpeedsStruct.java
199
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/kinematics/struct/ChassisSpeedsStruct.h
200
+ */
201
+ export interface ChassisSpeedsStruct {
202
+ readonly vx: number;
203
+ readonly vy: number;
204
+ readonly omega: number;
205
+ }
206
+
207
+ /**
208
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/kinematics/struct/DifferentialDriveKinematicsStruct.java
209
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/kinematics/struct/DifferentialDriveKinematicsStruct.h
210
+ */
211
+ export interface DifferentialDriveKinematicsStruct {
212
+ readonly track_width: number;
213
+ }
214
+
215
+ /**
216
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/kinematics/struct/DifferentialDriveWheelPositionsStruct.java
217
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/kinematics/struct/DifferentialDriveWheelPositionsStruct.h
218
+ */
219
+ export interface DifferentialDriveWheelPositionsStruct {
220
+ readonly left: number;
221
+ readonly right: number;
222
+ }
223
+
224
+ /**
225
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/kinematics/struct/DifferentialDriveWheelSpeedsStruct.java
226
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/kinematics/struct/DifferentialDriveWheelSpeedsStruct.h
227
+ */
228
+ export interface DifferentialDriveWheelSpeedsStruct {
229
+ readonly left: number;
230
+ readonly right: number;
231
+ }
232
+
233
+ /**
234
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/kinematics/struct/MecanumDriveKinematicsStruct.java
235
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/kinematics/struct/MecanumDriveKinematicsStruct.h
236
+ */
237
+ export interface MecanumDriveKinematicsStruct {
238
+ readonly front_left: Translation2dStruct;
239
+ readonly front_right: Translation2dStruct;
240
+ readonly rear_left: Translation2dStruct;
241
+ readonly rear_right: Translation2dStruct;
242
+ }
243
+
244
+ /**
245
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/kinematics/struct/MecanumDriveMotorVoltagesStruct.java
246
+ */
247
+ export interface MecanumDriveMotorVoltagesStruct {
248
+ readonly front_left: number;
249
+ readonly front_right: number;
250
+ readonly rear_left: number;
251
+ readonly rear_right: number;
252
+ }
253
+
254
+ /**
255
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/kinematics/struct/MecanumDriveWheelPositionsStruct.java
256
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/kinematics/struct/MecanumDriveWheelPositionsStruct.h
257
+ */
258
+ export interface MecanumDriveWheelPositionsStruct {
259
+ readonly front_left: number;
260
+ readonly front_right: number;
261
+ readonly rear_left: number;
262
+ readonly rear_right: number;
263
+ }
264
+
265
+ /**
266
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/kinematics/struct/MecanumDriveWheelSpeedsStruct.java
267
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/kinematics/struct/MecanumDriveWheelSpeedsStruct.h
268
+ */
269
+ export interface MecanumDriveWheelSpeedsStruct {
270
+ readonly front_left: number;
271
+ readonly front_right: number;
272
+ readonly rear_left: number;
273
+ readonly rear_right: number;
274
+ }
275
+
276
+ /**
277
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/kinematics/struct/SwerveDriveKinematicsStruct.java
278
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/kinematics/struct/SwerveDriveKinematicsStruct.h
279
+ */
280
+ export interface SwerveDriveKinematicsStruct {
281
+ readonly modules: ReadonlyArray<Translation2dStruct>;
282
+ }
283
+
284
+ /**
285
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/kinematics/struct/SwerveModulePositionStruct.java
286
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/kinematics/struct/SwerveModulePositionStruct.h
287
+ */
288
+ export interface SwerveModulePositionStruct {
289
+ readonly distance: number;
290
+ readonly angle: Rotation2dStruct;
291
+ }
292
+
293
+ /**
294
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/kinematics/struct/SwerveModuleStateStruct.java
295
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/kinematics/struct/SwerveModuleStateStruct.h
296
+ */
297
+ export interface SwerveModuleStateStruct {
298
+ readonly speed: number;
299
+ readonly angle: Rotation2dStruct;
300
+ }
301
+
302
+ /**
303
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/spline/struct/CubicHermiteSplineStruct.java
304
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/spline/struct/CubicHermiteSplineStruct.h
305
+ */
306
+ export interface CubicHermiteSplineStruct {
307
+ readonly xInitial: readonly [number, number];
308
+ readonly xFinal: readonly [number, number];
309
+ readonly yInitial: readonly [number, number];
310
+ readonly yFinal: readonly [number, number];
311
+ }
312
+
313
+ /**
314
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/spline/struct/QuinticHermiteSplineStruct.java
315
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/spline/struct/QuinticHermiteSplineStruct.h
316
+ */
317
+ export interface QuinticHermiteSplineStruct {
318
+ readonly xInitial: readonly [number, number, number];
319
+ readonly xFinal: readonly [number, number, number];
320
+ readonly yInitial: readonly [number, number, number];
321
+ readonly yFinal: readonly [number, number, number];
322
+ }
323
+
324
+ /**
325
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/java/edu/wpi/first/math/system/struct/LinearSystemStruct.java
326
+ * @see https://github.com/wpilibsuite/allwpilib/blob/main/wpimath/src/main/native/include/frc/system/struct/LinearSystemStruct.h
327
+ */
328
+ export interface LinearSystemStruct {
329
+ readonly a: MatrixStruct;
330
+ readonly b: MatrixStruct;
331
+ readonly c: MatrixStruct;
332
+ readonly d: MatrixStruct;
333
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,241 @@
1
+ import type { DataChannelRecord, DataTypeImpl } from "./abstractions";
2
+
3
+ /** Constructs the {@link DataView} from the buffer source. */
4
+ export function toDataView(source: unknown) {
5
+ if (source instanceof DataView) {
6
+ return source;
7
+ }
8
+
9
+ if (source instanceof ArrayBuffer) {
10
+ return new DataView(source);
11
+ }
12
+
13
+ if (ArrayBuffer.isView(source)) {
14
+ return new DataView(source.buffer, source.byteOffset, source.byteLength);
15
+ }
16
+
17
+ throw new Error(`Parameter of type '${typeof source}' cannot be wrapped into DataView`);
18
+ }
19
+
20
+ /** Returns the specified buffer source as {@link Uint8Array} view. */
21
+ export function toUint8Array(source: unknown) {
22
+ if (source instanceof ArrayBuffer) {
23
+ return new Uint8Array(source);
24
+ }
25
+
26
+ if (ArrayBuffer.isView(source)) {
27
+ return source instanceof Uint8Array ? source : new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
28
+ }
29
+
30
+ throw new Error(`Parameter of type '${typeof source}' cannot be represented as Uint8Array`);
31
+ }
32
+
33
+ /**
34
+ * Adds timestamped record to an array of records.
35
+ *
36
+ * The `records` array is assumed to be sorted by the timestamp in ascending order,
37
+ * earlier records have smaller indices.
38
+ *
39
+ * @returns the index at which the record has been inserted or updated
40
+ */
41
+ export function addTimestampedRecord<T extends DataTypeImpl>(
42
+ records: Array<DataChannelRecord<T>>,
43
+ timestamp: number,
44
+ value: T
45
+ ): number {
46
+ if (records.length === 0) {
47
+ records.push({
48
+ timestamp,
49
+ value,
50
+ });
51
+
52
+ return 0;
53
+ }
54
+
55
+ // records must be sorted by the timestamp in ascending order
56
+ // timestamps of 0 and 1 are special, representing "weak" (set default)
57
+ // and "strong" (set while disconnected) timestamps; otherwise we
58
+ // expect timestamps to be generally newer than existing data
59
+
60
+ const tail = records[0]!;
61
+ let index = 0;
62
+
63
+ if (timestamp === 0) {
64
+ // replace value at timestamp zero
65
+ if (tail.timestamp === 0) {
66
+ tail.value = value;
67
+ } else {
68
+ records.unshift({
69
+ timestamp,
70
+ value,
71
+ });
72
+ }
73
+ } else if (timestamp === 1) {
74
+ if (tail.timestamp === 1) {
75
+ tail.value = value;
76
+ } else if (tail.timestamp === 0) {
77
+ index = 1;
78
+ if (records.length === 1) {
79
+ records.push({
80
+ timestamp,
81
+ value,
82
+ });
83
+ } else if (records[1]!.timestamp === 1) {
84
+ records[1]!.value = value;
85
+ } else {
86
+ records.splice(1, 0, {
87
+ timestamp,
88
+ value,
89
+ });
90
+ }
91
+ } else {
92
+ records.unshift({
93
+ timestamp,
94
+ value,
95
+ });
96
+ }
97
+ } else {
98
+ for (let i = records.length - 1; i >= 0; --i) {
99
+ const record = records[i]!;
100
+ if (record.timestamp === timestamp) {
101
+ index = i;
102
+ record.value = value;
103
+ return index;
104
+ }
105
+ if (record.timestamp < timestamp) {
106
+ index = i + 1;
107
+ records.splice(i + 1, 0, {
108
+ timestamp,
109
+ value,
110
+ });
111
+ return index;
112
+ }
113
+ }
114
+
115
+ records.unshift({
116
+ timestamp,
117
+ value,
118
+ });
119
+ }
120
+
121
+ return index;
122
+ }
123
+
124
+ /**
125
+ * Gets nearest (latest) timestamped record and its index.
126
+ *
127
+ * The `records` array is assumed to be sorted by the timestamp in ascending order,
128
+ * earlier records have smaller indices.
129
+ */
130
+ export function getTimestampedRecord<T extends DataTypeImpl>(
131
+ records: Array<DataChannelRecord<T>>,
132
+ timestamp: number
133
+ ): [DataChannelRecord<T>, number] | [undefined, -1] {
134
+ if (records.length === 0) {
135
+ return [undefined, -1];
136
+ }
137
+
138
+ // timestamps of 0 and 1 are special
139
+ if (timestamp === 0) {
140
+ return records[0]!.timestamp === 0 ? [records[0]!, 0] : [undefined, -1];
141
+ }
142
+
143
+ if (timestamp === 1) {
144
+ if (records[0]!.timestamp === 1) {
145
+ return [records[0]!, 0];
146
+ }
147
+
148
+ if (records.length > 1 && records[1]!.timestamp === 1) {
149
+ return [records[1]!, 1];
150
+ }
151
+
152
+ return [undefined, -1];
153
+ }
154
+
155
+ for (let i = records.length - 1; i >= 0; --i) {
156
+ const record = records[i]!;
157
+ if (record.timestamp <= timestamp) {
158
+ return [record, i];
159
+ }
160
+ }
161
+
162
+ return [undefined, -1];
163
+ }
164
+
165
+ /**
166
+ * Reduces timestamps records array by pruning records with older timestamps
167
+ * per `maxSize` and `cutoff` window retention settings.
168
+ *
169
+ * Always retains special records with timestamps 0 and 1, these do not count
170
+ * towards maximum size limit.
171
+ *
172
+ * @param records array of records sorted by the timestamp in ascending order
173
+ * @param maxSize maximum number of entries to retain
174
+ * @param cutoff cutoff timestamp
175
+ */
176
+ export function pruneTimestampedRecords(
177
+ records: Array<DataChannelRecord<DataTypeImpl>>,
178
+ maxSize?: number,
179
+ cutoff?: number
180
+ ) {
181
+ // shortcuts
182
+ if (cutoff == null && (maxSize == null || records.length <= maxSize)) {
183
+ return records;
184
+ }
185
+
186
+ // skip special records and find the oldest timestamp
187
+ const tailIndex = records.findIndex((_) => _.timestamp > 1);
188
+ if (tailIndex < 0) {
189
+ return records;
190
+ }
191
+
192
+ // apply maximum size policy first
193
+ if (maxSize != null && records.length - tailIndex - maxSize > 0) {
194
+ records.splice(tailIndex, records.length - tailIndex - maxSize);
195
+ }
196
+
197
+ // apply time window policy
198
+ if (cutoff != null) {
199
+ const oldest = records[tailIndex]!;
200
+ if (oldest.timestamp >= cutoff) {
201
+ return records;
202
+ }
203
+
204
+ let cutoffIndex = tailIndex;
205
+ // do not delete last record even if it is outside of the window
206
+ for (; cutoffIndex < records.length - 1; ++cutoffIndex) {
207
+ if (records[cutoffIndex]!.timestamp >= cutoff) {
208
+ break;
209
+ }
210
+ }
211
+
212
+ if (cutoffIndex > tailIndex) {
213
+ records.splice(tailIndex, cutoffIndex - tailIndex);
214
+ }
215
+ }
216
+
217
+ return records;
218
+ }
219
+
220
+ /** Sets deeply nested object value. */
221
+ export function setValueByPath(
222
+ object: Record<string, unknown>,
223
+ path: Array<string>,
224
+ value: unknown,
225
+ insertOnly = false
226
+ ) {
227
+ path.reduce((o, key, i) => {
228
+ if (i === path.length - 1) {
229
+ if (!insertOnly || o[key] === undefined) {
230
+ o[key] = value;
231
+ }
232
+ } else if (o[key] === undefined) {
233
+ o[key] = {};
234
+ }
235
+
236
+ return o[key];
237
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
238
+ }, object as any);
239
+
240
+ return object;
241
+ }