diff --git a/packages/omgidl-serialization/package.json b/packages/omgidl-serialization/package.json index 941b543..1f7f8ce 100644 --- a/packages/omgidl-serialization/package.json +++ b/packages/omgidl-serialization/package.json @@ -48,7 +48,7 @@ "typescript": "4.9.5" }, "dependencies": { - "@foxglove/cdr": "2.1.0", + "@foxglove/cdr": "3.0.0", "@foxglove/message-definition": "^0.2.0" } } diff --git a/packages/omgidl-serialization/src/MessageReader.ts b/packages/omgidl-serialization/src/MessageReader.ts index 4c04e17..7a4e993 100644 --- a/packages/omgidl-serialization/src/MessageReader.ts +++ b/packages/omgidl-serialization/src/MessageReader.ts @@ -6,8 +6,13 @@ import { export type Deserializer = ( reader: CdrReader, + /** + * Optional length only applied for string types as character length. + * Prevents reader from reading sequence length again if already read via header. + */ length?: number, ) => boolean | number | bigint | string; + export type ArrayDeserializer = ( reader: CdrReader, count: number, @@ -93,7 +98,7 @@ export class MessageReader { let fieldLength = field.arrayLength; if (readMemberHeader) { const { id, objectSize: objectSizeBytes } = reader.emHeader(); - const itemSize = typeToByteLength[field.type]; + const itemSize = typeToByteLength(field.type); if (itemSize != undefined) { fieldLength ??= Math.ceil(objectSizeBytes / itemSize); } @@ -147,20 +152,28 @@ export class MessageReader { } } -const typeToByteLength: Record = { - bool: 1, - int8: 1, - uint8: 1, - int16: 2, - uint16: 2, - int32: 4, - uint32: 4, - int64: 8, - uint64: 8, - float32: 4, - float64: 8, - string: 1, -}; +function typeToByteLength(type: string): number | undefined { + switch (type) { + case "bool": + case "int8": + case "uint8": + case "string": + return 1; + case "int16": + case "uint16": + return 2; + case "int32": + case "uint32": + case "float32": + return 4; + case "int64": + case "uint64": + case "float64": + return 8; + default: + return undefined; + } +} const deserializers = new Map([ ["bool", (reader) => Boolean(reader.int8())], diff --git a/yarn.lock b/yarn.lock index b78c056..82974a7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -452,10 +452,10 @@ __metadata: languageName: node linkType: hard -"@foxglove/cdr@npm:2.1.0": - version: 2.1.0 - resolution: "@foxglove/cdr@npm:2.1.0" - checksum: 153632a8911a76d047d896f0a17e12b5a6779465a35790c2fe349efc0eb8a618c877d015a066d8b13a804f0c72131a7e9ca0bf48ed116aa3095a3d7252ab1062 +"@foxglove/cdr@npm:3.0.0": + version: 3.0.0 + resolution: "@foxglove/cdr@npm:3.0.0" + checksum: 3ad9b7f92b842aaf8e6f968d0deb060e8cd5b63ee536fce34563bca66d4cb8f488cb03d3b77481a68bbbcbd5fb11e49181575585644046f38401a678218fd589 languageName: node linkType: hard @@ -534,7 +534,7 @@ __metadata: version: 0.0.0-use.local resolution: "@foxglove/omgidl-serialization@workspace:packages/omgidl-serialization" dependencies: - "@foxglove/cdr": 2.1.0 + "@foxglove/cdr": 3.0.0 "@foxglove/message-definition": ^0.2.0 "@foxglove/omgidl-parser": "workspace:*" "@sounisi5011/jest-binary-data-matchers": 1.2.1