@foxglove/cdr
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -5,6 +5,6 @@ import { EncapsulationKind } from "./encapsulationKind"; | ||
private view; | ||
private offset; | ||
private littleEndian; | ||
private hostLittleEndian; | ||
private textDecoder; | ||
offset: number; | ||
get kind(): EncapsulationKind; | ||
@@ -11,0 +11,0 @@ get data(): Uint8Array; |
@@ -190,2 +190,5 @@ "use strict"; | ||
typedArray(TypedArrayConstructor, getter, count) { | ||
if (count === 0) { | ||
return new TypedArrayConstructor(); | ||
} | ||
this.align(TypedArrayConstructor.BYTES_PER_ELEMENT); | ||
@@ -192,0 +195,0 @@ const totalOffset = this.data.byteOffset + this.offset; |
@@ -8,3 +8,3 @@ "use strict"; | ||
describe("CdrReader", () => { | ||
it("parses an example message", () => { | ||
it("parses an example tf2_msgs/TFMessage message", () => { | ||
const data = Uint8Array.from(Buffer.from(tf2_msg__TFMessage, "hex")); | ||
@@ -30,3 +30,32 @@ const reader = new CdrReader_1.CdrReader(data); | ||
expect(reader.float64()).toBeCloseTo(1); // float64 w | ||
expect(reader.offset).toBe(data.length); | ||
}); | ||
it("parses an example rcl_interfaces/ParameterEvent", () => { | ||
const data = Uint8Array.from(Buffer.from("00010000a9b71561a570ea01110000002f5f726f7332636c695f33373833363300000000010000000d0000007573655f73696d5f74696d650001000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000", "hex")); | ||
const reader = new CdrReader_1.CdrReader(data); | ||
// builtin_interfaces/Time stamp | ||
expect(reader.uint32()).toEqual(1628813225); // uint32 sec | ||
expect(reader.uint32()).toEqual(32141477); // uint32 nsec | ||
// string node | ||
expect(reader.string()).toEqual("/_ros2cli_378363"); | ||
// Parameter[] new_parameters | ||
expect(reader.sequenceLength()).toEqual(1); | ||
expect(reader.string()).toEqual("use_sim_time"); // string name | ||
// ParameterValue value | ||
expect(reader.uint8()).toEqual(1); // uint8 type | ||
expect(reader.int8()).toEqual(0); // bool bool_value | ||
expect(reader.int64()).toEqual(0n); // int64 integer_value | ||
expect(reader.float64()).toEqual(0); // float64 double_value | ||
expect(reader.string()).toEqual(""); // string string_value | ||
expect(reader.int8Array()).toEqual(new Int8Array()); // byte[] byte_array_value | ||
expect(reader.uint8Array()).toEqual(new Uint8Array()); // bool[] bool_array_value | ||
expect(reader.int64Array()).toEqual(new BigInt64Array()); // int64[] integer_array_value | ||
expect(reader.float64Array()).toEqual(new Float64Array()); // float64[] double_array_value | ||
expect(reader.stringArray()).toEqual([]); // string[] string_array_value | ||
// Parameter[] changed_parameters | ||
expect(reader.sequenceLength()).toEqual(0); | ||
// Parameter[] deleted_parameters | ||
expect(reader.sequenceLength()).toEqual(0); | ||
expect(reader.offset).toBe(data.length); | ||
}); | ||
it("reads big endian values", () => { | ||
@@ -94,2 +123,3 @@ const data = Uint8Array.from(Buffer.from("000100001234000056789abcdef0000000000000", "hex")); | ||
expectToBeCloseToArray(Array.from(reader.float32Array().values()), [7.5, 8.5], 6); | ||
expect(reader.offset).toBe(writer.data.length); | ||
}); | ||
@@ -104,3 +134,23 @@ it("reads stringArray", () => { | ||
expect(reader.stringArray(reader.sequenceLength())).toEqual(["abc", "", "test string"]); | ||
expect(reader.offset).toBe(writer.data.length); | ||
}); | ||
it.each([ | ||
"int8Array", | ||
"uint8Array", | ||
"int16Array", | ||
"uint16Array", | ||
"int32Array", | ||
"uint32Array", | ||
"int64Array", | ||
"uint64Array", | ||
"float32Array", | ||
"float64Array", | ||
])("handles alignment correctly for empty %s", (key) => { | ||
const writer = new CdrWriter_1.CdrWriter(); | ||
writer[key]([], true); | ||
expect(writer.data.length).toBe(8); | ||
const reader = new CdrReader_1.CdrReader(writer.data); | ||
expect(reader[key]().length).toEqual(0); | ||
expect(reader.offset).toEqual(writer.data.length); | ||
}); | ||
}); | ||
@@ -107,0 +157,0 @@ function writeArray(writer, setter, array) { |
{ | ||
"name": "@foxglove/cdr", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Common Data Representation serialization and deserialization library", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -73,1 +73,5 @@ # @foxglove/cdr | ||
3. GitHub Actions will take care of the rest | ||
## Stay in touch | ||
Join our [Slack channel](https://foxglove.dev/join-slack) to ask questions, share feedback, and stay up to date on what our team is working on. |
@@ -20,3 +20,3 @@ import { CdrReader } from "./CdrReader"; | ||
describe("CdrReader", () => { | ||
it("parses an example message", () => { | ||
it("parses an example tf2_msgs/TFMessage message", () => { | ||
const data = Uint8Array.from(Buffer.from(tf2_msg__TFMessage, "hex")); | ||
@@ -43,4 +43,46 @@ const reader = new CdrReader(data); | ||
expect(reader.float64()).toBeCloseTo(1); // float64 w | ||
expect(reader.offset).toBe(data.length); | ||
}); | ||
it("parses an example rcl_interfaces/ParameterEvent", () => { | ||
const data = Uint8Array.from( | ||
Buffer.from( | ||
"00010000a9b71561a570ea01110000002f5f726f7332636c695f33373833363300000000010000000d0000007573655f73696d5f74696d650001000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000", | ||
"hex", | ||
), | ||
); | ||
const reader = new CdrReader(data); | ||
// builtin_interfaces/Time stamp | ||
expect(reader.uint32()).toEqual(1628813225); // uint32 sec | ||
expect(reader.uint32()).toEqual(32141477); // uint32 nsec | ||
// string node | ||
expect(reader.string()).toEqual("/_ros2cli_378363"); | ||
// Parameter[] new_parameters | ||
expect(reader.sequenceLength()).toEqual(1); | ||
expect(reader.string()).toEqual("use_sim_time"); // string name | ||
// ParameterValue value | ||
expect(reader.uint8()).toEqual(1); // uint8 type | ||
expect(reader.int8()).toEqual(0); // bool bool_value | ||
expect(reader.int64()).toEqual(0n); // int64 integer_value | ||
expect(reader.float64()).toEqual(0); // float64 double_value | ||
expect(reader.string()).toEqual(""); // string string_value | ||
expect(reader.int8Array()).toEqual(new Int8Array()); // byte[] byte_array_value | ||
expect(reader.uint8Array()).toEqual(new Uint8Array()); // bool[] bool_array_value | ||
expect(reader.int64Array()).toEqual(new BigInt64Array()); // int64[] integer_array_value | ||
expect(reader.float64Array()).toEqual(new Float64Array()); // float64[] double_array_value | ||
expect(reader.stringArray()).toEqual([]); // string[] string_array_value | ||
// Parameter[] changed_parameters | ||
expect(reader.sequenceLength()).toEqual(0); | ||
// Parameter[] deleted_parameters | ||
expect(reader.sequenceLength()).toEqual(0); | ||
expect(reader.offset).toBe(data.length); | ||
}); | ||
it("reads big endian values", () => { | ||
@@ -120,2 +162,3 @@ const data = Uint8Array.from(Buffer.from("000100001234000056789abcdef0000000000000", "hex")); | ||
expectToBeCloseToArray(Array.from(reader.float32Array().values()), [7.5, 8.5], 6); | ||
expect(reader.offset).toBe(writer.data.length); | ||
}); | ||
@@ -132,3 +175,25 @@ | ||
expect(reader.stringArray(reader.sequenceLength())).toEqual(["abc", "", "test string"]); | ||
expect(reader.offset).toBe(writer.data.length); | ||
}); | ||
it.each([ | ||
"int8Array", | ||
"uint8Array", | ||
"int16Array", | ||
"uint16Array", | ||
"int32Array", | ||
"uint32Array", | ||
"int64Array", | ||
"uint64Array", | ||
"float32Array", | ||
"float64Array", | ||
] as const)("handles alignment correctly for empty %s", (key) => { | ||
const writer = new CdrWriter(); | ||
writer[key]([], true); | ||
expect(writer.data.length).toBe(8); | ||
const reader = new CdrReader(writer.data); | ||
expect(reader[key]().length).toEqual(0); | ||
expect(reader.offset).toEqual(writer.data.length); | ||
}); | ||
}); | ||
@@ -135,0 +200,0 @@ |
@@ -29,3 +29,2 @@ import { EncapsulationKind } from "./encapsulationKind"; | ||
private view: DataView; | ||
private offset: number; | ||
private littleEndian: boolean; | ||
@@ -35,2 +34,4 @@ private hostLittleEndian: boolean; | ||
offset: number; | ||
get kind(): EncapsulationKind { | ||
@@ -258,2 +259,5 @@ return this.array[1] as EncapsulationKind; | ||
) { | ||
if (count === 0) { | ||
return new TypedArrayConstructor(); | ||
} | ||
this.align(TypedArrayConstructor.BYTES_PER_ELEMENT); | ||
@@ -260,0 +264,0 @@ const totalOffset = this.data.byteOffset + this.offset; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
135568
2223
77
1