@foxglove/cdr
Advanced tools
Comparing version 1.1.1 to 1.2.0
import { EncapsulationKind } from "./encapsulationKind"; | ||
export declare class CdrReader { | ||
private array; | ||
private view; | ||
@@ -10,5 +9,4 @@ private littleEndian; | ||
get kind(): EncapsulationKind; | ||
get data(): Uint8Array; | ||
get decodedBytes(): number; | ||
constructor(data: Uint8Array); | ||
constructor(data: ArrayBufferView); | ||
int8(): number; | ||
@@ -15,0 +13,0 @@ uint8(): number; |
@@ -9,7 +9,6 @@ "use strict"; | ||
this.textDecoder = new TextDecoder("utf8"); | ||
this.hostLittleEndian = !isBigEndian_1.isBigEndian(); | ||
this.hostLittleEndian = !(0, isBigEndian_1.isBigEndian)(); | ||
if (data.byteLength < 4) { | ||
throw new Error(`Invalid CDR data size ${data.byteLength}, must contain at least a 4-byte header`); | ||
} | ||
this.array = data; | ||
this.view = new DataView(data.buffer, data.byteOffset, data.byteLength); | ||
@@ -21,7 +20,4 @@ const kind = this.kind; | ||
get kind() { | ||
return this.array[1]; | ||
return this.view.getUint8(1); | ||
} | ||
get data() { | ||
return this.array; | ||
} | ||
get decodedBytes() { | ||
@@ -112,3 +108,3 @@ return this.offset; | ||
} | ||
const data = new Uint8Array(this.array.buffer, this.array.byteOffset + this.offset, length - 1); | ||
const data = new Uint8Array(this.view.buffer, this.view.byteOffset + this.offset, length - 1); | ||
const value = this.textDecoder.decode(data); | ||
@@ -122,3 +118,3 @@ this.offset += length; | ||
int8Array(count = this.sequenceLength()) { | ||
const array = new Int8Array(this.data.buffer, this.data.byteOffset + this.offset, count); | ||
const array = new Int8Array(this.view.buffer, this.view.byteOffset + this.offset, count); | ||
this.offset += count; | ||
@@ -128,3 +124,3 @@ return array; | ||
uint8Array(count = this.sequenceLength()) { | ||
const array = new Uint8Array(this.data.buffer, this.data.byteOffset + this.offset, count); | ||
const array = new Uint8Array(this.view.buffer, this.view.byteOffset + this.offset, count); | ||
this.offset += count; | ||
@@ -171,3 +167,3 @@ return array; | ||
const newOffset = this.offset + relativeOffset; | ||
if (newOffset < 4 || newOffset >= this.data.byteLength) { | ||
if (newOffset < 4 || newOffset >= this.view.byteLength) { | ||
throw new Error(`seek(${relativeOffset}) failed, ${newOffset} is outside the data range`); | ||
@@ -183,3 +179,3 @@ } | ||
seekTo(offset) { | ||
if (offset < 4 || offset >= this.data.byteLength) { | ||
if (offset < 4 || offset >= this.view.byteLength) { | ||
throw new Error(`seekTo(${offset}) failed, value is outside the data range`); | ||
@@ -201,3 +197,3 @@ } | ||
this.align(TypedArrayConstructor.BYTES_PER_ELEMENT); | ||
const totalOffset = this.data.byteOffset + this.offset; | ||
const totalOffset = this.view.byteOffset + this.offset; | ||
if (this.littleEndian !== this.hostLittleEndian) { | ||
@@ -209,3 +205,3 @@ // Slowest path | ||
// Fastest path | ||
const array = new TypedArrayConstructor(this.data.buffer, totalOffset, count); | ||
const array = new TypedArrayConstructor(this.view.buffer, totalOffset, count); | ||
this.offset += TypedArrayConstructor.BYTES_PER_ELEMENT * count; | ||
@@ -212,0 +208,0 @@ return array; |
@@ -85,3 +85,3 @@ "use strict"; | ||
["uint32Array", "uint32", [0, 4294967295, 3]], | ||
])("reads %s", (getter, setter, expected) => { | ||
])("reads int %s", (getter, setter, expected) => { | ||
const writer = new CdrWriter_1.CdrWriter(); | ||
@@ -97,3 +97,3 @@ writeArray(writer, setter, expected); | ||
["float64Array", "float64", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -0.123456789121212121212], 15], | ||
])("reads %s", (getter, setter, expected, numDigits) => { | ||
])("reads float %s", (getter, setter, expected, numDigits) => { | ||
const writer = new CdrWriter_1.CdrWriter(); | ||
@@ -121,2 +121,3 @@ writeArray(writer, setter, expected); | ||
const reader = new CdrReader_1.CdrReader(writer.data); | ||
expect(reader).toBeDefined(); | ||
expectToBeCloseToArray(Array.from(reader.float32Array().values()), [5.5, 6.5], 6); | ||
@@ -123,0 +124,0 @@ expectToBeCloseToArray(Array.from(reader.float32Array().values()), [7.5, 8.5], 6); |
@@ -20,3 +20,3 @@ "use strict"; | ||
this.littleEndian = kind === encapsulationKind_1.EncapsulationKind.CDR_LE || kind === encapsulationKind_1.EncapsulationKind.PL_CDR_LE; | ||
this.hostLittleEndian = !isBigEndian_1.isBigEndian(); | ||
this.hostLittleEndian = !(0, isBigEndian_1.isBigEndian)(); | ||
this.array = new Uint8Array(this.buffer); | ||
@@ -23,0 +23,0 @@ this.view = new DataView(this.buffer); |
@@ -31,3 +31,3 @@ "use strict"; | ||
describe("CdrWriter", () => { | ||
it("serializes an example message with size calculation", () => { | ||
it("serializes an example message with internal preallocation", () => { | ||
// Example tf2_msgs/TFMessage | ||
@@ -39,3 +39,3 @@ const writer = new CdrWriter_1.CdrWriter({ size: 100 }); | ||
}); | ||
it("serializes an example message with preallocation", () => { | ||
it("serializes an example message with external preallocation", () => { | ||
// Example tf2_msgs/TFMessage | ||
@@ -47,3 +47,3 @@ const writer = new CdrWriter_1.CdrWriter({ buffer: new ArrayBuffer(100) }); | ||
}); | ||
it("serializes an example message with size calculation", () => { | ||
it("serializes an example message with no preallocation", () => { | ||
// Example tf2_msgs/TFMessage | ||
@@ -50,0 +50,0 @@ const writer = new CdrWriter_1.CdrWriter(); |
{ | ||
"name": "@foxglove/cdr", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Common Data Representation serialization and deserialization library", | ||
@@ -32,3 +32,4 @@ "license": "MIT", | ||
"scripts": { | ||
"build": "tsc -b", | ||
"build": "yarn clean && tsc -b", | ||
"clean": "rimraf dist *.tsbuildinfo", | ||
"lint:ci": "eslint --report-unused-disable-directives .", | ||
@@ -44,21 +45,19 @@ "lint": "eslint --report-unused-disable-directives --fix .", | ||
"devDependencies": { | ||
"@foxglove/eslint-plugin": "0.13.0", | ||
"@types/eslint": "^7", | ||
"@types/eslint-plugin-prettier": "^3", | ||
"@types/jest": "26.0.24", | ||
"@types/prettier": "2.3.2", | ||
"@typescript-eslint/eslint-plugin": "4.28.4", | ||
"@typescript-eslint/parser": "4.28.4", | ||
"esbuild": "0.12.15", | ||
"esbuild-jest": "0.5.0", | ||
"eslint": "7.31.0", | ||
"@foxglove/eslint-plugin": "0.17.0", | ||
"@types/jest": "27.0.1", | ||
"@typescript-eslint/eslint-plugin": "4.31.1", | ||
"@typescript-eslint/parser": "4.31.1", | ||
"eslint": "7.32.0", | ||
"eslint-config-prettier": "8.3.0", | ||
"eslint-plugin-filenames": "^1.3.2", | ||
"eslint-plugin-import": "2.23.4", | ||
"eslint-plugin-jest": "24.3.6", | ||
"eslint-plugin-prettier": "3.4.0", | ||
"jest": "27.0.6", | ||
"prettier": "2.3.2", | ||
"typescript": "4.3.5" | ||
"eslint-plugin-es": "4.1.0", | ||
"eslint-plugin-filenames": "1.3.2", | ||
"eslint-plugin-import": "2.24.2", | ||
"eslint-plugin-jest": "24.4.0", | ||
"eslint-plugin-prettier": "4.0.0", | ||
"jest": "27.2.0", | ||
"prettier": "2.4.0", | ||
"rimraf": "3.0.2", | ||
"ts-jest": "27.0.5", | ||
"typescript": "4.4.3" | ||
} | ||
} |
@@ -5,2 +5,4 @@ # @foxglove/cdr | ||
[](https://www.npmjs.com/package/@foxglove/cdr) | ||
## Introduction | ||
@@ -7,0 +9,0 @@ |
@@ -117,3 +117,3 @@ import { CdrReader } from "./CdrReader"; | ||
["uint32Array", "uint32", [0, 4294967295, 3]], | ||
])("reads %s", (getter: string, setter: string, expected: number[]) => { | ||
])("reads int %s", (getter: string, setter: string, expected: number[]) => { | ||
const writer = new CdrWriter(); | ||
@@ -131,3 +131,3 @@ writeArray(writer, setter as Setter, expected); | ||
["float64Array", "float64", [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -0.123456789121212121212], 15], | ||
])("reads %s", (getter: string, setter: string, expected: number[], numDigits: number) => { | ||
])("reads float %s", (getter: string, setter: string, expected: number[], numDigits: number) => { | ||
const writer = new CdrWriter(); | ||
@@ -160,2 +160,3 @@ writeArray(writer, setter as Setter, expected); | ||
const reader = new CdrReader(writer.data); | ||
expect(reader).toBeDefined(); | ||
expectToBeCloseToArray(Array.from(reader.float32Array().values()), [5.5, 6.5], 6); | ||
@@ -162,0 +163,0 @@ expectToBeCloseToArray(Array.from(reader.float32Array().values()), [7.5, 8.5], 6); |
@@ -27,3 +27,2 @@ import { EncapsulationKind } from "./encapsulationKind"; | ||
export class CdrReader { | ||
private array: Uint8Array; | ||
private view: DataView; | ||
@@ -37,9 +36,5 @@ private littleEndian: boolean; | ||
get kind(): EncapsulationKind { | ||
return this.array[1] as EncapsulationKind; | ||
return this.view.getUint8(1) as EncapsulationKind; | ||
} | ||
get data(): Uint8Array { | ||
return this.array; | ||
} | ||
get decodedBytes(): number { | ||
@@ -49,3 +44,3 @@ return this.offset; | ||
constructor(data: Uint8Array) { | ||
constructor(data: ArrayBufferView) { | ||
this.hostLittleEndian = !isBigEndian(); | ||
@@ -58,3 +53,2 @@ | ||
} | ||
this.array = data; | ||
this.view = new DataView(data.buffer, data.byteOffset, data.byteLength); | ||
@@ -161,3 +155,3 @@ const kind = this.kind; | ||
} | ||
const data = new Uint8Array(this.array.buffer, this.array.byteOffset + this.offset, length - 1); | ||
const data = new Uint8Array(this.view.buffer, this.view.byteOffset + this.offset, length - 1); | ||
const value = this.textDecoder.decode(data); | ||
@@ -173,3 +167,3 @@ this.offset += length; | ||
int8Array(count: number = this.sequenceLength()): Int8Array { | ||
const array = new Int8Array(this.data.buffer, this.data.byteOffset + this.offset, count); | ||
const array = new Int8Array(this.view.buffer, this.view.byteOffset + this.offset, count); | ||
this.offset += count; | ||
@@ -180,3 +174,3 @@ return array; | ||
uint8Array(count: number = this.sequenceLength()): Uint8Array { | ||
const array = new Uint8Array(this.data.buffer, this.data.byteOffset + this.offset, count); | ||
const array = new Uint8Array(this.view.buffer, this.view.byteOffset + this.offset, count); | ||
this.offset += count; | ||
@@ -233,3 +227,3 @@ return array; | ||
const newOffset = this.offset + relativeOffset; | ||
if (newOffset < 4 || newOffset >= this.data.byteLength) { | ||
if (newOffset < 4 || newOffset >= this.view.byteLength) { | ||
throw new Error(`seek(${relativeOffset}) failed, ${newOffset} is outside the data range`); | ||
@@ -246,3 +240,3 @@ } | ||
seekTo(offset: number): void { | ||
if (offset < 4 || offset >= this.data.byteLength) { | ||
if (offset < 4 || offset >= this.view.byteLength) { | ||
throw new Error(`seekTo(${offset}) failed, value is outside the data range`); | ||
@@ -270,3 +264,3 @@ } | ||
this.align(TypedArrayConstructor.BYTES_PER_ELEMENT); | ||
const totalOffset = this.data.byteOffset + this.offset; | ||
const totalOffset = this.view.byteOffset + this.offset; | ||
if (this.littleEndian !== this.hostLittleEndian) { | ||
@@ -277,3 +271,3 @@ // Slowest path | ||
// Fastest path | ||
const array = new TypedArrayConstructor(this.data.buffer, totalOffset, count); | ||
const array = new TypedArrayConstructor(this.view.buffer, totalOffset, count); | ||
this.offset += TypedArrayConstructor.BYTES_PER_ELEMENT * count; | ||
@@ -280,0 +274,0 @@ return array; |
@@ -34,3 +34,3 @@ import { CdrReader } from "./CdrReader"; | ||
describe("CdrWriter", () => { | ||
it("serializes an example message with size calculation", () => { | ||
it("serializes an example message with internal preallocation", () => { | ||
// Example tf2_msgs/TFMessage | ||
@@ -43,3 +43,3 @@ const writer = new CdrWriter({ size: 100 }); | ||
it("serializes an example message with preallocation", () => { | ||
it("serializes an example message with external preallocation", () => { | ||
// Example tf2_msgs/TFMessage | ||
@@ -52,3 +52,3 @@ const writer = new CdrWriter({ buffer: new ArrayBuffer(100) }); | ||
it("serializes an example message with size calculation", () => { | ||
it("serializes an example message with no preallocation", () => { | ||
// Example tf2_msgs/TFMessage | ||
@@ -55,0 +55,0 @@ const writer = new CdrWriter(); |
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
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
16
79
0
135520
2214