@foxglove/cdr
Advanced tools
Comparing version 0.2.1 to 1.0.0
@@ -0,1 +1,2 @@ | ||
import { EncapsulationKind } from "./encapsulationKind"; | ||
export declare class CdrReader { | ||
@@ -8,2 +9,3 @@ private array; | ||
private textDecoder; | ||
get kind(): EncapsulationKind; | ||
get data(): Uint8Array; | ||
@@ -20,2 +22,5 @@ get decodedBytes(): number; | ||
uint64(): bigint; | ||
uint16BE(): number; | ||
uint32BE(): number; | ||
uint64BE(): bigint; | ||
float32(): number; | ||
@@ -22,0 +27,0 @@ float64(): number; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CdrReader = void 0; | ||
const encapsulationKind_1 = require("./encapsulationKind"); | ||
const isBigEndian_1 = require("./isBigEndian"); | ||
@@ -14,5 +15,9 @@ class CdrReader { | ||
this.view = new DataView(data.buffer, data.byteOffset, data.byteLength); | ||
this.littleEndian = data[1] !== 0; | ||
const kind = this.kind; | ||
this.littleEndian = kind === encapsulationKind_1.EncapsulationKind.CDR_LE || kind === encapsulationKind_1.EncapsulationKind.PL_CDR_LE; | ||
this.offset = 4; | ||
} | ||
get kind() { | ||
return this.array[1]; | ||
} | ||
get data() { | ||
@@ -70,2 +75,20 @@ return this.array; | ||
} | ||
uint16BE() { | ||
this.align(2); | ||
const value = this.view.getUint16(this.offset, false); | ||
this.offset += 2; | ||
return value; | ||
} | ||
uint32BE() { | ||
this.align(4); | ||
const value = this.view.getUint32(this.offset, false); | ||
this.offset += 4; | ||
return value; | ||
} | ||
uint64BE() { | ||
this.align(8); | ||
const value = this.view.getBigUint64(this.offset, false); | ||
this.offset += 8; | ||
return value; | ||
} | ||
float32() { | ||
@@ -97,4 +120,3 @@ this.align(4); | ||
} | ||
int8Array(count) { | ||
count ?? (count = this.sequenceLength()); | ||
int8Array(count = this.sequenceLength()) { | ||
const array = new Int8Array(this.data.buffer, this.data.byteOffset + this.offset, count); | ||
@@ -104,4 +126,3 @@ this.offset += count; | ||
} | ||
uint8Array(count) { | ||
count ?? (count = this.sequenceLength()); | ||
uint8Array(count = this.sequenceLength()) { | ||
const array = new Uint8Array(this.data.buffer, this.data.byteOffset + this.offset, count); | ||
@@ -111,36 +132,27 @@ this.offset += count; | ||
} | ||
int16Array(count) { | ||
count ?? (count = this.sequenceLength()); | ||
int16Array(count = this.sequenceLength()) { | ||
return this.typedArray(Int16Array, "getInt16", count); | ||
} | ||
uint16Array(count) { | ||
count ?? (count = this.sequenceLength()); | ||
uint16Array(count = this.sequenceLength()) { | ||
return this.typedArray(Uint16Array, "getUint16", count); | ||
} | ||
int32Array(count) { | ||
count ?? (count = this.sequenceLength()); | ||
int32Array(count = this.sequenceLength()) { | ||
return this.typedArray(Int32Array, "getInt32", count); | ||
} | ||
uint32Array(count) { | ||
count ?? (count = this.sequenceLength()); | ||
uint32Array(count = this.sequenceLength()) { | ||
return this.typedArray(Uint32Array, "getUint32", count); | ||
} | ||
int64Array(count) { | ||
count ?? (count = this.sequenceLength()); | ||
int64Array(count = this.sequenceLength()) { | ||
return this.typedArray(BigInt64Array, "getBigInt64", count); | ||
} | ||
uint64Array(count) { | ||
count ?? (count = this.sequenceLength()); | ||
uint64Array(count = this.sequenceLength()) { | ||
return this.typedArray(BigUint64Array, "getBigUint64", count); | ||
} | ||
float32Array(count) { | ||
count ?? (count = this.sequenceLength()); | ||
float32Array(count = this.sequenceLength()) { | ||
return this.typedArray(Float32Array, "getFloat32", count); | ||
} | ||
float64Array(count) { | ||
count ?? (count = this.sequenceLength()); | ||
float64Array(count = this.sequenceLength()) { | ||
return this.typedArray(Float64Array, "getFloat64", count); | ||
} | ||
stringArray(count) { | ||
count ?? (count = this.sequenceLength()); | ||
stringArray(count = this.sequenceLength()) { | ||
const output = []; | ||
@@ -147,0 +159,0 @@ for (let i = 0; i < count; i++) { |
@@ -30,2 +30,9 @@ "use strict"; | ||
}); | ||
it("reads big endian values", () => { | ||
const data = Uint8Array.from(Buffer.from("000100001234000056789abcdef0000000000000", "hex")); | ||
const reader = new CdrReader_1.CdrReader(data); | ||
expect(reader.uint16BE()).toEqual(0x1234); | ||
expect(reader.uint32BE()).toEqual(0x56789abc); | ||
expect(reader.uint64BE()).toEqual(0xdef0000000000000n); | ||
}); | ||
it("seeks to absolute and relative positions", () => { | ||
@@ -32,0 +39,0 @@ const data = Uint8Array.from(Buffer.from(tf2_msg__TFMessage, "hex")); |
@@ -0,5 +1,6 @@ | ||
import { EncapsulationKind } from "./encapsulationKind"; | ||
export declare type CdrWriterOpts = { | ||
buffer?: ArrayBuffer; | ||
size?: number; | ||
bigEndian?: boolean; | ||
kind?: EncapsulationKind; | ||
}; | ||
@@ -27,2 +28,5 @@ export declare class CdrWriter { | ||
uint64(value: bigint): CdrWriter; | ||
uint16BE(value: number): CdrWriter; | ||
uint32BE(value: number): CdrWriter; | ||
uint64BE(value: bigint): CdrWriter; | ||
float32(value: number): CdrWriter; | ||
@@ -29,0 +33,0 @@ float64(value: number): CdrWriter; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.CdrWriter = void 0; | ||
const encapsulationKind_1 = require("./encapsulationKind"); | ||
const isBigEndian_1 = require("./isBigEndian"); | ||
@@ -17,3 +18,4 @@ class CdrWriter { | ||
} | ||
this.littleEndian = !(options.bigEndian === true); | ||
const kind = options.kind ?? encapsulationKind_1.EncapsulationKind.CDR_LE; | ||
this.littleEndian = kind === encapsulationKind_1.EncapsulationKind.CDR_LE || kind === encapsulationKind_1.EncapsulationKind.PL_CDR_LE; | ||
this.hostLittleEndian = !isBigEndian_1.isBigEndian(); | ||
@@ -24,5 +26,4 @@ this.array = new Uint8Array(this.buffer); | ||
this.resizeIfNeeded(4); | ||
// {0x00, 0x00} -- PLAIN_CDR, BIG_ENDIAN, | ||
// {0x00, 0x01} -- PLAIN_CDR, LITTLE_ENDIAN | ||
this.view.setUint16(0, this.littleEndian ? 1 : 0, false); | ||
this.view.setUint8(0, 0); // Upper bits of EncapsulationKind, unused | ||
this.view.setUint8(1, kind); | ||
// The RTPS specification does not define any settings for the 2 byte | ||
@@ -88,2 +89,20 @@ // options field and further states that a receiver should not interpret it | ||
} | ||
uint16BE(value) { | ||
this.align(2); | ||
this.view.setUint16(this.offset, value, false); | ||
this.offset += 2; | ||
return this; | ||
} | ||
uint32BE(value) { | ||
this.align(4); | ||
this.view.setUint32(this.offset, value, false); | ||
this.offset += 4; | ||
return this; | ||
} | ||
uint64BE(value) { | ||
this.align(8); | ||
this.view.setBigUint64(this.offset, value, false); | ||
this.offset += 8; | ||
return this; | ||
} | ||
float32(value) { | ||
@@ -277,4 +296,3 @@ this.align(4); | ||
// resize if needed, and write padding bytes for alignment | ||
align(size, bytesToWrite) { | ||
bytesToWrite ?? (bytesToWrite = size); | ||
align(size, bytesToWrite = size) { | ||
// The four byte header is not considered for alignment | ||
@@ -281,0 +299,0 @@ const alignment = (this.offset - 4) % size; |
@@ -58,2 +58,5 @@ "use strict"; | ||
writer.uint64(8000000003n); | ||
writer.uint16BE(0x1234); | ||
writer.uint32BE(0x12345678); | ||
writer.uint64BE(0x123456789abcdef0n); | ||
writer.float32(-9.14); | ||
@@ -64,3 +67,3 @@ writer.float64(1.7976931348623158e100); | ||
const data = writer.data; | ||
expect(data.byteLength).toEqual(64); | ||
expect(data.byteLength).toEqual(80); | ||
const reader = new CdrReader_1.CdrReader(data); | ||
@@ -75,2 +78,5 @@ expect(reader.int8()).toEqual(-1); | ||
expect(reader.uint64()).toEqual(8000000003n); | ||
expect(reader.uint16BE()).toEqual(0x1234); | ||
expect(reader.uint32BE()).toEqual(0x12345678); | ||
expect(reader.uint64BE()).toEqual(0x123456789abcdef0n); | ||
expect(reader.float32()).toBeCloseTo(-9.14); | ||
@@ -77,0 +83,0 @@ expect(reader.float64()).toBeCloseTo(1.7976931348623158e100); |
export * from "./CdrReader"; | ||
export * from "./CdrSizeCalculator"; | ||
export * from "./CdrWriter"; | ||
export * from "./encapsulationKind"; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -16,2 +16,3 @@ "use strict"; | ||
__exportStar(require("./CdrWriter"), exports); | ||
__exportStar(require("./encapsulationKind"), exports); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@foxglove/cdr", | ||
"version": "0.2.1", | ||
"version": "1.0.0", | ||
"description": "Common Data Representation serialization and deserialization library", | ||
@@ -43,20 +43,21 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@foxglove/eslint-plugin": "0.10.0", | ||
"@foxglove/eslint-plugin": "0.13.0", | ||
"@types/eslint": "^7", | ||
"@types/eslint-plugin-prettier": "^3", | ||
"@types/jest": "26.0.23", | ||
"@types/prettier": "2.3.0", | ||
"@typescript-eslint/eslint-plugin": "4.28.0", | ||
"@typescript-eslint/parser": "4.28.0", | ||
"esbuild": "0.12.9", | ||
"@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.29.0", | ||
"eslint": "7.31.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.5", | ||
"prettier": "2.3.1", | ||
"typescript": "4.3.4" | ||
"jest": "27.0.6", | ||
"prettier": "2.3.2", | ||
"typescript": "4.3.5" | ||
} | ||
} |
@@ -44,2 +44,10 @@ import { CdrReader } from "./CdrReader"; | ||
it("reads big endian values", () => { | ||
const data = Uint8Array.from(Buffer.from("000100001234000056789abcdef0000000000000", "hex")); | ||
const reader = new CdrReader(data); | ||
expect(reader.uint16BE()).toEqual(0x1234); | ||
expect(reader.uint32BE()).toEqual(0x56789abc); | ||
expect(reader.uint64BE()).toEqual(0xdef0000000000000n); | ||
}); | ||
it("seeks to absolute and relative positions", () => { | ||
@@ -46,0 +54,0 @@ const data = Uint8Array.from(Buffer.from(tf2_msg__TFMessage, "hex")); |
@@ -0,1 +1,2 @@ | ||
import { EncapsulationKind } from "./encapsulationKind"; | ||
import { isBigEndian } from "./isBigEndian"; | ||
@@ -33,2 +34,6 @@ | ||
get kind(): EncapsulationKind { | ||
return this.array[1] as EncapsulationKind; | ||
} | ||
get data(): Uint8Array { | ||
@@ -52,3 +57,4 @@ return this.array; | ||
this.view = new DataView(data.buffer, data.byteOffset, data.byteLength); | ||
this.littleEndian = data[1] !== 0; | ||
const kind = this.kind; | ||
this.littleEndian = kind === EncapsulationKind.CDR_LE || kind === EncapsulationKind.PL_CDR_LE; | ||
this.offset = 4; | ||
@@ -111,2 +117,23 @@ } | ||
uint16BE(): number { | ||
this.align(2); | ||
const value = this.view.getUint16(this.offset, false); | ||
this.offset += 2; | ||
return value; | ||
} | ||
uint32BE(): number { | ||
this.align(4); | ||
const value = this.view.getUint32(this.offset, false); | ||
this.offset += 4; | ||
return value; | ||
} | ||
uint64BE(): bigint { | ||
this.align(8); | ||
const value = this.view.getBigUint64(this.offset, false); | ||
this.offset += 8; | ||
return value; | ||
} | ||
float32(): number { | ||
@@ -142,4 +169,3 @@ this.align(4); | ||
int8Array(count?: number): Int8Array { | ||
count ??= this.sequenceLength(); | ||
int8Array(count: number = this.sequenceLength()): Int8Array { | ||
const array = new Int8Array(this.data.buffer, this.data.byteOffset + this.offset, count); | ||
@@ -150,4 +176,3 @@ this.offset += count; | ||
uint8Array(count?: number): Uint8Array { | ||
count ??= this.sequenceLength(); | ||
uint8Array(count: number = this.sequenceLength()): Uint8Array { | ||
const array = new Uint8Array(this.data.buffer, this.data.byteOffset + this.offset, count); | ||
@@ -158,44 +183,35 @@ this.offset += count; | ||
int16Array(count?: number): Int16Array { | ||
count ??= this.sequenceLength(); | ||
int16Array(count: number = this.sequenceLength()): Int16Array { | ||
return this.typedArray(Int16Array, "getInt16", count); | ||
} | ||
uint16Array(count?: number): Uint16Array { | ||
count ??= this.sequenceLength(); | ||
uint16Array(count: number = this.sequenceLength()): Uint16Array { | ||
return this.typedArray(Uint16Array, "getUint16", count); | ||
} | ||
int32Array(count?: number): Int32Array { | ||
count ??= this.sequenceLength(); | ||
int32Array(count: number = this.sequenceLength()): Int32Array { | ||
return this.typedArray(Int32Array, "getInt32", count); | ||
} | ||
uint32Array(count?: number): Uint32Array { | ||
count ??= this.sequenceLength(); | ||
uint32Array(count: number = this.sequenceLength()): Uint32Array { | ||
return this.typedArray(Uint32Array, "getUint32", count); | ||
} | ||
int64Array(count?: number): BigInt64Array { | ||
count ??= this.sequenceLength(); | ||
int64Array(count: number = this.sequenceLength()): BigInt64Array { | ||
return this.typedArray(BigInt64Array, "getBigInt64", count); | ||
} | ||
uint64Array(count?: number): BigUint64Array { | ||
count ??= this.sequenceLength(); | ||
uint64Array(count: number = this.sequenceLength()): BigUint64Array { | ||
return this.typedArray(BigUint64Array, "getBigUint64", count); | ||
} | ||
float32Array(count?: number): Float32Array { | ||
count ??= this.sequenceLength(); | ||
float32Array(count: number = this.sequenceLength()): Float32Array { | ||
return this.typedArray(Float32Array, "getFloat32", count); | ||
} | ||
float64Array(count?: number): Float64Array { | ||
count ??= this.sequenceLength(); | ||
float64Array(count: number = this.sequenceLength()): Float64Array { | ||
return this.typedArray(Float64Array, "getFloat64", count); | ||
} | ||
stringArray(count?: number): string[] { | ||
count ??= this.sequenceLength(); | ||
stringArray(count: number = this.sequenceLength()): string[] { | ||
const output: string[] = []; | ||
@@ -202,0 +218,0 @@ for (let i = 0; i < count; i++) { |
@@ -63,2 +63,5 @@ import { CdrReader } from "./CdrReader"; | ||
writer.uint64(8_000_000_003n); | ||
writer.uint16BE(0x1234); | ||
writer.uint32BE(0x12345678); | ||
writer.uint64BE(0x123456789abcdef0n); | ||
writer.float32(-9.14); | ||
@@ -69,3 +72,3 @@ writer.float64(1.7976931348623158e100); | ||
const data = writer.data; | ||
expect(data.byteLength).toEqual(64); | ||
expect(data.byteLength).toEqual(80); | ||
@@ -81,2 +84,5 @@ const reader = new CdrReader(data); | ||
expect(reader.uint64()).toEqual(8_000_000_003n); | ||
expect(reader.uint16BE()).toEqual(0x1234); | ||
expect(reader.uint32BE()).toEqual(0x12345678); | ||
expect(reader.uint64BE()).toEqual(0x123456789abcdef0n); | ||
expect(reader.float32()).toBeCloseTo(-9.14); | ||
@@ -83,0 +89,0 @@ expect(reader.float64()).toBeCloseTo(1.7976931348623158e100); |
@@ -0,1 +1,2 @@ | ||
import { EncapsulationKind } from "./encapsulationKind"; | ||
import { isBigEndian } from "./isBigEndian"; | ||
@@ -6,3 +7,3 @@ | ||
size?: number; | ||
bigEndian?: boolean; | ||
kind?: EncapsulationKind; | ||
}; | ||
@@ -39,3 +40,4 @@ | ||
this.littleEndian = !(options.bigEndian === true); | ||
const kind = options.kind ?? EncapsulationKind.CDR_LE; | ||
this.littleEndian = kind === EncapsulationKind.CDR_LE || kind === EncapsulationKind.PL_CDR_LE; | ||
this.hostLittleEndian = !isBigEndian(); | ||
@@ -47,5 +49,4 @@ this.array = new Uint8Array(this.buffer); | ||
this.resizeIfNeeded(4); | ||
// {0x00, 0x00} -- PLAIN_CDR, BIG_ENDIAN, | ||
// {0x00, 0x01} -- PLAIN_CDR, LITTLE_ENDIAN | ||
this.view.setUint16(0, this.littleEndian ? 1 : 0, false); | ||
this.view.setUint8(0, 0); // Upper bits of EncapsulationKind, unused | ||
this.view.setUint8(1, kind); | ||
// The RTPS specification does not define any settings for the 2 byte | ||
@@ -114,2 +115,23 @@ // options field and further states that a receiver should not interpret it | ||
uint16BE(value: number): CdrWriter { | ||
this.align(2); | ||
this.view.setUint16(this.offset, value, false); | ||
this.offset += 2; | ||
return this; | ||
} | ||
uint32BE(value: number): CdrWriter { | ||
this.align(4); | ||
this.view.setUint32(this.offset, value, false); | ||
this.offset += 4; | ||
return this; | ||
} | ||
uint64BE(value: bigint): CdrWriter { | ||
this.align(8); | ||
this.view.setBigUint64(this.offset, value, false); | ||
this.offset += 8; | ||
return this; | ||
} | ||
float32(value: number): CdrWriter { | ||
@@ -325,4 +347,3 @@ this.align(4); | ||
// resize if needed, and write padding bytes for alignment | ||
private align(size: number, bytesToWrite?: number): void { | ||
bytesToWrite ??= size; | ||
private align(size: number, bytesToWrite: number = size): void { | ||
// The four byte header is not considered for alignment | ||
@@ -329,0 +350,0 @@ const alignment = (this.offset - 4) % size; |
export * from "./CdrReader"; | ||
export * from "./CdrSizeCalculator"; | ||
export * from "./CdrWriter"; | ||
export * from "./encapsulationKind"; |
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
123499
48
2043
0
18