wpilib-protobuf 0.0.0-stage → 0.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
@@ -1,3 +1,35 @@
1
- # Temporary Holding Version
1
+ # wpilib-protobuf
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Decode WPILib protobuf values in TypeScript & JavaScript, from the schemas WPILib logs and publishes to NetworkTables.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install wpilib-protobuf
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ WPILib sends a schema for every `.proto` file a protobuf value uses, as `/.schema/proto:<file>` entries holding a serialized `google.protobuf.FileDescriptorProto`.
14
+ Register each one, then decode values by the message name from their type, ex. `wpi.proto.ProtobufPose2d` for a `proto:wpi.proto.ProtobufPose2d` entry:
15
+
16
+ ```ts
17
+ import { ProtobufRegistry } from 'wpilib-protobuf';
18
+
19
+ const registry = new ProtobufRegistry();
20
+ registry.register(geometry2dSchema);
21
+
22
+ console.log(registry.decode('wpi.proto.ProtobufPose2d', bytes));
23
+ // { translation: { x: 1.5, y: -2 }, rotation: { value: 3 } }
24
+ ```
25
+
26
+ `decode()` returns a plain object with fields named as declared in the schema. WPILib's messages use the same field names as its structs.
27
+
28
+ - Fields without presence tracking, like most proto3 scalars, always have a value, their default when not set.
29
+ - Unset message fields, `optional` fields, and oneof members are left out.
30
+ - 64-bit integers are `bigint` values, enums are numbers, repeated fields are arrays, and maps are objects.
31
+
32
+ Schemas can be registered in any order. If a file a message uses isn't registered yet, decoding returns the message name instead of a decoded value, and `has()` returns `false`.
33
+ `register()` and `decode()` throw for invalid schemas and messages.
34
+
35
+ A summary of the full API is available in [wpilib-protobuf.api.md](https://github.com/jonahsnider/wpilog-parser/blob/main/packages/wpilib-protobuf/etc/wpilib-protobuf.api.md).
@@ -0,0 +1,32 @@
1
+ /** A decoded protobuf field value. 64-bit integers are `bigint`s, enums are numbers, and maps are objects. */
2
+ type ProtobufValue = number | bigint | boolean | string | Uint8Array | ProtobufPayload | ProtobufValue[];
3
+ /** A decoded protobuf message. Fields, including nested messages, are own properties of a plain object named as declared in the schema. */
4
+ type ProtobufPayload = {
5
+ [field: string]: ProtobufValue | undefined;
6
+ };
7
+ /**
8
+ * Decodes protobuf messages from the file descriptors that describe them, like the `/.schema/proto:*` entries WPILib
9
+ * logs and publishes to NetworkTables.
10
+ */
11
+ export declare class ProtobufRegistry {
12
+ private readonly files;
13
+ private readonly waiting;
14
+ private readonly messages;
15
+ /**
16
+ * Register a serialized `google.protobuf.FileDescriptorProto`. Files can be registered in any order, a file's
17
+ * messages can be decoded once every file it imports is registered. Registering a file again is ignored.
18
+ * Throws if the descriptor is invalid.
19
+ */
20
+ register(descriptor: Uint8Array): void;
21
+ /** Whether messages of a type can be decoded, ex. `wpi.proto.ProtobufPose2d` */
22
+ has(messageName: string): boolean;
23
+ /**
24
+ * Decode a message of a type, ex. `wpi.proto.ProtobufPose2d`. Returns the type's name instead when its file, or a
25
+ * file it imports, isn't registered yet. Throws if the bytes aren't a valid message of that type.
26
+ *
27
+ * Fields without presence tracking, like most proto3 scalars, always have a value, their default when not set.
28
+ * Unset message fields, `optional` fields, and oneof members are left out.
29
+ */
30
+ decode(messageName: string, bytes: Uint8Array): ProtobufPayload | string;
31
+ }
32
+ export type { ProtobufPayload, ProtobufValue };
package/dist/index.mjs ADDED
@@ -0,0 +1,93 @@
1
+ import { createFileRegistry, fromBinary } from "@bufbuild/protobuf";
2
+ import { nestedTypes, reflect } from "@bufbuild/protobuf/reflect";
3
+ import { FeatureSet_FieldPresence, FileDescriptorProtoSchema } from "@bufbuild/protobuf/wkt";
4
+ // src/protobuf-registry.ts
5
+ /**
6
+ * Decodes protobuf messages from the file descriptors that describe them, like the `/.schema/proto:*` entries WPILib
7
+ * logs and publishes to NetworkTables.
8
+ */
9
+ var ProtobufRegistry = class {
10
+ files = /* @__PURE__ */ new Map();
11
+ waiting = /* @__PURE__ */ new Map();
12
+ messages = /* @__PURE__ */ new Map();
13
+ /**
14
+ * Register a serialized `google.protobuf.FileDescriptorProto`. Files can be registered in any order, a file's
15
+ * messages can be decoded once every file it imports is registered. Registering a file again is ignored.
16
+ * Throws if the descriptor is invalid.
17
+ */
18
+ register(descriptor) {
19
+ const file = fromBinary(FileDescriptorProtoSchema, descriptor);
20
+ if (this.files.has(file.name) || this.waiting.has(file.name)) return;
21
+ this.waiting.set(file.name, file);
22
+ let added = true;
23
+ while (added) {
24
+ added = false;
25
+ for (const [name, proto] of this.waiting) {
26
+ if (!proto.dependency.every((dependency) => this.files.has(dependency))) continue;
27
+ this.waiting.delete(name);
28
+ const file = createFileRegistry(proto, (dependency) => this.files.get(dependency)).getFile(name);
29
+ this.files.set(name, file);
30
+ for (const type of nestedTypes(file)) if (type.kind === "message") this.messages.set(type.typeName, type);
31
+ added = true;
32
+ }
33
+ }
34
+ }
35
+ /** Whether messages of a type can be decoded, ex. `wpi.proto.ProtobufPose2d` */
36
+ has(messageName) {
37
+ return this.messages.has(messageName);
38
+ }
39
+ /**
40
+ * Decode a message of a type, ex. `wpi.proto.ProtobufPose2d`. Returns the type's name instead when its file, or a
41
+ * file it imports, isn't registered yet. Throws if the bytes aren't a valid message of that type.
42
+ *
43
+ * Fields without presence tracking, like most proto3 scalars, always have a value, their default when not set.
44
+ * Unset message fields, `optional` fields, and oneof members are left out.
45
+ */
46
+ decode(messageName, bytes) {
47
+ const desc = this.messages.get(messageName);
48
+ if (desc === void 0) return messageName;
49
+ return toPayload(reflect(desc, fromBinary(desc, bytes)));
50
+ }
51
+ };
52
+ function toPayload(message) {
53
+ const payload = {};
54
+ for (const field of message.fields) {
55
+ let value;
56
+ switch (field.fieldKind) {
57
+ case "scalar":
58
+ case "enum":
59
+ if (field.presence !== FeatureSet_FieldPresence.IMPLICIT && !message.isSet(field)) continue;
60
+ value = message.get(field);
61
+ break;
62
+ case "message":
63
+ if (!message.isSet(field)) continue;
64
+ value = toPayload(message.get(field));
65
+ break;
66
+ case "list": {
67
+ const list = message.get(field);
68
+ value = [];
69
+ for (const item of list) value.push(field.listKind === "message" ? toPayload(item) : item);
70
+ break;
71
+ }
72
+ case "map": {
73
+ const map = {};
74
+ for (const [key, item] of message.get(field)) setField(map, String(key), field.mapKind === "message" ? toPayload(item) : item);
75
+ value = map;
76
+ break;
77
+ }
78
+ }
79
+ setField(payload, field.name, value);
80
+ }
81
+ return payload;
82
+ }
83
+ /** Set an own property, including for keys like `__proto__` that assignment would treat as the prototype */
84
+ function setField(payload, key, value) {
85
+ if (key === "__proto__") Object.defineProperty(payload, key, {
86
+ value,
87
+ enumerable: true,
88
+ writable: true,
89
+ configurable: true
90
+ });
91
+ else payload[key] = value;
92
+ }
93
+ export { ProtobufRegistry };
@@ -0,0 +1,11 @@
1
+ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
+ // It should be published with your NPM package. It should not be tracked by Git.
3
+ {
4
+ "tsdocVersion": "0.12",
5
+ "toolPackages": [
6
+ {
7
+ "packageName": "@microsoft/api-extractor",
8
+ "packageVersion": "7.60.1"
9
+ }
10
+ ]
11
+ }
package/package.json CHANGED
@@ -1,6 +1,45 @@
1
1
  {
2
2
  "name": "wpilib-protobuf",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.0.0",
4
+ "description": "Decode WPILib protobuf values from their schemas",
5
+ "homepage": "https://github.com/jonahsnider/wpilog-parser/tree/main/packages/wpilib-protobuf#readme",
6
+ "license": "MIT",
7
+ "author": {
8
+ "name": "Jonah Snider",
9
+ "email": "jonah@jonahsnider.com",
10
+ "url": "https://jonahsnider.com"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/jonahsnider/wpilog-parser.git",
15
+ "directory": "packages/wpilib-protobuf"
16
+ },
17
+ "files": [
18
+ "dist"
19
+ ],
20
+ "type": "module",
21
+ "exports": {
22
+ ".": "./dist/index.mjs",
23
+ "./package.json": "./package.json"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "dependencies": {
29
+ "@bufbuild/protobuf": "^2.17.0"
30
+ },
31
+ "devDependencies": {
32
+ "@microsoft/api-extractor": "7.60.1",
33
+ "vite-plus": "1.8.0",
34
+ "vitest": "5.1.1"
35
+ },
36
+ "tsdocMetadata": "./dist/tsdoc-metadata.json",
37
+ "scripts": {
38
+ "api:check": "api-extractor run",
39
+ "api:update": "api-extractor run --local",
40
+ "bench": "vp test bench",
41
+ "build": "vp pack",
42
+ "dev": "vp pack --watch",
43
+ "test": "vp test run"
44
+ }
6
45
  }